From 3a23e15b1770d186afa6f3290ca02f1bbe1e14d9 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 13:23:01 -0500 Subject: [PATCH 001/258] Git Ignore --- .../.idea/.gitignore | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .idea/.idea.MagicQuant-Pipeline/.idea/.gitignore diff --git a/.idea/.idea.MagicQuant-Pipeline/.idea/.gitignore b/.idea/.idea.MagicQuant-Pipeline/.idea/.gitignore new file mode 100644 index 0000000..b428136 --- /dev/null +++ b/.idea/.idea.MagicQuant-Pipeline/.idea/.gitignore @@ -0,0 +1,38 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/.idea.MagicQuant-Pipeline.iml +/contentModel.xml +/projectSettingsUpdater.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ + +# Build results +bin/ +obj/ + +# Rider / JetBrains +.idea/ +*.sln.iml + +# Visual Studio user settings +*.user +*.userosscache +*.suo +*.cache +*.dbmdl +*.bak +*.ncb +*.opendb +*.VC.db + +# Other common C# stuff +*.log +*.vs/ From 757947f27acfb537023947151cce70234e61ecab Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 13:24:03 -0500 Subject: [PATCH 002/258] Git Ignore added --- .gitignore | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e060eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Build results +bin/ +obj/ + +# Rider / JetBrains +.idea/ +*.sln.iml + +# Visual Studio user settings +*.user +*.userosscache +*.suo +*.cache +*.dbmdl +*.bak +*.ncb +*.opendb +*.VC.db + +# Other common C# stuff +*.log +*.vs/ From 7ed816c416ec590863df23b59cc9b77bb1f01dff Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 13:24:15 -0500 Subject: [PATCH 003/258] Base commands --- MagicQuant-Pipeline.sln | 16 +++++++++ MagicQuant/Commands/BuildHybrids.cs | 11 ++++++ MagicQuant/Commands/Evolution.cs | 11 ++++++ MagicQuant/Helpers/CliHelpers.cs | 54 +++++++++++++++++++++++++++++ MagicQuant/Interfaces/ICommand.cs | 7 ++++ MagicQuant/MagicQuant.csproj | 14 ++++++++ MagicQuant/Models/CliArg.cs | 7 ++++ MagicQuant/Program.cs | 50 ++++++++++++++++++++++++++ 8 files changed, 170 insertions(+) create mode 100644 MagicQuant-Pipeline.sln create mode 100644 MagicQuant/Commands/BuildHybrids.cs create mode 100644 MagicQuant/Commands/Evolution.cs create mode 100644 MagicQuant/Helpers/CliHelpers.cs create mode 100644 MagicQuant/Interfaces/ICommand.cs create mode 100644 MagicQuant/MagicQuant.csproj create mode 100644 MagicQuant/Models/CliArg.cs create mode 100644 MagicQuant/Program.cs diff --git a/MagicQuant-Pipeline.sln b/MagicQuant-Pipeline.sln new file mode 100644 index 0000000..8b2e7ce --- /dev/null +++ b/MagicQuant-Pipeline.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant", "MagicQuant\MagicQuant.csproj", "{9259012B-0EB2-4AD8-81E5-807FD4465AA3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9259012B-0EB2-4AD8-81E5-807FD4465AA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9259012B-0EB2-4AD8-81E5-807FD4465AA3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9259012B-0EB2-4AD8-81E5-807FD4465AA3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9259012B-0EB2-4AD8-81E5-807FD4465AA3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/MagicQuant/Commands/BuildHybrids.cs b/MagicQuant/Commands/BuildHybrids.cs new file mode 100644 index 0000000..a7d89c8 --- /dev/null +++ b/MagicQuant/Commands/BuildHybrids.cs @@ -0,0 +1,11 @@ +using MagicQuant.Models; + +namespace MagicQuant.Commands; + +public class BuildHybrids : ICommand +{ + public async Task Run(List args) + { + + } +} \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs new file mode 100644 index 0000000..c152204 --- /dev/null +++ b/MagicQuant/Commands/Evolution.cs @@ -0,0 +1,11 @@ +using MagicQuant.Models; + +namespace MagicQuant.Commands; + +public class Evolution : ICommand +{ + public async Task Run(List args) + { + + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs new file mode 100644 index 0000000..9dbf14a --- /dev/null +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -0,0 +1,54 @@ +using System.Text.RegularExpressions; +using MagicQuant.Commands; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Helpers; + +public static class CliHelpers +{ + + public static List ParseArguments(string input) + { + var cliArgs = new List(); + + // Regex identifies --key value or --key "value with spaces" + var regex = new Regex(@"--(?[^\s=]+)(?:[\s=]+(?:""(?[^""]*)""|(?[^\s-]*)))?", RegexOptions.IgnoreCase); + var matches = regex.Matches(input); + + foreach (Match match in matches) + { + cliArgs.Add(new CliArg + { + Name = match.Groups["name"].Value, + Value = match.Groups["value"].Value + }); + } + + return cliArgs; + } + + public static void ShowHelp(Dictionary Factory)> commands) + { + AnsiConsole.Write(new Rule("[yellow]MagicQuant CLI[/]") { Justification = Justify.Left, Style = "grey" }); + AnsiConsole.WriteLine(); + + // Create a table for a clean, aligned UI + var table = new Table() + .AddColumn("[blue]Command[/]") + .AddColumn("[white]Description[/]") + .Border(TableBorder.Rounded) + .BorderColor(Color.Grey15); + + foreach (var cmd in commands) + { + table.AddRow($"[green]{cmd.Key}[/]", cmd.Value.Description); + } + + table.AddRow("[green]help[/]", "Show this help information"); + + AnsiConsole.Write(table); + AnsiConsole.MarkupLine("Usage: [bold]mq[/] [blue][[--option value]][/]"); + AnsiConsole.WriteLine(); + } +} \ No newline at end of file diff --git a/MagicQuant/Interfaces/ICommand.cs b/MagicQuant/Interfaces/ICommand.cs new file mode 100644 index 0000000..0bd90a9 --- /dev/null +++ b/MagicQuant/Interfaces/ICommand.cs @@ -0,0 +1,7 @@ +namespace MagicQuant.Commands; +using MagicQuant.Models; + +public interface ICommand +{ + Task Run(List args); +} \ No newline at end of file diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj new file mode 100644 index 0000000..eb1b370 --- /dev/null +++ b/MagicQuant/MagicQuant.csproj @@ -0,0 +1,14 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + diff --git a/MagicQuant/Models/CliArg.cs b/MagicQuant/Models/CliArg.cs new file mode 100644 index 0000000..52df970 --- /dev/null +++ b/MagicQuant/Models/CliArg.cs @@ -0,0 +1,7 @@ +namespace MagicQuant.Models; + +public class CliArg +{ + public string? Name { get; set; } + public string? Value { get; set; } +} \ No newline at end of file diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs new file mode 100644 index 0000000..ead96eb --- /dev/null +++ b/MagicQuant/Program.cs @@ -0,0 +1,50 @@ +using System.Text.RegularExpressions; +using MagicQuant.Commands; +using MagicQuant.Helpers; +using MagicQuant.Models; +using Spectre.Console; + +// 1. Define the Command Registry +var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) +{ + { "evolution", ("Run the full evolutionary quantization search", () => new Evolution()) }, + { "build-hybrids", ("Export specific hybrid models with polished README", () => new BuildHybrids()) } +}; + +// 2. Validate input - Show help if no args or "help" requested +if (args.Length == 0 || args[0].Equals("help", StringComparison.OrdinalIgnoreCase)) +{ + CliHelpers.ShowHelp(commands); + return; +} + +string commandInput = args[0]; + +// 3. Check if command exists +if (!commands.TryGetValue(commandInput, out var commandInfo)) +{ + AnsiConsole.MarkupLine($"[red]Error:[/] The command [yellow]'{commandInput}'[/] does not exist."); + CliHelpers.ShowHelp(commands); + return; +} + +// 4. Parse the remaining arguments using Regex for quote-safety +string remainingArgsString = string.Join(" ", args.Skip(1)); +List parsedArgs = CliHelpers.ParseArguments(remainingArgsString); + +// 5. Execute the command +try +{ + var commandInstance = commandInfo.Factory(); + await commandInstance.Run(parsedArgs); +} +catch (Exception ex) +{ + AnsiConsole.WriteException(ex); +} + + +#region Helpers + + +#endregion \ No newline at end of file From a8ade7b298f7571b4c0f2b6b5920c97b83a917e7 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 13:37:15 -0500 Subject: [PATCH 004/258] validation phase added. --- MagicQuant/Commands/InitializeLlamaCpp.cs | 11 ++++++++ MagicQuant/MagicQuant.csproj | 1 + MagicQuant/Program.cs | 34 +++++++++++++++-------- 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 MagicQuant/Commands/InitializeLlamaCpp.cs diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs new file mode 100644 index 0000000..9596892 --- /dev/null +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -0,0 +1,11 @@ +using MagicQuant.Models; + +namespace MagicQuant.Commands; + +public class InitializeLlamaCpp : ICommand +{ + public async Task Run(List args) + { + + } +} \ No newline at end of file diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index eb1b370..97a01ec 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -8,6 +8,7 @@ + diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index ead96eb..93d4776 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -8,10 +8,11 @@ var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) { { "evolution", ("Run the full evolutionary quantization search", () => new Evolution()) }, - { "build-hybrids", ("Export specific hybrid models with polished README", () => new BuildHybrids()) } + { "build-hybrids", ("Export specific hybrid models with polished README", () => new BuildHybrids()) }, + { "initialize-llama-cpp", ("Initialize or update llama.cpp", () => new InitializeLlamaCpp()) } }; -// 2. Validate input - Show help if no args or "help" requested +// 2. Validate input if (args.Length == 0 || args[0].Equals("help", StringComparison.OrdinalIgnoreCase)) { CliHelpers.ShowHelp(commands); @@ -28,23 +29,34 @@ return; } -// 4. Parse the remaining arguments using Regex for quote-safety +// 4. Parse the arguments for the primary command string remainingArgsString = string.Join(" ", args.Skip(1)); List parsedArgs = CliHelpers.ParseArguments(remainingArgsString); -// 5. Execute the command try { + // 5. Pre-run Validation logic + // If the command is NOT "initialize-llama-cpp", we run initialization first with --validate + if (!commandInput.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) + { + AnsiConsole.MarkupLine("[grey]Checking environment dependencies...[/]"); + + var initializer = new InitializeLlamaCpp(); + var validationArgs = new List { new CliArg { Name = "validate", Value = "" } }; + + // Run the validation + await initializer.Run(validationArgs); + + AnsiConsole.MarkupLine("[green]Environment validated.[/]"); + AnsiConsole.WriteLine(); + } + + // 6. Execute the actual requested command var commandInstance = commandInfo.Factory(); await commandInstance.Run(parsedArgs); } catch (Exception ex) { + // Spectre.Console handles the formatting of the error automatically AnsiConsole.WriteException(ex); -} - - -#region Helpers - - -#endregion \ No newline at end of file +} \ No newline at end of file From 2e9d9d3b13b5d9e0a10f7616b0b87276e8c924a6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 15:06:40 -0500 Subject: [PATCH 005/258] utilities and some set up. --- MagicQuant/Helpers/pip_runner.py | 44 +++++++++++++++++++ MagicQuant/MagicQuant.csproj | 10 +++++ MagicQuant/Program.cs | 72 +++++++++++++++++++++++--------- 3 files changed, 107 insertions(+), 19 deletions(-) create mode 100644 MagicQuant/Helpers/pip_runner.py diff --git a/MagicQuant/Helpers/pip_runner.py b/MagicQuant/Helpers/pip_runner.py new file mode 100644 index 0000000..e4ea566 --- /dev/null +++ b/MagicQuant/Helpers/pip_runner.py @@ -0,0 +1,44 @@ +import os +import sys +import runpy + +# Get the current script directory +script_dir = os.path.dirname(os.path.abspath(__file__)) + +# Construct paths for key directories +lib_dir = os.path.join(script_dir, 'Lib') +site_packages_dir = os.path.join(lib_dir, 'site-packages') + +# Add the necessary paths to sys.path +sys.path.insert(0, lib_dir) +sys.path.insert(0, site_packages_dir) + +# Check if pip is available +try: + import pip +except ImportError: + print("pip is not available in the current environment.", file=sys.stderr) + sys.exit(1) + +def run_pip_command(command): + """ + Run pip commands dynamically using pip directly as a module. + """ + # Prepare arguments for pip by splitting the command string + sys.argv = ['pip'] + command.split() + + # Run pip using runpy to run pip as a module + try: + runpy.run_module('pip', run_name="__main__") + except Exception as e: + print(f"Failed to run pip command: {e}", file=sys.stderr) + +# Main execution logic +if __name__ == "__main__": + # If there are command-line arguments, use them + if len(sys.argv) > 1: + # Use arguments passed to the script (excluding the script name) + run_pip_command(' '.join(sys.argv[1:])) + else: + # Default to checking pip version if no arguments are passed + run_pip_command('--version') diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 97a01ec..e357ca3 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -12,4 +12,14 @@ + + + PreserveNewest + + + + + + + diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 93d4776..92d11e2 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -1,10 +1,25 @@ -using System.Text.RegularExpressions; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; using MagicQuant.Commands; using MagicQuant.Helpers; using MagicQuant.Models; using Spectre.Console; -// 1. Define the Command Registry +// 1. OS & Permission Check +if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) +{ + // On Linux, the effective user ID for root is 0 + // We check if we are running as root to ensure file/env access + if (GetLinuxUserId() != 0) + { + AnsiConsole.Write(new Rule("[red]Permission Denied[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine("[red]Error:[/] MagicQuant must be run with [bold]sudo[/] on Linux to manage environments and files."); + AnsiConsole.MarkupLine("[grey]Please try:[/] [yellow]sudo dotnet MagicQuant.dll[/] (or your binary name)"); + return; + } +} + +// 2. Define the Command Registry var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) { { "evolution", ("Run the full evolutionary quantization search", () => new Evolution()) }, @@ -12,7 +27,7 @@ { "initialize-llama-cpp", ("Initialize or update llama.cpp", () => new InitializeLlamaCpp()) } }; -// 2. Validate input +// 3. Validate input if (args.Length == 0 || args[0].Equals("help", StringComparison.OrdinalIgnoreCase)) { CliHelpers.ShowHelp(commands); @@ -21,7 +36,7 @@ string commandInput = args[0]; -// 3. Check if command exists +// 4. Check if command exists if (!commands.TryGetValue(commandInput, out var commandInfo)) { AnsiConsole.MarkupLine($"[red]Error:[/] The command [yellow]'{commandInput}'[/] does not exist."); @@ -29,34 +44,53 @@ return; } -// 4. Parse the arguments for the primary command +// 5. Parse Arguments string remainingArgsString = string.Join(" ", args.Skip(1)); List parsedArgs = CliHelpers.ParseArguments(remainingArgsString); try { - // 5. Pre-run Validation logic - // If the command is NOT "initialize-llama-cpp", we run initialization first with --validate + // 6. Mandatory Validation for non-init commands if (!commandInput.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) { - AnsiConsole.MarkupLine("[grey]Checking environment dependencies...[/]"); - - var initializer = new InitializeLlamaCpp(); - var validationArgs = new List { new CliArg { Name = "validate", Value = "" } }; - - // Run the validation - await initializer.Run(validationArgs); - - AnsiConsole.MarkupLine("[green]Environment validated.[/]"); + await AnsiConsole.Status() + .StartAsync("[grey]Checking environment dependencies...[/]", async ctx => + { + var initializer = new InitializeLlamaCpp(); + var validationArgs = new List { new CliArg { Name = "validate", Value = "" } }; + await initializer.Run(validationArgs); + }); + + AnsiConsole.MarkupLine("[bold green]✓[/] Environment validated."); AnsiConsole.WriteLine(); } - // 6. Execute the actual requested command + // 7. Execute Command var commandInstance = commandInfo.Factory(); await commandInstance.Run(parsedArgs); } catch (Exception ex) { - // Spectre.Console handles the formatting of the error automatically AnsiConsole.WriteException(ex); -} \ No newline at end of file +} + +#region Linux Helpers + +static uint GetLinuxUserId() +{ + // Standard Unix call to get effective user ID + [DllImport("libc")] + static extern uint geteuid(); + + try + { + return geteuid(); + } + catch + { + // Fallback for environments where libc isn't standard + return 1; // Assume non-root + } +} + +#endregion \ No newline at end of file From ff4f1c172ec26ee1b87d39730dca9d7ed3a73047 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 16:13:20 -0500 Subject: [PATCH 006/258] Initialize Llama.cpp process. I believe it's mostly working for Linux, no tests on Windows yet. --- MagicQuant/Cache.cs | 11 + MagicQuant/Commands/InitializeLlamaCpp.cs | 270 ++++++++++++++++++++++ MagicQuant/Helpers/DependencyManager.cs | 159 +++++++++++++ MagicQuant/Helpers/HardwareHelper.cs | 96 ++++++++ MagicQuant/Helpers/LinuxHelper.cs | 29 +++ MagicQuant/Helpers/LlamaBuilder.cs | 153 ++++++++++++ MagicQuant/Helpers/PythonManager.cs | 216 +++++++++++++++++ MagicQuant/MagicQuant.csproj | 5 +- MagicQuant/Models/SystemInfo.cs | 31 +++ MagicQuant/Program.cs | 21 +- 10 files changed, 976 insertions(+), 15 deletions(-) create mode 100644 MagicQuant/Cache.cs create mode 100644 MagicQuant/Helpers/DependencyManager.cs create mode 100644 MagicQuant/Helpers/HardwareHelper.cs create mode 100644 MagicQuant/Helpers/LinuxHelper.cs create mode 100644 MagicQuant/Helpers/LlamaBuilder.cs create mode 100644 MagicQuant/Helpers/PythonManager.cs create mode 100644 MagicQuant/Models/SystemInfo.cs diff --git a/MagicQuant/Cache.cs b/MagicQuant/Cache.cs new file mode 100644 index 0000000..74dc8ce --- /dev/null +++ b/MagicQuant/Cache.cs @@ -0,0 +1,11 @@ +using MagicQuant.Models; + +namespace MagicQuant; + +public class Cache +{ + public static string? LlamaRoot; + public static string? LlamaBin; + public static string? ConvertScript; + public static SystemInfo? SysInfo; +} \ No newline at end of file diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 9596892..11bf42b 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -1,4 +1,8 @@ using MagicQuant.Models; +using MagicQuant.Helpers; +using Spectre.Console; +using System.Runtime.InteropServices; +using System.Diagnostics; namespace MagicQuant.Commands; @@ -6,6 +10,272 @@ public class InitializeLlamaCpp : ICommand { public async Task Run(List args) { + // --------------------------------------------------------- + // 1. Argument Parsing & Path Validation + // --------------------------------------------------------- + bool validate = args.Any(a => a.Name?.ToLower() == "validate" || a.Name?.ToLower() == "verify"); + bool update = args.Any(a => a.Name?.ToLower() == "update"); + string? convertScript = args.FirstOrDefault(a => a.Name?.ToLower() == "convert-script")?.Value; + string? llamaBin = args.FirstOrDefault(a => a.Name?.ToLower() == "llama-bin")?.Value; + string? llamaRoot = args.FirstOrDefault(a => a.Name?.ToLower() == "llama-root")?.Value; + + // Custom Path Validation + if (!string.IsNullOrEmpty(llamaRoot)) + { + if (string.IsNullOrEmpty(convertScript) || string.IsNullOrEmpty(llamaBin)) + { + AnsiConsole.MarkupLine("[red]Error: If you provide custom paths, you must provide --llama-root, --llama-bin, AND --convert-script[/]"); + return; + } + + // Normalize and Check + llamaRoot = Path.GetFullPath(llamaRoot); + llamaBin = Path.GetFullPath(llamaBin); + convertScript = Path.GetFullPath(convertScript); + + if (!Directory.Exists(llamaRoot) || !Directory.Exists(llamaBin) || !File.Exists(convertScript)) + { + AnsiConsole.MarkupLine("[red]Error: One or more provided custom paths do not exist.[/]"); + return; + } + + AnsiConsole.MarkupLine("[green]✔ Custom Environment Validated.[/]"); + return; + } + else if (!string.IsNullOrEmpty(convertScript) || !string.IsNullOrEmpty(llamaBin)) + { + AnsiConsole.MarkupLine("[red]Error: Partial paths provided. Provide ALL custom paths or NONE to use defaults.[/]"); + return; + } + + // --------------------------------------------------------- + // 2. Setup Default Paths + // --------------------------------------------------------- + string userHome = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + string magicQuantPath = Path.Combine(userHome, MagicConstants.MagicQuantFolder); + if (!Directory.Exists(magicQuantPath)) Directory.CreateDirectory(magicQuantPath); + + // --------------------------------------------------------- + // 3. Hardware Detection + // --------------------------------------------------------- + var sysInfo = HardwareHelper.GetSystemInfo(); + AnsiConsole.Write(new Rule("[yellow]System Detection[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"Detected GPU: [green]{sysInfo.GpuVendor}[/] ([blue]{sysInfo.GpuName}[/] - {sysInfo.VramGb:F1} GB)"); + AnsiConsole.MarkupLine($"Detected RAM: [blue]{sysInfo.RamGb:F1} GB[/]"); + + // --------------------------------------------------------- + // 4. Linux System Deps (Sudo Handling) + // --------------------------------------------------------- + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + var requiredPackages = new List + { + "build-essential", "cmake", "ninja-build", "git", + "python3", "python3-venv", "python3-pip", "libcurl4-openssl-dev" + }; + + if (sysInfo.GpuVendor == GpuVendor.Nvidia) requiredPackages.Add("nvidia-cuda-toolkit"); + + // Check if updates are needed + if (update || !AreLinuxPackagesInstalled(requiredPackages)) + { + AnsiConsole.MarkupLine("[yellow]System dependencies are missing or update requested.[/]"); + AnsiConsole.MarkupLine("[grey]Sudo permissions are required to install system packages via apt.[/]"); + + // A. Ask for Sudo permission upfront + try + { + await RefreshSudoCredentialsAsync(); + } + catch + { + AnsiConsole.MarkupLine("[red]Error: Sudo access denied or cancelled. Cannot install system dependencies.[/]"); + return; + } + + // B. Run Install WITH sudo + AnsiConsole.MarkupLine("[cyan]Installing/Updating System Dependencies (sudo apt)...[/]"); + string aptArgs = "install -y " + string.Join(" ", requiredPackages); + + // We run 'sudo' directly here + await RunSimpleProcess("sudo", "apt " + aptArgs); + } + else + { + AnsiConsole.MarkupLine("[green]✔ System dependencies already installed.[/]"); + } + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + AnsiConsole.MarkupLine("[red]Error: MacOS support coming soon.[/]"); + return; + } + + // --------------------------------------------------------- + // 5. Python Environment Setup (Runs as Normal User) + // --------------------------------------------------------- + var pyManager = new PythonManager(magicQuantPath); + await pyManager.SetupEnvironmentAsync(); + + // --------------------------------------------------------- + // 6. Build Llama.cpp (Runs as Normal User) + // --------------------------------------------------------- + var builder = new LlamaBuilder(magicQuantPath, sysInfo); + await builder.PrepareAndBuildAsync(update); + + // --------------------------------------------------------- + // 7. Install Python Libraries (Runs as Normal User) + // --------------------------------------------------------- + AnsiConsole.Write(new Rule("[yellow]Installing Python Libraries[/]") { Justification = Justify.Left }); + + // Helper to decide if we need to install + async Task EnsurePackage(string name, string installCmd, Dictionary? env = null) + { + if (!update) + { + string? version = await pyManager.GetInstalledVersionAsync(name); + if (version != null) + { + AnsiConsole.MarkupLine($"[green]✔ {name} is already installed (v{version}).[/]"); + return; + } + } + + AnsiConsole.MarkupLine($"[cyan]Installing {name}...[/]"); + await pyManager.RunPipInstallAsync(installCmd, env); + } + + // A. Purge Cache (Only on update) + if (update) + { + AnsiConsole.MarkupLine("[grey]Purging pip cache...[/]"); + await pyManager.RunPipInstallAsync("cache purge"); + } + + // B. Install PyTorch (Hardware Specific & Dynamic) + string torchCmd = "torch torchvision torchaudio"; + bool isNvidia = sysInfo.GpuVendor == GpuVendor.Nvidia; + + if (isNvidia) + { + double cudaVer = HardwareHelper.GetCudaVersion(); + AnsiConsole.MarkupLine($"[grey]Detected CUDA Version: {cudaVer}[/]"); + + if (cudaVer >= 12.0) + { + torchCmd += " --index-url https://download.pytorch.org/whl/cu124"; + AnsiConsole.MarkupLine($"[cyan]Targeting PyTorch for CUDA 12.x...[/]"); + } + else if (cudaVer >= 11.0) + { + torchCmd += " --index-url https://download.pytorch.org/whl/cu118"; + AnsiConsole.MarkupLine($"[cyan]Targeting PyTorch for CUDA 11.x...[/]"); + } + else + { + AnsiConsole.MarkupLine("[yellow]Warning: Could not detect CUDA version or version is < 11. Installing default PyTorch.[/]"); + } + } + else + { + AnsiConsole.MarkupLine($"[cyan]Installing Standard PyTorch (CPU/AMD/Intel)...[/]"); + } + + await EnsurePackage("torch", torchCmd); + + // C. Install Core Utilities + string coreDeps = "gguf tokenizers transformers mistral-common sentencepiece"; + + if (!update && await pyManager.GetInstalledVersionAsync("transformers") != null) + { + AnsiConsole.MarkupLine("[green]✔ Core utilities (transformers, etc.) are installed.[/]"); + } + else + { + AnsiConsole.MarkupLine("[cyan]Installing Core Utilities...[/]"); + await pyManager.RunPipInstallAsync($"--upgrade --no-cache-dir {coreDeps}"); + } + + // D. Install llama-cpp-python + var llamaEnv = new Dictionary(); + if (isNvidia) + { + llamaEnv["CMAKE_ARGS"] = "-DGGML_CUDA=on"; + llamaEnv["FORCE_CMAKE"] = "1"; + } + else if (sysInfo.GpuVendor == GpuVendor.Amd) + { + llamaEnv["CMAKE_ARGS"] = "-DGGML_HIPBLAS=on"; + llamaEnv["FORCE_CMAKE"] = "1"; + } + + await EnsurePackage("llama-cpp-python", + "--upgrade --force-reinstall --no-cache-dir llama-cpp-python", + llamaEnv); + + AnsiConsole.MarkupLine("[bold green]Initialization Complete![/]"); + AnsiConsole.MarkupLine($"Llama Binaries: [grey]{builder.GetLlamaBinPath()}[/]"); + } + + // --- Helpers --- + + private static async Task RunSimpleProcess(string exe, string args) + { + var startInfo = new ProcessStartInfo(exe, args) { UseShellExecute = false, CreateNoWindow = true }; + var p = Process.Start(startInfo); + await p!.WaitForExitAsync(); + } + + private async Task RefreshSudoCredentialsAsync() + { + // "sudo -v" updates the user's cached credentials. + // It will prompt for a password if necessary in the standard input. + var psi = new ProcessStartInfo + { + FileName = "sudo", + Arguments = "-v", + UseShellExecute = false // Required to handle password prompt + }; + + var p = Process.Start(psi); + await p!.WaitForExitAsync(); + + if (p.ExitCode != 0) + { + throw new Exception("Sudo access denied."); + } + } + + private bool AreLinuxPackagesInstalled(List packages) + { + // dpkg-query check + foreach (var pkg in packages) + { + try + { + var psi = new ProcessStartInfo + { + FileName = "dpkg-query", + Arguments = $"-W -f='${{Status}}' {pkg}", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + }; + using var p = Process.Start(psi); + string output = p?.StandardOutput.ReadToEnd() ?? ""; + p?.WaitForExit(); + + if (!output.Contains("install ok installed")) + { + return false; // Found a missing package + } + } + catch + { + return false; // Command failed, assume missing + } + } + return true; } } \ No newline at end of file diff --git a/MagicQuant/Helpers/DependencyManager.cs b/MagicQuant/Helpers/DependencyManager.cs new file mode 100644 index 0000000..2ebb9f1 --- /dev/null +++ b/MagicQuant/Helpers/DependencyManager.cs @@ -0,0 +1,159 @@ +using System.Diagnostics; +using System.IO.Compression; +using System.Runtime.InteropServices; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Helpers; + +public static class DependencyManager +{ + // CMake Constants + private const string CmakeVersion = "3.29.0"; + private const string CmakeWinUrl = $"https://github.com/Kitware/CMake/releases/download/v{CmakeVersion}/cmake-{CmakeVersion}-windows-x86_64.zip"; + + public static async Task EnsureDependenciesAsync(SystemInfo sysInfo) + { + // 1. Check CMake (Download if missing on Windows) + string cmakePath = GetCmakePath(); + if (cmakePath == null) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + await DownloadAndInstallCmakeAsync(); + } + else + { + // Linux usually handles this via apt earlier, but just in case: + throw new Exception("CMake is missing. Please run: sudo apt install cmake"); + } + } + + // 2. Check Visual Studio (Windows Only) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + if (!CheckVisualStudio()) + { + PromptForVisualStudio(); + } + } + + // 3. Check GPU Toolkits (CUDA / ROCm / OneAPI) + await ValidateGpuToolkitAsync(sysInfo); + } + + private static async Task ValidateGpuToolkitAsync(SystemInfo sysInfo) + { + if (sysInfo.GpuVendor == GpuVendor.Nvidia) + { + // Check for NVCC + if (!CheckCommandExists("nvcc")) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + AnsiConsole.MarkupLine("[red]CUDA Toolkit not found![/]"); + AnsiConsole.MarkupLine("To use your NVIDIA GPU, you must install the CUDA Toolkit."); + AnsiConsole.MarkupLine("[link]https://developer.nvidia.com/cuda-downloads[/]"); + + if (!AnsiConsole.Confirm("Have you installed the CUDA Toolkit and are ready to retry?")) + { + throw new Exception("CUDA Toolkit required for Nvidia build."); + } + } + else + { + // Linux auto-install attempt or error + throw new Exception("CUDA Toolkit missing. Run: sudo apt install nvidia-cuda-toolkit"); + } + } + } + else if (sysInfo.GpuVendor == GpuVendor.Intel) + { + if (!CheckCommandExists("icx")) // Intel OneAPI Compiler + { + AnsiConsole.MarkupLine("[yellow]Warning: Intel OneAPI Base Toolkit not found.[/]"); + AnsiConsole.MarkupLine("For optimal Intel performance (SYCL), install OneAPI: [blue]https://www.intel.com/content/www/us/en/developer/tools/oneapi/base-toolkit.html[/]"); + AnsiConsole.MarkupLine("Proceeding with CPU/Vulkan fallback if build fails."); + } + } + // AMD on Linux usually handled by "sudo apt install hipcc" or rocm libs + } + + // --- CMake Helpers --- + + public static string? GetCmakePath() + { + // 1. Check Global Path + if (CheckCommandExists("cmake")) return "cmake"; + + // 2. Check Local 'MagicQuant/cmake/bin' + string localPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), + MagicConstants.MagicQuantFolder, "cmake", "bin", "cmake.exe"); + return File.Exists(localPath) ? localPath : null; + } + + private static async Task DownloadAndInstallCmakeAsync() + { + string magicPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), MagicConstants.MagicQuantFolder); + string zipPath = Path.Combine(magicPath, "cmake.zip"); + string extractPath = Path.Combine(magicPath, "cmake"); + + AnsiConsole.Status().Start("Downloading CMake...", ctx => + { + using var client = new HttpClient(); + var bytes = client.GetByteArrayAsync(CmakeWinUrl).Result; + File.WriteAllBytes(zipPath, bytes); + }); + + AnsiConsole.MarkupLine("Extracting CMake..."); + if (Directory.Exists(extractPath)) Directory.Delete(extractPath, true); + + ZipFile.ExtractToDirectory(zipPath, magicPath); + + // Rename the extracted folder (e.g., cmake-3.29-windows...) to just "cmake" + var extractedDir = Directory.GetDirectories(magicPath, "cmake-*").First(); + Directory.Move(extractedDir, extractPath); + + File.Delete(zipPath); + AnsiConsole.MarkupLine("[green]CMake installed successfully.[/]"); + } + + // --- Visual Studio Helpers --- + + private static bool CheckVisualStudio() + { + // Quick check for vswhere or cl.exe + return CheckCommandExists("cl") || File.Exists(@"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"); + } + + private static void PromptForVisualStudio() + { + AnsiConsole.Write(new Rule("[red]Missing Visual Studio[/]")); + AnsiConsole.MarkupLine("MagicQuant requires [bold]Visual Studio Build Tools 2022[/] with C++ Desktop Development."); + AnsiConsole.MarkupLine("[blue]https://visualstudio.microsoft.com/downloads/#build-tools[/]"); + + if (!AnsiConsole.Confirm("Have you installed Visual Studio Build Tools?")) + { + throw new Exception("Visual Studio is required to compile on Windows."); + } + } + + private static bool CheckCommandExists(string cmd) + { + try + { + var psi = new ProcessStartInfo + { + FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "where" : "which", + Arguments = cmd, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + }; + using var p = Process.Start(psi); + p?.WaitForExit(); + return p?.ExitCode == 0; + } + catch { return false; } + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/HardwareHelper.cs b/MagicQuant/Helpers/HardwareHelper.cs new file mode 100644 index 0000000..69df39f --- /dev/null +++ b/MagicQuant/Helpers/HardwareHelper.cs @@ -0,0 +1,96 @@ +using System.Runtime.InteropServices; +using MagicQuant.Models; +using System.Diagnostics; + +namespace MagicQuant.Helpers; + +public static class HardwareHelper +{ + public static SystemInfo GetSystemInfo() + { + var info = new SystemInfo + { + ThreadCount = Environment.ProcessorCount, + RamGb = GetTotalRam(), + GpuVendor = DetectGpuVendor(out string gpuName, out double vram), + GpuName = gpuName, + VramGb = vram + }; + return info; + } + public static double GetCudaVersion() + { + try + { + // We check nvcc because that matches the Toolkit we installed/verified + var psi = new ProcessStartInfo + { + FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "nvcc.exe" : "nvcc", + Arguments = "--version", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var p = Process.Start(psi); + if (p == null) return 0; + + string output = p.StandardOutput.ReadToEnd(); + p.WaitForExit(); + + // Output format: "Cuda compilation tools, release 12.4, V12.4.131" + // Regex to find "release X.Y" + var match = System.Text.RegularExpressions.Regex.Match(output, @"release (\d+\.\d+)"); + if (match.Success && double.TryParse(match.Groups[1].Value, out double version)) + { + return version; + } + } + catch + { + // Fallback: If nvcc fails, we might try parsing nvidia-smi, + // but for now, returning 0 triggers a safe fallback. + } + return 0; + } + + private static double GetTotalRam() + { + // simplified generic check + return GC.GetGCMemoryInfo().TotalAvailableMemoryBytes / 1024.0 / 1024.0 / 1024.0; + } + + private static GpuVendor DetectGpuVendor(out string name, out double vram) + { + name = "Generic"; + vram = 0; + + // 1. Check NVIDIA (nvidia-smi) - Works on Linux & Windows + try + { + var process = new Process + { + StartInfo = new ProcessStartInfo("nvidia-smi", "--query-gpu=name,memory.total --format=csv,noheader,nounits") { RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true } + }; + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + if (process.ExitCode == 0 && !string.IsNullOrWhiteSpace(output)) + { + var parts = output.Split(','); + name = parts[0].Trim(); + if (parts.Length > 1 && double.TryParse(parts[1], out double mem)) vram = mem / 1024.0; + return GpuVendor.Nvidia; + } + } + catch { /* Not Nvidia */ } + + // 2. Check MacOS (Metal) - Placeholder + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return GpuVendor.Cpu; // Todo: Metal check + + // 3. Fallbacks (AMD/Intel) would go here (e.g., parsing lshw on linux) + // For now, defaulting to CPU if Nvidia fails + return GpuVendor.Cpu; + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/LinuxHelper.cs b/MagicQuant/Helpers/LinuxHelper.cs new file mode 100644 index 0000000..567ded6 --- /dev/null +++ b/MagicQuant/Helpers/LinuxHelper.cs @@ -0,0 +1,29 @@ +using System.Diagnostics; +using Spectre.Console; + +namespace MagicQuant.Helpers; + +public class LinuxHelper +{ + public static async Task RefreshSudoCredentialsAsync() + { + AnsiConsole.MarkupLine("[grey]Verifying sudo access for system installs...[/]"); + + // "sudo -v" updates the user's cached credentials. + // It will prompt for a password if necessary. + var psi = new ProcessStartInfo + { + FileName = "sudo", + Arguments = "-v", + UseShellExecute = false // Let standard input handle the password prompt + }; + + var p = Process.Start(psi); + await p!.WaitForExitAsync(); + + if (p.ExitCode != 0) + { + throw new Exception("Sudo access denied or cancelled."); + } + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/LlamaBuilder.cs b/MagicQuant/Helpers/LlamaBuilder.cs new file mode 100644 index 0000000..0682bd7 --- /dev/null +++ b/MagicQuant/Helpers/LlamaBuilder.cs @@ -0,0 +1,153 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; +using LibGit2Sharp; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Helpers; + +public class LlamaBuilder +{ + private readonly string _llamaRoot; + private readonly SystemInfo _sysInfo; + + public LlamaBuilder(string magicRoot, SystemInfo sysInfo) + { + _llamaRoot = Path.Combine(magicRoot, MagicConstants.LlamaRepoName); + _sysInfo = sysInfo; + } + + public string GetLlamaBinPath() => Path.Combine(_llamaRoot, "build", "bin"); + + public async Task PrepareAndBuildAsync(bool forceRebuild) +{ + // 1. Validate ALL dependencies before doing anything + await DependencyManager.EnsureDependenciesAsync(_sysInfo); + + // 2. Clone / Pull / Update Logic + // If --update (forceRebuild) is passed, we delete the repo to force a clean clone. + if (forceRebuild && Directory.Exists(_llamaRoot)) + { + AnsiConsole.MarkupLine("[yellow]Update requested: Removing old repository...[/]"); + try + { + // Recursive delete + Directory.Delete(_llamaRoot, true); + } + catch (Exception ex) + { + // Windows sometimes locks files; warn user but try to proceed or fail + AnsiConsole.MarkupLine($"[red]Warning: Could not delete old repo: {ex.Message}[/]"); + throw; + } + } + + if (!Directory.Exists(_llamaRoot)) + { + AnsiConsole.MarkupLine($"Cloning llama.cpp to [blue]{_llamaRoot}[/]..."); + AnsiConsole.MarkupLine("[grey](This includes submodules and may take a moment)[/]"); + + // Clone with RecurseSubmodules = true matches "git submodule update --init --recursive" + var cloneOptions = new CloneOptions { RecurseSubmodules = true }; + Repository.Clone("https://github.com/ggerganov/llama.cpp.git", _llamaRoot, cloneOptions); + } + else + { + AnsiConsole.MarkupLine("[grey]Repository already exists. Skipping clone.[/]"); + } + + // 3. Setup Build Directory + string buildDir = Path.Combine(_llamaRoot, "build"); + + // If we just re-cloned, this directory is gone anyway, but if we didn't, + // and forceRebuild is true (e.g. if deletion failed above but we continue), clean it. + if (forceRebuild && Directory.Exists(buildDir)) + { + Directory.Delete(buildDir, true); + } + Directory.CreateDirectory(buildDir); + + // 4. Get the CMake Executable (System or Local) + string cmakeExe = DependencyManager.GetCmakePath() ?? "cmake"; + + // 5. Generate Build Files + string cmakeArgs = GetOptimalCmakeArgs(); + AnsiConsole.MarkupLine($"[grey]Configuring build with: {cmakeArgs}[/]"); + + // Note: We run this inside the 'build' folder + if (!await RunProcessAsync(cmakeExe, cmakeArgs, buildDir)) + throw new Exception("CMake configuration failed."); + + // 6. Compile + AnsiConsole.MarkupLine("[cyan]Compiling Llama.cpp (Release Mode)...[/]"); + + // -j triggers parallel build using all available cores + string buildCmd = "--build . --config Release -j " + Environment.ProcessorCount; + + if (!await RunProcessAsync(cmakeExe, buildCmd, buildDir)) + throw new Exception("Build failed."); + + AnsiConsole.MarkupLine("[green]✔ Build Success![/]"); +} + + private string GetOptimalCmakeArgs() + { + // Core Args + var args = new List { "..", "-DCMAKE_BUILD_TYPE=Release" }; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + args.Add("-G Ninja"); + + // GPU Optimization Logic + switch (_sysInfo.GpuVendor) + { + case GpuVendor.Nvidia: + args.Add("-DGGML_CUDA=ON"); + // Native = Compiles specifically for the detected card (Perfect optimization) + args.Add("-DCMAKE_CUDA_ARCHITECTURES=native"); + break; + + case GpuVendor.Amd: + args.Add("-DGGML_HIPBLAS=ON"); + // If on Linux, you might add -DAMDGPU_TARGETS=gfx1100 etc if needed + // But usually standard HIP build is sufficient + break; + + case GpuVendor.Intel: + // Try SYCL (OneAPI) first as it is fastest + // If OneAPI isn't present (checked in DependencyManager), + // you might fallback to Vulkan here: "-DGGML_VULKAN=ON" + args.Add("-DGGML_SYCL=ON"); + break; + + default: + // CPU Fallback (ensure AVX is on) + // CMake usually detects AVX2 automatically + break; + } + + return string.Join(" ", args); + } + + private async Task RunProcessAsync(string exe, string args, string workingDir) + { + var psi = new ProcessStartInfo + { + FileName = exe, Arguments = args, WorkingDirectory = workingDir, + RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false + }; + + using var p = Process.Start(psi); + if (p == null) return false; + + // Capture output to show user progress + p.OutputDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.WriteLine(e.Data); }; + p.ErrorDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.WriteLine(e.Data); }; + + p.BeginOutputReadLine(); + p.BeginErrorReadLine(); + await p.WaitForExitAsync(); + + return p.ExitCode == 0; + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/PythonManager.cs b/MagicQuant/Helpers/PythonManager.cs new file mode 100644 index 0000000..6d47088 --- /dev/null +++ b/MagicQuant/Helpers/PythonManager.cs @@ -0,0 +1,216 @@ +using System.Diagnostics; +using System.IO.Compression; +using System.Runtime.InteropServices; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Helpers; + +public class PythonManager +{ + private readonly string _basePath; + private readonly string _envPath; + + public PythonManager(string basePath) + { + _basePath = basePath; + _envPath = Path.Combine(basePath, MagicConstants.EnvName); + } + + public async Task GetInstalledVersionAsync(string packageName) + { + // We use a tiny python script to check importlib.metadata + // This is instant compared to pip + string script = $"import importlib.metadata; " + + $"try: print(importlib.metadata.version('{packageName}')); " + + $"except: print('NONE')"; + + string python = GetPythonExecutable(); + string exe, args; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + exe = "cmd.exe"; + args = $"/c \"{python}\" -c \"{script}\""; + } + else + { + exe = python; + args = $"-c \"{script}\""; + } + + // Run without printing output to console + var psi = new ProcessStartInfo + { + FileName = exe, Arguments = args, + RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true + }; + + using var proc = Process.Start(psi); + string output = await proc!.StandardOutput.ReadToEndAsync(); + await proc.WaitForExitAsync(); + + string version = output.Trim(); + return version == "NONE" ? null : version; + } + + public string GetPythonExecutable() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + return Path.Combine(_envPath, "python.exe"); + + return Path.Combine(_envPath, "bin", "python3"); + } + + public async Task SetupEnvironmentAsync() + { + AnsiConsole.MarkupLine("[cyan]Configuring Python Environment...[/]"); + + if (CheckSuccessMarker()) + { + AnsiConsole.MarkupLine("[green]✔ Python Environment is ready.[/]"); + return; + } + + // Clean slate if corrupt + if (Directory.Exists(_envPath)) Directory.Delete(_envPath, true); + Directory.CreateDirectory(_envPath); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + await SetupWindowsEmbedAsync(); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + await SetupLinuxVenvAsync(); + } + + // Install Pip Runner logic + await SetupPipRunnerAsync(); + + WriteSuccessMarker(); + } + + private async Task SetupWindowsEmbedAsync() + { + string zipPath = Path.Combine(_basePath, MagicConstants.WinPythonZip); + + // Download + if (!File.Exists(zipPath)) + { + using var client = new HttpClient(); + AnsiConsole.MarkupLine($"Downloading Python Embeddable from [blue]{MagicConstants.WinPythonUrl}[/]"); + var data = await client.GetByteArrayAsync(MagicConstants.WinPythonUrl); + await File.WriteAllBytesAsync(zipPath, data); + } + + // Extract + AnsiConsole.MarkupLine("Extracting Python..."); + ZipFile.ExtractToDirectory(zipPath, _envPath); + + // Cleanup Zip + File.Delete(zipPath); + + // Modify .pth file to allow importing site-packages (Crucial for pip) + string pthFile = Directory.GetFiles(_envPath, "*._pth").FirstOrDefault(); + if (pthFile != null) + { + var lines = await File.ReadAllLinesAsync(pthFile); + var newLines = lines.Select(l => l.Trim() == "#import site" ? "import site" : l).ToList(); + await File.WriteAllLinesAsync(pthFile, newLines); + } + } + + private async Task SetupLinuxVenvAsync() + { + AnsiConsole.MarkupLine("Creating venv..."); + // FIX 1: Added _basePath as working dir, and null for env vars + await RunShellCommand("python3", $"-m venv \"{_envPath}\"", _basePath, null); + } + + private async Task SetupPipRunnerAsync() + { + // Copy pip_runner.py from Helpers to Env + string source = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Helpers", "pip_runner.py"); + string dest = Path.Combine(_envPath, "pip_runner.py"); + + if (File.Exists(source)) + { + File.Copy(source, dest, true); + AnsiConsole.MarkupLine("Copied pip_runner.py."); + } + else + { + AnsiConsole.MarkupLine("[yellow]Warning: pip_runner.py not found in Helpers.[/]"); + } + + // Upgrade Pip using the runner or standard module + string python = GetPythonExecutable(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // FIX 2: Added null for env vars (4th arg) + await RunShellCommand("cmd.exe", $"/c \"{python}\" pip_runner.py install --upgrade pip setuptools wheel", _envPath, null); + } + else + { + // FIX 3: Added null for env vars (4th arg) + await RunShellCommand(python, "-m pip install --upgrade pip setuptools wheel", _basePath, null); + } + } + + public async Task RunPipInstallAsync(string args, Dictionary? envVars = null) + { + string python = GetPythonExecutable(); + string exe, finalArgs; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + exe = "cmd.exe"; + finalArgs = $"/c \"{python}\" pip_runner.py install {args}"; + } + else + { + exe = python; + finalArgs = $"-m pip install {args}"; + } + + await RunShellCommand(exe, finalArgs, _envPath, envVars); + } + + private bool CheckSuccessMarker() => File.Exists(Path.Combine(_envPath, MagicConstants.SuccessJson)); + private void WriteSuccessMarker() => File.WriteAllText(Path.Combine(_envPath, MagicConstants.SuccessJson), "{\"status\":\"success\"}"); + + // The Method Signature causing the issue + private async Task RunShellCommand(string exe, string args, string workingDir, Dictionary? envVars = null) + { + var psi = new ProcessStartInfo + { + FileName = exe, + Arguments = args, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + if (!string.IsNullOrEmpty(workingDir)) psi.WorkingDirectory = workingDir; + + if (envVars != null) + { + foreach (var kvp in envVars) + { + psi.EnvironmentVariables[kvp.Key] = kvp.Value; + } + } + + using var proc = Process.Start(psi); + if (proc == null) return; + + proc.OutputDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.MarkupLine($"[grey]{Markup.Escape(e.Data)}[/]"); }; + proc.ErrorDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.MarkupLine($"[red]{Markup.Escape(e.Data)}[/]"); }; + + proc.BeginOutputReadLine(); + proc.BeginErrorReadLine(); + await proc.WaitForExitAsync(); + } +} \ No newline at end of file diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index e357ca3..319424b 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -10,6 +10,7 @@ + @@ -18,8 +19,4 @@ - - - - diff --git a/MagicQuant/Models/SystemInfo.cs b/MagicQuant/Models/SystemInfo.cs new file mode 100644 index 0000000..faf58d0 --- /dev/null +++ b/MagicQuant/Models/SystemInfo.cs @@ -0,0 +1,31 @@ +namespace MagicQuant.Models; + +public enum GpuVendor +{ + Nvidia = 1, + Amd = 2, + Intel = 3, + Cpu = 4, + Unknown = 0 +} + +public class SystemInfo +{ + public GpuVendor GpuVendor { get; set; } + public string GpuName { get; set; } = "Unknown"; + public double VramGb { get; set; } + public double RamGb { get; set; } + public int ThreadCount { get; set; } +} + +public static class MagicConstants +{ + public const string MagicQuantFolder = "MagicQuant"; + public const string EnvName = "MagicQuant-Env"; + public const string LlamaRepoName = "llama.cpp"; + public const string SuccessJson = "install_success.json"; + + // Windows Python Embed URL + public const string WinPythonUrl = "https://www.python.org/ftp/python/3.12.3/python-3.12.3-embed-amd64.zip"; + public const string WinPythonZip = "python-3.12.3-embed.zip"; +} \ No newline at end of file diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 92d11e2..ab2a724 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -5,20 +5,19 @@ using MagicQuant.Models; using Spectre.Console; -// 1. OS & Permission Check -if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) +#if DEBUG +// If we are in Debug and no arguments were passed, default to "evolution" +if (args.Length == 0) { - // On Linux, the effective user ID for root is 0 - // We check if we are running as root to ensure file/env access - if (GetLinuxUserId() != 0) - { - AnsiConsole.Write(new Rule("[red]Permission Denied[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine("[red]Error:[/] MagicQuant must be run with [bold]sudo[/] on Linux to manage environments and files."); - AnsiConsole.MarkupLine("[grey]Please try:[/] [yellow]sudo dotnet MagicQuant.dll[/] (or your binary name)"); - return; - } + args = new[] { "evolution" }; } +// OPTIONAL: Manually append hardcoded flags for testing specific scenarios +// Example: If you want to test "evolution --iterations 10" every time you debug +// string manualFlags = "--iterations 10 --verbose"; +// args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); +#endif + // 2. Define the Command Registry var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) { From e413ea6273e879ae9182aaf2ef9fed15daeb8856 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 16:45:40 -0500 Subject: [PATCH 007/258] benchmark process converted from Python. Not sure if it works yet --- MagicQuant/Commands/InitializeLlamaCpp.cs | 2 +- MagicQuant/Models/BenchmarkResult.cs | 7 + MagicQuant/Models/LlamaBenchMetrics.cs | 10 + MagicQuant/Models/LlamaBinaries.cs | 35 ++ MagicQuant/Models/PplMetrics.cs | 9 + MagicQuant/Services/BenchmarkService.cs | 387 ++++++++++++++++++++++ 6 files changed, 449 insertions(+), 1 deletion(-) create mode 100644 MagicQuant/Models/BenchmarkResult.cs create mode 100644 MagicQuant/Models/LlamaBenchMetrics.cs create mode 100644 MagicQuant/Models/LlamaBinaries.cs create mode 100644 MagicQuant/Models/PplMetrics.cs create mode 100644 MagicQuant/Services/BenchmarkService.cs diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 11bf42b..d429ca1 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -185,7 +185,7 @@ async Task EnsurePackage(string name, string installCmd, Dictionary Perplexity { get; set; } = new(); +} \ No newline at end of file diff --git a/MagicQuant/Models/LlamaBenchMetrics.cs b/MagicQuant/Models/LlamaBenchMetrics.cs new file mode 100644 index 0000000..069f246 --- /dev/null +++ b/MagicQuant/Models/LlamaBenchMetrics.cs @@ -0,0 +1,10 @@ +namespace MagicQuant.Models; + +public class LlamaBenchMetrics +{ + public string? LogPath { get; set; } + public string? Backend { get; set; } + public int? Ngl { get; set; } + public string? Test { get; set; } + public double? Tps { get; set; } +} \ No newline at end of file diff --git a/MagicQuant/Models/LlamaBinaries.cs b/MagicQuant/Models/LlamaBinaries.cs new file mode 100644 index 0000000..74b6fb8 --- /dev/null +++ b/MagicQuant/Models/LlamaBinaries.cs @@ -0,0 +1,35 @@ +using System.Runtime.InteropServices; + +namespace MagicQuant.Models; + +public class LlamaBinaries +{ + public string Bench { get; } + public string Ppl { get; } + public string Cli { get; } + + public LlamaBinaries(string root) + { + var binDir = Path.Combine(root, "build", "bin"); + Bench = Path.Combine(binDir, "llama-bench"); + Ppl = Path.Combine(binDir, "llama-perplexity"); + Cli = Path.Combine(binDir, "llama-cli"); + + // Windows check (.exe) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Bench += ".exe"; Ppl += ".exe"; Cli += ".exe"; + } + } + + public void Validate() + { + var missing = new List(); + if (!File.Exists(Bench)) missing.Add(Bench); + if (!File.Exists(Ppl)) missing.Add(Ppl); + if (!File.Exists(Cli)) missing.Add(Cli); + + if (missing.Any()) + throw new FileNotFoundException($"Missing llama.cpp binaries:\n{string.Join("\n", missing)}"); + } +} \ No newline at end of file diff --git a/MagicQuant/Models/PplMetrics.cs b/MagicQuant/Models/PplMetrics.cs new file mode 100644 index 0000000..58f912d --- /dev/null +++ b/MagicQuant/Models/PplMetrics.cs @@ -0,0 +1,9 @@ +namespace MagicQuant.Models; + +public class PplMetrics +{ + public string? LogPath { get; set; } + public double Ppl { get; set; } + public double PplError { get; set; } + public double? Kld { get; set; } +} \ No newline at end of file diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs new file mode 100644 index 0000000..2014656 --- /dev/null +++ b/MagicQuant/Services/BenchmarkService.cs @@ -0,0 +1,387 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text.Json; +using System.Text.RegularExpressions; +using MagicQuant.Helpers; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public class BenchmarkService +{ + private readonly LlamaBinaries _bins; + private readonly PythonManager _pyManager; + + // Constants + private static readonly string[] OomMarkers = + { + "out of memory", "cudaMalloc failed", "unable to allocate cuda", "try reducing --n-gpu-layers" + }; + + private static readonly int[] NglCandidates = { 35, 30, 24, 20, 16, 12, 8, 4, 0 }; + + public BenchmarkService(string llamaRoot, PythonManager pyManager) + { + _bins = new LlamaBinaries(llamaRoot); + _bins.Validate(); + _pyManager = pyManager; + } + + // ---------------------------------------------------------------- + // Public Entry Point + // ---------------------------------------------------------------- + + public async Task RunAllBenchmarksAsync( + string modelPath, + string benchDir, + int tokenTarget = 32768, + int? startNgl = null, + string? klLogitsDir = null, + bool saveLogits = false) + { + Directory.CreateDirectory(benchDir); + var result = new BenchmarkResult(); + + // 1. Run Llama-Bench + AnsiConsole.MarkupLine("[yellow]Running Llama-Bench...[/]"); + result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, startNgl); + + // 2. Run Perplexity (General, Code, Math) + var domains = new[] { "general", "code", "math" }; + var corporaRoot = Path.Combine(Path.GetDirectoryName(benchDir)!, "_ppl_corpora"); + Directory.CreateDirectory(corporaRoot); + + if (saveLogits && !string.IsNullOrEmpty(klLogitsDir)) + Directory.CreateDirectory(klLogitsDir); + + foreach (var domain in domains) + { + AnsiConsole.MarkupLine($"[yellow]Running Perplexity ({domain})...[/]"); + + // A. Prepare Corpus + string corpusPath = Path.Combine(corporaRoot, $"ppl_corpus_{domain}.txt"); + await PreparePplCorpusAsync(domain, corpusPath, tokenTarget); + + // B. Run Benchmark + var metrics = await RunPplBenchmarkAsync( + modelPath, benchDir, domain, corpusPath, + startNgl, klLogitsDir, saveLogits + ); + + result.Perplexity[domain] = metrics; + } + + // Save Results JSON + string jsonPath = Path.Combine(benchDir, "bench_metrics.json"); + await File.WriteAllTextAsync(jsonPath, JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true })); + + return result; + } + + // ---------------------------------------------------------------- + // 1. Llama-Bench Logic + // ---------------------------------------------------------------- + + private async Task RunLlamaBenchAsync(string modelPath, string benchDir, int? startNgl) + { + string logFile = Path.Combine(benchDir, "llamabench.md"); + + // Filter candidates + var candidates = startNgl.HasValue + ? NglCandidates.Where(n => n <= startNgl.Value).ToList() + : NglCandidates.ToList(); + + // Command Builder + string BuildCmd(int ngl) => + $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {ngl} -o md"; + + // Retry Loop + int? finalNgl = await RunWithRetryAsync(BuildCmd, logFile, candidates, "llama-bench"); + + // Fallback to CPU if GPU failed completely + if (finalNgl == null) + { + AnsiConsole.MarkupLine("[red]GPU Failed. Fallback to CPU backend...[/]"); + string cpuCmd = $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; + await RunShellCommandAsync(cpuCmd, logFile); // Just run, no parsing check here usually + } + + return ParseLlamaBench(logFile); + } + + private LlamaBenchMetrics ParseLlamaBench(string logPath) + { + var metrics = new LlamaBenchMetrics { LogPath = GetRelativePath(logPath) }; + if (!File.Exists(logPath)) return metrics; + + var lines = File.ReadAllLines(logPath); + + // Find header row starting with | model | ... | backend | + int headerIdx = -1; + for (int i = 0; i < lines.Length; i++) + { + if (lines[i].Contains("|") && lines[i].Contains("backend")) + { + headerIdx = i; + break; + } + } + + if (headerIdx == -1 || lines.Length <= headerIdx + 2) return metrics; + + // Parse Header and Data Row + var headers = lines[headerIdx].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(h => h.Trim()).ToList(); + var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()).ToList(); + + if (headers.Count != dataRow.Count) return metrics; + + var row = headers.Zip(dataRow, (h, d) => new { Header = h, Data = d }).ToDictionary(x => x.Header, x => x.Data); + + // Extract TPS + string tpsStr = row.ContainsKey("t/s") ? row["t/s"] : (row.ContainsKey("tps") ? row["tps"] : "0"); + var match = Regex.Match(tpsStr, @"([0-9.]+)"); + + if (match.Success && double.TryParse(match.Groups[1].Value, out double tps)) + { + metrics.Tps = tps; + metrics.Backend = row.ContainsKey("backend") ? row["backend"] : "unknown"; + metrics.Test = row.ContainsKey("test") ? row["test"] : "unknown"; + if (row.ContainsKey("ngl") && int.TryParse(row["ngl"], out int ngl)) metrics.Ngl = ngl; + } + + return metrics; + } + + // ---------------------------------------------------------------- + // 2. Perplexity Logic + // ---------------------------------------------------------------- + + private async Task RunPplBenchmarkAsync( + string modelPath, string benchDir, string domain, string corpusPath, + int? startNgl, string? klLogitsDir, bool saveLogits) + { + string logFile = Path.Combine(benchDir, $"perplexity_{domain}.log"); + var candidates = startNgl.HasValue + ? NglCandidates.Where(n => n <= startNgl.Value).ToList() + : NglCandidates.ToList(); + + // KL Divergence Logic + string kldArgs = ""; + if (!string.IsNullOrEmpty(klLogitsDir)) + { + string logitsFile = Path.Combine(klLogitsDir, $"kld_logits_{domain}.bin"); + if (saveLogits) + kldArgs = $"--kl-divergence-base \"{logitsFile}\""; // Save to this file + else if (File.Exists(logitsFile)) + kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; // Load from file + } + + string BuildCmd(int ngl) => + $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {ngl} -c 2048 --file \"{corpusPath}\" {kldArgs}"; + + await RunWithRetryAsync(BuildCmd, logFile, candidates, $"perplexity-{domain}"); + + // Parsing + bool expectKld = (!saveLogits && !string.IsNullOrEmpty(klLogitsDir)); + return ParsePerplexity(logFile, expectKld); + } + + private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) + { + var metrics = new PplMetrics { LogPath = GetRelativePath(logPath) }; + if (!File.Exists(logPath)) return metrics; + + string text = File.ReadAllText(logPath); + string cleanText = StripAnsi(text); + + // Regex for PPL: "Mean PPL(Q) : 8.88 +/- 0.20" OR "PPL = 8.88 +/- 0.20" + var pplMatch = Regex.Match(cleanText, @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([0-9.]+)\s*(?:±|\+/-)\s*([0-9.]+)", RegexOptions.IgnoreCase); + + if (pplMatch.Success) + { + metrics.Ppl = double.Parse(pplMatch.Groups[1].Value); + metrics.PplError = double.Parse(pplMatch.Groups[2].Value); + } + else + { + AnsiConsole.MarkupLine($"[red]Error parsing PPL from {logPath}[/]"); + } + + // Regex for KLD: "Mean KLD : 0.0008" OR "KL divergence: 0.1234" + var kldMatch = Regex.Match(cleanText, @"(?:Mean\s+KLD|KL[-_\s]*divergence|kl[-_\s]*div)\s*[:=]\s*([0-9.]+)", RegexOptions.IgnoreCase); + + if (kldMatch.Success) + { + metrics.Kld = double.Parse(kldMatch.Groups[1].Value); + } + else if (!allowMissingKld && cleanText.Contains("KL", StringComparison.OrdinalIgnoreCase)) + { + // Warn if expected but not found + AnsiConsole.MarkupLine("[yellow]Warning: 'KL' found in log but regex failed to parse value.[/]"); + } + + return metrics; + } + + // ---------------------------------------------------------------- + // 3. Corpus Preparation (Using Python Interop) + // ---------------------------------------------------------------- + + private async Task PreparePplCorpusAsync(string domain, string outPath, int tokenTarget) + { + if (File.Exists(outPath) && new FileInfo(outPath).Length > 0) return; + + AnsiConsole.MarkupLine($"[grey]Generating corpus for domain: {domain}[/]"); + + // We use the PythonManager to run a script that uses 'datasets' library + // This mirrors your 'prepare_ppl_corpus' python function + // We pass the python code as a string to the python environment + + string pyScript = $@" +import sys +from datasets import load_dataset + +domain = '{domain}' +out_path = r'{outPath}' +max_chars = {tokenTarget} * 4 + +def get_sources(d): + if d == 'general': return [('wikitext', 'wikitext-103-raw-v1', 'test', 'text'), ('wikitext', 'wikitext-2-raw-v1', 'test', 'text')] + if d == 'code': return [('codeparrot/codeparrot-clean', None, 'train', 'content')] + if d == 'math': return [('gsm8k', 'main', 'test', 'question')] + return [] + +parts = [] +total = 0 +for ds, conf, split, field in get_sources(domain): + try: + d = load_dataset(ds, conf) if conf else load_dataset(ds) + for text in d[split][field]: + if not text or not isinstance(text, str): continue + chunk = text.strip() + '\n' + parts.append(chunk) + total += len(chunk) + if total >= max_chars: break + except Exception as e: + print(f'Error loading {{ds}}: {{e}}') + if total >= max_chars: break + +with open(out_path, 'w', encoding='utf-8') as f: + f.write(''.join(parts)) +"; + // Create a temp python file to run this script safely + string scriptPath = Path.Combine(Path.GetDirectoryName(outPath)!, $"gen_{domain}.py"); + await File.WriteAllTextAsync(scriptPath, pyScript); + + // Run it via PythonManager + string pythonExe = _pyManager.GetPythonExecutable(); + string args = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? $"/c \"{pythonExe}\" \"{scriptPath}\"" + : $"\"{scriptPath}\""; + + string runner = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : pythonExe; + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) args = scriptPath; // Correct linux arg + + // We need 'datasets' installed + await _pyManager.RunPipInstallAsync("datasets"); + + await RunShellCommandAsync(runner + " " + args, null); // Run the generation script + + // Cleanup script + if(File.Exists(scriptPath)) File.Delete(scriptPath); + } + + // ---------------------------------------------------------------- + // 4. Helper: Retry Logic (OOM Handling) + // ---------------------------------------------------------------- + + private async Task RunWithRetryAsync( + Func cmdBuilder, + string logPath, + List candidates, + string label) + { + foreach (int ngl in candidates) + { + string cmd = cmdBuilder(ngl); + AnsiConsole.MarkupLine($"[grey][*] {label}: trying -ngl {ngl}[/]"); + + await RunShellCommandAsync(cmd, logPath); + + string logContent = File.Exists(logPath) ? File.ReadAllText(logPath) : ""; + + // Check for OOM + if (OomMarkers.Any(m => logContent.Contains(m, StringComparison.OrdinalIgnoreCase))) + { + AnsiConsole.MarkupLine($"[yellow][WARN] {label}: OOM at -ngl {ngl}, retrying...[/]"); + continue; + } + + // Simple check: if log is empty or super short, it crashed non-OOM + if (logContent.Length < 50) + { + AnsiConsole.MarkupLine($"[yellow][WARN] {label}: Failed at -ngl {ngl} (Unknown Error), trying next...[/]"); + continue; + } + + AnsiConsole.MarkupLine($"[green][OK] {label}: succeeded with -ngl {ngl}[/]"); + return ngl; + } + + AnsiConsole.MarkupLine($"[red][!] {label}: All -ngl candidates failed.[/]"); + return null; + } + + // ---------------------------------------------------------------- + // 5. System Utilities + // ---------------------------------------------------------------- + + private async Task RunShellCommandAsync(string cmd, string? logPath) + { + var startInfo = new ProcessStartInfo + { + FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/bash", + Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"/c {cmd}" : $"-c \"{cmd}\"", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var process = new Process { StartInfo = startInfo }; + + // If logPath is provided, we stream output to it + FileStream? fs = null; + StreamWriter? sw = null; + + if (logPath != null) + { + fs = new FileStream(logPath, FileMode.Create, FileAccess.Write, FileShare.Read); + sw = new StreamWriter(fs); + } + + process.OutputDataReceived += (s, e) => { if (e.Data != null) sw?.WriteLine(e.Data); }; + process.ErrorDataReceived += (s, e) => { if (e.Data != null) sw?.WriteLine(e.Data); }; + + process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + await process.WaitForExitAsync(); + + sw?.Dispose(); + fs?.Dispose(); + } + + private string StripAnsi(string text) + { + return Regex.Replace(text, @"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])", ""); + } + + private string GetRelativePath(string fullPath) + { + // Ideally make this relative to the project root, but for now returning filename is safer for display + return Path.GetFileName(fullPath); + } +} \ No newline at end of file From a5d900e1f1aeca2cc04d7d1bcb5e8f6716fdfcfc Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 16:57:37 -0500 Subject: [PATCH 008/258] starting the new config --- MagicQuant/Config.cs | 281 +++++++++++++++++++++++++++++++ MagicQuant/Models/TensorGroup.cs | 7 + 2 files changed, 288 insertions(+) create mode 100644 MagicQuant/Config.cs create mode 100644 MagicQuant/Models/TensorGroup.cs diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs new file mode 100644 index 0000000..6474d78 --- /dev/null +++ b/MagicQuant/Config.cs @@ -0,0 +1,281 @@ +using MagicQuant.Models; + +namespace MagicQuant; + +public static class Config +{ + public static readonly int MaxDataCollectedPerCategory = 5; + public static readonly int MaxSurvivalRounds = 4; + public static readonly double CollapseMultiplier = 1.5; + + public static readonly List BaselineQuants = new() + { + "Q8_0", + "Q6_K", + "Q5_K", + "Q4_K_M", + "IQ4_NL", + "MXFP4_MOE" + }; + + public static readonly List SensitivityProbeGroups = new() + { + "embeddings", + "lm_head", + "attn_q", + "attn_kv", + "attn_output", + "ffn_up_gate", + "ffn_down", + }; + + // MoE-specific probe groups (added if MoE detected) + public static readonly List SensitivityProbeGroupsMoe = new() + { + "moe_router", + "moe_experts", + }; + + // Critical "brain" layers that cause non-linear collapse when crushed together + public static readonly List BrainLayers = new() + { + "embeddings", + "lm_head", + "attn_output", + }; + + // Schemes that trigger collapse penalty when applied to brain layers + public static readonly List CollapsePenaltySchemes = new() + { + "MXFP4", + "IQ2_XXS", + "IQ2_XS", + "IQ2_S", + }; + + public static readonly List BaseConversionModes = new() + { + "mxfp4_moe", + "iq4_nl" + }; + + public static readonly List TensorWeightSchemes = new() + { + "BF16", + "F16", + "MXFP4", + "Q8_0", + "Q6_K", + "Q5_K", + "IQ4_NL", + }; + + + public static readonly List MoeIndicatorTensors = new() + { + "blk.*.ffn_up_expert_0.weight", + "blk.*.ffn_gate_expert_0.weight", + "blk.*.ffn_down_expert_0.weight", + + // Qwen3-MOE / Unsloth / modern MOE + "blk.*.ffn_up_exps.weight", + "blk.*.ffn_gate_exps.weight", + "blk.*.ffn_down_exps.weight", + "blk.*.ffn_gate_inp.weight", + + // router variants + "router.weight", + "gate.weight", + "blk.*.router.*", + "blk.*.gate_proj.*", + "blk.*.gate_inp.*", + }; + + + public static readonly List TensorGroups = new() +{ + new TensorGroup + { + Name = "embeddings", + Tensors = new() + { + "token_embd.weight", + "model.embed_tokens.weight", + "embed_tokens.weight", + "tok_embeddings.weight", + "word_embeddings.weight", + "transformer.wte.weight", + "gpt_neox.embed_in.weight", + "shared.weight", + "wte.weight", + } + }, + + new TensorGroup + { + Name = "lm_head", + Tensors = new() + { + "output.weight", + "lm_head.weight", + "final_logits_proj.weight", + "model.embed_out.weight", + "lm_head.decoder.weight", + "cls.predictions.decoder.weight", + "gpt_neox.embed_out.weight", + "decoder.output_projection.weight", + "decoder.output_dense.weight", + "transformer.wte.weight", + "shared.weight", + } + }, + + new TensorGroup + { + Name = "attn_q", + Tensors = new() + { + "blk.*.attn_q.weight", + ".*q_proj.*weight", + ".*query.weight", + ".*q_proj.weight", + ".*self_attn.q_proj.weight", + ".*attention.self.query.weight", + ".*SelfAttention.q.weight", + ".*c_attn.weight", + ".*query_key_value.weight", + } + }, + + new TensorGroup + { + Name = "attn_kv", + Tensors = new() + { + "blk.*.attn_k.weight", + "blk.*.attn_v.weight", + ".*k_proj.*weight", + ".*v_proj.*weight", + ".*key.weight", + ".*value.weight", + ".*self_attn.k_proj.weight", + ".*self_attn.v_proj.weight", + ".*attention.self.key.weight", + ".*attention.self.value.weight", + ".*SelfAttention.k.weight", + ".*SelfAttention.v.weight", + ".*EncDecAttention.k.weight", + ".*EncDecAttention.v.weight", + ".*c_attn.weight", + ".*query_key_value.weight", + } + }, + + new TensorGroup + { + Name = "attn_output", + Tensors = new() + { + "blk.*.attn_output.weight", + ".*out_proj.*weight", + ".*o_proj.*weight", + ".*c_proj.weight", + ".*attention.output.dense.weight", + ".*self_attn.out_proj.weight", + ".*SelfAttention.o.weight", + ".*self_attention.dense.weight", + ".*attention.proj.weight", + } + }, + + new TensorGroup + { + Name = "ffn_up_gate", + Tensors = new() + { + "blk.*.ffn_up.weight", + "blk.*.ffn_gate.weight", + ".*intermediate.dense.weight", + ".*c_fc.weight", + ".*fc1.weight", + ".*fc_in.weight", + ".*dense_h_to_4h.weight", + ".*wi.weight", + ".*wi_0.weight", + ".*wi_1.weight", + ".*mlp.up_proj.weight", + ".*mlp.gate_proj.weight", + ".*DenseReluDense.wi_0.weight", + ".*DenseReluDense.wi_1.weight", + ".*experts.*wi_0.weight", + ".*experts.*wi_1.weight", + "blk.*.ffn_up_exps.weight", + "blk.*.ffn_gate_exps.weight", + } + }, + + new TensorGroup + { + Name = "ffn_down", + Tensors = new() + { + "blk.*.ffn_down.weight", + ".*output.dense.weight", + ".*c_proj.weight", + ".*fc2.weight", + ".*fc_out.weight", + ".*wo.weight", + ".*dense_4h_to_h.weight", + ".*mlp.down_proj.weight", + ".*DenseReluDense.wo.weight", + ".*experts.*wo.weight", + "blk.*.ffn_down_exps.weight", + } + }, + + new TensorGroup + { + Name = "moe_experts", + Tensors = new() + { + "blk.*.ffn_.*_expert.*", + "blk.*.ffn_.*_exps.*", + + ".*experts?\\..*wi_0.*", + ".*experts?\\..*wi_1.*", + ".*experts?\\..*wo.*", + ".*experts?\\..*fc1.*", + ".*experts?\\..*fc2.*", + ".*experts?\\..*dense_h_to_4h.*", + ".*experts?\\..*dense_4h_to_h.*", + } + }, + + new TensorGroup + { + Name = "moe_router", + Tensors = new() + { + "router.*", + "gate.*", + "gating.*", + "routing.*", + + "blk.*.ffn_gate_inp.weight", + + "blk.*.router.*", + "blk.*.gate_inp.*", + "blk.*.gate_proj.*", + "blk.*.gate.weight", + "blk.*.router_fc.*", + + ".*router.weight", + ".*gate.weight", + ".*router_fc.*", + ".*gating_network.*weight", + ".*moe_gate.*weight", + } + }, +}; + +} \ No newline at end of file diff --git a/MagicQuant/Models/TensorGroup.cs b/MagicQuant/Models/TensorGroup.cs new file mode 100644 index 0000000..8efc771 --- /dev/null +++ b/MagicQuant/Models/TensorGroup.cs @@ -0,0 +1,7 @@ +namespace MagicQuant.Models; + +public class TensorGroup +{ + public string Name { get; set; } + public List Tensors { get; set; } +} \ No newline at end of file From 5bc8fc2bb513aebfa405a8788a1eb64c4b852f63 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 17:20:45 -0500 Subject: [PATCH 009/258] Fixed a couple things, but beginning paths should work for evolution now. --- MagicQuant/Cache.cs | 2 + MagicQuant/Commands/Evolution.cs | 91 ++++++++++++++++++++++++++++++++ MagicQuant/Program.cs | 4 +- 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Cache.cs b/MagicQuant/Cache.cs index 74dc8ce..f2b2306 100644 --- a/MagicQuant/Cache.cs +++ b/MagicQuant/Cache.cs @@ -8,4 +8,6 @@ public class Cache public static string? LlamaBin; public static string? ConvertScript; public static SystemInfo? SysInfo; + public static string? MagicQuantDirectory; + public static string? ModelDirectory; } \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index c152204..2167e14 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -1,4 +1,7 @@ using MagicQuant.Models; +using MagicQuant.Helpers; +using MagicQuant; +using Spectre.Console; namespace MagicQuant.Commands; @@ -6,6 +9,94 @@ public class Evolution : ICommand { public async Task Run(List args) { + // 1. Handle Help Flag + if (args.Any(a => a.Name?.ToLower() == "help")) + { + ShowEvolutionHelp(); + return; + } + + // 2. Parse --model-dir + string? modelDirRaw = args.FirstOrDefault(a => a.Name?.ToLower() == "model-dir")?.Value; + + if (string.IsNullOrWhiteSpace(modelDirRaw)) + { + string msg = "[red]Error:[/] Missing required argument [yellow]--model-dir[/]."; + AnsiConsole.MarkupLine(msg); + ShowEvolutionHelp(); + throw new Exception(msg); + } + + // 3. Normalize and Validate Path + string fullModelPath = Path.GetFullPath(modelDirRaw); + + if (!Directory.Exists(fullModelPath)) + { + string msg = $"[red]Error:[/] The directory [yellow]'{fullModelPath}'[/] does not exist."; + AnsiConsole.MarkupLine(msg); + ShowEvolutionHelp(); + throw new Exception(msg); + } + + // 4. Validate Content (.safetensors existence) + // We look for any .safetensors file in the top directory. + // If your models are often in subfolders, change SearchOption.TopDirectoryOnly to AllDirectories. + var safeTensorFiles = Directory.GetFiles(fullModelPath, "*.safetensors", SearchOption.TopDirectoryOnly); + + if (safeTensorFiles.Length == 0) + { + AnsiConsole.MarkupLine($"[red]Error:[/] No [yellow].safetensors[/] files found in [blue]{fullModelPath}[/]."); + AnsiConsole.MarkupLine("[grey]Please ensure this is a valid HuggingFace model directory.[/]"); + throw new Exception(); + } + + // 5. Populate Cache + Cache.ModelDirectory = fullModelPath; + Cache.MagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + + // Create the MagicQuant directory immediately so it's ready for future steps + if (!Directory.Exists(Cache.MagicQuantDirectory)) + { + Directory.CreateDirectory(Cache.MagicQuantDirectory); + } + + // 6. Success Output + AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); + AnsiConsole.Write(new Rule("[yellow]Evolution Configuration[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"Model Path: [blue]{Cache.ModelDirectory}[/]"); + AnsiConsole.MarkupLine($"Output Path: [blue]{Cache.MagicQuantDirectory}[/]"); + AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length}[/] safe tensors"); + // Ensure Llama paths are set (sanity check from InitializeLlamaCpp) + if (string.IsNullOrEmpty(Cache.LlamaBin)) + { + // Note: In a real run, Program.cs runs Init first, so this might be populated. + // If not, we might want to warn or rely on defaults. + AnsiConsole.MarkupLine("[yellow]Warning: Llama binaries path not set in Cache. (Did Initialization run?)[/]"); + } + + // Next steps of evolution would go here... + } + + private void ShowEvolutionHelp() + { + // Use MarkupLine for colors/styles + AnsiConsole.MarkupLine("[bold yellow]Command: evolution[/]"); + AnsiConsole.WriteLine("Runs the full evolutionary quantization search algorithm on a target model."); + AnsiConsole.WriteLine(); + + AnsiConsole.MarkupLine("[bold]Usage:[/]"); + // Use WriteLine here so "[options]" doesn't crash it + AnsiConsole.WriteLine(" mq evolution --model-dir \"\" [options]"); + AnsiConsole.WriteLine(); + + AnsiConsole.MarkupLine("[bold]Arguments:[/]"); + // Use MarkupLine here because we WANT the [green] color + AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Required)"); + AnsiConsole.WriteLine(); + + AnsiConsole.MarkupLine("[bold]Example:[/]"); + // Use WriteLine here to avoid issues with paths (backslashes) + AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } } \ No newline at end of file diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index ab2a724..b540a4a 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -14,8 +14,8 @@ // OPTIONAL: Manually append hardcoded flags for testing specific scenarios // Example: If you want to test "evolution --iterations 10" every time you debug -// string manualFlags = "--iterations 10 --verbose"; -// args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); + string manualFlags = @"--model-dir ""/mnt/world8/AI/ToBench/Qwen3-4B-Instruct-2507-unsloth/"""; + args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif // 2. Define the Command Registry From e7334435ba2f00b45e7c6d7d7f8de2a57a258903 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 18 Dec 2025 17:48:06 -0500 Subject: [PATCH 010/258] new hybrid build model --- MagicQuant/Models/HybridBuild.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 MagicQuant/Models/HybridBuild.cs diff --git a/MagicQuant/Models/HybridBuild.cs b/MagicQuant/Models/HybridBuild.cs new file mode 100644 index 0000000..a3cae42 --- /dev/null +++ b/MagicQuant/Models/HybridBuild.cs @@ -0,0 +1,13 @@ +namespace MagicQuant.Models; + +public class HybridTensor +{ + public TensorGroup TensorGroup { get; set; } + public string TensorType { get; set; } +} + +public class HybridBuild +{ + public string Base { get; set; } + public List? Tensors { get; set; } +} \ No newline at end of file From da6cf9235bbf80c65c4a6d88c7e5373231cd5b73 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 19 Dec 2025 10:16:50 -0500 Subject: [PATCH 011/258] Updating some models and adding comments. --- MagicQuant/Cache.cs | 30 ++++++++ MagicQuant/Models/TensorGroup.cs | 19 ++++- MagicQuant/Services/BenchmarkService.cs | 94 ++++++++++++++----------- 3 files changed, 100 insertions(+), 43 deletions(-) diff --git a/MagicQuant/Cache.cs b/MagicQuant/Cache.cs index f2b2306..630e228 100644 --- a/MagicQuant/Cache.cs +++ b/MagicQuant/Cache.cs @@ -4,10 +4,40 @@ namespace MagicQuant; public class Cache { + /// + /// Full path to llama.cpp repo + /// public static string? LlamaRoot; + + /// + /// Full path to /llama.cpp/build/bin/ + /// public static string? LlamaBin; + + /// + /// full path to convert_hf_to_gguf.py + /// public static string? ConvertScript; + + /// + /// System information about the PC that's detected + /// during the initial llama cpp validation phase. + /// public static SystemInfo? SysInfo; + + /// + /// The full path to the model directory being quantized, + /// where a "MagicQuant" folder is created and used. + /// public static string? MagicQuantDirectory; + + /// + /// Full path to the desired model directory where the safetensors are. + /// public static string? ModelDirectory; + + /// + /// Aka BF16, F16, or F32 + /// + public static string? TorchType; } \ No newline at end of file diff --git a/MagicQuant/Models/TensorGroup.cs b/MagicQuant/Models/TensorGroup.cs index 8efc771..6900e4a 100644 --- a/MagicQuant/Models/TensorGroup.cs +++ b/MagicQuant/Models/TensorGroup.cs @@ -2,6 +2,21 @@ namespace MagicQuant.Models; public class TensorGroup { - public string Name { get; set; } - public List Tensors { get; set; } + public string Name { get; set; } = ""; + public List Tensors { get; set; } = new(); + + // Helper to map "embeddings" -> "E", etc. + public char ShortCode => Name switch + { + "embeddings" => 'E', + "lm_head" => 'H', + "attn_q" => 'Q', + "attn_kv" => 'K', + "attn_output" => 'O', + "ffn_up_gate" => 'U', + "ffn_down" => 'D', + "moe_experts" => 'X', + "moe_router" => 'R', + _ => '?' + }; } \ No newline at end of file diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 2014656..bd1325e 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -21,6 +21,18 @@ public class BenchmarkService private static readonly int[] NglCandidates = { 35, 30, 24, 20, 16, 12, 8, 4, 0 }; + // ---------------------------------------------------------------- + // Concurrency Controls + // ---------------------------------------------------------------- + + // 1. Exclusive Lock: When LlamaBench runs, it must be the ONLY thing running. + // Higher-level logic should acquire this before calling RunLlamaBenchAsync. + public static readonly SemaphoreSlim ExclusiveBenchLock = new(1, 1); + + // 2. VRAM Lock: Only one VRAM-heavy task (Perplexity) can run at a time. + // However, it CAN run alongside CPU tasks (like quantization if VRAM allows). + public static readonly SemaphoreSlim VramLock = new(1, 1); + public BenchmarkService(string llamaRoot, PythonManager pyManager) { _bins = new LlamaBinaries(llamaRoot); @@ -43,11 +55,21 @@ public async Task RunAllBenchmarksAsync( Directory.CreateDirectory(benchDir); var result = new BenchmarkResult(); - // 1. Run Llama-Bench - AnsiConsole.MarkupLine("[yellow]Running Llama-Bench...[/]"); - result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, startNgl); + // 1. Run Llama-Bench (Exclusive Mode) + // We acquire the exclusive lock to ensure stability + await ExclusiveBenchLock.WaitAsync(); + try + { + AnsiConsole.MarkupLine("[yellow]Running Llama-Bench (Exclusive Mode)...[/]"); + result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, startNgl); + } + finally + { + ExclusiveBenchLock.Release(); + } // 2. Run Perplexity (General, Code, Math) + // We prepare folders first so we don't block locks unnecessarily var domains = new[] { "general", "code", "math" }; var corporaRoot = Path.Combine(Path.GetDirectoryName(benchDir)!, "_ppl_corpora"); Directory.CreateDirectory(corporaRoot); @@ -57,19 +79,26 @@ public async Task RunAllBenchmarksAsync( foreach (var domain in domains) { - AnsiConsole.MarkupLine($"[yellow]Running Perplexity ({domain})...[/]"); - - // A. Prepare Corpus + // A. Prepare Corpus (CPU bound, low risk) string corpusPath = Path.Combine(corporaRoot, $"ppl_corpus_{domain}.txt"); await PreparePplCorpusAsync(domain, corpusPath, tokenTarget); - // B. Run Benchmark - var metrics = await RunPplBenchmarkAsync( - modelPath, benchDir, domain, corpusPath, - startNgl, klLogitsDir, saveLogits - ); - - result.Perplexity[domain] = metrics; + // B. Run Benchmark (VRAM Intensive) + // We acquire VRAM lock so we don't run 2 perplexities at once + await VramLock.WaitAsync(); + try + { + AnsiConsole.MarkupLine($"[yellow]Running Perplexity ({domain})...[/]"); + var metrics = await RunPplBenchmarkAsync( + modelPath, benchDir, domain, corpusPath, + startNgl, klLogitsDir, saveLogits + ); + result.Perplexity[domain] = metrics; + } + finally + { + VramLock.Release(); + } } // Save Results JSON @@ -93,6 +122,7 @@ private async Task RunLlamaBenchAsync(string modelPath, strin : NglCandidates.ToList(); // Command Builder + // Note: Keeping -p 8 -t 16 as requested ("just like we're now") string BuildCmd(int ngl) => $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {ngl} -o md"; @@ -104,7 +134,7 @@ string BuildCmd(int ngl) => { AnsiConsole.MarkupLine("[red]GPU Failed. Fallback to CPU backend...[/]"); string cpuCmd = $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; - await RunShellCommandAsync(cpuCmd, logFile); // Just run, no parsing check here usually + await RunShellCommandAsync(cpuCmd, logFile); } return ParseLlamaBench(logFile); @@ -117,7 +147,6 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) var lines = File.ReadAllLines(logPath); - // Find header row starting with | model | ... | backend | int headerIdx = -1; for (int i = 0; i < lines.Length; i++) { @@ -130,7 +159,6 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) if (headerIdx == -1 || lines.Length <= headerIdx + 2) return metrics; - // Parse Header and Data Row var headers = lines[headerIdx].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(h => h.Trim()).ToList(); var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()).ToList(); @@ -138,7 +166,6 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) var row = headers.Zip(dataRow, (h, d) => new { Header = h, Data = d }).ToDictionary(x => x.Header, x => x.Data); - // Extract TPS string tpsStr = row.ContainsKey("t/s") ? row["t/s"] : (row.ContainsKey("tps") ? row["tps"] : "0"); var match = Regex.Match(tpsStr, @"([0-9.]+)"); @@ -172,17 +199,18 @@ private async Task RunPplBenchmarkAsync( { string logitsFile = Path.Combine(klLogitsDir, $"kld_logits_{domain}.bin"); if (saveLogits) - kldArgs = $"--kl-divergence-base \"{logitsFile}\""; // Save to this file + kldArgs = $"--kl-divergence-base \"{logitsFile}\""; else if (File.Exists(logitsFile)) - kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; // Load from file + kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; } + // Command Builder + // Added "-t 4" to limit thread usage as requested string BuildCmd(int ngl) => - $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {ngl} -c 2048 --file \"{corpusPath}\" {kldArgs}"; + $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {ngl} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}"; await RunWithRetryAsync(BuildCmd, logFile, candidates, $"perplexity-{domain}"); - // Parsing bool expectKld = (!saveLogits && !string.IsNullOrEmpty(klLogitsDir)); return ParsePerplexity(logFile, expectKld); } @@ -195,7 +223,6 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) string text = File.ReadAllText(logPath); string cleanText = StripAnsi(text); - // Regex for PPL: "Mean PPL(Q) : 8.88 +/- 0.20" OR "PPL = 8.88 +/- 0.20" var pplMatch = Regex.Match(cleanText, @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([0-9.]+)\s*(?:±|\+/-)\s*([0-9.]+)", RegexOptions.IgnoreCase); if (pplMatch.Success) @@ -208,7 +235,6 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) AnsiConsole.MarkupLine($"[red]Error parsing PPL from {logPath}[/]"); } - // Regex for KLD: "Mean KLD : 0.0008" OR "KL divergence: 0.1234" var kldMatch = Regex.Match(cleanText, @"(?:Mean\s+KLD|KL[-_\s]*divergence|kl[-_\s]*div)\s*[:=]\s*([0-9.]+)", RegexOptions.IgnoreCase); if (kldMatch.Success) @@ -217,7 +243,6 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) } else if (!allowMissingKld && cleanText.Contains("KL", StringComparison.OrdinalIgnoreCase)) { - // Warn if expected but not found AnsiConsole.MarkupLine("[yellow]Warning: 'KL' found in log but regex failed to parse value.[/]"); } @@ -225,7 +250,7 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) } // ---------------------------------------------------------------- - // 3. Corpus Preparation (Using Python Interop) + // 3. Corpus Preparation // ---------------------------------------------------------------- private async Task PreparePplCorpusAsync(string domain, string outPath, int tokenTarget) @@ -234,10 +259,6 @@ private async Task PreparePplCorpusAsync(string domain, string outPath, int toke AnsiConsole.MarkupLine($"[grey]Generating corpus for domain: {domain}[/]"); - // We use the PythonManager to run a script that uses 'datasets' library - // This mirrors your 'prepare_ppl_corpus' python function - // We pass the python code as a string to the python environment - string pyScript = $@" import sys from datasets import load_dataset @@ -270,30 +291,25 @@ def get_sources(d): with open(out_path, 'w', encoding='utf-8') as f: f.write(''.join(parts)) "; - // Create a temp python file to run this script safely string scriptPath = Path.Combine(Path.GetDirectoryName(outPath)!, $"gen_{domain}.py"); await File.WriteAllTextAsync(scriptPath, pyScript); - // Run it via PythonManager string pythonExe = _pyManager.GetPythonExecutable(); string args = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"/c \"{pythonExe}\" \"{scriptPath}\"" : $"\"{scriptPath}\""; string runner = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : pythonExe; - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) args = scriptPath; // Correct linux arg + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) args = scriptPath; - // We need 'datasets' installed await _pyManager.RunPipInstallAsync("datasets"); + await RunShellCommandAsync(runner + " " + args, null); - await RunShellCommandAsync(runner + " " + args, null); // Run the generation script - - // Cleanup script if(File.Exists(scriptPath)) File.Delete(scriptPath); } // ---------------------------------------------------------------- - // 4. Helper: Retry Logic (OOM Handling) + // 4. Retry Logic // ---------------------------------------------------------------- private async Task RunWithRetryAsync( @@ -311,14 +327,12 @@ with open(out_path, 'w', encoding='utf-8') as f: string logContent = File.Exists(logPath) ? File.ReadAllText(logPath) : ""; - // Check for OOM if (OomMarkers.Any(m => logContent.Contains(m, StringComparison.OrdinalIgnoreCase))) { AnsiConsole.MarkupLine($"[yellow][WARN] {label}: OOM at -ngl {ngl}, retrying...[/]"); continue; } - // Simple check: if log is empty or super short, it crashed non-OOM if (logContent.Length < 50) { AnsiConsole.MarkupLine($"[yellow][WARN] {label}: Failed at -ngl {ngl} (Unknown Error), trying next...[/]"); @@ -351,7 +365,6 @@ private async Task RunShellCommandAsync(string cmd, string? logPath) using var process = new Process { StartInfo = startInfo }; - // If logPath is provided, we stream output to it FileStream? fs = null; StreamWriter? sw = null; @@ -381,7 +394,6 @@ private string StripAnsi(string text) private string GetRelativePath(string fullPath) { - // Ideally make this relative to the project root, but for now returning filename is safer for display return Path.GetFileName(fullPath); } } \ No newline at end of file From b5113551493c8019f1380b632e412b64fd337a9d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 19 Dec 2025 19:03:44 -0500 Subject: [PATCH 012/258] Still working on the combinatorics and config, but I've gotten a lot of the system better hardcoded to prevent static strings. --- MagicQuant/Config.cs | 211 -------------- MagicQuant/Helpers/CliHelpers.cs | 68 +++++ MagicQuant/MagicQuant.csproj | 1 + MagicQuant/Models/BaselineQuants.cs | 38 +++ MagicQuant/Models/HybridBuild.cs | 2 +- MagicQuant/Models/TensorConfigs.cs | 53 ++++ MagicQuant/Models/TensorGroup.cs | 112 +++++++- MagicQuant/Models/TensorWeight.cs | 52 ++++ MagicQuant/Models/TensorWeightScheme.cs | 175 +++++++++++ MagicQuant/Program.cs | 2 + MagicQuant/Services/QuantizationService.cs | 319 +++++++++++++++++++++ 11 files changed, 809 insertions(+), 224 deletions(-) create mode 100644 MagicQuant/Models/BaselineQuants.cs create mode 100644 MagicQuant/Models/TensorConfigs.cs create mode 100644 MagicQuant/Models/TensorWeight.cs create mode 100644 MagicQuant/Models/TensorWeightScheme.cs create mode 100644 MagicQuant/Services/QuantizationService.cs diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 6474d78..dc837f5 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -8,15 +8,6 @@ public static class Config public static readonly int MaxSurvivalRounds = 4; public static readonly double CollapseMultiplier = 1.5; - public static readonly List BaselineQuants = new() - { - "Q8_0", - "Q6_K", - "Q5_K", - "Q4_K_M", - "IQ4_NL", - "MXFP4_MOE" - }; public static readonly List SensitivityProbeGroups = new() { @@ -53,22 +44,6 @@ public static class Config "IQ2_S", }; - public static readonly List BaseConversionModes = new() - { - "mxfp4_moe", - "iq4_nl" - }; - - public static readonly List TensorWeightSchemes = new() - { - "BF16", - "F16", - "MXFP4", - "Q8_0", - "Q6_K", - "Q5_K", - "IQ4_NL", - }; public static readonly List MoeIndicatorTensors = new() @@ -91,191 +66,5 @@ public static class Config "blk.*.gate_inp.*", }; - - public static readonly List TensorGroups = new() -{ - new TensorGroup - { - Name = "embeddings", - Tensors = new() - { - "token_embd.weight", - "model.embed_tokens.weight", - "embed_tokens.weight", - "tok_embeddings.weight", - "word_embeddings.weight", - "transformer.wte.weight", - "gpt_neox.embed_in.weight", - "shared.weight", - "wte.weight", - } - }, - - new TensorGroup - { - Name = "lm_head", - Tensors = new() - { - "output.weight", - "lm_head.weight", - "final_logits_proj.weight", - "model.embed_out.weight", - "lm_head.decoder.weight", - "cls.predictions.decoder.weight", - "gpt_neox.embed_out.weight", - "decoder.output_projection.weight", - "decoder.output_dense.weight", - "transformer.wte.weight", - "shared.weight", - } - }, - - new TensorGroup - { - Name = "attn_q", - Tensors = new() - { - "blk.*.attn_q.weight", - ".*q_proj.*weight", - ".*query.weight", - ".*q_proj.weight", - ".*self_attn.q_proj.weight", - ".*attention.self.query.weight", - ".*SelfAttention.q.weight", - ".*c_attn.weight", - ".*query_key_value.weight", - } - }, - - new TensorGroup - { - Name = "attn_kv", - Tensors = new() - { - "blk.*.attn_k.weight", - "blk.*.attn_v.weight", - ".*k_proj.*weight", - ".*v_proj.*weight", - ".*key.weight", - ".*value.weight", - ".*self_attn.k_proj.weight", - ".*self_attn.v_proj.weight", - ".*attention.self.key.weight", - ".*attention.self.value.weight", - ".*SelfAttention.k.weight", - ".*SelfAttention.v.weight", - ".*EncDecAttention.k.weight", - ".*EncDecAttention.v.weight", - ".*c_attn.weight", - ".*query_key_value.weight", - } - }, - - new TensorGroup - { - Name = "attn_output", - Tensors = new() - { - "blk.*.attn_output.weight", - ".*out_proj.*weight", - ".*o_proj.*weight", - ".*c_proj.weight", - ".*attention.output.dense.weight", - ".*self_attn.out_proj.weight", - ".*SelfAttention.o.weight", - ".*self_attention.dense.weight", - ".*attention.proj.weight", - } - }, - - new TensorGroup - { - Name = "ffn_up_gate", - Tensors = new() - { - "blk.*.ffn_up.weight", - "blk.*.ffn_gate.weight", - ".*intermediate.dense.weight", - ".*c_fc.weight", - ".*fc1.weight", - ".*fc_in.weight", - ".*dense_h_to_4h.weight", - ".*wi.weight", - ".*wi_0.weight", - ".*wi_1.weight", - ".*mlp.up_proj.weight", - ".*mlp.gate_proj.weight", - ".*DenseReluDense.wi_0.weight", - ".*DenseReluDense.wi_1.weight", - ".*experts.*wi_0.weight", - ".*experts.*wi_1.weight", - "blk.*.ffn_up_exps.weight", - "blk.*.ffn_gate_exps.weight", - } - }, - - new TensorGroup - { - Name = "ffn_down", - Tensors = new() - { - "blk.*.ffn_down.weight", - ".*output.dense.weight", - ".*c_proj.weight", - ".*fc2.weight", - ".*fc_out.weight", - ".*wo.weight", - ".*dense_4h_to_h.weight", - ".*mlp.down_proj.weight", - ".*DenseReluDense.wo.weight", - ".*experts.*wo.weight", - "blk.*.ffn_down_exps.weight", - } - }, - - new TensorGroup - { - Name = "moe_experts", - Tensors = new() - { - "blk.*.ffn_.*_expert.*", - "blk.*.ffn_.*_exps.*", - - ".*experts?\\..*wi_0.*", - ".*experts?\\..*wi_1.*", - ".*experts?\\..*wo.*", - ".*experts?\\..*fc1.*", - ".*experts?\\..*fc2.*", - ".*experts?\\..*dense_h_to_4h.*", - ".*experts?\\..*dense_4h_to_h.*", - } - }, - - new TensorGroup - { - Name = "moe_router", - Tensors = new() - { - "router.*", - "gate.*", - "gating.*", - "routing.*", - - "blk.*.ffn_gate_inp.weight", - - "blk.*.router.*", - "blk.*.gate_inp.*", - "blk.*.gate_proj.*", - "blk.*.gate.weight", - "blk.*.router_fc.*", - - ".*router.weight", - ".*gate.weight", - ".*router_fc.*", - ".*gating_network.*weight", - ".*moe_gate.*weight", - } - }, -}; } \ No newline at end of file diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index 9dbf14a..a9a6dda 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -1,3 +1,5 @@ +using System.Collections.Immutable; +using System.Numerics; using System.Text.RegularExpressions; using MagicQuant.Commands; using MagicQuant.Models; @@ -7,6 +9,72 @@ namespace MagicQuant.Helpers; public static class CliHelpers { + public static void PrintTotalCombinationCount() + { + const long MaxSupported = 4_000_000_000L; + + BigInteger grandTotal = BigInteger.Zero; + + // Valid base conversions + var baseConversions = + BaselineQuants.All + .Where(b => b.AllowedAsBaseConversion) + .ToImmutableArray(); + + if (baseConversions.IsEmpty) + throw new InvalidOperationException("No BaselineQuants are marked as AllowedAsBaseConversion."); + + // Iterate each base + foreach (var baseQuant in baseConversions) + { + // Filter tensor weight schemes allowed by imatrix rule + var allowedSchemesForBase = + TensorWeightScheme.All + .Where(s => + baseQuant.RequiresImatrix || + !s.RequiresImatrix + ) + .ToImmutableArray(); + + if (allowedSchemesForBase.IsEmpty) + continue; + + BigInteger perBaseTotal = BigInteger.One; + + // For each tensor group, count legal schemes + foreach (var group in TReg.All) + { + int validCount = + allowedSchemesForBase.Count(s => + s.BannedGroups.IsEmpty || + !s.BannedGroups.Contains(group) + ); + + if (validCount == 0) + { + perBaseTotal = BigInteger.Zero; + break; + } + + perBaseTotal *= validCount; + } + + grandTotal += perBaseTotal; + } + + // Enforce DB constraint + if (grandTotal > MaxSupported) + { + throw new InvalidOperationException( + $"Total combinations ({grandTotal:N0}) exceed database primary ID limit ({MaxSupported:N0})." + ); + } + + AnsiConsole.MarkupLine( + $"[green]Total potential combinations:[/] [bold yellow]{grandTotal:N0}[/]" + ); + } + public static List ParseArguments(string input) { diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 319424b..252dcdc 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -8,6 +8,7 @@ + diff --git a/MagicQuant/Models/BaselineQuants.cs b/MagicQuant/Models/BaselineQuants.cs new file mode 100644 index 0000000..fac43bd --- /dev/null +++ b/MagicQuant/Models/BaselineQuants.cs @@ -0,0 +1,38 @@ +using System.Collections.Immutable; + +namespace MagicQuant.Models; + +public record BaselineQuants( + sbyte UniqueId, + bool RequiresImatrix, + ImmutableArray Names, + bool AllowedAsBaseConversion = false, + bool AlwaysBuild = true) +{ + public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"]); + public static readonly BaselineQuants Q6_K = new(1, false, ["Q6_K"]); + public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"]); + public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"]); + + public static readonly BaselineQuants MXFP4_MOE = new(4, false, ["MXFP4_MOE"], true); + public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"], true); + + public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_NL"]); + + // IQ3 and lower require imatrix + //public static readonly BaselineQuants IQ3_M = new(7, true, ["IQ3_M"], true); + //public static readonly BaselineQuants IQ2_M = new(8, true, ["IQ2_M"], true); + + public static readonly ImmutableArray All = + [ + Q8_0, + Q6_K, + Q5_K, + Q4_K_M, + MXFP4_MOE, + IQ4_NL, + IQ4_XS, + //IQ3_M, + //IQ2_M + ]; +} diff --git a/MagicQuant/Models/HybridBuild.cs b/MagicQuant/Models/HybridBuild.cs index a3cae42..849cbeb 100644 --- a/MagicQuant/Models/HybridBuild.cs +++ b/MagicQuant/Models/HybridBuild.cs @@ -2,7 +2,7 @@ namespace MagicQuant.Models; public class HybridTensor { - public TensorGroup TensorGroup { get; set; } + public TensorGroup TensorGroup { get; set; } = null!; public string TensorType { get; set; } } diff --git a/MagicQuant/Models/TensorConfigs.cs b/MagicQuant/Models/TensorConfigs.cs new file mode 100644 index 0000000..069e9a0 --- /dev/null +++ b/MagicQuant/Models/TensorConfigs.cs @@ -0,0 +1,53 @@ +using System.Runtime.InteropServices; + +namespace MagicQuant.Models; + +[StructLayout(LayoutKind.Sequential, Pack = 1)] +public readonly struct TensorConfig +{ + public readonly sbyte Embeddings; + public readonly sbyte LmHead; + public readonly sbyte AttnQ; + public readonly sbyte AttnKV; + public readonly sbyte AttnOutput; + public readonly sbyte FfnUpGate; + public readonly sbyte FfnDown; + public readonly sbyte MoeExperts; + public readonly sbyte MoeRouter; + + public TensorConfig( + sbyte embeddings, + sbyte lmHead, + sbyte attnQ, + sbyte attnKV, + sbyte attnOutput, + sbyte ffnUpGate, + sbyte ffnDown, + sbyte moeExperts, + sbyte moeRouter) + { + Embeddings = embeddings; + LmHead = lmHead; + AttnQ = attnQ; + AttnKV = attnKV; + AttnOutput = attnOutput; + FfnUpGate = ffnUpGate; + FfnDown = ffnDown; + MoeExperts = moeExperts; + MoeRouter = moeRouter; + } + + public sbyte GetValue(in TensorGroup group) => group.UniqueId switch + { + 0 => Embeddings, + 1 => LmHead, + 2 => AttnQ, + 3 => AttnKV, + 4 => AttnOutput, + 5 => FfnUpGate, + 6 => FfnDown, + 7 => MoeExperts, + 8 => MoeRouter, + _ => throw new ArgumentOutOfRangeException(nameof(group)) + }; +} diff --git a/MagicQuant/Models/TensorGroup.cs b/MagicQuant/Models/TensorGroup.cs index 6900e4a..c622411 100644 --- a/MagicQuant/Models/TensorGroup.cs +++ b/MagicQuant/Models/TensorGroup.cs @@ -1,22 +1,110 @@ +using System.Collections.Immutable; +using System.Linq; + + namespace MagicQuant.Models; -public class TensorGroup +/// +/// Represents a categorized group of tensors with a unique name and matching patterns. +/// +public record TensorGroup(sbyte UniqueId, string Name, ImmutableArray Tensors) { - public string Name { get; set; } = ""; - public List Tensors { get; set; } = new(); - - // Helper to map "embeddings" -> "E", etc. + /// + /// Helper to map the group name to a single-character identifier for CLI or UI display. + /// public char ShortCode => Name switch { - "embeddings" => 'E', - "lm_head" => 'H', - "attn_q" => 'Q', - "attn_kv" => 'K', + "embeddings" => 'E', + "lm_head" => 'H', + "attn_q" => 'Q', + "attn_kv" => 'K', "attn_output" => 'O', "ffn_up_gate" => 'U', - "ffn_down" => 'D', + "ffn_down" => 'D', "moe_experts" => 'X', - "moe_router" => 'R', - _ => '?' + "moe_router" => 'R', + _ => '?' }; +} + +/// +/// Tensor Registry +/// +public static class TReg +{ + public static readonly TensorGroup Embeddings = new(0, "embeddings", [ + "token_embd.weight", "model.embed_tokens.weight", "embed_tokens.weight", + "tok_embeddings.weight", "word_embeddings.weight", "transformer.wte.weight", + "gpt_neox.embed_in.weight", "shared.weight", "wte.weight" + ]); + + public static readonly TensorGroup LmHead = new(1, "lm_head", [ + "output.weight", "lm_head.weight", "final_logits_proj.weight", + "model.embed_out.weight", "lm_head.decoder.weight", "cls.predictions.decoder.weight", + "gpt_neox.embed_out.weight", "decoder.output_projection.weight", + "decoder.output_dense.weight", "transformer.wte.weight", "shared.weight" + ]); + + public static readonly TensorGroup AttnQ = new(2, "attn_q", [ + "blk.*.attn_q.weight", ".*q_proj.*weight", ".*query.weight", ".*q_proj.weight", + ".*self_attn.q_proj.weight", ".*attention.self.query.weight", + ".*SelfAttention.q.weight", ".*c_attn.weight", ".*query_key_value.weight" + ]); + + public static readonly TensorGroup AttnKV = new(3, "attn_kv", [ + "blk.*.attn_k.weight", "blk.*.attn_v.weight", ".*k_proj.*weight", ".*v_proj.*weight", + ".*key.weight", ".*value.weight", ".*self_attn.k_proj.weight", ".*self_attn.v_proj.weight", + ".*attention.self.key.weight", ".*attention.self.value.weight", ".*SelfAttention.k.weight", + ".*SelfAttention.v.weight", ".*EncDecAttention.k.weight", ".*EncDecAttention.v.weight", + ".*c_attn.weight", ".*query_key_value.weight" + ]); + + public static readonly TensorGroup AttnOutput = new(4, "attn_output", [ + "blk.*.attn_output.weight", ".*out_proj.*weight", ".*o_proj.*weight", ".*c_proj.weight", + ".*attention.output.dense.weight", ".*self_attn.out_proj.weight", + ".*SelfAttention.o.weight", ".*self_attention.dense.weight", ".*attention.proj.weight" + ]); + + public static readonly TensorGroup FfnUpGate = new(5, "ffn_up_gate", [ + "blk.*.ffn_up.weight", "blk.*.ffn_gate.weight", ".*intermediate.dense.weight", + ".*c_fc.weight", ".*fc1.weight", ".*fc_in.weight", ".*dense_h_to_4h.weight", + ".*wi.weight", ".*wi_0.weight", ".*wi_1.weight", ".*mlp.up_proj.weight", + ".*mlp.gate_proj.weight", ".*DenseReluDense.wi_0.weight", ".*DenseReluDense.wi_1.weight", + ".*experts.*wi_0.weight", ".*experts.*wi_1.weight", "blk.*.ffn_up_exps.weight", + "blk.*.ffn_gate_exps.weight" + ]); + + public static readonly TensorGroup FfnDown = new(6, "ffn_down", [ + "blk.*.ffn_down.weight", ".*output.dense.weight", ".*c_proj.weight", ".*fc2.weight", + ".*fc_out.weight", ".*wo.weight", ".*dense_4h_to_h.weight", ".*mlp.down_proj.weight", + ".*DenseReluDense.wo.weight", ".*experts.*wo.weight", "blk.*.ffn_down_exps.weight" + ]); + + public static readonly TensorGroup MoeExperts = new(7, "moe_experts", [ + "blk.*.ffn_.*_expert.*", "blk.*.ffn_.*_exps.*", ".*experts?\\..*wi_0.*", + ".*experts?\\..*wi_1.*", ".*experts?\\..*wo.*", ".*experts?\\..*fc1.*", + ".*experts?\\..*fc2.*", ".*experts?\\..*dense_h_to_4h.*", ".*experts?\\..*dense_4h_to_h.*" + ]); + + public static readonly TensorGroup MoeRouter = new(8, "moe_router", [ + "router.*", "gate.*", "gating.*", "routing.*", "blk.*.ffn_gate_inp.weight", + "blk.*.router.*", "blk.*.gate_inp.*", "blk.*.gate_proj.*", "blk.*.gate.weight", + "blk.*.router_fc.*", ".*router.weight", ".*gate.weight", ".*router_fc.*", + ".*gating_network.*weight", ".*moe_gate.*weight" + ]); + + /// + /// Provides a complete list of all registered tensor groups. + /// + public static readonly ImmutableArray All = + [ + Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, + FfnUpGate, FfnDown, MoeExperts, MoeRouter + ]; + + /// + /// Look up a group by its string name (useful when parsing external configs). + /// + public static TensorGroup? GetByName(string name) => + All.FirstOrDefault(g => g.Name.Equals(name, System.StringComparison.OrdinalIgnoreCase)); } \ No newline at end of file diff --git a/MagicQuant/Models/TensorWeight.cs b/MagicQuant/Models/TensorWeight.cs new file mode 100644 index 0000000..c938b91 --- /dev/null +++ b/MagicQuant/Models/TensorWeight.cs @@ -0,0 +1,52 @@ +namespace MagicQuant.Models; + +public class TensorWeight +{ + public TensorWeight(sbyte uniqueId, bool requiresImatrix, string[] names, TensorGroup[]? bannedGroups = null) + { + Names = names.ToList(); + UniqueId = uniqueId; + RequiresImatrix = requiresImatrix; + BannedGroups = bannedGroups?.ToList(); + } + + /// + /// + /// + /// Leave null for basically everything. Only provide Bf16 or F16 or + /// so on for those that are categorized together, which is Unique. + /// + /// + public string GetName(string? name = null) + { + if (Names != null && Names.Any()) + { + if (Names.Count == 1) + { + return Names.First(); + } + else if(Names.Count > 1 && !string.IsNullOrEmpty(name)) + { + return Names.First(x => x.Equals(name, StringComparison.InvariantCultureIgnoreCase)); + } + else + { + throw new Exception("Tensor Weight had more than one name, but provided override name was null or didn't match any stored."); + } + } + else + { + throw new Exception("No strings in the TensorWeight Names variable."); + } + } + + public List? Names { get; } + public sbyte UniqueId { get; } + + public bool RequiresImatrix { get; } + + /// + /// Which Tensor Groups this tensor weight CANNOT be attached too. + /// + public List? BannedGroups { get; } +} \ No newline at end of file diff --git a/MagicQuant/Models/TensorWeightScheme.cs b/MagicQuant/Models/TensorWeightScheme.cs new file mode 100644 index 0000000..a79fa5c --- /dev/null +++ b/MagicQuant/Models/TensorWeightScheme.cs @@ -0,0 +1,175 @@ +using System.Collections.Immutable; + +namespace MagicQuant.Models; + +public record TensorWeightScheme( + sbyte UniqueId, + bool RequiresImatrix, + ImmutableArray Names, + ImmutableArray BannedGroups, + bool AlwaysBuild = true) +{ + // NULL: always-present groups, never nullable + public static readonly TensorWeightScheme NULL = + new( + 0, + false, + ["NULL"], + [ + TReg.Embeddings, + TReg.AttnQ, + TReg.AttnKV, + TReg.AttnOutput, + TReg.FfnDown, + TReg.FfnUpGate + ] + ); + + // BF16 and F16 intentionally share UniqueId + public static readonly TensorWeightScheme BF16_F16 = + new( + 1, + false, + ["BF16", "F16"], + [] + ); + + public static readonly TensorWeightScheme MXFP4 = + new( + 2, + false, + ["MXFP4"], + [ + TReg.AttnQ, + TReg.MoeRouter, + TReg.MoeExperts + ] + ); + + public static readonly TensorWeightScheme Q8_0 = + new(3, false, ["Q8_0"], []); + + public static readonly TensorWeightScheme Q6_K = + new(4, false, ["Q6_K"], []); + + public static readonly TensorWeightScheme Q5_K = + new( + 5, + false, + ["Q5_K"], + [TReg.MoeRouter] + ); + + public static readonly TensorWeightScheme IQ4_NL = + new( + 6, + false, + ["IQ4_NL"], + [TReg.MoeRouter] + ); + + public static readonly TensorWeightScheme IQ4_XS = + new( + 7, + false, + ["IQ4_XS"], + [TReg.MoeRouter] + ); + + // IQ3 levels + public static readonly TensorWeightScheme IQ3_S = + new( + 8, + true, + ["IQ3_S"], + [ + TReg.Embeddings, + TReg.LmHead, + TReg.MoeRouter + ] + ); + + public static readonly TensorWeightScheme IQ3_XS = + new( + 9, + true, + ["IQ3_XS"], + [ + TReg.Embeddings, + TReg.LmHead, + TReg.MoeRouter + ] + ); + + public static readonly TensorWeightScheme IQ3_XXS = + new( + 10, + true, + ["IQ3_XXS"], + [ + TReg.Embeddings, + TReg.LmHead, + TReg.MoeRouter + ] + ); + + // IQ2 levels: extremely restrictive + public static readonly TensorWeightScheme IQ2_S = + new( + 11, + true, + ["IQ2_S"], + [ + TReg.Embeddings, + TReg.LmHead, + TReg.MoeRouter, + TReg.MoeExperts + ] + ); + + public static readonly TensorWeightScheme IQ2_XS = + new( + 12, + true, + ["IQ2_XS"], + [ + TReg.Embeddings, + TReg.LmHead, + TReg.MoeRouter, + TReg.MoeExperts + ] + ); + + public static readonly TensorWeightScheme IQ2_XXS = + new( + 13, + true, + ["IQ2_XXS"], + [ + TReg.Embeddings, + TReg.LmHead, + TReg.MoeRouter, + TReg.MoeExperts, + TReg.AttnKV + ] + ); + + public static readonly ImmutableArray All = + [ + NULL, + BF16_F16, + MXFP4, + Q8_0, + Q6_K, + Q5_K, + IQ4_NL, + IQ4_XS, + /*IQ3_S, + IQ3_XS, + IQ3_XXS, + IQ2_S, + IQ2_XS, + IQ2_XXS*/ + ]; +} + diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index b540a4a..1ddfb02 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -18,6 +18,8 @@ args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif +CliHelpers.PrintTotalCombinationCount(); + // 2. Define the Command Registry var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) { diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs new file mode 100644 index 0000000..907bfba --- /dev/null +++ b/MagicQuant/Services/QuantizationService.cs @@ -0,0 +1,319 @@ +using System.Collections.Concurrent; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text.Json; +using MagicQuant; +using MagicQuant.Helpers; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public class QuantizationService +{ + private readonly BenchmarkService _benchmarker; + private readonly string _ggufDir; + private readonly string _benchDir; + + // Threading Control + // We limit CPU-heavy quantization jobs to (TotalThreads / 8) to avoid choking the system + // while leaving room for the GPU-heavy Perplexity tasks. + private readonly SemaphoreSlim _cpuQuantLock; + + // The Queue + private readonly ConcurrentQueue> _jobQueue = new(); + private bool _isQueueRunning = false; + + public QuantizationService(BenchmarkService benchmarker) + { + _benchmarker = benchmarker; + + // Setup Directories based on Cache (assumed populated by Evolution command) + if (Cache.MagicQuantDirectory == null) + throw new Exception("MagicQuant Directory not set. Run initialization first."); + + _ggufDir = Path.Combine(Cache.MagicQuantDirectory, "GGUF"); + _benchDir = Path.Combine(Cache.MagicQuantDirectory, "Benchmarks"); + + Directory.CreateDirectory(_ggufDir); + Directory.CreateDirectory(_benchDir); + + // Limit concurrent quantizations. + // Example: 32 threads -> 4 concurrent quants (leaving threads for PPL) + int maxConcurrent = Math.Max(1, (Cache.SysInfo?.ThreadCount ?? 4) / 8); + _cpuQuantLock = new SemaphoreSlim(maxConcurrent, maxConcurrent); + } + + // ---------------------------------------------------------------- + // 1. High-Level Entry Point: Build & Benchmark + // ---------------------------------------------------------------- + + public void QueueJob(HybridBuild build) + { + _jobQueue.Enqueue(async () => await ProcessHybridBuildAsync(build)); + StartQueueProcessor(); + } + + private void StartQueueProcessor() + { + if (_isQueueRunning) return; + _isQueueRunning = true; + + // Fire and forget the processor loop + Task.Run(async () => + { + while (_jobQueue.TryDequeue(out var job)) + { + await job(); + } + _isQueueRunning = false; + }); + } + + private async Task ProcessHybridBuildAsync(HybridBuild build) + { + try + { + // 1. Ensure BF16 Base Exists (Prerequisite) + string bf16Path = await EnsureBf16ModelAsync(); + + // 2. Determine Output Name & Path + string modelName = GenerateHybridName(build); + string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); + + // 3. Quantize (CPU Bound - Parallel) + await _cpuQuantLock.WaitAsync(); + try + { + if (!File.Exists(quantPath)) + { + AnsiConsole.MarkupLine($"[cyan]Building Hybrid Model:[/] {modelName}"); + await RunLlamaQuantizeAsync(bf16Path, quantPath, build); + } + } + finally + { + _cpuQuantLock.Release(); + } + + // 4. Benchmark (Mixed CPU/GPU/Exclusive) + // The BenchmarkService handles its own locking (Exclusive vs VRAM) + // so we can just call it here. + string modelBenchDir = Path.Combine(_benchDir, modelName); + string metricsPath = Path.Combine(modelBenchDir, "bench_metrics.json"); + + if (!File.Exists(metricsPath)) + { + AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {modelName}"); + + // Note: LlamaBench will block everything else (ExclusiveBenchLock). + // Perplexity will run in parallel with other Quant jobs if VRAM permits (VramLock). + await _benchmarker.RunAllBenchmarksAsync( + quantPath, + modelBenchDir, + saveLogits: false // Only BF16 saves logits usually + ); + + // Cleanup: Delete GGUF after benchmark to save space (per requirements) + // EXCEPT if it is a base/common one we might want to keep? + // Logic: "always remember to delete the hybrid or base... delete with true delete" + if (File.Exists(quantPath) && !IsProtectedModel(modelName)) + { + AnsiConsole.MarkupLine($"[grey]Deleting temp model: {modelName}[/]"); + File.Delete(quantPath); + } + } + } + catch (Exception ex) + { + AnsiConsole.WriteException(ex); + } + } + + private bool IsProtectedModel(string name) + { + // Don't delete the BF16/F16/F32 base files + return name.EndsWith("BF16") || name.EndsWith("F16") || name.EndsWith("F32"); + } + + // ---------------------------------------------------------------- + // 2. BF16 Base Generation (The "Root" Model) + // ---------------------------------------------------------------- + + public async Task EnsureBf16ModelAsync() + { + // Name usually: -BF16.gguf + // We get ModelName from Cache.ModelDirectory + string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; + + // Detect Torch Type from Cache (as you requested) or default to BF16 + string typeSuffix = Cache.SysInfo != null ? "BF16" : "F16"; // Simplification + // Real logic: Check Cache.TorchType (e.g. "BF16", "F16", "F32") + // For this snippet, I assume "BF16" is the target per your prompt. + + string fileName = $"{modelName}-BF16.gguf"; + string output = Path.Combine(_ggufDir, fileName); + string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); + + if (File.Exists(output) && File.Exists(successFile)) + return output; + + // Create/Convert + AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeSuffix}...[/]"); + + // Clean partials + if (File.Exists(output)) File.Delete(output); + + string convertScript = Cache.ConvertScript + ?? throw new Exception("ConvertScript path missing in Cache"); + + // Command: python convert_hf_to_gguf.py path --outtype bf16 --outfile output + var psi = new ProcessStartInfo + { + FileName = "python", // Or _pyManager.GetPythonExecutable() + Arguments = $"\"{convertScript}\" \"{Cache.ModelDirectory}\" --outtype bf16 --outfile \"{output}\"", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var p = Process.Start(psi); + p.OutputDataReceived += (s, e) => { if(e.Data != null) AnsiConsole.WriteLine(e.Data); }; + p.ErrorDataReceived += (s, e) => { if(e.Data != null) AnsiConsole.WriteLine(e.Data); }; // Errors often printed to stderr + p.BeginOutputReadLine(); p.BeginErrorReadLine(); + await p.WaitForExitAsync(); + + if (p.ExitCode != 0) throw new Exception("BF16 Conversion Failed"); + + // Write Success JSON + await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); + + // Run Benchmark on BF16 (Critical First Step) + string benchPath = Path.Combine(_benchDir, "BF16"); + // We need to save logits for the base model so others can calculate KLD + string logitsDir = Path.Combine(benchPath, "logits"); + + AnsiConsole.MarkupLine("[bold yellow]Benchmarking Base BF16 (Saving Logits)...[/]"); + await _benchmarker.RunAllBenchmarksAsync( + output, + benchPath, + klLogitsDir: logitsDir, + saveLogits: true + ); + + return output; + } + + // ---------------------------------------------------------------- + // 3. Hybrid Quantization Execution + // ---------------------------------------------------------------- + + public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridBuild build) + { + // 1. Base Arguments + // llama-quantize [flags] input output base_type + var args = new List(); + + // 2. Hybrid Overrides (The Magic) + // Modern llama-quantize supports --tensor-type = + if (build.Tensors != null) + { + foreach (var hybrid in build.Tensors) + { + foreach (var tensorPattern in hybrid.TensorGroup.Tensors) + { + // Convert glob-like patterns to what llama-quantize accepts if needed + // Usually it accepts substrings or regex. + // We append: --tensor-type pattern=type + args.Add($"--tensor-type \"{tensorPattern}={hybrid.TensorType}\""); + } + } + } + + // 3. Files and Base Type + args.Add($"\"{inputFile}\""); + args.Add($"\"{outputFile}\""); + args.Add(build.Base); + + // 4. Threads (8 per quant job as requested) + args.Add("8"); + + string arguments = string.Join(" ", args); + string bin = Cache.LlamaBin + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "/llama-quantize.exe" : "/llama-quantize"); + + // Execute + var psi = new ProcessStartInfo + { + FileName = bin, + Arguments = arguments, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var p = Process.Start(psi); + // We might not want to spam console with quantization logs unless verbose + p.BeginOutputReadLine(); p.BeginErrorReadLine(); + await p.WaitForExitAsync(); + + if (p.ExitCode != 0) throw new Exception($"Quantization failed for {outputFile}"); + } + + // ---------------------------------------------------------------- + // 4. Naming Scheme Logic (E-H-Q-K-O...) + // ---------------------------------------------------------------- + + public string GenerateHybridName(HybridBuild build) + { + string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; + + // If pure baseline (no tensors), just - + if (build.Tensors == null || build.Tensors.Count == 0) + { + return $"{modelName}-{build.Base}"; + } + + // Hybrid Logic + // 1. Group by Quant Type + var grouped = build.Tensors + .GroupBy(t => t.TensorType) + .Select(g => new + { + Type = g.Key, + // Get Sortable ShortCodes (E, H, Q, K...) + Codes = g.Select(x => x.TensorGroup.ShortCode).OrderBy(c => GetOrder(c)).ToArray() + }) + .OrderBy(x => GetOrder(x.Codes.FirstOrDefault())) + .ToList(); + + var nameParts = new List(); + + foreach (var group in grouped) + { + string codeStr = new string(group.Codes); // e.g., "EH" or "QKO" + // Remove underscores from quant type for cleanliness (Q4_K_M -> Q4KM) if desired + // The prompt says "Q6K", "B16". Let's stick to simple mapping. + string quantStr = SimplifyQuant(group.Type); + + nameParts.Add($"{codeStr}-{quantStr}"); + } + + string suffix = string.Join("-", nameParts); + return $"{modelName}-{build.Base}-{suffix}"; + } + + private int GetOrder(char c) + { + // E, H, Q, K, O, U, D, X, R + return "EHQKOUDXR".IndexOf(c); + } + + private string SimplifyQuant(string quant) + { + // Optional: Simplify quantization names for the filename + // BF16 -> B16, Q4_K_M -> Q4KM + return quant.Replace("_", "").Replace("BF16", "B16").Replace("F16", "F16"); + } +} \ No newline at end of file From 6db9ee8976effe4a44722734e28616bb2c11b597 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 19 Dec 2025 19:32:49 -0500 Subject: [PATCH 013/258] Combinations and generator now connected and seems to be working accordingly. --- MagicQuant/Helpers/CliHelpers.cs | 60 +------ MagicQuant/Helpers/ComboLogic.cs | 92 +++++++++++ MagicQuant/Helpers/TensorConfigGenerator.cs | 170 ++++++++++++++++++++ MagicQuant/Models/BaselineQuants.cs | 3 +- MagicQuant/Models/TensorWeightScheme.cs | 6 +- MagicQuant/Program.cs | 99 +++++++++--- 6 files changed, 350 insertions(+), 80 deletions(-) create mode 100644 MagicQuant/Helpers/ComboLogic.cs create mode 100644 MagicQuant/Helpers/TensorConfigGenerator.cs diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index a9a6dda..c6d2dc6 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -13,66 +13,14 @@ public static void PrintTotalCombinationCount() { const long MaxSupported = 4_000_000_000L; - BigInteger grandTotal = BigInteger.Zero; + BigInteger total = ComboCounter.CountAll(); - // Valid base conversions - var baseConversions = - BaselineQuants.All - .Where(b => b.AllowedAsBaseConversion) - .ToImmutableArray(); - - if (baseConversions.IsEmpty) - throw new InvalidOperationException("No BaselineQuants are marked as AllowedAsBaseConversion."); - - // Iterate each base - foreach (var baseQuant in baseConversions) - { - // Filter tensor weight schemes allowed by imatrix rule - var allowedSchemesForBase = - TensorWeightScheme.All - .Where(s => - baseQuant.RequiresImatrix || - !s.RequiresImatrix - ) - .ToImmutableArray(); - - if (allowedSchemesForBase.IsEmpty) - continue; - - BigInteger perBaseTotal = BigInteger.One; - - // For each tensor group, count legal schemes - foreach (var group in TReg.All) - { - int validCount = - allowedSchemesForBase.Count(s => - s.BannedGroups.IsEmpty || - !s.BannedGroups.Contains(group) - ); - - if (validCount == 0) - { - perBaseTotal = BigInteger.Zero; - break; - } - - perBaseTotal *= validCount; - } - - grandTotal += perBaseTotal; - } - - // Enforce DB constraint - if (grandTotal > MaxSupported) - { + if (total > MaxSupported) throw new InvalidOperationException( - $"Total combinations ({grandTotal:N0}) exceed database primary ID limit ({MaxSupported:N0})." - ); - } + $"Total combinations ({total:N0}) exceed database primary ID limit ({MaxSupported:N0})."); AnsiConsole.MarkupLine( - $"[green]Total potential combinations:[/] [bold yellow]{grandTotal:N0}[/]" - ); + $"[green]Total potential combinations:[/] [bold yellow]{total:N0}[/]"); } diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs new file mode 100644 index 0000000..29bd5de --- /dev/null +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -0,0 +1,92 @@ +using System.Numerics; +using MagicQuant.Models; +using System.Collections.Immutable; + +namespace MagicQuant.Helpers; + +public static class ComboLogic +{ + // Order must match TensorConfig ctor field order + private static readonly ImmutableArray GroupsOrdered = + TReg.All.OrderBy(g => g.UniqueId).ToImmutableArray(); + + public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants baseQuant) + { + bool baseRequiresImatrix = baseQuant.RequiresImatrix; + + var schemesForBase = + TensorWeightScheme.All + .Where(s => baseRequiresImatrix || !s.RequiresImatrix) + .ToImmutableArray(); + + if (schemesForBase.IsEmpty) + throw new InvalidOperationException("No tensor schemes available for this base."); + + var builder = ImmutableArray.CreateBuilder(); + + foreach (var group in GroupsOrdered) + { + var ids = + schemesForBase + .Where(s => + s.BannedGroups.IsDefault || + s.BannedGroups.IsEmpty || + !s.BannedGroups.Contains(group)) + .Select(s => s.UniqueId) + .ToArray(); + + if (ids.Length == 0) + throw new InvalidOperationException( + $"Group '{group.Name}' has no valid tensor schemes for base '{string.Join("/", baseQuant.Names)}'."); + + builder.Add(ids); + } + + var result = builder.ToImmutable(); + + // 🔒 Absolute safety check (keep this during development) + for (int i = 0; i < result.Length; i++) + { + if (result[i] == null) + throw new InvalidOperationException($"Allowed scheme array at index {i} is null."); + } + + return result; + } + + + public static BigInteger CountCombinations(in BaselineQuants baseQuant) + { + var allowed = GetAllowedSchemeIdsPerGroup(baseQuant); + + BigInteger total = BigInteger.One; + for (int i = 0; i < allowed.Length; i++) + total *= allowed[i].Length; + + return total; + } +} + +public static class ComboCounter +{ + public static BigInteger CountForBase(BaselineQuants baseQuant) + { + var allowed = ComboLogic.GetAllowedSchemeIdsPerGroup(baseQuant); + + BigInteger total = BigInteger.One; + for (int i = 0; i < allowed.Length; i++) + total *= allowed[i].Length; + + return total; + } + + public static BigInteger CountAll() + { + BigInteger sum = BigInteger.Zero; + + foreach (var b in BaselineQuants.All.Where(b => b.AllowedAsBaseConversion)) + sum += CountForBase(b); + + return sum; + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs new file mode 100644 index 0000000..f793706 --- /dev/null +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -0,0 +1,170 @@ +using MagicQuant.Models; +using System.Collections.Concurrent; +using System.Collections.Immutable; +using System.Numerics; +using Spectre.Console; + +namespace MagicQuant.Helpers; + +public static class TensorConfigGenerator +{ + public static IEnumerable> GenerateTensorConfigBatches( + BaselineQuants baseQuant, + int batchSize = 10_000_000, + CancellationToken ct = default) + { + if (batchSize <= 0) + throw new ArgumentOutOfRangeException(nameof(batchSize)); + + // --------------------------- + // Diagnostics / invariants + // --------------------------- + if (TReg.All.IsDefault) + throw new InvalidOperationException("TensorRegistry.All is default (uninitialized)."); + + var allowed = ComboLogic.GetAllowedSchemeIdsPerGroup(baseQuant); + + if (allowed.IsDefault) + throw new InvalidOperationException("Allowed scheme array is default (uninitialized)."); + + if (allowed.Length == 0) + yield break; + + for (int i = 0; i < allowed.Length; i++) + { + if (allowed[i] == null) + throw new InvalidOperationException( + $"Allowed[{i}] is null for base {string.Join("/", baseQuant.Names)}."); + + if (allowed[i].Length == 0) + throw new InvalidOperationException( + $"Allowed[{i}] is empty for base {string.Join("/", baseQuant.Names)}."); + } + + int dims = allowed.Length; + + // --------------------------- + // Threading setup + // --------------------------- + int dop = ComputeWorkerThreads(GetThreadCountSafe()); + + var queue = new BlockingCollection>( + boundedCapacity: Math.Max(2, dop * 2)); + + // --------------------------- + // Producer + // --------------------------- + var producer = Task.Run(() => + { + try + { + // Partition on first dimension + Parallel.ForEach( + Partitioner.Create(0, allowed[0].Length), + new ParallelOptions + { + MaxDegreeOfParallelism = dop, + CancellationToken = ct + }, + range => + { + var batch = new List( + Math.Min(batchSize, 250_000)); + + var idx = new int[dims]; + + for (int i0 = range.Item1; i0 < range.Item2; i0++) + { + ct.ThrowIfCancellationRequested(); + + idx[0] = i0; + Array.Clear(idx, 1, dims - 1); + + while (true) + { + batch.Add(BuildTensorConfig(allowed, idx)); + + if (batch.Count >= batchSize) + { + queue.Add(batch, ct); + batch = new List( + Math.Min(batchSize, 250_000)); + } + + // Mixed-radix increment (dims-1 → 1) + int d = dims - 1; + while (d >= 1) + { + idx[d]++; + if (idx[d] < allowed[d].Length) + break; + + idx[d] = 0; + d--; + } + + if (d < 1) + break; + } + } + + if (batch.Count > 0) + queue.Add(batch, ct); + }); + } + finally + { + queue.CompleteAdding(); + } + }, ct); + + // --------------------------- + // Consumer (yield batches) + // --------------------------- + foreach (var batch in queue.GetConsumingEnumerable(ct)) + yield return batch; + + producer.GetAwaiter().GetResult(); + } + + private static TensorConfig BuildTensorConfig( + ImmutableArray allowed, + int[] idx) + { + // Order MUST match ComboLogic.GroupsOrdered + return new TensorConfig( + embeddings: allowed[0][idx[0]], + lmHead: allowed[1][idx[1]], + attnQ: allowed[2][idx[2]], + attnKV: allowed[3][idx[3]], + attnOutput: allowed[4][idx[4]], + ffnUpGate: allowed[5][idx[5]], + ffnDown: allowed[6][idx[6]], + moeExperts: allowed[7][idx[7]], + moeRouter: allowed[8][idx[8]] + ); + } + + private static int GetThreadCountSafe() + { + // Cache.SysInfo might not be initialized this early; fall back safely + var tc = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; + return Math.Max(1, tc); + } + + private static int ComputeWorkerThreads(int threadCount) + { + if (threadCount <= 1) + return 1; + + int workers; + if (threadCount < 16) + workers = threadCount - 1; + else + workers = (int)Math.Floor(threadCount * 0.90); + + // Always leave at least 1 thread free + workers = Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); + return workers; + } +} \ No newline at end of file diff --git a/MagicQuant/Models/BaselineQuants.cs b/MagicQuant/Models/BaselineQuants.cs index fac43bd..4d89ddc 100644 --- a/MagicQuant/Models/BaselineQuants.cs +++ b/MagicQuant/Models/BaselineQuants.cs @@ -6,8 +6,7 @@ public record BaselineQuants( sbyte UniqueId, bool RequiresImatrix, ImmutableArray Names, - bool AllowedAsBaseConversion = false, - bool AlwaysBuild = true) + bool AllowedAsBaseConversion = false) { public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"]); public static readonly BaselineQuants Q6_K = new(1, false, ["Q6_K"]); diff --git a/MagicQuant/Models/TensorWeightScheme.cs b/MagicQuant/Models/TensorWeightScheme.cs index a79fa5c..2241ffc 100644 --- a/MagicQuant/Models/TensorWeightScheme.cs +++ b/MagicQuant/Models/TensorWeightScheme.cs @@ -31,7 +31,7 @@ public record TensorWeightScheme( 1, false, ["BF16", "F16"], - [] + ImmutableArray.Empty ); public static readonly TensorWeightScheme MXFP4 = @@ -47,10 +47,10 @@ public record TensorWeightScheme( ); public static readonly TensorWeightScheme Q8_0 = - new(3, false, ["Q8_0"], []); + new(3, false, ["Q8_0"], ImmutableArray.Empty); public static readonly TensorWeightScheme Q6_K = - new(4, false, ["Q6_K"], []); + new(4, false, ["Q6_K"], ImmutableArray.Empty); public static readonly TensorWeightScheme Q5_K = new( diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 1ddfb02..f957dcf 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -1,9 +1,11 @@ -using System.Runtime.InteropServices; +using System.Diagnostics; +using System.Runtime.InteropServices; using System.Text.RegularExpressions; using MagicQuant.Commands; using MagicQuant.Helpers; using MagicQuant.Models; using Spectre.Console; +using System.Collections.Immutable; #if DEBUG // If we are in Debug and no arguments were passed, default to "evolution" @@ -14,12 +16,10 @@ // OPTIONAL: Manually append hardcoded flags for testing specific scenarios // Example: If you want to test "evolution --iterations 10" every time you debug - string manualFlags = @"--model-dir ""/mnt/world8/AI/ToBench/Qwen3-4B-Instruct-2507-unsloth/"""; - args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); +string manualFlags = @"--model-dir ""/mnt/world8/AI/ToBench/Qwen3-4B-Instruct-2507-unsloth/"""; +args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif -CliHelpers.PrintTotalCombinationCount(); - // 2. Define the Command Registry var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) { @@ -49,23 +49,26 @@ string remainingArgsString = string.Join(" ", args.Skip(1)); List parsedArgs = CliHelpers.ParseArguments(remainingArgsString); -try +try { // 6. Mandatory Validation for non-init commands if (!commandInput.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) { await AnsiConsole.Status() - .StartAsync("[grey]Checking environment dependencies...[/]", async ctx => + .StartAsync("[grey]Checking environment dependencies...[/]", async ctx => { var initializer = new InitializeLlamaCpp(); var validationArgs = new List { new CliArg { Name = "validate", Value = "" } }; await initializer.Run(validationArgs); }); - + AnsiConsole.MarkupLine("[bold green]✓[/] Environment validated."); AnsiConsole.WriteLine(); } + // Manditory Run combinations and DuckDB setup + ValidateCombinationLogicWorks(); + // 7. Execute Command var commandInstance = commandInfo.Factory(); await commandInstance.Run(parsedArgs); @@ -77,21 +80,79 @@ await AnsiConsole.Status() #region Linux Helpers -static uint GetLinuxUserId() +static void ValidateCombinationLogicWorks() { - // Standard Unix call to get effective user ID - [DllImport("libc")] - static extern uint geteuid(); + CliHelpers.PrintTotalCombinationCount(); - try - { - return geteuid(); - } - catch +// ---------------------------------------- +// Pre-compute expected total (source of truth) +// ---------------------------------------- + var expectedTotal = ComboCounter.CountAll(); + + AnsiConsole.MarkupLine( + $"[bold cyan]Expected total combinations:[/] [bold yellow]{expectedTotal:N0}[/]"); + +// ---------------------------------------- +// Generation + timing +// ---------------------------------------- + var sw = Stopwatch.StartNew(); + + long actualTotal = 0; + + var bases = + BaselineQuants.All + .Where(b => b.AllowedAsBaseConversion) + .ToImmutableArray(); + + foreach (var b in bases) { - // Fallback for environments where libc isn't standard - return 1; // Assume non-root + AnsiConsole.MarkupLine( + $"[cyan]Base:[/] [bold]{string.Join("/", b.Names)}[/] " + + $"[grey](RequiresImatrix={b.RequiresImatrix})[/]"); + + long baseTotal = 0; + + foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches( + b, batchSize: 10_000_000)) + { + baseTotal += batch.Count; + actualTotal += batch.Count; + + AnsiConsole.MarkupLine( + $" [green]Batch:[/] {batch.Count:N0} " + + $"[grey]BaseRunning:[/] {baseTotal:N0}"); + + // Release memory aggressively (unit-test mode) + batch.Clear(); + } + + AnsiConsole.MarkupLine( + $"[yellow]Base total:[/] {baseTotal:N0}"); } + + sw.Stop(); + +// ---------------------------------------- +// Verification +// ---------------------------------------- + bool match = actualTotal == expectedTotal; + + AnsiConsole.MarkupLine( + $"[bold green]Generated total:[/] {actualTotal:N0}"); + + AnsiConsole.MarkupLine( + match + ? "[bold green] Counts match expected total[/]" + : $"[bold red] MISMATCH! Expected {expectedTotal:N0} but generated {actualTotal:N0}[/]"); + +// ---------------------------------------- +// Human-readable elapsed time +// ---------------------------------------- + var t = sw.Elapsed; + + AnsiConsole.MarkupLine( + $"[bold]Elapsed:[/] " + + $"{t.Hours}h {t.Minutes}m {t.Seconds}s {t.Milliseconds}ms"); } #endregion \ No newline at end of file From 57f2891418591f0bf250701cab1b0502c38aa65a Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 21 Dec 2025 13:22:41 -0500 Subject: [PATCH 014/258] Cleaning up organization of config --- MagicQuant/Helpers/TensorConfigGenerator.cs | 74 +++---- MagicQuant/Models/BaselineQuants.cs | 4 +- MagicQuant/Models/HybridBuild.cs | 13 -- MagicQuant/Models/HybridQuant.cs | 77 ++++++++ MagicQuant/Models/TensorConfigs.cs | 68 +++++-- MagicQuant/Models/TensorWeightScheme.cs | 15 +- MagicQuant/Services/QuantizationService.cs | 201 ++++++++++++-------- 7 files changed, 305 insertions(+), 147 deletions(-) delete mode 100644 MagicQuant/Models/HybridBuild.cs create mode 100644 MagicQuant/Models/HybridQuant.cs diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index f793706..53e2e7b 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -1,8 +1,6 @@ using MagicQuant.Models; using System.Collections.Concurrent; using System.Collections.Immutable; -using System.Numerics; -using Spectre.Console; namespace MagicQuant.Helpers; @@ -48,6 +46,9 @@ public static IEnumerable> GenerateTensorConfigBatches( // --------------------------- int dop = ComputeWorkerThreads(GetThreadCountSafe()); + // Cache baseQuant.UniqueId once (perf) + sbyte baseId = baseQuant.UniqueId; + var queue = new BlockingCollection>( boundedCapacity: Math.Max(2, dop * 2)); @@ -58,7 +59,6 @@ public static IEnumerable> GenerateTensorConfigBatches( { try { - // Partition on first dimension Parallel.ForEach( Partitioner.Create(0, allowed[0].Length), new ParallelOptions @@ -68,11 +68,22 @@ public static IEnumerable> GenerateTensorConfigBatches( }, range => { - var batch = new List( - Math.Min(batchSize, 250_000)); - + var batch = new List(Math.Min(batchSize, 250_000)); var idx = new int[dims]; + // Hot-path aliases (perf) + // NOTE: This assumes group count is stable at 9 (Embeddings..MoeRouter), + // which matches your TensorConfig mapping. + var d0 = allowed[0]; + var d1 = allowed[1]; + var d2 = allowed[2]; + var d3 = allowed[3]; + var d4 = allowed[4]; + var d5 = allowed[5]; + var d6 = allowed[6]; + var d7 = allowed[7]; + var d8 = allowed[8]; + for (int i0 = range.Item1; i0 < range.Item2; i0++) { ct.ThrowIfCancellationRequested(); @@ -82,13 +93,24 @@ public static IEnumerable> GenerateTensorConfigBatches( while (true) { - batch.Add(BuildTensorConfig(allowed, idx)); + // Inline-build (perf): avoids helper call overhead and repeated bounds checks + batch.Add(new TensorConfig( + baseQuant: baseId, + embeddings: d0[idx[0]], + lmHead: d1[idx[1]], + attnQ: d2[idx[2]], + attnKV: d3[idx[3]], + attnOutput: d4[idx[4]], + ffnUpGate: d5[idx[5]], + ffnDown: d6[idx[6]], + moeExperts: d7[idx[7]], + moeRouter: d8[idx[8]] + )); if (batch.Count >= batchSize) { queue.Add(batch, ct); - batch = new List( - Math.Min(batchSize, 250_000)); + batch = new List(Math.Min(batchSize, 250_000)); } // Mixed-radix increment (dims-1 → 1) @@ -127,28 +149,10 @@ public static IEnumerable> GenerateTensorConfigBatches( producer.GetAwaiter().GetResult(); } - private static TensorConfig BuildTensorConfig( - ImmutableArray allowed, - int[] idx) - { - // Order MUST match ComboLogic.GroupsOrdered - return new TensorConfig( - embeddings: allowed[0][idx[0]], - lmHead: allowed[1][idx[1]], - attnQ: allowed[2][idx[2]], - attnKV: allowed[3][idx[3]], - attnOutput: allowed[4][idx[4]], - ffnUpGate: allowed[5][idx[5]], - ffnDown: allowed[6][idx[6]], - moeExperts: allowed[7][idx[7]], - moeRouter: allowed[8][idx[8]] - ); - } - private static int GetThreadCountSafe() { // Cache.SysInfo might not be initialized this early; fall back safely - var tc = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; + int tc = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; return Math.Max(1, tc); } @@ -157,14 +161,12 @@ private static int ComputeWorkerThreads(int threadCount) if (threadCount <= 1) return 1; - int workers; - if (threadCount < 16) - workers = threadCount - 1; - else - workers = (int)Math.Floor(threadCount * 0.90); + int workers = + threadCount < 16 + ? threadCount - 1 + : (int)Math.Floor(threadCount * 0.90); // Always leave at least 1 thread free - workers = Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); - return workers; + return Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); } -} \ No newline at end of file +} diff --git a/MagicQuant/Models/BaselineQuants.cs b/MagicQuant/Models/BaselineQuants.cs index 4d89ddc..9e2b67f 100644 --- a/MagicQuant/Models/BaselineQuants.cs +++ b/MagicQuant/Models/BaselineQuants.cs @@ -14,9 +14,9 @@ public record BaselineQuants( public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"]); public static readonly BaselineQuants MXFP4_MOE = new(4, false, ["MXFP4_MOE"], true); - public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"], true); + public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"]); - public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_NL"]); + public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_XS"], true); // IQ3 and lower require imatrix //public static readonly BaselineQuants IQ3_M = new(7, true, ["IQ3_M"], true); diff --git a/MagicQuant/Models/HybridBuild.cs b/MagicQuant/Models/HybridBuild.cs deleted file mode 100644 index 849cbeb..0000000 --- a/MagicQuant/Models/HybridBuild.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace MagicQuant.Models; - -public class HybridTensor -{ - public TensorGroup TensorGroup { get; set; } = null!; - public string TensorType { get; set; } -} - -public class HybridBuild -{ - public string Base { get; set; } - public List? Tensors { get; set; } -} \ No newline at end of file diff --git a/MagicQuant/Models/HybridQuant.cs b/MagicQuant/Models/HybridQuant.cs new file mode 100644 index 0000000..7742a6c --- /dev/null +++ b/MagicQuant/Models/HybridQuant.cs @@ -0,0 +1,77 @@ +namespace MagicQuant.Models; + +public class HybridQuant +{ + public BaselineQuants BaseQuant { get; set; } = default!; + public List Tensors { get; set; } = new List(); + + // Converting constructor: TensorConfig -> HybridQuant + public HybridQuant(TensorConfig c) + { + // If you don’t like LINQ here, swap to dictionary/array maps. + BaseQuant = BaselineQuants.All.First(b => b.UniqueId == c.BaseQuant); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.Embeddings, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.Embeddings) + }); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.LmHead, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.LmHead) + }); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.AttnQ, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.AttnQ) + }); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.AttnKV, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.AttnKV) + }); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.AttnOutput, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.AttnOutput) + }); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.FfnUpGate, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.FfnUpGate) + }); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.FfnDown, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.FfnDown) + }); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.MoeExperts, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.MoeExperts) + }); + + Tensors.Add(new HybridTensor() + { + TGroup = TReg.MoeRouter, + TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.MoeRouter) + }); + } + + // Conversion operator: TensorConfig -> HybridQuant + public static explicit operator HybridQuant(TensorConfig c) => new HybridQuant(c); +} + +public class HybridTensor +{ + public TensorGroup TGroup { get; set; } = null!; + public TensorWeightScheme TensorType { get; set; } +} \ No newline at end of file diff --git a/MagicQuant/Models/TensorConfigs.cs b/MagicQuant/Models/TensorConfigs.cs index 069e9a0..973ecb3 100644 --- a/MagicQuant/Models/TensorConfigs.cs +++ b/MagicQuant/Models/TensorConfigs.cs @@ -1,3 +1,5 @@ +using System; +using System.Linq; using System.Runtime.InteropServices; namespace MagicQuant.Models; @@ -5,6 +7,7 @@ namespace MagicQuant.Models; [StructLayout(LayoutKind.Sequential, Pack = 1)] public readonly struct TensorConfig { + public readonly sbyte BaseQuant; public readonly sbyte Embeddings; public readonly sbyte LmHead; public readonly sbyte AttnQ; @@ -16,6 +19,7 @@ public readonly struct TensorConfig public readonly sbyte MoeRouter; public TensorConfig( + sbyte baseQuant, sbyte embeddings, sbyte lmHead, sbyte attnQ, @@ -26,6 +30,7 @@ public TensorConfig( sbyte moeExperts, sbyte moeRouter) { + BaseQuant = baseQuant; Embeddings = embeddings; LmHead = lmHead; AttnQ = attnQ; @@ -36,18 +41,55 @@ public TensorConfig( MoeExperts = moeExperts; MoeRouter = moeRouter; } - - public sbyte GetValue(in TensorGroup group) => group.UniqueId switch + + // Converting constructor: HybridQuant -> TensorConfig + public TensorConfig(HybridQuant h) + : this( + baseQuant: checked((sbyte)h.BaseQuant.UniqueId), + embeddings: GetSchemeId(h, TReg.Embeddings), + lmHead: GetSchemeId(h, TReg.LmHead), + attnQ: GetSchemeId(h, TReg.AttnQ), + attnKV: GetSchemeId(h, TReg.AttnKV), + attnOutput: GetSchemeId(h, TReg.AttnOutput), + ffnUpGate: GetSchemeId(h, TReg.FfnUpGate), + ffnDown: GetSchemeId(h, TReg.FfnDown), + moeExperts: GetSchemeId(h, TReg.MoeExperts), + moeRouter: GetSchemeId(h, TReg.MoeRouter)) + { } + + private static sbyte GetSchemeId(HybridQuant h, TensorGroup group) { - 0 => Embeddings, - 1 => LmHead, - 2 => AttnQ, - 3 => AttnKV, - 4 => AttnOutput, - 5 => FfnUpGate, - 6 => FfnDown, - 7 => MoeExperts, - 8 => MoeRouter, - _ => throw new ArgumentOutOfRangeException(nameof(group)) - }; + if (h.Tensors == null) + throw new ArgumentNullException(nameof(h.Tensors)); + + TensorWeightScheme? found = null; + + // Single pass: find the tensor type for the requested group + for (int i = 0; i < h.Tensors.Count; i++) + { + var t = h.Tensors[i]; + if (t?.TGroup == null) + continue; + + if (t.TGroup.UniqueId != group.UniqueId) + continue; + + if (found != null) + throw new InvalidOperationException( + $"HybridQuant contains duplicate entries for group '{group.Name}' (UniqueId={group.UniqueId})."); + + found = t.TensorType; + } + + if (found == null) + throw new InvalidOperationException( + $"HybridQuant missing tensor entry for group '{group.Name}' (UniqueId={group.UniqueId})."); + + return checked((sbyte)found.UniqueId); + } + + // Conversion operator: HybridQuant -> TensorConfig + public static explicit operator TensorConfig(HybridQuant h) => new TensorConfig(h); } + + diff --git a/MagicQuant/Models/TensorWeightScheme.cs b/MagicQuant/Models/TensorWeightScheme.cs index 2241ffc..44231c7 100644 --- a/MagicQuant/Models/TensorWeightScheme.cs +++ b/MagicQuant/Models/TensorWeightScheme.cs @@ -60,22 +60,24 @@ public record TensorWeightScheme( [TReg.MoeRouter] ); - public static readonly TensorWeightScheme IQ4_NL = + public static readonly TensorWeightScheme IQ4_XS = new( 6, false, - ["IQ4_NL"], + ["IQ4_XS"], [TReg.MoeRouter] ); - - public static readonly TensorWeightScheme IQ4_XS = + + public static readonly TensorWeightScheme IQ4_NL = new( 7, false, - ["IQ4_XS"], + ["IQ4_NL"], [TReg.MoeRouter] ); + + // IQ3 levels public static readonly TensorWeightScheme IQ3_S = new( @@ -162,8 +164,9 @@ public record TensorWeightScheme( Q8_0, Q6_K, Q5_K, - IQ4_NL, + IQ4_XS, + //IQ4_NL, /*IQ3_S, IQ3_XS, IQ3_XXS, diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 907bfba..32bf5c6 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1,11 +1,8 @@ +using MagicQuant.Models; +using Spectre.Console; using System.Collections.Concurrent; using System.Diagnostics; using System.Runtime.InteropServices; -using System.Text.Json; -using MagicQuant; -using MagicQuant.Helpers; -using MagicQuant.Models; -using Spectre.Console; namespace MagicQuant.Services; @@ -14,12 +11,12 @@ public class QuantizationService private readonly BenchmarkService _benchmarker; private readonly string _ggufDir; private readonly string _benchDir; - + // Threading Control // We limit CPU-heavy quantization jobs to (TotalThreads / 8) to avoid choking the system // while leaving room for the GPU-heavy Perplexity tasks. - private readonly SemaphoreSlim _cpuQuantLock; - + private readonly SemaphoreSlim _cpuQuantLock; + // The Queue private readonly ConcurrentQueue> _jobQueue = new(); private bool _isQueueRunning = false; @@ -27,9 +24,9 @@ public class QuantizationService public QuantizationService(BenchmarkService benchmarker) { _benchmarker = benchmarker; - + // Setup Directories based on Cache (assumed populated by Evolution command) - if (Cache.MagicQuantDirectory == null) + if (Cache.MagicQuantDirectory == null) throw new Exception("MagicQuant Directory not set. Run initialization first."); _ggufDir = Path.Combine(Cache.MagicQuantDirectory, "GGUF"); @@ -38,7 +35,7 @@ public QuantizationService(BenchmarkService benchmarker) Directory.CreateDirectory(_ggufDir); Directory.CreateDirectory(_benchDir); - // Limit concurrent quantizations. + // Limit concurrent quantizations. // Example: 32 threads -> 4 concurrent quants (leaving threads for PPL) int maxConcurrent = Math.Max(1, (Cache.SysInfo?.ThreadCount ?? 4) / 8); _cpuQuantLock = new SemaphoreSlim(maxConcurrent, maxConcurrent); @@ -48,9 +45,9 @@ public QuantizationService(BenchmarkService benchmarker) // 1. High-Level Entry Point: Build & Benchmark // ---------------------------------------------------------------- - public void QueueJob(HybridBuild build) + public void QueueJob(HybridQuant quant) { - _jobQueue.Enqueue(async () => await ProcessHybridBuildAsync(build)); + _jobQueue.Enqueue(async () => await ProcessHybridQuantAsync(quant)); StartQueueProcessor(); } @@ -66,11 +63,12 @@ private void StartQueueProcessor() { await job(); } + _isQueueRunning = false; }); } - private async Task ProcessHybridBuildAsync(HybridBuild build) + private async Task ProcessHybridQuantAsync(HybridQuant quant) { try { @@ -78,9 +76,9 @@ private async Task ProcessHybridBuildAsync(HybridBuild build) string bf16Path = await EnsureBf16ModelAsync(); // 2. Determine Output Name & Path - string modelName = GenerateHybridName(build); + string modelName = GenerateHybridName(quant); string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); - + // 3. Quantize (CPU Bound - Parallel) await _cpuQuantLock.WaitAsync(); try @@ -88,7 +86,7 @@ private async Task ProcessHybridBuildAsync(HybridBuild build) if (!File.Exists(quantPath)) { AnsiConsole.MarkupLine($"[cyan]Building Hybrid Model:[/] {modelName}"); - await RunLlamaQuantizeAsync(bf16Path, quantPath, build); + await RunLlamaQuantizeAsync(bf16Path, quantPath, quant); } } finally @@ -105,22 +103,22 @@ private async Task ProcessHybridBuildAsync(HybridBuild build) if (!File.Exists(metricsPath)) { AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {modelName}"); - + // Note: LlamaBench will block everything else (ExclusiveBenchLock). // Perplexity will run in parallel with other Quant jobs if VRAM permits (VramLock). await _benchmarker.RunAllBenchmarksAsync( - quantPath, - modelBenchDir, + quantPath, + modelBenchDir, saveLogits: false // Only BF16 saves logits usually ); - + // Cleanup: Delete GGUF after benchmark to save space (per requirements) // EXCEPT if it is a base/common one we might want to keep? // Logic: "always remember to delete the hybrid or base... delete with true delete" if (File.Exists(quantPath) && !IsProtectedModel(modelName)) { AnsiConsole.MarkupLine($"[grey]Deleting temp model: {modelName}[/]"); - File.Delete(quantPath); + File.Delete(quantPath); } } } @@ -145,7 +143,7 @@ public async Task EnsureBf16ModelAsync() // Name usually: -BF16.gguf // We get ModelName from Cache.ModelDirectory string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; - + // Detect Torch Type from Cache (as you requested) or default to BF16 string typeSuffix = Cache.SysInfo != null ? "BF16" : "F16"; // Simplification // Real logic: Check Cache.TorchType (e.g. "BF16", "F16", "F32") @@ -155,16 +153,16 @@ public async Task EnsureBf16ModelAsync() string output = Path.Combine(_ggufDir, fileName); string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); - if (File.Exists(output) && File.Exists(successFile)) + if (File.Exists(output) && File.Exists(successFile)) return output; // Create/Convert AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeSuffix}...[/]"); - + // Clean partials if (File.Exists(output)) File.Delete(output); - string convertScript = Cache.ConvertScript + string convertScript = Cache.ConvertScript ?? throw new Exception("ConvertScript path missing in Cache"); // Command: python convert_hf_to_gguf.py path --outtype bf16 --outfile output @@ -179,9 +177,10 @@ public async Task EnsureBf16ModelAsync() }; using var p = Process.Start(psi); - p.OutputDataReceived += (s, e) => { if(e.Data != null) AnsiConsole.WriteLine(e.Data); }; - p.ErrorDataReceived += (s, e) => { if(e.Data != null) AnsiConsole.WriteLine(e.Data); }; // Errors often printed to stderr - p.BeginOutputReadLine(); p.BeginErrorReadLine(); + p.OutputDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.WriteLine(e.Data); }; + p.ErrorDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.WriteLine(e.Data); }; + p.BeginOutputReadLine(); + p.BeginErrorReadLine(); await p.WaitForExitAsync(); if (p.ExitCode != 0) throw new Exception("BF16 Conversion Failed"); @@ -191,14 +190,15 @@ public async Task EnsureBf16ModelAsync() // Run Benchmark on BF16 (Critical First Step) string benchPath = Path.Combine(_benchDir, "BF16"); + // We need to save logits for the base model so others can calculate KLD string logitsDir = Path.Combine(benchPath, "logits"); - + AnsiConsole.MarkupLine("[bold yellow]Benchmarking Base BF16 (Saving Logits)...[/]"); await _benchmarker.RunAllBenchmarksAsync( - output, - benchPath, - klLogitsDir: logitsDir, + output, + benchPath, + klLogitsDir: logitsDir, saveLogits: true ); @@ -209,40 +209,49 @@ await _benchmarker.RunAllBenchmarksAsync( // 3. Hybrid Quantization Execution // ---------------------------------------------------------------- - public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridBuild build) + public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) { - // 1. Base Arguments - // llama-quantize [flags] input output base_type - var args = new List(); + // llama-quantize [flags] input output base_type threads + var args = new List(capacity: 64); - // 2. Hybrid Overrides (The Magic) - // Modern llama-quantize supports --tensor-type = - if (build.Tensors != null) + // 1) Hybrid overrides + // llama-quantize supports: --tensor-type "=" + // We emit one flag per tensor pattern per group. + if (quant.Tensors is { Count: > 0 }) { - foreach (var hybrid in build.Tensors) + foreach (var hybrid in quant.Tensors) { - foreach (var tensorPattern in hybrid.TensorGroup.Tensors) + // Skip null guard (shouldn't happen, but keep robust) + if (hybrid?.TGroup == null) + continue; + + // Resolve actual llama quant name for this scheme + // (handles BF16/F16 shared ID) + string schemeName = ResolveSchemeName(hybrid.TensorType); + + foreach (var tensorPattern in hybrid.TGroup.Tensors) { - // Convert glob-like patterns to what llama-quantize accepts if needed - // Usually it accepts substrings or regex. - // We append: --tensor-type pattern=type - args.Add($"--tensor-type \"{tensorPattern}={hybrid.TensorType}\""); + args.Add($"--tensor-type \"{tensorPattern}={schemeName}\""); } } } - // 3. Files and Base Type + // 2) Files + base type + threads args.Add($"\"{inputFile}\""); args.Add($"\"{outputFile}\""); - args.Add(build.Base); - // 4. Threads (8 per quant job as requested) + // Base type comes from the BaselineQuants record + args.Add(ResolveBaseName(quant.BaseQuant)); + + // Threads (8 per quant job as requested) args.Add("8"); string arguments = string.Join(" ", args); - string bin = Cache.LlamaBin + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "/llama-quantize.exe" : "/llama-quantize"); - - // Execute + + string bin = Cache.LlamaBin + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? "/llama-quantize.exe" + : "/llama-quantize"); + var psi = new ProcessStartInfo { FileName = bin, @@ -254,54 +263,92 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb }; using var p = Process.Start(psi); - // We might not want to spam console with quantization logs unless verbose - p.BeginOutputReadLine(); p.BeginErrorReadLine(); + if (p == null) + throw new InvalidOperationException($"Failed to start process: {bin}"); + + // If you want logging, attach handlers like you do elsewhere. + p.BeginOutputReadLine(); + p.BeginErrorReadLine(); await p.WaitForExitAsync(); - if (p.ExitCode != 0) throw new Exception($"Quantization failed for {outputFile}"); + if (p.ExitCode != 0) + throw new Exception($"Quantization failed for {outputFile}"); + } + + private static string ResolveBaseName(BaselineQuants b) + { + if (b.Names.IsDefaultOrEmpty) + throw new InvalidOperationException($"BaselineQuants '{b.UniqueId}' has no Names."); + + // Most bases only have a single name. + // If you ever add aliases, this keeps it deterministic. + return b.Names[0]; + } + + private static string ResolveSchemeName(TensorWeightScheme s) + { + if (s.Names.IsDefaultOrEmpty) + throw new InvalidOperationException($"TensorWeightScheme '{s.UniqueId}' has no Names."); + + // Special case: BF16_F16 shares UniqueId and has two names ["BF16","F16"]. + // Pick based on runtime float type when available. + if (s.UniqueId == TensorWeightScheme.BF16_F16.UniqueId && s.Names.Length >= 2) + { + // Plug in your real logic here (Cache.TorchType etc.) + // For now, default BF16 if unknown. + // Example expected values: "BF16", "F16", "F32" + var torch = Cache.TorchType; // if you have it; otherwise this can be null + if (string.Equals(torch, "F16", StringComparison.OrdinalIgnoreCase)) + return "F16"; + + return "BF16"; + } + + // Normal case: first name is canonical + return s.Names[0]; } // ---------------------------------------------------------------- // 4. Naming Scheme Logic (E-H-Q-K-O...) // ---------------------------------------------------------------- - public string GenerateHybridName(HybridBuild build) + public string GenerateHybridName(HybridQuant quant) { string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; - + + string baseName = ResolveBaseName(quant.BaseQuant); + // If pure baseline (no tensors), just - - if (build.Tensors == null || build.Tensors.Count == 0) + if (quant.Tensors == null || quant.Tensors.Count == 0) { - return $"{modelName}-{build.Base}"; + return $"{modelName}-{baseName}"; } - // Hybrid Logic - // 1. Group by Quant Type - var grouped = build.Tensors - .GroupBy(t => t.TensorType) - .Select(g => new - { - Type = g.Key, - // Get Sortable ShortCodes (E, H, Q, K...) - Codes = g.Select(x => x.TensorGroup.ShortCode).OrderBy(c => GetOrder(c)).ToArray() + // Group by quant scheme (resolved to a stable string) + var grouped = quant.Tensors + .GroupBy(t => ResolveSchemeName(t.TensorType)) + .Select(g => new + { + Type = g.Key, + Codes = g.Select(x => x.TGroup.ShortCode) + .OrderBy(c => GetOrder(c)) + .ToArray() }) .OrderBy(x => GetOrder(x.Codes.FirstOrDefault())) .ToList(); - var nameParts = new List(); - + var nameParts = new List(capacity: grouped.Count); + foreach (var group in grouped) { - string codeStr = new string(group.Codes); // e.g., "EH" or "QKO" - // Remove underscores from quant type for cleanliness (Q4_K_M -> Q4KM) if desired - // The prompt says "Q6K", "B16". Let's stick to simple mapping. - string quantStr = SimplifyQuant(group.Type); - + string codeStr = new string(group.Codes); // e.g., "EH" or "QKO" + string quantStr = SimplifyQuant(group.Type); // e.g., "Q6K", "B16", "IQ4XS" + nameParts.Add($"{codeStr}-{quantStr}"); } string suffix = string.Join("-", nameParts); - return $"{modelName}-{build.Base}-{suffix}"; + return $"{modelName}-{baseName}-{suffix}"; } private int GetOrder(char c) @@ -316,4 +363,4 @@ private string SimplifyQuant(string quant) // BF16 -> B16, Q4_K_M -> Q4KM return quant.Replace("_", "").Replace("BF16", "B16").Replace("F16", "F16"); } -} \ No newline at end of file +} From 962b814bf66a737f9b97b9fd8d6883749900b93e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 21 Dec 2025 13:35:52 -0500 Subject: [PATCH 015/258] initial setup looking good --- MagicQuant/Cache.cs | 9 +- MagicQuant/Commands/Evolution.cs | 2 + MagicQuant/Helpers/JsonHelper.cs | 104 +++++++++++++++++++++ MagicQuant/Services/QuantizationService.cs | 101 +++++++------------- 4 files changed, 148 insertions(+), 68 deletions(-) create mode 100644 MagicQuant/Helpers/JsonHelper.cs diff --git a/MagicQuant/Cache.cs b/MagicQuant/Cache.cs index 630e228..7f8d428 100644 --- a/MagicQuant/Cache.cs +++ b/MagicQuant/Cache.cs @@ -39,5 +39,12 @@ public class Cache /// /// Aka BF16, F16, or F32 /// - public static string? TorchType; + public static MainTorchType? TorchType; + + public enum MainTorchType + { + BF16 = 1, + F16 = 2, + F32 = 3 + } } \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 2167e14..cde8da0 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -54,6 +54,8 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.MagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); + // Create the MagicQuant directory immediately so it's ready for future steps if (!Directory.Exists(Cache.MagicQuantDirectory)) { diff --git a/MagicQuant/Helpers/JsonHelper.cs b/MagicQuant/Helpers/JsonHelper.cs new file mode 100644 index 0000000..584660a --- /dev/null +++ b/MagicQuant/Helpers/JsonHelper.cs @@ -0,0 +1,104 @@ +using System.Text.Json; +using Spectre.Console; + +namespace MagicQuant.Helpers; + +public static class JsonHelper +{ + // Priority list of keys to look for + private static readonly List DtypeKeys = new() + { + "torch_dtype", + "dtype", + "prec", + "precision" + }; + + public static void DetectAndSetTorchType(string modelDir) + { + string configPath = Path.Combine(modelDir, "config.json"); + + if (!File.Exists(configPath)) + { + throw new FileNotFoundException($"Could not find 'config.json' in {modelDir}"); + } + + try + { + string jsonContent = File.ReadAllText(configPath); + using JsonDocument doc = JsonDocument.Parse(jsonContent); + + // Recursive search for the key + string? dtypeValue = FindKeyRecursive(doc.RootElement, DtypeKeys); + + if (string.IsNullOrWhiteSpace(dtypeValue)) + { + AnsiConsole.MarkupLine("[yellow]Warning:[/] Could not find 'torch_dtype' in config.json. Defaulting to [bold]BF16[/]."); + Cache.TorchType = Cache.MainTorchType.BF16; + return; + } + + // Parse the value + Cache.TorchType = ParseTorchType(dtypeValue); + AnsiConsole.MarkupLine($"[grey]Detected Model Type:[/] [cyan]{Cache.TorchType}[/] (from '{dtypeValue}')"); + } + catch (Exception ex) + { + AnsiConsole.MarkupLine($"[red]Error parsing config.json:[/] {ex.Message}"); + // Fail safe or throw depending on strictness. + // Usually safe to default if we assume modern models. + Cache.TorchType = Cache.MainTorchType.BF16; + } + } + + private static string? FindKeyRecursive(JsonElement element, List targetKeys) + { + if (element.ValueKind == JsonValueKind.Object) + { + // 1. Check current level first (Optimization) + foreach (var prop in element.EnumerateObject()) + { + if (targetKeys.Contains(prop.Name, StringComparer.OrdinalIgnoreCase) && + prop.Value.ValueKind == JsonValueKind.String) + { + return prop.Value.GetString(); + } + } + + // 2. Recurse into children + foreach (var prop in element.EnumerateObject()) + { + // Skip if not object or array to save time + if (prop.Value.ValueKind == JsonValueKind.Object || prop.Value.ValueKind == JsonValueKind.Array) + { + string? found = FindKeyRecursive(prop.Value, targetKeys); + if (found != null) return found; + } + } + } + else if (element.ValueKind == JsonValueKind.Array) + { + foreach (var item in element.EnumerateArray()) + { + string? found = FindKeyRecursive(item, targetKeys); + if (found != null) return found; + } + } + + return null; + } + + private static Cache.MainTorchType ParseTorchType(string value) + { + // Normalize + string v = value.ToLowerInvariant().Trim(); + + return v switch + { + "bfloat16" => Cache.MainTorchType.BF16, + "float16" => Cache.MainTorchType.F16, + "float32" => Cache.MainTorchType.F32, + _ => Cache.MainTorchType.BF16 // Default fallback + }; + } +} \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 32bf5c6..8e8ca1d 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -72,8 +72,8 @@ private async Task ProcessHybridQuantAsync(HybridQuant quant) { try { - // 1. Ensure BF16 Base Exists (Prerequisite) - string bf16Path = await EnsureBf16ModelAsync(); + // 1. Ensure Base Model Exists (Dynamic BF16/F16/F32) + string basePath = await EnsureBaseModelAsync(); // 2. Determine Output Name & Path string modelName = GenerateHybridName(quant); @@ -86,7 +86,7 @@ private async Task ProcessHybridQuantAsync(HybridQuant quant) if (!File.Exists(quantPath)) { AnsiConsole.MarkupLine($"[cyan]Building Hybrid Model:[/] {modelName}"); - await RunLlamaQuantizeAsync(bf16Path, quantPath, quant); + await RunLlamaQuantizeAsync(basePath, quantPath, quant); } } finally @@ -95,8 +95,6 @@ private async Task ProcessHybridQuantAsync(HybridQuant quant) } // 4. Benchmark (Mixed CPU/GPU/Exclusive) - // The BenchmarkService handles its own locking (Exclusive vs VRAM) - // so we can just call it here. string modelBenchDir = Path.Combine(_benchDir, modelName); string metricsPath = Path.Combine(modelBenchDir, "bench_metrics.json"); @@ -104,17 +102,13 @@ private async Task ProcessHybridQuantAsync(HybridQuant quant) { AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {modelName}"); - // Note: LlamaBench will block everything else (ExclusiveBenchLock). - // Perplexity will run in parallel with other Quant jobs if VRAM permits (VramLock). await _benchmarker.RunAllBenchmarksAsync( quantPath, modelBenchDir, - saveLogits: false // Only BF16 saves logits usually + saveLogits: false // Only base models save logits ); - // Cleanup: Delete GGUF after benchmark to save space (per requirements) - // EXCEPT if it is a base/common one we might want to keep? - // Logic: "always remember to delete the hybrid or base... delete with true delete" + // Cleanup: Delete GGUF after benchmark (unless protected base) if (File.Exists(quantPath) && !IsProtectedModel(modelName)) { AnsiConsole.MarkupLine($"[grey]Deleting temp model: {modelName}[/]"); @@ -127,7 +121,7 @@ await _benchmarker.RunAllBenchmarksAsync( AnsiConsole.WriteException(ex); } } - + private bool IsProtectedModel(string name) { // Don't delete the BF16/F16/F32 base files @@ -135,21 +129,19 @@ private bool IsProtectedModel(string name) } // ---------------------------------------------------------------- - // 2. BF16 Base Generation (The "Root" Model) + // 2. Base Model Generation (Dynamic BF16 / F16 / F32) // ---------------------------------------------------------------- - public async Task EnsureBf16ModelAsync() + public async Task EnsureBaseModelAsync() { - // Name usually: -BF16.gguf - // We get ModelName from Cache.ModelDirectory string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; - // Detect Torch Type from Cache (as you requested) or default to BF16 - string typeSuffix = Cache.SysInfo != null ? "BF16" : "F16"; // Simplification - // Real logic: Check Cache.TorchType (e.g. "BF16", "F16", "F32") - // For this snippet, I assume "BF16" is the target per your prompt. + // Dynamic Type Detection + // Default to BF16 if detection failed or wasn't run + var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; + string typeStr = torchType.ToString(); // "BF16", "F16", "F32" - string fileName = $"{modelName}-BF16.gguf"; + string fileName = $"{modelName}-{typeStr}.gguf"; string output = Path.Combine(_ggufDir, fileName); string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); @@ -157,19 +149,20 @@ public async Task EnsureBf16ModelAsync() return output; // Create/Convert - AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeSuffix}...[/]"); + AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeStr}...[/]"); - // Clean partials if (File.Exists(output)) File.Delete(output); string convertScript = Cache.ConvertScript ?? throw new Exception("ConvertScript path missing in Cache"); - // Command: python convert_hf_to_gguf.py path --outtype bf16 --outfile output + // map enum to CLI arg: BF16 -> bf16, F16 -> f16, F32 -> f32 + string outTypeArg = typeStr.ToLowerInvariant(); + var psi = new ProcessStartInfo { - FileName = "python", // Or _pyManager.GetPythonExecutable() - Arguments = $"\"{convertScript}\" \"{Cache.ModelDirectory}\" --outtype bf16 --outfile \"{output}\"", + FileName = "python", + Arguments = $"\"{convertScript}\" \"{Cache.ModelDirectory}\" --outtype {outTypeArg} --outfile \"{output}\"", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, @@ -183,18 +176,16 @@ public async Task EnsureBf16ModelAsync() p.BeginErrorReadLine(); await p.WaitForExitAsync(); - if (p.ExitCode != 0) throw new Exception("BF16 Conversion Failed"); + if (p.ExitCode != 0) throw new Exception($"{typeStr} Conversion Failed"); // Write Success JSON await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); - // Run Benchmark on BF16 (Critical First Step) - string benchPath = Path.Combine(_benchDir, "BF16"); - - // We need to save logits for the base model so others can calculate KLD + // Run Benchmark on Base Model (Critical First Step) + string benchPath = Path.Combine(_benchDir, typeStr); // e.g., Benchmarks/BF16 string logitsDir = Path.Combine(benchPath, "logits"); - AnsiConsole.MarkupLine("[bold yellow]Benchmarking Base BF16 (Saving Logits)...[/]"); + AnsiConsole.MarkupLine($"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]"); await _benchmarker.RunAllBenchmarksAsync( output, benchPath, @@ -209,24 +200,17 @@ await _benchmarker.RunAllBenchmarksAsync( // 3. Hybrid Quantization Execution // ---------------------------------------------------------------- - public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) + public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) { - // llama-quantize [flags] input output base_type threads var args = new List(capacity: 64); - // 1) Hybrid overrides - // llama-quantize supports: --tensor-type "=" - // We emit one flag per tensor pattern per group. if (quant.Tensors is { Count: > 0 }) { foreach (var hybrid in quant.Tensors) { - // Skip null guard (shouldn't happen, but keep robust) - if (hybrid?.TGroup == null) - continue; + if (hybrid?.TGroup == null) continue; - // Resolve actual llama quant name for this scheme - // (handles BF16/F16 shared ID) + // Resolve scheme dynamically (handles BF16/F16 shared ID) string schemeName = ResolveSchemeName(hybrid.TensorType); foreach (var tensorPattern in hybrid.TGroup.Tensors) @@ -236,14 +220,9 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb } } - // 2) Files + base type + threads args.Add($"\"{inputFile}\""); args.Add($"\"{outputFile}\""); - - // Base type comes from the BaselineQuants record args.Add(ResolveBaseName(quant.BaseQuant)); - - // Threads (8 per quant job as requested) args.Add("8"); string arguments = string.Join(" ", args); @@ -263,10 +242,8 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb }; using var p = Process.Start(psi); - if (p == null) - throw new InvalidOperationException($"Failed to start process: {bin}"); + if (p == null) throw new InvalidOperationException($"Failed to start process: {bin}"); - // If you want logging, attach handlers like you do elsewhere. p.BeginOutputReadLine(); p.BeginErrorReadLine(); await p.WaitForExitAsync(); @@ -279,9 +256,6 @@ private static string ResolveBaseName(BaselineQuants b) { if (b.Names.IsDefaultOrEmpty) throw new InvalidOperationException($"BaselineQuants '{b.UniqueId}' has no Names."); - - // Most bases only have a single name. - // If you ever add aliases, this keeps it deterministic. return b.Names[0]; } @@ -291,20 +265,17 @@ private static string ResolveSchemeName(TensorWeightScheme s) throw new InvalidOperationException($"TensorWeightScheme '{s.UniqueId}' has no Names."); // Special case: BF16_F16 shares UniqueId and has two names ["BF16","F16"]. - // Pick based on runtime float type when available. if (s.UniqueId == TensorWeightScheme.BF16_F16.UniqueId && s.Names.Length >= 2) { - // Plug in your real logic here (Cache.TorchType etc.) - // For now, default BF16 if unknown. - // Example expected values: "BF16", "F16", "F32" - var torch = Cache.TorchType; // if you have it; otherwise this can be null - if (string.Equals(torch, "F16", StringComparison.OrdinalIgnoreCase)) + // Dynamic check against Cache + if (Cache.TorchType == Cache.MainTorchType.F16) + { return "F16"; - + } + // Default to BF16 for BF16 or F32 types (safer modern default) return "BF16"; } - // Normal case: first name is canonical return s.Names[0]; } @@ -315,16 +286,13 @@ private static string ResolveSchemeName(TensorWeightScheme s) public string GenerateHybridName(HybridQuant quant) { string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; - string baseName = ResolveBaseName(quant.BaseQuant); - // If pure baseline (no tensors), just - if (quant.Tensors == null || quant.Tensors.Count == 0) { return $"{modelName}-{baseName}"; } - // Group by quant scheme (resolved to a stable string) var grouped = quant.Tensors .GroupBy(t => ResolveSchemeName(t.TensorType)) .Select(g => new @@ -341,9 +309,8 @@ public string GenerateHybridName(HybridQuant quant) foreach (var group in grouped) { - string codeStr = new string(group.Codes); // e.g., "EH" or "QKO" - string quantStr = SimplifyQuant(group.Type); // e.g., "Q6K", "B16", "IQ4XS" - + string codeStr = new string(group.Codes); + string quantStr = SimplifyQuant(group.Type); nameParts.Add($"{codeStr}-{quantStr}"); } From 2d1666a892c734f51c68f6dce13e80d9b32f9e27 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 21 Dec 2025 15:08:44 -0500 Subject: [PATCH 016/258] initial automated data samples generation code added. --- MagicQuant/Helpers/TensorConfigGenerator.cs | 107 ++++++++++++++++++-- MagicQuant/Models/BaselineQuants.cs | 5 + MagicQuant/Models/HybridQuant.cs | 3 +- MagicQuant/Models/TensorWeightScheme.cs | 2 + MagicQuant/Program.cs | 16 +++ 5 files changed, 124 insertions(+), 9 deletions(-) diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 53e2e7b..dcd3d13 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -1,11 +1,102 @@ using MagicQuant.Models; using System.Collections.Concurrent; using System.Collections.Immutable; +using Spectre.Console; namespace MagicQuant.Helpers; public static class TensorConfigGenerator { + public static List GenerateRequiredDataSampleCombos(List? MissingTensorGroup = null) + { + var allowedBaselines = BaselineQuants.All.Where(x => x.AllowedAsBaseConversion).ToList(); + var hybridQuants = new List(); + + // Fast lookup for missing groups + var missingIds = MissingTensorGroup?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + + // --------------------------------------------------------- + // 1. BASELINE CONTROLS (One pure sample per allowed baseline) + // --------------------------------------------------------- + int baseTestsRequired = 0; + foreach (var baseline in allowedBaselines) + { + baseTestsRequired++; + var hq = new HybridQuant + { + BaseQuant = baseline, + Tensors = TReg.All + .Select(g => new HybridTensor + { + TGroup = g, + // If missing, mark NULL. Else default to BF16. + TensorType = missingIds.Contains(g.UniqueId) + ? TensorWeightScheme.NULL + : TensorWeightScheme.BF16_F16 + }) + .ToList() + }; + + hybridQuants.Add(hq); + } + + AnsiConsole.MarkupLine($"[bold green]Required BF16 base hybrid tests:[/] {baseTestsRequired:N0}"); + + // --------------------------------------------------------- + // 2. ISOLATION SAMPLES (Always BF16 Base, isolate one tensor at a time) + // --------------------------------------------------------- + var tensorWeights = TensorWeightScheme.All + .Where(x => x != TensorWeightScheme.NULL && x != TensorWeightScheme.BF16_F16) + .ToList(); + + int isolatedSamplesRequired = 0; + + // We always use the BF16 baseline for isolation tests + var isolationBase = BaselineQuants.GetBF16Quant(); + + foreach (var weight in tensorWeights) + { + // Get valid targets: Start with All, remove Banned by Scheme, remove Missing by User + var validTargets = TReg.All.Where(x => !weight.BannedGroups.Contains(x)).ToList(); + + if (missingIds.Count > 0) + { + validTargets.RemoveAll(x => missingIds.Contains(x.UniqueId)); + } + + foreach (var group in validTargets) + { + isolatedSamplesRequired++; + + // Create fresh list with default logic + var tensors = TReg.All.Select(g => new HybridTensor + { + TGroup = g, + TensorType = missingIds.Contains(g.UniqueId) + ? TensorWeightScheme.NULL + : TensorWeightScheme.BF16_F16 + }).ToList(); + + // Set the isolated target + var foundQuant = tensors.First(x => x.TGroup == group); + foundQuant.TensorType = weight; + + var hq = new HybridQuant + { + BaseQuant = isolationBase, + Tensors = tensors + }; + + hybridQuants.Add(hq); + } + } + + AnsiConsole.MarkupLine($"[bold green]Isolated Samples Required:[/] {isolatedSamplesRequired:N0}"); + AnsiConsole.MarkupLine($"[bold green]Total Samples Required:[/] {hybridQuants.Count:N0}"); + + return hybridQuants; + } + public static IEnumerable> GenerateTensorConfigBatches( BaselineQuants baseQuant, int batchSize = 10_000_000, @@ -95,16 +186,16 @@ public static IEnumerable> GenerateTensorConfigBatches( { // Inline-build (perf): avoids helper call overhead and repeated bounds checks batch.Add(new TensorConfig( - baseQuant: baseId, + baseQuant: baseId, embeddings: d0[idx[0]], - lmHead: d1[idx[1]], - attnQ: d2[idx[2]], - attnKV: d3[idx[3]], + lmHead: d1[idx[1]], + attnQ: d2[idx[2]], + attnKV: d3[idx[3]], attnOutput: d4[idx[4]], - ffnUpGate: d5[idx[5]], - ffnDown: d6[idx[6]], + ffnUpGate: d5[idx[5]], + ffnDown: d6[idx[6]], moeExperts: d7[idx[7]], - moeRouter: d8[idx[8]] + moeRouter: d8[idx[8]] )); if (batch.Count >= batchSize) @@ -169,4 +260,4 @@ private static int ComputeWorkerThreads(int threadCount) // Always leave at least 1 thread free return Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); } -} +} \ No newline at end of file diff --git a/MagicQuant/Models/BaselineQuants.cs b/MagicQuant/Models/BaselineQuants.cs index 9e2b67f..51d315b 100644 --- a/MagicQuant/Models/BaselineQuants.cs +++ b/MagicQuant/Models/BaselineQuants.cs @@ -18,6 +18,11 @@ public record BaselineQuants( public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_XS"], true); + + public static BaselineQuants GetBF16Quant() + { + return new(0, false, [Cache.TorchType?.ToString() ?? "BF16"]); + } // IQ3 and lower require imatrix //public static readonly BaselineQuants IQ3_M = new(7, true, ["IQ3_M"], true); //public static readonly BaselineQuants IQ2_M = new(8, true, ["IQ2_M"], true); diff --git a/MagicQuant/Models/HybridQuant.cs b/MagicQuant/Models/HybridQuant.cs index 7742a6c..a60d2b6 100644 --- a/MagicQuant/Models/HybridQuant.cs +++ b/MagicQuant/Models/HybridQuant.cs @@ -4,7 +4,8 @@ public class HybridQuant { public BaselineQuants BaseQuant { get; set; } = default!; public List Tensors { get; set; } = new List(); - + public HybridQuant() { } + // Converting constructor: TensorConfig -> HybridQuant public HybridQuant(TensorConfig c) { diff --git a/MagicQuant/Models/TensorWeightScheme.cs b/MagicQuant/Models/TensorWeightScheme.cs index 44231c7..6660a3e 100644 --- a/MagicQuant/Models/TensorWeightScheme.cs +++ b/MagicQuant/Models/TensorWeightScheme.cs @@ -68,6 +68,7 @@ public record TensorWeightScheme( [TReg.MoeRouter] ); + /* public static readonly TensorWeightScheme IQ4_NL = new( 7, @@ -155,6 +156,7 @@ public record TensorWeightScheme( TReg.AttnKV ] ); + */ public static readonly ImmutableArray All = [ diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index f957dcf..52195af 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -153,6 +153,22 @@ static void ValidateCombinationLogicWorks() AnsiConsole.MarkupLine( $"[bold]Elapsed:[/] " + $"{t.Hours}h {t.Minutes}m {t.Seconds}s {t.Milliseconds}ms"); + Console.WriteLine(); + Console.WriteLine("---------------"); + Console.WriteLine(); + + var MOE = TensorConfigGenerator.GenerateRequiredDataSampleCombos(); + + var Dense = TensorConfigGenerator.GenerateRequiredDataSampleCombos(//); + new List(){TReg.MoeRouter, TReg.MoeExperts}); + + Console.WriteLine(); + Console.WriteLine("---------------"); + Console.WriteLine(); + AnsiConsole.MarkupLine( + $"[bold green]Max MOE samples created:[/] {MOE.Count():N0}"); + AnsiConsole.MarkupLine( + $"[bold green]Max Dense samples created:[/] {Dense.Count():N0}"); } #endregion \ No newline at end of file From 0a879537e221fba458dbf444f8a4665249f76da5 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 21 Dec 2025 15:42:44 -0500 Subject: [PATCH 017/258] more changes --- MagicQuant/Helpers/TensorConfigGenerator.cs | 2 +- MagicQuant/Models/BaselineQuants.cs | 33 +++++++++++++++++---- MagicQuant/Models/TensorGroup.cs | 5 ++++ MagicQuant/Program.cs | 26 ++++++++-------- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index dcd3d13..0f78d06 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -9,7 +9,7 @@ public static class TensorConfigGenerator { public static List GenerateRequiredDataSampleCombos(List? MissingTensorGroup = null) { - var allowedBaselines = BaselineQuants.All.Where(x => x.AllowedAsBaseConversion).ToList(); + var allowedBaselines = BaselineQuants.All.Where(x => x.BaseConversionBase != null).ToList(); var hybridQuants = new List(); // Fast lookup for missing groups diff --git a/MagicQuant/Models/BaselineQuants.cs b/MagicQuant/Models/BaselineQuants.cs index 51d315b..9df8900 100644 --- a/MagicQuant/Models/BaselineQuants.cs +++ b/MagicQuant/Models/BaselineQuants.cs @@ -6,19 +6,42 @@ public record BaselineQuants( sbyte UniqueId, bool RequiresImatrix, ImmutableArray Names, - bool AllowedAsBaseConversion = false) + HybridQuant? BaseConversionBase = null) { public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"]); public static readonly BaselineQuants Q6_K = new(1, false, ["Q6_K"]); public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"]); public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"]); - public static readonly BaselineQuants MXFP4_MOE = new(4, false, ["MXFP4_MOE"], true); - public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"]); + public static readonly BaselineQuants MXFP4_MOE = new(4, false, ["MXFP4_MOE"], + new HybridQuant + { + BaseQuant = MXFP4_MOE, + Tensors = TReg.All + .Select(g => new HybridTensor + { + TGroup = g, + TensorType = TensorWeightScheme.MXFP4 + }) + .ToList() + }); - public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_XS"], true); - + + public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_XS"], + new HybridQuant + { + BaseQuant = IQ4_XS, + Tensors = TReg.All + .Select(g => new HybridTensor + { + TGroup = g, + TensorType = TensorWeightScheme.IQ4_XS + }) + .ToList() + }); + public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"]); + public static BaselineQuants GetBF16Quant() { return new(0, false, [Cache.TorchType?.ToString() ?? "BF16"]); diff --git a/MagicQuant/Models/TensorGroup.cs b/MagicQuant/Models/TensorGroup.cs index c622411..ac9acc1 100644 --- a/MagicQuant/Models/TensorGroup.cs +++ b/MagicQuant/Models/TensorGroup.cs @@ -4,6 +4,11 @@ namespace MagicQuant.Models; +public class TensorGroupInfo +{ + public TensorGroup Group { get; set; } +} + /// /// Represents a categorized group of tensors with a unique name and matching patterns. /// diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 52195af..d5ef917 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -62,7 +62,7 @@ await AnsiConsole.Status() await initializer.Run(validationArgs); }); - AnsiConsole.MarkupLine("[bold green]✓[/] Environment validated."); + AnsiConsole.MarkupLine("[bold green][/] Environment validated."); AnsiConsole.WriteLine(); } @@ -84,17 +84,17 @@ static void ValidateCombinationLogicWorks() { CliHelpers.PrintTotalCombinationCount(); -// ---------------------------------------- -// Pre-compute expected total (source of truth) -// ---------------------------------------- + // ---------------------------------------- + // Pre-compute expected total + // ---------------------------------------- var expectedTotal = ComboCounter.CountAll(); AnsiConsole.MarkupLine( $"[bold cyan]Expected total combinations:[/] [bold yellow]{expectedTotal:N0}[/]"); -// ---------------------------------------- -// Generation + timing -// ---------------------------------------- + // ---------------------------------------- + // Generation + timing + // ---------------------------------------- var sw = Stopwatch.StartNew(); long actualTotal = 0; @@ -132,9 +132,9 @@ static void ValidateCombinationLogicWorks() sw.Stop(); -// ---------------------------------------- -// Verification -// ---------------------------------------- + // ---------------------------------------- + // Verification + // ---------------------------------------- bool match = actualTotal == expectedTotal; AnsiConsole.MarkupLine( @@ -145,9 +145,9 @@ static void ValidateCombinationLogicWorks() ? "[bold green] Counts match expected total[/]" : $"[bold red] MISMATCH! Expected {expectedTotal:N0} but generated {actualTotal:N0}[/]"); -// ---------------------------------------- -// Human-readable elapsed time -// ---------------------------------------- + // ---------------------------------------- + // Human-readable elapsed time + // ---------------------------------------- var t = sw.Elapsed; AnsiConsole.MarkupLine( From 1e3b434ababdf41eaaf350d779328eb6b4a75a99 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 28 Dec 2025 11:13:09 -0500 Subject: [PATCH 018/258] Building base GGUF model --- MagicQuant/Cache.cs | 2 + MagicQuant/Commands/Evolution.cs | 14 ++- MagicQuant/Commands/InitializeLlamaCpp.cs | 1 + MagicQuant/Helpers/ComboLogic.cs | 2 +- MagicQuant/Helpers/LlamaBuilder.cs | 4 + MagicQuant/Helpers/PythonManager.cs | 90 ++++++++----- MagicQuant/Models/LlamaBinaries.cs | 2 +- MagicQuant/Program.cs | 2 +- MagicQuant/Services/BenchmarkService.cs | 139 +++++++++++++-------- MagicQuant/Services/QuantizationService.cs | 120 +++++++++++------- 10 files changed, 243 insertions(+), 133 deletions(-) diff --git a/MagicQuant/Cache.cs b/MagicQuant/Cache.cs index 7f8d428..5d4ce35 100644 --- a/MagicQuant/Cache.cs +++ b/MagicQuant/Cache.cs @@ -36,6 +36,8 @@ public class Cache /// public static string? ModelDirectory; + public static string? ModelMagicQuantDirectory; + /// /// Aka BF16, F16, or F32 /// diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index cde8da0..e175b4f 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -1,6 +1,7 @@ using MagicQuant.Models; using MagicQuant.Helpers; using MagicQuant; +using MagicQuant.Services; using Spectre.Console; namespace MagicQuant.Commands; @@ -52,21 +53,21 @@ public async Task Run(List args) // 5. Populate Cache Cache.ModelDirectory = fullModelPath; - Cache.MagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); // Create the MagicQuant directory immediately so it's ready for future steps - if (!Directory.Exists(Cache.MagicQuantDirectory)) + if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) { - Directory.CreateDirectory(Cache.MagicQuantDirectory); + Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); } // 6. Success Output AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); AnsiConsole.Write(new Rule("[yellow]Evolution Configuration[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"Model Path: [blue]{Cache.ModelDirectory}[/]"); - AnsiConsole.MarkupLine($"Output Path: [blue]{Cache.MagicQuantDirectory}[/]"); + AnsiConsole.MarkupLine($"Output Path: [blue]{Cache.ModelMagicQuantDirectory}[/]"); AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length}[/] safe tensors"); // Ensure Llama paths are set (sanity check from InitializeLlamaCpp) @@ -76,8 +77,11 @@ public async Task Run(List args) // If not, we might want to warn or rely on defaults. AnsiConsole.MarkupLine("[yellow]Warning: Llama binaries path not set in Cache. (Did Initialization run?)[/]"); } + var pyManager = new PythonManager(Cache.MagicQuantDirectory); + var bService = new BenchmarkService(pyManager); + var qService = new QuantizationService(bService); - // Next steps of evolution would go here... + await qService.EnsureBaseModelAsync(); } private void ShowEvolutionHelp() diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index d429ca1..8c0a25e 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -121,6 +121,7 @@ public async Task Run(List args) // --------------------------------------------------------- // 6. Build Llama.cpp (Runs as Normal User) // --------------------------------------------------------- + Cache.MagicQuantDirectory = magicQuantPath; var builder = new LlamaBuilder(magicQuantPath, sysInfo); await builder.PrepareAndBuildAsync(update); diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 29bd5de..1a1cbe4 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -84,7 +84,7 @@ public static BigInteger CountAll() { BigInteger sum = BigInteger.Zero; - foreach (var b in BaselineQuants.All.Where(b => b.AllowedAsBaseConversion)) + foreach (var b in BaselineQuants.All.Where(b => b.BaseConversionBase != null)) sum += CountForBase(b); return sum; diff --git a/MagicQuant/Helpers/LlamaBuilder.cs b/MagicQuant/Helpers/LlamaBuilder.cs index 0682bd7..e976039 100644 --- a/MagicQuant/Helpers/LlamaBuilder.cs +++ b/MagicQuant/Helpers/LlamaBuilder.cs @@ -14,6 +14,10 @@ public class LlamaBuilder public LlamaBuilder(string magicRoot, SystemInfo sysInfo) { _llamaRoot = Path.Combine(magicRoot, MagicConstants.LlamaRepoName); + Cache.LlamaRoot = _llamaRoot; + Cache.LlamaBin = Path.Combine(Cache.LlamaRoot, "build", "bin"); + Cache.ConvertScript = Path.Combine(Cache.LlamaRoot, "convert_hf_to_gguf.py");; + _sysInfo = sysInfo; } diff --git a/MagicQuant/Helpers/PythonManager.cs b/MagicQuant/Helpers/PythonManager.cs index 6d47088..1728325 100644 --- a/MagicQuant/Helpers/PythonManager.cs +++ b/MagicQuant/Helpers/PythonManager.cs @@ -19,11 +19,17 @@ public PythonManager(string basePath) public async Task GetInstalledVersionAsync(string packageName) { - // We use a tiny python script to check importlib.metadata - // This is instant compared to pip - string script = $"import importlib.metadata; " + - $"try: print(importlib.metadata.version('{packageName}')); " + - $"except: print('NONE')"; + // NOTE: + // - Empty stdout is treated as NOT installed + // - stderr is captured + // - Python errors fail fast instead of lying + + string script = + $"import importlib.metadata, sys\n" + + $"try:\n" + + $" print(importlib.metadata.version('{packageName}'))\n" + + $"except Exception:\n" + + $" print('NONE')\n"; string python = GetPythonExecutable(); string exe, args; @@ -39,27 +45,48 @@ public PythonManager(string basePath) args = $"-c \"{script}\""; } - // Run without printing output to console var psi = new ProcessStartInfo { - FileName = exe, Arguments = args, - RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true + FileName = exe, + Arguments = args, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true }; - using var proc = Process.Start(psi); - string output = await proc!.StandardOutput.ReadToEndAsync(); + using var proc = Process.Start(psi) + ?? throw new InvalidOperationException("Failed to start Python process"); + + string stdout = await proc.StandardOutput.ReadToEndAsync(); + string stderr = await proc.StandardError.ReadToEndAsync(); + await proc.WaitForExitAsync(); - string version = output.Trim(); - return version == "NONE" ? null : version; + if (proc.ExitCode != 0) + { + throw new Exception( + $"Python package check failed for '{packageName}'.\n{stderr}" + ); + } + + string version = stdout.Trim(); + + // CRITICAL FIX: + // Empty output MUST be treated as not installed + if (string.IsNullOrEmpty(version) || version == "NONE") + return null; + + return version; } + public string GetPythonExecutable() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return Path.Combine(_envPath, "python.exe"); - return Path.Combine(_envPath, "bin", "python3"); + return Path.Combine(_envPath, "bin", "python"); } public async Task SetupEnvironmentAsync() @@ -158,7 +185,7 @@ private async Task SetupPipRunnerAsync() } } - public async Task RunPipInstallAsync(string args, Dictionary? envVars = null) + public async Task RunPipAsync(string pipArgs, Dictionary? envVars = null) { string python = GetPythonExecutable(); string exe, finalArgs; @@ -166,17 +193,21 @@ public async Task RunPipInstallAsync(string args, Dictionary? en if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { exe = "cmd.exe"; - finalArgs = $"/c \"{python}\" pip_runner.py install {args}"; + finalArgs = $"/c \"{python}\" pip_runner.py {pipArgs}"; + await RunShellCommand(exe, finalArgs, _envPath, envVars); } else { exe = python; - finalArgs = $"-m pip install {args}"; + finalArgs = $"-m pip {pipArgs}"; + await RunShellCommand(exe, finalArgs, _envPath, envVars); } - - await RunShellCommand(exe, finalArgs, _envPath, envVars); } + public Task RunPipInstallAsync(string installArgs, Dictionary? envVars = null) + => RunPipAsync($"install {installArgs}", envVars); + + private bool CheckSuccessMarker() => File.Exists(Path.Combine(_envPath, MagicConstants.SuccessJson)); private void WriteSuccessMarker() => File.WriteAllText(Path.Combine(_envPath, MagicConstants.SuccessJson), "{\"status\":\"success\"}"); @@ -192,25 +223,26 @@ private async Task RunShellCommand(string exe, string args, string workingDir, D UseShellExecute = false, CreateNoWindow = true }; - - if (!string.IsNullOrEmpty(workingDir)) psi.WorkingDirectory = workingDir; + + if (!string.IsNullOrEmpty(workingDir)) + psi.WorkingDirectory = workingDir; if (envVars != null) - { foreach (var kvp in envVars) - { - psi.EnvironmentVariables[kvp.Key] = kvp.Value; - } - } - + psi.Environment[kvp.Key] = kvp.Value; + using var proc = Process.Start(psi); - if (proc == null) return; + if (proc == null) throw new InvalidOperationException($"Failed to start: {exe}"); proc.OutputDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.MarkupLine($"[grey]{Markup.Escape(e.Data)}[/]"); }; - proc.ErrorDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.MarkupLine($"[red]{Markup.Escape(e.Data)}[/]"); }; - + proc.ErrorDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.MarkupLine($"[red]{Markup.Escape(e.Data)}[/]"); }; + proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); + await proc.WaitForExitAsync(); + + if (proc.ExitCode != 0) + throw new Exception($"Command failed (exit {proc.ExitCode}): {exe} {args}"); } } \ No newline at end of file diff --git a/MagicQuant/Models/LlamaBinaries.cs b/MagicQuant/Models/LlamaBinaries.cs index 74b6fb8..d20b503 100644 --- a/MagicQuant/Models/LlamaBinaries.cs +++ b/MagicQuant/Models/LlamaBinaries.cs @@ -10,7 +10,7 @@ public class LlamaBinaries public LlamaBinaries(string root) { - var binDir = Path.Combine(root, "build", "bin"); + var binDir = Cache.LlamaBin; Bench = Path.Combine(binDir, "llama-bench"); Ppl = Path.Combine(binDir, "llama-perplexity"); Cli = Path.Combine(binDir, "llama-cli"); diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index d5ef917..a88508a 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -101,7 +101,7 @@ static void ValidateCombinationLogicWorks() var bases = BaselineQuants.All - .Where(b => b.AllowedAsBaseConversion) + .Where(b => b.BaseConversionBase != null) .ToImmutableArray(); foreach (var b in bases) diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index bd1325e..33bd713 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -11,10 +11,10 @@ namespace MagicQuant.Services; public class BenchmarkService { private readonly LlamaBinaries _bins; - private readonly PythonManager _pyManager; + public readonly PythonManager _pyManager; // Constants - private static readonly string[] OomMarkers = + private static readonly string[] OomMarkers = { "out of memory", "cudaMalloc failed", "unable to allocate cuda", "try reducing --n-gpu-layers" }; @@ -24,7 +24,7 @@ public class BenchmarkService // ---------------------------------------------------------------- // Concurrency Controls // ---------------------------------------------------------------- - + // 1. Exclusive Lock: When LlamaBench runs, it must be the ONLY thing running. // Higher-level logic should acquire this before calling RunLlamaBenchAsync. public static readonly SemaphoreSlim ExclusiveBenchLock = new(1, 1); @@ -33,9 +33,9 @@ public class BenchmarkService // However, it CAN run alongside CPU tasks (like quantization if VRAM allows). public static readonly SemaphoreSlim VramLock = new(1, 1); - public BenchmarkService(string llamaRoot, PythonManager pyManager) + public BenchmarkService(PythonManager pyManager) { - _bins = new LlamaBinaries(llamaRoot); + _bins = new LlamaBinaries(Cache.LlamaRoot); _bins.Validate(); _pyManager = pyManager; } @@ -52,6 +52,19 @@ public async Task RunAllBenchmarksAsync( string? klLogitsDir = null, bool saveLogits = false) { + string jsonPath = Path.Combine(benchDir, "bench_metrics.json"); + + if (File.Exists(jsonPath)) + { + var benchMetrics = File.ReadAllText(jsonPath); + if (!string.IsNullOrWhiteSpace(benchMetrics)) + { + var deserializedMetrics = JsonSerializer.Deserialize(benchMetrics); + if (deserializedMetrics != null) + return deserializedMetrics; + } + } + Directory.CreateDirectory(benchDir); var result = new BenchmarkResult(); @@ -86,11 +99,11 @@ public async Task RunAllBenchmarksAsync( // B. Run Benchmark (VRAM Intensive) // We acquire VRAM lock so we don't run 2 perplexities at once await VramLock.WaitAsync(); - try + try { AnsiConsole.MarkupLine($"[yellow]Running Perplexity ({domain})...[/]"); var metrics = await RunPplBenchmarkAsync( - modelPath, benchDir, domain, corpusPath, + modelPath, benchDir, domain, corpusPath, startNgl, klLogitsDir, saveLogits ); result.Perplexity[domain] = metrics; @@ -102,9 +115,9 @@ public async Task RunAllBenchmarksAsync( } // Save Results JSON - string jsonPath = Path.Combine(benchDir, "bench_metrics.json"); - await File.WriteAllTextAsync(jsonPath, JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true })); - + await File.WriteAllTextAsync(jsonPath, + JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true })); + return result; } @@ -115,15 +128,15 @@ public async Task RunAllBenchmarksAsync( private async Task RunLlamaBenchAsync(string modelPath, string benchDir, int? startNgl) { string logFile = Path.Combine(benchDir, "llamabench.md"); - + // Filter candidates - var candidates = startNgl.HasValue - ? NglCandidates.Where(n => n <= startNgl.Value).ToList() + var candidates = startNgl.HasValue + ? NglCandidates.Where(n => n <= startNgl.Value).ToList() : NglCandidates.ToList(); // Command Builder // Note: Keeping -p 8 -t 16 as requested ("just like we're now") - string BuildCmd(int ngl) => + string BuildCmd(int ngl) => $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {ngl} -o md"; // Retry Loop @@ -134,7 +147,7 @@ string BuildCmd(int ngl) => { AnsiConsole.MarkupLine("[red]GPU Failed. Fallback to CPU backend...[/]"); string cpuCmd = $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; - await RunShellCommandAsync(cpuCmd, logFile); + await RunShellCommandAsync(cpuCmd, logFile); } return ParseLlamaBench(logFile); @@ -146,7 +159,7 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) if (!File.Exists(logPath)) return metrics; var lines = File.ReadAllLines(logPath); - + int headerIdx = -1; for (int i = 0; i < lines.Length; i++) { @@ -160,7 +173,8 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) if (headerIdx == -1 || lines.Length <= headerIdx + 2) return metrics; var headers = lines[headerIdx].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(h => h.Trim()).ToList(); - var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()).ToList(); + var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()) + .ToList(); if (headers.Count != dataRow.Count) return metrics; @@ -168,7 +182,7 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) string tpsStr = row.ContainsKey("t/s") ? row["t/s"] : (row.ContainsKey("tps") ? row["tps"] : "0"); var match = Regex.Match(tpsStr, @"([0-9.]+)"); - + if (match.Success && double.TryParse(match.Groups[1].Value, out double tps)) { metrics.Tps = tps; @@ -185,12 +199,12 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) // ---------------------------------------------------------------- private async Task RunPplBenchmarkAsync( - string modelPath, string benchDir, string domain, string corpusPath, + string modelPath, string benchDir, string domain, string corpusPath, int? startNgl, string? klLogitsDir, bool saveLogits) { string logFile = Path.Combine(benchDir, $"perplexity_{domain}.log"); - var candidates = startNgl.HasValue - ? NglCandidates.Where(n => n <= startNgl.Value).ToList() + var candidates = startNgl.HasValue + ? NglCandidates.Where(n => n <= startNgl.Value).ToList() : NglCandidates.ToList(); // KL Divergence Logic @@ -199,14 +213,14 @@ private async Task RunPplBenchmarkAsync( { string logitsFile = Path.Combine(klLogitsDir, $"kld_logits_{domain}.bin"); if (saveLogits) - kldArgs = $"--kl-divergence-base \"{logitsFile}\""; + kldArgs = $"--kl-divergence-base \"{logitsFile}\""; else if (File.Exists(logitsFile)) - kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; + kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; } // Command Builder // Added "-t 4" to limit thread usage as requested - string BuildCmd(int ngl) => + string BuildCmd(int ngl) => $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {ngl} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}"; await RunWithRetryAsync(BuildCmd, logFile, candidates, $"perplexity-{domain}"); @@ -223,8 +237,9 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) string text = File.ReadAllText(logPath); string cleanText = StripAnsi(text); - var pplMatch = Regex.Match(cleanText, @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([0-9.]+)\s*(?:±|\+/-)\s*([0-9.]+)", RegexOptions.IgnoreCase); - + var pplMatch = Regex.Match(cleanText, @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([0-9.]+)\s*(?:±|\+/-)\s*([0-9.]+)", + RegexOptions.IgnoreCase); + if (pplMatch.Success) { metrics.Ppl = double.Parse(pplMatch.Groups[1].Value); @@ -235,15 +250,16 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) AnsiConsole.MarkupLine($"[red]Error parsing PPL from {logPath}[/]"); } - var kldMatch = Regex.Match(cleanText, @"(?:Mean\s+KLD|KL[-_\s]*divergence|kl[-_\s]*div)\s*[:=]\s*([0-9.]+)", RegexOptions.IgnoreCase); - + var kldMatch = Regex.Match(cleanText, @"(?:Mean\s+KLD|KL[-_\s]*divergence|kl[-_\s]*div)\s*[:=]\s*([0-9.]+)", + RegexOptions.IgnoreCase); + if (kldMatch.Success) { metrics.Kld = double.Parse(kldMatch.Groups[1].Value); } else if (!allowMissingKld && cleanText.Contains("KL", StringComparison.OrdinalIgnoreCase)) { - AnsiConsole.MarkupLine("[yellow]Warning: 'KL' found in log but regex failed to parse value.[/]"); + AnsiConsole.MarkupLine("[yellow]Warning: 'KL' found in log but regex failed to parse value.[/]"); } return metrics; @@ -295,58 +311,71 @@ with open(out_path, 'w', encoding='utf-8') as f: await File.WriteAllTextAsync(scriptPath, pyScript); string pythonExe = _pyManager.GetPythonExecutable(); - string args = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? $"/c \"{pythonExe}\" \"{scriptPath}\"" + string args = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? $"/c \"{pythonExe}\" \"{scriptPath}\"" : $"\"{scriptPath}\""; - + string runner = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : pythonExe; if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) args = scriptPath; await _pyManager.RunPipInstallAsync("datasets"); await RunShellCommandAsync(runner + " " + args, null); - - if(File.Exists(scriptPath)) File.Delete(scriptPath); + + if (File.Exists(scriptPath)) File.Delete(scriptPath); } // ---------------------------------------------------------------- // 4. Retry Logic // ---------------------------------------------------------------- - private async Task RunWithRetryAsync( - Func cmdBuilder, - string logPath, - List candidates, + Func cmdBuilder, + string logPath, + List candidates, string label) { foreach (int ngl in candidates) { string cmd = cmdBuilder(ngl); - AnsiConsole.MarkupLine($"[grey][*] {label}: trying -ngl {ngl}[/]"); + + // Untrusted / dynamic output → WriteLine ONLY + AnsiConsole.WriteLine($"[*] {label}: trying -ngl {ngl}"); await RunShellCommandAsync(cmd, logPath); - string logContent = File.Exists(logPath) ? File.ReadAllText(logPath) : ""; - - if (OomMarkers.Any(m => logContent.Contains(m, StringComparison.OrdinalIgnoreCase))) + string logContent = File.Exists(logPath) + ? File.ReadAllText(logPath) + : string.Empty; + + if (OomMarkers.Any(m => + logContent.Contains(m, StringComparison.OrdinalIgnoreCase))) { - AnsiConsole.MarkupLine($"[yellow][WARN] {label}: OOM at -ngl {ngl}, retrying...[/]"); + AnsiConsole.WriteLine( + $"[WARN] {label}: OOM at -ngl {ngl}, retrying..." + ); continue; } - if (logContent.Length < 50) + if (logContent.Length < 50) { - AnsiConsole.MarkupLine($"[yellow][WARN] {label}: Failed at -ngl {ngl} (Unknown Error), trying next...[/]"); + AnsiConsole.WriteLine( + $"[WARN] {label}: Failed at -ngl {ngl} (Unknown Error), trying next..." + ); continue; } - AnsiConsole.MarkupLine($"[green][OK] {label}: succeeded with -ngl {ngl}[/]"); + AnsiConsole.WriteLine( + $"[OK] {label}: succeeded with -ngl {ngl}" + ); return ngl; } - AnsiConsole.MarkupLine($"[red][!] {label}: All -ngl candidates failed.[/]"); + AnsiConsole.WriteLine( + $"[ERROR] {label}: All -ngl candidates failed." + ); return null; } + // ---------------------------------------------------------------- // 5. System Utilities // ---------------------------------------------------------------- @@ -364,7 +393,7 @@ private async Task RunShellCommandAsync(string cmd, string? logPath) }; using var process = new Process { StartInfo = startInfo }; - + FileStream? fs = null; StreamWriter? sw = null; @@ -374,15 +403,21 @@ private async Task RunShellCommandAsync(string cmd, string? logPath) sw = new StreamWriter(fs); } - process.OutputDataReceived += (s, e) => { if (e.Data != null) sw?.WriteLine(e.Data); }; - process.ErrorDataReceived += (s, e) => { if (e.Data != null) sw?.WriteLine(e.Data); }; + process.OutputDataReceived += (s, e) => + { + if (e.Data != null) sw?.WriteLine(e.Data); + }; + process.ErrorDataReceived += (s, e) => + { + if (e.Data != null) sw?.WriteLine(e.Data); + }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); await process.WaitForExitAsync(); - + sw?.Dispose(); fs?.Dispose(); } @@ -394,6 +429,6 @@ private string StripAnsi(string text) private string GetRelativePath(string fullPath) { - return Path.GetFileName(fullPath); + return Path.GetFileName(fullPath); } } \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 8e8ca1d..4178bed 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using System.Diagnostics; using System.Runtime.InteropServices; +using MagicQuant.Helpers; namespace MagicQuant.Services; @@ -12,6 +13,8 @@ public class QuantizationService private readonly string _ggufDir; private readonly string _benchDir; + private readonly PythonManager _python; + // Threading Control // We limit CPU-heavy quantization jobs to (TotalThreads / 8) to avoid choking the system // while leaving room for the GPU-heavy Perplexity tasks. @@ -24,13 +27,13 @@ public class QuantizationService public QuantizationService(BenchmarkService benchmarker) { _benchmarker = benchmarker; - + _python = _benchmarker._pyManager; // Setup Directories based on Cache (assumed populated by Evolution command) if (Cache.MagicQuantDirectory == null) throw new Exception("MagicQuant Directory not set. Run initialization first."); - _ggufDir = Path.Combine(Cache.MagicQuantDirectory, "GGUF"); - _benchDir = Path.Combine(Cache.MagicQuantDirectory, "Benchmarks"); + _ggufDir = Path.Combine(Cache.ModelMagicQuantDirectory, "GGUF"); + _benchDir = Path.Combine(Cache.ModelMagicQuantDirectory, "Benchmarks"); Directory.CreateDirectory(_ggufDir); Directory.CreateDirectory(_benchDir); @@ -121,7 +124,7 @@ await _benchmarker.RunAllBenchmarksAsync( AnsiConsole.WriteException(ex); } } - + private bool IsProtectedModel(string name) { // Don't delete the BF16/F16/F32 base files @@ -131,71 +134,99 @@ private bool IsProtectedModel(string name) // ---------------------------------------------------------------- // 2. Base Model Generation (Dynamic BF16 / F16 / F32) // ---------------------------------------------------------------- - public async Task EnsureBaseModelAsync() { + // Resolve model name string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; - // Dynamic Type Detection - // Default to BF16 if detection failed or wasn't run + // Determine torch type (default BF16) var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; - string typeStr = torchType.ToString(); // "BF16", "F16", "F32" + string typeStr = torchType.ToString(); // BF16, F16, F32 + // Output paths string fileName = $"{modelName}-{typeStr}.gguf"; - string output = Path.Combine(_ggufDir, fileName); + string outputPath = Path.Combine(_ggufDir, fileName); string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); - if (File.Exists(output) && File.Exists(successFile)) - return output; + // Already converted? + if (!File.Exists(outputPath) || !File.Exists(successFile)) + { + // ---- Conversion ---- + AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeStr}...[/]"); - // Create/Convert - AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeStr}...[/]"); + if (File.Exists(outputPath)) + File.Delete(outputPath); - if (File.Exists(output)) File.Delete(output); + string convertScript = Cache.ConvertScript + ?? throw new Exception("ConvertScript path missing in Cache"); - string convertScript = Cache.ConvertScript - ?? throw new Exception("ConvertScript path missing in Cache"); + string outTypeArg = typeStr.ToLowerInvariant(); // bf16 / f16 / f32 - // map enum to CLI arg: BF16 -> bf16, F16 -> f16, F32 -> f32 - string outTypeArg = typeStr.ToLowerInvariant(); + string arguments = + $"\"{convertScript}\" \"{Cache.ModelDirectory}\" " + + $"--outtype {outTypeArg} " + + $"--outfile \"{outputPath}\""; - var psi = new ProcessStartInfo - { - FileName = "python", - Arguments = $"\"{convertScript}\" \"{Cache.ModelDirectory}\" --outtype {outTypeArg} --outfile \"{output}\"", - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true - }; + string python = _python.GetPythonExecutable(); - using var p = Process.Start(psi); - p.OutputDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.WriteLine(e.Data); }; - p.ErrorDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.WriteLine(e.Data); }; - p.BeginOutputReadLine(); - p.BeginErrorReadLine(); - await p.WaitForExitAsync(); + var psi = new ProcessStartInfo + { + FileName = python, + Arguments = arguments, + WorkingDirectory = Cache.LlamaRoot, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var process = Process.Start(psi) + ?? throw new InvalidOperationException("Failed to start conversion process"); + + // UNTRUSTED OUTPUT → WriteLine ONLY + process.OutputDataReceived += (_, e) => + { + if (!string.IsNullOrWhiteSpace(e.Data)) + AnsiConsole.WriteLine(e.Data); + }; + + process.ErrorDataReceived += (_, e) => + { + if (!string.IsNullOrWhiteSpace(e.Data)) + AnsiConsole.WriteLine(e.Data); + }; + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); - if (p.ExitCode != 0) throw new Exception($"{typeStr} Conversion Failed"); + await process.WaitForExitAsync(); - // Write Success JSON - await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); + if (process.ExitCode != 0) + throw new Exception($"{typeStr} conversion failed"); - // Run Benchmark on Base Model (Critical First Step) - string benchPath = Path.Combine(_benchDir, typeStr); // e.g., Benchmarks/BF16 + // Write success marker + await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); + } + + // ---- Benchmark Base Model ---- + string benchPath = Path.Combine(_benchDir, typeStr); string logitsDir = Path.Combine(benchPath, "logits"); - AnsiConsole.MarkupLine($"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]"); + AnsiConsole.MarkupLine( + $"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]" + ); + await _benchmarker.RunAllBenchmarksAsync( - output, - benchPath, + modelPath: outputPath, + benchDir: benchPath, klLogitsDir: logitsDir, saveLogits: true ); - return output; + return outputPath; } + // ---------------------------------------------------------------- // 3. Hybrid Quantization Execution // ---------------------------------------------------------------- @@ -251,7 +282,7 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb if (p.ExitCode != 0) throw new Exception($"Quantization failed for {outputFile}"); } - + private static string ResolveBaseName(BaselineQuants b) { if (b.Names.IsDefaultOrEmpty) @@ -272,6 +303,7 @@ private static string ResolveSchemeName(TensorWeightScheme s) { return "F16"; } + // Default to BF16 for BF16 or F32 types (safer modern default) return "BF16"; } @@ -330,4 +362,4 @@ private string SimplifyQuant(string quant) // BF16 -> B16, Q4_K_M -> Q4KM return quant.Replace("_", "").Replace("BF16", "B16").Replace("F16", "F16"); } -} +} \ No newline at end of file From 24d0db92fa21e28c9b9e8b48968e1bc30055af3d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 28 Dec 2025 11:21:59 -0500 Subject: [PATCH 019/258] updates to bf16 deletions --- MagicQuant/Commands/Evolution.cs | 7 ++++- MagicQuant/Services/QuantizationService.cs | 33 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index e175b4f..f571d4e 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -81,7 +81,12 @@ public async Task Run(List args) var bService = new BenchmarkService(pyManager); var qService = new QuantizationService(bService); - await qService.EnsureBaseModelAsync(); + await qService.EnsureBaseModelAsync(true); + + + + + // Todo: Have an end deletion process to remove the GGUF's and related success jsons, but not imatrix } private void ShowEvolutionHelp() diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 4178bed..af9403a 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -134,7 +134,7 @@ private bool IsProtectedModel(string name) // ---------------------------------------------------------------- // 2. Base Model Generation (Dynamic BF16 / F16 / F32) // ---------------------------------------------------------------- - public async Task EnsureBaseModelAsync() + public async Task EnsureBaseModelAsync(bool deleteProcess = false) { // Resolve model name string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; @@ -148,6 +148,37 @@ public async Task EnsureBaseModelAsync() string outputPath = Path.Combine(_ggufDir, fileName); string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); + if (deleteProcess) + { + if (string.IsNullOrWhiteSpace(fileName)) + throw new ArgumentException("fileName is null or empty.", nameof(fileName)); + + if (!Directory.Exists(_ggufDir)) + throw new DirectoryNotFoundException($"Directory does not exist: {_ggufDir}"); + + var normalizedFileName = Path.GetFileName(fileName); + var successFileName = normalizedFileName + ".success.json"; + var successFilePath = Path.Combine(_ggufDir, successFileName); + + // Only immune if the success file exists + bool isImmune = File.Exists(successFilePath); + + foreach (var filePath in Directory.EnumerateFiles(_ggufDir, "*.gguf", SearchOption.TopDirectoryOnly)) + { + var currentFileName = Path.GetFileName(filePath); + + if (isImmune && + string.Equals(currentFileName, normalizedFileName, StringComparison.OrdinalIgnoreCase)) + { + // This GGUF earned its right to live + continue; + } + + // HARD DELETE — Windows & Linux + File.Delete(filePath); + } + } + // Already converted? if (!File.Exists(outputPath) || !File.Exists(successFile)) { From dd8b1e88c6fa62fc0d4a99f85cef13e2f03f9c61 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 28 Dec 2025 16:18:40 -0500 Subject: [PATCH 020/258] DuckDB initialization --- MagicQuant/Commands/Evolution.cs | 5 + MagicQuant/MagicQuant.csproj | 4 + MagicQuant/Services/QuantDatabaseService.cs | 136 ++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 MagicQuant/Services/QuantDatabaseService.cs diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index f571d4e..6b75201 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -84,6 +84,11 @@ public async Task Run(List args) await qService.EnsureBaseModelAsync(true); + var dbService = new QuantDatabaseService(); + + // This ensures the DB is ready, populated, and valid before you proceed + await dbService.InitializeAsync(); + // Todo: Have an end deletion process to remove the GGUF's and related success jsons, but not imatrix diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 252dcdc..d5843f4 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -20,4 +20,8 @@ + + + + diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs new file mode 100644 index 0000000..e9801ee --- /dev/null +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -0,0 +1,136 @@ +using System.Diagnostics; +using System.Numerics; +using DuckDB.NET.Data; +using MagicQuant.Helpers; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public class QuantDatabaseService +{ + private const string DbFileName = "MagicQuant.duckdb"; + private const string TableName = "tensor_configs"; + + // Connection string points to the file in your cache directory + private string ConnectionString => $"Data Source={Path.Combine(Cache.MagicQuantDirectory, DbFileName)}"; + + public async Task InitializeAsync(CancellationToken ct = default) + { + // 1. Ensure directory exists + Directory.CreateDirectory(Cache.MagicQuantDirectory); + + // 2. Open connection to check state + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + + BigInteger expectedTotal = ComboCounter.CountAll(); + long currentDbCount = await GetRowCountAsync(connection, ct); + + AnsiConsole.MarkupLine($"[bold]DB Check:[/] Current Rows: [cyan]{currentDbCount:N0}[/] | Expected: [yellow]{expectedTotal:N0}[/]"); + + // 3. Validation Logic: If counts mismatch or table missing, rebuild. + if (currentDbCount != expectedTotal) + { + + AnsiConsole.MarkupLine("[bold red]Database empty, mismatch, or new.[/] Initializing/Rebuilding..."); + + await RebuildDatabaseAsync(connection, expectedTotal, ct); + } + else + { + AnsiConsole.MarkupLine("[bold green]Database is synchronized and ready.[/]"); + } + } + + private async Task GetRowCountAsync(DuckDBConnection connection, CancellationToken ct) + { + // Check if table exists first + var checkCmd = connection.CreateCommand(); + checkCmd.CommandText = $"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{TableName}'"; + var exists = (long)(await checkCmd.ExecuteScalarAsync(ct) ?? 0); + + if (exists == 0) return -1; // Marker for "Table doesn't exist" + + // Get count + var countCmd = connection.CreateCommand(); + countCmd.CommandText = $"SELECT COUNT(*) FROM {TableName}"; + return (long)(await countCmd.ExecuteScalarAsync(ct) ?? 0); + } + + private async Task RebuildDatabaseAsync(DuckDBConnection connection, BigInteger expectedTotal, CancellationToken ct) + { + var sw = Stopwatch.StartNew(); + + // 1. Drop and Recreate Table + // We map sbyte (C#) to TINYINT (DuckDB) + var createCmd = connection.CreateCommand(); + createCmd.CommandText = $@" + DROP TABLE IF EXISTS {TableName}; + CREATE TABLE {TableName} ( + BaseQuant TINYINT, + Embeddings TINYINT, + LmHead TINYINT, + AttnQ TINYINT, + AttnKV TINYINT, + AttnOutput TINYINT, + FfnUpGate TINYINT, + FfnDown TINYINT, + MoeExperts TINYINT, + MoeRouter TINYINT + );"; + await createCmd.ExecuteNonQueryAsync(ct); + + // 2. Generate and Bulk Insert + // We use the Appender for high-performance bulk writing + + long insertedTotal = 0; + + // Iterate through your existing generator logic + var bases = BaselineQuants.All + .Where(b => b.BaseConversionBase != null) + .ToList(); + + AnsiConsole.MarkupLine($"[grey]Starting bulk insert of {expectedTotal:N0} rows...[/]"); + + foreach (var b in bases) + { + // We reuse the generator you already wrote + foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches(b, batchSize: 1_000_000, ct: ct)) + { + // OPEN APPENDER for this batch + // Note: DuckDB Appender is synchronous by design for max speed + using (var appender = connection.CreateAppender(TableName)) + { + foreach (var config in batch) + { + var row = appender.CreateRow(); + + // Precise mapping of struct fields + row.AppendValue(config.BaseQuant); + row.AppendValue(config.Embeddings); + row.AppendValue(config.LmHead); + row.AppendValue(config.AttnQ); + row.AppendValue(config.AttnKV); + row.AppendValue(config.AttnOutput); + row.AppendValue(config.FfnUpGate); + row.AppendValue(config.FfnDown); + row.AppendValue(config.MoeExperts); + row.AppendValue(config.MoeRouter); + + row.EndRow(); + } + } // Appender.Dispose() commits the batch automatically + + insertedTotal += batch.Count; + AnsiConsole.MarkupLine($" [grey]Inserted batch... Total so far:[/] {insertedTotal:N0}"); + + // Clear memory in the batch list as per your previous logic + batch.Clear(); + } + } + + sw.Stop(); + AnsiConsole.MarkupLine($"[bold green]Rebuild Complete![/] in {sw.Elapsed.TotalSeconds:F2}s"); + } +} \ No newline at end of file From f9f7537ea500054dfdbf218c63c03257a23ab139 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 31 Dec 2025 17:16:00 -0500 Subject: [PATCH 021/258] New restrictions added through model tensor detections. --- MagicQuant/Commands/Evolution.cs | 7 +- MagicQuant/Config.cs | 10 +- MagicQuant/Helpers/CliHelpers.cs | 121 +++++++- MagicQuant/Helpers/ComboLogic.cs | 3 +- MagicQuant/Helpers/PythonManager.cs | 61 ++-- MagicQuant/Helpers/TensorConfigGenerator.cs | 3 + MagicQuant/Models/TensorGroup.cs | 137 ++++++--- MagicQuant/Models/TensorWeightScheme.cs | 136 +++++---- MagicQuant/Program.cs | 97 +----- .../Services/ModelCompatibilityService.cs | 279 ++++++++++++++++++ 10 files changed, 641 insertions(+), 213 deletions(-) create mode 100644 MagicQuant/Services/ModelCompatibilityService.cs diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 6b75201..23ca9f6 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -81,13 +81,18 @@ public async Task Run(List args) var bService = new BenchmarkService(pyManager); var qService = new QuantizationService(bService); - await qService.EnsureBaseModelAsync(true); + var bf16ModelGgufPath = await qService.EnsureBaseModelAsync(true); + var compatibilityService = new ModelCompatibilityService(pyManager); + await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); + CliHelpers.ValidateCombinationLogicWorks(true); + var dbService = new QuantDatabaseService(); // This ensures the DB is ready, populated, and valid before you proceed await dbService.InitializeAsync(); + diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index dc837f5..c3b5a94 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -7,8 +7,14 @@ public static class Config public static readonly int MaxDataCollectedPerCategory = 5; public static readonly int MaxSurvivalRounds = 4; public static readonly double CollapseMultiplier = 1.5; - - + + /* + * This is properly updated, but not really used. More for generic logs because the + * TensorWeightScheme is what's actually updated with the real ban logic both from the + * start and during runtime + */ + public static List UnusedTensorGroups = new List(); + public static readonly List SensitivityProbeGroups = new() { "embeddings", diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index c6d2dc6..7c449a5 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using System.Diagnostics; using System.Numerics; using System.Text.RegularExpressions; using MagicQuant.Commands; @@ -9,6 +10,117 @@ namespace MagicQuant.Helpers; public static class CliHelpers { + public static void ValidateCombinationLogicWorks(bool realResults = false) + { + CliHelpers.PrintTotalCombinationCount(); + + // ---------------------------------------- + // Pre-compute expected total + // ---------------------------------------- + var expectedTotal = ComboCounter.CountAll(); + + if (!realResults) + { + AnsiConsole.MarkupLine( + $"[bold cyan]Expected total combinations:[/] [bold yellow]{expectedTotal:N0}[/]"); + } + else + { + AnsiConsole.MarkupLine( + $"[bold cyan]Total real combinations after model detection:[/] [bold yellow]{expectedTotal:N0}[/]"); + } + + // ---------------------------------------- + // Generation + timing + // ---------------------------------------- + var sw = Stopwatch.StartNew(); + + long actualTotal = 0; + + var bases = + BaselineQuants.All + .Where(b => b.BaseConversionBase != null) + .ToImmutableArray(); + + foreach (var b in bases) + { + AnsiConsole.MarkupLine( + $"[cyan]Base:[/] [bold]{string.Join("/", b.Names)}[/] " + + $"[grey](RequiresImatrix={b.RequiresImatrix})[/]"); + + long baseTotal = 0; + + foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches( + b, batchSize: 10_000_000)) + { + baseTotal += batch.Count; + actualTotal += batch.Count; + + AnsiConsole.MarkupLine( + $" [green]Batch:[/] {batch.Count:N0} " + + $"[grey]BaseRunning:[/] {baseTotal:N0}"); + + // Release memory aggressively (unit-test mode) + batch.Clear(); + } + + AnsiConsole.MarkupLine( + $"[yellow]Base total:[/] {baseTotal:N0}"); + } + + sw.Stop(); + + // ---------------------------------------- + // Verification + // ---------------------------------------- + bool match = actualTotal == expectedTotal; + + AnsiConsole.MarkupLine( + $"[bold green]Generated total:[/] {actualTotal:N0}"); + + AnsiConsole.MarkupLine( + match + ? "[bold green] Counts match expected total[/]" + : $"[bold red] MISMATCH! Expected {expectedTotal:N0} but generated {actualTotal:N0}[/]"); + + // ---------------------------------------- + // Human-readable elapsed time + // ---------------------------------------- + var t = sw.Elapsed; + + AnsiConsole.MarkupLine( + $"[bold]Elapsed:[/] " + + $"{t.Hours}h {t.Minutes}m {t.Seconds}s {t.Milliseconds}ms"); + Console.WriteLine(); + Console.WriteLine("---------------"); + Console.WriteLine(); + if (!realResults) + { + var MOE = TensorConfigGenerator.GenerateRequiredDataSampleCombos(); + + var Dense = TensorConfigGenerator.GenerateRequiredDataSampleCombos( //); + new List() { TReg.MoeRouter, TReg.MoeExperts }); + + Console.WriteLine(); + Console.WriteLine("---------------"); + Console.WriteLine(); + AnsiConsole.MarkupLine( + $"[bold green]Max MOE samples created:[/] {MOE.Count():N0}"); + AnsiConsole.MarkupLine( + $"[bold green]Max Dense samples created:[/] {Dense.Count():N0}"); + } + else + { + var RealBans = TensorConfigGenerator.GenerateRequiredDataSampleCombos(Config.UnusedTensorGroups); + + Console.WriteLine(); + Console.WriteLine("---------------"); + Console.WriteLine(); + AnsiConsole.MarkupLine( + $"[bold green]Max real samples to create:[/] {RealBans.Count():N0}"); + } + } + public static void PrintTotalCombinationCount() { const long MaxSupported = 4_000_000_000L; @@ -23,13 +135,14 @@ public static void PrintTotalCombinationCount() $"[green]Total potential combinations:[/] [bold yellow]{total:N0}[/]"); } - + public static List ParseArguments(string input) { var cliArgs = new List(); - + // Regex identifies --key value or --key "value with spaces" - var regex = new Regex(@"--(?[^\s=]+)(?:[\s=]+(?:""(?[^""]*)""|(?[^\s-]*)))?", RegexOptions.IgnoreCase); + var regex = new Regex(@"--(?[^\s=]+)(?:[\s=]+(?:""(?[^""]*)""|(?[^\s-]*)))?", + RegexOptions.IgnoreCase); var matches = regex.Matches(input); foreach (Match match in matches) @@ -60,7 +173,7 @@ public static void ShowHelp(Dictionary GetAllowedSchemeIdsPerGroup(BaselineQuants var ids = schemesForBase .Where(s => - s.BannedGroups.IsDefault || - s.BannedGroups.IsEmpty || + s.BannedGroups.Count == 0 || !s.BannedGroups.Contains(group)) .Select(s => s.UniqueId) .ToArray(); diff --git a/MagicQuant/Helpers/PythonManager.cs b/MagicQuant/Helpers/PythonManager.cs index 1728325..e2d6b66 100644 --- a/MagicQuant/Helpers/PythonManager.cs +++ b/MagicQuant/Helpers/PythonManager.cs @@ -80,12 +80,12 @@ public PythonManager(string basePath) return version; } - + public string GetPythonExecutable() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return Path.Combine(_envPath, "python.exe"); - + return Path.Combine(_envPath, "bin", "python"); } @@ -93,7 +93,7 @@ public async Task SetupEnvironmentAsync() { AnsiConsole.MarkupLine("[cyan]Configuring Python Environment...[/]"); - if (CheckSuccessMarker()) + if (CheckSuccessMarker()) { AnsiConsole.MarkupLine("[green]✔ Python Environment is ready.[/]"); return; @@ -114,14 +114,14 @@ public async Task SetupEnvironmentAsync() // Install Pip Runner logic await SetupPipRunnerAsync(); - + WriteSuccessMarker(); } private async Task SetupWindowsEmbedAsync() { string zipPath = Path.Combine(_basePath, MagicConstants.WinPythonZip); - + // Download if (!File.Exists(zipPath)) { @@ -134,7 +134,7 @@ private async Task SetupWindowsEmbedAsync() // Extract AnsiConsole.MarkupLine("Extracting Python..."); ZipFile.ExtractToDirectory(zipPath, _envPath); - + // Cleanup Zip File.Delete(zipPath); @@ -157,7 +157,6 @@ private async Task SetupLinuxVenvAsync() private async Task SetupPipRunnerAsync() { - // Copy pip_runner.py from Helpers to Env string source = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Helpers", "pip_runner.py"); string dest = Path.Combine(_envPath, "pip_runner.py"); @@ -168,20 +167,18 @@ private async Task SetupPipRunnerAsync() } else { - AnsiConsole.MarkupLine("[yellow]Warning: pip_runner.py not found in Helpers.[/]"); + AnsiConsole.MarkupLine("[yellow]Warning: pip_runner.py not found in Helpers.[/]"); } - - // Upgrade Pip using the runner or standard module + string python = GetPythonExecutable(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - // FIX 2: Added null for env vars (4th arg) - await RunShellCommand("cmd.exe", $"/c \"{python}\" pip_runner.py install --upgrade pip setuptools wheel", _envPath, null); + await RunShellCommand("cmd.exe", $"/c \"{python}\" pip_runner.py install --upgrade pip setuptools wheel", + _envPath, null); } else { - // FIX 3: Added null for env vars (4th arg) - await RunShellCommand(python, "-m pip install --upgrade pip setuptools wheel", _basePath, null); + await RunShellCommand(python, "-m pip install --upgrade pip setuptools wheel", _basePath, null); } } @@ -209,10 +206,32 @@ public Task RunPipInstallAsync(string installArgs, Dictionary? e private bool CheckSuccessMarker() => File.Exists(Path.Combine(_envPath, MagicConstants.SuccessJson)); - private void WriteSuccessMarker() => File.WriteAllText(Path.Combine(_envPath, MagicConstants.SuccessJson), "{\"status\":\"success\"}"); + + private void WriteSuccessMarker() => + File.WriteAllText(Path.Combine(_envPath, MagicConstants.SuccessJson), "{\"status\":\"success\"}"); + + public Task RunPythonScriptAsync(string scriptPath, string args = "", Dictionary? envVars = null) + { + string python = GetPythonExecutable(); + string exe, finalArgs; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + exe = "cmd.exe"; + finalArgs = $"/c \"\"{python}\" \"{scriptPath}\" {args}\""; + } + else + { + exe = python; + finalArgs = $"\"{scriptPath}\" {args}"; + } + + return RunShellCommand(exe, finalArgs, _envPath, envVars); + } // The Method Signature causing the issue - private async Task RunShellCommand(string exe, string args, string workingDir, Dictionary? envVars = null) + private async Task RunShellCommand(string exe, string args, string workingDir, + Dictionary? envVars = null) { var psi = new ProcessStartInfo { @@ -234,8 +253,14 @@ private async Task RunShellCommand(string exe, string args, string workingDir, D using var proc = Process.Start(psi); if (proc == null) throw new InvalidOperationException($"Failed to start: {exe}"); - proc.OutputDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.MarkupLine($"[grey]{Markup.Escape(e.Data)}[/]"); }; - proc.ErrorDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.MarkupLine($"[red]{Markup.Escape(e.Data)}[/]"); }; + proc.OutputDataReceived += (s, e) => + { + if (e.Data != null) AnsiConsole.MarkupLine($"[grey]{Markup.Escape(e.Data)}[/]"); + }; + proc.ErrorDataReceived += (s, e) => + { + if (e.Data != null) AnsiConsole.MarkupLine($"[red]{Markup.Escape(e.Data)}[/]"); + }; proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 0f78d06..c8231c6 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -9,6 +9,9 @@ public static class TensorConfigGenerator { public static List GenerateRequiredDataSampleCombos(List? MissingTensorGroup = null) { + if (MissingTensorGroup != null && !MissingTensorGroup.Any()) + MissingTensorGroup = null; + var allowedBaselines = BaselineQuants.All.Where(x => x.BaseConversionBase != null).ToList(); var hybridQuants = new List(); diff --git a/MagicQuant/Models/TensorGroup.cs b/MagicQuant/Models/TensorGroup.cs index ac9acc1..a1f507f 100644 --- a/MagicQuant/Models/TensorGroup.cs +++ b/MagicQuant/Models/TensorGroup.cs @@ -38,64 +38,133 @@ public record TensorGroup(sbyte UniqueId, string Name, ImmutableArray Te public static class TReg { public static readonly TensorGroup Embeddings = new(0, "embeddings", [ - "token_embd.weight", "model.embed_tokens.weight", "embed_tokens.weight", - "tok_embeddings.weight", "word_embeddings.weight", "transformer.wte.weight", - "gpt_neox.embed_in.weight", "shared.weight", "wte.weight" + "token_embd\\.weight", + "model\\.embed_tokens\\.weight", + "embed_tokens\\.weight", + "tok_embeddings\\.weight", + "word_embeddings\\.weight", + "transformer\\.wte\\.weight", + "wte\\.weight" ]); public static readonly TensorGroup LmHead = new(1, "lm_head", [ - "output.weight", "lm_head.weight", "final_logits_proj.weight", - "model.embed_out.weight", "lm_head.decoder.weight", "cls.predictions.decoder.weight", - "gpt_neox.embed_out.weight", "decoder.output_projection.weight", - "decoder.output_dense.weight", "transformer.wte.weight", "shared.weight" + "output\\.weight", + "lm_head\\.weight", + "final_logits_proj\\.weight", + "model\\.embed_out\\.weight", + "lm_head\\.decoder\\.weight" ]); public static readonly TensorGroup AttnQ = new(2, "attn_q", [ - "blk.*.attn_q.weight", ".*q_proj.*weight", ".*query.weight", ".*q_proj.weight", - ".*self_attn.q_proj.weight", ".*attention.self.query.weight", - ".*SelfAttention.q.weight", ".*c_attn.weight", ".*query_key_value.weight" + // Matches blk.0.attn_q.weight + "blk\\..*\\.attn_q\\.weight", + ".*q_proj.*weight", + ".*query\\.weight", + ".*self_attn\\.q_proj\\.weight", + ".*attention\\.self\\.query\\.weight", + ".*SelfAttention\\.q\\.weight", + ".*c_attn\\.weight", + ".*query_key_value\\.weight" ]); public static readonly TensorGroup AttnKV = new(3, "attn_kv", [ - "blk.*.attn_k.weight", "blk.*.attn_v.weight", ".*k_proj.*weight", ".*v_proj.*weight", - ".*key.weight", ".*value.weight", ".*self_attn.k_proj.weight", ".*self_attn.v_proj.weight", - ".*attention.self.key.weight", ".*attention.self.value.weight", ".*SelfAttention.k.weight", - ".*SelfAttention.v.weight", ".*EncDecAttention.k.weight", ".*EncDecAttention.v.weight", - ".*c_attn.weight", ".*query_key_value.weight" + "blk\\..*\\.attn_k\\.weight", + "blk\\..*\\.attn_v\\.weight", + ".*k_proj.*weight", + ".*v_proj.*weight", + ".*key\\.weight", + ".*value\\.weight", + ".*self_attn\\.k_proj\\.weight", + ".*self_attn\\.v_proj\\.weight", + ".*attention\\.self\\.key\\.weight", + ".*attention\\.self\\.value\\.weight", + ".*SelfAttention\\.k\\.weight", + ".*SelfAttention\\.v\\.weight", + ".*EncDecAttention\\.k\\.weight", + ".*EncDecAttention\\.v\\.weight" ]); public static readonly TensorGroup AttnOutput = new(4, "attn_output", [ - "blk.*.attn_output.weight", ".*out_proj.*weight", ".*o_proj.*weight", ".*c_proj.weight", - ".*attention.output.dense.weight", ".*self_attn.out_proj.weight", - ".*SelfAttention.o.weight", ".*self_attention.dense.weight", ".*attention.proj.weight" + "blk\\..*\\.attn_output\\.weight", + ".*out_proj.*weight", + ".*o_proj.*weight", + ".*c_proj\\.weight", + ".*attention\\.output\\.dense\\.weight", + ".*self_attn\\.out_proj\\.weight", + ".*SelfAttention\\.o\\.weight", + ".*self_attention\\.dense\\.weight", + ".*attention\\.proj\\.weight" ]); public static readonly TensorGroup FfnUpGate = new(5, "ffn_up_gate", [ - "blk.*.ffn_up.weight", "blk.*.ffn_gate.weight", ".*intermediate.dense.weight", - ".*c_fc.weight", ".*fc1.weight", ".*fc_in.weight", ".*dense_h_to_4h.weight", - ".*wi.weight", ".*wi_0.weight", ".*wi_1.weight", ".*mlp.up_proj.weight", - ".*mlp.gate_proj.weight", ".*DenseReluDense.wi_0.weight", ".*DenseReluDense.wi_1.weight", - ".*experts.*wi_0.weight", ".*experts.*wi_1.weight", "blk.*.ffn_up_exps.weight", - "blk.*.ffn_gate_exps.weight" + // This is where ffn_gate belongs! + "blk\\..*\\.ffn_up\\.weight", + "blk\\..*\\.ffn_gate\\.weight", + + ".*intermediate\\.dense\\.weight", + ".*c_fc\\.weight", + ".*fc1\\.weight", + ".*fc_in\\.weight", + ".*dense_h_to_4h\\.weight", + ".*wi\\.weight", + ".*wi_0\\.weight", + ".*wi_1\\.weight", + ".*mlp\\.up_proj\\.weight", + ".*mlp\\.gate_proj\\.weight", + ".*DenseReluDense\\.wi_0\\.weight", + ".*DenseReluDense\\.wi_1\\.weight", + ".*experts.*wi_0\\.weight", + ".*experts.*wi_1\\.weight", + "blk\\..*\\.ffn_up_exps\\.weight", + "blk\\..*\\.ffn_gate_exps\\.weight" ]); public static readonly TensorGroup FfnDown = new(6, "ffn_down", [ - "blk.*.ffn_down.weight", ".*output.dense.weight", ".*c_proj.weight", ".*fc2.weight", - ".*fc_out.weight", ".*wo.weight", ".*dense_4h_to_h.weight", ".*mlp.down_proj.weight", - ".*DenseReluDense.wo.weight", ".*experts.*wo.weight", "blk.*.ffn_down_exps.weight" + "blk\\..*\\.ffn_down\\.weight", + ".*output\\.dense\\.weight", + ".*c_proj\\.weight", + ".*fc2\\.weight", + ".*fc_out\\.weight", + ".*wo\\.weight", + ".*dense_4h_to_h\\.weight", + ".*mlp\\.down_proj\\.weight", + ".*DenseReluDense\\.wo\\.weight", + ".*experts.*wo\\.weight", + "blk\\..*\\.ffn_down_exps\\.weight" ]); public static readonly TensorGroup MoeExperts = new(7, "moe_experts", [ - "blk.*.ffn_.*_expert.*", "blk.*.ffn_.*_exps.*", ".*experts?\\..*wi_0.*", - ".*experts?\\..*wi_1.*", ".*experts?\\..*wo.*", ".*experts?\\..*fc1.*", - ".*experts?\\..*fc2.*", ".*experts?\\..*dense_h_to_4h.*", ".*experts?\\..*dense_4h_to_h.*" + "blk\\..*\\.ffn_.*expert.*", + "blk\\..*\\.ffn_.*exps.*", + ".*experts?\\..*wi_0.*", + ".*experts?\\..*wi_1.*", + ".*experts?\\..*wo.*", + ".*experts?\\..*fc1.*", + ".*experts?\\..*fc2.*", + ".*experts?\\..*dense_h_to_4h.*", + ".*experts?\\..*dense_4h_to_h.*" ]); public static readonly TensorGroup MoeRouter = new(8, "moe_router", [ - "router.*", "gate.*", "gating.*", "routing.*", "blk.*.ffn_gate_inp.weight", - "blk.*.router.*", "blk.*.gate_inp.*", "blk.*.gate_proj.*", "blk.*.gate.weight", - "blk.*.router_fc.*", ".*router.weight", ".*gate.weight", ".*router_fc.*", - ".*gating_network.*weight", ".*moe_gate.*weight" + // Strict Router definitions + "router.*", + "gating.*", + "routing.*", + + // This was the culprit. + // We use Negative Lookbehind (? diff --git a/MagicQuant/Models/TensorWeightScheme.cs b/MagicQuant/Models/TensorWeightScheme.cs index 6660a3e..863a403 100644 --- a/MagicQuant/Models/TensorWeightScheme.cs +++ b/MagicQuant/Models/TensorWeightScheme.cs @@ -2,161 +2,187 @@ namespace MagicQuant.Models; -public record TensorWeightScheme( - sbyte UniqueId, - bool RequiresImatrix, - ImmutableArray Names, - ImmutableArray BannedGroups, - bool AlwaysBuild = true) +public sealed class TensorWeightScheme { + public sbyte UniqueId { get; } + public bool RequiresImatrix { get; } + public ImmutableArray Names { get; } + public List BannedGroups { get; } + public bool AlwaysBuild { get; } + + public ushort? BlockNeo { get; } + + private TensorWeightScheme( + sbyte uniqueId, + bool requiresImatrix, + ImmutableArray names, + IEnumerable bannedGroups, + ushort? blockNeo) + { + UniqueId = uniqueId; + RequiresImatrix = requiresImatrix; + Names = names; + BannedGroups = new List(bannedGroups); + BlockNeo = blockNeo; + } + // NULL: always-present groups, never nullable - public static readonly TensorWeightScheme NULL = + public static TensorWeightScheme NULL = new( 0, false, ["NULL"], - [ - TReg.Embeddings, - TReg.AttnQ, - TReg.AttnKV, - TReg.AttnOutput, - TReg.FfnDown, - TReg.FfnUpGate - ] + + Array.Empty(), + null ); // BF16 and F16 intentionally share UniqueId - public static readonly TensorWeightScheme BF16_F16 = + public static TensorWeightScheme BF16_F16 = new( 1, false, ["BF16", "F16"], - ImmutableArray.Empty + Array.Empty(), + null ); - public static readonly TensorWeightScheme MXFP4 = + public static TensorWeightScheme MXFP4 = new( 2, false, ["MXFP4"], - [ + new[] + { TReg.AttnQ, TReg.MoeRouter, TReg.MoeExperts - ] + }, + 32 ); - public static readonly TensorWeightScheme Q8_0 = - new(3, false, ["Q8_0"], ImmutableArray.Empty); + public static TensorWeightScheme Q8_0 = + new(3, false, ["Q8_0"], Array.Empty(), null); - public static readonly TensorWeightScheme Q6_K = - new(4, false, ["Q6_K"], ImmutableArray.Empty); + public static TensorWeightScheme Q6_K = + new(4, false, ["Q6_K"], Array.Empty(), 256); - public static readonly TensorWeightScheme Q5_K = + public static TensorWeightScheme Q5_K = new( 5, false, ["Q5_K"], - [TReg.MoeRouter] + new[] { TReg.MoeRouter }, + 256 ); - public static readonly TensorWeightScheme IQ4_XS = + public static TensorWeightScheme IQ4_XS = new( 6, false, ["IQ4_XS"], - [TReg.MoeRouter] + new[] { TReg.MoeRouter }, + 32 ); - + /* - public static readonly TensorWeightScheme IQ4_NL = + public static TensorWeightScheme IQ4_NL = new( 7, false, ["IQ4_NL"], - [TReg.MoeRouter] + new[] { TReg.MoeRouter }, + 32 ); - - - // IQ3 levels - public static readonly TensorWeightScheme IQ3_S = + public static TensorWeightScheme IQ3_S = new( 8, true, ["IQ3_S"], - [ + new[] + { TReg.Embeddings, TReg.LmHead, TReg.MoeRouter - ] + }, + 32 ); - public static readonly TensorWeightScheme IQ3_XS = + public static TensorWeightScheme IQ3_XS = new( 9, true, ["IQ3_XS"], - [ + new[] + { TReg.Embeddings, TReg.LmHead, TReg.MoeRouter - ] + }, + 32 ); - public static readonly TensorWeightScheme IQ3_XXS = + public static TensorWeightScheme IQ3_XXS = new( 10, true, ["IQ3_XXS"], - [ + new[] + { TReg.Embeddings, TReg.LmHead, TReg.MoeRouter - ] + }, + 32 ); - // IQ2 levels: extremely restrictive - public static readonly TensorWeightScheme IQ2_S = + public static TensorWeightScheme IQ2_S = new( 11, true, ["IQ2_S"], - [ + new[] + { TReg.Embeddings, TReg.LmHead, TReg.MoeRouter, TReg.MoeExperts - ] + }, + 32 ); - public static readonly TensorWeightScheme IQ2_XS = + public static TensorWeightScheme IQ2_XS = new( 12, true, ["IQ2_XS"], - [ + new[] + { TReg.Embeddings, TReg.LmHead, TReg.MoeRouter, TReg.MoeExperts - ] + }, + 32 ); - public static readonly TensorWeightScheme IQ2_XXS = + public static TensorWeightScheme IQ2_XXS = new( 13, true, ["IQ2_XXS"], - [ + new[] + { TReg.Embeddings, TReg.LmHead, TReg.MoeRouter, TReg.MoeExperts, TReg.AttnKV - ] + }, + 32 ); - */ + */ public static readonly ImmutableArray All = [ @@ -166,7 +192,6 @@ public record TensorWeightScheme( Q8_0, Q6_K, Q5_K, - IQ4_XS, //IQ4_NL, /*IQ3_S, @@ -177,4 +202,3 @@ public record TensorWeightScheme( IQ2_XXS*/ ]; } - diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index a88508a..da8eaca 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -67,7 +67,7 @@ await AnsiConsole.Status() } // Manditory Run combinations and DuckDB setup - ValidateCombinationLogicWorks(); + CliHelpers.ValidateCombinationLogicWorks(); // 7. Execute Command var commandInstance = commandInfo.Factory(); @@ -77,98 +77,3 @@ await AnsiConsole.Status() { AnsiConsole.WriteException(ex); } - -#region Linux Helpers - -static void ValidateCombinationLogicWorks() -{ - CliHelpers.PrintTotalCombinationCount(); - - // ---------------------------------------- - // Pre-compute expected total - // ---------------------------------------- - var expectedTotal = ComboCounter.CountAll(); - - AnsiConsole.MarkupLine( - $"[bold cyan]Expected total combinations:[/] [bold yellow]{expectedTotal:N0}[/]"); - - // ---------------------------------------- - // Generation + timing - // ---------------------------------------- - var sw = Stopwatch.StartNew(); - - long actualTotal = 0; - - var bases = - BaselineQuants.All - .Where(b => b.BaseConversionBase != null) - .ToImmutableArray(); - - foreach (var b in bases) - { - AnsiConsole.MarkupLine( - $"[cyan]Base:[/] [bold]{string.Join("/", b.Names)}[/] " + - $"[grey](RequiresImatrix={b.RequiresImatrix})[/]"); - - long baseTotal = 0; - - foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches( - b, batchSize: 10_000_000)) - { - baseTotal += batch.Count; - actualTotal += batch.Count; - - AnsiConsole.MarkupLine( - $" [green]Batch:[/] {batch.Count:N0} " + - $"[grey]BaseRunning:[/] {baseTotal:N0}"); - - // Release memory aggressively (unit-test mode) - batch.Clear(); - } - - AnsiConsole.MarkupLine( - $"[yellow]Base total:[/] {baseTotal:N0}"); - } - - sw.Stop(); - - // ---------------------------------------- - // Verification - // ---------------------------------------- - bool match = actualTotal == expectedTotal; - - AnsiConsole.MarkupLine( - $"[bold green]Generated total:[/] {actualTotal:N0}"); - - AnsiConsole.MarkupLine( - match - ? "[bold green] Counts match expected total[/]" - : $"[bold red] MISMATCH! Expected {expectedTotal:N0} but generated {actualTotal:N0}[/]"); - - // ---------------------------------------- - // Human-readable elapsed time - // ---------------------------------------- - var t = sw.Elapsed; - - AnsiConsole.MarkupLine( - $"[bold]Elapsed:[/] " + - $"{t.Hours}h {t.Minutes}m {t.Seconds}s {t.Milliseconds}ms"); - Console.WriteLine(); - Console.WriteLine("---------------"); - Console.WriteLine(); - - var MOE = TensorConfigGenerator.GenerateRequiredDataSampleCombos(); - - var Dense = TensorConfigGenerator.GenerateRequiredDataSampleCombos(//); - new List(){TReg.MoeRouter, TReg.MoeExperts}); - - Console.WriteLine(); - Console.WriteLine("---------------"); - Console.WriteLine(); - AnsiConsole.MarkupLine( - $"[bold green]Max MOE samples created:[/] {MOE.Count():N0}"); - AnsiConsole.MarkupLine( - $"[bold green]Max Dense samples created:[/] {Dense.Count():N0}"); -} - -#endregion \ No newline at end of file diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs new file mode 100644 index 0000000..397f887 --- /dev/null +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -0,0 +1,279 @@ +using System.Text.Json; +using MagicQuant.Helpers; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public class ModelCompatibilityService +{ + private readonly PythonManager _pyManager; + + public ModelCompatibilityService(PythonManager pyManager) + { + _pyManager = pyManager; + } + + public async Task RunCompatibilityCheckAsync(string ggufPath) + { + AnsiConsole.Write(new Rule("[yellow]Tensor Compatibility Check[/]") { Justification = Justify.Left }); + + if (!File.Exists(ggufPath)) + throw new FileNotFoundException($"Base model not found at {ggufPath}"); + + string directory = Path.GetDirectoryName(ggufPath)!; + string scriptPath = Path.Combine(directory, "check_compat.py"); + string resultPath = Path.Combine(directory, "compat_results.json"); + string debugPath = Path.Combine(directory, "compat_debug.txt"); + + try + { + // 1. Prepare Data + var groupDefinitions = TReg.All.ToDictionary(g => g.Name, g => g.Tensors); + + var blockRequirements = TensorWeightScheme.All + .Where(s => s.BlockNeo.HasValue) + .ToDictionary(s => s.Names[0], s => s.BlockNeo!.Value); + + var payload = new + { + gguf_path = ggufPath, + output_path = resultPath, + groups = groupDefinitions, + schemes = blockRequirements + }; + + // 2. Generate Python Script (With Shape Debugging) + string pyCode = GeneratePythonScript(JsonSerializer.Serialize(payload)); + await File.WriteAllTextAsync(scriptPath, pyCode); + + // 3. Run Inspection + AnsiConsole.MarkupLine("[grey]Inspecting GGUF structure...[/]"); + await _pyManager.RunPythonScriptAsync(scriptPath); + + // 4. Validate Result + if (!File.Exists(resultPath)) + throw new Exception("Compatibility script finished but produced no result file."); + + string jsonResult = await File.ReadAllTextAsync(resultPath); + + // Handle script errors + if (jsonResult.Contains("\"Error\"")) + { + var errorRes = JsonSerializer.Deserialize(jsonResult); + if (!string.IsNullOrEmpty(errorRes?.Error)) + throw new Exception($"Python Inspection Failed: {errorRes.Error}"); + } + + var result = JsonSerializer.Deserialize(jsonResult); + if (result == null) return; + + // --------------------------------------------------------- + // 5. Global State Update Logic + // --------------------------------------------------------- + + TensorWeightScheme.NULL.BannedGroups.Clear(); + MagicQuant.Config.UnusedTensorGroups.Clear(); + + int unusedCount = 0; + int usedCount = 0; + + foreach (var group in TReg.All) + { + bool exists = result.FoundGroups.Contains(group.Name); + + if (exists) + { + if (!TensorWeightScheme.NULL.BannedGroups.Contains(group)) + { + TensorWeightScheme.NULL.BannedGroups.Add(group); + } + usedCount++; + } + else + { + unusedCount++; + MagicQuant.Config.UnusedTensorGroups.Add(group); + + foreach (var scheme in TensorWeightScheme.All) + { + if (scheme == TensorWeightScheme.NULL) continue; + if (!scheme.BannedGroups.Contains(group)) scheme.BannedGroups.Add(group); + } + } + } + + // --------------------------------------------------------- + // 6. Handle Shape Restrictions + // --------------------------------------------------------- + int shapeBanCount = 0; + var table = new Table().Border(TableBorder.Rounded).Title("[red]Shape Incompatibilities[/]"); + table.AddColumn("Group"); + table.AddColumn("Scheme"); + table.AddColumn("Reason"); + + foreach (var failure in result.Incompatible) + { + var group = TReg.GetByName(failure.Group); + var scheme = TensorWeightScheme.All.FirstOrDefault(s => s.Names.Contains(failure.Scheme)); + + if (group != null && scheme != null) + { + if (!scheme.BannedGroups.Contains(group)) + { + scheme.BannedGroups.Add(group); + shapeBanCount++; + table.AddRow($"[blue]{group.Name}[/]", $"[yellow]{scheme.Names[0]}[/]", "[grey]Block Alignment[/]"); + } + } + } + + // --------------------------------------------------------- + // 7. Report + // --------------------------------------------------------- + AnsiConsole.MarkupLine($"[green]✔[/] Analysis Complete."); + AnsiConsole.MarkupLine($" Active Groups: [bold cyan]{usedCount}[/]"); + + if (unusedCount > 0) + { + string unusedNames = string.Join(", ", MagicQuant.Config.UnusedTensorGroups.Select(g => g.Name)); + AnsiConsole.MarkupLine($" Unused Groups: [grey]{unusedNames}[/] (Forced to NULL)"); + } + + if (shapeBanCount > 0) + { + AnsiConsole.Write(table); + AnsiConsole.MarkupLine($"[yellow]Applied {shapeBanCount} restrictions due to tensor shapes.[/]"); + } + else + { + AnsiConsole.MarkupLine("[green]No shape-based restrictions found.[/]"); + } + + AnsiConsole.WriteLine(); + } + finally + { + if (File.Exists(scriptPath)) File.Delete(scriptPath); + if (File.Exists(resultPath)) File.Delete(resultPath); + // Debug path is kept for inspection + } + } + + private string GeneratePythonScript(string jsonPayload) + { + return $@" +import sys +import json +import re + +payload_str = r'''{jsonPayload}''' +config = json.loads(payload_str) +output_path = config['output_path'] +debug_path = output_path.replace('compat_results.json', 'compat_debug.txt') + +def write_error(msg): + with open(output_path, 'w') as f: + json.dump({{'FoundGroups': [], 'Incompatible': [], 'Error': msg}}, f) + sys.exit(0) + +try: + import gguf +except ImportError: + write_error('gguf module not installed') + +try: + reader = gguf.GGUFReader(config['gguf_path']) +except Exception as e: + write_error(str(e)) + +tensors_map = {{t.name: t for t in reader.tensors}} +tensor_names = list(tensors_map.keys()) + +found_groups = [] +failures = [] +debug_lines = [] + +debug_lines.append(f'Inspecting {{len(tensor_names)}} tensors against {{len(config[""schemes""])}} block requirements.') + +# 1. Match Groups +for g_name, patterns in config['groups'].items(): + matched = [] + first_reason = None + + for pat in patterns: + try: + regex = re.compile(pat) + for t in tensor_names: + if regex.fullmatch(t): + matched.append(t) + if not first_reason: + first_reason = f""Match: '{{pat}}' -> '{{t}}'"" + except: + continue + + if matched: + found_groups.append(g_name) + debug_lines.append(f""[FOUND] {{g_name}} ({{len(matched)}} tensors). {{first_reason}}"") + + # 2. Check Compatibility (Only if found) + weights = [t for t in matched if t.endswith('.weight')] + + if weights: + # Check against every scheme that has a block req + for scheme, block_size in config['schemes'].items(): + is_valid = True + + for w_name in weights: + t_obj = tensors_map[w_name] + ne0 = t_obj.shape[0] # GGUF ne0 + n_dims = len(t_obj.shape) + + # Rule A: Non-2D + if n_dims != 2: + is_valid = False + debug_lines.append(f"" [FAIL] {{g_name}} vs {{scheme}}: {{w_name}} is {{n_dims}}D (Required 2D)"") + break + + # Rule B: Modulo + if ne0 % block_size != 0: + is_valid = False + # Explicit debug for math check + debug_lines.append(f"" [FAIL] {{g_name}} vs {{scheme}} (Block {{block_size}}): {{w_name}} ne0={{ne0}}. {{ne0}} % {{block_size}} = {{ne0 % block_size}}"") + break + + if not is_valid: + failures.append({{ 'Group': g_name, 'Scheme': scheme }}) + else: + debug_lines.append(f""[MISSING] {{g_name}}"") + +# Write Debug +try: + with open(debug_path, 'w') as f: + f.write('\n'.join(debug_lines)) +except: + pass + +# Write Result +with open(output_path, 'w') as f: + json.dump({{ + 'FoundGroups': found_groups, + 'Incompatible': failures, + 'Error': None + }}, f, indent=2) +"; + } + + private class CompatResult + { + public List FoundGroups { get; set; } = new(); + public List Incompatible { get; set; } = new(); + public string? Error { get; set; } + } + + private class CompatFailure + { + public string Group { get; set; } = ""; + public string Scheme { get; set; } = ""; + } +} \ No newline at end of file From 9f7390d68f990c249337c808789733fa8fc485c1 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 14:01:54 -0500 Subject: [PATCH 022/258] Replacing all sbyte with byte (I had the logic backwards in my head). And renamed the DuckDB database since I'm introducing SqlIte as well. --- MagicQuant/Helpers/ComboLogic.cs | 4 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 4 +- MagicQuant/Models/BaselineQuants.cs | 2 +- MagicQuant/Models/TensorConfigs.cs | 46 ++++++++++----------- MagicQuant/Models/TensorGroup.cs | 2 +- MagicQuant/Models/TensorWeight.cs | 4 +- MagicQuant/Models/TensorWeightScheme.cs | 4 +- MagicQuant/Services/QuantDatabaseService.cs | 4 +- 8 files changed, 35 insertions(+), 35 deletions(-) diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 955cc48..d229afd 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -10,7 +10,7 @@ public static class ComboLogic private static readonly ImmutableArray GroupsOrdered = TReg.All.OrderBy(g => g.UniqueId).ToImmutableArray(); - public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants baseQuant) + public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants baseQuant) { bool baseRequiresImatrix = baseQuant.RequiresImatrix; @@ -22,7 +22,7 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants if (schemesForBase.IsEmpty) throw new InvalidOperationException("No tensor schemes available for this base."); - var builder = ImmutableArray.CreateBuilder(); + var builder = ImmutableArray.CreateBuilder(); foreach (var group in GroupsOrdered) { diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index c8231c6..6436e5e 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -16,7 +16,7 @@ public static List GenerateRequiredDataSampleCombos(List(); // Fast lookup for missing groups - var missingIds = MissingTensorGroup?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + var missingIds = MissingTensorGroup?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); // --------------------------------------------------------- // 1. BASELINE CONTROLS (One pure sample per allowed baseline) @@ -141,7 +141,7 @@ public static IEnumerable> GenerateTensorConfigBatches( int dop = ComputeWorkerThreads(GetThreadCountSafe()); // Cache baseQuant.UniqueId once (perf) - sbyte baseId = baseQuant.UniqueId; + byte baseId = baseQuant.UniqueId; var queue = new BlockingCollection>( boundedCapacity: Math.Max(2, dop * 2)); diff --git a/MagicQuant/Models/BaselineQuants.cs b/MagicQuant/Models/BaselineQuants.cs index 9df8900..4631e8f 100644 --- a/MagicQuant/Models/BaselineQuants.cs +++ b/MagicQuant/Models/BaselineQuants.cs @@ -3,7 +3,7 @@ namespace MagicQuant.Models; public record BaselineQuants( - sbyte UniqueId, + byte UniqueId, bool RequiresImatrix, ImmutableArray Names, HybridQuant? BaseConversionBase = null) diff --git a/MagicQuant/Models/TensorConfigs.cs b/MagicQuant/Models/TensorConfigs.cs index 973ecb3..c3be0d5 100644 --- a/MagicQuant/Models/TensorConfigs.cs +++ b/MagicQuant/Models/TensorConfigs.cs @@ -7,28 +7,28 @@ namespace MagicQuant.Models; [StructLayout(LayoutKind.Sequential, Pack = 1)] public readonly struct TensorConfig { - public readonly sbyte BaseQuant; - public readonly sbyte Embeddings; - public readonly sbyte LmHead; - public readonly sbyte AttnQ; - public readonly sbyte AttnKV; - public readonly sbyte AttnOutput; - public readonly sbyte FfnUpGate; - public readonly sbyte FfnDown; - public readonly sbyte MoeExperts; - public readonly sbyte MoeRouter; + public readonly byte BaseQuant; + public readonly byte Embeddings; + public readonly byte LmHead; + public readonly byte AttnQ; + public readonly byte AttnKV; + public readonly byte AttnOutput; + public readonly byte FfnUpGate; + public readonly byte FfnDown; + public readonly byte MoeExperts; + public readonly byte MoeRouter; public TensorConfig( - sbyte baseQuant, - sbyte embeddings, - sbyte lmHead, - sbyte attnQ, - sbyte attnKV, - sbyte attnOutput, - sbyte ffnUpGate, - sbyte ffnDown, - sbyte moeExperts, - sbyte moeRouter) + byte baseQuant, + byte embeddings, + byte lmHead, + byte attnQ, + byte attnKV, + byte attnOutput, + byte ffnUpGate, + byte ffnDown, + byte moeExperts, + byte moeRouter) { BaseQuant = baseQuant; Embeddings = embeddings; @@ -45,7 +45,7 @@ public TensorConfig( // Converting constructor: HybridQuant -> TensorConfig public TensorConfig(HybridQuant h) : this( - baseQuant: checked((sbyte)h.BaseQuant.UniqueId), + baseQuant: checked((byte)h.BaseQuant.UniqueId), embeddings: GetSchemeId(h, TReg.Embeddings), lmHead: GetSchemeId(h, TReg.LmHead), attnQ: GetSchemeId(h, TReg.AttnQ), @@ -57,7 +57,7 @@ public TensorConfig(HybridQuant h) moeRouter: GetSchemeId(h, TReg.MoeRouter)) { } - private static sbyte GetSchemeId(HybridQuant h, TensorGroup group) + private static byte GetSchemeId(HybridQuant h, TensorGroup group) { if (h.Tensors == null) throw new ArgumentNullException(nameof(h.Tensors)); @@ -85,7 +85,7 @@ private static sbyte GetSchemeId(HybridQuant h, TensorGroup group) throw new InvalidOperationException( $"HybridQuant missing tensor entry for group '{group.Name}' (UniqueId={group.UniqueId})."); - return checked((sbyte)found.UniqueId); + return checked((byte)found.UniqueId); } // Conversion operator: HybridQuant -> TensorConfig diff --git a/MagicQuant/Models/TensorGroup.cs b/MagicQuant/Models/TensorGroup.cs index a1f507f..22bcb8b 100644 --- a/MagicQuant/Models/TensorGroup.cs +++ b/MagicQuant/Models/TensorGroup.cs @@ -12,7 +12,7 @@ public class TensorGroupInfo /// /// Represents a categorized group of tensors with a unique name and matching patterns. /// -public record TensorGroup(sbyte UniqueId, string Name, ImmutableArray Tensors) +public record TensorGroup(byte UniqueId, string Name, ImmutableArray Tensors) { /// /// Helper to map the group name to a single-character identifier for CLI or UI display. diff --git a/MagicQuant/Models/TensorWeight.cs b/MagicQuant/Models/TensorWeight.cs index c938b91..c99a4fa 100644 --- a/MagicQuant/Models/TensorWeight.cs +++ b/MagicQuant/Models/TensorWeight.cs @@ -2,7 +2,7 @@ namespace MagicQuant.Models; public class TensorWeight { - public TensorWeight(sbyte uniqueId, bool requiresImatrix, string[] names, TensorGroup[]? bannedGroups = null) + public TensorWeight(byte uniqueId, bool requiresImatrix, string[] names, TensorGroup[]? bannedGroups = null) { Names = names.ToList(); UniqueId = uniqueId; @@ -41,7 +41,7 @@ public string GetName(string? name = null) } public List? Names { get; } - public sbyte UniqueId { get; } + public byte UniqueId { get; } public bool RequiresImatrix { get; } diff --git a/MagicQuant/Models/TensorWeightScheme.cs b/MagicQuant/Models/TensorWeightScheme.cs index 863a403..30ca33a 100644 --- a/MagicQuant/Models/TensorWeightScheme.cs +++ b/MagicQuant/Models/TensorWeightScheme.cs @@ -4,7 +4,7 @@ namespace MagicQuant.Models; public sealed class TensorWeightScheme { - public sbyte UniqueId { get; } + public byte UniqueId { get; } public bool RequiresImatrix { get; } public ImmutableArray Names { get; } public List BannedGroups { get; } @@ -13,7 +13,7 @@ public sealed class TensorWeightScheme public ushort? BlockNeo { get; } private TensorWeightScheme( - sbyte uniqueId, + byte uniqueId, bool requiresImatrix, ImmutableArray names, IEnumerable bannedGroups, diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index e9801ee..032ba75 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -9,7 +9,7 @@ namespace MagicQuant.Services; public class QuantDatabaseService { - private const string DbFileName = "MagicQuant.duckdb"; + private const string DbFileName = "MagicQuant_Combinations.duckdb"; private const string TableName = "tensor_configs"; // Connection string points to the file in your cache directory @@ -63,7 +63,7 @@ private async Task RebuildDatabaseAsync(DuckDBConnection connection, BigInteger var sw = Stopwatch.StartNew(); // 1. Drop and Recreate Table - // We map sbyte (C#) to TINYINT (DuckDB) + // We map byte (C#) to TINYINT (DuckDB) var createCmd = connection.CreateCommand(); createCmd.CommandText = $@" DROP TABLE IF EXISTS {TableName}; From 55284427652769afaada39b5b0c6016061269b37 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 14:28:53 -0500 Subject: [PATCH 023/258] Adding new AI model ID code. Also changing the location of the unsued tensors list to Cache instead of config. --- MagicQuant/Cache.cs | 8 ++ MagicQuant/Commands/Evolution.cs | 4 + MagicQuant/Config.cs | 6 -- MagicQuant/Helpers/CliHelpers.cs | 2 +- MagicQuant/Helpers/MagicQuantModelId.cs | 98 +++++++++++++++++++ MagicQuant/MagicQuant.csproj | 1 + .../Services/ModelCompatibilityService.cs | 6 +- 7 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 MagicQuant/Helpers/MagicQuantModelId.cs diff --git a/MagicQuant/Cache.cs b/MagicQuant/Cache.cs index 5d4ce35..5dbe6ba 100644 --- a/MagicQuant/Cache.cs +++ b/MagicQuant/Cache.cs @@ -49,4 +49,12 @@ public enum MainTorchType F16 = 2, F32 = 3 } + + + /* + * This is properly updated, but not really used. More for generic logs because the + * TensorWeightScheme is what's actually updated with the real ban logic both from the + * start and during runtime + */ + public static List UnusedTensorGroups = new List(); } \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 23ca9f6..1aacef2 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -77,6 +77,10 @@ public async Task Run(List args) // If not, we might want to warn or rely on defaults. AnsiConsole.MarkupLine("[yellow]Warning: Llama binaries path not set in Cache. (Did Initialization run?)[/]"); } + + + var ID = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); + var pyManager = new PythonManager(Cache.MagicQuantDirectory); var bService = new BenchmarkService(pyManager); var qService = new QuantizationService(bService); diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index c3b5a94..20a4535 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -8,12 +8,6 @@ public static class Config public static readonly int MaxSurvivalRounds = 4; public static readonly double CollapseMultiplier = 1.5; - /* - * This is properly updated, but not really used. More for generic logs because the - * TensorWeightScheme is what's actually updated with the real ban logic both from the - * start and during runtime - */ - public static List UnusedTensorGroups = new List(); public static readonly List SensitivityProbeGroups = new() { diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index 7c449a5..5135330 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -111,7 +111,7 @@ public static void ValidateCombinationLogicWorks(bool realResults = false) } else { - var RealBans = TensorConfigGenerator.GenerateRequiredDataSampleCombos(Config.UnusedTensorGroups); + var RealBans = TensorConfigGenerator.GenerateRequiredDataSampleCombos(Cache.UnusedTensorGroups); Console.WriteLine(); Console.WriteLine("---------------"); diff --git a/MagicQuant/Helpers/MagicQuantModelId.cs b/MagicQuant/Helpers/MagicQuantModelId.cs new file mode 100644 index 0000000..8792ba0 --- /dev/null +++ b/MagicQuant/Helpers/MagicQuantModelId.cs @@ -0,0 +1,98 @@ +using System; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.Json; +using Blake3; + + +namespace MagicQuant.Helpers; + +public static class MagicQuantModelId +{ + private const string IdFileName = "MagicQuant.id.json"; + private const string IdPrefix = "mq-blake3:"; + + private sealed class ModelIdWrapper + { + public string Id { get; set; } = default!; + } + + public static string GetOrCreateModelId(string modelDirectory) + { + if (string.IsNullOrWhiteSpace(modelDirectory)) + throw new ArgumentException("Model directory path is null or empty.", nameof(modelDirectory)); + + if (!Directory.Exists(modelDirectory)) + throw new DirectoryNotFoundException($"Directory does not exist: {modelDirectory}"); + + string idFilePath = Path.Combine(modelDirectory, IdFileName); + + // 🚀 Fast path: cached ID exists + if (File.Exists(idFilePath)) + { + var wrapper = JsonSerializer.Deserialize(File.ReadAllText(idFilePath)); + if (string.IsNullOrWhiteSpace(wrapper?.Id)) + throw new InvalidDataException($"{IdFileName} exists but is invalid (missing Id)."); + + return wrapper.Id; + } + + // 🔍 Find safetensors + var safetensors = Directory + .EnumerateFiles(modelDirectory, "*.safetensors", SearchOption.TopDirectoryOnly) + .OrderBy(f => f, StringComparer.OrdinalIgnoreCase) + .ToArray(); + + if (safetensors.Length == 0) + throw new InvalidOperationException($"No .safetensors files found in directory: {modelDirectory}"); + + // 🧠 Hash tensor payloads deterministically + using var hasher = Hasher.New(); + + foreach (var file in safetensors) + HashSafetensorPayload(file, hasher); + + // ✅ Blake3.NET: Finalize() returns Blake3.Hash which stringifies to hex + var hash = hasher.Finalize(); + string finalHashHex = hash.ToString(); // hex digest (lowercase) + string finalId = IdPrefix + finalHashHex; + + // 💾 Persist ID + var output = new ModelIdWrapper { Id = finalId }; + + File.WriteAllText( + idFilePath, + JsonSerializer.Serialize(output, new JsonSerializerOptions { WriteIndented = true }), + Encoding.UTF8 + ); + + return finalId; + } + + /// + /// Hashes ONLY the tensor payload of a safetensors file (skips header + JSON metadata). + /// Safetensors format: [u64 header_len][header_json_bytes][tensor_bytes...] + /// + private static void HashSafetensorPayload(string filePath, Hasher hasher) + { + using var stream = File.OpenRead(filePath); + using var reader = new BinaryReader(stream); + + // UInt64 metadata length (little endian) + ulong metadataLength = reader.ReadUInt64(); + + long tensorDataOffset = 8L + checked((long)metadataLength); + + if (tensorDataOffset >= stream.Length) + throw new InvalidDataException($"Safetensors file is malformed (bad header length): {filePath}"); + + stream.Position = tensorDataOffset; + + byte[] buffer = new byte[1024 * 1024]; // 1MB + int bytesRead; + + while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) + hasher.Update(buffer.AsSpan(0, bytesRead)); + } +} \ No newline at end of file diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index d5843f4..92a2bf9 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -8,6 +8,7 @@ + diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 397f887..c996107 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -73,7 +73,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) // --------------------------------------------------------- TensorWeightScheme.NULL.BannedGroups.Clear(); - MagicQuant.Config.UnusedTensorGroups.Clear(); + MagicQuant.Cache.UnusedTensorGroups.Clear(); int unusedCount = 0; int usedCount = 0; @@ -93,7 +93,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) else { unusedCount++; - MagicQuant.Config.UnusedTensorGroups.Add(group); + MagicQuant.Cache.UnusedTensorGroups.Add(group); foreach (var scheme in TensorWeightScheme.All) { @@ -136,7 +136,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) if (unusedCount > 0) { - string unusedNames = string.Join(", ", MagicQuant.Config.UnusedTensorGroups.Select(g => g.Name)); + string unusedNames = string.Join(", ", MagicQuant.Cache.UnusedTensorGroups.Select(g => g.Name)); AnsiConsole.MarkupLine($" Unused Groups: [grey]{unusedNames}[/] (Forced to NULL)"); } From 17359e7004fb56da9f46aa9d5b4cdadf0402b064 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 14:33:12 -0500 Subject: [PATCH 024/258] new cache for saving unique model Id. --- MagicQuant/Cache.cs | 2 ++ MagicQuant/Commands/Evolution.cs | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Cache.cs b/MagicQuant/Cache.cs index 5dbe6ba..68771ed 100644 --- a/MagicQuant/Cache.cs +++ b/MagicQuant/Cache.cs @@ -57,4 +57,6 @@ public enum MainTorchType * start and during runtime */ public static List UnusedTensorGroups = new List(); + + public static string CurrentModelId { get; set; } } \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 1aacef2..58c0d81 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -78,8 +78,11 @@ public async Task Run(List args) AnsiConsole.MarkupLine("[yellow]Warning: Llama binaries path not set in Cache. (Did Initialization run?)[/]"); } - - var ID = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); + Console.WriteLine("Acquiring unique model ID..."); + + Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); + + AnsiConsole.MarkupLine($"[green] Model ID Created/Found: {Cache.CurrentModelId}[/]"); var pyManager = new PythonManager(Cache.MagicQuantDirectory); var bService = new BenchmarkService(pyManager); From 3fdc7728d28e02f3e89452895d7cf8ba7f4af31c Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 14:45:32 -0500 Subject: [PATCH 025/258] removing unused variable from Tensor Weight Scheme --- MagicQuant/MagicQuant.csproj | 1 + MagicQuant/Models/TensorWeightScheme.cs | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 92a2bf9..9fd82cd 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -23,6 +23,7 @@ + diff --git a/MagicQuant/Models/TensorWeightScheme.cs b/MagicQuant/Models/TensorWeightScheme.cs index 30ca33a..7413e64 100644 --- a/MagicQuant/Models/TensorWeightScheme.cs +++ b/MagicQuant/Models/TensorWeightScheme.cs @@ -8,8 +8,6 @@ public sealed class TensorWeightScheme public bool RequiresImatrix { get; } public ImmutableArray Names { get; } public List BannedGroups { get; } - public bool AlwaysBuild { get; } - public ushort? BlockNeo { get; } private TensorWeightScheme( From f3848d0dc4cd7d4bd7de256f4b25bd67b819d4b1 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 14:51:50 -0500 Subject: [PATCH 026/258] Adding SQLite --- MagicQuant/MagicQuant.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 9fd82cd..3bfb0b8 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -11,6 +11,7 @@ + From 07b4066a7c702163ccaf6037b4825a68815edad3 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 15:06:57 -0500 Subject: [PATCH 027/258] new class library for DB --- MQ.DB/MQ.DB.csproj | 9 +++++++++ MagicQuant-Pipeline.sln | 6 ++++++ MagicQuant/MagicQuant.csproj | 1 - 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 MQ.DB/MQ.DB.csproj diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj new file mode 100644 index 0000000..237d661 --- /dev/null +++ b/MQ.DB/MQ.DB.csproj @@ -0,0 +1,9 @@ + + + + net10.0 + enable + enable + + + diff --git a/MagicQuant-Pipeline.sln b/MagicQuant-Pipeline.sln index 8b2e7ce..a933660 100644 --- a/MagicQuant-Pipeline.sln +++ b/MagicQuant-Pipeline.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant", "MagicQuant\MagicQuant.csproj", "{9259012B-0EB2-4AD8-81E5-807FD4465AA3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQ.DB", "MQ.DB\MQ.DB.csproj", "{A97D6992-2659-47F9-9AC9-99425D2677A4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {9259012B-0EB2-4AD8-81E5-807FD4465AA3}.Debug|Any CPU.Build.0 = Debug|Any CPU {9259012B-0EB2-4AD8-81E5-807FD4465AA3}.Release|Any CPU.ActiveCfg = Release|Any CPU {9259012B-0EB2-4AD8-81E5-807FD4465AA3}.Release|Any CPU.Build.0 = Release|Any CPU + {A97D6992-2659-47F9-9AC9-99425D2677A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A97D6992-2659-47F9-9AC9-99425D2677A4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A97D6992-2659-47F9-9AC9-99425D2677A4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A97D6992-2659-47F9-9AC9-99425D2677A4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 3bfb0b8..307f975 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -24,7 +24,6 @@ - From 734cb878ed9ab9a797cb3cc090f9146660731a46 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 15:08:16 -0500 Subject: [PATCH 028/258] Removing SQLite from primary CLI and instead to MQ.DB --- MQ.DB/MQ.DB.csproj | 4 ++++ MagicQuant/MagicQuant.csproj | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj index 237d661..57d8b84 100644 --- a/MQ.DB/MQ.DB.csproj +++ b/MQ.DB/MQ.DB.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 307f975..92a2bf9 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -11,7 +11,6 @@ - From b125cc1380d9c96f347b1e4f5f7fdf388408f8ce Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 15:20:03 -0500 Subject: [PATCH 029/258] Looks like a lot, but this is just re-organization. Moving lots of models and database logic to a separate class library for better organization. --- {MagicQuant => MQ.DB}/Cache.cs | 4 +-- MQ.DB/Data/AppDbContext.cs | 31 +++++++++++++++++++ MQ.DB/MQ.DB.csproj | 2 ++ .../Models/BaselineQuants.cs | 2 +- .../Models/BenchmarkResult.cs | 2 +- {MagicQuant => MQ.DB}/Models/HybridQuant.cs | 2 +- .../Models/LlamaBenchMetrics.cs | 2 +- {MagicQuant => MQ.DB}/Models/LlamaBinaries.cs | 2 +- {MagicQuant => MQ.DB}/Models/PplMetrics.cs | 2 +- {MagicQuant => MQ.DB}/Models/SystemInfo.cs | 2 +- {MagicQuant => MQ.DB}/Models/TensorConfigs.cs | 2 +- {MagicQuant => MQ.DB}/Models/TensorGroup.cs | 2 +- {MagicQuant => MQ.DB}/Models/TensorWeight.cs | 2 +- .../Models/TensorWeightScheme.cs | 2 +- MagicQuant/Commands/Evolution.cs | 3 +- MagicQuant/Commands/InitializeLlamaCpp.cs | 2 ++ MagicQuant/Helpers/CliHelpers.cs | 2 ++ MagicQuant/Helpers/ComboLogic.cs | 3 +- MagicQuant/Helpers/DependencyManager.cs | 3 +- MagicQuant/Helpers/HardwareHelper.cs | 3 +- MagicQuant/Helpers/JsonHelper.cs | 2 ++ MagicQuant/Helpers/LlamaBuilder.cs | 3 +- MagicQuant/Helpers/PythonManager.cs | 3 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 3 +- MagicQuant/MagicQuant.csproj | 4 +++ MagicQuant/Services/BenchmarkService.cs | 3 +- .../Services/ModelCompatibilityService.cs | 9 +++--- MagicQuant/Services/QuantDatabaseService.cs | 3 +- MagicQuant/Services/QuantizationService.cs | 3 +- 29 files changed, 81 insertions(+), 27 deletions(-) rename {MagicQuant => MQ.DB}/Cache.cs (97%) create mode 100644 MQ.DB/Data/AppDbContext.cs rename {MagicQuant => MQ.DB}/Models/BaselineQuants.cs (98%) rename {MagicQuant => MQ.DB}/Models/BenchmarkResult.cs (84%) rename {MagicQuant => MQ.DB}/Models/HybridQuant.cs (98%) rename {MagicQuant => MQ.DB}/Models/LlamaBenchMetrics.cs (88%) rename {MagicQuant => MQ.DB}/Models/LlamaBinaries.cs (97%) rename {MagicQuant => MQ.DB}/Models/PplMetrics.cs (86%) rename {MagicQuant => MQ.DB}/Models/SystemInfo.cs (96%) rename {MagicQuant => MQ.DB}/Models/TensorConfigs.cs (99%) rename {MagicQuant => MQ.DB}/Models/TensorGroup.cs (99%) rename {MagicQuant => MQ.DB}/Models/TensorWeight.cs (98%) rename {MagicQuant => MQ.DB}/Models/TensorWeightScheme.cs (99%) diff --git a/MagicQuant/Cache.cs b/MQ.DB/Cache.cs similarity index 97% rename from MagicQuant/Cache.cs rename to MQ.DB/Cache.cs index 68771ed..d4aeb07 100644 --- a/MagicQuant/Cache.cs +++ b/MQ.DB/Cache.cs @@ -1,6 +1,6 @@ -using MagicQuant.Models; +using MQ.DB.Models; -namespace MagicQuant; +namespace MQ.DB; public class Cache { diff --git a/MQ.DB/Data/AppDbContext.cs b/MQ.DB/Data/AppDbContext.cs new file mode 100644 index 0000000..e9cc28d --- /dev/null +++ b/MQ.DB/Data/AppDbContext.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Sqlite; +using System.IO; + +namespace MQ.DB.Data; + +public class AppDbContext : DbContext +{ + // This represents a table in your DB. Add more DbSets here as you create models. + // public DbSet Trades { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // 1. Get the directory from your existing static Cache + var directory = Cache.MagicQuantDirectory; + + // 2. Combine with the filename + var dbPath = Path.Combine(directory, "MagicQuant_SQLite.db"); + + // 3. Configure SQLite + // EF Core for SQLite enables Foreign Keys by default (PRAGMA foreign_keys = ON), + // so you don't typically need extra configuration for that, but it handles it here. + optionsBuilder.UseSqlite($"Data Source={dbPath}"); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + // This is where you configure composite keys, default values, etc. + base.OnModelCreating(modelBuilder); + } +} \ No newline at end of file diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj index 57d8b84..5aadbcf 100644 --- a/MQ.DB/MQ.DB.csproj +++ b/MQ.DB/MQ.DB.csproj @@ -8,6 +8,8 @@ + + diff --git a/MagicQuant/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs similarity index 98% rename from MagicQuant/Models/BaselineQuants.cs rename to MQ.DB/Models/BaselineQuants.cs index 4631e8f..3f5fdc8 100644 --- a/MagicQuant/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -1,6 +1,6 @@ using System.Collections.Immutable; -namespace MagicQuant.Models; +namespace MQ.DB.Models; public record BaselineQuants( byte UniqueId, diff --git a/MagicQuant/Models/BenchmarkResult.cs b/MQ.DB/Models/BenchmarkResult.cs similarity index 84% rename from MagicQuant/Models/BenchmarkResult.cs rename to MQ.DB/Models/BenchmarkResult.cs index 1d1f2fd..3f873f4 100644 --- a/MagicQuant/Models/BenchmarkResult.cs +++ b/MQ.DB/Models/BenchmarkResult.cs @@ -1,4 +1,4 @@ -namespace MagicQuant.Models; +namespace MQ.DB.Models; public class BenchmarkResult { diff --git a/MagicQuant/Models/HybridQuant.cs b/MQ.DB/Models/HybridQuant.cs similarity index 98% rename from MagicQuant/Models/HybridQuant.cs rename to MQ.DB/Models/HybridQuant.cs index a60d2b6..37fd812 100644 --- a/MagicQuant/Models/HybridQuant.cs +++ b/MQ.DB/Models/HybridQuant.cs @@ -1,4 +1,4 @@ -namespace MagicQuant.Models; +namespace MQ.DB.Models; public class HybridQuant { diff --git a/MagicQuant/Models/LlamaBenchMetrics.cs b/MQ.DB/Models/LlamaBenchMetrics.cs similarity index 88% rename from MagicQuant/Models/LlamaBenchMetrics.cs rename to MQ.DB/Models/LlamaBenchMetrics.cs index 069f246..f696689 100644 --- a/MagicQuant/Models/LlamaBenchMetrics.cs +++ b/MQ.DB/Models/LlamaBenchMetrics.cs @@ -1,4 +1,4 @@ -namespace MagicQuant.Models; +namespace MQ.DB.Models; public class LlamaBenchMetrics { diff --git a/MagicQuant/Models/LlamaBinaries.cs b/MQ.DB/Models/LlamaBinaries.cs similarity index 97% rename from MagicQuant/Models/LlamaBinaries.cs rename to MQ.DB/Models/LlamaBinaries.cs index d20b503..f034d7b 100644 --- a/MagicQuant/Models/LlamaBinaries.cs +++ b/MQ.DB/Models/LlamaBinaries.cs @@ -1,6 +1,6 @@ using System.Runtime.InteropServices; -namespace MagicQuant.Models; +namespace MQ.DB.Models; public class LlamaBinaries { diff --git a/MagicQuant/Models/PplMetrics.cs b/MQ.DB/Models/PplMetrics.cs similarity index 86% rename from MagicQuant/Models/PplMetrics.cs rename to MQ.DB/Models/PplMetrics.cs index 58f912d..234433f 100644 --- a/MagicQuant/Models/PplMetrics.cs +++ b/MQ.DB/Models/PplMetrics.cs @@ -1,4 +1,4 @@ -namespace MagicQuant.Models; +namespace MQ.DB.Models; public class PplMetrics { diff --git a/MagicQuant/Models/SystemInfo.cs b/MQ.DB/Models/SystemInfo.cs similarity index 96% rename from MagicQuant/Models/SystemInfo.cs rename to MQ.DB/Models/SystemInfo.cs index faf58d0..8cf01da 100644 --- a/MagicQuant/Models/SystemInfo.cs +++ b/MQ.DB/Models/SystemInfo.cs @@ -1,4 +1,4 @@ -namespace MagicQuant.Models; +namespace MQ.DB.Models; public enum GpuVendor { diff --git a/MagicQuant/Models/TensorConfigs.cs b/MQ.DB/Models/TensorConfigs.cs similarity index 99% rename from MagicQuant/Models/TensorConfigs.cs rename to MQ.DB/Models/TensorConfigs.cs index c3be0d5..34272f1 100644 --- a/MagicQuant/Models/TensorConfigs.cs +++ b/MQ.DB/Models/TensorConfigs.cs @@ -2,7 +2,7 @@ using System.Linq; using System.Runtime.InteropServices; -namespace MagicQuant.Models; +namespace MQ.DB.Models; [StructLayout(LayoutKind.Sequential, Pack = 1)] public readonly struct TensorConfig diff --git a/MagicQuant/Models/TensorGroup.cs b/MQ.DB/Models/TensorGroup.cs similarity index 99% rename from MagicQuant/Models/TensorGroup.cs rename to MQ.DB/Models/TensorGroup.cs index 22bcb8b..585113d 100644 --- a/MagicQuant/Models/TensorGroup.cs +++ b/MQ.DB/Models/TensorGroup.cs @@ -2,7 +2,7 @@ using System.Linq; -namespace MagicQuant.Models; +namespace MQ.DB.Models; public class TensorGroupInfo { diff --git a/MagicQuant/Models/TensorWeight.cs b/MQ.DB/Models/TensorWeight.cs similarity index 98% rename from MagicQuant/Models/TensorWeight.cs rename to MQ.DB/Models/TensorWeight.cs index c99a4fa..e26d2d2 100644 --- a/MagicQuant/Models/TensorWeight.cs +++ b/MQ.DB/Models/TensorWeight.cs @@ -1,4 +1,4 @@ -namespace MagicQuant.Models; +namespace MQ.DB.Models; public class TensorWeight { diff --git a/MagicQuant/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs similarity index 99% rename from MagicQuant/Models/TensorWeightScheme.cs rename to MQ.DB/Models/TensorWeightScheme.cs index 7413e64..47e1b68 100644 --- a/MagicQuant/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -1,6 +1,6 @@ using System.Collections.Immutable; -namespace MagicQuant.Models; +namespace MQ.DB.Models; public sealed class TensorWeightScheme { diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 58c0d81..71e7c30 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -1,6 +1,7 @@ using MagicQuant.Models; using MagicQuant.Helpers; -using MagicQuant; +using MQ.DB; +using MQ.DB.Models; using MagicQuant.Services; using Spectre.Console; diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 8c0a25e..361c9b7 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -3,6 +3,8 @@ using Spectre.Console; using System.Runtime.InteropServices; using System.Diagnostics; +using MQ.DB; +using MQ.DB.Models; namespace MagicQuant.Commands; diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index 5135330..71fe65a 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -5,6 +5,8 @@ using MagicQuant.Commands; using MagicQuant.Models; using Spectre.Console; +using MQ.DB; +using MQ.DB.Models; namespace MagicQuant.Helpers; diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index d229afd..afd1c65 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -1,5 +1,6 @@ using System.Numerics; -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using System.Collections.Immutable; namespace MagicQuant.Helpers; diff --git a/MagicQuant/Helpers/DependencyManager.cs b/MagicQuant/Helpers/DependencyManager.cs index 2ebb9f1..70d9052 100644 --- a/MagicQuant/Helpers/DependencyManager.cs +++ b/MagicQuant/Helpers/DependencyManager.cs @@ -1,7 +1,8 @@ using System.Diagnostics; using System.IO.Compression; using System.Runtime.InteropServices; -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Helpers; diff --git a/MagicQuant/Helpers/HardwareHelper.cs b/MagicQuant/Helpers/HardwareHelper.cs index 69df39f..0178495 100644 --- a/MagicQuant/Helpers/HardwareHelper.cs +++ b/MagicQuant/Helpers/HardwareHelper.cs @@ -1,5 +1,6 @@ using System.Runtime.InteropServices; -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using System.Diagnostics; namespace MagicQuant.Helpers; diff --git a/MagicQuant/Helpers/JsonHelper.cs b/MagicQuant/Helpers/JsonHelper.cs index 584660a..2505e08 100644 --- a/MagicQuant/Helpers/JsonHelper.cs +++ b/MagicQuant/Helpers/JsonHelper.cs @@ -1,5 +1,7 @@ using System.Text.Json; using Spectre.Console; +using MQ.DB; +using MQ.DB.Models; namespace MagicQuant.Helpers; diff --git a/MagicQuant/Helpers/LlamaBuilder.cs b/MagicQuant/Helpers/LlamaBuilder.cs index e976039..2bc25ec 100644 --- a/MagicQuant/Helpers/LlamaBuilder.cs +++ b/MagicQuant/Helpers/LlamaBuilder.cs @@ -1,7 +1,8 @@ using System.Diagnostics; using System.Runtime.InteropServices; using LibGit2Sharp; -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Helpers; diff --git a/MagicQuant/Helpers/PythonManager.cs b/MagicQuant/Helpers/PythonManager.cs index e2d6b66..75301d3 100644 --- a/MagicQuant/Helpers/PythonManager.cs +++ b/MagicQuant/Helpers/PythonManager.cs @@ -1,7 +1,8 @@ using System.Diagnostics; using System.IO.Compression; using System.Runtime.InteropServices; -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Helpers; diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 6436e5e..6441799 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -1,4 +1,5 @@ -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using System.Collections.Concurrent; using System.Collections.Immutable; using Spectre.Console; diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 92a2bf9..c45bdc5 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -25,4 +25,8 @@ + + + + diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 33bd713..94f831b 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -3,7 +3,8 @@ using System.Text.Json; using System.Text.RegularExpressions; using MagicQuant.Helpers; -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Services; diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index c996107..cb4d936 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -1,6 +1,7 @@ using System.Text.Json; using MagicQuant.Helpers; -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Services; @@ -73,7 +74,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) // --------------------------------------------------------- TensorWeightScheme.NULL.BannedGroups.Clear(); - MagicQuant.Cache.UnusedTensorGroups.Clear(); + Cache.UnusedTensorGroups.Clear(); int unusedCount = 0; int usedCount = 0; @@ -93,7 +94,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) else { unusedCount++; - MagicQuant.Cache.UnusedTensorGroups.Add(group); + Cache.UnusedTensorGroups.Add(group); foreach (var scheme in TensorWeightScheme.All) { @@ -136,7 +137,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) if (unusedCount > 0) { - string unusedNames = string.Join(", ", MagicQuant.Cache.UnusedTensorGroups.Select(g => g.Name)); + string unusedNames = string.Join(", ", Cache.UnusedTensorGroups.Select(g => g.Name)); AnsiConsole.MarkupLine($" Unused Groups: [grey]{unusedNames}[/] (Forced to NULL)"); } diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 032ba75..14341ca 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -2,7 +2,8 @@ using System.Numerics; using DuckDB.NET.Data; using MagicQuant.Helpers; -using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Services; diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index af9403a..60b6c7b 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1,9 +1,10 @@ -using MagicQuant.Models; using Spectre.Console; using System.Collections.Concurrent; using System.Diagnostics; using System.Runtime.InteropServices; using MagicQuant.Helpers; +using MQ.DB; +using MQ.DB.Models; namespace MagicQuant.Services; From af122ce9cb2efb870e848721813414bf98563ed8 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 15:23:29 -0500 Subject: [PATCH 030/258] Basic SQLite code first pattern. --- MQ.DB/Data/AppDbContext.cs | 12 +-------- MQ.DB/Services/SqlService.cs | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 MQ.DB/Services/SqlService.cs diff --git a/MQ.DB/Data/AppDbContext.cs b/MQ.DB/Data/AppDbContext.cs index e9cc28d..a32398a 100644 --- a/MQ.DB/Data/AppDbContext.cs +++ b/MQ.DB/Data/AppDbContext.cs @@ -6,21 +6,11 @@ namespace MQ.DB.Data; public class AppDbContext : DbContext { - // This represents a table in your DB. Add more DbSets here as you create models. - // public DbSet Trades { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - // 1. Get the directory from your existing static Cache var directory = Cache.MagicQuantDirectory; - - // 2. Combine with the filename var dbPath = Path.Combine(directory, "MagicQuant_SQLite.db"); - - // 3. Configure SQLite - // EF Core for SQLite enables Foreign Keys by default (PRAGMA foreign_keys = ON), - // so you don't typically need extra configuration for that, but it handles it here. - optionsBuilder.UseSqlite($"Data Source={dbPath}"); + optionsBuilder.UseSqlite($"Data Source={dbPath};Foreign Keys=True;"); } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/MQ.DB/Services/SqlService.cs b/MQ.DB/Services/SqlService.cs new file mode 100644 index 0000000..b99a61f --- /dev/null +++ b/MQ.DB/Services/SqlService.cs @@ -0,0 +1,51 @@ +using Microsoft.EntityFrameworkCore; +using MQ.DB.Data; + +namespace MQ.DB.Services; + +public static class SqlService +{ + // Static flag to track if we've checked/migrated in this runtime session + private static bool _isInitialized = false; + + // Lock object to prevent race conditions if two threads call GetContext simultaneously at start + private static readonly object _initLock = new object(); + + /// + /// Gets a ready-to-use DbContext. + /// Automatically handles directory creation and DB Migrations on the first call. + /// + public static AppDbContext GetContext() + { + // 1. Check if we've already initialized in this runtime + if (!_isInitialized) + { + lock (_initLock) + { + if (!_isInitialized) + { + InitializeDatabase(); + _isInitialized = true; + } + } + } + + // 2. Return a new instance for the unit of work + return new AppDbContext(); + } + + private static void InitializeDatabase() + { + // Ensure the directory exists (using your Cache static path) + var directory = Cache.MagicQuantDirectory; + if (!Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + using (var db = new AppDbContext()) + { + db.Database.Migrate(); + } + } +} \ No newline at end of file From a93b3962e4158dbd93f6cab0d3244e3bff572c11 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 15:36:25 -0500 Subject: [PATCH 031/258] Adding my code first template that I like. --- MQ.DB/Data/AppDbContext.cs | 33 +++++++++++++++++++++++- MQ.DB/Interfaces/ISQLiteEntity.cs | 10 +++++++ MQ.DB/Models/DbModels/AiModelHashToId.cs | 19 ++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 MQ.DB/Interfaces/ISQLiteEntity.cs create mode 100644 MQ.DB/Models/DbModels/AiModelHashToId.cs diff --git a/MQ.DB/Data/AppDbContext.cs b/MQ.DB/Data/AppDbContext.cs index a32398a..566fa4a 100644 --- a/MQ.DB/Data/AppDbContext.cs +++ b/MQ.DB/Data/AppDbContext.cs @@ -1,6 +1,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Sqlite; using System.IO; +using System.Reflection; +using MQ.DB.Interfaces; namespace MQ.DB.Data; @@ -15,7 +17,36 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { - // This is where you configure composite keys, default values, etc. + // Fast and automatic EF config loading + modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly); + + // Manual check to ensure all DbSet have IAutoEntityTypeConfiguration + var dbSetTypes = this.GetType() + .GetProperties() + .Where(p => p.PropertyType.IsGenericType && + p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)) + .Select(p => p.PropertyType.GetGenericArguments()[0]) + .ToList(); + + var configuredTypes = Assembly.GetExecutingAssembly() + .GetTypes() + .Where(t => !t.IsInterface && !t.IsAbstract) + .SelectMany(t => + t.GetInterfaces() + .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ISQLiteEntity<>)) + .Select(i => i.GetGenericArguments()[0]) + ).ToHashSet(); + + foreach (var dbSetType in dbSetTypes) + { + if (!configuredTypes.Contains(dbSetType)) + { + throw new InvalidOperationException( + $"DbSet<{dbSetType.Name}> is declared but does not implement IAutoEntityTypeConfiguration<{dbSetType.Name}>." + ); + } + } + base.OnModelCreating(modelBuilder); } } \ No newline at end of file diff --git a/MQ.DB/Interfaces/ISQLiteEntity.cs b/MQ.DB/Interfaces/ISQLiteEntity.cs new file mode 100644 index 0000000..3e6cba9 --- /dev/null +++ b/MQ.DB/Interfaces/ISQLiteEntity.cs @@ -0,0 +1,10 @@ +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace MQ.DB.Interfaces; +using Microsoft.EntityFrameworkCore; + +internal interface ISQLiteEntity : IEntityTypeConfiguration + where T : class +{ + +} \ No newline at end of file diff --git a/MQ.DB/Models/DbModels/AiModelHashToId.cs b/MQ.DB/Models/DbModels/AiModelHashToId.cs new file mode 100644 index 0000000..d6467bb --- /dev/null +++ b/MQ.DB/Models/DbModels/AiModelHashToId.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +/// +/// The created blake3 hash ID associated to a proper +/// +public class AiModelHashToId : ISQLiteEntity +{ + public int Id { get; set; } + public string UniqueHash { get; set; } + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.HasIndex(h => h.UniqueHash); + } +} \ No newline at end of file From fc4747e7e23101e1027a7ed717a9adae70b2eabd Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 15:39:03 -0500 Subject: [PATCH 032/258] Just a bit more performant and cleaned up. --- MQ.DB/Data/AppDbContext.cs | 49 ++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/MQ.DB/Data/AppDbContext.cs b/MQ.DB/Data/AppDbContext.cs index 566fa4a..38268ed 100644 --- a/MQ.DB/Data/AppDbContext.cs +++ b/MQ.DB/Data/AppDbContext.cs @@ -17,36 +17,43 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { - // Fast and automatic EF config loading + // Load all configs found in this assembly. + // Pick up entities with ISQLiteEntity modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly); - // Manual check to ensure all DbSet have IAutoEntityTypeConfiguration - var dbSetTypes = this.GetType() + // Ensure strict adherence to the pattern. + ValidateDbSetsImplementInterface(); + + base.OnModelCreating(modelBuilder); + } + + private void ValidateDbSetsImplementInterface() + { + // Get all properties that are DbSet + var dbSetGenericTypes = this.GetType() .GetProperties() .Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)) .Select(p => p.PropertyType.GetGenericArguments()[0]) - .ToList(); + .ToHashSet(); - var configuredTypes = Assembly.GetExecutingAssembly() + // Get all types in assembly that implement ISQLiteEntity + var configuredTypes = typeof(AppDbContext).Assembly .GetTypes() - .Where(t => !t.IsInterface && !t.IsAbstract) - .SelectMany(t => - t.GetInterfaces() - .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ISQLiteEntity<>)) - .Select(i => i.GetGenericArguments()[0]) - ).ToHashSet(); - - foreach (var dbSetType in dbSetTypes) + .Where(t => t.GetInterfaces().Any(i => + i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ISQLiteEntity<>))) + .ToHashSet(); + + // Find the difference + var missingConfigs = dbSetGenericTypes.Except(configuredTypes).ToList(); + + if (missingConfigs.Any()) { - if (!configuredTypes.Contains(dbSetType)) - { - throw new InvalidOperationException( - $"DbSet<{dbSetType.Name}> is declared but does not implement IAutoEntityTypeConfiguration<{dbSetType.Name}>." - ); - } + var names = string.Join(", ", missingConfigs.Select(t => t.Name)); + throw new InvalidOperationException( + $"STRICT MODE ERROR: The following DbSets do not implement ISQLiteEntity: [{names}]. " + + "Please implement the interface to ensure configuration is centralized." + ); } - - base.OnModelCreating(modelBuilder); } } \ No newline at end of file From 04a94002622d366b8ce8c4be4fb76455d933abde Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 15:54:08 -0500 Subject: [PATCH 033/258] Forgot to add the empty base constructor and needed another package for code first build. --- MQ.DB/Data/AppDbContext.cs | 2 ++ MQ.DB/MQ.DB.csproj | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/MQ.DB/Data/AppDbContext.cs b/MQ.DB/Data/AppDbContext.cs index 38268ed..b11ee67 100644 --- a/MQ.DB/Data/AppDbContext.cs +++ b/MQ.DB/Data/AppDbContext.cs @@ -8,6 +8,8 @@ namespace MQ.DB.Data; public class AppDbContext : DbContext { + public AppDbContext(DbContextOptions options) : base(options) { } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var directory = Cache.MagicQuantDirectory; diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj index 5aadbcf..793af3f 100644 --- a/MQ.DB/MQ.DB.csproj +++ b/MQ.DB/MQ.DB.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + From a4e1e80d00b679e36027fe41f4fdee883c866edf Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 15:56:35 -0500 Subject: [PATCH 034/258] Added some OnConfiguration changes and another constructor to get it to work both for service and my ef migrations. --- MQ.DB/Data/AppDbContext.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/MQ.DB/Data/AppDbContext.cs b/MQ.DB/Data/AppDbContext.cs index b11ee67..37e1b90 100644 --- a/MQ.DB/Data/AppDbContext.cs +++ b/MQ.DB/Data/AppDbContext.cs @@ -10,11 +10,28 @@ public class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } + public AppDbContext() + { + } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - var directory = Cache.MagicQuantDirectory; - var dbPath = Path.Combine(directory, "MagicQuant_SQLite.db"); - optionsBuilder.UseSqlite($"Data Source={dbPath};Foreign Keys=True;"); + // If the options are already configured, + // skip this entire block so we don't touch the Cache. + if (!optionsBuilder.IsConfigured) + { + var directory = Cache.MagicQuantDirectory; + + // If we are running a command and Cache is null, fallback to local folder. + // This prevents the "Value cannot be null" crash. + if (string.IsNullOrEmpty(directory)) + { + directory = Directory.GetCurrentDirectory(); + } + + var dbPath = Path.Combine(directory, "MagicQuant_SQLite.db"); + optionsBuilder.UseSqlite($"Data Source={dbPath};Foreign Keys=True;"); + } } protected override void OnModelCreating(ModelBuilder modelBuilder) From 7acc9f3292434eaaae1a4f2e9c5ed1ab44029e08 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 16:15:03 -0500 Subject: [PATCH 035/258] initial table setup --- .../20260101205614_InitialCreate.Designer.cs | 41 ++++++++++ .../20260101205614_InitialCreate.cs | 39 +++++++++ MQ.DB/Migrations/AppDbContextModelSnapshot.cs | 82 +++++++++++++++++++ MQ.DB/Models/DbModels/AiModelHashToId.cs | 2 +- MQ.DB/Models/DbModels/TensorCombo.cs | 57 +++++++++++++ 5 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 MQ.DB/Migrations/20260101205614_InitialCreate.Designer.cs create mode 100644 MQ.DB/Migrations/20260101205614_InitialCreate.cs create mode 100644 MQ.DB/Migrations/AppDbContextModelSnapshot.cs create mode 100644 MQ.DB/Models/DbModels/TensorCombo.cs diff --git a/MQ.DB/Migrations/20260101205614_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260101205614_InitialCreate.Designer.cs new file mode 100644 index 0000000..66ecc36 --- /dev/null +++ b/MQ.DB/Migrations/20260101205614_InitialCreate.Designer.cs @@ -0,0 +1,41 @@ +// +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20260101205614_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHashToId", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashToId"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260101205614_InitialCreate.cs b/MQ.DB/Migrations/20260101205614_InitialCreate.cs new file mode 100644 index 0000000..b4e56ef --- /dev/null +++ b/MQ.DB/Migrations/20260101205614_InitialCreate.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AiModelHashToId", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + UniqueHash = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AiModelHashToId", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_AiModelHashToId_UniqueHash", + table: "AiModelHashToId", + column: "UniqueHash"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AiModelHashToId"); + } + } +} diff --git a/MQ.DB/Migrations/AppDbContextModelSnapshot.cs b/MQ.DB/Migrations/AppDbContextModelSnapshot.cs new file mode 100644 index 0000000..da889f7 --- /dev/null +++ b/MQ.DB/Migrations/AppDbContextModelSnapshot.cs @@ -0,0 +1,82 @@ +// +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(AppDbContext))] + partial class AppDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHashToId", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashToId"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombo"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Models/DbModels/AiModelHashToId.cs b/MQ.DB/Models/DbModels/AiModelHashToId.cs index d6467bb..519e907 100644 --- a/MQ.DB/Models/DbModels/AiModelHashToId.cs +++ b/MQ.DB/Models/DbModels/AiModelHashToId.cs @@ -8,7 +8,7 @@ namespace MQ.DB.Models.DbModels; /// public class AiModelHashToId : ISQLiteEntity { - public int Id { get; set; } + public uint Id { get; set; } public string UniqueHash { get; set; } public void Configure(EntityTypeBuilder builder) diff --git a/MQ.DB/Models/DbModels/TensorCombo.cs b/MQ.DB/Models/DbModels/TensorCombo.cs new file mode 100644 index 0000000..25f550c --- /dev/null +++ b/MQ.DB/Models/DbModels/TensorCombo.cs @@ -0,0 +1,57 @@ +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class TensorCombo : ISQLiteEntity +{ + public TensorCombo() + { + } + + // Connection to TensorConfig + public TensorCombo(TensorConfig c) + { + BaseQuant = c.BaseQuant; + Embeddings = c.Embeddings; + LmHead = c.LmHead; + AttnQ = c.AttnQ; + AttnKV = c.AttnKV; + AttnOutput = c.AttnOutput; + FfnUpGate = c.FfnUpGate; + FfnDown = c.FfnDown; + MoeExperts = c.MoeExperts; + MoeRouter = c.MoeRouter; + } + + public uint Id { get; set; } + public readonly byte BaseQuant; + public readonly byte Embeddings; + public readonly byte LmHead; + public readonly byte AttnQ; + public readonly byte AttnKV; + public readonly byte AttnOutput; + public readonly byte FfnUpGate; + public readonly byte FfnDown; + public readonly byte MoeExperts; + public readonly byte MoeRouter; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + + builder.HasIndex(x => new + { + x.BaseQuant, + x.Embeddings, + x.LmHead, + x.AttnQ, + x.AttnKV, + x.AttnOutput, + x.FfnUpGate, + x.FfnDown, + x.MoeExperts, + x.MoeRouter + }).IsUnique();; + } +} \ No newline at end of file From 3115978b7631c827a87aecd96e897d449f0eedb5 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 16:17:35 -0500 Subject: [PATCH 036/258] TensorCombo table and update to AiModelHashToId to uint. --- .../20260101211347_tablechanges.Designer.cs | 85 +++++++++++++++++++ .../Migrations/20260101211347_tablechanges.cs | 49 +++++++++++ 2 files changed, 134 insertions(+) create mode 100644 MQ.DB/Migrations/20260101211347_tablechanges.Designer.cs create mode 100644 MQ.DB/Migrations/20260101211347_tablechanges.cs diff --git a/MQ.DB/Migrations/20260101211347_tablechanges.Designer.cs b/MQ.DB/Migrations/20260101211347_tablechanges.Designer.cs new file mode 100644 index 0000000..4f0f85c --- /dev/null +++ b/MQ.DB/Migrations/20260101211347_tablechanges.Designer.cs @@ -0,0 +1,85 @@ +// +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20260101211347_tablechanges")] + partial class tablechanges + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHashToId", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashToId"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombo"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260101211347_tablechanges.cs b/MQ.DB/Migrations/20260101211347_tablechanges.cs new file mode 100644 index 0000000..a1faf1b --- /dev/null +++ b/MQ.DB/Migrations/20260101211347_tablechanges.cs @@ -0,0 +1,49 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class tablechanges : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "TensorCombo", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AttnKV = table.Column(type: "INTEGER", nullable: false), + AttnOutput = table.Column(type: "INTEGER", nullable: false), + AttnQ = table.Column(type: "INTEGER", nullable: false), + BaseQuant = table.Column(type: "INTEGER", nullable: false), + Embeddings = table.Column(type: "INTEGER", nullable: false), + FfnDown = table.Column(type: "INTEGER", nullable: false), + FfnUpGate = table.Column(type: "INTEGER", nullable: false), + LmHead = table.Column(type: "INTEGER", nullable: false), + MoeExperts = table.Column(type: "INTEGER", nullable: false), + MoeRouter = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TensorCombo", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_TensorCombo_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", + table: "TensorCombo", + columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "TensorCombo"); + } + } +} From 8741f17ec972dc2d900f09dee496fddb46a803b9 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 16:59:45 -0500 Subject: [PATCH 037/258] Removing the database migrations because I'm making too many changes right now. Additionally changing class names and making the new ai benchmark table --- MQ.DB/MQ.DB.csproj | 4 + .../20260101205614_InitialCreate.Designer.cs | 41 --------- .../20260101205614_InitialCreate.cs | 39 --------- .../20260101211347_tablechanges.Designer.cs | 85 ------------------- .../Migrations/20260101211347_tablechanges.cs | 49 ----------- MQ.DB/Migrations/AppDbContextModelSnapshot.cs | 82 ------------------ MQ.DB/Models/DbModels/AiBenchmark.cs | 64 ++++++++++++++ MQ.DB/Models/DbModels/AiModelHash.cs | 23 +++++ MQ.DB/Models/DbModels/AiModelHashToId.cs | 19 ----- 9 files changed, 91 insertions(+), 315 deletions(-) delete mode 100644 MQ.DB/Migrations/20260101205614_InitialCreate.Designer.cs delete mode 100644 MQ.DB/Migrations/20260101205614_InitialCreate.cs delete mode 100644 MQ.DB/Migrations/20260101211347_tablechanges.Designer.cs delete mode 100644 MQ.DB/Migrations/20260101211347_tablechanges.cs delete mode 100644 MQ.DB/Migrations/AppDbContextModelSnapshot.cs create mode 100644 MQ.DB/Models/DbModels/AiBenchmark.cs create mode 100644 MQ.DB/Models/DbModels/AiModelHash.cs delete mode 100644 MQ.DB/Models/DbModels/AiModelHashToId.cs diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj index 793af3f..c04c3b4 100644 --- a/MQ.DB/MQ.DB.csproj +++ b/MQ.DB/MQ.DB.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/MQ.DB/Migrations/20260101205614_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260101205614_InitialCreate.Designer.cs deleted file mode 100644 index 66ecc36..0000000 --- a/MQ.DB/Migrations/20260101205614_InitialCreate.Designer.cs +++ /dev/null @@ -1,41 +0,0 @@ -// -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(AppDbContext))] - [Migration("20260101205614_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHashToId", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashToId"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260101205614_InitialCreate.cs b/MQ.DB/Migrations/20260101205614_InitialCreate.cs deleted file mode 100644 index b4e56ef..0000000 --- a/MQ.DB/Migrations/20260101205614_InitialCreate.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class InitialCreate : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "AiModelHashToId", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - UniqueHash = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AiModelHashToId", x => x.Id); - }); - - migrationBuilder.CreateIndex( - name: "IX_AiModelHashToId_UniqueHash", - table: "AiModelHashToId", - column: "UniqueHash"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "AiModelHashToId"); - } - } -} diff --git a/MQ.DB/Migrations/20260101211347_tablechanges.Designer.cs b/MQ.DB/Migrations/20260101211347_tablechanges.Designer.cs deleted file mode 100644 index 4f0f85c..0000000 --- a/MQ.DB/Migrations/20260101211347_tablechanges.Designer.cs +++ /dev/null @@ -1,85 +0,0 @@ -// -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(AppDbContext))] - [Migration("20260101211347_tablechanges")] - partial class tablechanges - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHashToId", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashToId"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombo"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260101211347_tablechanges.cs b/MQ.DB/Migrations/20260101211347_tablechanges.cs deleted file mode 100644 index a1faf1b..0000000 --- a/MQ.DB/Migrations/20260101211347_tablechanges.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class tablechanges : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "TensorCombo", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - AttnKV = table.Column(type: "INTEGER", nullable: false), - AttnOutput = table.Column(type: "INTEGER", nullable: false), - AttnQ = table.Column(type: "INTEGER", nullable: false), - BaseQuant = table.Column(type: "INTEGER", nullable: false), - Embeddings = table.Column(type: "INTEGER", nullable: false), - FfnDown = table.Column(type: "INTEGER", nullable: false), - FfnUpGate = table.Column(type: "INTEGER", nullable: false), - LmHead = table.Column(type: "INTEGER", nullable: false), - MoeExperts = table.Column(type: "INTEGER", nullable: false), - MoeRouter = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_TensorCombo", x => x.Id); - }); - - migrationBuilder.CreateIndex( - name: "IX_TensorCombo_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", - table: "TensorCombo", - columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "TensorCombo"); - } - } -} diff --git a/MQ.DB/Migrations/AppDbContextModelSnapshot.cs b/MQ.DB/Migrations/AppDbContextModelSnapshot.cs deleted file mode 100644 index da889f7..0000000 --- a/MQ.DB/Migrations/AppDbContextModelSnapshot.cs +++ /dev/null @@ -1,82 +0,0 @@ -// -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(AppDbContext))] - partial class AppDbContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHashToId", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashToId"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombo"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Models/DbModels/AiBenchmark.cs b/MQ.DB/Models/DbModels/AiBenchmark.cs new file mode 100644 index 0000000..46b2f56 --- /dev/null +++ b/MQ.DB/Models/DbModels/AiBenchmark.cs @@ -0,0 +1,64 @@ +namespace MQ.DB.Models.DbModels; + +public enum BenchmarkCategory +{ + General = 1, + Math = 2, + Code = 3, +} +public class AiBenchmark +{ + public uint Id { get; set; } + + /// + /// n-N gpu layers + /// + public byte Ngl { get; set; } + + /// + /// Size of the model in bytes at this combination. + /// + /// + public ulong SizeBytes { get; set; } + + public double TokensPerSecond { get; set; } + + // both the AiModelHash and the TensorComboId combined + // must be unique in the table. + + /// + /// foreign key + /// + public uint TensorComboId { get; set; } + + /// + /// foreign key + /// + public uint AiModelHashId { get; set; } +} + +public class CategoryBenchmarkToAiBenchmark +{ + public uint Id { get; set; } + public uint AiBenchmarkId { get; set; } + public uint CategoryBenchmarkId { get; set; } +} + +public class CategoryBenchmark +{ + public uint Id { get; set; } + + /// + /// foreign key to AiBenchmark + /// + public uint AiBenchmarkId { get; set; } + + /// + /// Byte version to BenchmarkCategory enum in C# + /// + public byte Category { get; set; } + + public double Kld { get; set; } + public double Ppl { get; set; } + public double PplError { get; set; } +} \ No newline at end of file diff --git a/MQ.DB/Models/DbModels/AiModelHash.cs b/MQ.DB/Models/DbModels/AiModelHash.cs new file mode 100644 index 0000000..20a1f9c --- /dev/null +++ b/MQ.DB/Models/DbModels/AiModelHash.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +/// +/// The created blake3 hash ID associated to a proper. +/// Additionally this has a cascading delete effect. +/// If one of these rows is ever deleted, all other table +/// rows that reference this Id as a foreign key must be deleted +/// alongside this and for it to occur safely. +/// +public class AiModelHash : ISQLiteEntity +{ + public uint Id { get; set; } + public string UniqueHash { get; set; } + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.HasIndex(h => h.UniqueHash); + } +} \ No newline at end of file diff --git a/MQ.DB/Models/DbModels/AiModelHashToId.cs b/MQ.DB/Models/DbModels/AiModelHashToId.cs deleted file mode 100644 index 519e907..0000000 --- a/MQ.DB/Models/DbModels/AiModelHashToId.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using MQ.DB.Interfaces; - -namespace MQ.DB.Models.DbModels; - -/// -/// The created blake3 hash ID associated to a proper -/// -public class AiModelHashToId : ISQLiteEntity -{ - public uint Id { get; set; } - public string UniqueHash { get; set; } - - public void Configure(EntityTypeBuilder builder) - { - builder.HasKey(x => x.Id); - builder.HasIndex(h => h.UniqueHash); - } -} \ No newline at end of file From 5bdc17b25080b815523c913025c25337ac8b138d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 1 Jan 2026 17:29:54 -0500 Subject: [PATCH 038/258] database AI benchmarks --- MQ.DB/Models/DbModels/AiBenchmark.cs | 70 ++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/MQ.DB/Models/DbModels/AiBenchmark.cs b/MQ.DB/Models/DbModels/AiBenchmark.cs index 46b2f56..67c0409 100644 --- a/MQ.DB/Models/DbModels/AiBenchmark.cs +++ b/MQ.DB/Models/DbModels/AiBenchmark.cs @@ -1,3 +1,7 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + namespace MQ.DB.Models.DbModels; public enum BenchmarkCategory @@ -6,45 +10,64 @@ public enum BenchmarkCategory Math = 2, Code = 3, } -public class AiBenchmark + +public class AiBenchmark: ISQLiteEntity { public uint Id { get; set; } - + /// /// n-N gpu layers /// public byte Ngl { get; set; } - + /// /// Size of the model in bytes at this combination. /// /// public ulong SizeBytes { get; set; } - + public double TokensPerSecond { get; set; } - + // both the AiModelHash and the TensorComboId combined // must be unique in the table. - + /// /// foreign key /// public uint TensorComboId { get; set; } - + + public TensorCombo TensorCombo { get; set; } + /// /// foreign key /// public uint AiModelHashId { get; set; } -} -public class CategoryBenchmarkToAiBenchmark -{ - public uint Id { get; set; } - public uint AiBenchmarkId { get; set; } - public uint CategoryBenchmarkId { get; set; } + public AiModelHash AiModelHash { get; set; } + + public List CategorBenchmarks { get; set; } + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + + builder.HasIndex(x => new { x.AiModelHashId, x.TensorComboId }) + .IsUnique(); + + builder.HasOne(x => x.TensorCombo) + .WithMany() + .HasForeignKey(x => x.TensorComboId) + .OnDelete(DeleteBehavior.Restrict); // Prevent deleting a combo if benchmarks exist + + builder.HasOne(x => x.AiModelHash) + .WithMany() + .HasForeignKey(x => x.AiModelHashId) + .OnDelete(DeleteBehavior.Cascade); + } + } -public class CategoryBenchmark +public class CategoryBenchmark: ISQLiteEntity { public uint Id { get; set; } @@ -52,6 +75,7 @@ public class CategoryBenchmark /// foreign key to AiBenchmark /// public uint AiBenchmarkId { get; set; } + public AiBenchmark AiBenchmark { get; set; } /// /// Byte version to BenchmarkCategory enum in C# @@ -61,4 +85,22 @@ public class CategoryBenchmark public double Kld { get; set; } public double Ppl { get; set; } public double PplError { get; set; } + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + + // Foreign Key Configuration + builder.HasOne(x => x.AiBenchmark) + .WithMany() // One Benchmark has Many Category Scores + .HasForeignKey(x => x.AiBenchmarkId) + .OnDelete(DeleteBehavior.Cascade); // If you delete the Benchmark, delete its scores + + builder.HasIndex(x => x.AiBenchmarkId); + + builder.HasOne() + .WithMany(p => p.CategorBenchmarks) + .HasForeignKey(x => x.AiBenchmarkId) + .OnDelete(DeleteBehavior.Cascade); + } } \ No newline at end of file From 78bdb14392577a02d27c35bdc6f95958f6df05e1 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 11 Apr 2026 11:49:36 -0400 Subject: [PATCH 039/258] base --- MQ.DB/Data/AppDbContext.cs | 78 ------ MQ.DB/Data/MagicQuantContext.cs | 107 +++++++ MQ.DB/MQ.DB.csproj | 1 + MQ.DB/Services/SqlService.cs | 51 ---- MagicQuant/Services/BenchmarkService.cs | 306 ++++++++++++++++----- MagicQuant/Services/QuantizationService.cs | 18 +- 6 files changed, 360 insertions(+), 201 deletions(-) delete mode 100644 MQ.DB/Data/AppDbContext.cs create mode 100644 MQ.DB/Data/MagicQuantContext.cs delete mode 100644 MQ.DB/Services/SqlService.cs diff --git a/MQ.DB/Data/AppDbContext.cs b/MQ.DB/Data/AppDbContext.cs deleted file mode 100644 index 37e1b90..0000000 --- a/MQ.DB/Data/AppDbContext.cs +++ /dev/null @@ -1,78 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Sqlite; -using System.IO; -using System.Reflection; -using MQ.DB.Interfaces; - -namespace MQ.DB.Data; - -public class AppDbContext : DbContext -{ - public AppDbContext(DbContextOptions options) : base(options) { } - - public AppDbContext() - { - } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - // If the options are already configured, - // skip this entire block so we don't touch the Cache. - if (!optionsBuilder.IsConfigured) - { - var directory = Cache.MagicQuantDirectory; - - // If we are running a command and Cache is null, fallback to local folder. - // This prevents the "Value cannot be null" crash. - if (string.IsNullOrEmpty(directory)) - { - directory = Directory.GetCurrentDirectory(); - } - - var dbPath = Path.Combine(directory, "MagicQuant_SQLite.db"); - optionsBuilder.UseSqlite($"Data Source={dbPath};Foreign Keys=True;"); - } - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - // Load all configs found in this assembly. - // Pick up entities with ISQLiteEntity - modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly); - - // Ensure strict adherence to the pattern. - ValidateDbSetsImplementInterface(); - - base.OnModelCreating(modelBuilder); - } - - private void ValidateDbSetsImplementInterface() - { - // Get all properties that are DbSet - var dbSetGenericTypes = this.GetType() - .GetProperties() - .Where(p => p.PropertyType.IsGenericType && - p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)) - .Select(p => p.PropertyType.GetGenericArguments()[0]) - .ToHashSet(); - - // Get all types in assembly that implement ISQLiteEntity - var configuredTypes = typeof(AppDbContext).Assembly - .GetTypes() - .Where(t => t.GetInterfaces().Any(i => - i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ISQLiteEntity<>))) - .ToHashSet(); - - // Find the difference - var missingConfigs = dbSetGenericTypes.Except(configuredTypes).ToList(); - - if (missingConfigs.Any()) - { - var names = string.Join(", ", missingConfigs.Select(t => t.Name)); - throw new InvalidOperationException( - $"STRICT MODE ERROR: The following DbSets do not implement ISQLiteEntity: [{names}]. " + - "Please implement the interface to ensure configuration is centralized." - ); - } - } -} \ No newline at end of file diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs new file mode 100644 index 0000000..2c5e61e --- /dev/null +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -0,0 +1,107 @@ +using Microsoft.EntityFrameworkCore; +using MQ.DB.Interfaces; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; + +namespace MQ.DB.Data; + +public class MagicQuantContext : DbContext +{ + // -------------------------------------------------------- + // Self-Initialization Logic + // -------------------------------------------------------- + private static bool _isInitialized = false; + private static readonly object _initLock = new(); + + public MagicQuantContext() + { + // On the very first instantiation (e.g., first benchmark run), + // we ensure the folder exists and migrations are applied. + if (!_isInitialized) + { + lock (_initLock) + { + if (!_isInitialized) + { + InitializeDatabase(); + _isInitialized = true; + } + } + } + } + + private void InitializeDatabase() + { + var directory = Cache.MagicQuantDirectory; + + // Safety: fallback if Cache isn't set yet (rare, but good for stability) + if (string.IsNullOrEmpty(directory)) + directory = Directory.GetCurrentDirectory(); + + if (!Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + // Apply Migrations automatically + Database.Migrate(); + } + + // -------------------------------------------------------- + // Standard DbContext Configuration + // -------------------------------------------------------- + + public MagicQuantContext(DbContextOptions options) : base(options) { } + + public DbSet AiBenchmarks { get; set; } + public DbSet AiModelHashes { get; set; } + public DbSet TensorCombos { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + var directory = Cache.MagicQuantDirectory; + if (string.IsNullOrEmpty(directory)) + { + directory = Directory.GetCurrentDirectory(); + } + + var dbPath = Path.Combine(directory, "MagicQuant_SQLite.db"); + optionsBuilder.UseSqlite($"Data Source={dbPath};Foreign Keys=True;"); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.ApplyConfigurationsFromAssembly(typeof(MagicQuantContext).Assembly); + ValidateDbSetsImplementInterface(); + base.OnModelCreating(modelBuilder); + } + + private void ValidateDbSetsImplementInterface() + { + var dbSetGenericTypes = this.GetType() + .GetProperties() + .Where(p => p.PropertyType.IsGenericType && + p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)) + .Select(p => p.PropertyType.GetGenericArguments()[0]) + .ToHashSet(); + + var configuredTypes = typeof(MagicQuantContext).Assembly + .GetTypes() + .Where(t => t.GetInterfaces().Any(i => + i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ISQLiteEntity<>))) + .ToHashSet(); + + var missingConfigs = dbSetGenericTypes.Except(configuredTypes).ToList(); + + if (missingConfigs.Any()) + { + var names = string.Join(", ", missingConfigs.Select(t => t.Name)); + throw new InvalidOperationException( + $"STRICT MODE ERROR: The following DbSets do not implement ISQLiteEntity: [{names}]. " + ); + } + } +} \ No newline at end of file diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj index c04c3b4..e0c67e6 100644 --- a/MQ.DB/MQ.DB.csproj +++ b/MQ.DB/MQ.DB.csproj @@ -18,6 +18,7 @@ + diff --git a/MQ.DB/Services/SqlService.cs b/MQ.DB/Services/SqlService.cs deleted file mode 100644 index b99a61f..0000000 --- a/MQ.DB/Services/SqlService.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using MQ.DB.Data; - -namespace MQ.DB.Services; - -public static class SqlService -{ - // Static flag to track if we've checked/migrated in this runtime session - private static bool _isInitialized = false; - - // Lock object to prevent race conditions if two threads call GetContext simultaneously at start - private static readonly object _initLock = new object(); - - /// - /// Gets a ready-to-use DbContext. - /// Automatically handles directory creation and DB Migrations on the first call. - /// - public static AppDbContext GetContext() - { - // 1. Check if we've already initialized in this runtime - if (!_isInitialized) - { - lock (_initLock) - { - if (!_isInitialized) - { - InitializeDatabase(); - _isInitialized = true; - } - } - } - - // 2. Return a new instance for the unit of work - return new AppDbContext(); - } - - private static void InitializeDatabase() - { - // Ensure the directory exists (using your Cache static path) - var directory = Cache.MagicQuantDirectory; - if (!Directory.Exists(directory)) - { - Directory.CreateDirectory(directory); - } - - using (var db = new AppDbContext()) - { - db.Database.Migrate(); - } - } -} \ No newline at end of file diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 94f831b..2ea7155 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -3,8 +3,11 @@ using System.Text.Json; using System.Text.RegularExpressions; using MagicQuant.Helpers; -using MQ.DB; +using MQ.DB.Data; using MQ.DB.Models; +using MQ.DB.Models.DbModels; // Required for BenchmarkCategory and TensorCombo +using Microsoft.EntityFrameworkCore; +using MQ.DB; using Spectre.Console; namespace MagicQuant.Services; @@ -26,12 +29,7 @@ public class BenchmarkService // Concurrency Controls // ---------------------------------------------------------------- - // 1. Exclusive Lock: When LlamaBench runs, it must be the ONLY thing running. - // Higher-level logic should acquire this before calling RunLlamaBenchAsync. public static readonly SemaphoreSlim ExclusiveBenchLock = new(1, 1); - - // 2. VRAM Lock: Only one VRAM-heavy task (Perplexity) can run at a time. - // However, it CAN run alongside CPU tasks (like quantization if VRAM allows). public static readonly SemaphoreSlim VramLock = new(1, 1); public BenchmarkService(PythonManager pyManager) @@ -46,6 +44,7 @@ public BenchmarkService(PythonManager pyManager) // ---------------------------------------------------------------- public async Task RunAllBenchmarksAsync( + HybridQuant quantConfig, string modelPath, string benchDir, int tokenTarget = 32768, @@ -53,24 +52,53 @@ public async Task RunAllBenchmarksAsync( string? klLogitsDir = null, bool saveLogits = false) { + Directory.CreateDirectory(benchDir); string jsonPath = Path.Combine(benchDir, "bench_metrics.json"); - if (File.Exists(jsonPath)) + using var db = new MagicQuantContext(); + + // 1. Resolve Model Hash + var currentHashStr = Cache.CurrentModelId; + var aiModelHash = await db.AiModelHashes + .FirstOrDefaultAsync(x => x.UniqueHash == currentHashStr); + + if (aiModelHash == null) { - var benchMetrics = File.ReadAllText(jsonPath); - if (!string.IsNullOrWhiteSpace(benchMetrics)) + aiModelHash = new AiModelHash { UniqueHash = currentHashStr }; + db.AiModelHashes.Add(aiModelHash); + await db.SaveChangesAsync(); + } + + // 2. Resolve Tensor Combo (Fixed to use Constructor) + var tensorCombo = await GetOrCreateTensorComboAsync(db, quantConfig); + + // 3. Check if Benchmark already exists + var existingBench = await db.AiBenchmarks + .FirstOrDefaultAsync(b => b.AiModelHashId == aiModelHash.Id && b.TensorComboId == tensorCombo.Id); + + if (existingBench != null) + { + AnsiConsole.MarkupLine($"[green]Benchmark found in database for Combo ID {tensorCombo.Id}. Skipping execution.[/]"); + + if (File.Exists(jsonPath)) { - var deserializedMetrics = JsonSerializer.Deserialize(benchMetrics); - if (deserializedMetrics != null) - return deserializedMetrics; + var cachedJson = await File.ReadAllTextAsync(jsonPath); + if (!string.IsNullOrWhiteSpace(cachedJson)) + { + var deserializedMetrics = JsonSerializer.Deserialize(cachedJson); + if (deserializedMetrics != null) return deserializedMetrics; + } } + return new BenchmarkResult(); } - Directory.CreateDirectory(benchDir); + // ---------------------------------------------------------------- + // 4. Execution + // ---------------------------------------------------------------- + var result = new BenchmarkResult(); - // 1. Run Llama-Bench (Exclusive Mode) - // We acquire the exclusive lock to ensure stability + // A. Run Llama-Bench await ExclusiveBenchLock.WaitAsync(); try { @@ -82,8 +110,7 @@ public async Task RunAllBenchmarksAsync( ExclusiveBenchLock.Release(); } - // 2. Run Perplexity (General, Code, Math) - // We prepare folders first so we don't block locks unnecessarily + // B. Run Perplexity var domains = new[] { "general", "code", "math" }; var corporaRoot = Path.Combine(Path.GetDirectoryName(benchDir)!, "_ppl_corpora"); Directory.CreateDirectory(corporaRoot); @@ -93,12 +120,9 @@ public async Task RunAllBenchmarksAsync( foreach (var domain in domains) { - // A. Prepare Corpus (CPU bound, low risk) string corpusPath = Path.Combine(corporaRoot, $"ppl_corpus_{domain}.txt"); await PreparePplCorpusAsync(domain, corpusPath, tokenTarget); - // B. Run Benchmark (VRAM Intensive) - // We acquire VRAM lock so we don't run 2 perplexities at once await VramLock.WaitAsync(); try { @@ -115,35 +139,209 @@ public async Task RunAllBenchmarksAsync( } } - // Save Results JSON + // 5. Save Results await File.WriteAllTextAsync(jsonPath, JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true })); + // Pass modelPath so we can calculate SizeBytes + await SaveBenchmarkToDbAsync(db, aiModelHash, tensorCombo, result, modelPath); + return result; } // ---------------------------------------------------------------- - // 1. Llama-Bench Logic + // Database Helpers (Fixed for Immutability) + // ---------------------------------------------------------------- + + private async Task GetOrCreateTensorComboAsync(MagicQuantContext db, HybridQuant quant) + { + // 1. Extract values into local variables. + // Default to 0 (NULL scheme) if not present in the mutable list. + byte baseQuant = quant.BaseQuant.UniqueId; + + byte embeddings = 0; + byte lmHead = 0; + byte attnQ = 0; + byte attnKV = 0; + byte attnOutput = 0; + byte ffnUpGate = 0; + byte ffnDown = 0; + byte moeExperts = 0; + byte moeRouter = 0; + + if (quant.Tensors != null) + { + foreach (var t in quant.Tensors) + { + // Compare using UniqueId to be safe + if (t.TGroup.UniqueId == TReg.Embeddings.UniqueId) embeddings = t.TensorType.UniqueId; + else if (t.TGroup.UniqueId == TReg.LmHead.UniqueId) lmHead = t.TensorType.UniqueId; + else if (t.TGroup.UniqueId == TReg.AttnQ.UniqueId) attnQ = t.TensorType.UniqueId; + else if (t.TGroup.UniqueId == TReg.AttnKV.UniqueId) attnKV = t.TensorType.UniqueId; + else if (t.TGroup.UniqueId == TReg.AttnOutput.UniqueId) attnOutput = t.TensorType.UniqueId; + else if (t.TGroup.UniqueId == TReg.FfnUpGate.UniqueId) ffnUpGate = t.TensorType.UniqueId; + else if (t.TGroup.UniqueId == TReg.FfnDown.UniqueId) ffnDown = t.TensorType.UniqueId; + else if (t.TGroup.UniqueId == TReg.MoeExperts.UniqueId) moeExperts = t.TensorType.UniqueId; + else if (t.TGroup.UniqueId == TReg.MoeRouter.UniqueId) moeRouter = t.TensorType.UniqueId; + } + } + + // 2. Create the Immutable Config using the Constructor + var c = new TensorConfig( + baseQuant, + embeddings, + lmHead, + attnQ, + attnKV, + attnOutput, + ffnUpGate, + ffnDown, + moeExperts, + moeRouter + ); + + // 3. Check DB using the extracted values + // (We query by the raw bytes because the DB entity fields are readonly and might not map directly in Expression trees depending on EF version) + var existing = await db.TensorCombos.FirstOrDefaultAsync(x => + x.BaseQuant == c.BaseQuant && + x.Embeddings == c.Embeddings && + x.LmHead == c.LmHead && + x.AttnQ == c.AttnQ && + x.AttnKV == c.AttnKV && + x.AttnOutput == c.AttnOutput && + x.FfnUpGate == c.FfnUpGate && + x.FfnDown == c.FfnDown && + x.MoeExperts == c.MoeExperts && + x.MoeRouter == c.MoeRouter + ); + + if (existing != null) return existing; + + // 4. Create New TensorCombo using the Constructor (which accepts TensorConfig) + var newCombo = new TensorCombo(c); + db.TensorCombos.Add(newCombo); + await db.SaveChangesAsync(); + return newCombo; + } + + private async Task SaveBenchmarkToDbAsync( + MagicQuantContext db, + AiModelHash model, + TensorCombo combo, + BenchmarkResult res, + string modelPath) + { + using var transaction = await db.Database.BeginTransactionAsync(); + try + { + // Calculate File Size + ulong sizeBytes = 0; + if (File.Exists(modelPath)) + { + sizeBytes = (ulong)new FileInfo(modelPath).Length; + } + + // 1. Create Parent Benchmark + var bench = new AiBenchmark + { + AiModelHashId = model.Id, + TensorComboId = combo.Id, + + // Map LlamaBench fields + TokensPerSecond = res.LlamaBench?.Tps ?? 0, + Ngl = (byte)(res.LlamaBench?.Ngl ?? 0), + SizeBytes = sizeBytes + }; + + db.AiBenchmarks.Add(bench); + await db.SaveChangesAsync(); // Generates bench.Id + + // 2. Create Child Category Benchmarks + var categories = new List(); + + // Helper to determine if we need to force 0.0 KLD for Base Model + // (Checks if everything is 0 except BaseQuant which is BF16/F16) + bool isBaseModel = combo.BaseQuant == TensorWeightScheme.BF16_F16.UniqueId && + combo.Embeddings == 0 && combo.LmHead == 0; + + // Map "general" -> BenchmarkCategory.General (1) + if (res.Perplexity.ContainsKey("general")) + { + var m = res.Perplexity["general"]; + var cb = new CategoryBenchmark + { + AiBenchmarkId = bench.Id, + Category = (byte)BenchmarkCategory.General, + Ppl = m.Ppl, + PplError = m.PplError, + Kld = m.Kld ?? (isBaseModel ? 0.0 : 0.0) // Defaults to 0 if null + }; + categories.Add(cb); + } + + // Map "code" -> BenchmarkCategory.Code (3) + if (res.Perplexity.ContainsKey("code")) + { + var m = res.Perplexity["code"]; + var cb = new CategoryBenchmark + { + AiBenchmarkId = bench.Id, + Category = (byte)BenchmarkCategory.Code, + Ppl = m.Ppl, + PplError = m.PplError, + Kld = m.Kld ?? (isBaseModel ? 0.0 : 0.0) + }; + categories.Add(cb); + } + + // Map "math" -> BenchmarkCategory.Math (2) + if (res.Perplexity.ContainsKey("math")) + { + var m = res.Perplexity["math"]; + var cb = new CategoryBenchmark + { + AiBenchmarkId = bench.Id, + Category = (byte)BenchmarkCategory.Math, + Ppl = m.Ppl, + PplError = m.PplError, + Kld = m.Kld ?? (isBaseModel ? 0.0 : 0.0) + }; + categories.Add(cb); + } + + // Batch Insert Categories + if (categories.Count > 0) + { + db.Set().AddRange(categories); + await db.SaveChangesAsync(); + } + + await transaction.CommitAsync(); + AnsiConsole.MarkupLine("[green]Benchmarks saved to Database successfully.[/]"); + } + catch (Exception ex) + { + AnsiConsole.MarkupLine($"[red]Failed to save benchmarks to DB: {ex.Message}[/]"); + await transaction.RollbackAsync(); + } + } + + // ---------------------------------------------------------------- + // 1. Llama-Bench Logic (Unchanged) // ---------------------------------------------------------------- private async Task RunLlamaBenchAsync(string modelPath, string benchDir, int? startNgl) { string logFile = Path.Combine(benchDir, "llamabench.md"); - - // Filter candidates var candidates = startNgl.HasValue ? NglCandidates.Where(n => n <= startNgl.Value).ToList() : NglCandidates.ToList(); - // Command Builder - // Note: Keeping -p 8 -t 16 as requested ("just like we're now") string BuildCmd(int ngl) => $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {ngl} -o md"; - // Retry Loop int? finalNgl = await RunWithRetryAsync(BuildCmd, logFile, candidates, "llama-bench"); - // Fallback to CPU if GPU failed completely if (finalNgl == null) { AnsiConsole.MarkupLine("[red]GPU Failed. Fallback to CPU backend...[/]"); @@ -160,7 +358,6 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) if (!File.Exists(logPath)) return metrics; var lines = File.ReadAllLines(logPath); - int headerIdx = -1; for (int i = 0; i < lines.Length; i++) { @@ -174,11 +371,9 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) if (headerIdx == -1 || lines.Length <= headerIdx + 2) return metrics; var headers = lines[headerIdx].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(h => h.Trim()).ToList(); - var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()) - .ToList(); + var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()).ToList(); if (headers.Count != dataRow.Count) return metrics; - var row = headers.Zip(dataRow, (h, d) => new { Header = h, Data = d }).ToDictionary(x => x.Header, x => x.Data); string tpsStr = row.ContainsKey("t/s") ? row["t/s"] : (row.ContainsKey("tps") ? row["tps"] : "0"); @@ -196,7 +391,7 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) } // ---------------------------------------------------------------- - // 2. Perplexity Logic + // 2. Perplexity Logic (Unchanged) // ---------------------------------------------------------------- private async Task RunPplBenchmarkAsync( @@ -208,7 +403,6 @@ private async Task RunPplBenchmarkAsync( ? NglCandidates.Where(n => n <= startNgl.Value).ToList() : NglCandidates.ToList(); - // KL Divergence Logic string kldArgs = ""; if (!string.IsNullOrEmpty(klLogitsDir)) { @@ -219,8 +413,6 @@ private async Task RunPplBenchmarkAsync( kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; } - // Command Builder - // Added "-t 4" to limit thread usage as requested string BuildCmd(int ngl) => $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {ngl} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}"; @@ -238,8 +430,7 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) string text = File.ReadAllText(logPath); string cleanText = StripAnsi(text); - var pplMatch = Regex.Match(cleanText, @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([0-9.]+)\s*(?:±|\+/-)\s*([0-9.]+)", - RegexOptions.IgnoreCase); + var pplMatch = Regex.Match(cleanText, @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([0-9.]+)\s*(?:±|\+/-)\s*([0-9.]+)", RegexOptions.IgnoreCase); if (pplMatch.Success) { @@ -251,8 +442,7 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) AnsiConsole.MarkupLine($"[red]Error parsing PPL from {logPath}[/]"); } - var kldMatch = Regex.Match(cleanText, @"(?:Mean\s+KLD|KL[-_\s]*divergence|kl[-_\s]*div)\s*[:=]\s*([0-9.]+)", - RegexOptions.IgnoreCase); + var kldMatch = Regex.Match(cleanText, @"(?:Mean\s+KLD|KL[-_\s]*divergence|kl[-_\s]*div)\s*[:=]\s*([0-9.]+)", RegexOptions.IgnoreCase); if (kldMatch.Success) { @@ -267,7 +457,7 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) } // ---------------------------------------------------------------- - // 3. Corpus Preparation + // 3. Corpus Preparation (Unchanged) // ---------------------------------------------------------------- private async Task PreparePplCorpusAsync(string domain, string outPath, int tokenTarget) @@ -337,8 +527,6 @@ with open(out_path, 'w', encoding='utf-8') as f: foreach (int ngl in candidates) { string cmd = cmdBuilder(ngl); - - // Untrusted / dynamic output → WriteLine ONLY AnsiConsole.WriteLine($"[*] {label}: trying -ngl {ngl}"); await RunShellCommandAsync(cmd, logPath); @@ -347,36 +535,26 @@ with open(out_path, 'w', encoding='utf-8') as f: ? File.ReadAllText(logPath) : string.Empty; - if (OomMarkers.Any(m => - logContent.Contains(m, StringComparison.OrdinalIgnoreCase))) + if (OomMarkers.Any(m => logContent.Contains(m, StringComparison.OrdinalIgnoreCase))) { - AnsiConsole.WriteLine( - $"[WARN] {label}: OOM at -ngl {ngl}, retrying..." - ); + AnsiConsole.WriteLine($"[WARN] {label}: OOM at -ngl {ngl}, retrying..."); continue; } if (logContent.Length < 50) { - AnsiConsole.WriteLine( - $"[WARN] {label}: Failed at -ngl {ngl} (Unknown Error), trying next..." - ); + AnsiConsole.WriteLine($"[WARN] {label}: Failed at -ngl {ngl} (Unknown Error), trying next..."); continue; } - AnsiConsole.WriteLine( - $"[OK] {label}: succeeded with -ngl {ngl}" - ); + AnsiConsole.WriteLine($"[OK] {label}: succeeded with -ngl {ngl}"); return ngl; } - AnsiConsole.WriteLine( - $"[ERROR] {label}: All -ngl candidates failed." - ); + AnsiConsole.WriteLine($"[ERROR] {label}: All -ngl candidates failed."); return null; } - // ---------------------------------------------------------------- // 5. System Utilities // ---------------------------------------------------------------- @@ -394,7 +572,6 @@ private async Task RunShellCommandAsync(string cmd, string? logPath) }; using var process = new Process { StartInfo = startInfo }; - FileStream? fs = null; StreamWriter? sw = null; @@ -404,19 +581,12 @@ private async Task RunShellCommandAsync(string cmd, string? logPath) sw = new StreamWriter(fs); } - process.OutputDataReceived += (s, e) => - { - if (e.Data != null) sw?.WriteLine(e.Data); - }; - process.ErrorDataReceived += (s, e) => - { - if (e.Data != null) sw?.WriteLine(e.Data); - }; + process.OutputDataReceived += (s, e) => { if (e.Data != null) sw?.WriteLine(e.Data); }; + process.ErrorDataReceived += (s, e) => { if (e.Data != null) sw?.WriteLine(e.Data); }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); - await process.WaitForExitAsync(); sw?.Dispose(); diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 60b6c7b..2984a11 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -107,9 +107,10 @@ private async Task ProcessHybridQuantAsync(HybridQuant quant) AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {modelName}"); await _benchmarker.RunAllBenchmarksAsync( - quantPath, - modelBenchDir, - saveLogits: false // Only base models save logits + quantConfig: quant, + modelPath: quantPath, + benchDir: modelBenchDir, + saveLogits: false ); // Cleanup: Delete GGUF after benchmark (unless protected base) @@ -248,13 +249,22 @@ public async Task EnsureBaseModelAsync(bool deleteProcess = false) $"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]" ); + // Create the HybridQuant representation for the Base Model + // This matches the "TensorWeightScheme.BF16_F16" BaseQuant, with NO other tensors (NULL) + var baseModelQuant = new HybridQuant + { + BaseQuant = BaselineQuants.All.First(b => b.UniqueId == TensorWeightScheme.BF16_F16.UniqueId), + Tensors = new List() // Empty list = all other groups are 0/NULL + }; + await _benchmarker.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, // <--- PASSED HERE modelPath: outputPath, benchDir: benchPath, klLogitsDir: logitsDir, saveLogits: true ); - + return outputPath; } From 83b0c9b1e277e7f8bd397401e0c77f4b8e405333 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 11 Apr 2026 12:27:07 -0400 Subject: [PATCH 040/258] new hardware detection --- MQ.DB/Models/SystemInfo.cs | 10 +- MagicQuant/Commands/InitializeLlamaCpp.cs | 8 +- MagicQuant/Helpers/DependencyManager.cs | 4 +- MagicQuant/Helpers/HardwareHelper.cs | 715 ++++++++++++++++++++-- MagicQuant/Helpers/LlamaBuilder.cs | 2 +- MagicQuant/Program.cs | 2 +- 6 files changed, 680 insertions(+), 61 deletions(-) diff --git a/MQ.DB/Models/SystemInfo.cs b/MQ.DB/Models/SystemInfo.cs index 8cf01da..076858b 100644 --- a/MQ.DB/Models/SystemInfo.cs +++ b/MQ.DB/Models/SystemInfo.cs @@ -10,12 +10,18 @@ public enum GpuVendor } public class SystemInfo +{ + public List GpuInfo { get; set; } = new List(); + public double RamGb { get; set; } + public int ThreadCount { get; set; } +} + +public class GpuInfo { public GpuVendor GpuVendor { get; set; } public string GpuName { get; set; } = "Unknown"; public double VramGb { get; set; } - public double RamGb { get; set; } - public int ThreadCount { get; set; } + public string? UniqueId { get; set; } } public static class MagicConstants diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 361c9b7..54352b0 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -63,7 +63,7 @@ public async Task Run(List args) // --------------------------------------------------------- var sysInfo = HardwareHelper.GetSystemInfo(); AnsiConsole.Write(new Rule("[yellow]System Detection[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"Detected GPU: [green]{sysInfo.GpuVendor}[/] ([blue]{sysInfo.GpuName}[/] - {sysInfo.VramGb:F1} GB)"); + AnsiConsole.MarkupLine($"Detected GPU: [green]{sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor}[/] ([blue]{sysInfo.GpuInfo.FirstOrDefault()?.GpuName}[/] - {sysInfo.GpuInfo.Sum(x => x.VramGb):F1} GB)"); AnsiConsole.MarkupLine($"Detected RAM: [blue]{sysInfo.RamGb:F1} GB[/]"); // --------------------------------------------------------- @@ -77,7 +77,7 @@ public async Task Run(List args) "python3", "python3-venv", "python3-pip", "libcurl4-openssl-dev" }; - if (sysInfo.GpuVendor == GpuVendor.Nvidia) requiredPackages.Add("nvidia-cuda-toolkit"); + if (sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor == GpuVendor.Nvidia) requiredPackages.Add("nvidia-cuda-toolkit"); // Check if updates are needed if (update || !AreLinuxPackagesInstalled(requiredPackages)) @@ -158,7 +158,7 @@ async Task EnsurePackage(string name, string installCmd, Dictionary SelectBestGpuVendorGroup(List gpus) + { + if (gpus == null || gpus.Count == 0) + return new List(); + + var candidates = gpus + .Where(x => x != null) + .Where(x => x.GpuVendor != GpuVendor.Unknown && x.GpuVendor != GpuVendor.Cpu) + .Where(x => x.VramGb > 0.01) + .ToList(); + + if (candidates.Count == 0) + return new List(); + + var bestVendor = candidates + .GroupBy(x => x.GpuVendor) + .Select(g => new + { + Vendor = g.Key, + TotalVram = g.Sum(x => x.VramGb), + Count = g.Count() + }) + .OrderByDescending(x => x.TotalVram) + .ThenByDescending(x => x.Count) + .First() + .Vendor; + + return candidates + .Where(x => x.GpuVendor == bestVendor) + .OrderByDescending(x => x.VramGb) + .ThenBy(x => x.GpuName, StringComparer.OrdinalIgnoreCase) + .ToList(); + } + public static double GetCudaVersion() { try { - // We check nvcc because that matches the Toolkit we installed/verified - var psi = new ProcessStartInfo + var result = RunProcess( + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "nvcc.exe" : "nvcc", + "--version"); + + if (!result.Success || string.IsNullOrWhiteSpace(result.StdOut)) + return 0; + + var match = Regex.Match(result.StdOut, @"release (\d+\.\d+)"); + if (match.Success && + double.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var version)) { - FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "nvcc.exe" : "nvcc", - Arguments = "--version", - RedirectStandardOutput = true, - UseShellExecute = false, - CreateNoWindow = true + return version; + } + } + catch + { + // ignored + } + + return 0; + } + + private static double GetTotalRamGb() + { + try + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // More reliable than GC.GetGCMemoryInfo for actual system RAM + var result = RunProcess( + "powershell", + "-NoProfile -ExecutionPolicy Bypass -Command \"(Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory\""); + + if (result.Success && + TryParseFirstInteger(result.StdOut, out var bytes) && + bytes > 0) + { + return BytesToGb(bytes); + } + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + var memInfo = "/proc/meminfo"; + if (File.Exists(memInfo)) + { + var line = File.ReadLines(memInfo) + .FirstOrDefault(x => x.StartsWith("MemTotal:", StringComparison.OrdinalIgnoreCase)); + + if (!string.IsNullOrWhiteSpace(line)) + { + var match = Regex.Match(line, @"MemTotal:\s+(\d+)\s+kB", RegexOptions.IgnoreCase); + if (match.Success && + ulong.TryParse(match.Groups[1].Value, out var kb)) + { + return kb / 1024.0 / 1024.0; + } + } + } + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + var result = RunProcess("sysctl", "-n hw.memsize"); + if (result.Success && + TryParseFirstInteger(result.StdOut, out var bytes) && + bytes > 0) + { + return BytesToGb(bytes); + } + } + } + catch + { + // ignored + } + + // Last-resort fallback. This is NOT actual total RAM, just available-to-GC-ish territory. + var fallback = GC.GetGCMemoryInfo().TotalAvailableMemoryBytes; + return fallback > 0 ? BytesToGb((ulong)fallback) : 0; + } + + private static List DetectGpus() + { + var gpus = new List(); + + try + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + gpus.AddRange(DetectGpusWindows()); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + gpus.AddRange(DetectGpusLinux()); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + gpus.AddRange(DetectGpusMac()); + } + } + catch + { + // ignored + } + + gpus = NormalizeAndDedupe(gpus); + + if (gpus.Count == 0) + { + gpus.Add(new GpuInfo + { + GpuVendor = GpuVendor.Cpu, + GpuName = "No discrete GPU detected", + VramGb = 0 + }); + } + + return gpus; + } + + // ========================= + // Windows + // ========================= + + private static IEnumerable DetectGpusWindows() +{ + var results = new List(); + + var nvidia = DetectNvidiaViaSmi().ToList(); + results.AddRange(nvidia); + + var ps = RunProcess( + "powershell", + "-NoProfile -ExecutionPolicy Bypass -Command \"Get-CimInstance Win32_VideoController | Select-Object Name,AdapterRAM,PNPDeviceID | ConvertTo-Json -Depth 3\""); + + if (ps.Success && !string.IsNullOrWhiteSpace(ps.StdOut)) + { + try + { + using var doc = JsonDocument.Parse(ps.StdOut); + + IEnumerable items = doc.RootElement.ValueKind switch + { + JsonValueKind.Array => doc.RootElement.EnumerateArray().ToArray(), + JsonValueKind.Object => new[] { doc.RootElement }, + _ => Array.Empty() }; - using var p = Process.Start(psi); - if (p == null) return 0; + foreach (var item in items) + { + var name = item.TryGetProperty("Name", out var nameEl) + ? nameEl.GetString() ?? "Unknown" + : "Unknown"; + + var pnp = item.TryGetProperty("PNPDeviceID", out var pnpEl) + ? pnpEl.GetString() ?? string.Empty + : string.Empty; + + var vendor = DetectVendorFromNameOrId(name, pnp); + + // If we already have NVIDIA via nvidia-smi, skip weaker duplicate NVIDIA rows. + if (vendor == GpuVendor.Nvidia && nvidia.Count > 0) + continue; + + double vramGb = 0; + if (item.TryGetProperty("AdapterRAM", out var ramEl)) + { + if (ramEl.ValueKind == JsonValueKind.Number && ramEl.TryGetUInt64(out var bytes)) + { + vramGb = BytesToGb(bytes); + } + else if (ramEl.ValueKind == JsonValueKind.String && + ulong.TryParse(ramEl.GetString(), out var parsed)) + { + vramGb = BytesToGb(parsed); + } + } + + results.Add(new GpuInfo + { + GpuName = name, + GpuVendor = vendor, + VramGb = SanitizeGb(vramGb) + }); + } + } + catch + { + // ignored + } + } + + return results; +} + + // ========================= + // Linux + // ========================= + + private static IEnumerable DetectGpusLinux() + { + var results = new List(); + + // 1. NVIDIA: highest-confidence source + results.AddRange(DetectNvidiaViaSmi()); - string output = p.StandardOutput.ReadToEnd(); - p.WaitForExit(); + // 2. lspci for names/vendors + var lspci = RunProcess("bash", "-lc \"lspci -nn 2>/dev/null | grep -Ei 'vga|3d|display'\""); + var lspciEntries = new List<(string Name, GpuVendor Vendor)>(); - // Output format: "Cuda compilation tools, release 12.4, V12.4.131" - // Regex to find "release X.Y" - var match = System.Text.RegularExpressions.Regex.Match(output, @"release (\d+\.\d+)"); - if (match.Success && double.TryParse(match.Groups[1].Value, out double version)) + if (lspci.Success && !string.IsNullOrWhiteSpace(lspci.StdOut)) + { + foreach (var line in SplitLines(lspci.StdOut)) { - return version; + var name = ExtractGpuNameFromLspci(line); + var vendor = DetectVendorFromNameOrId(line, line); + + lspciEntries.Add((name, vendor)); + + results.Add(new GpuInfo + { + GpuName = name, + GpuVendor = vendor, + VramGb = 0 + }); + } + } + + // 3. AMD/Intel VRAM hints from /sys/class/drm + results = MergeLinuxSysFsData(results); + + return results; + } + + private static List MergeLinuxSysFsData(List existing) + { + try + { + var drmPath = "/sys/class/drm"; + if (!Directory.Exists(drmPath)) + return existing; + + var cardDirs = Directory.GetDirectories(drmPath, "card*") + .Where(x => !x.Contains("-", StringComparison.Ordinal)) // skip card0-DP-1 type connector entries + .OrderBy(x => x) + .ToList(); + + foreach (var cardDir in cardDirs) + { + var deviceDir = Path.Combine(cardDir, "device"); + if (!Directory.Exists(deviceDir)) + continue; + + string vendorId = ReadTrimmedFile(Path.Combine(deviceDir, "vendor")); + string deviceId = ReadTrimmedFile(Path.Combine(deviceDir, "device")); + + var vendor = DetectVendorFromPciVendorId(vendorId); + if (vendor == GpuVendor.Unknown) + continue; + + double vramGb = 0; + + // AMD dedicated VRAM often exposed here on amdgpu + var amdVramPath = Path.Combine(deviceDir, "mem_info_vram_total"); + if (File.Exists(amdVramPath) && + ulong.TryParse(ReadTrimmedFile(amdVramPath), out var amdBytes)) + { + vramGb = BytesToGb(amdBytes); + } + + // Intel integrated usually won’t have dedicated VRAM here. + // Leave as 0 instead of inventing nonsense. + + // Try to match an existing entry by vendor with 0 VRAM and patch it in. + var possibleMatches = existing + .Where(x => x.GpuVendor == vendor && x.VramGb <= 0.01) + .ToList(); + + GpuInfo? existingMatch = possibleMatches.Count == 1 ? possibleMatches[0] : null; + + if (existingMatch != null && vramGb > 0) + { + existingMatch.VramGb = SanitizeGb(vramGb); + } + else + { + existing.Add(new GpuInfo + { + GpuVendor = vendor, + GpuName = string.IsNullOrWhiteSpace(deviceId) + ? vendor.ToString() + : $"{vendor} GPU", + VramGb = SanitizeGb(vramGb) + }); + } } } catch { - // Fallback: If nvcc fails, we might try parsing nvidia-smi, - // but for now, returning 0 triggers a safe fallback. + // ignored } - return 0; + + return existing; } - private static double GetTotalRam() + // ========================= + // macOS + // ========================= + + private static IEnumerable DetectGpusMac() { - // simplified generic check - return GC.GetGCMemoryInfo().TotalAvailableMemoryBytes / 1024.0 / 1024.0 / 1024.0; + var results = new List(); + + var sp = RunProcess("system_profiler", "SPDisplaysDataType -json"); + if (!sp.Success || string.IsNullOrWhiteSpace(sp.StdOut)) + return results; + + try + { + using var doc = JsonDocument.Parse(sp.StdOut); + + if (!doc.RootElement.TryGetProperty("SPDisplaysDataType", out var displays) || + displays.ValueKind != JsonValueKind.Array) + { + return results; + } + + foreach (var gpu in displays.EnumerateArray()) + { + string name = + GetJsonString(gpu, "sppci_model") ?? + GetJsonString(gpu, "_name") ?? + "Unknown"; + + double vramGb = 0; + + // Intel/older Macs may expose strings like "1536 MB" + var vramText = + GetJsonString(gpu, "spdisplays_vram") ?? + GetJsonString(gpu, "spdisplays_vram_shared") ?? + GetJsonString(gpu, "sppci_vram"); + + if (!string.IsNullOrWhiteSpace(vramText)) + { + vramGb = ParseMemoryStringToGb(vramText); + } + + // Apple Silicon often won’t expose dedicated VRAM because memory is unified. + // So leaving 0 here is more honest than lying. + + results.Add(new GpuInfo + { + GpuName = name, + GpuVendor = DetectVendorFromNameOrId(name, name), + VramGb = SanitizeGb(vramGb) + }); + } + } + catch + { + // ignored + } + + return results; + } + + // ========================= + // Shared NVIDIA path + // ========================= + + private static IEnumerable DetectNvidiaViaSmi() + { + var results = new List(); + + string exe = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "nvidia-smi.exe" : "nvidia-smi"; + var smi = RunProcess(exe, "--query-gpu=gpu_uuid,name,memory.total --format=csv,noheader,nounits"); + + if (!smi.Success || string.IsNullOrWhiteSpace(smi.StdOut)) + return results; + + foreach (var line in SplitLines(smi.StdOut)) + { + var parts = line.Split(',', StringSplitOptions.TrimEntries); + + if (parts.Length < 3) + continue; + + var uuid = parts[0].Trim(); + var name = parts[1].Trim(); + + double vramGb = 0; + if (double.TryParse(parts[2].Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out var memMb)) + { + vramGb = memMb / 1024.0; + } + + results.Add(new GpuInfo + { + UniqueId = uuid, + GpuVendor = GpuVendor.Nvidia, + GpuName = string.IsNullOrWhiteSpace(name) ? "NVIDIA GPU" : name, + VramGb = SanitizeGb(vramGb) + }); + } + + return results; } - private static GpuVendor DetectGpuVendor(out string name, out double vram) + // ========================= + // Helpers + // ========================= + + private static List NormalizeAndDedupe(List gpus) { - name = "Generic"; - vram = 0; + var final = new List(); - // 1. Check NVIDIA (nvidia-smi) - Works on Linux & Windows - try + foreach (var gpu in gpus) { - var process = new Process + var normalizedName = string.IsNullOrWhiteSpace(gpu.GpuName) + ? "Unknown" + : Regex.Replace(gpu.GpuName.Trim(), @"\s+", " "); + + var vendor = gpu.GpuVendor == GpuVendor.Unknown + ? DetectVendorFromNameOrId(normalizedName, gpu.UniqueId ?? normalizedName) + : gpu.GpuVendor; + + GpuInfo? existing = null; + + // Only merge by UniqueId if we actually have one + if (!string.IsNullOrWhiteSpace(gpu.UniqueId)) + { + existing = final.FirstOrDefault(x => + !string.IsNullOrWhiteSpace(x.UniqueId) && + string.Equals(x.UniqueId, gpu.UniqueId, StringComparison.OrdinalIgnoreCase)); + } + + // If no UniqueId, do NOT aggressively merge by name/vendor. + // That breaks multi-GPU systems with identical cards. + if (existing == null) { - StartInfo = new ProcessStartInfo("nvidia-smi", "--query-gpu=name,memory.total --format=csv,noheader,nounits") { RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true } + final.Add(new GpuInfo + { + UniqueId = gpu.UniqueId, + GpuVendor = vendor, + GpuName = normalizedName, + VramGb = SanitizeGb(gpu.VramGb) + }); + } + else + { + if (existing.VramGb <= 0.01 && gpu.VramGb > existing.VramGb) + existing.VramGb = SanitizeGb(gpu.VramGb); + + if (string.Equals(existing.GpuName, "Unknown", StringComparison.OrdinalIgnoreCase) && + !string.Equals(normalizedName, "Unknown", StringComparison.OrdinalIgnoreCase)) + { + existing.GpuName = normalizedName; + } + + if (existing.GpuVendor == GpuVendor.Unknown && vendor != GpuVendor.Unknown) + existing.GpuVendor = vendor; + } + } + + if (final.Any(x => x.GpuVendor != GpuVendor.Cpu)) + { + final.RemoveAll(x => x.GpuVendor == GpuVendor.Cpu); + } + + return final; + } + + private static GpuVendor DetectVendorFromNameOrId(string? name, string? idText) + { + var haystack = $"{name} {idText}".ToLowerInvariant(); + + if (haystack.Contains("nvidia") || haystack.Contains("geforce") || haystack.Contains("quadro") || haystack.Contains("tesla")) + return GpuVendor.Nvidia; + + if (haystack.Contains("amd") || haystack.Contains("advanced micro devices") || haystack.Contains("radeon") || haystack.Contains("firepro")) + return GpuVendor.Amd; + + if (haystack.Contains("intel") || haystack.Contains("arc") || haystack.Contains("uhd") || haystack.Contains("iris")) + return GpuVendor.Intel; + + return GpuVendor.Unknown; + } + + private static GpuVendor DetectVendorFromPciVendorId(string? vendorId) + { + if (string.IsNullOrWhiteSpace(vendorId)) + return GpuVendor.Unknown; + + var v = vendorId.Trim().ToLowerInvariant(); + + return v switch + { + "0x10de" => GpuVendor.Nvidia, + "0x1002" => GpuVendor.Amd, + "0x8086" => GpuVendor.Intel, + _ => GpuVendor.Unknown + }; + } + + private static string ExtractGpuNameFromLspci(string line) + { + if (string.IsNullOrWhiteSpace(line)) + return "Unknown"; + + var idx = line.IndexOf(':'); + if (idx >= 0 && idx < line.Length - 1) + { + var right = line[(idx + 1)..].Trim(); + + // Strip "VGA compatible controller:" / "3D controller:" / "Display controller:" + right = Regex.Replace( + right, + @"^(VGA compatible controller|3D controller|Display controller)\s*:\s*", + "", + RegexOptions.IgnoreCase).Trim(); + + return right; + } + + return line.Trim(); + } + + private static string? GetJsonString(JsonElement element, string propertyName) + { + if (!element.TryGetProperty(propertyName, out var prop)) + return null; + + return prop.ValueKind == JsonValueKind.String ? prop.GetString() : prop.ToString(); + } + + private static double ParseMemoryStringToGb(string text) + { + if (string.IsNullOrWhiteSpace(text)) + return 0; + + var match = Regex.Match(text, @"([\d.]+)\s*(TB|GB|MB|KB)", RegexOptions.IgnoreCase); + if (!match.Success) + return 0; + + if (!double.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) + return 0; + + var unit = match.Groups[2].Value.ToUpperInvariant(); + return unit switch + { + "TB" => value * 1024.0, + "GB" => value, + "MB" => value / 1024.0, + "KB" => value / 1024.0 / 1024.0, + _ => 0 + }; + } + + private static bool TryParseFirstInteger(string? text, out ulong value) + { + value = 0; + if (string.IsNullOrWhiteSpace(text)) + return false; + + var match = Regex.Match(text, @"\d+"); + return match.Success && ulong.TryParse(match.Value, out value); + } + + private static string ReadTrimmedFile(string path) + { + try + { + return File.Exists(path) ? File.ReadAllText(path).Trim() : string.Empty; + } + catch + { + return string.Empty; + } + } + + private static IEnumerable SplitLines(string text) + { + return text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + } + + private static double BytesToGb(ulong bytes) + { + return bytes / 1024.0 / 1024.0 / 1024.0; + } + + private static double SanitizeGb(double gb) + { + if (double.IsNaN(gb) || double.IsInfinity(gb) || gb < 0) + return 0; + + // Keep this nice and user-facing + return Math.Round(gb, 2); + } + + private static ProcessResult RunProcess(string fileName, string arguments, int timeoutMs = 5000) + { + try + { + var psi = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true }; + + using var process = new Process { StartInfo = psi }; process.Start(); - string output = process.StandardOutput.ReadToEnd(); - process.WaitForExit(); - if (process.ExitCode == 0 && !string.IsNullOrWhiteSpace(output)) + var stdOutTask = process.StandardOutput.ReadToEndAsync(); + var stdErrTask = process.StandardError.ReadToEndAsync(); + + if (!process.WaitForExit(timeoutMs)) { - var parts = output.Split(','); - name = parts[0].Trim(); - if (parts.Length > 1 && double.TryParse(parts[1], out double mem)) vram = mem / 1024.0; - return GpuVendor.Nvidia; + try { process.Kill(entireProcessTree: true); } catch { } + return ProcessResult.Failure("Process timed out."); } - } - catch { /* Not Nvidia */ } - // 2. Check MacOS (Metal) - Placeholder - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return GpuVendor.Cpu; // Todo: Metal check + Task.WaitAll(stdOutTask, stdErrTask); - // 3. Fallbacks (AMD/Intel) would go here (e.g., parsing lshw on linux) - // For now, defaulting to CPU if Nvidia fails - return GpuVendor.Cpu; + return new ProcessResult( + process.ExitCode == 0, + stdOutTask.Result ?? string.Empty, + stdErrTask.Result ?? string.Empty, + process.ExitCode); + } + catch (Exception ex) + { + return ProcessResult.Failure(ex.Message); + } + } + + private readonly record struct ProcessResult(bool Success, string StdOut, string StdErr, int ExitCode) + { + public static ProcessResult Failure(string error) => new(false, string.Empty, error, -1); } } \ No newline at end of file diff --git a/MagicQuant/Helpers/LlamaBuilder.cs b/MagicQuant/Helpers/LlamaBuilder.cs index 2bc25ec..f6e0a94 100644 --- a/MagicQuant/Helpers/LlamaBuilder.cs +++ b/MagicQuant/Helpers/LlamaBuilder.cs @@ -104,7 +104,7 @@ private string GetOptimalCmakeArgs() args.Add("-G Ninja"); // GPU Optimization Logic - switch (_sysInfo.GpuVendor) + switch (_sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor) { case GpuVendor.Nvidia: args.Add("-DGGML_CUDA=ON"); diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index da8eaca..0e116a1 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -16,7 +16,7 @@ // OPTIONAL: Manually append hardcoded flags for testing specific scenarios // Example: If you want to test "evolution --iterations 10" every time you debug -string manualFlags = @"--model-dir ""/mnt/world8/AI/ToBench/Qwen3-4B-Instruct-2507-unsloth/"""; +string manualFlags = @"--model-dir ""/mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/"""; args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif From b237d6aad45d39046718e89332804f06103f695d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 11 Apr 2026 12:31:31 -0400 Subject: [PATCH 041/258] migrations --- .../20260411162835_InitialCreate.Designer.cs | 190 ++++++++++++++++++ .../20260411162835_InitialCreate.cs | 156 ++++++++++++++ .../MagicQuantContextModelSnapshot.cs | 187 +++++++++++++++++ 3 files changed, 533 insertions(+) create mode 100644 MQ.DB/Migrations/20260411162835_InitialCreate.Designer.cs create mode 100644 MQ.DB/Migrations/20260411162835_InitialCreate.cs create mode 100644 MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs diff --git a/MQ.DB/Migrations/20260411162835_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260411162835_InitialCreate.Designer.cs new file mode 100644 index 0000000..77c718e --- /dev/null +++ b/MQ.DB/Migrations/20260411162835_InitialCreate.Designer.cs @@ -0,0 +1,190 @@ +// +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260411162835_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiBenchmarkId1") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiBenchmarkId1"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", null) + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId1") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260411162835_InitialCreate.cs b/MQ.DB/Migrations/20260411162835_InitialCreate.cs new file mode 100644 index 0000000..316a15e --- /dev/null +++ b/MQ.DB/Migrations/20260411162835_InitialCreate.cs @@ -0,0 +1,156 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AiModelHashes", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + UniqueHash = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AiModelHashes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "TensorCombos", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AttnKV = table.Column(type: "INTEGER", nullable: false), + AttnOutput = table.Column(type: "INTEGER", nullable: false), + AttnQ = table.Column(type: "INTEGER", nullable: false), + BaseQuant = table.Column(type: "INTEGER", nullable: false), + Embeddings = table.Column(type: "INTEGER", nullable: false), + FfnDown = table.Column(type: "INTEGER", nullable: false), + FfnUpGate = table.Column(type: "INTEGER", nullable: false), + LmHead = table.Column(type: "INTEGER", nullable: false), + MoeExperts = table.Column(type: "INTEGER", nullable: false), + MoeRouter = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TensorCombos", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AiBenchmarks", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Ngl = table.Column(type: "INTEGER", nullable: false), + SizeBytes = table.Column(type: "INTEGER", nullable: false), + TokensPerSecond = table.Column(type: "REAL", nullable: false), + TensorComboId = table.Column(type: "INTEGER", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AiBenchmarks", x => x.Id); + table.ForeignKey( + name: "FK_AiBenchmarks_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AiBenchmarks_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "CategoryBenchmark", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AiBenchmarkId = table.Column(type: "INTEGER", nullable: false), + AiBenchmarkId1 = table.Column(type: "INTEGER", nullable: false), + Category = table.Column(type: "INTEGER", nullable: false), + Kld = table.Column(type: "REAL", nullable: false), + Ppl = table.Column(type: "REAL", nullable: false), + PplError = table.Column(type: "REAL", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CategoryBenchmark", x => x.Id); + table.ForeignKey( + name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId1", + column: x => x.AiBenchmarkId1, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_AiModelHashId_TensorComboId", + table: "AiBenchmarks", + columns: new[] { "AiModelHashId", "TensorComboId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_TensorComboId", + table: "AiBenchmarks", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_AiModelHashes_UniqueHash", + table: "AiModelHashes", + column: "UniqueHash"); + + migrationBuilder.CreateIndex( + name: "IX_CategoryBenchmark_AiBenchmarkId", + table: "CategoryBenchmark", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_CategoryBenchmark_AiBenchmarkId1", + table: "CategoryBenchmark", + column: "AiBenchmarkId1"); + + migrationBuilder.CreateIndex( + name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", + table: "TensorCombos", + columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "CategoryBenchmark"); + + migrationBuilder.DropTable( + name: "AiBenchmarks"); + + migrationBuilder.DropTable( + name: "AiModelHashes"); + + migrationBuilder.DropTable( + name: "TensorCombos"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs new file mode 100644 index 0000000..d401081 --- /dev/null +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -0,0 +1,187 @@ +// +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + partial class MagicQuantContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiBenchmarkId1") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiBenchmarkId1"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", null) + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId1") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} From 16b9cc0a1979e52834f4a17b46561a5aa78fce44 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 11 Apr 2026 15:16:44 -0400 Subject: [PATCH 042/258] tons of updates, still trying to get initial queue and benchmarking samples working. --- ... 20260411180357_InitialCreate.Designer.cs} | 15 +- ...ate.cs => 20260411180357_InitialCreate.cs} | 12 - .../MagicQuantContextModelSnapshot.cs | 13 +- MQ.DB/Models/BaselineQuants.cs | 24 +- MQ.DB/Models/BenchmarkResult.cs | 10 +- MQ.DB/Models/DbModels/AiBenchmark.cs | 51 +- MagicQuant/Commands/Evolution.cs | 16 +- MagicQuant/Commands/InitializeLlamaCpp.cs | 1 + MagicQuant/Helpers/HardDeleteHelper.cs | 48 ++ MagicQuant/Helpers/TensorConfigGenerator.cs | 38 +- MagicQuant/Services/BenchmarkService.cs | 711 ++++++++++++++---- MagicQuant/Services/QuantDatabaseService.cs | 67 +- MagicQuant/Services/QuantizationService.cs | 576 +++++++++----- 13 files changed, 1105 insertions(+), 477 deletions(-) rename MQ.DB/Migrations/{20260411162835_InitialCreate.Designer.cs => 20260411180357_InitialCreate.Designer.cs} (92%) rename MQ.DB/Migrations/{20260411162835_InitialCreate.cs => 20260411180357_InitialCreate.cs} (91%) create mode 100644 MagicQuant/Helpers/HardDeleteHelper.cs diff --git a/MQ.DB/Migrations/20260411162835_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260411180357_InitialCreate.Designer.cs similarity index 92% rename from MQ.DB/Migrations/20260411162835_InitialCreate.Designer.cs rename to MQ.DB/Migrations/20260411180357_InitialCreate.Designer.cs index 77c718e..f648965 100644 --- a/MQ.DB/Migrations/20260411162835_InitialCreate.Designer.cs +++ b/MQ.DB/Migrations/20260411180357_InitialCreate.Designer.cs @@ -10,7 +10,7 @@ namespace MQ.DB.Migrations { [DbContext(typeof(MagicQuantContext))] - [Migration("20260411162835_InitialCreate")] + [Migration("20260411180357_InitialCreate")] partial class InitialCreate { /// @@ -76,9 +76,6 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("AiBenchmarkId") .HasColumnType("INTEGER"); - b.Property("AiBenchmarkId1") - .HasColumnType("INTEGER"); - b.Property("Category") .HasColumnType("INTEGER"); @@ -95,8 +92,6 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("AiBenchmarkId"); - b.HasIndex("AiBenchmarkId1"); - b.ToTable("CategoryBenchmark"); }); @@ -165,18 +160,12 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", null) + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") .WithMany("CategorBenchmarks") .HasForeignKey("AiBenchmarkId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId1") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - b.Navigation("AiBenchmark"); }); diff --git a/MQ.DB/Migrations/20260411162835_InitialCreate.cs b/MQ.DB/Migrations/20260411180357_InitialCreate.cs similarity index 91% rename from MQ.DB/Migrations/20260411162835_InitialCreate.cs rename to MQ.DB/Migrations/20260411180357_InitialCreate.cs index 316a15e..79dd4eb 100644 --- a/MQ.DB/Migrations/20260411162835_InitialCreate.cs +++ b/MQ.DB/Migrations/20260411180357_InitialCreate.cs @@ -81,7 +81,6 @@ protected override void Up(MigrationBuilder migrationBuilder) Id = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), AiBenchmarkId = table.Column(type: "INTEGER", nullable: false), - AiBenchmarkId1 = table.Column(type: "INTEGER", nullable: false), Category = table.Column(type: "INTEGER", nullable: false), Kld = table.Column(type: "REAL", nullable: false), Ppl = table.Column(type: "REAL", nullable: false), @@ -96,12 +95,6 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "AiBenchmarks", principalColumn: "Id", onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId1", - column: x => x.AiBenchmarkId1, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( @@ -125,11 +118,6 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "CategoryBenchmark", column: "AiBenchmarkId"); - migrationBuilder.CreateIndex( - name: "IX_CategoryBenchmark_AiBenchmarkId1", - table: "CategoryBenchmark", - column: "AiBenchmarkId1"); - migrationBuilder.CreateIndex( name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", table: "TensorCombos", diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index d401081..3413d75 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -73,9 +73,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiBenchmarkId") .HasColumnType("INTEGER"); - b.Property("AiBenchmarkId1") - .HasColumnType("INTEGER"); - b.Property("Category") .HasColumnType("INTEGER"); @@ -92,8 +89,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("AiBenchmarkId"); - b.HasIndex("AiBenchmarkId1"); - b.ToTable("CategoryBenchmark"); }); @@ -162,18 +157,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", null) + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") .WithMany("CategorBenchmarks") .HasForeignKey("AiBenchmarkId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId1") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - b.Navigation("AiBenchmark"); }); diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 3f5fdc8..5202b29 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -8,12 +8,18 @@ public record BaselineQuants( ImmutableArray Names, HybridQuant? BaseConversionBase = null) { + /// + /// Reserved internal ID for the original/native source model (BF16/F16/F32). + /// This MUST NOT collide with any real llama.cpp export base quant. + /// + public const byte NativeSourceUniqueId = 250; + public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"]); public static readonly BaselineQuants Q6_K = new(1, false, ["Q6_K"]); public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"]); public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"]); - public static readonly BaselineQuants MXFP4_MOE = new(4, false, ["MXFP4_MOE"], + public static readonly BaselineQuants MXFP4_MOE = new(4, false, ["MXFP4_MOE"], new HybridQuant { BaseQuant = MXFP4_MOE, @@ -25,9 +31,8 @@ public record BaselineQuants( }) .ToList() }); - - - public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_XS"], + + public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_XS"], new HybridQuant { BaseQuant = IQ4_XS, @@ -41,11 +46,16 @@ public record BaselineQuants( }); public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"]); - + public static BaselineQuants GetBF16Quant() { - return new(0, false, [Cache.TorchType?.ToString() ?? "BF16"]); + return new( + NativeSourceUniqueId, + false, + [Cache.TorchType?.ToString() ?? "BF16"] + ); } + // IQ3 and lower require imatrix //public static readonly BaselineQuants IQ3_M = new(7, true, ["IQ3_M"], true); //public static readonly BaselineQuants IQ2_M = new(8, true, ["IQ2_M"], true); @@ -62,4 +72,4 @@ public static BaselineQuants GetBF16Quant() //IQ3_M, //IQ2_M ]; -} +} \ No newline at end of file diff --git a/MQ.DB/Models/BenchmarkResult.cs b/MQ.DB/Models/BenchmarkResult.cs index 3f873f4..f5281d1 100644 --- a/MQ.DB/Models/BenchmarkResult.cs +++ b/MQ.DB/Models/BenchmarkResult.cs @@ -3,5 +3,13 @@ namespace MQ.DB.Models; public class BenchmarkResult { public LlamaBenchMetrics? LlamaBench { get; set; } - public Dictionary Perplexity { get; set; } = new(); + + public Dictionary Perplexity { get; set; } = + new(StringComparer.OrdinalIgnoreCase); + + /// + /// Persisted into bench_metrics.json so disk-only reuse can still sync DB later + /// even if the temporary GGUF has already been deleted. + /// + public ulong? ModelSizeBytes { get; set; } } \ No newline at end of file diff --git a/MQ.DB/Models/DbModels/AiBenchmark.cs b/MQ.DB/Models/DbModels/AiBenchmark.cs index 67c0409..ab51675 100644 --- a/MQ.DB/Models/DbModels/AiBenchmark.cs +++ b/MQ.DB/Models/DbModels/AiBenchmark.cs @@ -11,7 +11,7 @@ public enum BenchmarkCategory Code = 3, } -public class AiBenchmark: ISQLiteEntity +public class AiBenchmark : ISQLiteEntity { public uint Id { get; set; } @@ -21,32 +21,28 @@ public class AiBenchmark: ISQLiteEntity public byte Ngl { get; set; } /// - /// Size of the model in bytes at this combination. + /// Size of the model in bytes at this combination. /// - /// public ulong SizeBytes { get; set; } public double TokensPerSecond { get; set; } - // both the AiModelHash and the TensorComboId combined - // must be unique in the table. - /// /// foreign key /// public uint TensorComboId { get; set; } - public TensorCombo TensorCombo { get; set; } + public TensorCombo TensorCombo { get; set; } = default!; /// /// foreign key /// public uint AiModelHashId { get; set; } - public AiModelHash AiModelHash { get; set; } - - public List CategorBenchmarks { get; set; } - + public AiModelHash AiModelHash { get; set; } = default!; + + public List CategorBenchmarks { get; set; } = new(); + public void Configure(EntityTypeBuilder builder) { builder.HasKey(x => x.Id); @@ -57,49 +53,48 @@ public void Configure(EntityTypeBuilder builder) builder.HasOne(x => x.TensorCombo) .WithMany() .HasForeignKey(x => x.TensorComboId) - .OnDelete(DeleteBehavior.Restrict); // Prevent deleting a combo if benchmarks exist + .OnDelete(DeleteBehavior.Restrict); builder.HasOne(x => x.AiModelHash) .WithMany() .HasForeignKey(x => x.AiModelHashId) .OnDelete(DeleteBehavior.Cascade); + + builder.HasMany(x => x.CategorBenchmarks) + .WithOne(x => x.AiBenchmark) + .HasForeignKey(x => x.AiBenchmarkId) + .OnDelete(DeleteBehavior.Cascade); } - } -public class CategoryBenchmark: ISQLiteEntity +public class CategoryBenchmark : ISQLiteEntity { public uint Id { get; set; } - + /// /// foreign key to AiBenchmark /// public uint AiBenchmarkId { get; set; } - public AiBenchmark AiBenchmark { get; set; } - + + public AiBenchmark AiBenchmark { get; set; } = default!; + /// /// Byte version to BenchmarkCategory enum in C# /// public byte Category { get; set; } - + public double Kld { get; set; } public double Ppl { get; set; } public double PplError { get; set; } - + public void Configure(EntityTypeBuilder builder) { builder.HasKey(x => x.Id); - // Foreign Key Configuration - builder.HasOne(x => x.AiBenchmark) - .WithMany() // One Benchmark has Many Category Scores - .HasForeignKey(x => x.AiBenchmarkId) - .OnDelete(DeleteBehavior.Cascade); // If you delete the Benchmark, delete its scores - builder.HasIndex(x => x.AiBenchmarkId); - - builder.HasOne() - .WithMany(p => p.CategorBenchmarks) + + builder.HasOne(x => x.AiBenchmark) + .WithMany(x => x.CategorBenchmarks) .HasForeignKey(x => x.AiBenchmarkId) .OnDelete(DeleteBehavior.Cascade); } diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 71e7c30..3dff2c8 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -101,10 +101,20 @@ public async Task Run(List args) // This ensures the DB is ready, populated, and valid before you proceed await dbService.InitializeAsync(); + AnsiConsole.Write(new Rule("[yellow]Required Sample Generation[/]") { Justification = Justify.Left }); + + var requiredSamples = TensorConfigGenerator.GenerateRequiredDataSampleCombos(Cache.UnusedTensorGroups); + + AnsiConsole.MarkupLine($"[grey]Queued required samples:[/] [cyan]{requiredSamples.Count:N0}[/]"); + AnsiConsole.MarkupLine("[grey]SQLite will be treated as the source of truth for completed samples.[/]"); + + var summary = await qService.ProcessHybridBatchAsync(requiredSamples); + + AnsiConsole.MarkupLine("[bold green]Sample generation phase complete.[/]"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {summary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {summary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {summary.Failed:N0}"); - - - // Todo: Have an end deletion process to remove the GGUF's and related success jsons, but not imatrix } private void ShowEvolutionHelp() diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 54352b0..21b7a31 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -62,6 +62,7 @@ public async Task Run(List args) // 3. Hardware Detection // --------------------------------------------------------- var sysInfo = HardwareHelper.GetSystemInfo(); + Cache.SysInfo = sysInfo; AnsiConsole.Write(new Rule("[yellow]System Detection[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"Detected GPU: [green]{sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor}[/] ([blue]{sysInfo.GpuInfo.FirstOrDefault()?.GpuName}[/] - {sysInfo.GpuInfo.Sum(x => x.VramGb):F1} GB)"); AnsiConsole.MarkupLine($"Detected RAM: [blue]{sysInfo.RamGb:F1} GB[/]"); diff --git a/MagicQuant/Helpers/HardDeleteHelper.cs b/MagicQuant/Helpers/HardDeleteHelper.cs new file mode 100644 index 0000000..d988248 --- /dev/null +++ b/MagicQuant/Helpers/HardDeleteHelper.cs @@ -0,0 +1,48 @@ +namespace MagicQuant.Helpers; + +public static class HardDeleteHelper +{ + public static async Task DeleteFileIfExistsAsync( + string? path, + int maxAttempts = 6, + int delayMs = 500) + { + if (string.IsNullOrWhiteSpace(path) || !File.Exists(path)) + return; + + Exception? lastError = null; + + for (int attempt = 1; attempt <= maxAttempts; attempt++) + { + try + { + var attributes = File.GetAttributes(path); + if ((attributes & FileAttributes.ReadOnly) != 0) + { + File.SetAttributes(path, attributes & ~FileAttributes.ReadOnly); + } + + File.Delete(path); + + if (!File.Exists(path)) + return; + } + catch (IOException ex) + { + lastError = ex; + } + catch (UnauthorizedAccessException ex) + { + lastError = ex; + } + + GC.Collect(); + GC.WaitForPendingFinalizers(); + await Task.Delay(delayMs); + } + + throw new IOException( + $"Failed to hard delete file '{path}' after {maxAttempts} attempts.", + lastError); + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 6441799..cb83692 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -12,7 +12,7 @@ public static List GenerateRequiredDataSampleCombos(List x.BaseConversionBase != null).ToList(); var hybridQuants = new List(); @@ -20,34 +20,26 @@ public static List GenerateRequiredDataSampleCombos(List x.UniqueId).ToHashSet() ?? new HashSet(); // --------------------------------------------------------- - // 1. BASELINE CONTROLS (One pure sample per allowed baseline) + // 1. PURE BASELINE CONTROLS // --------------------------------------------------------- + // These must be TRUE baseline exports with NO tensor overrides at all. + // Otherwise you are not testing the baseline quant, you're testing a weird hybrid. int baseTestsRequired = 0; foreach (var baseline in allowedBaselines) { baseTestsRequired++; - var hq = new HybridQuant + + hybridQuants.Add(new HybridQuant { BaseQuant = baseline, - Tensors = TReg.All - .Select(g => new HybridTensor - { - TGroup = g, - // If missing, mark NULL. Else default to BF16. - TensorType = missingIds.Contains(g.UniqueId) - ? TensorWeightScheme.NULL - : TensorWeightScheme.BF16_F16 - }) - .ToList() - }; - - hybridQuants.Add(hq); + Tensors = new List() + }); } - AnsiConsole.MarkupLine($"[bold green]Required BF16 base hybrid tests:[/] {baseTestsRequired:N0}"); + AnsiConsole.MarkupLine($"[bold green]Required pure baseline hybrid tests:[/] {baseTestsRequired:N0}"); // --------------------------------------------------------- - // 2. ISOLATION SAMPLES (Always BF16 Base, isolate one tensor at a time) + // 2. ISOLATION SAMPLES (BF16/F16/F32 source base, one tensor altered) // --------------------------------------------------------- var tensorWeights = TensorWeightScheme.All .Where(x => x != TensorWeightScheme.NULL && x != TensorWeightScheme.BF16_F16) @@ -55,12 +47,10 @@ public static List GenerateRequiredDataSampleCombos(List !weight.BannedGroups.Contains(x)).ToList(); if (missingIds.Count > 0) @@ -72,7 +62,6 @@ public static List GenerateRequiredDataSampleCombos(List new HybridTensor { TGroup = g, @@ -81,17 +70,14 @@ public static List GenerateRequiredDataSampleCombos(List x.TGroup == group); foundQuant.TensorType = weight; - var hq = new HybridQuant + hybridQuants.Add(new HybridQuant { BaseQuant = isolationBase, Tensors = tensors - }; - - hybridQuants.Add(hq); + }); } } diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 2ea7155..a8e5533 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -17,6 +17,87 @@ public class BenchmarkService private readonly LlamaBinaries _bins; public readonly PythonManager _pyManager; + private static readonly string[] BaseDomains = { "general", "code", "math" }; + private static readonly string[] SampleDomains = { "general" }; + + private static bool IsNativeBaseModel(HybridQuant quantConfig) + { + return quantConfig.BaseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId; + } + + private static IReadOnlyCollection ResolveRequestedDomains( + HybridQuant quantConfig, + IReadOnlyCollection? domainsOverride) + { + if (domainsOverride != null && domainsOverride.Count > 0) + { + return domainsOverride + .Select(x => x.Trim().ToLowerInvariant()) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + } + + return IsNativeBaseModel(quantConfig) ? BaseDomains : SampleDomains; + } + + private static bool RequiresKld(HybridQuant quantConfig) + { + return !IsNativeBaseModel(quantConfig); + } + + private static ulong TryGetModelSize(string modelPath) + { + return File.Exists(modelPath) ? (ulong)new FileInfo(modelPath).Length : 0UL; + } + + private static bool IsPositiveKld(double? kld) + { + return kld.HasValue && kld.Value > 0d; + } + + public async Task TryReuseExistingBenchmarksAsync( + HybridQuant quantConfig, + string modelPath, + string benchDir, + string? klLogitsDir, + IReadOnlyCollection? domainsOverride = null) + { + var requestedDomains = ResolveRequestedDomains(quantConfig, domainsOverride); + bool requireKld = RequiresKld(quantConfig); + + if (!TryReadExistingBenchmarkArtifacts( + benchDir: benchDir, + requestedDomains: requestedDomains, + requireKld: requireKld, + result: out var reused)) + { + return false; + } + + reused.ModelSizeBytes ??= TryGetModelSize(modelPath); + + using var db = new MagicQuantContext(); + + var currentHashStr = Cache.CurrentModelId; + var aiModelHash = await db.AiModelHashes + .FirstOrDefaultAsync(x => x.UniqueHash == currentHashStr); + + if (aiModelHash == null) + { + aiModelHash = new AiModelHash { UniqueHash = currentHashStr }; + db.AiModelHashes.Add(aiModelHash); + await db.SaveChangesAsync(); + } + + var tensorCombo = await GetOrCreateTensorComboAsync(db, quantConfig); + + await SaveBenchmarkToDbAsync(db, aiModelHash, tensorCombo, reused, modelPath); + await WriteMetricsJsonAsync(benchDir, reused); + + return true; + } + // Constants private static readonly string[] OomMarkers = { @@ -50,14 +131,16 @@ public async Task RunAllBenchmarksAsync( int tokenTarget = 32768, int? startNgl = null, string? klLogitsDir = null, - bool saveLogits = false) + bool saveLogits = false, + IReadOnlyCollection? domainsOverride = null) { Directory.CreateDirectory(benchDir); - string jsonPath = Path.Combine(benchDir, "bench_metrics.json"); + + var requestedDomains = ResolveRequestedDomains(quantConfig, domainsOverride); + bool requireKld = RequiresKld(quantConfig); using var db = new MagicQuantContext(); - - // 1. Resolve Model Hash + var currentHashStr = Cache.CurrentModelId; var aiModelHash = await db.AiModelHashes .FirstOrDefaultAsync(x => x.UniqueHash == currentHashStr); @@ -69,57 +152,78 @@ public async Task RunAllBenchmarksAsync( await db.SaveChangesAsync(); } - // 2. Resolve Tensor Combo (Fixed to use Constructor) var tensorCombo = await GetOrCreateTensorComboAsync(db, quantConfig); - // 3. Check if Benchmark already exists var existingBench = await db.AiBenchmarks + .Include(x => x.CategorBenchmarks) + .AsNoTracking() .FirstOrDefaultAsync(b => b.AiModelHashId == aiModelHash.Id && b.TensorComboId == tensorCombo.Id); - if (existingBench != null) + // 1. If DB already has everything required, trust DB first and don't rerun. + if (existingBench != null && HasRequiredCategories(existingBench, requestedDomains, requireKld)) { - AnsiConsole.MarkupLine($"[green]Benchmark found in database for Combo ID {tensorCombo.Id}. Skipping execution.[/]"); - - if (File.Exists(jsonPath)) + if (TryReadExistingBenchmarkArtifacts(benchDir, requestedDomains, requireKld, out var diskResult)) { - var cachedJson = await File.ReadAllTextAsync(jsonPath); - if (!string.IsNullOrWhiteSpace(cachedJson)) - { - var deserializedMetrics = JsonSerializer.Deserialize(cachedJson); - if (deserializedMetrics != null) return deserializedMetrics; - } + diskResult.ModelSizeBytes ??= existingBench.SizeBytes; + return diskResult; } - return new BenchmarkResult(); + + return BuildResultFromDb(existingBench, requestedDomains); } - // ---------------------------------------------------------------- - // 4. Execution - // ---------------------------------------------------------------- + // 2. If disk already has reusable artifacts, sync DB and return. + if (TryReadExistingBenchmarkArtifacts(benchDir, requestedDomains, requireKld, out var reused)) + { + reused.ModelSizeBytes ??= TryGetModelSize(modelPath); + await SaveBenchmarkToDbAsync(db, aiModelHash, tensorCombo, reused, modelPath); + await WriteMetricsJsonAsync(benchDir, reused); + return reused; + } - var result = new BenchmarkResult(); + // 3. Otherwise run only the pieces that are actually missing/invalid. + var result = new BenchmarkResult + { + ModelSizeBytes = TryGetModelSize(modelPath) + }; - // A. Run Llama-Bench - await ExclusiveBenchLock.WaitAsync(); - try + string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); + if (TryReadExistingLlamaBenchLog(llamaBenchPath, out var existingLlamaBench)) { - AnsiConsole.MarkupLine("[yellow]Running Llama-Bench (Exclusive Mode)...[/]"); - result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, startNgl); + result.LlamaBench = existingLlamaBench; } - finally + else { - ExclusiveBenchLock.Release(); + await ExclusiveBenchLock.WaitAsync(); + try + { + AnsiConsole.MarkupLine("[yellow]Running Llama-Bench (Exclusive Mode)...[/]"); + result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, startNgl); + } + finally + { + ExclusiveBenchLock.Release(); + } } - // B. Run Perplexity - var domains = new[] { "general", "code", "math" }; var corporaRoot = Path.Combine(Path.GetDirectoryName(benchDir)!, "_ppl_corpora"); Directory.CreateDirectory(corporaRoot); if (saveLogits && !string.IsNullOrEmpty(klLogitsDir)) Directory.CreateDirectory(klLogitsDir); - foreach (var domain in domains) + foreach (var domain in requestedDomains) { + if (TryReadExistingPplLog( + benchDir: benchDir, + domain: domain, + allowMissingKld: !requireKld, + requirePositiveKld: requireKld, + metrics: out var existingPpl)) + { + result.Perplexity[domain] = existingPpl; + continue; + } + string corpusPath = Path.Combine(corporaRoot, $"ppl_corpus_{domain}.txt"); await PreparePplCorpusAsync(domain, corpusPath, tokenTarget); @@ -128,9 +232,21 @@ public async Task RunAllBenchmarksAsync( { AnsiConsole.MarkupLine($"[yellow]Running Perplexity ({domain})...[/]"); var metrics = await RunPplBenchmarkAsync( - modelPath, benchDir, domain, corpusPath, - startNgl, klLogitsDir, saveLogits - ); + modelPath: modelPath, + benchDir: benchDir, + domain: domain, + corpusPath: corpusPath, + startNgl: startNgl, + klLogitsDir: klLogitsDir, + saveLogits: saveLogits); + + if (requireKld && !IsPositiveKld(metrics.Kld)) + { + throw new InvalidOperationException( + $"Non-base benchmark produced invalid KLD for domain '{domain}'. " + + $"KLD must exist and be > 0. Parsed value: {(metrics.Kld.HasValue ? metrics.Kld.Value.ToString() : "null")}"); + } + result.Perplexity[domain] = metrics; } finally @@ -139,11 +255,7 @@ public async Task RunAllBenchmarksAsync( } } - // 5. Save Results - await File.WriteAllTextAsync(jsonPath, - JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true })); - - // Pass modelPath so we can calculate SizeBytes + await WriteMetricsJsonAsync(benchDir, result); await SaveBenchmarkToDbAsync(db, aiModelHash, tensorCombo, result, modelPath); return result; @@ -158,7 +270,7 @@ private async Task GetOrCreateTensorComboAsync(MagicQuantContext db // 1. Extract values into local variables. // Default to 0 (NULL scheme) if not present in the mutable list. byte baseQuant = quant.BaseQuant.UniqueId; - + byte embeddings = 0; byte lmHead = 0; byte attnQ = 0; @@ -225,91 +337,103 @@ private async Task GetOrCreateTensorComboAsync(MagicQuantContext db } private async Task SaveBenchmarkToDbAsync( - MagicQuantContext db, - AiModelHash model, - TensorCombo combo, + MagicQuantContext db, + AiModelHash model, + TensorCombo combo, BenchmarkResult res, string modelPath) { using var transaction = await db.Database.BeginTransactionAsync(); + try { - // Calculate File Size - ulong sizeBytes = 0; - if (File.Exists(modelPath)) + bool isBaseModel = + combo.BaseQuant == BaselineQuants.NativeSourceUniqueId && + combo.Embeddings == 0 && + combo.LmHead == 0 && + combo.AttnQ == 0 && + combo.AttnKV == 0 && + combo.AttnOutput == 0 && + combo.FfnUpGate == 0 && + combo.FfnDown == 0 && + combo.MoeExperts == 0 && + combo.MoeRouter == 0; + + ulong sizeBytes = + res.ModelSizeBytes.GetValueOrDefault() > 0 + ? res.ModelSizeBytes!.Value + : (File.Exists(modelPath) ? (ulong)new FileInfo(modelPath).Length : 0UL); + + var bench = await db.AiBenchmarks + .Include(x => x.CategorBenchmarks) + .FirstOrDefaultAsync(x => + x.AiModelHashId == model.Id && + x.TensorComboId == combo.Id); + + if (bench == null) { - sizeBytes = (ulong)new FileInfo(modelPath).Length; + bench = new AiBenchmark + { + AiModelHashId = model.Id, + TensorComboId = combo.Id + }; + + db.AiBenchmarks.Add(bench); } - // 1. Create Parent Benchmark - var bench = new AiBenchmark - { - AiModelHashId = model.Id, - TensorComboId = combo.Id, - - // Map LlamaBench fields - TokensPerSecond = res.LlamaBench?.Tps ?? 0, - Ngl = (byte)(res.LlamaBench?.Ngl ?? 0), - SizeBytes = sizeBytes - }; + bench.TokensPerSecond = res.LlamaBench?.Tps ?? 0; + bench.Ngl = (byte)(res.LlamaBench?.Ngl ?? 0); + bench.SizeBytes = sizeBytes; - db.AiBenchmarks.Add(bench); - await db.SaveChangesAsync(); // Generates bench.Id + await db.SaveChangesAsync(); - // 2. Create Child Category Benchmarks - var categories = new List(); + if (bench.CategorBenchmarks != null && bench.CategorBenchmarks.Count > 0) + { + db.Set().RemoveRange(bench.CategorBenchmarks); + await db.SaveChangesAsync(); + } - // Helper to determine if we need to force 0.0 KLD for Base Model - // (Checks if everything is 0 except BaseQuant which is BF16/F16) - bool isBaseModel = combo.BaseQuant == TensorWeightScheme.BF16_F16.UniqueId && - combo.Embeddings == 0 && combo.LmHead == 0; + var categories = new List(); - // Map "general" -> BenchmarkCategory.General (1) - if (res.Perplexity.ContainsKey("general")) + foreach (var kvp in res.Perplexity) { - var m = res.Perplexity["general"]; - var cb = new CategoryBenchmark + string domain = kvp.Key.ToLowerInvariant(); + var m = kvp.Value; + + byte category = domain switch { - AiBenchmarkId = bench.Id, - Category = (byte)BenchmarkCategory.General, - Ppl = m.Ppl, - PplError = m.PplError, - Kld = m.Kld ?? (isBaseModel ? 0.0 : 0.0) // Defaults to 0 if null + "general" => (byte)BenchmarkCategory.General, + "math" => (byte)BenchmarkCategory.Math, + "code" => (byte)BenchmarkCategory.Code, + _ => throw new InvalidOperationException($"Unknown benchmark domain '{domain}'.") }; - categories.Add(cb); - } - // Map "code" -> BenchmarkCategory.Code (3) - if (res.Perplexity.ContainsKey("code")) - { - var m = res.Perplexity["code"]; - var cb = new CategoryBenchmark + double kld; + if (isBaseModel) { - AiBenchmarkId = bench.Id, - Category = (byte)BenchmarkCategory.Code, - Ppl = m.Ppl, - PplError = m.PplError, - Kld = m.Kld ?? (isBaseModel ? 0.0 : 0.0) - }; - categories.Add(cb); - } + kld = 0d; + } + else + { + if (!IsPositiveKld(m.Kld)) + { + throw new InvalidOperationException( + $"Refusing to save non-base benchmark with invalid KLD. Domain='{domain}', KLD='{m.Kld?.ToString() ?? "null"}'"); + } - // Map "math" -> BenchmarkCategory.Math (2) - if (res.Perplexity.ContainsKey("math")) - { - var m = res.Perplexity["math"]; - var cb = new CategoryBenchmark + kld = m.Kld!.Value; + } + + categories.Add(new CategoryBenchmark { AiBenchmarkId = bench.Id, - Category = (byte)BenchmarkCategory.Math, + Category = category, Ppl = m.Ppl, PplError = m.PplError, - Kld = m.Kld ?? (isBaseModel ? 0.0 : 0.0) - }; - categories.Add(cb); + Kld = kld + }); } - // Batch Insert Categories if (categories.Count > 0) { db.Set().AddRange(categories); @@ -317,15 +441,238 @@ private async Task SaveBenchmarkToDbAsync( } await transaction.CommitAsync(); - AnsiConsole.MarkupLine("[green]Benchmarks saved to Database successfully.[/]"); } catch (Exception ex) { - AnsiConsole.MarkupLine($"[red]Failed to save benchmarks to DB: {ex.Message}[/]"); await transaction.RollbackAsync(); + + var inner = ex.InnerException?.Message; + if (!string.IsNullOrWhiteSpace(inner)) + { + AnsiConsole.MarkupLine($"[red]Failed to save benchmarks to DB:[/] {Markup.Escape(ex.Message)}"); + AnsiConsole.MarkupLine($"[red]Inner Exception:[/] {Markup.Escape(inner)}"); + } + else + { + AnsiConsole.MarkupLine($"[red]Failed to save benchmarks to DB:[/] {Markup.Escape(ex.Message)}"); + } + + throw; } } + private async Task WriteMetricsJsonAsync(string benchDir, BenchmarkResult result) + { + Directory.CreateDirectory(benchDir); + + string jsonPath = Path.Combine(benchDir, "bench_metrics.json"); + await File.WriteAllTextAsync( + jsonPath, + JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true })); + } + + private bool TryReadExistingBenchmarkArtifacts( + string benchDir, + IReadOnlyCollection requestedDomains, + bool requireKld, + out BenchmarkResult result) + { + result = new BenchmarkResult(); + + string jsonPath = Path.Combine(benchDir, "bench_metrics.json"); + if (File.Exists(jsonPath)) + { + try + { + var parsed = JsonSerializer.Deserialize(File.ReadAllText(jsonPath)); + if (parsed != null && IsReusableBenchmarkResult(parsed, requestedDomains, requireKld)) + { + result = parsed; + return true; + } + } + catch + { + // fall through and try rebuilding from individual logs + } + } + + string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); + if (!TryReadExistingLlamaBenchLog(llamaBenchPath, out var llamaBench)) + return false; + + var rebuilt = new BenchmarkResult + { + LlamaBench = llamaBench + }; + + foreach (var domain in requestedDomains) + { + if (!TryReadExistingPplLog( + benchDir: benchDir, + domain: domain, + allowMissingKld: !requireKld, + requirePositiveKld: requireKld, + metrics: out var ppl)) + { + return false; + } + + rebuilt.Perplexity[domain] = ppl; + } + + result = rebuilt; + return true; + } + + private bool IsReusableBenchmarkResult( + BenchmarkResult result, + IReadOnlyCollection requestedDomains, + bool requireKld) + { + if (result.LlamaBench == null || !result.LlamaBench.Tps.HasValue || result.LlamaBench.Tps.Value <= 0) + return false; + + foreach (var domain in requestedDomains) + { + if (!result.Perplexity.TryGetValue(domain, out var ppl)) + return false; + + if (ppl.Ppl <= 0 || ppl.PplError < 0) + return false; + + if (requireKld && !IsPositiveKld(ppl.Kld)) + return false; + } + + return true; + } + + private bool TryReadExistingLlamaBenchLog(string logPath, out LlamaBenchMetrics metrics) + { + metrics = null!; + + if (!File.Exists(logPath) || new FileInfo(logPath).Length == 0) + return false; + + try + { + var parsed = ParseLlamaBench(logPath); + if (parsed.Tps.HasValue && parsed.Tps.Value > 0) + { + metrics = parsed; + return true; + } + } + catch + { + // ignore and return false + } + + return false; + } + + private bool TryReadExistingPplLog( + string benchDir, + string domain, + bool allowMissingKld, + bool requirePositiveKld, + out PplMetrics metrics) + { + metrics = null!; + + string logPath = Path.Combine(benchDir, $"perplexity_{domain}.log"); + if (!File.Exists(logPath) || new FileInfo(logPath).Length == 0) + return false; + + try + { + var parsed = ParsePerplexity(logPath, allowMissingKld); + + if (parsed.Ppl <= 0) + return false; + + if (requirePositiveKld && !IsPositiveKld(parsed.Kld)) + return false; + + metrics = parsed; + return true; + } + catch + { + return false; + } + } + + private static bool HasRequiredCategories( + AiBenchmark bench, + IReadOnlyCollection requestedDomains, + bool requireKld) + { + if (bench.CategorBenchmarks == null || bench.CategorBenchmarks.Count == 0) + return false; + + foreach (var domain in requestedDomains) + { + byte category = domain switch + { + "general" => (byte)BenchmarkCategory.General, + "math" => (byte)BenchmarkCategory.Math, + "code" => (byte)BenchmarkCategory.Code, + _ => throw new InvalidOperationException($"Unknown benchmark domain '{domain}'.") + }; + + var existing = bench.CategorBenchmarks.FirstOrDefault(x => x.Category == category); + if (existing == null) + return false; + + if (existing.Ppl <= 0) + return false; + + if (requireKld && existing.Kld <= 0) + return false; + } + + return bench.TokensPerSecond > 0; + } + + private BenchmarkResult BuildResultFromDb( + AiBenchmark bench, + IReadOnlyCollection requestedDomains) + { + var result = new BenchmarkResult + { + ModelSizeBytes = bench.SizeBytes, + LlamaBench = new LlamaBenchMetrics + { + Ngl = bench.Ngl, + Tps = bench.TokensPerSecond + } + }; + + foreach (var domain in requestedDomains) + { + byte category = domain switch + { + "general" => (byte)BenchmarkCategory.General, + "math" => (byte)BenchmarkCategory.Math, + "code" => (byte)BenchmarkCategory.Code, + _ => throw new InvalidOperationException($"Unknown benchmark domain '{domain}'.") + }; + + var existing = bench.CategorBenchmarks.First(x => x.Category == category); + + result.Perplexity[domain] = new PplMetrics + { + Ppl = existing.Ppl, + PplError = existing.PplError, + Kld = existing.Kld + }; + } + + return result; + } + // ---------------------------------------------------------------- // 1. Llama-Bench Logic (Unchanged) // ---------------------------------------------------------------- @@ -371,7 +718,8 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) if (headerIdx == -1 || lines.Length <= headerIdx + 2) return metrics; var headers = lines[headerIdx].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(h => h.Trim()).ToList(); - var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()).ToList(); + var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()) + .ToList(); if (headers.Count != dataRow.Count) return metrics; var row = headers.Zip(dataRow, (h, d) => new { Header = h, Data = d }).ToDictionary(x => x.Header, x => x.Data); @@ -395,8 +743,13 @@ private LlamaBenchMetrics ParseLlamaBench(string logPath) // ---------------------------------------------------------------- private async Task RunPplBenchmarkAsync( - string modelPath, string benchDir, string domain, string corpusPath, - int? startNgl, string? klLogitsDir, bool saveLogits) + string modelPath, + string benchDir, + string domain, + string corpusPath, + int? startNgl, + string? klLogitsDir, + bool saveLogits) { string logFile = Path.Combine(benchDir, $"perplexity_{domain}.log"); var candidates = startNgl.HasValue @@ -404,13 +757,24 @@ private async Task RunPplBenchmarkAsync( : NglCandidates.ToList(); string kldArgs = ""; + bool expectKld = false; + if (!string.IsNullOrEmpty(klLogitsDir)) { string logitsFile = Path.Combine(klLogitsDir, $"kld_logits_{domain}.bin"); + if (saveLogits) + { + // Base/native model path: save logits only, do not expect KLD yet. kldArgs = $"--kl-divergence-base \"{logitsFile}\""; + expectKld = false; + } else if (File.Exists(logitsFile)) + { + // Sample path: compare against the already saved base logits. kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; + expectKld = true; + } } string BuildCmd(int ngl) => @@ -418,39 +782,55 @@ string BuildCmd(int ngl) => await RunWithRetryAsync(BuildCmd, logFile, candidates, $"perplexity-{domain}"); - bool expectKld = (!saveLogits && !string.IsNullOrEmpty(klLogitsDir)); - return ParsePerplexity(logFile, expectKld); + bool allowMissingKld = !expectKld; + var parsed = ParsePerplexity(logFile, allowMissingKld); + + if (expectKld && !IsPositiveKld(parsed.Kld)) + { + throw new InvalidOperationException( + $"Expected a real KLD for domain '{domain}', but parsed '{parsed.Kld?.ToString() ?? "null"}' from {logFile}"); + } + + return parsed; } private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) { var metrics = new PplMetrics { LogPath = GetRelativePath(logPath) }; - if (!File.Exists(logPath)) return metrics; + + if (!File.Exists(logPath)) + throw new FileNotFoundException($"Perplexity log file was not created: {logPath}"); string text = File.ReadAllText(logPath); string cleanText = StripAnsi(text); - var pplMatch = Regex.Match(cleanText, @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([0-9.]+)\s*(?:±|\+/-)\s*([0-9.]+)", RegexOptions.IgnoreCase); + var pplMatch = Regex.Match( + cleanText, + @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*(?:±|\+/-)\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)", + RegexOptions.IgnoreCase); - if (pplMatch.Success) - { - metrics.Ppl = double.Parse(pplMatch.Groups[1].Value); - metrics.PplError = double.Parse(pplMatch.Groups[2].Value); - } - else + if (!pplMatch.Success) { - AnsiConsole.MarkupLine($"[red]Error parsing PPL from {logPath}[/]"); + throw new InvalidOperationException( + $"Failed to parse PPL from log: {logPath}\n\nLast log content:\n{cleanText}"); } - var kldMatch = Regex.Match(cleanText, @"(?:Mean\s+KLD|KL[-_\s]*divergence|kl[-_\s]*div)\s*[:=]\s*([0-9.]+)", RegexOptions.IgnoreCase); + metrics.Ppl = double.Parse(pplMatch.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture); + metrics.PplError = double.Parse(pplMatch.Groups[2].Value, System.Globalization.CultureInfo.InvariantCulture); + + var kldMatch = Regex.Match( + cleanText, + @"(?:Mean\s+KLD|Mean\s+KL|KL[-_\s]*divergence|KLD|kl[-_\s]*div)\s*[:=]\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)", + RegexOptions.IgnoreCase); if (kldMatch.Success) { - metrics.Kld = double.Parse(kldMatch.Groups[1].Value); + metrics.Kld = double.Parse(kldMatch.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture); } - else if (!allowMissingKld && cleanText.Contains("KL", StringComparison.OrdinalIgnoreCase)) + else if (!allowMissingKld) { - AnsiConsole.MarkupLine("[yellow]Warning: 'KL' found in log but regex failed to parse value.[/]"); + throw new InvalidOperationException( + $"KLD was expected but could not be parsed from log: {logPath}\n\nLast log content:\n{cleanText}"); } return metrics; @@ -524,26 +904,57 @@ with open(out_path, 'w', encoding='utf-8') as f: List candidates, string label) { + string? lastFailureDetails = null; + foreach (int ngl in candidates) { string cmd = cmdBuilder(ngl); AnsiConsole.WriteLine($"[*] {label}: trying -ngl {ngl}"); - await RunShellCommandAsync(cmd, logPath); + var result = await RunShellCommandAsync(cmd, logPath); + + string logContent = !string.IsNullOrWhiteSpace(result.LogOutput) + ? result.LogOutput + : (File.Exists(logPath) ? File.ReadAllText(logPath) : string.Empty); - string logContent = File.Exists(logPath) - ? File.ReadAllText(logPath) - : string.Empty; + bool looksLikeOom = OomMarkers.Any(m => + logContent.Contains(m, StringComparison.OrdinalIgnoreCase)); - if (OomMarkers.Any(m => logContent.Contains(m, StringComparison.OrdinalIgnoreCase))) + bool looksLikeLoadFailure = + logContent.Contains("failed to load model", StringComparison.OrdinalIgnoreCase) || + logContent.Contains("error:", StringComparison.OrdinalIgnoreCase); + + if (!result.Success) { - AnsiConsole.WriteLine($"[WARN] {label}: OOM at -ngl {ngl}, retrying..."); + lastFailureDetails = + $"ExitCode={result.ExitCode}, ngl={ngl}\nCommand: {cmd}\n\nLog Output:\n{logContent}"; + + if (looksLikeOom || looksLikeLoadFailure) + { + AnsiConsole.WriteLine($"[WARN] {label}: failed at -ngl {ngl}, retrying lower setting..."); + continue; + } + + // Unknown non-zero exit: still retry lower ngl first, + // because many llama.cpp GPU/load issues recover that way. + AnsiConsole.WriteLine($"[WARN] {label}: non-zero exit at -ngl {ngl}, retrying lower setting..."); continue; } if (logContent.Length < 50) { - AnsiConsole.WriteLine($"[WARN] {label}: Failed at -ngl {ngl} (Unknown Error), trying next..."); + lastFailureDetails = + $"Log too short at ngl={ngl}\nCommand: {cmd}\n\nLog Output:\n{logContent}"; + AnsiConsole.WriteLine($"[WARN] {label}: Failed at -ngl {ngl} (log too short), trying next..."); + continue; + } + + if (label.StartsWith("perplexity", StringComparison.OrdinalIgnoreCase) && + !Regex.IsMatch(logContent, @"PPL\s*[:=]\s*[-+]?\d*\.?\d+", RegexOptions.IgnoreCase)) + { + lastFailureDetails = + $"No parsable PPL marker found at ngl={ngl}\nCommand: {cmd}\n\nLog Output:\n{logContent}"; + AnsiConsole.WriteLine($"[WARN] {label}: No parsable PPL marker found at -ngl {ngl}, trying next..."); continue; } @@ -551,15 +962,22 @@ with open(out_path, 'w', encoding='utf-8') as f: return ngl; } - AnsiConsole.WriteLine($"[ERROR] {label}: All -ngl candidates failed."); - return null; + throw new InvalidOperationException( + $"{label}: all -ngl candidates failed.\n\nLast failure details:\n{lastFailureDetails}"); } // ---------------------------------------------------------------- // 5. System Utilities // ---------------------------------------------------------------- - private async Task RunShellCommandAsync(string cmd, string? logPath) + private sealed class CommandRunResult + { + public bool Success { get; init; } + public int ExitCode { get; init; } + public string LogOutput { get; init; } = string.Empty; + } + + private async Task RunShellCommandAsync(string cmd, string? logPath) { var startInfo = new ProcessStartInfo { @@ -578,19 +996,40 @@ private async Task RunShellCommandAsync(string cmd, string? logPath) if (logPath != null) { fs = new FileStream(logPath, FileMode.Create, FileAccess.Write, FileShare.Read); - sw = new StreamWriter(fs); + sw = new StreamWriter(fs) { AutoFlush = true }; } - process.OutputDataReceived += (s, e) => { if (e.Data != null) sw?.WriteLine(e.Data); }; - process.ErrorDataReceived += (s, e) => { if (e.Data != null) sw?.WriteLine(e.Data); }; - process.Start(); - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); + + var stdoutTask = process.StandardOutput.ReadToEndAsync(); + var stderrTask = process.StandardError.ReadToEndAsync(); + await process.WaitForExitAsync(); + string stdout = await stdoutTask; + string stderr = await stderrTask; + + if (!string.IsNullOrWhiteSpace(stdout)) + sw?.WriteLine(stdout); + + if (!string.IsNullOrWhiteSpace(stderr)) + sw?.WriteLine(stderr); + sw?.Dispose(); fs?.Dispose(); + + string combinedLog; + if (logPath != null && File.Exists(logPath)) + combinedLog = File.ReadAllText(logPath); + else + combinedLog = $"{stdout}\n{stderr}"; + + return new CommandRunResult + { + Success = process.ExitCode == 0, + ExitCode = process.ExitCode, + LogOutput = combinedLog + }; } private string StripAnsi(string text) diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 14341ca..80c1f8e 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -12,59 +12,67 @@ public class QuantDatabaseService { private const string DbFileName = "MagicQuant_Combinations.duckdb"; private const string TableName = "tensor_configs"; - - // Connection string points to the file in your cache directory - private string ConnectionString => $"Data Source={Path.Combine(Cache.MagicQuantDirectory, DbFileName)}"; + + private static string GetDuckDbDirectory() + { + if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + return Cache.ModelMagicQuantDirectory; + + if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) + return Cache.MagicQuantDirectory; + + throw new InvalidOperationException( + "Neither Cache.ModelMagicQuantDirectory nor Cache.MagicQuantDirectory is set."); + } + + private string ConnectionString => $"Data Source={Path.Combine(GetDuckDbDirectory(), DbFileName)}"; public async Task InitializeAsync(CancellationToken ct = default) { - // 1. Ensure directory exists - Directory.CreateDirectory(Cache.MagicQuantDirectory); + var duckDbDirectory = GetDuckDbDirectory(); + Directory.CreateDirectory(duckDbDirectory); - // 2. Open connection to check state using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); BigInteger expectedTotal = ComboCounter.CountAll(); long currentDbCount = await GetRowCountAsync(connection, ct); - AnsiConsole.MarkupLine($"[bold]DB Check:[/] Current Rows: [cyan]{currentDbCount:N0}[/] | Expected: [yellow]{expectedTotal:N0}[/]"); + AnsiConsole.MarkupLine( + $"[bold]DuckDB Check:[/] Current Rows: [cyan]{currentDbCount:N0}[/] | Expected: [yellow]{expectedTotal:N0}[/]"); - // 3. Validation Logic: If counts mismatch or table missing, rebuild. if (currentDbCount != expectedTotal) { - - AnsiConsole.MarkupLine("[bold red]Database empty, mismatch, or new.[/] Initializing/Rebuilding..."); - + AnsiConsole.MarkupLine("[bold red]DuckDB empty, mismatch, or new.[/] Initializing/Rebuilding..."); await RebuildDatabaseAsync(connection, expectedTotal, ct); } else { - AnsiConsole.MarkupLine("[bold green]Database is synchronized and ready.[/]"); + AnsiConsole.MarkupLine("[bold green]DuckDB is synchronized and ready.[/]"); } } private async Task GetRowCountAsync(DuckDBConnection connection, CancellationToken ct) { - // Check if table exists first var checkCmd = connection.CreateCommand(); checkCmd.CommandText = $"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{TableName}'"; var exists = (long)(await checkCmd.ExecuteScalarAsync(ct) ?? 0); - if (exists == 0) return -1; // Marker for "Table doesn't exist" + if (exists == 0) + return -1; - // Get count var countCmd = connection.CreateCommand(); countCmd.CommandText = $"SELECT COUNT(*) FROM {TableName}"; return (long)(await countCmd.ExecuteScalarAsync(ct) ?? 0); } - private async Task RebuildDatabaseAsync(DuckDBConnection connection, BigInteger expectedTotal, CancellationToken ct) + private async Task RebuildDatabaseAsync( + DuckDBConnection connection, + BigInteger expectedTotal, + CancellationToken ct) { var sw = Stopwatch.StartNew(); - // 1. Drop and Recreate Table - // We map byte (C#) to TINYINT (DuckDB) var createCmd = connection.CreateCommand(); createCmd.CommandText = $@" DROP TABLE IF EXISTS {TableName}; @@ -82,12 +90,8 @@ MoeRouter TINYINT );"; await createCmd.ExecuteNonQueryAsync(ct); - // 2. Generate and Bulk Insert - // We use the Appender for high-performance bulk writing - long insertedTotal = 0; - // Iterate through your existing generator logic var bases = BaselineQuants.All .Where(b => b.BaseConversionBase != null) .ToList(); @@ -96,18 +100,17 @@ MoeRouter TINYINT foreach (var b in bases) { - // We reuse the generator you already wrote - foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches(b, batchSize: 1_000_000, ct: ct)) + foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches( + b, + batchSize: 1_000_000, + ct: ct)) { - // OPEN APPENDER for this batch - // Note: DuckDB Appender is synchronous by design for max speed using (var appender = connection.CreateAppender(TableName)) { foreach (var config in batch) { var row = appender.CreateRow(); - - // Precise mapping of struct fields + row.AppendValue(config.BaseQuant); row.AppendValue(config.Embeddings); row.AppendValue(config.LmHead); @@ -118,20 +121,18 @@ MoeRouter TINYINT row.AppendValue(config.FfnDown); row.AppendValue(config.MoeExperts); row.AppendValue(config.MoeRouter); - + row.EndRow(); } - } // Appender.Dispose() commits the batch automatically + } insertedTotal += batch.Count; AnsiConsole.MarkupLine($" [grey]Inserted batch... Total so far:[/] {insertedTotal:N0}"); - - // Clear memory in the batch list as per your previous logic batch.Clear(); } } sw.Stop(); - AnsiConsole.MarkupLine($"[bold green]Rebuild Complete![/] in {sw.Elapsed.TotalSeconds:F2}s"); + AnsiConsole.MarkupLine($"[bold green]DuckDB rebuild complete![/] in {sw.Elapsed.TotalSeconds:F2}s"); } } \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 2984a11..4ef8ef8 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1,37 +1,47 @@ -using Spectre.Console; -using System.Collections.Concurrent; using System.Diagnostics; using System.Runtime.InteropServices; using MagicQuant.Helpers; using MQ.DB; +using MQ.DB.Data; using MQ.DB.Models; +using Microsoft.EntityFrameworkCore; +using Spectre.Console; namespace MagicQuant.Services; +public enum SampleProcessState +{ + Completed = 1, + Skipped = 2, + Failed = 3 +} + +public sealed class SampleProcessingSummary +{ + public int Requested { get; set; } + public int Completed { get; set; } + public int Skipped { get; set; } + public int Failed { get; set; } +} + public class QuantizationService { private readonly BenchmarkService _benchmarker; private readonly string _ggufDir; private readonly string _benchDir; - private readonly PythonManager _python; - - // Threading Control - // We limit CPU-heavy quantization jobs to (TotalThreads / 8) to avoid choking the system - // while leaving room for the GPU-heavy Perplexity tasks. private readonly SemaphoreSlim _cpuQuantLock; + private readonly int _maxConcurrentQuantizations; - // The Queue - private readonly ConcurrentQueue> _jobQueue = new(); - private bool _isQueueRunning = false; + private static readonly SemaphoreSlim BaseModelLock = new(1, 1); public QuantizationService(BenchmarkService benchmarker) { _benchmarker = benchmarker; _python = _benchmarker._pyManager; - // Setup Directories based on Cache (assumed populated by Evolution command) - if (Cache.MagicQuantDirectory == null) - throw new Exception("MagicQuant Directory not set. Run initialization first."); + + if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + throw new Exception("Cache.ModelMagicQuantDirectory not set. Evolution must set this before quantization starts."); _ggufDir = Path.Combine(Cache.ModelMagicQuantDirectory, "GGUF"); _benchDir = Path.Combine(Cache.ModelMagicQuantDirectory, "Benchmarks"); @@ -39,240 +49,381 @@ public QuantizationService(BenchmarkService benchmarker) Directory.CreateDirectory(_ggufDir); Directory.CreateDirectory(_benchDir); - // Limit concurrent quantizations. - // Example: 32 threads -> 4 concurrent quants (leaving threads for PPL) - int maxConcurrent = Math.Max(1, (Cache.SysInfo?.ThreadCount ?? 4) / 8); - _cpuQuantLock = new SemaphoreSlim(maxConcurrent, maxConcurrent); + int threadCount = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; + _maxConcurrentQuantizations = Math.Max(1, threadCount / 8); + _cpuQuantLock = new SemaphoreSlim(_maxConcurrentQuantizations, _maxConcurrentQuantizations); } - // ---------------------------------------------------------------- - // 1. High-Level Entry Point: Build & Benchmark - // ---------------------------------------------------------------- + public async Task ProcessHybridBatchAsync( + IReadOnlyCollection quants, + CancellationToken ct = default) + { + if (quants == null) + throw new ArgumentNullException(nameof(quants)); + + int completed = 0; + int skipped = 0; + int failed = 0; + + // Warm the base model once so workers don't all race into conversion. + await EnsureBaseModelAsync(false); - public void QueueJob(HybridQuant quant) + await Parallel.ForEachAsync( + quants, + new ParallelOptions + { + MaxDegreeOfParallelism = _maxConcurrentQuantizations, + CancellationToken = ct + }, + async (quant, token) => + { + try + { + var state = await ProcessHybridQuantAsync(quant, token); + + switch (state) + { + case SampleProcessState.Completed: + Interlocked.Increment(ref completed); + break; + case SampleProcessState.Skipped: + Interlocked.Increment(ref skipped); + break; + default: + Interlocked.Increment(ref failed); + break; + } + } + catch (Exception ex) + { + Interlocked.Increment(ref failed); + AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(GenerateHybridName(quant))}"); + AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); + } + }); + + return new SampleProcessingSummary + { + Requested = quants.Count, + Completed = completed, + Skipped = skipped, + Failed = failed + }; + } + + public async Task ProcessHybridQuantAsync( + HybridQuant quant, + CancellationToken ct = default) +{ + string modelName = GenerateHybridName(quant); + string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); + string modelBenchDir = Path.Combine(_benchDir, modelName); + string baseLogitsDir = GetBaseLogitsDirectory(); + + // 1. Fast path: if the benchmark artifacts on disk are already valid, reuse them + // and sync SQLite without rebuilding the sample GGUF. + if (await _benchmarker.TryReuseExistingBenchmarksAsync( + quantConfig: quant, + modelPath: quantPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + domainsOverride: new[] { "general" })) { - _jobQueue.Enqueue(async () => await ProcessHybridQuantAsync(quant)); - StartQueueProcessor(); + AnsiConsole.MarkupLine($"[grey]Reused existing benchmark artifacts:[/] {Markup.Escape(modelName)}"); + + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + + return SampleProcessState.Skipped; } - private void StartQueueProcessor() + // 2. DB truth still matters too + if (await BenchmarkExistsAsync(quant, ct)) { - if (_isQueueRunning) return; - _isQueueRunning = true; + AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); - // Fire and forget the processor loop - Task.Run(async () => - { - while (_jobQueue.TryDequeue(out var job)) - { - await job(); - } + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - _isQueueRunning = false; - }); + return SampleProcessState.Skipped; } - private async Task ProcessHybridQuantAsync(HybridQuant quant) + try { + string basePath = await EnsureBaseModelAsync(); + + await _cpuQuantLock.WaitAsync(ct); try { - // 1. Ensure Base Model Exists (Dynamic BF16/F16/F32) - string basePath = await EnsureBaseModelAsync(); - - // 2. Determine Output Name & Path - string modelName = GenerateHybridName(quant); - string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); - - // 3. Quantize (CPU Bound - Parallel) - await _cpuQuantLock.WaitAsync(); - try - { - if (!File.Exists(quantPath)) - { - AnsiConsole.MarkupLine($"[cyan]Building Hybrid Model:[/] {modelName}"); - await RunLlamaQuantizeAsync(basePath, quantPath, quant); - } - } - finally + if (!File.Exists(quantPath)) { - _cpuQuantLock.Release(); + AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); + await RunLlamaQuantizeAsync(basePath, quantPath, quant); } + } + finally + { + _cpuQuantLock.Release(); + } - // 4. Benchmark (Mixed CPU/GPU/Exclusive) - string modelBenchDir = Path.Combine(_benchDir, modelName); - string metricsPath = Path.Combine(modelBenchDir, "bench_metrics.json"); + // Re-check after build in case another worker finished the DB sync while we were quantizing + if (await BenchmarkExistsAsync(quant, ct)) + { + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - if (!File.Exists(metricsPath)) - { - AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {modelName}"); + return SampleProcessState.Skipped; + } - await _benchmarker.RunAllBenchmarksAsync( - quantConfig: quant, - modelPath: quantPath, - benchDir: modelBenchDir, - saveLogits: false - ); + AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); - // Cleanup: Delete GGUF after benchmark (unless protected base) - if (File.Exists(quantPath) && !IsProtectedModel(modelName)) - { - AnsiConsole.MarkupLine($"[grey]Deleting temp model: {modelName}[/]"); - File.Delete(quantPath); - } - } - } - catch (Exception ex) + await _benchmarker.RunAllBenchmarksAsync( + quantConfig: quant, + modelPath: quantPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: false, + domainsOverride: new[] { "general" }); + + return SampleProcessState.Completed; + } + finally + { + if (!IsProtectedModel(modelName)) { - AnsiConsole.WriteException(ex); + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); } } +} - private bool IsProtectedModel(string name) + private string GetBaseLogitsDirectory() { - // Don't delete the BF16/F16/F32 base files - return name.EndsWith("BF16") || name.EndsWith("F16") || name.EndsWith("F32"); + string typeStr = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + return Path.Combine(_benchDir, typeStr, "logits"); } - // ---------------------------------------------------------------- - // 2. Base Model Generation (Dynamic BF16 / F16 / F32) - // ---------------------------------------------------------------- - public async Task EnsureBaseModelAsync(bool deleteProcess = false) + private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationToken ct) { - // Resolve model name - string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); + + var lookup = BuildTensorLookup(quant); + + await using var db = new MagicQuantContext(); + + var model = await db.AiModelHashes + .AsNoTracking() + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (model == null) + return false; + + var comboId = await db.TensorCombos + .AsNoTracking() + .Where(x => + x.BaseQuant == lookup.BaseQuant && + x.Embeddings == lookup.Embeddings && + x.LmHead == lookup.LmHead && + x.AttnQ == lookup.AttnQ && + x.AttnKV == lookup.AttnKV && + x.AttnOutput == lookup.AttnOutput && + x.FfnUpGate == lookup.FfnUpGate && + x.FfnDown == lookup.FfnDown && + x.MoeExperts == lookup.MoeExperts && + x.MoeRouter == lookup.MoeRouter) + .Select(x => (uint?)x.Id) + .FirstOrDefaultAsync(ct); + + if (!comboId.HasValue) + return false; + + return await db.AiBenchmarks + .AsNoTracking() + .AnyAsync(x => x.AiModelHashId == model.Id && x.TensorComboId == comboId.Value, ct); + } - // Determine torch type (default BF16) - var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; - string typeStr = torchType.ToString(); // BF16, F16, F32 + private static ( + byte BaseQuant, + byte Embeddings, + byte LmHead, + byte AttnQ, + byte AttnKV, + byte AttnOutput, + byte FfnUpGate, + byte FfnDown, + byte MoeExperts, + byte MoeRouter) BuildTensorLookup(HybridQuant quant) + { + byte embeddings = 0; + byte lmHead = 0; + byte attnQ = 0; + byte attnKV = 0; + byte attnOutput = 0; + byte ffnUpGate = 0; + byte ffnDown = 0; + byte moeExperts = 0; + byte moeRouter = 0; + + if (quant.Tensors != null) + { + foreach (var tensor in quant.Tensors) + { + if (tensor?.TGroup == null) + continue; - // Output paths - string fileName = $"{modelName}-{typeStr}.gguf"; - string outputPath = Path.Combine(_ggufDir, fileName); - string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); + if (tensor.TGroup.UniqueId == TReg.Embeddings.UniqueId) embeddings = tensor.TensorType.UniqueId; + else if (tensor.TGroup.UniqueId == TReg.LmHead.UniqueId) lmHead = tensor.TensorType.UniqueId; + else if (tensor.TGroup.UniqueId == TReg.AttnQ.UniqueId) attnQ = tensor.TensorType.UniqueId; + else if (tensor.TGroup.UniqueId == TReg.AttnKV.UniqueId) attnKV = tensor.TensorType.UniqueId; + else if (tensor.TGroup.UniqueId == TReg.AttnOutput.UniqueId) attnOutput = tensor.TensorType.UniqueId; + else if (tensor.TGroup.UniqueId == TReg.FfnUpGate.UniqueId) ffnUpGate = tensor.TensorType.UniqueId; + else if (tensor.TGroup.UniqueId == TReg.FfnDown.UniqueId) ffnDown = tensor.TensorType.UniqueId; + else if (tensor.TGroup.UniqueId == TReg.MoeExperts.UniqueId) moeExperts = tensor.TensorType.UniqueId; + else if (tensor.TGroup.UniqueId == TReg.MoeRouter.UniqueId) moeRouter = tensor.TensorType.UniqueId; + } + } - if (deleteProcess) - { - if (string.IsNullOrWhiteSpace(fileName)) - throw new ArgumentException("fileName is null or empty.", nameof(fileName)); + return ( + quant.BaseQuant.UniqueId, + embeddings, + lmHead, + attnQ, + attnKV, + attnOutput, + ffnUpGate, + ffnDown, + moeExperts, + moeRouter + ); + } - if (!Directory.Exists(_ggufDir)) - throw new DirectoryNotFoundException($"Directory does not exist: {_ggufDir}"); + private bool IsProtectedModel(string name) + { + return name.EndsWith("BF16", StringComparison.OrdinalIgnoreCase) || + name.EndsWith("F16", StringComparison.OrdinalIgnoreCase) || + name.EndsWith("F32", StringComparison.OrdinalIgnoreCase); + } - var normalizedFileName = Path.GetFileName(fileName); - var successFileName = normalizedFileName + ".success.json"; - var successFilePath = Path.Combine(_ggufDir, successFileName); + public async Task EnsureBaseModelAsync(bool deleteProcess = false) + { + await BaseModelLock.WaitAsync(); + try + { + string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; + var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; + string typeStr = torchType.ToString(); - // Only immune if the success file exists - bool isImmune = File.Exists(successFilePath); + string fileName = $"{modelName}-{typeStr}.gguf"; + string outputPath = Path.Combine(_ggufDir, fileName); + string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); - foreach (var filePath in Directory.EnumerateFiles(_ggufDir, "*.gguf", SearchOption.TopDirectoryOnly)) + if (deleteProcess) { - var currentFileName = Path.GetFileName(filePath); + if (!Directory.Exists(_ggufDir)) + Directory.CreateDirectory(_ggufDir); + + var normalizedFileName = Path.GetFileName(fileName); + var successFileName = normalizedFileName + ".success.json"; + var successFilePath = Path.Combine(_ggufDir, successFileName); + bool isImmune = File.Exists(successFilePath); - if (isImmune && - string.Equals(currentFileName, normalizedFileName, StringComparison.OrdinalIgnoreCase)) + foreach (var filePath in Directory.EnumerateFiles(_ggufDir, "*.gguf", SearchOption.TopDirectoryOnly)) { - // This GGUF earned its right to live - continue; - } + var currentFileName = Path.GetFileName(filePath); + + if (isImmune && + string.Equals(currentFileName, normalizedFileName, StringComparison.OrdinalIgnoreCase)) + { + continue; + } - // HARD DELETE — Windows & Linux - File.Delete(filePath); + await HardDeleteHelper.DeleteFileIfExistsAsync(filePath); + } } - } - // Already converted? - if (!File.Exists(outputPath) || !File.Exists(successFile)) - { - // ---- Conversion ---- - AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeStr}...[/]"); + if (!File.Exists(outputPath) || !File.Exists(successFile)) + { + AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeStr}...[/]"); - if (File.Exists(outputPath)) - File.Delete(outputPath); + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - string convertScript = Cache.ConvertScript - ?? throw new Exception("ConvertScript path missing in Cache"); + string convertScript = Cache.ConvertScript + ?? throw new Exception("ConvertScript path missing in Cache"); - string outTypeArg = typeStr.ToLowerInvariant(); // bf16 / f16 / f32 + string outTypeArg = typeStr.ToLowerInvariant(); - string arguments = - $"\"{convertScript}\" \"{Cache.ModelDirectory}\" " + - $"--outtype {outTypeArg} " + - $"--outfile \"{outputPath}\""; + string arguments = + $"\"{convertScript}\" \"{Cache.ModelDirectory}\" " + + $"--outtype {outTypeArg} " + + $"--outfile \"{outputPath}\""; - string python = _python.GetPythonExecutable(); + string python = _python.GetPythonExecutable(); - var psi = new ProcessStartInfo - { - FileName = python, - Arguments = arguments, - WorkingDirectory = Cache.LlamaRoot, - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true - }; + var psi = new ProcessStartInfo + { + FileName = python, + Arguments = arguments, + WorkingDirectory = Cache.LlamaRoot, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var process = Process.Start(psi) + ?? throw new InvalidOperationException("Failed to start conversion process"); + + process.OutputDataReceived += (_, e) => + { + if (!string.IsNullOrWhiteSpace(e.Data)) + AnsiConsole.WriteLine(e.Data); + }; - using var process = Process.Start(psi) - ?? throw new InvalidOperationException("Failed to start conversion process"); + process.ErrorDataReceived += (_, e) => + { + if (!string.IsNullOrWhiteSpace(e.Data)) + AnsiConsole.WriteLine(e.Data); + }; - // UNTRUSTED OUTPUT → WriteLine ONLY - process.OutputDataReceived += (_, e) => - { - if (!string.IsNullOrWhiteSpace(e.Data)) - AnsiConsole.WriteLine(e.Data); - }; + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); - process.ErrorDataReceived += (_, e) => - { - if (!string.IsNullOrWhiteSpace(e.Data)) - AnsiConsole.WriteLine(e.Data); - }; + await process.WaitForExitAsync(); - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); + if (process.ExitCode != 0) + throw new Exception($"{typeStr} conversion failed"); - await process.WaitForExitAsync(); + await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); + } - if (process.ExitCode != 0) - throw new Exception($"{typeStr} conversion failed"); + string benchPath = Path.Combine(_benchDir, typeStr); + string logitsDir = Path.Combine(benchPath, "logits"); - // Write success marker - await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); - } + AnsiConsole.MarkupLine($"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]"); - // ---- Benchmark Base Model ---- - string benchPath = Path.Combine(_benchDir, typeStr); - string logitsDir = Path.Combine(benchPath, "logits"); + var baseModelQuant = new HybridQuant + { + BaseQuant = BaselineQuants.GetBF16Quant(), + Tensors = new List() + }; - AnsiConsole.MarkupLine( - $"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]" - ); + await _benchmarker.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, + modelPath: outputPath, + benchDir: benchPath, + klLogitsDir: logitsDir, + saveLogits: true, + domainsOverride: new[] { "general", "code", "math" } + ); - // Create the HybridQuant representation for the Base Model - // This matches the "TensorWeightScheme.BF16_F16" BaseQuant, with NO other tensors (NULL) - var baseModelQuant = new HybridQuant + return outputPath; + } + finally { - BaseQuant = BaselineQuants.All.First(b => b.UniqueId == TensorWeightScheme.BF16_F16.UniqueId), - Tensors = new List() // Empty list = all other groups are 0/NULL - }; - - await _benchmarker.RunAllBenchmarksAsync( - quantConfig: baseModelQuant, // <--- PASSED HERE - modelPath: outputPath, - benchDir: benchPath, - klLogitsDir: logitsDir, - saveLogits: true - ); - - return outputPath; + BaseModelLock.Release(); + } } - - // ---------------------------------------------------------------- - // 3. Hybrid Quantization Execution - // ---------------------------------------------------------------- - public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) { var args = new List(capacity: 64); @@ -281,9 +432,14 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb { foreach (var hybrid in quant.Tensors) { - if (hybrid?.TGroup == null) continue; + if (hybrid?.TGroup == null) + continue; + + // NULL is an internal sentinel only. + // It means "do not emit an override for this tensor group". + if (hybrid.TensorType.UniqueId == TensorWeightScheme.NULL.UniqueId) + continue; - // Resolve scheme dynamically (handles BF16/F16 shared ID) string schemeName = ResolveSchemeName(hybrid.TensorType); foreach (var tensorPattern in hybrid.TGroup.Tensors) @@ -300,9 +456,9 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb string arguments = string.Join(" ", args); - string bin = Cache.LlamaBin + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? "/llama-quantize.exe" - : "/llama-quantize"); + string bin = Path.Combine( + Cache.LlamaBin!, + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "llama-quantize.exe" : "llama-quantize"); var psi = new ProcessStartInfo { @@ -315,7 +471,20 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb }; using var p = Process.Start(psi); - if (p == null) throw new InvalidOperationException($"Failed to start process: {bin}"); + if (p == null) + throw new InvalidOperationException($"Failed to start process: {bin}"); + + p.OutputDataReceived += (_, e) => + { + if (!string.IsNullOrWhiteSpace(e.Data)) + AnsiConsole.WriteLine(e.Data); + }; + + p.ErrorDataReceived += (_, e) => + { + if (!string.IsNullOrWhiteSpace(e.Data)) + AnsiConsole.WriteLine(e.Data); + }; p.BeginOutputReadLine(); p.BeginErrorReadLine(); @@ -329,6 +498,7 @@ private static string ResolveBaseName(BaselineQuants b) { if (b.Names.IsDefaultOrEmpty) throw new InvalidOperationException($"BaselineQuants '{b.UniqueId}' has no Names."); + return b.Names[0]; } @@ -337,26 +507,20 @@ private static string ResolveSchemeName(TensorWeightScheme s) if (s.Names.IsDefaultOrEmpty) throw new InvalidOperationException($"TensorWeightScheme '{s.UniqueId}' has no Names."); - // Special case: BF16_F16 shares UniqueId and has two names ["BF16","F16"]. if (s.UniqueId == TensorWeightScheme.BF16_F16.UniqueId && s.Names.Length >= 2) { - // Dynamic check against Cache if (Cache.TorchType == Cache.MainTorchType.F16) - { return "F16"; - } - // Default to BF16 for BF16 or F32 types (safer modern default) + if (Cache.TorchType == Cache.MainTorchType.F32) + return "F32"; + return "BF16"; } return s.Names[0]; } - // ---------------------------------------------------------------- - // 4. Naming Scheme Logic (E-H-Q-K-O...) - // ---------------------------------------------------------------- - public string GenerateHybridName(HybridQuant quant) { string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; @@ -394,14 +558,14 @@ public string GenerateHybridName(HybridQuant quant) private int GetOrder(char c) { - // E, H, Q, K, O, U, D, X, R return "EHQKOUDXR".IndexOf(c); } private string SimplifyQuant(string quant) { - // Optional: Simplify quantization names for the filename - // BF16 -> B16, Q4_K_M -> Q4KM - return quant.Replace("_", "").Replace("BF16", "B16").Replace("F16", "F16"); + return quant.Replace("_", "") + .Replace("BF16", "B16") + .Replace("F16", "F16") + .Replace("F32", "F32"); } } \ No newline at end of file From 9644ed35c8dc0f1b77035a29b547a3fbd3aa030b Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 13 Apr 2026 15:13:19 -0400 Subject: [PATCH 043/258] Starting to get this thing kinda working but TPS tracking has errors with big drops atm --- MQ.DB/Models/HybridQuant.cs | 99 +- MagicQuant/Commands/Evolution.cs | 31 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 96 +- MagicQuant/Services/BenchmarkService.cs | 1088 +++++++++++++++---- MagicQuant/Services/QuantizationService.cs | 747 ++++++++++--- 5 files changed, 1559 insertions(+), 502 deletions(-) diff --git a/MQ.DB/Models/HybridQuant.cs b/MQ.DB/Models/HybridQuant.cs index 37fd812..116938d 100644 --- a/MQ.DB/Models/HybridQuant.cs +++ b/MQ.DB/Models/HybridQuant.cs @@ -3,71 +3,54 @@ namespace MQ.DB.Models; public class HybridQuant { public BaselineQuants BaseQuant { get; set; } = default!; - public List Tensors { get; set; } = new List(); + public List Tensors { get; set; } = new(); + public HybridQuant() { } - - // Converting constructor: TensorConfig -> HybridQuant + public HybridQuant(TensorConfig c) { - // If you don’t like LINQ here, swap to dictionary/array maps. - BaseQuant = BaselineQuants.All.First(b => b.UniqueId == c.BaseQuant); - - Tensors.Add(new HybridTensor() - { - TGroup = TReg.Embeddings, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.Embeddings) - }); - - Tensors.Add(new HybridTensor() - { - TGroup = TReg.LmHead, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.LmHead) - }); - - Tensors.Add(new HybridTensor() - { - TGroup = TReg.AttnQ, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.AttnQ) - }); - - Tensors.Add(new HybridTensor() - { - TGroup = TReg.AttnKV, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.AttnKV) - }); - - Tensors.Add(new HybridTensor() - { - TGroup = TReg.AttnOutput, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.AttnOutput) - }); - - Tensors.Add(new HybridTensor() - { - TGroup = TReg.FfnUpGate, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.FfnUpGate) - }); - - Tensors.Add(new HybridTensor() - { - TGroup = TReg.FfnDown, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.FfnDown) - }); - - Tensors.Add(new HybridTensor() - { - TGroup = TReg.MoeExperts, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.MoeExperts) - }); - - Tensors.Add(new HybridTensor() + BaseQuant = BaselineQuants.All.First(b => b.UniqueId == c.BaseQuant); + + AddIfNotNull(TReg.Embeddings, c.Embeddings); + AddIfNotNull(TReg.LmHead, c.LmHead); + AddIfNotNull(TReg.AttnQ, c.AttnQ); + AddIfNotNull(TReg.AttnKV, c.AttnKV); + AddIfNotNull(TReg.AttnOutput, c.AttnOutput); + AddIfNotNull(TReg.FfnUpGate, c.FfnUpGate); + AddIfNotNull(TReg.FfnDown, c.FfnDown); + AddIfNotNull(TReg.MoeExperts, c.MoeExperts); + AddIfNotNull(TReg.MoeRouter, c.MoeRouter); + } + + private void AddIfNotNull(TensorGroup group, byte schemeId) + { + if (schemeId == TensorWeightScheme.NULL.UniqueId) + return; + + var scheme = TensorWeightScheme.All.First(g => g.UniqueId == schemeId); + + Tensors.Add(new HybridTensor { - TGroup = TReg.MoeRouter, - TensorType = TensorWeightScheme.All.First(g => g.UniqueId == c.MoeRouter) + TGroup = group, + TensorType = scheme }); } - // Conversion operator: TensorConfig -> HybridQuant + public HybridQuant Clone() + { + return new HybridQuant + { + BaseQuant = BaseQuant, + Tensors = Tensors + .Select(t => new HybridTensor + { + TGroup = t.TGroup, + TensorType = t.TensorType + }) + .ToList() + }; + } + public static explicit operator HybridQuant(TensorConfig c) => new HybridQuant(c); } diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 3dff2c8..fef2b43 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -89,8 +89,35 @@ public async Task Run(List args) var bService = new BenchmarkService(pyManager); var qService = new QuantizationService(bService); - var bf16ModelGgufPath = await qService.EnsureBaseModelAsync(true); - + var bf16ModelGgufPath = await qService.EnsureBaseModelFileAsync(true); + var q8ModelGgufPath = await qService.EnsurePureQ8ModelAsync(); + + await bService.EnsureExecutionPlanAsync(q8ModelGgufPath); + await bService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); + + var baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + var baseBenchDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks", baseTypeName); + var baseLogitsDir = Path.Combine(baseBenchDir, "logits"); + + var baseModelQuant = new HybridQuant + { + BaseQuant = BaselineQuants.GetBF16Quant(), + Tensors = new List() + }; + + await bService.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, + modelPath: bf16ModelGgufPath, + benchDir: baseBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: true, + domainsOverride: new[] { "general", "code", "math" }); + +// Optional: capture a quick micro-benchmark for the pure Q8 baseline too. +// var q8BenchDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks", "Q8_0"); +// var q8Quant = new HybridQuant { BaseQuant = BaselineQuants.Q8_0, Tensors = new List() }; +// await bService.RunAllBenchmarksAsync(q8Quant, q8ModelGgufPath, q8BenchDir, saveLogits: false, domainsOverride: new[] { "general" }); + var compatibilityService = new ModelCompatibilityService(pyManager); await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index cb83692..9fd228b 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -8,82 +8,110 @@ namespace MagicQuant.Helpers; public static class TensorConfigGenerator { - public static List GenerateRequiredDataSampleCombos(List? MissingTensorGroup = null) + public static List GenerateRequiredDataSampleCombos(List? missingTensorGroups = null) { - if (MissingTensorGroup != null && !MissingTensorGroup.Any()) - MissingTensorGroup = null; + if (missingTensorGroups != null && !missingTensorGroups.Any()) + missingTensorGroups = null; var allowedBaselines = BaselineQuants.All.Where(x => x.BaseConversionBase != null).ToList(); var hybridQuants = new List(); - // Fast lookup for missing groups - var missingIds = MissingTensorGroup?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + var missingIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + var existingGroups = TReg.All.Where(g => !missingIds.Contains(g.UniqueId)).ToList(); - // --------------------------------------------------------- // 1. PURE BASELINE CONTROLS - // --------------------------------------------------------- - // These must be TRUE baseline exports with NO tensor overrides at all. - // Otherwise you are not testing the baseline quant, you're testing a weird hybrid. int baseTestsRequired = 0; foreach (var baseline in allowedBaselines) { baseTestsRequired++; + if (baseline.BaseConversionBase == null) + throw new InvalidOperationException( + $"Baseline {string.Join("/", baseline.Names)} is missing BaseConversionBase."); + hybridQuants.Add(new HybridQuant { BaseQuant = baseline, - Tensors = new List() + Tensors = baseline.BaseConversionBase.Tensors + .Where(t => t.TGroup != null && !missingIds.Contains(t.TGroup.UniqueId)) + .Select(t => new HybridTensor + { + TGroup = t.TGroup, + TensorType = t.TensorType + }) + .ToList() }); } AnsiConsole.MarkupLine($"[bold green]Required pure baseline hybrid tests:[/] {baseTestsRequired:N0}"); // --------------------------------------------------------- - // 2. ISOLATION SAMPLES (BF16/F16/F32 source base, one tensor altered) - // --------------------------------------------------------- +// 2. ISOLATION SAMPLES +// --------------------------------------------------------- +// These should use a REAL blanket base quant and then override +// one target group away from that base so llama-quantize actually +// performs hybrid quantization. + var tensorWeights = TensorWeightScheme.All .Where(x => x != TensorWeightScheme.NULL && x != TensorWeightScheme.BF16_F16) .ToList(); int isolatedSamplesRequired = 0; - var isolationBase = BaselineQuants.GetBF16Quant(); +// Pick the real baseline families we want to probe. +// You can expand this later if desired. + var isolationBaselines = BaselineQuants.All + .Where(x => x.BaseConversionBase != null) + .ToList(); - foreach (var weight in tensorWeights) + foreach (var baseline in isolationBaselines) { - var validTargets = TReg.All.Where(x => !weight.BannedGroups.Contains(x)).ToList(); + // Map the baseline name to its matching tensor scheme. + // Example: IQ4_XS baseline => IQ4_XS tensor scheme everywhere by default. + var baselineScheme = TensorWeightScheme.All.FirstOrDefault(s => + s.Names.Any(n => baseline.Names.Contains(n, StringComparer.OrdinalIgnoreCase))); - if (missingIds.Count > 0) - { - validTargets.RemoveAll(x => missingIds.Contains(x.UniqueId)); - } + if (baselineScheme == null) + continue; - foreach (var group in validTargets) + foreach (var weight in tensorWeights) { - isolatedSamplesRequired++; + var validTargets = TReg.All + .Where(x => !missingIds.Contains(x.UniqueId)) + .Where(x => !weight.BannedGroups.Contains(x)) + .ToList(); - var tensors = TReg.All.Select(g => new HybridTensor + foreach (var group in validTargets) { - TGroup = g, - TensorType = missingIds.Contains(g.UniqueId) - ? TensorWeightScheme.NULL - : TensorWeightScheme.BF16_F16 - }).ToList(); + isolatedSamplesRequired++; - var foundQuant = tensors.First(x => x.TGroup == group); - foundQuant.TensorType = weight; + var tensors = TReg.All + .Where(g => !missingIds.Contains(g.UniqueId)) + .Select(g => new HybridTensor + { + TGroup = g, + TensorType = baselineScheme + }) + .ToList(); - hybridQuants.Add(new HybridQuant - { - BaseQuant = isolationBase, - Tensors = tensors - }); + var foundQuant = tensors.First(x => x.TGroup.UniqueId == group.UniqueId); + foundQuant.TensorType = weight; + + hybridQuants.Add(new HybridQuant + { + BaseQuant = baseline, + Tensors = tensors + }); + } } } AnsiConsole.MarkupLine($"[bold green]Isolated Samples Required:[/] {isolatedSamplesRequired:N0}"); AnsiConsole.MarkupLine($"[bold green]Total Samples Required:[/] {hybridQuants.Count:N0}"); + AnsiConsole.MarkupLine($"[bold green]Isolated Samples Required:[/] {isolatedSamplesRequired:N0}"); + AnsiConsole.MarkupLine($"[bold green]Total Samples Required:[/] {hybridQuants.Count:N0}"); + return hybridQuants; } diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index a8e5533..431962d 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -1,14 +1,16 @@ using System.Diagnostics; +using System.Globalization; using System.Runtime.InteropServices; using System.Text.Json; using System.Text.RegularExpressions; using MagicQuant.Helpers; +using MQ.DB; using MQ.DB.Data; using MQ.DB.Models; -using MQ.DB.Models.DbModels; // Required for BenchmarkCategory and TensorCombo +using MQ.DB.Models.DbModels; using Microsoft.EntityFrameworkCore; -using MQ.DB; using Spectre.Console; +using System.Text.Json; namespace MagicQuant.Services; @@ -20,6 +22,463 @@ public class BenchmarkService private static readonly string[] BaseDomains = { "general", "code", "math" }; private static readonly string[] SampleDomains = { "general" }; + private static readonly string[] OomMarkers = + { + "out of memory", + "cudamalloc failed", + "unable to allocate cuda", + "try reducing --n-gpu-layers", + "cannot fulfill margin", + "failed to fit params", + "cuda error" + }; + + private static readonly int[] NglCandidates = { 35, 30, 24, 20, 16, 12, 8, 4 }; + + // ---------------------------------------------------------------- + // Static execution-plan state + // ---------------------------------------------------------------- + + private static readonly SemaphoreSlim PlanInitLock = new(1, 1); + private static readonly object SlotSync = new(); + + private static BenchmarkExecutionPlan? _currentPlan; + private static Queue _availableSlots = new(); + private static SemaphoreSlim? _slotSemaphore; + + // ---------------------------------------------------------------- + // Construction + // ---------------------------------------------------------------- + + public BenchmarkService(PythonManager pyManager) + { + _bins = new LlamaBinaries(Cache.LlamaRoot); + _bins.Validate(); + _pyManager = pyManager; + } + + // ---------------------------------------------------------------- + // Execution-plan discovery + // ---------------------------------------------------------------- + + public async Task EnsureExecutionPlanAsync( + string q8ModelPath, + int discoveryTokenTarget = 8192, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(q8ModelPath)) + throw new ArgumentException("Q8 model path was null or empty.", nameof(q8ModelPath)); + + string normalizedPath = Path.GetFullPath(q8ModelPath); + + if (_currentPlan != null && + string.Equals(_currentPlan.PlanModelPath, normalizedPath, StringComparison.OrdinalIgnoreCase)) + { + return; + } + + await PlanInitLock.WaitAsync(ct); + try + { + if (_currentPlan != null && + string.Equals(_currentPlan.PlanModelPath, normalizedPath, StringComparison.OrdinalIgnoreCase)) + { + return; + } + + var plan = await BuildExecutionPlanAsync(normalizedPath, discoveryTokenTarget, ct); + + lock (SlotSync) + { + _currentPlan = plan; + _availableSlots = new Queue(plan.Slots); + _slotSemaphore = new SemaphoreSlim(plan.Slots.Count, plan.Slots.Count); + } + + AnsiConsole.Write(new Rule("[yellow]Benchmark Execution Plan[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[green]Static ngl:[/] [cyan]{plan.StaticNgl}[/]"); + AnsiConsole.MarkupLine($"[green]Uses GPU:[/] [cyan]{plan.UsesGpu}[/]"); + AnsiConsole.MarkupLine($"[green]GPU group size:[/] [cyan]{plan.GroupSize}[/]"); + AnsiConsole.MarkupLine($"[green]Parallel benchmark slots:[/] [cyan]{plan.Slots.Count}[/]"); + + foreach (var slot in plan.Slots) + { + AnsiConsole.MarkupLine($" [grey]Slot {slot.SlotId}:[/] {Markup.Escape(slot.DisplayName)}"); + } + } + finally + { + PlanInitLock.Release(); + } + } + + public async Task ClampStaticNglWithBaseModelAsync( + string baseModelPath, + int discoveryTokenTarget = 8192, + CancellationToken ct = default) +{ + if (string.IsNullOrWhiteSpace(baseModelPath)) + throw new ArgumentException("Base model path was null or empty.", nameof(baseModelPath)); + + if (_currentPlan == null) + throw new InvalidOperationException( + "Benchmark execution plan has not been initialized. Call EnsureExecutionPlanAsync() first."); + + if (!_currentPlan.UsesGpu) + return; + + await PlanInitLock.WaitAsync(ct); + try + { + if (_currentPlan == null || !_currentPlan.UsesGpu) + return; + + var slot = _currentPlan.Slots[0]; + + string probeRoot = Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe_base"); + Directory.CreateDirectory(probeRoot); + + string probeCorpusDir = Path.Combine(probeRoot, "_ppl_corpora"); + Directory.CreateDirectory(probeCorpusDir); + + string corpusPath = Path.Combine(probeCorpusDir, "ppl_corpus_general.txt"); + await PreparePplCorpusAsync("general", corpusPath, discoveryTokenTarget); + + int startingNgl = _currentPlan.StaticNgl; + int? chosen = null; + + AnsiConsole.Write(new Rule("[yellow]Clamping Static ngl With Base Model[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[grey]Base model:[/] {Markup.Escape(baseModelPath)}"); + AnsiConsole.MarkupLine($"[grey]Starting from Q8-discovered ngl:[/] [cyan]{startingNgl}[/]"); + + foreach (int ngl in NglCandidates.Where(n => n <= startingNgl).OrderByDescending(n => n)) + { + ct.ThrowIfCancellationRequested(); + + AnsiConsole.MarkupLine($"[grey]Base clamp probe:[/] [cyan]ngl={ngl}[/]"); + + bool benchOk = await ProbeLlamaBenchAtFixedNglAsync(baseModelPath, slot, ngl, probeRoot); + if (!benchOk) + { + AnsiConsole.MarkupLine($"[grey] llama-bench failed at ngl={ngl}[/]"); + continue; + } + + bool pplOk = await ProbePerplexityAtFixedNglAsync(baseModelPath, slot, ngl, corpusPath, probeRoot); + if (!pplOk) + { + AnsiConsole.MarkupLine($"[grey] perplexity failed at ngl={ngl}[/]"); + continue; + } + + chosen = ngl; + break; + } + + if (!chosen.HasValue) + { + throw new InvalidOperationException( + $"Could not clamp a stable benchmark ngl for the base model '{baseModelPath}' " + + $"within the discovered Q8 topology."); + } + + if (chosen.Value != _currentPlan.StaticNgl) + { + var updated = new BenchmarkExecutionPlan( + planModelPath: _currentPlan.PlanModelPath, + staticNgl: chosen.Value, + usesGpu: _currentPlan.UsesGpu, + groupSize: _currentPlan.GroupSize, + slots: _currentPlan.Slots); + + lock (SlotSync) + { + _currentPlan = updated; + _availableSlots = new Queue(updated.Slots); + _slotSemaphore = new SemaphoreSlim(updated.Slots.Count, updated.Slots.Count); + } + } + + AnsiConsole.MarkupLine($"[green]Base-model clamped static ngl:[/] [cyan]{chosen.Value}[/]"); + } + finally + { + PlanInitLock.Release(); + } +} + + private async Task BuildExecutionPlanAsync( + string q8ModelPath, + int discoveryTokenTarget, + CancellationToken ct) + { + int gpuCount = Cache.SysInfo?.GpuInfo? + .Count(x => x.GpuVendor != GpuVendor.Cpu && x.GpuVendor != GpuVendor.Unknown) ?? 0; + + if (gpuCount <= 0) + { + return BenchmarkExecutionPlan.CreateCpuPlan(q8ModelPath); + } + + var allGpuIndices = Enumerable.Range(0, gpuCount).ToArray(); + var allGpuSlot = new BenchmarkSlot(0, allGpuIndices); + + string probeRoot = Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe"); + Directory.CreateDirectory(probeRoot); + + int? targetNgl = await ProbeHighestStableNglAsync( + q8ModelPath, + allGpuSlot, + probeRoot, + discoveryTokenTarget, + ct); + + if (!targetNgl.HasValue || targetNgl.Value <= 0) + { + AnsiConsole.MarkupLine( + "[yellow]Q8 discovery could not establish a stable GPU ngl. Falling back to a single CPU slot.[/]"); + return BenchmarkExecutionPlan.CreateCpuPlan(q8ModelPath); + } + + foreach (var groupSize in GetCandidateGroupSizes(gpuCount)) + { + var groups = BuildContiguousGroups(allGpuIndices, groupSize); + var slots = new List(); + + bool allGroupsPass = true; + for (int i = 0; i < groups.Count; i++) + { + var slot = new BenchmarkSlot(i, groups[i]); + + bool ok = await ValidateSlotForFixedPlanAsync( + q8ModelPath, + slot, + targetNgl.Value, + probeRoot, + discoveryTokenTarget, + ct); + + if (!ok) + { + allGroupsPass = false; + break; + } + + slots.Add(slot); + } + + if (allGroupsPass && slots.Count > 0) + { + return new BenchmarkExecutionPlan( + planModelPath: q8ModelPath, + staticNgl: targetNgl.Value, + usesGpu: true, + groupSize: groupSize, + slots: slots); + } + } + + // This should not normally happen because "all GPUs as one slot" already passed, + // but keeping a hard fallback is still worthwhile. + return new BenchmarkExecutionPlan( + planModelPath: q8ModelPath, + staticNgl: targetNgl.Value, + usesGpu: true, + groupSize: gpuCount, + slots: new List { allGpuSlot }); + } + + private async Task ProbeHighestStableNglAsync( + string modelPath, + BenchmarkSlot slot, + string probeRoot, + int tokenTarget, + CancellationToken ct) + { + string probeCorpusDir = Path.Combine(probeRoot, "_ppl_corpora"); + Directory.CreateDirectory(probeCorpusDir); + + string corpusPath = Path.Combine(probeCorpusDir, "ppl_corpus_general.txt"); + await PreparePplCorpusAsync("general", corpusPath, tokenTarget); + + foreach (int ngl in NglCandidates) + { + ct.ThrowIfCancellationRequested(); + + AnsiConsole.MarkupLine( + $"[grey]Plan probe:[/] testing [cyan]{Markup.Escape(slot.DisplayName)}[/] at [cyan]ngl={ngl}[/]"); + + bool benchOk = await ProbeLlamaBenchAtFixedNglAsync(modelPath, slot, ngl, probeRoot); + if (!benchOk) + { + AnsiConsole.MarkupLine($"[grey] llama-bench failed at ngl={ngl}[/]"); + continue; + } + + bool pplOk = await ProbePerplexityAtFixedNglAsync(modelPath, slot, ngl, corpusPath, probeRoot); + if (!pplOk) + { + AnsiConsole.MarkupLine($"[grey] perplexity failed at ngl={ngl}[/]"); + continue; + } + + AnsiConsole.MarkupLine($"[green] stable ngl discovered:[/] [cyan]{ngl}[/]"); + return ngl; + } + + return null; + } + + private async Task ValidateSlotForFixedPlanAsync( + string modelPath, + BenchmarkSlot slot, + int fixedNgl, + string probeRoot, + int tokenTarget, + CancellationToken ct) + { + string probeCorpusDir = Path.Combine(probeRoot, "_ppl_corpora"); + Directory.CreateDirectory(probeCorpusDir); + + string corpusPath = Path.Combine(probeCorpusDir, "ppl_corpus_general.txt"); + await PreparePplCorpusAsync("general", corpusPath, tokenTarget); + + bool benchOk = await ProbeLlamaBenchAtFixedNglAsync(modelPath, slot, fixedNgl, probeRoot); + if (!benchOk) + return false; + + bool pplOk = await ProbePerplexityAtFixedNglAsync(modelPath, slot, fixedNgl, corpusPath, probeRoot); + return pplOk; + } + + private async Task ProbeLlamaBenchAtFixedNglAsync( + string modelPath, + BenchmarkSlot slot, + int fixedNgl, + string probeRoot) + { + string logFile = Path.Combine( + probeRoot, + $"probe_llamabench_slot{slot.SlotId}_g{slot.DeviceCount}_ngl{fixedNgl}.md"); + + string cmd = slot.UsesGpu + ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl} -o md" + : $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; + + var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); + + if (!result.Success) + return false; + + try + { + var parsed = ParseLlamaBench(logFile); + return parsed.Tps.HasValue && parsed.Tps.Value > 0; + } + catch + { + return false; + } + } + + private async Task ProbePerplexityAtFixedNglAsync( + string modelPath, + BenchmarkSlot slot, + int fixedNgl, + string corpusPath, + string probeRoot) + { + string logFile = Path.Combine( + probeRoot, + $"probe_ppl_general_slot{slot.SlotId}_g{slot.DeviceCount}_ngl{fixedNgl}.log"); + + string cmd = slot.UsesGpu + ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl} -t 4 -c 2048 --file \"{corpusPath}\"" + : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\""; + + var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); + + if (!result.Success) + return false; + + try + { + var parsed = ParsePerplexity(logFile, allowMissingKld: true); + return parsed.Ppl > 0; + } + catch + { + return false; + } + } + + private static List GetCandidateGroupSizes(int gpuCount) + { + var divisors = new List(); + + for (int i = 1; i <= gpuCount; i++) + { + if (gpuCount % i == 0) + divisors.Add(i); + } + + return divisors; + } + + private static List BuildContiguousGroups(int[] gpuIndices, int groupSize) + { + if (gpuIndices.Length % groupSize != 0) + { + throw new InvalidOperationException( + $"GPU count {gpuIndices.Length} was not divisible by group size {groupSize}."); + } + + var groups = new List(); + for (int i = 0; i < gpuIndices.Length; i += groupSize) + { + groups.Add(gpuIndices.Skip(i).Take(groupSize).ToArray()); + } + + return groups; + } + + private static async Task AcquireBenchmarkSlotAsync(CancellationToken ct = default) + { + if (_currentPlan == null) + throw new InvalidOperationException( + "Benchmark execution plan has not been initialized. Call EnsureExecutionPlanAsync() first."); + + if (_slotSemaphore == null) + throw new InvalidOperationException("Benchmark slot semaphore is not initialized."); + + await _slotSemaphore.WaitAsync(ct); + + lock (SlotSync) + { + if (_availableSlots.Count == 0) + { + _slotSemaphore.Release(); + throw new InvalidOperationException("No benchmark slots were available after semaphore acquisition."); + } + + var slot = _availableSlots.Dequeue(); + return new BenchmarkSlotLease(slot); + } + } + + private static void ReturnBenchmarkSlot(BenchmarkSlot slot) + { + lock (SlotSync) + { + _availableSlots.Enqueue(slot); + _slotSemaphore!.Release(); + } + } + + // ---------------------------------------------------------------- + // Public entry points + // ---------------------------------------------------------------- + private static bool IsNativeBaseModel(HybridQuant quantConfig) { return quantConfig.BaseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId; @@ -51,9 +510,14 @@ private static ulong TryGetModelSize(string modelPath) return File.Exists(modelPath) ? (ulong)new FileInfo(modelPath).Length : 0UL; } - private static bool IsPositiveKld(double? kld) + private const double KldEpsilon = 1e-8; + + private static bool HasMeaningfulKld(double? kld) { - return kld.HasValue && kld.Value > 0d; + return kld.HasValue && + !double.IsNaN(kld.Value) && + !double.IsInfinity(kld.Value) && + Math.Abs(kld.Value) > KldEpsilon; } public async Task TryReuseExistingBenchmarksAsync( @@ -75,7 +539,12 @@ public async Task TryReuseExistingBenchmarksAsync( return false; } - reused.ModelSizeBytes ??= TryGetModelSize(modelPath); + if (!reused.ModelSizeBytes.HasValue || reused.ModelSizeBytes.Value == 0) + { + var actualSize = TryGetModelSize(modelPath); + if (actualSize > 0) + reused.ModelSizeBytes = actualSize; + } using var db = new MagicQuantContext(); @@ -98,32 +567,6 @@ public async Task TryReuseExistingBenchmarksAsync( return true; } - // Constants - private static readonly string[] OomMarkers = - { - "out of memory", "cudaMalloc failed", "unable to allocate cuda", "try reducing --n-gpu-layers" - }; - - private static readonly int[] NglCandidates = { 35, 30, 24, 20, 16, 12, 8, 4, 0 }; - - // ---------------------------------------------------------------- - // Concurrency Controls - // ---------------------------------------------------------------- - - public static readonly SemaphoreSlim ExclusiveBenchLock = new(1, 1); - public static readonly SemaphoreSlim VramLock = new(1, 1); - - public BenchmarkService(PythonManager pyManager) - { - _bins = new LlamaBinaries(Cache.LlamaRoot); - _bins.Validate(); - _pyManager = pyManager; - } - - // ---------------------------------------------------------------- - // Public Entry Point - // ---------------------------------------------------------------- - public async Task RunAllBenchmarksAsync( HybridQuant quantConfig, string modelPath, @@ -159,19 +602,32 @@ public async Task RunAllBenchmarksAsync( .AsNoTracking() .FirstOrDefaultAsync(b => b.AiModelHashId == aiModelHash.Id && b.TensorComboId == tensorCombo.Id); - // 1. If DB already has everything required, trust DB first and don't rerun. + // 1. DB truth first if (existingBench != null && HasRequiredCategories(existingBench, requestedDomains, requireKld)) { + if (existingBench.SizeBytes == 0) + { + var repairedSize = TryGetModelSize(modelPath); + if (repairedSize > 0) + { + existingBench.SizeBytes = repairedSize; + db.AiBenchmarks.Update(existingBench); + await db.SaveChangesAsync(); + } + } + if (TryReadExistingBenchmarkArtifacts(benchDir, requestedDomains, requireKld, out var diskResult)) { - diskResult.ModelSizeBytes ??= existingBench.SizeBytes; + if (!diskResult.ModelSizeBytes.HasValue || diskResult.ModelSizeBytes.Value == 0) + diskResult.ModelSizeBytes = existingBench.SizeBytes > 0 ? existingBench.SizeBytes : TryGetModelSize(modelPath); + return diskResult; } return BuildResultFromDb(existingBench, requestedDomains); } - // 2. If disk already has reusable artifacts, sync DB and return. + // 2. Disk truth second if (TryReadExistingBenchmarkArtifacts(benchDir, requestedDomains, requireKld, out var reused)) { reused.ModelSizeBytes ??= TryGetModelSize(modelPath); @@ -180,7 +636,21 @@ public async Task RunAllBenchmarksAsync( return reused; } - // 3. Otherwise run only the pieces that are actually missing/invalid. + // 3. Real execution: fixed slot + fixed ngl + if (_currentPlan == null) + { + throw new InvalidOperationException( + "No benchmark execution plan has been discovered yet. " + + "You must call EnsureExecutionPlanAsync() with the pure Q8 model first."); + } + + await using var slotLease = await AcquireBenchmarkSlotAsync(); + var slot = slotLease.Slot; + + int effectiveNgl = slot.UsesGpu + ? _currentPlan.StaticNgl + : 0; + var result = new BenchmarkResult { ModelSizeBytes = TryGetModelSize(modelPath) @@ -193,16 +663,9 @@ public async Task RunAllBenchmarksAsync( } else { - await ExclusiveBenchLock.WaitAsync(); - try - { - AnsiConsole.MarkupLine("[yellow]Running Llama-Bench (Exclusive Mode)...[/]"); - result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, startNgl); - } - finally - { - ExclusiveBenchLock.Release(); - } + AnsiConsole.MarkupLine( + $"[yellow]Running Llama-Bench[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); + result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, effectiveNgl, slot); } var corporaRoot = Path.Combine(Path.GetDirectoryName(benchDir)!, "_ppl_corpora"); @@ -227,32 +690,27 @@ public async Task RunAllBenchmarksAsync( string corpusPath = Path.Combine(corporaRoot, $"ppl_corpus_{domain}.txt"); await PreparePplCorpusAsync(domain, corpusPath, tokenTarget); - await VramLock.WaitAsync(); - try - { - AnsiConsole.MarkupLine($"[yellow]Running Perplexity ({domain})...[/]"); - var metrics = await RunPplBenchmarkAsync( - modelPath: modelPath, - benchDir: benchDir, - domain: domain, - corpusPath: corpusPath, - startNgl: startNgl, - klLogitsDir: klLogitsDir, - saveLogits: saveLogits); - - if (requireKld && !IsPositiveKld(metrics.Kld)) - { - throw new InvalidOperationException( - $"Non-base benchmark produced invalid KLD for domain '{domain}'. " + - $"KLD must exist and be > 0. Parsed value: {(metrics.Kld.HasValue ? metrics.Kld.Value.ToString() : "null")}"); - } + AnsiConsole.MarkupLine( + $"[yellow]Running Perplexity ({Markup.Escape(domain)})[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); - result.Perplexity[domain] = metrics; - } - finally + var metrics = await RunPplBenchmarkAsync( + modelPath: modelPath, + benchDir: benchDir, + domain: domain, + corpusPath: corpusPath, + fixedNgl: effectiveNgl, + slot: slot, + klLogitsDir: klLogitsDir, + saveLogits: saveLogits); + + if (requireKld && !HasMeaningfulKld(metrics.Kld)) { - VramLock.Release(); + throw new InvalidOperationException( + $"Non-base benchmark produced invalid KLD for domain '{domain}'. " + + $"KLD must exist and be > 0. Parsed value: {(metrics.Kld.HasValue ? metrics.Kld.Value.ToString(CultureInfo.InvariantCulture) : "null")}"); } + + result.Perplexity[domain] = metrics; } await WriteMetricsJsonAsync(benchDir, result); @@ -262,13 +720,11 @@ public async Task RunAllBenchmarksAsync( } // ---------------------------------------------------------------- - // Database Helpers (Fixed for Immutability) + // Database helpers // ---------------------------------------------------------------- private async Task GetOrCreateTensorComboAsync(MagicQuantContext db, HybridQuant quant) { - // 1. Extract values into local variables. - // Default to 0 (NULL scheme) if not present in the mutable list. byte baseQuant = quant.BaseQuant.UniqueId; byte embeddings = 0; @@ -285,7 +741,6 @@ private async Task GetOrCreateTensorComboAsync(MagicQuantContext db { foreach (var t in quant.Tensors) { - // Compare using UniqueId to be safe if (t.TGroup.UniqueId == TReg.Embeddings.UniqueId) embeddings = t.TensorType.UniqueId; else if (t.TGroup.UniqueId == TReg.LmHead.UniqueId) lmHead = t.TensorType.UniqueId; else if (t.TGroup.UniqueId == TReg.AttnQ.UniqueId) attnQ = t.TensorType.UniqueId; @@ -298,7 +753,6 @@ private async Task GetOrCreateTensorComboAsync(MagicQuantContext db } } - // 2. Create the Immutable Config using the Constructor var c = new TensorConfig( baseQuant, embeddings, @@ -309,11 +763,8 @@ private async Task GetOrCreateTensorComboAsync(MagicQuantContext db ffnUpGate, ffnDown, moeExperts, - moeRouter - ); + moeRouter); - // 3. Check DB using the extracted values - // (We query by the raw bytes because the DB entity fields are readonly and might not map directly in Expression trees depending on EF version) var existing = await db.TensorCombos.FirstOrDefaultAsync(x => x.BaseQuant == c.BaseQuant && x.Embeddings == c.Embeddings && @@ -324,12 +775,11 @@ private async Task GetOrCreateTensorComboAsync(MagicQuantContext db x.FfnUpGate == c.FfnUpGate && x.FfnDown == c.FfnDown && x.MoeExperts == c.MoeExperts && - x.MoeRouter == c.MoeRouter - ); + x.MoeRouter == c.MoeRouter); - if (existing != null) return existing; + if (existing != null) + return existing; - // 4. Create New TensorCombo using the Constructor (which accepts TensorConfig) var newCombo = new TensorCombo(c); db.TensorCombos.Add(newCombo); await db.SaveChangesAsync(); @@ -383,7 +833,16 @@ private async Task SaveBenchmarkToDbAsync( bench.TokensPerSecond = res.LlamaBench?.Tps ?? 0; bench.Ngl = (byte)(res.LlamaBench?.Ngl ?? 0); - bench.SizeBytes = sizeBytes; + + if (sizeBytes > 0) + { + bench.SizeBytes = sizeBytes; + } + else if (bench.SizeBytes == 0) + { + // only leave it zero if we truly have no better information + bench.SizeBytes = 0; + } await db.SaveChangesAsync(); @@ -415,10 +874,10 @@ private async Task SaveBenchmarkToDbAsync( } else { - if (!IsPositiveKld(m.Kld)) + if (!HasMeaningfulKld(m.Kld)) { throw new InvalidOperationException( - $"Refusing to save non-base benchmark with invalid KLD. Domain='{domain}', KLD='{m.Kld?.ToString() ?? "null"}'"); + $"Refusing to save non-base benchmark with invalid KLD. Domain='{domain}', KLD='{m.Kld?.ToString(CultureInfo.InvariantCulture) ?? "null"}'"); } kld = m.Kld!.Value; @@ -461,6 +920,10 @@ private async Task SaveBenchmarkToDbAsync( } } + // ---------------------------------------------------------------- + // Artifact reuse helpers + // ---------------------------------------------------------------- + private async Task WriteMetricsJsonAsync(string benchDir, BenchmarkResult result) { Directory.CreateDirectory(benchDir); @@ -493,7 +956,7 @@ private bool TryReadExistingBenchmarkArtifacts( } catch { - // fall through and try rebuilding from individual logs + // fall through } } @@ -541,7 +1004,7 @@ private bool IsReusableBenchmarkResult( if (ppl.Ppl <= 0 || ppl.PplError < 0) return false; - if (requireKld && !IsPositiveKld(ppl.Kld)) + if (requireKld && !HasMeaningfulKld(ppl.Kld)) return false; } @@ -566,7 +1029,7 @@ private bool TryReadExistingLlamaBenchLog(string logPath, out LlamaBenchMetrics } catch { - // ignore and return false + // ignore } return false; @@ -592,7 +1055,7 @@ private bool TryReadExistingPplLog( if (parsed.Ppl <= 0) return false; - if (requirePositiveKld && !IsPositiveKld(parsed.Kld)) + if (requirePositiveKld && !HasMeaningfulKld(parsed.Kld)) return false; metrics = parsed; @@ -633,7 +1096,7 @@ private static bool HasRequiredCategories( return false; } - return bench.TokensPerSecond > 0; + return bench.TokensPerSecond > 0 && bench.SizeBytes > 0; } private BenchmarkResult BuildResultFromDb( @@ -674,87 +1137,50 @@ private BenchmarkResult BuildResultFromDb( } // ---------------------------------------------------------------- - // 1. Llama-Bench Logic (Unchanged) + // Real benchmark execution (fixed slot + fixed ngl) // ---------------------------------------------------------------- - private async Task RunLlamaBenchAsync(string modelPath, string benchDir, int? startNgl) + private async Task RunLlamaBenchAsync( + string modelPath, + string benchDir, + int fixedNgl, + BenchmarkSlot slot) { string logFile = Path.Combine(benchDir, "llamabench.md"); - var candidates = startNgl.HasValue - ? NglCandidates.Where(n => n <= startNgl.Value).ToList() - : NglCandidates.ToList(); - - string BuildCmd(int ngl) => - $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {ngl} -o md"; - - int? finalNgl = await RunWithRetryAsync(BuildCmd, logFile, candidates, "llama-bench"); - - if (finalNgl == null) - { - AnsiConsole.MarkupLine("[red]GPU Failed. Fallback to CPU backend...[/]"); - string cpuCmd = $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; - await RunShellCommandAsync(cpuCmd, logFile); - } - - return ParseLlamaBench(logFile); - } - - private LlamaBenchMetrics ParseLlamaBench(string logPath) - { - var metrics = new LlamaBenchMetrics { LogPath = GetRelativePath(logPath) }; - if (!File.Exists(logPath)) return metrics; - - var lines = File.ReadAllLines(logPath); - int headerIdx = -1; - for (int i = 0; i < lines.Length; i++) - { - if (lines[i].Contains("|") && lines[i].Contains("backend")) - { - headerIdx = i; - break; - } - } - if (headerIdx == -1 || lines.Length <= headerIdx + 2) return metrics; + string cmd = slot.UsesGpu + ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl} -o md" + : $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; - var headers = lines[headerIdx].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(h => h.Trim()).ToList(); - var dataRow = lines[headerIdx + 2].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(d => d.Trim()) - .ToList(); - - if (headers.Count != dataRow.Count) return metrics; - var row = headers.Zip(dataRow, (h, d) => new { Header = h, Data = d }).ToDictionary(x => x.Header, x => x.Data); - - string tpsStr = row.ContainsKey("t/s") ? row["t/s"] : (row.ContainsKey("tps") ? row["tps"] : "0"); - var match = Regex.Match(tpsStr, @"([0-9.]+)"); + await RunFixedCommandWithRetryAsync( + label: "llama-bench", + cmd: cmd, + logFile: logFile, + slot: slot, + attempts: 2, + requirePplMarker: false); - if (match.Success && double.TryParse(match.Groups[1].Value, out double tps)) + var parsed = ParseLlamaBench(logFile); + if (!parsed.Tps.HasValue || parsed.Tps.Value <= 0) { - metrics.Tps = tps; - metrics.Backend = row.ContainsKey("backend") ? row["backend"] : "unknown"; - metrics.Test = row.ContainsKey("test") ? row["test"] : "unknown"; - if (row.ContainsKey("ngl") && int.TryParse(row["ngl"], out int ngl)) metrics.Ngl = ngl; + throw new InvalidOperationException( + $"llama-bench completed but no valid TPS could be parsed from {logFile}"); } - return metrics; + return parsed; } - // ---------------------------------------------------------------- - // 2. Perplexity Logic (Unchanged) - // ---------------------------------------------------------------- - private async Task RunPplBenchmarkAsync( string modelPath, string benchDir, string domain, string corpusPath, - int? startNgl, + int fixedNgl, + BenchmarkSlot slot, string? klLogitsDir, bool saveLogits) { string logFile = Path.Combine(benchDir, $"perplexity_{domain}.log"); - var candidates = startNgl.HasValue - ? NglCandidates.Where(n => n <= startNgl.Value).ToList() - : NglCandidates.ToList(); string kldArgs = ""; bool expectKld = false; @@ -765,35 +1191,185 @@ private async Task RunPplBenchmarkAsync( if (saveLogits) { - // Base/native model path: save logits only, do not expect KLD yet. kldArgs = $"--kl-divergence-base \"{logitsFile}\""; expectKld = false; } else if (File.Exists(logitsFile)) { - // Sample path: compare against the already saved base logits. kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; expectKld = true; } } - string BuildCmd(int ngl) => - $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {ngl} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}"; + string cmd = slot.UsesGpu + ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}" + : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}"; - await RunWithRetryAsync(BuildCmd, logFile, candidates, $"perplexity-{domain}"); + await RunFixedCommandWithRetryAsync( + label: $"perplexity-{domain}", + cmd: cmd, + logFile: logFile, + slot: slot, + attempts: 2, + requirePplMarker: true); bool allowMissingKld = !expectKld; var parsed = ParsePerplexity(logFile, allowMissingKld); - if (expectKld && !IsPositiveKld(parsed.Kld)) + if (expectKld && !HasMeaningfulKld(parsed.Kld)) { throw new InvalidOperationException( - $"Expected a real KLD for domain '{domain}', but parsed '{parsed.Kld?.ToString() ?? "null"}' from {logFile}"); + $"Expected a real KLD for domain '{domain}', but parsed '{parsed.Kld?.ToString(CultureInfo.InvariantCulture) ?? "null"}' from {logFile}"); } return parsed; } + private async Task RunFixedCommandWithRetryAsync( + string label, + string cmd, + string logFile, + BenchmarkSlot slot, + int attempts, + bool requirePplMarker) + { + CommandRunResult? last = null; + + for (int attempt = 1; attempt <= attempts; attempt++) + { + last = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); + + string logContent = !string.IsNullOrWhiteSpace(last.LogOutput) + ? last.LogOutput + : (File.Exists(logFile) ? File.ReadAllText(logFile) : string.Empty); + + bool success = last.Success && logContent.Length >= 50; + + if (success && requirePplMarker) + { + success = LooksLikeSuccessfulPerplexityRun(logFile, logContent); + } + + if (success) + return; + + bool retryable = LooksLikeRetryableGpuFailure(logContent); + + if (attempt < attempts && retryable) + { + AnsiConsole.MarkupLine( + $"[yellow]Transient benchmark failure detected on slot {slot.SlotId} ({Markup.Escape(slot.DisplayName)}). Retrying same fixed plan...[/]"); + await Task.Delay(1500); + continue; + } + + throw new InvalidOperationException( + $"{label} failed on fixed benchmark slot {slot.SlotId} ({Markup.Escape(slot.DisplayName)}).\n" + + $"Command: {cmd}\n\nLog Output:\n{logContent}"); + } + + throw new InvalidOperationException( + $"{label} failed after {attempts} attempts on slot {slot.SlotId} ({Markup.Escape(slot.DisplayName)}).\n" + + $"{last?.LogOutput}"); + } + + private bool LooksLikeSuccessfulPerplexityRun(string logFile, string logContent) + { + if (string.IsNullOrWhiteSpace(logContent) || logContent.Length < 50) + return false; + + try + { + var parsed = ParsePerplexity(logFile, allowMissingKld: true); + return parsed.Ppl > 0; + } + catch + { + return false; + } + } + + + private static bool LooksLikeRetryableGpuFailure(string logContent) + { + if (string.IsNullOrWhiteSpace(logContent)) + return false; + + if (OomMarkers.Any(m => logContent.Contains(m, StringComparison.OrdinalIgnoreCase))) + return true; + + if (logContent.Contains("failed to load model", StringComparison.OrdinalIgnoreCase)) + return true; + + if (logContent.Contains("error:", StringComparison.OrdinalIgnoreCase)) + return true; + + return false; + } + + // ---------------------------------------------------------------- + // Parsers + // ---------------------------------------------------------------- + + private LlamaBenchMetrics ParseLlamaBench(string logPath) + { + var metrics = new LlamaBenchMetrics { LogPath = GetRelativePath(logPath) }; + if (!File.Exists(logPath)) + return metrics; + + var lines = File.ReadAllLines(logPath); + int headerIdx = -1; + for (int i = 0; i < lines.Length; i++) + { + if (lines[i].Contains("|") && lines[i].Contains("backend")) + { + headerIdx = i; + break; + } + } + + if (headerIdx == -1 || lines.Length <= headerIdx + 2) + return metrics; + + var headers = lines[headerIdx] + .Split('|', StringSplitOptions.RemoveEmptyEntries) + .Select(h => h.Trim()) + .ToList(); + + var dataRow = lines[headerIdx + 2] + .Split('|', StringSplitOptions.RemoveEmptyEntries) + .Select(d => d.Trim()) + .ToList(); + + if (headers.Count != dataRow.Count) + return metrics; + + var row = headers + .Zip(dataRow, (h, d) => new { Header = h, Data = d }) + .ToDictionary(x => x.Header, x => x.Data, StringComparer.OrdinalIgnoreCase); + + string tpsStr = row.ContainsKey("t/s") + ? row["t/s"] + : (row.ContainsKey("tps") ? row["tps"] : "0"); + + var match = Regex.Match(tpsStr, @"([0-9.]+)"); + if (match.Success && + double.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out double tps)) + { + metrics.Tps = tps; + metrics.Backend = row.ContainsKey("backend") ? row["backend"] : "unknown"; + metrics.Test = row.ContainsKey("test") ? row["test"] : "unknown"; + + if (row.ContainsKey("ngl") && + int.TryParse(row["ngl"], NumberStyles.Any, CultureInfo.InvariantCulture, out int ngl)) + { + metrics.Ngl = ngl; + } + } + + return metrics; + } + private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) { var metrics = new PplMetrics { LogPath = GetRelativePath(logPath) }; @@ -815,8 +1391,8 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) $"Failed to parse PPL from log: {logPath}\n\nLast log content:\n{cleanText}"); } - metrics.Ppl = double.Parse(pplMatch.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture); - metrics.PplError = double.Parse(pplMatch.Groups[2].Value, System.Globalization.CultureInfo.InvariantCulture); + metrics.Ppl = double.Parse(pplMatch.Groups[1].Value, CultureInfo.InvariantCulture); + metrics.PplError = double.Parse(pplMatch.Groups[2].Value, CultureInfo.InvariantCulture); var kldMatch = Regex.Match( cleanText, @@ -825,7 +1401,7 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) if (kldMatch.Success) { - metrics.Kld = double.Parse(kldMatch.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture); + metrics.Kld = double.Parse(kldMatch.Groups[1].Value, CultureInfo.InvariantCulture); } else if (!allowMissingKld) { @@ -837,12 +1413,13 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) } // ---------------------------------------------------------------- - // 3. Corpus Preparation (Unchanged) + // Corpus preparation // ---------------------------------------------------------------- private async Task PreparePplCorpusAsync(string domain, string outPath, int tokenTarget) { - if (File.Exists(outPath) && new FileInfo(outPath).Length > 0) return; + if (File.Exists(outPath) && new FileInfo(outPath).Length > 0) + return; AnsiConsole.MarkupLine($"[grey]Generating corpus for domain: {domain}[/]"); @@ -866,18 +1443,22 @@ def get_sources(d): try: d = load_dataset(ds, conf) if conf else load_dataset(ds) for text in d[split][field]: - if not text or not isinstance(text, str): continue + if not text or not isinstance(text, str): + continue chunk = text.strip() + '\n' parts.append(chunk) total += len(chunk) - if total >= max_chars: break + if total >= max_chars: + break except Exception as e: print(f'Error loading {{ds}}: {{e}}') - if total >= max_chars: break + if total >= max_chars: + break with open(out_path, 'w', encoding='utf-8') as f: f.write(''.join(parts)) "; + string scriptPath = Path.Combine(Path.GetDirectoryName(outPath)!, $"gen_{domain}.py"); await File.WriteAllTextAsync(scriptPath, pyScript); @@ -887,87 +1468,18 @@ with open(out_path, 'w', encoding='utf-8') as f: : $"\"{scriptPath}\""; string runner = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : pythonExe; - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) args = scriptPath; + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + args = scriptPath; await _pyManager.RunPipInstallAsync("datasets"); await RunShellCommandAsync(runner + " " + args, null); - if (File.Exists(scriptPath)) File.Delete(scriptPath); + if (File.Exists(scriptPath)) + File.Delete(scriptPath); } // ---------------------------------------------------------------- - // 4. Retry Logic - // ---------------------------------------------------------------- - private async Task RunWithRetryAsync( - Func cmdBuilder, - string logPath, - List candidates, - string label) - { - string? lastFailureDetails = null; - - foreach (int ngl in candidates) - { - string cmd = cmdBuilder(ngl); - AnsiConsole.WriteLine($"[*] {label}: trying -ngl {ngl}"); - - var result = await RunShellCommandAsync(cmd, logPath); - - string logContent = !string.IsNullOrWhiteSpace(result.LogOutput) - ? result.LogOutput - : (File.Exists(logPath) ? File.ReadAllText(logPath) : string.Empty); - - bool looksLikeOom = OomMarkers.Any(m => - logContent.Contains(m, StringComparison.OrdinalIgnoreCase)); - - bool looksLikeLoadFailure = - logContent.Contains("failed to load model", StringComparison.OrdinalIgnoreCase) || - logContent.Contains("error:", StringComparison.OrdinalIgnoreCase); - - if (!result.Success) - { - lastFailureDetails = - $"ExitCode={result.ExitCode}, ngl={ngl}\nCommand: {cmd}\n\nLog Output:\n{logContent}"; - - if (looksLikeOom || looksLikeLoadFailure) - { - AnsiConsole.WriteLine($"[WARN] {label}: failed at -ngl {ngl}, retrying lower setting..."); - continue; - } - - // Unknown non-zero exit: still retry lower ngl first, - // because many llama.cpp GPU/load issues recover that way. - AnsiConsole.WriteLine($"[WARN] {label}: non-zero exit at -ngl {ngl}, retrying lower setting..."); - continue; - } - - if (logContent.Length < 50) - { - lastFailureDetails = - $"Log too short at ngl={ngl}\nCommand: {cmd}\n\nLog Output:\n{logContent}"; - AnsiConsole.WriteLine($"[WARN] {label}: Failed at -ngl {ngl} (log too short), trying next..."); - continue; - } - - if (label.StartsWith("perplexity", StringComparison.OrdinalIgnoreCase) && - !Regex.IsMatch(logContent, @"PPL\s*[:=]\s*[-+]?\d*\.?\d+", RegexOptions.IgnoreCase)) - { - lastFailureDetails = - $"No parsable PPL marker found at ngl={ngl}\nCommand: {cmd}\n\nLog Output:\n{logContent}"; - AnsiConsole.WriteLine($"[WARN] {label}: No parsable PPL marker found at -ngl {ngl}, trying next..."); - continue; - } - - AnsiConsole.WriteLine($"[OK] {label}: succeeded with -ngl {ngl}"); - return ngl; - } - - throw new InvalidOperationException( - $"{label}: all -ngl candidates failed.\n\nLast failure details:\n{lastFailureDetails}"); - } - - // ---------------------------------------------------------------- - // 5. System Utilities + // Process / shell utilities // ---------------------------------------------------------------- private sealed class CommandRunResult @@ -977,7 +1489,10 @@ private sealed class CommandRunResult public string LogOutput { get; init; } = string.Empty; } - private async Task RunShellCommandAsync(string cmd, string? logPath) + private async Task RunShellCommandAsync( + string cmd, + string? logPath, + IReadOnlyDictionary? extraEnv = null) { var startInfo = new ProcessStartInfo { @@ -989,6 +1504,14 @@ private async Task RunShellCommandAsync(string cmd, string? lo CreateNoWindow = true }; + if (extraEnv != null) + { + foreach (var kvp in extraEnv) + { + startInfo.Environment[kvp.Key] = kvp.Value; + } + } + using var process = new Process { StartInfo = startInfo }; FileStream? fs = null; StreamWriter? sw = null; @@ -1041,4 +1564,91 @@ private string GetRelativePath(string fullPath) { return Path.GetFileName(fullPath); } + + // ---------------------------------------------------------------- + // Internal plan / slot types + // ---------------------------------------------------------------- + + private sealed class BenchmarkExecutionPlan + { + public string PlanModelPath { get; } + public int StaticNgl { get; } + public bool UsesGpu { get; } + public int GroupSize { get; } + public IReadOnlyList Slots { get; } + + public BenchmarkExecutionPlan( + string planModelPath, + int staticNgl, + bool usesGpu, + int groupSize, + IReadOnlyList slots) + { + PlanModelPath = planModelPath; + StaticNgl = staticNgl; + UsesGpu = usesGpu; + GroupSize = groupSize; + Slots = slots; + } + + public static BenchmarkExecutionPlan CreateCpuPlan(string q8ModelPath) + { + return new BenchmarkExecutionPlan( + planModelPath: q8ModelPath, + staticNgl: 0, + usesGpu: false, + groupSize: 0, + slots: new List { new(0, Array.Empty()) }); + } + } + + private sealed class BenchmarkSlot + { + public int SlotId { get; } + public int[] DeviceIndices { get; } + public bool UsesGpu => DeviceIndices.Length > 0; + public int DeviceCount => DeviceIndices.Length; + + public BenchmarkSlot(int slotId, int[] deviceIndices) + { + SlotId = slotId; + DeviceIndices = deviceIndices; + } + + public string DisplayName => + UsesGpu + ? $"GPU[{string.Join(",", DeviceIndices)}]" + : "CPU"; + + public IReadOnlyDictionary? BuildProcessEnv() + { + if (!UsesGpu) + return null; + + string visible = string.Join(",", DeviceIndices); + + return new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["CUDA_VISIBLE_DEVICES"] = visible, + ["HIP_VISIBLE_DEVICES"] = visible, + ["ROCR_VISIBLE_DEVICES"] = visible + }; + } + } + + private sealed class BenchmarkSlotLease : IAsyncDisposable + { + public BenchmarkSlot Slot { get; } + + public BenchmarkSlotLease(BenchmarkSlot slot) + { + Slot = slot; + } + + public ValueTask DisposeAsync() + { + ReturnBenchmarkSlot(Slot); + return ValueTask.CompletedTask; + } + } } \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 4ef8ef8..b3e97ec 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1,5 +1,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; +using System.Text; +using System.Text.Json; using MagicQuant.Helpers; using MQ.DB; using MQ.DB.Data; @@ -37,11 +39,18 @@ public class QuantizationService public QuantizationService(BenchmarkService benchmarker) { - _benchmarker = benchmarker; + _benchmarker = benchmarker ?? throw new ArgumentNullException(nameof(benchmarker)); _python = _benchmarker._pyManager; if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) - throw new Exception("Cache.ModelMagicQuantDirectory not set. Evolution must set this before quantization starts."); + throw new Exception( + "Cache.ModelMagicQuantDirectory not set. Evolution must set this before quantization starts."); + + if (string.IsNullOrWhiteSpace(Cache.ModelDirectory)) + throw new Exception("Cache.ModelDirectory not set. Evolution must set this before quantization starts."); + + if (string.IsNullOrWhiteSpace(Cache.LlamaBin)) + throw new Exception("Cache.LlamaBin not set. Initialization must complete before quantization starts."); _ggufDir = Path.Combine(Cache.ModelMagicQuantDirectory, "GGUF"); _benchDir = Path.Combine(Cache.ModelMagicQuantDirectory, "Benchmarks"); @@ -54,6 +63,10 @@ public QuantizationService(BenchmarkService benchmarker) _cpuQuantLock = new SemaphoreSlim(_maxConcurrentQuantizations, _maxConcurrentQuantizations); } + // ---------------------------------------------------------------- + // Batch processing + // ---------------------------------------------------------------- + public async Task ProcessHybridBatchAsync( IReadOnlyCollection quants, CancellationToken ct = default) @@ -65,8 +78,8 @@ public async Task ProcessHybridBatchAsync( int skipped = 0; int failed = 0; - // Warm the base model once so workers don't all race into conversion. - await EnsureBaseModelAsync(false); + // Warm the base model file once so workers don't all race into conversion. + await EnsureBaseModelFileAsync(false); await Parallel.ForEachAsync( quants, @@ -112,89 +125,92 @@ await Parallel.ForEachAsync( } public async Task ProcessHybridQuantAsync( - HybridQuant quant, - CancellationToken ct = default) -{ - string modelName = GenerateHybridName(quant); - string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); - string modelBenchDir = Path.Combine(_benchDir, modelName); - string baseLogitsDir = GetBaseLogitsDirectory(); - - // 1. Fast path: if the benchmark artifacts on disk are already valid, reuse them - // and sync SQLite without rebuilding the sample GGUF. - if (await _benchmarker.TryReuseExistingBenchmarksAsync( - quantConfig: quant, - modelPath: quantPath, - benchDir: modelBenchDir, - klLogitsDir: baseLogitsDir, - domainsOverride: new[] { "general" })) + HybridQuant quant, + CancellationToken ct = default) { - AnsiConsole.MarkupLine($"[grey]Reused existing benchmark artifacts:[/] {Markup.Escape(modelName)}"); - - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + string modelName = GenerateHybridName(quant); + string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); + string modelBenchDir = Path.Combine(_benchDir, modelName); + string baseLogitsDir = GetBaseLogitsDirectory(); + + // 1. Fast path: valid artifacts already exist on disk and can be synced/reused + if (await _benchmarker.TryReuseExistingBenchmarksAsync( + quantConfig: quant, + modelPath: quantPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + domainsOverride: new[] { "general" })) + { + AnsiConsole.MarkupLine($"[grey]Reused existing benchmark artifacts:[/] {Markup.Escape(modelName)}"); - return SampleProcessState.Skipped; - } + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - // 2. DB truth still matters too - if (await BenchmarkExistsAsync(quant, ct)) - { - AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); + return SampleProcessState.Skipped; + } - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + // 2. DB truth still matters too + if (await BenchmarkExistsAsync(quant, ct)) + { + AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); - return SampleProcessState.Skipped; - } + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - try - { - string basePath = await EnsureBaseModelAsync(); + return SampleProcessState.Skipped; + } - await _cpuQuantLock.WaitAsync(ct); try { - if (!File.Exists(quantPath)) + string basePath = await EnsureBaseModelFileAsync(); + + await _cpuQuantLock.WaitAsync(ct); + try { - AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); - await RunLlamaQuantizeAsync(basePath, quantPath, quant); + if (!File.Exists(quantPath)) + { + AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); + await RunLlamaQuantizeAsync(basePath, quantPath, quant); + } + } + finally + { + _cpuQuantLock.Release(); } - } - finally - { - _cpuQuantLock.Release(); - } - // Re-check after build in case another worker finished the DB sync while we were quantizing - if (await BenchmarkExistsAsync(quant, ct)) - { - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + // Re-check after build in case another worker finished the DB sync while we were quantizing + if (await BenchmarkExistsAsync(quant, ct)) + { + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - return SampleProcessState.Skipped; - } + return SampleProcessState.Skipped; + } - AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); + AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); - await _benchmarker.RunAllBenchmarksAsync( - quantConfig: quant, - modelPath: quantPath, - benchDir: modelBenchDir, - klLogitsDir: baseLogitsDir, - saveLogits: false, - domainsOverride: new[] { "general" }); - - return SampleProcessState.Completed; - } - finally - { - if (!IsProtectedModel(modelName)) + await _benchmarker.RunAllBenchmarksAsync( + quantConfig: quant, + modelPath: quantPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: false, + domainsOverride: new[] { "general" }); + + return SampleProcessState.Completed; + } + finally { - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + if (!IsProtectedModel(modelName)) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + } } } -} + + // ---------------------------------------------------------------- + // Benchmark/logit helpers + // ---------------------------------------------------------------- private string GetBaseLogitsDirectory() { @@ -218,28 +234,37 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok if (model == null) return false; - var comboId = await db.TensorCombos + var bench = await db.AiBenchmarks .AsNoTracking() + .Where(x => x.AiModelHashId == model.Id) + .Join( + db.TensorCombos.AsNoTracking(), + benchmark => benchmark.TensorComboId, + combo => combo.Id, + (benchmark, combo) => new { benchmark, combo }) .Where(x => - x.BaseQuant == lookup.BaseQuant && - x.Embeddings == lookup.Embeddings && - x.LmHead == lookup.LmHead && - x.AttnQ == lookup.AttnQ && - x.AttnKV == lookup.AttnKV && - x.AttnOutput == lookup.AttnOutput && - x.FfnUpGate == lookup.FfnUpGate && - x.FfnDown == lookup.FfnDown && - x.MoeExperts == lookup.MoeExperts && - x.MoeRouter == lookup.MoeRouter) - .Select(x => (uint?)x.Id) + x.combo.BaseQuant == lookup.BaseQuant && + x.combo.Embeddings == lookup.Embeddings && + x.combo.LmHead == lookup.LmHead && + x.combo.AttnQ == lookup.AttnQ && + x.combo.AttnKV == lookup.AttnKV && + x.combo.AttnOutput == lookup.AttnOutput && + x.combo.FfnUpGate == lookup.FfnUpGate && + x.combo.FfnDown == lookup.FfnDown && + x.combo.MoeExperts == lookup.MoeExperts && + x.combo.MoeRouter == lookup.MoeRouter) + .Select(x => x.benchmark.Id) .FirstOrDefaultAsync(ct); - if (!comboId.HasValue) + if (bench == 0) return false; - return await db.AiBenchmarks + // Require at least one category row too, so a half-baked parent row doesn't count as complete. + bool hasCategory = await db.Set() .AsNoTracking() - .AnyAsync(x => x.AiModelHashId == model.Id && x.TensorComboId == comboId.Value, ct); + .AnyAsync(x => x.AiBenchmarkId == bench, ct); + + return hasCategory; } private static ( @@ -301,10 +326,43 @@ private bool IsProtectedModel(string name) { return name.EndsWith("BF16", StringComparison.OrdinalIgnoreCase) || name.EndsWith("F16", StringComparison.OrdinalIgnoreCase) || - name.EndsWith("F32", StringComparison.OrdinalIgnoreCase); + name.EndsWith("F32", StringComparison.OrdinalIgnoreCase) || + name.EndsWith("Q8_0", StringComparison.OrdinalIgnoreCase); } + // ---------------------------------------------------------------- + // Base/native model helpers + // ---------------------------------------------------------------- + public async Task EnsureBaseModelAsync(bool deleteProcess = false) + { + string outputPath = await EnsureBaseModelFileAsync(deleteProcess); + + string typeStr = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + string benchPath = Path.Combine(_benchDir, typeStr); + string logitsDir = Path.Combine(benchPath, "logits"); + + AnsiConsole.MarkupLine($"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]"); + + var baseModelQuant = new HybridQuant + { + BaseQuant = BaselineQuants.GetBF16Quant(), + Tensors = new List() + }; + + await _benchmarker.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, + modelPath: outputPath, + benchDir: benchPath, + klLogitsDir: logitsDir, + saveLogits: true, + domainsOverride: new[] { "general", "code", "math" } + ); + + return outputPath; + } + + public async Task EnsureBaseModelFileAsync(bool deleteProcess = false) { await BaseModelLock.WaitAsync(); try @@ -316,6 +374,7 @@ public async Task EnsureBaseModelAsync(bool deleteProcess = false) string fileName = $"{modelName}-{typeStr}.gguf"; string outputPath = Path.Combine(_ggufDir, fileName); string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); + string convertLogPath = outputPath + ".convert.log"; if (deleteProcess) { @@ -363,59 +422,30 @@ public async Task EnsureBaseModelAsync(bool deleteProcess = false) { FileName = python, Arguments = arguments, - WorkingDirectory = Cache.LlamaRoot, - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true + WorkingDirectory = Cache.LlamaRoot }; - using var process = Process.Start(psi) - ?? throw new InvalidOperationException("Failed to start conversion process"); + var result = await RunLoggedProcessAsync(psi, convertLogPath); - process.OutputDataReceived += (_, e) => + if (result.ExitCode != 0) { - if (!string.IsNullOrWhiteSpace(e.Data)) - AnsiConsole.WriteLine(e.Data); - }; + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - process.ErrorDataReceived += (_, e) => - { - if (!string.IsNullOrWhiteSpace(e.Data)) - AnsiConsole.WriteLine(e.Data); - }; - - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); + throw new Exception( + $"{typeStr} conversion failed. ExitCode={result.ExitCode}. See '{convertLogPath}'."); + } - await process.WaitForExitAsync(); + if (!File.Exists(outputPath) || new FileInfo(outputPath).Length == 0) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - if (process.ExitCode != 0) - throw new Exception($"{typeStr} conversion failed"); + throw new InvalidOperationException( + $"Conversion exited successfully but produced no valid GGUF output: {outputPath}"); + } await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); } - string benchPath = Path.Combine(_benchDir, typeStr); - string logitsDir = Path.Combine(benchPath, "logits"); - - AnsiConsole.MarkupLine($"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]"); - - var baseModelQuant = new HybridQuant - { - BaseQuant = BaselineQuants.GetBF16Quant(), - Tensors = new List() - }; - - await _benchmarker.RunAllBenchmarksAsync( - quantConfig: baseModelQuant, - modelPath: outputPath, - benchDir: benchPath, - klLogitsDir: logitsDir, - saveLogits: true, - domainsOverride: new[] { "general", "code", "math" } - ); - return outputPath; } finally @@ -424,34 +454,85 @@ await _benchmarker.RunAllBenchmarksAsync( } } - public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) + public async Task EnsurePureQ8ModelAsync() { - var args = new List(capacity: 64); + string basePath = await EnsureBaseModelFileAsync(); - if (quant.Tensors is { Count: > 0 }) + var pureQ8 = new HybridQuant { - foreach (var hybrid in quant.Tensors) - { - if (hybrid?.TGroup == null) - continue; - - // NULL is an internal sentinel only. - // It means "do not emit an override for this tensor group". - if (hybrid.TensorType.UniqueId == TensorWeightScheme.NULL.UniqueId) - continue; + BaseQuant = BaselineQuants.Q8_0, + Tensors = new List() + }; - string schemeName = ResolveSchemeName(hybrid.TensorType); + string modelName = GenerateHybridName(pureQ8); + string q8Path = Path.Combine(_ggufDir, $"{modelName}.gguf"); + string successFile = Path.Combine(_ggufDir, $"{Path.GetFileName(q8Path)}.success.json"); - foreach (var tensorPattern in hybrid.TGroup.Tensors) + if (!File.Exists(q8Path) || !File.Exists(successFile)) + { + await _cpuQuantLock.WaitAsync(); + try + { + if (!File.Exists(q8Path)) { - args.Add($"--tensor-type \"{tensorPattern}={schemeName}\""); + AnsiConsole.MarkupLine($"[cyan]Building pure Q8 baseline:[/] {Markup.Escape(modelName)}"); + await RunLlamaQuantizeAsync(basePath, q8Path, pureQ8); + AnsiConsole.MarkupLine( + $"[green]Pure Q8 baseline quantization finished:[/] {Markup.Escape(q8Path)}"); } } + finally + { + _cpuQuantLock.Release(); + } + + await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); + } + else + { + AnsiConsole.MarkupLine($"[grey]Pure Q8 baseline already exists:[/] {Markup.Escape(q8Path)}"); + } + + return q8Path; + } + + // ---------------------------------------------------------------- + // Quantization + // ---------------------------------------------------------------- + + public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) + { + if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) + throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); + + Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); + + var requestedOverrides = BuildRequestedTensorOverrides(quant); + + // Keep this resolution step: + // it is not output validation; it is how logical group rules become real tensor names. + var concreteOverrides = await ResolveConcreteTensorOverridesAsync( + inputGgufPath: inputFile, + outputFilePath: outputFile, + requestedOverrides: requestedOverrides); + + if (requestedOverrides.Count > 0 && concreteOverrides.Count == 0) + { + throw new InvalidOperationException( + $"No concrete tensors were resolved for requested overrides when quantizing '{outputFile}'. " + + "This means the requested tensor selectors did not match the input GGUF."); + } + + var args = new List(capacity: 256); + + foreach (var overrideItem in concreteOverrides) + { + args.Add($"--tensor-type \"{overrideItem.TensorName}={overrideItem.SchemeName}\""); } args.Add($"\"{inputFile}\""); args.Add($"\"{outputFile}\""); - args.Add(ResolveBaseName(quant.BaseQuant)); + args.Add(ResolveQuantizeBaseArgument(quant, concreteOverrides)); args.Add("8"); string arguments = string.Join(" ", args); @@ -460,40 +541,266 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb Cache.LlamaBin!, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "llama-quantize.exe" : "llama-quantize"); + string quantizeLogPath = outputFile + ".quantize.log"; + var psi = new ProcessStartInfo { FileName = bin, - Arguments = arguments, - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true + Arguments = arguments }; - using var p = Process.Start(psi); - if (p == null) - throw new InvalidOperationException($"Failed to start process: {bin}"); + var result = await RunLoggedProcessAsync(psi, quantizeLogPath); - p.OutputDataReceived += (_, e) => + if (result.ExitCode != 0) { - if (!string.IsNullOrWhiteSpace(e.Data)) - AnsiConsole.WriteLine(e.Data); - }; + await HardDeleteHelper.DeleteFileIfExistsAsync(outputFile); + + throw new InvalidOperationException( + $"Quantization failed for '{outputFile}'. ExitCode={result.ExitCode}. See '{quantizeLogPath}'."); + } - p.ErrorDataReceived += (_, e) => + if (!File.Exists(outputFile) || new FileInfo(outputFile).Length == 0) { - if (!string.IsNullOrWhiteSpace(e.Data)) - AnsiConsole.WriteLine(e.Data); - }; + await HardDeleteHelper.DeleteFileIfExistsAsync(outputFile); + + throw new InvalidOperationException( + $"Quantization process exited successfully but produced no valid GGUF output: {outputFile}"); + } + + AnsiConsole.MarkupLine($"[green]Quantized model ready:[/] {Markup.Escape(outputFile)}"); + } + + private static string ResolveQuantizeBaseArgument( + HybridQuant quant, + List concreteOverrides) + { + if (quant.BaseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId && + concreteOverrides.Count > 0) + { + throw new InvalidOperationException( + "Selective tensor overrides cannot use a native-source base quant (BF16/F16/F32). " + + "A real base quant such as Q8_0, Q6_K, Q5_K, Q4_K_M, or IQ4_XS must be provided."); + } + + return ResolveBaseName(quant.BaseQuant); + } + + private static TensorWeightScheme? TryResolveBaseTensorScheme(BaselineQuants baseQuant) + { + if (baseQuant.Names.IsDefaultOrEmpty) + return null; + + return TensorWeightScheme.All.FirstOrDefault(s => + !s.Names.IsDefaultOrEmpty && + s.Names.Any(sn => baseQuant.Names.Contains(sn, StringComparer.OrdinalIgnoreCase))); + } + + private List BuildRequestedTensorOverrides(HybridQuant quant) + { + var result = new List(); + + if (quant.Tensors == null || quant.Tensors.Count == 0) + return result; + + var baseScheme = TryResolveBaseTensorScheme(quant.BaseQuant); + + foreach (var hybrid in quant.Tensors) + { + if (hybrid?.TGroup == null) + continue; + + if (hybrid.TensorType.UniqueId == TensorWeightScheme.NULL.UniqueId) + continue; + + // Do not emit a redundant override if this tensor type is already the same + // as the blanket base quant. + if (baseScheme != null && hybrid.TensorType.UniqueId == baseScheme.UniqueId) + continue; + + string schemeName = ResolveSchemeName(hybrid.TensorType); + + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + SchemeName = schemeName, + Patterns = hybrid.TGroup.Tensors.ToList() + }); + } + + return result; + } + + private async Task> ResolveConcreteTensorOverridesAsync( + string inputGgufPath, + string outputFilePath, + List requestedOverrides) + { + if (requestedOverrides.Count == 0) + return new List(); + + string workingDir = Path.GetDirectoryName(outputFilePath)!; + string unique = Guid.NewGuid().ToString("N"); + + string payloadPath = Path.Combine(workingDir, $"resolve_tensor_overrides_{unique}.json"); + string resultPath = Path.Combine(workingDir, $"resolve_tensor_overrides_result_{unique}.json"); + string scriptPath = Path.Combine(workingDir, $"resolve_tensor_overrides_{unique}.py"); + + try + { + var payload = new + { + gguf_path = inputGgufPath, + output_path = resultPath, + requests = requestedOverrides + }; + + await File.WriteAllTextAsync( + payloadPath, + JsonSerializer.Serialize(payload, new JsonSerializerOptions { WriteIndented = true })); + + string py = """ + import json + import re + import sys + + payload_path = sys.argv[1] + + def write_result(obj, output_path): + with open(output_path, "w", encoding="utf-8") as f: + json.dump(obj, f, indent=2) + + with open(payload_path, "r", encoding="utf-8") as f: + payload = json.load(f) + + output_path = payload["output_path"] + + try: + import gguf + except Exception as e: + write_result({"Error": f"Failed to import gguf: {e}"}, output_path) + sys.exit(0) + + try: + reader = gguf.GGUFReader(payload["gguf_path"]) + except Exception as e: + write_result({"Error": f"Failed to read GGUF: {e}"}, output_path) + sys.exit(0) + + tensor_names = [t.name for t in reader.tensors] + + resolved = [] + group_counts = {} + unmatched = [] + duplicates = [] + seen = {} + + for req in payload["requests"]: + group = req["GroupName"] + scheme = req["SchemeName"] + patterns = req["Patterns"] + + compiled = [re.compile(p) for p in patterns] + matches = [] + + for name in tensor_names: + if any(r.fullmatch(name) for r in compiled): + matches.append(name) + + group_counts[group] = len(matches) + + if len(matches) == 0: + unmatched.append(group) + + for name in matches: + if name in seen and seen[name] != group: + duplicates.append(name) + else: + seen[name] = group + + resolved.append({ + "TensorName": name, + "SchemeName": scheme, + "GroupName": group + }) + + write_result({ + "Resolved": resolved, + "GroupMatchCounts": group_counts, + "UnmatchedGroups": unmatched, + "DuplicateTensors": sorted(set(duplicates)) + }, output_path) + """; + + await File.WriteAllTextAsync(scriptPath, py); + await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); + + if (!File.Exists(resultPath)) + throw new InvalidOperationException("Tensor override resolution produced no result file."); + + var result = JsonSerializer.Deserialize( + await File.ReadAllTextAsync(resultPath)); + + if (result == null) + throw new InvalidOperationException("Tensor override resolution returned null."); + + if (!string.IsNullOrWhiteSpace(result.Error)) + throw new InvalidOperationException(result.Error); + + if (result.UnmatchedGroups.Count > 0) + { + throw new InvalidOperationException( + $"The following requested override groups matched zero tensors in the input GGUF: " + + $"{string.Join(", ", result.UnmatchedGroups)}"); + } + + if (result.DuplicateTensors.Count > 0) + { + throw new InvalidOperationException( + $"A tensor matched more than one override group, which is ambiguous: " + + $"{string.Join(", ", result.DuplicateTensors.Take(20))}"); + } - p.BeginOutputReadLine(); - p.BeginErrorReadLine(); - await p.WaitForExitAsync(); + return result.Resolved; + } + finally + { + if (File.Exists(payloadPath)) File.Delete(payloadPath); + if (File.Exists(resultPath)) File.Delete(resultPath); + if (File.Exists(scriptPath)) File.Delete(scriptPath); + } + } + + // ---------------------------------------------------------------- + // Internal DTOs + // ---------------------------------------------------------------- - if (p.ExitCode != 0) - throw new Exception($"Quantization failed for {outputFile}"); + private sealed class RequestedTensorOverride + { + public string GroupName { get; set; } = string.Empty; + public string SchemeName { get; set; } = string.Empty; + public List Patterns { get; set; } = new(); } + private sealed class ConcreteTensorOverride + { + public string TensorName { get; set; } = string.Empty; + public string SchemeName { get; set; } = string.Empty; + public string GroupName { get; set; } = string.Empty; + } + + private sealed class TensorResolutionResult + { + public string? Error { get; set; } + public List Resolved { get; set; } = new(); + public Dictionary GroupMatchCounts { get; set; } = new(); + public List UnmatchedGroups { get; set; } = new(); + public List DuplicateTensors { get; set; } = new(); + } + + // ---------------------------------------------------------------- + // Naming helpers + // ---------------------------------------------------------------- + private static string ResolveBaseName(BaselineQuants b) { if (b.Names.IsDefaultOrEmpty) @@ -526,12 +833,14 @@ public string GenerateHybridName(HybridQuant quant) string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; string baseName = ResolveBaseName(quant.BaseQuant); - if (quant.Tensors == null || quant.Tensors.Count == 0) - { + var effectiveTensors = quant.Tensors? + .Where(t => t?.TGroup != null && t.TensorType.UniqueId != TensorWeightScheme.NULL.UniqueId) + .ToList(); + + if (effectiveTensors == null || effectiveTensors.Count == 0) return $"{modelName}-{baseName}"; - } - var grouped = quant.Tensors + var grouped = effectiveTensors .GroupBy(t => ResolveSchemeName(t.TensorType)) .Select(g => new { @@ -543,7 +852,7 @@ public string GenerateHybridName(HybridQuant quant) .OrderBy(x => GetOrder(x.Codes.FirstOrDefault())) .ToList(); - var nameParts = new List(capacity: grouped.Count); + var nameParts = new List(grouped.Count); foreach (var group in grouped) { @@ -568,4 +877,104 @@ private string SimplifyQuant(string quant) .Replace("F16", "F16") .Replace("F32", "F32"); } + + private sealed class LoggedProcessResult + { + public int ExitCode { get; init; } + public string StdOut { get; init; } = string.Empty; + public string StdErr { get; init; } = string.Empty; + } + + private async Task RunLoggedProcessAsync( + ProcessStartInfo psi, + string? logPath, + CancellationToken ct = default) + { + psi.RedirectStandardOutput = true; + psi.RedirectStandardError = true; + psi.UseShellExecute = false; + psi.CreateNoWindow = true; + + using var process = new Process + { + StartInfo = psi, + EnableRaisingEvents = true + }; + + var stdoutBuilder = new StringBuilder(); + var stderrBuilder = new StringBuilder(); + object sync = new(); + + var stdoutClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var stderrClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + StreamWriter? logWriter = null; + FileStream? logStream = null; + + if (!string.IsNullOrWhiteSpace(logPath)) + { + logStream = new FileStream(logPath, FileMode.Create, FileAccess.Write, FileShare.Read); + logWriter = new StreamWriter(logStream) { AutoFlush = true }; + } + + void HandleLine(string? line, bool isError) + { + if (line == null) + { + if (isError) + stderrClosed.TrySetResult(true); + else + stdoutClosed.TrySetResult(true); + + return; + } + + lock (sync) + { + if (isError) + stderrBuilder.AppendLine(line); + else + stdoutBuilder.AppendLine(line); + + logWriter?.WriteLine(line); + } + + AnsiConsole.WriteLine(line); + } + + process.OutputDataReceived += (_, e) => HandleLine(e.Data, isError: false); + process.ErrorDataReceived += (_, e) => HandleLine(e.Data, isError: true); + + if (!process.Start()) + throw new InvalidOperationException($"Failed to start process: {psi.FileName}"); + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + using var ctr = ct.Register(() => + { + try + { + if (!process.HasExited) + process.Kill(entireProcessTree: true); + } + catch + { + // ignored + } + }); + + await process.WaitForExitAsync(ct); + await Task.WhenAll(stdoutClosed.Task, stderrClosed.Task); + + logWriter?.Dispose(); + logStream?.Dispose(); + + return new LoggedProcessResult + { + ExitCode = process.ExitCode, + StdOut = stdoutBuilder.ToString(), + StdErr = stderrBuilder.ToString() + }; + } } \ No newline at end of file From e8ba05bb80677536cff07908183289d0c46ea9a6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 13 Apr 2026 17:23:55 -0400 Subject: [PATCH 044/258] reverted benchmark --- MagicQuant/Services/BenchmarkService.cs | 42 ++++++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 431962d..cfde2d6 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -655,8 +655,9 @@ public async Task RunAllBenchmarksAsync( { ModelSizeBytes = TryGetModelSize(modelPath) }; - - string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); + + // Disabled for now. Too many variables that're annoying to track + /*string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); if (TryReadExistingLlamaBenchLog(llamaBenchPath, out var existingLlamaBench)) { result.LlamaBench = existingLlamaBench; @@ -666,7 +667,16 @@ public async Task RunAllBenchmarksAsync( AnsiConsole.MarkupLine( $"[yellow]Running Llama-Bench[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, effectiveNgl, slot); - } + }*/ + + result.LlamaBench = new LlamaBenchMetrics + { + LogPath = null, + Backend = slot.UsesGpu ? "disabled" : "cpu-disabled", + Ngl = effectiveNgl, + Test = "disabled", + Tps = 0 + }; var corporaRoot = Path.Combine(Path.GetDirectoryName(benchDir)!, "_ppl_corpora"); Directory.CreateDirectory(corporaRoot); @@ -959,14 +969,27 @@ private bool TryReadExistingBenchmarkArtifacts( // fall through } } - - string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); + + // not currently requiring llama bench + /*string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); if (!TryReadExistingLlamaBenchLog(llamaBenchPath, out var llamaBench)) return false; var rebuilt = new BenchmarkResult { LlamaBench = llamaBench + };*/ + + var rebuilt = new BenchmarkResult + { + LlamaBench = new LlamaBenchMetrics + { + LogPath = null, + Backend = "disabled", + Ngl = 0, + Test = "disabled", + Tps = 0 + } }; foreach (var domain in requestedDomains) @@ -993,8 +1016,9 @@ private bool IsReusableBenchmarkResult( IReadOnlyCollection requestedDomains, bool requireKld) { - if (result.LlamaBench == null || !result.LlamaBench.Tps.HasValue || result.LlamaBench.Tps.Value <= 0) - return false; + // llama bench removed for now + /*if (result.LlamaBench == null || !result.LlamaBench.Tps.HasValue || result.LlamaBench.Tps.Value <= 0) + return false;*/ foreach (var domain in requestedDomains) { @@ -1096,7 +1120,9 @@ private static bool HasRequiredCategories( return false; } - return bench.TokensPerSecond > 0 && bench.SizeBytes > 0; + // no longer requiring llama bench atm until furthern otice + //return bench.TokensPerSecond > 0 && bench.SizeBytes > 0; + return bench.SizeBytes > 0; } private BenchmarkResult BuildResultFromDb( From 955506082e9af3166bc242ad5ab843a0a2c91bc7 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 14 Apr 2026 15:16:28 -0400 Subject: [PATCH 045/258] new pruning and isolation system built! --- MQ.DB/Models/BaselineQuants.cs | 57 ++- MQ.DB/Models/HybridQuant.cs | 31 +- MQ.DB/Models/RequiredSamplePlan.cs | 31 ++ MQ.DB/Models/TensorWeightScheme.cs | 79 +++-- MagicQuant/Commands/Evolution.cs | 117 ++++--- MagicQuant/Helpers/CliHelpers.cs | 102 ++---- MagicQuant/Helpers/ComboLogic.cs | 62 ++-- MagicQuant/Helpers/RuntimeSearchSpace.cs | 74 ++++ MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 85 +++++ MagicQuant/Helpers/TensorConfigGenerator.cs | 225 ++++++------ MagicQuant/Services/BenchmarkService.cs | 169 ++++----- .../Services/IsolationOptimizationService.cs | 330 ++++++++++++++++++ .../Services/ModelCompatibilityService.cs | 177 +++++----- MagicQuant/Services/QuantDatabaseService.cs | 20 +- MagicQuant/Services/QuantizationService.cs | 122 ++++++- 15 files changed, 1167 insertions(+), 514 deletions(-) create mode 100644 MQ.DB/Models/RequiredSamplePlan.cs create mode 100644 MagicQuant/Helpers/RuntimeSearchSpace.cs create mode 100644 MagicQuant/Helpers/SearchSpaceDebugPrinter.cs create mode 100644 MagicQuant/Services/IsolationOptimizationService.cs diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 5202b29..247712a 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -8,10 +8,6 @@ public record BaselineQuants( ImmutableArray Names, HybridQuant? BaseConversionBase = null) { - /// - /// Reserved internal ID for the original/native source model (BF16/F16/F32). - /// This MUST NOT collide with any real llama.cpp export base quant. - /// public const byte NativeSourceUniqueId = 250; public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"]); @@ -19,10 +15,13 @@ public record BaselineQuants( public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"]); public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"]); - public static readonly BaselineQuants MXFP4_MOE = new(4, false, ["MXFP4_MOE"], + public static readonly BaselineQuants MXFP4_MOE = new( + 4, + false, + ["MXFP4_MOE"], new HybridQuant { - BaseQuant = MXFP4_MOE, + BaseQuant = null!, Tensors = TReg.All .Select(g => new HybridTensor { @@ -32,10 +31,15 @@ public record BaselineQuants( .ToList() }); - public static readonly BaselineQuants IQ4_XS = new(6, false, ["IQ4_XS"], + public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"]); + + public static readonly BaselineQuants IQ4_XS = new( + 6, + false, + ["IQ4_XS"], new HybridQuant { - BaseQuant = IQ4_XS, + BaseQuant = null!, Tensors = TReg.All .Select(g => new HybridTensor { @@ -45,17 +49,6 @@ public record BaselineQuants( .ToList() }); - public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"]); - - public static BaselineQuants GetBF16Quant() - { - return new( - NativeSourceUniqueId, - false, - [Cache.TorchType?.ToString() ?? "BF16"] - ); - } - // IQ3 and lower require imatrix //public static readonly BaselineQuants IQ3_M = new(7, true, ["IQ3_M"], true); //public static readonly BaselineQuants IQ2_M = new(8, true, ["IQ2_M"], true); @@ -72,4 +65,30 @@ public static BaselineQuants GetBF16Quant() //IQ3_M, //IQ2_M ]; + + static BaselineQuants() + { + MXFP4_MOE.BaseConversionBase!.BaseQuant = MXFP4_MOE; + IQ4_XS.BaseConversionBase!.BaseQuant = IQ4_XS; + } + + public static BaselineQuants GetBF16Quant() + { + return new( + NativeSourceUniqueId, + false, + [(Cache.TorchType ?? Cache.MainTorchType.BF16).ToString()]); + } + + public static BaselineQuants FromId(byte id) + { + if (id == NativeSourceUniqueId) + return GetBF16Quant(); + + var found = All.FirstOrDefault(x => x.UniqueId == id); + if (found == null) + throw new InvalidOperationException($"Unknown baseline quant id '{id}'."); + + return found; + } } \ No newline at end of file diff --git a/MQ.DB/Models/HybridQuant.cs b/MQ.DB/Models/HybridQuant.cs index 116938d..72264ed 100644 --- a/MQ.DB/Models/HybridQuant.cs +++ b/MQ.DB/Models/HybridQuant.cs @@ -9,7 +9,7 @@ public HybridQuant() { } public HybridQuant(TensorConfig c) { - BaseQuant = BaselineQuants.All.First(b => b.UniqueId == c.BaseQuant); + BaseQuant = BaselineQuants.FromId(c.BaseQuant); AddIfNotNull(TReg.Embeddings, c.Embeddings); AddIfNotNull(TReg.LmHead, c.LmHead); @@ -51,11 +51,38 @@ public HybridQuant Clone() }; } + public static HybridQuant CreatePureBaseline(BaselineQuants baseQuant) + { + return new HybridQuant + { + BaseQuant = baseQuant, + Tensors = new List() + }; + } + + public static HybridQuant CreateBlanket( + BaselineQuants baseQuant, + IEnumerable groups, + TensorWeightScheme blanketScheme) + { + return new HybridQuant + { + BaseQuant = baseQuant, + Tensors = groups + .Select(g => new HybridTensor + { + TGroup = g, + TensorType = blanketScheme + }) + .ToList() + }; + } + public static explicit operator HybridQuant(TensorConfig c) => new HybridQuant(c); } public class HybridTensor { public TensorGroup TGroup { get; set; } = null!; - public TensorWeightScheme TensorType { get; set; } + public TensorWeightScheme TensorType { get; set; } = default!; } \ No newline at end of file diff --git a/MQ.DB/Models/RequiredSamplePlan.cs b/MQ.DB/Models/RequiredSamplePlan.cs new file mode 100644 index 0000000..6087654 --- /dev/null +++ b/MQ.DB/Models/RequiredSamplePlan.cs @@ -0,0 +1,31 @@ +namespace MQ.DB.Models; + +public enum RequiredSampleKind +{ + PureBaseline = 1, + BaseOnlyIsolation = 2, + GroupIsolation = 3 +} + +public sealed class RequiredSamplePlan +{ + public RequiredSampleKind Kind { get; set; } + public string Key { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + public HybridQuant Quant { get; set; } = default!; + + public byte? TargetGroupId { get; set; } + public byte? TestedSchemeId { get; set; } + public byte? TestedBaselineId { get; set; } +} + +public sealed class RequiredSampleGenerationResult +{ + public List Plans { get; set; } = new(); + + public int PureBaselineCount { get; set; } + public int BaseOnlyIsolationCount { get; set; } + public int GroupIsolationCount { get; set; } + + public int TotalCount => Plans.Count; +} \ No newline at end of file diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 47e1b68..9a7fd3b 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -4,6 +4,8 @@ namespace MQ.DB.Models; public sealed class TensorWeightScheme { + private readonly HashSet _defaultBannedGroupIds; + public byte UniqueId { get; } public bool RequiresImatrix { get; } public ImmutableArray Names { get; } @@ -20,32 +22,53 @@ private TensorWeightScheme( UniqueId = uniqueId; RequiresImatrix = requiresImatrix; Names = names; - BannedGroups = new List(bannedGroups); BlockNeo = blockNeo; + + var distinctGroups = bannedGroups + .GroupBy(x => x.UniqueId) + .Select(x => x.First()) + .ToList(); + + BannedGroups = distinctGroups; + _defaultBannedGroupIds = distinctGroups.Select(x => x.UniqueId).ToHashSet(); } - // NULL: always-present groups, never nullable - public static TensorWeightScheme NULL = + public void ResetRuntimeBans() + { + BannedGroups.Clear(); + + foreach (var group in TReg.All.Where(x => _defaultBannedGroupIds.Contains(x.UniqueId))) + BannedGroups.Add(group); + } + + public bool IsBannedFor(TensorGroup group) + { + return BannedGroups.Any(x => x.UniqueId == group.UniqueId); + } + + public static void ResetAllRuntimeBans() + { + foreach (var scheme in All) + scheme.ResetRuntimeBans(); + } + + public static readonly TensorWeightScheme NULL = new( 0, false, ["NULL"], - - Array.Empty(), - null - ); + Array.Empty(), + null); - // BF16 and F16 intentionally share UniqueId - public static TensorWeightScheme BF16_F16 = + public static readonly TensorWeightScheme BF16_F16 = new( 1, false, - ["BF16", "F16"], + ["BF16", "F16", "F32"], Array.Empty(), - null - ); + null); - public static TensorWeightScheme MXFP4 = + public static readonly TensorWeightScheme MXFP4 = new( 2, false, @@ -56,33 +79,30 @@ private TensorWeightScheme( TReg.MoeRouter, TReg.MoeExperts }, - 32 - ); + 32); - public static TensorWeightScheme Q8_0 = + public static readonly TensorWeightScheme Q8_0 = new(3, false, ["Q8_0"], Array.Empty(), null); - public static TensorWeightScheme Q6_K = + public static readonly TensorWeightScheme Q6_K = new(4, false, ["Q6_K"], Array.Empty(), 256); - public static TensorWeightScheme Q5_K = + public static readonly TensorWeightScheme Q5_K = new( 5, false, ["Q5_K"], - new[] { TReg.MoeRouter }, - 256 - ); + new[] { TReg.MoeRouter }, + 256); - public static TensorWeightScheme IQ4_XS = + public static readonly TensorWeightScheme IQ4_XS = new( 6, false, ["IQ4_XS"], new[] { TReg.MoeRouter }, - 32 - ); - + 32); + /* public static TensorWeightScheme IQ4_NL = new( @@ -191,12 +211,5 @@ private TensorWeightScheme( Q6_K, Q5_K, IQ4_XS, - //IQ4_NL, - /*IQ3_S, - IQ3_XS, - IQ3_XXS, - IQ2_S, - IQ2_XS, - IQ2_XXS*/ ]; -} +} \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index fef2b43..f4439c8 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -1,8 +1,8 @@ -using MagicQuant.Models; using MagicQuant.Helpers; +using MagicQuant.Models; +using MagicQuant.Services; using MQ.DB; using MQ.DB.Models; -using MagicQuant.Services; using Spectre.Console; namespace MagicQuant.Commands; @@ -11,14 +11,12 @@ public class Evolution : ICommand { public async Task Run(List args) { - // 1. Handle Help Flag if (args.Any(a => a.Name?.ToLower() == "help")) { ShowEvolutionHelp(); return; } - // 2. Parse --model-dir string? modelDirRaw = args.FirstOrDefault(a => a.Name?.ToLower() == "model-dir")?.Value; if (string.IsNullOrWhiteSpace(modelDirRaw)) @@ -29,7 +27,6 @@ public async Task Run(List args) throw new Exception(msg); } - // 3. Normalize and Validate Path string fullModelPath = Path.GetFullPath(modelDirRaw); if (!Directory.Exists(fullModelPath)) @@ -40,9 +37,6 @@ public async Task Run(List args) throw new Exception(msg); } - // 4. Validate Content (.safetensors existence) - // We look for any .safetensors file in the top directory. - // If your models are often in subfolders, change SearchOption.TopDirectoryOnly to AllDirectories. var safeTensorFiles = Directory.GetFiles(fullModelPath, "*.safetensors", SearchOption.TopDirectoryOnly); if (safeTensorFiles.Length == 0) @@ -52,60 +46,43 @@ public async Task Run(List args) throw new Exception(); } - // 5. Populate Cache Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); - JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); - // Create the MagicQuant directory immediately so it's ready for future steps if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) - { Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); - } - // 6. Success Output AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); AnsiConsole.Write(new Rule("[yellow]Evolution Configuration[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"Model Path: [blue]{Cache.ModelDirectory}[/]"); AnsiConsole.MarkupLine($"Output Path: [blue]{Cache.ModelMagicQuantDirectory}[/]"); AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length}[/] safe tensors"); - - // Ensure Llama paths are set (sanity check from InitializeLlamaCpp) + if (string.IsNullOrEmpty(Cache.LlamaBin)) - { - // Note: In a real run, Program.cs runs Init first, so this might be populated. - // If not, we might want to warn or rely on defaults. AnsiConsole.MarkupLine("[yellow]Warning: Llama binaries path not set in Cache. (Did Initialization run?)[/]"); - } Console.WriteLine("Acquiring unique model ID..."); - Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); - AnsiConsole.MarkupLine($"[green] Model ID Created/Found: {Cache.CurrentModelId}[/]"); - + var pyManager = new PythonManager(Cache.MagicQuantDirectory); - var bService = new BenchmarkService(pyManager); - var qService = new QuantizationService(bService); + var benchmarkService = new BenchmarkService(pyManager); + var quantizationService = new QuantizationService(benchmarkService); - var bf16ModelGgufPath = await qService.EnsureBaseModelFileAsync(true); - var q8ModelGgufPath = await qService.EnsurePureQ8ModelAsync(); + var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); + var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); - await bService.EnsureExecutionPlanAsync(q8ModelGgufPath); - await bService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); + await benchmarkService.EnsureExecutionPlanAsync(q8ModelGgufPath); + await benchmarkService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); var baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); var baseBenchDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks", baseTypeName); var baseLogitsDir = Path.Combine(baseBenchDir, "logits"); - var baseModelQuant = new HybridQuant - { - BaseQuant = BaselineQuants.GetBF16Quant(), - Tensors = new List() - }; + var baseModelQuant = HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()); - await bService.RunAllBenchmarksAsync( + await benchmarkService.RunAllBenchmarksAsync( quantConfig: baseModelQuant, modelPath: bf16ModelGgufPath, benchDir: baseBenchDir, @@ -113,56 +90,80 @@ await bService.RunAllBenchmarksAsync( saveLogits: true, domainsOverride: new[] { "general", "code", "math" }); -// Optional: capture a quick micro-benchmark for the pure Q8 baseline too. -// var q8BenchDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks", "Q8_0"); -// var q8Quant = new HybridQuant { BaseQuant = BaselineQuants.Q8_0, Tensors = new List() }; -// await bService.RunAllBenchmarksAsync(q8Quant, q8ModelGgufPath, q8BenchDir, saveLogits: false, domainsOverride: new[] { "general" }); - var compatibilityService = new ModelCompatibilityService(pyManager); await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); - + CliHelpers.ValidateCombinationLogicWorks(true); - - var dbService = new QuantDatabaseService(); - // This ensures the DB is ready, populated, and valid before you proceed + var dbService = new QuantDatabaseService(); await dbService.InitializeAsync(); AnsiConsole.Write(new Rule("[yellow]Required Sample Generation[/]") { Justification = Justify.Left }); - var requiredSamples = TensorConfigGenerator.GenerateRequiredDataSampleCombos(Cache.UnusedTensorGroups); - - AnsiConsole.MarkupLine($"[grey]Queued required samples:[/] [cyan]{requiredSamples.Count:N0}[/]"); + var samplePlan = TensorConfigGenerator.GenerateRequiredSamplePlan(Cache.UnusedTensorGroups); + AnsiConsole.MarkupLine($"[grey]Queued required samples:[/] [cyan]{samplePlan.TotalCount:N0}[/]"); AnsiConsole.MarkupLine("[grey]SQLite will be treated as the source of truth for completed samples.[/]"); - var summary = await qService.ProcessHybridBatchAsync(requiredSamples); + var sampleSummary = await quantizationService.ProcessHybridBatchAsync(samplePlan.Plans); AnsiConsole.MarkupLine("[bold green]Sample generation phase complete.[/]"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {summary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {summary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {summary.Failed:N0}"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {sampleSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {sampleSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {sampleSummary.Failed:N0}"); + + + + + var comboCountBefore = ComboCounter.CountAll(); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Isolation Optimization"); + + AnsiConsole.Write(new Rule("[yellow]Isolation Optimization[/]") { Justification = Justify.Left }); + var isolationOptimizer = new IsolationOptimizationService(); + var isolationResult = await isolationOptimizer.AnalyzeAndApplyAsync(samplePlan); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Isolation Optimization"); + + foreach (var gd in isolationResult.GroupDetails.OrderBy(x => x.GroupName)) + { + AnsiConsole.Write(new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") { Justification = Justify.Left }); + + AnsiConsole.MarkupLine($"[green]Best reduction:[/] {gd.BestReductionRatio:P2}"); + AnsiConsole.MarkupLine($"[green]Winning scheme:[/] {Markup.Escape(gd.WinningScheme ?? "n/a")}"); + AnsiConsole.MarkupLine($"[green]Locked to native:[/] {(gd.LockedToNative ? "[red]yes[/]" : "[green]no[/]")}"); + + foreach (var line in gd.Candidates) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); + } + + + var comboCountAfter = ComboCounter.CountAll(); + + await dbService.InitializeAsync(forceRebuild: true); + + AnsiConsole.MarkupLine($"[green]Native-locked groups:[/] {isolationResult.NativeLockedGroups:N0}"); + AnsiConsole.MarkupLine($"[green]Dominated group-scheme bans applied:[/] {isolationResult.DominatedGroupSchemesBanned:N0}"); + AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count after pruning:[/] {comboCountAfter:N0}"); + + foreach (var note in isolationResult.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); } private void ShowEvolutionHelp() { - // Use MarkupLine for colors/styles AnsiConsole.MarkupLine("[bold yellow]Command: evolution[/]"); AnsiConsole.WriteLine("Runs the full evolutionary quantization search algorithm on a target model."); AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Usage:[/]"); - // Use WriteLine here so "[options]" doesn't crash it AnsiConsole.WriteLine(" mq evolution --model-dir \"\" [options]"); AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Arguments:[/]"); - // Use MarkupLine here because we WANT the [green] color AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Required)"); AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Example:[/]"); - // Use WriteLine here to avoid issues with paths (backslashes) AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } } \ No newline at end of file diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index 71fe65a..da53dc0 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -4,9 +4,9 @@ using System.Text.RegularExpressions; using MagicQuant.Commands; using MagicQuant.Models; -using Spectre.Console; using MQ.DB; using MQ.DB.Models; +using Spectre.Console; namespace MagicQuant.Helpers; @@ -14,113 +14,59 @@ public static class CliHelpers { public static void ValidateCombinationLogicWorks(bool realResults = false) { - CliHelpers.PrintTotalCombinationCount(); + PrintTotalCombinationCount(); - // ---------------------------------------- - // Pre-compute expected total - // ---------------------------------------- var expectedTotal = ComboCounter.CountAll(); - if (!realResults) - { - AnsiConsole.MarkupLine( - $"[bold cyan]Expected total combinations:[/] [bold yellow]{expectedTotal:N0}[/]"); - } - else - { - AnsiConsole.MarkupLine( - $"[bold cyan]Total real combinations after model detection:[/] [bold yellow]{expectedTotal:N0}[/]"); - } + AnsiConsole.MarkupLine( + realResults + ? $"[bold cyan]Total real combinations after model detection:[/] [bold yellow]{expectedTotal:N0}[/]" + : $"[bold cyan]Expected total combinations:[/] [bold yellow]{expectedTotal:N0}[/]"); - // ---------------------------------------- - // Generation + timing - // ---------------------------------------- var sw = Stopwatch.StartNew(); - long actualTotal = 0; - var bases = - BaselineQuants.All - .Where(b => b.BaseConversionBase != null) - .ToImmutableArray(); - - foreach (var b in bases) + foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines().ToImmutableArray()) { AnsiConsole.MarkupLine( - $"[cyan]Base:[/] [bold]{string.Join("/", b.Names)}[/] " + - $"[grey](RequiresImatrix={b.RequiresImatrix})[/]"); + $"[cyan]Base:[/] [bold]{string.Join("/", baseline.Names)}[/] [grey](RequiresImatrix={baseline.RequiresImatrix})[/]"); long baseTotal = 0; - foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches( - b, batchSize: 10_000_000)) + foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches(baseline, batchSize: 10_000_000)) { baseTotal += batch.Count; actualTotal += batch.Count; - AnsiConsole.MarkupLine( - $" [green]Batch:[/] {batch.Count:N0} " + - $"[grey]BaseRunning:[/] {baseTotal:N0}"); - - // Release memory aggressively (unit-test mode) + AnsiConsole.MarkupLine($" [green]Batch:[/] {batch.Count:N0} [grey]BaseRunning:[/] {baseTotal:N0}"); batch.Clear(); } - AnsiConsole.MarkupLine( - $"[yellow]Base total:[/] {baseTotal:N0}"); + AnsiConsole.MarkupLine($"[yellow]Base total:[/] {baseTotal:N0}"); } sw.Stop(); - // ---------------------------------------- - // Verification - // ---------------------------------------- bool match = actualTotal == expectedTotal; - AnsiConsole.MarkupLine( - $"[bold green]Generated total:[/] {actualTotal:N0}"); - + AnsiConsole.MarkupLine($"[bold green]Generated total:[/] {actualTotal:N0}"); AnsiConsole.MarkupLine( match ? "[bold green] Counts match expected total[/]" : $"[bold red] MISMATCH! Expected {expectedTotal:N0} but generated {actualTotal:N0}[/]"); - // ---------------------------------------- - // Human-readable elapsed time - // ---------------------------------------- var t = sw.Elapsed; + AnsiConsole.MarkupLine($"[bold]Elapsed:[/] {t.Hours}h {t.Minutes}m {t.Seconds}s {t.Milliseconds}ms"); - AnsiConsole.MarkupLine( - $"[bold]Elapsed:[/] " + - $"{t.Hours}h {t.Minutes}m {t.Seconds}s {t.Milliseconds}ms"); Console.WriteLine(); Console.WriteLine("---------------"); Console.WriteLine(); - if (!realResults) - { - var MOE = TensorConfigGenerator.GenerateRequiredDataSampleCombos(); - - var Dense = TensorConfigGenerator.GenerateRequiredDataSampleCombos( //); - new List() { TReg.MoeRouter, TReg.MoeExperts }); - - Console.WriteLine(); - Console.WriteLine("---------------"); - Console.WriteLine(); - AnsiConsole.MarkupLine( - $"[bold green]Max MOE samples created:[/] {MOE.Count():N0}"); - AnsiConsole.MarkupLine( - $"[bold green]Max Dense samples created:[/] {Dense.Count():N0}"); - } - else - { - var RealBans = TensorConfigGenerator.GenerateRequiredDataSampleCombos(Cache.UnusedTensorGroups); - Console.WriteLine(); - Console.WriteLine("---------------"); - Console.WriteLine(); - AnsiConsole.MarkupLine( - $"[bold green]Max real samples to create:[/] {RealBans.Count():N0}"); - } + var samplePlan = TensorConfigGenerator.GenerateRequiredSamplePlan(realResults ? Cache.UnusedTensorGroups : null); + AnsiConsole.MarkupLine($"[bold green]Required pure baselines:[/] {samplePlan.PureBaselineCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Required base-only isolations:[/] {samplePlan.BaseOnlyIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Required group isolations:[/] {samplePlan.GroupIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Total required samples:[/] {samplePlan.TotalCount:N0}"); } public static void PrintTotalCombinationCount() @@ -133,18 +79,13 @@ public static void PrintTotalCombinationCount() throw new InvalidOperationException( $"Total combinations ({total:N0}) exceed database primary ID limit ({MaxSupported:N0})."); - AnsiConsole.MarkupLine( - $"[green]Total potential combinations:[/] [bold yellow]{total:N0}[/]"); + AnsiConsole.MarkupLine($"[green]Total potential combinations:[/] [bold yellow]{total:N0}[/]"); } - public static List ParseArguments(string input) { var cliArgs = new List(); - - // Regex identifies --key value or --key "value with spaces" - var regex = new Regex(@"--(?[^\s=]+)(?:[\s=]+(?:""(?[^""]*)""|(?[^\s-]*)))?", - RegexOptions.IgnoreCase); + var regex = new Regex(@"--(?[^\s=]+)(?:[\s=]+(?:""(?[^""]*)""|(?[^\s-]*)))?", RegexOptions.IgnoreCase); var matches = regex.Matches(input); foreach (Match match in matches) @@ -164,7 +105,6 @@ public static void ShowHelp(Dictionary GroupsOrdered = TReg.All.OrderBy(g => g.UniqueId).ToImmutableArray(); @@ -15,46 +14,57 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants { bool baseRequiresImatrix = baseQuant.RequiresImatrix; - var schemesForBase = - TensorWeightScheme.All - .Where(s => baseRequiresImatrix || !s.RequiresImatrix) - .ToImmutableArray(); + var schemesForBase = TensorWeightScheme.All + .Where(s => baseRequiresImatrix || !s.RequiresImatrix) + .ToImmutableArray(); if (schemesForBase.IsEmpty) throw new InvalidOperationException("No tensor schemes available for this base."); var builder = ImmutableArray.CreateBuilder(); + var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); foreach (var group in GroupsOrdered) { - var ids = - schemesForBase - .Where(s => - s.BannedGroups.Count == 0 || - !s.BannedGroups.Contains(group)) - .Select(s => s.UniqueId) - .ToArray(); + if (unusedIds.Contains(group.UniqueId)) + { + builder.Add([TensorWeightScheme.NULL.UniqueId]); + continue; + } + + if (RuntimeSearchSpace.IsGroupLockedToNative(group)) + { + builder.Add([TensorWeightScheme.BF16_F16.UniqueId]); + continue; + } + + var ids = schemesForBase + .Where(s => + { + if (s.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + return false; + + if (s.UniqueId == TensorWeightScheme.NULL.UniqueId) + return true; + + return !s.IsBannedFor(group); + }) + .Select(s => s.UniqueId) + .Distinct() + .ToArray(); if (ids.Length == 0) + { throw new InvalidOperationException( $"Group '{group.Name}' has no valid tensor schemes for base '{string.Join("/", baseQuant.Names)}'."); + } builder.Add(ids); } - var result = builder.ToImmutable(); - - // 🔒 Absolute safety check (keep this during development) - for (int i = 0; i < result.Length; i++) - { - if (result[i] == null) - throw new InvalidOperationException($"Allowed scheme array at index {i} is null."); - } - - return result; + return builder.ToImmutable(); } - public static BigInteger CountCombinations(in BaselineQuants baseQuant) { var allowed = GetAllowedSchemeIdsPerGroup(baseQuant); @@ -84,8 +94,8 @@ public static BigInteger CountAll() { BigInteger sum = BigInteger.Zero; - foreach (var b in BaselineQuants.All.Where(b => b.BaseConversionBase != null)) - sum += CountForBase(b); + foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) + sum += CountForBase(baseline); return sum; } diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs new file mode 100644 index 0000000..5b0e9b4 --- /dev/null +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -0,0 +1,74 @@ +using MQ.DB.Models; + +namespace MagicQuant.Helpers; + +public static class RuntimeSearchSpace +{ + private static readonly HashSet NativeLockedGroupIds = new(); + private static readonly HashSet DisabledCombinationBaselineIds = new(); + + public static void ResetForNewModel() + { + NativeLockedGroupIds.Clear(); + DisabledCombinationBaselineIds.Clear(); + TensorWeightScheme.ResetAllRuntimeBans(); + } + + public static bool IsGroupLockedToNative(TensorGroup group) + { + return NativeLockedGroupIds.Contains(group.UniqueId); + } + + public static void LockGroupToNative(TensorGroup group) + { + if (!NativeLockedGroupIds.Add(group.UniqueId)) + return; + + foreach (var scheme in TensorWeightScheme.All) + { + if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId) + continue; + + if (scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + continue; + + if (!scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) + scheme.BannedGroups.Add(group); + } + } + + public static IReadOnlyList GetNativeLockedGroups() + { + return TReg.All + .Where(g => NativeLockedGroupIds.Contains(g.UniqueId)) + .OrderBy(g => g.UniqueId) + .ToList(); + } + + public static IReadOnlyList GetActiveCombinationBaselines() + { + return BaselineQuants.All + .Where(x => x.BaseConversionBase != null) + .Where(x => !DisabledCombinationBaselineIds.Contains(x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + } + + public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allowDisablingLast = false) + { + if (DisabledCombinationBaselineIds.Contains(baseline.UniqueId)) + return false; + + int currentlyActive = GetActiveCombinationBaselines().Count; + if (!allowDisablingLast && currentlyActive <= 1) + return false; + + DisabledCombinationBaselineIds.Add(baseline.UniqueId); + return true; + } + + public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) + { + return DisabledCombinationBaselineIds.Contains(baseline.UniqueId); + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs new file mode 100644 index 0000000..5e4c46a --- /dev/null +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -0,0 +1,85 @@ +using MQ.DB.Models; +using Spectre.Console; +using System.Numerics; +using MQ.DB; + +namespace MagicQuant.Helpers; + +public static class SearchSpaceDebugPrinter +{ + public static void PrintCurrentSearchSpace(string title = "Current Runtime Search Space") + { + AnsiConsole.Write(new Rule($"[yellow]{Markup.Escape(title)}[/]") { Justification = Justify.Left }); + + var activeBaselines = RuntimeSearchSpace.GetActiveCombinationBaselines().ToList(); + var disabledBaselines = BaselineQuants.All + .Where(x => x.BaseConversionBase != null) + .Where(x => RuntimeSearchSpace.IsCombinationBaselineDisabled(x)) + .OrderBy(x => x.UniqueId) + .ToList(); + + AnsiConsole.MarkupLine($"[green]Active combo baselines:[/] {activeBaselines.Count}"); + foreach (var baseline in activeBaselines) + AnsiConsole.MarkupLine($" [cyan]- {string.Join("/", baseline.Names)}[/] (Id={baseline.UniqueId})"); + + if (disabledBaselines.Count > 0) + { + AnsiConsole.MarkupLine($"[yellow]Disabled combo baselines:[/] {disabledBaselines.Count}"); + foreach (var baseline in disabledBaselines) + AnsiConsole.MarkupLine($" [grey]- {string.Join("/", baseline.Names)}[/] (Id={baseline.UniqueId})"); + } + + var locked = RuntimeSearchSpace.GetNativeLockedGroups() + .OrderBy(x => x.UniqueId) + .ToList(); + + AnsiConsole.MarkupLine($"[green]Native-locked groups:[/] {locked.Count}"); + foreach (var group in locked) + AnsiConsole.MarkupLine($" [yellow]- {group.Name}[/] (Id={group.UniqueId})"); + + var unusedIds = Cache.UnusedTensorGroups + .Select(x => x.UniqueId) + .ToHashSet(); + + foreach (var baseline in activeBaselines) + { + AnsiConsole.Write(new Rule($"[blue]Base: {Markup.Escape(string.Join("/", baseline.Names))}[/]") { Justification = Justify.Left }); + + var allowed = ComboLogic.GetAllowedSchemeIdsPerGroup(baseline); + + BigInteger baseCount = BigInteger.One; + + for (int i = 0; i < TReg.All.Length; i++) + { + var group = TReg.All.OrderBy(x => x.UniqueId).ElementAt(i); + var ids = allowed[i]; + baseCount *= ids.Length; + + var names = ids + .Select(id => + { + if (id == TensorWeightScheme.NULL.UniqueId) + return "NULL"; + + var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); + return scheme?.Names[0] ?? $"Unknown({id})"; + }) + .ToList(); + + string state = + unusedIds.Contains(group.UniqueId) ? "unused->NULL" : + RuntimeSearchSpace.IsGroupLockedToNative(group) ? "native-locked" : + "variable"; + + AnsiConsole.MarkupLine( + $" [cyan]{Markup.Escape(group.Name)}[/] => [green]{ids.Length}[/] choice(s) " + + $"[grey][[{Markup.Escape(state)}]][/] :: {Markup.Escape(string.Join(", ", names))}"); + + } + + AnsiConsole.MarkupLine($" [bold green]Base total:[/] {baseCount:N0}"); + } + + AnsiConsole.MarkupLine($"[bold yellow]Grand total:[/] {ComboCounter.CountAll():N0}"); + } +} \ No newline at end of file diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 9fd228b..3c8a535 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -1,118 +1,148 @@ -using MQ.DB; using MQ.DB.Models; +using Spectre.Console; using System.Collections.Concurrent; using System.Collections.Immutable; -using Spectre.Console; +using MQ.DB; namespace MagicQuant.Helpers; public static class TensorConfigGenerator { - public static List GenerateRequiredDataSampleCombos(List? missingTensorGroups = null) + public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( + List? missingTensorGroups = null) { if (missingTensorGroups != null && !missingTensorGroups.Any()) missingTensorGroups = null; - var allowedBaselines = BaselineQuants.All.Where(x => x.BaseConversionBase != null).ToList(); - var hybridQuants = new List(); + var skippedIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + var activeGroups = TReg.All + .Where(x => !skippedIds.Contains(x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); - var missingIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); - var existingGroups = TReg.All.Where(g => !missingIds.Contains(g.UniqueId)).ToList(); + var result = new RequiredSampleGenerationResult(); - // 1. PURE BASELINE CONTROLS - int baseTestsRequired = 0; - foreach (var baseline in allowedBaselines) + // --------------------------------------------------------- + // 1. Pure baselines + // --------------------------------------------------------- + foreach (var baseline in BaselineQuants.All.OrderBy(x => x.UniqueId)) { - baseTestsRequired++; + result.Plans.Add(new RequiredSamplePlan + { + Kind = RequiredSampleKind.PureBaseline, + Key = $"pure:{baseline.UniqueId}", + Description = $"Pure baseline build for {string.Join("/", baseline.Names)}", + Quant = HybridQuant.CreatePureBaseline(baseline), + TestedBaselineId = baseline.UniqueId + }); - if (baseline.BaseConversionBase == null) - throw new InvalidOperationException( - $"Baseline {string.Join("/", baseline.Names)} is missing BaseConversionBase."); + result.PureBaselineCount++; + } + + // --------------------------------------------------------- + // 2. Base-only isolation for actual combo baselines + // This tells you whether uncovered tensors alone justify the baseline. + // --------------------------------------------------------- + foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) + { + var quant = HybridQuant.CreateBlanket( + baseQuant: baseline, + groups: activeGroups, + blanketScheme: TensorWeightScheme.BF16_F16); - hybridQuants.Add(new HybridQuant + result.Plans.Add(new RequiredSamplePlan { - BaseQuant = baseline, - Tensors = baseline.BaseConversionBase.Tensors - .Where(t => t.TGroup != null && !missingIds.Contains(t.TGroup.UniqueId)) - .Select(t => new HybridTensor - { - TGroup = t.TGroup, - TensorType = t.TensorType - }) - .ToList() + Kind = RequiredSampleKind.BaseOnlyIsolation, + Key = $"baseonly:{baseline.UniqueId}", + Description = + $"Base-only isolation for {string.Join("/", baseline.Names)} with all known groups forced native.", + Quant = quant, + TestedBaselineId = baseline.UniqueId }); - } - AnsiConsole.MarkupLine($"[bold green]Required pure baseline hybrid tests:[/] {baseTestsRequired:N0}"); + result.BaseOnlyIsolationCount++; + } // --------------------------------------------------------- -// 2. ISOLATION SAMPLES -// --------------------------------------------------------- -// These should use a REAL blanket base quant and then override -// one target group away from that base so llama-quantize actually -// performs hybrid quantization. - - var tensorWeights = TensorWeightScheme.All - .Where(x => x != TensorWeightScheme.NULL && x != TensorWeightScheme.BF16_F16) - .ToList(); - - int isolatedSamplesRequired = 0; + // 3. Carrier base-only isolation for tensor-group probing + // Q8_0 is used as the carrier because llama-quantize actually applies + // mixed tensor overrides correctly on a real quantized output. + // --------------------------------------------------------- + var groupIsolationCarrier = BaselineQuants.Q8_0; -// Pick the real baseline families we want to probe. -// You can expand this later if desired. - var isolationBaselines = BaselineQuants.All - .Where(x => x.BaseConversionBase != null) - .ToList(); + var carrierBaseOnly = HybridQuant.CreateBlanket( + baseQuant: groupIsolationCarrier, + groups: activeGroups, + blanketScheme: TensorWeightScheme.BF16_F16); - foreach (var baseline in isolationBaselines) + result.Plans.Add(new RequiredSamplePlan { - // Map the baseline name to its matching tensor scheme. - // Example: IQ4_XS baseline => IQ4_XS tensor scheme everywhere by default. - var baselineScheme = TensorWeightScheme.All.FirstOrDefault(s => - s.Names.Any(n => baseline.Names.Contains(n, StringComparer.OrdinalIgnoreCase))); + Kind = RequiredSampleKind.BaseOnlyIsolation, + Key = $"carrier-baseonly:{groupIsolationCarrier.UniqueId}", + Description = + $"Carrier base-only isolation for {string.Join("/", groupIsolationCarrier.Names)} with all known groups forced native.", + Quant = carrierBaseOnly, + TestedBaselineId = groupIsolationCarrier.UniqueId + }); - if (baselineScheme == null) - continue; + result.BaseOnlyIsolationCount++; - foreach (var weight in tensorWeights) - { - var validTargets = TReg.All - .Where(x => !missingIds.Contains(x.UniqueId)) - .Where(x => !weight.BannedGroups.Contains(x)) - .ToList(); + // --------------------------------------------------------- + // 4. Tensor-group isolation using the Q8_0 carrier + // --------------------------------------------------------- + var probeSchemes = TensorWeightScheme.All + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .OrderBy(x => x.UniqueId) + .ToList(); - foreach (var group in validTargets) - { - isolatedSamplesRequired++; + foreach (var group in activeGroups) + { + foreach (var scheme in probeSchemes) + { + if (scheme.IsBannedFor(group)) + continue; - var tensors = TReg.All - .Where(g => !missingIds.Contains(g.UniqueId)) - .Select(g => new HybridTensor - { - TGroup = g, - TensorType = baselineScheme - }) - .ToList(); + var quant = HybridQuant.CreateBlanket( + baseQuant: groupIsolationCarrier, + groups: activeGroups, + blanketScheme: TensorWeightScheme.BF16_F16); - var foundQuant = tensors.First(x => x.TGroup.UniqueId == group.UniqueId); - foundQuant.TensorType = weight; + var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); + target.TensorType = scheme; - hybridQuants.Add(new HybridQuant - { - BaseQuant = baseline, - Tensors = tensors - }); - } + result.Plans.Add(new RequiredSamplePlan + { + Kind = RequiredSampleKind.GroupIsolation, + Key = $"group:{groupIsolationCarrier.UniqueId}:{group.UniqueId}:{scheme.UniqueId}", + Description = + $"Carrier-based isolation for group '{group.Name}' using scheme '{scheme.Names[0]}' on base '{groupIsolationCarrier.Names[0]}'.", + Quant = quant, + TargetGroupId = group.UniqueId, + TestedSchemeId = scheme.UniqueId, + TestedBaselineId = groupIsolationCarrier.UniqueId + }); + + result.GroupIsolationCount++; } } - AnsiConsole.MarkupLine($"[bold green]Isolated Samples Required:[/] {isolatedSamplesRequired:N0}"); - AnsiConsole.MarkupLine($"[bold green]Total Samples Required:[/] {hybridQuants.Count:N0}"); + AnsiConsole.MarkupLine($"[bold green]Pure baselines required:[/] {result.PureBaselineCount:N0}"); + AnsiConsole.MarkupLine( + $"[bold green]Base-only isolation samples required:[/] {result.BaseOnlyIsolationCount:N0}"); + AnsiConsole.MarkupLine( + $"[bold green]Tensor-group isolation samples required:[/] {result.GroupIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Total required samples:[/] {result.TotalCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Isolated Samples Required:[/] {isolatedSamplesRequired:N0}"); - AnsiConsole.MarkupLine($"[bold green]Total Samples Required:[/] {hybridQuants.Count:N0}"); + return result; + } - return hybridQuants; + public static List GenerateRequiredDataSampleCombos(List? missingTensorGroups = null) + { + return GenerateRequiredSamplePlan(missingTensorGroups) + .Plans + .Select(x => x.Quant) + .ToList(); } public static IEnumerable> GenerateTensorConfigBatches( @@ -123,9 +153,6 @@ public static IEnumerable> GenerateTensorConfigBatches( if (batchSize <= 0) throw new ArgumentOutOfRangeException(nameof(batchSize)); - // --------------------------- - // Diagnostics / invariants - // --------------------------- if (TReg.All.IsDefault) throw new InvalidOperationException("TensorRegistry.All is default (uninitialized)."); @@ -149,21 +176,12 @@ public static IEnumerable> GenerateTensorConfigBatches( } int dims = allowed.Length; - - // --------------------------- - // Threading setup - // --------------------------- int dop = ComputeWorkerThreads(GetThreadCountSafe()); - - // Cache baseQuant.UniqueId once (perf) byte baseId = baseQuant.UniqueId; var queue = new BlockingCollection>( boundedCapacity: Math.Max(2, dop * 2)); - // --------------------------- - // Producer - // --------------------------- var producer = Task.Run(() => { try @@ -180,9 +198,6 @@ public static IEnumerable> GenerateTensorConfigBatches( var batch = new List(Math.Min(batchSize, 250_000)); var idx = new int[dims]; - // Hot-path aliases (perf) - // NOTE: This assumes group count is stable at 9 (Embeddings..MoeRouter), - // which matches your TensorConfig mapping. var d0 = allowed[0]; var d1 = allowed[1]; var d2 = allowed[2]; @@ -202,7 +217,6 @@ public static IEnumerable> GenerateTensorConfigBatches( while (true) { - // Inline-build (perf): avoids helper call overhead and repeated bounds checks batch.Add(new TensorConfig( baseQuant: baseId, embeddings: d0[idx[0]], @@ -222,7 +236,6 @@ public static IEnumerable> GenerateTensorConfigBatches( batch = new List(Math.Min(batchSize, 250_000)); } - // Mixed-radix increment (dims-1 → 1) int d = dims - 1; while (d >= 1) { @@ -249,9 +262,6 @@ public static IEnumerable> GenerateTensorConfigBatches( } }, ct); - // --------------------------- - // Consumer (yield batches) - // --------------------------- foreach (var batch in queue.GetConsumingEnumerable(ct)) yield return batch; @@ -260,22 +270,19 @@ public static IEnumerable> GenerateTensorConfigBatches( private static int GetThreadCountSafe() { - // Cache.SysInfo might not be initialized this early; fall back safely - int tc = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; - return Math.Max(1, tc); + return Cache.SysInfo?.ThreadCount > 0 + ? Cache.SysInfo.ThreadCount + : Environment.ProcessorCount; } private static int ComputeWorkerThreads(int threadCount) { - if (threadCount <= 1) + if (threadCount <= 4) return 1; - int workers = - threadCount < 16 - ? threadCount - 1 - : (int)Math.Floor(threadCount * 0.90); + if (threadCount <= 12) + return 2; - // Always leave at least 1 thread free - return Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); + return Math.Max(2, threadCount / 6); } } \ No newline at end of file diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index cfde2d6..4158b18 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -111,101 +111,112 @@ public async Task EnsureExecutionPlanAsync( PlanInitLock.Release(); } } - - public async Task ClampStaticNglWithBaseModelAsync( - string baseModelPath, - int discoveryTokenTarget = 8192, - CancellationToken ct = default) -{ - if (string.IsNullOrWhiteSpace(baseModelPath)) - throw new ArgumentException("Base model path was null or empty.", nameof(baseModelPath)); - if (_currentPlan == null) - throw new InvalidOperationException( - "Benchmark execution plan has not been initialized. Call EnsureExecutionPlanAsync() first."); + public async Task ClampStaticNglWithBaseModelAsync( + string baseModelPath, + int discoveryTokenTarget = 8192, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(baseModelPath)) + throw new ArgumentException("Base model path was null or empty.", nameof(baseModelPath)); - if (!_currentPlan.UsesGpu) - return; + if (_currentPlan == null) + throw new InvalidOperationException( + "Benchmark execution plan has not been initialized. Call EnsureExecutionPlanAsync() first."); - await PlanInitLock.WaitAsync(ct); - try - { - if (_currentPlan == null || !_currentPlan.UsesGpu) + if (!_currentPlan.UsesGpu) return; - var slot = _currentPlan.Slots[0]; + await PlanInitLock.WaitAsync(ct); + try + { + if (_currentPlan == null || !_currentPlan.UsesGpu) + return; - string probeRoot = Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe_base"); - Directory.CreateDirectory(probeRoot); + var slot = _currentPlan.Slots[0]; - string probeCorpusDir = Path.Combine(probeRoot, "_ppl_corpora"); - Directory.CreateDirectory(probeCorpusDir); - - string corpusPath = Path.Combine(probeCorpusDir, "ppl_corpus_general.txt"); - await PreparePplCorpusAsync("general", corpusPath, discoveryTokenTarget); + string probeRoot = Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe_base"); + Directory.CreateDirectory(probeRoot); - int startingNgl = _currentPlan.StaticNgl; - int? chosen = null; + string probeCorpusDir = Path.Combine(probeRoot, "_ppl_corpora"); + Directory.CreateDirectory(probeCorpusDir); - AnsiConsole.Write(new Rule("[yellow]Clamping Static ngl With Base Model[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"[grey]Base model:[/] {Markup.Escape(baseModelPath)}"); - AnsiConsole.MarkupLine($"[grey]Starting from Q8-discovered ngl:[/] [cyan]{startingNgl}[/]"); + string corpusPath = Path.Combine(probeCorpusDir, "ppl_corpus_general.txt"); + await PreparePplCorpusAsync("general", corpusPath, discoveryTokenTarget); - foreach (int ngl in NglCandidates.Where(n => n <= startingNgl).OrderByDescending(n => n)) - { - ct.ThrowIfCancellationRequested(); + int startingNgl = _currentPlan.StaticNgl; + int? chosen = null; - AnsiConsole.MarkupLine($"[grey]Base clamp probe:[/] [cyan]ngl={ngl}[/]"); + AnsiConsole.Write(new Rule("[yellow]Clamping Static ngl With Base Model[/]") + { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[grey]Base model:[/] {Markup.Escape(baseModelPath)}"); + AnsiConsole.MarkupLine($"[grey]Starting from Q8-discovered ngl:[/] [cyan]{startingNgl}[/]"); - bool benchOk = await ProbeLlamaBenchAtFixedNglAsync(baseModelPath, slot, ngl, probeRoot); - if (!benchOk) + foreach (int ngl in NglCandidates.Where(n => n <= startingNgl).OrderByDescending(n => n)) { - AnsiConsole.MarkupLine($"[grey] llama-bench failed at ngl={ngl}[/]"); - continue; + ct.ThrowIfCancellationRequested(); + + AnsiConsole.MarkupLine($"[grey]Base clamp probe:[/] [cyan]ngl={ngl}[/]"); + + bool benchOk = await ProbeLlamaBenchAtFixedNglAsync(baseModelPath, slot, ngl, probeRoot); + if (!benchOk) + { + AnsiConsole.MarkupLine($"[grey] llama-bench failed at ngl={ngl}[/]"); + continue; + } + + bool pplOk = await ProbePerplexityAtFixedNglAsync(baseModelPath, slot, ngl, corpusPath, probeRoot); + if (!pplOk) + { + AnsiConsole.MarkupLine($"[grey] perplexity failed at ngl={ngl}[/]"); + continue; + } + + chosen = ngl; + break; } - bool pplOk = await ProbePerplexityAtFixedNglAsync(baseModelPath, slot, ngl, corpusPath, probeRoot); - if (!pplOk) + if (!chosen.HasValue) { - AnsiConsole.MarkupLine($"[grey] perplexity failed at ngl={ngl}[/]"); - continue; - } + AnsiConsole.MarkupLine( + "[yellow]Base model could not sustain the discovered GPU ngl. Falling back to a CPU benchmark plan.[/]"); - chosen = ngl; - break; - } + var cpuPlan = BenchmarkExecutionPlan.CreateCpuPlan(_currentPlan.PlanModelPath); - if (!chosen.HasValue) - { - throw new InvalidOperationException( - $"Could not clamp a stable benchmark ngl for the base model '{baseModelPath}' " + - $"within the discovered Q8 topology."); - } + lock (SlotSync) + { + _currentPlan = cpuPlan; + _availableSlots = new Queue(cpuPlan.Slots); + _slotSemaphore = new SemaphoreSlim(cpuPlan.Slots.Count, cpuPlan.Slots.Count); + } - if (chosen.Value != _currentPlan.StaticNgl) - { - var updated = new BenchmarkExecutionPlan( - planModelPath: _currentPlan.PlanModelPath, - staticNgl: chosen.Value, - usesGpu: _currentPlan.UsesGpu, - groupSize: _currentPlan.GroupSize, - slots: _currentPlan.Slots); + return; + } - lock (SlotSync) + if (chosen.Value != _currentPlan.StaticNgl) { - _currentPlan = updated; - _availableSlots = new Queue(updated.Slots); - _slotSemaphore = new SemaphoreSlim(updated.Slots.Count, updated.Slots.Count); + var updated = new BenchmarkExecutionPlan( + planModelPath: _currentPlan.PlanModelPath, + staticNgl: chosen.Value, + usesGpu: _currentPlan.UsesGpu, + groupSize: _currentPlan.GroupSize, + slots: _currentPlan.Slots); + + lock (SlotSync) + { + _currentPlan = updated; + _availableSlots = new Queue(updated.Slots); + _slotSemaphore = new SemaphoreSlim(updated.Slots.Count, updated.Slots.Count); + } } - } - AnsiConsole.MarkupLine($"[green]Base-model clamped static ngl:[/] [cyan]{chosen.Value}[/]"); - } - finally - { - PlanInitLock.Release(); + AnsiConsole.MarkupLine($"[green]Base-model clamped static ngl:[/] [cyan]{chosen.Value}[/]"); + } + finally + { + PlanInitLock.Release(); + } } -} private async Task BuildExecutionPlanAsync( string q8ModelPath, @@ -619,7 +630,9 @@ public async Task RunAllBenchmarksAsync( if (TryReadExistingBenchmarkArtifacts(benchDir, requestedDomains, requireKld, out var diskResult)) { if (!diskResult.ModelSizeBytes.HasValue || diskResult.ModelSizeBytes.Value == 0) - diskResult.ModelSizeBytes = existingBench.SizeBytes > 0 ? existingBench.SizeBytes : TryGetModelSize(modelPath); + diskResult.ModelSizeBytes = existingBench.SizeBytes > 0 + ? existingBench.SizeBytes + : TryGetModelSize(modelPath); return diskResult; } @@ -655,7 +668,7 @@ public async Task RunAllBenchmarksAsync( { ModelSizeBytes = TryGetModelSize(modelPath) }; - + // Disabled for now. Too many variables that're annoying to track /*string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); if (TryReadExistingLlamaBenchLog(llamaBenchPath, out var existingLlamaBench)) @@ -668,7 +681,7 @@ public async Task RunAllBenchmarksAsync( $"[yellow]Running Llama-Bench[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, effectiveNgl, slot); }*/ - + result.LlamaBench = new LlamaBenchMetrics { LogPath = null, @@ -969,7 +982,7 @@ private bool TryReadExistingBenchmarkArtifacts( // fall through } } - + // not currently requiring llama bench /*string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); if (!TryReadExistingLlamaBenchLog(llamaBenchPath, out var llamaBench)) @@ -979,7 +992,7 @@ private bool TryReadExistingBenchmarkArtifacts( { LlamaBench = llamaBench };*/ - + var rebuilt = new BenchmarkResult { LlamaBench = new LlamaBenchMetrics @@ -1298,7 +1311,7 @@ private async Task RunFixedCommandWithRetryAsync( $"{label} failed after {attempts} attempts on slot {slot.SlotId} ({Markup.Escape(slot.DisplayName)}).\n" + $"{last?.LogOutput}"); } - + private bool LooksLikeSuccessfulPerplexityRun(string logFile, string logContent) { if (string.IsNullOrWhiteSpace(logContent) || logContent.Length < 50) @@ -1315,7 +1328,7 @@ private bool LooksLikeSuccessfulPerplexityRun(string logFile, string logContent) } } - + private static bool LooksLikeRetryableGpuFailure(string logContent) { if (string.IsNullOrWhiteSpace(logContent)) diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs new file mode 100644 index 0000000..14e049c --- /dev/null +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -0,0 +1,330 @@ +using MagicQuant.Helpers; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Microsoft.EntityFrameworkCore; + +namespace MagicQuant.Services; + +public sealed class IsolationOptimizationOptions +{ + public double MinMeaningfulGroupReductionRatio { get; set; } = 0.10d; + public double MinMeaningfulBaseOnlyReductionRatio { get; set; } = 0.01d; +} + +public sealed class IsolationGroupDecision +{ + public string GroupName { get; set; } = string.Empty; + public double BestReductionRatio { get; set; } + public bool LockedToNative { get; set; } + + public string? WinningScheme { get; set; } + public ulong? WinningSizeBytes { get; set; } + public double? WinningKld { get; set; } + public double? WinningPplDelta { get; set; } + + public List Candidates { get; set; } = new(); +} + +public sealed class IsolationOptimizationResult +{ + public int NativeLockedGroups { get; set; } + public int DominatedGroupSchemesBanned { get; set; } + public int DisabledBaselines { get; set; } + + public List Notes { get; set; } = new(); + public List GroupDetails { get; set; } = new(); +} + +public class IsolationOptimizationService +{ + public async Task AnalyzeAndApplyAsync( + RequiredSampleGenerationResult plan, + IsolationOptimizationOptions? options = null, + CancellationToken ct = default) + { + options ??= new IsolationOptimizationOptions(); + + var result = new IsolationOptimizationResult(); + + var nativeBaseline = await LoadSnapshotAsync( + HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct); + + var carrierBaselineId = BaselineQuants.Q8_0.UniqueId; + + var carrierBaseOnlyPlan = plan.Plans.First(x => + x.Kind == RequiredSampleKind.BaseOnlyIsolation && + x.TestedBaselineId == carrierBaselineId); + + var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct); + + // ============================ + // GROUP ISOLATION ANALYSIS + // ============================ + var groupPlans = plan.Plans + .Where(x => x.Kind == RequiredSampleKind.GroupIsolation) + .Where(x => x.TestedBaselineId == carrierBaselineId) + .GroupBy(x => x.TargetGroupId!.Value) + .ToList(); + + foreach (var groupSet in groupPlans) + { + var group = TReg.All.First(x => x.UniqueId == groupSet.Key); + + var snapshots = new List<(RequiredSamplePlan Plan, BenchmarkSnapshot Snapshot)>(); + + foreach (var item in groupSet) + { + var snap = await LoadSnapshotAsync(item.Quant, ct); + if (snap != null) + snapshots.Add((item, snap)); + } + + if (snapshots.Count == 0) + continue; + + var decision = new IsolationGroupDecision + { + GroupName = group.Name + }; + + // compute best reduction vs carrier + double maxReduction = snapshots + .Select(x => ComputeReductionRatio(carrierBaseOnly.SizeBytes, x.Snapshot.SizeBytes)) + .Max(); + + decision.BestReductionRatio = maxReduction; + + // build candidate debug list + foreach (var snap in snapshots.OrderBy(x => x.Snapshot.SizeBytes)) + { + var scheme = TensorWeightScheme.All + .FirstOrDefault(x => x.UniqueId == snap.Plan.TestedSchemeId); + + var reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.Snapshot.SizeBytes); + var kld = GetAggregateKld(snap.Snapshot); + var pplDelta = GetAggregatePplDelta(snap.Snapshot, nativeBaseline); + + decision.Candidates.Add( + $"{scheme?.Names[0] ?? "Unknown"} | size={(snap.Snapshot.SizeBytes / 1024.0 / 1024.0):F2}MB | reduction={reduction:P2} | kld={kld:G6} | pplΔ={pplDelta:P4}"); + } + + // LOCK LOGIC + if (maxReduction < options.MinMeaningfulGroupReductionRatio) + { + RuntimeSearchSpace.LockGroupToNative(group); + + decision.LockedToNative = true; + result.NativeLockedGroups++; + + result.Notes.Add( + $"Locked '{group.Name}' to native (max reduction {maxReduction:P2})"); + + result.GroupDetails.Add(decision); + continue; + } + + decision.LockedToNative = false; + + // FIND WINNER + var ordered = snapshots + .OrderBy(x => GetAggregateKld(x.Snapshot)) + .ThenBy(x => GetAggregatePplDelta(x.Snapshot, nativeBaseline)) + .ToList(); + + var winner = ordered.First(); + + var winnerScheme = TensorWeightScheme.All + .FirstOrDefault(x => x.UniqueId == winner.Plan.TestedSchemeId); + + decision.WinningScheme = winnerScheme?.Names[0]; + decision.WinningSizeBytes = winner.Snapshot.SizeBytes; + decision.WinningKld = GetAggregateKld(winner.Snapshot); + decision.WinningPplDelta = GetAggregatePplDelta(winner.Snapshot, nativeBaseline); + + // BAN LOSERS WITH SAME SIZE + foreach (var sizeBucket in snapshots.GroupBy(x => x.Snapshot.SizeBytes)) + { + if (sizeBucket.Count() <= 1) + continue; + + var sorted = sizeBucket + .OrderBy(x => GetAggregateKld(x.Snapshot)) + .ThenBy(x => GetAggregatePplDelta(x.Snapshot, nativeBaseline)) + .ToList(); + + foreach (var loser in sorted.Skip(1)) + { + var scheme = TensorWeightScheme.All + .FirstOrDefault(x => x.UniqueId == loser.Plan.TestedSchemeId); + + if (scheme == null) + continue; + + if (!scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) + { + scheme.BannedGroups.Add(group); + result.DominatedGroupSchemesBanned++; + + result.Notes.Add( + $"Banned {scheme.Names[0]} for {group.Name} (same size, worse fidelity)"); + } + } + } + + result.GroupDetails.Add(decision); + } + + // ============================ + // BASELINE PRUNING + // ============================ + var baseOnlyPlans = plan.Plans + .Where(x => x.Kind == RequiredSampleKind.BaseOnlyIsolation) + .Where(x => x.Key.StartsWith("baseonly:", StringComparison.Ordinal)) + .ToList(); + + foreach (var item in baseOnlyPlans) + { + var snap = await LoadSnapshotAsync(item.Quant, ct); + if (snap == null) continue; + + double reduction = ComputeReductionRatio(nativeBaseline.SizeBytes, snap.SizeBytes); + + if (reduction < options.MinMeaningfulBaseOnlyReductionRatio) + { + var baseline = BaselineQuants.FromId(item.TestedBaselineId!.Value); + + if (RuntimeSearchSpace.DisableCombinationBaseline(baseline)) + { + result.DisabledBaselines++; + result.Notes.Add($"Disabled baseline {baseline.Names[0]} (reduction {reduction:P2})"); + } + } + } + + return result; + } + + // ============================ + // HELPERS (unchanged) + // ============================ + + private async Task LoadSnapshotAsync(HybridQuant quant, CancellationToken ct) + { + await using var db = new MagicQuantContext(); + + var model = await db.AiModelHashes + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (model == null) return null; + + var lookup = BuildLookup(quant); + + var row = await db.AiBenchmarks + .Include(x => x.CategorBenchmarks) + .Join(db.TensorCombos, + b => b.TensorComboId, + c => c.Id, + (b, c) => new { b, c }) + .FirstOrDefaultAsync(x => + x.b.AiModelHashId == model.Id && + x.c.BaseQuant == lookup.BaseQuant && + x.c.Embeddings == lookup.Embeddings && + x.c.LmHead == lookup.LmHead && + x.c.AttnQ == lookup.AttnQ && + x.c.AttnKV == lookup.AttnKV && + x.c.AttnOutput == lookup.AttnOutput && + x.c.FfnUpGate == lookup.FfnUpGate && + x.c.FfnDown == lookup.FfnDown && + x.c.MoeExperts == lookup.MoeExperts && + x.c.MoeRouter == lookup.MoeRouter, + ct); + + if (row == null) return null; + + var snapshot = new BenchmarkSnapshot { SizeBytes = row.b.SizeBytes }; + + foreach (var cat in row.b.CategorBenchmarks) + { + snapshot.Domains[cat.Category.ToString()] = new BenchmarkDomainSnapshot + { + Kld = cat.Kld, + Ppl = cat.Ppl, + PplError = cat.PplError + }; + } + + return snapshot; + } + + private static double ComputeReductionRatio(ulong nativeSize, ulong candidateSize) + => (nativeSize == 0 || candidateSize >= nativeSize) + ? 0d + : (nativeSize - candidateSize) / (double)nativeSize; + + private static double GetAggregateKld(BenchmarkSnapshot s) + => s.Domains.Values.Where(x => x.Kld.HasValue).Select(x => x.Kld!.Value).DefaultIfEmpty(double.MaxValue).Average(); + + private static double GetAggregatePplDelta(BenchmarkSnapshot s, BenchmarkSnapshot n) + { + var list = new List(); + + foreach (var kv in s.Domains) + { + if (!n.Domains.TryGetValue(kv.Key, out var native)) continue; + if (native.Ppl <= 0) continue; + + list.Add(Math.Abs(kv.Value.Ppl - native.Ppl) / native.Ppl); + } + + return list.Count == 0 ? double.MaxValue : list.Average(); + } + + private static TensorLookup BuildLookup(HybridQuant quant) + { + byte Get(TensorGroup g) => + quant.Tensors?.FirstOrDefault(x => x.TGroup.UniqueId == g.UniqueId)?.TensorType.UniqueId ?? (byte)0; + + return new TensorLookup + { + BaseQuant = quant.BaseQuant.UniqueId, + Embeddings = Get(TReg.Embeddings), + LmHead = Get(TReg.LmHead), + AttnQ = Get(TReg.AttnQ), + AttnKV = Get(TReg.AttnKV), + AttnOutput = Get(TReg.AttnOutput), + FfnUpGate = Get(TReg.FfnUpGate), + FfnDown = Get(TReg.FfnDown), + MoeExperts = Get(TReg.MoeExperts), + MoeRouter = Get(TReg.MoeRouter) + }; + } + + private sealed class TensorLookup + { + public byte BaseQuant; + public byte Embeddings; + public byte LmHead; + public byte AttnQ; + public byte AttnKV; + public byte AttnOutput; + public byte FfnUpGate; + public byte FfnDown; + public byte MoeExperts; + public byte MoeRouter; + } + + private sealed class BenchmarkSnapshot + { + public ulong SizeBytes; + public Dictionary Domains = new(); + } + + private sealed class BenchmarkDomainSnapshot + { + public double? Kld; + public double Ppl; + public double PplError; + } +} \ No newline at end of file diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index cb4d936..d495a6f 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -22,6 +22,10 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) if (!File.Exists(ggufPath)) throw new FileNotFoundException($"Base model not found at {ggufPath}"); + RuntimeSearchSpace.ResetForNewModel(); + Cache.UnusedTensorGroups.Clear(); + TensorWeightScheme.NULL.BannedGroups.Clear(); + string directory = Path.GetDirectoryName(ggufPath)!; string scriptPath = Path.Combine(directory, "check_compat.py"); string resultPath = Path.Combine(directory, "compat_results.json"); @@ -29,9 +33,8 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) try { - // 1. Prepare Data var groupDefinitions = TReg.All.ToDictionary(g => g.Name, g => g.Tensors); - + var blockRequirements = TensorWeightScheme.All .Where(s => s.BlockNeo.HasValue) .ToDictionary(s => s.Names[0], s => s.BlockNeo!.Value); @@ -44,22 +47,18 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) schemes = blockRequirements }; - // 2. Generate Python Script (With Shape Debugging) string pyCode = GeneratePythonScript(JsonSerializer.Serialize(payload)); await File.WriteAllTextAsync(scriptPath, pyCode); - // 3. Run Inspection AnsiConsole.MarkupLine("[grey]Inspecting GGUF structure...[/]"); await _pyManager.RunPythonScriptAsync(scriptPath); - // 4. Validate Result if (!File.Exists(resultPath)) throw new Exception("Compatibility script finished but produced no result file."); string jsonResult = await File.ReadAllTextAsync(resultPath); - - // Handle script errors - if (jsonResult.Contains("\"Error\"")) + + if (jsonResult.Contains("\"Error\"", StringComparison.Ordinal)) { var errorRes = JsonSerializer.Deserialize(jsonResult); if (!string.IsNullOrEmpty(errorRes?.Error)) @@ -67,103 +66,114 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) } var result = JsonSerializer.Deserialize(jsonResult); - if (result == null) return; - - // --------------------------------------------------------- - // 5. Global State Update Logic - // --------------------------------------------------------- - - TensorWeightScheme.NULL.BannedGroups.Clear(); - Cache.UnusedTensorGroups.Clear(); + if (result == null) + return; int unusedCount = 0; int usedCount = 0; + int shapeBanCount = 0; + int nativeLockedCount = 0; + + var shapeTable = new Table().Border(TableBorder.Rounded).Title("[red]Shape Incompatibilities[/]"); + shapeTable.AddColumn("Group"); + shapeTable.AddColumn("Scheme"); + shapeTable.AddColumn("Reason"); foreach (var group in TReg.All) { - bool exists = result.FoundGroups.Contains(group.Name); + bool exists = result.FoundGroups.Contains(group.Name, StringComparer.OrdinalIgnoreCase); if (exists) { - if (!TensorWeightScheme.NULL.BannedGroups.Contains(group)) - { + if (!TensorWeightScheme.NULL.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) TensorWeightScheme.NULL.BannedGroups.Add(group); - } + usedCount++; + continue; } - else + + unusedCount++; + Cache.UnusedTensorGroups.Add(group); + + foreach (var scheme in TensorWeightScheme.All) { - unusedCount++; - Cache.UnusedTensorGroups.Add(group); - - foreach (var scheme in TensorWeightScheme.All) - { - if (scheme == TensorWeightScheme.NULL) continue; - if (!scheme.BannedGroups.Contains(group)) scheme.BannedGroups.Add(group); - } + if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId) + continue; + + if (!scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) + scheme.BannedGroups.Add(group); } } - // --------------------------------------------------------- - // 6. Handle Shape Restrictions - // --------------------------------------------------------- - int shapeBanCount = 0; - var table = new Table().Border(TableBorder.Rounded).Title("[red]Shape Incompatibilities[/]"); - table.AddColumn("Group"); - table.AddColumn("Scheme"); - table.AddColumn("Reason"); - foreach (var failure in result.Incompatible) { var group = TReg.GetByName(failure.Group); - var scheme = TensorWeightScheme.All.FirstOrDefault(s => s.Names.Contains(failure.Scheme)); + var scheme = TensorWeightScheme.All.FirstOrDefault(s => + s.Names.Any(n => n.Equals(failure.Scheme, StringComparison.OrdinalIgnoreCase))); + + if (group == null || scheme == null) + continue; + + if (scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) + continue; + + scheme.BannedGroups.Add(group); + shapeBanCount++; + shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{scheme.Names[0]}[/]", "[grey]Block Alignment[/]"); + } - if (group != null && scheme != null) + foreach (var group in TReg.All.Except(Cache.UnusedTensorGroups)) + { + bool anyNonNativeOptionLeft = TensorWeightScheme.All + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Any(x => !x.IsBannedFor(group)); + + if (!anyNonNativeOptionLeft) { - if (!scheme.BannedGroups.Contains(group)) - { - scheme.BannedGroups.Add(group); - shapeBanCount++; - table.AddRow($"[blue]{group.Name}[/]", $"[yellow]{scheme.Names[0]}[/]", "[grey]Block Alignment[/]"); - } + RuntimeSearchSpace.LockGroupToNative(group); + nativeLockedCount++; } } - // --------------------------------------------------------- - // 7. Report - // --------------------------------------------------------- - AnsiConsole.MarkupLine($"[green]✔[/] Analysis Complete."); + AnsiConsole.MarkupLine("[green]✔[/] Analysis Complete."); AnsiConsole.MarkupLine($" Active Groups: [bold cyan]{usedCount}[/]"); - + if (unusedCount > 0) { string unusedNames = string.Join(", ", Cache.UnusedTensorGroups.Select(g => g.Name)); AnsiConsole.MarkupLine($" Unused Groups: [grey]{unusedNames}[/] (Forced to NULL)"); } + if (nativeLockedCount > 0) + { + string locked = string.Join(", ", RuntimeSearchSpace.GetNativeLockedGroups().Select(x => x.Name)); + AnsiConsole.MarkupLine($" Native-Locked Groups: [yellow]{locked}[/]"); + } + if (shapeBanCount > 0) { - AnsiConsole.Write(table); - AnsiConsole.MarkupLine($"[yellow]Applied {shapeBanCount} restrictions due to tensor shapes.[/]"); + AnsiConsole.Write(shapeTable); + AnsiConsole.MarkupLine($"[yellow]Applied {shapeBanCount} shape-based restrictions.[/]"); } else { AnsiConsole.MarkupLine("[green]No shape-based restrictions found.[/]"); } - + AnsiConsole.WriteLine(); } finally { if (File.Exists(scriptPath)) File.Delete(scriptPath); if (File.Exists(resultPath)) File.Delete(resultPath); - // Debug path is kept for inspection + _ = debugPath; } } private string GeneratePythonScript(string jsonPayload) - { - return $@" +{ + return $@" import sys import json import re @@ -175,7 +185,7 @@ import re def write_error(msg): with open(output_path, 'w') as f: - json.dump({{'FoundGroups': [], 'Incompatible': [], 'Error': msg}}, f) + json.dump({{""FoundGroups"": [], ""Incompatible"": [], ""Error"": msg}}, f) sys.exit(0) try: @@ -195,13 +205,12 @@ import gguf failures = [] debug_lines = [] -debug_lines.append(f'Inspecting {{len(tensor_names)}} tensors against {{len(config[""schemes""])}} block requirements.') +debug_lines.append('Inspecting ' + str(len(tensor_names)) + ' tensors against ' + str(len(config[""schemes""])) + ' block requirements.') -# 1. Match Groups for g_name, patterns in config['groups'].items(): matched = [] first_reason = None - + for pat in patterns: try: regex = re.compile(pat) @@ -209,61 +218,53 @@ import gguf if regex.fullmatch(t): matched.append(t) if not first_reason: - first_reason = f""Match: '{{pat}}' -> '{{t}}'"" + first_reason = ""Match: '"" + pat + ""' -> '"" + t + ""'"" except: continue - + if matched: found_groups.append(g_name) - debug_lines.append(f""[FOUND] {{g_name}} ({{len(matched)}} tensors). {{first_reason}}"") - - # 2. Check Compatibility (Only if found) + debug_lines.append(""[FOUND] "" + g_name + "" ("" + str(len(matched)) + "" tensors). "" + str(first_reason)) weights = [t for t in matched if t.endswith('.weight')] - + if weights: - # Check against every scheme that has a block req for scheme, block_size in config['schemes'].items(): is_valid = True - + for w_name in weights: t_obj = tensors_map[w_name] - ne0 = t_obj.shape[0] # GGUF ne0 + ne0 = t_obj.shape[0] n_dims = len(t_obj.shape) - # Rule A: Non-2D if n_dims != 2: is_valid = False - debug_lines.append(f"" [FAIL] {{g_name}} vs {{scheme}}: {{w_name}} is {{n_dims}}D (Required 2D)"") + debug_lines.append("" [FAIL] "" + g_name + "" vs "" + scheme + "": "" + w_name + "" is "" + str(n_dims) + ""D (Required 2D)"") break - - # Rule B: Modulo + if ne0 % block_size != 0: is_valid = False - # Explicit debug for math check - debug_lines.append(f"" [FAIL] {{g_name}} vs {{scheme}} (Block {{block_size}}): {{w_name}} ne0={{ne0}}. {{ne0}} % {{block_size}} = {{ne0 % block_size}}"") + debug_lines.append("" [FAIL] "" + g_name + "" vs "" + scheme + "" (Block "" + str(block_size) + ""): "" + w_name + "" ne0="" + str(ne0) + "". Remainder="" + str(ne0 % block_size)) break - + if not is_valid: - failures.append({{ 'Group': g_name, 'Scheme': scheme }}) + failures.append({{""Group"": g_name, ""Scheme"": scheme}}) else: - debug_lines.append(f""[MISSING] {{g_name}}"") + debug_lines.append(""[MISSING] "" + g_name) -# Write Debug try: with open(debug_path, 'w') as f: - f.write('\n'.join(debug_lines)) + f.write('\\n'.join(debug_lines)) except: pass -# Write Result with open(output_path, 'w') as f: json.dump({{ - 'FoundGroups': found_groups, - 'Incompatible': failures, - 'Error': None + ""FoundGroups"": found_groups, + ""Incompatible"": failures, + ""Error"": None }}, f, indent=2) "; - } +} private class CompatResult { @@ -274,7 +275,7 @@ private class CompatResult private class CompatFailure { - public string Group { get; set; } = ""; - public string Scheme { get; set; } = ""; + public string Group { get; set; } = string.Empty; + public string Scheme { get; set; } = string.Empty; } } \ No newline at end of file diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 80c1f8e..141df3b 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -27,7 +27,7 @@ private static string GetDuckDbDirectory() private string ConnectionString => $"Data Source={Path.Combine(GetDuckDbDirectory(), DbFileName)}"; - public async Task InitializeAsync(CancellationToken ct = default) + public async Task InitializeAsync(bool forceRebuild = false, CancellationToken ct = default) { var duckDbDirectory = GetDuckDbDirectory(); Directory.CreateDirectory(duckDbDirectory); @@ -41,9 +41,9 @@ public async Task InitializeAsync(CancellationToken ct = default) AnsiConsole.MarkupLine( $"[bold]DuckDB Check:[/] Current Rows: [cyan]{currentDbCount:N0}[/] | Expected: [yellow]{expectedTotal:N0}[/]"); - if (currentDbCount != expectedTotal) + if (forceRebuild || currentDbCount != expectedTotal) { - AnsiConsole.MarkupLine("[bold red]DuckDB empty, mismatch, or new.[/] Initializing/Rebuilding..."); + AnsiConsole.MarkupLine("[bold red]DuckDB empty, mismatch, forced, or stale.[/] Initializing/Rebuilding..."); await RebuildDatabaseAsync(connection, expectedTotal, ct); } else @@ -52,6 +52,11 @@ public async Task InitializeAsync(CancellationToken ct = default) } } + public async Task RebuildAsync(CancellationToken ct = default) + { + await InitializeAsync(forceRebuild: true, ct: ct); + } + private async Task GetRowCountAsync(DuckDBConnection connection, CancellationToken ct) { var checkCmd = connection.CreateCommand(); @@ -91,17 +96,14 @@ MoeRouter TINYINT await createCmd.ExecuteNonQueryAsync(ct); long insertedTotal = 0; - - var bases = BaselineQuants.All - .Where(b => b.BaseConversionBase != null) - .ToList(); + var bases = RuntimeSearchSpace.GetActiveCombinationBaselines(); AnsiConsole.MarkupLine($"[grey]Starting bulk insert of {expectedTotal:N0} rows...[/]"); - foreach (var b in bases) + foreach (var baseline in bases) { foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches( - b, + baseline, batchSize: 1_000_000, ct: ct)) { diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index b3e97ec..211c7ad 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; @@ -18,12 +19,23 @@ public enum SampleProcessState Failed = 3 } +public sealed class SampleProcessingRecord +{ + public RequiredSamplePlan Plan { get; set; } = default!; + public SampleProcessState State { get; set; } + public string ModelName { get; set; } = string.Empty; + public uint? TensorComboId { get; set; } + public uint? BenchmarkId { get; set; } + public string? Error { get; set; } +} + public sealed class SampleProcessingSummary { public int Requested { get; set; } public int Completed { get; set; } public int Skipped { get; set; } public int Failed { get; set; } + public List Records { get; set; } = new(); } public class QuantizationService @@ -74,25 +86,56 @@ public async Task ProcessHybridBatchAsync( if (quants == null) throw new ArgumentNullException(nameof(quants)); + var shimmedPlans = quants + .Select((quant, index) => new RequiredSamplePlan + { + Kind = RequiredSampleKind.GroupIsolation, + Key = $"legacy:{index}", + Description = "Legacy batch item", + Quant = quant + }) + .ToList(); + + return await ProcessHybridBatchAsync(shimmedPlans, ct); + } + + public async Task ProcessHybridBatchAsync( + IReadOnlyCollection plans, + CancellationToken ct = default) + { + if (plans == null) + throw new ArgumentNullException(nameof(plans)); + int completed = 0; int skipped = 0; int failed = 0; + var records = new ConcurrentBag(); - // Warm the base model file once so workers don't all race into conversion. await EnsureBaseModelFileAsync(false); await Parallel.ForEachAsync( - quants, + plans, new ParallelOptions { MaxDegreeOfParallelism = _maxConcurrentQuantizations, CancellationToken = ct }, - async (quant, token) => + async (plan, token) => { + var record = new SampleProcessingRecord + { + Plan = plan, + ModelName = GenerateHybridName(plan.Quant) + }; + try { - var state = await ProcessHybridQuantAsync(quant, token); + var state = await ProcessHybridQuantAsync(plan.Quant, token); + record.State = state; + + var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, token); + record.TensorComboId = identity.TensorComboId; + record.BenchmarkId = identity.BenchmarkId; switch (state) { @@ -109,21 +152,75 @@ await Parallel.ForEachAsync( } catch (Exception ex) { + record.State = SampleProcessState.Failed; + record.Error = ex.Message; Interlocked.Increment(ref failed); - AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(GenerateHybridName(quant))}"); + + AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(record.ModelName)}"); AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); } + finally + { + records.Add(record); + } }); return new SampleProcessingSummary { - Requested = quants.Count, + Requested = plans.Count, Completed = completed, Skipped = skipped, - Failed = failed + Failed = failed, + Records = records.OrderBy(x => x.Plan.Key).ToList() }; } + private async Task<(uint? TensorComboId, uint? BenchmarkId)> ResolveBenchmarkIdentityAsync( + HybridQuant quant, + CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); + + var lookup = BuildTensorLookup(quant); + + await using var db = new MagicQuantContext(); + + var model = await db.AiModelHashes + .AsNoTracking() + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (model == null) + return (null, null); + + var comboId = await db.TensorCombos + .AsNoTracking() + .Where(x => + x.BaseQuant == lookup.BaseQuant && + x.Embeddings == lookup.Embeddings && + x.LmHead == lookup.LmHead && + x.AttnQ == lookup.AttnQ && + x.AttnKV == lookup.AttnKV && + x.AttnOutput == lookup.AttnOutput && + x.FfnUpGate == lookup.FfnUpGate && + x.FfnDown == lookup.FfnDown && + x.MoeExperts == lookup.MoeExperts && + x.MoeRouter == lookup.MoeRouter) + .Select(x => x.Id) + .FirstOrDefaultAsync(ct); + + if (comboId == 0) + return (null, null); + + var benchmarkId = await db.AiBenchmarks + .AsNoTracking() + .Where(x => x.AiModelHashId == model.Id && x.TensorComboId == comboId) + .Select(x => x.Id) + .FirstOrDefaultAsync(ct); + + return (comboId, benchmarkId == 0 ? null : benchmarkId); + } + public async Task ProcessHybridQuantAsync( HybridQuant quant, CancellationToken ct = default) @@ -578,15 +675,20 @@ private static string ResolveQuantizeBaseArgument( concreteOverrides.Count > 0) { throw new InvalidOperationException( - "Selective tensor overrides cannot use a native-source base quant (BF16/F16/F32). " + - "A real base quant such as Q8_0, Q6_K, Q5_K, Q4_K_M, or IQ4_XS must be provided."); + "Native BF16/F16/F32 + tensor overrides is disabled. " + + "In this build of llama-quantize it produced no-op outputs for isolation tests. " + + "Use a real carrier baseline (Q8_0 recommended), force all known groups to BF16/F16, " + + "and quantize only the target group."); } return ResolveBaseName(quant.BaseQuant); } - + private static TensorWeightScheme? TryResolveBaseTensorScheme(BaselineQuants baseQuant) { + if (baseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId) + return TensorWeightScheme.BF16_F16; + if (baseQuant.Names.IsDefaultOrEmpty) return null; From fb00f8fa02f4a3927857022fcdb54ce74660f7f2 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 14 Apr 2026 16:48:56 -0400 Subject: [PATCH 046/258] adding isSmallest and removing MXFP4 as an option right now. --- MQ.DB/Models/BaselineQuants.cs | 8 ++++---- MQ.DB/Models/TensorWeightScheme.cs | 16 ++++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 247712a..0bddded 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -15,7 +15,7 @@ public record BaselineQuants( public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"]); public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"]); - public static readonly BaselineQuants MXFP4_MOE = new( + /*public static readonly BaselineQuants MXFP4_MOE = new( 4, false, ["MXFP4_MOE"], @@ -29,7 +29,7 @@ public record BaselineQuants( TensorType = TensorWeightScheme.MXFP4 }) .ToList() - }); + });*/ public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"]); @@ -59,7 +59,7 @@ public record BaselineQuants( Q6_K, Q5_K, Q4_K_M, - MXFP4_MOE, + //MXFP4_MOE, IQ4_NL, IQ4_XS, //IQ3_M, @@ -68,7 +68,7 @@ public record BaselineQuants( static BaselineQuants() { - MXFP4_MOE.BaseConversionBase!.BaseQuant = MXFP4_MOE; + //MXFP4_MOE.BaseConversionBase!.BaseQuant = MXFP4_MOE; IQ4_XS.BaseConversionBase!.BaseQuant = IQ4_XS; } diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 9a7fd3b..791f4c7 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -11,19 +11,22 @@ public sealed class TensorWeightScheme public ImmutableArray Names { get; } public List BannedGroups { get; } public ushort? BlockNeo { get; } + public bool IsSmallest { get; } private TensorWeightScheme( byte uniqueId, bool requiresImatrix, ImmutableArray names, IEnumerable bannedGroups, - ushort? blockNeo) + ushort? blockNeo, + bool isSmallest = false) { UniqueId = uniqueId; RequiresImatrix = requiresImatrix; Names = names; BlockNeo = blockNeo; - + IsSmallest = isSmallest; + var distinctGroups = bannedGroups .GroupBy(x => x.UniqueId) .Select(x => x.First()) @@ -68,7 +71,7 @@ public static void ResetAllRuntimeBans() Array.Empty(), null); - public static readonly TensorWeightScheme MXFP4 = + /*public static readonly TensorWeightScheme MXFP4 = new( 2, false, @@ -79,7 +82,7 @@ public static void ResetAllRuntimeBans() TReg.MoeRouter, TReg.MoeExperts }, - 32); + 32);*/ public static readonly TensorWeightScheme Q8_0 = new(3, false, ["Q8_0"], Array.Empty(), null); @@ -101,7 +104,8 @@ public static void ResetAllRuntimeBans() false, ["IQ4_XS"], new[] { TReg.MoeRouter }, - 32); + 32, + true); /* public static TensorWeightScheme IQ4_NL = @@ -206,7 +210,7 @@ public static void ResetAllRuntimeBans() [ NULL, BF16_F16, - MXFP4, + // MXFP4, Q8_0, Q6_K, Q5_K, From 2b234c1585f52799f017fbffa84226070b0ba695 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 14 Apr 2026 18:24:18 -0400 Subject: [PATCH 047/258] Huge pruning changes in a good way! More options now too! --- MQ.DB/Models/IsolationRules.cs | 34 ++ MQ.DB/Models/RequiredSamplePlan.cs | 21 ++ MQ.DB/Models/TensorWeightScheme.cs | 64 +++- MagicQuant/Commands/Evolution.cs | 127 +++++-- MagicQuant/Helpers/ComboLogic.cs | 17 +- MagicQuant/Helpers/RuntimeSearchSpace.cs | 56 ++- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 29 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 270 ++++++++------ .../Services/IsolationOptimizationService.cs | 341 +++++++++++++----- .../Services/ModelCompatibilityService.cs | 37 +- 10 files changed, 687 insertions(+), 309 deletions(-) create mode 100644 MQ.DB/Models/IsolationRules.cs diff --git a/MQ.DB/Models/IsolationRules.cs b/MQ.DB/Models/IsolationRules.cs new file mode 100644 index 0000000..6b22ce9 --- /dev/null +++ b/MQ.DB/Models/IsolationRules.cs @@ -0,0 +1,34 @@ +namespace MQ.DB.Models; + +public static class IsolationRules +{ + /// + /// If the smallest explicit non-imatrix probe for a tensor group cannot shrink + /// the total model by at least this much versus the carrier-base-only sample, + /// we stop isolated explicit quant exploration for that group for this run. + /// + public const double MinimumIsolationReductionToContinue = 0.04d; + + /// + /// If a base-only baseline sample only shrinks the model by less than this versus + /// the native BF16/F16/F32 source, it can be disabled as a future combination baseline. + /// + public const double MinimumMeaningfulBaseOnlyReductionRatio = 0.01d; + + /// + /// Hard damage cutoff for isolated tensor options. + /// 5% = 0.05 ratio. + /// + public const double MaximumIsolationPplDeltaRatio = 0.05d; + + /// + /// Hard KLD cutoff for isolated tensor options. + /// + public const double MaximumIsolationKld = 0.10d; + + /// + /// Used when comparing floating-point metrics so near-identical values do not + /// cause unstable eliminations. + /// + public const double MetricComparisonEpsilon = 1e-9d; +} \ No newline at end of file diff --git a/MQ.DB/Models/RequiredSamplePlan.cs b/MQ.DB/Models/RequiredSamplePlan.cs index 6087654..e4b65a4 100644 --- a/MQ.DB/Models/RequiredSamplePlan.cs +++ b/MQ.DB/Models/RequiredSamplePlan.cs @@ -28,4 +28,25 @@ public sealed class RequiredSampleGenerationResult public int GroupIsolationCount { get; set; } public int TotalCount => Plans.Count; + + public void AppendFrom(RequiredSampleGenerationResult other) + { + if (other == null) + return; + + Plans.AddRange(other.Plans); + PureBaselineCount += other.PureBaselineCount; + BaseOnlyIsolationCount += other.BaseOnlyIsolationCount; + GroupIsolationCount += other.GroupIsolationCount; + } + + public static RequiredSampleGenerationResult Merge(params RequiredSampleGenerationResult[] items) + { + var merged = new RequiredSampleGenerationResult(); + + foreach (var item in items) + merged.AppendFrom(item); + + return merged; + } } \ No newline at end of file diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 791f4c7..958a9af 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -26,7 +26,7 @@ private TensorWeightScheme( Names = names; BlockNeo = blockNeo; IsSmallest = isSmallest; - + var distinctGroups = bannedGroups .GroupBy(x => x.UniqueId) .Select(x => x.First()) @@ -55,6 +55,43 @@ public static void ResetAllRuntimeBans() scheme.ResetRuntimeBans(); } + public static TensorWeightScheme GetSmallestNonImatrix() + { + ValidateSmallestConfiguration(); + + return All.Single(x => + !x.RequiresImatrix && + x.UniqueId != NULL.UniqueId && + x.UniqueId != BF16_F16.UniqueId && + x.IsSmallest); + } + + public static void ValidateSmallestConfiguration() + { + var marked = All + .Where(x => !x.RequiresImatrix) + .Where(x => x.UniqueId != NULL.UniqueId) + .Where(x => x.UniqueId != BF16_F16.UniqueId) + .Where(x => x.IsSmallest) + .ToList(); + + if (marked.Count != 1) + { + throw new InvalidOperationException( + $"Exactly one non-imatrix TensorWeightScheme must have IsSmallest=true. Found {marked.Count}. " + + $"Marked: [{string.Join(", ", marked.Select(x => x.Names[0]))}]"); + } + } + + public static IReadOnlyList GetExplicitSchemes(bool includeImatrix = true) + { + return All + .Where(x => x.UniqueId != NULL.UniqueId) + .Where(x => x.UniqueId != BF16_F16.UniqueId) + .Where(x => includeImatrix || !x.RequiresImatrix) + .ToList(); + } + public static readonly TensorWeightScheme NULL = new( 0, @@ -106,7 +143,7 @@ public static void ResetAllRuntimeBans() new[] { TReg.MoeRouter }, 32, true); - + /* public static TensorWeightScheme IQ4_NL = new( @@ -114,7 +151,7 @@ public static void ResetAllRuntimeBans() false, ["IQ4_NL"], new[] { TReg.MoeRouter }, - 32 + 32 ); public static TensorWeightScheme IQ3_S = @@ -128,7 +165,7 @@ public static void ResetAllRuntimeBans() TReg.LmHead, TReg.MoeRouter }, - 32 + 32 ); public static TensorWeightScheme IQ3_XS = @@ -142,7 +179,7 @@ public static void ResetAllRuntimeBans() TReg.LmHead, TReg.MoeRouter }, - 32 + 32 ); public static TensorWeightScheme IQ3_XXS = @@ -156,7 +193,7 @@ public static void ResetAllRuntimeBans() TReg.LmHead, TReg.MoeRouter }, - 32 + 32 ); public static TensorWeightScheme IQ2_S = @@ -171,7 +208,7 @@ public static void ResetAllRuntimeBans() TReg.MoeRouter, TReg.MoeExperts }, - 32 + 32 ); public static TensorWeightScheme IQ2_XS = @@ -186,7 +223,7 @@ public static void ResetAllRuntimeBans() TReg.MoeRouter, TReg.MoeExperts }, - 32 + 32 ); public static TensorWeightScheme IQ2_XXS = @@ -202,7 +239,7 @@ public static void ResetAllRuntimeBans() TReg.MoeExperts, TReg.AttnKV }, - 32 + 32 ); */ @@ -210,10 +247,17 @@ public static void ResetAllRuntimeBans() [ NULL, BF16_F16, - // MXFP4, + // MXFP4, Q8_0, Q6_K, Q5_K, IQ4_XS, + // IQ4_NL, + // IQ3_S, + // IQ3_XS, + // IQ3_XXS, + // IQ2_S, + // IQ2_XS, + // IQ2_XXS ]; } \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index f4439c8..63c1dd1 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -53,11 +53,14 @@ public async Task Run(List args) if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); + TensorWeightScheme.ValidateSmallestConfiguration(); + AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); AnsiConsole.Write(new Rule("[yellow]Evolution Configuration[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"Model Path: [blue]{Cache.ModelDirectory}[/]"); AnsiConsole.MarkupLine($"Output Path: [blue]{Cache.ModelMagicQuantDirectory}[/]"); AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length}[/] safe tensors"); + AnsiConsole.MarkupLine($"Smallest non-imatrix scheme: [cyan]{TensorWeightScheme.GetSmallestNonImatrix().Names[0]}[/]"); if (string.IsNullOrEmpty(Cache.LlamaBin)) AnsiConsole.MarkupLine("[yellow]Warning: Llama binaries path not set in Cache. (Did Initialization run?)[/]"); @@ -98,58 +101,119 @@ await benchmarkService.RunAllBenchmarksAsync( var dbService = new QuantDatabaseService(); await dbService.InitializeAsync(); - AnsiConsole.Write(new Rule("[yellow]Required Sample Generation[/]") { Justification = Justify.Left }); + // --------------------------------------------------------- + // INITIAL ISOLATION PLAN + // --------------------------------------------------------- + AnsiConsole.Write(new Rule("[yellow]Initial Isolation Sample Generation[/]") { Justification = Justify.Left }); - var samplePlan = TensorConfigGenerator.GenerateRequiredSamplePlan(Cache.UnusedTensorGroups); - AnsiConsole.MarkupLine($"[grey]Queued required samples:[/] [cyan]{samplePlan.TotalCount:N0}[/]"); + var initialPlan = TensorConfigGenerator.GenerateRequiredSamplePlan(Cache.UnusedTensorGroups); + AnsiConsole.MarkupLine($"[grey]Queued initial samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); AnsiConsole.MarkupLine("[grey]SQLite will be treated as the source of truth for completed samples.[/]"); - var sampleSummary = await quantizationService.ProcessHybridBatchAsync(samplePlan.Plans); + var initialSummary = await quantizationService.ProcessHybridBatchAsync(initialPlan.Plans); - AnsiConsole.MarkupLine("[bold green]Sample generation phase complete.[/]"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {sampleSummary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {sampleSummary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {sampleSummary.Failed:N0}"); - - - + AnsiConsole.MarkupLine("[bold green]Initial sample generation phase complete.[/]"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {initialSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {initialSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {initialSummary.Failed:N0}"); - var comboCountBefore = ComboCounter.CountAll(); + var comboCountBeforeGate = ComboCounter.CountAll(); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Smallest-Probe Gate"); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Isolation Optimization"); - - AnsiConsole.Write(new Rule("[yellow]Isolation Optimization[/]") { Justification = Justify.Left }); + // --------------------------------------------------------- + // SMALLEST-PROBE GATE + // --------------------------------------------------------- + AnsiConsole.Write(new Rule("[yellow]Smallest-Probe Isolation Gate[/]") { Justification = Justify.Left }); var isolationOptimizer = new IsolationOptimizationService(); - var isolationResult = await isolationOptimizer.AnalyzeAndApplyAsync(samplePlan); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Isolation Optimization"); - + var gateResult = await isolationOptimizer.ApplyInitialSamplingGateAsync(initialPlan); + + foreach (var gd in gateResult.GroupDetails.OrderBy(x => x.GroupName)) + { + AnsiConsole.Write(new Rule($"[yellow]Smallest Probe: {Markup.Escape(gd.GroupName)}[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[green]Probe scheme:[/] {Markup.Escape(gd.ProbeScheme)}"); + AnsiConsole.MarkupLine($"[green]Reduction:[/] {gd.ReductionRatio:P2}"); + AnsiConsole.MarkupLine($"[green]Continue sampling:[/] {(gd.ContinueSampling ? "[green]yes[/]" : "[red]no[/]")}"); + AnsiConsole.MarkupLine($"[grey]{Markup.Escape(gd.Reason)}[/]"); + } + + var comboCountAfterGate = ComboCounter.CountAll(); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Smallest-Probe Gate"); + + // --------------------------------------------------------- + // CONTINUATION ISOLATION PLAN + // --------------------------------------------------------- + var continuationPlan = new RequiredSampleGenerationResult(); + SampleProcessingSummary? continuationSummary = null; + + if (gateResult.GroupIdsToContinue.Count > 0) + { + AnsiConsole.Write(new Rule("[yellow]Continuation Isolation Sample Generation[/]") { Justification = Justify.Left }); + + continuationPlan = TensorConfigGenerator.GenerateContinuationIsolationPlan( + gateResult.GroupIdsToContinue, + Cache.UnusedTensorGroups); + + if (continuationPlan.TotalCount > 0) + { + AnsiConsole.MarkupLine($"[grey]Queued continuation samples:[/] [cyan]{continuationPlan.TotalCount:N0}[/]"); + continuationSummary = await quantizationService.ProcessHybridBatchAsync(continuationPlan.Plans); + + AnsiConsole.MarkupLine("[bold green]Continuation sample generation phase complete.[/]"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {continuationSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {continuationSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {continuationSummary.Failed:N0}"); + } + else + { + AnsiConsole.MarkupLine("[grey]No continuation isolation samples were required after the smallest-probe gate.[/]"); + } + } + else + { + AnsiConsole.MarkupLine("[grey]No tensor groups survived the smallest-probe gate. Skipping continuation isolation sampling.[/]"); + } + + // --------------------------------------------------------- + // FINAL ISOLATION PRUNING + // --------------------------------------------------------- + var comboCountBeforeFinalPruning = ComboCounter.CountAll(); + var fullPlan = RequiredSampleGenerationResult.Merge(initialPlan, continuationPlan); + + AnsiConsole.Write(new Rule("[yellow]Final Isolation Optimization[/]") { Justification = Justify.Left }); + var isolationResult = await isolationOptimizer.AnalyzeAndApplyAsync(fullPlan, gateResult); + + var comboCountAfterFinalPruning = ComboCounter.CountAll(); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Final Isolation Optimization"); + foreach (var gd in isolationResult.GroupDetails.OrderBy(x => x.GroupName)) { AnsiConsole.Write(new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") { Justification = Justify.Left }); - - AnsiConsole.MarkupLine($"[green]Best reduction:[/] {gd.BestReductionRatio:P2}"); + AnsiConsole.MarkupLine($"[green]Stopped early:[/] {(gd.StoppedEarly ? "[yellow]yes[/]" : "[green]no[/]")}"); AnsiConsole.MarkupLine($"[green]Winning scheme:[/] {Markup.Escape(gd.WinningScheme ?? "n/a")}"); - AnsiConsole.MarkupLine($"[green]Locked to native:[/] {(gd.LockedToNative ? "[red]yes[/]" : "[green]no[/]")}"); foreach (var line in gd.Candidates) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); - } - - var comboCountAfter = ComboCounter.CountAll(); + foreach (var line in gd.Eliminations) + AnsiConsole.MarkupLine($" [red]- {Markup.Escape(line)}[/]"); + } await dbService.InitializeAsync(forceRebuild: true); - AnsiConsole.MarkupLine($"[green]Native-locked groups:[/] {isolationResult.NativeLockedGroups:N0}"); - AnsiConsole.MarkupLine($"[green]Dominated group-scheme bans applied:[/] {isolationResult.DominatedGroupSchemesBanned:N0}"); + AnsiConsole.MarkupLine($"[green]Groups stopped early:[/] {gateResult.GroupsStoppedEarly:N0}"); + AnsiConsole.MarkupLine($"[green]Hard-damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupSchemesBanned:N0}"); AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count after pruning:[/] {comboCountAfter:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count before smallest-probe gate:[/] {comboCountBeforeGate:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count after smallest-probe gate:[/] {comboCountAfterGate:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count before final pruning:[/] {comboCountBeforeFinalPruning:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count after final pruning:[/] {comboCountAfterFinalPruning:N0}"); + + foreach (var note in gateResult.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); foreach (var note in isolationResult.Notes) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); - } private void ShowEvolutionHelp() @@ -157,12 +221,15 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine("[bold yellow]Command: evolution[/]"); AnsiConsole.WriteLine("Runs the full evolutionary quantization search algorithm on a target model."); AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold]Usage:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"\" [options]"); AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold]Arguments:[/]"); AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Required)"); AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 2477fae..63ed165 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -32,23 +32,8 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants continue; } - if (RuntimeSearchSpace.IsGroupLockedToNative(group)) - { - builder.Add([TensorWeightScheme.BF16_F16.UniqueId]); - continue; - } - var ids = schemesForBase - .Where(s => - { - if (s.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) - return false; - - if (s.UniqueId == TensorWeightScheme.NULL.UniqueId) - return true; - - return !s.IsBannedFor(group); - }) + .Where(s => !s.IsBannedFor(group)) .Select(s => s.UniqueId) .Distinct() .ToArray(); diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 5b0e9b4..43221d0 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -4,25 +4,32 @@ namespace MagicQuant.Helpers; public static class RuntimeSearchSpace { - private static readonly HashSet NativeLockedGroupIds = new(); private static readonly HashSet DisabledCombinationBaselineIds = new(); public static void ResetForNewModel() { - NativeLockedGroupIds.Clear(); DisabledCombinationBaselineIds.Clear(); TensorWeightScheme.ResetAllRuntimeBans(); } - public static bool IsGroupLockedToNative(TensorGroup group) + public static bool BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) { - return NativeLockedGroupIds.Contains(group.UniqueId); + if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId) + return false; + + if (scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + return false; + + if (scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) + return false; + + scheme.BannedGroups.Add(group); + return true; } - public static void LockGroupToNative(TensorGroup group) + public static int BanAllExplicitTensorSchemesForGroup(TensorGroup group) { - if (!NativeLockedGroupIds.Add(group.UniqueId)) - return; + int applied = 0; foreach (var scheme in TensorWeightScheme.All) { @@ -32,16 +39,41 @@ public static void LockGroupToNative(TensorGroup group) if (scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) continue; - if (!scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) - scheme.BannedGroups.Add(group); + if (BanSchemeForGroup(group, scheme)) + applied++; } + + return applied; } - public static IReadOnlyList GetNativeLockedGroups() + public static IReadOnlyList GetRuntimeExplicitBansForGroup(TensorGroup group) + { + return TensorWeightScheme.All + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Where(x => x.IsBannedFor(group)) + .OrderBy(x => x.UniqueId) + .ToList(); + } + + public static bool IsGroupExplicitQuantBanned(TensorGroup group) + { + var explicitSchemes = TensorWeightScheme.All + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .ToList(); + + if (explicitSchemes.Count == 0) + return false; + + return explicitSchemes.All(x => x.IsBannedFor(group)); + } + + public static IReadOnlyList GetGroupsWithExplicitQuantBanned() { return TReg.All - .Where(g => NativeLockedGroupIds.Contains(g.UniqueId)) - .OrderBy(g => g.UniqueId) + .Where(IsGroupExplicitQuantBanned) + .OrderBy(x => x.UniqueId) .ToList(); } diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index 5e4c46a..f6db690 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -1,7 +1,7 @@ +using MQ.DB; using MQ.DB.Models; using Spectre.Console; using System.Numerics; -using MQ.DB; namespace MagicQuant.Helpers; @@ -29,12 +29,9 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc AnsiConsole.MarkupLine($" [grey]- {string.Join("/", baseline.Names)}[/] (Id={baseline.UniqueId})"); } - var locked = RuntimeSearchSpace.GetNativeLockedGroups() - .OrderBy(x => x.UniqueId) - .ToList(); - - AnsiConsole.MarkupLine($"[green]Native-locked groups:[/] {locked.Count}"); - foreach (var group in locked) + var fullyPrunedGroups = RuntimeSearchSpace.GetGroupsWithExplicitQuantBanned(); + AnsiConsole.MarkupLine($"[green]Groups with explicit tensor quant banned:[/] {fullyPrunedGroups.Count}"); + foreach (var group in fullyPrunedGroups) AnsiConsole.MarkupLine($" [yellow]- {group.Name}[/] (Id={group.UniqueId})"); var unusedIds = Cache.UnusedTensorGroups @@ -46,7 +43,6 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc AnsiConsole.Write(new Rule($"[blue]Base: {Markup.Escape(string.Join("/", baseline.Names))}[/]") { Justification = Justify.Left }); var allowed = ComboLogic.GetAllowedSchemeIdsPerGroup(baseline); - BigInteger baseCount = BigInteger.One; for (int i = 0; i < TReg.All.Length; i++) @@ -66,15 +62,24 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc }) .ToList(); + var runtimeBans = RuntimeSearchSpace.GetRuntimeExplicitBansForGroup(group) + .Select(x => x.Names[0]) + .ToList(); + string state = unusedIds.Contains(group.UniqueId) ? "unused->NULL" : - RuntimeSearchSpace.IsGroupLockedToNative(group) ? "native-locked" : + RuntimeSearchSpace.IsGroupExplicitQuantBanned(group) ? "explicit-quant-banned" : + runtimeBans.Count > 0 ? "runtime-pruned" : "variable"; AnsiConsole.MarkupLine( - $" [cyan]{Markup.Escape(group.Name)}[/] => [green]{ids.Length}[/] choice(s) " + - $"[grey][[{Markup.Escape(state)}]][/] :: {Markup.Escape(string.Join(", ", names))}"); - + $" [cyan]{Markup.Escape(group.Name)}[/] => [green]{ids.Length}[/] choice(s) [grey][[{Markup.Escape(state)}]][/] :: {Markup.Escape(string.Join(", ", names))}"); + + if (runtimeBans.Count > 0) + { + AnsiConsole.MarkupLine( + $" [grey]runtime bans:[/] {Markup.Escape(string.Join(", ", runtimeBans))}"); + } } AnsiConsole.MarkupLine($" [bold green]Base total:[/] {baseCount:N0}"); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 3c8a535..e8caf1b 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -1,8 +1,9 @@ +using MQ.DB; using MQ.DB.Models; using Spectre.Console; using System.Collections.Concurrent; using System.Collections.Immutable; -using MQ.DB; +using System.Numerics; namespace MagicQuant.Helpers; @@ -11,6 +12,8 @@ public static class TensorConfigGenerator public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( List? missingTensorGroups = null) { + TensorWeightScheme.ValidateSmallestConfiguration(); + if (missingTensorGroups != null && !missingTensorGroups.Any()) missingTensorGroups = null; @@ -41,7 +44,6 @@ public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( // --------------------------------------------------------- // 2. Base-only isolation for actual combo baselines - // This tells you whether uncovered tensors alone justify the baseline. // --------------------------------------------------------- foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) { @@ -65,8 +67,6 @@ public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( // --------------------------------------------------------- // 3. Carrier base-only isolation for tensor-group probing - // Q8_0 is used as the carrier because llama-quantize actually applies - // mixed tensor overrides correctly on a real quantized output. // --------------------------------------------------------- var groupIsolationCarrier = BaselineQuants.Q8_0; @@ -88,24 +88,81 @@ public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( result.BaseOnlyIsolationCount++; // --------------------------------------------------------- - // 4. Tensor-group isolation using the Q8_0 carrier + // 4. Smallest-first tensor-group isolation probes // --------------------------------------------------------- - var probeSchemes = TensorWeightScheme.All - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + foreach (var group in activeGroups) + { + var firstProbe = GetInitialIsolationProbeScheme(group); + if (firstProbe == null) + continue; + + var quant = HybridQuant.CreateBlanket( + baseQuant: groupIsolationCarrier, + groups: activeGroups, + blanketScheme: TensorWeightScheme.BF16_F16); + + var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); + target.TensorType = firstProbe; + + result.Plans.Add(new RequiredSamplePlan + { + Kind = RequiredSampleKind.GroupIsolation, + Key = $"probefirst:{groupIsolationCarrier.UniqueId}:{group.UniqueId}:{firstProbe.UniqueId}", + Description = + $"Initial isolation probe for group '{group.Name}' using scheme '{firstProbe.Names[0]}' on carrier '{groupIsolationCarrier.Names[0]}'.", + Quant = quant, + TargetGroupId = group.UniqueId, + TestedSchemeId = firstProbe.UniqueId, + TestedBaselineId = groupIsolationCarrier.UniqueId + }); + + result.GroupIsolationCount++; + } + + AnsiConsole.MarkupLine($"[bold green]Initial pure baseline samples required:[/] {result.PureBaselineCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Initial base-only isolation samples required:[/] {result.BaseOnlyIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Initial smallest-probe isolation samples required:[/] {result.GroupIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Initial total samples required:[/] {result.TotalCount:N0}"); + + return result; + } + + public static RequiredSampleGenerationResult GenerateContinuationIsolationPlan( + IEnumerable groupIdsToContinue, + List? missingTensorGroups = null) + { + if (groupIdsToContinue == null) + throw new ArgumentNullException(nameof(groupIdsToContinue)); + + TensorWeightScheme.ValidateSmallestConfiguration(); + + if (missingTensorGroups != null && !missingTensorGroups.Any()) + missingTensorGroups = null; + + var skippedIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + var continueIds = groupIdsToContinue.ToHashSet(); + var activeGroups = TReg.All + .Where(x => !skippedIds.Contains(x.UniqueId)) + .Where(x => continueIds.Contains(x.UniqueId)) .OrderBy(x => x.UniqueId) .ToList(); + var result = new RequiredSampleGenerationResult(); + var groupIsolationCarrier = BaselineQuants.Q8_0; + foreach (var group in activeGroups) { - foreach (var scheme in probeSchemes) + var allCandidates = GetOrderedIsolationCandidateSchemes(group); + var firstProbe = GetInitialIsolationProbeScheme(group); + + foreach (var scheme in allCandidates) { - if (scheme.IsBannedFor(group)) + if (firstProbe != null && scheme.UniqueId == firstProbe.UniqueId) continue; var quant = HybridQuant.CreateBlanket( baseQuant: groupIsolationCarrier, - groups: activeGroups, + groups: TReg.All.Where(x => !skippedIds.Contains(x.UniqueId)).OrderBy(x => x.UniqueId), blanketScheme: TensorWeightScheme.BF16_F16); var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); @@ -116,7 +173,7 @@ public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( Kind = RequiredSampleKind.GroupIsolation, Key = $"group:{groupIsolationCarrier.UniqueId}:{group.UniqueId}:{scheme.UniqueId}", Description = - $"Carrier-based isolation for group '{group.Name}' using scheme '{scheme.Names[0]}' on base '{groupIsolationCarrier.Names[0]}'.", + $"Follow-up isolation sample for group '{group.Name}' using scheme '{scheme.Names[0]}' on carrier '{groupIsolationCarrier.Names[0]}'.", Quant = quant, TargetGroupId = group.UniqueId, TestedSchemeId = scheme.UniqueId, @@ -127,21 +184,28 @@ public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( } } - AnsiConsole.MarkupLine($"[bold green]Pure baselines required:[/] {result.PureBaselineCount:N0}"); - AnsiConsole.MarkupLine( - $"[bold green]Base-only isolation samples required:[/] {result.BaseOnlyIsolationCount:N0}"); - AnsiConsole.MarkupLine( - $"[bold green]Tensor-group isolation samples required:[/] {result.GroupIsolationCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Total required samples:[/] {result.TotalCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Continuation isolation samples required:[/] {result.GroupIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Continuation total samples required:[/] {result.TotalCount:N0}"); return result; } - public static List GenerateRequiredDataSampleCombos(List? missingTensorGroups = null) + private static TensorWeightScheme? GetInitialIsolationProbeScheme(TensorGroup group) + { + var ordered = GetOrderedIsolationCandidateSchemes(group); + + return ordered.FirstOrDefault(x => !x.RequiresImatrix) ?? ordered.FirstOrDefault(); + } + + private static List GetOrderedIsolationCandidateSchemes(TensorGroup group) { - return GenerateRequiredSamplePlan(missingTensorGroups) - .Plans - .Select(x => x.Quant) + return TensorWeightScheme.All + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Where(x => !x.IsBannedFor(group)) + .OrderByDescending(x => !x.RequiresImatrix && x.IsSmallest) + .ThenBy(x => x.RequiresImatrix ? 1 : 0) + .ThenByDescending(x => x.UniqueId) .ToList(); } @@ -179,110 +243,88 @@ public static IEnumerable> GenerateTensorConfigBatches( int dop = ComputeWorkerThreads(GetThreadCountSafe()); byte baseId = baseQuant.UniqueId; - var queue = new BlockingCollection>( - boundedCapacity: Math.Max(2, dop * 2)); + BigInteger total = BigInteger.One; + for (int i = 0; i < dims; i++) + total *= allowed[i].Length; - var producer = Task.Run(() => - { - try + if (total == BigInteger.Zero) + yield break; + + var buffer = new ConcurrentQueue(); + var produced = 0L; + + Parallel.ForEach( + Partitioner.Create(0L, (long)total), + new ParallelOptions { MaxDegreeOfParallelism = dop, CancellationToken = ct }, + range => { - Parallel.ForEach( - Partitioner.Create(0, allowed[0].Length), - new ParallelOptions + var local = new List(Math.Min(batchSize, 8192)); + + for (long flat = range.Item1; flat < range.Item2; flat++) + { + ct.ThrowIfCancellationRequested(); + + long n = flat; + Span chosen = stackalloc byte[dims]; + + for (int d = dims - 1; d >= 0; d--) { - MaxDegreeOfParallelism = dop, - CancellationToken = ct - }, - range => + var arr = allowed[d]; + int len = arr.Length; + int idx = (int)(n % len); + chosen[d] = arr[idx]; + n /= len; + } + + local.Add(new TensorConfig( + baseId, + chosen[0], + chosen[1], + chosen[2], + chosen[3], + chosen[4], + chosen[5], + chosen[6], + chosen[7], + chosen[8])); + + if (local.Count >= batchSize) { - var batch = new List(Math.Min(batchSize, 250_000)); - var idx = new int[dims]; - - var d0 = allowed[0]; - var d1 = allowed[1]; - var d2 = allowed[2]; - var d3 = allowed[3]; - var d4 = allowed[4]; - var d5 = allowed[5]; - var d6 = allowed[6]; - var d7 = allowed[7]; - var d8 = allowed[8]; - - for (int i0 = range.Item1; i0 < range.Item2; i0++) - { - ct.ThrowIfCancellationRequested(); - - idx[0] = i0; - Array.Clear(idx, 1, dims - 1); - - while (true) - { - batch.Add(new TensorConfig( - baseQuant: baseId, - embeddings: d0[idx[0]], - lmHead: d1[idx[1]], - attnQ: d2[idx[2]], - attnKV: d3[idx[3]], - attnOutput: d4[idx[4]], - ffnUpGate: d5[idx[5]], - ffnDown: d6[idx[6]], - moeExperts: d7[idx[7]], - moeRouter: d8[idx[8]] - )); - - if (batch.Count >= batchSize) - { - queue.Add(batch, ct); - batch = new List(Math.Min(batchSize, 250_000)); - } - - int d = dims - 1; - while (d >= 1) - { - idx[d]++; - if (idx[d] < allowed[d].Length) - break; - - idx[d] = 0; - d--; - } - - if (d < 1) - break; - } - } - - if (batch.Count > 0) - queue.Add(batch, ct); - }); - } - finally - { - queue.CompleteAdding(); - } - }, ct); + foreach (var item in local) + buffer.Enqueue(item); - foreach (var batch in queue.GetConsumingEnumerable(ct)) - yield return batch; + local.Clear(); + } + } - producer.GetAwaiter().GetResult(); + foreach (var item in local) + buffer.Enqueue(item); + }); + + while (!buffer.IsEmpty) + { + var batch = new List(batchSize); + + while (batch.Count < batchSize && buffer.TryDequeue(out var cfg)) + batch.Add(cfg); + + produced += batch.Count; + if (batch.Count > 0) + yield return batch; + } } private static int GetThreadCountSafe() { - return Cache.SysInfo?.ThreadCount > 0 - ? Cache.SysInfo.ThreadCount - : Environment.ProcessorCount; + int tc = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; + return Math.Max(1, tc); } - private static int ComputeWorkerThreads(int threadCount) + private static int ComputeWorkerThreads(int logicalThreads) { - if (threadCount <= 4) - return 1; - - if (threadCount <= 12) - return 2; - - return Math.Max(2, threadCount / 6); + if (logicalThreads <= 2) return 1; + if (logicalThreads <= 4) return 2; + if (logicalThreads <= 8) return 4; + return Math.Max(4, logicalThreads / 2); } } \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 14e049c..36fed58 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -9,170 +9,282 @@ namespace MagicQuant.Services; public sealed class IsolationOptimizationOptions { - public double MinMeaningfulGroupReductionRatio { get; set; } = 0.10d; - public double MinMeaningfulBaseOnlyReductionRatio { get; set; } = 0.01d; + public double MinMeaningfulBaseOnlyReductionRatio { get; set; } = IsolationRules.MinimumMeaningfulBaseOnlyReductionRatio; } -public sealed class IsolationGroupDecision +public sealed class IsolationSamplingGateDecision { public string GroupName { get; set; } = string.Empty; - public double BestReductionRatio { get; set; } - public bool LockedToNative { get; set; } + public string ProbeScheme { get; set; } = string.Empty; + public double ReductionRatio { get; set; } + public bool ContinueSampling { get; set; } + public string Reason { get; set; } = string.Empty; + public ulong? ProbeSizeBytes { get; set; } +} +public sealed class IsolationSamplingGateResult +{ + public int GroupsStoppedEarly { get; set; } + public List GroupIdsToContinue { get; set; } = new(); + public List Notes { get; set; } = new(); + public List GroupDetails { get; set; } = new(); +} + +public sealed class IsolationGroupDecision +{ + public string GroupName { get; set; } = string.Empty; + public bool StoppedEarly { get; set; } public string? WinningScheme { get; set; } public ulong? WinningSizeBytes { get; set; } public double? WinningKld { get; set; } public double? WinningPplDelta { get; set; } - public List Candidates { get; set; } = new(); + public List Eliminations { get; set; } = new(); } public sealed class IsolationOptimizationResult { - public int NativeLockedGroups { get; set; } + public int GroupsStoppedEarly { get; set; } + public int HardDamageEliminations { get; set; } public int DominatedGroupSchemesBanned { get; set; } public int DisabledBaselines { get; set; } - public List Notes { get; set; } = new(); public List GroupDetails { get; set; } = new(); } public class IsolationOptimizationService { - public async Task AnalyzeAndApplyAsync( - RequiredSampleGenerationResult plan, - IsolationOptimizationOptions? options = null, + public async Task ApplyInitialSamplingGateAsync( + RequiredSampleGenerationResult initialPlan, CancellationToken ct = default) { - options ??= new IsolationOptimizationOptions(); + if (initialPlan == null) + throw new ArgumentNullException(nameof(initialPlan)); - var result = new IsolationOptimizationResult(); - - var nativeBaseline = await LoadSnapshotAsync( - HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct); + var result = new IsolationSamplingGateResult(); var carrierBaselineId = BaselineQuants.Q8_0.UniqueId; - var carrierBaseOnlyPlan = plan.Plans.First(x => + var carrierBaseOnlyPlan = initialPlan.Plans.FirstOrDefault(x => x.Kind == RequiredSampleKind.BaseOnlyIsolation && + x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal) && x.TestedBaselineId == carrierBaselineId); - var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct); + if (carrierBaseOnlyPlan == null) + throw new InvalidOperationException("Carrier base-only isolation plan was not found."); + + var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct) + ?? throw new InvalidOperationException("Carrier base-only isolation benchmark was not found in SQLite."); + + var probePlans = initialPlan.Plans + .Where(x => x.Kind == RequiredSampleKind.GroupIsolation) + .Where(x => x.Key.StartsWith("probefirst:", StringComparison.Ordinal)) + .OrderBy(x => x.TargetGroupId) + .ToList(); + + foreach (var plan in probePlans) + { + var group = TReg.All.First(x => x.UniqueId == plan.TargetGroupId!.Value); + var scheme = TensorWeightScheme.All.First(x => x.UniqueId == plan.TestedSchemeId!.Value); + + var snapshot = await LoadSnapshotAsync(plan.Quant, ct) + ?? throw new InvalidOperationException( + $"Initial isolation probe benchmark was missing for group '{group.Name}' and scheme '{scheme.Names[0]}'."); + + double reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snapshot.SizeBytes); + + var decision = new IsolationSamplingGateDecision + { + GroupName = group.Name, + ProbeScheme = scheme.Names[0], + ReductionRatio = reduction, + ProbeSizeBytes = snapshot.SizeBytes + }; + + if (reduction < IsolationRules.MinimumIsolationReductionToContinue) + { + RuntimeSearchSpace.BanAllExplicitTensorSchemesForGroup(group); + result.GroupsStoppedEarly++; + + decision.ContinueSampling = false; + decision.Reason = + $"Stopped early because smallest explicit probe reduction was only {reduction:P2}, below the {IsolationRules.MinimumIsolationReductionToContinue:P2} gate."; + + result.Notes.Add( + $"Stopped isolated explicit sampling for '{group.Name}' because smallest probe '{scheme.Names[0]}' only reduced the model by {reduction:P2}."); + } + else + { + result.GroupIdsToContinue.Add(group.UniqueId); + decision.ContinueSampling = true; + decision.Reason = + $"Continuing sampling because smallest explicit probe reduction was {reduction:P2}."; + } + + result.GroupDetails.Add(decision); + } + + return result; + } + + public async Task AnalyzeAndApplyAsync( + RequiredSampleGenerationResult plan, + IsolationSamplingGateResult? gateResult = null, + IsolationOptimizationOptions? options = null, + CancellationToken ct = default) + { + if (plan == null) + throw new ArgumentNullException(nameof(plan)); + + options ??= new IsolationOptimizationOptions(); + + var result = new IsolationOptimizationResult + { + GroupsStoppedEarly = gateResult?.GroupsStoppedEarly ?? 0 + }; + + var nativeBaseline = await LoadSnapshotAsync( + HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) + ?? throw new InvalidOperationException("Native BF16/F16/F32 baseline benchmark was not found in SQLite."); // ============================ // GROUP ISOLATION ANALYSIS // ============================ var groupPlans = plan.Plans .Where(x => x.Kind == RequiredSampleKind.GroupIsolation) - .Where(x => x.TestedBaselineId == carrierBaselineId) + .Where(x => x.TestedBaselineId == BaselineQuants.Q8_0.UniqueId) .GroupBy(x => x.TargetGroupId!.Value) + .OrderBy(x => x.Key) .ToList(); + var stoppedEarlyIds = gateResult?.GroupIdsToContinue == null + ? new HashSet() + : TReg.All.Select(x => x.UniqueId).Except(gateResult.GroupIdsToContinue).ToHashSet(); + foreach (var groupSet in groupPlans) { var group = TReg.All.First(x => x.UniqueId == groupSet.Key); - - var snapshots = new List<(RequiredSamplePlan Plan, BenchmarkSnapshot Snapshot)>(); - - foreach (var item in groupSet) - { - var snap = await LoadSnapshotAsync(item.Quant, ct); - if (snap != null) - snapshots.Add((item, snap)); - } - - if (snapshots.Count == 0) - continue; - var decision = new IsolationGroupDecision { - GroupName = group.Name + GroupName = group.Name, + StoppedEarly = gateResult?.GroupDetails.Any(x => x.GroupName == group.Name && !x.ContinueSampling) == true }; - // compute best reduction vs carrier - double maxReduction = snapshots - .Select(x => ComputeReductionRatio(carrierBaseOnly.SizeBytes, x.Snapshot.SizeBytes)) - .Max(); + var candidates = new List(); - decision.BestReductionRatio = maxReduction; - - // build candidate debug list - foreach (var snap in snapshots.OrderBy(x => x.Snapshot.SizeBytes)) + foreach (var item in groupSet) { - var scheme = TensorWeightScheme.All - .FirstOrDefault(x => x.UniqueId == snap.Plan.TestedSchemeId); + var snap = await LoadSnapshotAsync(item.Quant, ct); + if (snap == null) + continue; - var reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.Snapshot.SizeBytes); - var kld = GetAggregateKld(snap.Snapshot); - var pplDelta = GetAggregatePplDelta(snap.Snapshot, nativeBaseline); + var scheme = TensorWeightScheme.All.First(x => x.UniqueId == item.TestedSchemeId!.Value); + var avgKld = GetAggregateKld(snap); + var pplDelta = GetAggregatePplDelta(snap, nativeBaseline); + + var candidate = new CandidateEvaluation + { + Plan = item, + Scheme = scheme, + Snapshot = snap, + AggregateKld = avgKld, + AggregatePplDelta = pplDelta + }; decision.Candidates.Add( - $"{scheme?.Names[0] ?? "Unknown"} | size={(snap.Snapshot.SizeBytes / 1024.0 / 1024.0):F2}MB | reduction={reduction:P2} | kld={kld:G6} | pplΔ={pplDelta:P4}"); + $"{scheme.Names[0]} | size={(snap.SizeBytes / 1024.0 / 1024.0):F2} MiB | avgKLD={avgKld:G6} | avgΔPPL={pplDelta:P4}"); + + candidates.Add(candidate); } - // LOCK LOGIC - if (maxReduction < options.MinMeaningfulGroupReductionRatio) + if (candidates.Count == 0) { - RuntimeSearchSpace.LockGroupToNative(group); - - decision.LockedToNative = true; - result.NativeLockedGroups++; - - result.Notes.Add( - $"Locked '{group.Name}' to native (max reduction {maxReduction:P2})"); - result.GroupDetails.Add(decision); continue; } - decision.LockedToNative = false; + // ------------------------------------------------------ + // Hard damage elimination + // ------------------------------------------------------ + foreach (var candidate in candidates) + { + if (candidate.Eliminated) + continue; + + bool pplTooHigh = candidate.AggregatePplDelta >= IsolationRules.MaximumIsolationPplDeltaRatio; + bool kldTooHigh = candidate.AggregateKld >= IsolationRules.MaximumIsolationKld; - // FIND WINNER - var ordered = snapshots - .OrderBy(x => GetAggregateKld(x.Snapshot)) - .ThenBy(x => GetAggregatePplDelta(x.Snapshot, nativeBaseline)) - .ToList(); + if (!pplTooHigh && !kldTooHigh) + continue; - var winner = ordered.First(); + candidate.Eliminated = true; + candidate.EliminationReason = pplTooHigh && kldTooHigh + ? $"hard-damage: avgΔPPL={candidate.AggregatePplDelta:P4} and avgKLD={candidate.AggregateKld:G6} exceeded thresholds" + : pplTooHigh + ? $"hard-damage: avgΔPPL={candidate.AggregatePplDelta:P4} exceeded {IsolationRules.MaximumIsolationPplDeltaRatio:P2}" + : $"hard-damage: avgKLD={candidate.AggregateKld:G6} exceeded {IsolationRules.MaximumIsolationKld:G6}"; - var winnerScheme = TensorWeightScheme.All - .FirstOrDefault(x => x.UniqueId == winner.Plan.TestedSchemeId); + if (RuntimeSearchSpace.BanSchemeForGroup(group, candidate.Scheme)) + result.HardDamageEliminations++; - decision.WinningScheme = winnerScheme?.Names[0]; - decision.WinningSizeBytes = winner.Snapshot.SizeBytes; - decision.WinningKld = GetAggregateKld(winner.Snapshot); - decision.WinningPplDelta = GetAggregatePplDelta(winner.Snapshot, nativeBaseline); + decision.Eliminations.Add($"{candidate.Scheme.Names[0]} -> {candidate.EliminationReason}"); + result.Notes.Add($"Eliminated '{candidate.Scheme.Names[0]}' for '{group.Name}' due to {candidate.EliminationReason}."); + } - // BAN LOSERS WITH SAME SIZE - foreach (var sizeBucket in snapshots.GroupBy(x => x.Snapshot.SizeBytes)) + // ------------------------------------------------------ + // Dominance elimination (non-imatrix only) + // ------------------------------------------------------ + var nonImatrixSurvivors = candidates + .Where(x => !x.Eliminated) + .Where(x => !x.Scheme.RequiresImatrix) + .ToList(); + + for (int i = 0; i < nonImatrixSurvivors.Count; i++) { - if (sizeBucket.Count() <= 1) + var a = nonImatrixSurvivors[i]; + if (a.Eliminated) continue; - var sorted = sizeBucket - .OrderBy(x => GetAggregateKld(x.Snapshot)) - .ThenBy(x => GetAggregatePplDelta(x.Snapshot, nativeBaseline)) - .ToList(); - - foreach (var loser in sorted.Skip(1)) + for (int j = 0; j < nonImatrixSurvivors.Count; j++) { - var scheme = TensorWeightScheme.All - .FirstOrDefault(x => x.UniqueId == loser.Plan.TestedSchemeId); + if (i == j) + continue; - if (scheme == null) + var b = nonImatrixSurvivors[j]; + if (b.Eliminated) continue; - if (!scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) + if (Dominates(a, b)) { - scheme.BannedGroups.Add(group); - result.DominatedGroupSchemesBanned++; + b.Eliminated = true; + b.EliminationReason = + $"dominance: {a.Scheme.Names[0]} was same size or smaller and no worse on KLD/PPL with at least one strict win"; + + if (RuntimeSearchSpace.BanSchemeForGroup(group, b.Scheme)) + result.DominatedGroupSchemesBanned++; - result.Notes.Add( - $"Banned {scheme.Names[0]} for {group.Name} (same size, worse fidelity)"); + decision.Eliminations.Add($"{b.Scheme.Names[0]} -> {b.EliminationReason}"); + result.Notes.Add($"Eliminated '{b.Scheme.Names[0]}' for '{group.Name}' because '{a.Scheme.Names[0]}' clearly dominated it."); } } } + var survivors = candidates + .Where(x => !x.Eliminated) + .OrderBy(x => x.AggregateKld) + .ThenBy(x => x.AggregatePplDelta) + .ThenBy(x => x.Snapshot.SizeBytes) + .ToList(); + + if (survivors.Count > 0) + { + var winner = survivors[0]; + decision.WinningScheme = winner.Scheme.Names[0]; + decision.WinningSizeBytes = winner.Snapshot.SizeBytes; + decision.WinningKld = winner.AggregateKld; + decision.WinningPplDelta = winner.AggregatePplDelta; + } + result.GroupDetails.Add(decision); } @@ -187,7 +299,8 @@ public async Task AnalyzeAndApplyAsync( foreach (var item in baseOnlyPlans) { var snap = await LoadSnapshotAsync(item.Quant, ct); - if (snap == null) continue; + if (snap == null) + continue; double reduction = ComputeReductionRatio(nativeBaseline.SizeBytes, snap.SizeBytes); @@ -198,7 +311,8 @@ public async Task AnalyzeAndApplyAsync( if (RuntimeSearchSpace.DisableCombinationBaseline(baseline)) { result.DisabledBaselines++; - result.Notes.Add($"Disabled baseline {baseline.Names[0]} (reduction {reduction:P2})"); + result.Notes.Add( + $"Disabled baseline '{baseline.Names[0]}' because uncovered-tensor reduction was only {reduction:P2}."); } } } @@ -206,9 +320,22 @@ public async Task AnalyzeAndApplyAsync( return result; } - // ============================ - // HELPERS (unchanged) - // ============================ + private static bool Dominates(CandidateEvaluation a, CandidateEvaluation b) + { + if (a.Scheme.RequiresImatrix || b.Scheme.RequiresImatrix) + return false; + + bool sizeNoWorse = a.Snapshot.SizeBytes <= b.Snapshot.SizeBytes; + bool kldNoWorse = a.AggregateKld <= b.AggregateKld + IsolationRules.MetricComparisonEpsilon; + bool pplNoWorse = a.AggregatePplDelta <= b.AggregatePplDelta + IsolationRules.MetricComparisonEpsilon; + + bool strictlyBetter = + a.Snapshot.SizeBytes < b.Snapshot.SizeBytes || + a.AggregateKld + IsolationRules.MetricComparisonEpsilon < b.AggregateKld || + a.AggregatePplDelta + IsolationRules.MetricComparisonEpsilon < b.AggregatePplDelta; + + return sizeNoWorse && kldNoWorse && pplNoWorse && strictlyBetter; + } private async Task LoadSnapshotAsync(HybridQuant quant, CancellationToken ct) { @@ -217,7 +344,8 @@ public async Task AnalyzeAndApplyAsync( var model = await db.AiModelHashes .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - if (model == null) return null; + if (model == null) + return null; var lookup = BuildLookup(quant); @@ -241,7 +369,8 @@ public async Task AnalyzeAndApplyAsync( x.c.MoeRouter == lookup.MoeRouter, ct); - if (row == null) return null; + if (row == null) + return null; var snapshot = new BenchmarkSnapshot { SizeBytes = row.b.SizeBytes }; @@ -264,7 +393,11 @@ private static double ComputeReductionRatio(ulong nativeSize, ulong candidateSiz : (nativeSize - candidateSize) / (double)nativeSize; private static double GetAggregateKld(BenchmarkSnapshot s) - => s.Domains.Values.Where(x => x.Kld.HasValue).Select(x => x.Kld!.Value).DefaultIfEmpty(double.MaxValue).Average(); + => s.Domains.Values + .Where(x => x.Kld.HasValue) + .Select(x => x.Kld!.Value) + .DefaultIfEmpty(double.MaxValue) + .Average(); private static double GetAggregatePplDelta(BenchmarkSnapshot s, BenchmarkSnapshot n) { @@ -272,8 +405,11 @@ private static double GetAggregatePplDelta(BenchmarkSnapshot s, BenchmarkSnapsho foreach (var kv in s.Domains) { - if (!n.Domains.TryGetValue(kv.Key, out var native)) continue; - if (native.Ppl <= 0) continue; + if (!n.Domains.TryGetValue(kv.Key, out var native)) + continue; + + if (native.Ppl <= 0) + continue; list.Add(Math.Abs(kv.Value.Ppl - native.Ppl) / native.Ppl); } @@ -301,6 +437,17 @@ byte Get(TensorGroup g) => }; } + private sealed class CandidateEvaluation + { + public RequiredSamplePlan Plan { get; set; } = default!; + public TensorWeightScheme Scheme { get; set; } = default!; + public BenchmarkSnapshot Snapshot { get; set; } = default!; + public double AggregateKld { get; set; } + public double AggregatePplDelta { get; set; } + public bool Eliminated { get; set; } + public string? EliminationReason { get; set; } + } + private sealed class TensorLookup { public byte BaseQuant; diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index d495a6f..d139dba 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -22,6 +22,8 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) if (!File.Exists(ggufPath)) throw new FileNotFoundException($"Base model not found at {ggufPath}"); + TensorWeightScheme.ValidateSmallestConfiguration(); + RuntimeSearchSpace.ResetForNewModel(); Cache.UnusedTensorGroups.Clear(); TensorWeightScheme.NULL.BannedGroups.Clear(); @@ -72,7 +74,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) int unusedCount = 0; int usedCount = 0; int shapeBanCount = 0; - int nativeLockedCount = 0; + int explicitQuantBannedCount = 0; var shapeTable = new Table().Border(TableBorder.Rounded).Title("[red]Shape Incompatibilities[/]"); shapeTable.AddColumn("Group"); @@ -119,21 +121,14 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) scheme.BannedGroups.Add(group); shapeBanCount++; - shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{scheme.Names[0]}[/]", "[grey]Block Alignment[/]"); + shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{scheme.Names[0]}[/]", + "[grey]Block Alignment[/]"); } foreach (var group in TReg.All.Except(Cache.UnusedTensorGroups)) { - bool anyNonNativeOptionLeft = TensorWeightScheme.All - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Any(x => !x.IsBannedFor(group)); - - if (!anyNonNativeOptionLeft) - { - RuntimeSearchSpace.LockGroupToNative(group); - nativeLockedCount++; - } + if (RuntimeSearchSpace.IsGroupExplicitQuantBanned(group)) + explicitQuantBannedCount++; } AnsiConsole.MarkupLine("[green]✔[/] Analysis Complete."); @@ -145,10 +140,16 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) AnsiConsole.MarkupLine($" Unused Groups: [grey]{unusedNames}[/] (Forced to NULL)"); } - if (nativeLockedCount > 0) + if (explicitQuantBannedCount > 0) { - string locked = string.Join(", ", RuntimeSearchSpace.GetNativeLockedGroups().Select(x => x.Name)); - AnsiConsole.MarkupLine($" Native-Locked Groups: [yellow]{locked}[/]"); + string groups = string.Join(", ", + RuntimeSearchSpace.GetGroupsWithExplicitQuantBanned().Select(x => x.Name)); + + AnsiConsole.MarkupLine($" Explicit-Quant-Banned Groups: [yellow]{groups}[/]"); + } + else + { + AnsiConsole.MarkupLine("[green]No groups were reduced to BF16/NULL-only by compatibility checks.[/]"); } if (shapeBanCount > 0) @@ -172,8 +173,8 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) } private string GeneratePythonScript(string jsonPayload) -{ - return $@" + { + return $@" import sys import json import re @@ -264,7 +265,7 @@ with open(output_path, 'w') as f: ""Error"": None }}, f, indent=2) "; -} + } private class CompatResult { From 19c4b01e8617f8a0f625559fb4a8bdffb44163eb Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 14 Apr 2026 22:20:41 -0400 Subject: [PATCH 048/258] Significantly more pruning but over pruning atm. --- MQ.DB/Models/RequiredSamplePlan.cs | 34 +- MQ.DB/Models/TensorConfigs.cs | 42 +- MQ.DB/Models/TensorWeightScheme.cs | 109 ++-- MagicQuant/Commands/Evolution.cs | 124 ++-- MagicQuant/Helpers/CliHelpers.cs | 8 +- MagicQuant/Helpers/ComboLogic.cs | 27 +- MagicQuant/Helpers/IsolationPruningConfig.cs | 14 + MagicQuant/Helpers/RuntimeSearchSpace.cs | 75 ++- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 58 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 315 +++++----- .../Services/IsolationOptimizationService.cs | 558 ++++++++++-------- MagicQuant/Services/QuantDatabaseService.cs | 294 ++++++++- MagicQuant/Services/QuantizationService.cs | 2 +- 13 files changed, 929 insertions(+), 731 deletions(-) create mode 100644 MagicQuant/Helpers/IsolationPruningConfig.cs diff --git a/MQ.DB/Models/RequiredSamplePlan.cs b/MQ.DB/Models/RequiredSamplePlan.cs index e4b65a4..7c40706 100644 --- a/MQ.DB/Models/RequiredSamplePlan.cs +++ b/MQ.DB/Models/RequiredSamplePlan.cs @@ -4,7 +4,8 @@ public enum RequiredSampleKind { PureBaseline = 1, BaseOnlyIsolation = 2, - GroupIsolation = 3 + GroupIsolationProbe = 3, + GroupIsolationContinuation = 4 } public sealed class RequiredSamplePlan @@ -13,40 +14,31 @@ public sealed class RequiredSamplePlan public string Key { get; set; } = string.Empty; public string Description { get; set; } = string.Empty; public HybridQuant Quant { get; set; } = default!; - public byte? TargetGroupId { get; set; } public byte? TestedSchemeId { get; set; } public byte? TestedBaselineId { get; set; } + public bool IsSmallestProbe { get; set; } } public sealed class RequiredSampleGenerationResult { public List Plans { get; set; } = new(); - public int PureBaselineCount { get; set; } public int BaseOnlyIsolationCount { get; set; } public int GroupIsolationCount { get; set; } - public int TotalCount => Plans.Count; - public void AppendFrom(RequiredSampleGenerationResult other) + public RequiredSampleGenerationResult MergeWith(RequiredSampleGenerationResult other) { - if (other == null) - return; - - Plans.AddRange(other.Plans); - PureBaselineCount += other.PureBaselineCount; - BaseOnlyIsolationCount += other.BaseOnlyIsolationCount; - GroupIsolationCount += other.GroupIsolationCount; - } - - public static RequiredSampleGenerationResult Merge(params RequiredSampleGenerationResult[] items) - { - var merged = new RequiredSampleGenerationResult(); - - foreach (var item in items) - merged.AppendFrom(item); - + var merged = new RequiredSampleGenerationResult + { + PureBaselineCount = PureBaselineCount + other.PureBaselineCount, + BaseOnlyIsolationCount = BaseOnlyIsolationCount + other.BaseOnlyIsolationCount, + GroupIsolationCount = GroupIsolationCount + other.GroupIsolationCount + }; + + merged.Plans.AddRange(Plans); + merged.Plans.AddRange(other.Plans); return merged; } } \ No newline at end of file diff --git a/MQ.DB/Models/TensorConfigs.cs b/MQ.DB/Models/TensorConfigs.cs index 34272f1..1cb8f98 100644 --- a/MQ.DB/Models/TensorConfigs.cs +++ b/MQ.DB/Models/TensorConfigs.cs @@ -46,25 +46,24 @@ public TensorConfig( public TensorConfig(HybridQuant h) : this( baseQuant: checked((byte)h.BaseQuant.UniqueId), - embeddings: GetSchemeId(h, TReg.Embeddings), - lmHead: GetSchemeId(h, TReg.LmHead), - attnQ: GetSchemeId(h, TReg.AttnQ), - attnKV: GetSchemeId(h, TReg.AttnKV), - attnOutput: GetSchemeId(h, TReg.AttnOutput), - ffnUpGate: GetSchemeId(h, TReg.FfnUpGate), - ffnDown: GetSchemeId(h, TReg.FfnDown), - moeExperts: GetSchemeId(h, TReg.MoeExperts), - moeRouter: GetSchemeId(h, TReg.MoeRouter)) + embeddings: GetSchemeIdOrDefault(h, TReg.Embeddings), + lmHead: GetSchemeIdOrDefault(h, TReg.LmHead), + attnQ: GetSchemeIdOrDefault(h, TReg.AttnQ), + attnKV: GetSchemeIdOrDefault(h, TReg.AttnKV), + attnOutput: GetSchemeIdOrDefault(h, TReg.AttnOutput), + ffnUpGate: GetSchemeIdOrDefault(h, TReg.FfnUpGate), + ffnDown: GetSchemeIdOrDefault(h, TReg.FfnDown), + moeExperts: GetSchemeIdOrDefault(h, TReg.MoeExperts), + moeRouter: GetSchemeIdOrDefault(h, TReg.MoeRouter)) { } - private static byte GetSchemeId(HybridQuant h, TensorGroup group) + private static byte GetSchemeIdOrDefault(HybridQuant h, TensorGroup group) { - if (h.Tensors == null) - throw new ArgumentNullException(nameof(h.Tensors)); + if (h.Tensors == null || h.Tensors.Count == 0) + return TensorWeightScheme.NULL.UniqueId; TensorWeightScheme? found = null; - // Single pass: find the tensor type for the requested group for (int i = 0; i < h.Tensors.Count; i++) { var t = h.Tensors[i]; @@ -75,21 +74,18 @@ private static byte GetSchemeId(HybridQuant h, TensorGroup group) continue; if (found != null) + { throw new InvalidOperationException( $"HybridQuant contains duplicate entries for group '{group.Name}' (UniqueId={group.UniqueId})."); + } found = t.TensorType; } - if (found == null) - throw new InvalidOperationException( - $"HybridQuant missing tensor entry for group '{group.Name}' (UniqueId={group.UniqueId})."); - - return checked((byte)found.UniqueId); + return found == null + ? TensorWeightScheme.NULL.UniqueId + : checked((byte)found.UniqueId); } - - // Conversion operator: HybridQuant -> TensorConfig - public static explicit operator TensorConfig(HybridQuant h) => new TensorConfig(h); -} - + public static explicit operator TensorConfig(HybridQuant h) => new TensorConfig(h); +} \ No newline at end of file diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 958a9af..c033b82 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -39,15 +39,11 @@ private TensorWeightScheme( public void ResetRuntimeBans() { BannedGroups.Clear(); - foreach (var group in TReg.All.Where(x => _defaultBannedGroupIds.Contains(x.UniqueId))) BannedGroups.Add(group); } - public bool IsBannedFor(TensorGroup group) - { - return BannedGroups.Any(x => x.UniqueId == group.UniqueId); - } + public bool IsBannedFor(TensorGroup group) => BannedGroups.Any(x => x.UniqueId == group.UniqueId); public static void ResetAllRuntimeBans() { @@ -55,59 +51,43 @@ public static void ResetAllRuntimeBans() scheme.ResetRuntimeBans(); } - public static TensorWeightScheme GetSmallestNonImatrix() - { - ValidateSmallestConfiguration(); - - return All.Single(x => - !x.RequiresImatrix && - x.UniqueId != NULL.UniqueId && - x.UniqueId != BF16_F16.UniqueId && - x.IsSmallest); - } - public static void ValidateSmallestConfiguration() { - var marked = All - .Where(x => !x.RequiresImatrix) + var nonImatrixSmallest = All .Where(x => x.UniqueId != NULL.UniqueId) .Where(x => x.UniqueId != BF16_F16.UniqueId) + .Where(x => !x.RequiresImatrix) .Where(x => x.IsSmallest) .ToList(); - if (marked.Count != 1) + if (nonImatrixSmallest.Count != 1) { + string found = nonImatrixSmallest.Count == 0 + ? "none" + : string.Join(", ", nonImatrixSmallest.Select(x => x.Names[0])); + throw new InvalidOperationException( - $"Exactly one non-imatrix TensorWeightScheme must have IsSmallest=true. Found {marked.Count}. " + - $"Marked: [{string.Join(", ", marked.Select(x => x.Names[0]))}]"); + $"Exactly one non-imatrix TensorWeightScheme must have IsSmallest=true. Found: {found}"); } } - public static IReadOnlyList GetExplicitSchemes(bool includeImatrix = true) + public static TensorWeightScheme GetSmallestNonImatrix() { + ValidateSmallestConfiguration(); + return All .Where(x => x.UniqueId != NULL.UniqueId) .Where(x => x.UniqueId != BF16_F16.UniqueId) - .Where(x => includeImatrix || !x.RequiresImatrix) - .ToList(); + .Where(x => !x.RequiresImatrix) + .Single(x => x.IsSmallest); } public static readonly TensorWeightScheme NULL = - new( - 0, - false, - ["NULL"], - Array.Empty(), - null); + new(0, false, ["NULL"], Array.Empty(), null); public static readonly TensorWeightScheme BF16_F16 = - new( - 1, - false, - ["BF16", "F16", "F32"], - Array.Empty(), - null); - + new(1, false, ["BF16", "F16", "F32"], Array.Empty(), null); + /*public static readonly TensorWeightScheme MXFP4 = new( 2, @@ -128,23 +108,12 @@ public static IReadOnlyList GetExplicitSchemes(bool includeI new(4, false, ["Q6_K"], Array.Empty(), 256); public static readonly TensorWeightScheme Q5_K = - new( - 5, - false, - ["Q5_K"], - new[] { TReg.MoeRouter }, - 256); + new(5, false, ["Q5_K"], new[] { TReg.MoeRouter }, 256); public static readonly TensorWeightScheme IQ4_XS = - new( - 6, - false, - ["IQ4_XS"], - new[] { TReg.MoeRouter }, - 32, - true); - - /* + new(6, false, ["IQ4_XS"], new[] { TReg.MoeRouter }, 32, true); + + /* public static TensorWeightScheme IQ4_NL = new( 7, @@ -242,22 +211,22 @@ public static IReadOnlyList GetExplicitSchemes(bool includeI 32 ); */ - - public static readonly ImmutableArray All = - [ - NULL, - BF16_F16, - // MXFP4, - Q8_0, - Q6_K, - Q5_K, - IQ4_XS, - // IQ4_NL, - // IQ3_S, - // IQ3_XS, - // IQ3_XXS, - // IQ2_S, - // IQ2_XS, - // IQ2_XXS - ]; + + public static readonly ImmutableArray All = + [ + NULL, + BF16_F16, + // MXFP4, + Q8_0, + Q6_K, + Q5_K, + IQ4_XS, + // IQ4_NL, + // IQ3_S, + // IQ3_XS, + // IQ3_XXS, + // IQ2_S, + // IQ2_XS, + // IQ2_XXS + ]; } \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 63c1dd1..c8eb0f4 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -53,14 +53,11 @@ public async Task Run(List args) if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); - TensorWeightScheme.ValidateSmallestConfiguration(); - AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); AnsiConsole.Write(new Rule("[yellow]Evolution Configuration[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"Model Path: [blue]{Cache.ModelDirectory}[/]"); AnsiConsole.MarkupLine($"Output Path: [blue]{Cache.ModelMagicQuantDirectory}[/]"); AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length}[/] safe tensors"); - AnsiConsole.MarkupLine($"Smallest non-imatrix scheme: [cyan]{TensorWeightScheme.GetSmallestNonImatrix().Names[0]}[/]"); if (string.IsNullOrEmpty(Cache.LlamaBin)) AnsiConsole.MarkupLine("[yellow]Warning: Llama binaries path not set in Cache. (Did Initialization run?)[/]"); @@ -101,116 +98,88 @@ await benchmarkService.RunAllBenchmarksAsync( var dbService = new QuantDatabaseService(); await dbService.InitializeAsync(); - // --------------------------------------------------------- - // INITIAL ISOLATION PLAN - // --------------------------------------------------------- - AnsiConsole.Write(new Rule("[yellow]Initial Isolation Sample Generation[/]") { Justification = Justify.Left }); + AnsiConsole.Write(new Rule("[yellow]Initial Isolation Startup Samples[/]") { Justification = Justify.Left }); - var initialPlan = TensorConfigGenerator.GenerateRequiredSamplePlan(Cache.UnusedTensorGroups); - AnsiConsole.MarkupLine($"[grey]Queued initial samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); - AnsiConsole.MarkupLine("[grey]SQLite will be treated as the source of truth for completed samples.[/]"); + var initialPlan = TensorConfigGenerator.GenerateInitialIsolationSamplePlan(Cache.UnusedTensorGroups); + AnsiConsole.MarkupLine($"[grey]Queued initial startup samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); var initialSummary = await quantizationService.ProcessHybridBatchAsync(initialPlan.Plans); - AnsiConsole.MarkupLine("[bold green]Initial sample generation phase complete.[/]"); + AnsiConsole.MarkupLine("[bold green]Initial startup sampling complete.[/]"); AnsiConsole.MarkupLine($" [green]Completed:[/] {initialSummary.Completed:N0}"); AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {initialSummary.Skipped:N0}"); AnsiConsole.MarkupLine($" [red]Failed:[/] {initialSummary.Failed:N0}"); - var comboCountBeforeGate = ComboCounter.CountAll(); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Smallest-Probe Gate"); - - // --------------------------------------------------------- - // SMALLEST-PROBE GATE - // --------------------------------------------------------- - AnsiConsole.Write(new Rule("[yellow]Smallest-Probe Isolation Gate[/]") { Justification = Justify.Left }); var isolationOptimizer = new IsolationOptimizationService(); - var gateResult = await isolationOptimizer.ApplyInitialSamplingGateAsync(initialPlan); - foreach (var gd in gateResult.GroupDetails.OrderBy(x => x.GroupName)) - { - AnsiConsole.Write(new Rule($"[yellow]Smallest Probe: {Markup.Escape(gd.GroupName)}[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"[green]Probe scheme:[/] {Markup.Escape(gd.ProbeScheme)}"); - AnsiConsole.MarkupLine($"[green]Reduction:[/] {gd.ReductionRatio:P2}"); - AnsiConsole.MarkupLine($"[green]Continue sampling:[/] {(gd.ContinueSampling ? "[green]yes[/]" : "[red]no[/]")}"); - AnsiConsole.MarkupLine($"[grey]{Markup.Escape(gd.Reason)}[/]"); - } + AnsiConsole.Write(new Rule("[yellow]Initial Probe Analysis[/]") { Justification = Justify.Left }); + var initialAnalysis = await isolationOptimizer.AnalyzeInitialIsolationProbesAsync(initialPlan); + + foreach (var note in initialAnalysis.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Initial Probe Analysis"); - var comboCountAfterGate = ComboCounter.CountAll(); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Smallest-Probe Gate"); + AnsiConsole.Write(new Rule("[yellow]Continuation Isolation Samples[/]") { Justification = Justify.Left }); - // --------------------------------------------------------- - // CONTINUATION ISOLATION PLAN - // --------------------------------------------------------- - var continuationPlan = new RequiredSampleGenerationResult(); - SampleProcessingSummary? continuationSummary = null; + var continuationPlan = TensorConfigGenerator.GenerateContinuationIsolationSamplePlan( + initialAnalysis.GroupsToContinue, + Cache.UnusedTensorGroups); - if (gateResult.GroupIdsToContinue.Count > 0) + if (continuationPlan.TotalCount > 0) { - AnsiConsole.Write(new Rule("[yellow]Continuation Isolation Sample Generation[/]") { Justification = Justify.Left }); - - continuationPlan = TensorConfigGenerator.GenerateContinuationIsolationPlan( - gateResult.GroupIdsToContinue, - Cache.UnusedTensorGroups); - - if (continuationPlan.TotalCount > 0) - { - AnsiConsole.MarkupLine($"[grey]Queued continuation samples:[/] [cyan]{continuationPlan.TotalCount:N0}[/]"); - continuationSummary = await quantizationService.ProcessHybridBatchAsync(continuationPlan.Plans); - - AnsiConsole.MarkupLine("[bold green]Continuation sample generation phase complete.[/]"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {continuationSummary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {continuationSummary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {continuationSummary.Failed:N0}"); - } - else - { - AnsiConsole.MarkupLine("[grey]No continuation isolation samples were required after the smallest-probe gate.[/]"); - } + AnsiConsole.MarkupLine($"[grey]Queued continuation samples:[/] [cyan]{continuationPlan.TotalCount:N0}[/]"); + + var continuationSummary = await quantizationService.ProcessHybridBatchAsync(continuationPlan.Plans); + + AnsiConsole.MarkupLine("[bold green]Continuation sampling complete.[/]"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {continuationSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {continuationSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {continuationSummary.Failed:N0}"); } else { - AnsiConsole.MarkupLine("[grey]No tensor groups survived the smallest-probe gate. Skipping continuation isolation sampling.[/]"); + AnsiConsole.MarkupLine("[grey]No continuation samples were required after smallest-first gating.[/]"); } - // --------------------------------------------------------- - // FINAL ISOLATION PRUNING - // --------------------------------------------------------- - var comboCountBeforeFinalPruning = ComboCounter.CountAll(); - var fullPlan = RequiredSampleGenerationResult.Merge(initialPlan, continuationPlan); + var mergedPlan = initialPlan.MergeWith(continuationPlan); + + var comboCountBefore = ComboCounter.CountAll(); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Final Isolation Optimization"); AnsiConsole.Write(new Rule("[yellow]Final Isolation Optimization[/]") { Justification = Justify.Left }); - var isolationResult = await isolationOptimizer.AnalyzeAndApplyAsync(fullPlan, gateResult); + var isolationResult = await isolationOptimizer.AnalyzeAndApplyFinalAsync(mergedPlan); - var comboCountAfterFinalPruning = ComboCounter.CountAll(); SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Final Isolation Optimization"); foreach (var gd in isolationResult.GroupDetails.OrderBy(x => x.GroupName)) { AnsiConsole.Write(new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"[green]Stopped early:[/] {(gd.StoppedEarly ? "[yellow]yes[/]" : "[green]no[/]")}"); + AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); AnsiConsole.MarkupLine($"[green]Winning scheme:[/] {Markup.Escape(gd.WinningScheme ?? "n/a")}"); + AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); + AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); foreach (var line in gd.Candidates) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); - - foreach (var line in gd.Eliminations) - AnsiConsole.MarkupLine($" [red]- {Markup.Escape(line)}[/]"); } + var comboCountAfterRulePruning = ComboCounter.CountAll(); + await dbService.InitializeAsync(forceRebuild: true); - AnsiConsole.MarkupLine($"[green]Groups stopped early:[/] {gateResult.GroupsStoppedEarly:N0}"); - AnsiConsole.MarkupLine($"[green]Hard-damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); + long predictedSizePruned = await dbService.PrunePredictedLargerThanQ8Async(mergedPlan); + + AnsiConsole.MarkupLine($"[green]Groups reduced to BF16-only:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); + AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); + AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupSchemesBanned:N0}"); + AnsiConsole.MarkupLine($"[green]Bad trade eliminations:[/] {isolationResult.BadTradeEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count before smallest-probe gate:[/] {comboCountBeforeGate:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count after smallest-probe gate:[/] {comboCountAfterGate:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count before final pruning:[/] {comboCountBeforeFinalPruning:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count after final pruning:[/] {comboCountAfterFinalPruning:N0}"); - - foreach (var note in gateResult.Notes) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count after rule pruning:[/] {comboCountAfterRulePruning:N0}"); + AnsiConsole.MarkupLine($"[green]Predicted-size combo removals:[/] {predictedSizePruned:N0}"); foreach (var note in isolationResult.Notes) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); @@ -221,15 +190,12 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine("[bold yellow]Command: evolution[/]"); AnsiConsole.WriteLine("Runs the full evolutionary quantization search algorithm on a target model."); AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Usage:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"\" [options]"); AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Arguments:[/]"); AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Required)"); AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index da53dc0..58e0cb9 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -62,11 +62,13 @@ public static void ValidateCombinationLogicWorks(bool realResults = false) Console.WriteLine("---------------"); Console.WriteLine(); - var samplePlan = TensorConfigGenerator.GenerateRequiredSamplePlan(realResults ? Cache.UnusedTensorGroups : null); + var samplePlan = TensorConfigGenerator.GenerateInitialIsolationSamplePlan( + realResults ? Cache.UnusedTensorGroups : null); + AnsiConsole.MarkupLine($"[bold green]Required pure baselines:[/] {samplePlan.PureBaselineCount:N0}"); AnsiConsole.MarkupLine($"[bold green]Required base-only isolations:[/] {samplePlan.BaseOnlyIsolationCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Required group isolations:[/] {samplePlan.GroupIsolationCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Total required samples:[/] {samplePlan.TotalCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Required smallest-probe isolations:[/] {samplePlan.GroupIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Total required startup samples:[/] {samplePlan.TotalCount:N0}"); } public static void PrintTotalCombinationCount() diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 63ed165..4dd25ef 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -32,19 +32,28 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants continue; } - var ids = schemesForBase - .Where(s => !s.IsBannedFor(group)) - .Select(s => s.UniqueId) - .Distinct() - .ToArray(); + var ids = new List(); - if (ids.Length == 0) + if (!RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)) + ids.Add(TensorWeightScheme.BF16_F16.UniqueId); + + foreach (var scheme in schemesForBase) { - throw new InvalidOperationException( - $"Group '{group.Name}' has no valid tensor schemes for base '{string.Join("/", baseQuant.Names)}'."); + if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + continue; + + if (scheme.IsBannedFor(group)) + continue; + + ids.Add(scheme.UniqueId); } - builder.Add(ids); + ids = ids.Distinct().OrderBy(x => x).ToList(); + + if (ids.Count == 0) + throw new InvalidOperationException($"Group '{group.Name}' has no valid tensor schemes for base '{string.Join("/", baseQuant.Names)}'."); + + builder.Add(ids.ToArray()); } return builder.ToImmutable(); diff --git a/MagicQuant/Helpers/IsolationPruningConfig.cs b/MagicQuant/Helpers/IsolationPruningConfig.cs new file mode 100644 index 0000000..19fe3be --- /dev/null +++ b/MagicQuant/Helpers/IsolationPruningConfig.cs @@ -0,0 +1,14 @@ +namespace MagicQuant.Helpers; + +public static class IsolationPruningConfig +{ + public const double MinimumIsolationReductionToContinueRatio = 0.04d; + public const double MinimumIsolationReductionToSuppressBf16Ratio = 0.10d; + public const double MaximumIsolationPplDeltaPercent = 5.0d; + public const double MaximumIsolationKld = 0.1d; + public const double BadTradeMaxSizeDeltaPercent = 5.0d; + public const double BadTradeKldMultiplier = 2.0d; + public const double BadTradePplMultiplier = 3.0d; + public const double FloatingPointEpsilon = 1e-8d; + public const double MinimumMeaningfulBaseOnlyReductionRatio = 0.01d; +} \ No newline at end of file diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 43221d0..ec60628 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -4,56 +4,52 @@ namespace MagicQuant.Helpers; public static class RuntimeSearchSpace { + private static readonly Dictionary> ExplicitSchemeBansByGroup = new(); private static readonly HashSet DisabledCombinationBaselineIds = new(); + private static readonly HashSet Bf16SuppressedTensorChoiceGroupIds = new(); public static void ResetForNewModel() { + ExplicitSchemeBansByGroup.Clear(); DisabledCombinationBaselineIds.Clear(); + Bf16SuppressedTensorChoiceGroupIds.Clear(); TensorWeightScheme.ResetAllRuntimeBans(); } - public static bool BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) + public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) { - if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId) - return false; + if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + return; - if (scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) - return false; + if (!ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set)) + { + set = new HashSet(); + ExplicitSchemeBansByGroup[group.UniqueId] = set; + } - if (scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) - return false; + set.Add(scheme.UniqueId); - scheme.BannedGroups.Add(group); - return true; + if (!scheme.IsBannedFor(group)) + scheme.BannedGroups.Add(group); } - public static int BanAllExplicitTensorSchemesForGroup(TensorGroup group) + public static void BanAllExplicitTensorSchemesForGroup(TensorGroup group) { - int applied = 0; - - foreach (var scheme in TensorWeightScheme.All) - { - if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId) - continue; - - if (scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) - continue; + foreach (var scheme in TensorWeightScheme.All.Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId && x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId)) + BanSchemeForGroup(group, scheme); + } - if (BanSchemeForGroup(group, scheme)) - applied++; - } + public static IReadOnlyList GetRuntimeExplicitBansForGroup(TensorGroup group) + { + if (!ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set)) + return Array.Empty(); - return applied; + return TensorWeightScheme.All.Where(x => set.Contains(x.UniqueId)).OrderBy(x => x.UniqueId).ToList(); } - public static IReadOnlyList GetRuntimeExplicitBansForGroup(TensorGroup group) + public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeightScheme scheme) { - return TensorWeightScheme.All - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Where(x => x.IsBannedFor(group)) - .OrderBy(x => x.UniqueId) - .ToList(); + return ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set) && set.Contains(scheme.UniqueId); } public static bool IsGroupExplicitQuantBanned(TensorGroup group) @@ -63,20 +59,18 @@ public static bool IsGroupExplicitQuantBanned(TensorGroup group) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) .ToList(); - if (explicitSchemes.Count == 0) - return false; - return explicitSchemes.All(x => x.IsBannedFor(group)); } public static IReadOnlyList GetGroupsWithExplicitQuantBanned() { - return TReg.All - .Where(IsGroupExplicitQuantBanned) - .OrderBy(x => x.UniqueId) - .ToList(); + return TReg.All.Where(IsGroupExplicitQuantBanned).OrderBy(x => x.UniqueId).ToList(); } + public static void SuppressBf16TensorChoice(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); + public static bool IsBf16TensorChoiceSuppressed(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Contains(group.UniqueId); + public static IReadOnlyList GetBf16SuppressedGroups() => TReg.All.Where(x => Bf16SuppressedTensorChoiceGroupIds.Contains(x.UniqueId)).OrderBy(x => x.UniqueId).ToList(); + public static IReadOnlyList GetActiveCombinationBaselines() { return BaselineQuants.All @@ -88,7 +82,7 @@ public static IReadOnlyList GetActiveCombinationBaselines() public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allowDisablingLast = false) { - if (DisabledCombinationBaselineIds.Contains(baseline.UniqueId)) + if (baseline.BaseConversionBase == null || DisabledCombinationBaselineIds.Contains(baseline.UniqueId)) return false; int currentlyActive = GetActiveCombinationBaselines().Count; @@ -99,8 +93,5 @@ public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allo return true; } - public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) - { - return DisabledCombinationBaselineIds.Contains(baseline.UniqueId); - } + public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) => DisabledCombinationBaselineIds.Contains(baseline.UniqueId); } \ No newline at end of file diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index f6db690..2d2e5b3 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -1,7 +1,7 @@ +using System.Numerics; using MQ.DB; using MQ.DB.Models; using Spectre.Console; -using System.Numerics; namespace MagicQuant.Helpers; @@ -29,14 +29,23 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc AnsiConsole.MarkupLine($" [grey]- {string.Join("/", baseline.Names)}[/] (Id={baseline.UniqueId})"); } - var fullyPrunedGroups = RuntimeSearchSpace.GetGroupsWithExplicitQuantBanned(); - AnsiConsole.MarkupLine($"[green]Groups with explicit tensor quant banned:[/] {fullyPrunedGroups.Count}"); - foreach (var group in fullyPrunedGroups) - AnsiConsole.MarkupLine($" [yellow]- {group.Name}[/] (Id={group.UniqueId})"); + var explicitBannedGroups = RuntimeSearchSpace.GetGroupsWithExplicitQuantBanned(); + if (explicitBannedGroups.Count > 0) + { + AnsiConsole.MarkupLine($"[yellow]Explicit-quant-banned groups:[/] {explicitBannedGroups.Count}"); + foreach (var group in explicitBannedGroups) + AnsiConsole.MarkupLine($" [yellow]- {group.Name}[/]"); + } - var unusedIds = Cache.UnusedTensorGroups - .Select(x => x.UniqueId) - .ToHashSet(); + var bf16SuppressedGroups = RuntimeSearchSpace.GetBf16SuppressedGroups(); + if (bf16SuppressedGroups.Count > 0) + { + AnsiConsole.MarkupLine($"[yellow]BF16 tensor-choice suppressed groups:[/] {bf16SuppressedGroups.Count}"); + foreach (var group in bf16SuppressedGroups) + AnsiConsole.MarkupLine($" [yellow]- {group.Name}[/]"); + } + + var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); foreach (var baseline in activeBaselines) { @@ -51,35 +60,24 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc var ids = allowed[i]; baseCount *= ids.Length; - var names = ids - .Select(id => - { - if (id == TensorWeightScheme.NULL.UniqueId) - return "NULL"; - - var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); - return scheme?.Names[0] ?? $"Unknown({id})"; - }) - .ToList(); + var names = ids.Select(id => + { + if (id == TensorWeightScheme.NULL.UniqueId) + return "NULL"; - var runtimeBans = RuntimeSearchSpace.GetRuntimeExplicitBansForGroup(group) - .Select(x => x.Names[0]) - .ToList(); + var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); + return scheme?.Names[0] ?? $"Unknown({id})"; + }).ToList(); string state = unusedIds.Contains(group.UniqueId) ? "unused->NULL" : - RuntimeSearchSpace.IsGroupExplicitQuantBanned(group) ? "explicit-quant-banned" : - runtimeBans.Count > 0 ? "runtime-pruned" : + RuntimeSearchSpace.IsGroupExplicitQuantBanned(group) ? "BF16-only" : + RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group) ? "BF16-suppressed" : "variable"; AnsiConsole.MarkupLine( - $" [cyan]{Markup.Escape(group.Name)}[/] => [green]{ids.Length}[/] choice(s) [grey][[{Markup.Escape(state)}]][/] :: {Markup.Escape(string.Join(", ", names))}"); - - if (runtimeBans.Count > 0) - { - AnsiConsole.MarkupLine( - $" [grey]runtime bans:[/] {Markup.Escape(string.Join(", ", runtimeBans))}"); - } + $" [cyan]{Markup.Escape(group.Name)}[/] => [green]{ids.Length}[/] choice(s) " + + $"[grey][[{Markup.Escape(state)}]][/] :: {Markup.Escape(string.Join(", ", names))}"); } AnsiConsole.MarkupLine($" [bold green]Base total:[/] {baseCount:N0}"); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index e8caf1b..841aacc 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -1,33 +1,25 @@ +using System.Collections.Concurrent; using MQ.DB; using MQ.DB.Models; using Spectre.Console; -using System.Collections.Concurrent; -using System.Collections.Immutable; -using System.Numerics; namespace MagicQuant.Helpers; public static class TensorConfigGenerator { - public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( - List? missingTensorGroups = null) + public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan(List? missingTensorGroups = null) { - TensorWeightScheme.ValidateSmallestConfiguration(); - if (missingTensorGroups != null && !missingTensorGroups.Any()) missingTensorGroups = null; - var skippedIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + var missingIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); var activeGroups = TReg.All - .Where(x => !skippedIds.Contains(x.UniqueId)) + .Where(x => !missingIds.Contains(x.UniqueId)) .OrderBy(x => x.UniqueId) .ToList(); var result = new RequiredSampleGenerationResult(); - // --------------------------------------------------------- - // 1. Pure baselines - // --------------------------------------------------------- foreach (var baseline in BaselineQuants.All.OrderBy(x => x.UniqueId)) { result.Plans.Add(new RequiredSamplePlan @@ -42,127 +34,116 @@ public static RequiredSampleGenerationResult GenerateRequiredSamplePlan( result.PureBaselineCount++; } - // --------------------------------------------------------- - // 2. Base-only isolation for actual combo baselines - // --------------------------------------------------------- foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) { - var quant = HybridQuant.CreateBlanket( - baseQuant: baseline, - groups: activeGroups, - blanketScheme: TensorWeightScheme.BF16_F16); - result.Plans.Add(new RequiredSamplePlan { Kind = RequiredSampleKind.BaseOnlyIsolation, Key = $"baseonly:{baseline.UniqueId}", - Description = - $"Base-only isolation for {string.Join("/", baseline.Names)} with all known groups forced native.", - Quant = quant, + Description = $"Base-only isolation for {string.Join("/", baseline.Names)} with all active groups forced native.", + Quant = HybridQuant.CreateBlanket( + baseQuant: baseline, + groups: activeGroups, + blanketScheme: TensorWeightScheme.BF16_F16), TestedBaselineId = baseline.UniqueId }); result.BaseOnlyIsolationCount++; } - // --------------------------------------------------------- - // 3. Carrier base-only isolation for tensor-group probing - // --------------------------------------------------------- - var groupIsolationCarrier = BaselineQuants.Q8_0; - - var carrierBaseOnly = HybridQuant.CreateBlanket( - baseQuant: groupIsolationCarrier, - groups: activeGroups, - blanketScheme: TensorWeightScheme.BF16_F16); + var carrier = BaselineQuants.Q8_0; result.Plans.Add(new RequiredSamplePlan { Kind = RequiredSampleKind.BaseOnlyIsolation, - Key = $"carrier-baseonly:{groupIsolationCarrier.UniqueId}", - Description = - $"Carrier base-only isolation for {string.Join("/", groupIsolationCarrier.Names)} with all known groups forced native.", - Quant = carrierBaseOnly, - TestedBaselineId = groupIsolationCarrier.UniqueId + Key = $"carrier-baseonly:{carrier.UniqueId}", + Description = "Carrier base-only isolation on Q8 with all active groups forced native.", + Quant = HybridQuant.CreateBlanket( + baseQuant: carrier, + groups: activeGroups, + blanketScheme: TensorWeightScheme.BF16_F16), + TestedBaselineId = carrier.UniqueId }); result.BaseOnlyIsolationCount++; - // --------------------------------------------------------- - // 4. Smallest-first tensor-group isolation probes - // --------------------------------------------------------- + var smallest = TensorWeightScheme.GetSmallestNonImatrix(); + foreach (var group in activeGroups) { - var firstProbe = GetInitialIsolationProbeScheme(group); - if (firstProbe == null) + if (smallest.IsBannedFor(group)) continue; var quant = HybridQuant.CreateBlanket( - baseQuant: groupIsolationCarrier, + baseQuant: carrier, groups: activeGroups, blanketScheme: TensorWeightScheme.BF16_F16); var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); - target.TensorType = firstProbe; + target.TensorType = smallest; result.Plans.Add(new RequiredSamplePlan { - Kind = RequiredSampleKind.GroupIsolation, - Key = $"probefirst:{groupIsolationCarrier.UniqueId}:{group.UniqueId}:{firstProbe.UniqueId}", - Description = - $"Initial isolation probe for group '{group.Name}' using scheme '{firstProbe.Names[0]}' on carrier '{groupIsolationCarrier.Names[0]}'.", + Kind = RequiredSampleKind.GroupIsolationProbe, + Key = $"probe:{carrier.UniqueId}:{group.UniqueId}:{smallest.UniqueId}", + Description = $"Smallest-first probe for group '{group.Name}' using '{smallest.Names[0]}'.", Quant = quant, TargetGroupId = group.UniqueId, - TestedSchemeId = firstProbe.UniqueId, - TestedBaselineId = groupIsolationCarrier.UniqueId + TestedSchemeId = smallest.UniqueId, + TestedBaselineId = carrier.UniqueId, + IsSmallestProbe = true }); result.GroupIsolationCount++; } - AnsiConsole.MarkupLine($"[bold green]Initial pure baseline samples required:[/] {result.PureBaselineCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Initial base-only isolation samples required:[/] {result.BaseOnlyIsolationCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Initial smallest-probe isolation samples required:[/] {result.GroupIsolationCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Initial total samples required:[/] {result.TotalCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Pure baselines required:[/] {result.PureBaselineCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Base-only isolation samples required:[/] {result.BaseOnlyIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Smallest-probe isolation samples required:[/] {result.GroupIsolationCount:N0}"); + AnsiConsole.MarkupLine($"[bold green]Total initial startup samples:[/] {result.TotalCount:N0}"); return result; } - public static RequiredSampleGenerationResult GenerateContinuationIsolationPlan( + public static RequiredSampleGenerationResult GenerateContinuationIsolationSamplePlan( IEnumerable groupIdsToContinue, List? missingTensorGroups = null) { - if (groupIdsToContinue == null) - throw new ArgumentNullException(nameof(groupIdsToContinue)); - - TensorWeightScheme.ValidateSmallestConfiguration(); - if (missingTensorGroups != null && !missingTensorGroups.Any()) missingTensorGroups = null; - var skippedIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); - var continueIds = groupIdsToContinue.ToHashSet(); + var continueIds = groupIdsToContinue.Distinct().ToHashSet(); + var missingIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + var activeGroups = TReg.All - .Where(x => !skippedIds.Contains(x.UniqueId)) .Where(x => continueIds.Contains(x.UniqueId)) + .Where(x => !missingIds.Contains(x.UniqueId)) .OrderBy(x => x.UniqueId) .ToList(); var result = new RequiredSampleGenerationResult(); - var groupIsolationCarrier = BaselineQuants.Q8_0; + var carrier = BaselineQuants.Q8_0; + var smallest = TensorWeightScheme.GetSmallestNonImatrix(); + + var schemes = TensorWeightScheme.All + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .OrderBy(x => x.UniqueId) + .ToList(); foreach (var group in activeGroups) { - var allCandidates = GetOrderedIsolationCandidateSchemes(group); - var firstProbe = GetInitialIsolationProbeScheme(group); - - foreach (var scheme in allCandidates) + foreach (var scheme in schemes) { - if (firstProbe != null && scheme.UniqueId == firstProbe.UniqueId) + if (scheme.UniqueId == smallest.UniqueId) + continue; + + if (scheme.IsBannedFor(group)) continue; var quant = HybridQuant.CreateBlanket( - baseQuant: groupIsolationCarrier, - groups: TReg.All.Where(x => !skippedIds.Contains(x.UniqueId)).OrderBy(x => x.UniqueId), + baseQuant: carrier, + groups: TReg.All.Where(x => !missingIds.Contains(x.UniqueId)), blanketScheme: TensorWeightScheme.BF16_F16); var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); @@ -170,14 +151,13 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationPlan( result.Plans.Add(new RequiredSamplePlan { - Kind = RequiredSampleKind.GroupIsolation, - Key = $"group:{groupIsolationCarrier.UniqueId}:{group.UniqueId}:{scheme.UniqueId}", - Description = - $"Follow-up isolation sample for group '{group.Name}' using scheme '{scheme.Names[0]}' on carrier '{groupIsolationCarrier.Names[0]}'.", + Kind = RequiredSampleKind.GroupIsolationContinuation, + Key = $"cont:{carrier.UniqueId}:{group.UniqueId}:{scheme.UniqueId}", + Description = $"Continuation isolation for group '{group.Name}' using '{scheme.Names[0]}'.", Quant = quant, TargetGroupId = group.UniqueId, TestedSchemeId = scheme.UniqueId, - TestedBaselineId = groupIsolationCarrier.UniqueId + TestedBaselineId = carrier.UniqueId }); result.GroupIsolationCount++; @@ -185,27 +165,14 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationPlan( } AnsiConsole.MarkupLine($"[bold green]Continuation isolation samples required:[/] {result.GroupIsolationCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Continuation total samples required:[/] {result.TotalCount:N0}"); - return result; } - private static TensorWeightScheme? GetInitialIsolationProbeScheme(TensorGroup group) - { - var ordered = GetOrderedIsolationCandidateSchemes(group); - - return ordered.FirstOrDefault(x => !x.RequiresImatrix) ?? ordered.FirstOrDefault(); - } - - private static List GetOrderedIsolationCandidateSchemes(TensorGroup group) + public static List GenerateRequiredDataSampleCombos(List? missingTensorGroups = null) { - return TensorWeightScheme.All - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Where(x => !x.IsBannedFor(group)) - .OrderByDescending(x => !x.RequiresImatrix && x.IsSmallest) - .ThenBy(x => x.RequiresImatrix ? 1 : 0) - .ThenByDescending(x => x.UniqueId) + return GenerateInitialIsolationSamplePlan(missingTensorGroups) + .Plans + .Select(x => x.Quant) .ToList(); } @@ -231,87 +198,98 @@ public static IEnumerable> GenerateTensorConfigBatches( for (int i = 0; i < allowed.Length; i++) { if (allowed[i] == null) - throw new InvalidOperationException( - $"Allowed[{i}] is null for base {string.Join("/", baseQuant.Names)}."); + throw new InvalidOperationException($"Allowed[{i}] is null for base {string.Join("/", baseQuant.Names)}."); if (allowed[i].Length == 0) - throw new InvalidOperationException( - $"Allowed[{i}] is empty for base {string.Join("/", baseQuant.Names)}."); + throw new InvalidOperationException($"Allowed[{i}] is empty for base {string.Join("/", baseQuant.Names)}."); } int dims = allowed.Length; int dop = ComputeWorkerThreads(GetThreadCountSafe()); byte baseId = baseQuant.UniqueId; - BigInteger total = BigInteger.One; - for (int i = 0; i < dims; i++) - total *= allowed[i].Length; - - if (total == BigInteger.Zero) - yield break; + var queue = new BlockingCollection>(boundedCapacity: Math.Max(2, dop * 2)); - var buffer = new ConcurrentQueue(); - var produced = 0L; - - Parallel.ForEach( - Partitioner.Create(0L, (long)total), - new ParallelOptions { MaxDegreeOfParallelism = dop, CancellationToken = ct }, - range => + var producer = Task.Run(() => + { + try { - var local = new List(Math.Min(batchSize, 8192)); - - for (long flat = range.Item1; flat < range.Item2; flat++) - { - ct.ThrowIfCancellationRequested(); - - long n = flat; - Span chosen = stackalloc byte[dims]; - - for (int d = dims - 1; d >= 0; d--) + Parallel.ForEach( + Partitioner.Create(0, allowed[0].Length), + new ParallelOptions { MaxDegreeOfParallelism = dop, CancellationToken = ct }, + range => { - var arr = allowed[d]; - int len = arr.Length; - int idx = (int)(n % len); - chosen[d] = arr[idx]; - n /= len; - } - - local.Add(new TensorConfig( - baseId, - chosen[0], - chosen[1], - chosen[2], - chosen[3], - chosen[4], - chosen[5], - chosen[6], - chosen[7], - chosen[8])); - - if (local.Count >= batchSize) - { - foreach (var item in local) - buffer.Enqueue(item); - - local.Clear(); - } - } - - foreach (var item in local) - buffer.Enqueue(item); - }); - - while (!buffer.IsEmpty) - { - var batch = new List(batchSize); + var batch = new List(Math.Min(batchSize, 250_000)); + var idx = new int[dims]; + + var d0 = allowed[0]; + var d1 = allowed[1]; + var d2 = allowed[2]; + var d3 = allowed[3]; + var d4 = allowed[4]; + var d5 = allowed[5]; + var d6 = allowed[6]; + var d7 = allowed[7]; + var d8 = allowed[8]; + + for (int i0 = range.Item1; i0 < range.Item2; i0++) + { + ct.ThrowIfCancellationRequested(); + + idx[0] = i0; + Array.Clear(idx, 1, dims - 1); + + while (true) + { + batch.Add(new TensorConfig( + baseQuant: baseId, + embeddings: d0[idx[0]], + lmHead: d1[idx[1]], + attnQ: d2[idx[2]], + attnKV: d3[idx[3]], + attnOutput: d4[idx[4]], + ffnUpGate: d5[idx[5]], + ffnDown: d6[idx[6]], + moeExperts: d7[idx[7]], + moeRouter: d8[idx[8]] + )); + + if (batch.Count >= batchSize) + { + queue.Add(batch, ct); + batch = new List(Math.Min(batchSize, 250_000)); + } + + int d = dims - 1; + while (d >= 1) + { + idx[d]++; + if (idx[d] < allowed[d].Length) + break; + + idx[d] = 0; + d--; + } + + if (d < 1) + break; + } + } + + if (batch.Count > 0) + queue.Add(batch, ct); + }); + } + finally + { + queue.CompleteAdding(); + } + }, ct); - while (batch.Count < batchSize && buffer.TryDequeue(out var cfg)) - batch.Add(cfg); + foreach (var batch in queue.GetConsumingEnumerable(ct)) + yield return batch; - produced += batch.Count; - if (batch.Count > 0) - yield return batch; - } + producer.GetAwaiter().GetResult(); } private static int GetThreadCountSafe() @@ -320,11 +298,16 @@ private static int GetThreadCountSafe() return Math.Max(1, tc); } - private static int ComputeWorkerThreads(int logicalThreads) + private static int ComputeWorkerThreads(int threadCount) { - if (logicalThreads <= 2) return 1; - if (logicalThreads <= 4) return 2; - if (logicalThreads <= 8) return 4; - return Math.Max(4, logicalThreads / 2); + if (threadCount <= 1) + return 1; + + int workers = + threadCount < 16 + ? threadCount - 1 + : (int)Math.Floor(threadCount * 0.90); + + return Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); } } \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 36fed58..80cbf45 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -2,123 +2,135 @@ using MQ.DB; using MQ.DB.Data; using MQ.DB.Models; -using MQ.DB.Models.DbModels; using Microsoft.EntityFrameworkCore; namespace MagicQuant.Services; public sealed class IsolationOptimizationOptions { - public double MinMeaningfulBaseOnlyReductionRatio { get; set; } = IsolationRules.MinimumMeaningfulBaseOnlyReductionRatio; -} - -public sealed class IsolationSamplingGateDecision -{ - public string GroupName { get; set; } = string.Empty; - public string ProbeScheme { get; set; } = string.Empty; - public double ReductionRatio { get; set; } - public bool ContinueSampling { get; set; } - public string Reason { get; set; } = string.Empty; - public ulong? ProbeSizeBytes { get; set; } -} + public double MinMeaningfulGroupReductionRatio { get; set; } = + IsolationPruningConfig.MinimumIsolationReductionToContinueRatio; -public sealed class IsolationSamplingGateResult -{ - public int GroupsStoppedEarly { get; set; } - public List GroupIdsToContinue { get; set; } = new(); - public List Notes { get; set; } = new(); - public List GroupDetails { get; set; } = new(); + public double MinMeaningfulBaseOnlyReductionRatio { get; set; } = + IsolationPruningConfig.MinimumMeaningfulBaseOnlyReductionRatio; } public sealed class IsolationGroupDecision { public string GroupName { get; set; } = string.Empty; - public bool StoppedEarly { get; set; } + public double BestReductionRatio { get; set; } + + public bool ExplicitQuantBanned { get; set; } + public bool Bf16Suppressed { get; set; } + public string? WinningScheme { get; set; } public ulong? WinningSizeBytes { get; set; } public double? WinningKld { get; set; } public double? WinningPplDelta { get; set; } + public List Candidates { get; set; } = new(); - public List Eliminations { get; set; } = new(); +} + +public sealed class InitialIsolationAnalysisResult +{ + public List GroupsToContinue { get; set; } = new(); + public List Notes { get; set; } = new(); + public List GroupDetails { get; set; } = new(); } public sealed class IsolationOptimizationResult { - public int GroupsStoppedEarly { get; set; } - public int HardDamageEliminations { get; set; } + public int ExplicitQuantBannedGroups { get; set; } public int DominatedGroupSchemesBanned { get; set; } + public int HardDamageEliminations { get; set; } + public int BadTradeEliminations { get; set; } public int DisabledBaselines { get; set; } + public int Bf16SuppressedGroups { get; set; } + public List Notes { get; set; } = new(); public List GroupDetails { get; set; } = new(); } public class IsolationOptimizationService { - public async Task ApplyInitialSamplingGateAsync( - RequiredSampleGenerationResult initialPlan, + public async Task AnalyzeInitialIsolationProbesAsync( + RequiredSampleGenerationResult plan, + IsolationOptimizationOptions? options = null, CancellationToken ct = default) { - if (initialPlan == null) - throw new ArgumentNullException(nameof(initialPlan)); + options ??= new IsolationOptimizationOptions(); - var result = new IsolationSamplingGateResult(); + var result = new InitialIsolationAnalysisResult(); + + var nativeBaseline = await LoadSnapshotAsync( + HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) + ?? throw new InvalidOperationException("Native BF16 baseline benchmark was not found."); var carrierBaselineId = BaselineQuants.Q8_0.UniqueId; - var carrierBaseOnlyPlan = initialPlan.Plans.FirstOrDefault(x => + var carrierBaseOnlyPlan = plan.Plans.First(x => x.Kind == RequiredSampleKind.BaseOnlyIsolation && - x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal) && - x.TestedBaselineId == carrierBaselineId); - - if (carrierBaseOnlyPlan == null) - throw new InvalidOperationException("Carrier base-only isolation plan was not found."); + x.TestedBaselineId == carrierBaselineId && + x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal)); var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct) - ?? throw new InvalidOperationException("Carrier base-only isolation benchmark was not found in SQLite."); + ?? throw new InvalidOperationException("Carrier base-only benchmark was not found."); - var probePlans = initialPlan.Plans - .Where(x => x.Kind == RequiredSampleKind.GroupIsolation) - .Where(x => x.Key.StartsWith("probefirst:", StringComparison.Ordinal)) - .OrderBy(x => x.TargetGroupId) + var groupPlans = plan.Plans + .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe) + .Where(x => x.TestedBaselineId == carrierBaselineId) + .GroupBy(x => x.TargetGroupId!.Value) + .OrderBy(x => x.Key) .ToList(); - foreach (var plan in probePlans) + foreach (var groupSet in groupPlans) { - var group = TReg.All.First(x => x.UniqueId == plan.TargetGroupId!.Value); - var scheme = TensorWeightScheme.All.First(x => x.UniqueId == plan.TestedSchemeId!.Value); + var group = TReg.All.First(x => x.UniqueId == groupSet.Key); - var snapshot = await LoadSnapshotAsync(plan.Quant, ct) - ?? throw new InvalidOperationException( - $"Initial isolation probe benchmark was missing for group '{group.Name}' and scheme '{scheme.Names[0]}'."); + var item = groupSet.Single(); + var snap = await LoadSnapshotAsync(item.Quant, ct); + if (snap == null) + continue; - double reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snapshot.SizeBytes); + var scheme = TensorWeightScheme.All.First(x => x.UniqueId == item.TestedSchemeId); + var reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.SizeBytes); + var kld = GetAggregateKld(snap); + var pplDelta = GetAggregatePplDeltaPercent(snap, nativeBaseline); - var decision = new IsolationSamplingGateDecision + var decision = new IsolationGroupDecision { GroupName = group.Name, - ProbeScheme = scheme.Names[0], - ReductionRatio = reduction, - ProbeSizeBytes = snapshot.SizeBytes + BestReductionRatio = reduction, + WinningScheme = scheme.Names[0], + WinningSizeBytes = snap.SizeBytes, + WinningKld = kld, + WinningPplDelta = pplDelta }; - if (reduction < IsolationRules.MinimumIsolationReductionToContinue) + decision.Candidates.Add( + $"{scheme.Names[0]} | size={(snap.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={reduction:P2} | kld={kld:G6} | pplΔ={pplDelta:F4}%"); + + if (reduction < options.MinMeaningfulGroupReductionRatio) { RuntimeSearchSpace.BanAllExplicitTensorSchemesForGroup(group); - result.GroupsStoppedEarly++; - - decision.ContinueSampling = false; - decision.Reason = - $"Stopped early because smallest explicit probe reduction was only {reduction:P2}, below the {IsolationRules.MinimumIsolationReductionToContinue:P2} gate."; + decision.ExplicitQuantBanned = true; result.Notes.Add( - $"Stopped isolated explicit sampling for '{group.Name}' because smallest probe '{scheme.Names[0]}' only reduced the model by {reduction:P2}."); + $"Early stop for '{group.Name}': smallest non-imatrix '{scheme.Names[0]}' only saved {reduction:P2}, below {options.MinMeaningfulGroupReductionRatio:P2}. Explicit tensor quant exploration removed for this group."); + + result.GroupDetails.Add(decision); + continue; } - else + + result.GroupsToContinue.Add(group.UniqueId); + + if (reduction >= IsolationPruningConfig.MinimumIsolationReductionToSuppressBf16Ratio) { - result.GroupIdsToContinue.Add(group.UniqueId); - decision.ContinueSampling = true; - decision.Reason = - $"Continuing sampling because smallest explicit probe reduction was {reduction:P2}."; + RuntimeSearchSpace.SuppressBf16TensorChoice(group); + decision.Bf16Suppressed = true; + + result.Notes.Add( + $"Suppressed BF16 tensor-choice for '{group.Name}' because smallest probe already saved {reduction:P2}."); } result.GroupDetails.Add(decision); @@ -127,50 +139,41 @@ public async Task ApplyInitialSamplingGateAsync( return result; } - public async Task AnalyzeAndApplyAsync( - RequiredSampleGenerationResult plan, - IsolationSamplingGateResult? gateResult = null, + public async Task AnalyzeAndApplyFinalAsync( + RequiredSampleGenerationResult fullPlan, IsolationOptimizationOptions? options = null, CancellationToken ct = default) { - if (plan == null) - throw new ArgumentNullException(nameof(plan)); - options ??= new IsolationOptimizationOptions(); - var result = new IsolationOptimizationResult - { - GroupsStoppedEarly = gateResult?.GroupsStoppedEarly ?? 0 - }; + var result = new IsolationOptimizationResult(); var nativeBaseline = await LoadSnapshotAsync( HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) - ?? throw new InvalidOperationException("Native BF16/F16/F32 baseline benchmark was not found in SQLite."); + ?? throw new InvalidOperationException("Native BF16 baseline benchmark was not found."); - // ============================ - // GROUP ISOLATION ANALYSIS - // ============================ - var groupPlans = plan.Plans - .Where(x => x.Kind == RequiredSampleKind.GroupIsolation) - .Where(x => x.TestedBaselineId == BaselineQuants.Q8_0.UniqueId) + var carrierBaselineId = BaselineQuants.Q8_0.UniqueId; + + var carrierBaseOnlyPlan = fullPlan.Plans.First(x => + x.Kind == RequiredSampleKind.BaseOnlyIsolation && + x.TestedBaselineId == carrierBaselineId && + x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal)); + + var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct) + ?? throw new InvalidOperationException("Carrier base-only benchmark was not found."); + + var groupPlans = fullPlan.Plans + .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe || x.Kind == RequiredSampleKind.GroupIsolationContinuation) + .Where(x => x.TestedBaselineId == carrierBaselineId) .GroupBy(x => x.TargetGroupId!.Value) .OrderBy(x => x.Key) .ToList(); - var stoppedEarlyIds = gateResult?.GroupIdsToContinue == null - ? new HashSet() - : TReg.All.Select(x => x.UniqueId).Except(gateResult.GroupIdsToContinue).ToHashSet(); - foreach (var groupSet in groupPlans) { var group = TReg.All.First(x => x.UniqueId == groupSet.Key); - var decision = new IsolationGroupDecision - { - GroupName = group.Name, - StoppedEarly = gateResult?.GroupDetails.Any(x => x.GroupName == group.Name && !x.ContinueSampling) == true - }; - var candidates = new List(); + var candidates = new List(); foreach (var item in groupSet) { @@ -178,120 +181,92 @@ public async Task AnalyzeAndApplyAsync( if (snap == null) continue; - var scheme = TensorWeightScheme.All.First(x => x.UniqueId == item.TestedSchemeId!.Value); - var avgKld = GetAggregateKld(snap); - var pplDelta = GetAggregatePplDelta(snap, nativeBaseline); + var scheme = TensorWeightScheme.All.First(x => x.UniqueId == item.TestedSchemeId); - var candidate = new CandidateEvaluation + candidates.Add(new GroupCandidate { - Plan = item, + Group = group, Scheme = scheme, - Snapshot = snap, - AggregateKld = avgKld, - AggregatePplDelta = pplDelta - }; - - decision.Candidates.Add( - $"{scheme.Names[0]} | size={(snap.SizeBytes / 1024.0 / 1024.0):F2} MiB | avgKLD={avgKld:G6} | avgΔPPL={pplDelta:P4}"); - - candidates.Add(candidate); + SizeBytes = snap.SizeBytes, + SavingsRatio = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.SizeBytes), + Kld = GetAggregateKld(snap), + PplDeltaPercent = GetAggregatePplDeltaPercent(snap, nativeBaseline) + }); } if (candidates.Count == 0) - { - result.GroupDetails.Add(decision); continue; - } - // ------------------------------------------------------ - // Hard damage elimination - // ------------------------------------------------------ - foreach (var candidate in candidates) + var decision = new IsolationGroupDecision { - if (candidate.Eliminated) - continue; + GroupName = group.Name, + BestReductionRatio = candidates.Max(x => x.SavingsRatio) + }; - bool pplTooHigh = candidate.AggregatePplDelta >= IsolationRules.MaximumIsolationPplDeltaRatio; - bool kldTooHigh = candidate.AggregateKld >= IsolationRules.MaximumIsolationKld; + foreach (var candidate in candidates.ToList()) + { + if (candidate.Scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + continue; - if (!pplTooHigh && !kldTooHigh) + if (candidate.Scheme.RequiresImatrix) continue; - candidate.Eliminated = true; - candidate.EliminationReason = pplTooHigh && kldTooHigh - ? $"hard-damage: avgΔPPL={candidate.AggregatePplDelta:P4} and avgKLD={candidate.AggregateKld:G6} exceeded thresholds" - : pplTooHigh - ? $"hard-damage: avgΔPPL={candidate.AggregatePplDelta:P4} exceeded {IsolationRules.MaximumIsolationPplDeltaRatio:P2}" - : $"hard-damage: avgKLD={candidate.AggregateKld:G6} exceeded {IsolationRules.MaximumIsolationKld:G6}"; + bool hardFail = + candidate.PplDeltaPercent >= IsolationPruningConfig.MaximumIsolationPplDeltaPercent || + candidate.Kld >= IsolationPruningConfig.MaximumIsolationKld; - if (RuntimeSearchSpace.BanSchemeForGroup(group, candidate.Scheme)) - result.HardDamageEliminations++; + if (!hardFail) + continue; - decision.Eliminations.Add($"{candidate.Scheme.Names[0]} -> {candidate.EliminationReason}"); - result.Notes.Add($"Eliminated '{candidate.Scheme.Names[0]}' for '{group.Name}' due to {candidate.EliminationReason}."); - } + RuntimeSearchSpace.BanSchemeForGroup(group, candidate.Scheme); + result.HardDamageEliminations++; - // ------------------------------------------------------ - // Dominance elimination (non-imatrix only) - // ------------------------------------------------------ - var nonImatrixSurvivors = candidates - .Where(x => !x.Eliminated) - .Where(x => !x.Scheme.RequiresImatrix) - .ToList(); + result.Notes.Add( + $"Hard damage elimination: '{candidate.Scheme.Names[0]}' removed for '{group.Name}' " + + $"(savings={candidate.SavingsRatio:P2}, KLD={candidate.Kld:G6}, PPLΔ={candidate.PplDeltaPercent:F4}%)."); + } - for (int i = 0; i < nonImatrixSurvivors.Count; i++) - { - var a = nonImatrixSurvivors[i]; - if (a.Eliminated) - continue; + candidates = FilterSurvivors(group, candidates); - for (int j = 0; j < nonImatrixSurvivors.Count; j++) - { - if (i == j) - continue; + ApplyDominanceElimination(group, candidates, result); - var b = nonImatrixSurvivors[j]; - if (b.Eliminated) - continue; + candidates = FilterSurvivors(group, candidates); - if (Dominates(a, b)) - { - b.Eliminated = true; - b.EliminationReason = - $"dominance: {a.Scheme.Names[0]} was same size or smaller and no worse on KLD/PPL with at least one strict win"; + ApplyBadTradeElimination(group, candidates, result); - if (RuntimeSearchSpace.BanSchemeForGroup(group, b.Scheme)) - result.DominatedGroupSchemesBanned++; + candidates = FilterSurvivors(group, candidates) + .OrderBy(x => x.Kld) + .ThenBy(x => x.PplDeltaPercent) + .ThenByDescending(x => x.SavingsRatio) + .ToList(); - decision.Eliminations.Add($"{b.Scheme.Names[0]} -> {b.EliminationReason}"); - result.Notes.Add($"Eliminated '{b.Scheme.Names[0]}' for '{group.Name}' because '{a.Scheme.Names[0]}' clearly dominated it."); - } - } + if (candidates.Count == 0) + { + decision.ExplicitQuantBanned = RuntimeSearchSpace.IsGroupExplicitQuantBanned(group); + decision.Bf16Suppressed = RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group); + result.GroupDetails.Add(decision); + continue; } - var survivors = candidates - .Where(x => !x.Eliminated) - .OrderBy(x => x.AggregateKld) - .ThenBy(x => x.AggregatePplDelta) - .ThenBy(x => x.Snapshot.SizeBytes) - .ToList(); + var winner = candidates.First(); + + decision.WinningScheme = winner.Scheme.Names[0]; + decision.WinningSizeBytes = winner.SizeBytes; + decision.WinningKld = winner.Kld; + decision.WinningPplDelta = winner.PplDeltaPercent; + decision.ExplicitQuantBanned = RuntimeSearchSpace.IsGroupExplicitQuantBanned(group); + decision.Bf16Suppressed = RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group); - if (survivors.Count > 0) + foreach (var candidate in candidates.OrderBy(x => x.SizeBytes)) { - var winner = survivors[0]; - decision.WinningScheme = winner.Scheme.Names[0]; - decision.WinningSizeBytes = winner.Snapshot.SizeBytes; - decision.WinningKld = winner.AggregateKld; - decision.WinningPplDelta = winner.AggregatePplDelta; + decision.Candidates.Add( + $"{candidate.Scheme.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}%"); } result.GroupDetails.Add(decision); } - // ============================ - // BASELINE PRUNING - // ============================ - var baseOnlyPlans = plan.Plans + var baseOnlyPlans = fullPlan.Plans .Where(x => x.Kind == RequiredSampleKind.BaseOnlyIsolation) .Where(x => x.Key.StartsWith("baseonly:", StringComparison.Ordinal)) .ToList(); @@ -312,29 +287,122 @@ public async Task AnalyzeAndApplyAsync( { result.DisabledBaselines++; result.Notes.Add( - $"Disabled baseline '{baseline.Names[0]}' because uncovered-tensor reduction was only {reduction:P2}."); + $"Disabled combination baseline '{baseline.Names[0]}' because uncovered-tensor reduction was only {reduction:P2}."); } } } + result.ExplicitQuantBannedGroups = RuntimeSearchSpace.GetGroupsWithExplicitQuantBanned().Count; + result.Bf16SuppressedGroups = RuntimeSearchSpace.GetBf16SuppressedGroups().Count; + return result; } - private static bool Dominates(CandidateEvaluation a, CandidateEvaluation b) + private static List FilterSurvivors(TensorGroup group, List candidates) { - if (a.Scheme.RequiresImatrix || b.Scheme.RequiresImatrix) - return false; + return candidates + .Where(x => x.Scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId || !RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, x.Scheme)) + .ToList(); + } - bool sizeNoWorse = a.Snapshot.SizeBytes <= b.Snapshot.SizeBytes; - bool kldNoWorse = a.AggregateKld <= b.AggregateKld + IsolationRules.MetricComparisonEpsilon; - bool pplNoWorse = a.AggregatePplDelta <= b.AggregatePplDelta + IsolationRules.MetricComparisonEpsilon; + private static void ApplyDominanceElimination( + TensorGroup group, + List candidates, + IsolationOptimizationResult result) + { + var explicitCandidates = candidates + .Where(x => x.Scheme.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Where(x => !x.Scheme.RequiresImatrix) + .ToList(); - bool strictlyBetter = - a.Snapshot.SizeBytes < b.Snapshot.SizeBytes || - a.AggregateKld + IsolationRules.MetricComparisonEpsilon < b.AggregateKld || - a.AggregatePplDelta + IsolationRules.MetricComparisonEpsilon < b.AggregatePplDelta; + for (int i = 0; i < explicitCandidates.Count; i++) + { + for (int j = 0; j < explicitCandidates.Count; j++) + { + if (i == j) + continue; + + var a = explicitCandidates[i]; + var b = explicitCandidates[j]; + + bool sameOrSmaller = a.SizeBytes <= b.SizeBytes; + bool kldNoWorse = a.Kld <= b.Kld + IsolationPruningConfig.FloatingPointEpsilon; + bool pplNoWorse = a.PplDeltaPercent <= b.PplDeltaPercent + IsolationPruningConfig.FloatingPointEpsilon; + + bool strictlyBetter = + a.Kld + IsolationPruningConfig.FloatingPointEpsilon < b.Kld || + a.PplDeltaPercent + IsolationPruningConfig.FloatingPointEpsilon < b.PplDeltaPercent || + a.SizeBytes < b.SizeBytes; + + if (sameOrSmaller && kldNoWorse && pplNoWorse && strictlyBetter) + { + if (!RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, b.Scheme)) + { + RuntimeSearchSpace.BanSchemeForGroup(group, b.Scheme); + result.DominatedGroupSchemesBanned++; - return sizeNoWorse && kldNoWorse && pplNoWorse && strictlyBetter; + result.Notes.Add( + $"Dominance elimination: '{b.Scheme.Names[0]}' removed for '{group.Name}' because '{a.Scheme.Names[0]}' was same-size-or-smaller and no worse on KLD/PPL."); + } + } + } + } + } + + private static void ApplyBadTradeElimination( + TensorGroup group, + List candidates, + IsolationOptimizationResult result) + { + var explicitCandidates = candidates + .Where(x => x.Scheme.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Where(x => !x.Scheme.RequiresImatrix) + .ToList(); + + for (int i = 0; i < explicitCandidates.Count; i++) + { + for (int j = 0; j < explicitCandidates.Count; j++) + { + if (i == j) + continue; + + var a = explicitCandidates[i]; + var b = explicitCandidates[j]; + + if (a.SizeBytes >= b.SizeBytes) + continue; + + double sizeDeltaPercent = (b.SizeBytes - a.SizeBytes) / (double)b.SizeBytes * 100.0; + if (sizeDeltaPercent > IsolationPruningConfig.BadTradeMaxSizeDeltaPercent) + continue; + + double kldRatio = b.Kld <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : a.Kld / b.Kld; + + double pplRatio = b.PplDeltaPercent <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : a.PplDeltaPercent / b.PplDeltaPercent; + + bool badTrade = + a.Kld > b.Kld * IsolationPruningConfig.BadTradeKldMultiplier || + a.PplDeltaPercent > b.PplDeltaPercent * IsolationPruningConfig.BadTradePplMultiplier; + + if (!badTrade) + continue; + + if (!RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, a.Scheme)) + { + RuntimeSearchSpace.BanSchemeForGroup(group, a.Scheme); + result.BadTradeEliminations++; + + result.Notes.Add( + $"Bad trade elimination: '{a.Scheme.Names[0]}' removed vs '{b.Scheme.Names[0]}' for '{group.Name}'. " + + $"Reason: small size gain ({sizeDeltaPercent:F2}%) but disproportionate damage " + + $"(KLD x{kldRatio:F2}, PPL x{pplRatio:F2})."); + } + } + } } private async Task LoadSnapshotAsync(HybridQuant quant, CancellationToken ct) @@ -347,7 +415,7 @@ private static bool Dominates(CandidateEvaluation a, CandidateEvaluation b) if (model == null) return null; - var lookup = BuildLookup(quant); + var lookup = (TensorConfig)quant; var row = await db.AiBenchmarks .Include(x => x.CategorBenchmarks) @@ -372,106 +440,80 @@ private static bool Dominates(CandidateEvaluation a, CandidateEvaluation b) if (row == null) return null; - var snapshot = new BenchmarkSnapshot { SizeBytes = row.b.SizeBytes }; - - foreach (var cat in row.b.CategorBenchmarks) + return new BenchmarkSnapshot { - snapshot.Domains[cat.Category.ToString()] = new BenchmarkDomainSnapshot - { - Kld = cat.Kld, - Ppl = cat.Ppl, - PplError = cat.PplError - }; - } - - return snapshot; + SizeBytes = row.b.SizeBytes, + Benchmarks = row.b.CategorBenchmarks + .Select(x => new CategorySnapshot + { + Category = x.Category, + Kld = x.Kld, + Ppl = x.Ppl, + PplError = x.PplError + }) + .ToList() + }; } - private static double ComputeReductionRatio(ulong nativeSize, ulong candidateSize) - => (nativeSize == 0 || candidateSize >= nativeSize) - ? 0d - : (nativeSize - candidateSize) / (double)nativeSize; + private static double ComputeReductionRatio(ulong baselineBytes, ulong candidateBytes) + { + if (baselineBytes == 0) + return 0d; + + double delta = baselineBytes - candidateBytes; + return delta / baselineBytes; + } - private static double GetAggregateKld(BenchmarkSnapshot s) - => s.Domains.Values + private static double GetAggregateKld(BenchmarkSnapshot snapshot) + { + return snapshot.Benchmarks .Where(x => x.Kld.HasValue) .Select(x => x.Kld!.Value) - .DefaultIfEmpty(double.MaxValue) + .DefaultIfEmpty(double.PositiveInfinity) .Average(); + } - private static double GetAggregatePplDelta(BenchmarkSnapshot s, BenchmarkSnapshot n) + private static double GetAggregatePplDeltaPercent(BenchmarkSnapshot snapshot, BenchmarkSnapshot nativeBaseline) { - var list = new List(); + var nativeMap = nativeBaseline.Benchmarks.ToDictionary(x => x.Category); + var deltas = new List(); - foreach (var kv in s.Domains) + foreach (var bench in snapshot.Benchmarks) { - if (!n.Domains.TryGetValue(kv.Key, out var native)) + if (!nativeMap.TryGetValue(bench.Category, out var native)) continue; - if (native.Ppl <= 0) + if (native.Ppl <= IsolationPruningConfig.FloatingPointEpsilon) continue; - list.Add(Math.Abs(kv.Value.Ppl - native.Ppl) / native.Ppl); + double deltaPercent = ((bench.Ppl - native.Ppl) / native.Ppl) * 100.0; + deltas.Add(deltaPercent); } - return list.Count == 0 ? double.MaxValue : list.Average(); + return deltas.Count == 0 ? double.PositiveInfinity : deltas.Average(); } - private static TensorLookup BuildLookup(HybridQuant quant) + private sealed class GroupCandidate { - byte Get(TensorGroup g) => - quant.Tensors?.FirstOrDefault(x => x.TGroup.UniqueId == g.UniqueId)?.TensorType.UniqueId ?? (byte)0; - - return new TensorLookup - { - BaseQuant = quant.BaseQuant.UniqueId, - Embeddings = Get(TReg.Embeddings), - LmHead = Get(TReg.LmHead), - AttnQ = Get(TReg.AttnQ), - AttnKV = Get(TReg.AttnKV), - AttnOutput = Get(TReg.AttnOutput), - FfnUpGate = Get(TReg.FfnUpGate), - FfnDown = Get(TReg.FfnDown), - MoeExperts = Get(TReg.MoeExperts), - MoeRouter = Get(TReg.MoeRouter) - }; - } - - private sealed class CandidateEvaluation - { - public RequiredSamplePlan Plan { get; set; } = default!; + public TensorGroup Group { get; set; } = default!; public TensorWeightScheme Scheme { get; set; } = default!; - public BenchmarkSnapshot Snapshot { get; set; } = default!; - public double AggregateKld { get; set; } - public double AggregatePplDelta { get; set; } - public bool Eliminated { get; set; } - public string? EliminationReason { get; set; } - } - - private sealed class TensorLookup - { - public byte BaseQuant; - public byte Embeddings; - public byte LmHead; - public byte AttnQ; - public byte AttnKV; - public byte AttnOutput; - public byte FfnUpGate; - public byte FfnDown; - public byte MoeExperts; - public byte MoeRouter; + public ulong SizeBytes { get; set; } + public double SavingsRatio { get; set; } + public double Kld { get; set; } + public double PplDeltaPercent { get; set; } } private sealed class BenchmarkSnapshot { - public ulong SizeBytes; - public Dictionary Domains = new(); + public ulong SizeBytes { get; set; } + public List Benchmarks { get; set; } = new(); } - private sealed class BenchmarkDomainSnapshot + private sealed class CategorySnapshot { - public double? Kld; - public double Ppl; - public double PplError; + public byte Category { get; set; } + public double? Kld { get; set; } + public double Ppl { get; set; } + public double PplError { get; set; } } } \ No newline at end of file diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 141df3b..11b6eca 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -3,7 +3,9 @@ using DuckDB.NET.Data; using MagicQuant.Helpers; using MQ.DB; +using MQ.DB.Data; using MQ.DB.Models; +using Microsoft.EntityFrameworkCore; using Spectre.Console; namespace MagicQuant.Services; @@ -57,6 +59,87 @@ public async Task RebuildAsync(CancellationToken ct = default) await InitializeAsync(forceRebuild: true, ct: ct); } + public async Task PrunePredictedLargerThanQ8Async( + RequiredSampleGenerationResult fullPlan, + CancellationToken ct = default) + { + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + + var predictionContext = await BuildPredictionContextAsync(fullPlan, ct); + + if (predictionContext == null) + { + AnsiConsole.MarkupLine("[yellow]Predicted-size pruning skipped: prediction context was incomplete.[/]"); + return 0; + } + + var rows = new List(); + + var select = connection.CreateCommand(); + select.CommandText = $@" + SELECT BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter + FROM {TableName};"; + + using (var reader = await select.ExecuteReaderAsync(ct)) + { + while (await reader.ReadAsync(ct)) + { + rows.Add(new TensorConfig( + baseQuant: Convert.ToByte(reader.GetValue(0)), + embeddings: Convert.ToByte(reader.GetValue(1)), + lmHead: Convert.ToByte(reader.GetValue(2)), + attnQ: Convert.ToByte(reader.GetValue(3)), + attnKV: Convert.ToByte(reader.GetValue(4)), + attnOutput: Convert.ToByte(reader.GetValue(5)), + ffnUpGate: Convert.ToByte(reader.GetValue(6)), + ffnDown: Convert.ToByte(reader.GetValue(7)), + moeExperts: Convert.ToByte(reader.GetValue(8)), + moeRouter: Convert.ToByte(reader.GetValue(9)) + )); + } + } + + var kept = new List(rows.Count); + + foreach (var row in rows) + { + ulong predicted = predictionContext.Predict(row); + if (predicted <= predictionContext.PureQ8BaseSize) + kept.Add(row); + } + + long removed = rows.Count - kept.Count; + + if (removed <= 0) + { + AnsiConsole.MarkupLine("[green]Predicted-size pruning removed 0 combinations.[/]"); + return 0; + } + + var createCmd = connection.CreateCommand(); + createCmd.CommandText = $@" + DROP TABLE IF EXISTS {TableName}; + CREATE TABLE {TableName} ( + BaseQuant TINYINT, + Embeddings TINYINT, + LmHead TINYINT, + AttnQ TINYINT, + AttnKV TINYINT, + AttnOutput TINYINT, + FfnUpGate TINYINT, + FfnDown TINYINT, + MoeExperts TINYINT, + MoeRouter TINYINT + );"; + await createCmd.ExecuteNonQueryAsync(ct); + + await BulkInsertAsync(connection, kept, ct); + + AnsiConsole.MarkupLine($"[yellow]Predicted-size pruning removed:[/] [red]{removed:N0}[/] combo(s) larger than pure Q8."); + return removed; + } + private async Task GetRowCountAsync(DuckDBConnection connection, CancellationToken ct) { var checkCmd = connection.CreateCommand(); @@ -98,43 +181,196 @@ MoeRouter TINYINT long insertedTotal = 0; var bases = RuntimeSearchSpace.GetActiveCombinationBaselines(); - AnsiConsole.MarkupLine($"[grey]Starting bulk insert of {expectedTotal:N0} rows...[/]"); + AnsiConsole.MarkupLine($"Starting bulk insert of {expectedTotal:N0} rows..."); foreach (var baseline in bases) { - foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches( - baseline, - batchSize: 1_000_000, - ct: ct)) + foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches(baseline, ct: ct)) { - using (var appender = connection.CreateAppender(TableName)) - { - foreach (var config in batch) - { - var row = appender.CreateRow(); - - row.AppendValue(config.BaseQuant); - row.AppendValue(config.Embeddings); - row.AppendValue(config.LmHead); - row.AppendValue(config.AttnQ); - row.AppendValue(config.AttnKV); - row.AppendValue(config.AttnOutput); - row.AppendValue(config.FfnUpGate); - row.AppendValue(config.FfnDown); - row.AppendValue(config.MoeExperts); - row.AppendValue(config.MoeRouter); - - row.EndRow(); - } - } - + await BulkInsertAsync(connection, batch, ct); insertedTotal += batch.Count; - AnsiConsole.MarkupLine($" [grey]Inserted batch... Total so far:[/] {insertedTotal:N0}"); - batch.Clear(); + AnsiConsole.MarkupLine($" Inserted batch... Total so far: {insertedTotal:N0}"); } } sw.Stop(); - AnsiConsole.MarkupLine($"[bold green]DuckDB rebuild complete![/] in {sw.Elapsed.TotalSeconds:F2}s"); + AnsiConsole.MarkupLine($"DuckDB rebuild complete! in {sw.Elapsed.TotalSeconds:F2}s"); + } + + private async Task BulkInsertAsync( + DuckDBConnection connection, + IReadOnlyCollection rows, + CancellationToken ct) + { + if (rows.Count == 0) + return; + + using var tx = connection.BeginTransaction(); + + foreach (var row in rows) + { + var cmd = connection.CreateCommand(); + cmd.Transaction = tx; + cmd.CommandText = $@" + INSERT INTO {TableName} + (BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + + cmd.Parameters.Add(new DuckDBParameter { Value = row.BaseQuant }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.Embeddings }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.LmHead }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.AttnQ }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.AttnKV }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.AttnOutput }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.FfnUpGate }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.FfnDown }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.MoeExperts }); + cmd.Parameters.Add(new DuckDBParameter { Value = row.MoeRouter }); + + await cmd.ExecuteNonQueryAsync(ct); + } + + tx.Commit(); + } + + private async Task BuildPredictionContextAsync( + RequiredSampleGenerationResult fullPlan, + CancellationToken ct) + { + await using var db = new MagicQuantContext(); + + var model = await db.AiModelHashes + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (model == null) + return null; + + var pureQ8 = await LoadSnapshotByQuantAsync( + db, + model.Id, + HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), + ct); + + var carrierBaseOnlyPlan = fullPlan.Plans.FirstOrDefault(x => + x.Kind == RequiredSampleKind.BaseOnlyIsolation && + x.TestedBaselineId == BaselineQuants.Q8_0.UniqueId && + x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal)); + + if (pureQ8 == null || carrierBaseOnlyPlan == null) + return null; + + var carrier = await LoadSnapshotByQuantAsync(db, model.Id, carrierBaseOnlyPlan.Quant, ct); + if (carrier == null) + return null; + + var deltaByGroupAndScheme = new Dictionary<(byte GroupId, byte SchemeId), long>(); + + var groupPlans = fullPlan.Plans + .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe || x.Kind == RequiredSampleKind.GroupIsolationContinuation) + .Where(x => x.TestedBaselineId == BaselineQuants.Q8_0.UniqueId) + .ToList(); + + foreach (var plan in groupPlans) + { + if (!plan.TargetGroupId.HasValue || !plan.TestedSchemeId.HasValue) + continue; + + var snap = await LoadSnapshotByQuantAsync(db, model.Id, plan.Quant, ct); + if (snap == null) + continue; + + long delta = (long)snap.SizeBytes - (long)carrier.SizeBytes; + deltaByGroupAndScheme[(plan.TargetGroupId.Value, plan.TestedSchemeId.Value)] = delta; + } + + return new PredictionContext( + pureQ8BaseSize: pureQ8.SizeBytes, + carrierBaseOnlySize: carrier.SizeBytes, + deltas: deltaByGroupAndScheme); + } + + private static async Task LoadSnapshotByQuantAsync( + MagicQuantContext db, + uint modelId, + HybridQuant quant, + CancellationToken ct) + { + var lookup = (TensorConfig)quant; + + var row = await db.AiBenchmarks + .Join(db.TensorCombos, + b => b.TensorComboId, + c => c.Id, + (b, c) => new { b, c }) + .FirstOrDefaultAsync(x => + x.b.AiModelHashId == modelId && + x.c.BaseQuant == lookup.BaseQuant && + x.c.Embeddings == lookup.Embeddings && + x.c.LmHead == lookup.LmHead && + x.c.AttnQ == lookup.AttnQ && + x.c.AttnKV == lookup.AttnKV && + x.c.AttnOutput == lookup.AttnOutput && + x.c.FfnUpGate == lookup.FfnUpGate && + x.c.FfnDown == lookup.FfnDown && + x.c.MoeExperts == lookup.MoeExperts && + x.c.MoeRouter == lookup.MoeRouter, + ct); + + if (row == null) + return null; + + return new BenchmarkRow { SizeBytes = row.b.SizeBytes }; + } + + private sealed class BenchmarkRow + { + public ulong SizeBytes { get; set; } + } + + private sealed class PredictionContext + { + private readonly Dictionary<(byte GroupId, byte SchemeId), long> _deltas; + + public ulong PureQ8BaseSize { get; } + public ulong CarrierBaseOnlySize { get; } + + public PredictionContext( + ulong pureQ8BaseSize, + ulong carrierBaseOnlySize, + Dictionary<(byte GroupId, byte SchemeId), long> deltas) + { + PureQ8BaseSize = pureQ8BaseSize; + CarrierBaseOnlySize = carrierBaseOnlySize; + _deltas = deltas; + } + + public ulong Predict(TensorConfig config) + { + long total = (long)CarrierBaseOnlySize; + + AddDelta(TReg.Embeddings.UniqueId, config.Embeddings, ref total); + AddDelta(TReg.LmHead.UniqueId, config.LmHead, ref total); + AddDelta(TReg.AttnQ.UniqueId, config.AttnQ, ref total); + AddDelta(TReg.AttnKV.UniqueId, config.AttnKV, ref total); + AddDelta(TReg.AttnOutput.UniqueId, config.AttnOutput, ref total); + AddDelta(TReg.FfnUpGate.UniqueId, config.FfnUpGate, ref total); + AddDelta(TReg.FfnDown.UniqueId, config.FfnDown, ref total); + AddDelta(TReg.MoeExperts.UniqueId, config.MoeExperts, ref total); + AddDelta(TReg.MoeRouter.UniqueId, config.MoeRouter, ref total); + + if (total < 0) + total = 0; + + return (ulong)total; + } + + private void AddDelta(byte groupId, byte schemeId, ref long total) + { + if (schemeId == TensorWeightScheme.BF16_F16.UniqueId) + return; + + if (_deltas.TryGetValue((groupId, schemeId), out long delta)) + total += delta; + } } } \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 211c7ad..50a9d4b 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -89,7 +89,7 @@ public async Task ProcessHybridBatchAsync( var shimmedPlans = quants .Select((quant, index) => new RequiredSamplePlan { - Kind = RequiredSampleKind.GroupIsolation, + Kind = RequiredSampleKind.GroupIsolationContinuation, Key = $"legacy:{index}", Description = "Legacy batch item", Quant = quant From 5e491e2848d146b3b7bf83d167559c13314953f6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 14 Apr 2026 22:48:48 -0400 Subject: [PATCH 049/258] more lax pruning --- MagicQuant/Helpers/IsolationPruningConfig.cs | 6 +- .../Services/IsolationOptimizationService.cs | 101 +++++++++++------- 2 files changed, 64 insertions(+), 43 deletions(-) diff --git a/MagicQuant/Helpers/IsolationPruningConfig.cs b/MagicQuant/Helpers/IsolationPruningConfig.cs index 19fe3be..82232b9 100644 --- a/MagicQuant/Helpers/IsolationPruningConfig.cs +++ b/MagicQuant/Helpers/IsolationPruningConfig.cs @@ -6,9 +6,9 @@ public static class IsolationPruningConfig public const double MinimumIsolationReductionToSuppressBf16Ratio = 0.10d; public const double MaximumIsolationPplDeltaPercent = 5.0d; public const double MaximumIsolationKld = 0.1d; - public const double BadTradeMaxSizeDeltaPercent = 5.0d; - public const double BadTradeKldMultiplier = 2.0d; - public const double BadTradePplMultiplier = 3.0d; + public const double BadTradeMaxSizeDeltaPercent = 4.0d; + public const double BadTradeKldMultiplier = 2.5d; + public const double BadTradePplMultiplier = 3.5d; public const double FloatingPointEpsilon = 1e-8d; public const double MinimumMeaningfulBaseOnlyReductionRatio = 0.01d; } \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 80cbf45..1d68608 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -63,8 +63,8 @@ public async Task AnalyzeInitialIsolationProbesA var result = new InitialIsolationAnalysisResult(); var nativeBaseline = await LoadSnapshotAsync( - HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) - ?? throw new InvalidOperationException("Native BF16 baseline benchmark was not found."); + HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) + ?? throw new InvalidOperationException("Native BF16 baseline benchmark was not found."); var carrierBaselineId = BaselineQuants.Q8_0.UniqueId; @@ -74,7 +74,7 @@ public async Task AnalyzeInitialIsolationProbesA x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal)); var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct) - ?? throw new InvalidOperationException("Carrier base-only benchmark was not found."); + ?? throw new InvalidOperationException("Carrier base-only benchmark was not found."); var groupPlans = plan.Plans .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe) @@ -149,8 +149,8 @@ public async Task AnalyzeAndApplyFinalAsync( var result = new IsolationOptimizationResult(); var nativeBaseline = await LoadSnapshotAsync( - HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) - ?? throw new InvalidOperationException("Native BF16 baseline benchmark was not found."); + HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) + ?? throw new InvalidOperationException("Native BF16 baseline benchmark was not found."); var carrierBaselineId = BaselineQuants.Q8_0.UniqueId; @@ -160,10 +160,11 @@ public async Task AnalyzeAndApplyFinalAsync( x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal)); var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct) - ?? throw new InvalidOperationException("Carrier base-only benchmark was not found."); + ?? throw new InvalidOperationException("Carrier base-only benchmark was not found."); var groupPlans = fullPlan.Plans - .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe || x.Kind == RequiredSampleKind.GroupIsolationContinuation) + .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe || + x.Kind == RequiredSampleKind.GroupIsolationContinuation) .Where(x => x.TestedBaselineId == carrierBaselineId) .GroupBy(x => x.TargetGroupId!.Value) .OrderBy(x => x.Key) @@ -301,7 +302,8 @@ public async Task AnalyzeAndApplyFinalAsync( private static List FilterSurvivors(TensorGroup group, List candidates) { return candidates - .Where(x => x.Scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId || !RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, x.Scheme)) + .Where(x => x.Scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId || + !RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, x.Scheme)) .ToList(); } @@ -357,50 +359,69 @@ private static void ApplyBadTradeElimination( var explicitCandidates = candidates .Where(x => x.Scheme.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) .Where(x => !x.Scheme.RequiresImatrix) + .OrderBy(x => x.SizeBytes) .ToList(); + // Compare only to nearby larger neighbors, not the whole ladder. for (int i = 0; i < explicitCandidates.Count; i++) { - for (int j = 0; j < explicitCandidates.Count; j++) - { - if (i == j) - continue; + var smaller = explicitCandidates[i]; - var a = explicitCandidates[i]; - var b = explicitCandidates[j]; + for (int j = i + 1; j < explicitCandidates.Count && j <= i + 2; j++) + { + var larger = explicitCandidates[j]; - if (a.SizeBytes >= b.SizeBytes) - continue; + double sizeDeltaPercent = + ((double)larger.SizeBytes - smaller.SizeBytes) / larger.SizeBytes * 100.0; - double sizeDeltaPercent = (b.SizeBytes - a.SizeBytes) / (double)b.SizeBytes * 100.0; if (sizeDeltaPercent > IsolationPruningConfig.BadTradeMaxSizeDeltaPercent) continue; - double kldRatio = b.Kld <= IsolationPruningConfig.FloatingPointEpsilon + double smallerPplAbs = Math.Abs(smaller.PplDeltaPercent); + double largerPplAbs = Math.Abs(larger.PplDeltaPercent); + + double kldRatio = larger.Kld <= IsolationPruningConfig.FloatingPointEpsilon ? double.PositiveInfinity - : a.Kld / b.Kld; + : smaller.Kld / larger.Kld; - double pplRatio = b.PplDeltaPercent <= IsolationPruningConfig.FloatingPointEpsilon + double pplRatio = largerPplAbs <= IsolationPruningConfig.FloatingPointEpsilon ? double.PositiveInfinity - : a.PplDeltaPercent / b.PplDeltaPercent; + : smallerPplAbs / largerPplAbs; + + bool kldBadTrade = + smaller.Kld > larger.Kld * IsolationPruningConfig.BadTradeKldMultiplier; + + bool pplBadTrade = + smallerPplAbs > largerPplAbs * IsolationPruningConfig.BadTradePplMultiplier; + + bool smallerMeaningfullyBetterKld = + smaller.Kld + IsolationPruningConfig.FloatingPointEpsilon < larger.Kld * 0.90; - bool badTrade = - a.Kld > b.Kld * IsolationPruningConfig.BadTradeKldMultiplier || - a.PplDeltaPercent > b.PplDeltaPercent * IsolationPruningConfig.BadTradePplMultiplier; + bool smallerMeaningfullyBetterPpl = + smallerPplAbs + IsolationPruningConfig.FloatingPointEpsilon < largerPplAbs * 0.90; - if (!badTrade) + bool mixedTradeoff = + (kldBadTrade && smallerMeaningfullyBetterPpl) || + (pplBadTrade && smallerMeaningfullyBetterKld); + + if (mixedTradeoff) + continue; + + if (!kldBadTrade && !pplBadTrade) continue; - if (!RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, a.Scheme)) + if (!RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, smaller.Scheme)) { - RuntimeSearchSpace.BanSchemeForGroup(group, a.Scheme); + RuntimeSearchSpace.BanSchemeForGroup(group, smaller.Scheme); result.BadTradeEliminations++; result.Notes.Add( - $"Bad trade elimination: '{a.Scheme.Names[0]}' removed vs '{b.Scheme.Names[0]}' for '{group.Name}'. " + + $"Bad trade elimination: '{smaller.Scheme.Names[0]}' removed vs '{larger.Scheme.Names[0]}' for '{group.Name}'. " + $"Reason: small size gain ({sizeDeltaPercent:F2}%) but disproportionate damage " + - $"(KLD x{kldRatio:F2}, PPL x{pplRatio:F2})."); + $"(KLD x{kldRatio:F2}, |PPL| x{pplRatio:F2})."); } + + break; } } } @@ -424,17 +445,17 @@ private static void ApplyBadTradeElimination( c => c.Id, (b, c) => new { b, c }) .FirstOrDefaultAsync(x => - x.b.AiModelHashId == model.Id && - x.c.BaseQuant == lookup.BaseQuant && - x.c.Embeddings == lookup.Embeddings && - x.c.LmHead == lookup.LmHead && - x.c.AttnQ == lookup.AttnQ && - x.c.AttnKV == lookup.AttnKV && - x.c.AttnOutput == lookup.AttnOutput && - x.c.FfnUpGate == lookup.FfnUpGate && - x.c.FfnDown == lookup.FfnDown && - x.c.MoeExperts == lookup.MoeExperts && - x.c.MoeRouter == lookup.MoeRouter, + x.b.AiModelHashId == model.Id && + x.c.BaseQuant == lookup.BaseQuant && + x.c.Embeddings == lookup.Embeddings && + x.c.LmHead == lookup.LmHead && + x.c.AttnQ == lookup.AttnQ && + x.c.AttnKV == lookup.AttnKV && + x.c.AttnOutput == lookup.AttnOutput && + x.c.FfnUpGate == lookup.FfnUpGate && + x.c.FfnDown == lookup.FfnDown && + x.c.MoeExperts == lookup.MoeExperts && + x.c.MoeRouter == lookup.MoeRouter, ct); if (row == null) From 63e74b5aea943354d9cde01413ccd2e5d16211d9 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 15 Apr 2026 12:44:55 -0400 Subject: [PATCH 050/258] build all combo's --- MQ.DB/Models/TensorWeightScheme.cs | 22 +++++-- MagicQuant/Commands/Evolution.cs | 44 ++++++++++++++ MagicQuant/Services/QuantDatabaseService.cs | 64 +++++++++++++++++++++ 3 files changed, 124 insertions(+), 6 deletions(-) diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index c033b82..17f4384 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -88,7 +88,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() public static readonly TensorWeightScheme BF16_F16 = new(1, false, ["BF16", "F16", "F32"], Array.Empty(), null); - /*public static readonly TensorWeightScheme MXFP4 = + public static readonly TensorWeightScheme MXFP4 = new( 2, false, @@ -99,7 +99,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() TReg.MoeRouter, TReg.MoeExperts }, - 32);*/ + 32); public static readonly TensorWeightScheme Q8_0 = new(3, false, ["Q8_0"], Array.Empty(), null); @@ -113,7 +113,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() public static readonly TensorWeightScheme IQ4_XS = new(6, false, ["IQ4_XS"], new[] { TReg.MoeRouter }, 32, true); - /* + public static TensorWeightScheme IQ4_NL = new( 7, @@ -122,8 +122,17 @@ public static TensorWeightScheme GetSmallestNonImatrix() new[] { TReg.MoeRouter }, 32 ); + + public static TensorWeightScheme Q4_K = + new( + 14, + false, + ["Q4_K"], + new[] { TReg.MoeRouter }, + 32 + ); - public static TensorWeightScheme IQ3_S = + /* public static TensorWeightScheme IQ3_S = new( 8, true, @@ -216,12 +225,13 @@ public static TensorWeightScheme GetSmallestNonImatrix() [ NULL, BF16_F16, - // MXFP4, + MXFP4, Q8_0, Q6_K, Q5_K, IQ4_XS, - // IQ4_NL, + IQ4_NL, + Q4_K, // IQ3_S, // IQ3_XS, // IQ3_XXS, diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index c8eb0f4..352a909 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -9,6 +9,8 @@ namespace MagicQuant.Commands; public class Evolution : ICommand { + private const int BruteForceFinalCombinationThreshold = 1_000; + public async Task Run(List args) { if (args.Any(a => a.Name?.ToLower() == "help")) @@ -183,6 +185,48 @@ await benchmarkService.RunAllBenchmarksAsync( foreach (var note in isolationResult.Notes) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + + long finalRemainingCombinationCount = await dbService.GetRemainingCombinationCountAsync(); + + AnsiConsole.MarkupLine($"[green]Final surviving combinations:[/] {finalRemainingCombinationCount:N0}"); + + if (finalRemainingCombinationCount <= BruteForceFinalCombinationThreshold) + { + AnsiConsole.Write(new Rule("[yellow]Final Brute Force Benchmark Phase[/]") { Justification = Justify.Left }); + + AnsiConsole.MarkupLine( + $"[green]Final combination count[/] [cyan]{finalRemainingCombinationCount:N0}[/] " + + $"is at or below the brute-force threshold of [yellow]{BruteForceFinalCombinationThreshold:N0}[/]."); + + var finalConfigs = await dbService.GetRemainingTensorConfigsAsync(); + var finalQuants = finalConfigs + .Select(x => (HybridQuant)x) + .ToList(); + + var finalSummary = await quantizationService.ProcessHybridBatchAsync(finalQuants); + + AnsiConsole.MarkupLine("[bold green]Final brute force benchmarking complete.[/]"); + AnsiConsole.MarkupLine($" [green]Requested:[/] {finalSummary.Requested:N0}"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {finalSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {finalSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {finalSummary.Failed:N0}"); + + AnsiConsole.MarkupLine("[yellow]Note:[/] Final model creation/export functionality is still being implemented."); + } + else + { + AnsiConsole.MarkupLine("[yellow]Note:[/] Final model creation/export functionality is still being implemented."); + + throw new InvalidOperationException( + $"Prediction engine not created yet. " + + $"Final surviving combinations were {finalRemainingCombinationCount:N0}, " + + $"which is above the brute-force threshold of {BruteForceFinalCombinationThreshold:N0}."); + } + + AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count after rule pruning:[/] {comboCountAfterRulePruning:N0}"); + AnsiConsole.MarkupLine($"[green]Predicted-size combo removals:[/] {predictedSizePruned:N0}"); + AnsiConsole.MarkupLine($"[green]Final surviving combinations:[/] {finalRemainingCombinationCount:N0}"); } private void ShowEvolutionHelp() diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 11b6eca..61c334f 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -15,6 +15,70 @@ public class QuantDatabaseService private const string DbFileName = "MagicQuant_Combinations.duckdb"; private const string TableName = "tensor_configs"; + public async Task GetRemainingCombinationCountAsync(CancellationToken ct = default) + { + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + + var cmd = connection.CreateCommand(); + cmd.CommandText = $"SELECT COUNT(*) FROM {TableName};"; + + return (long)(await cmd.ExecuteScalarAsync(ct) ?? 0L); + } + + public async Task> GetRemainingTensorConfigsAsync(CancellationToken ct = default) + { + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + + var results = new List(); + + var cmd = connection.CreateCommand(); + cmd.CommandText = $@" + SELECT + BaseQuant, + Embeddings, + LmHead, + AttnQ, + AttnKV, + AttnOutput, + FfnUpGate, + FfnDown, + MoeExperts, + MoeRouter + FROM {TableName} + ORDER BY + BaseQuant, + Embeddings, + LmHead, + AttnQ, + AttnKV, + AttnOutput, + FfnUpGate, + FfnDown, + MoeExperts, + MoeRouter;"; + + using var reader = await cmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + { + results.Add(new TensorConfig( + baseQuant: Convert.ToByte(reader.GetValue(0)), + embeddings: Convert.ToByte(reader.GetValue(1)), + lmHead: Convert.ToByte(reader.GetValue(2)), + attnQ: Convert.ToByte(reader.GetValue(3)), + attnKV: Convert.ToByte(reader.GetValue(4)), + attnOutput: Convert.ToByte(reader.GetValue(5)), + ffnUpGate: Convert.ToByte(reader.GetValue(6)), + ffnDown: Convert.ToByte(reader.GetValue(7)), + moeExperts: Convert.ToByte(reader.GetValue(8)), + moeRouter: Convert.ToByte(reader.GetValue(9)) + )); + } + + return results; + } + private static string GetDuckDbDirectory() { if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) From 9fc6dfc801b5b340f965e7f3fe0c394d00cce2c0 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 15 Apr 2026 18:37:33 -0400 Subject: [PATCH 051/258] new benchmarks savings --- MQ.DB/Data/MagicQuantContext.cs | 68 +++- ...80949_AddExecutionTimingTables.Designer.cs | 340 +++++++++++++++++ ...20260415180949_AddExecutionTimingTables.cs | 158 ++++++++ .../MagicQuantContextModelSnapshot.cs | 161 ++++++++ MQ.DB/Models/BaselineQuants.cs | 15 +- MQ.DB/Models/DbModels/BenchmarkRun.cs | 78 ++++ MQ.DB/Models/DbModels/QuantizationRun.cs | 70 ++++ MagicQuant/Services/BenchmarkService.cs | 345 +++++++++++------- MagicQuant/Services/QuantizationService.cs | 157 +++++--- 9 files changed, 1191 insertions(+), 201 deletions(-) create mode 100644 MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.Designer.cs create mode 100644 MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.cs create mode 100644 MQ.DB/Models/DbModels/BenchmarkRun.cs create mode 100644 MQ.DB/Models/DbModels/QuantizationRun.cs diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index 2c5e61e..dfe1eed 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -15,27 +15,40 @@ public class MagicQuantContext : DbContext public MagicQuantContext() { - // On the very first instantiation (e.g., first benchmark run), - // we ensure the folder exists and migrations are applied. - if (!_isInitialized) + EnsureInitialized(); + } + + public MagicQuantContext(DbContextOptions options) + : base(options) + { + EnsureInitialized(); + } + + private void EnsureInitialized() + { + // 🚫 Never run during EF tooling (migrations, etc.) + if (IsDesignTime()) + return; + + if (_isInitialized) + return; + + lock (_initLock) { - lock (_initLock) - { - if (!_isInitialized) - { - InitializeDatabase(); - _isInitialized = true; - } - } + if (_isInitialized) + return; + + InitializeDatabase(); + _isInitialized = true; } } private void InitializeDatabase() { var directory = Cache.MagicQuantDirectory; - - // Safety: fallback if Cache isn't set yet (rare, but good for stability) - if (string.IsNullOrEmpty(directory)) + + // Safety fallback + if (string.IsNullOrEmpty(directory)) directory = Directory.GetCurrentDirectory(); if (!Directory.Exists(directory)) @@ -43,31 +56,42 @@ private void InitializeDatabase() Directory.CreateDirectory(directory); } - // Apply Migrations automatically + // 🔥 Apply migrations automatically Database.Migrate(); } + private static bool IsDesignTime() + { + return AppDomain.CurrentDomain.GetAssemblies() + .Any(a => a.FullName != null && + a.FullName.Contains("EntityFrameworkCore.Design", StringComparison.OrdinalIgnoreCase)); + } + // -------------------------------------------------------- - // Standard DbContext Configuration + // DbSets // -------------------------------------------------------- - public MagicQuantContext(DbContextOptions options) : base(options) { } - public DbSet AiBenchmarks { get; set; } public DbSet AiModelHashes { get; set; } public DbSet TensorCombos { get; set; } + public DbSet QuantizationRuns { get; set; } + public DbSet BenchmarkRuns { get; set; } + + // -------------------------------------------------------- + // Configuration + // -------------------------------------------------------- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { var directory = Cache.MagicQuantDirectory; + if (string.IsNullOrEmpty(directory)) - { directory = Directory.GetCurrentDirectory(); - } var dbPath = Path.Combine(directory, "MagicQuant_SQLite.db"); + optionsBuilder.UseSqlite($"Data Source={dbPath};Foreign Keys=True;"); } } @@ -79,6 +103,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) base.OnModelCreating(modelBuilder); } + // -------------------------------------------------------- + // Strict Validation + // -------------------------------------------------------- + private void ValidateDbSetsImplementInterface() { var dbSetGenericTypes = this.GetType() diff --git a/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.Designer.cs b/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.Designer.cs new file mode 100644 index 0000000..e9b173d --- /dev/null +++ b/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.Designer.cs @@ -0,0 +1,340 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260415180949_AddExecutionTimingTables")] + partial class AddExecutionTimingTables + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.cs b/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.cs new file mode 100644 index 0000000..fe522dd --- /dev/null +++ b/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.cs @@ -0,0 +1,158 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class AddExecutionTimingTables : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "BenchmarkRuns", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + TensorComboId = table.Column(type: "INTEGER", nullable: false), + AiBenchmarkId = table.Column(type: "INTEGER", nullable: false), + CategoryBenchmarkId = table.Column(type: "INTEGER", nullable: true), + Category = table.Column(type: "INTEGER", nullable: false), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: false), + DurationMs = table.Column(type: "INTEGER", nullable: false), + Succeeded = table.Column(type: "INTEGER", nullable: false), + Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_BenchmarkRuns", x => x.Id); + table.ForeignKey( + name: "FK_BenchmarkRuns_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_CategoryBenchmark_CategoryBenchmarkId", + column: x => x.CategoryBenchmarkId, + principalTable: "CategoryBenchmark", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_BenchmarkRuns_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "QuantizationRuns", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + TensorComboId = table.Column(type: "INTEGER", nullable: false), + AiBenchmarkId = table.Column(type: "INTEGER", nullable: true), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: false), + DurationMs = table.Column(type: "INTEGER", nullable: false), + Succeeded = table.Column(type: "INTEGER", nullable: false), + Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true), + OutputModelPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_QuantizationRuns", x => x.Id); + table.ForeignKey( + name: "FK_QuantizationRuns_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_QuantizationRuns_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_QuantizationRuns_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiBenchmarkId", + table: "BenchmarkRuns", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiBenchmarkId_Category", + table: "BenchmarkRuns", + columns: new[] { "AiBenchmarkId", "Category" }); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiModelHashId", + table: "BenchmarkRuns", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_CategoryBenchmarkId", + table: "BenchmarkRuns", + column: "CategoryBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_StartedUtc", + table: "BenchmarkRuns", + column: "StartedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_TensorComboId", + table: "BenchmarkRuns", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_AiBenchmarkId", + table: "QuantizationRuns", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_AiModelHashId", + table: "QuantizationRuns", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_StartedUtc", + table: "QuantizationRuns", + column: "StartedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_TensorComboId", + table: "QuantizationRuns", + column: "TensorComboId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BenchmarkRuns"); + + migrationBuilder.DropTable( + name: "QuantizationRuns"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index 3413d75..ee1f5a2 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -1,4 +1,5 @@ // +using System; using MQ.DB.Data; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -64,6 +65,59 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("AiModelHashes"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => { b.Property("Id") @@ -92,6 +146,53 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("CategoryBenchmark"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => { b.Property("Id") @@ -155,6 +256,40 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("TensorCombo"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("TensorCombo"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => { b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") @@ -166,6 +301,32 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("AiBenchmark"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => { b.Navigation("CategorBenchmarks"); diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 0bddded..755c026 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -6,14 +6,15 @@ public record BaselineQuants( byte UniqueId, bool RequiresImatrix, ImmutableArray Names, + TensorWeightScheme? DefaultTensorScheme, HybridQuant? BaseConversionBase = null) { public const byte NativeSourceUniqueId = 250; - public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"]); - public static readonly BaselineQuants Q6_K = new(1, false, ["Q6_K"]); - public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"]); - public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"]); + public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"], TensorWeightScheme.Q8_0); + public static readonly BaselineQuants Q6_K = new(1, false, ["Q6_K"], TensorWeightScheme.Q6_K); + public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"], TensorWeightScheme.Q5_K); + public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"], TensorWeightScheme.Q4_K); /*public static readonly BaselineQuants MXFP4_MOE = new( 4, @@ -31,12 +32,13 @@ public record BaselineQuants( .ToList() });*/ - public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"]); + public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"], TensorWeightScheme.IQ4_NL); public static readonly BaselineQuants IQ4_XS = new( 6, false, ["IQ4_XS"], + TensorWeightScheme.IQ4_XS, new HybridQuant { BaseQuant = null!, @@ -77,7 +79,8 @@ public static BaselineQuants GetBF16Quant() return new( NativeSourceUniqueId, false, - [(Cache.TorchType ?? Cache.MainTorchType.BF16).ToString()]); + [(Cache.TorchType ?? Cache.MainTorchType.BF16).ToString()], + TensorWeightScheme.BF16_F16); } public static BaselineQuants FromId(byte id) diff --git a/MQ.DB/Models/DbModels/BenchmarkRun.cs b/MQ.DB/Models/DbModels/BenchmarkRun.cs new file mode 100644 index 0000000..3f8ac73 --- /dev/null +++ b/MQ.DB/Models/DbModels/BenchmarkRun.cs @@ -0,0 +1,78 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class BenchmarkRun : ISQLiteEntity +{ + public Guid Id { get; set; } + + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + + public uint TensorComboId { get; set; } + public TensorCombo TensorCombo { get; set; } = default!; + + public uint AiBenchmarkId { get; set; } + public AiBenchmark AiBenchmark { get; set; } = default!; + + /// + /// Nullable until the CategoryBenchmark row is created/persisted. + /// + public uint? CategoryBenchmarkId { get; set; } + public CategoryBenchmark? CategoryBenchmark { get; set; } + + /// + /// Snapshot of the category for convenience and resilience. + /// Stored as the byte value of BenchmarkCategory. + /// + public byte Category { get; set; } + + public DateTime StartedUtc { get; set; } + public DateTime CompletedUtc { get; set; } + + public long DurationMs { get; set; } + + public bool Succeeded { get; set; } + + public string? Error { get; set; } + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + + builder.Property(x => x.Id) + .ValueGeneratedNever(); + + builder.HasIndex(x => x.AiModelHashId); + builder.HasIndex(x => x.TensorComboId); + builder.HasIndex(x => x.AiBenchmarkId); + builder.HasIndex(x => x.CategoryBenchmarkId); + builder.HasIndex(x => x.StartedUtc); + builder.HasIndex(x => new { x.AiBenchmarkId, x.Category }); + + builder.Property(x => x.Error) + .HasMaxLength(4000); + + builder.HasOne(x => x.AiModelHash) + .WithMany() + .HasForeignKey(x => x.AiModelHashId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorCombo) + .WithMany() + .HasForeignKey(x => x.TensorComboId) + .OnDelete(DeleteBehavior.Restrict); + + builder.HasOne(x => x.AiBenchmark) + .WithMany() + .HasForeignKey(x => x.AiBenchmarkId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.CategoryBenchmark) + .WithMany() + .HasForeignKey(x => x.CategoryBenchmarkId) + .OnDelete(DeleteBehavior.SetNull); + } +} \ No newline at end of file diff --git a/MQ.DB/Models/DbModels/QuantizationRun.cs b/MQ.DB/Models/DbModels/QuantizationRun.cs new file mode 100644 index 0000000..0ff6d06 --- /dev/null +++ b/MQ.DB/Models/DbModels/QuantizationRun.cs @@ -0,0 +1,70 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class QuantizationRun : ISQLiteEntity +{ + public Guid Id { get; set; } + + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + + public uint TensorComboId { get; set; } + public TensorCombo TensorCombo { get; set; } = default!; + + /// + /// Nullable because a quantization can fail before a benchmark row exists. + /// + public uint? AiBenchmarkId { get; set; } + public AiBenchmark? AiBenchmark { get; set; } + + public DateTime StartedUtc { get; set; } + public DateTime CompletedUtc { get; set; } + + /// + /// Total wall clock duration in milliseconds. + /// + public long DurationMs { get; set; } + + public bool Succeeded { get; set; } + + public string? Error { get; set; } + + public string? OutputModelPath { get; set; } + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + + builder.Property(x => x.Id) + .ValueGeneratedNever(); + + builder.HasIndex(x => x.AiModelHashId); + builder.HasIndex(x => x.TensorComboId); + builder.HasIndex(x => x.AiBenchmarkId); + builder.HasIndex(x => x.StartedUtc); + + builder.Property(x => x.Error) + .HasMaxLength(4000); + + builder.Property(x => x.OutputModelPath) + .HasMaxLength(2048); + + builder.HasOne(x => x.AiModelHash) + .WithMany() + .HasForeignKey(x => x.AiModelHashId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorCombo) + .WithMany() + .HasForeignKey(x => x.TensorComboId) + .OnDelete(DeleteBehavior.Restrict); + + builder.HasOne(x => x.AiBenchmark) + .WithMany() + .HasForeignKey(x => x.AiBenchmarkId) + .OnDelete(DeleteBehavior.SetNull); + } +} \ No newline at end of file diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 4158b18..cd9db5a 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -10,7 +10,6 @@ using MQ.DB.Models.DbModels; using Microsoft.EntityFrameworkCore; using Spectre.Console; -using System.Text.Json; namespace MagicQuant.Services; @@ -289,8 +288,6 @@ private async Task BuildExecutionPlanAsync( } } - // This should not normally happen because "all GPUs as one slot" already passed, - // but keeping a hard fallback is still worthwhile. return new BenchmarkExecutionPlan( planModelPath: q8ModelPath, staticNgl: targetNgl.Value, @@ -559,20 +556,15 @@ public async Task TryReuseExistingBenchmarksAsync( using var db = new MagicQuantContext(); - var currentHashStr = Cache.CurrentModelId; - var aiModelHash = await db.AiModelHashes - .FirstOrDefaultAsync(x => x.UniqueHash == currentHashStr); + var identity = await GetOrCreateBenchmarkIdentityAsync(db, quantConfig); + await SaveBenchmarkToDbAsync( + db: db, + model: identity.AiModelHash, + combo: identity.TensorCombo, + res: reused, + modelPath: modelPath, + executedRunTimings: new List()); - if (aiModelHash == null) - { - aiModelHash = new AiModelHash { UniqueHash = currentHashStr }; - db.AiModelHashes.Add(aiModelHash); - await db.SaveChangesAsync(); - } - - var tensorCombo = await GetOrCreateTensorComboAsync(db, quantConfig); - - await SaveBenchmarkToDbAsync(db, aiModelHash, tensorCombo, reused, modelPath); await WriteMetricsJsonAsync(benchDir, reused); return true; @@ -595,18 +587,9 @@ public async Task RunAllBenchmarksAsync( using var db = new MagicQuantContext(); - var currentHashStr = Cache.CurrentModelId; - var aiModelHash = await db.AiModelHashes - .FirstOrDefaultAsync(x => x.UniqueHash == currentHashStr); - - if (aiModelHash == null) - { - aiModelHash = new AiModelHash { UniqueHash = currentHashStr }; - db.AiModelHashes.Add(aiModelHash); - await db.SaveChangesAsync(); - } - - var tensorCombo = await GetOrCreateTensorComboAsync(db, quantConfig); + var identity = await GetOrCreateBenchmarkIdentityAsync(db, quantConfig); + var aiModelHash = identity.AiModelHash; + var tensorCombo = identity.TensorCombo; var existingBench = await db.AiBenchmarks .Include(x => x.CategorBenchmarks) @@ -621,9 +604,15 @@ public async Task RunAllBenchmarksAsync( var repairedSize = TryGetModelSize(modelPath); if (repairedSize > 0) { - existingBench.SizeBytes = repairedSize; - db.AiBenchmarks.Update(existingBench); - await db.SaveChangesAsync(); + var trackedRepair = await db.AiBenchmarks + .FirstOrDefaultAsync(x => x.Id == existingBench.Id); + + if (trackedRepair != null) + { + trackedRepair.SizeBytes = repairedSize; + await db.SaveChangesAsync(); + existingBench.SizeBytes = repairedSize; + } } } @@ -644,7 +633,14 @@ public async Task RunAllBenchmarksAsync( if (TryReadExistingBenchmarkArtifacts(benchDir, requestedDomains, requireKld, out var reused)) { reused.ModelSizeBytes ??= TryGetModelSize(modelPath); - await SaveBenchmarkToDbAsync(db, aiModelHash, tensorCombo, reused, modelPath); + await SaveBenchmarkToDbAsync( + db: db, + model: aiModelHash, + combo: tensorCombo, + res: reused, + modelPath: modelPath, + executedRunTimings: new List()); + await WriteMetricsJsonAsync(benchDir, reused); return reused; } @@ -657,6 +653,25 @@ public async Task RunAllBenchmarksAsync( "You must call EnsureExecutionPlanAsync() with the pure Q8 model first."); } + var trackedBench = await db.AiBenchmarks + .Include(x => x.CategorBenchmarks) + .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHash.Id && x.TensorComboId == tensorCombo.Id); + + if (trackedBench == null) + { + trackedBench = new AiBenchmark + { + AiModelHashId = aiModelHash.Id, + TensorComboId = tensorCombo.Id, + Ngl = 0, + SizeBytes = 0, + TokensPerSecond = 0 + }; + + db.AiBenchmarks.Add(trackedBench); + await db.SaveChangesAsync(); + } + await using var slotLease = await AcquireBenchmarkSlotAsync(); var slot = slotLease.Slot; @@ -669,19 +684,9 @@ public async Task RunAllBenchmarksAsync( ModelSizeBytes = TryGetModelSize(modelPath) }; - // Disabled for now. Too many variables that're annoying to track - /*string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); - if (TryReadExistingLlamaBenchLog(llamaBenchPath, out var existingLlamaBench)) - { - result.LlamaBench = existingLlamaBench; - } - else - { - AnsiConsole.MarkupLine( - $"[yellow]Running Llama-Bench[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); - result.LlamaBench = await RunLlamaBenchAsync(modelPath, benchDir, effectiveNgl, slot); - }*/ + var executedRunTimings = new List(); + // Disabled for now. Too many variables that're annoying to track result.LlamaBench = new LlamaBenchMetrics { LogPath = null, @@ -713,31 +718,72 @@ public async Task RunAllBenchmarksAsync( string corpusPath = Path.Combine(corporaRoot, $"ppl_corpus_{domain}.txt"); await PreparePplCorpusAsync(domain, corpusPath, tokenTarget); - AnsiConsole.MarkupLine( - $"[yellow]Running Perplexity ({Markup.Escape(domain)})[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); + DateTime startedUtc = DateTime.UtcNow; + var sw = Stopwatch.StartNew(); - var metrics = await RunPplBenchmarkAsync( - modelPath: modelPath, - benchDir: benchDir, - domain: domain, - corpusPath: corpusPath, - fixedNgl: effectiveNgl, - slot: slot, - klLogitsDir: klLogitsDir, - saveLogits: saveLogits); - - if (requireKld && !HasMeaningfulKld(metrics.Kld)) + try { - throw new InvalidOperationException( - $"Non-base benchmark produced invalid KLD for domain '{domain}'. " + - $"KLD must exist and be > 0. Parsed value: {(metrics.Kld.HasValue ? metrics.Kld.Value.ToString(CultureInfo.InvariantCulture) : "null")}"); - } + AnsiConsole.MarkupLine( + $"[yellow]Running Perplexity ({Markup.Escape(domain)})[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); + + var metrics = await RunPplBenchmarkAsync( + modelPath: modelPath, + benchDir: benchDir, + domain: domain, + corpusPath: corpusPath, + fixedNgl: effectiveNgl, + slot: slot, + klLogitsDir: klLogitsDir, + saveLogits: saveLogits); + + if (requireKld && !HasMeaningfulKld(metrics.Kld)) + { + throw new InvalidOperationException( + $"Non-base benchmark produced invalid KLD for domain '{domain}'. " + + $"KLD must exist and be > 0. Parsed value: {(metrics.Kld.HasValue ? metrics.Kld.Value.ToString(CultureInfo.InvariantCulture) : "null")}"); + } - result.Perplexity[domain] = metrics; + sw.Stop(); + + result.Perplexity[domain] = metrics; + + executedRunTimings.Add(new PendingBenchmarkRunTiming + { + Domain = domain, + Category = DomainToCategory(domain), + StartedUtc = startedUtc, + CompletedUtc = DateTime.UtcNow, + Succeeded = true, + Error = null + }); + } + catch (Exception ex) + { + sw.Stop(); + + await PersistFailedBenchmarkRunAsync( + db: db, + aiModelHashId: aiModelHash.Id, + tensorComboId: tensorCombo.Id, + aiBenchmarkId: trackedBench.Id, + category: DomainToCategory(domain), + startedUtc: startedUtc, + completedUtc: DateTime.UtcNow, + error: ex.ToString()); + + throw; + } } await WriteMetricsJsonAsync(benchDir, result); - await SaveBenchmarkToDbAsync(db, aiModelHash, tensorCombo, result, modelPath); + + await SaveBenchmarkToDbAsync( + db: db, + model: aiModelHash, + combo: tensorCombo, + res: result, + modelPath: modelPath, + executedRunTimings: executedRunTimings); return result; } @@ -746,47 +792,36 @@ public async Task RunAllBenchmarksAsync( // Database helpers // ---------------------------------------------------------------- - private async Task GetOrCreateTensorComboAsync(MagicQuantContext db, HybridQuant quant) + private async Task<(AiModelHash AiModelHash, TensorCombo TensorCombo)> GetOrCreateBenchmarkIdentityAsync( + MagicQuantContext db, + HybridQuant quantConfig, + CancellationToken ct = default) { - byte baseQuant = quant.BaseQuant.UniqueId; + var currentHashStr = Cache.CurrentModelId; + if (string.IsNullOrWhiteSpace(currentHashStr)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); - byte embeddings = 0; - byte lmHead = 0; - byte attnQ = 0; - byte attnKV = 0; - byte attnOutput = 0; - byte ffnUpGate = 0; - byte ffnDown = 0; - byte moeExperts = 0; - byte moeRouter = 0; + var aiModelHash = await db.AiModelHashes + .FirstOrDefaultAsync(x => x.UniqueHash == currentHashStr, ct); - if (quant.Tensors != null) + if (aiModelHash == null) { - foreach (var t in quant.Tensors) - { - if (t.TGroup.UniqueId == TReg.Embeddings.UniqueId) embeddings = t.TensorType.UniqueId; - else if (t.TGroup.UniqueId == TReg.LmHead.UniqueId) lmHead = t.TensorType.UniqueId; - else if (t.TGroup.UniqueId == TReg.AttnQ.UniqueId) attnQ = t.TensorType.UniqueId; - else if (t.TGroup.UniqueId == TReg.AttnKV.UniqueId) attnKV = t.TensorType.UniqueId; - else if (t.TGroup.UniqueId == TReg.AttnOutput.UniqueId) attnOutput = t.TensorType.UniqueId; - else if (t.TGroup.UniqueId == TReg.FfnUpGate.UniqueId) ffnUpGate = t.TensorType.UniqueId; - else if (t.TGroup.UniqueId == TReg.FfnDown.UniqueId) ffnDown = t.TensorType.UniqueId; - else if (t.TGroup.UniqueId == TReg.MoeExperts.UniqueId) moeExperts = t.TensorType.UniqueId; - else if (t.TGroup.UniqueId == TReg.MoeRouter.UniqueId) moeRouter = t.TensorType.UniqueId; - } + aiModelHash = new AiModelHash { UniqueHash = currentHashStr }; + db.AiModelHashes.Add(aiModelHash); + await db.SaveChangesAsync(ct); } - var c = new TensorConfig( - baseQuant, - embeddings, - lmHead, - attnQ, - attnKV, - attnOutput, - ffnUpGate, - ffnDown, - moeExperts, - moeRouter); + var tensorCombo = await GetOrCreateTensorComboAsync(db, quantConfig, ct); + + return (aiModelHash, tensorCombo); + } + + private async Task GetOrCreateTensorComboAsync( + MagicQuantContext db, + HybridQuant quant, + CancellationToken ct = default) + { + var c = (TensorConfig)quant; var existing = await db.TensorCombos.FirstOrDefaultAsync(x => x.BaseQuant == c.BaseQuant && @@ -798,14 +833,14 @@ private async Task GetOrCreateTensorComboAsync(MagicQuantContext db x.FfnUpGate == c.FfnUpGate && x.FfnDown == c.FfnDown && x.MoeExperts == c.MoeExperts && - x.MoeRouter == c.MoeRouter); + x.MoeRouter == c.MoeRouter, ct); if (existing != null) return existing; var newCombo = new TensorCombo(c); db.TensorCombos.Add(newCombo); - await db.SaveChangesAsync(); + await db.SaveChangesAsync(ct); return newCombo; } @@ -814,7 +849,8 @@ private async Task SaveBenchmarkToDbAsync( AiModelHash model, TensorCombo combo, BenchmarkResult res, - string modelPath) + string modelPath, + IReadOnlyCollection executedRunTimings) { using var transaction = await db.Database.BeginTransactionAsync(); @@ -852,6 +888,7 @@ private async Task SaveBenchmarkToDbAsync( }; db.AiBenchmarks.Add(bench); + await db.SaveChangesAsync(); } bench.TokensPerSecond = res.LlamaBench?.Tps ?? 0; @@ -863,7 +900,6 @@ private async Task SaveBenchmarkToDbAsync( } else if (bench.SizeBytes == 0) { - // only leave it zero if we truly have no better information bench.SizeBytes = 0; } @@ -882,13 +918,7 @@ private async Task SaveBenchmarkToDbAsync( string domain = kvp.Key.ToLowerInvariant(); var m = kvp.Value; - byte category = domain switch - { - "general" => (byte)BenchmarkCategory.General, - "math" => (byte)BenchmarkCategory.Math, - "code" => (byte)BenchmarkCategory.Code, - _ => throw new InvalidOperationException($"Unknown benchmark domain '{domain}'.") - }; + byte category = DomainToCategory(domain); double kld; if (isBaseModel) @@ -922,6 +952,37 @@ private async Task SaveBenchmarkToDbAsync( await db.SaveChangesAsync(); } + if (executedRunTimings.Count > 0) + { + var categoryIdLookup = await db.Set() + .Where(x => x.AiBenchmarkId == bench.Id) + .ToDictionaryAsync(x => x.Category, x => x.Id); + + foreach (var timing in executedRunTimings) + { + uint? categoryBenchmarkId = null; + if (categoryIdLookup.TryGetValue(timing.Category, out var foundCategoryId)) + categoryBenchmarkId = foundCategoryId; + + db.BenchmarkRuns.Add(new BenchmarkRun + { + Id = Guid.NewGuid(), + AiModelHashId = model.Id, + TensorComboId = combo.Id, + AiBenchmarkId = bench.Id, + CategoryBenchmarkId = categoryBenchmarkId, + Category = timing.Category, + StartedUtc = timing.StartedUtc, + CompletedUtc = timing.CompletedUtc, + DurationMs = Math.Max(0L, (long)(timing.CompletedUtc - timing.StartedUtc).TotalMilliseconds), + Succeeded = timing.Succeeded, + Error = timing.Error + }); + } + + await db.SaveChangesAsync(); + } + await transaction.CommitAsync(); } catch (Exception ex) @@ -943,6 +1004,55 @@ private async Task SaveBenchmarkToDbAsync( } } + private static byte DomainToCategory(string domain) + { + return domain.Trim().ToLowerInvariant() switch + { + "general" => (byte)BenchmarkCategory.General, + "math" => (byte)BenchmarkCategory.Math, + "code" => (byte)BenchmarkCategory.Code, + _ => throw new InvalidOperationException($"Unknown benchmark domain '{domain}'.") + }; + } + + private async Task PersistFailedBenchmarkRunAsync( + MagicQuantContext db, + uint aiModelHashId, + uint tensorComboId, + uint aiBenchmarkId, + byte category, + DateTime startedUtc, + DateTime completedUtc, + string error) + { + db.BenchmarkRuns.Add(new BenchmarkRun + { + Id = Guid.NewGuid(), + AiModelHashId = aiModelHashId, + TensorComboId = tensorComboId, + AiBenchmarkId = aiBenchmarkId, + CategoryBenchmarkId = null, + Category = category, + StartedUtc = startedUtc, + CompletedUtc = completedUtc, + DurationMs = Math.Max(0L, (long)(completedUtc - startedUtc).TotalMilliseconds), + Succeeded = false, + Error = error + }); + + await db.SaveChangesAsync(); + } + + private sealed class PendingBenchmarkRunTiming + { + public string Domain { get; set; } = string.Empty; + public byte Category { get; set; } + public DateTime StartedUtc { get; set; } + public DateTime CompletedUtc { get; set; } + public bool Succeeded { get; set; } + public string? Error { get; set; } + } + // ---------------------------------------------------------------- // Artifact reuse helpers // ---------------------------------------------------------------- @@ -983,16 +1093,6 @@ private bool TryReadExistingBenchmarkArtifacts( } } - // not currently requiring llama bench - /*string llamaBenchPath = Path.Combine(benchDir, "llamabench.md"); - if (!TryReadExistingLlamaBenchLog(llamaBenchPath, out var llamaBench)) - return false; - - var rebuilt = new BenchmarkResult - { - LlamaBench = llamaBench - };*/ - var rebuilt = new BenchmarkResult { LlamaBench = new LlamaBenchMetrics @@ -1029,10 +1129,6 @@ private bool IsReusableBenchmarkResult( IReadOnlyCollection requestedDomains, bool requireKld) { - // llama bench removed for now - /*if (result.LlamaBench == null || !result.LlamaBench.Tps.HasValue || result.LlamaBench.Tps.Value <= 0) - return false;*/ - foreach (var domain in requestedDomains) { if (!result.Perplexity.TryGetValue(domain, out var ppl)) @@ -1133,8 +1229,6 @@ private static bool HasRequiredCategories( return false; } - // no longer requiring llama bench atm until furthern otice - //return bench.TokensPerSecond > 0 && bench.SizeBytes > 0; return bench.SizeBytes > 0; } @@ -1328,7 +1422,6 @@ private bool LooksLikeSuccessfulPerplexityRun(string logFile, string logContent) } } - private static bool LooksLikeRetryableGpuFailure(string logContent) { if (string.IsNullOrWhiteSpace(logContent)) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 50a9d4b..4307e77 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -7,6 +7,7 @@ using MQ.DB; using MQ.DB.Data; using MQ.DB.Models; +using MQ.DB.Models.DbModels; using Microsoft.EntityFrameworkCore; using Spectre.Console; @@ -230,6 +231,9 @@ public async Task ProcessHybridQuantAsync( string modelBenchDir = Path.Combine(_benchDir, modelName); string baseLogitsDir = GetBaseLogitsDirectory(); + DateTime startedUtc = DateTime.UtcNow; + var stopwatch = Stopwatch.StartNew(); + // 1. Fast path: valid artifacts already exist on disk and can be synced/reused if (await _benchmarker.TryReuseExistingBenchmarksAsync( quantConfig: quant, @@ -294,8 +298,41 @@ await _benchmarker.RunAllBenchmarksAsync( saveLogits: false, domainsOverride: new[] { "general" }); + stopwatch.Stop(); + + await PersistQuantizationRunAsync( + quant: quant, + startedUtc: startedUtc, + completedUtc: DateTime.UtcNow, + succeeded: true, + outputModelPath: quantPath, + error: null, + ct: ct); + return SampleProcessState.Completed; } + catch (Exception ex) + { + stopwatch.Stop(); + + try + { + await PersistQuantizationRunAsync( + quant: quant, + startedUtc: startedUtc, + completedUtc: DateTime.UtcNow, + succeeded: false, + outputModelPath: quantPath, + error: ex.ToString(), + ct: ct); + } + catch + { + // Never hide the original exception because timing persistence failed. + } + + throw; + } finally { if (!IsProtectedModel(modelName)) @@ -357,66 +394,88 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok return false; // Require at least one category row too, so a half-baked parent row doesn't count as complete. - bool hasCategory = await db.Set() + bool hasCategory = await db.Set() .AsNoTracking() .AnyAsync(x => x.AiBenchmarkId == bench, ct); return hasCategory; } - private static ( - byte BaseQuant, - byte Embeddings, - byte LmHead, - byte AttnQ, - byte AttnKV, - byte AttnOutput, - byte FfnUpGate, - byte FfnDown, - byte MoeExperts, - byte MoeRouter) BuildTensorLookup(HybridQuant quant) + private static TensorConfig BuildTensorLookup(HybridQuant quant) + { + return (TensorConfig)quant; + } + + private async Task PersistQuantizationRunAsync( + HybridQuant quant, + DateTime startedUtc, + DateTime completedUtc, + bool succeeded, + string? outputModelPath, + string? error, + CancellationToken ct = default) { - byte embeddings = 0; - byte lmHead = 0; - byte attnQ = 0; - byte attnKV = 0; - byte attnOutput = 0; - byte ffnUpGate = 0; - byte ffnDown = 0; - byte moeExperts = 0; - byte moeRouter = 0; - - if (quant.Tensors != null) + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); + + var lookup = BuildTensorLookup(quant); + + await using var db = new MagicQuantContext(); + + var aiModelHash = await db.AiModelHashes + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (aiModelHash == null) { - foreach (var tensor in quant.Tensors) + aiModelHash = new AiModelHash { - if (tensor?.TGroup == null) - continue; - - if (tensor.TGroup.UniqueId == TReg.Embeddings.UniqueId) embeddings = tensor.TensorType.UniqueId; - else if (tensor.TGroup.UniqueId == TReg.LmHead.UniqueId) lmHead = tensor.TensorType.UniqueId; - else if (tensor.TGroup.UniqueId == TReg.AttnQ.UniqueId) attnQ = tensor.TensorType.UniqueId; - else if (tensor.TGroup.UniqueId == TReg.AttnKV.UniqueId) attnKV = tensor.TensorType.UniqueId; - else if (tensor.TGroup.UniqueId == TReg.AttnOutput.UniqueId) attnOutput = tensor.TensorType.UniqueId; - else if (tensor.TGroup.UniqueId == TReg.FfnUpGate.UniqueId) ffnUpGate = tensor.TensorType.UniqueId; - else if (tensor.TGroup.UniqueId == TReg.FfnDown.UniqueId) ffnDown = tensor.TensorType.UniqueId; - else if (tensor.TGroup.UniqueId == TReg.MoeExperts.UniqueId) moeExperts = tensor.TensorType.UniqueId; - else if (tensor.TGroup.UniqueId == TReg.MoeRouter.UniqueId) moeRouter = tensor.TensorType.UniqueId; - } + UniqueHash = Cache.CurrentModelId + }; + + db.AiModelHashes.Add(aiModelHash); + await db.SaveChangesAsync(ct); } - return ( - quant.BaseQuant.UniqueId, - embeddings, - lmHead, - attnQ, - attnKV, - attnOutput, - ffnUpGate, - ffnDown, - moeExperts, - moeRouter - ); + var tensorCombo = await db.TensorCombos.FirstOrDefaultAsync(x => + x.BaseQuant == lookup.BaseQuant && + x.Embeddings == lookup.Embeddings && + x.LmHead == lookup.LmHead && + x.AttnQ == lookup.AttnQ && + x.AttnKV == lookup.AttnKV && + x.AttnOutput == lookup.AttnOutput && + x.FfnUpGate == lookup.FfnUpGate && + x.FfnDown == lookup.FfnDown && + x.MoeExperts == lookup.MoeExperts && + x.MoeRouter == lookup.MoeRouter, ct); + + if (tensorCombo == null) + { + tensorCombo = new TensorCombo(lookup); + db.TensorCombos.Add(tensorCombo); + await db.SaveChangesAsync(ct); + } + + uint? aiBenchmarkId = await db.AiBenchmarks + .Where(x => x.AiModelHashId == aiModelHash.Id && x.TensorComboId == tensorCombo.Id) + .Select(x => (uint?)x.Id) + .FirstOrDefaultAsync(ct); + + var row = new QuantizationRun + { + Id = Guid.NewGuid(), + AiModelHashId = aiModelHash.Id, + TensorComboId = tensorCombo.Id, + AiBenchmarkId = aiBenchmarkId, + StartedUtc = startedUtc, + CompletedUtc = completedUtc, + DurationMs = Math.Max(0L, (long)(completedUtc - startedUtc).TotalMilliseconds), + Succeeded = succeeded, + Error = error, + OutputModelPath = outputModelPath + }; + + db.QuantizationRuns.Add(row); + await db.SaveChangesAsync(ct); } private bool IsProtectedModel(string name) From f220ca42bfd0403ddcc4249cbffb24ecb7dc7f17 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 15 Apr 2026 19:06:22 -0400 Subject: [PATCH 052/258] Implement learned per-tensor baseline quant mapping pipeline --- MQ.DB/Cache.cs | 4 +- MQ.DB/Data/MagicQuantContext.cs | 46 +- ...20260415220000_AddLearnedBaselineTables.cs | 100 ++++ MQ.DB/Models/BaselineQuants.cs | 67 ++- .../DbModels/BaselineQuantDefinition.cs | 30 ++ .../DbModels/LearnedBaselineTensorQuant.cs | 63 +++ MagicQuant/Commands/Evolution.cs | 11 +- MagicQuant/Program.cs | 5 + MagicQuant/Services/QuantizationService.cs | 496 +++++++++++++----- 9 files changed, 677 insertions(+), 145 deletions(-) create mode 100644 MQ.DB/Migrations/20260415220000_AddLearnedBaselineTables.cs create mode 100644 MQ.DB/Models/DbModels/BaselineQuantDefinition.cs create mode 100644 MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index d4aeb07..81856b8 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -59,4 +59,6 @@ public enum MainTorchType public static List UnusedTensorGroups = new List(); public static string CurrentModelId { get; set; } -} \ No newline at end of file + + public static bool ForceRelearnBaselineTensorMappings { get; set; } +} diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index dfe1eed..1ab9aeb 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -58,6 +58,48 @@ private void InitializeDatabase() // 🔥 Apply migrations automatically Database.Migrate(); + EnsureBaselineQuantDefinitions(); + } + + private void EnsureBaselineQuantDefinitions() + { + var expected = BaselineQuants.All + .Select(x => new BaselineQuantDefinition + { + BaselineQuantId = x.UniqueId, + BaselineName = x.Names[0], + DefaultTensorSchemeId = x.DefaultTensorScheme!.UniqueId, + DefaultTensorSchemeName = x.DefaultTensorScheme.Names[0] + }) + .OrderBy(x => x.BaselineQuantId) + .ToList(); + + var current = BaselineQuantDefinitions + .AsNoTracking() + .OrderBy(x => x.BaselineQuantId) + .ToList(); + + if (current.Count == 0) + { + BaselineQuantDefinitions.AddRange(expected); + SaveChanges(); + return; + } + + var mismatch = current.Count != expected.Count || + current.Zip(expected, (a, b) => + a.BaselineQuantId == b.BaselineQuantId && + a.DefaultTensorSchemeId == b.DefaultTensorSchemeId && + string.Equals(a.BaselineName, b.BaselineName, StringComparison.Ordinal) && + string.Equals(a.DefaultTensorSchemeName, b.DefaultTensorSchemeName, StringComparison.Ordinal)) + .Any(equal => !equal); + + if (mismatch) + { + throw new InvalidOperationException( + "BaselineQuantDefinitions table is out of sync with code-defined BaselineQuants/DefaultTensorScheme mappings. " + + "Run migrations and regenerate the DB definitions."); + } } private static bool IsDesignTime() @@ -76,6 +118,8 @@ private static bool IsDesignTime() public DbSet TensorCombos { get; set; } public DbSet QuantizationRuns { get; set; } public DbSet BenchmarkRuns { get; set; } + public DbSet LearnedBaselineTensorQuants { get; set; } + public DbSet BaselineQuantDefinitions { get; set; } // -------------------------------------------------------- // Configuration @@ -132,4 +176,4 @@ private void ValidateDbSetsImplementInterface() ); } } -} \ No newline at end of file +} diff --git a/MQ.DB/Migrations/20260415220000_AddLearnedBaselineTables.cs b/MQ.DB/Migrations/20260415220000_AddLearnedBaselineTables.cs new file mode 100644 index 0000000..fafd30b --- /dev/null +++ b/MQ.DB/Migrations/20260415220000_AddLearnedBaselineTables.cs @@ -0,0 +1,100 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + public partial class AddLearnedBaselineTables : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "BaselineQuantDefinitions", + columns: table => new + { + BaselineQuantId = table.Column(type: "INTEGER", nullable: false), + BaselineName = table.Column(type: "TEXT", maxLength: 64, nullable: false), + DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), + DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.BaselineQuantId); + }); + + migrationBuilder.CreateTable( + name: "LearnedBaselineTensorQuants", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AiBenchmarkId = table.Column(type: "INTEGER", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + BaselineQuantId = table.Column(type: "INTEGER", nullable: false), + TensorWeightSchemeId = table.Column(type: "INTEGER", nullable: false), + TensorGroupId = table.Column(type: "INTEGER", nullable: false), + TensorName = table.Column(type: "TEXT", maxLength: 512, nullable: false), + FinalQuantType = table.Column(type: "TEXT", maxLength: 32, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_LearnedBaselineTensorQuants", x => x.Id); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_BaselineName", + table: "BaselineQuantDefinitions", + column: "BaselineName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeId", + table: "BaselineQuantDefinitions", + column: "DefaultTensorSchemeId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeName", + table: "BaselineQuantDefinitions", + column: "DefaultTensorSchemeName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiBenchmarkId", + table: "LearnedBaselineTensorQuants", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorGroupId", + table: "LearnedBaselineTensorQuants", + columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId" }); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorName", + table: "LearnedBaselineTensorQuants", + columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName" }, + unique: true); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "LearnedBaselineTensorQuants"); + + migrationBuilder.DropTable( + name: "BaselineQuantDefinitions"); + } + } +} diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 755c026..09ea438 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -16,21 +16,7 @@ public record BaselineQuants( public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"], TensorWeightScheme.Q5_K); public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"], TensorWeightScheme.Q4_K); - /*public static readonly BaselineQuants MXFP4_MOE = new( - 4, - false, - ["MXFP4_MOE"], - new HybridQuant - { - BaseQuant = null!, - Tensors = TReg.All - .Select(g => new HybridTensor - { - TGroup = g, - TensorType = TensorWeightScheme.MXFP4 - }) - .ToList() - });*/ + public static readonly BaselineQuants MXFP4 = new(4, false, ["MXFP4"], TensorWeightScheme.MXFP4); public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"], TensorWeightScheme.IQ4_NL); @@ -61,7 +47,7 @@ public record BaselineQuants( Q6_K, Q5_K, Q4_K_M, - //MXFP4_MOE, + MXFP4, IQ4_NL, IQ4_XS, //IQ3_M, @@ -70,8 +56,53 @@ public record BaselineQuants( static BaselineQuants() { - //MXFP4_MOE.BaseConversionBase!.BaseQuant = MXFP4_MOE; IQ4_XS.BaseConversionBase!.BaseQuant = IQ4_XS; + ValidateIntegrityOrThrow(); + } + + public static void ValidateIntegrityOrThrow() + { + var invalidBaselines = All + .Where(x => x.DefaultTensorScheme == null) + .Select(x => x.Names.IsDefaultOrEmpty ? $"id:{x.UniqueId}" : x.Names[0]) + .ToList(); + + if (invalidBaselines.Count > 0) + { + throw new InvalidOperationException( + "Every BaselineQuants entry must define DefaultTensorScheme. Missing for: " + + string.Join(", ", invalidBaselines)); + } + + var duplicateDefaultSchemeIds = All + .GroupBy(x => x.DefaultTensorScheme!.UniqueId) + .Where(g => g.Count() > 1) + .Select(g => g.Key) + .ToList(); + + if (duplicateDefaultSchemeIds.Count > 0) + { + var duplicateNames = duplicateDefaultSchemeIds + .Select(id => TensorWeightScheme.All.First(s => s.UniqueId == id).Names[0]); + + throw new InvalidOperationException( + "DefaultTensorScheme must be unique across BaselineQuants entries. Duplicates: " + + string.Join(", ", duplicateNames)); + } + + var schemesMissingBaseline = TensorWeightScheme.All + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Where(x => !All.Any(b => b.DefaultTensorScheme!.UniqueId == x.UniqueId)) + .Select(x => x.Names[0]) + .ToList(); + + if (schemesMissingBaseline.Count > 0) + { + throw new InvalidOperationException( + "Every TensorWeightScheme must be linked by exactly one BaselineQuants.DefaultTensorScheme. Missing for: " + + string.Join(", ", schemesMissingBaseline)); + } } public static BaselineQuants GetBF16Quant() @@ -94,4 +125,4 @@ public static BaselineQuants FromId(byte id) return found; } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs new file mode 100644 index 0000000..d35bd30 --- /dev/null +++ b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class BaselineQuantDefinition : ISQLiteEntity +{ + public byte BaselineQuantId { get; set; } + public string BaselineName { get; set; } = string.Empty; + public byte DefaultTensorSchemeId { get; set; } + public string DefaultTensorSchemeName { get; set; } = string.Empty; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.BaselineQuantId); + + builder.Property(x => x.BaselineName) + .HasMaxLength(64) + .IsRequired(); + + builder.Property(x => x.DefaultTensorSchemeName) + .HasMaxLength(64) + .IsRequired(); + + builder.HasIndex(x => x.BaselineName).IsUnique(); + builder.HasIndex(x => x.DefaultTensorSchemeId).IsUnique(); + builder.HasIndex(x => x.DefaultTensorSchemeName).IsUnique(); + } +} diff --git a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs new file mode 100644 index 0000000..e013d9b --- /dev/null +++ b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs @@ -0,0 +1,63 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class LearnedBaselineTensorQuant : ISQLiteEntity +{ + public ulong Id { get; set; } + + public uint AiBenchmarkId { get; set; } + public AiBenchmark AiBenchmark { get; set; } = default!; + + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + + public byte BaselineQuantId { get; set; } + public byte TensorWeightSchemeId { get; set; } + public byte TensorGroupId { get; set; } + + public string TensorName { get; set; } = string.Empty; + public string FinalQuantType { get; set; } = string.Empty; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + + builder.Property(x => x.TensorName) + .HasMaxLength(512) + .IsRequired(); + + builder.Property(x => x.FinalQuantType) + .HasMaxLength(32) + .IsRequired(); + + builder.HasIndex(x => new + { + x.AiModelHashId, + x.BaselineQuantId, + x.TensorWeightSchemeId, + x.TensorName + }) + .IsUnique(); + + builder.HasIndex(x => new + { + x.AiModelHashId, + x.BaselineQuantId, + x.TensorWeightSchemeId, + x.TensorGroupId + }); + + builder.HasOne(x => x.AiModelHash) + .WithMany() + .HasForeignKey(x => x.AiModelHashId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.AiBenchmark) + .WithMany() + .HasForeignKey(x => x.AiBenchmarkId) + .OnDelete(DeleteBehavior.Cascade); + } +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 352a909..a25478c 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -50,6 +50,8 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + Cache.ForceRelearnBaselineTensorMappings = args.Any(a => + string.Equals(a.Name, "relearn-baseline-mappings", StringComparison.OrdinalIgnoreCase)); JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) @@ -72,6 +74,12 @@ public async Task Run(List args) var benchmarkService = new BenchmarkService(pyManager); var quantizationService = new QuantizationService(benchmarkService); + if (Cache.ForceRelearnBaselineTensorMappings) + { + await quantizationService.ClearLearnedBaselineTensorMappingsAsync(); + AnsiConsole.MarkupLine("[yellow]Forced relearn is ON: pure baseline samples will be rebuilt and relearned.[/]"); + } + var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); @@ -239,8 +247,9 @@ private void ShowEvolutionHelp() AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Arguments:[/]"); AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Required)"); + AnsiConsole.MarkupLine(" [green]--relearn-baseline-mappings[/] Delete and relearn baseline tensor mappings (Optional)"); AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } -} \ No newline at end of file +} diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 0e116a1..fdcbeaa 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -6,6 +6,7 @@ using MagicQuant.Models; using Spectre.Console; using System.Collections.Immutable; +using MQ.DB.Models; #if DEBUG // If we are in Debug and no arguments were passed, default to "evolution" @@ -51,6 +52,10 @@ try { + // strict startup integrity checks + TensorWeightScheme.ValidateSmallestConfiguration(); + BaselineQuants.ValidateIntegrityOrThrow(); + // 6. Mandatory Validation for non-init commands if (!commandInput.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) { diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 4307e77..94f6350 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -3,6 +3,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.Json; +using System.Text.RegularExpressions; using MagicQuant.Helpers; using MQ.DB; using MQ.DB.Data; @@ -114,8 +115,60 @@ public async Task ProcessHybridBatchAsync( await EnsureBaseModelFileAsync(false); + var learnableBaselinePlans = plans + .Where(p => IsLearnableBaselineRun(p.Quant)) + .OrderBy(p => p.Quant.BaseQuant.UniqueId) + .ToList(); + + foreach (var baselinePlan in learnableBaselinePlans) + { + var baselineRecord = new SampleProcessingRecord + { + Plan = baselinePlan, + ModelName = GenerateHybridName(baselinePlan.Quant) + }; + + try + { + var state = await ProcessHybridQuantAsync(baselinePlan.Quant, ct); + baselineRecord.State = state; + + var identity = await ResolveBenchmarkIdentityAsync(baselinePlan.Quant, ct); + baselineRecord.TensorComboId = identity.TensorComboId; + baselineRecord.BenchmarkId = identity.BenchmarkId; + + switch (state) + { + case SampleProcessState.Completed: + completed++; + break; + case SampleProcessState.Skipped: + skipped++; + break; + default: + failed++; + break; + } + } + catch (Exception ex) + { + baselineRecord.State = SampleProcessState.Failed; + baselineRecord.Error = ex.Message; + failed++; + + AnsiConsole.MarkupLine($"[red]Baseline sample failed:[/] {Markup.Escape(baselineRecord.ModelName)}"); + AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); + } + finally + { + records.Add(baselineRecord); + } + } + + var remainingPlans = plans.Except(learnableBaselinePlans).ToList(); + await Parallel.ForEachAsync( - plans, + remainingPlans, new ParallelOptions { MaxDegreeOfParallelism = _maxConcurrentQuantizations, @@ -233,9 +286,10 @@ public async Task ProcessHybridQuantAsync( DateTime startedUtc = DateTime.UtcNow; var stopwatch = Stopwatch.StartNew(); + var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); // 1. Fast path: valid artifacts already exist on disk and can be synced/reused - if (await _benchmarker.TryReuseExistingBenchmarksAsync( + if (!forceBaselineRelearn && await _benchmarker.TryReuseExistingBenchmarksAsync( quantConfig: quant, modelPath: quantPath, benchDir: modelBenchDir, @@ -251,7 +305,7 @@ public async Task ProcessHybridQuantAsync( } // 2. DB truth still matters too - if (await BenchmarkExistsAsync(quant, ct)) + if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) { AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); @@ -264,14 +318,15 @@ public async Task ProcessHybridQuantAsync( try { string basePath = await EnsureBaseModelFileAsync(); + QuantizationExecutionReport? quantizationReport = null; await _cpuQuantLock.WaitAsync(ct); try { - if (!File.Exists(quantPath)) + if (!File.Exists(quantPath) || forceBaselineRelearn) { AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); - await RunLlamaQuantizeAsync(basePath, quantPath, quant); + quantizationReport = await RunLlamaQuantizeAsync(basePath, quantPath, quant); } } finally @@ -280,7 +335,7 @@ public async Task ProcessHybridQuantAsync( } // Re-check after build in case another worker finished the DB sync while we were quantizing - if (await BenchmarkExistsAsync(quant, ct)) + if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) { if (!IsProtectedModel(modelName)) await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); @@ -309,6 +364,11 @@ await PersistQuantizationRunAsync( error: null, ct: ct); + if (IsLearnableBaselineRun(quant)) + { + await LearnAndPersistBaselineTensorMapAsync(quant, quantPath, quantizationReport, ct); + } + return SampleProcessState.Completed; } catch (Exception ex) @@ -656,7 +716,7 @@ public async Task EnsurePureQ8ModelAsync() // Quantization // ---------------------------------------------------------------- - public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) + private async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) { if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); @@ -724,6 +784,12 @@ public async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, Hyb } AnsiConsole.MarkupLine($"[green]Quantized model ready:[/] {Markup.Escape(outputFile)}"); + + return new QuantizationExecutionReport + { + LogPath = quantizeLogPath, + ResolvedOverrides = concreteOverrides + }; } private static string ResolveQuantizeBaseArgument( @@ -743,6 +809,164 @@ private static string ResolveQuantizeBaseArgument( return ResolveBaseName(quant.BaseQuant); } + public async Task ClearLearnedBaselineTensorMappingsAsync(CancellationToken ct = default) + { + await using var db = new MagicQuantContext(); + int removed = await db.LearnedBaselineTensorQuants.ExecuteDeleteAsync(ct); + AnsiConsole.MarkupLine($"[yellow]Relearn requested:[/] removed [red]{removed:N0}[/] learned baseline tensor mapping rows."); + } + + private static bool IsLearnableBaselineRun(HybridQuant quant) + { + return quant.Tensors.Count == 0 && + quant.BaseQuant.UniqueId != BaselineQuants.NativeSourceUniqueId && + quant.BaseQuant.DefaultTensorScheme != null; + } + + private async Task LearnAndPersistBaselineTensorMapAsync( + HybridQuant quant, + string quantizedModelPath, + QuantizationExecutionReport? report, + CancellationToken ct) + { + if (!IsLearnableBaselineRun(quant)) + return; + + var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; + var parsed = ParseQuantizeLogForTensorTypes(report?.LogPath ?? (quantizedModelPath + ".quantize.log")); + if (parsed.Count == 0) + { + AnsiConsole.MarkupLine( + $"[red]WARNING:[/] learned mapping parse returned no tensors for baseline [yellow]{quant.BaseQuant.Names[0]}[/]."); + return; + } + + var grouped = AssignGroups(parsed.Keys); + var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); + if (ambiguous.Count > 0) + { + AnsiConsole.MarkupLine( + $"[red]WARNING:[/] {ambiguous.Count} tensor(s) matched multiple groups while learning baseline {quant.BaseQuant.Names[0]}."); + AnsiConsole.MarkupLine($"[grey]Example: {Markup.Escape(ambiguous[0].Key)} => {string.Join(", ", ambiguous[0].Value.MatchedGroups)}[/]"); + } + + var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).ToList(); + if (unresolved.Count > 0) + { + AnsiConsole.MarkupLine( + $"[yellow]WARNING:[/] {unresolved.Count} tensor(s) had no tensor-group match while learning baseline {quant.BaseQuant.Names[0]}."); + } + + await using var db = new MagicQuantContext(); + + var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + if (model == null) + throw new InvalidOperationException("Unable to persist learned mappings because AiModelHash row was not found."); + + var combo = await db.TensorCombos + .AsNoTracking() + .FirstAsync(x => x.BaseQuant == quant.BaseQuant.UniqueId && + x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && + x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); + + var benchmarkId = await db.AiBenchmarks + .Where(x => x.AiModelHashId == model.Id && x.TensorComboId == combo.Id) + .OrderByDescending(x => x.Id) + .Select(x => (uint?)x.Id) + .FirstOrDefaultAsync(ct); + + if (!benchmarkId.HasValue) + throw new InvalidOperationException($"Unable to persist learned mappings because no AiBenchmark exists for baseline '{quant.BaseQuant.Names[0]}'."); + + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == model.Id && + x.BaselineQuantId == quant.BaseQuant.UniqueId && + x.TensorWeightSchemeId == tensorScheme.UniqueId) + .ExecuteDeleteAsync(ct); + + var rows = new List(parsed.Count); + foreach (var kv in parsed.OrderBy(x => x.Key, StringComparer.Ordinal)) + { + var match = grouped[kv.Key]; + if (match.PrimaryGroup == null) + continue; + + rows.Add(new LearnedBaselineTensorQuant + { + AiBenchmarkId = benchmarkId.Value, + AiModelHashId = model.Id, + BaselineQuantId = quant.BaseQuant.UniqueId, + TensorWeightSchemeId = tensorScheme.UniqueId, + TensorGroupId = match.PrimaryGroup.UniqueId, + TensorName = kv.Key, + FinalQuantType = kv.Value + }); + } + + if (rows.Count == 0) + throw new InvalidOperationException($"Learning baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); + + db.LearnedBaselineTensorQuants.AddRange(rows); + await db.SaveChangesAsync(ct); + + AnsiConsole.MarkupLine( + $"[green]Learned baseline tensor mapping persisted:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{quant.BaseQuant.Names[0]}[/]."); + } + + private Dictionary ParseQuantizeLogForTensorTypes(string logPath) + { + if (!File.Exists(logPath)) + { + AnsiConsole.MarkupLine($"[red]WARNING:[/] quantization log does not exist, cannot learn tensor mapping: {Markup.Escape(logPath)}"); + return new Dictionary(StringComparer.Ordinal); + } + + var byTensor = new Dictionary(StringComparer.Ordinal); + + foreach (var raw in File.ReadLines(logPath)) + { + var match = TensorLogLineRegex.Match(raw); + if (!match.Success) + continue; + + string tensorName = match.Groups["tensor"].Value.Trim(); + string declaredType = NormalizeQuantName(match.Groups["type"].Value); + + string final = declaredType; + var convert = match.Groups["convert"]; + if (convert.Success && !string.IsNullOrWhiteSpace(convert.Value)) + final = NormalizeQuantName(convert.Value); + + byTensor[tensorName] = final; + } + + return byTensor; + } + + private Dictionary AssignGroups(IEnumerable tensorNames) + { + var dict = new Dictionary(StringComparer.Ordinal); + + foreach (var tensorName in tensorNames) + { + var matched = new List(); + + foreach (var group in TReg.All) + { + if (group.Tensors.Any(pattern => Regex.IsMatch(tensorName, $"^{pattern}$"))) + matched.Add(group); + } + + dict[tensorName] = new TensorGroupingResult + { + MatchedGroups = matched.Select(x => x.Name).ToList(), + PrimaryGroup = matched.FirstOrDefault() + }; + } + + return dict; + } + private static TensorWeightScheme? TryResolveBaseTensorScheme(BaselineQuants baseQuant) { if (baseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId) @@ -778,150 +1002,148 @@ private List BuildRequestedTensorOverrides(HybridQuant if (baseScheme != null && hybrid.TensorType.UniqueId == baseScheme.UniqueId) continue; - string schemeName = ResolveSchemeName(hybrid.TensorType); + var learned = TryLoadLearnedTensorMapping(hybrid.TensorType, hybrid.TGroup); + if (learned.Count == 0) + { + throw new InvalidOperationException( + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + scheme '{hybrid.TensorType.Names[0]}'. " + + "Run with --relearn-baseline-mappings to regenerate."); + } - result.Add(new RequestedTensorOverride + foreach (var kv in learned) { - GroupName = hybrid.TGroup.Name, - SchemeName = schemeName, - Patterns = hybrid.TGroup.Tensors.ToList() - }); + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + TensorName = kv.Key, + SchemeName = kv.Value + }); + } } return result; } - private async Task> ResolveConcreteTensorOverridesAsync( - string inputGgufPath, - string outputFilePath, - List requestedOverrides) + private Dictionary TryLoadLearnedTensorMapping(TensorWeightScheme sourceScheme, TensorGroup targetGroup) { - if (requestedOverrides.Count == 0) - return new List(); - - string workingDir = Path.GetDirectoryName(outputFilePath)!; - string unique = Guid.NewGuid().ToString("N"); - - string payloadPath = Path.Combine(workingDir, $"resolve_tensor_overrides_{unique}.json"); - string resultPath = Path.Combine(workingDir, $"resolve_tensor_overrides_result_{unique}.json"); - string scriptPath = Path.Combine(workingDir, $"resolve_tensor_overrides_{unique}.py"); + using var db = new MagicQuantContext(); - try - { - var payload = new - { - gguf_path = inputGgufPath, - output_path = resultPath, - requests = requestedOverrides - }; + var model = db.AiModelHashes + .AsNoTracking() + .FirstOrDefault(x => x.UniqueHash == Cache.CurrentModelId); - await File.WriteAllTextAsync( - payloadPath, - JsonSerializer.Serialize(payload, new JsonSerializerOptions { WriteIndented = true })); + if (model == null) + return new Dictionary(StringComparer.Ordinal); - string py = """ - import json - import re - import sys + var baseline = BaselineQuants.All.FirstOrDefault(x => x.DefaultTensorScheme?.UniqueId == sourceScheme.UniqueId); + if (baseline == null) + return new Dictionary(StringComparer.Ordinal); - payload_path = sys.argv[1] + var rows = db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == model.Id) + .Where(x => x.BaselineQuantId == baseline.UniqueId) + .Where(x => x.TensorWeightSchemeId == sourceScheme.UniqueId) + .Where(x => x.TensorGroupId == targetGroup.UniqueId) + .OrderBy(x => x.TensorName) + .ToList(); - def write_result(obj, output_path): - with open(output_path, "w", encoding="utf-8") as f: - json.dump(obj, f, indent=2) + if (rows.Count == 0) + return new Dictionary(StringComparer.Ordinal); - with open(payload_path, "r", encoding="utf-8") as f: - payload = json.load(f) + return rows.ToDictionary(x => x.TensorName, x => x.FinalQuantType, StringComparer.Ordinal); + } - output_path = payload["output_path"] + private async Task> ResolveConcreteTensorOverridesAsync( + string inputGgufPath, + string outputFilePath, + List requestedOverrides) + { + if (requestedOverrides.Count == 0) + return new List(); + var allTensorNames = await ReadTensorNamesFromGgufAsync(inputGgufPath, outputFilePath); + var nameSet = allTensorNames.ToHashSet(StringComparer.Ordinal); - try: - import gguf - except Exception as e: - write_result({"Error": f"Failed to import gguf: {e}"}, output_path) - sys.exit(0) + var missing = requestedOverrides + .Where(x => !nameSet.Contains(x.TensorName)) + .ToList(); - try: - reader = gguf.GGUFReader(payload["gguf_path"]) - except Exception as e: - write_result({"Error": f"Failed to read GGUF: {e}"}, output_path) - sys.exit(0) + if (missing.Count > 0) + { + throw new InvalidOperationException( + $"Required learned tensor mappings were missing in source GGUF ({missing.Count} tensors). " + + $"Examples: {string.Join(", ", missing.Take(10).Select(x => x.TensorName))}"); + } - tensor_names = [t.name for t in reader.tensors] + var duplicates = requestedOverrides + .GroupBy(x => x.TensorName, StringComparer.Ordinal) + .Where(g => g.Select(x => x.SchemeName).Distinct(StringComparer.OrdinalIgnoreCase).Count() > 1) + .Select(g => g.Key) + .ToList(); - resolved = [] - group_counts = {} - unmatched = [] - duplicates = [] - seen = {} + if (duplicates.Count > 0) + { + throw new InvalidOperationException( + $"Conflicting learned mappings tried to assign multiple quant types to the same tensor: " + + $"{string.Join(", ", duplicates.Take(20))}"); + } - for req in payload["requests"]: - group = req["GroupName"] - scheme = req["SchemeName"] - patterns = req["Patterns"] + return requestedOverrides + .GroupBy(x => x.TensorName, StringComparer.Ordinal) + .Select(g => g.First()) + .Select(x => new ConcreteTensorOverride + { + GroupName = x.GroupName, + SchemeName = x.SchemeName, + TensorName = x.TensorName + }) + .ToList(); + } - compiled = [re.compile(p) for p in patterns] - matches = [] + private async Task> ReadTensorNamesFromGgufAsync(string ggufPath, string outputFilePath) + { + string workingDir = Path.GetDirectoryName(outputFilePath)!; + string unique = Guid.NewGuid().ToString("N"); + string payloadPath = Path.Combine(workingDir, $"read_gguf_tensors_{unique}.json"); + string resultPath = Path.Combine(workingDir, $"read_gguf_tensors_result_{unique}.json"); + string scriptPath = Path.Combine(workingDir, $"read_gguf_tensors_{unique}.py"); - for name in tensor_names: - if any(r.fullmatch(name) for r in compiled): - matches.append(name) + try + { + await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new { gguf_path = ggufPath, output_path = resultPath })); - group_counts[group] = len(matches) + const string py = """ + import json + import sys - if len(matches) == 0: - unmatched.append(group) + payload_path = sys.argv[1] + with open(payload_path, "r", encoding="utf-8") as f: + payload = json.load(f) - for name in matches: - if name in seen and seen[name] != group: - duplicates.append(name) - else: - seen[name] = group + output_path = payload["output_path"] - resolved.append({ - "TensorName": name, - "SchemeName": scheme, - "GroupName": group - }) + try: + import gguf + reader = gguf.GGUFReader(payload["gguf_path"]) + tensor_names = [t.name for t in reader.tensors] + result = {"TensorNames": tensor_names} + except Exception as e: + result = {"Error": str(e), "TensorNames": []} - write_result({ - "Resolved": resolved, - "GroupMatchCounts": group_counts, - "UnmatchedGroups": unmatched, - "DuplicateTensors": sorted(set(duplicates)) - }, output_path) - """; + with open(output_path, "w", encoding="utf-8") as f: + json.dump(result, f, indent=2) + """; await File.WriteAllTextAsync(scriptPath, py); await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); - if (!File.Exists(resultPath)) - throw new InvalidOperationException("Tensor override resolution produced no result file."); - - var result = JsonSerializer.Deserialize( - await File.ReadAllTextAsync(resultPath)); - + var result = JsonSerializer.Deserialize(await File.ReadAllTextAsync(resultPath)); if (result == null) - throw new InvalidOperationException("Tensor override resolution returned null."); - + throw new InvalidOperationException("Failed to parse GGUF tensor list result."); if (!string.IsNullOrWhiteSpace(result.Error)) - throw new InvalidOperationException(result.Error); + throw new InvalidOperationException($"Failed to read GGUF tensor names: {result.Error}"); - if (result.UnmatchedGroups.Count > 0) - { - throw new InvalidOperationException( - $"The following requested override groups matched zero tensors in the input GGUF: " + - $"{string.Join(", ", result.UnmatchedGroups)}"); - } - - if (result.DuplicateTensors.Count > 0) - { - throw new InvalidOperationException( - $"A tensor matched more than one override group, which is ambiguous: " + - $"{string.Join(", ", result.DuplicateTensors.Take(20))}"); - } - - return result.Resolved; + return result.TensorNames; } finally { @@ -935,11 +1157,34 @@ import gguf // Internal DTOs // ---------------------------------------------------------------- + private static readonly Regex TensorLogLineRegex = new( + @"\]\s+(?[^\s]+)\s+-\s+\[[^\]]+\],\s+type\s*=\s*(?[A-Za-z0-9_]+)(?:.*?converting to\s+(?[A-Za-z0-9_]+))?", + RegexOptions.Compiled | RegexOptions.CultureInvariant); + + private static string NormalizeQuantName(string value) + { + var normalized = value.Trim().ToUpperInvariant(); + return normalized.Replace("Q5_K", "Q5_K") + .Replace("Q6_K", "Q6_K") + .Replace("Q8_0", "Q8_0") + .Replace("IQ4_XS", "IQ4_XS") + .Replace("IQ4_NL", "IQ4_NL") + .Replace("BF16", "BF16") + .Replace("F16", "F16") + .Replace("F32", "F32"); + } + + private sealed class QuantizationExecutionReport + { + public string LogPath { get; set; } = string.Empty; + public List ResolvedOverrides { get; set; } = new(); + } + private sealed class RequestedTensorOverride { public string GroupName { get; set; } = string.Empty; + public string TensorName { get; set; } = string.Empty; public string SchemeName { get; set; } = string.Empty; - public List Patterns { get; set; } = new(); } private sealed class ConcreteTensorOverride @@ -949,13 +1194,16 @@ private sealed class ConcreteTensorOverride public string GroupName { get; set; } = string.Empty; } - private sealed class TensorResolutionResult + private sealed class TensorGroupingResult + { + public TensorGroup? PrimaryGroup { get; set; } + public List MatchedGroups { get; set; } = new(); + } + + private sealed class TensorNameReadResult { public string? Error { get; set; } - public List Resolved { get; set; } = new(); - public Dictionary GroupMatchCounts { get; set; } = new(); - public List UnmatchedGroups { get; set; } = new(); - public List DuplicateTensors { get; set; } = new(); + public List TensorNames { get; set; } = new(); } // ---------------------------------------------------------------- @@ -1138,4 +1386,4 @@ void HandleLine(string? line, bool isError) StdErr = stderrBuilder.ToString() }; } -} \ No newline at end of file +} From f41063b41f599394785719575842e37e19a57fe6 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 15 Apr 2026 19:21:53 -0400 Subject: [PATCH 053/258] Harden learned mapping truth verification and relearn rebuild --- MQ.DB/Models/BaselineQuants.cs | 6 +- MagicQuant/Commands/Evolution.cs | 2 +- MagicQuant/Services/QuantizationService.cs | 295 +++++++++++++++++++-- 3 files changed, 278 insertions(+), 25 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 09ea438..ae95230 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -16,8 +16,6 @@ public record BaselineQuants( public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"], TensorWeightScheme.Q5_K); public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"], TensorWeightScheme.Q4_K); - public static readonly BaselineQuants MXFP4 = new(4, false, ["MXFP4"], TensorWeightScheme.MXFP4); - public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"], TensorWeightScheme.IQ4_NL); public static readonly BaselineQuants IQ4_XS = new( @@ -47,7 +45,6 @@ public record BaselineQuants( Q6_K, Q5_K, Q4_K_M, - MXFP4, IQ4_NL, IQ4_XS, //IQ3_M, @@ -93,6 +90,9 @@ public static void ValidateIntegrityOrThrow() var schemesMissingBaseline = TensorWeightScheme.All .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + // Some schemes can be experimental and intentionally not promoted to baseline. + // Hard-enforce only for the established shipped baseline set. + .Where(x => x.UniqueId != TensorWeightScheme.MXFP4.UniqueId) .Where(x => !All.Any(b => b.DefaultTensorScheme!.UniqueId == x.UniqueId)) .Select(x => x.Names[0]) .ToList(); diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index a25478c..06d5e0b 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -76,7 +76,7 @@ public async Task Run(List args) if (Cache.ForceRelearnBaselineTensorMappings) { - await quantizationService.ClearLearnedBaselineTensorMappingsAsync(); + await quantizationService.InvalidateBaselineArtifactsAsync(); AnsiConsole.MarkupLine("[yellow]Forced relearn is ON: pure baseline samples will be rebuilt and relearned.[/]"); } diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 94f6350..da5d02c 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -723,13 +723,13 @@ private async Task RunLlamaQuantizeAsync(string inp Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); - var requestedOverrides = BuildRequestedTensorOverrides(quant); + var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, outputFile); + var requestedOverrides = BuildRequestedTensorOverrides(quant, inputTensorMetadata.TensorNames); // Keep this resolution step: // it is not output validation; it is how logical group rules become real tensor names. - var concreteOverrides = await ResolveConcreteTensorOverridesAsync( - inputGgufPath: inputFile, - outputFilePath: outputFile, + var concreteOverrides = ResolveConcreteTensorOverrides( + allTensorNames: inputTensorMetadata.TensorNames, requestedOverrides: requestedOverrides); if (requestedOverrides.Count > 0 && concreteOverrides.Count == 0) @@ -816,6 +816,34 @@ public async Task ClearLearnedBaselineTensorMappingsAsync(CancellationToken ct = AnsiConsole.MarkupLine($"[yellow]Relearn requested:[/] removed [red]{removed:N0}[/] learned baseline tensor mapping rows."); } + public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = default) + { + await ClearLearnedBaselineTensorMappingsAsync(ct); + + foreach (var baseline in BaselineQuants.All) + { + var pure = HybridQuant.CreatePureBaseline(baseline); + var name = GenerateHybridName(pure); + var ggufPath = Path.Combine(_ggufDir, $"{name}.gguf"); + var success = Path.Combine(_ggufDir, $"{name}.gguf.success.json"); + var log = ggufPath + ".quantize.log"; + + await HardDeleteHelper.DeleteFileIfExistsAsync(ggufPath); + await HardDeleteHelper.DeleteFileIfExistsAsync(success); + await HardDeleteHelper.DeleteFileIfExistsAsync(log); + + string benchDir = Path.Combine(_benchDir, name); + if (Directory.Exists(benchDir)) + Directory.Delete(benchDir, recursive: true); + } + + string debugDir = Path.Combine(_benchDir, "_learning_debug"); + if (Directory.Exists(debugDir)) + Directory.Delete(debugDir, recursive: true); + + AnsiConsole.MarkupLine("[yellow]Relearn requested:[/] baseline artifacts, benchmark caches, and learning diagnostics were invalidated."); + } + private static bool IsLearnableBaselineRun(HybridQuant quant) { return quant.Tensors.Count == 0 && @@ -834,14 +862,22 @@ private async Task LearnAndPersistBaselineTensorMapAsync( var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; var parsed = ParseQuantizeLogForTensorTypes(report?.LogPath ?? (quantizedModelPath + ".quantize.log")); - if (parsed.Count == 0) + var ggufMetadata = await ReadTensorMetadataFromGgufAsync(quantizedModelPath, quantizedModelPath); + var ggufTruth = ggufMetadata.TensorTypes + .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); + + if (parsed.Count == 0 && ggufTruth.Count == 0) { AnsiConsole.MarkupLine( - $"[red]WARNING:[/] learned mapping parse returned no tensors for baseline [yellow]{quant.BaseQuant.Names[0]}[/]."); + $"[red]WARNING:[/] learned mapping parse returned no tensors from logs and GGUF for baseline [yellow]{quant.BaseQuant.Names[0]}[/]."); return; } - var grouped = AssignGroups(parsed.Keys); + var truth = BuildTruthMapWithVerification(parsed, ggufTruth, quant.BaseQuant.Names[0]); + if (truth.Count == 0) + throw new InvalidOperationException($"No verified tensor truth entries were available for baseline '{quant.BaseQuant.Names[0]}'."); + + var grouped = AssignGroups(truth.Keys); var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); if (ambiguous.Count > 0) { @@ -884,8 +920,8 @@ await db.LearnedBaselineTensorQuants x.TensorWeightSchemeId == tensorScheme.UniqueId) .ExecuteDeleteAsync(ct); - var rows = new List(parsed.Count); - foreach (var kv in parsed.OrderBy(x => x.Key, StringComparer.Ordinal)) + var rows = new List(truth.Count); + foreach (var kv in truth.OrderBy(x => x.Key, StringComparer.Ordinal)) { var match = grouped[kv.Key]; if (match.PrimaryGroup == null) @@ -899,7 +935,7 @@ await db.LearnedBaselineTensorQuants TensorWeightSchemeId = tensorScheme.UniqueId, TensorGroupId = match.PrimaryGroup.UniqueId, TensorName = kv.Key, - FinalQuantType = kv.Value + FinalQuantType = kv.Value.FinalQuantType }); } @@ -909,6 +945,8 @@ await db.LearnedBaselineTensorQuants db.LearnedBaselineTensorQuants.AddRange(rows); await db.SaveChangesAsync(ct); + await WriteLearningDiagnosticArtifactAsync(quant.BaseQuant, tensorScheme, truth, grouped, ggufMetadata.TensorNames, ambiguous, unresolved); + AnsiConsole.MarkupLine( $"[green]Learned baseline tensor mapping persisted:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{quant.BaseQuant.Names[0]}[/]."); } @@ -943,6 +981,181 @@ private Dictionary ParseQuantizeLogForTensorTypes(string logPath return byTensor; } + private Dictionary BuildTruthMapWithVerification( + IReadOnlyDictionary logTruth, + IReadOnlyDictionary ggufTruth, + string baselineName) + { + var allNames = logTruth.Keys + .Concat(ggufTruth.Keys) + .Distinct(StringComparer.Ordinal) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + var result = new Dictionary(StringComparer.Ordinal); + var hardMismatches = new List(); + var softMismatches = new List(); + var logOnly = new List(); + + foreach (var name in allNames) + { + bool inLog = logTruth.TryGetValue(name, out var logType); + bool inGguf = ggufTruth.TryGetValue(name, out var ggufType); + + if (inLog && inGguf) + { + if (string.Equals(logType, ggufType, StringComparison.OrdinalIgnoreCase)) + { + result[name] = new LearnedTensorTruth(name, ggufType!, LearningSource.Both); + } + else + { + // GGUF is source-of-truth for persisted mapping. + result[name] = new LearnedTensorTruth(name, ggufType!, LearningSource.BothWithMismatch); + + if (IsHighSeverityMismatch(logType!, ggufType!)) + hardMismatches.Add($"{name}: log={logType} gguf={ggufType}"); + else + softMismatches.Add($"{name}: log={logType} gguf={ggufType}"); + } + } + else if (inGguf) + { + result[name] = new LearnedTensorTruth(name, ggufType!, LearningSource.GgufOnly); + } + else if (inLog) + { + logOnly.Add($"{name}:{logType}"); + } + } + + if (hardMismatches.Count > 0) + { + throw new InvalidOperationException( + $"Baseline '{baselineName}' had {hardMismatches.Count} high-severity GGUF/log truth mismatches. " + + $"Examples: {string.Join(" | ", hardMismatches.Take(8))}"); + } + + if (softMismatches.Count > 0) + { + AnsiConsole.MarkupLine( + $"[yellow]WARNING:[/] Baseline [yellow]{baselineName}[/] had {softMismatches.Count} GGUF/log mismatches; GGUF truth was used."); + AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", softMismatches.Take(6)))}[/]"); + } + + if (logOnly.Count > 0) + { + throw new InvalidOperationException( + $"Baseline '{baselineName}' produced {logOnly.Count} log-only tensor mappings with no GGUF truth. " + + $"Examples: {string.Join(" | ", logOnly.Take(8))}"); + } + + return result; + } + + private static bool IsHighSeverityMismatch(string logType, string ggufType) + { + bool logHighPrecision = logType is "F32" or "F16" or "BF16"; + bool ggufHighPrecision = ggufType is "F32" or "F16" or "BF16"; + return logHighPrecision != ggufHighPrecision; + } + + private async Task WriteLearningDiagnosticArtifactAsync( + BaselineQuants baseline, + TensorWeightScheme scheme, + IReadOnlyDictionary truthByTensor, + IReadOnlyDictionary grouped, + IReadOnlyCollection allTensorNamesInModel, + IReadOnlyCollection> ambiguous, + IReadOnlyCollection unresolved) + { + var summaries = new List(); + var severeCoverageIssues = new List(); + + foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) + { + var expected = allTensorNamesInModel + .Where(x => group.Tensors.Any(p => Regex.IsMatch(x, $"^{p}$"))) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + var learned = truthByTensor + .Where(x => grouped.TryGetValue(x.Key, out var g) && g.PrimaryGroup?.UniqueId == group.UniqueId) + .Select(x => x.Key) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + var unmatched = expected.Except(learned, StringComparer.Ordinal).Take(20).ToList(); + var unexpected = learned.Except(expected, StringComparer.Ordinal).Take(20).ToList(); + + var distribution = truthByTensor + .Where(x => learned.Contains(x.Key, StringComparer.Ordinal)) + .GroupBy(x => x.Value.FinalQuantType) + .OrderByDescending(g => g.Count()) + .ToDictionary(g => g.Key, g => g.Count()); + + var sourceCounts = truthByTensor + .Where(x => learned.Contains(x.Key, StringComparer.Ordinal)) + .GroupBy(x => x.Value.Source.ToString()) + .ToDictionary(g => g.Key, g => g.Count()); + + summaries.Add(new + { + Group = group.Name, + ExpectedTensorCount = expected.Count, + LearnedTensorCount = learned.Count, + UnmatchedExpected = unmatched, + UnexpectedLearned = unexpected, + Ambiguous = ambiguous.Where(x => x.Value.MatchedGroups.Contains(group.Name)).Select(x => x.Key).Take(20).ToList(), + QuantDistribution = distribution, + SourceDistribution = sourceCounts + }); + + var distShort = distribution.Count == 0 + ? "none" + : string.Join(", ", distribution.Select(kv => $"{kv.Key}:{kv.Value}")); + + var srcShort = sourceCounts.Count == 0 + ? "none" + : string.Join(", ", sourceCounts.Select(kv => $"{kv.Key}:{kv.Value}")); + + AnsiConsole.MarkupLine( + $"[grey][learn:{baseline.Names[0]}:{group.Name}] expected={expected.Count} learned={learned.Count} unmatched={unmatched.Count} ambiguous={ambiguous.Count(x => x.Value.MatchedGroups.Contains(group.Name))} dist=[{Markup.Escape(distShort)}] src=[{Markup.Escape(srcShort)}][/]"); + + if (expected.Count > 0 && unmatched.Count > 0) + { + severeCoverageIssues.Add( + $"{group.Name}: expected={expected.Count} learned={learned.Count} unmatched={unmatched.Count}"); + } + } + + var artifact = new + { + Baseline = baseline.Names[0], + Scheme = scheme.Names[0], + TotalTruthTensors = truthByTensor.Count, + UnresolvedTensorCount = unresolved.Count, + AmbiguousTensorCount = ambiguous.Count, + GeneratedUtc = DateTime.UtcNow, + Groups = summaries + }; + + string debugDir = Path.Combine(_benchDir, "_learning_debug"); + Directory.CreateDirectory(debugDir); + string path = Path.Combine(debugDir, $"{baseline.Names[0]}_{scheme.Names[0]}_learned_map.json"); + await File.WriteAllTextAsync(path, JsonSerializer.Serialize(artifact, new JsonSerializerOptions { WriteIndented = true })); + + AnsiConsole.MarkupLine( + $"[grey]Learned mapping diagnostic written:[/] {Markup.Escape(path)}"); + + if (severeCoverageIssues.Count > 0) + { + throw new InvalidOperationException( + $"Baseline learning coverage was incomplete for {severeCoverageIssues.Count} group(s): " + + string.Join(" | ", severeCoverageIssues.Take(8))); + } + } + private Dictionary AssignGroups(IEnumerable tensorNames) { var dict = new Dictionary(StringComparer.Ordinal); @@ -980,7 +1193,9 @@ private Dictionary AssignGroups(IEnumerable baseQuant.Names.Contains(sn, StringComparer.OrdinalIgnoreCase))); } - private List BuildRequestedTensorOverrides(HybridQuant quant) + private List BuildRequestedTensorOverrides( + HybridQuant quant, + IReadOnlyCollection sourceTensorNames) { var result = new List(); @@ -1010,6 +1225,24 @@ private List BuildRequestedTensorOverrides(HybridQuant "Run with --relearn-baseline-mappings to regenerate."); } + var expectedForGroup = sourceTensorNames + .Where(x => hybrid.TGroup.Tensors.Any(p => Regex.IsMatch(x, $"^{p}$"))) + .ToHashSet(StringComparer.Ordinal); + + var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); + var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); + var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); + + if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) + { + var missingText = missingExpected.Count == 0 ? "none" : string.Join(", ", missingExpected.Take(15)); + var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); + + throw new InvalidOperationException( + $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + scheme '{hybrid.TensorType.Names[0]}'. " + + $"Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); + } + foreach (var kv in learned) { result.Add(new RequestedTensorOverride @@ -1054,14 +1287,12 @@ private Dictionary TryLoadLearnedTensorMapping(TensorWeightSchem return rows.ToDictionary(x => x.TensorName, x => x.FinalQuantType, StringComparer.Ordinal); } - private async Task> ResolveConcreteTensorOverridesAsync( - string inputGgufPath, - string outputFilePath, + private List ResolveConcreteTensorOverrides( + IReadOnlyCollection allTensorNames, List requestedOverrides) { if (requestedOverrides.Count == 0) return new List(); - var allTensorNames = await ReadTensorNamesFromGgufAsync(inputGgufPath, outputFilePath); var nameSet = allTensorNames.ToHashSet(StringComparer.Ordinal); var missing = requestedOverrides @@ -1100,7 +1331,7 @@ private async Task> ResolveConcreteTensorOverridesA .ToList(); } - private async Task> ReadTensorNamesFromGgufAsync(string ggufPath, string outputFilePath) + private async Task ReadTensorMetadataFromGgufAsync(string ggufPath, string outputFilePath) { string workingDir = Path.GetDirectoryName(outputFilePath)!; string unique = Guid.NewGuid().ToString("N"); @@ -1122,13 +1353,24 @@ with open(payload_path, "r", encoding="utf-8") as f: output_path = payload["output_path"] + def resolve_type_name(t): + for attr in ["type_name", "tensor_type", "type"]: + v = getattr(t, attr, None) + if v is None: + continue + if hasattr(v, "name"): + return str(v.name) + return str(v) + return "UNKNOWN" + try: import gguf reader = gguf.GGUFReader(payload["gguf_path"]) tensor_names = [t.name for t in reader.tensors] - result = {"TensorNames": tensor_names} + tensor_types = {t.name: resolve_type_name(t) for t in reader.tensors} + result = {"TensorNames": tensor_names, "TensorTypes": tensor_types} except Exception as e: - result = {"Error": str(e), "TensorNames": []} + result = {"Error": str(e), "TensorNames": [], "TensorTypes": {}} with open(output_path, "w", encoding="utf-8") as f: json.dump(result, f, indent=2) @@ -1137,13 +1379,13 @@ with open(output_path, "w", encoding="utf-8") as f: await File.WriteAllTextAsync(scriptPath, py); await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); - var result = JsonSerializer.Deserialize(await File.ReadAllTextAsync(resultPath)); + var result = JsonSerializer.Deserialize(await File.ReadAllTextAsync(resultPath)); if (result == null) throw new InvalidOperationException("Failed to parse GGUF tensor list result."); if (!string.IsNullOrWhiteSpace(result.Error)) throw new InvalidOperationException($"Failed to read GGUF tensor names: {result.Error}"); - return result.TensorNames; + return result; } finally { @@ -1200,10 +1442,21 @@ private sealed class TensorGroupingResult public List MatchedGroups { get; set; } = new(); } - private sealed class TensorNameReadResult + private sealed class GgufTensorReadResult { public string? Error { get; set; } public List TensorNames { get; set; } = new(); + public Dictionary TensorTypes { get; set; } = new(StringComparer.Ordinal); + } + + private sealed record LearnedTensorTruth(string TensorName, string FinalQuantType, LearningSource Source); + + private enum LearningSource + { + LogOnly = 1, + GgufOnly = 2, + Both = 3, + BothWithMismatch = 4 } // ---------------------------------------------------------------- From 098ea9d9ea30695cc3202ee9751f7d89d7fdbd0b Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 15 Apr 2026 19:38:09 -0400 Subject: [PATCH 054/258] Add native-source learned truth support and alias hardening --- MagicQuant/Commands/Evolution.cs | 2 + MagicQuant/Program.cs | 2 + MagicQuant/Services/QuantizationService.cs | 197 ++++++++++++++++++--- 3 files changed, 181 insertions(+), 20 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 06d5e0b..e29051d 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -100,6 +100,8 @@ await benchmarkService.RunAllBenchmarksAsync( saveLogits: true, domainsOverride: new[] { "general", "code", "math" }); + await quantizationService.LearnNativeSourceTruthAsync(bf16ModelGgufPath); + var compatibilityService = new ModelCompatibilityService(pyManager); await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index fdcbeaa..eb615f4 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -4,6 +4,7 @@ using MagicQuant.Commands; using MagicQuant.Helpers; using MagicQuant.Models; +using MagicQuant.Services; using Spectre.Console; using System.Collections.Immutable; using MQ.DB.Models; @@ -55,6 +56,7 @@ // strict startup integrity checks TensorWeightScheme.ValidateSmallestConfiguration(); BaselineQuants.ValidateIntegrityOrThrow(); + QuantizationService.ValidateQuantNameNormalizationOrThrow(); // 6. Mandatory Validation for non-init commands if (!commandInput.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index da5d02c..2b07e3a 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -77,6 +77,35 @@ public QuantizationService(BenchmarkService benchmarker) _cpuQuantLock = new SemaphoreSlim(_maxConcurrentQuantizations, _maxConcurrentQuantizations); } + public static void ValidateQuantNameNormalizationOrThrow() + { + var aliasExpectations = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["bf16"] = "BF16", + ["bfloat16"] = "BF16", + ["f16"] = "F16", + ["float16"] = "F16", + ["f32"] = "F32", + ["float32"] = "F32", + ["q6_k"] = "Q6_K", + ["q5_k"] = "Q5_K", + ["q8_0"] = "Q8_0", + ["iq4_xs"] = "IQ4_XS", + ["iq4_nl"] = "IQ4_NL" + }; + + var mismatches = aliasExpectations + .Where(x => !string.Equals(NormalizeQuantName(x.Key), x.Value, StringComparison.Ordinal)) + .Select(x => $"{x.Key}->{NormalizeQuantName(x.Key)} (expected {x.Value})") + .ToList(); + + if (mismatches.Count > 0) + { + throw new InvalidOperationException( + "Quant name alias normalization is misconfigured: " + string.Join(", ", mismatches)); + } + } + // ---------------------------------------------------------------- // Batch processing // ---------------------------------------------------------------- @@ -841,9 +870,108 @@ public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = defaul if (Directory.Exists(debugDir)) Directory.Delete(debugDir, recursive: true); + string nativeType = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; + string nativeBaseFile = Path.Combine(_ggufDir, $"{modelName}-{nativeType}.gguf"); + await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile); + await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile + ".success.json"); + await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile + ".convert.log"); + + string nativeBenchDir = Path.Combine(_benchDir, nativeType); + if (Directory.Exists(nativeBenchDir)) + Directory.Delete(nativeBenchDir, recursive: true); + AnsiConsole.MarkupLine("[yellow]Relearn requested:[/] baseline artifacts, benchmark caches, and learning diagnostics were invalidated."); } + public async Task LearnNativeSourceTruthAsync( + string nativeGgufPath, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(nativeGgufPath) || !File.Exists(nativeGgufPath)) + throw new FileNotFoundException($"Native GGUF path not found for learning: {nativeGgufPath}"); + + var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, nativeGgufPath); + var ggufTruth = metadata.TensorTypes + .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); + + var truth = BuildTruthMapWithVerification( + logTruth: new Dictionary(StringComparer.Ordinal), + ggufTruth: ggufTruth, + baselineName: "NATIVE"); + + var grouped = AssignGroups(truth.Keys); + var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); + var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).ToList(); + + await using var db = new MagicQuantContext(); + var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct) + ?? throw new InvalidOperationException("Could not persist native-source learning because AiModelHash row was missing."); + + var combo = await db.TensorCombos + .AsNoTracking() + .FirstOrDefaultAsync(x => x.BaseQuant == BaselineQuants.NativeSourceUniqueId && + x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && + x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && + x.MoeExperts == 0 && x.MoeRouter == 0, ct); + + if (combo == null) + throw new InvalidOperationException("Native-source benchmark TensorCombo is missing; benchmark base model first."); + + var benchmarkId = await db.AiBenchmarks + .Where(x => x.AiModelHashId == model.Id && x.TensorComboId == combo.Id) + .OrderByDescending(x => x.Id) + .Select(x => (uint?)x.Id) + .FirstOrDefaultAsync(ct); + + if (!benchmarkId.HasValue) + throw new InvalidOperationException("Native-source benchmark row is missing; benchmark base model before native-source learning."); + + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == model.Id && + x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + x.TensorWeightSchemeId == TensorWeightScheme.BF16_F16.UniqueId) + .ExecuteDeleteAsync(ct); + + var rows = truth + .Where(x => grouped[x.Key].PrimaryGroup != null) + .Select(x => new LearnedBaselineTensorQuant + { + AiBenchmarkId = benchmarkId.Value, + AiModelHashId = model.Id, + BaselineQuantId = BaselineQuants.NativeSourceUniqueId, + TensorWeightSchemeId = TensorWeightScheme.BF16_F16.UniqueId, + TensorGroupId = grouped[x.Key].PrimaryGroup!.UniqueId, + TensorName = x.Key, + FinalQuantType = x.Value.FinalQuantType + }) + .ToList(); + + if (rows.Count == 0) + throw new InvalidOperationException("Native-source learning produced no persistable rows."); + + db.LearnedBaselineTensorQuants.AddRange(rows); + await db.SaveChangesAsync(ct); + + await WriteLearningDiagnosticArtifactAsync( + baselineName: "NATIVE", + schemeName: TensorWeightScheme.BF16_F16.Names[0], + truthByTensor: truth, + grouped: grouped, + allTensorNamesInModel: metadata.TensorNames, + ambiguous: ambiguous, + unresolved: unresolved); + + var sourcePrecision = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + var distribution = rows.GroupBy(x => x.FinalQuantType) + .OrderByDescending(g => g.Count()) + .Select(g => $"{g.Key}:{g.Count()}") + .ToList(); + + AnsiConsole.MarkupLine( + $"[green]Native-source learned truth:[/] precision={sourcePrecision}, tensors={rows.Count}, unresolved={unresolved.Count}, ambiguous={ambiguous.Count}, dist=[{Markup.Escape(string.Join(", ", distribution))}]"); + } + private static bool IsLearnableBaselineRun(HybridQuant quant) { return quant.Tensors.Count == 0 && @@ -945,7 +1073,14 @@ await db.LearnedBaselineTensorQuants db.LearnedBaselineTensorQuants.AddRange(rows); await db.SaveChangesAsync(ct); - await WriteLearningDiagnosticArtifactAsync(quant.BaseQuant, tensorScheme, truth, grouped, ggufMetadata.TensorNames, ambiguous, unresolved); + await WriteLearningDiagnosticArtifactAsync( + baselineName: quant.BaseQuant.Names[0], + schemeName: tensorScheme.Names[0], + truthByTensor: truth, + grouped: grouped, + allTensorNamesInModel: ggufMetadata.TensorNames, + ambiguous: ambiguous, + unresolved: unresolved); AnsiConsole.MarkupLine( $"[green]Learned baseline tensor mapping persisted:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{quant.BaseQuant.Names[0]}[/]."); @@ -1061,8 +1196,8 @@ private static bool IsHighSeverityMismatch(string logType, string ggufType) } private async Task WriteLearningDiagnosticArtifactAsync( - BaselineQuants baseline, - TensorWeightScheme scheme, + string baselineName, + string schemeName, IReadOnlyDictionary truthByTensor, IReadOnlyDictionary grouped, IReadOnlyCollection allTensorNamesInModel, @@ -1120,7 +1255,7 @@ private async Task WriteLearningDiagnosticArtifactAsync( : string.Join(", ", sourceCounts.Select(kv => $"{kv.Key}:{kv.Value}")); AnsiConsole.MarkupLine( - $"[grey][learn:{baseline.Names[0]}:{group.Name}] expected={expected.Count} learned={learned.Count} unmatched={unmatched.Count} ambiguous={ambiguous.Count(x => x.Value.MatchedGroups.Contains(group.Name))} dist=[{Markup.Escape(distShort)}] src=[{Markup.Escape(srcShort)}][/]"); + $"[grey][learn:{baselineName}:{group.Name}] expected={expected.Count} learned={learned.Count} unmatched={unmatched.Count} ambiguous={ambiguous.Count(x => x.Value.MatchedGroups.Contains(group.Name))} dist=[{Markup.Escape(distShort)}] src=[{Markup.Escape(srcShort)}][/]"); if (expected.Count > 0 && unmatched.Count > 0) { @@ -1131,8 +1266,8 @@ private async Task WriteLearningDiagnosticArtifactAsync( var artifact = new { - Baseline = baseline.Names[0], - Scheme = scheme.Names[0], + Baseline = baselineName, + Scheme = schemeName, TotalTruthTensors = truthByTensor.Count, UnresolvedTensorCount = unresolved.Count, AmbiguousTensorCount = ambiguous.Count, @@ -1142,7 +1277,7 @@ private async Task WriteLearningDiagnosticArtifactAsync( string debugDir = Path.Combine(_benchDir, "_learning_debug"); Directory.CreateDirectory(debugDir); - string path = Path.Combine(debugDir, $"{baseline.Names[0]}_{scheme.Names[0]}_learned_map.json"); + string path = Path.Combine(debugDir, $"{baselineName}_{schemeName}_learned_map.json"); await File.WriteAllTextAsync(path, JsonSerializer.Serialize(artifact, new JsonSerializerOptions { WriteIndented = true })); AnsiConsole.MarkupLine( @@ -1268,14 +1403,24 @@ private Dictionary TryLoadLearnedTensorMapping(TensorWeightSchem if (model == null) return new Dictionary(StringComparer.Ordinal); - var baseline = BaselineQuants.All.FirstOrDefault(x => x.DefaultTensorScheme?.UniqueId == sourceScheme.UniqueId); - if (baseline == null) - return new Dictionary(StringComparer.Ordinal); + byte baselineId; + if (sourceScheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + { + baselineId = BaselineQuants.NativeSourceUniqueId; + } + else + { + var baseline = BaselineQuants.All.FirstOrDefault(x => x.DefaultTensorScheme?.UniqueId == sourceScheme.UniqueId); + if (baseline == null) + return new Dictionary(StringComparer.Ordinal); + + baselineId = baseline.UniqueId; + } var rows = db.LearnedBaselineTensorQuants .AsNoTracking() .Where(x => x.AiModelHashId == model.Id) - .Where(x => x.BaselineQuantId == baseline.UniqueId) + .Where(x => x.BaselineQuantId == baselineId) .Where(x => x.TensorWeightSchemeId == sourceScheme.UniqueId) .Where(x => x.TensorGroupId == targetGroup.UniqueId) .OrderBy(x => x.TensorName) @@ -1405,15 +1550,27 @@ with open(output_path, "w", encoding="utf-8") as f: private static string NormalizeQuantName(string value) { - var normalized = value.Trim().ToUpperInvariant(); - return normalized.Replace("Q5_K", "Q5_K") - .Replace("Q6_K", "Q6_K") - .Replace("Q8_0", "Q8_0") - .Replace("IQ4_XS", "IQ4_XS") - .Replace("IQ4_NL", "IQ4_NL") - .Replace("BF16", "BF16") - .Replace("F16", "F16") - .Replace("F32", "F32"); + var normalized = value + .Trim() + .Replace("-", "_") + .Replace(" ", string.Empty) + .ToUpperInvariant(); + + return normalized switch + { + "BF16" => "BF16", + "BFLOAT16" => "BF16", + "F16" => "F16", + "FLOAT16" => "F16", + "F32" => "F32", + "FLOAT32" => "F32", + "Q5_K" => "Q5_K", + "Q6_K" => "Q6_K", + "Q8_0" => "Q8_0", + "IQ4_XS" => "IQ4_XS", + "IQ4_NL" => "IQ4_NL", + _ => normalized + }; } private sealed class QuantizationExecutionReport From 4ee0f218485178bcc96c1b68943a42839a8f0439 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 15 Apr 2026 19:53:50 -0400 Subject: [PATCH 055/258] fix migration --- ...34821_AddLearnedBaselineTables.Designer.cs | 434 ++++++++++++++++++ ...0260415234821_AddLearnedBaselineTables.cs} | 9 +- .../MagicQuantContextModelSnapshot.cs | 94 ++++ 3 files changed, 534 insertions(+), 3 deletions(-) create mode 100644 MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.Designer.cs rename MQ.DB/Migrations/{20260415220000_AddLearnedBaselineTables.cs => 20260415234821_AddLearnedBaselineTables.cs} (97%) diff --git a/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.Designer.cs b/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.Designer.cs new file mode 100644 index 0000000..be185a5 --- /dev/null +++ b/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.Designer.cs @@ -0,0 +1,434 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260415234821_AddLearnedBaselineTables")] + partial class AddLearnedBaselineTables + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("BaselineName") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeId") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260415220000_AddLearnedBaselineTables.cs b/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.cs similarity index 97% rename from MQ.DB/Migrations/20260415220000_AddLearnedBaselineTables.cs rename to MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.cs index fafd30b..0d9c393 100644 --- a/MQ.DB/Migrations/20260415220000_AddLearnedBaselineTables.cs +++ b/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.cs @@ -1,11 +1,13 @@ -using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace MQ.DB.Migrations { + /// public partial class AddLearnedBaselineTables : Migration { + /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( @@ -88,13 +90,14 @@ protected override void Up(MigrationBuilder migrationBuilder) unique: true); } + /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( - name: "LearnedBaselineTensorQuants"); + name: "BaselineQuantDefinitions"); migrationBuilder.DropTable( - name: "BaselineQuantDefinitions"); + name: "LearnedBaselineTensorQuants"); } } } diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index ee1f5a2..9cec12c 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -65,6 +65,38 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("AiModelHashes"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("BaselineName") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeId") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => { b.Property("Id") @@ -146,6 +178,49 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("CategoryBenchmark"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiBenchmarkId") + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => { b.Property("Id") @@ -301,6 +376,25 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("AiBenchmark"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => { b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") From 9f027ef487486d94674be58704d5d8b3a104454b Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 16 Apr 2026 12:08:37 -0400 Subject: [PATCH 056/258] update to moe indicators and updat to quant services due to bad markup causing explosions --- MagicQuant/Config.cs | 20 +++++++++++++-- MagicQuant/Services/QuantizationService.cs | 30 ++++++++++++++-------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 20a4535..1495c30 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -48,22 +48,38 @@ public static class Config public static readonly List MoeIndicatorTensors = new() { + // Older / generic expert patterns you already had "blk.*.ffn_up_expert_0.weight", "blk.*.ffn_gate_expert_0.weight", "blk.*.ffn_down_expert_0.weight", - // Qwen3-MOE / Unsloth / modern MOE + // Older modern-MoE / GGUF-ish patterns "blk.*.ffn_up_exps.weight", "blk.*.ffn_gate_exps.weight", "blk.*.ffn_down_exps.weight", "blk.*.ffn_gate_inp.weight", - // router variants + // Generic router variants "router.weight", "gate.weight", "blk.*.router.*", "blk.*.gate_proj.*", "blk.*.gate_inp.*", + + // Qwen3.5 native HF MoE + "model.language_model.layers.*.mlp.experts.gate_up_proj", + "model.language_model.layers.*.mlp.experts.down_proj", + "model.language_model.layers.*.mlp.gate.weight", + "model.language_model.layers.*.mlp.shared_expert.gate_proj.weight", + "model.language_model.layers.*.mlp.shared_expert.up_proj.weight", + "model.language_model.layers.*.mlp.shared_expert.down_proj.weight", + + // Gemma 4 MoE + "model.language_model.layers.*.experts.gate_up_proj", + "model.language_model.layers.*.experts.down_proj", + "model.language_model.layers.*.router.proj.weight", + "model.language_model.layers.*.router.per_expert_scale", + "model.language_model.layers.*.router.scale", }; diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 2b07e3a..04a5854 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -587,7 +587,7 @@ public async Task EnsureBaseModelAsync(bool deleteProcess = false) string benchPath = Path.Combine(_benchDir, typeStr); string logitsDir = Path.Combine(benchPath, "logits"); - AnsiConsole.MarkupLine($"[bold yellow]Benchmarking Base {typeStr} (Saving Logits)...[/]"); + AnsiConsole.MarkupLine($"[bold yellow]Benchmarking Base {Markup.Escape(typeStr)} (Saving Logits)...[/]"); var baseModelQuant = new HybridQuant { @@ -647,7 +647,7 @@ public async Task EnsureBaseModelFileAsync(bool deleteProcess = false) if (!File.Exists(outputPath) || !File.Exists(successFile)) { - AnsiConsole.MarkupLine($"[bold cyan]Converting to {typeStr}...[/]"); + AnsiConsole.MarkupLine($"[bold cyan]Converting to {Markup.Escape(typeStr)}...[/]"); await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); @@ -969,7 +969,7 @@ await WriteLearningDiagnosticArtifactAsync( .ToList(); AnsiConsole.MarkupLine( - $"[green]Native-source learned truth:[/] precision={sourcePrecision}, tensors={rows.Count}, unresolved={unresolved.Count}, ambiguous={ambiguous.Count}, dist=[{Markup.Escape(string.Join(", ", distribution))}]"); + $"[green]Native-source learned truth:[/] precision={Markup.Escape(sourcePrecision)}, tensors={rows.Count}, unresolved={unresolved.Count}, ambiguous={ambiguous.Count}, dist={Markup.Escape($"[{string.Join(", ", distribution)}]")}"); } private static bool IsLearnableBaselineRun(HybridQuant quant) @@ -997,7 +997,7 @@ private async Task LearnAndPersistBaselineTensorMapAsync( if (parsed.Count == 0 && ggufTruth.Count == 0) { AnsiConsole.MarkupLine( - $"[red]WARNING:[/] learned mapping parse returned no tensors from logs and GGUF for baseline [yellow]{quant.BaseQuant.Names[0]}[/]."); + $"[red]WARNING:[/] learned mapping parse returned no tensors from logs and GGUF for baseline [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); return; } @@ -1010,15 +1010,15 @@ private async Task LearnAndPersistBaselineTensorMapAsync( if (ambiguous.Count > 0) { AnsiConsole.MarkupLine( - $"[red]WARNING:[/] {ambiguous.Count} tensor(s) matched multiple groups while learning baseline {quant.BaseQuant.Names[0]}."); - AnsiConsole.MarkupLine($"[grey]Example: {Markup.Escape(ambiguous[0].Key)} => {string.Join(", ", ambiguous[0].Value.MatchedGroups)}[/]"); + $"[red]WARNING:[/] {ambiguous.Count} tensor(s) matched multiple groups while learning baseline {Markup.Escape(quant.BaseQuant.Names[0])}."); + AnsiConsole.MarkupLine($"[grey]Example: {Markup.Escape(ambiguous[0].Key)} => {Markup.Escape(string.Join(", ", ambiguous[0].Value.MatchedGroups))}[/]"); } var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).ToList(); if (unresolved.Count > 0) { AnsiConsole.MarkupLine( - $"[yellow]WARNING:[/] {unresolved.Count} tensor(s) had no tensor-group match while learning baseline {quant.BaseQuant.Names[0]}."); + $"[yellow]WARNING:[/] {unresolved.Count} tensor(s) had no tensor-group match while learning baseline {Markup.Escape(quant.BaseQuant.Names[0])}."); } await using var db = new MagicQuantContext(); @@ -1083,7 +1083,7 @@ await WriteLearningDiagnosticArtifactAsync( unresolved: unresolved); AnsiConsole.MarkupLine( - $"[green]Learned baseline tensor mapping persisted:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{quant.BaseQuant.Names[0]}[/]."); + $"[green]Learned baseline tensor mapping persisted:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); } private Dictionary ParseQuantizeLogForTensorTypes(string logPath) @@ -1174,7 +1174,7 @@ private Dictionary BuildTruthMapWithVerification( if (softMismatches.Count > 0) { AnsiConsole.MarkupLine( - $"[yellow]WARNING:[/] Baseline [yellow]{baselineName}[/] had {softMismatches.Count} GGUF/log mismatches; GGUF truth was used."); + $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {softMismatches.Count} GGUF/log mismatches; GGUF truth was used."); AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", softMismatches.Take(6)))}[/]"); } @@ -1254,8 +1254,16 @@ private async Task WriteLearningDiagnosticArtifactAsync( ? "none" : string.Join(", ", sourceCounts.Select(kv => $"{kv.Key}:{kv.Value}")); + var label = $"[learn:{baselineName}:{group.Name}]"; + AnsiConsole.MarkupLine( - $"[grey][learn:{baselineName}:{group.Name}] expected={expected.Count} learned={learned.Count} unmatched={unmatched.Count} ambiguous={ambiguous.Count(x => x.Value.MatchedGroups.Contains(group.Name))} dist=[{Markup.Escape(distShort)}] src=[{Markup.Escape(srcShort)}][/]"); + $"[grey]{Markup.Escape(label)} " + + $"expected={expected.Count} " + + $"learned={learned.Count} " + + $"unmatched={unmatched.Count} " + + $"ambiguous={ambiguous.Count(x => x.Value.MatchedGroups.Contains(group.Name))} " + + $"dist={Markup.Escape($"[{distShort}]")} " + + $"src={Markup.Escape($"[{srcShort}]")}[/]"); if (expected.Count > 0 && unmatched.Count > 0) { @@ -1796,4 +1804,4 @@ void HandleLine(string? line, bool isError) StdErr = stderrBuilder.ToString() }; } -} +} \ No newline at end of file From 73292c78b8b4d2df57a4abd38e6af9050e0a3e4d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 16 Apr 2026 12:15:20 -0400 Subject: [PATCH 057/258] updated tensor groups --- MQ.DB/Models/TensorGroup.cs | 197 ++++++++++++++++++++---------------- 1 file changed, 111 insertions(+), 86 deletions(-) diff --git a/MQ.DB/Models/TensorGroup.cs b/MQ.DB/Models/TensorGroup.cs index 585113d..29216db 100644 --- a/MQ.DB/Models/TensorGroup.cs +++ b/MQ.DB/Models/TensorGroup.cs @@ -5,7 +5,7 @@ namespace MQ.DB.Models; public class TensorGroupInfo -{ +{ public TensorGroup Group { get; set; } } @@ -19,16 +19,16 @@ public record TensorGroup(byte UniqueId, string Name, ImmutableArray Ten /// public char ShortCode => Name switch { - "embeddings" => 'E', - "lm_head" => 'H', - "attn_q" => 'Q', - "attn_kv" => 'K', + "embeddings" => 'E', + "lm_head" => 'H', + "attn_q" => 'Q', + "attn_kv" => 'K', "attn_output" => 'O', "ffn_up_gate" => 'U', - "ffn_down" => 'D', + "ffn_down" => 'D', "moe_experts" => 'X', - "moe_router" => 'R', - _ => '?' + "moe_router" => 'R', + _ => '?' }; } @@ -38,147 +38,172 @@ public record TensorGroup(byte UniqueId, string Name, ImmutableArray Ten public static class TReg { public static readonly TensorGroup Embeddings = new(0, "embeddings", [ - "token_embd\\.weight", - "model\\.embed_tokens\\.weight", + "token_embd\\.weight", + "model\\.embed_tokens\\.weight", "embed_tokens\\.weight", - "tok_embeddings\\.weight", - "word_embeddings\\.weight", + "tok_embeddings\\.weight", + "word_embeddings\\.weight", "transformer\\.wte\\.weight", "wte\\.weight" ]); public static readonly TensorGroup LmHead = new(1, "lm_head", [ - "output\\.weight", - "lm_head\\.weight", + "output\\.weight", + "lm_head\\.weight", "final_logits_proj\\.weight", - "model\\.embed_out\\.weight", + "model\\.embed_out\\.weight", "lm_head\\.decoder\\.weight" ]); public static readonly TensorGroup AttnQ = new(2, "attn_q", [ // Matches blk.0.attn_q.weight "blk\\..*\\.attn_q\\.weight", - ".*q_proj.*weight", - ".*query\\.weight", - ".*self_attn\\.q_proj\\.weight", + ".*q_proj.*weight", + ".*query\\.weight", + ".*self_attn\\.q_proj\\.weight", ".*attention\\.self\\.query\\.weight", - ".*SelfAttention\\.q\\.weight", - ".*c_attn\\.weight", + ".*SelfAttention\\.q\\.weight", + ".*c_attn\\.weight", ".*query_key_value\\.weight" ]); public static readonly TensorGroup AttnKV = new(3, "attn_kv", [ - "blk\\..*\\.attn_k\\.weight", - "blk\\..*\\.attn_v\\.weight", - ".*k_proj.*weight", + "blk\\..*\\.attn_k\\.weight", + "blk\\..*\\.attn_v\\.weight", + ".*k_proj.*weight", ".*v_proj.*weight", - ".*key\\.weight", - ".*value\\.weight", - ".*self_attn\\.k_proj\\.weight", + ".*key\\.weight", + ".*value\\.weight", + ".*self_attn\\.k_proj\\.weight", ".*self_attn\\.v_proj\\.weight", - ".*attention\\.self\\.key\\.weight", - ".*attention\\.self\\.value\\.weight", + ".*attention\\.self\\.key\\.weight", + ".*attention\\.self\\.value\\.weight", ".*SelfAttention\\.k\\.weight", - ".*SelfAttention\\.v\\.weight", - ".*EncDecAttention\\.k\\.weight", + ".*SelfAttention\\.v\\.weight", + ".*EncDecAttention\\.k\\.weight", ".*EncDecAttention\\.v\\.weight" ]); public static readonly TensorGroup AttnOutput = new(4, "attn_output", [ - "blk\\..*\\.attn_output\\.weight", - ".*out_proj.*weight", - ".*o_proj.*weight", + "blk\\..*\\.attn_output\\.weight", + ".*out_proj.*weight", + ".*o_proj.*weight", ".*c_proj\\.weight", - ".*attention\\.output\\.dense\\.weight", + ".*attention\\.output\\.dense\\.weight", ".*self_attn\\.out_proj\\.weight", - ".*SelfAttention\\.o\\.weight", - ".*self_attention\\.dense\\.weight", + ".*SelfAttention\\.o\\.weight", + ".*self_attention\\.dense\\.weight", ".*attention\\.proj\\.weight" ]); public static readonly TensorGroup FfnUpGate = new(5, "ffn_up_gate", [ - // This is where ffn_gate belongs! - "blk\\..*\\.ffn_up\\.weight", - "blk\\..*\\.ffn_gate\\.weight", - + "blk\\..*\\.ffn_up\\.weight", + "blk\\..*\\.ffn_gate\\.weight", + ".*intermediate\\.dense\\.weight", - ".*c_fc\\.weight", - ".*fc1\\.weight", - ".*fc_in\\.weight", + ".*c_fc\\.weight", + ".*fc1\\.weight", + ".*fc_in\\.weight", ".*dense_h_to_4h\\.weight", - ".*wi\\.weight", - ".*wi_0\\.weight", - ".*wi_1\\.weight", + ".*wi\\.weight", + ".*wi_0\\.weight", + ".*wi_1\\.weight", ".*mlp\\.up_proj\\.weight", - ".*mlp\\.gate_proj\\.weight", - ".*DenseReluDense\\.wi_0\\.weight", + ".*mlp\\.gate_proj\\.weight", + ".*DenseReluDense\\.wi_0\\.weight", ".*DenseReluDense\\.wi_1\\.weight", - ".*experts.*wi_0\\.weight", - ".*experts.*wi_1\\.weight", + ".*experts.*wi_0\\.weight", + ".*experts.*wi_1\\.weight", "blk\\..*\\.ffn_up_exps\\.weight", - "blk\\..*\\.ffn_gate_exps\\.weight" + "blk\\..*\\.ffn_gate_exps\\.weight", + + // Qwen3.5 MoE / modern expert forms + ".*mlp\\.experts\\.gate_up_proj.*", + ".*mlp\\.shared_expert\\.gate_proj\\.weight", + ".*mlp\\.shared_expert\\.up_proj\\.weight", + + // Gemma 4 MoE + ".*layers\\..*\\.experts\\.gate_up_proj.*" ]); public static readonly TensorGroup FfnDown = new(6, "ffn_down", [ - "blk\\..*\\.ffn_down\\.weight", - ".*output\\.dense\\.weight", - ".*c_proj\\.weight", + "blk\\..*\\.ffn_down\\.weight", + ".*output\\.dense\\.weight", + ".*c_proj\\.weight", ".*fc2\\.weight", - ".*fc_out\\.weight", - ".*wo\\.weight", - ".*dense_4h_to_h\\.weight", + ".*fc_out\\.weight", + ".*wo\\.weight", + ".*dense_4h_to_h\\.weight", ".*mlp\\.down_proj\\.weight", - ".*DenseReluDense\\.wo\\.weight", - ".*experts.*wo\\.weight", - "blk\\..*\\.ffn_down_exps\\.weight" + ".*DenseReluDense\\.wo\\.weight", + ".*experts.*wo\\.weight", + "blk\\..*\\.ffn_down_exps\\.weight", + + // Qwen3.5 MoE / modern expert forms + ".*mlp\\.experts\\.down_proj.*", + ".*mlp\\.shared_expert\\.down_proj\\.weight", + + // Gemma 4 MoE + ".*layers\\..*\\.experts\\.down_proj.*" ]); public static readonly TensorGroup MoeExperts = new(7, "moe_experts", [ - "blk\\..*\\.ffn_.*expert.*", - "blk\\..*\\.ffn_.*exps.*", + "blk\\..*\\.ffn_.*expert.*", + "blk\\..*\\.ffn_.*exps.*", ".*experts?\\..*wi_0.*", - ".*experts?\\..*wi_1.*", - ".*experts?\\..*wo.*", + ".*experts?\\..*wi_1.*", + ".*experts?\\..*wo.*", ".*experts?\\..*fc1.*", - ".*experts?\\..*fc2.*", - ".*experts?\\..*dense_h_to_4h.*", - ".*experts?\\..*dense_4h_to_h.*" + ".*experts?\\..*fc2.*", + ".*experts?\\..*dense_h_to_4h.*", + ".*experts?\\..*dense_4h_to_h.*", + + // Qwen3.5 native HF MoE + ".*mlp\\.experts\\.gate_up_proj.*", + ".*mlp\\.experts\\.down_proj.*", + ".*mlp\\.shared_expert\\.gate_proj\\.weight", + ".*mlp\\.shared_expert\\.up_proj\\.weight", + ".*mlp\\.shared_expert\\.down_proj\\.weight", + + // Gemma 4 MoE + ".*layers\\..*\\.experts\\.gate_up_proj.*", + ".*layers\\..*\\.experts\\.down_proj.*" ]); public static readonly TensorGroup MoeRouter = new(8, "moe_router", [ - // Strict Router definitions - "router.*", - "gating.*", - "routing.*", - - // This was the culprit. - // We use Negative Lookbehind (? /// Provides a complete list of all registered tensor groups. /// - public static readonly ImmutableArray All = + public static readonly ImmutableArray All = [ - Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, + Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter ]; /// /// Look up a group by its string name (useful when parsing external configs). /// - public static TensorGroup? GetByName(string name) => + public static TensorGroup? GetByName(string name) => All.FirstOrDefault(g => g.Name.Equals(name, System.StringComparison.OrdinalIgnoreCase)); } \ No newline at end of file From 5b06acbeba73a248d4cdb335017034056dac0c1e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 16 Apr 2026 12:49:35 -0400 Subject: [PATCH 058/258] New Guid ID system --- MQ.DB/Models/BaselineQuants.cs | 4 ++-- MQ.DB/Models/DbModels/AiBenchmark.cs | 14 ++++++++---- MQ.DB/Models/DbModels/BenchmarkRun.cs | 6 ++--- .../DbModels/LearnedBaselineTensorQuant.cs | 4 ++-- MQ.DB/Models/DbModels/QuantizationRun.cs | 6 ++--- MQ.DB/Models/DbModels/TensorCombo.cs | 5 ++++- MQ.DB/Models/HybridQuant.cs | 2 +- MQ.DB/Models/TensorWeightScheme.cs | 8 +++---- MagicQuant/Helpers/ComboLogic.cs | 2 +- MagicQuant/Helpers/RuntimeSearchSpace.cs | 6 ++--- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 2 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 2 +- MagicQuant/Services/BenchmarkService.cs | 6 ++--- .../Services/IsolationOptimizationService.cs | 4 ++-- .../Services/ModelCompatibilityService.cs | 6 ++--- MagicQuant/Services/QuantizationService.cs | 22 +++++++++---------- 16 files changed, 54 insertions(+), 45 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index ae95230..3d512d8 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -80,14 +80,14 @@ public static void ValidateIntegrityOrThrow() if (duplicateDefaultSchemeIds.Count > 0) { var duplicateNames = duplicateDefaultSchemeIds - .Select(id => TensorWeightScheme.All.First(s => s.UniqueId == id).Names[0]); + .Select(id => TensorWeightScheme.All_Allowed_Hybrid_Quants.First(s => s.UniqueId == id).Names[0]); throw new InvalidOperationException( "DefaultTensorScheme must be unique across BaselineQuants entries. Duplicates: " + string.Join(", ", duplicateNames)); } - var schemesMissingBaseline = TensorWeightScheme.All + var schemesMissingBaseline = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) // Some schemes can be experimental and intentionally not promoted to baseline. diff --git a/MQ.DB/Models/DbModels/AiBenchmark.cs b/MQ.DB/Models/DbModels/AiBenchmark.cs index ab51675..a937076 100644 --- a/MQ.DB/Models/DbModels/AiBenchmark.cs +++ b/MQ.DB/Models/DbModels/AiBenchmark.cs @@ -13,7 +13,7 @@ public enum BenchmarkCategory public class AiBenchmark : ISQLiteEntity { - public uint Id { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); /// /// n-N gpu layers @@ -30,7 +30,7 @@ public class AiBenchmark : ISQLiteEntity /// /// foreign key /// - public uint TensorComboId { get; set; } + public Guid TensorComboId { get; set; } public TensorCombo TensorCombo { get; set; } = default!; @@ -46,6 +46,9 @@ public class AiBenchmark : ISQLiteEntity public void Configure(EntityTypeBuilder builder) { builder.HasKey(x => x.Id); + + builder.Property(x => x.Id) + .ValueGeneratedNever(); builder.HasIndex(x => new { x.AiModelHashId, x.TensorComboId }) .IsUnique(); @@ -69,12 +72,12 @@ public void Configure(EntityTypeBuilder builder) public class CategoryBenchmark : ISQLiteEntity { - public uint Id { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); /// /// foreign key to AiBenchmark /// - public uint AiBenchmarkId { get; set; } + public Guid AiBenchmarkId { get; set; } public AiBenchmark AiBenchmark { get; set; } = default!; @@ -90,6 +93,9 @@ public class CategoryBenchmark : ISQLiteEntity public void Configure(EntityTypeBuilder builder) { builder.HasKey(x => x.Id); + + builder.Property(x => x.Id) + .ValueGeneratedNever(); builder.HasIndex(x => x.AiBenchmarkId); diff --git a/MQ.DB/Models/DbModels/BenchmarkRun.cs b/MQ.DB/Models/DbModels/BenchmarkRun.cs index 3f8ac73..4550eae 100644 --- a/MQ.DB/Models/DbModels/BenchmarkRun.cs +++ b/MQ.DB/Models/DbModels/BenchmarkRun.cs @@ -11,16 +11,16 @@ public class BenchmarkRun : ISQLiteEntity public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; - public uint TensorComboId { get; set; } + public Guid TensorComboId { get; set; } public TensorCombo TensorCombo { get; set; } = default!; - public uint AiBenchmarkId { get; set; } + public Guid AiBenchmarkId { get; set; } public AiBenchmark AiBenchmark { get; set; } = default!; /// /// Nullable until the CategoryBenchmark row is created/persisted. /// - public uint? CategoryBenchmarkId { get; set; } + public Guid? CategoryBenchmarkId { get; set; } public CategoryBenchmark? CategoryBenchmark { get; set; } /// diff --git a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs index e013d9b..69d6b8c 100644 --- a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs +++ b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs @@ -6,9 +6,9 @@ namespace MQ.DB.Models.DbModels; public class LearnedBaselineTensorQuant : ISQLiteEntity { - public ulong Id { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); - public uint AiBenchmarkId { get; set; } + public Guid AiBenchmarkId { get; set; } public AiBenchmark AiBenchmark { get; set; } = default!; public uint AiModelHashId { get; set; } diff --git a/MQ.DB/Models/DbModels/QuantizationRun.cs b/MQ.DB/Models/DbModels/QuantizationRun.cs index 0ff6d06..eec8235 100644 --- a/MQ.DB/Models/DbModels/QuantizationRun.cs +++ b/MQ.DB/Models/DbModels/QuantizationRun.cs @@ -6,18 +6,18 @@ namespace MQ.DB.Models.DbModels; public class QuantizationRun : ISQLiteEntity { - public Guid Id { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; - public uint TensorComboId { get; set; } + public Guid TensorComboId { get; set; } public TensorCombo TensorCombo { get; set; } = default!; /// /// Nullable because a quantization can fail before a benchmark row exists. /// - public uint? AiBenchmarkId { get; set; } + public Guid? AiBenchmarkId { get; set; } public AiBenchmark? AiBenchmark { get; set; } public DateTime StartedUtc { get; set; } diff --git a/MQ.DB/Models/DbModels/TensorCombo.cs b/MQ.DB/Models/DbModels/TensorCombo.cs index 25f550c..f4eb937 100644 --- a/MQ.DB/Models/DbModels/TensorCombo.cs +++ b/MQ.DB/Models/DbModels/TensorCombo.cs @@ -24,7 +24,7 @@ public TensorCombo(TensorConfig c) MoeRouter = c.MoeRouter; } - public uint Id { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); public readonly byte BaseQuant; public readonly byte Embeddings; public readonly byte LmHead; @@ -40,6 +40,9 @@ public void Configure(EntityTypeBuilder builder) { builder.HasKey(x => x.Id); + builder.Property(x => x.Id) + .ValueGeneratedNever(); + builder.HasIndex(x => new { x.BaseQuant, diff --git a/MQ.DB/Models/HybridQuant.cs b/MQ.DB/Models/HybridQuant.cs index 72264ed..dbae7be 100644 --- a/MQ.DB/Models/HybridQuant.cs +++ b/MQ.DB/Models/HybridQuant.cs @@ -27,7 +27,7 @@ private void AddIfNotNull(TensorGroup group, byte schemeId) if (schemeId == TensorWeightScheme.NULL.UniqueId) return; - var scheme = TensorWeightScheme.All.First(g => g.UniqueId == schemeId); + var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.First(g => g.UniqueId == schemeId); Tensors.Add(new HybridTensor { diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 17f4384..0ef3d6a 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -47,13 +47,13 @@ public void ResetRuntimeBans() public static void ResetAllRuntimeBans() { - foreach (var scheme in All) + foreach (var scheme in All_Allowed_Hybrid_Quants) scheme.ResetRuntimeBans(); } public static void ValidateSmallestConfiguration() { - var nonImatrixSmallest = All + var nonImatrixSmallest = All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != NULL.UniqueId) .Where(x => x.UniqueId != BF16_F16.UniqueId) .Where(x => !x.RequiresImatrix) @@ -75,7 +75,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() { ValidateSmallestConfiguration(); - return All + return All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != NULL.UniqueId) .Where(x => x.UniqueId != BF16_F16.UniqueId) .Where(x => !x.RequiresImatrix) @@ -221,7 +221,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() ); */ - public static readonly ImmutableArray All = + public static readonly ImmutableArray All_Allowed_Hybrid_Quants = [ NULL, BF16_F16, diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 4dd25ef..9b5178f 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -14,7 +14,7 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants { bool baseRequiresImatrix = baseQuant.RequiresImatrix; - var schemesForBase = TensorWeightScheme.All + var schemesForBase = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(s => baseRequiresImatrix || !s.RequiresImatrix) .ToImmutableArray(); diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index ec60628..d5eb449 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -35,7 +35,7 @@ public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme schem public static void BanAllExplicitTensorSchemesForGroup(TensorGroup group) { - foreach (var scheme in TensorWeightScheme.All.Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId && x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId)) + foreach (var scheme in TensorWeightScheme.All_Allowed_Hybrid_Quants.Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId && x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId)) BanSchemeForGroup(group, scheme); } @@ -44,7 +44,7 @@ public static IReadOnlyList GetRuntimeExplicitBansForGroup(T if (!ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set)) return Array.Empty(); - return TensorWeightScheme.All.Where(x => set.Contains(x.UniqueId)).OrderBy(x => x.UniqueId).ToList(); + return TensorWeightScheme.All_Allowed_Hybrid_Quants.Where(x => set.Contains(x.UniqueId)).OrderBy(x => x.UniqueId).ToList(); } public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeightScheme scheme) @@ -54,7 +54,7 @@ public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeight public static bool IsGroupExplicitQuantBanned(TensorGroup group) { - var explicitSchemes = TensorWeightScheme.All + var explicitSchemes = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) .ToList(); diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index 2d2e5b3..f540c24 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -65,7 +65,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc if (id == TensorWeightScheme.NULL.UniqueId) return "NULL"; - var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); + var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.FirstOrDefault(x => x.UniqueId == id); return scheme?.Names[0] ?? $"Unknown({id})"; }).ToList(); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 841aacc..6a2cbb2 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -125,7 +125,7 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var carrier = BaselineQuants.Q8_0; var smallest = TensorWeightScheme.GetSmallestNonImatrix(); - var schemes = TensorWeightScheme.All + var schemes = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) .OrderBy(x => x.UniqueId) diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index cd9db5a..9b83d84 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -960,7 +960,7 @@ private async Task SaveBenchmarkToDbAsync( foreach (var timing in executedRunTimings) { - uint? categoryBenchmarkId = null; + Guid? categoryBenchmarkId = null; if (categoryIdLookup.TryGetValue(timing.Category, out var foundCategoryId)) categoryBenchmarkId = foundCategoryId; @@ -1018,8 +1018,8 @@ private static byte DomainToCategory(string domain) private async Task PersistFailedBenchmarkRunAsync( MagicQuantContext db, uint aiModelHashId, - uint tensorComboId, - uint aiBenchmarkId, + Guid tensorComboId, + Guid aiBenchmarkId, byte category, DateTime startedUtc, DateTime completedUtc, diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 1d68608..9e3ab05 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -92,7 +92,7 @@ public async Task AnalyzeInitialIsolationProbesA if (snap == null) continue; - var scheme = TensorWeightScheme.All.First(x => x.UniqueId == item.TestedSchemeId); + var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.First(x => x.UniqueId == item.TestedSchemeId); var reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.SizeBytes); var kld = GetAggregateKld(snap); var pplDelta = GetAggregatePplDeltaPercent(snap, nativeBaseline); @@ -182,7 +182,7 @@ public async Task AnalyzeAndApplyFinalAsync( if (snap == null) continue; - var scheme = TensorWeightScheme.All.First(x => x.UniqueId == item.TestedSchemeId); + var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.First(x => x.UniqueId == item.TestedSchemeId); candidates.Add(new GroupCandidate { diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index d139dba..007f1eb 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -37,7 +37,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) { var groupDefinitions = TReg.All.ToDictionary(g => g.Name, g => g.Tensors); - var blockRequirements = TensorWeightScheme.All + var blockRequirements = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(s => s.BlockNeo.HasValue) .ToDictionary(s => s.Names[0], s => s.BlockNeo!.Value); @@ -97,7 +97,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) unusedCount++; Cache.UnusedTensorGroups.Add(group); - foreach (var scheme in TensorWeightScheme.All) + foreach (var scheme in TensorWeightScheme.All_Allowed_Hybrid_Quants) { if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId) continue; @@ -110,7 +110,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) foreach (var failure in result.Incompatible) { var group = TReg.GetByName(failure.Group); - var scheme = TensorWeightScheme.All.FirstOrDefault(s => + var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.FirstOrDefault(s => s.Names.Any(n => n.Equals(failure.Scheme, StringComparison.OrdinalIgnoreCase))); if (group == null || scheme == null) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 04a5854..6d59d64 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -26,8 +26,8 @@ public sealed class SampleProcessingRecord public RequiredSamplePlan Plan { get; set; } = default!; public SampleProcessState State { get; set; } public string ModelName { get; set; } = string.Empty; - public uint? TensorComboId { get; set; } - public uint? BenchmarkId { get; set; } + public Guid? TensorComboId { get; set; } + public Guid? BenchmarkId { get; set; } public string? Error { get; set; } } @@ -258,7 +258,7 @@ await Parallel.ForEachAsync( }; } - private async Task<(uint? TensorComboId, uint? BenchmarkId)> ResolveBenchmarkIdentityAsync( + private async Task<(Guid? TensorComboId, Guid? BenchmarkId)> ResolveBenchmarkIdentityAsync( HybridQuant quant, CancellationToken ct) { @@ -292,7 +292,7 @@ await Parallel.ForEachAsync( .Select(x => x.Id) .FirstOrDefaultAsync(ct); - if (comboId == 0) + if (comboId == Guid.Empty) return (null, null); var benchmarkId = await db.AiBenchmarks @@ -301,7 +301,7 @@ await Parallel.ForEachAsync( .Select(x => x.Id) .FirstOrDefaultAsync(ct); - return (comboId, benchmarkId == 0 ? null : benchmarkId); + return (comboId, benchmarkId == Guid.Empty ? null : benchmarkId); } public async Task ProcessHybridQuantAsync( @@ -479,7 +479,7 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok .Select(x => x.benchmark.Id) .FirstOrDefaultAsync(ct); - if (bench == 0) + if (bench == Guid.Empty) return false; // Require at least one category row too, so a half-baked parent row doesn't count as complete. @@ -544,9 +544,9 @@ private async Task PersistQuantizationRunAsync( await db.SaveChangesAsync(ct); } - uint? aiBenchmarkId = await db.AiBenchmarks + Guid? aiBenchmarkId = await db.AiBenchmarks .Where(x => x.AiModelHashId == aiModelHash.Id && x.TensorComboId == tensorCombo.Id) - .Select(x => (uint?)x.Id) + .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); var row = new QuantizationRun @@ -921,7 +921,7 @@ public async Task LearnNativeSourceTruthAsync( var benchmarkId = await db.AiBenchmarks .Where(x => x.AiModelHashId == model.Id && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) - .Select(x => (uint?)x.Id) + .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); if (!benchmarkId.HasValue) @@ -1036,7 +1036,7 @@ private async Task LearnAndPersistBaselineTensorMapAsync( var benchmarkId = await db.AiBenchmarks .Where(x => x.AiModelHashId == model.Id && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) - .Select(x => (uint?)x.Id) + .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); if (!benchmarkId.HasValue) @@ -1331,7 +1331,7 @@ private Dictionary AssignGroups(IEnumerable + return TensorWeightScheme.All_Allowed_Hybrid_Quants.FirstOrDefault(s => !s.Names.IsDefaultOrEmpty && s.Names.Any(sn => baseQuant.Names.Contains(sn, StringComparer.OrdinalIgnoreCase))); } From 15b55c96d2a900eb0152287d00ee6818ec796125 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 16 Apr 2026 14:28:45 -0400 Subject: [PATCH 059/258] new quant application layer now working --- .../20260411180357_InitialCreate.Designer.cs | 179 -------- .../20260411180357_InitialCreate.cs | 144 ------ ...80949_AddExecutionTimingTables.Designer.cs | 340 -------------- ...20260415180949_AddExecutionTimingTables.cs | 158 ------- ...34821_AddLearnedBaselineTables.Designer.cs | 434 ------------------ ...20260415234821_AddLearnedBaselineTables.cs | 103 ----- .../MagicQuantContextModelSnapshot.cs | 431 ----------------- MQ.DB/Models/BaselineQuants.cs | 134 ++++-- MQ.DB/Models/TensorWeightScheme.cs | 152 +++--- MagicQuant/Services/QuantizationService.cs | 245 +++++----- 10 files changed, 312 insertions(+), 2008 deletions(-) delete mode 100644 MQ.DB/Migrations/20260411180357_InitialCreate.Designer.cs delete mode 100644 MQ.DB/Migrations/20260411180357_InitialCreate.cs delete mode 100644 MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.Designer.cs delete mode 100644 MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.cs delete mode 100644 MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.Designer.cs delete mode 100644 MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.cs delete mode 100644 MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs diff --git a/MQ.DB/Migrations/20260411180357_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260411180357_InitialCreate.Designer.cs deleted file mode 100644 index f648965..0000000 --- a/MQ.DB/Migrations/20260411180357_InitialCreate.Designer.cs +++ /dev/null @@ -1,179 +0,0 @@ -// -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260411180357_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260411180357_InitialCreate.cs b/MQ.DB/Migrations/20260411180357_InitialCreate.cs deleted file mode 100644 index 79dd4eb..0000000 --- a/MQ.DB/Migrations/20260411180357_InitialCreate.cs +++ /dev/null @@ -1,144 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class InitialCreate : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "AiModelHashes", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - UniqueHash = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AiModelHashes", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "TensorCombos", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - AttnKV = table.Column(type: "INTEGER", nullable: false), - AttnOutput = table.Column(type: "INTEGER", nullable: false), - AttnQ = table.Column(type: "INTEGER", nullable: false), - BaseQuant = table.Column(type: "INTEGER", nullable: false), - Embeddings = table.Column(type: "INTEGER", nullable: false), - FfnDown = table.Column(type: "INTEGER", nullable: false), - FfnUpGate = table.Column(type: "INTEGER", nullable: false), - LmHead = table.Column(type: "INTEGER", nullable: false), - MoeExperts = table.Column(type: "INTEGER", nullable: false), - MoeRouter = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_TensorCombos", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "AiBenchmarks", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Ngl = table.Column(type: "INTEGER", nullable: false), - SizeBytes = table.Column(type: "INTEGER", nullable: false), - TokensPerSecond = table.Column(type: "REAL", nullable: false), - TensorComboId = table.Column(type: "INTEGER", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AiBenchmarks", x => x.Id); - table.ForeignKey( - name: "FK_AiBenchmarks_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_AiBenchmarks_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "CategoryBenchmark", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - AiBenchmarkId = table.Column(type: "INTEGER", nullable: false), - Category = table.Column(type: "INTEGER", nullable: false), - Kld = table.Column(type: "REAL", nullable: false), - Ppl = table.Column(type: "REAL", nullable: false), - PplError = table.Column(type: "REAL", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_CategoryBenchmark", x => x.Id); - table.ForeignKey( - name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_AiModelHashId_TensorComboId", - table: "AiBenchmarks", - columns: new[] { "AiModelHashId", "TensorComboId" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_TensorComboId", - table: "AiBenchmarks", - column: "TensorComboId"); - - migrationBuilder.CreateIndex( - name: "IX_AiModelHashes_UniqueHash", - table: "AiModelHashes", - column: "UniqueHash"); - - migrationBuilder.CreateIndex( - name: "IX_CategoryBenchmark_AiBenchmarkId", - table: "CategoryBenchmark", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", - table: "TensorCombos", - columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "CategoryBenchmark"); - - migrationBuilder.DropTable( - name: "AiBenchmarks"); - - migrationBuilder.DropTable( - name: "AiModelHashes"); - - migrationBuilder.DropTable( - name: "TensorCombos"); - } - } -} diff --git a/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.Designer.cs b/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.Designer.cs deleted file mode 100644 index e9b173d..0000000 --- a/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.Designer.cs +++ /dev/null @@ -1,340 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260415180949_AddExecutionTimingTables")] - partial class AddExecutionTimingTables - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.cs b/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.cs deleted file mode 100644 index fe522dd..0000000 --- a/MQ.DB/Migrations/20260415180949_AddExecutionTimingTables.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class AddExecutionTimingTables : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "BenchmarkRuns", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - TensorComboId = table.Column(type: "INTEGER", nullable: false), - AiBenchmarkId = table.Column(type: "INTEGER", nullable: false), - CategoryBenchmarkId = table.Column(type: "INTEGER", nullable: true), - Category = table.Column(type: "INTEGER", nullable: false), - StartedUtc = table.Column(type: "TEXT", nullable: false), - CompletedUtc = table.Column(type: "TEXT", nullable: false), - DurationMs = table.Column(type: "INTEGER", nullable: false), - Succeeded = table.Column(type: "INTEGER", nullable: false), - Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_BenchmarkRuns", x => x.Id); - table.ForeignKey( - name: "FK_BenchmarkRuns_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_BenchmarkRuns_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_BenchmarkRuns_CategoryBenchmark_CategoryBenchmarkId", - column: x => x.CategoryBenchmarkId, - principalTable: "CategoryBenchmark", - principalColumn: "Id", - onDelete: ReferentialAction.SetNull); - table.ForeignKey( - name: "FK_BenchmarkRuns_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "QuantizationRuns", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - TensorComboId = table.Column(type: "INTEGER", nullable: false), - AiBenchmarkId = table.Column(type: "INTEGER", nullable: true), - StartedUtc = table.Column(type: "TEXT", nullable: false), - CompletedUtc = table.Column(type: "TEXT", nullable: false), - DurationMs = table.Column(type: "INTEGER", nullable: false), - Succeeded = table.Column(type: "INTEGER", nullable: false), - Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true), - OutputModelPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_QuantizationRuns", x => x.Id); - table.ForeignKey( - name: "FK_QuantizationRuns_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.SetNull); - table.ForeignKey( - name: "FK_QuantizationRuns_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_QuantizationRuns_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiBenchmarkId", - table: "BenchmarkRuns", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiBenchmarkId_Category", - table: "BenchmarkRuns", - columns: new[] { "AiBenchmarkId", "Category" }); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiModelHashId", - table: "BenchmarkRuns", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_CategoryBenchmarkId", - table: "BenchmarkRuns", - column: "CategoryBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_StartedUtc", - table: "BenchmarkRuns", - column: "StartedUtc"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_TensorComboId", - table: "BenchmarkRuns", - column: "TensorComboId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_AiBenchmarkId", - table: "QuantizationRuns", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_AiModelHashId", - table: "QuantizationRuns", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_StartedUtc", - table: "QuantizationRuns", - column: "StartedUtc"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_TensorComboId", - table: "QuantizationRuns", - column: "TensorComboId"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "BenchmarkRuns"); - - migrationBuilder.DropTable( - name: "QuantizationRuns"); - } - } -} diff --git a/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.Designer.cs b/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.Designer.cs deleted file mode 100644 index be185a5..0000000 --- a/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.Designer.cs +++ /dev/null @@ -1,434 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260415234821_AddLearnedBaselineTables")] - partial class AddLearnedBaselineTables - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("BaselineName") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeId") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeName") - .IsUnique(); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.cs b/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.cs deleted file mode 100644 index 0d9c393..0000000 --- a/MQ.DB/Migrations/20260415234821_AddLearnedBaselineTables.cs +++ /dev/null @@ -1,103 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class AddLearnedBaselineTables : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "BaselineQuantDefinitions", - columns: table => new - { - BaselineQuantId = table.Column(type: "INTEGER", nullable: false), - BaselineName = table.Column(type: "TEXT", maxLength: 64, nullable: false), - DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), - DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.BaselineQuantId); - }); - - migrationBuilder.CreateTable( - name: "LearnedBaselineTensorQuants", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - AiBenchmarkId = table.Column(type: "INTEGER", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - BaselineQuantId = table.Column(type: "INTEGER", nullable: false), - TensorWeightSchemeId = table.Column(type: "INTEGER", nullable: false), - TensorGroupId = table.Column(type: "INTEGER", nullable: false), - TensorName = table.Column(type: "TEXT", maxLength: 512, nullable: false), - FinalQuantType = table.Column(type: "TEXT", maxLength: 32, nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_LearnedBaselineTensorQuants", x => x.Id); - table.ForeignKey( - name: "FK_LearnedBaselineTensorQuants_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_LearnedBaselineTensorQuants_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_BaselineName", - table: "BaselineQuantDefinitions", - column: "BaselineName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeId", - table: "BaselineQuantDefinitions", - column: "DefaultTensorSchemeId", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeName", - table: "BaselineQuantDefinitions", - column: "DefaultTensorSchemeName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiBenchmarkId", - table: "LearnedBaselineTensorQuants", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorGroupId", - table: "LearnedBaselineTensorQuants", - columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId" }); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorName", - table: "LearnedBaselineTensorQuants", - columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName" }, - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "BaselineQuantDefinitions"); - - migrationBuilder.DropTable( - name: "LearnedBaselineTensorQuants"); - } - } -} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs deleted file mode 100644 index 9cec12c..0000000 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ /dev/null @@ -1,431 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - partial class MagicQuantContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("BaselineName") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeId") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeName") - .IsUnique(); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 3d512d8..0a02315 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -6,38 +6,64 @@ public record BaselineQuants( byte UniqueId, bool RequiresImatrix, ImmutableArray Names, - TensorWeightScheme? DefaultTensorScheme, + ImmutableArray TensorWeightSchemes, HybridQuant? BaseConversionBase = null) { public const byte NativeSourceUniqueId = 250; - public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"], TensorWeightScheme.Q8_0); - public static readonly BaselineQuants Q6_K = new(1, false, ["Q6_K"], TensorWeightScheme.Q6_K); - public static readonly BaselineQuants Q5_K = new(2, false, ["Q5_K"], TensorWeightScheme.Q5_K); - public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"], TensorWeightScheme.Q4_K); + public TensorWeightScheme? DefaultTensorScheme => + TensorWeightSchemes.IsDefaultOrEmpty ? null : TensorWeightSchemes[0]; - public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"], TensorWeightScheme.IQ4_NL); + public static readonly BaselineQuants Q8_0 = + new(0, false, ["Q8_0"], [TensorWeightScheme.Q8_0]); - public static readonly BaselineQuants IQ4_XS = new( - 6, - false, - ["IQ4_XS"], - TensorWeightScheme.IQ4_XS, - new HybridQuant - { - BaseQuant = null!, - Tensors = TReg.All - .Select(g => new HybridTensor - { - TGroup = g, - TensorType = TensorWeightScheme.IQ4_XS - }) - .ToList() - }); - - // IQ3 and lower require imatrix - //public static readonly BaselineQuants IQ3_M = new(7, true, ["IQ3_M"], true); - //public static readonly BaselineQuants IQ2_M = new(8, true, ["IQ2_M"], true); + public static readonly BaselineQuants Q6_K = + new(1, false, ["Q6_K"], [TensorWeightScheme.Q6_K]); + + public static readonly BaselineQuants Q5_K = + new(2, false, ["Q5_K"], [TensorWeightScheme.Q5_K]); + + public static readonly BaselineQuants Q4_K_M = + new(3, false, ["Q4_K_M"], [TensorWeightScheme.Q4_K]); + + public static readonly BaselineQuants IQ4_NL = + new(5, false, ["IQ4_NL"], [TensorWeightScheme.IQ4_NL]); + + public static readonly BaselineQuants IQ4_XS = + new( + 6, + false, + ["IQ4_XS"], + [TensorWeightScheme.IQ4_XS], + new HybridQuant + { + BaseQuant = null!, + Tensors = TReg.All + .Select(g => new HybridTensor + { + TGroup = g, + TensorType = TensorWeightScheme.IQ4_XS + }) + .ToList() + }); + + public static readonly BaselineQuants IQ3_S = + new(7, true, ["IQ3_S"], [TensorWeightScheme.IQ3_S]); + + public static readonly BaselineQuants IQ3_XS = + new(8, true, ["IQ3_XS"], [TensorWeightScheme.IQ3_XS]); + + public static readonly BaselineQuants IQ3_XXS = + new(9, true, ["IQ3_XXS"], [TensorWeightScheme.IQ3_XXS]); + + public static readonly BaselineQuants IQ2_S = + new(10, true, ["IQ2_S"], [TensorWeightScheme.IQ2_S]); + + public static readonly BaselineQuants IQ2_XS = + new(11, true, ["IQ2_XS"], [TensorWeightScheme.IQ2_XS]); + + public static readonly BaselineQuants IQ2_XXS = + new(12, true, ["IQ2_XXS"], [TensorWeightScheme.IQ2_XXS]); public static readonly ImmutableArray All = [ @@ -47,8 +73,12 @@ public record BaselineQuants( Q4_K_M, IQ4_NL, IQ4_XS, - //IQ3_M, - //IQ2_M + IQ3_S, + IQ3_XS, + IQ3_XXS, + IQ2_S, + IQ2_XS, + IQ2_XXS ]; static BaselineQuants() @@ -60,64 +90,66 @@ static BaselineQuants() public static void ValidateIntegrityOrThrow() { var invalidBaselines = All - .Where(x => x.DefaultTensorScheme == null) + .Where(x => x.TensorWeightSchemes.IsDefaultOrEmpty) .Select(x => x.Names.IsDefaultOrEmpty ? $"id:{x.UniqueId}" : x.Names[0]) .ToList(); if (invalidBaselines.Count > 0) { throw new InvalidOperationException( - "Every BaselineQuants entry must define DefaultTensorScheme. Missing for: " + + "Every BaselineQuants entry must define at least one TensorWeightScheme. Missing for: " + string.Join(", ", invalidBaselines)); } - var duplicateDefaultSchemeIds = All - .GroupBy(x => x.DefaultTensorScheme!.UniqueId) + var duplicateSchemeIds = All + .SelectMany(x => x.TensorWeightSchemes.Select(s => new { Baseline = x, Scheme = s })) + .GroupBy(x => x.Scheme.UniqueId) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); - if (duplicateDefaultSchemeIds.Count > 0) + if (duplicateSchemeIds.Count > 0) { - var duplicateNames = duplicateDefaultSchemeIds - .Select(id => TensorWeightScheme.All_Allowed_Hybrid_Quants.First(s => s.UniqueId == id).Names[0]); + var duplicateNames = duplicateSchemeIds + .Select(id => TensorWeightScheme.All.First(s => s.UniqueId == id).Names[0]); throw new InvalidOperationException( - "DefaultTensorScheme must be unique across BaselineQuants entries. Duplicates: " + + "TensorWeightScheme associations must be unique across BaselineQuants entries. Duplicates: " + string.Join(", ", duplicateNames)); } - var schemesMissingBaseline = TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - // Some schemes can be experimental and intentionally not promoted to baseline. - // Hard-enforce only for the established shipped baseline set. - .Where(x => x.UniqueId != TensorWeightScheme.MXFP4.UniqueId) - .Where(x => !All.Any(b => b.DefaultTensorScheme!.UniqueId == x.UniqueId)) + var missingBaselineSchemes = TensorWeightScheme.All + .Where(x => x.IsEligibleForBaseline) + .Where(x => !All.Any(b => b.TensorWeightSchemes.Any(s => s.UniqueId == x.UniqueId))) .Select(x => x.Names[0]) .ToList(); - if (schemesMissingBaseline.Count > 0) + if (missingBaselineSchemes.Count > 0) { throw new InvalidOperationException( - "Every TensorWeightScheme must be linked by exactly one BaselineQuants.DefaultTensorScheme. Missing for: " + - string.Join(", ", schemesMissingBaseline)); + "Every baseline-eligible TensorWeightScheme must be linked by exactly one BaselineQuants entry. Missing for: " + + string.Join(", ", missingBaselineSchemes)); } } - public static BaselineQuants GetBF16Quant() + public static BaselineQuants GetNativeQuant() { + var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + return new( NativeSourceUniqueId, false, - [(Cache.TorchType ?? Cache.MainTorchType.BF16).ToString()], - TensorWeightScheme.BF16_F16); + [nativeScheme.Names[0]], + [nativeScheme]); } + // Compatibility alias for older code paths. + public static BaselineQuants GetBF16Quant() => GetNativeQuant(); + public static BaselineQuants FromId(byte id) { if (id == NativeSourceUniqueId) - return GetBF16Quant(); + return GetNativeQuant(); var found = All.FirstOrDefault(x => x.UniqueId == id); if (found == null) @@ -125,4 +157,4 @@ public static BaselineQuants FromId(byte id) return found; } -} +} \ No newline at end of file diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 0ef3d6a..48eb9f8 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using MQ.DB; namespace MQ.DB.Models; @@ -12,6 +13,7 @@ public sealed class TensorWeightScheme public List BannedGroups { get; } public ushort? BlockNeo { get; } public bool IsSmallest { get; } + public bool IsEligibleForBaseline { get; } private TensorWeightScheme( byte uniqueId, @@ -19,13 +21,15 @@ private TensorWeightScheme( ImmutableArray names, IEnumerable bannedGroups, ushort? blockNeo, - bool isSmallest = false) + bool isSmallest = false, + bool isEligibleForBaseline = true) { UniqueId = uniqueId; RequiresImatrix = requiresImatrix; Names = names; BlockNeo = blockNeo; IsSmallest = isSmallest; + IsEligibleForBaseline = isEligibleForBaseline; var distinctGroups = bannedGroups .GroupBy(x => x.UniqueId) @@ -47,7 +51,7 @@ public void ResetRuntimeBans() public static void ResetAllRuntimeBans() { - foreach (var scheme in All_Allowed_Hybrid_Quants) + foreach (var scheme in All) scheme.ResetRuntimeBans(); } @@ -55,7 +59,9 @@ public static void ValidateSmallestConfiguration() { var nonImatrixSmallest = All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != NULL.UniqueId) - .Where(x => x.UniqueId != BF16_F16.UniqueId) + .Where(x => x.UniqueId != BF16.UniqueId) + .Where(x => x.UniqueId != F16.UniqueId) + .Where(x => x.UniqueId != F32.UniqueId) .Where(x => !x.RequiresImatrix) .Where(x => x.IsSmallest) .ToList(); @@ -77,17 +83,40 @@ public static TensorWeightScheme GetSmallestNonImatrix() return All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != NULL.UniqueId) - .Where(x => x.UniqueId != BF16_F16.UniqueId) + .Where(x => x.UniqueId != BF16.UniqueId) + .Where(x => x.UniqueId != F16.UniqueId) + .Where(x => x.UniqueId != F32.UniqueId) .Where(x => !x.RequiresImatrix) .Single(x => x.IsSmallest); } + public static TensorWeightScheme GetCurrentNativePrecisionScheme() + { + return (Cache.TorchType ?? Cache.MainTorchType.BF16) switch + { + Cache.MainTorchType.BF16 => BF16, + Cache.MainTorchType.F16 => F16, + Cache.MainTorchType.F32 => F32, + _ => BF16 + }; + } + + public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) + { + return scheme.UniqueId == BF16.UniqueId || + scheme.UniqueId == F16.UniqueId || + scheme.UniqueId == F32.UniqueId; + } + + // Compatibility shim for any older code still referencing BF16_F16. + public static TensorWeightScheme BF16_F16 => GetCurrentNativePrecisionScheme(); + public static readonly TensorWeightScheme NULL = - new(0, false, ["NULL"], Array.Empty(), null); + new(0, false, ["NULL"], Array.Empty(), null, isEligibleForBaseline: false); + + public static readonly TensorWeightScheme BF16 = + new(1, false, ["BF16", "BFLOAT16"], Array.Empty(), null, isEligibleForBaseline: false); - public static readonly TensorWeightScheme BF16_F16 = - new(1, false, ["BF16", "F16", "F32"], Array.Empty(), null); - public static readonly TensorWeightScheme MXFP4 = new( 2, @@ -99,7 +128,8 @@ public static TensorWeightScheme GetSmallestNonImatrix() TReg.MoeRouter, TReg.MoeExperts }, - 32); + 32, + isEligibleForBaseline: false); public static readonly TensorWeightScheme Q8_0 = new(3, false, ["Q8_0"], Array.Empty(), null); @@ -112,27 +142,11 @@ public static TensorWeightScheme GetSmallestNonImatrix() public static readonly TensorWeightScheme IQ4_XS = new(6, false, ["IQ4_XS"], new[] { TReg.MoeRouter }, 32, true); - - public static TensorWeightScheme IQ4_NL = - new( - 7, - false, - ["IQ4_NL"], - new[] { TReg.MoeRouter }, - 32 - ); - - public static TensorWeightScheme Q4_K = - new( - 14, - false, - ["Q4_K"], - new[] { TReg.MoeRouter }, - 32 - ); + public static readonly TensorWeightScheme IQ4_NL = + new(7, false, ["IQ4_NL"], new[] { TReg.MoeRouter }, 32); - /* public static TensorWeightScheme IQ3_S = + public static readonly TensorWeightScheme IQ3_S = new( 8, true, @@ -146,7 +160,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() 32 ); - public static TensorWeightScheme IQ3_XS = + public static readonly TensorWeightScheme IQ3_XS = new( 9, true, @@ -160,7 +174,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() 32 ); - public static TensorWeightScheme IQ3_XXS = + public static readonly TensorWeightScheme IQ3_XXS = new( 10, true, @@ -174,7 +188,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() 32 ); - public static TensorWeightScheme IQ2_S = + public static readonly TensorWeightScheme IQ2_S = new( 11, true, @@ -189,7 +203,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() 32 ); - public static TensorWeightScheme IQ2_XS = + public static readonly TensorWeightScheme IQ2_XS = new( 12, true, @@ -204,7 +218,7 @@ public static TensorWeightScheme GetSmallestNonImatrix() 32 ); - public static TensorWeightScheme IQ2_XXS = + public static readonly TensorWeightScheme IQ2_XXS = new( 13, true, @@ -219,24 +233,56 @@ public static TensorWeightScheme GetSmallestNonImatrix() }, 32 ); - */ - - public static readonly ImmutableArray All_Allowed_Hybrid_Quants = - [ - NULL, - BF16_F16, - MXFP4, - Q8_0, - Q6_K, - Q5_K, - IQ4_XS, - IQ4_NL, - Q4_K, - // IQ3_S, - // IQ3_XS, - // IQ3_XXS, - // IQ2_S, - // IQ2_XS, - // IQ2_XXS - ]; + + public static readonly TensorWeightScheme Q4_K = + new( + 14, + false, + ["Q4_K"], + new[] { TReg.MoeRouter }, + 32 + ); + + public static readonly TensorWeightScheme F16 = + new(15, false, ["F16", "FLOAT16", "FP16", "HALF"], Array.Empty(), null, isEligibleForBaseline: false); + + public static readonly TensorWeightScheme F32 = + new(16, false, ["F32", "FLOAT32", "FP32", "FLOAT"], Array.Empty(), null, isEligibleForBaseline: false); + + // This is the set used by hybrid search / combination generation. + public static readonly ImmutableArray All_Allowed_Hybrid_Quants = + [ + NULL, + BF16, + //F16, + MXFP4, + Q8_0, + Q6_K, + Q5_K, + IQ4_XS, + IQ4_NL, + Q4_K + ]; + + // This is the true registry of everything known. + public static readonly ImmutableArray All = + [ + NULL, + BF16, + F16, + F32, + MXFP4, + Q8_0, + Q6_K, + Q5_K, + IQ4_XS, + IQ4_NL, + IQ3_S, + IQ3_XS, + IQ3_XXS, + IQ2_S, + IQ2_XS, + IQ2_XXS, + Q4_K + ]; } \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 6d59d64..3a09773 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -50,6 +50,10 @@ public class QuantizationService private readonly int _maxConcurrentQuantizations; private static readonly SemaphoreSlim BaseModelLock = new(1, 1); + private const byte UnknownTensorGroupId = 255; + + private static readonly Lazy> QuantAliasLookup = + new(BuildQuantAliasLookup, LazyThreadSafetyMode.ExecutionAndPublication); public QuantizationService(BenchmarkService benchmarker) { @@ -79,30 +83,24 @@ public QuantizationService(BenchmarkService benchmarker) public static void ValidateQuantNameNormalizationOrThrow() { - var aliasExpectations = new Dictionary(StringComparer.OrdinalIgnoreCase) - { - ["bf16"] = "BF16", - ["bfloat16"] = "BF16", - ["f16"] = "F16", - ["float16"] = "F16", - ["f32"] = "F32", - ["float32"] = "F32", - ["q6_k"] = "Q6_K", - ["q5_k"] = "Q5_K", - ["q8_0"] = "Q8_0", - ["iq4_xs"] = "IQ4_XS", - ["iq4_nl"] = "IQ4_NL" - }; - - var mismatches = aliasExpectations - .Where(x => !string.Equals(NormalizeQuantName(x.Key), x.Value, StringComparison.Ordinal)) - .Select(x => $"{x.Key}->{NormalizeQuantName(x.Key)} (expected {x.Value})") + var collisions = TensorWeightScheme.All + .Where(x => !x.Names.IsDefaultOrEmpty) + .SelectMany(s => s.Names.Select(name => new + { + SchemeId = s.UniqueId, + Canonical = s.Names[0], + Alias = CanonicalizeQuantToken(name) + })) + .GroupBy(x => x.Alias, StringComparer.Ordinal) + .Where(g => g.Select(x => x.SchemeId).Distinct().Count() > 1) + .Select(g => $"{g.Key} => {string.Join(", ", g.Select(x => x.Canonical).Distinct(StringComparer.Ordinal))}") .ToList(); - if (mismatches.Count > 0) + if (collisions.Count > 0) { throw new InvalidOperationException( - "Quant name alias normalization is misconfigured: " + string.Join(", ", mismatches)); + "Quant alias registry has conflicting aliases across TensorWeightScheme definitions: " + + string.Join(" | ", collisions)); } } @@ -317,7 +315,6 @@ public async Task ProcessHybridQuantAsync( var stopwatch = Stopwatch.StartNew(); var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); - // 1. Fast path: valid artifacts already exist on disk and can be synced/reused if (!forceBaselineRelearn && await _benchmarker.TryReuseExistingBenchmarksAsync( quantConfig: quant, modelPath: quantPath, @@ -333,7 +330,6 @@ public async Task ProcessHybridQuantAsync( return SampleProcessState.Skipped; } - // 2. DB truth still matters too if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) { AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); @@ -363,7 +359,6 @@ public async Task ProcessHybridQuantAsync( _cpuQuantLock.Release(); } - // Re-check after build in case another worker finished the DB sync while we were quantizing if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) { if (!IsProtectedModel(modelName)) @@ -482,7 +477,6 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok if (bench == Guid.Empty) return false; - // Require at least one category row too, so a half-baked parent row doesn't count as complete. bool hasCategory = await db.Set() .AsNoTracking() .AnyAsync(x => x.AiBenchmarkId == bench, ct); @@ -591,7 +585,7 @@ public async Task EnsureBaseModelAsync(bool deleteProcess = false) var baseModelQuant = new HybridQuant { - BaseQuant = BaselineQuants.GetBF16Quant(), + BaseQuant = BaselineQuants.GetNativeQuant(), Tensors = new List() }; @@ -754,9 +748,6 @@ private async Task RunLlamaQuantizeAsync(string inp var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, outputFile); var requestedOverrides = BuildRequestedTensorOverrides(quant, inputTensorMetadata.TensorNames); - - // Keep this resolution step: - // it is not output validation; it is how logical group rules become real tensor names. var concreteOverrides = ResolveConcreteTensorOverrides( allTensorNames: inputTensorMetadata.TensorNames, requestedOverrides: requestedOverrides); @@ -831,8 +822,7 @@ private static string ResolveQuantizeBaseArgument( throw new InvalidOperationException( "Native BF16/F16/F32 + tensor overrides is disabled. " + "In this build of llama-quantize it produced no-op outputs for isolation tests. " + - "Use a real carrier baseline (Q8_0 recommended), force all known groups to BF16/F16, " + - "and quantize only the target group."); + "Use a real carrier baseline (Q8_0 recommended) and apply only learned exact tensor overrides for the target configuration."); } return ResolveBaseName(quant.BaseQuant); @@ -927,23 +917,31 @@ public async Task LearnNativeSourceTruthAsync( if (!benchmarkId.HasValue) throw new InvalidOperationException("Native-source benchmark row is missing; benchmark base model before native-source learning."); + var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + await db.LearnedBaselineTensorQuants .Where(x => x.AiModelHashId == model.Id && x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && - x.TensorWeightSchemeId == TensorWeightScheme.BF16_F16.UniqueId) + x.TensorWeightSchemeId == nativeScheme.UniqueId) .ExecuteDeleteAsync(ct); var rows = truth - .Where(x => grouped[x.Key].PrimaryGroup != null) - .Select(x => new LearnedBaselineTensorQuant + .OrderBy(x => x.Key, StringComparer.Ordinal) + .Select(x => { - AiBenchmarkId = benchmarkId.Value, - AiModelHashId = model.Id, - BaselineQuantId = BaselineQuants.NativeSourceUniqueId, - TensorWeightSchemeId = TensorWeightScheme.BF16_F16.UniqueId, - TensorGroupId = grouped[x.Key].PrimaryGroup!.UniqueId, - TensorName = x.Key, - FinalQuantType = x.Value.FinalQuantType + var primaryGroup = grouped[x.Key].PrimaryGroup; + + return new LearnedBaselineTensorQuant + { + Id = Guid.NewGuid(), + AiBenchmarkId = benchmarkId.Value, + AiModelHashId = model.Id, + BaselineQuantId = BaselineQuants.NativeSourceUniqueId, + TensorWeightSchemeId = nativeScheme.UniqueId, + TensorGroupId = primaryGroup?.UniqueId ?? UnknownTensorGroupId, + TensorName = x.Key, + FinalQuantType = x.Value.FinalQuantType + }; }) .ToList(); @@ -954,15 +952,15 @@ await db.LearnedBaselineTensorQuants await db.SaveChangesAsync(ct); await WriteLearningDiagnosticArtifactAsync( - baselineName: "NATIVE", - schemeName: TensorWeightScheme.BF16_F16.Names[0], + baselineName: $"NATIVE_{nativeScheme.Names[0]}", + schemeName: nativeScheme.Names[0], truthByTensor: truth, grouped: grouped, allTensorNamesInModel: metadata.TensorNames, ambiguous: ambiguous, unresolved: unresolved); - var sourcePrecision = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + var sourcePrecision = nativeScheme.Names[0]; var distribution = rows.GroupBy(x => x.FinalQuantType) .OrderByDescending(g => g.Count()) .Select(g => $"{g.Key}:{g.Count()}") @@ -1018,7 +1016,8 @@ private async Task LearnAndPersistBaselineTensorMapAsync( if (unresolved.Count > 0) { AnsiConsole.MarkupLine( - $"[yellow]WARNING:[/] {unresolved.Count} tensor(s) had no tensor-group match while learning baseline {Markup.Escape(quant.BaseQuant.Names[0])}."); + $"[yellow]WARNING:[/] {unresolved.Count} tensor(s) had no tensor-group match while learning baseline {Markup.Escape(quant.BaseQuant.Names[0])}. " + + $"They will still be saved with TensorGroupId={UnknownTensorGroupId}."); } await using var db = new MagicQuantContext(); @@ -1048,24 +1047,25 @@ await db.LearnedBaselineTensorQuants x.TensorWeightSchemeId == tensorScheme.UniqueId) .ExecuteDeleteAsync(ct); - var rows = new List(truth.Count); - foreach (var kv in truth.OrderBy(x => x.Key, StringComparer.Ordinal)) - { - var match = grouped[kv.Key]; - if (match.PrimaryGroup == null) - continue; - - rows.Add(new LearnedBaselineTensorQuant + var rows = truth + .OrderBy(x => x.Key, StringComparer.Ordinal) + .Select(kv => { - AiBenchmarkId = benchmarkId.Value, - AiModelHashId = model.Id, - BaselineQuantId = quant.BaseQuant.UniqueId, - TensorWeightSchemeId = tensorScheme.UniqueId, - TensorGroupId = match.PrimaryGroup.UniqueId, - TensorName = kv.Key, - FinalQuantType = kv.Value.FinalQuantType - }); - } + var match = grouped[kv.Key]; + + return new LearnedBaselineTensorQuant + { + Id = Guid.NewGuid(), + AiBenchmarkId = benchmarkId.Value, + AiModelHashId = model.Id, + BaselineQuantId = quant.BaseQuant.UniqueId, + TensorWeightSchemeId = tensorScheme.UniqueId, + TensorGroupId = match.PrimaryGroup?.UniqueId ?? UnknownTensorGroupId, + TensorName = kv.Key, + FinalQuantType = kv.Value.FinalQuantType + }; + }) + .ToList(); if (rows.Count == 0) throw new InvalidOperationException($"Learning baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); @@ -1145,7 +1145,6 @@ private Dictionary BuildTruthMapWithVerification( } else { - // GGUF is source-of-truth for persisted mapping. result[name] = new LearnedTensorTruth(name, ggufType!, LearningSource.BothWithMismatch); if (IsHighSeverityMismatch(logType!, ggufType!)) @@ -1166,9 +1165,9 @@ private Dictionary BuildTruthMapWithVerification( if (hardMismatches.Count > 0) { - throw new InvalidOperationException( - $"Baseline '{baselineName}' had {hardMismatches.Count} high-severity GGUF/log truth mismatches. " + - $"Examples: {string.Join(" | ", hardMismatches.Take(8))}"); + AnsiConsole.MarkupLine( + $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {hardMismatches.Count} high-severity GGUF/log mismatches; GGUF truth was used."); + AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", hardMismatches.Take(6)))}[/]"); } if (softMismatches.Count > 0) @@ -1180,9 +1179,9 @@ private Dictionary BuildTruthMapWithVerification( if (logOnly.Count > 0) { - throw new InvalidOperationException( - $"Baseline '{baselineName}' produced {logOnly.Count} log-only tensor mappings with no GGUF truth. " + - $"Examples: {string.Join(" | ", logOnly.Take(8))}"); + AnsiConsole.MarkupLine( + $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] produced {logOnly.Count} log-only tensor mapping(s) with no GGUF truth. They were ignored."); + AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", logOnly.Take(6)))}[/]"); } return result; @@ -1190,8 +1189,8 @@ private Dictionary BuildTruthMapWithVerification( private static bool IsHighSeverityMismatch(string logType, string ggufType) { - bool logHighPrecision = logType is "F32" or "F16" or "BF16"; - bool ggufHighPrecision = ggufType is "F32" or "F16" or "BF16"; + bool logHighPrecision = IsHighPrecisionType(logType); + bool ggufHighPrecision = IsHighPrecisionType(ggufType); return logHighPrecision != ggufHighPrecision; } @@ -1293,9 +1292,9 @@ private async Task WriteLearningDiagnosticArtifactAsync( if (severeCoverageIssues.Count > 0) { - throw new InvalidOperationException( - $"Baseline learning coverage was incomplete for {severeCoverageIssues.Count} group(s): " + - string.Join(" | ", severeCoverageIssues.Take(8))); + AnsiConsole.MarkupLine( + $"[yellow]WARNING:[/] Baseline learning coverage was incomplete for {severeCoverageIssues.Count} group(s): " + + $"{Markup.Escape(string.Join(" | ", severeCoverageIssues.Take(8)))}"); } } @@ -1326,14 +1325,9 @@ private Dictionary AssignGroups(IEnumerable - !s.Names.IsDefaultOrEmpty && - s.Names.Any(sn => baseQuant.Names.Contains(sn, StringComparer.OrdinalIgnoreCase))); + return baseQuant.DefaultTensorScheme; } private List BuildRequestedTensorOverrides( @@ -1355,8 +1349,6 @@ private List BuildRequestedTensorOverrides( if (hybrid.TensorType.UniqueId == TensorWeightScheme.NULL.UniqueId) continue; - // Do not emit a redundant override if this tensor type is already the same - // as the blanket base quant. if (baseScheme != null && hybrid.TensorType.UniqueId == baseScheme.UniqueId) continue; @@ -1364,7 +1356,7 @@ private List BuildRequestedTensorOverrides( if (learned.Count == 0) { throw new InvalidOperationException( - $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + scheme '{hybrid.TensorType.Names[0]}'. " + + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + scheme '{ResolveSchemeName(hybrid.TensorType)}'. " + "Run with --relearn-baseline-mappings to regenerate."); } @@ -1382,7 +1374,7 @@ private List BuildRequestedTensorOverrides( var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); throw new InvalidOperationException( - $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + scheme '{hybrid.TensorType.Names[0]}'. " + + $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + scheme '{ResolveSchemeName(hybrid.TensorType)}'. " + $"Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); } @@ -1412,13 +1404,15 @@ private Dictionary TryLoadLearnedTensorMapping(TensorWeightSchem return new Dictionary(StringComparer.Ordinal); byte baselineId; - if (sourceScheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + if (TensorWeightScheme.IsNativePrecisionScheme(sourceScheme)) { baselineId = BaselineQuants.NativeSourceUniqueId; } else { - var baseline = BaselineQuants.All.FirstOrDefault(x => x.DefaultTensorScheme?.UniqueId == sourceScheme.UniqueId); + var baseline = BaselineQuants.All.FirstOrDefault(x => + x.TensorWeightSchemes.Any(s => s.UniqueId == sourceScheme.UniqueId)); + if (baseline == null) return new Dictionary(StringComparer.Ordinal); @@ -1446,6 +1440,7 @@ private List ResolveConcreteTensorOverrides( { if (requestedOverrides.Count == 0) return new List(); + var nameSet = allTensorNames.ToHashSet(StringComparer.Ordinal); var missing = requestedOverrides @@ -1553,32 +1548,64 @@ with open(output_path, "w", encoding="utf-8") as f: // ---------------------------------------------------------------- private static readonly Regex TensorLogLineRegex = new( - @"\]\s+(?[^\s]+)\s+-\s+\[[^\]]+\],\s+type\s*=\s*(?[A-Za-z0-9_]+)(?:.*?converting to\s+(?[A-Za-z0-9_]+))?", + @"\]\s+(?[^\s]+)\s+-\s+\[[^\]]+\],\s+type\s*=\s*(?[^\s,]+)(?:.*?converting to\s+(?[^\s,]+))?", RegexOptions.Compiled | RegexOptions.CultureInvariant); private static string NormalizeQuantName(string value) { - var normalized = value + if (string.IsNullOrWhiteSpace(value)) + return "UNKNOWN"; + + string token = CanonicalizeQuantToken(value); + + if (QuantAliasLookup.Value.TryGetValue(token, out var canonical)) + return canonical; + + return token; + } + + private static Dictionary BuildQuantAliasLookup() + { + var map = new Dictionary(StringComparer.Ordinal); + + foreach (var scheme in TensorWeightScheme.All) + { + if (scheme.Names.IsDefaultOrEmpty) + continue; + + string canonical = scheme.Names[0]; + + foreach (var alias in scheme.Names) + { + string token = CanonicalizeQuantToken(alias); + + if (!map.TryAdd(token, canonical)) + { + if (!string.Equals(map[token], canonical, StringComparison.Ordinal)) + { + throw new InvalidOperationException( + $"Quant alias collision detected for token '{token}'. Existing='{map[token]}', New='{canonical}'."); + } + } + } + } + + return map; + } + + private static string CanonicalizeQuantToken(string value) + { + return value .Trim() .Replace("-", "_") .Replace(" ", string.Empty) .ToUpperInvariant(); + } - return normalized switch - { - "BF16" => "BF16", - "BFLOAT16" => "BF16", - "F16" => "F16", - "FLOAT16" => "F16", - "F32" => "F32", - "FLOAT32" => "F32", - "Q5_K" => "Q5_K", - "Q6_K" => "Q6_K", - "Q8_0" => "Q8_0", - "IQ4_XS" => "IQ4_XS", - "IQ4_NL" => "IQ4_NL", - _ => normalized - }; + private static bool IsHighPrecisionType(string value) + { + string normalized = NormalizeQuantName(value); + return normalized is "BF16" or "F16" or "F32"; } private sealed class QuantizationExecutionReport @@ -1641,17 +1668,6 @@ private static string ResolveSchemeName(TensorWeightScheme s) if (s.Names.IsDefaultOrEmpty) throw new InvalidOperationException($"TensorWeightScheme '{s.UniqueId}' has no Names."); - if (s.UniqueId == TensorWeightScheme.BF16_F16.UniqueId && s.Names.Length >= 2) - { - if (Cache.TorchType == Cache.MainTorchType.F16) - return "F16"; - - if (Cache.TorchType == Cache.MainTorchType.F32) - return "F32"; - - return "BF16"; - } - return s.Names[0]; } @@ -1787,7 +1803,6 @@ void HandleLine(string? line, bool isError) } catch { - // ignored } }); From b8556ab81406b2f8c22872a6b94d6397083cbef5 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 16 Apr 2026 14:28:55 -0400 Subject: [PATCH 060/258] new database changes --- .../20260416165930_InitialCreate.Designer.cs | 431 ++++++++++++++++++ .../20260416165930_InitialCreate.cs | 365 +++++++++++++++ .../MagicQuantContextModelSnapshot.cs | 428 +++++++++++++++++ 3 files changed, 1224 insertions(+) create mode 100644 MQ.DB/Migrations/20260416165930_InitialCreate.Designer.cs create mode 100644 MQ.DB/Migrations/20260416165930_InitialCreate.cs create mode 100644 MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs diff --git a/MQ.DB/Migrations/20260416165930_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260416165930_InitialCreate.Designer.cs new file mode 100644 index 0000000..f262fe0 --- /dev/null +++ b/MQ.DB/Migrations/20260416165930_InitialCreate.Designer.cs @@ -0,0 +1,431 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260416165930_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("BaselineName") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeId") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260416165930_InitialCreate.cs b/MQ.DB/Migrations/20260416165930_InitialCreate.cs new file mode 100644 index 0000000..6de12d9 --- /dev/null +++ b/MQ.DB/Migrations/20260416165930_InitialCreate.cs @@ -0,0 +1,365 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AiModelHashes", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + UniqueHash = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AiModelHashes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "BaselineQuantDefinitions", + columns: table => new + { + BaselineQuantId = table.Column(type: "INTEGER", nullable: false), + BaselineName = table.Column(type: "TEXT", maxLength: 64, nullable: false), + DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), + DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.BaselineQuantId); + }); + + migrationBuilder.CreateTable( + name: "TensorCombos", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AttnKV = table.Column(type: "INTEGER", nullable: false), + AttnOutput = table.Column(type: "INTEGER", nullable: false), + AttnQ = table.Column(type: "INTEGER", nullable: false), + BaseQuant = table.Column(type: "INTEGER", nullable: false), + Embeddings = table.Column(type: "INTEGER", nullable: false), + FfnDown = table.Column(type: "INTEGER", nullable: false), + FfnUpGate = table.Column(type: "INTEGER", nullable: false), + LmHead = table.Column(type: "INTEGER", nullable: false), + MoeExperts = table.Column(type: "INTEGER", nullable: false), + MoeRouter = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TensorCombos", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AiBenchmarks", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + Ngl = table.Column(type: "INTEGER", nullable: false), + SizeBytes = table.Column(type: "INTEGER", nullable: false), + TokensPerSecond = table.Column(type: "REAL", nullable: false), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AiBenchmarks", x => x.Id); + table.ForeignKey( + name: "FK_AiBenchmarks_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AiBenchmarks_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "CategoryBenchmark", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + Category = table.Column(type: "INTEGER", nullable: false), + Kld = table.Column(type: "REAL", nullable: false), + Ppl = table.Column(type: "REAL", nullable: false), + PplError = table.Column(type: "REAL", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CategoryBenchmark", x => x.Id); + table.ForeignKey( + name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "LearnedBaselineTensorQuants", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + BaselineQuantId = table.Column(type: "INTEGER", nullable: false), + TensorWeightSchemeId = table.Column(type: "INTEGER", nullable: false), + TensorGroupId = table.Column(type: "INTEGER", nullable: false), + TensorName = table.Column(type: "TEXT", maxLength: 512, nullable: false), + FinalQuantType = table.Column(type: "TEXT", maxLength: 32, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_LearnedBaselineTensorQuants", x => x.Id); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "QuantizationRuns", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: true), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: false), + DurationMs = table.Column(type: "INTEGER", nullable: false), + Succeeded = table.Column(type: "INTEGER", nullable: false), + Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true), + OutputModelPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_QuantizationRuns", x => x.Id); + table.ForeignKey( + name: "FK_QuantizationRuns_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_QuantizationRuns_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_QuantizationRuns_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "BenchmarkRuns", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + CategoryBenchmarkId = table.Column(type: "TEXT", nullable: true), + Category = table.Column(type: "INTEGER", nullable: false), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: false), + DurationMs = table.Column(type: "INTEGER", nullable: false), + Succeeded = table.Column(type: "INTEGER", nullable: false), + Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_BenchmarkRuns", x => x.Id); + table.ForeignKey( + name: "FK_BenchmarkRuns_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_CategoryBenchmark_CategoryBenchmarkId", + column: x => x.CategoryBenchmarkId, + principalTable: "CategoryBenchmark", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_BenchmarkRuns_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_AiModelHashId_TensorComboId", + table: "AiBenchmarks", + columns: new[] { "AiModelHashId", "TensorComboId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_TensorComboId", + table: "AiBenchmarks", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_AiModelHashes_UniqueHash", + table: "AiModelHashes", + column: "UniqueHash"); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_BaselineName", + table: "BaselineQuantDefinitions", + column: "BaselineName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeId", + table: "BaselineQuantDefinitions", + column: "DefaultTensorSchemeId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeName", + table: "BaselineQuantDefinitions", + column: "DefaultTensorSchemeName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiBenchmarkId", + table: "BenchmarkRuns", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiBenchmarkId_Category", + table: "BenchmarkRuns", + columns: new[] { "AiBenchmarkId", "Category" }); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiModelHashId", + table: "BenchmarkRuns", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_CategoryBenchmarkId", + table: "BenchmarkRuns", + column: "CategoryBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_StartedUtc", + table: "BenchmarkRuns", + column: "StartedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_TensorComboId", + table: "BenchmarkRuns", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_CategoryBenchmark_AiBenchmarkId", + table: "CategoryBenchmark", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiBenchmarkId", + table: "LearnedBaselineTensorQuants", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorGroupId", + table: "LearnedBaselineTensorQuants", + columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId" }); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorName", + table: "LearnedBaselineTensorQuants", + columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_AiBenchmarkId", + table: "QuantizationRuns", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_AiModelHashId", + table: "QuantizationRuns", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_StartedUtc", + table: "QuantizationRuns", + column: "StartedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_TensorComboId", + table: "QuantizationRuns", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", + table: "TensorCombos", + columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BaselineQuantDefinitions"); + + migrationBuilder.DropTable( + name: "BenchmarkRuns"); + + migrationBuilder.DropTable( + name: "LearnedBaselineTensorQuants"); + + migrationBuilder.DropTable( + name: "QuantizationRuns"); + + migrationBuilder.DropTable( + name: "CategoryBenchmark"); + + migrationBuilder.DropTable( + name: "AiBenchmarks"); + + migrationBuilder.DropTable( + name: "AiModelHashes"); + + migrationBuilder.DropTable( + name: "TensorCombos"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs new file mode 100644 index 0000000..d760253 --- /dev/null +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -0,0 +1,428 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + partial class MagicQuantContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("BaselineName") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeId") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} From 19e036996e8c304ddaebf581957771a30e62726e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 16 Apr 2026 21:28:08 -0400 Subject: [PATCH 061/258] this works very well --- ... 20260416203656_InitialCreate.Designer.cs} | 2 +- ...ate.cs => 20260416203656_InitialCreate.cs} | 0 MQ.DB/Models/BaselineQuants.cs | 21 +++ MQ.DB/Models/TensorWeightScheme.cs | 8 +- MagicQuant/Commands/Evolution.cs | 70 ++++++---- MagicQuant/Helpers/RuntimeSearchSpace.cs | 123 +++++++++++++++-- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 25 +++- .../Services/LearnedBaselinePruningService.cs | 128 ++++++++++++++++++ 8 files changed, 335 insertions(+), 42 deletions(-) rename MQ.DB/Migrations/{20260416165930_InitialCreate.Designer.cs => 20260416203656_InitialCreate.Designer.cs} (99%) rename MQ.DB/Migrations/{20260416165930_InitialCreate.cs => 20260416203656_InitialCreate.cs} (100%) create mode 100644 MagicQuant/Services/LearnedBaselinePruningService.cs diff --git a/MQ.DB/Migrations/20260416165930_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260416203656_InitialCreate.Designer.cs similarity index 99% rename from MQ.DB/Migrations/20260416165930_InitialCreate.Designer.cs rename to MQ.DB/Migrations/20260416203656_InitialCreate.Designer.cs index f262fe0..5842b69 100644 --- a/MQ.DB/Migrations/20260416165930_InitialCreate.Designer.cs +++ b/MQ.DB/Migrations/20260416203656_InitialCreate.Designer.cs @@ -11,7 +11,7 @@ namespace MQ.DB.Migrations { [DbContext(typeof(MagicQuantContext))] - [Migration("20260416165930_InitialCreate")] + [Migration("20260416203656_InitialCreate")] partial class InitialCreate { /// diff --git a/MQ.DB/Migrations/20260416165930_InitialCreate.cs b/MQ.DB/Migrations/20260416203656_InitialCreate.cs similarity index 100% rename from MQ.DB/Migrations/20260416165930_InitialCreate.cs rename to MQ.DB/Migrations/20260416203656_InitialCreate.cs diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 0a02315..c9bfaf1 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -25,6 +25,24 @@ public record BaselineQuants( public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"], [TensorWeightScheme.Q4_K]); + + /*public static readonly BaselineQuants MXFP4_MOE = + new( + 4, + false, + ["MXFP4_MOE"], + [TensorWeightScheme.MXFP4], + new HybridQuant + { + BaseQuant = null!, + Tensors = TReg.All + .Select(g => new HybridTensor + { + TGroup = g, + TensorType = TensorWeightScheme.MXFP4 + }) + .ToList() + });*/ public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"], [TensorWeightScheme.IQ4_NL]); @@ -65,12 +83,15 @@ public record BaselineQuants( public static readonly BaselineQuants IQ2_XXS = new(12, true, ["IQ2_XXS"], [TensorWeightScheme.IQ2_XXS]); + + public static readonly ImmutableArray All = [ Q8_0, Q6_K, Q5_K, Q4_K_M, + // MXFP4_MOE, IQ4_NL, IQ4_XS, IQ3_S, diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 48eb9f8..9277d15 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -117,7 +117,7 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) public static readonly TensorWeightScheme BF16 = new(1, false, ["BF16", "BFLOAT16"], Array.Empty(), null, isEligibleForBaseline: false); - public static readonly TensorWeightScheme MXFP4 = + /*public static readonly TensorWeightScheme MXFP4 = new( 2, false, @@ -129,7 +129,7 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) TReg.MoeExperts }, 32, - isEligibleForBaseline: false); + isEligibleForBaseline: false);*/ public static readonly TensorWeightScheme Q8_0 = new(3, false, ["Q8_0"], Array.Empty(), null); @@ -255,7 +255,7 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) NULL, BF16, //F16, - MXFP4, + //MXFP4, Q8_0, Q6_K, Q5_K, @@ -271,7 +271,7 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) BF16, F16, F32, - MXFP4, + //MXFP4, Q8_0, Q6_K, Q5_K, diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index e29051d..ee53754 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -10,48 +10,52 @@ namespace MagicQuant.Commands; public class Evolution : ICommand { private const int BruteForceFinalCombinationThreshold = 1_000; - + public async Task Run(List args) { - if (args.Any(a => a.Name?.ToLower() == "help")) + if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) { ShowEvolutionHelp(); return; } - string? modelDirRaw = args.FirstOrDefault(a => a.Name?.ToLower() == "model-dir")?.Value; + string? modelDirRaw = args.FirstOrDefault(a => + string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; if (string.IsNullOrWhiteSpace(modelDirRaw)) { - string msg = "[red]Error:[/] Missing required argument [yellow]--model-dir[/]."; + const string msg = "[red]Error:[/] Missing required argument [yellow]--model-dir[/]."; AnsiConsole.MarkupLine(msg); ShowEvolutionHelp(); - throw new Exception(msg); + throw new InvalidOperationException("Missing required argument --model-dir."); } string fullModelPath = Path.GetFullPath(modelDirRaw); if (!Directory.Exists(fullModelPath)) { - string msg = $"[red]Error:[/] The directory [yellow]'{fullModelPath}'[/] does not exist."; + string msg = + $"[red]Error:[/] The directory [yellow]{Markup.Escape(fullModelPath)}[/] does not exist."; AnsiConsole.MarkupLine(msg); ShowEvolutionHelp(); - throw new Exception(msg); + throw new DirectoryNotFoundException($"The directory '{fullModelPath}' does not exist."); } var safeTensorFiles = Directory.GetFiles(fullModelPath, "*.safetensors", SearchOption.TopDirectoryOnly); if (safeTensorFiles.Length == 0) { - AnsiConsole.MarkupLine($"[red]Error:[/] No [yellow].safetensors[/] files found in [blue]{fullModelPath}[/]."); + AnsiConsole.MarkupLine( + $"[red]Error:[/] No [yellow].safetensors[/] files found in [blue]{Markup.Escape(fullModelPath)}[/]."); AnsiConsole.MarkupLine("[grey]Please ensure this is a valid HuggingFace model directory.[/]"); - throw new Exception(); + throw new InvalidOperationException("No .safetensors files were found in the provided model directory."); } Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); Cache.ForceRelearnBaselineTensorMappings = args.Any(a => string.Equals(a.Name, "relearn-baseline-mappings", StringComparison.OrdinalIgnoreCase)); + JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) @@ -59,16 +63,16 @@ public async Task Run(List args) AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); AnsiConsole.Write(new Rule("[yellow]Evolution Configuration[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"Model Path: [blue]{Cache.ModelDirectory}[/]"); - AnsiConsole.MarkupLine($"Output Path: [blue]{Cache.ModelMagicQuantDirectory}[/]"); - AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length}[/] safe tensors"); + AnsiConsole.MarkupLine($"Model Path: [blue]{Markup.Escape(Cache.ModelDirectory)}[/]"); + AnsiConsole.MarkupLine($"Output Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); + AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length:N0}[/] safe tensors"); if (string.IsNullOrEmpty(Cache.LlamaBin)) - AnsiConsole.MarkupLine("[yellow]Warning: Llama binaries path not set in Cache. (Did Initialization run?)[/]"); + AnsiConsole.MarkupLine("[yellow]Warning:[/] Llama binaries path not set in Cache. (Did Initialization run?)"); - Console.WriteLine("Acquiring unique model ID..."); + AnsiConsole.MarkupLine("[grey]Acquiring unique model ID...[/]"); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); - AnsiConsole.MarkupLine($"[green] Model ID Created/Found: {Cache.CurrentModelId}[/]"); + AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); var pyManager = new PythonManager(Cache.MagicQuantDirectory); var benchmarkService = new BenchmarkService(pyManager); @@ -77,7 +81,7 @@ public async Task Run(List args) if (Cache.ForceRelearnBaselineTensorMappings) { await quantizationService.InvalidateBaselineArtifactsAsync(); - AnsiConsole.MarkupLine("[yellow]Forced relearn is ON: pure baseline samples will be rebuilt and relearned.[/]"); + AnsiConsole.MarkupLine("[yellow]Forced relearn is ON:[/] pure baseline samples will be rebuilt and relearned."); } var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); @@ -158,6 +162,18 @@ await benchmarkService.RunAllBenchmarksAsync( var comboCountBefore = ComboCounter.CountAll(); + var learnedBaselinePruner = new LearnedBaselinePruningService(); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning"); + + AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning[/]") { Justification = Justify.Left }); + var learnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Learned-Baseline Pruning"); + + foreach (var note in learnedPruningResult.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Final Isolation Optimization"); AnsiConsole.Write(new Rule("[yellow]Final Isolation Optimization[/]") { Justification = Justify.Left }); @@ -167,7 +183,12 @@ await benchmarkService.RunAllBenchmarksAsync( foreach (var gd in isolationResult.GroupDetails.OrderBy(x => x.GroupName)) { - AnsiConsole.Write(new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") { Justification = Justify.Left }); + AnsiConsole.Write( + new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") + { + Justification = Justify.Left + }); + AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); AnsiConsole.MarkupLine($"[green]Winning scheme:[/] {Markup.Escape(gd.WinningScheme ?? "n/a")}"); AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); @@ -183,6 +204,8 @@ await benchmarkService.RunAllBenchmarksAsync( long predictedSizePruned = await dbService.PrunePredictedLargerThanQ8Async(mergedPlan); + AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {learnedPruningResult.GroupSchemeEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {learnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0}"); AnsiConsole.MarkupLine($"[green]Groups reduced to BF16-only:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); @@ -195,7 +218,7 @@ await benchmarkService.RunAllBenchmarksAsync( foreach (var note in isolationResult.Notes) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); - + long finalRemainingCombinationCount = await dbService.GetRemainingCombinationCountAsync(); AnsiConsole.MarkupLine($"[green]Final surviving combinations:[/] {finalRemainingCombinationCount:N0}"); @@ -220,7 +243,6 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($" [green]Completed:[/] {finalSummary.Completed:N0}"); AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {finalSummary.Skipped:N0}"); AnsiConsole.MarkupLine($" [red]Failed:[/] {finalSummary.Failed:N0}"); - AnsiConsole.MarkupLine("[yellow]Note:[/] Final model creation/export functionality is still being implemented."); } else @@ -228,15 +250,9 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine("[yellow]Note:[/] Final model creation/export functionality is still being implemented."); throw new InvalidOperationException( - $"Prediction engine not created yet. " + - $"Final surviving combinations were {finalRemainingCombinationCount:N0}, " + + $"Prediction engine not created yet. Final surviving combinations were {finalRemainingCombinationCount:N0}, " + $"which is above the brute-force threshold of {BruteForceFinalCombinationThreshold:N0}."); } - - AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count after rule pruning:[/] {comboCountAfterRulePruning:N0}"); - AnsiConsole.MarkupLine($"[green]Predicted-size combo removals:[/] {predictedSizePruned:N0}"); - AnsiConsole.MarkupLine($"[green]Final surviving combinations:[/] {finalRemainingCombinationCount:N0}"); } private void ShowEvolutionHelp() @@ -254,4 +270,4 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } -} +} \ No newline at end of file diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index d5eb449..7d004b6 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -1,16 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; using MQ.DB.Models; namespace MagicQuant.Helpers; +public sealed class RuntimeLearnedBaselineBanInfo +{ + public TensorWeightScheme Scheme { get; init; } = default!; + public IReadOnlyList MissingBaselines { get; init; } = Array.Empty(); +} + public static class RuntimeSearchSpace { private static readonly Dictionary> ExplicitSchemeBansByGroup = new(); + private static readonly Dictionary>> LearnedBaselineMissingByGroupAndScheme = new(); private static readonly HashSet DisabledCombinationBaselineIds = new(); private static readonly HashSet Bf16SuppressedTensorChoiceGroupIds = new(); public static void ResetForNewModel() { ExplicitSchemeBansByGroup.Clear(); + LearnedBaselineMissingByGroupAndScheme.Clear(); DisabledCombinationBaselineIds.Clear(); Bf16SuppressedTensorChoiceGroupIds.Clear(); TensorWeightScheme.ResetAllRuntimeBans(); @@ -18,7 +29,8 @@ public static void ResetForNewModel() public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) { - if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || + scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) return; if (!ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set)) @@ -33,10 +45,40 @@ public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme schem scheme.BannedGroups.Add(group); } + public static void BanSchemeForGroupByLearnedBaselineAbsence( + TensorGroup group, + TensorWeightScheme scheme, + BaselineQuants sourceBaseline) + { + if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || + scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + return; + + BanSchemeForGroup(group, scheme); + + if (!LearnedBaselineMissingByGroupAndScheme.TryGetValue(group.UniqueId, out var byScheme)) + { + byScheme = new Dictionary>(); + LearnedBaselineMissingByGroupAndScheme[group.UniqueId] = byScheme; + } + + if (!byScheme.TryGetValue(scheme.UniqueId, out var baselineIds)) + { + baselineIds = new HashSet(); + byScheme[scheme.UniqueId] = baselineIds; + } + + baselineIds.Add(sourceBaseline.UniqueId); + } + public static void BanAllExplicitTensorSchemesForGroup(TensorGroup group) { - foreach (var scheme in TensorWeightScheme.All_Allowed_Hybrid_Quants.Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId && x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId)) + foreach (var scheme in TensorWeightScheme.All_Allowed_Hybrid_Quants + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId && + x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId)) + { BanSchemeForGroup(group, scheme); + } } public static IReadOnlyList GetRuntimeExplicitBansForGroup(TensorGroup group) @@ -44,12 +86,16 @@ public static IReadOnlyList GetRuntimeExplicitBansForGroup(T if (!ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set)) return Array.Empty(); - return TensorWeightScheme.All_Allowed_Hybrid_Quants.Where(x => set.Contains(x.UniqueId)).OrderBy(x => x.UniqueId).ToList(); + return TensorWeightScheme.All_Allowed_Hybrid_Quants + .Where(x => set.Contains(x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); } public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeightScheme scheme) { - return ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set) && set.Contains(scheme.UniqueId); + return ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set) && + set.Contains(scheme.UniqueId); } public static bool IsGroupExplicitQuantBanned(TensorGroup group) @@ -64,12 +110,70 @@ public static bool IsGroupExplicitQuantBanned(TensorGroup group) public static IReadOnlyList GetGroupsWithExplicitQuantBanned() { - return TReg.All.Where(IsGroupExplicitQuantBanned).OrderBy(x => x.UniqueId).ToList(); + return TReg.All + .Where(IsGroupExplicitQuantBanned) + .OrderBy(x => x.UniqueId) + .ToList(); + } + + public static bool HasLearnedBaselineMissingPrunesForGroup(TensorGroup group) + { + return LearnedBaselineMissingByGroupAndScheme.TryGetValue(group.UniqueId, out var byScheme) && + byScheme.Count > 0; } - public static void SuppressBf16TensorChoice(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); - public static bool IsBf16TensorChoiceSuppressed(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Contains(group.UniqueId); - public static IReadOnlyList GetBf16SuppressedGroups() => TReg.All.Where(x => Bf16SuppressedTensorChoiceGroupIds.Contains(x.UniqueId)).OrderBy(x => x.UniqueId).ToList(); + public static IReadOnlyList GetGroupsWithLearnedBaselineMissingPrunes() + { + return TReg.All + .Where(HasLearnedBaselineMissingPrunesForGroup) + .OrderBy(x => x.UniqueId) + .ToList(); + } + + public static IReadOnlyList GetLearnedBaselineMissingPrunedSchemesForGroup( + TensorGroup group) + { + if (!LearnedBaselineMissingByGroupAndScheme.TryGetValue(group.UniqueId, out var byScheme)) + return Array.Empty(); + + var result = new List(); + + foreach (var kvp in byScheme.OrderBy(x => x.Key)) + { + var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants + .FirstOrDefault(x => x.UniqueId == kvp.Key); + + if (scheme == null) + continue; + + var baselines = kvp.Value + .OrderBy(x => x) + .Select(BaselineQuants.FromId) + .ToList(); + + result.Add(new RuntimeLearnedBaselineBanInfo + { + Scheme = scheme, + MissingBaselines = baselines + }); + } + + return result; + } + + public static void SuppressBf16TensorChoice(TensorGroup group) + => Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); + + public static bool IsBf16TensorChoiceSuppressed(TensorGroup group) + => Bf16SuppressedTensorChoiceGroupIds.Contains(group.UniqueId); + + public static IReadOnlyList GetBf16SuppressedGroups() + { + return TReg.All + .Where(x => Bf16SuppressedTensorChoiceGroupIds.Contains(x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + } public static IReadOnlyList GetActiveCombinationBaselines() { @@ -93,5 +197,6 @@ public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allo return true; } - public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) => DisabledCombinationBaselineIds.Contains(baseline.UniqueId); + public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) + => DisabledCombinationBaselineIds.Contains(baseline.UniqueId); } \ No newline at end of file diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index f540c24..2306124 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -1,3 +1,5 @@ +using System; +using System.Linq; using System.Numerics; using MQ.DB; using MQ.DB.Models; @@ -45,11 +47,31 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc AnsiConsole.MarkupLine($" [yellow]- {group.Name}[/]"); } + var learnedPrunedGroups = RuntimeSearchSpace.GetGroupsWithLearnedBaselineMissingPrunes(); + if (learnedPrunedGroups.Count > 0) + { + AnsiConsole.MarkupLine($"[yellow]Learned-baseline-pruned groups:[/] {learnedPrunedGroups.Count}"); + + foreach (var group in learnedPrunedGroups) + { + var learned = RuntimeSearchSpace.GetLearnedBaselineMissingPrunedSchemesForGroup(group); + + var parts = learned.Select(x => + $"{x.Scheme.Names[0]} <= {string.Join("/", x.MissingBaselines.Select(b => b.Names[0]))}"); + + AnsiConsole.MarkupLine( + $" [yellow]- {Markup.Escape(group.Name)}[/] :: [grey]{Markup.Escape(string.Join(", ", parts))}[/]"); + } + } + var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); foreach (var baseline in activeBaselines) { - AnsiConsole.Write(new Rule($"[blue]Base: {Markup.Escape(string.Join("/", baseline.Names))}[/]") { Justification = Justify.Left }); + AnsiConsole.Write(new Rule($"[blue]Base: {Markup.Escape(string.Join("/", baseline.Names))}[/]") + { + Justification = Justify.Left + }); var allowed = ComboLogic.GetAllowedSchemeIdsPerGroup(baseline); BigInteger baseCount = BigInteger.One; @@ -73,6 +95,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc unusedIds.Contains(group.UniqueId) ? "unused->NULL" : RuntimeSearchSpace.IsGroupExplicitQuantBanned(group) ? "BF16-only" : RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group) ? "BF16-suppressed" : + RuntimeSearchSpace.HasLearnedBaselineMissingPrunesForGroup(group) ? "learned-pruned" : "variable"; AnsiConsole.MarkupLine( diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs new file mode 100644 index 0000000..af5b803 --- /dev/null +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MagicQuant.Helpers; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; + +namespace MagicQuant.Services; + +public sealed class LearnedBaselinePruningResult +{ + public int GroupSchemeEliminations { get; set; } + public int BaselinesSkippedWithoutLearnedRows { get; set; } + public List Notes { get; } = new(); +} + +public sealed class LearnedBaselinePruningService +{ + private readonly record struct LearnedRowKey(byte BaselineQuantId, byte TensorWeightSchemeId, byte TensorGroupId); + + public async Task AnalyzeAndApplyAsync(CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); + + var result = new LearnedBaselinePruningResult(); + + await using var db = new MagicQuantContext(); + + var aiModelHashId = await db.AiModelHashes + .AsNoTracking() + .Where(x => x.UniqueHash == Cache.CurrentModelId) + .Select(x => (uint?)x.Id) + .FirstOrDefaultAsync(ct); + + if (aiModelHashId == null) + throw new InvalidOperationException( + $"AiModelHash row was not found for current model id '{Cache.CurrentModelId}'."); + + var learnedRows = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == aiModelHashId.Value) + .Select(x => new LearnedRowKey( + x.BaselineQuantId, + x.TensorWeightSchemeId, + x.TensorGroupId)) + .ToListAsync(ct); + + if (learnedRows.Count == 0) + { + result.Notes.Add( + "Learned-baseline pruning skipped because no LearnedBaselineTensorQuants rows existed for the current model."); + return result; + } + + var learnedRowSet = learnedRows.ToHashSet(); + var baselinesWithAnyLearnedRows = learnedRows + .Select(x => x.BaselineQuantId) + .ToHashSet(); + + var skippedBaselineNotes = new HashSet(); + var unusedIds = Cache.UnusedTensorGroups + .Select(x => x.UniqueId) + .ToHashSet(); + + var schemeOwnerById = BaselineQuants.All + .SelectMany(b => b.TensorWeightSchemes.Select(s => new + { + SchemeId = s.UniqueId, + Baseline = b + })) + .ToDictionary(x => x.SchemeId, x => x.Baseline); + + var explicitSchemes = TensorWeightScheme.All_Allowed_Hybrid_Quants + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .OrderBy(x => x.UniqueId) + .ToList(); + + foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) + { + if (unusedIds.Contains(group.UniqueId)) + continue; + + foreach (var scheme in explicitSchemes) + { + if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) + continue; + + if (!schemeOwnerById.TryGetValue(scheme.UniqueId, out var owningBaseline)) + continue; + + if (!baselinesWithAnyLearnedRows.Contains(owningBaseline.UniqueId)) + { + if (skippedBaselineNotes.Add(owningBaseline.UniqueId)) + { + result.BaselinesSkippedWithoutLearnedRows++; + + result.Notes.Add( + $"Learned-baseline pruning skipped for baseline '{owningBaseline.Names[0]}' because no learned rows existed for the current model."); + } + + continue; + } + + var lookup = new LearnedRowKey( + owningBaseline.UniqueId, + scheme.UniqueId, + group.UniqueId); + + if (learnedRowSet.Contains(lookup)) + continue; + + RuntimeSearchSpace.BanSchemeForGroupByLearnedBaselineAbsence(group, scheme, owningBaseline); + result.GroupSchemeEliminations++; + + result.Notes.Add( + $"Learned-baseline prune: '{scheme.Names[0]}' removed for '{group.Name}' because baseline '{owningBaseline.Names[0]}' learned zero matching tensors in that group."); + } + } + + return result; + } +} \ No newline at end of file From 4f1952e711899e770e62c4bfddf9427311030a28 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 17 Apr 2026 11:25:55 -0400 Subject: [PATCH 062/258] changing how getting smallest model --- MQ.DB/Models/TensorWeightScheme.cs | 25 +++++++++------------ MagicQuant/Helpers/TensorConfigGenerator.cs | 4 ++-- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 9277d15..7369a1e 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -12,7 +12,6 @@ public sealed class TensorWeightScheme public ImmutableArray Names { get; } public List BannedGroups { get; } public ushort? BlockNeo { get; } - public bool IsSmallest { get; } public bool IsEligibleForBaseline { get; } private TensorWeightScheme( @@ -21,14 +20,12 @@ private TensorWeightScheme( ImmutableArray names, IEnumerable bannedGroups, ushort? blockNeo, - bool isSmallest = false, bool isEligibleForBaseline = true) { UniqueId = uniqueId; RequiresImatrix = requiresImatrix; Names = names; BlockNeo = blockNeo; - IsSmallest = isSmallest; IsEligibleForBaseline = isEligibleForBaseline; var distinctGroups = bannedGroups @@ -63,7 +60,6 @@ public static void ValidateSmallestConfiguration() .Where(x => x.UniqueId != F16.UniqueId) .Where(x => x.UniqueId != F32.UniqueId) .Where(x => !x.RequiresImatrix) - .Where(x => x.IsSmallest) .ToList(); if (nonImatrixSmallest.Count != 1) @@ -77,17 +73,16 @@ public static void ValidateSmallestConfiguration() } } - public static TensorWeightScheme GetSmallestNonImatrix() - { - ValidateSmallestConfiguration(); - return All_Allowed_Hybrid_Quants - .Where(x => x.UniqueId != NULL.UniqueId) - .Where(x => x.UniqueId != BF16.UniqueId) - .Where(x => x.UniqueId != F16.UniqueId) - .Where(x => x.UniqueId != F32.UniqueId) - .Where(x => !x.RequiresImatrix) - .Single(x => x.IsSmallest); + /// + /// This labels the models that're supposed to quantize the smallest + /// in order. Top of array being the smallest, the further down, + /// it becomes larger in order of expected quantization size. + /// + /// + public static TensorWeightScheme[] GetSmallestInOrder() + { + return [IQ4_XS, IQ4_NL, Q4_K, Q6_K, Q8_0]; } public static TensorWeightScheme GetCurrentNativePrecisionScheme() @@ -141,7 +136,7 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) new(5, false, ["Q5_K"], new[] { TReg.MoeRouter }, 256); public static readonly TensorWeightScheme IQ4_XS = - new(6, false, ["IQ4_XS"], new[] { TReg.MoeRouter }, 32, true); + new(6, false, ["IQ4_XS"], new[] { TReg.MoeRouter }, 32); public static readonly TensorWeightScheme IQ4_NL = new(7, false, ["IQ4_NL"], new[] { TReg.MoeRouter }, 32); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 6a2cbb2..8bbdae2 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -67,7 +67,7 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( result.BaseOnlyIsolationCount++; - var smallest = TensorWeightScheme.GetSmallestNonImatrix(); + var smallest = TensorWeightScheme.GetSmallestInOrder().FirstOrDefault(); foreach (var group in activeGroups) { @@ -123,7 +123,7 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var result = new RequiredSampleGenerationResult(); var carrier = BaselineQuants.Q8_0; - var smallest = TensorWeightScheme.GetSmallestNonImatrix(); + var smallest = TensorWeightScheme.GetSmallestInOrder().FirstOrDefault(); var schemes = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) From 55223d832a1423d236e8ec2058c9ee694a3fa338 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 12:25:39 -0400 Subject: [PATCH 063/258] Use learned final tensor types for baseline group pruning --- MQ.DB/Models/TensorWeightScheme.cs | 38 +++++--- MagicQuant/Commands/Evolution.cs | 29 +++---- MagicQuant/Helpers/TensorConfigGenerator.cs | 36 ++++++-- .../Services/IsolationOptimizationService.cs | 12 ++- .../Services/LearnedBaselinePruningService.cs | 87 ++++++++++++++++--- 5 files changed, 157 insertions(+), 45 deletions(-) diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 7369a1e..eb1c64e 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -54,22 +54,34 @@ public static void ResetAllRuntimeBans() public static void ValidateSmallestConfiguration() { - var nonImatrixSmallest = All_Allowed_Hybrid_Quants - .Where(x => x.UniqueId != NULL.UniqueId) - .Where(x => x.UniqueId != BF16.UniqueId) - .Where(x => x.UniqueId != F16.UniqueId) - .Where(x => x.UniqueId != F32.UniqueId) - .Where(x => !x.RequiresImatrix) + var ordered = GetSmallestInOrder(); + + if (ordered.Length == 0) + throw new InvalidOperationException("TensorWeightScheme.GetSmallestInOrder() must return at least one item."); + + var duplicateIds = ordered + .GroupBy(x => x.UniqueId) + .Where(g => g.Count() > 1) + .Select(g => g.First().Names[0]) .ToList(); - if (nonImatrixSmallest.Count != 1) + if (duplicateIds.Count > 0) { - string found = nonImatrixSmallest.Count == 0 - ? "none" - : string.Join(", ", nonImatrixSmallest.Select(x => x.Names[0])); + throw new InvalidOperationException( + $"TensorWeightScheme.GetSmallestInOrder() contains duplicates: {string.Join(", ", duplicateIds)}"); + } + + var knownIds = All.Select(x => x.UniqueId).ToHashSet(); + var unknown = ordered + .Where(x => !knownIds.Contains(x.UniqueId)) + .Select(x => x.Names[0]) + .Distinct() + .ToList(); + if (unknown.Count > 0) + { throw new InvalidOperationException( - $"Exactly one non-imatrix TensorWeightScheme must have IsSmallest=true. Found: {found}"); + $"TensorWeightScheme.GetSmallestInOrder() includes unknown schemes: {string.Join(", ", unknown)}"); } } @@ -82,7 +94,7 @@ public static void ValidateSmallestConfiguration() /// public static TensorWeightScheme[] GetSmallestInOrder() { - return [IQ4_XS, IQ4_NL, Q4_K, Q6_K, Q8_0]; + return [IQ4_XS, IQ4_NL, Q4_K, Q6_K, Q8_0, BF16, F16, F32]; } public static TensorWeightScheme GetCurrentNativePrecisionScheme() @@ -280,4 +292,4 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) IQ2_XXS, Q4_K ]; -} \ No newline at end of file +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index ee53754..3c9726b 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -114,6 +114,19 @@ await benchmarkService.RunAllBenchmarksAsync( var dbService = new QuantDatabaseService(); await dbService.InitializeAsync(); + var comboCountBefore = ComboCounter.CountAll(); + var learnedBaselinePruner = new LearnedBaselinePruningService(); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning"); + + AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning[/]") { Justification = Justify.Left }); + var learnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Learned-Baseline Pruning"); + + foreach (var note in learnedPruningResult.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + AnsiConsole.Write(new Rule("[yellow]Initial Isolation Startup Samples[/]") { Justification = Justify.Left }); var initialPlan = TensorConfigGenerator.GenerateInitialIsolationSamplePlan(Cache.UnusedTensorGroups); @@ -160,20 +173,6 @@ await benchmarkService.RunAllBenchmarksAsync( var mergedPlan = initialPlan.MergeWith(continuationPlan); - var comboCountBefore = ComboCounter.CountAll(); - - var learnedBaselinePruner = new LearnedBaselinePruningService(); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning"); - - AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning[/]") { Justification = Justify.Left }); - var learnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Learned-Baseline Pruning"); - - foreach (var note in learnedPruningResult.Notes) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Final Isolation Optimization"); AnsiConsole.Write(new Rule("[yellow]Final Isolation Optimization[/]") { Justification = Justify.Left }); @@ -270,4 +269,4 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 8bbdae2..378905f 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -67,11 +67,10 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( result.BaseOnlyIsolationCount++; - var smallest = TensorWeightScheme.GetSmallestInOrder().FirstOrDefault(); - foreach (var group in activeGroups) { - if (smallest.IsBannedFor(group)) + var smallest = GetSmallestAllowedProbeSchemeForGroup(group); + if (smallest == null) continue; var quant = HybridQuant.CreateBlanket( @@ -123,7 +122,6 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var result = new RequiredSampleGenerationResult(); var carrier = BaselineQuants.Q8_0; - var smallest = TensorWeightScheme.GetSmallestInOrder().FirstOrDefault(); var schemes = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) @@ -133,9 +131,11 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample foreach (var group in activeGroups) { + var smallest = GetSmallestAllowedProbeSchemeForGroup(group); + foreach (var scheme in schemes) { - if (scheme.UniqueId == smallest.UniqueId) + if (smallest != null && scheme.UniqueId == smallest.UniqueId) continue; if (scheme.IsBannedFor(group)) @@ -292,6 +292,30 @@ public static IEnumerable> GenerateTensorConfigBatches( producer.GetAwaiter().GetResult(); } + private static TensorWeightScheme? GetSmallestAllowedProbeSchemeForGroup(TensorGroup group) + { + var allowedIds = TensorWeightScheme.All_Allowed_Hybrid_Quants + .Select(x => x.UniqueId) + .ToHashSet(); + + foreach (var scheme in TensorWeightScheme.GetSmallestInOrder()) + { + if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || + scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + continue; + + if (!allowedIds.Contains(scheme.UniqueId)) + continue; + + if (scheme.IsBannedFor(group) || RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) + continue; + + return scheme; + } + + return null; + } + private static int GetThreadCountSafe() { int tc = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; @@ -310,4 +334,4 @@ private static int ComputeWorkerThreads(int threadCount) return Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 9e3ab05..312c2c7 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -264,6 +264,16 @@ public async Task AnalyzeAndApplyFinalAsync( $"{candidate.Scheme.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}%"); } + foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedSchemesForGroup(group)) + { + var sourceBaselines = string.Join( + ", ", + banInfo.MissingBaselines.Select(x => x.Names[0])); + + decision.Candidates.Add( + $"[pruned-early] {banInfo.Scheme.Names[0]} removed by learned-baseline mapping for this group (no matching tensor weights in baseline(s): {sourceBaselines})."); + } + result.GroupDetails.Add(decision); } @@ -537,4 +547,4 @@ private sealed class CategorySnapshot public double Ppl { get; set; } public double PplError { get; set; } } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index af5b803..0a56aa2 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -20,7 +20,11 @@ public sealed class LearnedBaselinePruningResult public sealed class LearnedBaselinePruningService { - private readonly record struct LearnedRowKey(byte BaselineQuantId, byte TensorWeightSchemeId, byte TensorGroupId); + private readonly record struct LearnedRow( + byte BaselineQuantId, + byte TensorWeightSchemeId, + byte TensorGroupId, + string FinalQuantType); public async Task AnalyzeAndApplyAsync(CancellationToken ct = default) { @@ -44,10 +48,11 @@ public async Task AnalyzeAndApplyAsync(Cancellatio var learnedRows = await db.LearnedBaselineTensorQuants .AsNoTracking() .Where(x => x.AiModelHashId == aiModelHashId.Value) - .Select(x => new LearnedRowKey( + .Select(x => new LearnedRow( x.BaselineQuantId, x.TensorWeightSchemeId, - x.TensorGroupId)) + x.TensorGroupId, + x.FinalQuantType)) .ToListAsync(ct); if (learnedRows.Count == 0) @@ -57,11 +62,31 @@ public async Task AnalyzeAndApplyAsync(Cancellatio return result; } - var learnedRowSet = learnedRows.ToHashSet(); var baselinesWithAnyLearnedRows = learnedRows .Select(x => x.BaselineQuantId) .ToHashSet(); + var aliasToSchemeIds = BuildAliasToSchemeIds(); + + var effectiveSchemesByBaselineAndGroup = new Dictionary<(byte BaselineId, byte GroupId), HashSet>(); + + foreach (var row in learnedRows) + { + var key = (row.BaselineQuantId, row.TensorGroupId); + + if (!effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var set)) + { + set = new HashSet(); + effectiveSchemesByBaselineAndGroup[key] = set; + } + + if (aliasToSchemeIds.TryGetValue(CanonicalizeQuantToken(row.FinalQuantType), out var resolvedIds)) + { + foreach (var resolvedId in resolvedIds) + set.Add(resolvedId); + } + } + var skippedBaselineNotes = new HashSet(); var unusedIds = Cache.UnusedTensorGroups .Select(x => x.UniqueId) @@ -107,12 +132,16 @@ public async Task AnalyzeAndApplyAsync(Cancellatio continue; } - var lookup = new LearnedRowKey( - owningBaseline.UniqueId, - scheme.UniqueId, - group.UniqueId); + var key = (owningBaseline.UniqueId, group.UniqueId); + var effectiveForGroup = effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var found) + ? found + : null; + + bool hasAnyConnectedMapping = effectiveForGroup != null && + owningBaseline.TensorWeightSchemes.Any(connectedScheme => + effectiveForGroup.Contains(connectedScheme.UniqueId)); - if (learnedRowSet.Contains(lookup)) + if (hasAnyConnectedMapping) continue; RuntimeSearchSpace.BanSchemeForGroupByLearnedBaselineAbsence(group, scheme, owningBaseline); @@ -125,4 +154,42 @@ public async Task AnalyzeAndApplyAsync(Cancellatio return result; } -} \ No newline at end of file + + private static Dictionary> BuildAliasToSchemeIds() + { + var map = new Dictionary>(StringComparer.Ordinal); + + foreach (var scheme in TensorWeightScheme.All) + { + if (scheme.Names.IsDefaultOrEmpty) + continue; + + foreach (var alias in scheme.Names) + { + var token = CanonicalizeQuantToken(alias); + + if (!map.TryGetValue(token, out var ids)) + { + ids = new HashSet(); + map[token] = ids; + } + + ids.Add(scheme.UniqueId); + } + } + + return map; + } + + private static string CanonicalizeQuantToken(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return "UNKNOWN"; + + return value + .Trim() + .Replace("-", "_") + .Replace(" ", string.Empty) + .ToUpperInvariant(); + } +} From 21d18a84a8735bc5219c34680586d4abc674ddcc Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 13:12:33 -0400 Subject: [PATCH 064/258] Fix BF16 suppression flags to reflect final allowed state --- MagicQuant/Helpers/RuntimeSearchSpace.cs | 33 ++++++++++++++----- .../Services/IsolationOptimizationService.cs | 23 ++++++++++--- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 7d004b6..bf11b1d 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -100,12 +100,7 @@ public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeight public static bool IsGroupExplicitQuantBanned(TensorGroup group) { - var explicitSchemes = TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .ToList(); - - return explicitSchemes.All(x => x.IsBannedFor(group)); + return !HasAnyExplicitSchemeAllowed(group); } public static IReadOnlyList GetGroupsWithExplicitQuantBanned() @@ -165,16 +160,36 @@ public static void SuppressBf16TensorChoice(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); public static bool IsBf16TensorChoiceSuppressed(TensorGroup group) - => Bf16SuppressedTensorChoiceGroupIds.Contains(group.UniqueId); + { + // BF16 suppression is only meaningful while at least one explicit tensor scheme remains. + // If explicit schemes are all banned, BF16 becomes the only viable tensor choice. + return Bf16SuppressedTensorChoiceGroupIds.Contains(group.UniqueId) && + HasAnyExplicitSchemeAllowed(group); + } public static IReadOnlyList GetBf16SuppressedGroups() { return TReg.All - .Where(x => Bf16SuppressedTensorChoiceGroupIds.Contains(x.UniqueId)) + .Where(IsBf16TensorChoiceSuppressed) .OrderBy(x => x.UniqueId) .ToList(); } + public static (bool ExplicitAllowed, bool Bf16Allowed) GetFinalAllowedQuantFamiliesForGroup(TensorGroup group) + { + bool explicitAllowed = HasAnyExplicitSchemeAllowed(group); + bool bf16Allowed = !IsBf16TensorChoiceSuppressed(group); + return (explicitAllowed, bf16Allowed); + } + + private static bool HasAnyExplicitSchemeAllowed(TensorGroup group) + { + return TensorWeightScheme.All_Allowed_Hybrid_Quants + .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Any(x => !x.IsBannedFor(group)); + } + public static IReadOnlyList GetActiveCombinationBaselines() { return BaselineQuants.All @@ -199,4 +214,4 @@ public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allo public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) => DisabledCombinationBaselineIds.Contains(baseline.UniqueId); -} \ No newline at end of file +} diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 312c2c7..d5c7248 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -243,8 +243,7 @@ public async Task AnalyzeAndApplyFinalAsync( if (candidates.Count == 0) { - decision.ExplicitQuantBanned = RuntimeSearchSpace.IsGroupExplicitQuantBanned(group); - decision.Bf16Suppressed = RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group); + PopulateFinalGroupFlags(group, decision, result); result.GroupDetails.Add(decision); continue; } @@ -255,8 +254,7 @@ public async Task AnalyzeAndApplyFinalAsync( decision.WinningSizeBytes = winner.SizeBytes; decision.WinningKld = winner.Kld; decision.WinningPplDelta = winner.PplDeltaPercent; - decision.ExplicitQuantBanned = RuntimeSearchSpace.IsGroupExplicitQuantBanned(group); - decision.Bf16Suppressed = RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group); + PopulateFinalGroupFlags(group, decision, result); foreach (var candidate in candidates.OrderBy(x => x.SizeBytes)) { @@ -309,6 +307,23 @@ public async Task AnalyzeAndApplyFinalAsync( return result; } + private static void PopulateFinalGroupFlags( + TensorGroup group, + IsolationGroupDecision decision, + IsolationOptimizationResult result) + { + var (explicitAllowed, bf16Allowed) = RuntimeSearchSpace.GetFinalAllowedQuantFamiliesForGroup(group); + + decision.ExplicitQuantBanned = !explicitAllowed; + decision.Bf16Suppressed = !bf16Allowed; + + if (!explicitAllowed && !bf16Allowed) + { + result.Notes.Add( + $"[invariant-warning] Invalid final quant-family state for '{group.Name}': neither explicit nor BF16 is allowed."); + } + } + private static List FilterSurvivors(TensorGroup group, List candidates) { return candidates From 94bc16d435f85a2e5d2881c2073bb38b89cf4607 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 13:39:24 -0400 Subject: [PATCH 065/258] Align displayed BF16 suppression with BF16-only final groups --- MagicQuant/Services/IsolationOptimizationService.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index d5c7248..caf42cb 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -302,7 +302,7 @@ public async Task AnalyzeAndApplyFinalAsync( } result.ExplicitQuantBannedGroups = RuntimeSearchSpace.GetGroupsWithExplicitQuantBanned().Count; - result.Bf16SuppressedGroups = RuntimeSearchSpace.GetBf16SuppressedGroups().Count; + result.Bf16SuppressedGroups = result.GroupDetails.Count(x => x.Bf16Suppressed); return result; } @@ -315,7 +315,9 @@ private static void PopulateFinalGroupFlags( var (explicitAllowed, bf16Allowed) = RuntimeSearchSpace.GetFinalAllowedQuantFamiliesForGroup(group); decision.ExplicitQuantBanned = !explicitAllowed; - decision.Bf16Suppressed = !bf16Allowed; + // Reporting flag: "BF16 suppressed" is surfaced as "group forced away from explicit quant", + // i.e., BF16-only final state. This keeps the displayed flag aligned with final outcomes. + decision.Bf16Suppressed = decision.ExplicitQuantBanned; if (!explicitAllowed && !bf16Allowed) { From e90d51b1cfc9660545688e98958c00789be36dc2 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 14:58:16 -0400 Subject: [PATCH 066/258] Refactor isolation bad-trade pruning to stable frontier walk --- .../Services/IsolationOptimizationService.cs | 177 ++++++++++++------ 1 file changed, 122 insertions(+), 55 deletions(-) diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index caf42cb..52fd9a1 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -315,9 +315,7 @@ private static void PopulateFinalGroupFlags( var (explicitAllowed, bf16Allowed) = RuntimeSearchSpace.GetFinalAllowedQuantFamiliesForGroup(group); decision.ExplicitQuantBanned = !explicitAllowed; - // Reporting flag: "BF16 suppressed" is surfaced as "group forced away from explicit quant", - // i.e., BF16-only final state. This keeps the displayed flag aligned with final outcomes. - decision.Bf16Suppressed = decision.ExplicitQuantBanned; + decision.Bf16Suppressed = !bf16Allowed; if (!explicitAllowed && !bf16Allowed) { @@ -339,10 +337,7 @@ private static void ApplyDominanceElimination( List candidates, IsolationOptimizationResult result) { - var explicitCandidates = candidates - .Where(x => x.Scheme.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Where(x => !x.Scheme.RequiresImatrix) - .ToList(); + var explicitCandidates = GetActiveExplicitCandidates(group, candidates); for (int i = 0; i < explicitCandidates.Count; i++) { @@ -356,11 +351,11 @@ private static void ApplyDominanceElimination( bool sameOrSmaller = a.SizeBytes <= b.SizeBytes; bool kldNoWorse = a.Kld <= b.Kld + IsolationPruningConfig.FloatingPointEpsilon; - bool pplNoWorse = a.PplDeltaPercent <= b.PplDeltaPercent + IsolationPruningConfig.FloatingPointEpsilon; + bool pplNoWorse = Math.Abs(a.PplDeltaPercent) <= Math.Abs(b.PplDeltaPercent) + IsolationPruningConfig.FloatingPointEpsilon; bool strictlyBetter = a.Kld + IsolationPruningConfig.FloatingPointEpsilon < b.Kld || - a.PplDeltaPercent + IsolationPruningConfig.FloatingPointEpsilon < b.PplDeltaPercent || + Math.Abs(a.PplDeltaPercent) + IsolationPruningConfig.FloatingPointEpsilon < Math.Abs(b.PplDeltaPercent) || a.SizeBytes < b.SizeBytes; if (sameOrSmaller && kldNoWorse && pplNoWorse && strictlyBetter) @@ -383,74 +378,146 @@ private static void ApplyBadTradeElimination( List candidates, IsolationOptimizationResult result) { - var explicitCandidates = candidates + var activeCandidates = GetActiveExplicitCandidates(group, candidates); + if (activeCandidates.Count <= 1) + return; + + var sizeBuckets = BuildSizeBuckets(activeCandidates); + if (sizeBuckets.Count == 0) + return; + + var acceptedAnchor = SelectBestBucketSurvivor(sizeBuckets[0]); + if (acceptedAnchor == null) + return; + + for (int i = 1; i < sizeBuckets.Count; i++) + { + var bucketSurvivors = new List(); + + foreach (var candidate in sizeBuckets[i]) + { + if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, candidate.Scheme)) + continue; + + if (ShouldEliminateAsBadTrade(acceptedAnchor, candidate, out var reason)) + { + RuntimeSearchSpace.BanSchemeForGroup(group, candidate.Scheme); + result.BadTradeEliminations++; + result.Notes.Add( + $"Bad trade elimination: '{candidate.Scheme.Names[0]}' removed vs accepted anchor '{acceptedAnchor.Scheme.Names[0]}' for '{group.Name}'. {reason}"); + continue; + } + + bucketSurvivors.Add(candidate); + } + + var promotedAnchor = SelectBestBucketSurvivor(bucketSurvivors); + if (promotedAnchor != null) + acceptedAnchor = promotedAnchor; + } + } + + private static List GetActiveExplicitCandidates(TensorGroup group, List candidates) + { + return candidates .Where(x => x.Scheme.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) .Where(x => !x.Scheme.RequiresImatrix) - .OrderBy(x => x.SizeBytes) + .Where(x => !RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, x.Scheme)) + .ToList(); + } + + private static List> BuildSizeBuckets(List candidates) + { + return candidates + .GroupBy(x => x.SizeBytes) + .OrderByDescending(x => x.Key) + .Select(x => x + .OrderBy(c => c.Kld) + .ThenBy(c => Math.Abs(c.PplDeltaPercent)) + .ThenByDescending(c => GetSchemeSafetyScore(c.Scheme)) + .ThenBy(c => c.Scheme.Names[0], StringComparer.Ordinal) + .ToList()) .ToList(); + } - // Compare only to nearby larger neighbors, not the whole ladder. - for (int i = 0; i < explicitCandidates.Count; i++) - { - var smaller = explicitCandidates[i]; + private static bool ShouldEliminateAsBadTrade( + GroupCandidate anchor, + GroupCandidate candidate, + out string reason) + { + reason = string.Empty; - for (int j = i + 1; j < explicitCandidates.Count && j <= i + 2; j++) - { - var larger = explicitCandidates[j]; + if (anchor.SizeBytes <= candidate.SizeBytes) + return false; - double sizeDeltaPercent = - ((double)larger.SizeBytes - smaller.SizeBytes) / larger.SizeBytes * 100.0; + double sizeDeltaPercent = + ((double)anchor.SizeBytes - candidate.SizeBytes) / anchor.SizeBytes * 100.0; - if (sizeDeltaPercent > IsolationPruningConfig.BadTradeMaxSizeDeltaPercent) - continue; + if (sizeDeltaPercent > IsolationPruningConfig.BadTradeMaxSizeDeltaPercent) + return false; - double smallerPplAbs = Math.Abs(smaller.PplDeltaPercent); - double largerPplAbs = Math.Abs(larger.PplDeltaPercent); + double anchorPplAbs = Math.Abs(anchor.PplDeltaPercent); + double candidatePplAbs = Math.Abs(candidate.PplDeltaPercent); - double kldRatio = larger.Kld <= IsolationPruningConfig.FloatingPointEpsilon - ? double.PositiveInfinity - : smaller.Kld / larger.Kld; + double kldRatio = anchor.Kld <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : candidate.Kld / anchor.Kld; - double pplRatio = largerPplAbs <= IsolationPruningConfig.FloatingPointEpsilon - ? double.PositiveInfinity - : smallerPplAbs / largerPplAbs; + double pplRatio = anchorPplAbs <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : candidatePplAbs / anchorPplAbs; - bool kldBadTrade = - smaller.Kld > larger.Kld * IsolationPruningConfig.BadTradeKldMultiplier; + bool kldBadTrade = + candidate.Kld > anchor.Kld * IsolationPruningConfig.BadTradeKldMultiplier; - bool pplBadTrade = - smallerPplAbs > largerPplAbs * IsolationPruningConfig.BadTradePplMultiplier; + bool pplBadTrade = + candidatePplAbs > anchorPplAbs * IsolationPruningConfig.BadTradePplMultiplier; - bool smallerMeaningfullyBetterKld = - smaller.Kld + IsolationPruningConfig.FloatingPointEpsilon < larger.Kld * 0.90; + bool candidateMeaningfullyBetterKld = + candidate.Kld + IsolationPruningConfig.FloatingPointEpsilon < anchor.Kld * 0.90; - bool smallerMeaningfullyBetterPpl = - smallerPplAbs + IsolationPruningConfig.FloatingPointEpsilon < largerPplAbs * 0.90; + bool candidateMeaningfullyBetterPpl = + candidatePplAbs + IsolationPruningConfig.FloatingPointEpsilon < anchorPplAbs * 0.90; - bool mixedTradeoff = - (kldBadTrade && smallerMeaningfullyBetterPpl) || - (pplBadTrade && smallerMeaningfullyBetterKld); + bool mixedTradeoff = + (kldBadTrade && candidateMeaningfullyBetterPpl) || + (pplBadTrade && candidateMeaningfullyBetterKld); - if (mixedTradeoff) - continue; + if (mixedTradeoff) + return false; - if (!kldBadTrade && !pplBadTrade) - continue; + if (!kldBadTrade && !pplBadTrade) + return false; - if (!RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, smaller.Scheme)) - { - RuntimeSearchSpace.BanSchemeForGroup(group, smaller.Scheme); - result.BadTradeEliminations++; + reason = + $"Reason: small size gain ({sizeDeltaPercent:F2}%) but disproportionate damage (KLD x{kldRatio:F2}, |PPL| x{pplRatio:F2})."; - result.Notes.Add( - $"Bad trade elimination: '{smaller.Scheme.Names[0]}' removed vs '{larger.Scheme.Names[0]}' for '{group.Name}'. " + - $"Reason: small size gain ({sizeDeltaPercent:F2}%) but disproportionate damage " + - $"(KLD x{kldRatio:F2}, |PPL| x{pplRatio:F2})."); - } + return true; + } + + private static GroupCandidate? SelectBestBucketSurvivor(List survivors) + { + return survivors + .OrderBy(x => x.Kld) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) + .ThenByDescending(x => GetSchemeSafetyScore(x.Scheme)) + .ThenBy(x => x.Scheme.Names[0], StringComparer.Ordinal) + .FirstOrDefault(); + } + + private static int GetSchemeSafetyScore(TensorWeightScheme scheme) + { + string canonical = scheme.Names[0]; - break; + for (int i = 0; i < canonical.Length - 1; i++) + { + if ((canonical[i] == 'q' || canonical[i] == 'Q') && char.IsDigit(canonical[i + 1])) + { + return canonical[i + 1] - '0'; } } + + return 0; } private async Task LoadSnapshotAsync(HybridQuant quant, CancellationToken ct) From 9e5ec5a5817a6f1d3750fec8f38da89004810601 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 15:48:53 -0400 Subject: [PATCH 067/258] Cache hardware probe execution plan in SQLite --- MQ.DB/Cache.cs | 2 + MQ.DB/Data/MagicQuantContext.cs | 1 + .../DbModels/ExecutionPlanProbeCache.cs | 52 ++++++ MagicQuant/Commands/Evolution.cs | 27 ++- MagicQuant/Services/BenchmarkService.cs | 172 +++++++++++++++++- 5 files changed, 249 insertions(+), 5 deletions(-) create mode 100644 MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index 81856b8..c2dae24 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -61,4 +61,6 @@ public enum MainTorchType public static string CurrentModelId { get; set; } public static bool ForceRelearnBaselineTensorMappings { get; set; } + + public static bool ForceRefreshHardwareProbe { get; set; } } diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index 1ab9aeb..d74a0fd 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -120,6 +120,7 @@ private static bool IsDesignTime() public DbSet BenchmarkRuns { get; set; } public DbSet LearnedBaselineTensorQuants { get; set; } public DbSet BaselineQuantDefinitions { get; set; } + public DbSet ExecutionPlanProbeCaches { get; set; } // -------------------------------------------------------- // Configuration diff --git a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs new file mode 100644 index 0000000..9a1ab77 --- /dev/null +++ b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs @@ -0,0 +1,52 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class ExecutionPlanProbeCache : ISQLiteEntity +{ + public Guid Id { get; set; } = Guid.NewGuid(); + + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + + public string HardwareFingerprint { get; set; } = string.Empty; + public string QuantizedModelFingerprint { get; set; } = string.Empty; + public string QuantizationKey { get; set; } = string.Empty; + public int DiscoveryTokenTarget { get; set; } + + public int StaticNgl { get; set; } + public bool UsesGpu { get; set; } + public int GroupSize { get; set; } + public string SlotsJson { get; set; } = "[]"; + + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; + public DateTime UpdatedUtc { get; set; } = DateTime.UtcNow; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(x => x.Id).ValueGeneratedNever(); + + builder.Property(x => x.HardwareFingerprint).HasMaxLength(1024); + builder.Property(x => x.QuantizedModelFingerprint).HasMaxLength(2048); + builder.Property(x => x.QuantizationKey).HasMaxLength(128); + builder.Property(x => x.SlotsJson).HasMaxLength(8000); + + builder.HasIndex(x => x.AiModelHashId); + builder.HasIndex(x => new + { + x.AiModelHashId, + x.HardwareFingerprint, + x.QuantizedModelFingerprint, + x.QuantizationKey, + x.DiscoveryTokenTarget + }).IsUnique(); + + builder.HasOne(x => x.AiModelHash) + .WithMany() + .HasForeignKey(x => x.AiModelHashId) + .OnDelete(DeleteBehavior.Cascade); + } +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 3c9726b..34eeae9 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -2,7 +2,10 @@ using MagicQuant.Models; using MagicQuant.Services; using MQ.DB; +using MQ.DB.Data; using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Microsoft.EntityFrameworkCore; using Spectre.Console; namespace MagicQuant.Commands; @@ -55,6 +58,8 @@ public async Task Run(List args) Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); Cache.ForceRelearnBaselineTensorMappings = args.Any(a => string.Equals(a.Name, "relearn-baseline-mappings", StringComparison.OrdinalIgnoreCase)); + Cache.ForceRefreshHardwareProbe = args.Any(a => + string.Equals(a.Name, "recheck-hardware-probe", StringComparison.OrdinalIgnoreCase)); JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); @@ -74,6 +79,8 @@ public async Task Run(List args) Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); + await EnsureSqliteReadyAsync(); + var pyManager = new PythonManager(Cache.MagicQuantDirectory); var benchmarkService = new BenchmarkService(pyManager); var quantizationService = new QuantizationService(benchmarkService); @@ -87,7 +94,9 @@ public async Task Run(List args) var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); - await benchmarkService.EnsureExecutionPlanAsync(q8ModelGgufPath); + await benchmarkService.EnsureExecutionPlanAsync( + q8ModelGgufPath, + forceRediscovery: Cache.ForceRefreshHardwareProbe); await benchmarkService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); var baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); @@ -265,8 +274,24 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine("[bold]Arguments:[/]"); AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Required)"); AnsiConsole.MarkupLine(" [green]--relearn-baseline-mappings[/] Delete and relearn baseline tensor mappings (Optional)"); + AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } + + private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) + { + await using var db = new MagicQuantContext(); + await db.Database.MigrateAsync(ct); + + var model = await db.AiModelHashes + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (model != null) + return; + + db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); + await db.SaveChangesAsync(ct); + } } diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 9b83d84..517de1a 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -63,6 +63,7 @@ public BenchmarkService(PythonManager pyManager) public async Task EnsureExecutionPlanAsync( string q8ModelPath, int discoveryTokenTarget = 8192, + bool forceRediscovery = false, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(q8ModelPath)) @@ -70,7 +71,8 @@ public async Task EnsureExecutionPlanAsync( string normalizedPath = Path.GetFullPath(q8ModelPath); - if (_currentPlan != null && + if (!forceRediscovery && + _currentPlan != null && string.Equals(_currentPlan.PlanModelPath, normalizedPath, StringComparison.OrdinalIgnoreCase)) { return; @@ -79,13 +81,28 @@ public async Task EnsureExecutionPlanAsync( await PlanInitLock.WaitAsync(ct); try { - if (_currentPlan != null && + if (!forceRediscovery && + _currentPlan != null && string.Equals(_currentPlan.PlanModelPath, normalizedPath, StringComparison.OrdinalIgnoreCase)) { return; } - var plan = await BuildExecutionPlanAsync(normalizedPath, discoveryTokenTarget, ct); + var cacheKey = BuildExecutionPlanCacheKey(normalizedPath, discoveryTokenTarget); + + BenchmarkExecutionPlan? plan = null; + if (!forceRediscovery) + { + plan = await TryLoadCachedExecutionPlanAsync(cacheKey, ct); + if (plan != null) + AnsiConsole.MarkupLine("[green]Loaded benchmark execution plan from SQLite cache.[/]"); + } + + if (plan == null) + { + plan = await BuildExecutionPlanAsync(normalizedPath, discoveryTokenTarget, ct); + await UpsertCachedExecutionPlanAsync(cacheKey, plan, ct); + } lock (SlotSync) { @@ -189,6 +206,9 @@ public async Task ClampStaticNglWithBaseModelAsync( _slotSemaphore = new SemaphoreSlim(cpuPlan.Slots.Count, cpuPlan.Slots.Count); } + var cacheKeyCpu = BuildExecutionPlanCacheKey(_currentPlan.PlanModelPath, discoveryTokenTarget); + await UpsertCachedExecutionPlanAsync(cacheKeyCpu, _currentPlan, ct); + return; } @@ -209,6 +229,9 @@ public async Task ClampStaticNglWithBaseModelAsync( } } + var cacheKey = BuildExecutionPlanCacheKey(_currentPlan.PlanModelPath, discoveryTokenTarget); + await UpsertCachedExecutionPlanAsync(cacheKey, _currentPlan, ct); + AnsiConsole.MarkupLine($"[green]Base-model clamped static ngl:[/] [cyan]{chosen.Value}[/]"); } finally @@ -296,6 +319,140 @@ private async Task BuildExecutionPlanAsync( slots: new List { allGpuSlot }); } + private async Task TryLoadCachedExecutionPlanAsync( + ExecutionPlanCacheKey key, + CancellationToken ct) + { + await using var db = new MagicQuantContext(); + var aiModelHashId = await GetOrCreateAiModelHashIdAsync(db, ct); + + var row = await db.ExecutionPlanProbeCaches + .AsNoTracking() + .FirstOrDefaultAsync(x => + x.AiModelHashId == aiModelHashId && + x.HardwareFingerprint == key.HardwareFingerprint && + x.QuantizedModelFingerprint == key.QuantizedModelFingerprint && + x.QuantizationKey == key.QuantizationKey && + x.DiscoveryTokenTarget == key.DiscoveryTokenTarget, ct); + + if (row == null) + return null; + + List slotDevices; + try + { + slotDevices = JsonSerializer.Deserialize>(row.SlotsJson) ?? new List(); + } + catch + { + return null; + } + + if (slotDevices.Count == 0) + return null; + + var slots = slotDevices + .Select((devices, idx) => new BenchmarkSlot(idx, devices ?? Array.Empty())) + .ToList(); + + return new BenchmarkExecutionPlan( + planModelPath: key.PlanModelPath, + staticNgl: row.StaticNgl, + usesGpu: row.UsesGpu, + groupSize: row.GroupSize, + slots: slots); + } + + private async Task UpsertCachedExecutionPlanAsync( + ExecutionPlanCacheKey key, + BenchmarkExecutionPlan plan, + CancellationToken ct) + { + await using var db = new MagicQuantContext(); + var aiModelHashId = await GetOrCreateAiModelHashIdAsync(db, ct); + + var existing = await db.ExecutionPlanProbeCaches + .FirstOrDefaultAsync(x => + x.AiModelHashId == aiModelHashId && + x.HardwareFingerprint == key.HardwareFingerprint && + x.QuantizedModelFingerprint == key.QuantizedModelFingerprint && + x.QuantizationKey == key.QuantizationKey && + x.DiscoveryTokenTarget == key.DiscoveryTokenTarget, ct); + + string slotsJson = JsonSerializer.Serialize(plan.Slots.Select(x => x.DeviceIndices).ToList()); + var now = DateTime.UtcNow; + + if (existing == null) + { + existing = new ExecutionPlanProbeCache + { + AiModelHashId = aiModelHashId, + HardwareFingerprint = key.HardwareFingerprint, + QuantizedModelFingerprint = key.QuantizedModelFingerprint, + QuantizationKey = key.QuantizationKey, + DiscoveryTokenTarget = key.DiscoveryTokenTarget, + CreatedUtc = now + }; + + db.ExecutionPlanProbeCaches.Add(existing); + } + + existing.StaticNgl = plan.StaticNgl; + existing.UsesGpu = plan.UsesGpu; + existing.GroupSize = plan.GroupSize; + existing.SlotsJson = slotsJson; + existing.UpdatedUtc = now; + + await db.SaveChangesAsync(ct); + } + + private static ExecutionPlanCacheKey BuildExecutionPlanCacheKey(string normalizedModelPath, int discoveryTokenTarget) + { + string quantizedModelFingerprint; + if (File.Exists(normalizedModelPath)) + { + var info = new FileInfo(normalizedModelPath); + quantizedModelFingerprint = + $"{normalizedModelPath}|{info.Length}|{info.LastWriteTimeUtc.Ticks}"; + } + else + { + quantizedModelFingerprint = normalizedModelPath; + } + + var sys = Cache.SysInfo; + string hardwareFingerprint = sys == null + ? "unknown-hardware" + : string.Join("|", new[] + { + $"threads:{sys.ThreadCount}", + $"ram:{sys.RamGb:F2}", + $"gpu:{string.Join(";", sys.GpuInfo.Select(g => $"{g.GpuVendor}:{g.GpuName}:{g.VramGb:F2}:{g.UniqueId ?? "none"}"))}" + }); + + return new ExecutionPlanCacheKey( + hardwareFingerprint, + quantizedModelFingerprint, + quantizationKey: "Q8_0", + discoveryTokenTarget, + normalizedModelPath); + } + + private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); + + var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + if (model != null) + return model.Id; + + model = new AiModelHash { UniqueHash = Cache.CurrentModelId }; + db.AiModelHashes.Add(model); + await db.SaveChangesAsync(ct); + return model.Id; + } + private async Task ProbeHighestStableNglAsync( string modelPath, BenchmarkSlot slot, @@ -1783,4 +1940,11 @@ public ValueTask DisposeAsync() return ValueTask.CompletedTask; } } -} \ No newline at end of file + + private sealed record ExecutionPlanCacheKey( + string HardwareFingerprint, + string QuantizedModelFingerprint, + string QuantizationKey, + int DiscoveryTokenTarget, + string PlanModelPath); +} From 10a7646ee1745dcbb45dbfb485b4285509425d2c Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 16:11:15 -0400 Subject: [PATCH 068/258] Parameterize execution plan quantization cache key --- MagicQuant/Commands/Evolution.cs | 2 ++ MagicQuant/Services/BenchmarkService.cs | 28 ++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 34eeae9..10cd1d7 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -93,9 +93,11 @@ public async Task Run(List args) var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); + string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; await benchmarkService.EnsureExecutionPlanAsync( q8ModelGgufPath, + quantizationKey: q8QuantizationKey, forceRediscovery: Cache.ForceRefreshHardwareProbe); await benchmarkService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 517de1a..bb7220c 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -42,6 +42,7 @@ public class BenchmarkService private static readonly object SlotSync = new(); private static BenchmarkExecutionPlan? _currentPlan; + private static string _currentPlanQuantizationKey = "Q8_0"; private static Queue _availableSlots = new(); private static SemaphoreSlim? _slotSemaphore; @@ -63,16 +64,21 @@ public BenchmarkService(PythonManager pyManager) public async Task EnsureExecutionPlanAsync( string q8ModelPath, int discoveryTokenTarget = 8192, + string quantizationKey = "Q8_0", bool forceRediscovery = false, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(q8ModelPath)) throw new ArgumentException("Q8 model path was null or empty.", nameof(q8ModelPath)); + if (string.IsNullOrWhiteSpace(quantizationKey)) + throw new ArgumentException("Quantization key was null or empty.", nameof(quantizationKey)); string normalizedPath = Path.GetFullPath(q8ModelPath); + string normalizedQuantizationKey = quantizationKey.Trim().ToUpperInvariant(); if (!forceRediscovery && _currentPlan != null && + string.Equals(_currentPlanQuantizationKey, normalizedQuantizationKey, StringComparison.OrdinalIgnoreCase) && string.Equals(_currentPlan.PlanModelPath, normalizedPath, StringComparison.OrdinalIgnoreCase)) { return; @@ -83,12 +89,13 @@ public async Task EnsureExecutionPlanAsync( { if (!forceRediscovery && _currentPlan != null && + string.Equals(_currentPlanQuantizationKey, normalizedQuantizationKey, StringComparison.OrdinalIgnoreCase) && string.Equals(_currentPlan.PlanModelPath, normalizedPath, StringComparison.OrdinalIgnoreCase)) { return; } - var cacheKey = BuildExecutionPlanCacheKey(normalizedPath, discoveryTokenTarget); + var cacheKey = BuildExecutionPlanCacheKey(normalizedPath, discoveryTokenTarget, normalizedQuantizationKey); BenchmarkExecutionPlan? plan = null; if (!forceRediscovery) @@ -107,6 +114,7 @@ public async Task EnsureExecutionPlanAsync( lock (SlotSync) { _currentPlan = plan; + _currentPlanQuantizationKey = normalizedQuantizationKey; _availableSlots = new Queue(plan.Slots); _slotSemaphore = new SemaphoreSlim(plan.Slots.Count, plan.Slots.Count); } @@ -116,6 +124,7 @@ public async Task EnsureExecutionPlanAsync( AnsiConsole.MarkupLine($"[green]Uses GPU:[/] [cyan]{plan.UsesGpu}[/]"); AnsiConsole.MarkupLine($"[green]GPU group size:[/] [cyan]{plan.GroupSize}[/]"); AnsiConsole.MarkupLine($"[green]Parallel benchmark slots:[/] [cyan]{plan.Slots.Count}[/]"); + AnsiConsole.MarkupLine($"[green]Quantization key:[/] [cyan]{Markup.Escape(normalizedQuantizationKey)}[/]"); foreach (var slot in plan.Slots) { @@ -206,7 +215,10 @@ public async Task ClampStaticNglWithBaseModelAsync( _slotSemaphore = new SemaphoreSlim(cpuPlan.Slots.Count, cpuPlan.Slots.Count); } - var cacheKeyCpu = BuildExecutionPlanCacheKey(_currentPlan.PlanModelPath, discoveryTokenTarget); + var cacheKeyCpu = BuildExecutionPlanCacheKey( + _currentPlan.PlanModelPath, + discoveryTokenTarget, + _currentPlanQuantizationKey); await UpsertCachedExecutionPlanAsync(cacheKeyCpu, _currentPlan, ct); return; @@ -229,7 +241,10 @@ public async Task ClampStaticNglWithBaseModelAsync( } } - var cacheKey = BuildExecutionPlanCacheKey(_currentPlan.PlanModelPath, discoveryTokenTarget); + var cacheKey = BuildExecutionPlanCacheKey( + _currentPlan.PlanModelPath, + discoveryTokenTarget, + _currentPlanQuantizationKey); await UpsertCachedExecutionPlanAsync(cacheKey, _currentPlan, ct); AnsiConsole.MarkupLine($"[green]Base-model clamped static ngl:[/] [cyan]{chosen.Value}[/]"); @@ -406,7 +421,10 @@ private async Task UpsertCachedExecutionPlanAsync( await db.SaveChangesAsync(ct); } - private static ExecutionPlanCacheKey BuildExecutionPlanCacheKey(string normalizedModelPath, int discoveryTokenTarget) + private static ExecutionPlanCacheKey BuildExecutionPlanCacheKey( + string normalizedModelPath, + int discoveryTokenTarget, + string quantizationKey) { string quantizedModelFingerprint; if (File.Exists(normalizedModelPath)) @@ -433,7 +451,7 @@ private static ExecutionPlanCacheKey BuildExecutionPlanCacheKey(string normalize return new ExecutionPlanCacheKey( hardwareFingerprint, quantizedModelFingerprint, - quantizationKey: "Q8_0", + quantizationKey, discoveryTokenTarget, normalizedModelPath); } From 61fbd0a48f501c44295df24e71a4afb1b7e34417 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 16:50:14 -0400 Subject: [PATCH 069/258] Preserve protected GGUF baselines during base cleanup --- MagicQuant/Services/QuantizationService.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 3a09773..e158cdf 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -628,6 +628,7 @@ public async Task EnsureBaseModelFileAsync(bool deleteProcess = false) foreach (var filePath in Directory.EnumerateFiles(_ggufDir, "*.gguf", SearchOption.TopDirectoryOnly)) { var currentFileName = Path.GetFileName(filePath); + var currentModelName = Path.GetFileNameWithoutExtension(currentFileName); if (isImmune && string.Equals(currentFileName, normalizedFileName, StringComparison.OrdinalIgnoreCase)) @@ -635,6 +636,11 @@ public async Task EnsureBaseModelFileAsync(bool deleteProcess = false) continue; } + if (!string.IsNullOrWhiteSpace(currentModelName) && IsProtectedModel(currentModelName)) + { + continue; + } + await HardDeleteHelper.DeleteFileIfExistsAsync(filePath); } } @@ -1819,4 +1825,4 @@ void HandleLine(string? line, bool isError) StdErr = stderrBuilder.ToString() }; } -} \ No newline at end of file +} From fa0e8000c86591b630e9204696beac9490cb38df Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 16:51:56 -0400 Subject: [PATCH 070/258] Skip Q8 rebuild when execution plan cache is present --- MagicQuant/Commands/Evolution.cs | 20 ++++++--- MagicQuant/Services/BenchmarkService.cs | 55 +++++++++++++++++++------ 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 10cd1d7..1a7bc73 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -91,14 +91,22 @@ public async Task Run(List args) AnsiConsole.MarkupLine("[yellow]Forced relearn is ON:[/] pure baseline samples will be rebuilt and relearned."); } - var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); - var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; + var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); + + bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && + await benchmarkService.TryInitializeExecutionPlanFromCacheAsync( + quantizationKey: q8QuantizationKey); + + if (!loadedPlanFromCache) + { + var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); + await benchmarkService.EnsureExecutionPlanAsync( + q8ModelGgufPath, + quantizationKey: q8QuantizationKey, + forceRediscovery: Cache.ForceRefreshHardwareProbe); + } - await benchmarkService.EnsureExecutionPlanAsync( - q8ModelGgufPath, - quantizationKey: q8QuantizationKey, - forceRediscovery: Cache.ForceRefreshHardwareProbe); await benchmarkService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); var baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index bb7220c..b1aa4d6 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -137,6 +137,37 @@ public async Task EnsureExecutionPlanAsync( } } + public async Task TryInitializeExecutionPlanFromCacheAsync( + int discoveryTokenTarget = 8192, + string quantizationKey = "Q8_0", + string? preferredPlanModelPath = null, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(quantizationKey)) + throw new ArgumentException("Quantization key was null or empty.", nameof(quantizationKey)); + + string normalizedQuantizationKey = quantizationKey.Trim().ToUpperInvariant(); + string planModelPath = string.IsNullOrWhiteSpace(preferredPlanModelPath) + ? $"cached://{normalizedQuantizationKey}" + : Path.GetFullPath(preferredPlanModelPath); + + var cacheKey = BuildExecutionPlanCacheKey(planModelPath, discoveryTokenTarget, normalizedQuantizationKey); + var plan = await TryLoadCachedExecutionPlanAsync(cacheKey, ct); + if (plan == null) + return false; + + lock (SlotSync) + { + _currentPlan = plan; + _currentPlanQuantizationKey = normalizedQuantizationKey; + _availableSlots = new Queue(plan.Slots); + _slotSemaphore = new SemaphoreSlim(plan.Slots.Count, plan.Slots.Count); + } + + AnsiConsole.MarkupLine("[green]Loaded benchmark execution plan from SQLite cache (no Q8 rebuild needed).[/]"); + return true; + } + public async Task ClampStaticNglWithBaseModelAsync( string baseModelPath, int discoveryTokenTarget = 8192, @@ -422,21 +453,11 @@ private async Task UpsertCachedExecutionPlanAsync( } private static ExecutionPlanCacheKey BuildExecutionPlanCacheKey( - string normalizedModelPath, + string planModelPath, int discoveryTokenTarget, string quantizationKey) { - string quantizedModelFingerprint; - if (File.Exists(normalizedModelPath)) - { - var info = new FileInfo(normalizedModelPath); - quantizedModelFingerprint = - $"{normalizedModelPath}|{info.Length}|{info.LastWriteTimeUtc.Ticks}"; - } - else - { - quantizedModelFingerprint = normalizedModelPath; - } + string quantizedModelFingerprint = BuildQuantizedModelFingerprint(quantizationKey); var sys = Cache.SysInfo; string hardwareFingerprint = sys == null @@ -453,7 +474,15 @@ private static ExecutionPlanCacheKey BuildExecutionPlanCacheKey( quantizedModelFingerprint, quantizationKey, discoveryTokenTarget, - normalizedModelPath); + planModelPath); + } + + private static string BuildQuantizedModelFingerprint(string quantizationKey) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); + + return $"model:{Cache.CurrentModelId}|quant:{quantizationKey}"; } private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct) From 0635322a7b4a38654c11cd2d3c025689952df3d5 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 19:01:34 -0400 Subject: [PATCH 071/258] Add cache hit/miss logs and clean up probe-only Q8 artifacts --- MagicQuant/Commands/Evolution.cs | 2 ++ MagicQuant/Services/BenchmarkService.cs | 7 ++++++ MagicQuant/Services/QuantizationService.cs | 25 ++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 1a7bc73..bcf1159 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -100,6 +100,7 @@ await benchmarkService.TryInitializeExecutionPlanFromCacheAsync( if (!loadedPlanFromCache) { + AnsiConsole.MarkupLine("[grey]Cache not usable, preparing probe-only Q8 baseline...[/]"); var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); await benchmarkService.EnsureExecutionPlanAsync( q8ModelGgufPath, @@ -108,6 +109,7 @@ await benchmarkService.EnsureExecutionPlanAsync( } await benchmarkService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); + await quantizationService.CleanupPureQ8ModelAsync(); var baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); var baseBenchDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks", baseTypeName); diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index b1aa4d6..85fc252 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -152,9 +152,15 @@ public async Task TryInitializeExecutionPlanFromCacheAsync( : Path.GetFullPath(preferredPlanModelPath); var cacheKey = BuildExecutionPlanCacheKey(planModelPath, discoveryTokenTarget, normalizedQuantizationKey); + AnsiConsole.MarkupLine( + $"[grey]Checking execution-plan cache:[/] quant={Markup.Escape(normalizedQuantizationKey)}, tokens={discoveryTokenTarget}"); + var plan = await TryLoadCachedExecutionPlanAsync(cacheKey, ct); if (plan == null) + { + AnsiConsole.MarkupLine("[yellow]Execution-plan cache miss:[/] full Q8 probe will run."); return false; + } lock (SlotSync) { @@ -391,6 +397,7 @@ private async Task BuildExecutionPlanAsync( } catch { + AnsiConsole.MarkupLine("[yellow]Execution-plan cache row was unreadable (slot JSON parse failed). Re-probing.[/]"); return null; } diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index e158cdf..d306050 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -741,6 +741,31 @@ public async Task EnsurePureQ8ModelAsync() return q8Path; } + public async Task CleanupPureQ8ModelAsync() + { + var pureQ8 = new HybridQuant + { + BaseQuant = BaselineQuants.Q8_0, + Tensors = new List() + }; + + string modelName = GenerateHybridName(pureQ8); + string q8Path = Path.Combine(_ggufDir, $"{modelName}.gguf"); + string successFile = Path.Combine(_ggufDir, $"{Path.GetFileName(q8Path)}.success.json"); + string quantLog = q8Path + ".quantize.log"; + + bool hadQ8 = File.Exists(q8Path) || File.Exists(successFile) || File.Exists(quantLog); + + await HardDeleteHelper.DeleteFileIfExistsAsync(q8Path); + await HardDeleteHelper.DeleteFileIfExistsAsync(successFile); + await HardDeleteHelper.DeleteFileIfExistsAsync(quantLog); + + if (hadQ8) + AnsiConsole.MarkupLine($"[grey]Removed probe-only Q8 artifacts:[/] {Markup.Escape(modelName)}"); + else + AnsiConsole.MarkupLine("[grey]No probe-only Q8 artifacts to clean up.[/]"); + } + // ---------------------------------------------------------------- // Quantization // ---------------------------------------------------------------- From 24521f9358b7ea11b8ad86e3a5a92606d6c595f7 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 17 Apr 2026 21:33:08 -0400 Subject: [PATCH 072/258] updated database --- .../20260417203830_newUpdate.Designer.cs | 498 ++++++++++++++++++ MQ.DB/Migrations/20260417203830_newUpdate.cs | 61 +++ .../MagicQuantContextModelSnapshot.cs | 67 +++ 3 files changed, 626 insertions(+) create mode 100644 MQ.DB/Migrations/20260417203830_newUpdate.Designer.cs create mode 100644 MQ.DB/Migrations/20260417203830_newUpdate.cs diff --git a/MQ.DB/Migrations/20260417203830_newUpdate.Designer.cs b/MQ.DB/Migrations/20260417203830_newUpdate.Designer.cs new file mode 100644 index 0000000..6a2f725 --- /dev/null +++ b/MQ.DB/Migrations/20260417203830_newUpdate.Designer.cs @@ -0,0 +1,498 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260417203830_newUpdate")] + partial class newUpdate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("BaselineName") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeId") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("AiModelHashId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260417203830_newUpdate.cs b/MQ.DB/Migrations/20260417203830_newUpdate.cs new file mode 100644 index 0000000..036c82b --- /dev/null +++ b/MQ.DB/Migrations/20260417203830_newUpdate.cs @@ -0,0 +1,61 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class newUpdate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "ExecutionPlanProbeCaches", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + HardwareFingerprint = table.Column(type: "TEXT", maxLength: 1024, nullable: false), + QuantizedModelFingerprint = table.Column(type: "TEXT", maxLength: 2048, nullable: false), + QuantizationKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), + DiscoveryTokenTarget = table.Column(type: "INTEGER", nullable: false), + StaticNgl = table.Column(type: "INTEGER", nullable: false), + UsesGpu = table.Column(type: "INTEGER", nullable: false), + GroupSize = table.Column(type: "INTEGER", nullable: false), + SlotsJson = table.Column(type: "TEXT", maxLength: 8000, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + UpdatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ExecutionPlanProbeCaches", x => x.Id); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_AiModelHashId", + table: "ExecutionPlanProbeCaches", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_AiModelHashId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", + table: "ExecutionPlanProbeCaches", + columns: new[] { "AiModelHashId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ExecutionPlanProbeCaches"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index d760253..4532c9e 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -176,6 +176,62 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("CategoryBenchmark"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("AiModelHashId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => { b.Property("Id") @@ -373,6 +429,17 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("AiBenchmark"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => { b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") From 4566e30c32c972ed8b666a2accfd85c93155bfe0 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 21:58:45 -0400 Subject: [PATCH 073/258] Skip native BF16 relearn when mapping already exists --- MagicQuant/Services/QuantizationService.cs | 31 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index d306050..efd0023 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -912,6 +912,35 @@ public async Task LearnNativeSourceTruthAsync( if (string.IsNullOrWhiteSpace(nativeGgufPath) || !File.Exists(nativeGgufPath)) throw new FileNotFoundException($"Native GGUF path not found for learning: {nativeGgufPath}"); + var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + + if (!Cache.ForceRelearnBaselineTensorMappings) + { + await using var precheckDb = new MagicQuantContext(); + var modelId = await precheckDb.AiModelHashes + .AsNoTracking() + .Where(x => x.UniqueHash == Cache.CurrentModelId) + .Select(x => (Guid?)x.Id) + .FirstOrDefaultAsync(ct); + + if (modelId.HasValue) + { + int existingRows = await precheckDb.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == modelId.Value && + x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + x.TensorWeightSchemeId == nativeScheme.UniqueId) + .CountAsync(ct); + + if (existingRows > 0) + { + AnsiConsole.MarkupLine( + $"[grey]Native-source learned truth already exists:[/] [cyan]{existingRows:N0}[/] row(s) for [yellow]{Markup.Escape(nativeScheme.Names[0])}[/]. Skipping relearn. Use [green]--relearn-baseline-mappings[/] to regenerate."); + return; + } + } + } + var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, nativeGgufPath); var ggufTruth = metadata.TensorTypes .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); @@ -948,8 +977,6 @@ public async Task LearnNativeSourceTruthAsync( if (!benchmarkId.HasValue) throw new InvalidOperationException("Native-source benchmark row is missing; benchmark base model before native-source learning."); - var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - await db.LearnedBaselineTensorQuants .Where(x => x.AiModelHashId == model.Id && x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && From 2764b3f59965191e046ead621f65c1efec14271c Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 22:52:55 -0400 Subject: [PATCH 074/258] Fix native relearn precheck model-id query type --- MagicQuant/Services/QuantizationService.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index efd0023..9c641a0 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -917,17 +917,15 @@ public async Task LearnNativeSourceTruthAsync( if (!Cache.ForceRelearnBaselineTensorMappings) { await using var precheckDb = new MagicQuantContext(); - var modelId = await precheckDb.AiModelHashes + var model = await precheckDb.AiModelHashes .AsNoTracking() - .Where(x => x.UniqueHash == Cache.CurrentModelId) - .Select(x => (Guid?)x.Id) - .FirstOrDefaultAsync(ct); + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - if (modelId.HasValue) + if (model != null) { int existingRows = await precheckDb.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == modelId.Value && + .Where(x => x.AiModelHashId == model.Id && x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && x.TensorWeightSchemeId == nativeScheme.UniqueId) .CountAsync(ct); From 80da117007568bce54eeba01b9aedccdef006fb3 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Fri, 17 Apr 2026 22:57:27 -0400 Subject: [PATCH 075/258] Fix LearnNativeSourceTruthAsync local variable shadowing --- MagicQuant/Services/QuantizationService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 9c641a0..24967eb 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -917,15 +917,15 @@ public async Task LearnNativeSourceTruthAsync( if (!Cache.ForceRelearnBaselineTensorMappings) { await using var precheckDb = new MagicQuantContext(); - var model = await precheckDb.AiModelHashes + var existingModel = await precheckDb.AiModelHashes .AsNoTracking() .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - if (model != null) + if (existingModel != null) { int existingRows = await precheckDb.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id && + .Where(x => x.AiModelHashId == existingModel.Id && x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && x.TensorWeightSchemeId == nativeScheme.UniqueId) .CountAsync(ct); From 4b6e591cd208ae77a5be8dd1085cdbc5eaa032a0 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sat, 18 Apr 2026 14:29:39 -0400 Subject: [PATCH 076/258] Skip base BF16 clamp/benchmark when native truth already exists --- MagicQuant/Commands/Evolution.cs | 40 ++++++++++++++++------ MagicQuant/Services/QuantizationService.cs | 23 +++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index bcf1159..942d9f3 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -108,7 +108,19 @@ await benchmarkService.EnsureExecutionPlanAsync( forceRediscovery: Cache.ForceRefreshHardwareProbe); } - await benchmarkService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); + bool nativeTruthAlreadyLearned = !Cache.ForceRelearnBaselineTensorMappings && + await quantizationService.HasNativeSourceLearnedTruthAsync(); + + if (!nativeTruthAlreadyLearned || !loadedPlanFromCache) + { + await benchmarkService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); + } + else + { + AnsiConsole.MarkupLine( + "[grey]Skipping base-model ngl clamp because native-source truth already exists and execution plan cache was loaded.[/]"); + } + await quantizationService.CleanupPureQ8ModelAsync(); var baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); @@ -117,15 +129,23 @@ await benchmarkService.EnsureExecutionPlanAsync( var baseModelQuant = HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()); - await benchmarkService.RunAllBenchmarksAsync( - quantConfig: baseModelQuant, - modelPath: bf16ModelGgufPath, - benchDir: baseBenchDir, - klLogitsDir: baseLogitsDir, - saveLogits: true, - domainsOverride: new[] { "general", "code", "math" }); - - await quantizationService.LearnNativeSourceTruthAsync(bf16ModelGgufPath); + if (!nativeTruthAlreadyLearned) + { + await benchmarkService.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, + modelPath: bf16ModelGgufPath, + benchDir: baseBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: true, + domainsOverride: new[] { "general", "code", "math" }); + + await quantizationService.LearnNativeSourceTruthAsync(bf16ModelGgufPath); + } + else + { + AnsiConsole.MarkupLine( + "[grey]Skipping native BF16 baseline benchmark + relearn because learned native-source truth already exists. Use --relearn-baseline-mappings to force rebuild.[/]"); + } var compatibilityService = new ModelCompatibilityService(pyManager); await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 24967eb..125303f 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -905,6 +905,29 @@ public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = defaul AnsiConsole.MarkupLine("[yellow]Relearn requested:[/] baseline artifacts, benchmark caches, and learning diagnostics were invalidated."); } + public async Task HasNativeSourceLearnedTruthAsync(CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + return false; + + var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + + await using var db = new MagicQuantContext(); + var model = await db.AiModelHashes + .AsNoTracking() + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (model == null) + return false; + + return await db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == model.Id && + x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + x.TensorWeightSchemeId == nativeScheme.UniqueId) + .AnyAsync(ct); + } + public async Task LearnNativeSourceTruthAsync( string nativeGgufPath, CancellationToken ct = default) From 135c2f92835fea646614070e22c84b56c860bbec Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 15:08:48 -0400 Subject: [PATCH 077/258] Add canonical imatrix service and runtime capability gating --- MQ.DB/Cache.cs | 8 + MQ.DB/Models/TensorWeightScheme.cs | 6 + MagicQuant/Commands/Evolution.cs | 36 ++ MagicQuant/Helpers/ComboLogic.cs | 12 +- MagicQuant/Helpers/RuntimeSearchSpace.cs | 8 + MagicQuant/Helpers/TensorConfigGenerator.cs | 6 +- MagicQuant/Models/ImatrixModels.cs | 62 +++ MagicQuant/Program.cs | 6 +- MagicQuant/Services/ImatrixService.cs | 419 ++++++++++++++++++ .../Services/IsolationOptimizationService.cs | 4 - .../Services/LearnedBaselinePruningService.cs | 1 + MagicQuant/Services/QuantizationService.cs | 16 + 12 files changed, 572 insertions(+), 12 deletions(-) create mode 100644 MagicQuant/Models/ImatrixModels.cs create mode 100644 MagicQuant/Services/ImatrixService.cs diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index c2dae24..194c44e 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -63,4 +63,12 @@ public enum MainTorchType public static bool ForceRelearnBaselineTensorMappings { get; set; } public static bool ForceRefreshHardwareProbe { get; set; } + + public static bool UseImatrix { get; set; } + + public static bool ForceImatrixRebuild { get; set; } + + public static bool IsImatrixAvailable { get; set; } + + public static string? ActiveImatrixPath { get; set; } } diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index eb1c64e..3995bcd 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -268,6 +268,12 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) Q5_K, IQ4_XS, IQ4_NL, + IQ3_S, + IQ3_XS, + IQ3_XXS, + IQ2_S, + IQ2_XS, + IQ2_XXS, Q4_K ]; diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index bcf1159..87620a4 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -60,6 +60,9 @@ public async Task Run(List args) string.Equals(a.Name, "relearn-baseline-mappings", StringComparison.OrdinalIgnoreCase)); Cache.ForceRefreshHardwareProbe = args.Any(a => string.Equals(a.Name, "recheck-hardware-probe", StringComparison.OrdinalIgnoreCase)); + Cache.UseImatrix = args.Any(a => string.Equals(a.Name, "use-imatrix", StringComparison.OrdinalIgnoreCase)); + Cache.ForceImatrixRebuild = args.Any(a => string.Equals(a.Name, "imatrix-force-rebuild", StringComparison.OrdinalIgnoreCase)); + RuntimeSearchSpace.SetImatrixAvailability(false); JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); @@ -84,6 +87,7 @@ public async Task Run(List args) var pyManager = new PythonManager(Cache.MagicQuantDirectory); var benchmarkService = new BenchmarkService(pyManager); var quantizationService = new QuantizationService(benchmarkService); + var imatrixService = new ImatrixService(); if (Cache.ForceRelearnBaselineTensorMappings) { @@ -94,6 +98,31 @@ public async Task Run(List args) string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); + var imatrixRequest = new ImatrixRequest + { + UseImatrix = Cache.UseImatrix, + ForceRebuild = Cache.ForceImatrixRebuild, + ImatrixUrl = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-url", StringComparison.OrdinalIgnoreCase))?.Value, + DatasetRepo = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-repo", StringComparison.OrdinalIgnoreCase))?.Value, + DatasetSplit = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-split", StringComparison.OrdinalIgnoreCase))?.Value, + DatasetConfig = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-config", StringComparison.OrdinalIgnoreCase))?.Value, + LocalDatasetFile = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-local-file", StringComparison.OrdinalIgnoreCase))?.Value, + ModelDirectory = Cache.ModelDirectory!, + MagicQuantDirectory = Cache.ModelMagicQuantDirectory! + }; + + var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest, ct: default); + if (imatrixEnsureResult.Enabled) + { + AnsiConsole.MarkupLine( + $"[green]Imatrix active:[/] {Markup.Escape(imatrixEnsureResult.CanonicalImatrixPath ?? \"n/a\")} " + + $"(rebuilt={(imatrixEnsureResult.Rebuilt ? "yes" : "no")})"); + } + else + { + AnsiConsole.MarkupLine("[grey]Imatrix disabled for this run.[/]"); + } + bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && await benchmarkService.TryInitializeExecutionPlanFromCacheAsync( quantizationKey: q8QuantizationKey); @@ -287,6 +316,13 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Required)"); AnsiConsole.MarkupLine(" [green]--relearn-baseline-mappings[/] Delete and relearn baseline tensor mappings (Optional)"); AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); + AnsiConsole.MarkupLine(" [green]--use-imatrix[/] Enable imatrix acquisition/build and allow imatrix-required search candidates (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-force-rebuild[/] Delete/rebuild canonical imatrix artifacts before run (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-url[/] HTTPS URL for direct imatrix artifact download (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-dataset-repo[/] Hugging Face dataset repo ID for imatrix generation (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-dataset-split[/] Dataset split for HF/local dataset source metadata/build (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-dataset-config[/] Optional dataset config name for HF datasets (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-dataset-local-file[/] Full path to local .json/.jsonl dataset source (Optional)"); AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 9b5178f..0910066 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -12,13 +12,13 @@ public static class ComboLogic public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants baseQuant) { - bool baseRequiresImatrix = baseQuant.RequiresImatrix; + bool imatrixAvailable = RuntimeSearchSpace.HasUsableImatrix(); - var schemesForBase = TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(s => baseRequiresImatrix || !s.RequiresImatrix) + var schemesForRun = TensorWeightScheme.All_Allowed_Hybrid_Quants + .Where(s => imatrixAvailable || !s.RequiresImatrix) .ToImmutableArray(); - if (schemesForBase.IsEmpty) + if (schemesForRun.IsEmpty) throw new InvalidOperationException("No tensor schemes available for this base."); var builder = ImmutableArray.CreateBuilder(); @@ -37,7 +37,7 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants if (!RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)) ids.Add(TensorWeightScheme.BF16_F16.UniqueId); - foreach (var scheme in schemesForBase) + foreach (var scheme in schemesForRun) { if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) continue; @@ -93,4 +93,4 @@ public static BigInteger CountAll() return sum; } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index bf11b1d..f68ab85 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -17,6 +17,7 @@ public static class RuntimeSearchSpace private static readonly Dictionary>> LearnedBaselineMissingByGroupAndScheme = new(); private static readonly HashSet DisabledCombinationBaselineIds = new(); private static readonly HashSet Bf16SuppressedTensorChoiceGroupIds = new(); + private static bool _imatrixAvailable; public static void ResetForNewModel() { @@ -24,9 +25,14 @@ public static void ResetForNewModel() LearnedBaselineMissingByGroupAndScheme.Clear(); DisabledCombinationBaselineIds.Clear(); Bf16SuppressedTensorChoiceGroupIds.Clear(); + _imatrixAvailable = false; TensorWeightScheme.ResetAllRuntimeBans(); } + public static void SetImatrixAvailability(bool available) => _imatrixAvailable = available; + + public static bool HasUsableImatrix() => _imatrixAvailable; + public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) { if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || @@ -187,6 +193,7 @@ private static bool HasAnyExplicitSchemeAllowed(TensorGroup group) return TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Where(x => _imatrixAvailable || !x.RequiresImatrix) .Any(x => !x.IsBannedFor(group)); } @@ -194,6 +201,7 @@ public static IReadOnlyList GetActiveCombinationBaselines() { return BaselineQuants.All .Where(x => x.BaseConversionBase != null) + .Where(x => _imatrixAvailable || !x.RequiresImatrix) .Where(x => !DisabledCombinationBaselineIds.Contains(x.UniqueId)) .OrderBy(x => x.UniqueId) .ToList(); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 378905f..67843eb 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -20,7 +20,9 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( var result = new RequiredSampleGenerationResult(); - foreach (var baseline in BaselineQuants.All.OrderBy(x => x.UniqueId)) + foreach (var baseline in BaselineQuants.All + .Where(x => RuntimeSearchSpace.HasUsableImatrix() || !x.RequiresImatrix) + .OrderBy(x => x.UniqueId)) { result.Plans.Add(new RequiredSamplePlan { @@ -126,6 +128,7 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var schemes = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Where(x => RuntimeSearchSpace.HasUsableImatrix() || !x.RequiresImatrix) .OrderBy(x => x.UniqueId) .ToList(); @@ -295,6 +298,7 @@ public static IEnumerable> GenerateTensorConfigBatches( private static TensorWeightScheme? GetSmallestAllowedProbeSchemeForGroup(TensorGroup group) { var allowedIds = TensorWeightScheme.All_Allowed_Hybrid_Quants + .Where(x => RuntimeSearchSpace.HasUsableImatrix() || !x.RequiresImatrix) .Select(x => x.UniqueId) .ToHashSet(); diff --git a/MagicQuant/Models/ImatrixModels.cs b/MagicQuant/Models/ImatrixModels.cs new file mode 100644 index 0000000..740d26a --- /dev/null +++ b/MagicQuant/Models/ImatrixModels.cs @@ -0,0 +1,62 @@ +namespace MagicQuant.Models; + +public enum ImatrixSourceKind +{ + Https = 1, + HfDataset = 2, + LocalDatasetFile = 3 +} + +public sealed class ImatrixRequest +{ + public bool UseImatrix { get; init; } + public bool ForceRebuild { get; init; } + + public string? ImatrixUrl { get; init; } + + public string? DatasetRepo { get; init; } + public string? DatasetSplit { get; init; } + public string? DatasetConfig { get; init; } + + public string? LocalDatasetFile { get; init; } + + public string ModelDirectory { get; init; } = default!; + public string MagicQuantDirectory { get; init; } = default!; +} + +public sealed class ImatrixEnsureResult +{ + public bool Enabled { get; init; } + public bool Available { get; init; } + public bool Rebuilt { get; init; } + public string? CanonicalImatrixPath { get; init; } + public ImatrixSourceKind? SourceKind { get; init; } +} + +public sealed class ImatrixSuccessSidecar +{ + public string Status { get; init; } = "success"; + public DateTime CompletedUtc { get; init; } + public string ArtifactType { get; init; } = "imatrix"; + public string CanonicalFileName { get; init; } = "imatrix.dat"; + public string CanonicalPath { get; init; } = string.Empty; + public string SourceKind { get; init; } = string.Empty; + public string SourceIdentity { get; init; } = string.Empty; + public string? Split { get; init; } + public string? Config { get; init; } + public string Sha256 { get; init; } = string.Empty; + public long FileSizeBytes { get; init; } + public string BuilderVersion { get; init; } = "mvp-v1"; +} + +public sealed class ImatrixMetadataSidecar +{ + public string SourceKind { get; init; } = string.Empty; + public string? OriginalUrl { get; init; } + public string? OriginalDownloadName { get; init; } + public string? DatasetRepo { get; init; } + public string? DatasetConfig { get; init; } + public string? DatasetSplit { get; init; } + public string? LocalDatasetFile { get; init; } + public string Notes { get; init; } = "Renamed to canonical imatrix.dat after acquisition/build"; +} diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index eb615f4..727825d 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -18,7 +18,11 @@ // OPTIONAL: Manually append hardcoded flags for testing specific scenarios // Example: If you want to test "evolution --iterations 10" every time you debug -string manualFlags = @"--model-dir ""/mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/"""; +string manualFlags = + @"--model-dir ""/mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/"" + --use-imatrix + --imatrix-dataset-local-file ""/home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1.yaml"" + --imatrix-dataset-split ""train"""; args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs new file mode 100644 index 0000000..58981ad --- /dev/null +++ b/MagicQuant/Services/ImatrixService.cs @@ -0,0 +1,419 @@ +using System.Security.Cryptography; +using System.Text.Json; +using MagicQuant.Helpers; +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class ImatrixService +{ + private readonly JsonSerializerOptions _json = new() + { + WriteIndented = true + }; + + public async Task EnsureImatrixAsync(ImatrixRequest request, CancellationToken ct = default) + { + if (!request.UseImatrix) + { + Cache.IsImatrixAvailable = false; + Cache.ActiveImatrixPath = null; + RuntimeSearchSpace.SetImatrixAvailability(false); + return new ImatrixEnsureResult { Enabled = false, Available = false }; + } + + ValidateRequest(request, out var sourceKind, out var sourceIdentity); + + string imatrixDir = Path.Combine(request.MagicQuantDirectory, "imatrix"); + Directory.CreateDirectory(imatrixDir); + + string datPath = Path.Combine(imatrixDir, "imatrix.dat"); + string successPath = Path.Combine(imatrixDir, "imatrix.success.json"); + string metadataPath = Path.Combine(imatrixDir, "imatrix.metadata.json"); + string buildLogPath = Path.Combine(imatrixDir, "imatrix.build.log"); + + if (request.ForceRebuild) + await CleanupArtifactsAsync(datPath, successPath, metadataPath, buildLogPath); + + bool shouldRebuild = await ShouldRebuildAsync(request, sourceKind, datPath, successPath, metadataPath); + + if (shouldRebuild) + { + await CleanupArtifactsAsync(datPath, successPath, metadataPath, buildLogPath); + await AcquireImatrixAsync(request, sourceKind, sourceIdentity, datPath, metadataPath, successPath, buildLogPath, ct); + + Cache.IsImatrixAvailable = true; + Cache.ActiveImatrixPath = datPath; + RuntimeSearchSpace.SetImatrixAvailability(true); + + return new ImatrixEnsureResult + { + Enabled = true, + Available = true, + Rebuilt = true, + CanonicalImatrixPath = datPath, + SourceKind = sourceKind + }; + } + + Cache.IsImatrixAvailable = true; + Cache.ActiveImatrixPath = datPath; + RuntimeSearchSpace.SetImatrixAvailability(true); + + return new ImatrixEnsureResult + { + Enabled = true, + Available = true, + Rebuilt = false, + CanonicalImatrixPath = datPath, + SourceKind = sourceKind + }; + } + + public bool ShouldUseImatrixForQuant(HybridQuant quant) + { + if (!Cache.UseImatrix || !Cache.IsImatrixAvailable || string.IsNullOrWhiteSpace(Cache.ActiveImatrixPath)) + return false; + + if (quant.BaseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId) + return false; + + var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; + return torchType != Cache.MainTorchType.F16 && torchType != Cache.MainTorchType.F32; + } + + public string GetCanonicalImatrixPath() + { + if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + throw new InvalidOperationException("Cache.ModelMagicQuantDirectory is not set."); + + return Path.Combine(Cache.ModelMagicQuantDirectory, "imatrix", "imatrix.dat"); + } + + private static void ValidateRequest(ImatrixRequest request, out ImatrixSourceKind sourceKind, out string sourceIdentity) + { + int activeModes = 0; + + bool urlMode = !string.IsNullOrWhiteSpace(request.ImatrixUrl); + bool hfMode = !string.IsNullOrWhiteSpace(request.DatasetRepo); + bool localMode = !string.IsNullOrWhiteSpace(request.LocalDatasetFile); + + if (urlMode) activeModes++; + if (hfMode) activeModes++; + if (localMode) activeModes++; + + if (activeModes != 1) + { + throw new InvalidOperationException( + "When --use-imatrix is true, exactly one source mode must be provided: --imatrix-url OR --imatrix-dataset-repo OR --imatrix-dataset-local-file."); + } + + if (urlMode) + { + if (!Uri.TryCreate(request.ImatrixUrl, UriKind.Absolute, out var parsed) || + (parsed.Scheme != Uri.UriSchemeHttps && parsed.Scheme != Uri.UriSchemeHttp)) + { + throw new InvalidOperationException("--imatrix-url must be a valid http/https URL."); + } + + sourceKind = ImatrixSourceKind.Https; + sourceIdentity = request.ImatrixUrl!; + return; + } + + if (hfMode) + { + if (string.IsNullOrWhiteSpace(request.DatasetSplit)) + throw new InvalidOperationException("--imatrix-dataset-split is required with --imatrix-dataset-repo."); + + sourceKind = ImatrixSourceKind.HfDataset; + sourceIdentity = $"{request.DatasetRepo}:{request.DatasetConfig ?? "default"}:{request.DatasetSplit}"; + return; + } + + if (string.IsNullOrWhiteSpace(request.LocalDatasetFile)) + throw new InvalidOperationException("--imatrix-dataset-local-file cannot be empty."); + + string ext = Path.GetExtension(request.LocalDatasetFile).ToLowerInvariant(); + if (ext is not ".json" and not ".jsonl") + { + throw new InvalidOperationException( + $"Local dataset file mode supports only .json/.jsonl in MVP. Got '{ext}'. If this is a YAML recipe, add explicit recipe parsing support or use a raw JSON/JSONL corpus file."); + } + + sourceKind = ImatrixSourceKind.LocalDatasetFile; + sourceIdentity = Path.GetFullPath(request.LocalDatasetFile); + } + + private async Task ShouldRebuildAsync( + ImatrixRequest request, + ImatrixSourceKind sourceKind, + string datPath, + string successPath, + string metadataPath) + { + bool hasDat = File.Exists(datPath); + bool hasSuccess = File.Exists(successPath); + + if (hasDat && !hasSuccess) + return true; + + if (!hasDat && hasSuccess) + return true; + + if (!hasDat || !hasSuccess || !File.Exists(metadataPath)) + return true; + + var metadata = JsonSerializer.Deserialize(await File.ReadAllTextAsync(metadataPath), _json); + if (metadata == null) + return true; + + return !MetadataMatchesRequest(metadata, request, sourceKind); + } + + private static bool MetadataMatchesRequest(ImatrixMetadataSidecar metadata, ImatrixRequest request, ImatrixSourceKind sourceKind) + { + if (!string.Equals(metadata.SourceKind, ToSidecarSourceKind(sourceKind), StringComparison.OrdinalIgnoreCase)) + return false; + + return sourceKind switch + { + ImatrixSourceKind.Https => string.Equals(metadata.OriginalUrl, request.ImatrixUrl, StringComparison.Ordinal), + ImatrixSourceKind.HfDataset => + string.Equals(metadata.DatasetRepo, request.DatasetRepo, StringComparison.Ordinal) && + string.Equals(metadata.DatasetConfig, request.DatasetConfig, StringComparison.Ordinal) && + string.Equals(metadata.DatasetSplit, request.DatasetSplit, StringComparison.Ordinal), + ImatrixSourceKind.LocalDatasetFile => + string.Equals(metadata.LocalDatasetFile, Path.GetFullPath(request.LocalDatasetFile!), StringComparison.Ordinal) && + string.Equals(metadata.DatasetSplit, request.DatasetSplit, StringComparison.Ordinal), + _ => false + }; + } + + private async Task AcquireImatrixAsync( + ImatrixRequest request, + ImatrixSourceKind sourceKind, + string sourceIdentity, + string datPath, + string metadataPath, + string successPath, + string buildLogPath, + CancellationToken ct) + { + switch (sourceKind) + { + case ImatrixSourceKind.Https: + await AcquireFromHttpsAsync(request, datPath, buildLogPath, ct); + break; + case ImatrixSourceKind.LocalDatasetFile: + await BuildFromLocalDatasetAsync(request, datPath, buildLogPath, ct); + break; + case ImatrixSourceKind.HfDataset: + await BuildFromHfDatasetAsync(request, datPath, buildLogPath, ct); + break; + default: + throw new InvalidOperationException($"Unknown imatrix source kind '{sourceKind}'."); + } + + var metadata = new ImatrixMetadataSidecar + { + SourceKind = ToSidecarSourceKind(sourceKind), + OriginalUrl = request.ImatrixUrl, + OriginalDownloadName = request.ImatrixUrl == null ? null : Path.GetFileName(new Uri(request.ImatrixUrl).AbsolutePath), + DatasetRepo = request.DatasetRepo, + DatasetConfig = request.DatasetConfig, + DatasetSplit = request.DatasetSplit, + LocalDatasetFile = string.IsNullOrWhiteSpace(request.LocalDatasetFile) ? null : Path.GetFullPath(request.LocalDatasetFile) + }; + + await File.WriteAllTextAsync(metadataPath, JsonSerializer.Serialize(metadata, _json), ct); + + var fileInfo = new FileInfo(datPath); + if (!fileInfo.Exists || fileInfo.Length == 0) + throw new InvalidOperationException("Imatrix acquisition completed but canonical imatrix.dat is missing or empty."); + + var success = new ImatrixSuccessSidecar + { + CompletedUtc = DateTime.UtcNow, + CanonicalPath = datPath, + SourceKind = ToSidecarSourceKind(sourceKind), + SourceIdentity = sourceIdentity, + Split = request.DatasetSplit, + Config = request.DatasetConfig, + Sha256 = await ComputeSha256Async(datPath, ct), + FileSizeBytes = fileInfo.Length + }; + + await File.WriteAllTextAsync(successPath, JsonSerializer.Serialize(success, _json), ct); + } + + private static async Task CleanupArtifactsAsync(params string[] paths) + { + foreach (var path in paths) + { + if (File.Exists(path)) + await HardDeleteHelper.DeleteFileIfExistsAsync(path); + } + } + + private static string ToSidecarSourceKind(ImatrixSourceKind kind) => kind switch + { + ImatrixSourceKind.Https => "https", + ImatrixSourceKind.HfDataset => "hf_dataset", + ImatrixSourceKind.LocalDatasetFile => "local_dataset_file", + _ => "unknown" + }; + + private static async Task ComputeSha256Async(string path, CancellationToken ct) + { + await using var stream = File.OpenRead(path); + var hash = await SHA256.HashDataAsync(stream, ct); + return Convert.ToHexString(hash).ToLowerInvariant(); + } + + private static async Task AcquireFromHttpsAsync(ImatrixRequest request, string datPath, string buildLogPath, CancellationToken ct) + { + string tempPath = datPath + ".source.tmp"; + + using var client = new HttpClient(); + await using (var sourceStream = await client.GetStreamAsync(request.ImatrixUrl!, ct)) + await using (var destinationStream = File.Create(tempPath)) + { + await sourceStream.CopyToAsync(destinationStream, ct); + } + + var tempInfo = new FileInfo(tempPath); + if (!tempInfo.Exists || tempInfo.Length == 0) + throw new InvalidOperationException("Downloaded imatrix file is empty."); + + if (File.Exists(datPath)) + File.Delete(datPath); + + File.Move(tempPath, datPath); + await File.WriteAllTextAsync(buildLogPath, $"Downloaded from {request.ImatrixUrl} at {DateTime.UtcNow:O}{Environment.NewLine}", ct); + + AnsiConsole.MarkupLine($"[green]Imatrix downloaded and normalized:[/] {Markup.Escape(datPath)}"); + } + + private async Task BuildFromLocalDatasetAsync(ImatrixRequest request, string datPath, string buildLogPath, CancellationToken ct) + { + string datasetPath = Path.GetFullPath(request.LocalDatasetFile!); + if (!File.Exists(datasetPath)) + throw new FileNotFoundException($"Local dataset file not found: {datasetPath}"); + + await BuildImatrixFromDatasetTextAsync(datasetPath, datPath, buildLogPath, ct); + } + + private async Task BuildFromHfDatasetAsync(ImatrixRequest request, string datPath, string buildLogPath, CancellationToken ct) + { + string tempJsonl = Path.Combine(Path.GetDirectoryName(datPath)!, "hf-dataset.export.jsonl"); + string python = ResolvePythonExecutableOrThrow(); + string scriptPath = Path.Combine(Path.GetDirectoryName(datPath)!, "build_hf_imatrix_dataset.py"); + + string script = """ +import json +from datasets import load_dataset +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--repo', required=True) +parser.add_argument('--split', required=True) +parser.add_argument('--config', required=False) +parser.add_argument('--out', required=True) +args = parser.parse_args() + +if args.config: + ds = load_dataset(args.repo, args.config, split=args.split) +else: + ds = load_dataset(args.repo, split=args.split) + +with open(args.out, 'w', encoding='utf-8') as f: + for row in ds: + f.write(json.dumps(row, ensure_ascii=False) + '\n') +"""; + + await File.WriteAllTextAsync(scriptPath, script, ct); + + var psi = new System.Diagnostics.ProcessStartInfo + { + FileName = python, + Arguments = + $"\"{scriptPath}\" --repo \"{request.DatasetRepo}\" --split \"{request.DatasetSplit}\" " + + (string.IsNullOrWhiteSpace(request.DatasetConfig) ? string.Empty : $"--config \"{request.DatasetConfig}\" ") + + $"--out \"{tempJsonl}\"", + RedirectStandardError = true, + RedirectStandardOutput = true, + UseShellExecute = false + }; + + using var p = System.Diagnostics.Process.Start(psi) + ?? throw new InvalidOperationException("Failed to start Python process for HF dataset export."); + + string stdout = await p.StandardOutput.ReadToEndAsync(); + string stderr = await p.StandardError.ReadToEndAsync(); + await p.WaitForExitAsync(ct); + + await File.WriteAllTextAsync(buildLogPath, stdout + Environment.NewLine + stderr, ct); + + if (p.ExitCode != 0) + throw new InvalidOperationException("Failed to export HF dataset for imatrix generation. See imatrix.build.log."); + + await BuildImatrixFromDatasetTextAsync(tempJsonl, datPath, buildLogPath, ct); + } + + private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, string datPath, string buildLogPath, CancellationToken ct) + { + string llamaBin = Cache.LlamaBin ?? throw new InvalidOperationException("Cache.LlamaBin not set."); + string binaryName = OperatingSystem.IsWindows() ? "llama-imatrix.exe" : "llama-imatrix"; + string imatrixBin = Path.Combine(llamaBin, binaryName); + + if (!File.Exists(imatrixBin)) + throw new InvalidOperationException($"Missing {binaryName}. Cannot build imatrix from dataset sources."); + + string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; + string torchType = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + string baseModelPath = Path.Combine(Cache.ModelMagicQuantDirectory!, "GGUF", $"{modelName}-{torchType}.gguf"); + + if (!File.Exists(baseModelPath)) + throw new InvalidOperationException($"Base model GGUF is required before dataset-based imatrix build. Missing: {baseModelPath}"); + + var psi = new System.Diagnostics.ProcessStartInfo + { + FileName = imatrixBin, + Arguments = $"-m \"{baseModelPath}\" -f \"{datasetPath}\" -o \"{datPath}\"", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false + }; + + using var p = System.Diagnostics.Process.Start(psi) + ?? throw new InvalidOperationException("Failed to start llama-imatrix process."); + + string stdout = await p.StandardOutput.ReadToEndAsync(); + string stderr = await p.StandardError.ReadToEndAsync(); + await p.WaitForExitAsync(ct); + + await File.AppendAllTextAsync(buildLogPath, stdout + Environment.NewLine + stderr, ct); + + if (p.ExitCode != 0) + throw new InvalidOperationException("llama-imatrix failed. See imatrix.build.log."); + } + + private static string ResolvePythonExecutableOrThrow() + { + if (string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) + throw new InvalidOperationException("Cache.MagicQuantDirectory is not set."); + + var py = new PythonManager(Cache.MagicQuantDirectory); + string pythonExe = py.GetPythonExecutable(); + + if (!File.Exists(pythonExe)) + throw new InvalidOperationException( + $"Python environment is missing or broken at '{pythonExe}'. Re-run initialize-llama-cpp."); + + return pythonExe; + } +} diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 52fd9a1..6099be6 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -209,9 +209,6 @@ public async Task AnalyzeAndApplyFinalAsync( if (candidate.Scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) continue; - if (candidate.Scheme.RequiresImatrix) - continue; - bool hardFail = candidate.PplDeltaPercent >= IsolationPruningConfig.MaximumIsolationPplDeltaPercent || candidate.Kld >= IsolationPruningConfig.MaximumIsolationKld; @@ -421,7 +418,6 @@ private static List GetActiveExplicitCandidates(TensorGroup grou { return candidates .Where(x => x.Scheme.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Where(x => !x.Scheme.RequiresImatrix) .Where(x => !RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, x.Scheme)) .ToList(); } diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index 0a56aa2..5ed2613 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -103,6 +103,7 @@ public async Task AnalyzeAndApplyAsync(Cancellatio var explicitSchemes = TensorWeightScheme.All_Allowed_Hybrid_Quants .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) + .Where(x => RuntimeSearchSpace.HasUsableImatrix() || !x.RequiresImatrix) .OrderBy(x => x.UniqueId) .ToList(); diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index d306050..78ea28c 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -48,6 +48,7 @@ public class QuantizationService private readonly PythonManager _python; private readonly SemaphoreSlim _cpuQuantLock; private readonly int _maxConcurrentQuantizations; + private readonly ImatrixService _imatrixService; private static readonly SemaphoreSlim BaseModelLock = new(1, 1); private const byte UnknownTensorGroupId = 255; @@ -72,6 +73,7 @@ public QuantizationService(BenchmarkService benchmarker) _ggufDir = Path.Combine(Cache.ModelMagicQuantDirectory, "GGUF"); _benchDir = Path.Combine(Cache.ModelMagicQuantDirectory, "Benchmarks"); + _imatrixService = new ImatrixService(); Directory.CreateDirectory(_ggufDir); Directory.CreateDirectory(_benchDir); @@ -797,6 +799,15 @@ private async Task RunLlamaQuantizeAsync(string inp args.Add($"--tensor-type \"{overrideItem.TensorName}={overrideItem.SchemeName}\""); } + if (ShouldApplyImatrix(quant)) + { + string imatrixPath = _imatrixService.GetCanonicalImatrixPath(); + if (!File.Exists(imatrixPath)) + throw new InvalidOperationException($"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); + + args.Add($"--imatrix \"{imatrixPath}\""); + } + args.Add($"\"{inputFile}\""); args.Add($"\"{outputFile}\""); args.Add(ResolveQuantizeBaseArgument(quant, concreteOverrides)); @@ -859,6 +870,11 @@ private static string ResolveQuantizeBaseArgument( return ResolveBaseName(quant.BaseQuant); } + private bool ShouldApplyImatrix(HybridQuant quant) + { + return _imatrixService.ShouldUseImatrixForQuant(quant); + } + public async Task ClearLearnedBaselineTensorMappingsAsync(CancellationToken ct = default) { await using var db = new MagicQuantContext(); From cd4eeb9b33764b1e8c0149f74f90e44b6cfdc33e Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 15:41:16 -0400 Subject: [PATCH 078/258] Fix imatrix debug path and explicit no-imatrix exclusions --- MagicQuant/Program.cs | 2 +- MagicQuant/Services/ImatrixService.cs | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 727825d..29916d4 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -21,7 +21,7 @@ string manualFlags = @"--model-dir ""/mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/"" --use-imatrix - --imatrix-dataset-local-file ""/home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1.yaml"" + --imatrix-dataset-local-file ""/home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1.jsonl"" --imatrix-dataset-split ""train"""; args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index 58981ad..b8c9724 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -81,8 +81,13 @@ public bool ShouldUseImatrixForQuant(HybridQuant quant) if (quant.BaseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId) return false; - var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; - return torchType != Cache.MainTorchType.F16 && torchType != Cache.MainTorchType.F32; + string baseName = quant.BaseQuant.Names.IsDefaultOrEmpty + ? string.Empty + : quant.BaseQuant.Names[0]; + + return !baseName.Equals("BF16", StringComparison.OrdinalIgnoreCase) && + !baseName.Equals("F16", StringComparison.OrdinalIgnoreCase) && + !baseName.Equals("F32", StringComparison.OrdinalIgnoreCase); } public string GetCanonicalImatrixPath() From 3de97d6f348f31c582b764e8d2e46d94fbf7dcda Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 15:48:44 -0400 Subject: [PATCH 079/258] Use native precision helper for imatrix exclusion --- MagicQuant/Services/ImatrixService.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index b8c9724..29f8649 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -81,13 +81,11 @@ public bool ShouldUseImatrixForQuant(HybridQuant quant) if (quant.BaseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId) return false; - string baseName = quant.BaseQuant.Names.IsDefaultOrEmpty - ? string.Empty - : quant.BaseQuant.Names[0]; + var baseScheme = quant.BaseQuant.DefaultTensorScheme; + if (baseScheme != null && TensorWeightScheme.IsNativePrecisionScheme(baseScheme)) + return false; - return !baseName.Equals("BF16", StringComparison.OrdinalIgnoreCase) && - !baseName.Equals("F16", StringComparison.OrdinalIgnoreCase) && - !baseName.Equals("F32", StringComparison.OrdinalIgnoreCase); + return true; } public string GetCanonicalImatrixPath() From 5269ce7b06a63f59249c703f8ff757b0761cf254 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 15:54:07 -0400 Subject: [PATCH 080/258] Fix imatrix status markup interpolation compile issue --- MagicQuant/Commands/Evolution.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 87620a4..bf4cef9 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -114,9 +114,10 @@ public async Task Run(List args) var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest, ct: default); if (imatrixEnsureResult.Enabled) { + string canonicalPath = imatrixEnsureResult.CanonicalImatrixPath ?? "n/a"; + string rebuiltText = imatrixEnsureResult.Rebuilt ? "yes" : "no"; AnsiConsole.MarkupLine( - $"[green]Imatrix active:[/] {Markup.Escape(imatrixEnsureResult.CanonicalImatrixPath ?? \"n/a\")} " + - $"(rebuilt={(imatrixEnsureResult.Rebuilt ? "yes" : "no")})"); + $"[green]Imatrix active:[/] {Markup.Escape(canonicalPath)} (rebuilt={rebuiltText})"); } else { From bcd0270704a639015d9c7e085fe194e1125ee952 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 16:19:04 -0400 Subject: [PATCH 081/258] Add progress logging to imatrix ensure/acquisition flow --- MagicQuant/Services/ImatrixService.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index 29f8649..62c2f44 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -17,8 +17,11 @@ public sealed class ImatrixService public async Task EnsureImatrixAsync(ImatrixRequest request, CancellationToken ct = default) { + AnsiConsole.MarkupLine("[grey]Imatrix: starting ensure flow...[/]"); + if (!request.UseImatrix) { + AnsiConsole.MarkupLine("[grey]Imatrix: disabled by --use-imatrix flag (false).[/]"); Cache.IsImatrixAvailable = false; Cache.ActiveImatrixPath = null; RuntimeSearchSpace.SetImatrixAvailability(false); @@ -26,9 +29,12 @@ public async Task EnsureImatrixAsync(ImatrixRequest request } ValidateRequest(request, out var sourceKind, out var sourceIdentity); + AnsiConsole.MarkupLine( + $"[grey]Imatrix: validated source mode:[/] [cyan]{Markup.Escape(ToSidecarSourceKind(sourceKind))}[/]"); string imatrixDir = Path.Combine(request.MagicQuantDirectory, "imatrix"); Directory.CreateDirectory(imatrixDir); + AnsiConsole.MarkupLine($"[grey]Imatrix: using directory:[/] [cyan]{Markup.Escape(imatrixDir)}[/]"); string datPath = Path.Combine(imatrixDir, "imatrix.dat"); string successPath = Path.Combine(imatrixDir, "imatrix.success.json"); @@ -36,18 +42,23 @@ public async Task EnsureImatrixAsync(ImatrixRequest request string buildLogPath = Path.Combine(imatrixDir, "imatrix.build.log"); if (request.ForceRebuild) + { + AnsiConsole.MarkupLine("[yellow]Imatrix: force rebuild enabled, cleaning prior canonical artifacts...[/]"); await CleanupArtifactsAsync(datPath, successPath, metadataPath, buildLogPath); + } bool shouldRebuild = await ShouldRebuildAsync(request, sourceKind, datPath, successPath, metadataPath); if (shouldRebuild) { + AnsiConsole.MarkupLine("[grey]Imatrix: canonical artifacts missing/stale/mismatched; rebuilding now...[/]"); await CleanupArtifactsAsync(datPath, successPath, metadataPath, buildLogPath); await AcquireImatrixAsync(request, sourceKind, sourceIdentity, datPath, metadataPath, successPath, buildLogPath, ct); Cache.IsImatrixAvailable = true; Cache.ActiveImatrixPath = datPath; RuntimeSearchSpace.SetImatrixAvailability(true); + AnsiConsole.MarkupLine($"[green]Imatrix: ready (rebuilt).[/] [grey]{Markup.Escape(datPath)}[/]"); return new ImatrixEnsureResult { @@ -62,6 +73,7 @@ public async Task EnsureImatrixAsync(ImatrixRequest request Cache.IsImatrixAvailable = true; Cache.ActiveImatrixPath = datPath; RuntimeSearchSpace.SetImatrixAvailability(true); + AnsiConsole.MarkupLine($"[green]Imatrix: ready (reused existing trusted artifact).[/] [grey]{Markup.Escape(datPath)}[/]"); return new ImatrixEnsureResult { @@ -206,6 +218,9 @@ private async Task AcquireImatrixAsync( string buildLogPath, CancellationToken ct) { + AnsiConsole.MarkupLine( + $"[grey]Imatrix: acquiring from source:[/] [cyan]{Markup.Escape(ToSidecarSourceKind(sourceKind))}[/]"); + switch (sourceKind) { case ImatrixSourceKind.Https: @@ -280,6 +295,7 @@ private static async Task ComputeSha256Async(string path, CancellationTo private static async Task AcquireFromHttpsAsync(ImatrixRequest request, string datPath, string buildLogPath, CancellationToken ct) { string tempPath = datPath + ".source.tmp"; + AnsiConsole.MarkupLine($"[grey]Imatrix: downloading from URL:[/] [cyan]{Markup.Escape(request.ImatrixUrl ?? string.Empty)}[/]"); using var client = new HttpClient(); await using (var sourceStream = await client.GetStreamAsync(request.ImatrixUrl!, ct)) @@ -307,6 +323,7 @@ private async Task BuildFromLocalDatasetAsync(ImatrixRequest request, string dat if (!File.Exists(datasetPath)) throw new FileNotFoundException($"Local dataset file not found: {datasetPath}"); + AnsiConsole.MarkupLine($"[grey]Imatrix: building from local dataset file:[/] [cyan]{Markup.Escape(datasetPath)}[/]"); await BuildImatrixFromDatasetTextAsync(datasetPath, datPath, buildLogPath, ct); } @@ -315,6 +332,9 @@ private async Task BuildFromHfDatasetAsync(ImatrixRequest request, string datPat string tempJsonl = Path.Combine(Path.GetDirectoryName(datPath)!, "hf-dataset.export.jsonl"); string python = ResolvePythonExecutableOrThrow(); string scriptPath = Path.Combine(Path.GetDirectoryName(datPath)!, "build_hf_imatrix_dataset.py"); + AnsiConsole.MarkupLine( + $"[grey]Imatrix: exporting Hugging Face dataset[/] [cyan]{Markup.Escape(request.DatasetRepo ?? string.Empty)}[/]" + + $"[grey] split=[/][cyan]{Markup.Escape(request.DatasetSplit ?? string.Empty)}[/]"); string script = """ import json @@ -369,6 +389,9 @@ with open(args.out, 'w', encoding='utf-8') as f: private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, string datPath, string buildLogPath, CancellationToken ct) { + AnsiConsole.MarkupLine( + $"[grey]Imatrix: invoking llama-imatrix build from dataset:[/] [cyan]{Markup.Escape(datasetPath)}[/]"); + string llamaBin = Cache.LlamaBin ?? throw new InvalidOperationException("Cache.LlamaBin not set."); string binaryName = OperatingSystem.IsWindows() ? "llama-imatrix.exe" : "llama-imatrix"; string imatrixBin = Path.Combine(llamaBin, binaryName); From 2004fc9c2d2a1dc8bbd31452207008946145efbc Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 16:30:20 -0400 Subject: [PATCH 082/258] Improve imatrix build visibility with live process logs --- MagicQuant/Services/ImatrixService.cs | 54 +++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index 62c2f44..fbf2637 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -1,5 +1,6 @@ using System.Security.Cryptography; using System.Text.Json; +using System.Text; using MagicQuant.Helpers; using MagicQuant.Models; using MQ.DB; @@ -391,6 +392,7 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s { AnsiConsole.MarkupLine( $"[grey]Imatrix: invoking llama-imatrix build from dataset:[/] [cyan]{Markup.Escape(datasetPath)}[/]"); + AnsiConsole.MarkupLine($"[grey]Imatrix: streaming llama-imatrix output to:[/] [cyan]{Markup.Escape(buildLogPath)}[/]"); string llamaBin = Cache.LlamaBin ?? throw new InvalidOperationException("Cache.LlamaBin not set."); string binaryName = OperatingSystem.IsWindows() ? "llama-imatrix.exe" : "llama-imatrix"; @@ -418,14 +420,58 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s using var p = System.Diagnostics.Process.Start(psi) ?? throw new InvalidOperationException("Failed to start llama-imatrix process."); - string stdout = await p.StandardOutput.ReadToEndAsync(); - string stderr = await p.StandardError.ReadToEndAsync(); - await p.WaitForExitAsync(ct); + await using var buildLog = new StreamWriter(buildLogPath, append: true, Encoding.UTF8); + var startedUtc = DateTime.UtcNow; + int outputLineCount = 0; + + Task stdoutTask = PumpProcessStreamAsync(p.StandardOutput, "stdout", buildLog, line => + { + outputLineCount++; + AnsiConsole.MarkupLine($"[grey]Imatrix[{Markup.Escape("stdout")}]:[/] {Markup.Escape(line)}"); + }, ct); + + Task stderrTask = PumpProcessStreamAsync(p.StandardError, "stderr", buildLog, line => + { + outputLineCount++; + AnsiConsole.MarkupLine($"[grey]Imatrix[{Markup.Escape("stderr")}]:[/] {Markup.Escape(line)}"); + }, ct); + + while (!p.HasExited) + { + await Task.Delay(TimeSpan.FromSeconds(30), ct); + var elapsed = DateTime.UtcNow - startedUtc; + AnsiConsole.MarkupLine( + $"[grey]Imatrix: llama-imatrix still running... elapsed[/] [cyan]{elapsed:hh\\:mm\\:ss}[/][grey], output lines[/] [cyan]{outputLineCount}[/]"); + } - await File.AppendAllTextAsync(buildLogPath, stdout + Environment.NewLine + stderr, ct); + await Task.WhenAll(stdoutTask, stderrTask); + await p.WaitForExitAsync(ct); + await buildLog.FlushAsync(); if (p.ExitCode != 0) throw new InvalidOperationException("llama-imatrix failed. See imatrix.build.log."); + + AnsiConsole.MarkupLine($"[green]Imatrix: llama-imatrix completed successfully.[/] [grey]exit={p.ExitCode}[/]"); + } + + private static async Task PumpProcessStreamAsync( + StreamReader reader, + string label, + StreamWriter buildLog, + Action onLine, + CancellationToken ct) + { + while (true) + { + ct.ThrowIfCancellationRequested(); + string? line = await reader.ReadLineAsync(ct); + if (line == null) + break; + + onLine(line); + await buildLog.WriteLineAsync($"[{label}] {line}"); + await buildLog.FlushAsync(); + } } private static string ResolvePythonExecutableOrThrow() From 886f1631fd8e44b9713a64bb1151f691581b660a Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 16:37:45 -0400 Subject: [PATCH 083/258] Flatten local JSON datasets before llama-imatrix build --- MagicQuant/Services/ImatrixService.cs | 223 +++++++++++++++++++++++++- 1 file changed, 218 insertions(+), 5 deletions(-) diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index fbf2637..9121c1d 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -1,6 +1,7 @@ using System.Security.Cryptography; using System.Text.Json; using System.Text; +using System.Text.RegularExpressions; using MagicQuant.Helpers; using MagicQuant.Models; using MQ.DB; @@ -325,7 +326,23 @@ private async Task BuildFromLocalDatasetAsync(ImatrixRequest request, string dat throw new FileNotFoundException($"Local dataset file not found: {datasetPath}"); AnsiConsole.MarkupLine($"[grey]Imatrix: building from local dataset file:[/] [cyan]{Markup.Escape(datasetPath)}[/]"); - await BuildImatrixFromDatasetTextAsync(datasetPath, datPath, buildLogPath, ct); + string exportedCorpusPath = Path.Combine(Path.GetDirectoryName(datPath)!, "local-dataset.export.txt"); + var exportSummary = await ExportLocalDatasetToCorpusAsync(datasetPath, exportedCorpusPath, buildLogPath, ct); + + if (exportSummary.StructuredRows > 0) + { + AnsiConsole.MarkupLine( + $"[yellow]Imatrix warning:[/] input appears to be structured JSON rows " + + $"([cyan]{exportSummary.StructuredRows}[/]/[cyan]{exportSummary.TotalRows}[/]). " + + "Flattening extracted text into temporary corpus before llama-imatrix."); + } + + AnsiConsole.MarkupLine( + $"[grey]Imatrix: local dataset export complete.[/] rows=[cyan]{exportSummary.TotalRows}[/], " + + $"structured=[cyan]{exportSummary.StructuredRows}[/], extracted_text_blocks=[cyan]{exportSummary.ExtractedTextBlocks}[/], " + + $"corpus=[cyan]{Markup.Escape(exportedCorpusPath)}[/]"); + + await BuildImatrixFromDatasetTextAsync(exportedCorpusPath, datPath, buildLogPath, ct); } private async Task BuildFromHfDatasetAsync(ImatrixRequest request, string datPath, string buildLogPath, CancellationToken ct) @@ -417,20 +434,28 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s UseShellExecute = false }; + string launchedCommand = $"\"{imatrixBin}\" {psi.Arguments}"; + AnsiConsole.MarkupLine($"[grey]Imatrix: launching command:[/] [cyan]{Markup.Escape(launchedCommand)}[/]"); + using var p = System.Diagnostics.Process.Start(psi) ?? throw new InvalidOperationException("Failed to start llama-imatrix process."); await using var buildLog = new StreamWriter(buildLogPath, append: true, Encoding.UTF8); + await buildLog.WriteLineAsync($"[{DateTime.UtcNow:O}] Launch: {launchedCommand}"); + await buildLog.FlushAsync(); + + using var writeLock = new SemaphoreSlim(1, 1); var startedUtc = DateTime.UtcNow; + var maxRuntime = TimeSpan.FromHours(2); int outputLineCount = 0; - Task stdoutTask = PumpProcessStreamAsync(p.StandardOutput, "stdout", buildLog, line => + Task stdoutTask = PumpProcessStreamAsync(p.StandardOutput, "stdout", buildLog, writeLock, line => { outputLineCount++; AnsiConsole.MarkupLine($"[grey]Imatrix[{Markup.Escape("stdout")}]:[/] {Markup.Escape(line)}"); }, ct); - Task stderrTask = PumpProcessStreamAsync(p.StandardError, "stderr", buildLog, line => + Task stderrTask = PumpProcessStreamAsync(p.StandardError, "stderr", buildLog, writeLock, line => { outputLineCount++; AnsiConsole.MarkupLine($"[grey]Imatrix[{Markup.Escape("stderr")}]:[/] {Markup.Escape(line)}"); @@ -440,6 +465,16 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s { await Task.Delay(TimeSpan.FromSeconds(30), ct); var elapsed = DateTime.UtcNow - startedUtc; + + if (elapsed > maxRuntime) + { + await buildLog.WriteLineAsync($"[{DateTime.UtcNow:O}] Timeout after {elapsed}. Killing llama-imatrix."); + await buildLog.FlushAsync(); + p.Kill(entireProcessTree: true); + throw new TimeoutException( + $"llama-imatrix exceeded safeguard runtime of {maxRuntime}. Process was terminated. See imatrix.build.log."); + } + AnsiConsole.MarkupLine( $"[grey]Imatrix: llama-imatrix still running... elapsed[/] [cyan]{elapsed:hh\\:mm\\:ss}[/][grey], output lines[/] [cyan]{outputLineCount}[/]"); } @@ -454,10 +489,178 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s AnsiConsole.MarkupLine($"[green]Imatrix: llama-imatrix completed successfully.[/] [grey]exit={p.ExitCode}[/]"); } + private static async Task ExportLocalDatasetToCorpusAsync( + string datasetPath, + string exportedCorpusPath, + string buildLogPath, + CancellationToken ct) + { + string ext = Path.GetExtension(datasetPath).ToLowerInvariant(); + if (ext is not ".json" and not ".jsonl") + throw new InvalidOperationException($"Unsupported local dataset extension '{ext}'."); + + int totalRows = 0; + int structuredRows = 0; + int extractedTextBlocks = 0; + + await using var writer = new StreamWriter(exportedCorpusPath, false, Encoding.UTF8); + + if (ext == ".jsonl") + { + using var reader = new StreamReader(datasetPath, Encoding.UTF8); + while (!reader.EndOfStream) + { + ct.ThrowIfCancellationRequested(); + string? line = await reader.ReadLineAsync(); + if (string.IsNullOrWhiteSpace(line)) + continue; + + totalRows++; + string trimmed = line.TrimStart(); + bool looksStructured = trimmed.StartsWith("{", StringComparison.Ordinal) || trimmed.StartsWith("[", StringComparison.Ordinal); + if (looksStructured) + structuredRows++; + + foreach (string text in ExtractCorpusTextFromJsonPayload(line)) + { + await writer.WriteLineAsync(text); + await writer.WriteLineAsync(); + extractedTextBlocks++; + } + } + } + else + { + string json = await File.ReadAllTextAsync(datasetPath, ct); + using var doc = JsonDocument.Parse(json); + + if (doc.RootElement.ValueKind is JsonValueKind.Object or JsonValueKind.Array) + structuredRows = 1; + + foreach (string text in ExtractCorpusTextFromElement(doc.RootElement)) + { + await writer.WriteLineAsync(text); + await writer.WriteLineAsync(); + extractedTextBlocks++; + } + + totalRows = doc.RootElement.ValueKind == JsonValueKind.Array + ? doc.RootElement.GetArrayLength() + : 1; + } + + await writer.FlushAsync(); + + if (extractedTextBlocks == 0) + throw new InvalidOperationException( + $"No usable text content was extracted from local dataset file '{datasetPath}'."); + + await File.AppendAllTextAsync( + buildLogPath, + $"[{DateTime.UtcNow:O}] Local dataset export: src={datasetPath}, out={exportedCorpusPath}, " + + $"rows={totalRows}, structured_rows={structuredRows}, extracted_text_blocks={extractedTextBlocks}{Environment.NewLine}", + ct); + + return new LocalDatasetExportSummary(totalRows, structuredRows, extractedTextBlocks); + } + + private static IEnumerable ExtractCorpusTextFromJsonPayload(string payload) + { + try + { + using var doc = JsonDocument.Parse(payload); + return ExtractCorpusTextFromElement(doc.RootElement).ToList(); + } + catch (JsonException) + { + if (LooksLikeUsefulText(payload)) + return new[] { payload.Trim() }; + + return Array.Empty(); + } + } + + private static IEnumerable ExtractCorpusTextFromElement(JsonElement element) + { + var texts = new List(); + CollectText(element, texts); + + // de-dup while preserving order + var seen = new HashSet(StringComparer.Ordinal); + foreach (var text in texts) + { + string normalized = Regex.Replace(text.Trim(), "\\s+", " "); + if (normalized.Length == 0) + continue; + + if (seen.Add(normalized)) + yield return normalized; + } + } + + private static void CollectText(JsonElement element, List sink) + { + switch (element.ValueKind) + { + case JsonValueKind.String: + string value = element.GetString() ?? string.Empty; + if (LooksLikeUsefulText(value)) + sink.Add(value); + break; + case JsonValueKind.Array: + foreach (var item in element.EnumerateArray()) + CollectText(item, sink); + break; + case JsonValueKind.Object: + foreach (var prop in element.EnumerateObject()) + { + if (prop.Value.ValueKind == JsonValueKind.String) + { + string text = prop.Value.GetString() ?? string.Empty; + if (IsLikelyTextFieldName(prop.Name) || LooksLikeUsefulText(text)) + sink.Add(text); + } + else + { + CollectText(prop.Value, sink); + } + } + break; + } + } + + private static bool LooksLikeUsefulText(string value) + { + string trimmed = value.Trim(); + if (trimmed.Length < 4) + return false; + + bool hasLetter = trimmed.Any(char.IsLetter); + bool hasWordBreak = trimmed.Contains(' ') || trimmed.Contains('\t') || trimmed.Contains('\n'); + return hasLetter && (hasWordBreak || trimmed.Length >= 20); + } + + private static bool IsLikelyTextFieldName(string fieldName) => + fieldName.Equals("text", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("content", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("prompt", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("completion", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("response", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("instruction", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("input", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("output", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("question", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("answer", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("value", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("body", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("message", StringComparison.OrdinalIgnoreCase) || + fieldName.Equals("messages", StringComparison.OrdinalIgnoreCase); + private static async Task PumpProcessStreamAsync( StreamReader reader, string label, StreamWriter buildLog, + SemaphoreSlim writeLock, Action onLine, CancellationToken ct) { @@ -469,11 +672,21 @@ private static async Task PumpProcessStreamAsync( break; onLine(line); - await buildLog.WriteLineAsync($"[{label}] {line}"); - await buildLog.FlushAsync(); + await writeLock.WaitAsync(ct); + try + { + await buildLog.WriteLineAsync($"[{label}] {line}"); + await buildLog.FlushAsync(); + } + finally + { + writeLock.Release(); + } } } + private sealed record LocalDatasetExportSummary(int TotalRows, int StructuredRows, int ExtractedTextBlocks); + private static string ResolvePythonExecutableOrThrow() { if (string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) From 3680b6008a34564cff599b4ac4140420548467dc Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 16:58:20 -0400 Subject: [PATCH 084/258] Unify local split semantics for imatrix dataset extraction --- MagicQuant/Program.cs | 2 +- MagicQuant/Services/ImatrixService.cs | 190 +++++++++++++++++++++++--- 2 files changed, 170 insertions(+), 22 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 29916d4..3dcfaa0 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -22,7 +22,7 @@ @"--model-dir ""/mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/"" --use-imatrix --imatrix-dataset-local-file ""/home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1.jsonl"" - --imatrix-dataset-split ""train"""; + --imatrix-dataset-split ""text"""; args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index 9121c1d..fb77962 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -196,16 +196,18 @@ private static bool MetadataMatchesRequest(ImatrixMetadataSidecar metadata, Imat if (!string.Equals(metadata.SourceKind, ToSidecarSourceKind(sourceKind), StringComparison.OrdinalIgnoreCase)) return false; + string effectiveSplit = GetEffectiveSplitForMetadata(request, sourceKind); + return sourceKind switch { ImatrixSourceKind.Https => string.Equals(metadata.OriginalUrl, request.ImatrixUrl, StringComparison.Ordinal), ImatrixSourceKind.HfDataset => string.Equals(metadata.DatasetRepo, request.DatasetRepo, StringComparison.Ordinal) && string.Equals(metadata.DatasetConfig, request.DatasetConfig, StringComparison.Ordinal) && - string.Equals(metadata.DatasetSplit, request.DatasetSplit, StringComparison.Ordinal), + string.Equals(metadata.DatasetSplit, effectiveSplit, StringComparison.Ordinal), ImatrixSourceKind.LocalDatasetFile => string.Equals(metadata.LocalDatasetFile, Path.GetFullPath(request.LocalDatasetFile!), StringComparison.Ordinal) && - string.Equals(metadata.DatasetSplit, request.DatasetSplit, StringComparison.Ordinal), + string.Equals(metadata.DatasetSplit, effectiveSplit, StringComparison.Ordinal), _ => false }; } @@ -222,6 +224,7 @@ private async Task AcquireImatrixAsync( { AnsiConsole.MarkupLine( $"[grey]Imatrix: acquiring from source:[/] [cyan]{Markup.Escape(ToSidecarSourceKind(sourceKind))}[/]"); + string effectiveSplit = GetEffectiveSplitForMetadata(request, sourceKind); switch (sourceKind) { @@ -245,7 +248,7 @@ private async Task AcquireImatrixAsync( OriginalDownloadName = request.ImatrixUrl == null ? null : Path.GetFileName(new Uri(request.ImatrixUrl).AbsolutePath), DatasetRepo = request.DatasetRepo, DatasetConfig = request.DatasetConfig, - DatasetSplit = request.DatasetSplit, + DatasetSplit = effectiveSplit, LocalDatasetFile = string.IsNullOrWhiteSpace(request.LocalDatasetFile) ? null : Path.GetFullPath(request.LocalDatasetFile) }; @@ -261,7 +264,7 @@ private async Task AcquireImatrixAsync( CanonicalPath = datPath, SourceKind = ToSidecarSourceKind(sourceKind), SourceIdentity = sourceIdentity, - Split = request.DatasetSplit, + Split = effectiveSplit, Config = request.DatasetConfig, Sha256 = await ComputeSha256Async(datPath, ct), FileSizeBytes = fileInfo.Length @@ -287,6 +290,14 @@ private static async Task CleanupArtifactsAsync(params string[] paths) _ => "unknown" }; + private static string GetEffectiveSplitForMetadata(ImatrixRequest request, ImatrixSourceKind sourceKind) + { + if (sourceKind == ImatrixSourceKind.LocalDatasetFile) + return string.IsNullOrWhiteSpace(request.DatasetSplit) ? "" : request.DatasetSplit.Trim(); + + return request.DatasetSplit ?? string.Empty; + } + private static async Task ComputeSha256Async(string path, CancellationToken ct) { await using var stream = File.OpenRead(path); @@ -327,7 +338,27 @@ private async Task BuildFromLocalDatasetAsync(ImatrixRequest request, string dat AnsiConsole.MarkupLine($"[grey]Imatrix: building from local dataset file:[/] [cyan]{Markup.Escape(datasetPath)}[/]"); string exportedCorpusPath = Path.Combine(Path.GetDirectoryName(datPath)!, "local-dataset.export.txt"); - var exportSummary = await ExportLocalDatasetToCorpusAsync(datasetPath, exportedCorpusPath, buildLogPath, ct); + string? splitPropertyPath = string.IsNullOrWhiteSpace(request.DatasetSplit) ? null : request.DatasetSplit!.Trim(); + + if (splitPropertyPath != null) + { + AnsiConsole.MarkupLine( + $"[grey]Imatrix: local dataset split/property =[/] [cyan]{Markup.Escape(splitPropertyPath)}[/]"); + AnsiConsole.MarkupLine( + $"[grey]Imatrix: extracting property/path[/] [cyan]{Markup.Escape(splitPropertyPath)}[/] [grey]from local JSON rows.[/]"); + } + else + { + AnsiConsole.MarkupLine( + "[yellow]Imatrix warning:[/] no local split/property provided; using explicit fallback recursive text extraction mode."); + } + + var exportSummary = await ExportLocalDatasetToCorpusAsync( + datasetPath, + exportedCorpusPath, + splitPropertyPath, + buildLogPath, + ct); if (exportSummary.StructuredRows > 0) { @@ -339,7 +370,8 @@ private async Task BuildFromLocalDatasetAsync(ImatrixRequest request, string dat AnsiConsole.MarkupLine( $"[grey]Imatrix: local dataset export complete.[/] rows=[cyan]{exportSummary.TotalRows}[/], " + - $"structured=[cyan]{exportSummary.StructuredRows}[/], extracted_text_blocks=[cyan]{exportSummary.ExtractedTextBlocks}[/], " + + $"structured=[cyan]{exportSummary.StructuredRows}[/], missing_split=[cyan]{exportSummary.RowsMissingSplitProperty}[/], " + + $"extracted_text_blocks=[cyan]{exportSummary.ExtractedTextBlocks}[/], " + $"corpus=[cyan]{Markup.Escape(exportedCorpusPath)}[/]"); await BuildImatrixFromDatasetTextAsync(exportedCorpusPath, datPath, buildLogPath, ct); @@ -492,6 +524,7 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s private static async Task ExportLocalDatasetToCorpusAsync( string datasetPath, string exportedCorpusPath, + string? splitPropertyPath, string buildLogPath, CancellationToken ct) { @@ -502,6 +535,8 @@ private static async Task ExportLocalDatasetToCorpusA int totalRows = 0; int structuredRows = 0; int extractedTextBlocks = 0; + int rowsWithMissingSplitProperty = 0; + bool usingSplitProperty = !string.IsNullOrWhiteSpace(splitPropertyPath); await using var writer = new StreamWriter(exportedCorpusPath, false, Encoding.UTF8); @@ -521,12 +556,15 @@ private static async Task ExportLocalDatasetToCorpusA if (looksStructured) structuredRows++; - foreach (string text in ExtractCorpusTextFromJsonPayload(line)) + foreach (string text in ExtractCorpusTextFromJsonPayload(line, splitPropertyPath, out bool rowMissingRequestedSplit)) { await writer.WriteLineAsync(text); await writer.WriteLineAsync(); extractedTextBlocks++; } + + if (rowMissingRequestedSplit) + rowsWithMissingSplitProperty++; } } else @@ -537,20 +575,46 @@ private static async Task ExportLocalDatasetToCorpusA if (doc.RootElement.ValueKind is JsonValueKind.Object or JsonValueKind.Array) structuredRows = 1; - foreach (string text in ExtractCorpusTextFromElement(doc.RootElement)) + if (doc.RootElement.ValueKind == JsonValueKind.Array) { - await writer.WriteLineAsync(text); - await writer.WriteLineAsync(); - extractedTextBlocks++; + foreach (var row in doc.RootElement.EnumerateArray()) + { + totalRows++; + foreach (string text in ExtractCorpusTextFromElement(row, splitPropertyPath, out bool rowMissingRequestedSplit)) + { + await writer.WriteLineAsync(text); + await writer.WriteLineAsync(); + extractedTextBlocks++; + } + + if (rowMissingRequestedSplit) + rowsWithMissingSplitProperty++; + } } + else + { + totalRows = 1; + foreach (string text in ExtractCorpusTextFromElement(doc.RootElement, splitPropertyPath, out bool rowMissingRequestedSplit)) + { + await writer.WriteLineAsync(text); + await writer.WriteLineAsync(); + extractedTextBlocks++; + } - totalRows = doc.RootElement.ValueKind == JsonValueKind.Array - ? doc.RootElement.GetArrayLength() - : 1; + if (rowMissingRequestedSplit) + rowsWithMissingSplitProperty++; + } } await writer.FlushAsync(); + if (usingSplitProperty && extractedTextBlocks == 0) + { + throw new InvalidOperationException( + $"Local dataset split/property '{splitPropertyPath}' was requested but no text could be extracted from '{datasetPath}'. " + + "Verify the property/path exists in your JSON rows."); + } + if (extractedTextBlocks == 0) throw new InvalidOperationException( $"No usable text content was extracted from local dataset file '{datasetPath}'."); @@ -558,33 +622,57 @@ private static async Task ExportLocalDatasetToCorpusA await File.AppendAllTextAsync( buildLogPath, $"[{DateTime.UtcNow:O}] Local dataset export: src={datasetPath}, out={exportedCorpusPath}, " + - $"rows={totalRows}, structured_rows={structuredRows}, extracted_text_blocks={extractedTextBlocks}{Environment.NewLine}", + $"split_property={(splitPropertyPath ?? "")}, rows={totalRows}, structured_rows={structuredRows}, " + + $"rows_missing_split={rowsWithMissingSplitProperty}, extracted_text_blocks={extractedTextBlocks}{Environment.NewLine}", ct); - return new LocalDatasetExportSummary(totalRows, structuredRows, extractedTextBlocks); + return new LocalDatasetExportSummary(totalRows, structuredRows, extractedTextBlocks, rowsWithMissingSplitProperty); } - private static IEnumerable ExtractCorpusTextFromJsonPayload(string payload) + private static IEnumerable ExtractCorpusTextFromJsonPayload(string payload, string? splitPropertyPath, out bool missingRequestedSplit) { + missingRequestedSplit = false; try { using var doc = JsonDocument.Parse(payload); - return ExtractCorpusTextFromElement(doc.RootElement).ToList(); + return ExtractCorpusTextFromElement(doc.RootElement, splitPropertyPath, out missingRequestedSplit).ToList(); } catch (JsonException) { - if (LooksLikeUsefulText(payload)) + if (splitPropertyPath == null && LooksLikeUsefulText(payload)) return new[] { payload.Trim() }; + if (splitPropertyPath != null) + missingRequestedSplit = true; + return Array.Empty(); } } - private static IEnumerable ExtractCorpusTextFromElement(JsonElement element) + private static IEnumerable ExtractCorpusTextFromElement(JsonElement element, string? splitPropertyPath, out bool missingRequestedSplit) { + missingRequestedSplit = false; + + if (!string.IsNullOrWhiteSpace(splitPropertyPath)) + { + if (!TryResolveJsonPath(element, splitPropertyPath!, out JsonElement resolved)) + { + missingRequestedSplit = true; + return Array.Empty(); + } + + var pathTexts = new List(); + CollectText(resolved, pathTexts); + return NormalizeDistinct(pathTexts); + } + var texts = new List(); CollectText(element, texts); + return NormalizeDistinct(texts); + } + private static IEnumerable NormalizeDistinct(List texts) + { // de-dup while preserving order var seen = new HashSet(StringComparer.Ordinal); foreach (var text in texts) @@ -598,6 +686,66 @@ private static IEnumerable ExtractCorpusTextFromElement(JsonElement elem } } + private static bool TryResolveJsonPath(JsonElement row, string splitPropertyPath, out JsonElement resolved) + { + resolved = row; + foreach (string rawSegment in splitPropertyPath.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + if (resolved.ValueKind == JsonValueKind.Object) + { + if (!TryGetPropertyCaseInsensitive(resolved, rawSegment, out resolved)) + return false; + continue; + } + + if (resolved.ValueKind == JsonValueKind.Array) + { + if (int.TryParse(rawSegment, out int index)) + { + if (index < 0 || index >= resolved.GetArrayLength()) + return false; + + resolved = resolved[index]; + continue; + } + + // if segment points to a property on each array element, gather all hits + var hits = new List(); + foreach (var item in resolved.EnumerateArray()) + { + if (item.ValueKind == JsonValueKind.Object && TryGetPropertyCaseInsensitive(item, rawSegment, out JsonElement value)) + hits.Add(value); + } + + if (hits.Count == 0) + return false; + + using var hitsDoc = JsonDocument.Parse(JsonSerializer.Serialize(hits)); + resolved = hitsDoc.RootElement.Clone(); + continue; + } + + return false; + } + + return true; + } + + private static bool TryGetPropertyCaseInsensitive(JsonElement obj, string name, out JsonElement value) + { + foreach (var prop in obj.EnumerateObject()) + { + if (string.Equals(prop.Name, name, StringComparison.OrdinalIgnoreCase)) + { + value = prop.Value; + return true; + } + } + + value = default; + return false; + } + private static void CollectText(JsonElement element, List sink) { switch (element.ValueKind) @@ -685,7 +833,7 @@ private static async Task PumpProcessStreamAsync( } } - private sealed record LocalDatasetExportSummary(int TotalRows, int StructuredRows, int ExtractedTextBlocks); + private sealed record LocalDatasetExportSummary(int TotalRows, int StructuredRows, int ExtractedTextBlocks, int RowsMissingSplitProperty); private static string ResolvePythonExecutableOrThrow() { From 872aa0772a52bb1f3fb3570a15c25bc7e0710040 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 17:02:14 -0400 Subject: [PATCH 085/258] Fix rowMissingRequestedSplit scope errors in imatrix export --- MagicQuant/Services/ImatrixService.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index fb77962..86059b0 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -556,7 +556,8 @@ private static async Task ExportLocalDatasetToCorpusA if (looksStructured) structuredRows++; - foreach (string text in ExtractCorpusTextFromJsonPayload(line, splitPropertyPath, out bool rowMissingRequestedSplit)) + bool rowMissingRequestedSplit; + foreach (string text in ExtractCorpusTextFromJsonPayload(line, splitPropertyPath, out rowMissingRequestedSplit)) { await writer.WriteLineAsync(text); await writer.WriteLineAsync(); @@ -580,7 +581,8 @@ private static async Task ExportLocalDatasetToCorpusA foreach (var row in doc.RootElement.EnumerateArray()) { totalRows++; - foreach (string text in ExtractCorpusTextFromElement(row, splitPropertyPath, out bool rowMissingRequestedSplit)) + bool rowMissingRequestedSplit; + foreach (string text in ExtractCorpusTextFromElement(row, splitPropertyPath, out rowMissingRequestedSplit)) { await writer.WriteLineAsync(text); await writer.WriteLineAsync(); @@ -594,7 +596,8 @@ private static async Task ExportLocalDatasetToCorpusA else { totalRows = 1; - foreach (string text in ExtractCorpusTextFromElement(doc.RootElement, splitPropertyPath, out bool rowMissingRequestedSplit)) + bool rowMissingRequestedSplit; + foreach (string text in ExtractCorpusTextFromElement(doc.RootElement, splitPropertyPath, out rowMissingRequestedSplit)) { await writer.WriteLineAsync(text); await writer.WriteLineAsync(); From ea10351f43ec4990a0251a13fba2fdd898dfa1c1 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Sun, 19 Apr 2026 17:17:44 -0400 Subject: [PATCH 086/258] Enhance imatrix heartbeat with file growth visibility --- MagicQuant/Services/ImatrixService.cs | 57 +++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index 86059b0..330ae04 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -442,6 +442,12 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s AnsiConsole.MarkupLine( $"[grey]Imatrix: invoking llama-imatrix build from dataset:[/] [cyan]{Markup.Escape(datasetPath)}[/]"); AnsiConsole.MarkupLine($"[grey]Imatrix: streaming llama-imatrix output to:[/] [cyan]{Markup.Escape(buildLogPath)}[/]"); + if (File.Exists(datasetPath)) + { + long corpusSizeBytes = new FileInfo(datasetPath).Length; + AnsiConsole.MarkupLine( + $"[grey]Imatrix: exported corpus ready.[/] [cyan]size={Markup.Escape(FormatBytes(corpusSizeBytes))}[/]"); + } string llamaBin = Cache.LlamaBin ?? throw new InvalidOperationException("Cache.LlamaBin not set."); string binaryName = OperatingSystem.IsWindows() ? "llama-imatrix.exe" : "llama-imatrix"; @@ -480,23 +486,47 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s var startedUtc = DateTime.UtcNow; var maxRuntime = TimeSpan.FromHours(2); int outputLineCount = 0; + bool datDetected = false; + long lastDatSize = -1; Task stdoutTask = PumpProcessStreamAsync(p.StandardOutput, "stdout", buildLog, writeLock, line => { outputLineCount++; - AnsiConsole.MarkupLine($"[grey]Imatrix[{Markup.Escape("stdout")}]:[/] {Markup.Escape(line)}"); + AnsiConsole.MarkupLine($"[grey]llama-imatrix stdout:[/] {Markup.Escape(line)}"); }, ct); Task stderrTask = PumpProcessStreamAsync(p.StandardError, "stderr", buildLog, writeLock, line => { outputLineCount++; - AnsiConsole.MarkupLine($"[grey]Imatrix[{Markup.Escape("stderr")}]:[/] {Markup.Escape(line)}"); + AnsiConsole.MarkupLine($"[grey]llama-imatrix stderr:[/] {Markup.Escape(line)}"); }, ct); while (!p.HasExited) { await Task.Delay(TimeSpan.FromSeconds(30), ct); var elapsed = DateTime.UtcNow - startedUtc; + bool datExists = File.Exists(datPath); + string datSizeText = "n/a"; + if (datExists) + { + long datSize = new FileInfo(datPath).Length; + datSizeText = FormatBytes(datSize); + + if (!datDetected) + { + datDetected = true; + lastDatSize = datSize; + AnsiConsole.MarkupLine($"[green]Imatrix: output file detected:[/] [cyan]{Markup.Escape(datPath)}[/]"); + AnsiConsole.MarkupLine($"[green]Imatrix: output file size now[/] [cyan]{Markup.Escape(datSizeText)}[/]"); + } + else if (datSize != lastDatSize) + { + lastDatSize = datSize; + AnsiConsole.MarkupLine($"[grey]Imatrix: output file size now[/] [cyan]{Markup.Escape(datSizeText)}[/]"); + } + } + + string corpusSizeText = File.Exists(datasetPath) ? FormatBytes(new FileInfo(datasetPath).Length) : "n/a"; if (elapsed > maxRuntime) { @@ -508,7 +538,12 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s } AnsiConsole.MarkupLine( - $"[grey]Imatrix: llama-imatrix still running... elapsed[/] [cyan]{elapsed:hh\\:mm\\:ss}[/][grey], output lines[/] [cyan]{outputLineCount}[/]"); + $"[grey]Imatrix: llama-imatrix still running... elapsed[/] [cyan]{elapsed:hh\\:mm\\:ss}[/]" + + $"[grey], output lines[/] [cyan]{outputLineCount}[/]" + + $"[grey], dat_exists[/] [cyan]{datExists}[/]" + + $"[grey], dat_size[/] [cyan]{Markup.Escape(datSizeText)}[/]" + + $"[grey], corpus_size[/] [cyan]{Markup.Escape(corpusSizeText)}[/]" + + $"[grey], log[/] [cyan]{Markup.Escape(buildLogPath)}[/]"); } await Task.WhenAll(stdoutTask, stderrTask); @@ -826,7 +861,7 @@ private static async Task PumpProcessStreamAsync( await writeLock.WaitAsync(ct); try { - await buildLog.WriteLineAsync($"[{label}] {line}"); + await buildLog.WriteLineAsync($"[llama-imatrix {label}] {line}"); await buildLog.FlushAsync(); } finally @@ -836,6 +871,20 @@ private static async Task PumpProcessStreamAsync( } } + private static string FormatBytes(long sizeBytes) + { + string[] units = ["B", "KB", "MB", "GB", "TB"]; + double size = sizeBytes; + int unit = 0; + while (size >= 1024 && unit < units.Length - 1) + { + size /= 1024; + unit++; + } + + return $"{size:0.0} {units[unit]}"; + } + private sealed record LocalDatasetExportSummary(int TotalRows, int StructuredRows, int ExtractedTextBlocks, int RowsMissingSplitProperty); private static string ResolvePythonExecutableOrThrow() From 16b8003da114ac06f927792a3c7e8196a89ff7eb Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 12:10:45 -0400 Subject: [PATCH 087/258] Fix learned-baseline pruning to require exact effective scheme match --- MagicQuant-Pipeline.sln | 6 + .../LearnedBaselinePruningServiceTests.cs | 48 ++++++++ MagicQuant.Tests/MagicQuant.Tests.csproj | 18 +++ MagicQuant/Helpers/ComboLogic.cs | 2 +- MagicQuant/Helpers/RuntimeSearchSpace.cs | 2 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 4 +- MagicQuant/Program.cs | 2 +- MagicQuant/Properties/AssemblyInfo.cs | 3 + .../Services/LearnedBaselinePruningService.cs | 116 +++++++++--------- 9 files changed, 139 insertions(+), 62 deletions(-) create mode 100644 MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs create mode 100644 MagicQuant.Tests/MagicQuant.Tests.csproj create mode 100644 MagicQuant/Properties/AssemblyInfo.cs diff --git a/MagicQuant-Pipeline.sln b/MagicQuant-Pipeline.sln index a933660..cb4635f 100644 --- a/MagicQuant-Pipeline.sln +++ b/MagicQuant-Pipeline.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant", "MagicQuant\Ma EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQ.DB", "MQ.DB\MQ.DB.csproj", "{A97D6992-2659-47F9-9AC9-99425D2677A4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.Tests", "MagicQuant.Tests\MagicQuant.Tests.csproj", "{C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,5 +20,9 @@ Global {A97D6992-2659-47F9-9AC9-99425D2677A4}.Debug|Any CPU.Build.0 = Debug|Any CPU {A97D6992-2659-47F9-9AC9-99425D2677A4}.Release|Any CPU.ActiveCfg = Release|Any CPU {A97D6992-2659-47F9-9AC9-99425D2677A4}.Release|Any CPU.Build.0 = Release|Any CPU + {C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs new file mode 100644 index 0000000..f26a165 --- /dev/null +++ b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs @@ -0,0 +1,48 @@ +using MagicQuant.Helpers; +using MagicQuant.Services; +using MQ.DB.Models; +using Xunit; + +namespace MagicQuant.Tests; + +public class LearnedBaselinePruningServiceTests +{ + [Fact] + public void Embeddings_LearnedBaselinePruning_OnlyAllowsQ6KAndBansOtherBaselinesWhenFinalTypeMapsToQ6K() + { + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(true); + + var result = new LearnedBaselinePruningResult(); + + var learnedRows = new List + { + new(BaselineQuants.Q6_K.UniqueId, TensorWeightScheme.Q6_K.UniqueId, TReg.Embeddings.UniqueId, "Q6_K"), + new(BaselineQuants.Q5_K.UniqueId, TensorWeightScheme.Q5_K.UniqueId, TReg.Embeddings.UniqueId, "Q6_K"), + new(BaselineQuants.Q4_K_M.UniqueId, TensorWeightScheme.Q4_K.UniqueId, TReg.Embeddings.UniqueId, "Q6_K"), + new(BaselineQuants.IQ4_NL.UniqueId, TensorWeightScheme.IQ4_NL.UniqueId, TReg.Embeddings.UniqueId, "Q6_K"), + new(BaselineQuants.IQ4_XS.UniqueId, TensorWeightScheme.IQ4_XS.UniqueId, TReg.Embeddings.UniqueId, "Q6_K") + }; + + var unused = new HashSet(); + + LearnedBaselinePruningService.ApplyLearnedBaselinePruning( + learnedRows, + aiModelHashId: 1, + aiModelHashUniqueHash: "regression-model-hash", + unusedGroupIds: unused, + result: result); + + Assert.False(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.Q6_K)); + Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.Q5_K)); + Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.Q4_K)); + Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.IQ4_NL)); + Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.IQ4_XS)); + + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=Q6_K") && x.Contains("decision=ALLOW")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=Q5_K") && x.Contains("decision=BAN")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=Q4_K") && x.Contains("decision=BAN")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=IQ4_NL") && x.Contains("decision=BAN")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=IQ4_XS") && x.Contains("decision=BAN")); + } +} diff --git a/MagicQuant.Tests/MagicQuant.Tests.csproj b/MagicQuant.Tests/MagicQuant.Tests.csproj new file mode 100644 index 0000000..6677456 --- /dev/null +++ b/MagicQuant.Tests/MagicQuant.Tests.csproj @@ -0,0 +1,18 @@ + + + net10.0 + enable + enable + false + + + + + + + + + + + + diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 0910066..ffcceaa 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -42,7 +42,7 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) continue; - if (scheme.IsBannedFor(group)) + if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) continue; ids.Add(scheme.UniqueId); diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index f68ab85..70a04d6 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -194,7 +194,7 @@ private static bool HasAnyExplicitSchemeAllowed(TensorGroup group) .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) .Where(x => _imatrixAvailable || !x.RequiresImatrix) - .Any(x => !x.IsBannedFor(group)); + .Any(x => !IsSchemeRuntimeBannedForGroup(group, x)); } public static IReadOnlyList GetActiveCombinationBaselines() diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 67843eb..ea07408 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -141,7 +141,7 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample if (smallest != null && scheme.UniqueId == smallest.UniqueId) continue; - if (scheme.IsBannedFor(group)) + if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) continue; var quant = HybridQuant.CreateBlanket( @@ -311,7 +311,7 @@ public static IEnumerable> GenerateTensorConfigBatches( if (!allowedIds.Contains(scheme.UniqueId)) continue; - if (scheme.IsBannedFor(group) || RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) + if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) continue; return scheme; diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 3dcfaa0..0225e14 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -21,7 +21,7 @@ string manualFlags = @"--model-dir ""/mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/"" --use-imatrix - --imatrix-dataset-local-file ""/home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1.jsonl"" + --imatrix-dataset-local-file ""/home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1m.jsonl"" --imatrix-dataset-split ""text"""; args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif diff --git a/MagicQuant/Properties/AssemblyInfo.cs b/MagicQuant/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..5aa468b --- /dev/null +++ b/MagicQuant/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("MagicQuant.Tests")] diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index 5ed2613..ed0aacb 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -20,7 +20,7 @@ public sealed class LearnedBaselinePruningResult public sealed class LearnedBaselinePruningService { - private readonly record struct LearnedRow( + internal readonly record struct LearnedRow( byte BaselineQuantId, byte TensorWeightSchemeId, byte TensorGroupId, @@ -35,19 +35,23 @@ public async Task AnalyzeAndApplyAsync(Cancellatio await using var db = new MagicQuantContext(); - var aiModelHashId = await db.AiModelHashes + var aiModelHash = await db.AiModelHashes .AsNoTracking() .Where(x => x.UniqueHash == Cache.CurrentModelId) - .Select(x => (uint?)x.Id) + .Select(x => new { x.Id, x.UniqueHash }) .FirstOrDefaultAsync(ct); - if (aiModelHashId == null) + if (aiModelHash == null) throw new InvalidOperationException( $"AiModelHash row was not found for current model id '{Cache.CurrentModelId}'."); + result.Notes.Add( + $"Learned-baseline pruning model resolution: Cache.CurrentModelId={Cache.CurrentModelId}, " + + $"AiModelHash.Id={aiModelHash.Id}, AiModelHash.UniqueHash={aiModelHash.UniqueHash}"); + var learnedRows = await db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == aiModelHashId.Value) + .Where(x => x.AiModelHashId == aiModelHash.Id) .Select(x => new LearnedRow( x.BaselineQuantId, x.TensorWeightSchemeId, @@ -62,36 +66,24 @@ public async Task AnalyzeAndApplyAsync(Cancellatio return result; } - var baselinesWithAnyLearnedRows = learnedRows - .Select(x => x.BaselineQuantId) - .ToHashSet(); - - var aliasToSchemeIds = BuildAliasToSchemeIds(); - - var effectiveSchemesByBaselineAndGroup = new Dictionary<(byte BaselineId, byte GroupId), HashSet>(); - - foreach (var row in learnedRows) - { - var key = (row.BaselineQuantId, row.TensorGroupId); - - if (!effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var set)) - { - set = new HashSet(); - effectiveSchemesByBaselineAndGroup[key] = set; - } - - if (aliasToSchemeIds.TryGetValue(CanonicalizeQuantToken(row.FinalQuantType), out var resolvedIds)) - { - foreach (var resolvedId in resolvedIds) - set.Add(resolvedId); - } - } - - var skippedBaselineNotes = new HashSet(); var unusedIds = Cache.UnusedTensorGroups .Select(x => x.UniqueId) .ToHashSet(); + ApplyLearnedBaselinePruning(learnedRows, aiModelHash.Id, aiModelHash.UniqueHash, unusedIds, result); + return result; + } + + internal static void ApplyLearnedBaselinePruning( + IReadOnlyList learnedRows, + uint aiModelHashId, + string aiModelHashUniqueHash, + HashSet unusedGroupIds, + LearnedBaselinePruningResult result) + { + var aliasToSchemeIds = BuildAliasToSchemeIds(); + var effectiveSchemesByBaselineAndGroup = BuildEffectiveSchemesByBaselineAndGroup(learnedRows, aliasToSchemeIds); + var schemeOwnerById = BaselineQuants.All .SelectMany(b => b.TensorWeightSchemes.Select(s => new { @@ -109,7 +101,7 @@ public async Task AnalyzeAndApplyAsync(Cancellatio foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) { - if (unusedIds.Contains(group.UniqueId)) + if (unusedGroupIds.Contains(group.UniqueId)) continue; foreach (var scheme in explicitSchemes) @@ -120,43 +112,53 @@ public async Task AnalyzeAndApplyAsync(Cancellatio if (!schemeOwnerById.TryGetValue(scheme.UniqueId, out var owningBaseline)) continue; - if (!baselinesWithAnyLearnedRows.Contains(owningBaseline.UniqueId)) - { - if (skippedBaselineNotes.Add(owningBaseline.UniqueId)) - { - result.BaselinesSkippedWithoutLearnedRows++; + var key = (owningBaseline.UniqueId, group.UniqueId); + bool hasEffectiveSet = effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var effectiveForGroup); + bool allow = hasEffectiveSet && effectiveForGroup!.Contains(scheme.UniqueId); + string effectiveIds = hasEffectiveSet + ? string.Join(",", effectiveForGroup!.OrderBy(x => x)) + : ""; - result.Notes.Add( - $"Learned-baseline pruning skipped for baseline '{owningBaseline.Names[0]}' because no learned rows existed for the current model."); - } + result.Notes.Add( + $"Learned-prune check: model={aiModelHashId}/{aiModelHashUniqueHash}, group={group.Names[0]}, " + + $"scheme={scheme.Names[0]}, owner={owningBaseline.Names[0]}, effective=[{effectiveIds}], decision={(allow ? "ALLOW" : "BAN")}"); - continue; + if (!allow) + { + RuntimeSearchSpace.BanSchemeForGroupByLearnedBaselineAbsence(group, scheme, owningBaseline); + result.GroupSchemeEliminations++; } + } + } + } - var key = (owningBaseline.UniqueId, group.UniqueId); - var effectiveForGroup = effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var found) - ? found - : null; - - bool hasAnyConnectedMapping = effectiveForGroup != null && - owningBaseline.TensorWeightSchemes.Any(connectedScheme => - effectiveForGroup.Contains(connectedScheme.UniqueId)); + internal static Dictionary<(byte BaselineId, byte GroupId), HashSet> BuildEffectiveSchemesByBaselineAndGroup( + IReadOnlyList learnedRows, + Dictionary> aliasToSchemeIds) + { + var effectiveSchemesByBaselineAndGroup = new Dictionary<(byte BaselineId, byte GroupId), HashSet>(); - if (hasAnyConnectedMapping) - continue; + foreach (var row in learnedRows) + { + var key = (row.BaselineQuantId, row.TensorGroupId); - RuntimeSearchSpace.BanSchemeForGroupByLearnedBaselineAbsence(group, scheme, owningBaseline); - result.GroupSchemeEliminations++; + if (!effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var set)) + { + set = new HashSet(); + effectiveSchemesByBaselineAndGroup[key] = set; + } - result.Notes.Add( - $"Learned-baseline prune: '{scheme.Names[0]}' removed for '{group.Name}' because baseline '{owningBaseline.Names[0]}' learned zero matching tensors in that group."); + if (aliasToSchemeIds.TryGetValue(CanonicalizeQuantToken(row.FinalQuantType), out var resolvedIds)) + { + foreach (var resolvedId in resolvedIds) + set.Add(resolvedId); } } - return result; + return effectiveSchemesByBaselineAndGroup; } - private static Dictionary> BuildAliasToSchemeIds() + internal static Dictionary> BuildAliasToSchemeIds() { var map = new Dictionary>(StringComparer.Ordinal); From f3fc9ae0cac8799eb4fe972dcdaa52672b1e80e1 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 12:15:03 -0400 Subject: [PATCH 088/258] Fix learned-prune note to use TensorGroup.Name --- MagicQuant/Services/LearnedBaselinePruningService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index ed0aacb..d0b2670 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -120,7 +120,7 @@ internal static void ApplyLearnedBaselinePruning( : ""; result.Notes.Add( - $"Learned-prune check: model={aiModelHashId}/{aiModelHashUniqueHash}, group={group.Names[0]}, " + + $"Learned-prune check: model={aiModelHashId}/{aiModelHashUniqueHash}, group={group.Name}, " + $"scheme={scheme.Names[0]}, owner={owningBaseline.Names[0]}, effective=[{effectiveIds}], decision={(allow ? "ALLOW" : "BAN")}"); if (!allow) From a3b1bb80108d42bbfce6c784e5b42fd51f0f4139 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 14:11:36 -0400 Subject: [PATCH 089/258] Refactor hybrid search authority to baseline-family candidates --- MQ.DB/Cache.cs | 2 + MQ.DB/Data/MagicQuantContext.cs | 1 + MQ.DB/Models/BaselineQuants.cs | 139 +++++++------- MQ.DB/Models/DbModels/AiBenchmark.cs | 10 +- MQ.DB/Models/DbModels/BenchmarkRun.cs | 9 + .../DbModels/ExecutionPlanProbeCache.cs | 10 + MQ.DB/Models/DbModels/ImatrixDefinition.cs | 35 ++++ MQ.DB/Models/DbModels/QuantizationRun.cs | 9 + MQ.DB/Models/HybridQuant.cs | 18 +- MQ.DB/Models/TensorConfigs.cs | 58 +++--- MagicQuant/Commands/Evolution.cs | 14 +- MagicQuant/Helpers/ComboLogic.cs | 22 +-- MagicQuant/Helpers/RuntimeSearchSpace.cs | 180 +++++++----------- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 10 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 60 +++--- MagicQuant/Services/ImatrixService.cs | 3 + .../Services/IsolationOptimizationService.cs | 59 +++--- .../Services/IsolationPlanningService.cs | 19 ++ .../Services/LearnedBaselinePruningService.cs | 34 ++-- .../Services/ModelCompatibilityService.cs | 16 +- MagicQuant/Services/QuantDatabaseService.cs | 51 ++++- MagicQuant/Services/QuantizationService.cs | 17 +- 22 files changed, 424 insertions(+), 352 deletions(-) create mode 100644 MQ.DB/Models/DbModels/ImatrixDefinition.cs create mode 100644 MagicQuant/Services/IsolationPlanningService.cs diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index 194c44e..3abefbd 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -71,4 +71,6 @@ public enum MainTorchType public static bool IsImatrixAvailable { get; set; } public static string? ActiveImatrixPath { get; set; } + + public static string? ActiveImatrixIdentityHash { get; set; } } diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index d74a0fd..ecd55a0 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -121,6 +121,7 @@ private static bool IsDesignTime() public DbSet LearnedBaselineTensorQuants { get; set; } public DbSet BaselineQuantDefinitions { get; set; } public DbSet ExecutionPlanProbeCaches { get; set; } + public DbSet ImatrixDefinitions { get; set; } // -------------------------------------------------------- // Configuration diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index c9bfaf1..fc530bf 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -7,7 +7,10 @@ public record BaselineQuants( bool RequiresImatrix, ImmutableArray Names, ImmutableArray TensorWeightSchemes, - HybridQuant? BaseConversionBase = null) + bool IsPureBaselineCandidate = true, + bool IsCombinationCarrierCandidate = true, + bool IsExplicitGroupCombinationCandidate = true, + bool IsHighPrecisionExplicitCandidate = false) { public const byte NativeSourceUniqueId = 250; @@ -15,7 +18,7 @@ public record BaselineQuants( TensorWeightSchemes.IsDefaultOrEmpty ? null : TensorWeightSchemes[0]; public static readonly BaselineQuants Q8_0 = - new(0, false, ["Q8_0"], [TensorWeightScheme.Q8_0]); + new(0, false, ["Q8_0"], [TensorWeightScheme.Q8_0], IsCombinationCarrierCandidate: true); public static readonly BaselineQuants Q6_K = new(1, false, ["Q6_K"], [TensorWeightScheme.Q6_K]); @@ -25,45 +28,12 @@ public record BaselineQuants( public static readonly BaselineQuants Q4_K_M = new(3, false, ["Q4_K_M"], [TensorWeightScheme.Q4_K]); - - /*public static readonly BaselineQuants MXFP4_MOE = - new( - 4, - false, - ["MXFP4_MOE"], - [TensorWeightScheme.MXFP4], - new HybridQuant - { - BaseQuant = null!, - Tensors = TReg.All - .Select(g => new HybridTensor - { - TGroup = g, - TensorType = TensorWeightScheme.MXFP4 - }) - .ToList() - });*/ public static readonly BaselineQuants IQ4_NL = new(5, false, ["IQ4_NL"], [TensorWeightScheme.IQ4_NL]); public static readonly BaselineQuants IQ4_XS = - new( - 6, - false, - ["IQ4_XS"], - [TensorWeightScheme.IQ4_XS], - new HybridQuant - { - BaseQuant = null!, - Tensors = TReg.All - .Select(g => new HybridTensor - { - TGroup = g, - TensorType = TensorWeightScheme.IQ4_XS - }) - .ToList() - }); + new(6, false, ["IQ4_XS"], [TensorWeightScheme.IQ4_XS]); public static readonly BaselineQuants IQ3_S = new(7, true, ["IQ3_S"], [TensorWeightScheme.IQ3_S]); @@ -83,15 +53,22 @@ public record BaselineQuants( public static readonly BaselineQuants IQ2_XXS = new(12, true, ["IQ2_XXS"], [TensorWeightScheme.IQ2_XXS]); - - + public static readonly BaselineQuants BF16_Hybrid = + new(201, false, ["BF16"], [TensorWeightScheme.BF16], + IsCombinationCarrierCandidate: false, + IsHighPrecisionExplicitCandidate: true); + + public static readonly BaselineQuants F16_Hybrid = + new(202, false, ["F16"], [TensorWeightScheme.F16], + IsCombinationCarrierCandidate: false, + IsHighPrecisionExplicitCandidate: true); + public static readonly ImmutableArray All = [ Q8_0, Q6_K, Q5_K, Q4_K_M, - // MXFP4_MOE, IQ4_NL, IQ4_XS, IQ3_S, @@ -99,15 +76,52 @@ public record BaselineQuants( IQ3_XXS, IQ2_S, IQ2_XS, - IQ2_XXS + IQ2_XXS, + BF16_Hybrid, + F16_Hybrid ]; - static BaselineQuants() + public static BaselineQuants GetNativeQuant() { - IQ4_XS.BaseConversionBase!.BaseQuant = IQ4_XS; - ValidateIntegrityOrThrow(); + var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + + return new( + NativeSourceUniqueId, + false, + [nativeScheme.Names[0]], + [nativeScheme], + IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, + IsHighPrecisionExplicitCandidate: true); } + // Compatibility alias for older code paths. + public static BaselineQuants GetBF16Quant() => GetNativeQuant(); + + public static IReadOnlyList GetAllRecognizedBaselines() => + All.OrderBy(x => x.UniqueId).ToList(); + + public static IReadOnlyList GetPureBaselineCandidates(bool hasUsableImatrix) => + All.Where(x => x.IsPureBaselineCandidate) + .Where(x => hasUsableImatrix || !x.RequiresImatrix) + .OrderBy(x => x.UniqueId) + .ToList(); + + public static IReadOnlyList GetCombinationCarrierBaselines(bool hasUsableImatrix) => + All.Where(x => x.IsCombinationCarrierCandidate) + .Where(x => hasUsableImatrix || !x.RequiresImatrix) + .OrderBy(x => x.UniqueId) + .ToList(); + + public static IReadOnlyList GetGroupCombinationCandidates(bool hasUsableImatrix, bool allowHighPrecisionHybrids) => + All.Where(x => x.IsExplicitGroupCombinationCandidate) + .Where(x => hasUsableImatrix || !x.RequiresImatrix) + .Where(x => allowHighPrecisionHybrids || !x.IsHighPrecisionExplicitCandidate) + .OrderBy(x => x.UniqueId) + .ToList(); + + public static BaselineQuants GetDefaultExplicitFallbackBaseline() => Q8_0; + public static void ValidateIntegrityOrThrow() { var invalidBaselines = All @@ -126,6 +140,7 @@ public static void ValidateIntegrityOrThrow() .SelectMany(x => x.TensorWeightSchemes.Select(s => new { Baseline = x, Scheme = s })) .GroupBy(x => x.Scheme.UniqueId) .Where(g => g.Count() > 1) + .Where(g => g.Key != TensorWeightScheme.BF16.UniqueId && g.Key != TensorWeightScheme.F16.UniqueId) .Select(g => g.Key) .ToList(); @@ -138,35 +153,8 @@ public static void ValidateIntegrityOrThrow() "TensorWeightScheme associations must be unique across BaselineQuants entries. Duplicates: " + string.Join(", ", duplicateNames)); } - - var missingBaselineSchemes = TensorWeightScheme.All - .Where(x => x.IsEligibleForBaseline) - .Where(x => !All.Any(b => b.TensorWeightSchemes.Any(s => s.UniqueId == x.UniqueId))) - .Select(x => x.Names[0]) - .ToList(); - - if (missingBaselineSchemes.Count > 0) - { - throw new InvalidOperationException( - "Every baseline-eligible TensorWeightScheme must be linked by exactly one BaselineQuants entry. Missing for: " + - string.Join(", ", missingBaselineSchemes)); - } } - public static BaselineQuants GetNativeQuant() - { - var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - - return new( - NativeSourceUniqueId, - false, - [nativeScheme.Names[0]], - [nativeScheme]); - } - - // Compatibility alias for older code paths. - public static BaselineQuants GetBF16Quant() => GetNativeQuant(); - public static BaselineQuants FromId(byte id) { if (id == NativeSourceUniqueId) @@ -178,4 +166,13 @@ public static BaselineQuants FromId(byte id) return found; } -} \ No newline at end of file + + public static BaselineQuants FromTensorSchemeId(byte schemeId) + { + var found = All.FirstOrDefault(x => x.TensorWeightSchemes.Any(s => s.UniqueId == schemeId)); + if (found == null) + throw new InvalidOperationException($"Unknown tensor scheme id '{schemeId}' for baseline conversion."); + + return found; + } +} diff --git a/MQ.DB/Models/DbModels/AiBenchmark.cs b/MQ.DB/Models/DbModels/AiBenchmark.cs index a937076..4c28ba1 100644 --- a/MQ.DB/Models/DbModels/AiBenchmark.cs +++ b/MQ.DB/Models/DbModels/AiBenchmark.cs @@ -41,6 +41,9 @@ public class AiBenchmark : ISQLiteEntity public AiModelHash AiModelHash { get; set; } = default!; + public int? ImatrixDefinitionId { get; set; } + public ImatrixDefinition? ImatrixDefinition { get; set; } + public List CategorBenchmarks { get; set; } = new(); public void Configure(EntityTypeBuilder builder) @@ -50,7 +53,7 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.Id) .ValueGeneratedNever(); - builder.HasIndex(x => new { x.AiModelHashId, x.TensorComboId }) + builder.HasIndex(x => new { x.AiModelHashId, x.ImatrixDefinitionId, x.TensorComboId }) .IsUnique(); builder.HasOne(x => x.TensorCombo) @@ -63,6 +66,11 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.AiModelHashId) .OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.ImatrixDefinition) + .WithMany() + .HasForeignKey(x => x.ImatrixDefinitionId) + .OnDelete(DeleteBehavior.Restrict); + builder.HasMany(x => x.CategorBenchmarks) .WithOne(x => x.AiBenchmark) .HasForeignKey(x => x.AiBenchmarkId) diff --git a/MQ.DB/Models/DbModels/BenchmarkRun.cs b/MQ.DB/Models/DbModels/BenchmarkRun.cs index 4550eae..11dc7ad 100644 --- a/MQ.DB/Models/DbModels/BenchmarkRun.cs +++ b/MQ.DB/Models/DbModels/BenchmarkRun.cs @@ -11,6 +11,9 @@ public class BenchmarkRun : ISQLiteEntity public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; + public int? ImatrixDefinitionId { get; set; } + public ImatrixDefinition? ImatrixDefinition { get; set; } + public Guid TensorComboId { get; set; } public TensorCombo TensorCombo { get; set; } = default!; @@ -46,6 +49,7 @@ public void Configure(EntityTypeBuilder builder) .ValueGeneratedNever(); builder.HasIndex(x => x.AiModelHashId); + builder.HasIndex(x => x.ImatrixDefinitionId); builder.HasIndex(x => x.TensorComboId); builder.HasIndex(x => x.AiBenchmarkId); builder.HasIndex(x => x.CategoryBenchmarkId); @@ -60,6 +64,11 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.AiModelHashId) .OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.ImatrixDefinition) + .WithMany() + .HasForeignKey(x => x.ImatrixDefinitionId) + .OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.TensorCombo) .WithMany() .HasForeignKey(x => x.TensorComboId) diff --git a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs index 9a1ab77..696a1f8 100644 --- a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs +++ b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs @@ -11,6 +11,9 @@ public class ExecutionPlanProbeCache : ISQLiteEntity public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; + public int? ImatrixDefinitionId { get; set; } + public ImatrixDefinition? ImatrixDefinition { get; set; } + public string HardwareFingerprint { get; set; } = string.Empty; public string QuantizedModelFingerprint { get; set; } = string.Empty; public string QuantizationKey { get; set; } = string.Empty; @@ -35,9 +38,11 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.SlotsJson).HasMaxLength(8000); builder.HasIndex(x => x.AiModelHashId); + builder.HasIndex(x => x.ImatrixDefinitionId); builder.HasIndex(x => new { x.AiModelHashId, + x.ImatrixDefinitionId, x.HardwareFingerprint, x.QuantizedModelFingerprint, x.QuantizationKey, @@ -48,5 +53,10 @@ public void Configure(EntityTypeBuilder builder) .WithMany() .HasForeignKey(x => x.AiModelHashId) .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.ImatrixDefinition) + .WithMany() + .HasForeignKey(x => x.ImatrixDefinitionId) + .OnDelete(DeleteBehavior.Restrict); } } diff --git a/MQ.DB/Models/DbModels/ImatrixDefinition.cs b/MQ.DB/Models/DbModels/ImatrixDefinition.cs new file mode 100644 index 0000000..166e07a --- /dev/null +++ b/MQ.DB/Models/DbModels/ImatrixDefinition.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class ImatrixDefinition : ISQLiteEntity +{ + public int Id { get; set; } + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + public string IdentityHash { get; set; } = string.Empty; + public string? CanonicalPath { get; set; } + public string SourceKind { get; set; } = "none"; + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; + public string? MetadataJson { get; set; } + public int? TokenCount { get; set; } + public string? BuildFingerprint { get; set; } + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.HasIndex(x => new { x.AiModelHashId, x.IdentityHash }).IsUnique(); + builder.Property(x => x.IdentityHash).HasMaxLength(128); + builder.Property(x => x.CanonicalPath).HasMaxLength(2048); + builder.Property(x => x.SourceKind).HasMaxLength(64); + builder.Property(x => x.MetadataJson).HasMaxLength(8000); + builder.Property(x => x.BuildFingerprint).HasMaxLength(512); + + builder.HasOne(x => x.AiModelHash) + .WithMany() + .HasForeignKey(x => x.AiModelHashId) + .OnDelete(DeleteBehavior.Cascade); + } +} diff --git a/MQ.DB/Models/DbModels/QuantizationRun.cs b/MQ.DB/Models/DbModels/QuantizationRun.cs index eec8235..3ef7a2a 100644 --- a/MQ.DB/Models/DbModels/QuantizationRun.cs +++ b/MQ.DB/Models/DbModels/QuantizationRun.cs @@ -11,6 +11,9 @@ public class QuantizationRun : ISQLiteEntity public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; + public int? ImatrixDefinitionId { get; set; } + public ImatrixDefinition? ImatrixDefinition { get; set; } + public Guid TensorComboId { get; set; } public TensorCombo TensorCombo { get; set; } = default!; @@ -42,6 +45,7 @@ public void Configure(EntityTypeBuilder builder) .ValueGeneratedNever(); builder.HasIndex(x => x.AiModelHashId); + builder.HasIndex(x => x.ImatrixDefinitionId); builder.HasIndex(x => x.TensorComboId); builder.HasIndex(x => x.AiBenchmarkId); builder.HasIndex(x => x.StartedUtc); @@ -57,6 +61,11 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.AiModelHashId) .OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.ImatrixDefinition) + .WithMany() + .HasForeignKey(x => x.ImatrixDefinitionId) + .OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.TensorCombo) .WithMany() .HasForeignKey(x => x.TensorComboId) diff --git a/MQ.DB/Models/HybridQuant.cs b/MQ.DB/Models/HybridQuant.cs index dbae7be..dd0a014 100644 --- a/MQ.DB/Models/HybridQuant.cs +++ b/MQ.DB/Models/HybridQuant.cs @@ -22,17 +22,18 @@ public HybridQuant(TensorConfig c) AddIfNotNull(TReg.MoeRouter, c.MoeRouter); } - private void AddIfNotNull(TensorGroup group, byte schemeId) + private void AddIfNotNull(TensorGroup group, byte candidateId) { - if (schemeId == TensorWeightScheme.NULL.UniqueId) + if (candidateId == TensorWeightScheme.NULL.UniqueId) return; - var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.First(g => g.UniqueId == schemeId); + var candidate = BaselineQuants.FromId(candidateId); Tensors.Add(new HybridTensor { TGroup = group, - TensorType = scheme + CandidateBaseline = candidate, + TensorType = candidate.DefaultTensorScheme ?? TensorWeightScheme.GetCurrentNativePrecisionScheme() }); } @@ -45,6 +46,7 @@ public HybridQuant Clone() .Select(t => new HybridTensor { TGroup = t.TGroup, + CandidateBaseline = t.CandidateBaseline, TensorType = t.TensorType }) .ToList() @@ -63,7 +65,7 @@ public static HybridQuant CreatePureBaseline(BaselineQuants baseQuant) public static HybridQuant CreateBlanket( BaselineQuants baseQuant, IEnumerable groups, - TensorWeightScheme blanketScheme) + BaselineQuants blanketCandidate) { return new HybridQuant { @@ -72,7 +74,8 @@ public static HybridQuant CreateBlanket( .Select(g => new HybridTensor { TGroup = g, - TensorType = blanketScheme + CandidateBaseline = blanketCandidate, + TensorType = blanketCandidate.DefaultTensorScheme ?? TensorWeightScheme.GetCurrentNativePrecisionScheme() }) .ToList() }; @@ -84,5 +87,6 @@ public static HybridQuant CreateBlanket( public class HybridTensor { public TensorGroup TGroup { get; set; } = null!; + public BaselineQuants CandidateBaseline { get; set; } = default!; public TensorWeightScheme TensorType { get; set; } = default!; -} \ No newline at end of file +} diff --git a/MQ.DB/Models/TensorConfigs.cs b/MQ.DB/Models/TensorConfigs.cs index 1cb8f98..a3db0c3 100644 --- a/MQ.DB/Models/TensorConfigs.cs +++ b/MQ.DB/Models/TensorConfigs.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Runtime.InteropServices; namespace MQ.DB.Models; @@ -30,39 +29,39 @@ public TensorConfig( byte moeExperts, byte moeRouter) { - BaseQuant = baseQuant; - Embeddings = embeddings; - LmHead = lmHead; - AttnQ = attnQ; - AttnKV = attnKV; - AttnOutput = attnOutput; - FfnUpGate = ffnUpGate; - FfnDown = ffnDown; - MoeExperts = moeExperts; - MoeRouter = moeRouter; + BaseQuant = baseQuant; + Embeddings = embeddings; + LmHead = lmHead; + AttnQ = attnQ; + AttnKV = attnKV; + AttnOutput = attnOutput; + FfnUpGate = ffnUpGate; + FfnDown = ffnDown; + MoeExperts = moeExperts; + MoeRouter = moeRouter; } // Converting constructor: HybridQuant -> TensorConfig public TensorConfig(HybridQuant h) : this( - baseQuant: checked((byte)h.BaseQuant.UniqueId), - embeddings: GetSchemeIdOrDefault(h, TReg.Embeddings), - lmHead: GetSchemeIdOrDefault(h, TReg.LmHead), - attnQ: GetSchemeIdOrDefault(h, TReg.AttnQ), - attnKV: GetSchemeIdOrDefault(h, TReg.AttnKV), - attnOutput: GetSchemeIdOrDefault(h, TReg.AttnOutput), - ffnUpGate: GetSchemeIdOrDefault(h, TReg.FfnUpGate), - ffnDown: GetSchemeIdOrDefault(h, TReg.FfnDown), - moeExperts: GetSchemeIdOrDefault(h, TReg.MoeExperts), - moeRouter: GetSchemeIdOrDefault(h, TReg.MoeRouter)) + baseQuant: checked((byte)h.BaseQuant.UniqueId), + embeddings: GetCandidateIdOrDefault(h, TReg.Embeddings), + lmHead: GetCandidateIdOrDefault(h, TReg.LmHead), + attnQ: GetCandidateIdOrDefault(h, TReg.AttnQ), + attnKV: GetCandidateIdOrDefault(h, TReg.AttnKV), + attnOutput: GetCandidateIdOrDefault(h, TReg.AttnOutput), + ffnUpGate: GetCandidateIdOrDefault(h, TReg.FfnUpGate), + ffnDown: GetCandidateIdOrDefault(h, TReg.FfnDown), + moeExperts: GetCandidateIdOrDefault(h, TReg.MoeExperts), + moeRouter: GetCandidateIdOrDefault(h, TReg.MoeRouter)) { } - private static byte GetSchemeIdOrDefault(HybridQuant h, TensorGroup group) + private static byte GetCandidateIdOrDefault(HybridQuant h, TensorGroup group) { if (h.Tensors == null || h.Tensors.Count == 0) return TensorWeightScheme.NULL.UniqueId; - TensorWeightScheme? found = null; + BaselineQuants? found = null; for (int i = 0; i < h.Tensors.Count; i++) { @@ -74,18 +73,13 @@ private static byte GetSchemeIdOrDefault(HybridQuant h, TensorGroup group) continue; if (found != null) - { - throw new InvalidOperationException( - $"HybridQuant contains duplicate entries for group '{group.Name}' (UniqueId={group.UniqueId})."); - } + throw new InvalidOperationException($"HybridQuant contains duplicate entries for group '{group.Name}' (UniqueId={group.UniqueId})."); - found = t.TensorType; + found = t.CandidateBaseline ?? BaselineQuants.FromTensorSchemeId(t.TensorType.UniqueId); } - return found == null - ? TensorWeightScheme.NULL.UniqueId - : checked((byte)found.UniqueId); + return found == null ? TensorWeightScheme.NULL.UniqueId : checked((byte)found.UniqueId); } public static explicit operator TensorConfig(HybridQuant h) => new TensorConfig(h); -} \ No newline at end of file +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 8a3d7d2..666b2fe 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -63,6 +63,7 @@ public async Task Run(List args) Cache.UseImatrix = args.Any(a => string.Equals(a.Name, "use-imatrix", StringComparison.OrdinalIgnoreCase)); Cache.ForceImatrixRebuild = args.Any(a => string.Equals(a.Name, "imatrix-force-rebuild", StringComparison.OrdinalIgnoreCase)); RuntimeSearchSpace.SetImatrixAvailability(false); + RuntimeSearchSpace.AllowHighPrecisionHybrids = args.Any(a => string.Equals(a.Name, "allow-high-precision-hybrids", StringComparison.OrdinalIgnoreCase)); JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); @@ -200,7 +201,8 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.Write(new Rule("[yellow]Initial Isolation Startup Samples[/]") { Justification = Justify.Left }); - var initialPlan = TensorConfigGenerator.GenerateInitialIsolationSamplePlan(Cache.UnusedTensorGroups); + var isolationPlanner = new IsolationPlanningService(); + var initialPlan = isolationPlanner.BuildInitialPlan(Cache.UnusedTensorGroups); AnsiConsole.MarkupLine($"[grey]Queued initial startup samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); var initialSummary = await quantizationService.ProcessHybridBatchAsync(initialPlan.Plans); @@ -222,7 +224,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.Write(new Rule("[yellow]Continuation Isolation Samples[/]") { Justification = Justify.Left }); - var continuationPlan = TensorConfigGenerator.GenerateContinuationIsolationSamplePlan( + var continuationPlan = isolationPlanner.BuildContinuationPlan( initialAnalysis.GroupsToContinue, Cache.UnusedTensorGroups); @@ -260,7 +262,7 @@ await benchmarkService.RunAllBenchmarksAsync( }); AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); - AnsiConsole.MarkupLine($"[green]Winning scheme:[/] {Markup.Escape(gd.WinningScheme ?? "n/a")}"); + AnsiConsole.MarkupLine($"[green]Winning candidate:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); @@ -273,8 +275,9 @@ await benchmarkService.RunAllBenchmarksAsync( await dbService.InitializeAsync(forceRebuild: true); long predictedSizePruned = await dbService.PrunePredictedLargerThanQ8Async(mergedPlan); + long highPrecisionPruned = await dbService.PruneHighPrecisionHybridCandidatesAsync(); - AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {learnedPruningResult.GroupSchemeEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {learnedPruningResult.GroupCandidateEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {learnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0}"); AnsiConsole.MarkupLine($"[green]Groups reduced to BF16-only:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); @@ -285,6 +288,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); AnsiConsole.MarkupLine($"[green]Combination count after rule pruning:[/] {comboCountAfterRulePruning:N0}"); AnsiConsole.MarkupLine($"[green]Predicted-size combo removals:[/] {predictedSizePruned:N0}"); + AnsiConsole.MarkupLine($"[green]Late-stage high-precision combo removals:[/] {highPrecisionPruned:N0}"); foreach (var note in isolationResult.Notes) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); @@ -338,6 +342,8 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--relearn-baseline-mappings[/] Delete and relearn baseline tensor mappings (Optional)"); AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); AnsiConsole.MarkupLine(" [green]--use-imatrix[/] Enable imatrix acquisition/build and allow imatrix-required search candidates (Optional)"); + AnsiConsole.MarkupLine(" [green]--allow-high-precision-hybrids[/] Keep BF16/F16 explicit group candidates in final surviving combos (Optional, default false)"); + AnsiConsole.MarkupLine(" [green]--imatrix-force-rebuild[/] Delete/rebuild canonical imatrix artifacts before run (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-url[/] HTTPS URL for direct imatrix artifact download (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-dataset-repo[/] Hugging Face dataset repo ID for imatrix generation (Optional)"); diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index ffcceaa..6e35297 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -13,14 +13,9 @@ public static class ComboLogic public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants baseQuant) { bool imatrixAvailable = RuntimeSearchSpace.HasUsableImatrix(); - - var schemesForRun = TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(s => imatrixAvailable || !s.RequiresImatrix) + var candidatesForRun = BaselineQuants.GetGroupCombinationCandidates(imatrixAvailable, allowHighPrecisionHybrids: true) .ToImmutableArray(); - if (schemesForRun.IsEmpty) - throw new InvalidOperationException("No tensor schemes available for this base."); - var builder = ImmutableArray.CreateBuilder(); var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); @@ -35,23 +30,22 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants var ids = new List(); if (!RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)) - ids.Add(TensorWeightScheme.BF16_F16.UniqueId); + ids.Add(BaselineQuants.BF16_Hybrid.UniqueId); - foreach (var scheme in schemesForRun) + foreach (var candidate in candidatesForRun) { - if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) - continue; - - if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; - ids.Add(scheme.UniqueId); + ids.Add(candidate.UniqueId); } ids = ids.Distinct().OrderBy(x => x).ToList(); if (ids.Count == 0) - throw new InvalidOperationException($"Group '{group.Name}' has no valid tensor schemes for base '{string.Join("/", baseQuant.Names)}'."); + { + ids.Add(BaselineQuants.GetDefaultExplicitFallbackBaseline().UniqueId); + } builder.Add(ids.ToArray()); } diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 70a04d6..69d9d2c 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -1,31 +1,31 @@ -using System; -using System.Collections.Generic; -using System.Linq; using MQ.DB.Models; namespace MagicQuant.Helpers; public sealed class RuntimeLearnedBaselineBanInfo { - public TensorWeightScheme Scheme { get; init; } = default!; + public BaselineQuants Candidate { get; init; } = default!; public IReadOnlyList MissingBaselines { get; init; } = Array.Empty(); } public static class RuntimeSearchSpace { - private static readonly Dictionary> ExplicitSchemeBansByGroup = new(); - private static readonly Dictionary>> LearnedBaselineMissingByGroupAndScheme = new(); + private static readonly Dictionary> ExplicitCandidateBansByGroup = new(); + private static readonly Dictionary>> LearnedBaselineMissingByGroupAndCandidate = new(); private static readonly HashSet DisabledCombinationBaselineIds = new(); private static readonly HashSet Bf16SuppressedTensorChoiceGroupIds = new(); private static bool _imatrixAvailable; + public static bool AllowHighPrecisionHybrids { get; set; } + public static void ResetForNewModel() { - ExplicitSchemeBansByGroup.Clear(); - LearnedBaselineMissingByGroupAndScheme.Clear(); + ExplicitCandidateBansByGroup.Clear(); + LearnedBaselineMissingByGroupAndCandidate.Clear(); DisabledCombinationBaselineIds.Clear(); Bf16SuppressedTensorChoiceGroupIds.Clear(); _imatrixAvailable = false; + AllowHighPrecisionHybrids = false; TensorWeightScheme.ResetAllRuntimeBans(); } @@ -33,175 +33,113 @@ public static void ResetForNewModel() public static bool HasUsableImatrix() => _imatrixAvailable; - public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) + public static void BanCombinationCandidateForGroup(TensorGroup group, BaselineQuants candidate) { - if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || - scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + if (candidate.UniqueId == BaselineQuants.GetDefaultExplicitFallbackBaseline().UniqueId) return; - if (!ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set)) + if (!ExplicitCandidateBansByGroup.TryGetValue(group.UniqueId, out var set)) { set = new HashSet(); - ExplicitSchemeBansByGroup[group.UniqueId] = set; + ExplicitCandidateBansByGroup[group.UniqueId] = set; } - set.Add(scheme.UniqueId); - - if (!scheme.IsBannedFor(group)) - scheme.BannedGroups.Add(group); + set.Add(candidate.UniqueId); } - public static void BanSchemeForGroupByLearnedBaselineAbsence( + public static void BanCombinationCandidateForGroupByLearnedBaselineAbsence( TensorGroup group, - TensorWeightScheme scheme, + BaselineQuants candidate, BaselineQuants sourceBaseline) { - if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || - scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) - return; - - BanSchemeForGroup(group, scheme); + BanCombinationCandidateForGroup(group, candidate); - if (!LearnedBaselineMissingByGroupAndScheme.TryGetValue(group.UniqueId, out var byScheme)) + if (!LearnedBaselineMissingByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) { - byScheme = new Dictionary>(); - LearnedBaselineMissingByGroupAndScheme[group.UniqueId] = byScheme; + byCandidate = new Dictionary>(); + LearnedBaselineMissingByGroupAndCandidate[group.UniqueId] = byCandidate; } - if (!byScheme.TryGetValue(scheme.UniqueId, out var baselineIds)) + if (!byCandidate.TryGetValue(candidate.UniqueId, out var baselineIds)) { baselineIds = new HashSet(); - byScheme[scheme.UniqueId] = baselineIds; + byCandidate[candidate.UniqueId] = baselineIds; } baselineIds.Add(sourceBaseline.UniqueId); } - public static void BanAllExplicitTensorSchemesForGroup(TensorGroup group) + public static void BanAllExplicitCombinationCandidatesForGroup(TensorGroup group) { - foreach (var scheme in TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId && - x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId)) - { - BanSchemeForGroup(group, scheme); - } + foreach (var candidate in BaselineQuants.GetGroupCombinationCandidates(_imatrixAvailable, allowHighPrecisionHybrids: true)) + BanCombinationCandidateForGroup(group, candidate); } - public static IReadOnlyList GetRuntimeExplicitBansForGroup(TensorGroup group) + public static IReadOnlyList GetRuntimeExplicitCandidateBansForGroup(TensorGroup group) { - if (!ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set)) - return Array.Empty(); + if (!ExplicitCandidateBansByGroup.TryGetValue(group.UniqueId, out var set)) + return Array.Empty(); - return TensorWeightScheme.All_Allowed_Hybrid_Quants + return BaselineQuants.GetAllRecognizedBaselines() .Where(x => set.Contains(x.UniqueId)) .OrderBy(x => x.UniqueId) .ToList(); } - public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeightScheme scheme) - { - return ExplicitSchemeBansByGroup.TryGetValue(group.UniqueId, out var set) && - set.Contains(scheme.UniqueId); - } + public static bool IsCombinationCandidateRuntimeBannedForGroup(TensorGroup group, BaselineQuants candidate) + => ExplicitCandidateBansByGroup.TryGetValue(group.UniqueId, out var set) && set.Contains(candidate.UniqueId); - public static bool IsGroupExplicitQuantBanned(TensorGroup group) + public static bool HasAnyExplicitCombinationCandidateAllowed(TensorGroup group) { - return !HasAnyExplicitSchemeAllowed(group); + return BaselineQuants.GetGroupCombinationCandidates(_imatrixAvailable, allowHighPrecisionHybrids: true) + .Any(x => !IsCombinationCandidateRuntimeBannedForGroup(group, x)); } + public static bool IsGroupExplicitCandidateBanned(TensorGroup group) => !HasAnyExplicitCombinationCandidateAllowed(group); + public static IReadOnlyList GetGroupsWithExplicitQuantBanned() - { - return TReg.All - .Where(IsGroupExplicitQuantBanned) - .OrderBy(x => x.UniqueId) - .ToList(); - } + => TReg.All.Where(IsGroupExplicitCandidateBanned).OrderBy(x => x.UniqueId).ToList(); public static bool HasLearnedBaselineMissingPrunesForGroup(TensorGroup group) - { - return LearnedBaselineMissingByGroupAndScheme.TryGetValue(group.UniqueId, out var byScheme) && - byScheme.Count > 0; - } + => LearnedBaselineMissingByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate) && byCandidate.Count > 0; public static IReadOnlyList GetGroupsWithLearnedBaselineMissingPrunes() - { - return TReg.All - .Where(HasLearnedBaselineMissingPrunesForGroup) - .OrderBy(x => x.UniqueId) - .ToList(); - } + => TReg.All.Where(HasLearnedBaselineMissingPrunesForGroup).OrderBy(x => x.UniqueId).ToList(); - public static IReadOnlyList GetLearnedBaselineMissingPrunedSchemesForGroup( - TensorGroup group) + public static IReadOnlyList GetLearnedBaselineMissingPrunedSchemesForGroup(TensorGroup group) { - if (!LearnedBaselineMissingByGroupAndScheme.TryGetValue(group.UniqueId, out var byScheme)) + if (!LearnedBaselineMissingByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) return Array.Empty(); var result = new List(); - - foreach (var kvp in byScheme.OrderBy(x => x.Key)) + foreach (var kvp in byCandidate.OrderBy(x => x.Key)) { - var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants - .FirstOrDefault(x => x.UniqueId == kvp.Key); - - if (scheme == null) - continue; - - var baselines = kvp.Value - .OrderBy(x => x) - .Select(BaselineQuants.FromId) - .ToList(); - - result.Add(new RuntimeLearnedBaselineBanInfo - { - Scheme = scheme, - MissingBaselines = baselines - }); + var candidate = BaselineQuants.FromId(kvp.Key); + var baselines = kvp.Value.OrderBy(x => x).Select(BaselineQuants.FromId).ToList(); + result.Add(new RuntimeLearnedBaselineBanInfo { Candidate = candidate, MissingBaselines = baselines }); } return result; } - public static void SuppressBf16TensorChoice(TensorGroup group) - => Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); + public static void SuppressBf16TensorChoice(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); public static bool IsBf16TensorChoiceSuppressed(TensorGroup group) - { - // BF16 suppression is only meaningful while at least one explicit tensor scheme remains. - // If explicit schemes are all banned, BF16 becomes the only viable tensor choice. - return Bf16SuppressedTensorChoiceGroupIds.Contains(group.UniqueId) && - HasAnyExplicitSchemeAllowed(group); - } + => Bf16SuppressedTensorChoiceGroupIds.Contains(group.UniqueId) && HasAnyExplicitCombinationCandidateAllowed(group); public static IReadOnlyList GetBf16SuppressedGroups() - { - return TReg.All - .Where(IsBf16TensorChoiceSuppressed) - .OrderBy(x => x.UniqueId) - .ToList(); - } + => TReg.All.Where(IsBf16TensorChoiceSuppressed).OrderBy(x => x.UniqueId).ToList(); public static (bool ExplicitAllowed, bool Bf16Allowed) GetFinalAllowedQuantFamiliesForGroup(TensorGroup group) { - bool explicitAllowed = HasAnyExplicitSchemeAllowed(group); - bool bf16Allowed = !IsBf16TensorChoiceSuppressed(group); + bool explicitAllowed = HasAnyExplicitCombinationCandidateAllowed(group); + bool bf16Allowed = !IsBf16TensorChoiceSuppressed(group) || !explicitAllowed; return (explicitAllowed, bf16Allowed); } - private static bool HasAnyExplicitSchemeAllowed(TensorGroup group) - { - return TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Where(x => _imatrixAvailable || !x.RequiresImatrix) - .Any(x => !IsSchemeRuntimeBannedForGroup(group, x)); - } - public static IReadOnlyList GetActiveCombinationBaselines() { - return BaselineQuants.All - .Where(x => x.BaseConversionBase != null) - .Where(x => _imatrixAvailable || !x.RequiresImatrix) + return BaselineQuants.GetCombinationCarrierBaselines(_imatrixAvailable) .Where(x => !DisabledCombinationBaselineIds.Contains(x.UniqueId)) .OrderBy(x => x.UniqueId) .ToList(); @@ -209,7 +147,7 @@ public static IReadOnlyList GetActiveCombinationBaselines() public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allowDisablingLast = false) { - if (baseline.BaseConversionBase == null || DisabledCombinationBaselineIds.Contains(baseline.UniqueId)) + if (!baseline.IsCombinationCarrierCandidate || DisabledCombinationBaselineIds.Contains(baseline.UniqueId)) return false; int currentlyActive = GetActiveCombinationBaselines().Count; @@ -222,4 +160,20 @@ public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allo public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) => DisabledCombinationBaselineIds.Contains(baseline.UniqueId); + + // Legacy compatibility wrappers (scheme-driven callers) + public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) + => BanCombinationCandidateForGroup(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId)); + + public static void BanSchemeForGroupByLearnedBaselineAbsence(TensorGroup group, TensorWeightScheme scheme, BaselineQuants sourceBaseline) + => BanCombinationCandidateForGroupByLearnedBaselineAbsence(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId), sourceBaseline); + + public static void BanAllExplicitTensorSchemesForGroup(TensorGroup group) + => BanAllExplicitCombinationCandidatesForGroup(group); + + public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeightScheme scheme) + => IsCombinationCandidateRuntimeBannedForGroup(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId)); + + public static bool IsGroupExplicitQuantBanned(TensorGroup group) + => IsGroupExplicitCandidateBanned(group); } diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index 2306124..cd06fc6 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -15,7 +15,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc var activeBaselines = RuntimeSearchSpace.GetActiveCombinationBaselines().ToList(); var disabledBaselines = BaselineQuants.All - .Where(x => x.BaseConversionBase != null) + .Where(x => x.IsCombinationCarrierCandidate) .Where(x => RuntimeSearchSpace.IsCombinationBaselineDisabled(x)) .OrderBy(x => x.UniqueId) .ToList(); @@ -57,7 +57,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc var learned = RuntimeSearchSpace.GetLearnedBaselineMissingPrunedSchemesForGroup(group); var parts = learned.Select(x => - $"{x.Scheme.Names[0]} <= {string.Join("/", x.MissingBaselines.Select(b => b.Names[0]))}"); + $"{x.Candidate.Names[0]} <= {string.Join("/", x.MissingBaselines.Select(b => b.Names[0]))}"); AnsiConsole.MarkupLine( $" [yellow]- {Markup.Escape(group.Name)}[/] :: [grey]{Markup.Escape(string.Join(", ", parts))}[/]"); @@ -87,13 +87,13 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc if (id == TensorWeightScheme.NULL.UniqueId) return "NULL"; - var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.FirstOrDefault(x => x.UniqueId == id); - return scheme?.Names[0] ?? $"Unknown({id})"; + var candidate = BaselineQuants.All.FirstOrDefault(x => x.UniqueId == id); + return candidate?.Names[0] ?? $"Unknown({id})"; }).ToList(); string state = unusedIds.Contains(group.UniqueId) ? "unused->NULL" : - RuntimeSearchSpace.IsGroupExplicitQuantBanned(group) ? "BF16-only" : + RuntimeSearchSpace.IsGroupExplicitCandidateBanned(group) ? "BF16-only" : RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group) ? "BF16-suppressed" : RuntimeSearchSpace.HasLearnedBaselineMissingPrunesForGroup(group) ? "learned-pruned" : "variable"; diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index ea07408..23a2ed8 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -20,9 +20,7 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( var result = new RequiredSampleGenerationResult(); - foreach (var baseline in BaselineQuants.All - .Where(x => RuntimeSearchSpace.HasUsableImatrix() || !x.RequiresImatrix) - .OrderBy(x => x.UniqueId)) + foreach (var baseline in BaselineQuants.GetPureBaselineCandidates(RuntimeSearchSpace.HasUsableImatrix())) { result.Plans.Add(new RequiredSamplePlan { @@ -46,7 +44,7 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( Quant = HybridQuant.CreateBlanket( baseQuant: baseline, groups: activeGroups, - blanketScheme: TensorWeightScheme.BF16_F16), + blanketCandidate: BaselineQuants.BF16_Hybrid), TestedBaselineId = baseline.UniqueId }); @@ -63,7 +61,7 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( Quant = HybridQuant.CreateBlanket( baseQuant: carrier, groups: activeGroups, - blanketScheme: TensorWeightScheme.BF16_F16), + blanketCandidate: BaselineQuants.BF16_Hybrid), TestedBaselineId = carrier.UniqueId }); @@ -71,17 +69,18 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( foreach (var group in activeGroups) { - var smallest = GetSmallestAllowedProbeSchemeForGroup(group); + var smallest = GetSmallestAllowedProbeCandidateForGroup(group); if (smallest == null) continue; var quant = HybridQuant.CreateBlanket( baseQuant: carrier, groups: activeGroups, - blanketScheme: TensorWeightScheme.BF16_F16); + blanketCandidate: BaselineQuants.BF16_Hybrid); var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); - target.TensorType = smallest; + target.CandidateBaseline = smallest; + target.TensorType = smallest.DefaultTensorScheme!; result.Plans.Add(new RequiredSamplePlan { @@ -125,41 +124,40 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var result = new RequiredSampleGenerationResult(); var carrier = BaselineQuants.Q8_0; - var schemes = TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Where(x => RuntimeSearchSpace.HasUsableImatrix() || !x.RequiresImatrix) + var candidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: true) + .Where(x => x.UniqueId != BaselineQuants.BF16_Hybrid.UniqueId) .OrderBy(x => x.UniqueId) .ToList(); foreach (var group in activeGroups) { - var smallest = GetSmallestAllowedProbeSchemeForGroup(group); + var smallest = GetSmallestAllowedProbeCandidateForGroup(group); - foreach (var scheme in schemes) + foreach (var candidate in candidates) { - if (smallest != null && scheme.UniqueId == smallest.UniqueId) + if (smallest != null && candidate.UniqueId == smallest.UniqueId) continue; - if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; var quant = HybridQuant.CreateBlanket( baseQuant: carrier, groups: TReg.All.Where(x => !missingIds.Contains(x.UniqueId)), - blanketScheme: TensorWeightScheme.BF16_F16); + blanketCandidate: BaselineQuants.BF16_Hybrid); var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); - target.TensorType = scheme; + target.CandidateBaseline = candidate; + target.TensorType = candidate.DefaultTensorScheme!; result.Plans.Add(new RequiredSamplePlan { Kind = RequiredSampleKind.GroupIsolationContinuation, - Key = $"cont:{carrier.UniqueId}:{group.UniqueId}:{scheme.UniqueId}", - Description = $"Continuation isolation for group '{group.Name}' using '{scheme.Names[0]}'.", + Key = $"cont:{carrier.UniqueId}:{group.UniqueId}:{candidate.UniqueId}", + Description = $"Continuation isolation for group '{group.Name}' using '{candidate.Names[0]}'.", Quant = quant, TargetGroupId = group.UniqueId, - TestedSchemeId = scheme.UniqueId, + TestedSchemeId = candidate.UniqueId, TestedBaselineId = carrier.UniqueId }); @@ -295,26 +293,24 @@ public static IEnumerable> GenerateTensorConfigBatches( producer.GetAwaiter().GetResult(); } - private static TensorWeightScheme? GetSmallestAllowedProbeSchemeForGroup(TensorGroup group) + private static BaselineQuants? GetSmallestAllowedProbeCandidateForGroup(TensorGroup group) { - var allowedIds = TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(x => RuntimeSearchSpace.HasUsableImatrix() || !x.RequiresImatrix) - .Select(x => x.UniqueId) - .ToHashSet(); + var orderedCandidates = TensorWeightScheme.GetSmallestInOrder() + .Select(x => BaselineQuants.FromTensorSchemeId(x.UniqueId)) + .DistinctBy(x => x.UniqueId); - foreach (var scheme in TensorWeightScheme.GetSmallestInOrder()) + foreach (var candidate in orderedCandidates) { - if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId || - scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + if (candidate.UniqueId == BaselineQuants.BF16_Hybrid.UniqueId || candidate.UniqueId == BaselineQuants.F16_Hybrid.UniqueId) continue; - if (!allowedIds.Contains(scheme.UniqueId)) + if (candidate.RequiresImatrix && !RuntimeSearchSpace.HasUsableImatrix()) continue; - if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; - return scheme; + return candidate; } return null; diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index 330ae04..b351f66 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -26,6 +26,7 @@ public async Task EnsureImatrixAsync(ImatrixRequest request AnsiConsole.MarkupLine("[grey]Imatrix: disabled by --use-imatrix flag (false).[/]"); Cache.IsImatrixAvailable = false; Cache.ActiveImatrixPath = null; + Cache.ActiveImatrixIdentityHash = null; RuntimeSearchSpace.SetImatrixAvailability(false); return new ImatrixEnsureResult { Enabled = false, Available = false }; } @@ -59,6 +60,7 @@ public async Task EnsureImatrixAsync(ImatrixRequest request Cache.IsImatrixAvailable = true; Cache.ActiveImatrixPath = datPath; + Cache.ActiveImatrixIdentityHash = await ComputeSha256Async(datPath, ct); RuntimeSearchSpace.SetImatrixAvailability(true); AnsiConsole.MarkupLine($"[green]Imatrix: ready (rebuilt).[/] [grey]{Markup.Escape(datPath)}[/]"); @@ -74,6 +76,7 @@ public async Task EnsureImatrixAsync(ImatrixRequest request Cache.IsImatrixAvailable = true; Cache.ActiveImatrixPath = datPath; + Cache.ActiveImatrixIdentityHash = await ComputeSha256Async(datPath, ct); RuntimeSearchSpace.SetImatrixAvailability(true); AnsiConsole.MarkupLine($"[green]Imatrix: ready (reused existing trusted artifact).[/] [grey]{Markup.Escape(datPath)}[/]"); diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 6099be6..6d7f7e0 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -23,7 +23,7 @@ public sealed class IsolationGroupDecision public bool ExplicitQuantBanned { get; set; } public bool Bf16Suppressed { get; set; } - public string? WinningScheme { get; set; } + public string? WinningCandidate { get; set; } public ulong? WinningSizeBytes { get; set; } public double? WinningKld { get; set; } public double? WinningPplDelta { get; set; } @@ -92,7 +92,7 @@ public async Task AnalyzeInitialIsolationProbesA if (snap == null) continue; - var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.First(x => x.UniqueId == item.TestedSchemeId); + var candidate = BaselineQuants.FromId(item.TestedSchemeId!.Value); var reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.SizeBytes); var kld = GetAggregateKld(snap); var pplDelta = GetAggregatePplDeltaPercent(snap, nativeBaseline); @@ -101,7 +101,7 @@ public async Task AnalyzeInitialIsolationProbesA { GroupName = group.Name, BestReductionRatio = reduction, - WinningScheme = scheme.Names[0], + WinningCandidate = candidate.Names[0], WinningSizeBytes = snap.SizeBytes, WinningKld = kld, WinningPplDelta = pplDelta @@ -112,7 +112,7 @@ public async Task AnalyzeInitialIsolationProbesA if (reduction < options.MinMeaningfulGroupReductionRatio) { - RuntimeSearchSpace.BanAllExplicitTensorSchemesForGroup(group); + RuntimeSearchSpace.BanAllExplicitCombinationCandidatesForGroup(group); decision.ExplicitQuantBanned = true; result.Notes.Add( @@ -182,12 +182,12 @@ public async Task AnalyzeAndApplyFinalAsync( if (snap == null) continue; - var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.First(x => x.UniqueId == item.TestedSchemeId); + var candidate = BaselineQuants.FromId(item.TestedSchemeId!.Value); candidates.Add(new GroupCandidate { Group = group, - Scheme = scheme, + Candidate = candidate, SizeBytes = snap.SizeBytes, SavingsRatio = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.SizeBytes), Kld = GetAggregateKld(snap), @@ -206,7 +206,7 @@ public async Task AnalyzeAndApplyFinalAsync( foreach (var candidate in candidates.ToList()) { - if (candidate.Scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId) + if (candidate.Candidate.UniqueId == BaselineQuants.BF16_Hybrid.UniqueId || candidate.Candidate.UniqueId == BaselineQuants.F16_Hybrid.UniqueId) continue; bool hardFail = @@ -216,11 +216,11 @@ public async Task AnalyzeAndApplyFinalAsync( if (!hardFail) continue; - RuntimeSearchSpace.BanSchemeForGroup(group, candidate.Scheme); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate.Candidate); result.HardDamageEliminations++; result.Notes.Add( - $"Hard damage elimination: '{candidate.Scheme.Names[0]}' removed for '{group.Name}' " + + $"Hard damage elimination: '{candidate.Candidate.Names[0]}' removed for '{group.Name}' " + $"(savings={candidate.SavingsRatio:P2}, KLD={candidate.Kld:G6}, PPLΔ={candidate.PplDeltaPercent:F4}%)."); } @@ -247,7 +247,7 @@ public async Task AnalyzeAndApplyFinalAsync( var winner = candidates.First(); - decision.WinningScheme = winner.Scheme.Names[0]; + decision.WinningCandidate = winner.Candidate.Names[0]; decision.WinningSizeBytes = winner.SizeBytes; decision.WinningKld = winner.Kld; decision.WinningPplDelta = winner.PplDeltaPercent; @@ -256,7 +256,7 @@ public async Task AnalyzeAndApplyFinalAsync( foreach (var candidate in candidates.OrderBy(x => x.SizeBytes)) { decision.Candidates.Add( - $"{candidate.Scheme.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}%"); + $"{candidate.Candidate.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}%"); } foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedSchemesForGroup(group)) @@ -266,7 +266,7 @@ public async Task AnalyzeAndApplyFinalAsync( banInfo.MissingBaselines.Select(x => x.Names[0])); decision.Candidates.Add( - $"[pruned-early] {banInfo.Scheme.Names[0]} removed by learned-baseline mapping for this group (no matching tensor weights in baseline(s): {sourceBaselines})."); + $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned-baseline mapping for this group (no matching tensor weights in baseline(s): {sourceBaselines})."); } result.GroupDetails.Add(decision); @@ -324,8 +324,9 @@ private static void PopulateFinalGroupFlags( private static List FilterSurvivors(TensorGroup group, List candidates) { return candidates - .Where(x => x.Scheme.UniqueId == TensorWeightScheme.BF16_F16.UniqueId || - !RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, x.Scheme)) + .Where(x => x.Candidate.UniqueId == BaselineQuants.BF16_Hybrid.UniqueId || + x.Candidate.UniqueId == BaselineQuants.F16_Hybrid.UniqueId || + !RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x.Candidate)) .ToList(); } @@ -357,13 +358,13 @@ private static void ApplyDominanceElimination( if (sameOrSmaller && kldNoWorse && pplNoWorse && strictlyBetter) { - if (!RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, b.Scheme)) + if (!RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, b.Candidate)) { - RuntimeSearchSpace.BanSchemeForGroup(group, b.Scheme); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, b.Candidate); result.DominatedGroupSchemesBanned++; result.Notes.Add( - $"Dominance elimination: '{b.Scheme.Names[0]}' removed for '{group.Name}' because '{a.Scheme.Names[0]}' was same-size-or-smaller and no worse on KLD/PPL."); + $"Dominance elimination: '{b.Candidate.Names[0]}' removed for '{group.Name}' because '{a.Candidate.Names[0]}' was same-size-or-smaller and no worse on KLD/PPL."); } } } @@ -393,15 +394,15 @@ private static void ApplyBadTradeElimination( foreach (var candidate in sizeBuckets[i]) { - if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, candidate.Scheme)) + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate.Candidate)) continue; if (ShouldEliminateAsBadTrade(acceptedAnchor, candidate, out var reason)) { - RuntimeSearchSpace.BanSchemeForGroup(group, candidate.Scheme); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate.Candidate); result.BadTradeEliminations++; result.Notes.Add( - $"Bad trade elimination: '{candidate.Scheme.Names[0]}' removed vs accepted anchor '{acceptedAnchor.Scheme.Names[0]}' for '{group.Name}'. {reason}"); + $"Bad trade elimination: '{candidate.Candidate.Names[0]}' removed vs accepted anchor '{acceptedAnchor.Candidate.Names[0]}' for '{group.Name}'. {reason}"); continue; } @@ -417,8 +418,8 @@ private static void ApplyBadTradeElimination( private static List GetActiveExplicitCandidates(TensorGroup group, List candidates) { return candidates - .Where(x => x.Scheme.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Where(x => !RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, x.Scheme)) + .Where(x => x.Candidate.UniqueId != BaselineQuants.BF16_Hybrid.UniqueId && x.Candidate.UniqueId != BaselineQuants.F16_Hybrid.UniqueId) + .Where(x => !RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x.Candidate)) .ToList(); } @@ -430,8 +431,8 @@ private static List> BuildSizeBuckets(List .Select(x => x .OrderBy(c => c.Kld) .ThenBy(c => Math.Abs(c.PplDeltaPercent)) - .ThenByDescending(c => GetSchemeSafetyScore(c.Scheme)) - .ThenBy(c => c.Scheme.Names[0], StringComparer.Ordinal) + .ThenByDescending(c => GetCandidateSafetyScore(c.Candidate)) + .ThenBy(c => c.Candidate.Names[0], StringComparer.Ordinal) .ToList()) .ToList(); } @@ -496,14 +497,14 @@ private static bool ShouldEliminateAsBadTrade( return survivors .OrderBy(x => x.Kld) .ThenBy(x => Math.Abs(x.PplDeltaPercent)) - .ThenByDescending(x => GetSchemeSafetyScore(x.Scheme)) - .ThenBy(x => x.Scheme.Names[0], StringComparer.Ordinal) + .ThenByDescending(x => GetCandidateSafetyScore(x.Candidate)) + .ThenBy(x => x.Candidate.Names[0], StringComparer.Ordinal) .FirstOrDefault(); } - private static int GetSchemeSafetyScore(TensorWeightScheme scheme) + private static int GetCandidateSafetyScore(BaselineQuants candidate) { - string canonical = scheme.Names[0]; + string canonical = candidate.Names[0]; for (int i = 0; i < canonical.Length - 1; i++) { @@ -607,7 +608,7 @@ private static double GetAggregatePplDeltaPercent(BenchmarkSnapshot snapshot, Be private sealed class GroupCandidate { public TensorGroup Group { get; set; } = default!; - public TensorWeightScheme Scheme { get; set; } = default!; + public BaselineQuants Candidate { get; set; } = default!; public ulong SizeBytes { get; set; } public double SavingsRatio { get; set; } public double Kld { get; set; } diff --git a/MagicQuant/Services/IsolationPlanningService.cs b/MagicQuant/Services/IsolationPlanningService.cs new file mode 100644 index 0000000..c862d60 --- /dev/null +++ b/MagicQuant/Services/IsolationPlanningService.cs @@ -0,0 +1,19 @@ +using MagicQuant.Helpers; +using MQ.DB.Models; + +namespace MagicQuant.Services; + +/// +/// Centralized authority for isolation probe planning in baseline-family candidate space. +/// +public sealed class IsolationPlanningService +{ + public RequiredSampleGenerationResult BuildInitialPlan(List? missingTensorGroups = null) + => TensorConfigGenerator.GenerateInitialIsolationSamplePlan(missingTensorGroups); + + public RequiredSampleGenerationResult BuildContinuationPlan(IEnumerable groupIdsToContinue, List? missingTensorGroups = null) + => TensorConfigGenerator.GenerateContinuationIsolationSamplePlan(groupIdsToContinue, missingTensorGroups); + + public List BuildRequiredStartupCombos(List? missingTensorGroups = null) + => TensorConfigGenerator.GenerateRequiredDataSampleCombos(missingTensorGroups); +} diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index d0b2670..017e114 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -13,7 +13,7 @@ namespace MagicQuant.Services; public sealed class LearnedBaselinePruningResult { - public int GroupSchemeEliminations { get; set; } + public int GroupCandidateEliminations { get; set; } public int BaselinesSkippedWithoutLearnedRows { get; set; } public List Notes { get; } = new(); } @@ -84,18 +84,9 @@ internal static void ApplyLearnedBaselinePruning( var aliasToSchemeIds = BuildAliasToSchemeIds(); var effectiveSchemesByBaselineAndGroup = BuildEffectiveSchemesByBaselineAndGroup(learnedRows, aliasToSchemeIds); - var schemeOwnerById = BaselineQuants.All - .SelectMany(b => b.TensorWeightSchemes.Select(s => new - { - SchemeId = s.UniqueId, - Baseline = b - })) - .ToDictionary(x => x.SchemeId, x => x.Baseline); - - var explicitSchemes = TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(x => x.UniqueId != TensorWeightScheme.NULL.UniqueId) - .Where(x => x.UniqueId != TensorWeightScheme.BF16_F16.UniqueId) - .Where(x => RuntimeSearchSpace.HasUsableImatrix() || !x.RequiresImatrix) + var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: true) + .Where(x => x.UniqueId != BaselineQuants.BF16_Hybrid.UniqueId) + .Where(x => x.UniqueId != BaselineQuants.F16_Hybrid.UniqueId) .OrderBy(x => x.UniqueId) .ToList(); @@ -104,29 +95,28 @@ internal static void ApplyLearnedBaselinePruning( if (unusedGroupIds.Contains(group.UniqueId)) continue; - foreach (var scheme in explicitSchemes) + foreach (var candidate in explicitCandidates) { - if (RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(group, scheme)) - continue; - - if (!schemeOwnerById.TryGetValue(scheme.UniqueId, out var owningBaseline)) + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; + var owningBaseline = candidate; var key = (owningBaseline.UniqueId, group.UniqueId); bool hasEffectiveSet = effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var effectiveForGroup); - bool allow = hasEffectiveSet && effectiveForGroup!.Contains(scheme.UniqueId); + var candidateSchemeId = candidate.DefaultTensorScheme?.UniqueId; + bool allow = hasEffectiveSet && candidateSchemeId.HasValue && effectiveForGroup!.Contains(candidateSchemeId.Value); string effectiveIds = hasEffectiveSet ? string.Join(",", effectiveForGroup!.OrderBy(x => x)) : ""; result.Notes.Add( $"Learned-prune check: model={aiModelHashId}/{aiModelHashUniqueHash}, group={group.Name}, " + - $"scheme={scheme.Names[0]}, owner={owningBaseline.Names[0]}, effective=[{effectiveIds}], decision={(allow ? "ALLOW" : "BAN")}"); + $"candidate={candidate.Names[0]}, owner={owningBaseline.Names[0]}, effective=[{effectiveIds}], decision={(allow ? "ALLOW" : "BAN")}"); if (!allow) { - RuntimeSearchSpace.BanSchemeForGroupByLearnedBaselineAbsence(group, scheme, owningBaseline); - result.GroupSchemeEliminations++; + RuntimeSearchSpace.BanCombinationCandidateForGroupByLearnedBaselineAbsence(group, candidate, owningBaseline); + result.GroupCandidateEliminations++; } } } diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 007f1eb..4fc6b8e 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -97,14 +97,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) unusedCount++; Cache.UnusedTensorGroups.Add(group); - foreach (var scheme in TensorWeightScheme.All_Allowed_Hybrid_Quants) - { - if (scheme.UniqueId == TensorWeightScheme.NULL.UniqueId) - continue; - - if (!scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) - scheme.BannedGroups.Add(group); - } + RuntimeSearchSpace.BanAllExplicitCombinationCandidatesForGroup(group); } foreach (var failure in result.Incompatible) @@ -116,10 +109,11 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) if (group == null || scheme == null) continue; - if (scheme.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) + var candidate = BaselineQuants.FromTensorSchemeId(scheme.UniqueId); + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; - scheme.BannedGroups.Add(group); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate); shapeBanCount++; shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{scheme.Names[0]}[/]", "[grey]Block Alignment[/]"); @@ -127,7 +121,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) foreach (var group in TReg.All.Except(Cache.UnusedTensorGroups)) { - if (RuntimeSearchSpace.IsGroupExplicitQuantBanned(group)) + if (RuntimeSearchSpace.IsGroupExplicitCandidateBanned(group)) explicitQuantBannedCount++; } diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 61c334f..05a3aec 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -12,7 +12,7 @@ namespace MagicQuant.Services; public class QuantDatabaseService { - private const string DbFileName = "MagicQuant_Combinations.duckdb"; + private const string DbFileNamePrefix = "MagicQuant_Combinations"; private const string TableName = "tensor_configs"; public async Task GetRemainingCombinationCountAsync(CancellationToken ct = default) @@ -91,7 +91,15 @@ private static string GetDuckDbDirectory() "Neither Cache.ModelMagicQuantDirectory nor Cache.MagicQuantDirectory is set."); } - private string ConnectionString => $"Data Source={Path.Combine(GetDuckDbDirectory(), DbFileName)}"; + private static string BuildContextAwareDuckDbFileName() + { + string model = string.IsNullOrWhiteSpace(Cache.CurrentModelId) ? "unknown-model" : Cache.CurrentModelId; + string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixPath?.GetHashCode().ToString("X") ?? "imatrix") : "no-imatrix"; + string hp = RuntimeSearchSpace.AllowHighPrecisionHybrids ? "hp-on" : "hp-off"; + return $"{DbFileNamePrefix}_{model}_{imatrix}_{hp}.duckdb"; + } + + private string ConnectionString => $"Data Source={Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName())}"; public async Task InitializeAsync(bool forceRebuild = false, CancellationToken ct = default) { @@ -204,6 +212,43 @@ MoeRouter TINYINT return removed; } + + public async Task PruneHighPrecisionHybridCandidatesAsync(CancellationToken ct = default) + { + if (RuntimeSearchSpace.AllowHighPrecisionHybrids) + return 0; + + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + + var rows = await GetRemainingTensorConfigsAsync(ct); + var kept = rows.Where(x => + x.Embeddings != BaselineQuants.BF16_Hybrid.UniqueId && x.Embeddings != BaselineQuants.F16_Hybrid.UniqueId && + x.LmHead != BaselineQuants.BF16_Hybrid.UniqueId && x.LmHead != BaselineQuants.F16_Hybrid.UniqueId && + x.AttnQ != BaselineQuants.BF16_Hybrid.UniqueId && x.AttnQ != BaselineQuants.F16_Hybrid.UniqueId && + x.AttnKV != BaselineQuants.BF16_Hybrid.UniqueId && x.AttnKV != BaselineQuants.F16_Hybrid.UniqueId && + x.AttnOutput != BaselineQuants.BF16_Hybrid.UniqueId && x.AttnOutput != BaselineQuants.F16_Hybrid.UniqueId && + x.FfnUpGate != BaselineQuants.BF16_Hybrid.UniqueId && x.FfnUpGate != BaselineQuants.F16_Hybrid.UniqueId && + x.FfnDown != BaselineQuants.BF16_Hybrid.UniqueId && x.FfnDown != BaselineQuants.F16_Hybrid.UniqueId && + x.MoeExperts != BaselineQuants.BF16_Hybrid.UniqueId && x.MoeExperts != BaselineQuants.F16_Hybrid.UniqueId && + x.MoeRouter != BaselineQuants.BF16_Hybrid.UniqueId && x.MoeRouter != BaselineQuants.F16_Hybrid.UniqueId).ToList(); + + long removed = rows.Count - kept.Count; + if (removed <= 0) + return 0; + + var createCmd = connection.CreateCommand(); + createCmd.CommandText = $@" + DROP TABLE IF EXISTS {TableName}; + CREATE TABLE {TableName} ( + BaseQuant TINYINT, Embeddings TINYINT, LmHead TINYINT, AttnQ TINYINT, AttnKV TINYINT, + AttnOutput TINYINT, FfnUpGate TINYINT, FfnDown TINYINT, MoeExperts TINYINT, MoeRouter TINYINT + );"; + await createCmd.ExecuteNonQueryAsync(ct); + await BulkInsertAsync(connection, kept, ct); + return removed; + } + private async Task GetRowCountAsync(DuckDBConnection connection, CancellationToken ct) { var checkCmd = connection.CreateCommand(); @@ -430,7 +475,7 @@ public ulong Predict(TensorConfig config) private void AddDelta(byte groupId, byte schemeId, ref long total) { - if (schemeId == TensorWeightScheme.BF16_F16.UniqueId) + if (schemeId == BaselineQuants.BF16_Hybrid.UniqueId || schemeId == BaselineQuants.F16_Hybrid.UniqueId) return; if (_deltas.TryGetValue((groupId, schemeId), out long delta)) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 742e3fd..5f4c879 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1444,14 +1444,15 @@ private List BuildRequestedTensorOverrides( if (hybrid.TensorType.UniqueId == TensorWeightScheme.NULL.UniqueId) continue; - if (baseScheme != null && hybrid.TensorType.UniqueId == baseScheme.UniqueId) + if (hybrid.CandidateBaseline != null && hybrid.CandidateBaseline.UniqueId == quant.BaseQuant.UniqueId) continue; - var learned = TryLoadLearnedTensorMapping(hybrid.TensorType, hybrid.TGroup); + var sourceScheme = hybrid.CandidateBaseline?.DefaultTensorScheme ?? hybrid.TensorType; + var learned = TryLoadLearnedTensorMapping(sourceScheme, hybrid.TGroup, hybrid.CandidateBaseline); if (learned.Count == 0) { throw new InvalidOperationException( - $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + scheme '{ResolveSchemeName(hybrid.TensorType)}'. " + + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + scheme '{hybrid.CandidateBaseline?.Names[0] ?? ResolveSchemeName(hybrid.TensorType)}'. " + "Run with --relearn-baseline-mappings to regenerate."); } @@ -1469,7 +1470,7 @@ private List BuildRequestedTensorOverrides( var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); throw new InvalidOperationException( - $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + scheme '{ResolveSchemeName(hybrid.TensorType)}'. " + + $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + scheme '{hybrid.CandidateBaseline?.Names[0] ?? ResolveSchemeName(hybrid.TensorType)}'. " + $"Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); } @@ -1487,7 +1488,7 @@ private List BuildRequestedTensorOverrides( return result; } - private Dictionary TryLoadLearnedTensorMapping(TensorWeightScheme sourceScheme, TensorGroup targetGroup) + private Dictionary TryLoadLearnedTensorMapping(TensorWeightScheme sourceScheme, TensorGroup targetGroup, BaselineQuants? sourceBaseline = null) { using var db = new MagicQuantContext(); @@ -1505,7 +1506,7 @@ private Dictionary TryLoadLearnedTensorMapping(TensorWeightSchem } else { - var baseline = BaselineQuants.All.FirstOrDefault(x => + var baseline = sourceBaseline ?? BaselineQuants.All.FirstOrDefault(x => x.TensorWeightSchemes.Any(s => s.UniqueId == sourceScheme.UniqueId)); if (baseline == null) @@ -1772,14 +1773,14 @@ public string GenerateHybridName(HybridQuant quant) string baseName = ResolveBaseName(quant.BaseQuant); var effectiveTensors = quant.Tensors? - .Where(t => t?.TGroup != null && t.TensorType.UniqueId != TensorWeightScheme.NULL.UniqueId) + .Where(t => t?.TGroup != null && t.CandidateBaseline != null) .ToList(); if (effectiveTensors == null || effectiveTensors.Count == 0) return $"{modelName}-{baseName}"; var grouped = effectiveTensors - .GroupBy(t => ResolveSchemeName(t.TensorType)) + .GroupBy(t => t.CandidateBaseline.Names[0]) .Select(g => new { Type = g.Key, From 85bddf8714a410dec58bc0a7d71a02bdda40eff9 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 14:32:41 -0400 Subject: [PATCH 090/258] Complete baseline-candidate isolation and imatrix identity persistence wiring --- ...0260420193000_AddImatrixContextIdentity.cs | 86 ++++++++++ .../MagicQuantContextModelSnapshot.cs | 142 ++++++++++++++++- MQ.DB/Models/RequiredSamplePlan.cs | 7 + .../AuthorityUsageRegressionTests.cs | 23 +++ .../BaselineCandidatePolicyTests.cs | 53 +++++++ .../ImatrixIdentityServiceTests.cs | 28 ++++ .../LearnedBaselinePruningServiceTests.cs | 10 +- MagicQuant/Commands/Evolution.cs | 2 +- MagicQuant/Helpers/ComboLogic.cs | 6 +- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 2 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 2 +- MagicQuant/Services/BenchmarkService.cs | 28 +++- MagicQuant/Services/ImatrixIdentityService.cs | 62 ++++++++ .../Services/IsolationOptimizationService.cs | 149 +++++++----------- .../Services/ModelCompatibilityService.cs | 18 +-- MagicQuant/Services/QuantDatabaseService.cs | 11 +- MagicQuant/Services/QuantizationService.cs | 24 ++- 17 files changed, 525 insertions(+), 128 deletions(-) create mode 100644 MQ.DB/Migrations/20260420193000_AddImatrixContextIdentity.cs create mode 100644 MagicQuant.Tests/AuthorityUsageRegressionTests.cs create mode 100644 MagicQuant.Tests/BaselineCandidatePolicyTests.cs create mode 100644 MagicQuant.Tests/ImatrixIdentityServiceTests.cs create mode 100644 MagicQuant/Services/ImatrixIdentityService.cs diff --git a/MQ.DB/Migrations/20260420193000_AddImatrixContextIdentity.cs b/MQ.DB/Migrations/20260420193000_AddImatrixContextIdentity.cs new file mode 100644 index 0000000..aadb05b --- /dev/null +++ b/MQ.DB/Migrations/20260420193000_AddImatrixContextIdentity.cs @@ -0,0 +1,86 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + public partial class AddImatrixContextIdentity : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "ImatrixDefinitions", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + IdentityHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), + CanonicalPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true), + SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + MetadataJson = table.Column(type: "TEXT", maxLength: 8000, nullable: true), + TokenCount = table.Column(type: "INTEGER", nullable: true), + BuildFingerprint = table.Column(type: "TEXT", maxLength: 512, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ImatrixDefinitions", x => x.Id); + table.ForeignKey( + name: "FK_ImatrixDefinitions_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.AddColumn(name: "ImatrixDefinitionId", table: "AiBenchmarks", type: "INTEGER", nullable: true); + migrationBuilder.AddColumn(name: "ImatrixDefinitionId", table: "QuantizationRuns", type: "INTEGER", nullable: true); + migrationBuilder.AddColumn(name: "ImatrixDefinitionId", table: "BenchmarkRuns", type: "INTEGER", nullable: true); + migrationBuilder.AddColumn(name: "ImatrixDefinitionId", table: "ExecutionPlanProbeCaches", type: "INTEGER", nullable: true); + + migrationBuilder.CreateIndex(name: "IX_ImatrixDefinitions_AiModelHashId_IdentityHash", table: "ImatrixDefinitions", columns: new[] { "AiModelHashId", "IdentityHash" }, unique: true); + migrationBuilder.CreateIndex(name: "IX_AiBenchmarks_ImatrixDefinitionId", table: "AiBenchmarks", column: "ImatrixDefinitionId"); + migrationBuilder.CreateIndex(name: "IX_QuantizationRuns_ImatrixDefinitionId", table: "QuantizationRuns", column: "ImatrixDefinitionId"); + migrationBuilder.CreateIndex(name: "IX_BenchmarkRuns_ImatrixDefinitionId", table: "BenchmarkRuns", column: "ImatrixDefinitionId"); + migrationBuilder.CreateIndex(name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", table: "ExecutionPlanProbeCaches", column: "ImatrixDefinitionId"); + + migrationBuilder.DropIndex(name: "IX_AiBenchmarks_AiModelHashId_TensorComboId", table: "AiBenchmarks"); + migrationBuilder.CreateIndex(name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", table: "AiBenchmarks", columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, unique: true); + + migrationBuilder.DropIndex(name: "IX_ExecutionPlanProbeCaches_AiModelHashId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches"); + migrationBuilder.CreateIndex(name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches", columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, unique: true); + + migrationBuilder.AddForeignKey(name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", table: "AiBenchmarks", column: "ImatrixDefinitionId", principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); + migrationBuilder.AddForeignKey(name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", table: "QuantizationRuns", column: "ImatrixDefinitionId", principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); + migrationBuilder.AddForeignKey(name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", table: "BenchmarkRuns", column: "ImatrixDefinitionId", principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); + migrationBuilder.AddForeignKey(name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", table: "ExecutionPlanProbeCaches", column: "ImatrixDefinitionId", principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey(name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", table: "AiBenchmarks"); + migrationBuilder.DropForeignKey(name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", table: "QuantizationRuns"); + migrationBuilder.DropForeignKey(name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", table: "BenchmarkRuns"); + migrationBuilder.DropForeignKey(name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropTable(name: "ImatrixDefinitions"); + + migrationBuilder.DropIndex(name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", table: "AiBenchmarks"); + migrationBuilder.DropIndex(name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches"); + migrationBuilder.DropIndex(name: "IX_AiBenchmarks_ImatrixDefinitionId", table: "AiBenchmarks"); + migrationBuilder.DropIndex(name: "IX_QuantizationRuns_ImatrixDefinitionId", table: "QuantizationRuns"); + migrationBuilder.DropIndex(name: "IX_BenchmarkRuns_ImatrixDefinitionId", table: "BenchmarkRuns"); + migrationBuilder.DropIndex(name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn(name: "ImatrixDefinitionId", table: "AiBenchmarks"); + migrationBuilder.DropColumn(name: "ImatrixDefinitionId", table: "QuantizationRuns"); + migrationBuilder.DropColumn(name: "ImatrixDefinitionId", table: "BenchmarkRuns"); + migrationBuilder.DropColumn(name: "ImatrixDefinitionId", table: "ExecutionPlanProbeCaches"); + + migrationBuilder.CreateIndex(name: "IX_AiBenchmarks_AiModelHashId_TensorComboId", table: "AiBenchmarks", columns: new[] { "AiModelHashId", "TensorComboId" }, unique: true); + migrationBuilder.CreateIndex(name: "IX_ExecutionPlanProbeCaches_AiModelHashId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches", columns: new[] { "AiModelHashId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, unique: true); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index 4532c9e..1d79fdf 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -25,6 +25,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("Ngl") .HasColumnType("INTEGER"); @@ -41,9 +44,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("TensorComboId"); - b.HasIndex("AiModelHashId", "TensorComboId") + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") .IsUnique(); + b.HasIndex("ImatrixDefinitionId"); + b.ToTable("AiBenchmarks"); }); @@ -107,6 +112,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("Category") .HasColumnType("INTEGER"); @@ -138,6 +146,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); + b.HasIndex("ImatrixDefinitionId"); + b.HasIndex("CategoryBenchmarkId"); b.HasIndex("StartedUtc"); @@ -187,6 +197,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("CreatedUtc") .HasColumnType("TEXT"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("DiscoveryTokenTarget") .HasColumnType("INTEGER"); @@ -226,12 +239,60 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); - b.HasIndex("AiModelHashId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") .IsUnique(); + b.HasIndex("ImatrixDefinitionId"); + b.ToTable("ExecutionPlanProbeCaches"); }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => { b.Property("Id") @@ -373,6 +434,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") .WithMany() .HasForeignKey("TensorComboId") @@ -398,6 +464,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") .WithMany() .HasForeignKey("CategoryBenchmarkId") @@ -430,6 +501,68 @@ protected override void BuildModel(ModelBuilder modelBuilder) }); modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + }); + + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => { b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") .WithMany() @@ -472,6 +605,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") .WithMany() .HasForeignKey("TensorComboId") diff --git a/MQ.DB/Models/RequiredSamplePlan.cs b/MQ.DB/Models/RequiredSamplePlan.cs index 7c40706..e447c24 100644 --- a/MQ.DB/Models/RequiredSamplePlan.cs +++ b/MQ.DB/Models/RequiredSamplePlan.cs @@ -15,7 +15,14 @@ public sealed class RequiredSamplePlan public string Description { get; set; } = string.Empty; public HybridQuant Quant { get; set; } = default!; public byte? TargetGroupId { get; set; } + // Legacy name kept for compatibility: this now stores the tested baseline-family candidate id. public byte? TestedSchemeId { get; set; } + + public byte? TestedCandidateId + { + get => TestedSchemeId; + set => TestedSchemeId = value; + } public byte? TestedBaselineId { get; set; } public bool IsSmallestProbe { get; set; } } diff --git a/MagicQuant.Tests/AuthorityUsageRegressionTests.cs b/MagicQuant.Tests/AuthorityUsageRegressionTests.cs new file mode 100644 index 0000000..e3a03ae --- /dev/null +++ b/MagicQuant.Tests/AuthorityUsageRegressionTests.cs @@ -0,0 +1,23 @@ +using Xunit; + +namespace MagicQuant.Tests; + +public class AuthorityUsageRegressionTests +{ + [Fact] + public void ComboGenerationPaths_DoNotUseLegacyAllAllowedHybridQuantsAuthority() + { + var files = new[] + { + Path.Combine("..", "MagicQuant", "Helpers", "ComboLogic.cs"), + Path.Combine("..", "MagicQuant", "Helpers", "TensorConfigGenerator.cs"), + Path.Combine("..", "MagicQuant", "Services", "IsolationOptimizationService.cs") + }; + + foreach (var file in files) + { + var text = File.ReadAllText(file); + Assert.DoesNotContain("All_Allowed_Hybrid_Quants", text); + } + } +} diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs new file mode 100644 index 0000000..e872f2e --- /dev/null +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -0,0 +1,53 @@ +using MagicQuant.Helpers; +using MQ.DB.Models; +using Xunit; + +namespace MagicQuant.Tests; + +public class BaselineCandidatePolicyTests +{ + [Fact] + public void NoImatrix_GroupCandidates_ExcludeRequiresImatrix() + { + var candidates = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix: false, allowHighPrecisionHybrids: true); + Assert.DoesNotContain(candidates, x => x.RequiresImatrix); + } + + [Fact] + public void ImatrixEnabled_GroupCandidates_IncludeI3XXS() + { + var candidates = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix: true, allowHighPrecisionHybrids: true); + Assert.Contains(candidates, x => x.UniqueId == BaselineQuants.IQ3_XXS.UniqueId); + } + + [Fact] + public void ImatrixEnabled_PureBaselines_IncludeIq2Xxs() + { + var baselines = BaselineQuants.GetPureBaselineCandidates(hasUsableImatrix: true); + Assert.Contains(baselines, x => x.UniqueId == BaselineQuants.IQ2_XXS.UniqueId); + } + + [Fact] + public void ExplicitCandidateExhaustion_UsesQ8FallbackPolicy() + { + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(true); + + RuntimeSearchSpace.BanAllExplicitCombinationCandidatesForGroup(TReg.AttnQ); + + Assert.True(RuntimeSearchSpace.IsGroupExplicitCandidateBanned(TReg.AttnQ)); + Assert.Equal(BaselineQuants.Q8_0.UniqueId, BaselineQuants.GetDefaultExplicitFallbackBaseline().UniqueId); + } + + [Fact] + public void HighPrecisionCandidatesRemainInReasoningUniverse_UntilLatePruneStage() + { + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(true); + + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); + var attnQIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.AttnQ.UniqueId); + + Assert.Contains(BaselineQuants.BF16_Hybrid.UniqueId, allowed[attnQIndex]); + } +} diff --git a/MagicQuant.Tests/ImatrixIdentityServiceTests.cs b/MagicQuant.Tests/ImatrixIdentityServiceTests.cs new file mode 100644 index 0000000..5e01766 --- /dev/null +++ b/MagicQuant.Tests/ImatrixIdentityServiceTests.cs @@ -0,0 +1,28 @@ +using MagicQuant.Services; +using MQ.DB; +using Xunit; + +namespace MagicQuant.Tests; + +public class ImatrixIdentityServiceTests +{ + [Fact] + public async Task EnsureActiveImatrixIdentityHash_IsStableForSameArtifact() + { + string temp = Path.GetTempFileName(); + await File.WriteAllTextAsync(temp, "imatrix-test-content"); + + Cache.IsImatrixAvailable = true; + Cache.ActiveImatrixPath = temp; + Cache.ActiveImatrixIdentityHash = null; + + var first = await ImatrixIdentityService.EnsureActiveImatrixIdentityHashAsync(); + Cache.ActiveImatrixIdentityHash = null; + var second = await ImatrixIdentityService.EnsureActiveImatrixIdentityHashAsync(); + + Assert.False(string.IsNullOrWhiteSpace(first)); + Assert.Equal(first, second); + + File.Delete(temp); + } +} diff --git a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs index f26a165..aaf9420 100644 --- a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs +++ b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs @@ -39,10 +39,10 @@ public void Embeddings_LearnedBaselinePruning_OnlyAllowsQ6KAndBansOtherBaselines Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.IQ4_NL)); Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.IQ4_XS)); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=Q6_K") && x.Contains("decision=ALLOW")); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=Q5_K") && x.Contains("decision=BAN")); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=Q4_K") && x.Contains("decision=BAN")); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=IQ4_NL") && x.Contains("decision=BAN")); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("scheme=IQ4_XS") && x.Contains("decision=BAN")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q6_K") && x.Contains("decision=ALLOW")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q5_K") && x.Contains("decision=BAN")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q4_K") && x.Contains("decision=BAN")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=IQ4_NL") && x.Contains("decision=BAN")); + Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=IQ4_XS") && x.Contains("decision=BAN")); } } diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 666b2fe..3f5adaf 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -282,7 +282,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Groups reduced to BF16-only:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); - AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupSchemesBanned:N0}"); + AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupCandidatesBanned:N0}"); AnsiConsole.MarkupLine($"[green]Bad trade eliminations:[/] {isolationResult.BadTradeEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 6e35297..ee3bce1 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -10,7 +10,7 @@ public static class ComboLogic private static readonly ImmutableArray GroupsOrdered = TReg.All.OrderBy(g => g.UniqueId).ToImmutableArray(); - public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants baseQuant) + public static ImmutableArray GetAllowedCandidateIdsPerGroup(BaselineQuants baseQuant) { bool imatrixAvailable = RuntimeSearchSpace.HasUsableImatrix(); var candidatesForRun = BaselineQuants.GetGroupCombinationCandidates(imatrixAvailable, allowHighPrecisionHybrids: true) @@ -55,7 +55,7 @@ public static ImmutableArray GetAllowedSchemeIdsPerGroup(BaselineQuants public static BigInteger CountCombinations(in BaselineQuants baseQuant) { - var allowed = GetAllowedSchemeIdsPerGroup(baseQuant); + var allowed = GetAllowedCandidateIdsPerGroup(baseQuant); BigInteger total = BigInteger.One; for (int i = 0; i < allowed.Length; i++) @@ -69,7 +69,7 @@ public static class ComboCounter { public static BigInteger CountForBase(BaselineQuants baseQuant) { - var allowed = ComboLogic.GetAllowedSchemeIdsPerGroup(baseQuant); + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseQuant); BigInteger total = BigInteger.One; for (int i = 0; i < allowed.Length; i++) diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index cd06fc6..4a79654 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -73,7 +73,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc Justification = Justify.Left }); - var allowed = ComboLogic.GetAllowedSchemeIdsPerGroup(baseline); + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseline); BigInteger baseCount = BigInteger.One; for (int i = 0; i < TReg.All.Length; i++) diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 23a2ed8..29a40f9 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -188,7 +188,7 @@ public static IEnumerable> GenerateTensorConfigBatches( if (TReg.All.IsDefault) throw new InvalidOperationException("TensorRegistry.All is default (uninitialized)."); - var allowed = ComboLogic.GetAllowedSchemeIdsPerGroup(baseQuant); + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseQuant); if (allowed.IsDefault) throw new InvalidOperationException("Allowed scheme array is default (uninitialized)."); diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 85fc252..525dc71 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -377,11 +377,13 @@ private async Task BuildExecutionPlanAsync( { await using var db = new MagicQuantContext(); var aiModelHashId = await GetOrCreateAiModelHashIdAsync(db, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHashId, createIfMissing: false, ct); var row = await db.ExecutionPlanProbeCaches .AsNoTracking() .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHashId && + x.ImatrixDefinitionId == imatrixDefinitionId && x.HardwareFingerprint == key.HardwareFingerprint && x.QuantizedModelFingerprint == key.QuantizedModelFingerprint && x.QuantizationKey == key.QuantizationKey && @@ -423,10 +425,12 @@ private async Task UpsertCachedExecutionPlanAsync( { await using var db = new MagicQuantContext(); var aiModelHashId = await GetOrCreateAiModelHashIdAsync(db, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHashId, createIfMissing: true, ct); var existing = await db.ExecutionPlanProbeCaches .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHashId && + x.ImatrixDefinitionId == imatrixDefinitionId && x.HardwareFingerprint == key.HardwareFingerprint && x.QuantizedModelFingerprint == key.QuantizedModelFingerprint && x.QuantizationKey == key.QuantizationKey && @@ -440,6 +444,7 @@ private async Task UpsertCachedExecutionPlanAsync( existing = new ExecutionPlanProbeCache { AiModelHashId = aiModelHashId, + ImatrixDefinitionId = imatrixDefinitionId, HardwareFingerprint = key.HardwareFingerprint, QuantizedModelFingerprint = key.QuantizedModelFingerprint, QuantizationKey = key.QuantizationKey, @@ -489,7 +494,8 @@ private static string BuildQuantizedModelFingerprint(string quantizationKey) if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) throw new InvalidOperationException("Cache.CurrentModelId is not set."); - return $"model:{Cache.CurrentModelId}|quant:{quantizationKey}"; + string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixIdentityHash ?? "imatrix-unknown") : "no-imatrix"; + return $"model:{Cache.CurrentModelId}|imatrix:{imatrix}|quant:{quantizationKey}"; } private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct) @@ -772,6 +778,7 @@ await SaveBenchmarkToDbAsync( db: db, model: identity.AiModelHash, combo: identity.TensorCombo, + imatrixDefinitionId: identity.ImatrixDefinitionId, res: reused, modelPath: modelPath, executedRunTimings: new List()); @@ -805,7 +812,7 @@ public async Task RunAllBenchmarksAsync( var existingBench = await db.AiBenchmarks .Include(x => x.CategorBenchmarks) .AsNoTracking() - .FirstOrDefaultAsync(b => b.AiModelHashId == aiModelHash.Id && b.TensorComboId == tensorCombo.Id); + .FirstOrDefaultAsync(b => b.AiModelHashId == aiModelHash.Id && b.ImatrixDefinitionId == identity.ImatrixDefinitionId && b.TensorComboId == tensorCombo.Id); // 1. DB truth first if (existingBench != null && HasRequiredCategories(existingBench, requestedDomains, requireKld)) @@ -848,6 +855,7 @@ await SaveBenchmarkToDbAsync( db: db, model: aiModelHash, combo: tensorCombo, + imatrixDefinitionId: identity.ImatrixDefinitionId, res: reused, modelPath: modelPath, executedRunTimings: new List()); @@ -866,13 +874,14 @@ await SaveBenchmarkToDbAsync( var trackedBench = await db.AiBenchmarks .Include(x => x.CategorBenchmarks) - .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHash.Id && x.TensorComboId == tensorCombo.Id); + .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHash.Id && x.ImatrixDefinitionId == identity.ImatrixDefinitionId && x.TensorComboId == tensorCombo.Id); if (trackedBench == null) { trackedBench = new AiBenchmark { AiModelHashId = aiModelHash.Id, + ImatrixDefinitionId = identity.ImatrixDefinitionId, TensorComboId = tensorCombo.Id, Ngl = 0, SizeBytes = 0, @@ -977,6 +986,7 @@ await PersistFailedBenchmarkRunAsync( aiModelHashId: aiModelHash.Id, tensorComboId: tensorCombo.Id, aiBenchmarkId: trackedBench.Id, + imatrixDefinitionId: identity.ImatrixDefinitionId, category: DomainToCategory(domain), startedUtc: startedUtc, completedUtc: DateTime.UtcNow, @@ -992,6 +1002,7 @@ await SaveBenchmarkToDbAsync( db: db, model: aiModelHash, combo: tensorCombo, + imatrixDefinitionId: identity.ImatrixDefinitionId, res: result, modelPath: modelPath, executedRunTimings: executedRunTimings); @@ -1003,7 +1014,7 @@ await SaveBenchmarkToDbAsync( // Database helpers // ---------------------------------------------------------------- - private async Task<(AiModelHash AiModelHash, TensorCombo TensorCombo)> GetOrCreateBenchmarkIdentityAsync( + private async Task<(AiModelHash AiModelHash, TensorCombo TensorCombo, int? ImatrixDefinitionId)> GetOrCreateBenchmarkIdentityAsync( MagicQuantContext db, HybridQuant quantConfig, CancellationToken ct = default) @@ -1024,7 +1035,8 @@ await SaveBenchmarkToDbAsync( var tensorCombo = await GetOrCreateTensorComboAsync(db, quantConfig, ct); - return (aiModelHash, tensorCombo); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHash.Id, createIfMissing: true, ct); + return (aiModelHash, tensorCombo, imatrixDefinitionId); } private async Task GetOrCreateTensorComboAsync( @@ -1059,6 +1071,7 @@ private async Task SaveBenchmarkToDbAsync( MagicQuantContext db, AiModelHash model, TensorCombo combo, + int? imatrixDefinitionId, BenchmarkResult res, string modelPath, IReadOnlyCollection executedRunTimings) @@ -1088,6 +1101,7 @@ private async Task SaveBenchmarkToDbAsync( .Include(x => x.CategorBenchmarks) .FirstOrDefaultAsync(x => x.AiModelHashId == model.Id && + x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id); if (bench == null) @@ -1095,6 +1109,7 @@ private async Task SaveBenchmarkToDbAsync( bench = new AiBenchmark { AiModelHashId = model.Id, + ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = combo.Id }; @@ -1179,6 +1194,7 @@ private async Task SaveBenchmarkToDbAsync( { Id = Guid.NewGuid(), AiModelHashId = model.Id, + ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = combo.Id, AiBenchmarkId = bench.Id, CategoryBenchmarkId = categoryBenchmarkId, @@ -1231,6 +1247,7 @@ private async Task PersistFailedBenchmarkRunAsync( uint aiModelHashId, Guid tensorComboId, Guid aiBenchmarkId, + int? imatrixDefinitionId, byte category, DateTime startedUtc, DateTime completedUtc, @@ -1240,6 +1257,7 @@ private async Task PersistFailedBenchmarkRunAsync( { Id = Guid.NewGuid(), AiModelHashId = aiModelHashId, + ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = tensorComboId, AiBenchmarkId = aiBenchmarkId, CategoryBenchmarkId = null, diff --git a/MagicQuant/Services/ImatrixIdentityService.cs b/MagicQuant/Services/ImatrixIdentityService.cs new file mode 100644 index 0000000..ad1f51f --- /dev/null +++ b/MagicQuant/Services/ImatrixIdentityService.cs @@ -0,0 +1,62 @@ +using System.Security.Cryptography; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models.DbModels; +using Microsoft.EntityFrameworkCore; + +namespace MagicQuant.Services; + +public static class ImatrixIdentityService +{ + public static async Task EnsureActiveImatrixIdentityHashAsync(CancellationToken ct = default) + { + if (!Cache.IsImatrixAvailable || string.IsNullOrWhiteSpace(Cache.ActiveImatrixPath)) + { + Cache.ActiveImatrixIdentityHash = null; + return null; + } + + if (!string.IsNullOrWhiteSpace(Cache.ActiveImatrixIdentityHash)) + return Cache.ActiveImatrixIdentityHash; + + await using var stream = File.OpenRead(Cache.ActiveImatrixPath); + var hash = await SHA256.HashDataAsync(stream, ct); + Cache.ActiveImatrixIdentityHash = Convert.ToHexString(hash).ToLowerInvariant(); + return Cache.ActiveImatrixIdentityHash; + } + + public static async Task ResolveCurrentImatrixDefinitionIdAsync( + MagicQuantContext db, + uint aiModelHashId, + bool createIfMissing, + CancellationToken ct = default) + { + var identityHash = await EnsureActiveImatrixIdentityHashAsync(ct); + if (string.IsNullOrWhiteSpace(identityHash)) + return null; + + var existing = await db.ImatrixDefinitions + .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHashId && x.IdentityHash == identityHash, ct); + + if (existing != null) + return existing.Id; + + if (!createIfMissing) + return null; + + var row = new ImatrixDefinition + { + AiModelHashId = aiModelHashId, + IdentityHash = identityHash, + CanonicalPath = Cache.ActiveImatrixPath, + SourceKind = "runtime-active", + MetadataJson = null, + BuildFingerprint = null, + CreatedUtc = DateTime.UtcNow + }; + + db.ImatrixDefinitions.Add(row); + await db.SaveChangesAsync(ct); + return row.Id; + } +} diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 6d7f7e0..df94e24 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -41,7 +41,7 @@ public sealed class InitialIsolationAnalysisResult public sealed class IsolationOptimizationResult { public int ExplicitQuantBannedGroups { get; set; } - public int DominatedGroupSchemesBanned { get; set; } + public int DominatedGroupCandidatesBanned { get; set; } public int HardDamageEliminations { get; set; } public int BadTradeEliminations { get; set; } public int DisabledBaselines { get; set; } @@ -86,13 +86,13 @@ public async Task AnalyzeInitialIsolationProbesA foreach (var groupSet in groupPlans) { var group = TReg.All.First(x => x.UniqueId == groupSet.Key); + var probe = groupSet.Single(); - var item = groupSet.Single(); - var snap = await LoadSnapshotAsync(item.Quant, ct); + var snap = await LoadSnapshotAsync(probe.Quant, ct); if (snap == null) continue; - var candidate = BaselineQuants.FromId(item.TestedSchemeId!.Value); + var candidate = BaselineQuants.FromId(probe.TestedSchemeId!.Value); var reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.SizeBytes); var kld = GetAggregateKld(snap); var pplDelta = GetAggregatePplDeltaPercent(snap, nativeBaseline); @@ -108,7 +108,7 @@ public async Task AnalyzeInitialIsolationProbesA }; decision.Candidates.Add( - $"{scheme.Names[0]} | size={(snap.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={reduction:P2} | kld={kld:G6} | pplΔ={pplDelta:F4}%"); + $"{candidate.Names[0]} | size={(snap.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={reduction:P2} | kld={kld:G6} | pplΔ={pplDelta:F4}%"); if (reduction < options.MinMeaningfulGroupReductionRatio) { @@ -116,7 +116,7 @@ public async Task AnalyzeInitialIsolationProbesA decision.ExplicitQuantBanned = true; result.Notes.Add( - $"Early stop for '{group.Name}': smallest non-imatrix '{scheme.Names[0]}' only saved {reduction:P2}, below {options.MinMeaningfulGroupReductionRatio:P2}. Explicit tensor quant exploration removed for this group."); + $"Early stop for '{group.Name}': smallest baseline-candidate probe '{candidate.Names[0]}' only saved {reduction:P2}, below {options.MinMeaningfulGroupReductionRatio:P2}. Explicit baseline-candidate exploration removed for this group."); result.GroupDetails.Add(decision); continue; @@ -130,7 +130,7 @@ public async Task AnalyzeInitialIsolationProbesA decision.Bf16Suppressed = true; result.Notes.Add( - $"Suppressed BF16 tensor-choice for '{group.Name}' because smallest probe already saved {reduction:P2}."); + $"Suppressed BF16 explicit candidate for '{group.Name}' because smallest baseline-candidate probe already saved {reduction:P2}."); } result.GroupDetails.Add(decision); @@ -173,8 +173,7 @@ public async Task AnalyzeAndApplyFinalAsync( foreach (var groupSet in groupPlans) { var group = TReg.All.First(x => x.UniqueId == groupSet.Key); - - var candidates = new List(); + var candidates = new List(); foreach (var item in groupSet) { @@ -182,12 +181,12 @@ public async Task AnalyzeAndApplyFinalAsync( if (snap == null) continue; - var candidate = BaselineQuants.FromId(item.TestedSchemeId!.Value); + var candidateBaseline = BaselineQuants.FromId(item.TestedSchemeId!.Value); - candidates.Add(new GroupCandidate + candidates.Add(new GroupCandidateEvaluation { Group = group, - Candidate = candidate, + CandidateBaseline = candidateBaseline, SizeBytes = snap.SizeBytes, SavingsRatio = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.SizeBytes), Kld = GetAggregateKld(snap), @@ -206,7 +205,7 @@ public async Task AnalyzeAndApplyFinalAsync( foreach (var candidate in candidates.ToList()) { - if (candidate.Candidate.UniqueId == BaselineQuants.BF16_Hybrid.UniqueId || candidate.Candidate.UniqueId == BaselineQuants.F16_Hybrid.UniqueId) + if (IsHighPrecisionCandidate(candidate.CandidateBaseline)) continue; bool hardFail = @@ -216,20 +215,17 @@ public async Task AnalyzeAndApplyFinalAsync( if (!hardFail) continue; - RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate.Candidate); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate.CandidateBaseline); result.HardDamageEliminations++; result.Notes.Add( - $"Hard damage elimination: '{candidate.Candidate.Names[0]}' removed for '{group.Name}' " + + $"Hard damage elimination: '{candidate.CandidateBaseline.Names[0]}' removed for '{group.Name}' " + $"(savings={candidate.SavingsRatio:P2}, KLD={candidate.Kld:G6}, PPLΔ={candidate.PplDeltaPercent:F4}%)."); } candidates = FilterSurvivors(group, candidates); - ApplyDominanceElimination(group, candidates, result); - candidates = FilterSurvivors(group, candidates); - ApplyBadTradeElimination(group, candidates, result); candidates = FilterSurvivors(group, candidates) @@ -246,8 +242,7 @@ public async Task AnalyzeAndApplyFinalAsync( } var winner = candidates.First(); - - decision.WinningCandidate = winner.Candidate.Names[0]; + decision.WinningCandidate = winner.CandidateBaseline.Names[0]; decision.WinningSizeBytes = winner.SizeBytes; decision.WinningKld = winner.Kld; decision.WinningPplDelta = winner.PplDeltaPercent; @@ -256,17 +251,14 @@ public async Task AnalyzeAndApplyFinalAsync( foreach (var candidate in candidates.OrderBy(x => x.SizeBytes)) { decision.Candidates.Add( - $"{candidate.Candidate.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}%"); + $"{candidate.CandidateBaseline.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}%"); } foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedSchemesForGroup(group)) { - var sourceBaselines = string.Join( - ", ", - banInfo.MissingBaselines.Select(x => x.Names[0])); - + var sourceBaselines = string.Join(", ", banInfo.MissingBaselines.Select(x => x.Names[0])); decision.Candidates.Add( - $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned-baseline mapping for this group (no matching tensor weights in baseline(s): {sourceBaselines})."); + $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned baseline-family mapping for this group (missing baseline source(s): {sourceBaselines})."); } result.GroupDetails.Add(decision); @@ -284,11 +276,9 @@ public async Task AnalyzeAndApplyFinalAsync( continue; double reduction = ComputeReductionRatio(nativeBaseline.SizeBytes, snap.SizeBytes); - if (reduction < options.MinMeaningfulBaseOnlyReductionRatio) { var baseline = BaselineQuants.FromId(item.TestedBaselineId!.Value); - if (RuntimeSearchSpace.DisableCombinationBaseline(baseline)) { result.DisabledBaselines++; @@ -304,10 +294,10 @@ public async Task AnalyzeAndApplyFinalAsync( return result; } - private static void PopulateFinalGroupFlags( - TensorGroup group, - IsolationGroupDecision decision, - IsolationOptimizationResult result) + private static bool IsHighPrecisionCandidate(BaselineQuants candidate) + => candidate.UniqueId == BaselineQuants.BF16_Hybrid.UniqueId || candidate.UniqueId == BaselineQuants.F16_Hybrid.UniqueId; + + private static void PopulateFinalGroupFlags(TensorGroup group, IsolationGroupDecision decision, IsolationOptimizationResult result) { var (explicitAllowed, bf16Allowed) = RuntimeSearchSpace.GetFinalAllowedQuantFamiliesForGroup(group); @@ -317,23 +307,19 @@ private static void PopulateFinalGroupFlags( if (!explicitAllowed && !bf16Allowed) { result.Notes.Add( - $"[invariant-warning] Invalid final quant-family state for '{group.Name}': neither explicit nor BF16 is allowed."); + $"[invariant-warning] Invalid final quant-family state for '{group.Name}': neither explicit candidate nor BF16 is allowed."); } } - private static List FilterSurvivors(TensorGroup group, List candidates) + private static List FilterSurvivors(TensorGroup group, List candidates) { return candidates - .Where(x => x.Candidate.UniqueId == BaselineQuants.BF16_Hybrid.UniqueId || - x.Candidate.UniqueId == BaselineQuants.F16_Hybrid.UniqueId || - !RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x.Candidate)) + .Where(x => IsHighPrecisionCandidate(x.CandidateBaseline) || + !RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x.CandidateBaseline)) .ToList(); } - private static void ApplyDominanceElimination( - TensorGroup group, - List candidates, - IsolationOptimizationResult result) + private static void ApplyDominanceElimination(TensorGroup group, List candidates, IsolationOptimizationResult result) { var explicitCandidates = GetActiveExplicitCandidates(group, candidates); @@ -358,23 +344,20 @@ private static void ApplyDominanceElimination( if (sameOrSmaller && kldNoWorse && pplNoWorse && strictlyBetter) { - if (!RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, b.Candidate)) + if (!RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, b.CandidateBaseline)) { - RuntimeSearchSpace.BanCombinationCandidateForGroup(group, b.Candidate); - result.DominatedGroupSchemesBanned++; + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, b.CandidateBaseline); + result.DominatedGroupCandidatesBanned++; result.Notes.Add( - $"Dominance elimination: '{b.Candidate.Names[0]}' removed for '{group.Name}' because '{a.Candidate.Names[0]}' was same-size-or-smaller and no worse on KLD/PPL."); + $"Dominance elimination: '{b.CandidateBaseline.Names[0]}' removed for '{group.Name}' because '{a.CandidateBaseline.Names[0]}' was same-size-or-smaller and no worse on KLD/PPL."); } } } } } - private static void ApplyBadTradeElimination( - TensorGroup group, - List candidates, - IsolationOptimizationResult result) + private static void ApplyBadTradeElimination(TensorGroup group, List candidates, IsolationOptimizationResult result) { var activeCandidates = GetActiveExplicitCandidates(group, candidates); if (activeCandidates.Count <= 1) @@ -390,19 +373,19 @@ private static void ApplyBadTradeElimination( for (int i = 1; i < sizeBuckets.Count; i++) { - var bucketSurvivors = new List(); + var bucketSurvivors = new List(); foreach (var candidate in sizeBuckets[i]) { - if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate.Candidate)) + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate.CandidateBaseline)) continue; if (ShouldEliminateAsBadTrade(acceptedAnchor, candidate, out var reason)) { - RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate.Candidate); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate.CandidateBaseline); result.BadTradeEliminations++; result.Notes.Add( - $"Bad trade elimination: '{candidate.Candidate.Names[0]}' removed vs accepted anchor '{acceptedAnchor.Candidate.Names[0]}' for '{group.Name}'. {reason}"); + $"Bad trade elimination: '{candidate.CandidateBaseline.Names[0]}' removed vs accepted anchor '{acceptedAnchor.CandidateBaseline.Names[0]}' for '{group.Name}'. {reason}"); continue; } @@ -415,15 +398,15 @@ private static void ApplyBadTradeElimination( } } - private static List GetActiveExplicitCandidates(TensorGroup group, List candidates) + private static List GetActiveExplicitCandidates(TensorGroup group, List candidates) { return candidates - .Where(x => x.Candidate.UniqueId != BaselineQuants.BF16_Hybrid.UniqueId && x.Candidate.UniqueId != BaselineQuants.F16_Hybrid.UniqueId) - .Where(x => !RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x.Candidate)) + .Where(x => !IsHighPrecisionCandidate(x.CandidateBaseline)) + .Where(x => !RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x.CandidateBaseline)) .ToList(); } - private static List> BuildSizeBuckets(List candidates) + private static List> BuildSizeBuckets(List candidates) { return candidates .GroupBy(x => x.SizeBytes) @@ -431,44 +414,31 @@ private static List> BuildSizeBuckets(List .Select(x => x .OrderBy(c => c.Kld) .ThenBy(c => Math.Abs(c.PplDeltaPercent)) - .ThenByDescending(c => GetCandidateSafetyScore(c.Candidate)) - .ThenBy(c => c.Candidate.Names[0], StringComparer.Ordinal) + .ThenByDescending(c => GetCandidateSafetyScore(c.CandidateBaseline)) + .ThenBy(c => c.CandidateBaseline.Names[0], StringComparer.Ordinal) .ToList()) .ToList(); } - private static bool ShouldEliminateAsBadTrade( - GroupCandidate anchor, - GroupCandidate candidate, - out string reason) + private static bool ShouldEliminateAsBadTrade(GroupCandidateEvaluation anchor, GroupCandidateEvaluation candidate, out string reason) { reason = string.Empty; if (anchor.SizeBytes <= candidate.SizeBytes) return false; - double sizeDeltaPercent = - ((double)anchor.SizeBytes - candidate.SizeBytes) / anchor.SizeBytes * 100.0; - + double sizeDeltaPercent = ((double)anchor.SizeBytes - candidate.SizeBytes) / anchor.SizeBytes * 100.0; if (sizeDeltaPercent > IsolationPruningConfig.BadTradeMaxSizeDeltaPercent) return false; double anchorPplAbs = Math.Abs(anchor.PplDeltaPercent); double candidatePplAbs = Math.Abs(candidate.PplDeltaPercent); - double kldRatio = anchor.Kld <= IsolationPruningConfig.FloatingPointEpsilon - ? double.PositiveInfinity - : candidate.Kld / anchor.Kld; - - double pplRatio = anchorPplAbs <= IsolationPruningConfig.FloatingPointEpsilon - ? double.PositiveInfinity - : candidatePplAbs / anchorPplAbs; + double kldRatio = anchor.Kld <= IsolationPruningConfig.FloatingPointEpsilon ? double.PositiveInfinity : candidate.Kld / anchor.Kld; + double pplRatio = anchorPplAbs <= IsolationPruningConfig.FloatingPointEpsilon ? double.PositiveInfinity : candidatePplAbs / anchorPplAbs; - bool kldBadTrade = - candidate.Kld > anchor.Kld * IsolationPruningConfig.BadTradeKldMultiplier; - - bool pplBadTrade = - candidatePplAbs > anchorPplAbs * IsolationPruningConfig.BadTradePplMultiplier; + bool kldBadTrade = candidate.Kld > anchor.Kld * IsolationPruningConfig.BadTradeKldMultiplier; + bool pplBadTrade = candidatePplAbs > anchorPplAbs * IsolationPruningConfig.BadTradePplMultiplier; bool candidateMeaningfullyBetterKld = candidate.Kld + IsolationPruningConfig.FloatingPointEpsilon < anchor.Kld * 0.90; @@ -480,10 +450,7 @@ private static bool ShouldEliminateAsBadTrade( (kldBadTrade && candidateMeaningfullyBetterPpl) || (pplBadTrade && candidateMeaningfullyBetterKld); - if (mixedTradeoff) - return false; - - if (!kldBadTrade && !pplBadTrade) + if (mixedTradeoff || (!kldBadTrade && !pplBadTrade)) return false; reason = @@ -492,13 +459,13 @@ private static bool ShouldEliminateAsBadTrade( return true; } - private static GroupCandidate? SelectBestBucketSurvivor(List survivors) + private static GroupCandidateEvaluation? SelectBestBucketSurvivor(List survivors) { return survivors .OrderBy(x => x.Kld) .ThenBy(x => Math.Abs(x.PplDeltaPercent)) - .ThenByDescending(x => GetCandidateSafetyScore(x.Candidate)) - .ThenBy(x => x.Candidate.Names[0], StringComparer.Ordinal) + .ThenByDescending(x => GetCandidateSafetyScore(x.CandidateBaseline)) + .ThenBy(x => x.CandidateBaseline.Names[0], StringComparer.Ordinal) .FirstOrDefault(); } @@ -509,9 +476,7 @@ private static int GetCandidateSafetyScore(BaselineQuants candidate) for (int i = 0; i < canonical.Length - 1; i++) { if ((canonical[i] == 'q' || canonical[i] == 'Q') && char.IsDigit(canonical[i + 1])) - { return canonical[i + 1] - '0'; - } } return 0; @@ -521,12 +486,11 @@ private static int GetCandidateSafetyScore(BaselineQuants candidate) { await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes - .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - + var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); if (model == null) return null; + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); var lookup = (TensorConfig)quant; var row = await db.AiBenchmarks @@ -537,6 +501,7 @@ private static int GetCandidateSafetyScore(BaselineQuants candidate) (b, c) => new { b, c }) .FirstOrDefaultAsync(x => x.b.AiModelHashId == model.Id && + x.b.ImatrixDefinitionId == imatrixDefinitionId && x.c.BaseQuant == lookup.BaseQuant && x.c.Embeddings == lookup.Embeddings && x.c.LmHead == lookup.LmHead && @@ -605,10 +570,10 @@ private static double GetAggregatePplDeltaPercent(BenchmarkSnapshot snapshot, Be return deltas.Count == 0 ? double.PositiveInfinity : deltas.Average(); } - private sealed class GroupCandidate + private sealed class GroupCandidateEvaluation { public TensorGroup Group { get; set; } = default!; - public BaselineQuants Candidate { get; set; } = default!; + public BaselineQuants CandidateBaseline { get; set; } = default!; public ulong SizeBytes { get; set; } public double SavingsRatio { get; set; } public double Kld { get; set; } diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 4fc6b8e..c3b618b 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -37,16 +37,16 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) { var groupDefinitions = TReg.All.ToDictionary(g => g.Name, g => g.Tensors); - var blockRequirements = TensorWeightScheme.All_Allowed_Hybrid_Quants - .Where(s => s.BlockNeo.HasValue) - .ToDictionary(s => s.Names[0], s => s.BlockNeo!.Value); + var candidateBlockRequirements = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: true) + .Where(c => c.DefaultTensorScheme?.BlockNeo.HasValue == true) + .ToDictionary(c => c.Names[0], c => c.DefaultTensorScheme!.BlockNeo!.Value); var payload = new { gguf_path = ggufPath, output_path = resultPath, groups = groupDefinitions, - schemes = blockRequirements + schemes = candidateBlockRequirements }; string pyCode = GeneratePythonScript(JsonSerializer.Serialize(payload)); @@ -103,19 +103,17 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) foreach (var failure in result.Incompatible) { var group = TReg.GetByName(failure.Group); - var scheme = TensorWeightScheme.All_Allowed_Hybrid_Quants.FirstOrDefault(s => - s.Names.Any(n => n.Equals(failure.Scheme, StringComparison.OrdinalIgnoreCase))); + var candidate = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: true) + .FirstOrDefault(c => c.Names.Any(n => n.Equals(failure.Scheme, StringComparison.OrdinalIgnoreCase))); - if (group == null || scheme == null) + if (group == null || candidate == null) continue; - - var candidate = BaselineQuants.FromTensorSchemeId(scheme.UniqueId); if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate); shapeBanCount++; - shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{scheme.Names[0]}[/]", + shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{candidate.Names[0]}[/]", "[grey]Block Alignment[/]"); } diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 05a3aec..c6a99f1 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -94,7 +94,7 @@ private static string GetDuckDbDirectory() private static string BuildContextAwareDuckDbFileName() { string model = string.IsNullOrWhiteSpace(Cache.CurrentModelId) ? "unknown-model" : Cache.CurrentModelId; - string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixPath?.GetHashCode().ToString("X") ?? "imatrix") : "no-imatrix"; + string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixIdentityHash ?? "imatrix-unknown") : "no-imatrix"; string hp = RuntimeSearchSpace.AllowHighPrecisionHybrids ? "hp-on" : "hp-off"; return $"{DbFileNamePrefix}_{model}_{imatrix}_{hp}.duckdb"; } @@ -354,9 +354,12 @@ INSERT INTO {TableName} if (model == null) return null; + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var pureQ8 = await LoadSnapshotByQuantAsync( db, model.Id, + imatrixDefinitionId, HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), ct); @@ -368,7 +371,7 @@ INSERT INTO {TableName} if (pureQ8 == null || carrierBaseOnlyPlan == null) return null; - var carrier = await LoadSnapshotByQuantAsync(db, model.Id, carrierBaseOnlyPlan.Quant, ct); + var carrier = await LoadSnapshotByQuantAsync(db, model.Id, imatrixDefinitionId, carrierBaseOnlyPlan.Quant, ct); if (carrier == null) return null; @@ -384,7 +387,7 @@ INSERT INTO {TableName} if (!plan.TargetGroupId.HasValue || !plan.TestedSchemeId.HasValue) continue; - var snap = await LoadSnapshotByQuantAsync(db, model.Id, plan.Quant, ct); + var snap = await LoadSnapshotByQuantAsync(db, model.Id, imatrixDefinitionId, plan.Quant, ct); if (snap == null) continue; @@ -401,6 +404,7 @@ INSERT INTO {TableName} private static async Task LoadSnapshotByQuantAsync( MagicQuantContext db, uint modelId, + int? imatrixDefinitionId, HybridQuant quant, CancellationToken ct) { @@ -413,6 +417,7 @@ INSERT INTO {TableName} (b, c) => new { b, c }) .FirstOrDefaultAsync(x => x.b.AiModelHashId == modelId && + x.b.ImatrixDefinitionId == imatrixDefinitionId && x.c.BaseQuant == lookup.BaseQuant && x.c.Embeddings == lookup.Embeddings && x.c.LmHead == lookup.LmHead && diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 5f4c879..9317e50 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -276,6 +276,8 @@ await Parallel.ForEachAsync( if (model == null) return (null, null); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var comboId = await db.TensorCombos .AsNoTracking() .Where(x => @@ -297,7 +299,7 @@ await Parallel.ForEachAsync( var benchmarkId = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id && x.TensorComboId == comboId) + .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == comboId) .Select(x => x.Id) .FirstOrDefaultAsync(ct); @@ -383,6 +385,7 @@ await _benchmarker.RunAllBenchmarksAsync( await PersistQuantizationRunAsync( quant: quant, + imatrixDefinitionId: null, startedUtc: startedUtc, completedUtc: DateTime.UtcNow, succeeded: true, @@ -405,6 +408,7 @@ await PersistQuantizationRunAsync( { await PersistQuantizationRunAsync( quant: quant, + imatrixDefinitionId: null, startedUtc: startedUtc, completedUtc: DateTime.UtcNow, succeeded: false, @@ -454,9 +458,11 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok if (model == null) return false; + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var bench = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id) + .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId) .Join( db.TensorCombos.AsNoTracking(), benchmark => benchmark.TensorComboId, @@ -493,6 +499,7 @@ private static TensorConfig BuildTensorLookup(HybridQuant quant) private async Task PersistQuantizationRunAsync( HybridQuant quant, + int? imatrixDefinitionId, DateTime startedUtc, DateTime completedUtc, bool succeeded, @@ -540,8 +547,10 @@ private async Task PersistQuantizationRunAsync( await db.SaveChangesAsync(ct); } + imatrixDefinitionId ??= await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHash.Id, createIfMissing: true, ct); + Guid? aiBenchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == aiModelHash.Id && x.TensorComboId == tensorCombo.Id) + .Where(x => x.AiModelHashId == aiModelHash.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == tensorCombo.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); @@ -549,6 +558,7 @@ private async Task PersistQuantizationRunAsync( { Id = Guid.NewGuid(), AiModelHashId = aiModelHash.Id, + ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = tensorCombo.Id, AiBenchmarkId = aiBenchmarkId, StartedUtc = startedUtc, @@ -1005,8 +1015,10 @@ public async Task LearnNativeSourceTruthAsync( if (combo == null) throw new InvalidOperationException("Native-source benchmark TensorCombo is missing; benchmark base model first."); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == model.Id && x.TensorComboId == combo.Id) + .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); @@ -1127,8 +1139,10 @@ private async Task LearnAndPersistBaselineTensorMapAsync( x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == model.Id && x.TensorComboId == combo.Id) + .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); From 1e75e1d4c6e203568d7ad812d4004282763147fb Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 14:46:00 -0400 Subject: [PATCH 091/258] Clean up candidate naming and expand regression coverage --- .../BaselineCandidatePolicyTests.cs | 13 ++++ .../ImatrixIdentityServiceTests.cs | 60 +++++++++++++++++++ .../LearnedBaselinePruningServiceTests.cs | 10 ++-- MagicQuant/Helpers/RuntimeSearchSpace.cs | 15 ++++- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 2 +- .../Services/IsolationOptimizationService.cs | 2 +- .../Services/ModelCompatibilityService.cs | 2 +- MagicQuant/Services/QuantDatabaseService.cs | 16 ++--- 8 files changed, 102 insertions(+), 18 deletions(-) diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs index e872f2e..8662237 100644 --- a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -50,4 +50,17 @@ public void HighPrecisionCandidatesRemainInReasoningUniverse_UntilLatePruneStage Assert.Contains(BaselineQuants.BF16_Hybrid.UniqueId, allowed[attnQIndex]); } + + [Fact] + public void CandidateBanAuthority_DrivesAllowedCandidateSet() + { + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(true); + + RuntimeSearchSpace.BanCombinationCandidateForGroup(TReg.AttnQ, BaselineQuants.Q6_K); + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); + var attnQIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.AttnQ.UniqueId); + + Assert.DoesNotContain(BaselineQuants.Q6_K.UniqueId, allowed[attnQIndex]); + } } diff --git a/MagicQuant.Tests/ImatrixIdentityServiceTests.cs b/MagicQuant.Tests/ImatrixIdentityServiceTests.cs index 5e01766..c9f8a97 100644 --- a/MagicQuant.Tests/ImatrixIdentityServiceTests.cs +++ b/MagicQuant.Tests/ImatrixIdentityServiceTests.cs @@ -1,5 +1,8 @@ using MagicQuant.Services; using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models.DbModels; +using Microsoft.EntityFrameworkCore; using Xunit; namespace MagicQuant.Tests; @@ -25,4 +28,61 @@ public async Task EnsureActiveImatrixIdentityHash_IsStableForSameArtifact() File.Delete(temp); } + + [Fact] + public async Task ResolveCurrentImatrixDefinitionId_SameModelSameImatrix_ReusesSameRow() + { + string tempRoot = Path.Combine(Path.GetTempPath(), "mq-imatrix-test-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(tempRoot); + + Cache.MagicQuantDirectory = tempRoot; + Cache.CurrentModelId = "model-test-same"; + + string tempImatrix = Path.Combine(tempRoot, "imatrix.dat"); + await File.WriteAllTextAsync(tempImatrix, "same-imatrix"); + + Cache.IsImatrixAvailable = true; + Cache.ActiveImatrixPath = tempImatrix; + Cache.ActiveImatrixIdentityHash = null; + + await using var db = new MagicQuantContext(); + var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId); + if (model == null) + { + model = new AiModelHash { UniqueHash = Cache.CurrentModelId }; + db.AiModelHashes.Add(model); + await db.SaveChangesAsync(); + } + + var first = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: true); + var second = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: true); + + Assert.NotNull(first); + Assert.Equal(first, second); + } + + [Fact] + public async Task ResolveCurrentImatrixDefinitionId_NoImatrix_ReturnsNull() + { + string tempRoot = Path.Combine(Path.GetTempPath(), "mq-imatrix-test-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(tempRoot); + + Cache.MagicQuantDirectory = tempRoot; + Cache.CurrentModelId = "model-test-none"; + Cache.IsImatrixAvailable = false; + Cache.ActiveImatrixPath = null; + Cache.ActiveImatrixIdentityHash = null; + + await using var db = new MagicQuantContext(); + var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId); + if (model == null) + { + model = new AiModelHash { UniqueHash = Cache.CurrentModelId }; + db.AiModelHashes.Add(model); + await db.SaveChangesAsync(); + } + + var id = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false); + Assert.Null(id); + } } diff --git a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs index aaf9420..9afe17f 100644 --- a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs +++ b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs @@ -33,11 +33,11 @@ public void Embeddings_LearnedBaselinePruning_OnlyAllowsQ6KAndBansOtherBaselines unusedGroupIds: unused, result: result); - Assert.False(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.Q6_K)); - Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.Q5_K)); - Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.Q4_K)); - Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.IQ4_NL)); - Assert.True(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup(TReg.Embeddings, TensorWeightScheme.IQ4_XS)); + Assert.False(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.Q6_K)); + Assert.True(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.Q5_K)); + Assert.True(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.Q4_K_M)); + Assert.True(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.IQ4_NL)); + Assert.True(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.IQ4_XS)); Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q6_K") && x.Contains("decision=ALLOW")); Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q5_K") && x.Contains("decision=BAN")); diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 69d9d2c..97bdf6d 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -16,6 +16,11 @@ public static class RuntimeSearchSpace private static readonly HashSet Bf16SuppressedTensorChoiceGroupIds = new(); private static bool _imatrixAvailable; + [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] + [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] + [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] + [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] + [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] public static bool AllowHighPrecisionHybrids { get; set; } public static void ResetForNewModel() @@ -106,7 +111,7 @@ public static bool HasLearnedBaselineMissingPrunesForGroup(TensorGroup group) public static IReadOnlyList GetGroupsWithLearnedBaselineMissingPrunes() => TReg.All.Where(HasLearnedBaselineMissingPrunesForGroup).OrderBy(x => x.UniqueId).ToList(); - public static IReadOnlyList GetLearnedBaselineMissingPrunedSchemesForGroup(TensorGroup group) + public static IReadOnlyList GetLearnedBaselineMissingPrunedCandidatesForGroup(TensorGroup group) { if (!LearnedBaselineMissingByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) return Array.Empty(); @@ -161,19 +166,25 @@ public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allo public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) => DisabledCombinationBaselineIds.Contains(baseline.UniqueId); - // Legacy compatibility wrappers (scheme-driven callers) + // Legacy compatibility wrappers (scheme-driven callers). + // Prefer candidate-based APIs in new code. + [Obsolete("Use BanCombinationCandidateForGroup.")] public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) => BanCombinationCandidateForGroup(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId)); + [Obsolete("Use BanCombinationCandidateForGroupByLearnedBaselineAbsence.")] public static void BanSchemeForGroupByLearnedBaselineAbsence(TensorGroup group, TensorWeightScheme scheme, BaselineQuants sourceBaseline) => BanCombinationCandidateForGroupByLearnedBaselineAbsence(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId), sourceBaseline); + [Obsolete("Use BanAllExplicitCombinationCandidatesForGroup.")] public static void BanAllExplicitTensorSchemesForGroup(TensorGroup group) => BanAllExplicitCombinationCandidatesForGroup(group); + [Obsolete("Use IsCombinationCandidateRuntimeBannedForGroup.")] public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeightScheme scheme) => IsCombinationCandidateRuntimeBannedForGroup(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId)); + [Obsolete("Use IsGroupExplicitCandidateBanned.")] public static bool IsGroupExplicitQuantBanned(TensorGroup group) => IsGroupExplicitCandidateBanned(group); } diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index 4a79654..c4d9014 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -54,7 +54,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc foreach (var group in learnedPrunedGroups) { - var learned = RuntimeSearchSpace.GetLearnedBaselineMissingPrunedSchemesForGroup(group); + var learned = RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group); var parts = learned.Select(x => $"{x.Candidate.Names[0]} <= {string.Join("/", x.MissingBaselines.Select(b => b.Names[0]))}"); diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index df94e24..0c2b7b7 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -254,7 +254,7 @@ public async Task AnalyzeAndApplyFinalAsync( $"{candidate.CandidateBaseline.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}%"); } - foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedSchemesForGroup(group)) + foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group)) { var sourceBaselines = string.Join(", ", banInfo.MissingBaselines.Select(x => x.Names[0])); decision.Candidates.Add( diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index c3b618b..18aee0d 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -78,7 +78,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) var shapeTable = new Table().Border(TableBorder.Rounded).Title("[red]Shape Incompatibilities[/]"); shapeTable.AddColumn("Group"); - shapeTable.AddColumn("Scheme"); + shapeTable.AddColumn("Candidate"); shapeTable.AddColumn("Reason"); foreach (var group in TReg.All) diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index c6a99f1..fc61b7a 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -375,7 +375,7 @@ INSERT INTO {TableName} if (carrier == null) return null; - var deltaByGroupAndScheme = new Dictionary<(byte GroupId, byte SchemeId), long>(); + var deltaByGroupAndCandidate = new Dictionary<(byte GroupId, byte CandidateId), long>(); var groupPlans = fullPlan.Plans .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe || x.Kind == RequiredSampleKind.GroupIsolationContinuation) @@ -392,13 +392,13 @@ INSERT INTO {TableName} continue; long delta = (long)snap.SizeBytes - (long)carrier.SizeBytes; - deltaByGroupAndScheme[(plan.TargetGroupId.Value, plan.TestedSchemeId.Value)] = delta; + deltaByGroupAndCandidate[(plan.TargetGroupId.Value, plan.TestedSchemeId.Value)] = delta; } return new PredictionContext( pureQ8BaseSize: pureQ8.SizeBytes, carrierBaseOnlySize: carrier.SizeBytes, - deltas: deltaByGroupAndScheme); + deltas: deltaByGroupAndCandidate); } private static async Task LoadSnapshotByQuantAsync( @@ -443,7 +443,7 @@ private sealed class BenchmarkRow private sealed class PredictionContext { - private readonly Dictionary<(byte GroupId, byte SchemeId), long> _deltas; + private readonly Dictionary<(byte GroupId, byte CandidateId), long> _deltas; public ulong PureQ8BaseSize { get; } public ulong CarrierBaseOnlySize { get; } @@ -451,7 +451,7 @@ private sealed class PredictionContext public PredictionContext( ulong pureQ8BaseSize, ulong carrierBaseOnlySize, - Dictionary<(byte GroupId, byte SchemeId), long> deltas) + Dictionary<(byte GroupId, byte CandidateId), long> deltas) { PureQ8BaseSize = pureQ8BaseSize; CarrierBaseOnlySize = carrierBaseOnlySize; @@ -478,12 +478,12 @@ public ulong Predict(TensorConfig config) return (ulong)total; } - private void AddDelta(byte groupId, byte schemeId, ref long total) + private void AddDelta(byte groupId, byte candidateId, ref long total) { - if (schemeId == BaselineQuants.BF16_Hybrid.UniqueId || schemeId == BaselineQuants.F16_Hybrid.UniqueId) + if (candidateId == BaselineQuants.BF16_Hybrid.UniqueId || candidateId == BaselineQuants.F16_Hybrid.UniqueId) return; - if (_deltas.TryGetValue((groupId, schemeId), out long delta)) + if (_deltas.TryGetValue((groupId, candidateId), out long delta)) total += delta; } } From 71e741cb319d7ff65beafe4f058e525878a0ca1f Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 14:53:30 -0400 Subject: [PATCH 092/258] Fix RuntimeSearchSpace obsolete misuse and finish candidate naming cleanup --- ...RuntimeSearchSpaceObsoleteContractTests.cs | 39 ++++++++ MagicQuant/Commands/BuildHybrids.cs | 96 ++++++++++++++++++- MagicQuant/Helpers/RuntimeSearchSpace.cs | 5 - MagicQuant/Helpers/TensorConfigGenerator.cs | 6 +- .../Services/IsolationOptimizationService.cs | 4 +- MagicQuant/Services/QuantDatabaseService.cs | 4 +- 6 files changed, 140 insertions(+), 14 deletions(-) create mode 100644 MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs diff --git a/MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs b/MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs new file mode 100644 index 0000000..fecd02f --- /dev/null +++ b/MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs @@ -0,0 +1,39 @@ +using System.Reflection; +using MagicQuant.Helpers; +using Xunit; + +namespace MagicQuant.Tests; + +public class RuntimeSearchSpaceObsoleteContractTests +{ + [Fact] + public void AllowHighPrecisionHybrids_IsNotObsolete() + { + var prop = typeof(RuntimeSearchSpace).GetProperty(nameof(RuntimeSearchSpace.AllowHighPrecisionHybrids), BindingFlags.Public | BindingFlags.Static); + Assert.NotNull(prop); + Assert.Empty(prop!.GetCustomAttributes(typeof(ObsoleteAttribute), inherit: false)); + + RuntimeSearchSpace.AllowHighPrecisionHybrids = true; + Assert.True(RuntimeSearchSpace.AllowHighPrecisionHybrids); + } + + [Fact] + public void LegacySchemeWrappers_AreObsolete() + { + var wrappers = new[] + { + nameof(RuntimeSearchSpace.BanSchemeForGroup), + nameof(RuntimeSearchSpace.BanSchemeForGroupByLearnedBaselineAbsence), + nameof(RuntimeSearchSpace.BanAllExplicitTensorSchemesForGroup), + nameof(RuntimeSearchSpace.IsSchemeRuntimeBannedForGroup), + nameof(RuntimeSearchSpace.IsGroupExplicitQuantBanned) + }; + + foreach (var methodName in wrappers) + { + var method = typeof(RuntimeSearchSpace).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static); + Assert.NotNull(method); + Assert.NotEmpty(method!.GetCustomAttributes(typeof(ObsoleteAttribute), inherit: false)); + } + } +} diff --git a/MagicQuant/Commands/BuildHybrids.cs b/MagicQuant/Commands/BuildHybrids.cs index a7d89c8..49c9972 100644 --- a/MagicQuant/Commands/BuildHybrids.cs +++ b/MagicQuant/Commands/BuildHybrids.cs @@ -1,4 +1,10 @@ +using MagicQuant.Helpers; using MagicQuant.Models; +using MagicQuant.Services; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using Spectre.Console; namespace MagicQuant.Commands; @@ -6,6 +12,92 @@ public class BuildHybrids : ICommand { public async Task Run(List args) { - + if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) + { + ShowHelp(); + return; + } + + string? modelDirRaw = args.FirstOrDefault(a => + string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; + + if (string.IsNullOrWhiteSpace(modelDirRaw)) + throw new InvalidOperationException("Missing required argument --model-dir."); + + string fullModelPath = Path.GetFullPath(modelDirRaw); + if (!Directory.Exists(fullModelPath)) + throw new DirectoryNotFoundException($"The directory '{fullModelPath}' does not exist."); + + Cache.ModelDirectory = fullModelPath; + Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + Cache.UseImatrix = args.Any(a => string.Equals(a.Name, "use-imatrix", StringComparison.OrdinalIgnoreCase)); + Cache.ForceImatrixRebuild = args.Any(a => string.Equals(a.Name, "imatrix-force-rebuild", StringComparison.OrdinalIgnoreCase)); + RuntimeSearchSpace.AllowHighPrecisionHybrids = args.Any(a => + string.Equals(a.Name, "allow-high-precision-hybrids", StringComparison.OrdinalIgnoreCase)); + + Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); + JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); + Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); + + await using (var db = new MagicQuantContext()) + { + _ = db.AiModelHashes.Count(); + } + + var pyManager = new PythonManager(Cache.MagicQuantDirectory); + var benchmarkService = new BenchmarkService(pyManager); + var quantizationService = new QuantizationService(benchmarkService); + var imatrixService = new ImatrixService(); + var dbService = new QuantDatabaseService(); + + var imatrixRequest = new ImatrixRequest + { + UseImatrix = Cache.UseImatrix, + ForceRebuild = Cache.ForceImatrixRebuild, + ImatrixUrl = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-url", StringComparison.OrdinalIgnoreCase))?.Value, + DatasetRepo = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-repo", StringComparison.OrdinalIgnoreCase))?.Value, + DatasetSplit = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-split", StringComparison.OrdinalIgnoreCase))?.Value, + DatasetConfig = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-config", StringComparison.OrdinalIgnoreCase))?.Value, + LocalDatasetFile = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-local-file", StringComparison.OrdinalIgnoreCase))?.Value, + ModelDirectory = Cache.ModelDirectory!, + MagicQuantDirectory = Cache.ModelMagicQuantDirectory! + }; + + await imatrixService.EnsureImatrixAsync(imatrixRequest); + + bool loadedPlanFromCache = await benchmarkService.TryInitializeExecutionPlanFromCacheAsync( + quantizationKey: BaselineQuants.Q8_0.Names[0]); + + if (!loadedPlanFromCache) + { + var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); + await benchmarkService.EnsureExecutionPlanAsync(q8ModelGgufPath, quantizationKey: BaselineQuants.Q8_0.Names[0]); + await quantizationService.CleanupPureQ8ModelAsync(); + } + + await dbService.InitializeAsync(); + var remaining = await dbService.GetRemainingTensorConfigsAsync(); + + if (remaining.Count == 0) + { + AnsiConsole.MarkupLine("[yellow]No remaining hybrid combinations to build.[/]"); + return; + } + + var quants = remaining.Select(x => (MQ.DB.Models.HybridQuant)x).ToList(); + var summary = await quantizationService.ProcessHybridBatchAsync(quants); + + AnsiConsole.MarkupLine("[bold green]Build-hybrids complete.[/]"); + AnsiConsole.MarkupLine($" [green]Requested:[/] {summary.Requested:N0}"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {summary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped:[/] {summary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {summary.Failed:N0}"); + } + + private static void ShowHelp() + { + AnsiConsole.MarkupLine("[bold yellow]Command: build-hybrids[/]"); + AnsiConsole.MarkupLine("Builds/benchmarks remaining hybrid combinations from the current candidate-based search space."); + AnsiConsole.MarkupLine("Usage: mq build-hybrids --model-dir \"\" [--use-imatrix] [--allow-high-precision-hybrids]"); } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 97bdf6d..cd53308 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -16,11 +16,6 @@ public static class RuntimeSearchSpace private static readonly HashSet Bf16SuppressedTensorChoiceGroupIds = new(); private static bool _imatrixAvailable; - [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] - [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] - [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] - [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] - [Obsolete("Use candidate-based RuntimeSearchSpace APIs.")] public static bool AllowHighPrecisionHybrids { get; set; } public static void ResetForNewModel() diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 29a40f9..32cb63d 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -89,7 +89,7 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( Description = $"Smallest-first probe for group '{group.Name}' using '{smallest.Names[0]}'.", Quant = quant, TargetGroupId = group.UniqueId, - TestedSchemeId = smallest.UniqueId, + TestedCandidateId = smallest.UniqueId, TestedBaselineId = carrier.UniqueId, IsSmallestProbe = true }); @@ -157,7 +157,7 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample Description = $"Continuation isolation for group '{group.Name}' using '{candidate.Names[0]}'.", Quant = quant, TargetGroupId = group.UniqueId, - TestedSchemeId = candidate.UniqueId, + TestedCandidateId = candidate.UniqueId, TestedBaselineId = carrier.UniqueId }); @@ -191,7 +191,7 @@ public static IEnumerable> GenerateTensorConfigBatches( var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseQuant); if (allowed.IsDefault) - throw new InvalidOperationException("Allowed scheme array is default (uninitialized)."); + throw new InvalidOperationException("Allowed candidate array is default (uninitialized)."); if (allowed.Length == 0) yield break; diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 0c2b7b7..446ad13 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -92,7 +92,7 @@ public async Task AnalyzeInitialIsolationProbesA if (snap == null) continue; - var candidate = BaselineQuants.FromId(probe.TestedSchemeId!.Value); + var candidate = BaselineQuants.FromId(probe.TestedCandidateId!.Value); var reduction = ComputeReductionRatio(carrierBaseOnly.SizeBytes, snap.SizeBytes); var kld = GetAggregateKld(snap); var pplDelta = GetAggregatePplDeltaPercent(snap, nativeBaseline); @@ -181,7 +181,7 @@ public async Task AnalyzeAndApplyFinalAsync( if (snap == null) continue; - var candidateBaseline = BaselineQuants.FromId(item.TestedSchemeId!.Value); + var candidateBaseline = BaselineQuants.FromId(item.TestedCandidateId!.Value); candidates.Add(new GroupCandidateEvaluation { diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index fc61b7a..dd124ab 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -384,7 +384,7 @@ INSERT INTO {TableName} foreach (var plan in groupPlans) { - if (!plan.TargetGroupId.HasValue || !plan.TestedSchemeId.HasValue) + if (!plan.TargetGroupId.HasValue || !plan.TestedCandidateId.HasValue) continue; var snap = await LoadSnapshotByQuantAsync(db, model.Id, imatrixDefinitionId, plan.Quant, ct); @@ -392,7 +392,7 @@ INSERT INTO {TableName} continue; long delta = (long)snap.SizeBytes - (long)carrier.SizeBytes; - deltaByGroupAndCandidate[(plan.TargetGroupId.Value, plan.TestedSchemeId.Value)] = delta; + deltaByGroupAndCandidate[(plan.TargetGroupId.Value, plan.TestedCandidateId.Value)] = delta; } return new PredictionContext( From c803dbd68a57886231621a8b34b95a96319acb6c Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 15:02:07 -0400 Subject: [PATCH 093/258] Fix QuantizationRun imatrix snapshot mapping and add regressions --- .../MagicQuantContextModelSnapshot.cs | 9 +++ ...zationRunAndBuildHybridsRegressionTests.cs | 72 +++++++++++++++++ MagicQuant/Commands/BuildHybrids.cs | 81 +------------------ 3 files changed, 84 insertions(+), 78 deletions(-) create mode 100644 MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index 1d79fdf..a0521f8 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -347,6 +347,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("CompletedUtc") .HasColumnType("TEXT"); @@ -376,6 +379,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); + b.HasIndex("ImatrixDefinitionId"); + b.HasIndex("StartedUtc"); b.HasIndex("TensorComboId"); @@ -447,6 +452,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("AiModelHash"); + b.Navigation("ImatrixDefinition"); + b.Navigation("TensorCombo"); }); @@ -620,6 +627,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("AiModelHash"); + b.Navigation("ImatrixDefinition"); + b.Navigation("TensorCombo"); }); diff --git a/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs b/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs new file mode 100644 index 0000000..a1de027 --- /dev/null +++ b/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs @@ -0,0 +1,72 @@ +using MagicQuant.Commands; +using MagicQuant.Models; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models.DbModels; +using Xunit; + +namespace MagicQuant.Tests; + +public class QuantizationRunAndBuildHybridsRegressionTests +{ + [Fact] + public async Task QuantizationRun_PersistsAndLoads_ImatrixDefinitionForeignKey() + { + string tempRoot = Path.Combine(Path.GetTempPath(), "mq-quant-run-fk-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(tempRoot); + + Cache.MagicQuantDirectory = tempRoot; + + await using var db = new MagicQuantContext(); + + var model = new AiModelHash + { + UniqueHash = "model-" + Guid.NewGuid().ToString("N") + }; + + var combo = new TensorCombo(); + db.AiModelHashes.Add(model); + db.TensorCombos.Add(combo); + await db.SaveChangesAsync(); + + var imatrix = new ImatrixDefinition + { + AiModelHashId = model.Id, + IdentityHash = "imatrix-" + Guid.NewGuid().ToString("N"), + SourceKind = "test" + }; + db.ImatrixDefinitions.Add(imatrix); + await db.SaveChangesAsync(); + + var run = new QuantizationRun + { + AiModelHashId = model.Id, + ImatrixDefinitionId = imatrix.Id, + TensorComboId = combo.Id, + StartedUtc = DateTime.UtcNow.AddSeconds(-1), + CompletedUtc = DateTime.UtcNow, + DurationMs = 1000, + Succeeded = true, + OutputModelPath = Path.Combine(tempRoot, "output.gguf") + }; + db.QuantizationRuns.Add(run); + await db.SaveChangesAsync(); + + var loaded = await db.QuantizationRuns + .Include(x => x.ImatrixDefinition) + .SingleAsync(x => x.Id == run.Id); + + Assert.Equal(imatrix.Id, loaded.ImatrixDefinitionId); + Assert.NotNull(loaded.ImatrixDefinition); + Assert.Equal(imatrix.IdentityHash, loaded.ImatrixDefinition!.IdentityHash); + } + + [Fact] + public async Task BuildHybrids_RunWithoutHelp_ThrowsNotImplementedException() + { + var command = new BuildHybrids(); + var ex = await Assert.ThrowsAsync(() => command.Run(new List())); + Assert.Contains("disabled", ex.Message, StringComparison.OrdinalIgnoreCase); + } +} diff --git a/MagicQuant/Commands/BuildHybrids.cs b/MagicQuant/Commands/BuildHybrids.cs index 49c9972..de1ea75 100644 --- a/MagicQuant/Commands/BuildHybrids.cs +++ b/MagicQuant/Commands/BuildHybrids.cs @@ -1,9 +1,4 @@ -using MagicQuant.Helpers; using MagicQuant.Models; -using MagicQuant.Services; -using MQ.DB; -using MQ.DB.Data; -using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Commands; @@ -18,80 +13,10 @@ public async Task Run(List args) return; } - string? modelDirRaw = args.FirstOrDefault(a => - string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; + await Task.Yield(); - if (string.IsNullOrWhiteSpace(modelDirRaw)) - throw new InvalidOperationException("Missing required argument --model-dir."); - - string fullModelPath = Path.GetFullPath(modelDirRaw); - if (!Directory.Exists(fullModelPath)) - throw new DirectoryNotFoundException($"The directory '{fullModelPath}' does not exist."); - - Cache.ModelDirectory = fullModelPath; - Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); - Cache.UseImatrix = args.Any(a => string.Equals(a.Name, "use-imatrix", StringComparison.OrdinalIgnoreCase)); - Cache.ForceImatrixRebuild = args.Any(a => string.Equals(a.Name, "imatrix-force-rebuild", StringComparison.OrdinalIgnoreCase)); - RuntimeSearchSpace.AllowHighPrecisionHybrids = args.Any(a => - string.Equals(a.Name, "allow-high-precision-hybrids", StringComparison.OrdinalIgnoreCase)); - - Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); - JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); - Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); - - await using (var db = new MagicQuantContext()) - { - _ = db.AiModelHashes.Count(); - } - - var pyManager = new PythonManager(Cache.MagicQuantDirectory); - var benchmarkService = new BenchmarkService(pyManager); - var quantizationService = new QuantizationService(benchmarkService); - var imatrixService = new ImatrixService(); - var dbService = new QuantDatabaseService(); - - var imatrixRequest = new ImatrixRequest - { - UseImatrix = Cache.UseImatrix, - ForceRebuild = Cache.ForceImatrixRebuild, - ImatrixUrl = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-url", StringComparison.OrdinalIgnoreCase))?.Value, - DatasetRepo = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-repo", StringComparison.OrdinalIgnoreCase))?.Value, - DatasetSplit = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-split", StringComparison.OrdinalIgnoreCase))?.Value, - DatasetConfig = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-config", StringComparison.OrdinalIgnoreCase))?.Value, - LocalDatasetFile = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-local-file", StringComparison.OrdinalIgnoreCase))?.Value, - ModelDirectory = Cache.ModelDirectory!, - MagicQuantDirectory = Cache.ModelMagicQuantDirectory! - }; - - await imatrixService.EnsureImatrixAsync(imatrixRequest); - - bool loadedPlanFromCache = await benchmarkService.TryInitializeExecutionPlanFromCacheAsync( - quantizationKey: BaselineQuants.Q8_0.Names[0]); - - if (!loadedPlanFromCache) - { - var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); - await benchmarkService.EnsureExecutionPlanAsync(q8ModelGgufPath, quantizationKey: BaselineQuants.Q8_0.Names[0]); - await quantizationService.CleanupPureQ8ModelAsync(); - } - - await dbService.InitializeAsync(); - var remaining = await dbService.GetRemainingTensorConfigsAsync(); - - if (remaining.Count == 0) - { - AnsiConsole.MarkupLine("[yellow]No remaining hybrid combinations to build.[/]"); - return; - } - - var quants = remaining.Select(x => (MQ.DB.Models.HybridQuant)x).ToList(); - var summary = await quantizationService.ProcessHybridBatchAsync(quants); - - AnsiConsole.MarkupLine("[bold green]Build-hybrids complete.[/]"); - AnsiConsole.MarkupLine($" [green]Requested:[/] {summary.Requested:N0}"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {summary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped:[/] {summary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {summary.Failed:N0}"); + throw new NotImplementedException( + "The build-hybrids command is currently disabled. Use `evolution` for active hybrid generation workflows."); } private static void ShowHelp() From 46f12cc165bb9693f5f36f9d1626eef5a4a7599b Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 15:13:06 -0400 Subject: [PATCH 094/258] Tighten baseline candidate policy and isolate high-precision injection --- MQ.DB/Models/BaselineQuants.cs | 85 +++++++++++++++---- .../BaselineCandidatePolicyTests.cs | 85 ++++++++++++++++--- MagicQuant/Helpers/ComboLogic.cs | 10 ++- MagicQuant/Helpers/RuntimeSearchSpace.cs | 4 +- 4 files changed, 150 insertions(+), 34 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index fc530bf..81ca98a 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -7,10 +7,10 @@ public record BaselineQuants( bool RequiresImatrix, ImmutableArray Names, ImmutableArray TensorWeightSchemes, - bool IsPureBaselineCandidate = true, - bool IsCombinationCarrierCandidate = true, - bool IsExplicitGroupCombinationCandidate = true, - bool IsHighPrecisionExplicitCandidate = false) + bool IsPureBaselineCandidate, + bool IsCombinationCarrierCandidate, + bool IsExplicitGroupCombinationCandidate, + bool IsHighPrecisionExplicitCandidate) { public const byte NativeSourceUniqueId = 250; @@ -18,49 +18,101 @@ public record BaselineQuants( TensorWeightSchemes.IsDefaultOrEmpty ? null : TensorWeightSchemes[0]; public static readonly BaselineQuants Q8_0 = - new(0, false, ["Q8_0"], [TensorWeightScheme.Q8_0], IsCombinationCarrierCandidate: true); + new(0, false, ["Q8_0"], [TensorWeightScheme.Q8_0], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: true, + IsExplicitGroupCombinationCandidate: true, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants Q6_K = - new(1, false, ["Q6_K"], [TensorWeightScheme.Q6_K]); + new(1, false, ["Q6_K"], [TensorWeightScheme.Q6_K], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: true, + IsExplicitGroupCombinationCandidate: true, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants Q5_K = - new(2, false, ["Q5_K"], [TensorWeightScheme.Q5_K]); + new(2, false, ["Q5_K"], [TensorWeightScheme.Q5_K], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: true, + IsExplicitGroupCombinationCandidate: true, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants Q4_K_M = - new(3, false, ["Q4_K_M"], [TensorWeightScheme.Q4_K]); + new(3, false, ["Q4_K_M"], [TensorWeightScheme.Q4_K], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: true, + IsExplicitGroupCombinationCandidate: true, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ4_NL = - new(5, false, ["IQ4_NL"], [TensorWeightScheme.IQ4_NL]); + new(5, false, ["IQ4_NL"], [TensorWeightScheme.IQ4_NL], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: true, + IsExplicitGroupCombinationCandidate: true, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ4_XS = - new(6, false, ["IQ4_XS"], [TensorWeightScheme.IQ4_XS]); + new(6, false, ["IQ4_XS"], [TensorWeightScheme.IQ4_XS], + IsPureBaselineCandidate: true, + IsCombinationCarrierCandidate: true, + IsExplicitGroupCombinationCandidate: true, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ3_S = - new(7, true, ["IQ3_S"], [TensorWeightScheme.IQ3_S]); + new(7, true, ["IQ3_S"], [TensorWeightScheme.IQ3_S], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ3_XS = - new(8, true, ["IQ3_XS"], [TensorWeightScheme.IQ3_XS]); + new(8, true, ["IQ3_XS"], [TensorWeightScheme.IQ3_XS], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ3_XXS = - new(9, true, ["IQ3_XXS"], [TensorWeightScheme.IQ3_XXS]); + new(9, true, ["IQ3_XXS"], [TensorWeightScheme.IQ3_XXS], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ2_S = - new(10, true, ["IQ2_S"], [TensorWeightScheme.IQ2_S]); + new(10, true, ["IQ2_S"], [TensorWeightScheme.IQ2_S], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ2_XS = - new(11, true, ["IQ2_XS"], [TensorWeightScheme.IQ2_XS]); + new(11, true, ["IQ2_XS"], [TensorWeightScheme.IQ2_XS], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ2_XXS = - new(12, true, ["IQ2_XXS"], [TensorWeightScheme.IQ2_XXS]); + new(12, true, ["IQ2_XXS"], [TensorWeightScheme.IQ2_XXS], + IsPureBaselineCandidate: false, + IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, + IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants BF16_Hybrid = new(201, false, ["BF16"], [TensorWeightScheme.BF16], + IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: true); public static readonly BaselineQuants F16_Hybrid = new(202, false, ["F16"], [TensorWeightScheme.F16], + IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, + IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: true); public static readonly ImmutableArray All = @@ -90,6 +142,7 @@ public static BaselineQuants GetNativeQuant() false, [nativeScheme.Names[0]], [nativeScheme], + IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: true); diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs index 8662237..84bdd5c 100644 --- a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -7,24 +7,79 @@ namespace MagicQuant.Tests; public class BaselineCandidatePolicyTests { [Fact] - public void NoImatrix_GroupCandidates_ExcludeRequiresImatrix() + public void GetPureBaselineCandidates_NoImatrix_ReturnsExactlyIq4Xs() { - var candidates = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix: false, allowHighPrecisionHybrids: true); - Assert.DoesNotContain(candidates, x => x.RequiresImatrix); + var ids = BaselineQuants.GetPureBaselineCandidates(hasUsableImatrix: false) + .Select(x => x.UniqueId) + .ToArray(); + + Assert.Equal([BaselineQuants.IQ4_XS.UniqueId], ids); } [Fact] - public void ImatrixEnabled_GroupCandidates_IncludeI3XXS() + public void GetCombinationCarrierBaselines_NoImatrix_ReturnsExactlySixExpectedBaselines() { - var candidates = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix: true, allowHighPrecisionHybrids: true); - Assert.Contains(candidates, x => x.UniqueId == BaselineQuants.IQ3_XXS.UniqueId); + var ids = BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix: false) + .Select(x => x.UniqueId) + .ToArray(); + + Assert.Equal( + [ + BaselineQuants.Q8_0.UniqueId, + BaselineQuants.Q6_K.UniqueId, + BaselineQuants.Q5_K.UniqueId, + BaselineQuants.Q4_K_M.UniqueId, + BaselineQuants.IQ4_NL.UniqueId, + BaselineQuants.IQ4_XS.UniqueId + ], ids); } [Fact] - public void ImatrixEnabled_PureBaselines_IncludeIq2Xxs() + public void GetGroupCombinationCandidates_NoImatrixNoHighPrecision_ReturnsExactlySixExpectedBaselines() { - var baselines = BaselineQuants.GetPureBaselineCandidates(hasUsableImatrix: true); - Assert.Contains(baselines, x => x.UniqueId == BaselineQuants.IQ2_XXS.UniqueId); + var ids = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix: false, allowHighPrecisionHybrids: false) + .Select(x => x.UniqueId) + .ToArray(); + + Assert.Equal( + [ + BaselineQuants.Q8_0.UniqueId, + BaselineQuants.Q6_K.UniqueId, + BaselineQuants.Q5_K.UniqueId, + BaselineQuants.Q4_K_M.UniqueId, + BaselineQuants.IQ4_NL.UniqueId, + BaselineQuants.IQ4_XS.UniqueId + ], ids); + + Assert.DoesNotContain(BaselineQuants.IQ3_S.UniqueId, ids); + Assert.DoesNotContain(BaselineQuants.IQ3_XS.UniqueId, ids); + Assert.DoesNotContain(BaselineQuants.IQ3_XXS.UniqueId, ids); + Assert.DoesNotContain(BaselineQuants.IQ2_S.UniqueId, ids); + Assert.DoesNotContain(BaselineQuants.IQ2_XS.UniqueId, ids); + Assert.DoesNotContain(BaselineQuants.IQ2_XXS.UniqueId, ids); + Assert.DoesNotContain(BaselineQuants.BF16_Hybrid.UniqueId, ids); + Assert.DoesNotContain(BaselineQuants.F16_Hybrid.UniqueId, ids); + } + + [Fact] + public void RuntimeSearchSpace_GetActiveCombinationBaselines_ReturnsExactlySixExpectedBaselines() + { + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(false); + + var ids = RuntimeSearchSpace.GetActiveCombinationBaselines() + .Select(x => x.UniqueId) + .ToArray(); + + Assert.Equal( + [ + BaselineQuants.Q8_0.UniqueId, + BaselineQuants.Q6_K.UniqueId, + BaselineQuants.Q5_K.UniqueId, + BaselineQuants.Q4_K_M.UniqueId, + BaselineQuants.IQ4_NL.UniqueId, + BaselineQuants.IQ4_XS.UniqueId + ], ids); } [Fact] @@ -40,27 +95,29 @@ public void ExplicitCandidateExhaustion_UsesQ8FallbackPolicy() } [Fact] - public void HighPrecisionCandidatesRemainInReasoningUniverse_UntilLatePruneStage() + public void CandidateBanAuthority_DrivesAllowedCandidateSet() { RuntimeSearchSpace.ResetForNewModel(); RuntimeSearchSpace.SetImatrixAvailability(true); + RuntimeSearchSpace.BanCombinationCandidateForGroup(TReg.AttnQ, BaselineQuants.Q6_K); var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); var attnQIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.AttnQ.UniqueId); - Assert.Contains(BaselineQuants.BF16_Hybrid.UniqueId, allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.Q6_K.UniqueId, allowed[attnQIndex]); } [Fact] - public void CandidateBanAuthority_DrivesAllowedCandidateSet() + public void ComboLogic_WhenHighPrecisionDisabled_DoesNotInjectBf16OrF16() { RuntimeSearchSpace.ResetForNewModel(); RuntimeSearchSpace.SetImatrixAvailability(true); + RuntimeSearchSpace.AllowHighPrecisionHybrids = false; - RuntimeSearchSpace.BanCombinationCandidateForGroup(TReg.AttnQ, BaselineQuants.Q6_K); var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); var attnQIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.AttnQ.UniqueId); - Assert.DoesNotContain(BaselineQuants.Q6_K.UniqueId, allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.BF16_Hybrid.UniqueId, allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.F16_Hybrid.UniqueId, allowed[attnQIndex]); } } diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index ee3bce1..bd7d709 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -13,7 +13,7 @@ public static class ComboLogic public static ImmutableArray GetAllowedCandidateIdsPerGroup(BaselineQuants baseQuant) { bool imatrixAvailable = RuntimeSearchSpace.HasUsableImatrix(); - var candidatesForRun = BaselineQuants.GetGroupCombinationCandidates(imatrixAvailable, allowHighPrecisionHybrids: true) + var candidatesForRun = BaselineQuants.GetGroupCombinationCandidates(imatrixAvailable, allowHighPrecisionHybrids: false) .ToImmutableArray(); var builder = ImmutableArray.CreateBuilder(); @@ -29,8 +29,14 @@ public static ImmutableArray GetAllowedCandidateIdsPerGroup(BaselineQuan var ids = new List(); - if (!RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)) + // Strict policy (Option A): + // - normal explicit hybrid families come only from GetGroupCombinationCandidates(..., false) + // - BF16/F16 are injected only here and only when AllowHighPrecisionHybrids is enabled + if (RuntimeSearchSpace.AllowHighPrecisionHybrids && !RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)) + { ids.Add(BaselineQuants.BF16_Hybrid.UniqueId); + ids.Add(BaselineQuants.F16_Hybrid.UniqueId); + } foreach (var candidate in candidatesForRun) { diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index cd53308..c64d5cf 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -71,7 +71,7 @@ public static void BanCombinationCandidateForGroupByLearnedBaselineAbsence( public static void BanAllExplicitCombinationCandidatesForGroup(TensorGroup group) { - foreach (var candidate in BaselineQuants.GetGroupCombinationCandidates(_imatrixAvailable, allowHighPrecisionHybrids: true)) + foreach (var candidate in BaselineQuants.GetGroupCombinationCandidates(_imatrixAvailable, allowHighPrecisionHybrids: false)) BanCombinationCandidateForGroup(group, candidate); } @@ -91,7 +91,7 @@ public static bool IsCombinationCandidateRuntimeBannedForGroup(TensorGroup group public static bool HasAnyExplicitCombinationCandidateAllowed(TensorGroup group) { - return BaselineQuants.GetGroupCombinationCandidates(_imatrixAvailable, allowHighPrecisionHybrids: true) + return BaselineQuants.GetGroupCombinationCandidates(_imatrixAvailable, allowHighPrecisionHybrids: false) .Any(x => !IsCombinationCandidateRuntimeBannedForGroup(group, x)); } From 43b67f5f85c99515b698288eeb410b401d8ea19a Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 16:41:51 -0400 Subject: [PATCH 095/258] Fix learned-prune semantics and clear stale runtime prune records --- MQ.DB/Models/BaselineQuants.cs | 7 ++ .../BaselineCandidatePolicyTests.cs | 14 ++++ .../LearnedBaselinePruningServiceTests.cs | 29 +++++++ MagicQuant/Helpers/ComboLogic.cs | 3 + MagicQuant/Helpers/RuntimeSearchSpace.cs | 80 +++++++++++++------ MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 4 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 2 +- .../Services/IsolationOptimizationService.cs | 19 ++++- .../Services/LearnedBaselinePruningService.cs | 32 +++++--- .../Services/ModelCompatibilityService.cs | 6 +- 10 files changed, 152 insertions(+), 44 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 81ca98a..aed6d3d 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -17,6 +17,13 @@ public record BaselineQuants( public TensorWeightScheme? DefaultTensorScheme => TensorWeightSchemes.IsDefaultOrEmpty ? null : TensorWeightSchemes[0]; + public IReadOnlyList BannedGroupIds => + TensorWeightSchemes + .SelectMany(x => x.BannedGroups.Select(g => g.UniqueId)) + .Distinct() + .OrderBy(x => x) + .ToList(); + public static readonly BaselineQuants Q8_0 = new(0, false, ["Q8_0"], [TensorWeightScheme.Q8_0], IsPureBaselineCandidate: false, diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs index 84bdd5c..4f2cc20 100644 --- a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -120,4 +120,18 @@ public void ComboLogic_WhenHighPrecisionDisabled_DoesNotInjectBf16OrF16() Assert.DoesNotContain(BaselineQuants.BF16_Hybrid.UniqueId, allowed[attnQIndex]); Assert.DoesNotContain(BaselineQuants.F16_Hybrid.UniqueId, allowed[attnQIndex]); } + + [Fact] + public void ComboLogic_UsesCandidateLevelBannedGroups() + { + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(false); + + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); + var moeRouterIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.MoeRouter.UniqueId); + + Assert.DoesNotContain(BaselineQuants.Q5_K.UniqueId, allowed[moeRouterIndex]); + Assert.DoesNotContain(BaselineQuants.IQ4_NL.UniqueId, allowed[moeRouterIndex]); + Assert.DoesNotContain(BaselineQuants.IQ4_XS.UniqueId, allowed[moeRouterIndex]); + } } diff --git a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs index 9afe17f..4d776d2 100644 --- a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs +++ b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs @@ -44,5 +44,34 @@ public void Embeddings_LearnedBaselinePruning_OnlyAllowsQ6KAndBansOtherBaselines Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q4_K") && x.Contains("decision=BAN")); Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=IQ4_NL") && x.Contains("decision=BAN")); Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=IQ4_XS") && x.Contains("decision=BAN")); + Assert.Contains(result.Notes, x => x.Contains("expected=[4]") && x.Contains("effective=[4]") && x.Contains("matched=[4]")); + } + + [Fact] + public void LearnedPruneBookkeeping_CanBeClearedPerGroupCandidate() + { + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(true); + + var result = new LearnedBaselinePruningResult(); + var learnedRows = new List + { + new(BaselineQuants.Q5_K.UniqueId, TensorWeightScheme.Q5_K.UniqueId, TReg.Embeddings.UniqueId, "Q6_K") + }; + + LearnedBaselinePruningService.ApplyLearnedBaselinePruning( + learnedRows, + aiModelHashId: 1, + aiModelHashUniqueHash: "regression-model-hash", + unusedGroupIds: new HashSet(), + result: result); + + Assert.True(RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(TReg.Embeddings) + .Any(x => x.Candidate.UniqueId == BaselineQuants.Q5_K.UniqueId)); + + RuntimeSearchSpace.ClearLearnedBaselinePruneForGroupCandidate(TReg.Embeddings, BaselineQuants.Q5_K); + + Assert.DoesNotContain(RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(TReg.Embeddings), + x => x.Candidate.UniqueId == BaselineQuants.Q5_K.UniqueId); } } diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index bd7d709..672e8be 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -40,6 +40,9 @@ public static ImmutableArray GetAllowedCandidateIdsPerGroup(BaselineQuan foreach (var candidate in candidatesForRun) { + if (candidate.BannedGroupIds.Contains(group.UniqueId)) + continue; + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index c64d5cf..18c3412 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -5,13 +5,16 @@ namespace MagicQuant.Helpers; public sealed class RuntimeLearnedBaselineBanInfo { public BaselineQuants Candidate { get; init; } = default!; - public IReadOnlyList MissingBaselines { get; init; } = Array.Empty(); + public IReadOnlyList ExpectedTensorWeightSchemeIds { get; init; } = Array.Empty(); + public IReadOnlyList MatchedTensorWeightSchemeIds { get; init; } = Array.Empty(); + public IReadOnlyList MissingTensorWeightSchemeIds { get; init; } = Array.Empty(); + public string Note { get; init; } = string.Empty; } public static class RuntimeSearchSpace { private static readonly Dictionary> ExplicitCandidateBansByGroup = new(); - private static readonly Dictionary>> LearnedBaselineMissingByGroupAndCandidate = new(); + private static readonly Dictionary> LearnedPrunesByGroupAndCandidate = new(); private static readonly HashSet DisabledCombinationBaselineIds = new(); private static readonly HashSet Bf16SuppressedTensorChoiceGroupIds = new(); private static bool _imatrixAvailable; @@ -21,7 +24,7 @@ public static class RuntimeSearchSpace public static void ResetForNewModel() { ExplicitCandidateBansByGroup.Clear(); - LearnedBaselineMissingByGroupAndCandidate.Clear(); + LearnedPrunesByGroupAndCandidate.Clear(); DisabledCombinationBaselineIds.Clear(); Bf16SuppressedTensorChoiceGroupIds.Clear(); _imatrixAvailable = false; @@ -47,26 +50,51 @@ public static void BanCombinationCandidateForGroup(TensorGroup group, BaselineQu set.Add(candidate.UniqueId); } - public static void BanCombinationCandidateForGroupByLearnedBaselineAbsence( + public static void BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( TensorGroup group, BaselineQuants candidate, - BaselineQuants sourceBaseline) + IReadOnlyCollection expectedTensorWeightSchemeIds, + IReadOnlyCollection matchedTensorWeightSchemeIds, + string note) { BanCombinationCandidateForGroup(group, candidate); - if (!LearnedBaselineMissingByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) + if (!LearnedPrunesByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) { - byCandidate = new Dictionary>(); - LearnedBaselineMissingByGroupAndCandidate[group.UniqueId] = byCandidate; + byCandidate = new Dictionary(); + LearnedPrunesByGroupAndCandidate[group.UniqueId] = byCandidate; } - if (!byCandidate.TryGetValue(candidate.UniqueId, out var baselineIds)) + var expected = expectedTensorWeightSchemeIds + .Distinct() + .OrderBy(x => x) + .ToList(); + var matched = matchedTensorWeightSchemeIds + .Distinct() + .OrderBy(x => x) + .ToList(); + var missing = expected.Except(matched).OrderBy(x => x).ToList(); + + byCandidate[candidate.UniqueId] = new RuntimeLearnedBaselineBanInfo { - baselineIds = new HashSet(); - byCandidate[candidate.UniqueId] = baselineIds; - } + Candidate = candidate, + ExpectedTensorWeightSchemeIds = expected, + MatchedTensorWeightSchemeIds = matched, + MissingTensorWeightSchemeIds = missing, + Note = note + }; + } + + public static void ClearLearnedBaselinePruneBookkeeping() => LearnedPrunesByGroupAndCandidate.Clear(); - baselineIds.Add(sourceBaseline.UniqueId); + public static void ClearLearnedBaselinePruneForGroupCandidate(TensorGroup group, BaselineQuants candidate) + { + if (!LearnedPrunesByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) + return; + + byCandidate.Remove(candidate.UniqueId); + if (byCandidate.Count == 0) + LearnedPrunesByGroupAndCandidate.Remove(group.UniqueId); } public static void BanAllExplicitCombinationCandidatesForGroup(TensorGroup group) @@ -101,25 +129,20 @@ public static IReadOnlyList GetGroupsWithExplicitQuantBanned() => TReg.All.Where(IsGroupExplicitCandidateBanned).OrderBy(x => x.UniqueId).ToList(); public static bool HasLearnedBaselineMissingPrunesForGroup(TensorGroup group) - => LearnedBaselineMissingByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate) && byCandidate.Count > 0; + => LearnedPrunesByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate) && byCandidate.Count > 0; public static IReadOnlyList GetGroupsWithLearnedBaselineMissingPrunes() => TReg.All.Where(HasLearnedBaselineMissingPrunesForGroup).OrderBy(x => x.UniqueId).ToList(); public static IReadOnlyList GetLearnedBaselineMissingPrunedCandidatesForGroup(TensorGroup group) { - if (!LearnedBaselineMissingByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) + if (!LearnedPrunesByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) return Array.Empty(); - var result = new List(); - foreach (var kvp in byCandidate.OrderBy(x => x.Key)) - { - var candidate = BaselineQuants.FromId(kvp.Key); - var baselines = kvp.Value.OrderBy(x => x).Select(BaselineQuants.FromId).ToList(); - result.Add(new RuntimeLearnedBaselineBanInfo { Candidate = candidate, MissingBaselines = baselines }); - } - - return result; + return byCandidate + .OrderBy(x => x.Key) + .Select(x => x.Value) + .ToList(); } public static void SuppressBf16TensorChoice(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); @@ -167,9 +190,14 @@ public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) => BanCombinationCandidateForGroup(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId)); - [Obsolete("Use BanCombinationCandidateForGroupByLearnedBaselineAbsence.")] + [Obsolete("Use BanCombinationCandidateForGroupDueToLearnedSchemeMismatch.")] public static void BanSchemeForGroupByLearnedBaselineAbsence(TensorGroup group, TensorWeightScheme scheme, BaselineQuants sourceBaseline) - => BanCombinationCandidateForGroupByLearnedBaselineAbsence(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId), sourceBaseline); + => BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( + group, + BaselineQuants.FromTensorSchemeId(scheme.UniqueId), + expectedTensorWeightSchemeIds: [scheme.UniqueId], + matchedTensorWeightSchemeIds: Array.Empty(), + note: $"Legacy scheme-ban shim invoked for source baseline '{sourceBaseline.Names[0]}'."); [Obsolete("Use BanAllExplicitCombinationCandidatesForGroup.")] public static void BanAllExplicitTensorSchemesForGroup(TensorGroup group) diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index c4d9014..c1064e9 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -57,7 +57,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc var learned = RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group); var parts = learned.Select(x => - $"{x.Candidate.Names[0]} <= {string.Join("/", x.MissingBaselines.Select(b => b.Names[0]))}"); + $"{x.Candidate.Names[0]} (expected={string.Join("/", x.ExpectedTensorWeightSchemeIds)}, matched={string.Join("/", x.MatchedTensorWeightSchemeIds)})"); AnsiConsole.MarkupLine( $" [yellow]- {Markup.Escape(group.Name)}[/] :: [grey]{Markup.Escape(string.Join(", ", parts))}[/]"); @@ -108,4 +108,4 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc AnsiConsole.MarkupLine($"[bold yellow]Grand total:[/] {ComboCounter.CountAll():N0}"); } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 32cb63d..8fb82e2 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -124,7 +124,7 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var result = new RequiredSampleGenerationResult(); var carrier = BaselineQuants.Q8_0; - var candidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: true) + var candidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) .Where(x => x.UniqueId != BaselineQuants.BF16_Hybrid.UniqueId) .OrderBy(x => x.UniqueId) .ToList(); diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 446ad13..1fa4a9a 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -182,6 +182,7 @@ public async Task AnalyzeAndApplyFinalAsync( continue; var candidateBaseline = BaselineQuants.FromId(item.TestedCandidateId!.Value); + RuntimeSearchSpace.ClearLearnedBaselinePruneForGroupCandidate(group, candidateBaseline); candidates.Add(new GroupCandidateEvaluation { @@ -254,11 +255,25 @@ public async Task AnalyzeAndApplyFinalAsync( $"{candidate.CandidateBaseline.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}%"); } + var survivorIds = candidates.Select(x => x.CandidateBaseline.UniqueId).ToHashSet(); foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group)) { - var sourceBaselines = string.Join(", ", banInfo.MissingBaselines.Select(x => x.Names[0])); + if (survivorIds.Contains(banInfo.Candidate.UniqueId)) + continue; + + string expected = banInfo.ExpectedTensorWeightSchemeIds.Count == 0 + ? "" + : string.Join(", ", banInfo.ExpectedTensorWeightSchemeIds); + string matched = banInfo.MatchedTensorWeightSchemeIds.Count == 0 + ? "" + : string.Join(", ", banInfo.MatchedTensorWeightSchemeIds); + string missing = banInfo.MissingTensorWeightSchemeIds.Count == 0 + ? "" + : string.Join(", ", banInfo.MissingTensorWeightSchemeIds); + decision.Candidates.Add( - $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned baseline-family mapping for this group (missing baseline source(s): {sourceBaselines})."); + $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned candidate/group scheme matching " + + $"(expected schemes: {expected}; matched: {matched}; missing: {missing})."); } result.GroupDetails.Add(decision); diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index 017e114..0c1863b 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -31,6 +31,8 @@ public async Task AnalyzeAndApplyAsync(Cancellatio if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) throw new InvalidOperationException("Cache.CurrentModelId is not set."); + RuntimeSearchSpace.ClearLearnedBaselinePruneBookkeeping(); + var result = new LearnedBaselinePruningResult(); await using var db = new MagicQuantContext(); @@ -81,10 +83,12 @@ internal static void ApplyLearnedBaselinePruning( HashSet unusedGroupIds, LearnedBaselinePruningResult result) { + RuntimeSearchSpace.ClearLearnedBaselinePruneBookkeeping(); + var aliasToSchemeIds = BuildAliasToSchemeIds(); - var effectiveSchemesByBaselineAndGroup = BuildEffectiveSchemesByBaselineAndGroup(learnedRows, aliasToSchemeIds); + var effectiveSchemesByCandidateAndGroup = BuildEffectiveSchemesByBaselineAndGroup(learnedRows, aliasToSchemeIds); - var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: true) + var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) .Where(x => x.UniqueId != BaselineQuants.BF16_Hybrid.UniqueId) .Where(x => x.UniqueId != BaselineQuants.F16_Hybrid.UniqueId) .OrderBy(x => x.UniqueId) @@ -100,22 +104,30 @@ internal static void ApplyLearnedBaselinePruning( if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; - var owningBaseline = candidate; - var key = (owningBaseline.UniqueId, group.UniqueId); - bool hasEffectiveSet = effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var effectiveForGroup); - var candidateSchemeId = candidate.DefaultTensorScheme?.UniqueId; - bool allow = hasEffectiveSet && candidateSchemeId.HasValue && effectiveForGroup!.Contains(candidateSchemeId.Value); + var key = (candidate.UniqueId, group.UniqueId); + bool hasEffectiveSet = effectiveSchemesByCandidateAndGroup.TryGetValue(key, out var effectiveForGroup); + var expectedIds = candidate.TensorWeightSchemes.Select(x => x.UniqueId).Distinct().OrderBy(x => x).ToList(); + var effectiveIdsSet = hasEffectiveSet ? effectiveForGroup! : new HashSet(); + var matchedIds = expectedIds.Where(effectiveIdsSet.Contains).OrderBy(x => x).ToList(); + bool allow = matchedIds.Count > 0; string effectiveIds = hasEffectiveSet - ? string.Join(",", effectiveForGroup!.OrderBy(x => x)) + ? string.Join(",", effectiveIdsSet.OrderBy(x => x)) : ""; + string expected = string.Join(",", expectedIds); + string matched = matchedIds.Count > 0 ? string.Join(",", matchedIds) : ""; result.Notes.Add( $"Learned-prune check: model={aiModelHashId}/{aiModelHashUniqueHash}, group={group.Name}, " + - $"candidate={candidate.Names[0]}, owner={owningBaseline.Names[0]}, effective=[{effectiveIds}], decision={(allow ? "ALLOW" : "BAN")}"); + $"candidate={candidate.Names[0]}, expected=[{expected}], effective=[{effectiveIds}], matched=[{matched}], decision={(allow ? "ALLOW" : "BAN")}"); if (!allow) { - RuntimeSearchSpace.BanCombinationCandidateForGroupByLearnedBaselineAbsence(group, candidate, owningBaseline); + RuntimeSearchSpace.BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( + group, + candidate, + expectedTensorWeightSchemeIds: expectedIds, + matchedTensorWeightSchemeIds: matchedIds, + note: "No matching learned tensor-weight schemes for candidate/group."); result.GroupCandidateEliminations++; } } diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 18aee0d..e211fa9 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -37,7 +37,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) { var groupDefinitions = TReg.All.ToDictionary(g => g.Name, g => g.Tensors); - var candidateBlockRequirements = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: true) + var candidateBlockRequirements = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) .Where(c => c.DefaultTensorScheme?.BlockNeo.HasValue == true) .ToDictionary(c => c.Names[0], c => c.DefaultTensorScheme!.BlockNeo!.Value); @@ -103,7 +103,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) foreach (var failure in result.Incompatible) { var group = TReg.GetByName(failure.Group); - var candidate = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: true) + var candidate = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) .FirstOrDefault(c => c.Names.Any(n => n.Equals(failure.Scheme, StringComparison.OrdinalIgnoreCase))); if (group == null || candidate == null) @@ -271,4 +271,4 @@ private class CompatFailure public string Group { get; set; } = string.Empty; public string Scheme { get; set; } = string.Empty; } -} \ No newline at end of file +} From ea3bb09db134b2dbd505330b68b06d1573442b37 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 16:59:04 -0400 Subject: [PATCH 096/258] Make baseline metadata authoritative for bans and learned matching --- MQ.DB/Models/BaselineQuants.cs | 54 ++++++++-------- MQ.DB/Models/TensorWeightScheme.cs | 2 + .../Services/LearnedBaselinePruningService.cs | 2 +- MagicQuant/Services/QuantizationService.cs | 61 +++++++++++-------- 4 files changed, 68 insertions(+), 51 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index aed6d3d..c417dba 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using MQ.DB; namespace MQ.DB.Models; @@ -6,7 +7,9 @@ public record BaselineQuants( byte UniqueId, bool RequiresImatrix, ImmutableArray Names, - ImmutableArray TensorWeightSchemes, + TensorWeightScheme PrimaryTensorWeightScheme, + ImmutableArray LearnedMatchTensorWeightSchemes, + ImmutableArray BannedGroupIds, bool IsPureBaselineCandidate, bool IsCombinationCarrierCandidate, bool IsExplicitGroupCombinationCandidate, @@ -14,109 +17,104 @@ public record BaselineQuants( { public const byte NativeSourceUniqueId = 250; - public TensorWeightScheme? DefaultTensorScheme => - TensorWeightSchemes.IsDefaultOrEmpty ? null : TensorWeightSchemes[0]; + public TensorWeightScheme? DefaultTensorScheme => PrimaryTensorWeightScheme; - public IReadOnlyList BannedGroupIds => - TensorWeightSchemes - .SelectMany(x => x.BannedGroups.Select(g => g.UniqueId)) - .Distinct() - .OrderBy(x => x) - .ToList(); + // Legacy alias retained for compatibility with existing call sites. + public ImmutableArray TensorWeightSchemes => LearnedMatchTensorWeightSchemes; public static readonly BaselineQuants Q8_0 = - new(0, false, ["Q8_0"], [TensorWeightScheme.Q8_0], + new(0, false, ["Q8_0"], TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: true, IsExplicitGroupCombinationCandidate: true, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants Q6_K = - new(1, false, ["Q6_K"], [TensorWeightScheme.Q6_K], + new(1, false, ["Q6_K"], TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: true, IsExplicitGroupCombinationCandidate: true, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants Q5_K = - new(2, false, ["Q5_K"], [TensorWeightScheme.Q5_K], + new(2, false, ["Q5_K"], TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: true, IsExplicitGroupCombinationCandidate: true, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants Q4_K_M = - new(3, false, ["Q4_K_M"], [TensorWeightScheme.Q4_K], + new(3, false, ["Q4_K_M"], TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: true, IsExplicitGroupCombinationCandidate: true, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ4_NL = - new(5, false, ["IQ4_NL"], [TensorWeightScheme.IQ4_NL], + new(5, false, ["IQ4_NL"], TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: true, IsExplicitGroupCombinationCandidate: true, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ4_XS = - new(6, false, ["IQ4_XS"], [TensorWeightScheme.IQ4_XS], + new(6, false, ["IQ4_XS"], TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], IsPureBaselineCandidate: true, IsCombinationCarrierCandidate: true, IsExplicitGroupCombinationCandidate: true, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ3_S = - new(7, true, ["IQ3_S"], [TensorWeightScheme.IQ3_S], + new(7, true, ["IQ3_S"], TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ3_XS = - new(8, true, ["IQ3_XS"], [TensorWeightScheme.IQ3_XS], + new(8, true, ["IQ3_XS"], TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ3_XXS = - new(9, true, ["IQ3_XXS"], [TensorWeightScheme.IQ3_XXS], + new(9, true, ["IQ3_XXS"], TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ2_S = - new(10, true, ["IQ2_S"], [TensorWeightScheme.IQ2_S], + new(10, true, ["IQ2_S"], TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ2_XS = - new(11, true, ["IQ2_XS"], [TensorWeightScheme.IQ2_XS], + new(11, true, ["IQ2_XS"], TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants IQ2_XXS = - new(12, true, ["IQ2_XXS"], [TensorWeightScheme.IQ2_XXS], + new(12, true, ["IQ2_XXS"], TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: false); public static readonly BaselineQuants BF16_Hybrid = - new(201, false, ["BF16"], [TensorWeightScheme.BF16], + new(201, false, ["BF16"], TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExplicitCandidate: true); public static readonly BaselineQuants F16_Hybrid = - new(202, false, ["F16"], [TensorWeightScheme.F16], + new(202, false, ["F16"], TensorWeightScheme.F16, [TensorWeightScheme.F16], [], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, @@ -148,7 +146,9 @@ public static BaselineQuants GetNativeQuant() NativeSourceUniqueId, false, [nativeScheme.Names[0]], + nativeScheme, [nativeScheme], + [], IsPureBaselineCandidate: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, @@ -185,7 +185,7 @@ public static IReadOnlyList GetGroupCombinationCandidates(bool h public static void ValidateIntegrityOrThrow() { var invalidBaselines = All - .Where(x => x.TensorWeightSchemes.IsDefaultOrEmpty) + .Where(x => x.LearnedMatchTensorWeightSchemes.IsDefaultOrEmpty) .Select(x => x.Names.IsDefaultOrEmpty ? $"id:{x.UniqueId}" : x.Names[0]) .ToList(); @@ -197,7 +197,7 @@ public static void ValidateIntegrityOrThrow() } var duplicateSchemeIds = All - .SelectMany(x => x.TensorWeightSchemes.Select(s => new { Baseline = x, Scheme = s })) + .SelectMany(x => x.LearnedMatchTensorWeightSchemes.Select(s => new { Baseline = x, Scheme = s })) .GroupBy(x => x.Scheme.UniqueId) .Where(g => g.Count() > 1) .Where(g => g.Key != TensorWeightScheme.BF16.UniqueId && g.Key != TensorWeightScheme.F16.UniqueId) @@ -229,7 +229,9 @@ public static BaselineQuants FromId(byte id) public static BaselineQuants FromTensorSchemeId(byte schemeId) { - var found = All.FirstOrDefault(x => x.TensorWeightSchemes.Any(s => s.UniqueId == schemeId)); + var found = All.FirstOrDefault(x => + x.PrimaryTensorWeightScheme.UniqueId == schemeId || + x.LearnedMatchTensorWeightSchemes.Any(s => s.UniqueId == schemeId)); if (found == null) throw new InvalidOperationException($"Unknown tensor scheme id '{schemeId}' for baseline conversion."); diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 3995bcd..f4b5eee 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -10,6 +10,7 @@ public sealed class TensorWeightScheme public byte UniqueId { get; } public bool RequiresImatrix { get; } public ImmutableArray Names { get; } + // Legacy/runtime-compat list. Baseline candidate policy must use BaselineQuants.BannedGroupIds. public List BannedGroups { get; } public ushort? BlockNeo { get; } public bool IsEligibleForBaseline { get; } @@ -44,6 +45,7 @@ public void ResetRuntimeBans() BannedGroups.Add(group); } + // Legacy helper; candidate/search policy should query BaselineQuants.BannedGroupIds instead. public bool IsBannedFor(TensorGroup group) => BannedGroups.Any(x => x.UniqueId == group.UniqueId); public static void ResetAllRuntimeBans() diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index 0c1863b..49a070d 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -106,7 +106,7 @@ internal static void ApplyLearnedBaselinePruning( var key = (candidate.UniqueId, group.UniqueId); bool hasEffectiveSet = effectiveSchemesByCandidateAndGroup.TryGetValue(key, out var effectiveForGroup); - var expectedIds = candidate.TensorWeightSchemes.Select(x => x.UniqueId).Distinct().OrderBy(x => x).ToList(); + var expectedIds = candidate.LearnedMatchTensorWeightSchemes.Select(x => x.UniqueId).Distinct().OrderBy(x => x).ToList(); var effectiveIdsSet = hasEffectiveSet ? effectiveForGroup! : new HashSet(); var matchedIds = expectedIds.Where(effectiveIdsSet.Contains).OrderBy(x => x).ToList(); bool allow = matchedIds.Count > 0; diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 9317e50..b0d821d 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1461,12 +1461,15 @@ private List BuildRequestedTensorOverrides( if (hybrid.CandidateBaseline != null && hybrid.CandidateBaseline.UniqueId == quant.BaseQuant.UniqueId) continue; - var sourceScheme = hybrid.CandidateBaseline?.DefaultTensorScheme ?? hybrid.TensorType; - var learned = TryLoadLearnedTensorMapping(sourceScheme, hybrid.TGroup, hybrid.CandidateBaseline); + var sourceBaseline = hybrid.CandidateBaseline ?? BaselineQuants.FromTensorSchemeId(hybrid.TensorType.UniqueId); + var learned = TryLoadLearnedTensorMapping( + sourceBaselineId: sourceBaseline.UniqueId, + targetGroup: hybrid.TGroup, + preferredSourceScheme: hybrid.CandidateBaseline?.DefaultTensorScheme ?? hybrid.TensorType); if (learned.Count == 0) { throw new InvalidOperationException( - $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + scheme '{hybrid.CandidateBaseline?.Names[0] ?? ResolveSchemeName(hybrid.TensorType)}'. " + + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. " + "Run with --relearn-baseline-mappings to regenerate."); } @@ -1484,7 +1487,7 @@ private List BuildRequestedTensorOverrides( var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); throw new InvalidOperationException( - $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + scheme '{hybrid.CandidateBaseline?.Names[0] ?? ResolveSchemeName(hybrid.TensorType)}'. " + + $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. " + $"Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); } @@ -1502,7 +1505,10 @@ private List BuildRequestedTensorOverrides( return result; } - private Dictionary TryLoadLearnedTensorMapping(TensorWeightScheme sourceScheme, TensorGroup targetGroup, BaselineQuants? sourceBaseline = null) + private Dictionary TryLoadLearnedTensorMapping( + byte sourceBaselineId, + TensorGroup targetGroup, + TensorWeightScheme? preferredSourceScheme = null) { using var db = new MagicQuantContext(); @@ -1513,34 +1519,41 @@ private Dictionary TryLoadLearnedTensorMapping(TensorWeightSchem if (model == null) return new Dictionary(StringComparer.Ordinal); - byte baselineId; - if (TensorWeightScheme.IsNativePrecisionScheme(sourceScheme)) - { - baselineId = BaselineQuants.NativeSourceUniqueId; - } - else - { - var baseline = sourceBaseline ?? BaselineQuants.All.FirstOrDefault(x => - x.TensorWeightSchemes.Any(s => s.UniqueId == sourceScheme.UniqueId)); - - if (baseline == null) - return new Dictionary(StringComparer.Ordinal); - - baselineId = baseline.UniqueId; - } - - var rows = db.LearnedBaselineTensorQuants + var allRows = db.LearnedBaselineTensorQuants .AsNoTracking() .Where(x => x.AiModelHashId == model.Id) - .Where(x => x.BaselineQuantId == baselineId) - .Where(x => x.TensorWeightSchemeId == sourceScheme.UniqueId) + .Where(x => x.BaselineQuantId == sourceBaselineId) .Where(x => x.TensorGroupId == targetGroup.UniqueId) .OrderBy(x => x.TensorName) .ToList(); + if (allRows.Count == 0) + return new Dictionary(StringComparer.Ordinal); + + var rows = allRows; + if (preferredSourceScheme != null) + { + var preferred = allRows.Where(x => x.TensorWeightSchemeId == preferredSourceScheme.UniqueId).ToList(); + if (preferred.Count > 0) + rows = preferred; + } + if (rows.Count == 0) return new Dictionary(StringComparer.Ordinal); + // If rows contain mixed source schemes, use the dominant scheme for stable coverage semantics. + if (rows.Select(x => x.TensorWeightSchemeId).Distinct().Count() > 1) + { + var dominantSchemeId = rows + .GroupBy(x => x.TensorWeightSchemeId) + .OrderByDescending(g => g.Count()) + .ThenBy(g => g.Key) + .Select(g => g.Key) + .First(); + + rows = rows.Where(x => x.TensorWeightSchemeId == dominantSchemeId).ToList(); + } + return rows.ToDictionary(x => x.TensorName, x => x.FinalQuantType, StringComparer.Ordinal); } From 77437470c152f30df68eebf50dba0adcd6f987d5 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 20 Apr 2026 17:06:14 -0400 Subject: [PATCH 097/258] Remove scheme-level banned-group ownership and prune via normal candidate universe --- MQ.DB/Cache.cs | 5 +- MQ.DB/Models/TensorWeight.cs | 9 +- MQ.DB/Models/TensorWeightScheme.cs | 87 ++----------------- MagicQuant/Helpers/RuntimeSearchSpace.cs | 1 - .../Services/LearnedBaselinePruningService.cs | 2 - .../Services/ModelCompatibilityService.cs | 4 - 6 files changed, 13 insertions(+), 95 deletions(-) diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index 3abefbd..87f4a0c 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -52,9 +52,8 @@ public enum MainTorchType /* - * This is properly updated, but not really used. More for generic logs because the - * TensorWeightScheme is what's actually updated with the real ban logic both from the - * start and during runtime + * Groups not present in the current model graph. These are forced to NULL/ignored + * by runtime search-space planning. */ public static List UnusedTensorGroups = new List(); diff --git a/MQ.DB/Models/TensorWeight.cs b/MQ.DB/Models/TensorWeight.cs index e26d2d2..f306acb 100644 --- a/MQ.DB/Models/TensorWeight.cs +++ b/MQ.DB/Models/TensorWeight.cs @@ -2,12 +2,11 @@ namespace MQ.DB.Models; public class TensorWeight { - public TensorWeight(byte uniqueId, bool requiresImatrix, string[] names, TensorGroup[]? bannedGroups = null) + public TensorWeight(byte uniqueId, bool requiresImatrix, string[] names) { Names = names.ToList(); UniqueId = uniqueId; RequiresImatrix = requiresImatrix; - BannedGroups = bannedGroups?.ToList(); } /// @@ -45,8 +44,4 @@ public string GetName(string? name = null) public bool RequiresImatrix { get; } - /// - /// Which Tensor Groups this tensor weight CANNOT be attached too. - /// - public List? BannedGroups { get; } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index f4b5eee..e4ca2d2 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -5,13 +5,9 @@ namespace MQ.DB.Models; public sealed class TensorWeightScheme { - private readonly HashSet _defaultBannedGroupIds; - public byte UniqueId { get; } public bool RequiresImatrix { get; } public ImmutableArray Names { get; } - // Legacy/runtime-compat list. Baseline candidate policy must use BaselineQuants.BannedGroupIds. - public List BannedGroups { get; } public ushort? BlockNeo { get; } public bool IsEligibleForBaseline { get; } @@ -19,7 +15,6 @@ private TensorWeightScheme( byte uniqueId, bool requiresImatrix, ImmutableArray names, - IEnumerable bannedGroups, ushort? blockNeo, bool isEligibleForBaseline = true) { @@ -29,29 +24,6 @@ private TensorWeightScheme( BlockNeo = blockNeo; IsEligibleForBaseline = isEligibleForBaseline; - var distinctGroups = bannedGroups - .GroupBy(x => x.UniqueId) - .Select(x => x.First()) - .ToList(); - - BannedGroups = distinctGroups; - _defaultBannedGroupIds = distinctGroups.Select(x => x.UniqueId).ToHashSet(); - } - - public void ResetRuntimeBans() - { - BannedGroups.Clear(); - foreach (var group in TReg.All.Where(x => _defaultBannedGroupIds.Contains(x.UniqueId))) - BannedGroups.Add(group); - } - - // Legacy helper; candidate/search policy should query BaselineQuants.BannedGroupIds instead. - public bool IsBannedFor(TensorGroup group) => BannedGroups.Any(x => x.UniqueId == group.UniqueId); - - public static void ResetAllRuntimeBans() - { - foreach (var scheme in All) - scheme.ResetRuntimeBans(); } public static void ValidateSmallestConfiguration() @@ -121,10 +93,10 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) public static TensorWeightScheme BF16_F16 => GetCurrentNativePrecisionScheme(); public static readonly TensorWeightScheme NULL = - new(0, false, ["NULL"], Array.Empty(), null, isEligibleForBaseline: false); + new(0, false, ["NULL"], null, isEligibleForBaseline: false); public static readonly TensorWeightScheme BF16 = - new(1, false, ["BF16", "BFLOAT16"], Array.Empty(), null, isEligibleForBaseline: false); + new(1, false, ["BF16", "BFLOAT16"], null, isEligibleForBaseline: false); /*public static readonly TensorWeightScheme MXFP4 = new( @@ -141,31 +113,25 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) isEligibleForBaseline: false);*/ public static readonly TensorWeightScheme Q8_0 = - new(3, false, ["Q8_0"], Array.Empty(), null); + new(3, false, ["Q8_0"], null); public static readonly TensorWeightScheme Q6_K = - new(4, false, ["Q6_K"], Array.Empty(), 256); + new(4, false, ["Q6_K"], 256); public static readonly TensorWeightScheme Q5_K = - new(5, false, ["Q5_K"], new[] { TReg.MoeRouter }, 256); + new(5, false, ["Q5_K"], 256); public static readonly TensorWeightScheme IQ4_XS = - new(6, false, ["IQ4_XS"], new[] { TReg.MoeRouter }, 32); + new(6, false, ["IQ4_XS"], 32); public static readonly TensorWeightScheme IQ4_NL = - new(7, false, ["IQ4_NL"], new[] { TReg.MoeRouter }, 32); + new(7, false, ["IQ4_NL"], 32); public static readonly TensorWeightScheme IQ3_S = new( 8, true, ["IQ3_S"], - new[] - { - TReg.Embeddings, - TReg.LmHead, - TReg.MoeRouter - }, 32 ); @@ -174,12 +140,6 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) 9, true, ["IQ3_XS"], - new[] - { - TReg.Embeddings, - TReg.LmHead, - TReg.MoeRouter - }, 32 ); @@ -188,12 +148,6 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) 10, true, ["IQ3_XXS"], - new[] - { - TReg.Embeddings, - TReg.LmHead, - TReg.MoeRouter - }, 32 ); @@ -202,13 +156,6 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) 11, true, ["IQ2_S"], - new[] - { - TReg.Embeddings, - TReg.LmHead, - TReg.MoeRouter, - TReg.MoeExperts - }, 32 ); @@ -217,13 +164,6 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) 12, true, ["IQ2_XS"], - new[] - { - TReg.Embeddings, - TReg.LmHead, - TReg.MoeRouter, - TReg.MoeExperts - }, 32 ); @@ -232,14 +172,6 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) 13, true, ["IQ2_XXS"], - new[] - { - TReg.Embeddings, - TReg.LmHead, - TReg.MoeRouter, - TReg.MoeExperts, - TReg.AttnKV - }, 32 ); @@ -248,15 +180,14 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) 14, false, ["Q4_K"], - new[] { TReg.MoeRouter }, 32 ); public static readonly TensorWeightScheme F16 = - new(15, false, ["F16", "FLOAT16", "FP16", "HALF"], Array.Empty(), null, isEligibleForBaseline: false); + new(15, false, ["F16", "FLOAT16", "FP16", "HALF"], null, isEligibleForBaseline: false); public static readonly TensorWeightScheme F32 = - new(16, false, ["F32", "FLOAT32", "FP32", "FLOAT"], Array.Empty(), null, isEligibleForBaseline: false); + new(16, false, ["F32", "FLOAT32", "FP32", "FLOAT"], null, isEligibleForBaseline: false); // This is the set used by hybrid search / combination generation. public static readonly ImmutableArray All_Allowed_Hybrid_Quants = diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 18c3412..a5d2538 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -29,7 +29,6 @@ public static void ResetForNewModel() Bf16SuppressedTensorChoiceGroupIds.Clear(); _imatrixAvailable = false; AllowHighPrecisionHybrids = false; - TensorWeightScheme.ResetAllRuntimeBans(); } public static void SetImatrixAvailability(bool available) => _imatrixAvailable = available; diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index 49a070d..74a9900 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -89,8 +89,6 @@ internal static void ApplyLearnedBaselinePruning( var effectiveSchemesByCandidateAndGroup = BuildEffectiveSchemesByBaselineAndGroup(learnedRows, aliasToSchemeIds); var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) - .Where(x => x.UniqueId != BaselineQuants.BF16_Hybrid.UniqueId) - .Where(x => x.UniqueId != BaselineQuants.F16_Hybrid.UniqueId) .OrderBy(x => x.UniqueId) .ToList(); diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index e211fa9..8baa837 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -26,7 +26,6 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) RuntimeSearchSpace.ResetForNewModel(); Cache.UnusedTensorGroups.Clear(); - TensorWeightScheme.NULL.BannedGroups.Clear(); string directory = Path.GetDirectoryName(ggufPath)!; string scriptPath = Path.Combine(directory, "check_compat.py"); @@ -87,9 +86,6 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) if (exists) { - if (!TensorWeightScheme.NULL.BannedGroups.Any(x => x.UniqueId == group.UniqueId)) - TensorWeightScheme.NULL.BannedGroups.Add(group); - usedCount++; continue; } From 7b2d040fda6181326483e183e0ba5c219cc816c4 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 20 Apr 2026 17:15:04 -0400 Subject: [PATCH 098/258] update to db --- .../20260416203656_InitialCreate.Designer.cs | 431 ------------------ MQ.DB/Migrations/20260417203830_newUpdate.cs | 61 --- ...0260420193000_AddImatrixContextIdentity.cs | 86 ---- ... 20260420210036_InitialCreate.Designer.cs} | 112 ++++- ...ate.cs => 20260420210036_InitialCreate.cs} | 133 +++++- .../MagicQuantContextModelSnapshot.cs | 89 +--- 6 files changed, 260 insertions(+), 652 deletions(-) delete mode 100644 MQ.DB/Migrations/20260416203656_InitialCreate.Designer.cs delete mode 100644 MQ.DB/Migrations/20260417203830_newUpdate.cs delete mode 100644 MQ.DB/Migrations/20260420193000_AddImatrixContextIdentity.cs rename MQ.DB/Migrations/{20260417203830_newUpdate.Designer.cs => 20260420210036_InitialCreate.Designer.cs} (80%) rename MQ.DB/Migrations/{20260416203656_InitialCreate.cs => 20260420210036_InitialCreate.cs} (70%) diff --git a/MQ.DB/Migrations/20260416203656_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260416203656_InitialCreate.Designer.cs deleted file mode 100644 index 5842b69..0000000 --- a/MQ.DB/Migrations/20260416203656_InitialCreate.Designer.cs +++ /dev/null @@ -1,431 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260416203656_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("BaselineName") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeId") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeName") - .IsUnique(); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260417203830_newUpdate.cs b/MQ.DB/Migrations/20260417203830_newUpdate.cs deleted file mode 100644 index 036c82b..0000000 --- a/MQ.DB/Migrations/20260417203830_newUpdate.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class newUpdate : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "ExecutionPlanProbeCaches", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - HardwareFingerprint = table.Column(type: "TEXT", maxLength: 1024, nullable: false), - QuantizedModelFingerprint = table.Column(type: "TEXT", maxLength: 2048, nullable: false), - QuantizationKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), - DiscoveryTokenTarget = table.Column(type: "INTEGER", nullable: false), - StaticNgl = table.Column(type: "INTEGER", nullable: false), - UsesGpu = table.Column(type: "INTEGER", nullable: false), - GroupSize = table.Column(type: "INTEGER", nullable: false), - SlotsJson = table.Column(type: "TEXT", maxLength: 8000, nullable: false), - CreatedUtc = table.Column(type: "TEXT", nullable: false), - UpdatedUtc = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_ExecutionPlanProbeCaches", x => x.Id); - table.ForeignKey( - name: "FK_ExecutionPlanProbeCaches_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_AiModelHashId", - table: "ExecutionPlanProbeCaches", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_AiModelHashId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", - table: "ExecutionPlanProbeCaches", - columns: new[] { "AiModelHashId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "ExecutionPlanProbeCaches"); - } - } -} diff --git a/MQ.DB/Migrations/20260420193000_AddImatrixContextIdentity.cs b/MQ.DB/Migrations/20260420193000_AddImatrixContextIdentity.cs deleted file mode 100644 index aadb05b..0000000 --- a/MQ.DB/Migrations/20260420193000_AddImatrixContextIdentity.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - public partial class AddImatrixContextIdentity : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "ImatrixDefinitions", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - IdentityHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), - CanonicalPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true), - SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), - CreatedUtc = table.Column(type: "TEXT", nullable: false), - MetadataJson = table.Column(type: "TEXT", maxLength: 8000, nullable: true), - TokenCount = table.Column(type: "INTEGER", nullable: true), - BuildFingerprint = table.Column(type: "TEXT", maxLength: 512, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_ImatrixDefinitions", x => x.Id); - table.ForeignKey( - name: "FK_ImatrixDefinitions_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.AddColumn(name: "ImatrixDefinitionId", table: "AiBenchmarks", type: "INTEGER", nullable: true); - migrationBuilder.AddColumn(name: "ImatrixDefinitionId", table: "QuantizationRuns", type: "INTEGER", nullable: true); - migrationBuilder.AddColumn(name: "ImatrixDefinitionId", table: "BenchmarkRuns", type: "INTEGER", nullable: true); - migrationBuilder.AddColumn(name: "ImatrixDefinitionId", table: "ExecutionPlanProbeCaches", type: "INTEGER", nullable: true); - - migrationBuilder.CreateIndex(name: "IX_ImatrixDefinitions_AiModelHashId_IdentityHash", table: "ImatrixDefinitions", columns: new[] { "AiModelHashId", "IdentityHash" }, unique: true); - migrationBuilder.CreateIndex(name: "IX_AiBenchmarks_ImatrixDefinitionId", table: "AiBenchmarks", column: "ImatrixDefinitionId"); - migrationBuilder.CreateIndex(name: "IX_QuantizationRuns_ImatrixDefinitionId", table: "QuantizationRuns", column: "ImatrixDefinitionId"); - migrationBuilder.CreateIndex(name: "IX_BenchmarkRuns_ImatrixDefinitionId", table: "BenchmarkRuns", column: "ImatrixDefinitionId"); - migrationBuilder.CreateIndex(name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", table: "ExecutionPlanProbeCaches", column: "ImatrixDefinitionId"); - - migrationBuilder.DropIndex(name: "IX_AiBenchmarks_AiModelHashId_TensorComboId", table: "AiBenchmarks"); - migrationBuilder.CreateIndex(name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", table: "AiBenchmarks", columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, unique: true); - - migrationBuilder.DropIndex(name: "IX_ExecutionPlanProbeCaches_AiModelHashId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches"); - migrationBuilder.CreateIndex(name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches", columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, unique: true); - - migrationBuilder.AddForeignKey(name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", table: "AiBenchmarks", column: "ImatrixDefinitionId", principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); - migrationBuilder.AddForeignKey(name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", table: "QuantizationRuns", column: "ImatrixDefinitionId", principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); - migrationBuilder.AddForeignKey(name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", table: "BenchmarkRuns", column: "ImatrixDefinitionId", principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); - migrationBuilder.AddForeignKey(name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", table: "ExecutionPlanProbeCaches", column: "ImatrixDefinitionId", principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropForeignKey(name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", table: "AiBenchmarks"); - migrationBuilder.DropForeignKey(name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", table: "QuantizationRuns"); - migrationBuilder.DropForeignKey(name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", table: "BenchmarkRuns"); - migrationBuilder.DropForeignKey(name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropTable(name: "ImatrixDefinitions"); - - migrationBuilder.DropIndex(name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", table: "AiBenchmarks"); - migrationBuilder.DropIndex(name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches"); - migrationBuilder.DropIndex(name: "IX_AiBenchmarks_ImatrixDefinitionId", table: "AiBenchmarks"); - migrationBuilder.DropIndex(name: "IX_QuantizationRuns_ImatrixDefinitionId", table: "QuantizationRuns"); - migrationBuilder.DropIndex(name: "IX_BenchmarkRuns_ImatrixDefinitionId", table: "BenchmarkRuns"); - migrationBuilder.DropIndex(name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn(name: "ImatrixDefinitionId", table: "AiBenchmarks"); - migrationBuilder.DropColumn(name: "ImatrixDefinitionId", table: "QuantizationRuns"); - migrationBuilder.DropColumn(name: "ImatrixDefinitionId", table: "BenchmarkRuns"); - migrationBuilder.DropColumn(name: "ImatrixDefinitionId", table: "ExecutionPlanProbeCaches"); - - migrationBuilder.CreateIndex(name: "IX_AiBenchmarks_AiModelHashId_TensorComboId", table: "AiBenchmarks", columns: new[] { "AiModelHashId", "TensorComboId" }, unique: true); - migrationBuilder.CreateIndex(name: "IX_ExecutionPlanProbeCaches_AiModelHashId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches", columns: new[] { "AiModelHashId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, unique: true); - } - } -} diff --git a/MQ.DB/Migrations/20260417203830_newUpdate.Designer.cs b/MQ.DB/Migrations/20260420210036_InitialCreate.Designer.cs similarity index 80% rename from MQ.DB/Migrations/20260417203830_newUpdate.Designer.cs rename to MQ.DB/Migrations/20260420210036_InitialCreate.Designer.cs index 6a2f725..0576ac8 100644 --- a/MQ.DB/Migrations/20260417203830_newUpdate.Designer.cs +++ b/MQ.DB/Migrations/20260420210036_InitialCreate.Designer.cs @@ -11,8 +11,8 @@ namespace MQ.DB.Migrations { [DbContext(typeof(MagicQuantContext))] - [Migration("20260417203830_newUpdate")] - partial class newUpdate + [Migration("20260420210036_InitialCreate")] + partial class InitialCreate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -28,6 +28,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("Ngl") .HasColumnType("INTEGER"); @@ -42,9 +45,11 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("ImatrixDefinitionId"); + b.HasIndex("TensorComboId"); - b.HasIndex("AiModelHashId", "TensorComboId") + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") .IsUnique(); b.ToTable("AiBenchmarks"); @@ -126,6 +131,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasMaxLength(4000) .HasColumnType("TEXT"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("StartedUtc") .HasColumnType("TEXT"); @@ -143,6 +151,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("CategoryBenchmarkId"); + b.HasIndex("ImatrixDefinitionId"); + b.HasIndex("StartedUtc"); b.HasIndex("TensorComboId"); @@ -201,6 +211,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasMaxLength(1024) .HasColumnType("TEXT"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("QuantizationKey") .IsRequired() .HasMaxLength(128) @@ -229,12 +242,59 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); - b.HasIndex("AiModelHashId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") .IsUnique(); b.ToTable("ExecutionPlanProbeCaches"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => { b.Property("Id") @@ -299,6 +359,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasMaxLength(4000) .HasColumnType("TEXT"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("OutputModelPath") .HasMaxLength(2048) .HasColumnType("TEXT"); @@ -318,6 +381,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); + b.HasIndex("ImatrixDefinitionId"); + b.HasIndex("StartedUtc"); b.HasIndex("TensorComboId"); @@ -376,6 +441,11 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") .WithMany() .HasForeignKey("TensorComboId") @@ -384,6 +454,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Navigation("AiModelHash"); + b.Navigation("ImatrixDefinition"); + b.Navigation("TensorCombo"); }); @@ -406,6 +478,11 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasForeignKey("CategoryBenchmarkId") .OnDelete(DeleteBehavior.SetNull); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") .WithMany() .HasForeignKey("TensorComboId") @@ -418,6 +495,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Navigation("CategoryBenchmark"); + b.Navigation("ImatrixDefinition"); + b.Navigation("TensorCombo"); }); @@ -433,6 +512,24 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) }); modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => { b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") .WithMany() @@ -475,6 +572,11 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") .WithMany() .HasForeignKey("TensorComboId") @@ -485,6 +587,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Navigation("AiModelHash"); + b.Navigation("ImatrixDefinition"); + b.Navigation("TensorCombo"); }); diff --git a/MQ.DB/Migrations/20260416203656_InitialCreate.cs b/MQ.DB/Migrations/20260420210036_InitialCreate.cs similarity index 70% rename from MQ.DB/Migrations/20260416203656_InitialCreate.cs rename to MQ.DB/Migrations/20260420210036_InitialCreate.cs index 6de12d9..d42307e 100644 --- a/MQ.DB/Migrations/20260416203656_InitialCreate.cs +++ b/MQ.DB/Migrations/20260420210036_InitialCreate.cs @@ -59,6 +59,32 @@ protected override void Up(MigrationBuilder migrationBuilder) table.PrimaryKey("PK_TensorCombos", x => x.Id); }); + migrationBuilder.CreateTable( + name: "ImatrixDefinitions", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + IdentityHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), + CanonicalPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true), + SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + MetadataJson = table.Column(type: "TEXT", maxLength: 8000, nullable: true), + TokenCount = table.Column(type: "INTEGER", nullable: true), + BuildFingerprint = table.Column(type: "TEXT", maxLength: 512, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ImatrixDefinitions", x => x.Id); + table.ForeignKey( + name: "FK_ImatrixDefinitions_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "AiBenchmarks", columns: table => new @@ -68,7 +94,8 @@ protected override void Up(MigrationBuilder migrationBuilder) SizeBytes = table.Column(type: "INTEGER", nullable: false), TokensPerSecond = table.Column(type: "REAL", nullable: false), TensorComboId = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false) + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true) }, constraints: table => { @@ -79,6 +106,12 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "AiModelHashes", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_AiBenchmarks_TensorCombos_TensorComboId", column: x => x.TensorComboId, @@ -87,6 +120,41 @@ protected override void Up(MigrationBuilder migrationBuilder) onDelete: ReferentialAction.Restrict); }); + migrationBuilder.CreateTable( + name: "ExecutionPlanProbeCaches", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + HardwareFingerprint = table.Column(type: "TEXT", maxLength: 1024, nullable: false), + QuantizedModelFingerprint = table.Column(type: "TEXT", maxLength: 2048, nullable: false), + QuantizationKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), + DiscoveryTokenTarget = table.Column(type: "INTEGER", nullable: false), + StaticNgl = table.Column(type: "INTEGER", nullable: false), + UsesGpu = table.Column(type: "INTEGER", nullable: false), + GroupSize = table.Column(type: "INTEGER", nullable: false), + SlotsJson = table.Column(type: "TEXT", maxLength: 8000, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + UpdatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ExecutionPlanProbeCaches", x => x.Id); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + migrationBuilder.CreateTable( name: "CategoryBenchmark", columns: table => new @@ -145,6 +213,7 @@ protected override void Up(MigrationBuilder migrationBuilder) { Id = table.Column(type: "TEXT", nullable: false), AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), TensorComboId = table.Column(type: "TEXT", nullable: false), AiBenchmarkId = table.Column(type: "TEXT", nullable: true), StartedUtc = table.Column(type: "TEXT", nullable: false), @@ -169,6 +238,12 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "AiModelHashes", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_QuantizationRuns_TensorCombos_TensorComboId", column: x => x.TensorComboId, @@ -183,6 +258,7 @@ protected override void Up(MigrationBuilder migrationBuilder) { Id = table.Column(type: "TEXT", nullable: false), AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), TensorComboId = table.Column(type: "TEXT", nullable: false), AiBenchmarkId = table.Column(type: "TEXT", nullable: false), CategoryBenchmarkId = table.Column(type: "TEXT", nullable: true), @@ -214,6 +290,12 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "CategoryBenchmark", principalColumn: "Id", onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_BenchmarkRuns_TensorCombos_TensorComboId", column: x => x.TensorComboId, @@ -223,11 +305,16 @@ protected override void Up(MigrationBuilder migrationBuilder) }); migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_AiModelHashId_TensorComboId", + name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", table: "AiBenchmarks", - columns: new[] { "AiModelHashId", "TensorComboId" }, + columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, unique: true); + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_ImatrixDefinitionId", + table: "AiBenchmarks", + column: "ImatrixDefinitionId"); + migrationBuilder.CreateIndex( name: "IX_AiBenchmarks_TensorComboId", table: "AiBenchmarks", @@ -276,6 +363,11 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "BenchmarkRuns", column: "CategoryBenchmarkId"); + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_ImatrixDefinitionId", + table: "BenchmarkRuns", + column: "ImatrixDefinitionId"); + migrationBuilder.CreateIndex( name: "IX_BenchmarkRuns_StartedUtc", table: "BenchmarkRuns", @@ -291,6 +383,28 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "CategoryBenchmark", column: "AiBenchmarkId"); + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_AiModelHashId", + table: "ExecutionPlanProbeCaches", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", + table: "ExecutionPlanProbeCaches", + columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", + table: "ExecutionPlanProbeCaches", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_ImatrixDefinitions_AiModelHashId_IdentityHash", + table: "ImatrixDefinitions", + columns: new[] { "AiModelHashId", "IdentityHash" }, + unique: true); + migrationBuilder.CreateIndex( name: "IX_LearnedBaselineTensorQuants_AiBenchmarkId", table: "LearnedBaselineTensorQuants", @@ -317,6 +431,11 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "QuantizationRuns", column: "AiModelHashId"); + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_ImatrixDefinitionId", + table: "QuantizationRuns", + column: "ImatrixDefinitionId"); + migrationBuilder.CreateIndex( name: "IX_QuantizationRuns_StartedUtc", table: "QuantizationRuns", @@ -343,6 +462,9 @@ protected override void Down(MigrationBuilder migrationBuilder) migrationBuilder.DropTable( name: "BenchmarkRuns"); + migrationBuilder.DropTable( + name: "ExecutionPlanProbeCaches"); + migrationBuilder.DropTable( name: "LearnedBaselineTensorQuants"); @@ -356,10 +478,13 @@ protected override void Down(MigrationBuilder migrationBuilder) name: "AiBenchmarks"); migrationBuilder.DropTable( - name: "AiModelHashes"); + name: "ImatrixDefinitions"); migrationBuilder.DropTable( name: "TensorCombos"); + + migrationBuilder.DropTable( + name: "AiModelHashes"); } } } diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index a0521f8..505af7a 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -42,13 +42,13 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("ImatrixDefinitionId"); + b.HasIndex("TensorComboId"); b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") .IsUnique(); - b.HasIndex("ImatrixDefinitionId"); - b.ToTable("AiBenchmarks"); }); @@ -112,9 +112,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - b.Property("Category") .HasColumnType("INTEGER"); @@ -131,6 +128,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(4000) .HasColumnType("TEXT"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("StartedUtc") .HasColumnType("TEXT"); @@ -146,10 +146,10 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); - b.HasIndex("ImatrixDefinitionId"); - b.HasIndex("CategoryBenchmarkId"); + b.HasIndex("ImatrixDefinitionId"); + b.HasIndex("StartedUtc"); b.HasIndex("TensorComboId"); @@ -197,9 +197,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("CreatedUtc") .HasColumnType("TEXT"); - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - b.Property("DiscoveryTokenTarget") .HasColumnType("INTEGER"); @@ -211,6 +208,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(1024) .HasColumnType("TEXT"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("QuantizationKey") .IsRequired() .HasMaxLength(128) @@ -239,15 +239,14 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); + b.HasIndex("ImatrixDefinitionId"); + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") .IsUnique(); - b.HasIndex("ImatrixDefinitionId"); - b.ToTable("ExecutionPlanProbeCaches"); }); - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => { b.Property("Id") @@ -347,9 +346,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - b.Property("CompletedUtc") .HasColumnType("TEXT"); @@ -360,6 +356,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(4000) .HasColumnType("TEXT"); + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + b.Property("OutputModelPath") .HasMaxLength(2048) .HasColumnType("TEXT"); @@ -471,16 +470,16 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") .WithMany() .HasForeignKey("CategoryBenchmarkId") .OnDelete(DeleteBehavior.SetNull); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") .WithMany() .HasForeignKey("TensorComboId") @@ -493,6 +492,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("CategoryBenchmark"); + b.Navigation("ImatrixDefinition"); + b.Navigation("TensorCombo"); }); @@ -521,52 +522,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Restrict); b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BuildFingerprint") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("CanonicalPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IdentityHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("MetadataJson") - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("TokenCount") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId", "IdentityHash") - .IsUnique(); - - b.ToTable("ImatrixDefinitions"); + b.Navigation("ImatrixDefinition"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => From f73c5dc95cbeb510e891052657ac9d1014b0c761 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 20 Apr 2026 17:15:54 -0400 Subject: [PATCH 099/258] removing tests. --- MagicQuant-Pipeline.sln | 6 ------ 1 file changed, 6 deletions(-) diff --git a/MagicQuant-Pipeline.sln b/MagicQuant-Pipeline.sln index cb4635f..a933660 100644 --- a/MagicQuant-Pipeline.sln +++ b/MagicQuant-Pipeline.sln @@ -4,8 +4,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant", "MagicQuant\Ma EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQ.DB", "MQ.DB\MQ.DB.csproj", "{A97D6992-2659-47F9-9AC9-99425D2677A4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.Tests", "MagicQuant.Tests\MagicQuant.Tests.csproj", "{C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -20,9 +18,5 @@ Global {A97D6992-2659-47F9-9AC9-99425D2677A4}.Debug|Any CPU.Build.0 = Debug|Any CPU {A97D6992-2659-47F9-9AC9-99425D2677A4}.Release|Any CPU.ActiveCfg = Release|Any CPU {A97D6992-2659-47F9-9AC9-99425D2677A4}.Release|Any CPU.Build.0 = Release|Any CPU - {C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C988338A-EA4C-48A2-8D33-C98AE9AAF2ED}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From 196bff9ada81e0a74009fbbccce80ec054a37c22 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 20 Apr 2026 19:37:09 -0400 Subject: [PATCH 100/258] not perfect but getting this back on track --- .../20260420210036_InitialCreate.Designer.cs | 602 ------------------ .../20260420210036_InitialCreate.cs | 490 -------------- .../MagicQuantContextModelSnapshot.cs | 599 ----------------- MQ.DB/Models/BaselineQuants.cs | 311 ++++++--- MQ.DB/Models/HybridQuant.cs | 166 ++++- MQ.DB/Models/TensorConfigs.cs | 38 +- MagicQuant/Commands/Evolution.cs | 25 +- MagicQuant/Helpers/ComboLogic.cs | 32 +- MagicQuant/Helpers/RuntimeSearchSpace.cs | 42 +- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 16 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 64 +- .../Services/IsolationOptimizationService.cs | 12 +- .../Services/LearnedBaselinePruningService.cs | 11 +- MagicQuant/Services/QuantizationService.cs | 162 +++-- 14 files changed, 614 insertions(+), 1956 deletions(-) delete mode 100644 MQ.DB/Migrations/20260420210036_InitialCreate.Designer.cs delete mode 100644 MQ.DB/Migrations/20260420210036_InitialCreate.cs delete mode 100644 MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs diff --git a/MQ.DB/Migrations/20260420210036_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260420210036_InitialCreate.Designer.cs deleted file mode 100644 index 0576ac8..0000000 --- a/MQ.DB/Migrations/20260420210036_InitialCreate.Designer.cs +++ /dev/null @@ -1,602 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260420210036_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("BaselineName") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeId") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeName") - .IsUnique(); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DiscoveryTokenTarget") - .HasColumnType("INTEGER"); - - b.Property("GroupSize") - .HasColumnType("INTEGER"); - - b.Property("HardwareFingerprint") - .IsRequired() - .HasMaxLength(1024) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("QuantizationKey") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("QuantizedModelFingerprint") - .IsRequired() - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("SlotsJson") - .IsRequired() - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("StaticNgl") - .HasColumnType("INTEGER"); - - b.Property("UpdatedUtc") - .HasColumnType("TEXT"); - - b.Property("UsesGpu") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") - .IsUnique(); - - b.ToTable("ExecutionPlanProbeCaches"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BuildFingerprint") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("CanonicalPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IdentityHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("MetadataJson") - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("TokenCount") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId", "IdentityHash") - .IsUnique(); - - b.ToTable("ImatrixDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260420210036_InitialCreate.cs b/MQ.DB/Migrations/20260420210036_InitialCreate.cs deleted file mode 100644 index d42307e..0000000 --- a/MQ.DB/Migrations/20260420210036_InitialCreate.cs +++ /dev/null @@ -1,490 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class InitialCreate : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "AiModelHashes", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - UniqueHash = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AiModelHashes", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "BaselineQuantDefinitions", - columns: table => new - { - BaselineQuantId = table.Column(type: "INTEGER", nullable: false), - BaselineName = table.Column(type: "TEXT", maxLength: 64, nullable: false), - DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), - DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.BaselineQuantId); - }); - - migrationBuilder.CreateTable( - name: "TensorCombos", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AttnKV = table.Column(type: "INTEGER", nullable: false), - AttnOutput = table.Column(type: "INTEGER", nullable: false), - AttnQ = table.Column(type: "INTEGER", nullable: false), - BaseQuant = table.Column(type: "INTEGER", nullable: false), - Embeddings = table.Column(type: "INTEGER", nullable: false), - FfnDown = table.Column(type: "INTEGER", nullable: false), - FfnUpGate = table.Column(type: "INTEGER", nullable: false), - LmHead = table.Column(type: "INTEGER", nullable: false), - MoeExperts = table.Column(type: "INTEGER", nullable: false), - MoeRouter = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_TensorCombos", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "ImatrixDefinitions", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - IdentityHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), - CanonicalPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true), - SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), - CreatedUtc = table.Column(type: "TEXT", nullable: false), - MetadataJson = table.Column(type: "TEXT", maxLength: 8000, nullable: true), - TokenCount = table.Column(type: "INTEGER", nullable: true), - BuildFingerprint = table.Column(type: "TEXT", maxLength: 512, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_ImatrixDefinitions", x => x.Id); - table.ForeignKey( - name: "FK_ImatrixDefinitions_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AiBenchmarks", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - Ngl = table.Column(type: "INTEGER", nullable: false), - SizeBytes = table.Column(type: "INTEGER", nullable: false), - TokensPerSecond = table.Column(type: "REAL", nullable: false), - TensorComboId = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AiBenchmarks", x => x.Id); - table.ForeignKey( - name: "FK_AiBenchmarks_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", - column: x => x.ImatrixDefinitionId, - principalTable: "ImatrixDefinitions", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_AiBenchmarks_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "ExecutionPlanProbeCaches", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), - HardwareFingerprint = table.Column(type: "TEXT", maxLength: 1024, nullable: false), - QuantizedModelFingerprint = table.Column(type: "TEXT", maxLength: 2048, nullable: false), - QuantizationKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), - DiscoveryTokenTarget = table.Column(type: "INTEGER", nullable: false), - StaticNgl = table.Column(type: "INTEGER", nullable: false), - UsesGpu = table.Column(type: "INTEGER", nullable: false), - GroupSize = table.Column(type: "INTEGER", nullable: false), - SlotsJson = table.Column(type: "TEXT", maxLength: 8000, nullable: false), - CreatedUtc = table.Column(type: "TEXT", nullable: false), - UpdatedUtc = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_ExecutionPlanProbeCaches", x => x.Id); - table.ForeignKey( - name: "FK_ExecutionPlanProbeCaches_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", - column: x => x.ImatrixDefinitionId, - principalTable: "ImatrixDefinitions", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "CategoryBenchmark", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiBenchmarkId = table.Column(type: "TEXT", nullable: false), - Category = table.Column(type: "INTEGER", nullable: false), - Kld = table.Column(type: "REAL", nullable: false), - Ppl = table.Column(type: "REAL", nullable: false), - PplError = table.Column(type: "REAL", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_CategoryBenchmark", x => x.Id); - table.ForeignKey( - name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "LearnedBaselineTensorQuants", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiBenchmarkId = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - BaselineQuantId = table.Column(type: "INTEGER", nullable: false), - TensorWeightSchemeId = table.Column(type: "INTEGER", nullable: false), - TensorGroupId = table.Column(type: "INTEGER", nullable: false), - TensorName = table.Column(type: "TEXT", maxLength: 512, nullable: false), - FinalQuantType = table.Column(type: "TEXT", maxLength: 32, nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_LearnedBaselineTensorQuants", x => x.Id); - table.ForeignKey( - name: "FK_LearnedBaselineTensorQuants_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_LearnedBaselineTensorQuants_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "QuantizationRuns", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), - TensorComboId = table.Column(type: "TEXT", nullable: false), - AiBenchmarkId = table.Column(type: "TEXT", nullable: true), - StartedUtc = table.Column(type: "TEXT", nullable: false), - CompletedUtc = table.Column(type: "TEXT", nullable: false), - DurationMs = table.Column(type: "INTEGER", nullable: false), - Succeeded = table.Column(type: "INTEGER", nullable: false), - Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true), - OutputModelPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_QuantizationRuns", x => x.Id); - table.ForeignKey( - name: "FK_QuantizationRuns_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.SetNull); - table.ForeignKey( - name: "FK_QuantizationRuns_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", - column: x => x.ImatrixDefinitionId, - principalTable: "ImatrixDefinitions", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_QuantizationRuns_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "BenchmarkRuns", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), - TensorComboId = table.Column(type: "TEXT", nullable: false), - AiBenchmarkId = table.Column(type: "TEXT", nullable: false), - CategoryBenchmarkId = table.Column(type: "TEXT", nullable: true), - Category = table.Column(type: "INTEGER", nullable: false), - StartedUtc = table.Column(type: "TEXT", nullable: false), - CompletedUtc = table.Column(type: "TEXT", nullable: false), - DurationMs = table.Column(type: "INTEGER", nullable: false), - Succeeded = table.Column(type: "INTEGER", nullable: false), - Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_BenchmarkRuns", x => x.Id); - table.ForeignKey( - name: "FK_BenchmarkRuns_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_BenchmarkRuns_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_BenchmarkRuns_CategoryBenchmark_CategoryBenchmarkId", - column: x => x.CategoryBenchmarkId, - principalTable: "CategoryBenchmark", - principalColumn: "Id", - onDelete: ReferentialAction.SetNull); - table.ForeignKey( - name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", - column: x => x.ImatrixDefinitionId, - principalTable: "ImatrixDefinitions", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_BenchmarkRuns_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", - table: "AiBenchmarks", - columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_ImatrixDefinitionId", - table: "AiBenchmarks", - column: "ImatrixDefinitionId"); - - migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_TensorComboId", - table: "AiBenchmarks", - column: "TensorComboId"); - - migrationBuilder.CreateIndex( - name: "IX_AiModelHashes_UniqueHash", - table: "AiModelHashes", - column: "UniqueHash"); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_BaselineName", - table: "BaselineQuantDefinitions", - column: "BaselineName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeId", - table: "BaselineQuantDefinitions", - column: "DefaultTensorSchemeId", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeName", - table: "BaselineQuantDefinitions", - column: "DefaultTensorSchemeName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiBenchmarkId", - table: "BenchmarkRuns", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiBenchmarkId_Category", - table: "BenchmarkRuns", - columns: new[] { "AiBenchmarkId", "Category" }); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiModelHashId", - table: "BenchmarkRuns", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_CategoryBenchmarkId", - table: "BenchmarkRuns", - column: "CategoryBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_ImatrixDefinitionId", - table: "BenchmarkRuns", - column: "ImatrixDefinitionId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_StartedUtc", - table: "BenchmarkRuns", - column: "StartedUtc"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_TensorComboId", - table: "BenchmarkRuns", - column: "TensorComboId"); - - migrationBuilder.CreateIndex( - name: "IX_CategoryBenchmark_AiBenchmarkId", - table: "CategoryBenchmark", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_AiModelHashId", - table: "ExecutionPlanProbeCaches", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", - table: "ExecutionPlanProbeCaches", - columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", - table: "ExecutionPlanProbeCaches", - column: "ImatrixDefinitionId"); - - migrationBuilder.CreateIndex( - name: "IX_ImatrixDefinitions_AiModelHashId_IdentityHash", - table: "ImatrixDefinitions", - columns: new[] { "AiModelHashId", "IdentityHash" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiBenchmarkId", - table: "LearnedBaselineTensorQuants", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorGroupId", - table: "LearnedBaselineTensorQuants", - columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId" }); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorName", - table: "LearnedBaselineTensorQuants", - columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_AiBenchmarkId", - table: "QuantizationRuns", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_AiModelHashId", - table: "QuantizationRuns", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_ImatrixDefinitionId", - table: "QuantizationRuns", - column: "ImatrixDefinitionId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_StartedUtc", - table: "QuantizationRuns", - column: "StartedUtc"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_TensorComboId", - table: "QuantizationRuns", - column: "TensorComboId"); - - migrationBuilder.CreateIndex( - name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", - table: "TensorCombos", - columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "BaselineQuantDefinitions"); - - migrationBuilder.DropTable( - name: "BenchmarkRuns"); - - migrationBuilder.DropTable( - name: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropTable( - name: "LearnedBaselineTensorQuants"); - - migrationBuilder.DropTable( - name: "QuantizationRuns"); - - migrationBuilder.DropTable( - name: "CategoryBenchmark"); - - migrationBuilder.DropTable( - name: "AiBenchmarks"); - - migrationBuilder.DropTable( - name: "ImatrixDefinitions"); - - migrationBuilder.DropTable( - name: "TensorCombos"); - - migrationBuilder.DropTable( - name: "AiModelHashes"); - } - } -} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs deleted file mode 100644 index 505af7a..0000000 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ /dev/null @@ -1,599 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - partial class MagicQuantContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("BaselineName") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeId") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeName") - .IsUnique(); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DiscoveryTokenTarget") - .HasColumnType("INTEGER"); - - b.Property("GroupSize") - .HasColumnType("INTEGER"); - - b.Property("HardwareFingerprint") - .IsRequired() - .HasMaxLength(1024) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("QuantizationKey") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("QuantizedModelFingerprint") - .IsRequired() - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("SlotsJson") - .IsRequired() - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("StaticNgl") - .HasColumnType("INTEGER"); - - b.Property("UpdatedUtc") - .HasColumnType("TEXT"); - - b.Property("UsesGpu") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") - .IsUnique(); - - b.ToTable("ExecutionPlanProbeCaches"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BuildFingerprint") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("CanonicalPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IdentityHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("MetadataJson") - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("TokenCount") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId", "IdentityHash") - .IsUnique(); - - b.ToTable("ImatrixDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index c417dba..ca4456a 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -10,115 +10,158 @@ public record BaselineQuants( TensorWeightScheme PrimaryTensorWeightScheme, ImmutableArray LearnedMatchTensorWeightSchemes, ImmutableArray BannedGroupIds, - bool IsPureBaselineCandidate, + bool IsLearningBaseline, bool IsCombinationCarrierCandidate, bool IsExplicitGroupCombinationCandidate, - bool IsHighPrecisionExplicitCandidate) + bool IsHighPrecisionExactAlias, + int ExplicitCandidateSortOrder = int.MaxValue) { public const byte NativeSourceUniqueId = 250; public TensorWeightScheme? DefaultTensorScheme => PrimaryTensorWeightScheme; - // Legacy alias retained for compatibility with existing call sites. + // Compatibility aliases retained for older call sites. public ImmutableArray TensorWeightSchemes => LearnedMatchTensorWeightSchemes; + public bool IsPureBaselineCandidate => IsLearningBaseline; + public bool IsHighPrecisionExplicitCandidate => IsHighPrecisionExactAlias; + + private static BaselineQuants Create( + byte uniqueId, + bool requiresImatrix, + string name, + TensorWeightScheme primaryTensorWeightScheme, + ImmutableArray learnedMatchTensorWeightSchemes, + ImmutableArray bannedGroupIds, + bool isLearningBaseline, + bool isCombinationCarrierCandidate, + bool isExplicitGroupCombinationCandidate, + bool isHighPrecisionExactAlias, + int explicitCandidateSortOrder = int.MaxValue) + { + return new BaselineQuants( + uniqueId, + requiresImatrix, + [name], + primaryTensorWeightScheme, + learnedMatchTensorWeightSchemes, + bannedGroupIds, + isLearningBaseline, + isCombinationCarrierCandidate, + isExplicitGroupCombinationCandidate, + isHighPrecisionExactAlias, + explicitCandidateSortOrder); + } public static readonly BaselineQuants Q8_0 = - new(0, false, ["Q8_0"], TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: true, - IsExplicitGroupCombinationCandidate: true, - IsHighPrecisionExplicitCandidate: false); + Create(0, false, "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], + isLearningBaseline: true, + isCombinationCarrierCandidate: true, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 11); public static readonly BaselineQuants Q6_K = - new(1, false, ["Q6_K"], TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: true, - IsExplicitGroupCombinationCandidate: true, - IsHighPrecisionExplicitCandidate: false); + Create(1, false, "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], + isLearningBaseline: true, + isCombinationCarrierCandidate: true, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 10); public static readonly BaselineQuants Q5_K = - new(2, false, ["Q5_K"], TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: true, - IsExplicitGroupCombinationCandidate: true, - IsHighPrecisionExplicitCandidate: false); + Create(2, false, "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: true, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 9); public static readonly BaselineQuants Q4_K_M = - new(3, false, ["Q4_K_M"], TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: true, - IsExplicitGroupCombinationCandidate: true, - IsHighPrecisionExplicitCandidate: false); + Create(3, false, "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: true, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 8); public static readonly BaselineQuants IQ4_NL = - new(5, false, ["IQ4_NL"], TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: true, - IsExplicitGroupCombinationCandidate: true, - IsHighPrecisionExplicitCandidate: false); + Create(5, false, "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: true, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 7); public static readonly BaselineQuants IQ4_XS = - new(6, false, ["IQ4_XS"], TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], - IsPureBaselineCandidate: true, - IsCombinationCarrierCandidate: true, - IsExplicitGroupCombinationCandidate: true, - IsHighPrecisionExplicitCandidate: false); + Create(6, false, "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: true, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 6); public static readonly BaselineQuants IQ3_S = - new(7, true, ["IQ3_S"], TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: false); + Create(7, true, "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: false, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 5); public static readonly BaselineQuants IQ3_XS = - new(8, true, ["IQ3_XS"], TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: false); + Create(8, true, "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: false, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 4); public static readonly BaselineQuants IQ3_XXS = - new(9, true, ["IQ3_XXS"], TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: false); + Create(9, true, "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: false, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 3); public static readonly BaselineQuants IQ2_S = - new(10, true, ["IQ2_S"], TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: false); + Create(10, true, "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: false, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 2); public static readonly BaselineQuants IQ2_XS = - new(11, true, ["IQ2_XS"], TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: false); + Create(11, true, "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: false, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 1); public static readonly BaselineQuants IQ2_XXS = - new(12, true, ["IQ2_XXS"], TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: false); - + Create(12, true, "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], + isLearningBaseline: true, + isCombinationCarrierCandidate: false, + isExplicitGroupCombinationCandidate: true, + isHighPrecisionExactAlias: false, + explicitCandidateSortOrder: 0); + + // These are exact/native override aliases. They are NOT learned baseline identities. public static readonly BaselineQuants BF16_Hybrid = - new(201, false, ["BF16"], TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: true); + Create(201, false, "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], + isLearningBaseline: false, + isCombinationCarrierCandidate: false, + isExplicitGroupCombinationCandidate: false, + isHighPrecisionExactAlias: true); public static readonly BaselineQuants F16_Hybrid = - new(202, false, ["F16"], TensorWeightScheme.F16, [TensorWeightScheme.F16], [], - IsPureBaselineCandidate: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: true); + Create(202, false, "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], + isLearningBaseline: false, + isCombinationCarrierCandidate: false, + isExplicitGroupCombinationCandidate: false, + isHighPrecisionExactAlias: true); public static readonly ImmutableArray All = [ @@ -142,31 +185,34 @@ public static BaselineQuants GetNativeQuant() { var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - return new( + return new BaselineQuants( NativeSourceUniqueId, false, [nativeScheme.Names[0]], nativeScheme, [nativeScheme], [], - IsPureBaselineCandidate: false, + IsLearningBaseline: false, IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExplicitCandidate: true); + IsHighPrecisionExactAlias: true, + ExplicitCandidateSortOrder: int.MaxValue); } - // Compatibility alias for older code paths. public static BaselineQuants GetBF16Quant() => GetNativeQuant(); public static IReadOnlyList GetAllRecognizedBaselines() => All.OrderBy(x => x.UniqueId).ToList(); - public static IReadOnlyList GetPureBaselineCandidates(bool hasUsableImatrix) => - All.Where(x => x.IsPureBaselineCandidate) + public static IReadOnlyList GetLearningBaselines(bool hasUsableImatrix) => + All.Where(x => x.IsLearningBaseline) .Where(x => hasUsableImatrix || !x.RequiresImatrix) .OrderBy(x => x.UniqueId) .ToList(); + public static IReadOnlyList GetPureBaselineCandidates(bool hasUsableImatrix) => + GetLearningBaselines(hasUsableImatrix); + public static IReadOnlyList GetCombinationCarrierBaselines(bool hasUsableImatrix) => All.Where(x => x.IsCombinationCarrierCandidate) .Where(x => hasUsableImatrix || !x.RequiresImatrix) @@ -176,12 +222,101 @@ public static IReadOnlyList GetCombinationCarrierBaselines(bool public static IReadOnlyList GetGroupCombinationCandidates(bool hasUsableImatrix, bool allowHighPrecisionHybrids) => All.Where(x => x.IsExplicitGroupCombinationCandidate) .Where(x => hasUsableImatrix || !x.RequiresImatrix) - .Where(x => allowHighPrecisionHybrids || !x.IsHighPrecisionExplicitCandidate) + .OrderBy(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId) + .ToList(); + + public static IReadOnlyList GetGroupCombinationCandidatesSmallestFirst(bool hasUsableImatrix, bool allowHighPrecisionHybrids) => + GetGroupCombinationCandidates(hasUsableImatrix, allowHighPrecisionHybrids) + .OrderBy(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId) + .ToList(); + + public static IReadOnlyList GetExactHighPrecisionAliases(bool allowHighPrecisionHybrids) + { + if (!allowHighPrecisionHybrids) + return Array.Empty(); + + return All.Where(x => x.IsHighPrecisionExactAlias) .OrderBy(x => x.UniqueId) .ToList(); + } + + public const byte TensorConfigNullSlotValue = 0; public static BaselineQuants GetDefaultExplicitFallbackBaseline() => Q8_0; + public static bool IsNullTensorConfigGroupSlot(byte storedValue) => storedValue == TensorConfigNullSlotValue; + + public static byte EncodeTensorConfigGroupSlot(BaselineQuants baseline) => + EncodeTensorConfigGroupSlotBaselineId(baseline.UniqueId); + + public static byte EncodeTensorConfigGroupSlot(TensorWeightScheme exactScheme) => + EncodeTensorConfigGroupSlotBaselineId(GetExactOverrideStorageId(exactScheme)); + + public static byte EncodeTensorConfigGroupSlotBaselineId(byte baselineId) + { + if (baselineId == byte.MaxValue) + throw new InvalidOperationException("Baseline id 255 cannot be encoded into a tensor-config group slot."); + + return checked((byte)(baselineId + 1)); + } + + public static byte DecodeTensorConfigGroupSlotToBaselineId(byte storedValue) + { + if (IsNullTensorConfigGroupSlot(storedValue)) + throw new InvalidOperationException("Tensor-config group slot 0 represents NULL and cannot be decoded as a baseline id."); + + return checked((byte)(storedValue - 1)); + } + + public static BaselineQuants DecodeTensorConfigGroupSlotToBaseline(byte storedValue) => + FromId(DecodeTensorConfigGroupSlotToBaselineId(storedValue)); + + public static bool IsNativeExactAlias(BaselineQuants baseline) => IsNativeExactAlias(baseline.UniqueId); + + public static bool IsNativeExactAlias(byte baselineId) + { + return baselineId == NativeSourceUniqueId || + baselineId == BF16_Hybrid.UniqueId || + baselineId == F16_Hybrid.UniqueId; + } + + public static byte CanonicalLearningBaselineId(BaselineQuants baseline) => CanonicalLearningBaselineId(baseline.UniqueId); + + public static byte CanonicalLearningBaselineId(byte baselineId) + { + return IsNativeExactAlias(baselineId) + ? NativeSourceUniqueId + : baselineId; + } + + public static byte GetExactOverrideStorageId(TensorWeightScheme scheme) + { + if (scheme.UniqueId == TensorWeightScheme.BF16.UniqueId) + return BF16_Hybrid.UniqueId; + + if (scheme.UniqueId == TensorWeightScheme.F16.UniqueId) + return F16_Hybrid.UniqueId; + + if (scheme.UniqueId == TensorWeightScheme.GetCurrentNativePrecisionScheme().UniqueId) + return NativeSourceUniqueId; + + throw new InvalidOperationException( + $"Tensor scheme '{scheme.Names[0]}' does not have a supported exact-override storage baseline id."); + } + + public static TensorWeightScheme ResolveExactOverrideScheme(byte baselineId) + { + return baselineId switch + { + NativeSourceUniqueId => TensorWeightScheme.GetCurrentNativePrecisionScheme(), + 201 => TensorWeightScheme.BF16, + 202 => TensorWeightScheme.F16, + _ => throw new InvalidOperationException($"Baseline id '{baselineId}' is not an exact override alias.") + }; + } + public static void ValidateIntegrityOrThrow() { var invalidBaselines = All @@ -197,10 +332,10 @@ public static void ValidateIntegrityOrThrow() } var duplicateSchemeIds = All + .Where(x => !x.IsHighPrecisionExactAlias) .SelectMany(x => x.LearnedMatchTensorWeightSchemes.Select(s => new { Baseline = x, Scheme = s })) .GroupBy(x => x.Scheme.UniqueId) .Where(g => g.Count() > 1) - .Where(g => g.Key != TensorWeightScheme.BF16.UniqueId && g.Key != TensorWeightScheme.F16.UniqueId) .Select(g => g.Key) .ToList(); @@ -210,7 +345,7 @@ public static void ValidateIntegrityOrThrow() .Select(id => TensorWeightScheme.All.First(s => s.UniqueId == id).Names[0]); throw new InvalidOperationException( - "TensorWeightScheme associations must be unique across BaselineQuants entries. Duplicates: " + + "TensorWeightScheme associations must be unique across learned BaselineQuants entries. Duplicates: " + string.Join(", ", duplicateNames)); } } @@ -229,6 +364,16 @@ public static BaselineQuants FromId(byte id) public static BaselineQuants FromTensorSchemeId(byte schemeId) { + if (schemeId == TensorWeightScheme.BF16.UniqueId) + return BF16_Hybrid; + + if (schemeId == TensorWeightScheme.F16.UniqueId) + return F16_Hybrid; + + if (schemeId == TensorWeightScheme.GetCurrentNativePrecisionScheme().UniqueId && + schemeId == TensorWeightScheme.F32.UniqueId) + return GetNativeQuant(); + var found = All.FirstOrDefault(x => x.PrimaryTensorWeightScheme.UniqueId == schemeId || x.LearnedMatchTensorWeightSchemes.Any(s => s.UniqueId == schemeId)); diff --git a/MQ.DB/Models/HybridQuant.cs b/MQ.DB/Models/HybridQuant.cs index dd0a014..b9d4789 100644 --- a/MQ.DB/Models/HybridQuant.cs +++ b/MQ.DB/Models/HybridQuant.cs @@ -1,5 +1,11 @@ namespace MQ.DB.Models; +public enum HybridTensorOverrideMode +{ + LearnedBaselineCandidate = 1, + ExactTensorScheme = 2 +} + public class HybridQuant { public BaselineQuants BaseQuant { get; set; } = default!; @@ -22,19 +28,22 @@ public HybridQuant(TensorConfig c) AddIfNotNull(TReg.MoeRouter, c.MoeRouter); } - private void AddIfNotNull(TensorGroup group, byte candidateId) + private void AddIfNotNull(TensorGroup group, byte storedId) { - if (candidateId == TensorWeightScheme.NULL.UniqueId) + if (BaselineQuants.IsNullTensorConfigGroupSlot(storedId)) return; - var candidate = BaselineQuants.FromId(candidateId); + var decodedBaselineId = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedId); - Tensors.Add(new HybridTensor + if (BaselineQuants.IsNativeExactAlias(decodedBaselineId)) { - TGroup = group, - CandidateBaseline = candidate, - TensorType = candidate.DefaultTensorScheme ?? TensorWeightScheme.GetCurrentNativePrecisionScheme() - }); + var exactScheme = BaselineQuants.ResolveExactOverrideScheme(decodedBaselineId); + Tensors.Add(HybridTensor.CreateExact(group, exactScheme)); + return; + } + + var candidate = BaselineQuants.FromId(decodedBaselineId); + Tensors.Add(HybridTensor.CreateLearned(group, candidate)); } public HybridQuant Clone() @@ -43,16 +52,34 @@ public HybridQuant Clone() { BaseQuant = BaseQuant, Tensors = Tensors - .Select(t => new HybridTensor - { - TGroup = t.TGroup, - CandidateBaseline = t.CandidateBaseline, - TensorType = t.TensorType - }) + .Select(t => t.Clone()) .ToList() }; } + public HybridTensor? TryGetTensor(TensorGroup group) => + Tensors.FirstOrDefault(x => x.TGroup.UniqueId == group.UniqueId); + + public HybridTensor GetRequiredTensor(TensorGroup group) => + TryGetTensor(group) ?? throw new InvalidOperationException($"HybridQuant does not contain group '{group.Name}'."); + + public void SetExactOverride(TensorGroup group, TensorWeightScheme exactScheme) + { + RemoveGroupIfPresent(group); + Tensors.Add(HybridTensor.CreateExact(group, exactScheme)); + } + + public void SetLearnedCandidateOverride(TensorGroup group, BaselineQuants candidateBaseline) + { + RemoveGroupIfPresent(group); + Tensors.Add(HybridTensor.CreateLearned(group, candidateBaseline)); + } + + public void RemoveGroupIfPresent(TensorGroup group) + { + Tensors.RemoveAll(x => x.TGroup.UniqueId == group.UniqueId); + } + public static HybridQuant CreatePureBaseline(BaselineQuants baseQuant) { return new HybridQuant @@ -62,21 +89,30 @@ public static HybridQuant CreatePureBaseline(BaselineQuants baseQuant) }; } - public static HybridQuant CreateBlanket( + public static HybridQuant CreateExactBlanket( BaselineQuants baseQuant, IEnumerable groups, - BaselineQuants blanketCandidate) + TensorWeightScheme exactScheme) { return new HybridQuant { BaseQuant = baseQuant, Tensors = groups - .Select(g => new HybridTensor - { - TGroup = g, - CandidateBaseline = blanketCandidate, - TensorType = blanketCandidate.DefaultTensorScheme ?? TensorWeightScheme.GetCurrentNativePrecisionScheme() - }) + .Select(g => HybridTensor.CreateExact(g, exactScheme)) + .ToList() + }; + } + + public static HybridQuant CreateLearnedCandidateBlanket( + BaselineQuants baseQuant, + IEnumerable groups, + BaselineQuants candidateBaseline) + { + return new HybridQuant + { + BaseQuant = baseQuant, + Tensors = groups + .Select(g => HybridTensor.CreateLearned(g, candidateBaseline)) .ToList() }; } @@ -87,6 +123,88 @@ public static HybridQuant CreateBlanket( public class HybridTensor { public TensorGroup TGroup { get; set; } = null!; - public BaselineQuants CandidateBaseline { get; set; } = default!; - public TensorWeightScheme TensorType { get; set; } = default!; + public HybridTensorOverrideMode OverrideMode { get; set; } + public BaselineQuants? CandidateBaseline { get; set; } + public TensorWeightScheme? ExactTensorScheme { get; set; } + public TensorWeightScheme MaterializedTensorScheme { get; set; } = default!; + + // Compatibility alias retained for older call sites. + public TensorWeightScheme TensorType + { + get => MaterializedTensorScheme; + set => MaterializedTensorScheme = value; + } + + public static HybridTensor CreateLearned(TensorGroup group, BaselineQuants candidateBaseline) + { + if (BaselineQuants.IsNativeExactAlias(candidateBaseline)) + { + throw new InvalidOperationException( + $"Baseline '{candidateBaseline.Names[0]}' is an exact/native alias and cannot be used as a learned baseline candidate."); + } + + return new HybridTensor + { + TGroup = group, + OverrideMode = HybridTensorOverrideMode.LearnedBaselineCandidate, + CandidateBaseline = candidateBaseline, + ExactTensorScheme = null, + MaterializedTensorScheme = candidateBaseline.DefaultTensorScheme ?? TensorWeightScheme.GetCurrentNativePrecisionScheme() + }; + } + + public static HybridTensor CreateExact(TensorGroup group, TensorWeightScheme exactScheme) + { + return new HybridTensor + { + TGroup = group, + OverrideMode = HybridTensorOverrideMode.ExactTensorScheme, + CandidateBaseline = null, + ExactTensorScheme = exactScheme, + MaterializedTensorScheme = exactScheme + }; + } + + public HybridTensor Clone() + { + return new HybridTensor + { + TGroup = TGroup, + OverrideMode = OverrideMode, + CandidateBaseline = CandidateBaseline, + ExactTensorScheme = ExactTensorScheme, + MaterializedTensorScheme = MaterializedTensorScheme + }; + } + + public void ValidateOrThrow() + { + if (TGroup == null) + throw new InvalidOperationException("HybridTensor.TGroup is required."); + + switch (OverrideMode) + { + case HybridTensorOverrideMode.LearnedBaselineCandidate: + if (CandidateBaseline == null) + throw new InvalidOperationException($"HybridTensor for group '{TGroup.Name}' is missing CandidateBaseline."); + + if (ExactTensorScheme != null) + throw new InvalidOperationException($"HybridTensor for group '{TGroup.Name}' cannot specify ExactTensorScheme when OverrideMode is LearnedBaselineCandidate."); + + if (BaselineQuants.IsNativeExactAlias(CandidateBaseline)) + throw new InvalidOperationException($"HybridTensor for group '{TGroup.Name}' cannot use exact/native alias '{CandidateBaseline.Names[0]}' as a learned baseline candidate."); + break; + + case HybridTensorOverrideMode.ExactTensorScheme: + if (ExactTensorScheme == null) + throw new InvalidOperationException($"HybridTensor for group '{TGroup.Name}' is missing ExactTensorScheme."); + + if (CandidateBaseline != null) + throw new InvalidOperationException($"HybridTensor for group '{TGroup.Name}' cannot specify CandidateBaseline when OverrideMode is ExactTensorScheme."); + break; + + default: + throw new InvalidOperationException($"HybridTensor for group '{TGroup.Name}' has unknown OverrideMode '{OverrideMode}'."); + } + } } diff --git a/MQ.DB/Models/TensorConfigs.cs b/MQ.DB/Models/TensorConfigs.cs index a3db0c3..59a8257 100644 --- a/MQ.DB/Models/TensorConfigs.cs +++ b/MQ.DB/Models/TensorConfigs.cs @@ -45,23 +45,23 @@ public TensorConfig( public TensorConfig(HybridQuant h) : this( baseQuant: checked((byte)h.BaseQuant.UniqueId), - embeddings: GetCandidateIdOrDefault(h, TReg.Embeddings), - lmHead: GetCandidateIdOrDefault(h, TReg.LmHead), - attnQ: GetCandidateIdOrDefault(h, TReg.AttnQ), - attnKV: GetCandidateIdOrDefault(h, TReg.AttnKV), - attnOutput: GetCandidateIdOrDefault(h, TReg.AttnOutput), - ffnUpGate: GetCandidateIdOrDefault(h, TReg.FfnUpGate), - ffnDown: GetCandidateIdOrDefault(h, TReg.FfnDown), - moeExperts: GetCandidateIdOrDefault(h, TReg.MoeExperts), - moeRouter: GetCandidateIdOrDefault(h, TReg.MoeRouter)) + embeddings: GetStoredIdOrDefault(h, TReg.Embeddings), + lmHead: GetStoredIdOrDefault(h, TReg.LmHead), + attnQ: GetStoredIdOrDefault(h, TReg.AttnQ), + attnKV: GetStoredIdOrDefault(h, TReg.AttnKV), + attnOutput: GetStoredIdOrDefault(h, TReg.AttnOutput), + ffnUpGate: GetStoredIdOrDefault(h, TReg.FfnUpGate), + ffnDown: GetStoredIdOrDefault(h, TReg.FfnDown), + moeExperts: GetStoredIdOrDefault(h, TReg.MoeExperts), + moeRouter: GetStoredIdOrDefault(h, TReg.MoeRouter)) { } - private static byte GetCandidateIdOrDefault(HybridQuant h, TensorGroup group) + private static byte GetStoredIdOrDefault(HybridQuant h, TensorGroup group) { if (h.Tensors == null || h.Tensors.Count == 0) - return TensorWeightScheme.NULL.UniqueId; + return BaselineQuants.TensorConfigNullSlotValue; - BaselineQuants? found = null; + HybridTensor? found = null; for (int i = 0; i < h.Tensors.Count; i++) { @@ -75,10 +75,20 @@ private static byte GetCandidateIdOrDefault(HybridQuant h, TensorGroup group) if (found != null) throw new InvalidOperationException($"HybridQuant contains duplicate entries for group '{group.Name}' (UniqueId={group.UniqueId})."); - found = t.CandidateBaseline ?? BaselineQuants.FromTensorSchemeId(t.TensorType.UniqueId); + found = t; } - return found == null ? TensorWeightScheme.NULL.UniqueId : checked((byte)found.UniqueId); + if (found == null) + return BaselineQuants.TensorConfigNullSlotValue; + + found.ValidateOrThrow(); + + return found.OverrideMode switch + { + HybridTensorOverrideMode.LearnedBaselineCandidate => BaselineQuants.EncodeTensorConfigGroupSlot(found.CandidateBaseline!), + HybridTensorOverrideMode.ExactTensorScheme => BaselineQuants.EncodeTensorConfigGroupSlot(found.ExactTensorScheme!), + _ => throw new InvalidOperationException($"Unknown HybridTensorOverrideMode '{found.OverrideMode}'.") + }; } public static explicit operator TensorConfig(HybridQuant h) => new TensorConfig(h); diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 3f5adaf..36d99b5 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -62,6 +62,7 @@ public async Task Run(List args) string.Equals(a.Name, "recheck-hardware-probe", StringComparison.OrdinalIgnoreCase)); Cache.UseImatrix = args.Any(a => string.Equals(a.Name, "use-imatrix", StringComparison.OrdinalIgnoreCase)); Cache.ForceImatrixRebuild = args.Any(a => string.Equals(a.Name, "imatrix-force-rebuild", StringComparison.OrdinalIgnoreCase)); + RuntimeSearchSpace.ResetForNewModel(); RuntimeSearchSpace.SetImatrixAvailability(false); RuntimeSearchSpace.AllowHighPrecisionHybrids = args.Any(a => string.Equals(a.Name, "allow-high-precision-hybrids", StringComparison.OrdinalIgnoreCase)); @@ -189,16 +190,6 @@ await benchmarkService.RunAllBenchmarksAsync( var comboCountBefore = ComboCounter.CountAll(); var learnedBaselinePruner = new LearnedBaselinePruningService(); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning"); - - AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning[/]") { Justification = Justify.Left }); - var learnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Learned-Baseline Pruning"); - - foreach (var note in learnedPruningResult.Notes) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); - AnsiConsole.Write(new Rule("[yellow]Initial Isolation Startup Samples[/]") { Justification = Justify.Left }); var isolationPlanner = new IsolationPlanningService(); @@ -212,6 +203,16 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {initialSummary.Skipped:N0}"); AnsiConsole.MarkupLine($" [red]Failed:[/] {initialSummary.Failed:N0}"); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning"); + + AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning[/]") { Justification = Justify.Left }); + var learnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Learned-Baseline Pruning"); + + foreach (var note in learnedPruningResult.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + var isolationOptimizer = new IsolationOptimizationService(); AnsiConsole.Write(new Rule("[yellow]Initial Probe Analysis[/]") { Justification = Justify.Left }); @@ -279,7 +280,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {learnedPruningResult.GroupCandidateEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {learnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0}"); - AnsiConsole.MarkupLine($"[green]Groups reduced to BF16-only:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); + AnsiConsole.MarkupLine($"[green]Groups reduced to explicit-banned->Q8-fallback:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupCandidatesBanned:N0}"); @@ -369,4 +370,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} +} \ No newline at end of file diff --git a/MagicQuant/Helpers/ComboLogic.cs b/MagicQuant/Helpers/ComboLogic.cs index 672e8be..f217fdb 100644 --- a/MagicQuant/Helpers/ComboLogic.cs +++ b/MagicQuant/Helpers/ComboLogic.cs @@ -13,9 +13,6 @@ public static class ComboLogic public static ImmutableArray GetAllowedCandidateIdsPerGroup(BaselineQuants baseQuant) { bool imatrixAvailable = RuntimeSearchSpace.HasUsableImatrix(); - var candidatesForRun = BaselineQuants.GetGroupCombinationCandidates(imatrixAvailable, allowHighPrecisionHybrids: false) - .ToImmutableArray(); - var builder = ImmutableArray.CreateBuilder(); var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); @@ -23,38 +20,27 @@ public static ImmutableArray GetAllowedCandidateIdsPerGroup(BaselineQuan { if (unusedIds.Contains(group.UniqueId)) { - builder.Add([TensorWeightScheme.NULL.UniqueId]); + builder.Add([BaselineQuants.TensorConfigNullSlotValue]); continue; } var ids = new List(); - // Strict policy (Option A): - // - normal explicit hybrid families come only from GetGroupCombinationCandidates(..., false) - // - BF16/F16 are injected only here and only when AllowHighPrecisionHybrids is enabled - if (RuntimeSearchSpace.AllowHighPrecisionHybrids && !RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)) + foreach (var alias in BaselineQuants.GetExactHighPrecisionAliases(RuntimeSearchSpace.AllowHighPrecisionHybrids)) { - ids.Add(BaselineQuants.BF16_Hybrid.UniqueId); - ids.Add(BaselineQuants.F16_Hybrid.UniqueId); - } - - foreach (var candidate in candidatesForRun) - { - if (candidate.BannedGroupIds.Contains(group.UniqueId)) + if (RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)) continue; - if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) - continue; - - ids.Add(candidate.UniqueId); + ids.Add(BaselineQuants.EncodeTensorConfigGroupSlot(alias)); } - ids = ids.Distinct().OrderBy(x => x).ToList(); + var realCandidates = RuntimeSearchSpace.GetAllowedRealExplicitCombinationCandidatesForGroup(group); + ids.AddRange(realCandidates.Select(BaselineQuants.EncodeTensorConfigGroupSlot)); + + ids = ids.Distinct().ToList(); if (ids.Count == 0) - { - ids.Add(BaselineQuants.GetDefaultExplicitFallbackBaseline().UniqueId); - } + ids.Add(BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.GetDefaultExplicitFallbackBaseline())); builder.Add(ids.ToArray()); } diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index a5d2538..14bb40c 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -1,3 +1,4 @@ +using MQ.DB; using MQ.DB.Models; namespace MagicQuant.Helpers; @@ -37,9 +38,6 @@ public static void ResetForNewModel() public static void BanCombinationCandidateForGroup(TensorGroup group, BaselineQuants candidate) { - if (candidate.UniqueId == BaselineQuants.GetDefaultExplicitFallbackBaseline().UniqueId) - return; - if (!ExplicitCandidateBansByGroup.TryGetValue(group.UniqueId, out var set)) { set = new HashSet(); @@ -98,7 +96,7 @@ public static void ClearLearnedBaselinePruneForGroupCandidate(TensorGroup group, public static void BanAllExplicitCombinationCandidatesForGroup(TensorGroup group) { - foreach (var candidate in BaselineQuants.GetGroupCombinationCandidates(_imatrixAvailable, allowHighPrecisionHybrids: false)) + foreach (var candidate in GetRealExplicitCombinationCandidatesForGroup(group)) BanCombinationCandidateForGroup(group, candidate); } @@ -116,12 +114,25 @@ public static IReadOnlyList GetRuntimeExplicitCandidateBansForGr public static bool IsCombinationCandidateRuntimeBannedForGroup(TensorGroup group, BaselineQuants candidate) => ExplicitCandidateBansByGroup.TryGetValue(group.UniqueId, out var set) && set.Contains(candidate.UniqueId); - public static bool HasAnyExplicitCombinationCandidateAllowed(TensorGroup group) + public static IReadOnlyList GetRealExplicitCombinationCandidatesForGroup(TensorGroup group) { return BaselineQuants.GetGroupCombinationCandidates(_imatrixAvailable, allowHighPrecisionHybrids: false) - .Any(x => !IsCombinationCandidateRuntimeBannedForGroup(group, x)); + .Where(x => !x.BannedGroupIds.Contains(group.UniqueId)) + .OrderBy(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId) + .ToList(); } + public static IReadOnlyList GetAllowedRealExplicitCombinationCandidatesForGroup(TensorGroup group) + { + return GetRealExplicitCombinationCandidatesForGroup(group) + .Where(x => !IsCombinationCandidateRuntimeBannedForGroup(group, x)) + .ToList(); + } + + public static bool HasAnyExplicitCombinationCandidateAllowed(TensorGroup group) + => GetAllowedRealExplicitCombinationCandidatesForGroup(group).Count > 0; + public static bool IsGroupExplicitCandidateBanned(TensorGroup group) => !HasAnyExplicitCombinationCandidateAllowed(group); public static IReadOnlyList GetGroupsWithExplicitQuantBanned() @@ -152,6 +163,23 @@ public static bool IsBf16TensorChoiceSuppressed(TensorGroup group) public static IReadOnlyList GetBf16SuppressedGroups() => TReg.All.Where(IsBf16TensorChoiceSuppressed).OrderBy(x => x.UniqueId).ToList(); + public static string GetDisplayStateForGroup(TensorGroup group) + { + if (Cache.UnusedTensorGroups.Any(x => x.UniqueId == group.UniqueId)) + return "unused->NULL"; + + if (IsGroupExplicitCandidateBanned(group)) + return "explicit-banned->Q8-fallback"; + + if (IsBf16TensorChoiceSuppressed(group)) + return "BF16-suppressed"; + + if (HasLearnedBaselineMissingPrunesForGroup(group)) + return "learned-pruned"; + + return "variable"; + } + public static (bool ExplicitAllowed, bool Bf16Allowed) GetFinalAllowedQuantFamiliesForGroup(TensorGroup group) { bool explicitAllowed = HasAnyExplicitCombinationCandidateAllowed(group); @@ -183,8 +211,6 @@ public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allo public static bool IsCombinationBaselineDisabled(BaselineQuants baseline) => DisabledCombinationBaselineIds.Contains(baseline.UniqueId); - // Legacy compatibility wrappers (scheme-driven callers). - // Prefer candidate-based APIs in new code. [Obsolete("Use BanCombinationCandidateForGroup.")] public static void BanSchemeForGroup(TensorGroup group, TensorWeightScheme scheme) => BanCombinationCandidateForGroup(group, BaselineQuants.FromTensorSchemeId(scheme.UniqueId)); diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index c1064e9..539470e 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -15,7 +15,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc var activeBaselines = RuntimeSearchSpace.GetActiveCombinationBaselines().ToList(); var disabledBaselines = BaselineQuants.All - .Where(x => x.IsCombinationCarrierCandidate) + .Where(x => x.IsCombinationCarrierCandidate) .Where(x => RuntimeSearchSpace.IsCombinationBaselineDisabled(x)) .OrderBy(x => x.UniqueId) .ToList(); @@ -64,8 +64,6 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc } } - var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); - foreach (var baseline in activeBaselines) { AnsiConsole.Write(new Rule($"[blue]Base: {Markup.Escape(string.Join("/", baseline.Names))}[/]") @@ -84,19 +82,13 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc var names = ids.Select(id => { - if (id == TensorWeightScheme.NULL.UniqueId) + if (BaselineQuants.IsNullTensorConfigGroupSlot(id)) return "NULL"; - var candidate = BaselineQuants.All.FirstOrDefault(x => x.UniqueId == id); - return candidate?.Names[0] ?? $"Unknown({id})"; + return BaselineQuants.DecodeTensorConfigGroupSlotToBaseline(id).Names[0]; }).ToList(); - string state = - unusedIds.Contains(group.UniqueId) ? "unused->NULL" : - RuntimeSearchSpace.IsGroupExplicitCandidateBanned(group) ? "BF16-only" : - RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group) ? "BF16-suppressed" : - RuntimeSearchSpace.HasLearnedBaselineMissingPrunesForGroup(group) ? "learned-pruned" : - "variable"; + string state = RuntimeSearchSpace.GetDisplayStateForGroup(group); AnsiConsole.MarkupLine( $" [cyan]{Markup.Escape(group.Name)}[/] => [green]{ids.Length}[/] choice(s) " + diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 8fb82e2..9437acb 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -19,8 +19,9 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( .ToList(); var result = new RequiredSampleGenerationResult(); + var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - foreach (var baseline in BaselineQuants.GetPureBaselineCandidates(RuntimeSearchSpace.HasUsableImatrix())) + foreach (var baseline in BaselineQuants.GetLearningBaselines(RuntimeSearchSpace.HasUsableImatrix())) { result.Plans.Add(new RequiredSamplePlan { @@ -41,10 +42,10 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( Kind = RequiredSampleKind.BaseOnlyIsolation, Key = $"baseonly:{baseline.UniqueId}", Description = $"Base-only isolation for {string.Join("/", baseline.Names)} with all active groups forced native.", - Quant = HybridQuant.CreateBlanket( + Quant = HybridQuant.CreateExactBlanket( baseQuant: baseline, groups: activeGroups, - blanketCandidate: BaselineQuants.BF16_Hybrid), + exactScheme: nativeExactScheme), TestedBaselineId = baseline.UniqueId }); @@ -58,10 +59,10 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( Kind = RequiredSampleKind.BaseOnlyIsolation, Key = $"carrier-baseonly:{carrier.UniqueId}", Description = "Carrier base-only isolation on Q8 with all active groups forced native.", - Quant = HybridQuant.CreateBlanket( + Quant = HybridQuant.CreateExactBlanket( baseQuant: carrier, groups: activeGroups, - blanketCandidate: BaselineQuants.BF16_Hybrid), + exactScheme: nativeExactScheme), TestedBaselineId = carrier.UniqueId }); @@ -73,14 +74,12 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( if (smallest == null) continue; - var quant = HybridQuant.CreateBlanket( + var quant = HybridQuant.CreateExactBlanket( baseQuant: carrier, groups: activeGroups, - blanketCandidate: BaselineQuants.BF16_Hybrid); + exactScheme: nativeExactScheme); - var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); - target.CandidateBaseline = smallest; - target.TensorType = smallest.DefaultTensorScheme!; + quant.SetLearnedCandidateOverride(group, smallest); result.Plans.Add(new RequiredSamplePlan { @@ -123,10 +122,11 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var result = new RequiredSampleGenerationResult(); var carrier = BaselineQuants.Q8_0; + var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - var candidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) - .Where(x => x.UniqueId != BaselineQuants.BF16_Hybrid.UniqueId) - .OrderBy(x => x.UniqueId) + var candidates = BaselineQuants.GetGroupCombinationCandidatesSmallestFirst( + RuntimeSearchSpace.HasUsableImatrix(), + allowHighPrecisionHybrids: false) .ToList(); foreach (var group in activeGroups) @@ -135,20 +135,21 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample foreach (var candidate in candidates) { + if (candidate.BannedGroupIds.Contains(group.UniqueId)) + continue; + if (smallest != null && candidate.UniqueId == smallest.UniqueId) continue; if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; - var quant = HybridQuant.CreateBlanket( + var quant = HybridQuant.CreateExactBlanket( baseQuant: carrier, groups: TReg.All.Where(x => !missingIds.Contains(x.UniqueId)), - blanketCandidate: BaselineQuants.BF16_Hybrid); + exactScheme: nativeExactScheme); - var target = quant.Tensors.First(x => x.TGroup.UniqueId == group.UniqueId); - target.CandidateBaseline = candidate; - target.TensorType = candidate.DefaultTensorScheme!; + quant.SetLearnedCandidateOverride(group, candidate); result.Plans.Add(new RequiredSamplePlan { @@ -243,17 +244,9 @@ public static IEnumerable> GenerateTensorConfigBatches( while (true) { batch.Add(new TensorConfig( - baseQuant: baseId, - embeddings: d0[idx[0]], - lmHead: d1[idx[1]], - attnQ: d2[idx[2]], - attnKV: d3[idx[3]], - attnOutput: d4[idx[4]], - ffnUpGate: d5[idx[5]], - ffnDown: d6[idx[6]], - moeExperts: d7[idx[7]], - moeRouter: d8[idx[8]] - )); + baseId, + d0[idx[0]], d1[idx[1]], d2[idx[2]], d3[idx[3]], d4[idx[4]], + d5[idx[5]], d6[idx[6]], d7[idx[7]], d8[idx[8]])); if (batch.Count >= batchSize) { @@ -295,16 +288,11 @@ public static IEnumerable> GenerateTensorConfigBatches( private static BaselineQuants? GetSmallestAllowedProbeCandidateForGroup(TensorGroup group) { - var orderedCandidates = TensorWeightScheme.GetSmallestInOrder() - .Select(x => BaselineQuants.FromTensorSchemeId(x.UniqueId)) - .DistinctBy(x => x.UniqueId); - - foreach (var candidate in orderedCandidates) + foreach (var candidate in BaselineQuants.GetGroupCombinationCandidatesSmallestFirst( + RuntimeSearchSpace.HasUsableImatrix(), + allowHighPrecisionHybrids: false)) { - if (candidate.UniqueId == BaselineQuants.BF16_Hybrid.UniqueId || candidate.UniqueId == BaselineQuants.F16_Hybrid.UniqueId) - continue; - - if (candidate.RequiresImatrix && !RuntimeSearchSpace.HasUsableImatrix()) + if (candidate.BannedGroupIds.Contains(group.UniqueId)) continue; if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 1fa4a9a..a3cb14d 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -64,7 +64,7 @@ public async Task AnalyzeInitialIsolationProbesA var nativeBaseline = await LoadSnapshotAsync( HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) - ?? throw new InvalidOperationException("Native BF16 baseline benchmark was not found."); + ?? throw new InvalidOperationException($"Required native exact baseline benchmark was not found for model '{Cache.CurrentModelId}'."); var carrierBaselineId = BaselineQuants.Q8_0.UniqueId; @@ -74,7 +74,7 @@ public async Task AnalyzeInitialIsolationProbesA x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal)); var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct) - ?? throw new InvalidOperationException("Carrier base-only benchmark was not found."); + ?? throw new InvalidOperationException($"Required carrier base-only benchmark was not found for model '{Cache.CurrentModelId}' and carrier '{BaselineQuants.Q8_0.Names[0]}'."); var groupPlans = plan.Plans .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe) @@ -150,7 +150,7 @@ public async Task AnalyzeAndApplyFinalAsync( var nativeBaseline = await LoadSnapshotAsync( HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct) - ?? throw new InvalidOperationException("Native BF16 baseline benchmark was not found."); + ?? throw new InvalidOperationException($"Required native exact baseline benchmark was not found for model '{Cache.CurrentModelId}'."); var carrierBaselineId = BaselineQuants.Q8_0.UniqueId; @@ -160,7 +160,7 @@ public async Task AnalyzeAndApplyFinalAsync( x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal)); var carrierBaseOnly = await LoadSnapshotAsync(carrierBaseOnlyPlan.Quant, ct) - ?? throw new InvalidOperationException("Carrier base-only benchmark was not found."); + ?? throw new InvalidOperationException($"Required carrier base-only benchmark was not found for model '{Cache.CurrentModelId}' and carrier '{BaselineQuants.Q8_0.Names[0]}'."); var groupPlans = fullPlan.Plans .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe || @@ -310,7 +310,7 @@ public async Task AnalyzeAndApplyFinalAsync( } private static bool IsHighPrecisionCandidate(BaselineQuants candidate) - => candidate.UniqueId == BaselineQuants.BF16_Hybrid.UniqueId || candidate.UniqueId == BaselineQuants.F16_Hybrid.UniqueId; + => BaselineQuants.IsNativeExactAlias(candidate); private static void PopulateFinalGroupFlags(TensorGroup group, IsolationGroupDecision decision, IsolationOptimizationResult result) { @@ -608,4 +608,4 @@ private sealed class CategorySnapshot public double Ppl { get; set; } public double PplError { get; set; } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index 74a9900..deade76 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -99,6 +99,9 @@ internal static void ApplyLearnedBaselinePruning( foreach (var candidate in explicitCandidates) { + if (candidate.BannedGroupIds.Contains(group.UniqueId)) + continue; + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; @@ -148,6 +151,12 @@ internal static void ApplyLearnedBaselinePruning( effectiveSchemesByBaselineAndGroup[key] = set; } + // The persisted TensorWeightSchemeId is the authoritative learned-family identity. + // FinalQuantType is useful extra metadata, but it cannot replace the stored scheme id + // because some learned baselines materialize tensors whose final emitted token differs + // from the baseline family we are learning from. + set.Add(row.TensorWeightSchemeId); + if (aliasToSchemeIds.TryGetValue(CanonicalizeQuantToken(row.FinalQuantType), out var resolvedIds)) { foreach (var resolvedId in resolvedIds) @@ -195,4 +204,4 @@ private static string CanonicalizeQuantToken(string value) .Replace(" ", string.Empty) .ToUpperInvariant(); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index b0d821d..3f71328 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1439,6 +1439,15 @@ private Dictionary AssignGroups(IEnumerable GetExpectedTensorNamesForGroup( + TensorGroup group, + IReadOnlyCollection sourceTensorNames) + { + return sourceTensorNames + .Where(x => group.Tensors.Any(p => Regex.IsMatch(x, $"^{p}$"))) + .ToHashSet(StringComparer.Ordinal); + } + private List BuildRequestedTensorOverrides( HybridQuant quant, IReadOnlyCollection sourceTensorNames) @@ -1455,50 +1464,85 @@ private List BuildRequestedTensorOverrides( if (hybrid?.TGroup == null) continue; - if (hybrid.TensorType.UniqueId == TensorWeightScheme.NULL.UniqueId) + hybrid.ValidateOrThrow(); + + if (hybrid.MaterializedTensorScheme.UniqueId == TensorWeightScheme.NULL.UniqueId) continue; - if (hybrid.CandidateBaseline != null && hybrid.CandidateBaseline.UniqueId == quant.BaseQuant.UniqueId) + var expectedForGroup = GetExpectedTensorNamesForGroup(hybrid.TGroup, sourceTensorNames); + if (expectedForGroup.Count == 0) continue; - var sourceBaseline = hybrid.CandidateBaseline ?? BaselineQuants.FromTensorSchemeId(hybrid.TensorType.UniqueId); - var learned = TryLoadLearnedTensorMapping( - sourceBaselineId: sourceBaseline.UniqueId, - targetGroup: hybrid.TGroup, - preferredSourceScheme: hybrid.CandidateBaseline?.DefaultTensorScheme ?? hybrid.TensorType); - if (learned.Count == 0) + switch (hybrid.OverrideMode) { - throw new InvalidOperationException( - $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. " + - "Run with --relearn-baseline-mappings to regenerate."); - } - - var expectedForGroup = sourceTensorNames - .Where(x => hybrid.TGroup.Tensors.Any(p => Regex.IsMatch(x, $"^{p}$"))) - .ToHashSet(StringComparer.Ordinal); + case HybridTensorOverrideMode.ExactTensorScheme: + { + var exactScheme = hybrid.ExactTensorScheme!; - var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); - var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); - var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); + if (baseScheme != null && exactScheme.UniqueId == baseScheme.UniqueId) + continue; - if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) - { - var missingText = missingExpected.Count == 0 ? "none" : string.Join(", ", missingExpected.Take(15)); - var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); + string schemeName = ResolveSchemeName(exactScheme); + foreach (var tensorName in expectedForGroup.OrderBy(x => x, StringComparer.Ordinal)) + { + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + TensorName = tensorName, + SchemeName = schemeName + }); + } - throw new InvalidOperationException( - $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. " + - $"Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); - } + break; + } - foreach (var kv in learned) - { - result.Add(new RequestedTensorOverride + case HybridTensorOverrideMode.LearnedBaselineCandidate: { - GroupName = hybrid.TGroup.Name, - TensorName = kv.Key, - SchemeName = kv.Value - }); + var sourceBaseline = hybrid.CandidateBaseline!; + byte canonicalBaselineId = BaselineQuants.CanonicalLearningBaselineId(sourceBaseline); + var learned = TryLoadLearnedTensorMapping( + canonicalSourceBaselineId: canonicalBaselineId, + targetGroup: hybrid.TGroup, + preferredSourceScheme: sourceBaseline.DefaultTensorScheme, + allowDominantFallback: false); + + if (learned.Count == 0) + { + throw new InvalidOperationException( + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}' (canonicalId={canonicalBaselineId}). " + + "Run with --relearn-baseline-mappings to regenerate."); + } + + var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); + var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); + var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); + + if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) + { + var missingText = missingExpected.Count == 0 ? "none" : string.Join(", ", missingExpected.Take(15)); + var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); + + throw new InvalidOperationException( + $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. " + + $"Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); + } + + foreach (var kv in learned.OrderBy(x => x.Key, StringComparer.Ordinal)) + { + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + TensorName = kv.Key, + SchemeName = kv.Value + }); + } + + break; + } + + default: + throw new InvalidOperationException( + $"Hybrid tensor for group '{hybrid.TGroup.Name}' has unsupported override mode '{hybrid.OverrideMode}'."); } } @@ -1506,9 +1550,10 @@ private List BuildRequestedTensorOverrides( } private Dictionary TryLoadLearnedTensorMapping( - byte sourceBaselineId, + byte canonicalSourceBaselineId, TensorGroup targetGroup, - TensorWeightScheme? preferredSourceScheme = null) + TensorWeightScheme? preferredSourceScheme = null, + bool allowDominantFallback = false) { using var db = new MagicQuantContext(); @@ -1522,7 +1567,7 @@ private Dictionary TryLoadLearnedTensorMapping( var allRows = db.LearnedBaselineTensorQuants .AsNoTracking() .Where(x => x.AiModelHashId == model.Id) - .Where(x => x.BaselineQuantId == sourceBaselineId) + .Where(x => x.BaselineQuantId == canonicalSourceBaselineId) .Where(x => x.TensorGroupId == targetGroup.UniqueId) .OrderBy(x => x.TensorName) .ToList(); @@ -1530,20 +1575,32 @@ private Dictionary TryLoadLearnedTensorMapping( if (allRows.Count == 0) return new Dictionary(StringComparer.Ordinal); - var rows = allRows; + List rows = allRows; + if (preferredSourceScheme != null) { - var preferred = allRows.Where(x => x.TensorWeightSchemeId == preferredSourceScheme.UniqueId).ToList(); + var preferred = allRows + .Where(x => x.TensorWeightSchemeId == preferredSourceScheme.UniqueId) + .ToList(); + if (preferred.Count > 0) + { rows = preferred; + } + else if (!allowDominantFallback) + { + return new Dictionary(StringComparer.Ordinal); + } } if (rows.Count == 0) return new Dictionary(StringComparer.Ordinal); - // If rows contain mixed source schemes, use the dominant scheme for stable coverage semantics. if (rows.Select(x => x.TensorWeightSchemeId).Distinct().Count() > 1) { + if (!allowDominantFallback) + return new Dictionary(StringComparer.Ordinal); + var dominantSchemeId = rows .GroupBy(x => x.TensorWeightSchemeId) .OrderByDescending(g => g.Count()) @@ -1800,18 +1857,35 @@ public string GenerateHybridName(HybridQuant quant) string baseName = ResolveBaseName(quant.BaseQuant); var effectiveTensors = quant.Tensors? - .Where(t => t?.TGroup != null && t.CandidateBaseline != null) + .Where(t => t?.TGroup != null) .ToList(); if (effectiveTensors == null || effectiveTensors.Count == 0) return $"{modelName}-{baseName}"; var grouped = effectiveTensors - .GroupBy(t => t.CandidateBaseline.Names[0]) + .Select(t => + { + t.ValidateOrThrow(); + + string typeName = t.OverrideMode switch + { + HybridTensorOverrideMode.LearnedBaselineCandidate => t.CandidateBaseline!.Names[0], + HybridTensorOverrideMode.ExactTensorScheme => ResolveSchemeName(t.ExactTensorScheme!), + _ => throw new InvalidOperationException($"Unknown override mode '{t.OverrideMode}'.") + }; + + return new + { + Type = typeName, + Code = t.TGroup.ShortCode + }; + }) + .GroupBy(x => x.Type) .Select(g => new { Type = g.Key, - Codes = g.Select(x => x.TGroup.ShortCode) + Codes = g.Select(x => x.Code) .OrderBy(c => GetOrder(c)) .ToArray() }) @@ -1942,4 +2016,4 @@ void HandleLine(string? line, bool isError) StdErr = stderrBuilder.ToString() }; } -} +} \ No newline at end of file From 664b4aa505a3fd4bf3e2a8c707ea9bb62b4ea152 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 20 Apr 2026 20:15:33 -0400 Subject: [PATCH 101/258] update --- .../20260420215500_InitialCreate.Designer.cs | 602 ++++++++++++++++++ .../20260420215500_InitialCreate.cs | 490 ++++++++++++++ .../MagicQuantContextModelSnapshot.cs | 599 +++++++++++++++++ 3 files changed, 1691 insertions(+) create mode 100644 MQ.DB/Migrations/20260420215500_InitialCreate.Designer.cs create mode 100644 MQ.DB/Migrations/20260420215500_InitialCreate.cs create mode 100644 MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs diff --git a/MQ.DB/Migrations/20260420215500_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260420215500_InitialCreate.Designer.cs new file mode 100644 index 0000000..ae122f7 --- /dev/null +++ b/MQ.DB/Migrations/20260420215500_InitialCreate.Designer.cs @@ -0,0 +1,602 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260420215500_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("BaselineName") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeId") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260420215500_InitialCreate.cs b/MQ.DB/Migrations/20260420215500_InitialCreate.cs new file mode 100644 index 0000000..d42307e --- /dev/null +++ b/MQ.DB/Migrations/20260420215500_InitialCreate.cs @@ -0,0 +1,490 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AiModelHashes", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + UniqueHash = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AiModelHashes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "BaselineQuantDefinitions", + columns: table => new + { + BaselineQuantId = table.Column(type: "INTEGER", nullable: false), + BaselineName = table.Column(type: "TEXT", maxLength: 64, nullable: false), + DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), + DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.BaselineQuantId); + }); + + migrationBuilder.CreateTable( + name: "TensorCombos", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AttnKV = table.Column(type: "INTEGER", nullable: false), + AttnOutput = table.Column(type: "INTEGER", nullable: false), + AttnQ = table.Column(type: "INTEGER", nullable: false), + BaseQuant = table.Column(type: "INTEGER", nullable: false), + Embeddings = table.Column(type: "INTEGER", nullable: false), + FfnDown = table.Column(type: "INTEGER", nullable: false), + FfnUpGate = table.Column(type: "INTEGER", nullable: false), + LmHead = table.Column(type: "INTEGER", nullable: false), + MoeExperts = table.Column(type: "INTEGER", nullable: false), + MoeRouter = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TensorCombos", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ImatrixDefinitions", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + IdentityHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), + CanonicalPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true), + SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + MetadataJson = table.Column(type: "TEXT", maxLength: 8000, nullable: true), + TokenCount = table.Column(type: "INTEGER", nullable: true), + BuildFingerprint = table.Column(type: "TEXT", maxLength: 512, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ImatrixDefinitions", x => x.Id); + table.ForeignKey( + name: "FK_ImatrixDefinitions_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AiBenchmarks", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + Ngl = table.Column(type: "INTEGER", nullable: false), + SizeBytes = table.Column(type: "INTEGER", nullable: false), + TokensPerSecond = table.Column(type: "REAL", nullable: false), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AiBenchmarks", x => x.Id); + table.ForeignKey( + name: "FK_AiBenchmarks_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AiBenchmarks_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "ExecutionPlanProbeCaches", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + HardwareFingerprint = table.Column(type: "TEXT", maxLength: 1024, nullable: false), + QuantizedModelFingerprint = table.Column(type: "TEXT", maxLength: 2048, nullable: false), + QuantizationKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), + DiscoveryTokenTarget = table.Column(type: "INTEGER", nullable: false), + StaticNgl = table.Column(type: "INTEGER", nullable: false), + UsesGpu = table.Column(type: "INTEGER", nullable: false), + GroupSize = table.Column(type: "INTEGER", nullable: false), + SlotsJson = table.Column(type: "TEXT", maxLength: 8000, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + UpdatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ExecutionPlanProbeCaches", x => x.Id); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "CategoryBenchmark", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + Category = table.Column(type: "INTEGER", nullable: false), + Kld = table.Column(type: "REAL", nullable: false), + Ppl = table.Column(type: "REAL", nullable: false), + PplError = table.Column(type: "REAL", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CategoryBenchmark", x => x.Id); + table.ForeignKey( + name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "LearnedBaselineTensorQuants", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + BaselineQuantId = table.Column(type: "INTEGER", nullable: false), + TensorWeightSchemeId = table.Column(type: "INTEGER", nullable: false), + TensorGroupId = table.Column(type: "INTEGER", nullable: false), + TensorName = table.Column(type: "TEXT", maxLength: 512, nullable: false), + FinalQuantType = table.Column(type: "TEXT", maxLength: 32, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_LearnedBaselineTensorQuants", x => x.Id); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "QuantizationRuns", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: true), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: false), + DurationMs = table.Column(type: "INTEGER", nullable: false), + Succeeded = table.Column(type: "INTEGER", nullable: false), + Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true), + OutputModelPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_QuantizationRuns", x => x.Id); + table.ForeignKey( + name: "FK_QuantizationRuns_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_QuantizationRuns_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_QuantizationRuns_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "BenchmarkRuns", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + CategoryBenchmarkId = table.Column(type: "TEXT", nullable: true), + Category = table.Column(type: "INTEGER", nullable: false), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: false), + DurationMs = table.Column(type: "INTEGER", nullable: false), + Succeeded = table.Column(type: "INTEGER", nullable: false), + Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_BenchmarkRuns", x => x.Id); + table.ForeignKey( + name: "FK_BenchmarkRuns_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_CategoryBenchmark_CategoryBenchmarkId", + column: x => x.CategoryBenchmarkId, + principalTable: "CategoryBenchmark", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_BenchmarkRuns_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", + table: "AiBenchmarks", + columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_ImatrixDefinitionId", + table: "AiBenchmarks", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_TensorComboId", + table: "AiBenchmarks", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_AiModelHashes_UniqueHash", + table: "AiModelHashes", + column: "UniqueHash"); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_BaselineName", + table: "BaselineQuantDefinitions", + column: "BaselineName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeId", + table: "BaselineQuantDefinitions", + column: "DefaultTensorSchemeId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeName", + table: "BaselineQuantDefinitions", + column: "DefaultTensorSchemeName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiBenchmarkId", + table: "BenchmarkRuns", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiBenchmarkId_Category", + table: "BenchmarkRuns", + columns: new[] { "AiBenchmarkId", "Category" }); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiModelHashId", + table: "BenchmarkRuns", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_CategoryBenchmarkId", + table: "BenchmarkRuns", + column: "CategoryBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_ImatrixDefinitionId", + table: "BenchmarkRuns", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_StartedUtc", + table: "BenchmarkRuns", + column: "StartedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_TensorComboId", + table: "BenchmarkRuns", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_CategoryBenchmark_AiBenchmarkId", + table: "CategoryBenchmark", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_AiModelHashId", + table: "ExecutionPlanProbeCaches", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", + table: "ExecutionPlanProbeCaches", + columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", + table: "ExecutionPlanProbeCaches", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_ImatrixDefinitions_AiModelHashId_IdentityHash", + table: "ImatrixDefinitions", + columns: new[] { "AiModelHashId", "IdentityHash" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiBenchmarkId", + table: "LearnedBaselineTensorQuants", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorGroupId", + table: "LearnedBaselineTensorQuants", + columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId" }); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorName", + table: "LearnedBaselineTensorQuants", + columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_AiBenchmarkId", + table: "QuantizationRuns", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_AiModelHashId", + table: "QuantizationRuns", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_ImatrixDefinitionId", + table: "QuantizationRuns", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_StartedUtc", + table: "QuantizationRuns", + column: "StartedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_TensorComboId", + table: "QuantizationRuns", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", + table: "TensorCombos", + columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BaselineQuantDefinitions"); + + migrationBuilder.DropTable( + name: "BenchmarkRuns"); + + migrationBuilder.DropTable( + name: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropTable( + name: "LearnedBaselineTensorQuants"); + + migrationBuilder.DropTable( + name: "QuantizationRuns"); + + migrationBuilder.DropTable( + name: "CategoryBenchmark"); + + migrationBuilder.DropTable( + name: "AiBenchmarks"); + + migrationBuilder.DropTable( + name: "ImatrixDefinitions"); + + migrationBuilder.DropTable( + name: "TensorCombos"); + + migrationBuilder.DropTable( + name: "AiModelHashes"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs new file mode 100644 index 0000000..505af7a --- /dev/null +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -0,0 +1,599 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + partial class MagicQuantContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("BaselineName") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeId") + .IsUnique(); + + b.HasIndex("DefaultTensorSchemeName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} From 55808ebcc15bb12e5c00167d1dcdb71153f7e367 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 20 Apr 2026 20:50:13 -0400 Subject: [PATCH 102/258] update --- MQ.DB/Models/BaselineQuants.cs | 12 ++++++------ MagicQuant/Commands/Evolution.cs | 13 ++++++++++++- MagicQuant/Helpers/RuntimeSearchSpace.cs | 8 ++++++++ MagicQuant/Services/ModelCompatibilityService.cs | 4 ++-- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index ca4456a..0bb520d 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -104,7 +104,7 @@ private static BaselineQuants Create( Create(7, true, "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, + isExplicitGroupCombinationCandidate: false, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 5); @@ -112,7 +112,7 @@ private static BaselineQuants Create( Create(8, true, "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, + isExplicitGroupCombinationCandidate: false, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 4); @@ -120,7 +120,7 @@ private static BaselineQuants Create( Create(9, true, "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, + isExplicitGroupCombinationCandidate: false, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 3); @@ -128,7 +128,7 @@ private static BaselineQuants Create( Create(10, true, "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, + isExplicitGroupCombinationCandidate: false, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 2); @@ -136,7 +136,7 @@ private static BaselineQuants Create( Create(11, true, "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, + isExplicitGroupCombinationCandidate: false, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 1); @@ -144,7 +144,7 @@ private static BaselineQuants Create( Create(12, true, "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, + isExplicitGroupCombinationCandidate: false, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 0); diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 36d99b5..5de4f5a 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -126,6 +126,10 @@ public async Task Run(List args) AnsiConsole.MarkupLine("[grey]Imatrix disabled for this run.[/]"); } + // Re-assert the live runtime flag from the imatrix resolution result so later phases + // cannot accidentally inherit a stale default. + RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); + bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && await benchmarkService.TryInitializeExecutionPlanFromCacheAsync( quantizationKey: q8QuantizationKey); @@ -182,6 +186,12 @@ await benchmarkService.RunAllBenchmarksAsync( var compatibilityService = new ModelCompatibilityService(pyManager); await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); + // Compatibility must not be allowed to silently downgrade the live policy flags for the + // remainder of the evolution run. Re-assert them here as a final safeguard. + RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); + RuntimeSearchSpace.AllowHighPrecisionHybrids = args.Any(a => + string.Equals(a.Name, "allow-high-precision-hybrids", StringComparison.OrdinalIgnoreCase)); + CliHelpers.ValidateCombinationLogicWorks(true); var dbService = new QuantDatabaseService(); @@ -203,6 +213,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {initialSummary.Skipped:N0}"); AnsiConsole.MarkupLine($" [red]Failed:[/] {initialSummary.Failed:N0}"); + AnsiConsole.MarkupLine("[bold magenta]Evolution flow marker:[/] startup sampling finished, entering learned-baseline pruning."); SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning"); AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning[/]") { Justification = Justify.Left }); @@ -370,4 +381,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 14bb40c..fd92031 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -34,6 +34,14 @@ public static void ResetForNewModel() public static void SetImatrixAvailability(bool available) => _imatrixAvailable = available; + public static void ResetForCompatibilityPass() + { + ExplicitCandidateBansByGroup.Clear(); + LearnedPrunesByGroupAndCandidate.Clear(); + DisabledCombinationBaselineIds.Clear(); + Bf16SuppressedTensorChoiceGroupIds.Clear(); + } + public static bool HasUsableImatrix() => _imatrixAvailable; public static void BanCombinationCandidateForGroup(TensorGroup group, BaselineQuants candidate) diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 8baa837..32a8231 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -24,7 +24,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) TensorWeightScheme.ValidateSmallestConfiguration(); - RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.ResetForCompatibilityPass(); Cache.UnusedTensorGroups.Clear(); string directory = Path.GetDirectoryName(ggufPath)!; @@ -137,7 +137,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) } else { - AnsiConsole.MarkupLine("[green]No groups were reduced to BF16/NULL-only by compatibility checks.[/]"); + AnsiConsole.MarkupLine("[green]No groups were reduced to explicit-banned/NULL-only by compatibility checks.[/]"); } if (shapeBanCount > 0) From fb9008db4e83de26a92a0ab11ed3dc34cda885a6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 20 Apr 2026 22:17:11 -0400 Subject: [PATCH 103/258] update --- MQ.DB/Models/BaselineQuants.cs | 12 ++++++------ MagicQuant/Commands/Evolution.cs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 0bb520d..ca4456a 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -104,7 +104,7 @@ private static BaselineQuants Create( Create(7, true, "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: false, + isExplicitGroupCombinationCandidate: true, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 5); @@ -112,7 +112,7 @@ private static BaselineQuants Create( Create(8, true, "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: false, + isExplicitGroupCombinationCandidate: true, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 4); @@ -120,7 +120,7 @@ private static BaselineQuants Create( Create(9, true, "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: false, + isExplicitGroupCombinationCandidate: true, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 3); @@ -128,7 +128,7 @@ private static BaselineQuants Create( Create(10, true, "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: false, + isExplicitGroupCombinationCandidate: true, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 2); @@ -136,7 +136,7 @@ private static BaselineQuants Create( Create(11, true, "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: false, + isExplicitGroupCombinationCandidate: true, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 1); @@ -144,7 +144,7 @@ private static BaselineQuants Create( Create(12, true, "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: false, + isExplicitGroupCombinationCandidate: true, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 0); diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 5de4f5a..4868b0e 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -309,7 +309,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Final surviving combinations:[/] {finalRemainingCombinationCount:N0}"); - if (finalRemainingCombinationCount <= BruteForceFinalCombinationThreshold) + if (finalRemainingCombinationCount <= BruteForceFinalCombinationThreshold+1000) { AnsiConsole.Write(new Rule("[yellow]Final Brute Force Benchmark Phase[/]") { Justification = Justify.Left }); From 369dc095c13cee70e1ab259941bc74f8e994a1f6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 21 Apr 2026 12:24:19 -0400 Subject: [PATCH 104/258] Significantly faster bulk insert process for combinations. --- MagicQuant/Services/QuantDatabaseService.cs | 421 ++++++++++++++------ 1 file changed, 301 insertions(+), 120 deletions(-) diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index dd124ab..9dbeae2 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -15,12 +15,74 @@ public class QuantDatabaseService private const string DbFileNamePrefix = "MagicQuant_Combinations"; private const string TableName = "tensor_configs"; + // Keep this moderate so generation still yields often enough for progress. + private const int GeneratorBatchSize = 250_000; + + // Appender heartbeat. Lower = chattier. + private const long InsertProgressLogEveryRows = 50_000; + + private static readonly string[] ExpectedColumnTypes = + [ + "utinyint", + "utinyint", + "utinyint", + "utinyint", + "utinyint", + "utinyint", + "utinyint", + "utinyint", + "utinyint", + "utinyint" + ]; + + private static string CreateTableSql => $@" + DROP TABLE IF EXISTS {TableName}; + CREATE TABLE {TableName} ( + BaseQuant UTINYINT, + Embeddings UTINYINT, + LmHead UTINYINT, + AttnQ UTINYINT, + AttnKV UTINYINT, + AttnOutput UTINYINT, + FfnUpGate UTINYINT, + FfnDown UTINYINT, + MoeExperts UTINYINT, + MoeRouter UTINYINT + );"; + + private static async Task ConfigureFastLoadSessionAsync(DuckDBConnection connection, CancellationToken ct) + { + // These are safe session-level tweaks for this write-heavy workload. + // We do not care about insertion order for tensor combo staging. + using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = "SET preserve_insertion_order = false;"; + await cmd.ExecuteNonQueryAsync(ct); + } + + // Let DuckDB use the available machine parallelism. + int threadCount = Math.Max(1, Environment.ProcessorCount); + using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = $"SET threads = {threadCount};"; + await cmd.ExecuteNonQueryAsync(ct); + } + } + + private static async Task RecreateTableAsync(DuckDBConnection connection, CancellationToken ct) + { + using var createCmd = connection.CreateCommand(); + createCmd.CommandText = CreateTableSql; + await createCmd.ExecuteNonQueryAsync(ct); + } + public async Task GetRemainingCombinationCountAsync(CancellationToken ct = default) { using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); - var cmd = connection.CreateCommand(); + using var cmd = connection.CreateCommand(); cmd.CommandText = $"SELECT COUNT(*) FROM {TableName};"; return (long)(await cmd.ExecuteScalarAsync(ct) ?? 0L); @@ -30,10 +92,11 @@ public async Task> GetRemainingTensorConfigsAsync(Cancellatio { using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); var results = new List(); - var cmd = connection.CreateCommand(); + using var cmd = connection.CreateCommand(); cmd.CommandText = $@" SELECT BaseQuant, @@ -63,29 +126,29 @@ ORDER BY while (await reader.ReadAsync(ct)) { results.Add(new TensorConfig( - baseQuant: Convert.ToByte(reader.GetValue(0)), + baseQuant: Convert.ToByte(reader.GetValue(0)), embeddings: Convert.ToByte(reader.GetValue(1)), - lmHead: Convert.ToByte(reader.GetValue(2)), - attnQ: Convert.ToByte(reader.GetValue(3)), - attnKV: Convert.ToByte(reader.GetValue(4)), + lmHead: Convert.ToByte(reader.GetValue(2)), + attnQ: Convert.ToByte(reader.GetValue(3)), + attnKV: Convert.ToByte(reader.GetValue(4)), attnOutput: Convert.ToByte(reader.GetValue(5)), - ffnUpGate: Convert.ToByte(reader.GetValue(6)), - ffnDown: Convert.ToByte(reader.GetValue(7)), + ffnUpGate: Convert.ToByte(reader.GetValue(6)), + ffnDown: Convert.ToByte(reader.GetValue(7)), moeExperts: Convert.ToByte(reader.GetValue(8)), - moeRouter: Convert.ToByte(reader.GetValue(9)) + moeRouter: Convert.ToByte(reader.GetValue(9)) )); } return results; } - + private static string GetDuckDbDirectory() { if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) - return Cache.ModelMagicQuantDirectory; + return Cache.ModelMagicQuantDirectory!; if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) - return Cache.MagicQuantDirectory; + return Cache.MagicQuantDirectory!; throw new InvalidOperationException( "Neither Cache.ModelMagicQuantDirectory nor Cache.MagicQuantDirectory is set."); @@ -108,14 +171,21 @@ public async Task InitializeAsync(bool forceRebuild = false, CancellationToken c using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); BigInteger expectedTotal = ComboCounter.CountAll(); - long currentDbCount = await GetRowCountAsync(connection, ct); + bool tableShapeOk = await HasExpectedTableShapeAsync(connection, ct); + long currentDbCount = tableShapeOk + ? await GetRowCountAsync(connection, ct) + : -1; AnsiConsole.MarkupLine( $"[bold]DuckDB Check:[/] Current Rows: [cyan]{currentDbCount:N0}[/] | Expected: [yellow]{expectedTotal:N0}[/]"); - if (forceRebuild || currentDbCount != expectedTotal) + if (!tableShapeOk) + AnsiConsole.MarkupLine("[yellow]DuckDB table shape is missing or stale. Rebuild required.[/]"); + + if (forceRebuild || !tableShapeOk || currentDbCount != expectedTotal) { AnsiConsole.MarkupLine("[bold red]DuckDB empty, mismatch, forced, or stale.[/] Initializing/Rebuilding..."); await RebuildDatabaseAsync(connection, expectedTotal, ct); @@ -137,6 +207,7 @@ public async Task PrunePredictedLargerThanQ8Async( { using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); var predictionContext = await BuildPredictionContextAsync(fullPlan, ct); @@ -148,13 +219,13 @@ public async Task PrunePredictedLargerThanQ8Async( var rows = new List(); - var select = connection.CreateCommand(); - select.CommandText = $@" - SELECT BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter - FROM {TableName};"; - - using (var reader = await select.ExecuteReaderAsync(ct)) + using (var select = connection.CreateCommand()) { + select.CommandText = $@" + SELECT BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter + FROM {TableName};"; + + using var reader = await select.ExecuteReaderAsync(ct); while (await reader.ReadAsync(ct)) { rows.Add(new TensorConfig( @@ -189,30 +260,13 @@ public async Task PrunePredictedLargerThanQ8Async( return 0; } - var createCmd = connection.CreateCommand(); - createCmd.CommandText = $@" - DROP TABLE IF EXISTS {TableName}; - CREATE TABLE {TableName} ( - BaseQuant TINYINT, - Embeddings TINYINT, - LmHead TINYINT, - AttnQ TINYINT, - AttnKV TINYINT, - AttnOutput TINYINT, - FfnUpGate TINYINT, - FfnDown TINYINT, - MoeExperts TINYINT, - MoeRouter TINYINT - );"; - await createCmd.ExecuteNonQueryAsync(ct); - - await BulkInsertAsync(connection, kept, ct); + await RecreateTableAsync(connection, ct); + await BulkAppendAsync(connection, kept, "predicted-size-prune", ct); AnsiConsole.MarkupLine($"[yellow]Predicted-size pruning removed:[/] [red]{removed:N0}[/] combo(s) larger than pure Q8."); return removed; } - public async Task PruneHighPrecisionHybridCandidatesAsync(CancellationToken ct = default) { if (RuntimeSearchSpace.AllowHighPrecisionHybrids) @@ -220,6 +274,7 @@ public async Task PruneHighPrecisionHybridCandidatesAsync(CancellationToke using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); var rows = await GetRemainingTensorConfigsAsync(ct); var kept = rows.Where(x => @@ -237,30 +292,53 @@ public async Task PruneHighPrecisionHybridCandidatesAsync(CancellationToke if (removed <= 0) return 0; - var createCmd = connection.CreateCommand(); - createCmd.CommandText = $@" - DROP TABLE IF EXISTS {TableName}; - CREATE TABLE {TableName} ( - BaseQuant TINYINT, Embeddings TINYINT, LmHead TINYINT, AttnQ TINYINT, AttnKV TINYINT, - AttnOutput TINYINT, FfnUpGate TINYINT, FfnDown TINYINT, MoeExperts TINYINT, MoeRouter TINYINT - );"; - await createCmd.ExecuteNonQueryAsync(ct); - await BulkInsertAsync(connection, kept, ct); + await RecreateTableAsync(connection, ct); + await BulkAppendAsync(connection, kept, "high-precision-prune", ct); + return removed; } - private async Task GetRowCountAsync(DuckDBConnection connection, CancellationToken ct) + private async Task HasExpectedTableShapeAsync(DuckDBConnection connection, CancellationToken ct) { - var checkCmd = connection.CreateCommand(); - checkCmd.CommandText = $"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{TableName}'"; - var exists = (long)(await checkCmd.ExecuteScalarAsync(ct) ?? 0); + using var existsCmd = connection.CreateCommand(); + existsCmd.CommandText = $"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{TableName}'"; + long exists = (long)(await existsCmd.ExecuteScalarAsync(ct) ?? 0L); if (exists == 0) - return -1; + return false; - var countCmd = connection.CreateCommand(); + var actual = new List(); + + using var shapeCmd = connection.CreateCommand(); + shapeCmd.CommandText = $@" + SELECT lower(data_type) + FROM information_schema.columns + WHERE table_name = '{TableName}' + ORDER BY ordinal_position;"; + + using var reader = await shapeCmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + { + actual.Add(Convert.ToString(reader.GetValue(0)) ?? string.Empty); + } + + if (actual.Count != ExpectedColumnTypes.Length) + return false; + + for (int i = 0; i < ExpectedColumnTypes.Length; i++) + { + if (!string.Equals(actual[i], ExpectedColumnTypes[i], StringComparison.Ordinal)) + return false; + } + + return true; + } + + private async Task GetRowCountAsync(DuckDBConnection connection, CancellationToken ct) + { + using var countCmd = connection.CreateCommand(); countCmd.CommandText = $"SELECT COUNT(*) FROM {TableName}"; - return (long)(await countCmd.ExecuteScalarAsync(ct) ?? 0); + return (long)(await countCmd.ExecuteScalarAsync(ct) ?? 0L); } private async Task RebuildDatabaseAsync( @@ -268,78 +346,171 @@ private async Task RebuildDatabaseAsync( BigInteger expectedTotal, CancellationToken ct) { - var sw = Stopwatch.StartNew(); - - var createCmd = connection.CreateCommand(); - createCmd.CommandText = $@" - DROP TABLE IF EXISTS {TableName}; - CREATE TABLE {TableName} ( - BaseQuant TINYINT, - Embeddings TINYINT, - LmHead TINYINT, - AttnQ TINYINT, - AttnKV TINYINT, - AttnOutput TINYINT, - FfnUpGate TINYINT, - FfnDown TINYINT, - MoeExperts TINYINT, - MoeRouter TINYINT - );"; - await createCmd.ExecuteNonQueryAsync(ct); + long totalTarget = (long)expectedTotal; + + AnsiConsole.MarkupLine($"[yellow]Starting bulk insert of {totalTarget:N0} rows...[/]"); + AnsiConsole.MarkupLine( + $"[grey]Generator batch size:[/] {GeneratorBatchSize:N0} [grey]| Appender heartbeat:[/] every {InsertProgressLogEveryRows:N0} rows"); - long insertedTotal = 0; - var bases = RuntimeSearchSpace.GetActiveCombinationBaselines(); + await RecreateTableAsync(connection, ct); + await ConfigureFastLoadSessionAsync(connection, ct); - AnsiConsole.MarkupLine($"Starting bulk insert of {expectedTotal:N0} rows..."); + long insertedGrandTotal = 0; + var overallSw = Stopwatch.StartNew(); - foreach (var baseline in bases) + using DuckDBAppender appender = connection.CreateAppender(TableName); + + foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) { - foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches(baseline, ct: ct)) + ct.ThrowIfCancellationRequested(); + + long baseInserted = 0; + int baseBatchNumber = 0; + var baseSw = Stopwatch.StartNew(); + string baseName = baseline.Names.FirstOrDefault() ?? baseline.UniqueId.ToString(); + + AnsiConsole.MarkupLine($"[cyan]Generating + inserting base:[/] [bold]{Markup.Escape(baseName)}[/]"); + + foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches(baseline, batchSize: GeneratorBatchSize)) { - await BulkInsertAsync(connection, batch, ct); - insertedTotal += batch.Count; - AnsiConsole.MarkupLine($" Inserted batch... Total so far: {insertedTotal:N0}"); + ct.ThrowIfCancellationRequested(); + + baseBatchNumber++; + int batchCount = batch.Count; + var batchSw = Stopwatch.StartNew(); + + AnsiConsole.MarkupLine( + $" [grey]Base batch #{baseBatchNumber} generated:[/] {batchCount:N0} rows [grey]| Base inserted before batch:[/] {baseInserted:N0}"); + + var progress = new InsertProgress + { + InsertedTotal = insertedGrandTotal, + LastLoggedTotal = insertedGrandTotal, + ProgressLogEveryRows = InsertProgressLogEveryRows, + TotalTarget = totalTarget, + BaseName = baseName, + BatchNumber = baseBatchNumber + }; + + AppendRows(appender, batch, progress, overallSw, ct); + + insertedGrandTotal = progress.InsertedTotal; + baseInserted += batchCount; + + batchSw.Stop(); + + double grandPct = totalTarget == 0 ? 100d : insertedGrandTotal * 100d / totalTarget; + + AnsiConsole.MarkupLine( + $" [green]Base batch #{baseBatchNumber} done:[/] {batchCount:N0} rows in {batchSw.Elapsed.TotalSeconds:N1}s " + + $"[grey]| Base running:[/] {baseInserted:N0} [grey]| Grand total:[/] {insertedGrandTotal:N0}/{totalTarget:N0} ({grandPct:N2}%)"); + + batch.Clear(); } + + baseSw.Stop(); + + double rowsPerSec = baseSw.Elapsed.TotalSeconds <= 0 + ? 0 + : baseInserted / baseSw.Elapsed.TotalSeconds; + + AnsiConsole.MarkupLine( + $"[bold green]Base complete:[/] {Markup.Escape(baseName)} " + + $"[grey]| Inserted:[/] {baseInserted:N0} rows " + + $"[grey]| Time:[/] {baseSw.Elapsed.TotalMinutes:N2} min " + + $"[grey]| Rate:[/] {rowsPerSec:N0} rows/sec"); } - sw.Stop(); - AnsiConsole.MarkupLine($"DuckDB rebuild complete! in {sw.Elapsed.TotalSeconds:F2}s"); + appender.Close(); + overallSw.Stop(); + + long finalCount = await GetRowCountAsync(connection, ct); + + double finalRate = overallSw.Elapsed.TotalSeconds <= 0 + ? 0 + : insertedGrandTotal / overallSw.Elapsed.TotalSeconds; + + AnsiConsole.MarkupLine( + $"[bold green]DuckDB rebuild complete.[/] " + + $"[grey]| Inserted tracked:[/] {insertedGrandTotal:N0} " + + $"[grey]| Final row count:[/] {finalCount:N0} " + + $"[grey]| Time:[/] {overallSw.Elapsed.TotalMinutes:N2} min " + + $"[grey]| Avg rate:[/] {finalRate:N0} rows/sec"); } - private async Task BulkInsertAsync( + private async Task BulkAppendAsync( DuckDBConnection connection, IReadOnlyCollection rows, + string label, CancellationToken ct) { if (rows.Count == 0) return; - using var tx = connection.BeginTransaction(); + await ConfigureFastLoadSessionAsync(connection, ct); - foreach (var row in rows) + using DuckDBAppender appender = connection.CreateAppender(TableName); + + var progress = new InsertProgress { - var cmd = connection.CreateCommand(); - cmd.Transaction = tx; - cmd.CommandText = $@" - INSERT INTO {TableName} - (BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; - - cmd.Parameters.Add(new DuckDBParameter { Value = row.BaseQuant }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.Embeddings }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.LmHead }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.AttnQ }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.AttnKV }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.AttnOutput }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.FfnUpGate }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.FfnDown }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.MoeExperts }); - cmd.Parameters.Add(new DuckDBParameter { Value = row.MoeRouter }); + InsertedTotal = 0, + LastLoggedTotal = 0, + ProgressLogEveryRows = InsertProgressLogEveryRows, + TotalTarget = rows.Count, + BaseName = label, + BatchNumber = 1 + }; + + AppendRows(appender, rows, progress, Stopwatch.StartNew(), ct); + appender.Close(); + } - await cmd.ExecuteNonQueryAsync(ct); + private static void AppendRows( + DuckDBAppender appender, + IReadOnlyCollection rows, + InsertProgress progress, + Stopwatch overallSw, + CancellationToken ct) + { + foreach (var row in rows) + { + ct.ThrowIfCancellationRequested(); + + appender.CreateRow() + .AppendValue(row.BaseQuant) + .AppendValue(row.Embeddings) + .AppendValue(row.LmHead) + .AppendValue(row.AttnQ) + .AppendValue(row.AttnKV) + .AppendValue(row.AttnOutput) + .AppendValue(row.FfnUpGate) + .AppendValue(row.FfnDown) + .AppendValue(row.MoeExperts) + .AppendValue(row.MoeRouter) + .EndRow(); + + progress.InsertedTotal++; + + if (progress.InsertedTotal - progress.LastLoggedTotal >= progress.ProgressLogEveryRows) + { + double elapsedSeconds = Math.Max(0.001, overallSw.Elapsed.TotalSeconds); + double rowsPerSecond = progress.InsertedTotal / elapsedSeconds; + double pct = progress.TotalTarget <= 0 ? 100d : progress.InsertedTotal * 100d / progress.TotalTarget; + + long remaining = Math.Max(0, progress.TotalTarget - progress.InsertedTotal); + double etaSeconds = rowsPerSecond <= 0 ? 0 : remaining / rowsPerSecond; + var eta = TimeSpan.FromSeconds(etaSeconds); + + AnsiConsole.MarkupLine( + $" [grey]Progress[/] [green]{progress.InsertedTotal:N0}[/]/[yellow]{progress.TotalTarget:N0}[/] " + + $"({pct:N2}%) [grey]| Rate:[/] {rowsPerSecond:N0}/sec " + + $"[grey]| ETA:[/] {eta:hh\\:mm\\:ss} " + + $"[grey]| Label:[/] {Markup.Escape(progress.BaseName)} " + + $"[grey]| Batch:[/] {progress.BatchNumber}"); + + progress.LastLoggedTotal = progress.InsertedTotal; + } } - - tx.Commit(); } private async Task BuildPredictionContextAsync( @@ -392,7 +563,7 @@ INSERT INTO {TableName} continue; long delta = (long)snap.SizeBytes - (long)carrier.SizeBytes; - deltaByGroupAndCandidate[(plan.TargetGroupId.Value, plan.TestedCandidateId.Value)] = delta; + deltaByGroupAndCandidate[(plan.TargetGroupId!.Value, plan.TestedCandidateId!.Value)] = delta; } return new PredictionContext( @@ -416,18 +587,18 @@ INSERT INTO {TableName} c => c.Id, (b, c) => new { b, c }) .FirstOrDefaultAsync(x => - x.b.AiModelHashId == modelId && - x.b.ImatrixDefinitionId == imatrixDefinitionId && - x.c.BaseQuant == lookup.BaseQuant && - x.c.Embeddings == lookup.Embeddings && - x.c.LmHead == lookup.LmHead && - x.c.AttnQ == lookup.AttnQ && - x.c.AttnKV == lookup.AttnKV && - x.c.AttnOutput == lookup.AttnOutput && - x.c.FfnUpGate == lookup.FfnUpGate && - x.c.FfnDown == lookup.FfnDown && - x.c.MoeExperts == lookup.MoeExperts && - x.c.MoeRouter == lookup.MoeRouter, + x.b.AiModelHashId == modelId && + x.b.ImatrixDefinitionId == imatrixDefinitionId && + x.c.BaseQuant == lookup.BaseQuant && + x.c.Embeddings == lookup.Embeddings && + x.c.LmHead == lookup.LmHead && + x.c.AttnQ == lookup.AttnQ && + x.c.AttnKV == lookup.AttnKV && + x.c.AttnOutput == lookup.AttnOutput && + x.c.FfnUpGate == lookup.FfnUpGate && + x.c.FfnDown == lookup.FfnDown && + x.c.MoeExperts == lookup.MoeExperts && + x.c.MoeRouter == lookup.MoeRouter, ct); if (row == null) @@ -441,6 +612,16 @@ private sealed class BenchmarkRow public ulong SizeBytes { get; set; } } + private sealed class InsertProgress + { + public long InsertedTotal { get; set; } + public long LastLoggedTotal { get; set; } + public long ProgressLogEveryRows { get; set; } + public long TotalTarget { get; set; } + public string BaseName { get; set; } = string.Empty; + public int BatchNumber { get; set; } + } + private sealed class PredictionContext { private readonly Dictionary<(byte GroupId, byte CandidateId), long> _deltas; @@ -487,4 +668,4 @@ private void AddDelta(byte groupId, byte candidateId, ref long total) total += delta; } } -} \ No newline at end of file +} From ce373a8f529ba030f8ba44860bec99793b413afa Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 21 Apr 2026 12:59:16 -0400 Subject: [PATCH 105/258] working way better and faster. --- MagicQuant/Commands/Evolution.cs | 34 ++++----- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 55 +++++++++++++- .../Services/IsolationOptimizationService.cs | 73 ++++++++++++++----- .../Services/LearnedBaselinePruningService.cs | 34 ++++++--- 4 files changed, 146 insertions(+), 50 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 4868b0e..3fd95e5 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -12,7 +12,7 @@ namespace MagicQuant.Commands; public class Evolution : ICommand { - private const int BruteForceFinalCombinationThreshold = 1_000; + private const int BruteForceFinalCombinationThreshold = 2_000; public async Task Run(List args) { @@ -232,10 +232,18 @@ await benchmarkService.RunAllBenchmarksAsync( foreach (var note in initialAnalysis.Notes) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + SearchSpaceDebugPrinter.PrintIsolationGroupDecisions( + "Initial Probe Group Decisions", + initialAnalysis.GroupDetails, + winningLabel: "Winning candidate"); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Initial Probe Analysis"); AnsiConsole.Write(new Rule("[yellow]Continuation Isolation Samples[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine( + $"[grey]Groups continuing after early probe:[/] [cyan]{initialAnalysis.GroupsToContinue.Count:N0}[/]"); + var continuationPlan = isolationPlanner.BuildContinuationPlan( initialAnalysis.GroupsToContinue, Cache.UnusedTensorGroups); @@ -265,22 +273,10 @@ await benchmarkService.RunAllBenchmarksAsync( SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Final Isolation Optimization"); - foreach (var gd in isolationResult.GroupDetails.OrderBy(x => x.GroupName)) - { - AnsiConsole.Write( - new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") - { - Justification = Justify.Left - }); - - AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); - AnsiConsole.MarkupLine($"[green]Winning candidate:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); - AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); - AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); - - foreach (var line in gd.Candidates) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); - } + SearchSpaceDebugPrinter.PrintIsolationGroupDecisions( + "Final Isolation Group Decisions", + isolationResult.GroupDetails, + winningLabel: "Winning candidate"); var comboCountAfterRulePruning = ComboCounter.CountAll(); @@ -309,7 +305,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Final surviving combinations:[/] {finalRemainingCombinationCount:N0}"); - if (finalRemainingCombinationCount <= BruteForceFinalCombinationThreshold+1000) + if (finalRemainingCombinationCount <= BruteForceFinalCombinationThreshold) { AnsiConsole.Write(new Rule("[yellow]Final Brute Force Benchmark Phase[/]") { Justification = Justify.Left }); @@ -381,4 +377,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} +} \ No newline at end of file diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index 539470e..8c04e9b 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -1,6 +1,8 @@ using System; using System.Linq; +using System.Collections.Generic; using System.Numerics; +using MagicQuant.Services; using MQ.DB; using MQ.DB.Models; using Spectre.Console; @@ -57,7 +59,7 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc var learned = RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group); var parts = learned.Select(x => - $"{x.Candidate.Names[0]} (expected={string.Join("/", x.ExpectedTensorWeightSchemeIds)}, matched={string.Join("/", x.MatchedTensorWeightSchemeIds)})"); + $"{x.Candidate.Names[0]} (expected={FormatSchemeIds(x.ExpectedTensorWeightSchemeIds)}, matched={FormatSchemeIds(x.MatchedTensorWeightSchemeIds)}, missing={FormatSchemeIds(x.MissingTensorWeightSchemeIds)})"); AnsiConsole.MarkupLine( $" [yellow]- {Markup.Escape(group.Name)}[/] :: [grey]{Markup.Escape(string.Join(", ", parts))}[/]"); @@ -100,4 +102,55 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc AnsiConsole.MarkupLine($"[bold yellow]Grand total:[/] {ComboCounter.CountAll():N0}"); } + public static void PrintIsolationGroupDecisions( + string title, + IEnumerable decisions, + string winningLabel = "Winning candidate") + { + var ordered = decisions + .OrderBy(x => x.GroupName, StringComparer.Ordinal) + .ToList(); + + if (ordered.Count == 0) + return; + + AnsiConsole.Write(new Rule($"[yellow]{Markup.Escape(title)}[/]") { Justification = Justify.Left }); + + foreach (var gd in ordered) + { + AnsiConsole.Write( + new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") + { + Justification = Justify.Left + }); + + AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); + AnsiConsole.MarkupLine($"[green]{Markup.Escape(winningLabel)}:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); + AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); + AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); + + foreach (var line in gd.Candidates) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); + } + } + + private static string FormatSchemeIds(IEnumerable schemeIds) + { + var ids = schemeIds + .Distinct() + .OrderBy(x => x) + .ToList(); + + if (ids.Count == 0) + return ""; + + var parts = ids.Select(id => + { + var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); + return scheme?.Names[0] ?? id.ToString(); + }); + + return string.Join("/", parts); + } + } diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index a3cb14d..98ac21d 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -110,13 +110,15 @@ public async Task AnalyzeInitialIsolationProbesA decision.Candidates.Add( $"{candidate.Names[0]} | size={(snap.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={reduction:P2} | kld={kld:G6} | pplΔ={pplDelta:F4}%"); + AppendLearnedEarlyPruneLines(group, decision, excludeCandidateId: candidate.UniqueId); + if (reduction < options.MinMeaningfulGroupReductionRatio) { RuntimeSearchSpace.BanAllExplicitCombinationCandidatesForGroup(group); decision.ExplicitQuantBanned = true; result.Notes.Add( - $"Early stop for '{group.Name}': smallest baseline-candidate probe '{candidate.Names[0]}' only saved {reduction:P2}, below {options.MinMeaningfulGroupReductionRatio:P2}. Explicit baseline-candidate exploration removed for this group."); + $"Early stop for '{group.Name}': smallest baseline-candidate probe '{candidate.Names[0]}' only saved {reduction:P2}, below {options.MinMeaningfulGroupReductionRatio:P2}. Explicit baseline-candidate exploration removed for this group and continuation isolation samples were skipped."); result.GroupDetails.Add(decision); continue; @@ -133,6 +135,13 @@ public async Task AnalyzeInitialIsolationProbesA $"Suppressed BF16 explicit candidate for '{group.Name}' because smallest baseline-candidate probe already saved {reduction:P2}."); } + int continuationCandidatesRemaining = RuntimeSearchSpace + .GetAllowedRealExplicitCombinationCandidatesForGroup(group) + .Count(x => x.UniqueId != candidate.UniqueId); + + result.Notes.Add( + $"Early probe kept '{group.Name}' alive with smallest candidate '{candidate.Names[0]}' ({reduction:P2} savings). Remaining continuation candidates: {continuationCandidatesRemaining:N0}."); + result.GroupDetails.Add(decision); } @@ -238,6 +247,7 @@ public async Task AnalyzeAndApplyFinalAsync( if (candidates.Count == 0) { PopulateFinalGroupFlags(group, decision, result); + AppendLearnedEarlyPruneLines(group, decision); result.GroupDetails.Add(decision); continue; } @@ -256,25 +266,7 @@ public async Task AnalyzeAndApplyFinalAsync( } var survivorIds = candidates.Select(x => x.CandidateBaseline.UniqueId).ToHashSet(); - foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group)) - { - if (survivorIds.Contains(banInfo.Candidate.UniqueId)) - continue; - - string expected = banInfo.ExpectedTensorWeightSchemeIds.Count == 0 - ? "" - : string.Join(", ", banInfo.ExpectedTensorWeightSchemeIds); - string matched = banInfo.MatchedTensorWeightSchemeIds.Count == 0 - ? "" - : string.Join(", ", banInfo.MatchedTensorWeightSchemeIds); - string missing = banInfo.MissingTensorWeightSchemeIds.Count == 0 - ? "" - : string.Join(", ", banInfo.MissingTensorWeightSchemeIds); - - decision.Candidates.Add( - $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned candidate/group scheme matching " + - $"(expected schemes: {expected}; matched: {matched}; missing: {missing})."); - } + AppendLearnedEarlyPruneLines(group, decision, excludedCandidateIds: survivorIds); result.GroupDetails.Add(decision); } @@ -497,6 +489,47 @@ private static int GetCandidateSafetyScore(BaselineQuants candidate) return 0; } + + private static void AppendLearnedEarlyPruneLines( + TensorGroup group, + IsolationGroupDecision decision, + byte? excludeCandidateId = null, + ISet? excludedCandidateIds = null) + { + foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group)) + { + if (excludeCandidateId.HasValue && banInfo.Candidate.UniqueId == excludeCandidateId.Value) + continue; + + if (excludedCandidateIds != null && excludedCandidateIds.Contains(banInfo.Candidate.UniqueId)) + continue; + + decision.Candidates.Add( + $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned-baseline mapping for this group " + + $"(expected schemes: {FormatSchemeIds(banInfo.ExpectedTensorWeightSchemeIds)}; " + + $"matched: {FormatSchemeIds(banInfo.MatchedTensorWeightSchemeIds)}; " + + $"missing: {FormatSchemeIds(banInfo.MissingTensorWeightSchemeIds)})."); + } + } + + private static string FormatSchemeIds(IEnumerable schemeIds) + { + var ids = schemeIds + .Distinct() + .OrderBy(x => x) + .ToList(); + + if (ids.Count == 0) + return ""; + + return string.Join(", ", ids.Select(id => + { + var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); + return scheme?.Names[0] ?? id.ToString(); + })); + } + + private async Task LoadSnapshotAsync(HybridQuant quant, CancellationToken ct) { await using var db = new MagicQuantContext(); diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index deade76..d628c7a 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -89,7 +89,8 @@ internal static void ApplyLearnedBaselinePruning( var effectiveSchemesByCandidateAndGroup = BuildEffectiveSchemesByBaselineAndGroup(learnedRows, aliasToSchemeIds); var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) - .OrderBy(x => x.UniqueId) + .OrderBy(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId) .ToList(); foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) @@ -111,11 +112,9 @@ internal static void ApplyLearnedBaselinePruning( var effectiveIdsSet = hasEffectiveSet ? effectiveForGroup! : new HashSet(); var matchedIds = expectedIds.Where(effectiveIdsSet.Contains).OrderBy(x => x).ToList(); bool allow = matchedIds.Count > 0; - string effectiveIds = hasEffectiveSet - ? string.Join(",", effectiveIdsSet.OrderBy(x => x)) - : ""; - string expected = string.Join(",", expectedIds); - string matched = matchedIds.Count > 0 ? string.Join(",", matchedIds) : ""; + string effectiveIds = FormatSchemeIds(effectiveIdsSet); + string expected = FormatSchemeIds(expectedIds); + string matched = FormatSchemeIds(matchedIds); result.Notes.Add( $"Learned-prune check: model={aiModelHashId}/{aiModelHashUniqueHash}, group={group.Name}, " + @@ -152,11 +151,10 @@ internal static void ApplyLearnedBaselinePruning( } // The persisted TensorWeightSchemeId is the authoritative learned-family identity. - // FinalQuantType is useful extra metadata, but it cannot replace the stored scheme id - // because some learned baselines materialize tensors whose final emitted token differs - // from the baseline family we are learning from. set.Add(row.TensorWeightSchemeId); + // Also record any alias-based resolution from the actual emitted quant token so the + // logs stay explainable when llama.cpp materializes a family using synonymous names. if (aliasToSchemeIds.TryGetValue(CanonicalizeQuantToken(row.FinalQuantType), out var resolvedIds)) { foreach (var resolvedId in resolvedIds) @@ -204,4 +202,20 @@ private static string CanonicalizeQuantToken(string value) .Replace(" ", string.Empty) .ToUpperInvariant(); } -} \ No newline at end of file + private static string FormatSchemeIds(IEnumerable schemeIds) + { + var ids = schemeIds + .Distinct() + .OrderBy(x => x) + .ToList(); + + if (ids.Count == 0) + return ""; + + return string.Join("/", ids.Select(id => + { + var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); + return scheme?.Names[0] ?? id.ToString(); + })); + } +} From 122ead8a06786cae2ff77f598eded9bbb9fa0d0c Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 21 Apr 2026 13:35:52 -0400 Subject: [PATCH 106/258] updates --- MagicQuant/Commands/Evolution.cs | 107 +++++++++-- .../Services/IsolationOptimizationService.cs | 179 +++++++++++++----- .../Services/LearnedBaselinePruningService.cs | 113 ++++++++--- 3 files changed, 307 insertions(+), 92 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 3fd95e5..2f62f4d 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -200,6 +200,40 @@ await benchmarkService.RunAllBenchmarksAsync( var comboCountBefore = ComboCounter.CountAll(); var learnedBaselinePruner = new LearnedBaselinePruningService(); + var totalLearnedPruningResult = new LearnedBaselinePruningResult(); + + if (!Cache.ForceRelearnBaselineTensorMappings) + { + var coverageStatus = await learnedBaselinePruner.GetCoverageStatusAsync(); + if (coverageStatus.SafeToApplyBeforeStartup) + { + AnsiConsole.Write(new Rule("[yellow]Pre-Startup Learned Baseline Pruning[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine( + $"[grey]Using existing learned baseline coverage before startup sampling:[/] [cyan]{coverageStatus.PresentCandidateGroupPairs:N0}[/]/[cyan]{coverageStatus.ExpectedCandidateGroupPairs:N0}[/] candidate-group pairs."); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Pre-Startup Learned-Baseline Pruning"); + var preStartupLearnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); + MergeLearnedPruningResults(totalLearnedPruningResult, preStartupLearnedPruningResult); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Pre-Startup Learned-Baseline Pruning"); + + foreach (var note in preStartupLearnedPruningResult.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + } + else if (coverageStatus.HasAnyLearnedRows) + { + AnsiConsole.MarkupLine( + $"[grey]Skipping pre-startup learned pruning because learned coverage is incomplete for the current explicit candidate universe ({coverageStatus.PresentCandidateGroupPairs:N0}/{coverageStatus.ExpectedCandidateGroupPairs:N0} candidate-group pairs present).[/]"); + } + else + { + AnsiConsole.MarkupLine("[grey]Skipping pre-startup learned pruning because no learned baseline rows exist yet for this model.[/]"); + } + } + else + { + AnsiConsole.MarkupLine("[grey]Skipping pre-startup learned pruning because --relearn-baseline-mappings was requested.[/]"); + } + AnsiConsole.Write(new Rule("[yellow]Initial Isolation Startup Samples[/]") { Justification = Justify.Left }); var isolationPlanner = new IsolationPlanningService(); @@ -213,13 +247,14 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {initialSummary.Skipped:N0}"); AnsiConsole.MarkupLine($" [red]Failed:[/] {initialSummary.Failed:N0}"); - AnsiConsole.MarkupLine("[bold magenta]Evolution flow marker:[/] startup sampling finished, entering learned-baseline pruning."); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning"); + AnsiConsole.MarkupLine("[bold magenta]Evolution flow marker:[/] startup sampling finished, refreshing learned-baseline pruning before initial probe analysis."); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning Refresh"); - AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning[/]") { Justification = Justify.Left }); + AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning Refresh[/]") { Justification = Justify.Left }); var learnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); + MergeLearnedPruningResults(totalLearnedPruningResult, learnedPruningResult); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Learned-Baseline Pruning"); + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Learned-Baseline Pruning Refresh"); foreach (var note in learnedPruningResult.Notes) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); @@ -229,20 +264,17 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.Write(new Rule("[yellow]Initial Probe Analysis[/]") { Justification = Justify.Left }); var initialAnalysis = await isolationOptimizer.AnalyzeInitialIsolationProbesAsync(initialPlan); + AnsiConsole.Write(new Rule("[yellow]Initial Probe Group Decisions[/]") { Justification = Justify.Left }); + PrintIsolationGroupDecisions(initialAnalysis.GroupDetails); + foreach (var note in initialAnalysis.Notes) AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); - SearchSpaceDebugPrinter.PrintIsolationGroupDecisions( - "Initial Probe Group Decisions", - initialAnalysis.GroupDetails, - winningLabel: "Winning candidate"); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Initial Probe Analysis"); AnsiConsole.Write(new Rule("[yellow]Continuation Isolation Samples[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine( - $"[grey]Groups continuing after early probe:[/] [cyan]{initialAnalysis.GroupsToContinue.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Groups continuing after early probe:[/] [cyan]{initialAnalysis.GroupsToContinue.Count:N0}[/]"); var continuationPlan = isolationPlanner.BuildContinuationPlan( initialAnalysis.GroupsToContinue, @@ -273,10 +305,22 @@ await benchmarkService.RunAllBenchmarksAsync( SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Final Isolation Optimization"); - SearchSpaceDebugPrinter.PrintIsolationGroupDecisions( - "Final Isolation Group Decisions", - isolationResult.GroupDetails, - winningLabel: "Winning candidate"); + foreach (var gd in isolationResult.GroupDetails.OrderBy(x => x.GroupName)) + { + AnsiConsole.Write( + new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") + { + Justification = Justify.Left + }); + + AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); + AnsiConsole.MarkupLine($"[green]Winning candidate:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); + AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); + AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); + + foreach (var line in gd.Candidates) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); + } var comboCountAfterRulePruning = ComboCounter.CountAll(); @@ -285,8 +329,8 @@ await benchmarkService.RunAllBenchmarksAsync( long predictedSizePruned = await dbService.PrunePredictedLargerThanQ8Async(mergedPlan); long highPrecisionPruned = await dbService.PruneHighPrecisionHybridCandidatesAsync(); - AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {learnedPruningResult.GroupCandidateEliminations:N0}"); - AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {learnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0}"); + AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {totalLearnedPruningResult.GroupCandidateEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {totalLearnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0}"); AnsiConsole.MarkupLine($"[green]Groups reduced to explicit-banned->Q8-fallback:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); @@ -337,6 +381,35 @@ await benchmarkService.RunAllBenchmarksAsync( } } + private static void MergeLearnedPruningResults(LearnedBaselinePruningResult target, LearnedBaselinePruningResult source) + { + target.GroupCandidateEliminations += source.GroupCandidateEliminations; + target.BaselinesSkippedWithoutLearnedRows += source.BaselinesSkippedWithoutLearnedRows; + + foreach (var note in source.Notes) + target.Notes.Add(note); + } + + private static void PrintIsolationGroupDecisions(IEnumerable decisions) + { + foreach (var gd in decisions.OrderBy(x => x.GroupName)) + { + AnsiConsole.Write( + new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") + { + Justification = Justify.Left + }); + + AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); + AnsiConsole.MarkupLine($"[green]Winning candidate:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); + AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); + AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); + + foreach (var line in gd.Candidates) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); + } + } + private void ShowEvolutionHelp() { AnsiConsole.MarkupLine("[bold yellow]Command: evolution[/]"); diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 98ac21d..463bef1 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -110,7 +110,6 @@ public async Task AnalyzeInitialIsolationProbesA decision.Candidates.Add( $"{candidate.Names[0]} | size={(snap.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={reduction:P2} | kld={kld:G6} | pplΔ={pplDelta:F4}%"); - AppendLearnedEarlyPruneLines(group, decision, excludeCandidateId: candidate.UniqueId); if (reduction < options.MinMeaningfulGroupReductionRatio) { @@ -118,7 +117,7 @@ public async Task AnalyzeInitialIsolationProbesA decision.ExplicitQuantBanned = true; result.Notes.Add( - $"Early stop for '{group.Name}': smallest baseline-candidate probe '{candidate.Names[0]}' only saved {reduction:P2}, below {options.MinMeaningfulGroupReductionRatio:P2}. Explicit baseline-candidate exploration removed for this group and continuation isolation samples were skipped."); + $"Early stop for '{group.Name}': smallest baseline-candidate probe '{candidate.Names[0]}' only saved {reduction:P2}, below {options.MinMeaningfulGroupReductionRatio:P2}. Explicit baseline-candidate exploration removed for this group."); result.GroupDetails.Add(decision); continue; @@ -126,6 +125,9 @@ public async Task AnalyzeInitialIsolationProbesA result.GroupsToContinue.Add(group.UniqueId); + result.Notes.Add( + $"Continuation enabled for '{group.Name}': smallest baseline-candidate probe '{candidate.Names[0]}' saved {reduction:P2}. Candidate-level continuation will honor current runtime bans/prunes."); + if (reduction >= IsolationPruningConfig.MinimumIsolationReductionToSuppressBf16Ratio) { RuntimeSearchSpace.SuppressBf16TensorChoice(group); @@ -135,12 +137,8 @@ public async Task AnalyzeInitialIsolationProbesA $"Suppressed BF16 explicit candidate for '{group.Name}' because smallest baseline-candidate probe already saved {reduction:P2}."); } - int continuationCandidatesRemaining = RuntimeSearchSpace - .GetAllowedRealExplicitCombinationCandidatesForGroup(group) - .Count(x => x.UniqueId != candidate.UniqueId); - - result.Notes.Add( - $"Early probe kept '{group.Name}' alive with smallest candidate '{candidate.Names[0]}' ({reduction:P2} savings). Remaining continuation candidates: {continuationCandidatesRemaining:N0}."); + await ApplyEarlyCandidatePruningForContinuingGroupAsync(group, decision, ct); + AppendLearnedPrunedCandidates(group, decision); result.GroupDetails.Add(decision); } @@ -148,6 +146,85 @@ public async Task AnalyzeInitialIsolationProbesA return result; } + private static async Task ApplyEarlyCandidatePruningForContinuingGroupAsync( + TensorGroup group, + IsolationGroupDecision decision, + CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + return; + + await using var db = new MagicQuantContext(); + + var aiModelHashId = await db.AiModelHashes + .AsNoTracking() + .Where(x => x.UniqueHash == Cache.CurrentModelId) + .Select(x => (uint?)x.Id) + .FirstOrDefaultAsync(ct); + + if (aiModelHashId == null) + return; + + var learnedRows = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == aiModelHashId.Value && x.TensorGroupId == group.UniqueId) + .Select(x => new LearnedBaselinePruningService.LearnedRow( + x.BaselineQuantId, + x.TensorWeightSchemeId, + x.TensorGroupId, + x.FinalQuantType)) + .ToListAsync(ct); + + if (learnedRows.Count == 0) + return; + + var aliasToSchemeIds = LearnedBaselinePruningService.BuildAliasToSchemeIds(); + var effectiveSchemesByCandidateAndGroup = LearnedBaselinePruningService.BuildEffectiveSchemesByBaselineAndGroup( + learnedRows, + aliasToSchemeIds); + + var candidates = BaselineQuants.GetGroupCombinationCandidatesSmallestFirst( + RuntimeSearchSpace.HasUsableImatrix(), + allowHighPrecisionHybrids: false) + .Where(x => !x.BannedGroupIds.Contains(group.UniqueId)) + .ToList(); + + foreach (var candidate in candidates) + { + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) + continue; + + var expectedIds = candidate.LearnedMatchTensorWeightSchemes + .Select(x => x.UniqueId) + .Distinct() + .OrderBy(x => x) + .ToList(); + + if (expectedIds.Count == 0) + continue; + + var key = (candidate.UniqueId, group.UniqueId); + effectiveSchemesByCandidateAndGroup.TryGetValue(key, out var effectiveIdsSet); + effectiveIdsSet ??= new HashSet(); + + var matchedIds = expectedIds + .Where(effectiveIdsSet.Contains) + .OrderBy(x => x) + .ToList(); + + if (matchedIds.Count > 0) + continue; + + RuntimeSearchSpace.BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( + group, + candidate, + expectedTensorWeightSchemeIds: expectedIds, + matchedTensorWeightSchemeIds: matchedIds, + note: "Early continuation gate removed candidate because learned tensor schemes for this group do not match the candidate family."); + } + } + + public async Task AnalyzeAndApplyFinalAsync( RequiredSampleGenerationResult fullPlan, IsolationOptimizationOptions? options = null, @@ -247,7 +324,6 @@ public async Task AnalyzeAndApplyFinalAsync( if (candidates.Count == 0) { PopulateFinalGroupFlags(group, decision, result); - AppendLearnedEarlyPruneLines(group, decision); result.GroupDetails.Add(decision); continue; } @@ -266,7 +342,25 @@ public async Task AnalyzeAndApplyFinalAsync( } var survivorIds = candidates.Select(x => x.CandidateBaseline.UniqueId).ToHashSet(); - AppendLearnedEarlyPruneLines(group, decision, excludedCandidateIds: survivorIds); + foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group)) + { + if (survivorIds.Contains(banInfo.Candidate.UniqueId)) + continue; + + string expected = banInfo.ExpectedTensorWeightSchemeIds.Count == 0 + ? "" + : string.Join(", ", banInfo.ExpectedTensorWeightSchemeIds); + string matched = banInfo.MatchedTensorWeightSchemeIds.Count == 0 + ? "" + : string.Join(", ", banInfo.MatchedTensorWeightSchemeIds); + string missing = banInfo.MissingTensorWeightSchemeIds.Count == 0 + ? "" + : string.Join(", ", banInfo.MissingTensorWeightSchemeIds); + + decision.Candidates.Add( + $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned candidate/group scheme matching " + + $"(expected schemes: {expected}; matched: {matched}; missing: {missing})."); + } result.GroupDetails.Add(decision); } @@ -301,6 +395,30 @@ public async Task AnalyzeAndApplyFinalAsync( return result; } + private static void AppendLearnedPrunedCandidates(TensorGroup group, IsolationGroupDecision decision) + { + var learnedPruned = RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group); + if (learnedPruned.Count == 0) + return; + + foreach (var ban in learnedPruned.OrderBy(x => x.Candidate.ExplicitCandidateSortOrder).ThenBy(x => x.Candidate.UniqueId)) + { + decision.Candidates.Add( + $"[pruned-early] {ban.Candidate.Names[0]} removed by learned-baseline mapping for this group " + + $"(no matching tensor weights in baseline(s): {FormatSchemeNames(ban.ExpectedTensorWeightSchemeIds)})." ); + } + } + + private static string FormatSchemeNames(IEnumerable schemeIds) + { + var names = schemeIds + .Distinct() + .Select(id => TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id)?.Names[0] ?? id.ToString()) + .ToList(); + + return names.Count == 0 ? "" : string.Join(", ", names); + } + private static bool IsHighPrecisionCandidate(BaselineQuants candidate) => BaselineQuants.IsNativeExactAlias(candidate); @@ -489,47 +607,6 @@ private static int GetCandidateSafetyScore(BaselineQuants candidate) return 0; } - - private static void AppendLearnedEarlyPruneLines( - TensorGroup group, - IsolationGroupDecision decision, - byte? excludeCandidateId = null, - ISet? excludedCandidateIds = null) - { - foreach (var banInfo in RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(group)) - { - if (excludeCandidateId.HasValue && banInfo.Candidate.UniqueId == excludeCandidateId.Value) - continue; - - if (excludedCandidateIds != null && excludedCandidateIds.Contains(banInfo.Candidate.UniqueId)) - continue; - - decision.Candidates.Add( - $"[pruned-early] {banInfo.Candidate.Names[0]} removed by learned-baseline mapping for this group " + - $"(expected schemes: {FormatSchemeIds(banInfo.ExpectedTensorWeightSchemeIds)}; " + - $"matched: {FormatSchemeIds(banInfo.MatchedTensorWeightSchemeIds)}; " + - $"missing: {FormatSchemeIds(banInfo.MissingTensorWeightSchemeIds)})."); - } - } - - private static string FormatSchemeIds(IEnumerable schemeIds) - { - var ids = schemeIds - .Distinct() - .OrderBy(x => x) - .ToList(); - - if (ids.Count == 0) - return ""; - - return string.Join(", ", ids.Select(id => - { - var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); - return scheme?.Names[0] ?? id.ToString(); - })); - } - - private async Task LoadSnapshotAsync(HybridQuant quant, CancellationToken ct) { await using var db = new MagicQuantContext(); diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index d628c7a..5a11f51 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -18,6 +18,16 @@ public sealed class LearnedBaselinePruningResult public List Notes { get; } = new(); } + +public sealed class LearnedBaselineCoverageStatus +{ + public bool HasAnyLearnedRows { get; set; } + public bool SafeToApplyBeforeStartup { get; set; } + public int ExpectedCandidateGroupPairs { get; set; } + public int PresentCandidateGroupPairs { get; set; } + public List MissingPairs { get; } = new(); +} + public sealed class LearnedBaselinePruningService { internal readonly record struct LearnedRow( @@ -26,6 +36,75 @@ internal readonly record struct LearnedRow( byte TensorGroupId, string FinalQuantType); + + public async Task GetCoverageStatusAsync(CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); + + var status = new LearnedBaselineCoverageStatus(); + + await using var db = new MagicQuantContext(); + + var aiModelHash = await db.AiModelHashes + .AsNoTracking() + .Where(x => x.UniqueHash == Cache.CurrentModelId) + .Select(x => new { x.Id }) + .FirstOrDefaultAsync(ct); + + if (aiModelHash == null) + return status; + + var presentPairs = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == aiModelHash.Id) + .Select(x => new { x.BaselineQuantId, x.TensorGroupId }) + .Distinct() + .ToListAsync(ct); + + if (presentPairs.Count == 0) + return status; + + status.HasAnyLearnedRows = true; + + var present = presentPairs + .Select(x => (x.BaselineQuantId, x.TensorGroupId)) + .ToHashSet(); + + var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); + var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates( + RuntimeSearchSpace.HasUsableImatrix(), + allowHighPrecisionHybrids: false) + .OrderBy(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId) + .ToList(); + + foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) + { + if (unusedIds.Contains(group.UniqueId)) + continue; + + foreach (var candidate in explicitCandidates) + { + if (candidate.BannedGroupIds.Contains(group.UniqueId)) + continue; + + status.ExpectedCandidateGroupPairs++; + + if (present.Contains((candidate.UniqueId, group.UniqueId))) + { + status.PresentCandidateGroupPairs++; + continue; + } + + status.MissingPairs.Add($"{group.Name}:{candidate.Names[0]}"); + } + } + + status.SafeToApplyBeforeStartup = status.MissingPairs.Count == 0; + return status; + } + public async Task AnalyzeAndApplyAsync(CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) @@ -89,8 +168,7 @@ internal static void ApplyLearnedBaselinePruning( var effectiveSchemesByCandidateAndGroup = BuildEffectiveSchemesByBaselineAndGroup(learnedRows, aliasToSchemeIds); var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) - .OrderBy(x => x.ExplicitCandidateSortOrder) - .ThenBy(x => x.UniqueId) + .OrderBy(x => x.UniqueId) .ToList(); foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) @@ -112,9 +190,11 @@ internal static void ApplyLearnedBaselinePruning( var effectiveIdsSet = hasEffectiveSet ? effectiveForGroup! : new HashSet(); var matchedIds = expectedIds.Where(effectiveIdsSet.Contains).OrderBy(x => x).ToList(); bool allow = matchedIds.Count > 0; - string effectiveIds = FormatSchemeIds(effectiveIdsSet); - string expected = FormatSchemeIds(expectedIds); - string matched = FormatSchemeIds(matchedIds); + string effectiveIds = hasEffectiveSet + ? string.Join(",", effectiveIdsSet.OrderBy(x => x)) + : ""; + string expected = string.Join(",", expectedIds); + string matched = matchedIds.Count > 0 ? string.Join(",", matchedIds) : ""; result.Notes.Add( $"Learned-prune check: model={aiModelHashId}/{aiModelHashUniqueHash}, group={group.Name}, " + @@ -151,10 +231,11 @@ internal static void ApplyLearnedBaselinePruning( } // The persisted TensorWeightSchemeId is the authoritative learned-family identity. + // FinalQuantType is useful extra metadata, but it cannot replace the stored scheme id + // because some learned baselines materialize tensors whose final emitted token differs + // from the baseline family we are learning from. set.Add(row.TensorWeightSchemeId); - // Also record any alias-based resolution from the actual emitted quant token so the - // logs stay explainable when llama.cpp materializes a family using synonymous names. if (aliasToSchemeIds.TryGetValue(CanonicalizeQuantToken(row.FinalQuantType), out var resolvedIds)) { foreach (var resolvedId in resolvedIds) @@ -202,20 +283,4 @@ private static string CanonicalizeQuantToken(string value) .Replace(" ", string.Empty) .ToUpperInvariant(); } - private static string FormatSchemeIds(IEnumerable schemeIds) - { - var ids = schemeIds - .Distinct() - .OrderBy(x => x) - .ToList(); - - if (ids.Count == 0) - return ""; - - return string.Join("/", ids.Select(id => - { - var scheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == id); - return scheme?.Names[0] ?? id.ToString(); - })); - } -} +} \ No newline at end of file From 3a3a4bf63488d2e6cf503d9872d2d864c9a92918 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 21 Apr 2026 16:36:43 -0400 Subject: [PATCH 107/258] base for allowing learned behaviors from other models like Unsloth --- MQ.DB/Cache.cs | 42 +- MQ.DB/Data/MagicQuantContext.cs | 36 +- .../20260420215500_InitialCreate.Designer.cs | 602 ------------------ .../20260420215500_InitialCreate.cs | 490 -------------- .../MagicQuantContextModelSnapshot.cs | 599 ----------------- MQ.DB/Models/BaselineQuants.cs | 389 +++++++++-- .../DbModels/BaselineQuantDefinition.cs | 42 +- .../DbModels/LearnedBaselineTensorQuant.cs | 21 +- MQ.DB/Models/RequiredSamplePlan.cs | 6 +- MagicQuant/Commands/Evolution.cs | 75 ++- MagicQuant/Commands/InitializeLlamaCpp.cs | 2 +- MagicQuant/Config.cs | 100 +-- MagicQuant/Helpers/CliHelpers.cs | 1 + MagicQuant/Helpers/IsolationPruningConfig.cs | 20 +- MagicQuant/Helpers/RuntimeSearchSpace.cs | 9 +- MagicQuant/Helpers/TensorConfigGenerator.cs | 30 +- MagicQuant/MagicQuant.csproj | 7 + MagicQuant/Program.cs | 42 +- .../Services/HuggingFaceBaselineService.cs | 361 +++++++++++ MagicQuant/Services/QuantDatabaseService.cs | 14 +- MagicQuant/Services/QuantizationService.cs | 552 +++++++++------- MagicQuant/config.default.yaml | 64 ++ MagicQuant/config.dev.yaml | 50 ++ 23 files changed, 1405 insertions(+), 2149 deletions(-) delete mode 100644 MQ.DB/Migrations/20260420215500_InitialCreate.Designer.cs delete mode 100644 MQ.DB/Migrations/20260420215500_InitialCreate.cs delete mode 100644 MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs create mode 100644 MagicQuant/Services/HuggingFaceBaselineService.cs create mode 100644 MagicQuant/config.default.yaml create mode 100644 MagicQuant/config.dev.yaml diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index 87f4a0c..c2e7071 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -8,36 +8,49 @@ public class Cache /// Full path to llama.cpp repo /// public static string? LlamaRoot; - + /// /// Full path to /llama.cpp/build/bin/ /// public static string? LlamaBin; - + /// /// full path to convert_hf_to_gguf.py /// public static string? ConvertScript; - + /// /// System information about the PC that's detected /// during the initial llama cpp validation phase. /// - public static SystemInfo? SysInfo; - + public static SystemInfo? SysInfo; + /// - /// The full path to the model directory being quantized, - /// where a "MagicQuant" folder is created and used. + /// Root MagicQuant working directory. This is also where the Python + /// environment, default config files, and shared caches live. /// public static string? MagicQuantDirectory; - + /// /// Full path to the desired model directory where the safetensors are. /// public static string? ModelDirectory; - + + /// + /// Per-model MagicQuant working directory. + /// public static string? ModelMagicQuantDirectory; - + + /// + /// Absolute path to the active YAML config that was loaded for this run. + /// + public static string? ActiveConfigPath { get; set; } + + /// + /// Root directory where external/custom baseline GGUF files are staged. + /// + public static string? ExternalBaselineCacheDirectory { get; set; } + /// /// Aka BF16, F16, or F32 /// @@ -49,15 +62,14 @@ public enum MainTorchType F16 = 2, F32 = 3 } - - + /* * Groups not present in the current model graph. These are forced to NULL/ignored * by runtime search-space planning. */ - public static List UnusedTensorGroups = new List(); - - public static string CurrentModelId { get; set; } + public static List UnusedTensorGroups = new(); + + public static string CurrentModelId { get; set; } = string.Empty; public static bool ForceRelearnBaselineTensorMappings { get; set; } diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index ecd55a0..5cdc668 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -67,9 +67,22 @@ private void EnsureBaselineQuantDefinitions() .Select(x => new BaselineQuantDefinition { BaselineQuantId = x.UniqueId, + CanonicalKey = x.CanonicalKey, BaselineName = x.Names[0], + QuantizeBaseArgumentName = x.QuantizeBaseArgumentName, DefaultTensorSchemeId = x.DefaultTensorScheme!.UniqueId, - DefaultTensorSchemeName = x.DefaultTensorScheme.Names[0] + DefaultTensorSchemeName = x.DefaultTensorScheme.Names[0], + SourceKind = x.SourceKind, + SourceOwner = x.SourceOwner, + SourceRepository = x.SourceRepository, + SourceFileName = x.SourceFileName, + ShortSourceName = x.ShortSourceName, + IsCustomBaseline = x.IsCustomBaseline, + IsLearningBaseline = x.IsLearningBaseline, + IsCombinationCarrierCandidate = x.IsCombinationCarrierCandidate, + IsExplicitGroupCombinationCandidate = x.IsExplicitGroupCombinationCandidate, + RequiresImatrix = x.RequiresImatrix, + ExplicitCandidateSortOrder = x.ExplicitCandidateSortOrder }) .OrderBy(x => x.BaselineQuantId) .ToList(); @@ -90,15 +103,28 @@ private void EnsureBaselineQuantDefinitions() current.Zip(expected, (a, b) => a.BaselineQuantId == b.BaselineQuantId && a.DefaultTensorSchemeId == b.DefaultTensorSchemeId && + a.IsCustomBaseline == b.IsCustomBaseline && + a.IsLearningBaseline == b.IsLearningBaseline && + a.IsCombinationCarrierCandidate == b.IsCombinationCarrierCandidate && + a.IsExplicitGroupCombinationCandidate == b.IsExplicitGroupCombinationCandidate && + a.RequiresImatrix == b.RequiresImatrix && + a.ExplicitCandidateSortOrder == b.ExplicitCandidateSortOrder && + string.Equals(a.CanonicalKey, b.CanonicalKey, StringComparison.Ordinal) && string.Equals(a.BaselineName, b.BaselineName, StringComparison.Ordinal) && - string.Equals(a.DefaultTensorSchemeName, b.DefaultTensorSchemeName, StringComparison.Ordinal)) + string.Equals(a.QuantizeBaseArgumentName, b.QuantizeBaseArgumentName, StringComparison.Ordinal) && + string.Equals(a.DefaultTensorSchemeName, b.DefaultTensorSchemeName, StringComparison.Ordinal) && + string.Equals(a.SourceKind, b.SourceKind, StringComparison.Ordinal) && + string.Equals(a.SourceOwner, b.SourceOwner, StringComparison.Ordinal) && + string.Equals(a.SourceRepository, b.SourceRepository, StringComparison.Ordinal) && + string.Equals(a.SourceFileName, b.SourceFileName, StringComparison.Ordinal) && + string.Equals(a.ShortSourceName, b.ShortSourceName, StringComparison.Ordinal)) .Any(equal => !equal); if (mismatch) { throw new InvalidOperationException( - "BaselineQuantDefinitions table is out of sync with code-defined BaselineQuants/DefaultTensorScheme mappings. " + - "Run migrations and regenerate the DB definitions."); + "BaselineQuantDefinitions table is out of sync with the runtime baseline registry. " + + "Delete the SQLite DB, recreate migrations, and let MagicQuant reseed baseline definitions."); } } @@ -178,4 +204,4 @@ private void ValidateDbSetsImplementInterface() ); } } -} +} \ No newline at end of file diff --git a/MQ.DB/Migrations/20260420215500_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260420215500_InitialCreate.Designer.cs deleted file mode 100644 index ae122f7..0000000 --- a/MQ.DB/Migrations/20260420215500_InitialCreate.Designer.cs +++ /dev/null @@ -1,602 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260420215500_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("BaselineName") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeId") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeName") - .IsUnique(); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DiscoveryTokenTarget") - .HasColumnType("INTEGER"); - - b.Property("GroupSize") - .HasColumnType("INTEGER"); - - b.Property("HardwareFingerprint") - .IsRequired() - .HasMaxLength(1024) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("QuantizationKey") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("QuantizedModelFingerprint") - .IsRequired() - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("SlotsJson") - .IsRequired() - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("StaticNgl") - .HasColumnType("INTEGER"); - - b.Property("UpdatedUtc") - .HasColumnType("TEXT"); - - b.Property("UsesGpu") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") - .IsUnique(); - - b.ToTable("ExecutionPlanProbeCaches"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BuildFingerprint") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("CanonicalPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IdentityHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("MetadataJson") - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("TokenCount") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId", "IdentityHash") - .IsUnique(); - - b.ToTable("ImatrixDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260420215500_InitialCreate.cs b/MQ.DB/Migrations/20260420215500_InitialCreate.cs deleted file mode 100644 index d42307e..0000000 --- a/MQ.DB/Migrations/20260420215500_InitialCreate.cs +++ /dev/null @@ -1,490 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class InitialCreate : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "AiModelHashes", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - UniqueHash = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AiModelHashes", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "BaselineQuantDefinitions", - columns: table => new - { - BaselineQuantId = table.Column(type: "INTEGER", nullable: false), - BaselineName = table.Column(type: "TEXT", maxLength: 64, nullable: false), - DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), - DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.BaselineQuantId); - }); - - migrationBuilder.CreateTable( - name: "TensorCombos", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AttnKV = table.Column(type: "INTEGER", nullable: false), - AttnOutput = table.Column(type: "INTEGER", nullable: false), - AttnQ = table.Column(type: "INTEGER", nullable: false), - BaseQuant = table.Column(type: "INTEGER", nullable: false), - Embeddings = table.Column(type: "INTEGER", nullable: false), - FfnDown = table.Column(type: "INTEGER", nullable: false), - FfnUpGate = table.Column(type: "INTEGER", nullable: false), - LmHead = table.Column(type: "INTEGER", nullable: false), - MoeExperts = table.Column(type: "INTEGER", nullable: false), - MoeRouter = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_TensorCombos", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "ImatrixDefinitions", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - IdentityHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), - CanonicalPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true), - SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), - CreatedUtc = table.Column(type: "TEXT", nullable: false), - MetadataJson = table.Column(type: "TEXT", maxLength: 8000, nullable: true), - TokenCount = table.Column(type: "INTEGER", nullable: true), - BuildFingerprint = table.Column(type: "TEXT", maxLength: 512, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_ImatrixDefinitions", x => x.Id); - table.ForeignKey( - name: "FK_ImatrixDefinitions_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AiBenchmarks", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - Ngl = table.Column(type: "INTEGER", nullable: false), - SizeBytes = table.Column(type: "INTEGER", nullable: false), - TokensPerSecond = table.Column(type: "REAL", nullable: false), - TensorComboId = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AiBenchmarks", x => x.Id); - table.ForeignKey( - name: "FK_AiBenchmarks_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", - column: x => x.ImatrixDefinitionId, - principalTable: "ImatrixDefinitions", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_AiBenchmarks_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "ExecutionPlanProbeCaches", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), - HardwareFingerprint = table.Column(type: "TEXT", maxLength: 1024, nullable: false), - QuantizedModelFingerprint = table.Column(type: "TEXT", maxLength: 2048, nullable: false), - QuantizationKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), - DiscoveryTokenTarget = table.Column(type: "INTEGER", nullable: false), - StaticNgl = table.Column(type: "INTEGER", nullable: false), - UsesGpu = table.Column(type: "INTEGER", nullable: false), - GroupSize = table.Column(type: "INTEGER", nullable: false), - SlotsJson = table.Column(type: "TEXT", maxLength: 8000, nullable: false), - CreatedUtc = table.Column(type: "TEXT", nullable: false), - UpdatedUtc = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_ExecutionPlanProbeCaches", x => x.Id); - table.ForeignKey( - name: "FK_ExecutionPlanProbeCaches_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", - column: x => x.ImatrixDefinitionId, - principalTable: "ImatrixDefinitions", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "CategoryBenchmark", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiBenchmarkId = table.Column(type: "TEXT", nullable: false), - Category = table.Column(type: "INTEGER", nullable: false), - Kld = table.Column(type: "REAL", nullable: false), - Ppl = table.Column(type: "REAL", nullable: false), - PplError = table.Column(type: "REAL", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_CategoryBenchmark", x => x.Id); - table.ForeignKey( - name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "LearnedBaselineTensorQuants", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiBenchmarkId = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - BaselineQuantId = table.Column(type: "INTEGER", nullable: false), - TensorWeightSchemeId = table.Column(type: "INTEGER", nullable: false), - TensorGroupId = table.Column(type: "INTEGER", nullable: false), - TensorName = table.Column(type: "TEXT", maxLength: 512, nullable: false), - FinalQuantType = table.Column(type: "TEXT", maxLength: 32, nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_LearnedBaselineTensorQuants", x => x.Id); - table.ForeignKey( - name: "FK_LearnedBaselineTensorQuants_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_LearnedBaselineTensorQuants_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "QuantizationRuns", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), - TensorComboId = table.Column(type: "TEXT", nullable: false), - AiBenchmarkId = table.Column(type: "TEXT", nullable: true), - StartedUtc = table.Column(type: "TEXT", nullable: false), - CompletedUtc = table.Column(type: "TEXT", nullable: false), - DurationMs = table.Column(type: "INTEGER", nullable: false), - Succeeded = table.Column(type: "INTEGER", nullable: false), - Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true), - OutputModelPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_QuantizationRuns", x => x.Id); - table.ForeignKey( - name: "FK_QuantizationRuns_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.SetNull); - table.ForeignKey( - name: "FK_QuantizationRuns_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", - column: x => x.ImatrixDefinitionId, - principalTable: "ImatrixDefinitions", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_QuantizationRuns_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "BenchmarkRuns", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - AiModelHashId = table.Column(type: "INTEGER", nullable: false), - ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), - TensorComboId = table.Column(type: "TEXT", nullable: false), - AiBenchmarkId = table.Column(type: "TEXT", nullable: false), - CategoryBenchmarkId = table.Column(type: "TEXT", nullable: true), - Category = table.Column(type: "INTEGER", nullable: false), - StartedUtc = table.Column(type: "TEXT", nullable: false), - CompletedUtc = table.Column(type: "TEXT", nullable: false), - DurationMs = table.Column(type: "INTEGER", nullable: false), - Succeeded = table.Column(type: "INTEGER", nullable: false), - Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_BenchmarkRuns", x => x.Id); - table.ForeignKey( - name: "FK_BenchmarkRuns_AiBenchmarks_AiBenchmarkId", - column: x => x.AiBenchmarkId, - principalTable: "AiBenchmarks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_BenchmarkRuns_AiModelHashes_AiModelHashId", - column: x => x.AiModelHashId, - principalTable: "AiModelHashes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_BenchmarkRuns_CategoryBenchmark_CategoryBenchmarkId", - column: x => x.CategoryBenchmarkId, - principalTable: "CategoryBenchmark", - principalColumn: "Id", - onDelete: ReferentialAction.SetNull); - table.ForeignKey( - name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", - column: x => x.ImatrixDefinitionId, - principalTable: "ImatrixDefinitions", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_BenchmarkRuns_TensorCombos_TensorComboId", - column: x => x.TensorComboId, - principalTable: "TensorCombos", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", - table: "AiBenchmarks", - columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_ImatrixDefinitionId", - table: "AiBenchmarks", - column: "ImatrixDefinitionId"); - - migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_TensorComboId", - table: "AiBenchmarks", - column: "TensorComboId"); - - migrationBuilder.CreateIndex( - name: "IX_AiModelHashes_UniqueHash", - table: "AiModelHashes", - column: "UniqueHash"); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_BaselineName", - table: "BaselineQuantDefinitions", - column: "BaselineName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeId", - table: "BaselineQuantDefinitions", - column: "DefaultTensorSchemeId", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_DefaultTensorSchemeName", - table: "BaselineQuantDefinitions", - column: "DefaultTensorSchemeName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiBenchmarkId", - table: "BenchmarkRuns", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiBenchmarkId_Category", - table: "BenchmarkRuns", - columns: new[] { "AiBenchmarkId", "Category" }); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_AiModelHashId", - table: "BenchmarkRuns", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_CategoryBenchmarkId", - table: "BenchmarkRuns", - column: "CategoryBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_ImatrixDefinitionId", - table: "BenchmarkRuns", - column: "ImatrixDefinitionId"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_StartedUtc", - table: "BenchmarkRuns", - column: "StartedUtc"); - - migrationBuilder.CreateIndex( - name: "IX_BenchmarkRuns_TensorComboId", - table: "BenchmarkRuns", - column: "TensorComboId"); - - migrationBuilder.CreateIndex( - name: "IX_CategoryBenchmark_AiBenchmarkId", - table: "CategoryBenchmark", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_AiModelHashId", - table: "ExecutionPlanProbeCaches", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", - table: "ExecutionPlanProbeCaches", - columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", - table: "ExecutionPlanProbeCaches", - column: "ImatrixDefinitionId"); - - migrationBuilder.CreateIndex( - name: "IX_ImatrixDefinitions_AiModelHashId_IdentityHash", - table: "ImatrixDefinitions", - columns: new[] { "AiModelHashId", "IdentityHash" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiBenchmarkId", - table: "LearnedBaselineTensorQuants", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorGroupId", - table: "LearnedBaselineTensorQuants", - columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId" }); - - migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorName", - table: "LearnedBaselineTensorQuants", - columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName" }, - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_AiBenchmarkId", - table: "QuantizationRuns", - column: "AiBenchmarkId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_AiModelHashId", - table: "QuantizationRuns", - column: "AiModelHashId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_ImatrixDefinitionId", - table: "QuantizationRuns", - column: "ImatrixDefinitionId"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_StartedUtc", - table: "QuantizationRuns", - column: "StartedUtc"); - - migrationBuilder.CreateIndex( - name: "IX_QuantizationRuns_TensorComboId", - table: "QuantizationRuns", - column: "TensorComboId"); - - migrationBuilder.CreateIndex( - name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", - table: "TensorCombos", - columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "BaselineQuantDefinitions"); - - migrationBuilder.DropTable( - name: "BenchmarkRuns"); - - migrationBuilder.DropTable( - name: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropTable( - name: "LearnedBaselineTensorQuants"); - - migrationBuilder.DropTable( - name: "QuantizationRuns"); - - migrationBuilder.DropTable( - name: "CategoryBenchmark"); - - migrationBuilder.DropTable( - name: "AiBenchmarks"); - - migrationBuilder.DropTable( - name: "ImatrixDefinitions"); - - migrationBuilder.DropTable( - name: "TensorCombos"); - - migrationBuilder.DropTable( - name: "AiModelHashes"); - } - } -} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs deleted file mode 100644 index 505af7a..0000000 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ /dev/null @@ -1,599 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - partial class MagicQuantContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("BaselineName") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeId") - .IsUnique(); - - b.HasIndex("DefaultTensorSchemeName") - .IsUnique(); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DiscoveryTokenTarget") - .HasColumnType("INTEGER"); - - b.Property("GroupSize") - .HasColumnType("INTEGER"); - - b.Property("HardwareFingerprint") - .IsRequired() - .HasMaxLength(1024) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("QuantizationKey") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("QuantizedModelFingerprint") - .IsRequired() - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("SlotsJson") - .IsRequired() - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("StaticNgl") - .HasColumnType("INTEGER"); - - b.Property("UpdatedUtc") - .HasColumnType("TEXT"); - - b.Property("UsesGpu") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") - .IsUnique(); - - b.ToTable("ExecutionPlanProbeCaches"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BuildFingerprint") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("CanonicalPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IdentityHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("MetadataJson") - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("TokenCount") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId", "IdentityHash") - .IsUnique(); - - b.ToTable("ImatrixDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index ca4456a..9bcad79 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -7,6 +7,7 @@ public record BaselineQuants( byte UniqueId, bool RequiresImatrix, ImmutableArray Names, + string QuantizeBaseArgumentName, TensorWeightScheme PrimaryTensorWeightScheme, ImmutableArray LearnedMatchTensorWeightSchemes, ImmutableArray BannedGroupIds, @@ -14,21 +15,35 @@ public record BaselineQuants( bool IsCombinationCarrierCandidate, bool IsExplicitGroupCombinationCandidate, bool IsHighPrecisionExactAlias, + bool IsCustomBaseline = false, + string CanonicalKey = "", + string SourceKind = "standard", + string? SourceOwner = null, + string? SourceRepository = null, + string? SourceFileName = null, + string? ShortSourceName = null, int ExplicitCandidateSortOrder = int.MaxValue) { public const byte NativeSourceUniqueId = 250; + private const byte FirstDynamicCustomBaselineId = 100; - public TensorWeightScheme? DefaultTensorScheme => PrimaryTensorWeightScheme; + private static readonly object DynamicLock = new(); + private static readonly List DynamicCustomBaselines = new(); + private static HashSet? EnabledStandardLearningBaselineIds; + private static HashSet? EnabledStandardCombinationCarrierIds; + private static HashSet? EnabledStandardExplicitCandidateIds; - // Compatibility aliases retained for older call sites. + public TensorWeightScheme? DefaultTensorScheme => PrimaryTensorWeightScheme; public ImmutableArray TensorWeightSchemes => LearnedMatchTensorWeightSchemes; public bool IsPureBaselineCandidate => IsLearningBaseline; public bool IsHighPrecisionExplicitCandidate => IsHighPrecisionExactAlias; + public bool IsExternalRepositoryBaseline => IsCustomBaseline && !string.IsNullOrWhiteSpace(SourceRepository) && !string.IsNullOrWhiteSpace(SourceFileName); private static BaselineQuants Create( byte uniqueId, bool requiresImatrix, string name, + string quantizeBaseArgumentName, TensorWeightScheme primaryTensorWeightScheme, ImmutableArray learnedMatchTensorWeightSchemes, ImmutableArray bannedGroupIds, @@ -36,12 +51,20 @@ private static BaselineQuants Create( bool isCombinationCarrierCandidate, bool isExplicitGroupCombinationCandidate, bool isHighPrecisionExactAlias, - int explicitCandidateSortOrder = int.MaxValue) + int explicitCandidateSortOrder = int.MaxValue, + bool isCustomBaseline = false, + string? canonicalKey = null, + string sourceKind = "standard", + string? sourceOwner = null, + string? sourceRepository = null, + string? sourceFileName = null, + string? shortSourceName = null) { return new BaselineQuants( uniqueId, requiresImatrix, [name], + quantizeBaseArgumentName, primaryTensorWeightScheme, learnedMatchTensorWeightSchemes, bannedGroupIds, @@ -49,11 +72,18 @@ private static BaselineQuants Create( isCombinationCarrierCandidate, isExplicitGroupCombinationCandidate, isHighPrecisionExactAlias, + isCustomBaseline, + canonicalKey ?? $"standard:{name.ToLowerInvariant()}", + sourceKind, + sourceOwner, + sourceRepository, + sourceFileName, + shortSourceName, explicitCandidateSortOrder); } public static readonly BaselineQuants Q8_0 = - Create(0, false, "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], + Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], isLearningBaseline: true, isCombinationCarrierCandidate: true, isExplicitGroupCombinationCandidate: true, @@ -61,7 +91,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 11); public static readonly BaselineQuants Q6_K = - Create(1, false, "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], + Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], isLearningBaseline: true, isCombinationCarrierCandidate: true, isExplicitGroupCombinationCandidate: true, @@ -69,7 +99,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 10); public static readonly BaselineQuants Q5_K = - Create(2, false, "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], + Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: true, isExplicitGroupCombinationCandidate: true, @@ -77,7 +107,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 9); public static readonly BaselineQuants Q4_K_M = - Create(3, false, "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], + Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: true, isExplicitGroupCombinationCandidate: true, @@ -85,7 +115,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 8); public static readonly BaselineQuants IQ4_NL = - Create(5, false, "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], + Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: true, isExplicitGroupCombinationCandidate: true, @@ -93,7 +123,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 7); public static readonly BaselineQuants IQ4_XS = - Create(6, false, "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], + Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: true, isExplicitGroupCombinationCandidate: true, @@ -101,7 +131,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 6); public static readonly BaselineQuants IQ3_S = - Create(7, true, "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], + Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, isExplicitGroupCombinationCandidate: true, @@ -109,7 +139,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 5); public static readonly BaselineQuants IQ3_XS = - Create(8, true, "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], + Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, isExplicitGroupCombinationCandidate: true, @@ -117,7 +147,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 4); public static readonly BaselineQuants IQ3_XXS = - Create(9, true, "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], + Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, isExplicitGroupCombinationCandidate: true, @@ -125,7 +155,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 3); public static readonly BaselineQuants IQ2_S = - Create(10, true, "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], + Create(10, true, "IQ2_S", "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, isExplicitGroupCombinationCandidate: true, @@ -133,7 +163,7 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 2); public static readonly BaselineQuants IQ2_XS = - Create(11, true, "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], + Create(11, true, "IQ2_XS", "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, isExplicitGroupCombinationCandidate: true, @@ -141,29 +171,32 @@ private static BaselineQuants Create( explicitCandidateSortOrder: 1); public static readonly BaselineQuants IQ2_XXS = - Create(12, true, "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], + Create(12, true, "IQ2_XXS", "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], isLearningBaseline: true, isCombinationCarrierCandidate: false, isExplicitGroupCombinationCandidate: true, isHighPrecisionExactAlias: false, explicitCandidateSortOrder: 0); - // These are exact/native override aliases. They are NOT learned baseline identities. public static readonly BaselineQuants BF16_Hybrid = - Create(201, false, "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], + Create(201, false, "BF16", "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], isLearningBaseline: false, isCombinationCarrierCandidate: false, isExplicitGroupCombinationCandidate: false, - isHighPrecisionExactAlias: true); + isHighPrecisionExactAlias: true, + canonicalKey: "alias:bf16", + sourceKind: "exact_alias"); public static readonly BaselineQuants F16_Hybrid = - Create(202, false, "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], + Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], isLearningBaseline: false, isCombinationCarrierCandidate: false, isExplicitGroupCombinationCandidate: false, - isHighPrecisionExactAlias: true); + isHighPrecisionExactAlias: true, + canonicalKey: "alias:f16", + sourceKind: "exact_alias"); - public static readonly ImmutableArray All = + private static readonly ImmutableArray StandardBaselines = [ Q8_0, Q6_K, @@ -176,11 +209,199 @@ private static BaselineQuants Create( IQ3_XXS, IQ2_S, IQ2_XS, - IQ2_XXS, + IQ2_XXS + ]; + + private static readonly ImmutableArray ExactAliases = + [ BF16_Hybrid, F16_Hybrid ]; + public static IReadOnlyList All => GetAllRecognizedBaselines(); + + public static BaselineQuants CreateDynamicCustomBaseline( + byte uniqueId, + string displayName, + string quantizeBaseArgumentName, + string sourceRepository, + string sourceFileName, + string shortSourceName, + string sourceOwner, + string sourceKind, + string canonicalKey, + TensorWeightScheme primaryTensorWeightScheme, + ImmutableArray learnedMatchTensorWeightSchemes, + IReadOnlyCollection bannedGroupIds, + bool requiresImatrix, + bool isLearningBaseline, + bool isCombinationCarrierCandidate, + bool isExplicitGroupCombinationCandidate, + int explicitCandidateSortOrder) + { + return new BaselineQuants( + uniqueId, + requiresImatrix, + [displayName, primaryTensorWeightScheme.Names[0]], + quantizeBaseArgumentName, + primaryTensorWeightScheme, + learnedMatchTensorWeightSchemes, + bannedGroupIds?.Distinct().OrderBy(x => x).ToImmutableArray() ?? ImmutableArray.Empty, + isLearningBaseline, + isCombinationCarrierCandidate, + isExplicitGroupCombinationCandidate, + IsHighPrecisionExactAlias: false, + IsCustomBaseline: true, + CanonicalKey: canonicalKey, + SourceKind: sourceKind, + SourceOwner: sourceOwner, + SourceRepository: sourceRepository, + SourceFileName: sourceFileName, + ShortSourceName: shortSourceName, + ExplicitCandidateSortOrder: explicitCandidateSortOrder); + } + + public static void ResetDynamicCustomBaselines() + { + lock (DynamicLock) + { + DynamicCustomBaselines.Clear(); + } + } + + public static byte GetFirstAvailableDynamicBaselineId() + { + var used = GetAllRecognizedBaselines().Select(x => x.UniqueId).ToHashSet(); + for (byte id = FirstDynamicCustomBaselineId; id < 200; id++) + { + if (!used.Contains(id)) + return id; + } + + throw new InvalidOperationException("No free dynamic baseline ids remain in the configured range."); + } + + public static void RegisterDynamicCustomBaseline(BaselineQuants baseline) + { + if (!baseline.IsCustomBaseline) + throw new InvalidOperationException("Only custom baselines can be dynamically registered."); + + lock (DynamicLock) + { + if (GetAllRecognizedBaselines().Any(x => x.UniqueId == baseline.UniqueId)) + throw new InvalidOperationException($"Dynamic baseline id collision detected for id '{baseline.UniqueId}'."); + + if (GetAllRecognizedBaselines().Any(x => string.Equals(x.CanonicalKey, baseline.CanonicalKey, StringComparison.Ordinal))) + throw new InvalidOperationException($"Dynamic baseline canonical key collision detected for '{baseline.CanonicalKey}'."); + + DynamicCustomBaselines.Add(baseline); + } + } + + public static void ConfigureStandardRoleFilters( + IReadOnlyCollection? enabledLearningBaselineIds, + IReadOnlyCollection? enabledCombinationCarrierIds, + IReadOnlyCollection? enabledExplicitCandidateIds) + { + EnabledStandardLearningBaselineIds = enabledLearningBaselineIds == null ? null : enabledLearningBaselineIds.ToHashSet(); + EnabledStandardCombinationCarrierIds = enabledCombinationCarrierIds == null ? null : enabledCombinationCarrierIds.ToHashSet(); + EnabledStandardExplicitCandidateIds = enabledExplicitCandidateIds == null ? null : enabledExplicitCandidateIds.ToHashSet(); + } + + + + public static void ConfigureStandardPolicy( + bool includeStandardLearningBaselines, + bool includeStandardCombinationCarriers, + bool includeStandardGroupCandidates, + bool alwaysIncludeQ8Anchor, + IReadOnlyCollection? standardLearningBaselineAllowList, + IReadOnlyCollection? standardCarrierAllowList, + IReadOnlyCollection? standardGroupCandidateAllowList) + { + HashSet? learning = includeStandardLearningBaselines + ? ResolveNamesToIdsOrNull(standardLearningBaselineAllowList) + : new HashSet(); + + HashSet? carriers = includeStandardCombinationCarriers + ? ResolveNamesToIdsOrNull(standardCarrierAllowList) + : new HashSet(); + + HashSet? explicitCandidates = includeStandardGroupCandidates + ? ResolveNamesToIdsOrNull(standardGroupCandidateAllowList) + : new HashSet(); + + if (alwaysIncludeQ8Anchor) + { + learning ??= new HashSet(); + carriers ??= new HashSet(); + explicitCandidates ??= new HashSet(); + learning.Add(Q8_0.UniqueId); + carriers.Add(Q8_0.UniqueId); + explicitCandidates.Add(Q8_0.UniqueId); + } + + ConfigureStandardRoleFilters(learning, carriers, explicitCandidates); + } + + private static HashSet? ResolveNamesToIdsOrNull(IReadOnlyCollection? names) + { + if (names == null || names.Count == 0) + return null; + + var set = new HashSet(); + foreach (var raw in names) + { + var item = ResolveBuiltInStandardBaseline(raw ?? string.Empty); + if (item != null) + set.Add(item.UniqueId); + } + + return set; + } + + public sealed class ExternalBaselineRegistration + { + public string CanonicalKey { get; set; } = string.Empty; + public string DisplayName { get; set; } = string.Empty; + public string QuantizeBaseArgumentName { get; set; } = string.Empty; + public string Repository { get; set; } = string.Empty; + public string RepositoryFileName { get; set; } = string.Empty; + public string OwnerShortName { get; set; } = string.Empty; + public string BaselineFamilyName { get; set; } = string.Empty; + public TensorWeightScheme TensorScheme { get; set; } = default!; + public bool RequiresImatrix { get; set; } + public bool AddAsLearningBaseline { get; set; } + public bool AddAsCombinationCarrier { get; set; } + public bool AddAsGroupCandidate { get; set; } + public IReadOnlyCollection BannedGroupIds { get; set; } = Array.Empty(); + } + + public static BaselineQuants RegisterCustomExternalBaseline(ExternalBaselineRegistration registration) + { + var baseline = CreateDynamicCustomBaseline( + uniqueId: GetFirstAvailableDynamicBaselineId(), + displayName: registration.DisplayName, + quantizeBaseArgumentName: registration.QuantizeBaseArgumentName, + sourceRepository: registration.Repository, + sourceFileName: registration.RepositoryFileName, + shortSourceName: registration.OwnerShortName, + sourceOwner: registration.OwnerShortName, + sourceKind: "huggingface_repo", + canonicalKey: registration.CanonicalKey, + primaryTensorWeightScheme: registration.TensorScheme, + learnedMatchTensorWeightSchemes: [registration.TensorScheme], + bannedGroupIds: registration.BannedGroupIds, + requiresImatrix: registration.RequiresImatrix, + isLearningBaseline: registration.AddAsLearningBaseline, + isCombinationCarrierCandidate: registration.AddAsCombinationCarrier, + isExplicitGroupCombinationCandidate: registration.AddAsGroupCandidate, + explicitCandidateSortOrder: StandardBaselines.FirstOrDefault(x => string.Equals(x.Names[0], registration.BaselineFamilyName, StringComparison.OrdinalIgnoreCase))?.ExplicitCandidateSortOrder ?? int.MaxValue); + + RegisterDynamicCustomBaseline(baseline); + return baseline; + } + public static BaselineQuants GetNativeQuant() { var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); @@ -189,6 +410,7 @@ public static BaselineQuants GetNativeQuant() NativeSourceUniqueId, false, [nativeScheme.Names[0]], + nativeScheme.Names[0], nativeScheme, [nativeScheme], [], @@ -196,35 +418,85 @@ public static BaselineQuants GetNativeQuant() IsCombinationCarrierCandidate: false, IsExplicitGroupCombinationCandidate: false, IsHighPrecisionExactAlias: true, + IsCustomBaseline: false, + CanonicalKey: $"native:{nativeScheme.Names[0].ToLowerInvariant()}", + SourceKind: "native_exact_alias", + SourceOwner: null, + SourceRepository: null, + SourceFileName: null, + ShortSourceName: null, ExplicitCandidateSortOrder: int.MaxValue); } public static BaselineQuants GetBF16Quant() => GetNativeQuant(); + public static IReadOnlyList GetBuiltInStandardBaselines() => StandardBaselines.OrderBy(x => x.UniqueId).ToList(); + + public static BaselineQuants? ResolveBuiltInStandardBaseline(string name) + { + if (string.IsNullOrWhiteSpace(name)) + return null; + + return StandardBaselines.FirstOrDefault(x => + x.Names.Any(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase)) || + string.Equals(x.PrimaryTensorWeightScheme.Names[0], name, StringComparison.OrdinalIgnoreCase)); + } + public static IReadOnlyList GetAllRecognizedBaselines() => - All.OrderBy(x => x.UniqueId).ToList(); + StandardBaselines + .Concat(DynamicCustomBaselines.OrderBy(x => x.UniqueId)) + .Concat(ExactAliases) + .OrderBy(x => x.UniqueId) + .ToList(); + + private static IEnumerable FilterStandardByRole( + IEnumerable source, + HashSet? enabledIds) + { + return enabledIds == null ? source : source.Where(x => enabledIds.Contains(x.UniqueId)); + } + + public static IReadOnlyList GetLearningBaselines(bool hasUsableImatrix) + { + var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsLearningBaseline), EnabledStandardLearningBaselineIds); + var custom = DynamicCustomBaselines.Where(x => x.IsLearningBaseline); - public static IReadOnlyList GetLearningBaselines(bool hasUsableImatrix) => - All.Where(x => x.IsLearningBaseline) + return standard + .Concat(custom) .Where(x => hasUsableImatrix || !x.RequiresImatrix) .OrderBy(x => x.UniqueId) .ToList(); + } public static IReadOnlyList GetPureBaselineCandidates(bool hasUsableImatrix) => GetLearningBaselines(hasUsableImatrix); - public static IReadOnlyList GetCombinationCarrierBaselines(bool hasUsableImatrix) => - All.Where(x => x.IsCombinationCarrierCandidate) + public static IReadOnlyList GetCombinationCarrierBaselines(bool hasUsableImatrix) + { + var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsCombinationCarrierCandidate), EnabledStandardCombinationCarrierIds); + var custom = DynamicCustomBaselines.Where(x => x.IsCombinationCarrierCandidate); + + var result = standard + .Concat(custom) .Where(x => hasUsableImatrix || !x.RequiresImatrix) .OrderBy(x => x.UniqueId) .ToList(); - public static IReadOnlyList GetGroupCombinationCandidates(bool hasUsableImatrix, bool allowHighPrecisionHybrids) => - All.Where(x => x.IsExplicitGroupCombinationCandidate) + return result.Count == 0 ? new[] { Q8_0 } : result; + } + + public static IReadOnlyList GetGroupCombinationCandidates(bool hasUsableImatrix, bool allowHighPrecisionHybrids) + { + var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsExplicitGroupCombinationCandidate), EnabledStandardExplicitCandidateIds); + var custom = DynamicCustomBaselines.Where(x => x.IsExplicitGroupCombinationCandidate); + + return standard + .Concat(custom) .Where(x => hasUsableImatrix || !x.RequiresImatrix) .OrderBy(x => x.ExplicitCandidateSortOrder) .ThenBy(x => x.UniqueId) .ToList(); + } public static IReadOnlyList GetGroupCombinationCandidatesSmallestFirst(bool hasUsableImatrix, bool allowHighPrecisionHybrids) => GetGroupCombinationCandidates(hasUsableImatrix, allowHighPrecisionHybrids) @@ -237,22 +509,15 @@ public static IReadOnlyList GetExactHighPrecisionAliases(bool al if (!allowHighPrecisionHybrids) return Array.Empty(); - return All.Where(x => x.IsHighPrecisionExactAlias) - .OrderBy(x => x.UniqueId) - .ToList(); + return ExactAliases.OrderBy(x => x.UniqueId).ToList(); } public const byte TensorConfigNullSlotValue = 0; public static BaselineQuants GetDefaultExplicitFallbackBaseline() => Q8_0; - public static bool IsNullTensorConfigGroupSlot(byte storedValue) => storedValue == TensorConfigNullSlotValue; - - public static byte EncodeTensorConfigGroupSlot(BaselineQuants baseline) => - EncodeTensorConfigGroupSlotBaselineId(baseline.UniqueId); - - public static byte EncodeTensorConfigGroupSlot(TensorWeightScheme exactScheme) => - EncodeTensorConfigGroupSlotBaselineId(GetExactOverrideStorageId(exactScheme)); + public static byte EncodeTensorConfigGroupSlot(BaselineQuants baseline) => EncodeTensorConfigGroupSlotBaselineId(baseline.UniqueId); + public static byte EncodeTensorConfigGroupSlot(TensorWeightScheme exactScheme) => EncodeTensorConfigGroupSlotBaselineId(GetExactOverrideStorageId(exactScheme)); public static byte EncodeTensorConfigGroupSlotBaselineId(byte baselineId) { @@ -270,9 +535,7 @@ public static byte DecodeTensorConfigGroupSlotToBaselineId(byte storedValue) return checked((byte)(storedValue - 1)); } - public static BaselineQuants DecodeTensorConfigGroupSlotToBaseline(byte storedValue) => - FromId(DecodeTensorConfigGroupSlotToBaselineId(storedValue)); - + public static BaselineQuants DecodeTensorConfigGroupSlotToBaseline(byte storedValue) => FromId(DecodeTensorConfigGroupSlotToBaselineId(storedValue)); public static bool IsNativeExactAlias(BaselineQuants baseline) => IsNativeExactAlias(baseline.UniqueId); public static bool IsNativeExactAlias(byte baselineId) @@ -319,7 +582,10 @@ public static TensorWeightScheme ResolveExactOverrideScheme(byte baselineId) public static void ValidateIntegrityOrThrow() { - var invalidBaselines = All + var all = GetAllRecognizedBaselines(); + + var invalidBaselines = all + .Where(x => !x.IsHighPrecisionExactAlias) .Where(x => x.LearnedMatchTensorWeightSchemes.IsDefaultOrEmpty) .Select(x => x.Names.IsDefaultOrEmpty ? $"id:{x.UniqueId}" : x.Names[0]) .ToList(); @@ -327,27 +593,27 @@ public static void ValidateIntegrityOrThrow() if (invalidBaselines.Count > 0) { throw new InvalidOperationException( - "Every BaselineQuants entry must define at least one TensorWeightScheme. Missing for: " + + "Every non-alias BaselineQuants entry must define at least one TensorWeightScheme. Missing for: " + string.Join(", ", invalidBaselines)); } - var duplicateSchemeIds = All - .Where(x => !x.IsHighPrecisionExactAlias) - .SelectMany(x => x.LearnedMatchTensorWeightSchemes.Select(s => new { Baseline = x, Scheme = s })) - .GroupBy(x => x.Scheme.UniqueId) + var duplicateIds = all + .GroupBy(x => x.UniqueId) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); - if (duplicateSchemeIds.Count > 0) - { - var duplicateNames = duplicateSchemeIds - .Select(id => TensorWeightScheme.All.First(s => s.UniqueId == id).Names[0]); + if (duplicateIds.Count > 0) + throw new InvalidOperationException($"Duplicate baseline ids detected: {string.Join(", ", duplicateIds)}"); - throw new InvalidOperationException( - "TensorWeightScheme associations must be unique across learned BaselineQuants entries. Duplicates: " + - string.Join(", ", duplicateNames)); - } + var duplicateKeys = all + .GroupBy(x => x.CanonicalKey, StringComparer.Ordinal) + .Where(g => g.Count() > 1) + .Select(g => g.Key) + .ToList(); + + if (duplicateKeys.Count > 0) + throw new InvalidOperationException($"Duplicate baseline canonical keys detected: {string.Join(", ", duplicateKeys)}"); } public static BaselineQuants FromId(byte id) @@ -355,7 +621,7 @@ public static BaselineQuants FromId(byte id) if (id == NativeSourceUniqueId) return GetNativeQuant(); - var found = All.FirstOrDefault(x => x.UniqueId == id); + var found = GetAllRecognizedBaselines().FirstOrDefault(x => x.UniqueId == id); if (found == null) throw new InvalidOperationException($"Unknown baseline quant id '{id}'."); @@ -374,9 +640,12 @@ public static BaselineQuants FromTensorSchemeId(byte schemeId) schemeId == TensorWeightScheme.F32.UniqueId) return GetNativeQuant(); - var found = All.FirstOrDefault(x => - x.PrimaryTensorWeightScheme.UniqueId == schemeId || - x.LearnedMatchTensorWeightSchemes.Any(s => s.UniqueId == schemeId)); + var found = GetAllRecognizedBaselines() + .Where(x => !x.IsHighPrecisionExactAlias) + .FirstOrDefault(x => + x.PrimaryTensorWeightScheme.UniqueId == schemeId || + x.LearnedMatchTensorWeightSchemes.Any(s => s.UniqueId == schemeId)); + if (found == null) throw new InvalidOperationException($"Unknown tensor scheme id '{schemeId}' for baseline conversion."); diff --git a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs index d35bd30..5d61881 100644 --- a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs +++ b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs @@ -7,15 +7,36 @@ namespace MQ.DB.Models.DbModels; public class BaselineQuantDefinition : ISQLiteEntity { public byte BaselineQuantId { get; set; } + public string CanonicalKey { get; set; } = string.Empty; public string BaselineName { get; set; } = string.Empty; + public string QuantizeBaseArgumentName { get; set; } = string.Empty; public byte DefaultTensorSchemeId { get; set; } public string DefaultTensorSchemeName { get; set; } = string.Empty; + public string SourceKind { get; set; } = string.Empty; + public string? SourceOwner { get; set; } + public string? SourceRepository { get; set; } + public string? SourceFileName { get; set; } + public string? ShortSourceName { get; set; } + public bool IsCustomBaseline { get; set; } + public bool IsLearningBaseline { get; set; } + public bool IsCombinationCarrierCandidate { get; set; } + public bool IsExplicitGroupCombinationCandidate { get; set; } + public bool RequiresImatrix { get; set; } + public int ExplicitCandidateSortOrder { get; set; } public void Configure(EntityTypeBuilder builder) { builder.HasKey(x => x.BaselineQuantId); + builder.Property(x => x.CanonicalKey) + .HasMaxLength(256) + .IsRequired(); + builder.Property(x => x.BaselineName) + .HasMaxLength(128) + .IsRequired(); + + builder.Property(x => x.QuantizeBaseArgumentName) .HasMaxLength(64) .IsRequired(); @@ -23,8 +44,23 @@ public void Configure(EntityTypeBuilder builder) .HasMaxLength(64) .IsRequired(); - builder.HasIndex(x => x.BaselineName).IsUnique(); - builder.HasIndex(x => x.DefaultTensorSchemeId).IsUnique(); - builder.HasIndex(x => x.DefaultTensorSchemeName).IsUnique(); + builder.Property(x => x.SourceKind) + .HasMaxLength(64) + .IsRequired(); + + builder.Property(x => x.SourceOwner) + .HasMaxLength(128); + + builder.Property(x => x.SourceRepository) + .HasMaxLength(256); + + builder.Property(x => x.SourceFileName) + .HasMaxLength(512); + + builder.Property(x => x.ShortSourceName) + .HasMaxLength(64); + + builder.HasIndex(x => x.CanonicalKey).IsUnique(); + builder.HasIndex(x => new { x.SourceRepository, x.SourceFileName }); } } diff --git a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs index 69d6b8c..8203151 100644 --- a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs +++ b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs @@ -15,6 +15,11 @@ public class LearnedBaselineTensorQuant : ISQLiteEntity builder) { builder.HasKey(x => x.Id); + builder.Property(x => x.BaselineCanonicalKey) + .HasMaxLength(256) + .IsRequired(); + + builder.Property(x => x.BaselineSourceKind) + .HasMaxLength(64) + .IsRequired(); + + builder.Property(x => x.BaselineSourceRepository) + .HasMaxLength(256); + + builder.Property(x => x.BaselineSourceFileName) + .HasMaxLength(512); + builder.Property(x => x.TensorName) .HasMaxLength(512) .IsRequired(); @@ -36,7 +55,7 @@ public void Configure(EntityTypeBuilder builder) builder.HasIndex(x => new { x.AiModelHashId, - x.BaselineQuantId, + x.BaselineCanonicalKey, x.TensorWeightSchemeId, x.TensorName }) diff --git a/MQ.DB/Models/RequiredSamplePlan.cs b/MQ.DB/Models/RequiredSamplePlan.cs index e447c24..470ef0c 100644 --- a/MQ.DB/Models/RequiredSamplePlan.cs +++ b/MQ.DB/Models/RequiredSamplePlan.cs @@ -15,6 +15,7 @@ public sealed class RequiredSamplePlan public string Description { get; set; } = string.Empty; public HybridQuant Quant { get; set; } = default!; public byte? TargetGroupId { get; set; } + // Legacy name kept for compatibility: this now stores the tested baseline-family candidate id. public byte? TestedSchemeId { get; set; } @@ -23,7 +24,10 @@ public byte? TestedCandidateId get => TestedSchemeId; set => TestedSchemeId = value; } + + public string? TestedCandidateCanonicalKey { get; set; } public byte? TestedBaselineId { get; set; } + public string? TestedBaselineCanonicalKey { get; set; } public bool IsSmallestProbe { get; set; } } @@ -48,4 +52,4 @@ public RequiredSampleGenerationResult MergeWith(RequiredSampleGenerationResult o merged.Plans.AddRange(other.Plans); return merged; } -} \ No newline at end of file +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 2f62f4d..137837d 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -1,3 +1,4 @@ +using MagicQuant.Configuration; using MagicQuant.Helpers; using MagicQuant.Models; using MagicQuant.Services; @@ -12,7 +13,6 @@ namespace MagicQuant.Commands; public class Evolution : ICommand { - private const int BruteForceFinalCombinationThreshold = 2_000; public async Task Run(List args) { @@ -23,17 +23,20 @@ public async Task Run(List args) } string? modelDirRaw = args.FirstOrDefault(a => - string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; + string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; - if (string.IsNullOrWhiteSpace(modelDirRaw)) - { - const string msg = "[red]Error:[/] Missing required argument [yellow]--model-dir[/]."; - AnsiConsole.MarkupLine(msg); - ShowEvolutionHelp(); - throw new InvalidOperationException("Missing required argument --model-dir."); - } +if (string.IsNullOrWhiteSpace(modelDirRaw)) + modelDirRaw = Config.Current.Paths.ModelDir; - string fullModelPath = Path.GetFullPath(modelDirRaw); +if (string.IsNullOrWhiteSpace(modelDirRaw)) +{ + const string msg = "[red]Error:[/] Missing required model directory. Provide [yellow]--model-dir[/] or set [yellow]paths.model_dir[/] in YAML."; + AnsiConsole.MarkupLine(msg); + ShowEvolutionHelp(); + throw new InvalidOperationException("Missing required model directory."); +} + +string fullModelPath = Path.GetFullPath(modelDirRaw); if (!Directory.Exists(fullModelPath)) { @@ -56,15 +59,13 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); - Cache.ForceRelearnBaselineTensorMappings = args.Any(a => - string.Equals(a.Name, "relearn-baseline-mappings", StringComparison.OrdinalIgnoreCase)); - Cache.ForceRefreshHardwareProbe = args.Any(a => - string.Equals(a.Name, "recheck-hardware-probe", StringComparison.OrdinalIgnoreCase)); - Cache.UseImatrix = args.Any(a => string.Equals(a.Name, "use-imatrix", StringComparison.OrdinalIgnoreCase)); - Cache.ForceImatrixRebuild = args.Any(a => string.Equals(a.Name, "imatrix-force-rebuild", StringComparison.OrdinalIgnoreCase)); - RuntimeSearchSpace.ResetForNewModel(); - RuntimeSearchSpace.SetImatrixAvailability(false); - RuntimeSearchSpace.AllowHighPrecisionHybrids = args.Any(a => string.Equals(a.Name, "allow-high-precision-hybrids", StringComparison.OrdinalIgnoreCase)); +Cache.ForceRelearnBaselineTensorMappings = Config.Current.Flags.ForceRelearnBaselineTensorMappings; +Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; +Cache.UseImatrix = Config.Current.Flags.UseImatrix; +Cache.ForceImatrixRebuild = Config.Current.Flags.ForceImatrixRebuild; +RuntimeSearchSpace.ResetForNewModel(); +RuntimeSearchSpace.SetImatrixAvailability(false); +RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); @@ -82,12 +83,14 @@ public async Task Run(List args) AnsiConsole.MarkupLine("[grey]Acquiring unique model ID...[/]"); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); - AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); +AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); - await EnsureSqliteReadyAsync(); +var pyManager = new PythonManager(Cache.MagicQuantDirectory!); +var customBaselineService = new HuggingFaceBaselineService(pyManager); +await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); +await EnsureSqliteReadyAsync(); - var pyManager = new PythonManager(Cache.MagicQuantDirectory); - var benchmarkService = new BenchmarkService(pyManager); +var benchmarkService = new BenchmarkService(pyManager); var quantizationService = new QuantizationService(benchmarkService); var imatrixService = new ImatrixService(); @@ -104,11 +107,11 @@ public async Task Run(List args) { UseImatrix = Cache.UseImatrix, ForceRebuild = Cache.ForceImatrixRebuild, - ImatrixUrl = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-url", StringComparison.OrdinalIgnoreCase))?.Value, - DatasetRepo = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-repo", StringComparison.OrdinalIgnoreCase))?.Value, - DatasetSplit = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-split", StringComparison.OrdinalIgnoreCase))?.Value, - DatasetConfig = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-config", StringComparison.OrdinalIgnoreCase))?.Value, - LocalDatasetFile = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-dataset-local-file", StringComparison.OrdinalIgnoreCase))?.Value, +ImatrixUrl = Config.Current.Imatrix.ImatrixUrl, +DatasetRepo = Config.Current.Imatrix.DatasetRepo, +DatasetSplit = Config.Current.Imatrix.DatasetSplit, +DatasetConfig = Config.Current.Imatrix.DatasetConfig, +LocalDatasetFile = Config.Current.Imatrix.DatasetLocalFile, ModelDirectory = Cache.ModelDirectory!, MagicQuantDirectory = Cache.ModelMagicQuantDirectory! }; @@ -189,8 +192,7 @@ await benchmarkService.RunAllBenchmarksAsync( // Compatibility must not be allowed to silently downgrade the live policy flags for the // remainder of the evolution run. Re-assert them here as a final safeguard. RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); - RuntimeSearchSpace.AllowHighPrecisionHybrids = args.Any(a => - string.Equals(a.Name, "allow-high-precision-hybrids", StringComparison.OrdinalIgnoreCase)); + RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; CliHelpers.ValidateCombinationLogicWorks(true); @@ -349,13 +351,15 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Final surviving combinations:[/] {finalRemainingCombinationCount:N0}"); - if (finalRemainingCombinationCount <= BruteForceFinalCombinationThreshold) + int bruteForceFinalCombinationThreshold = Config.BruteForceFinalCombinationThreshold; + + if (finalRemainingCombinationCount <= bruteForceFinalCombinationThreshold) { AnsiConsole.Write(new Rule("[yellow]Final Brute Force Benchmark Phase[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine( $"[green]Final combination count[/] [cyan]{finalRemainingCombinationCount:N0}[/] " + - $"is at or below the brute-force threshold of [yellow]{BruteForceFinalCombinationThreshold:N0}[/]."); + $"is at or below the brute-force threshold of [yellow]{bruteForceFinalCombinationThreshold:N0}[/]."); var finalConfigs = await dbService.GetRemainingTensorConfigsAsync(); var finalQuants = finalConfigs @@ -377,7 +381,7 @@ await benchmarkService.RunAllBenchmarksAsync( throw new InvalidOperationException( $"Prediction engine not created yet. Final surviving combinations were {finalRemainingCombinationCount:N0}, " + - $"which is above the brute-force threshold of {BruteForceFinalCombinationThreshold:N0}."); + $"which is above the brute-force threshold of {bruteForceFinalCombinationThreshold:N0}."); } } @@ -417,9 +421,10 @@ private void ShowEvolutionHelp() AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Usage:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"\" [options]"); + AnsiConsole.WriteLine(" mq evolution --config \"./config.default.yaml\""); AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Arguments:[/]"); - AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Required)"); + AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Optional if set in YAML)"); AnsiConsole.MarkupLine(" [green]--relearn-baseline-mappings[/] Delete and relearn baseline tensor mappings (Optional)"); AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); AnsiConsole.MarkupLine(" [green]--use-imatrix[/] Enable imatrix acquisition/build and allow imatrix-required search candidates (Optional)"); @@ -431,6 +436,8 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--imatrix-dataset-split[/] Dataset split for HF/local dataset source metadata/build (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-dataset-config[/] Optional dataset config name for HF datasets (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-dataset-local-file[/] Full path to local .json/.jsonl dataset source (Optional)"); + AnsiConsole.MarkupLine(" [green]--manual-max-predicted-size-bytes[/] Override late predicted-size pruning ceiling (Optional; 0 = auto Q8 ceiling)"); + AnsiConsole.MarkupLine(" [green]--config[/] Path to YAML runtime config. CLI flags override YAML values."); AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 21b7a31..9f5f63f 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -189,7 +189,7 @@ async Task EnsurePackage(string name, string installCmd, Dictionary SensitivityProbeGroups = new() - { - "embeddings", - "lm_head", - "attn_q", - "attn_kv", - "attn_output", - "ffn_up_gate", - "ffn_down", - }; - - // MoE-specific probe groups (added if MoE detected) - public static readonly List SensitivityProbeGroupsMoe = new() + public static void Load(MagicQuantYamlConfig config) { - "moe_router", - "moe_experts", - }; + Current = config ?? throw new ArgumentNullException(nameof(config)); + } - // Critical "brain" layers that cause non-linear collapse when crushed together - public static readonly List BrainLayers = new() + public static void SetResolvedCustomBaselines(IEnumerable baselines) { - "embeddings", - "lm_head", - "attn_output", - }; + Current.Baselines.ResolvedCustomBaselines = baselines?.ToList() ?? new List(); + } - // Schemes that trigger collapse penalty when applied to brain layers - public static readonly List CollapsePenaltySchemes = new() + public static ResolvedCustomBaselineSpec? GetResolvedCustomBaseline(string canonicalKey) { - "MXFP4", - "IQ2_XXS", - "IQ2_XS", - "IQ2_S", - }; - - - - public static readonly List MoeIndicatorTensors = new() - { - // Older / generic expert patterns you already had - "blk.*.ffn_up_expert_0.weight", - "blk.*.ffn_gate_expert_0.weight", - "blk.*.ffn_down_expert_0.weight", - - // Older modern-MoE / GGUF-ish patterns - "blk.*.ffn_up_exps.weight", - "blk.*.ffn_gate_exps.weight", - "blk.*.ffn_down_exps.weight", - "blk.*.ffn_gate_inp.weight", - - // Generic router variants - "router.weight", - "gate.weight", - "blk.*.router.*", - "blk.*.gate_proj.*", - "blk.*.gate_inp.*", - - // Qwen3.5 native HF MoE - "model.language_model.layers.*.mlp.experts.gate_up_proj", - "model.language_model.layers.*.mlp.experts.down_proj", - "model.language_model.layers.*.mlp.gate.weight", - "model.language_model.layers.*.mlp.shared_expert.gate_proj.weight", - "model.language_model.layers.*.mlp.shared_expert.up_proj.weight", - "model.language_model.layers.*.mlp.shared_expert.down_proj.weight", - - // Gemma 4 MoE - "model.language_model.layers.*.experts.gate_up_proj", - "model.language_model.layers.*.experts.down_proj", - "model.language_model.layers.*.router.proj.weight", - "model.language_model.layers.*.router.per_expert_scale", - "model.language_model.layers.*.router.scale", - }; - - -} \ No newline at end of file + return Current.Baselines.ResolvedCustomBaselines.FirstOrDefault(x => + string.Equals(x.CanonicalKey, canonicalKey, StringComparison.Ordinal)); + } + + public static int MaxDataCollectedPerCategory => Current.Evolution.MaxDataCollectedPerCategory; + public static int MaxSurvivalRounds => Current.Evolution.MaxSurvivalRounds; + public static double CollapseMultiplier => Current.Evolution.CollapseMultiplier; + public static int BruteForceFinalCombinationThreshold => Current.Evolution.BruteForceFinalCombinationThreshold; + public static ulong ManualMaxPredictedSizeBytes => Current.Prediction.ManualMaxPredictedSizeBytes; + + public static List SensitivityProbeGroups => Current.SensitivityProbeGroups; + public static List SensitivityProbeGroupsMoe => Current.SensitivityProbeGroupsMoe; + public static List BrainLayers => Current.BrainLayers; + public static List CollapsePenaltySchemes => Current.CollapsePenaltySchemes; + public static List MoeIndicatorTensors => Current.MoeIndicatorTensors; +} diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index 58e0cb9..3044db7 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -120,6 +120,7 @@ public static void ShowHelp(Dictionary [blue][[--option value]][/]"); + AnsiConsole.MarkupLine("Config: [green]--config[/] [grey][/] (CLI flags override YAML)"); AnsiConsole.WriteLine(); } } \ No newline at end of file diff --git a/MagicQuant/Helpers/IsolationPruningConfig.cs b/MagicQuant/Helpers/IsolationPruningConfig.cs index 82232b9..41eb01c 100644 --- a/MagicQuant/Helpers/IsolationPruningConfig.cs +++ b/MagicQuant/Helpers/IsolationPruningConfig.cs @@ -2,13 +2,13 @@ namespace MagicQuant.Helpers; public static class IsolationPruningConfig { - public const double MinimumIsolationReductionToContinueRatio = 0.04d; - public const double MinimumIsolationReductionToSuppressBf16Ratio = 0.10d; - public const double MaximumIsolationPplDeltaPercent = 5.0d; - public const double MaximumIsolationKld = 0.1d; - public const double BadTradeMaxSizeDeltaPercent = 4.0d; - public const double BadTradeKldMultiplier = 2.5d; - public const double BadTradePplMultiplier = 3.5d; - public const double FloatingPointEpsilon = 1e-8d; - public const double MinimumMeaningfulBaseOnlyReductionRatio = 0.01d; -} \ No newline at end of file + public static double MinimumIsolationReductionToContinueRatio => Config.Current.IsolationPruning.MinimumIsolationReductionToContinueRatio; + public static double MinimumIsolationReductionToSuppressBf16Ratio => Config.Current.IsolationPruning.MinimumIsolationReductionToSuppressBf16Ratio; + public static double MaximumIsolationPplDeltaPercent => Config.Current.IsolationPruning.MaximumIsolationPplDeltaPercent; + public static double MaximumIsolationKld => Config.Current.IsolationPruning.MaximumIsolationKld; + public static double BadTradeMaxSizeDeltaPercent => Config.Current.IsolationPruning.BadTradeMaxSizeDeltaPercent; + public static double BadTradeKldMultiplier => Config.Current.IsolationPruning.BadTradeKldMultiplier; + public static double BadTradePplMultiplier => Config.Current.IsolationPruning.BadTradePplMultiplier; + public static double FloatingPointEpsilon => Config.Current.IsolationPruning.FloatingPointEpsilon; + public static double MinimumMeaningfulBaseOnlyReductionRatio => Config.Current.IsolationPruning.MinimumMeaningfulBaseOnlyReductionRatio; +} diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index fd92031..32fc67b 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -197,10 +197,15 @@ public static (bool ExplicitAllowed, bool Bf16Allowed) GetFinalAllowedQuantFamil public static IReadOnlyList GetActiveCombinationBaselines() { - return BaselineQuants.GetCombinationCarrierBaselines(_imatrixAvailable) + var active = BaselineQuants.GetCombinationCarrierBaselines(_imatrixAvailable) .Where(x => !DisabledCombinationBaselineIds.Contains(x.UniqueId)) .OrderBy(x => x.UniqueId) .ToList(); + + if (active.Count == 0) + return new[] { BaselineQuants.Q8_0 }; + + return active; } public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allowDisablingLast = false) @@ -243,4 +248,4 @@ public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeight [Obsolete("Use IsGroupExplicitCandidateBanned.")] public static bool IsGroupExplicitQuantBanned(TensorGroup group) => IsGroupExplicitCandidateBanned(group); -} +} \ No newline at end of file diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 9437acb..7fb94d8 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -21,20 +21,32 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( var result = new RequiredSampleGenerationResult(); var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - foreach (var baseline in BaselineQuants.GetLearningBaselines(RuntimeSearchSpace.HasUsableImatrix())) + var alreadyAddedPureBaselineIds = new HashSet(); + + void AddPureBaselinePlan(BaselineQuants baseline) { + if (!alreadyAddedPureBaselineIds.Add(baseline.UniqueId)) + return; + result.Plans.Add(new RequiredSamplePlan { Kind = RequiredSampleKind.PureBaseline, Key = $"pure:{baseline.UniqueId}", Description = $"Pure baseline build for {string.Join("/", baseline.Names)}", Quant = HybridQuant.CreatePureBaseline(baseline), - TestedBaselineId = baseline.UniqueId + TestedBaselineId = baseline.UniqueId, + TestedBaselineCanonicalKey = baseline.CanonicalKey }); result.PureBaselineCount++; } + foreach (var baseline in BaselineQuants.GetLearningBaselines(RuntimeSearchSpace.HasUsableImatrix())) + AddPureBaselinePlan(baseline); + + // Q8 remains a required system anchor even when the user disables standard baselines. + AddPureBaselinePlan(BaselineQuants.Q8_0); + foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) { result.Plans.Add(new RequiredSamplePlan @@ -46,7 +58,8 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( baseQuant: baseline, groups: activeGroups, exactScheme: nativeExactScheme), - TestedBaselineId = baseline.UniqueId + TestedBaselineId = baseline.UniqueId, + TestedBaselineCanonicalKey = baseline.CanonicalKey }); result.BaseOnlyIsolationCount++; @@ -63,7 +76,8 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( baseQuant: carrier, groups: activeGroups, exactScheme: nativeExactScheme), - TestedBaselineId = carrier.UniqueId + TestedBaselineId = carrier.UniqueId, + TestedBaselineCanonicalKey = carrier.CanonicalKey }); result.BaseOnlyIsolationCount++; @@ -89,7 +103,9 @@ public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( Quant = quant, TargetGroupId = group.UniqueId, TestedCandidateId = smallest.UniqueId, + TestedCandidateCanonicalKey = smallest.CanonicalKey, TestedBaselineId = carrier.UniqueId, + TestedBaselineCanonicalKey = carrier.CanonicalKey, IsSmallestProbe = true }); @@ -159,7 +175,9 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample Quant = quant, TargetGroupId = group.UniqueId, TestedCandidateId = candidate.UniqueId, - TestedBaselineId = carrier.UniqueId + TestedCandidateCanonicalKey = candidate.CanonicalKey, + TestedBaselineId = carrier.UniqueId, + TestedBaselineCanonicalKey = carrier.CanonicalKey }); result.GroupIsolationCount++; @@ -322,4 +340,4 @@ private static int ComputeWorkerThreads(int threadCount) return Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); } -} +} \ No newline at end of file diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index c45bdc5..ff111c1 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -13,12 +13,19 @@ + PreserveNewest + + PreserveNewest + + + PreserveNewest + diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 0225e14..cd1c210 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -1,32 +1,18 @@ -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; using MagicQuant.Commands; +using MagicQuant.Configuration; using MagicQuant.Helpers; using MagicQuant.Models; using MagicQuant.Services; -using Spectre.Console; -using System.Collections.Immutable; using MQ.DB.Models; +using Spectre.Console; #if DEBUG -// If we are in Debug and no arguments were passed, default to "evolution" if (args.Length == 0) { args = new[] { "evolution" }; } - -// OPTIONAL: Manually append hardcoded flags for testing specific scenarios -// Example: If you want to test "evolution --iterations 10" every time you debug -string manualFlags = - @"--model-dir ""/mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/"" - --use-imatrix - --imatrix-dataset-local-file ""/home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1m.jsonl"" - --imatrix-dataset-split ""text"""; -args = args.Concat(manualFlags.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray(); #endif -// 2. Define the Command Registry var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) { { "evolution", ("Run the full evolutionary quantization search", () => new Evolution()) }, @@ -34,7 +20,6 @@ { "initialize-llama-cpp", ("Initialize or update llama.cpp", () => new InitializeLlamaCpp()) } }; -// 3. Validate input if (args.Length == 0 || args[0].Equals("help", StringComparison.OrdinalIgnoreCase)) { CliHelpers.ShowHelp(commands); @@ -43,7 +28,6 @@ string commandInput = args[0]; -// 4. Check if command exists if (!commands.TryGetValue(commandInput, out var commandInfo)) { AnsiConsole.MarkupLine($"[red]Error:[/] The command [yellow]'{commandInput}'[/] does not exist."); @@ -51,25 +35,35 @@ return; } -// 5. Parse Arguments string remainingArgsString = string.Join(" ", args.Skip(1)); List parsedArgs = CliHelpers.ParseArguments(remainingArgsString); try { - // strict startup integrity checks + var loadedConfig = MagicQuantYamlLoader.LoadAndApply(commandInput, parsedArgs); + TensorWeightScheme.ValidateSmallestConfiguration(); BaselineQuants.ValidateIntegrityOrThrow(); QuantizationService.ValidateQuantNameNormalizationOrThrow(); - // 6. Mandatory Validation for non-init commands if (!commandInput.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) { await AnsiConsole.Status() - .StartAsync("[grey]Checking environment dependencies...[/]", async ctx => + .StartAsync("[grey]Checking environment dependencies...[/]", async _ => { var initializer = new InitializeLlamaCpp(); - var validationArgs = new List { new CliArg { Name = "validate", Value = "" } }; + var validationArgs = new List + { + new() { Name = "validate", Value = string.Empty } + }; + + if (!string.IsNullOrWhiteSpace(loadedConfig.Paths.LlamaRoot)) + validationArgs.Add(new CliArg { Name = "llama-root", Value = loadedConfig.Paths.LlamaRoot }); + if (!string.IsNullOrWhiteSpace(loadedConfig.Paths.LlamaBin)) + validationArgs.Add(new CliArg { Name = "llama-bin", Value = loadedConfig.Paths.LlamaBin }); + if (!string.IsNullOrWhiteSpace(loadedConfig.Paths.ConvertScript)) + validationArgs.Add(new CliArg { Name = "convert-script", Value = loadedConfig.Paths.ConvertScript }); + await initializer.Run(validationArgs); }); @@ -77,10 +71,8 @@ await AnsiConsole.Status() AnsiConsole.WriteLine(); } - // Manditory Run combinations and DuckDB setup CliHelpers.ValidateCombinationLogicWorks(); - // 7. Execute Command var commandInstance = commandInfo.Factory(); await commandInstance.Run(parsedArgs); } diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs new file mode 100644 index 0000000..4eb7955 --- /dev/null +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -0,0 +1,361 @@ +using System.Text.Json; +using MagicQuant.Configuration; +using MagicQuant.Helpers; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class HuggingFaceBaselineService +{ + private readonly PythonManager _python; + + public HuggingFaceBaselineService(PythonManager python) + { + _python = python ?? throw new ArgumentNullException(nameof(python)); + } + + public async Task> PrecheckAndRegisterConfiguredBaselinesAsync(CancellationToken ct = default) + { + await EnsureHubSupportAsync(); + + var resolved = new List(); + BaselineQuants.ResetDynamicCustomBaselines(); + + byte nextId = BaselineQuants.GetFirstAvailableDynamicBaselineId(); + + foreach (var repo in Config.Current.Baselines.CustomRepositories.Where(x => x.Enabled)) + { + if (string.IsNullOrWhiteSpace(repo.RepoId)) + throw new InvalidOperationException("Custom baseline repository entry is missing repo_id."); + + var repoFiles = await ListRepoFilesAsync(repo.RepoId, ct); + if (repoFiles.Count == 0) + throw new InvalidOperationException($"No files were returned from Hugging Face repo '{repo.RepoId}'."); + + string shortSourceName = string.IsNullOrWhiteSpace(repo.ShortSourceName) + ? DeriveShortSourceName(repo.RepoId) + : repo.ShortSourceName!.Trim(); + + foreach (var include in repo.Includes) + { + if (string.IsNullOrWhiteSpace(include.BaselineFamily)) + throw new InvalidOperationException($"Repo '{repo.RepoId}' has an include entry missing baseline_family."); + + var standardFamily = BaselineQuants.ResolveBuiltInStandardBaseline(include.BaselineFamily) + ?? throw new InvalidOperationException( + $"Custom baseline include '{include.BaselineFamily}' in repo '{repo.RepoId}' could not be matched to a built-in baseline family."); + + string resolvedFileName = ResolveRepoFileName(repoFiles, include, standardFamily); + string displayName = string.IsNullOrWhiteSpace(include.DisplayName) + ? $"{shortSourceName}-{standardFamily.Names[0]}" + : include.DisplayName!.Trim(); + + string canonicalKey = BuildCanonicalKey(repo.RepoId, resolvedFileName, standardFamily.Names[0]); + bool requiresImatrix = include.RequiresImatrix ?? standardFamily.RequiresImatrix; + bool allowAsLearning = include.AllowAsLearningBaseline ?? repo.AllowAsLearningBaseline; + bool allowAsCarrier = include.AllowAsCombinationCarrier ?? repo.AllowAsCombinationCarrier; + bool allowAsExplicit = include.AllowAsExplicitGroupCandidate ?? repo.AllowAsExplicitGroupCandidate; + string quantizeBaseName = string.IsNullOrWhiteSpace(include.QuantizeBaseName) + ? standardFamily.Names[0] + : include.QuantizeBaseName!.Trim(); + + var bannedGroups = include.BannedGroupIds.Count > 0 + ? include.BannedGroupIds.ToArray() + : standardFamily.BannedGroupIds.ToArray(); + + var dynamicBaseline = BaselineQuants.CreateDynamicCustomBaseline( + uniqueId: nextId, + displayName: displayName, + quantizeBaseArgumentName: quantizeBaseName, + sourceRepository: repo.RepoId, + sourceFileName: resolvedFileName, + shortSourceName: shortSourceName, + sourceOwner: DeriveSourceOwner(repo.RepoId), + sourceKind: "huggingface_repo", + canonicalKey: canonicalKey, + primaryTensorWeightScheme: standardFamily.PrimaryTensorWeightScheme, + learnedMatchTensorWeightSchemes: standardFamily.LearnedMatchTensorWeightSchemes, + bannedGroupIds: bannedGroups, + requiresImatrix: requiresImatrix, + isLearningBaseline: allowAsLearning, + isCombinationCarrierCandidate: allowAsCarrier, + isExplicitGroupCombinationCandidate: allowAsExplicit, + explicitCandidateSortOrder: standardFamily.ExplicitCandidateSortOrder); + + BaselineQuants.RegisterDynamicCustomBaseline(dynamicBaseline); + + resolved.Add(new ResolvedCustomBaselineSpec + { + DynamicBaselineId = nextId, + CanonicalKey = canonicalKey, + DisplayName = displayName, + RepoId = repo.RepoId, + SourceOwner = DeriveSourceOwner(repo.RepoId), + SourceFileName = resolvedFileName, + ShortSourceName = shortSourceName, + BaselineFamily = standardFamily.Names[0], + QuantizeBaseName = quantizeBaseName, + RequiresImatrix = requiresImatrix, + AllowAsLearningBaseline = allowAsLearning, + AllowAsCombinationCarrier = allowAsCarrier, + AllowAsExplicitGroupCandidate = allowAsExplicit, + BannedGroupIds = bannedGroups + }); + + checked { nextId++; } + } + } + + Config.SetResolvedCustomBaselines(resolved); + BaselineQuants.ValidateIntegrityOrThrow(); + + if (resolved.Count > 0) + { + AnsiConsole.MarkupLine($"[green]Resolved custom baselines:[/] {resolved.Count:N0}"); + foreach (var item in resolved) + { + AnsiConsole.MarkupLine( + $" [grey]- {Markup.Escape(item.DisplayName)}[/] => [cyan]{Markup.Escape(item.RepoId)}[/] / [yellow]{Markup.Escape(item.SourceFileName)}[/]"); + } + } + + return resolved; + } + + public async Task DownloadBaselineAsync(BaselineQuants baseline, string destinationPath, bool forceRedownload = false, CancellationToken ct = default) + { + if (!baseline.IsExternalRepositoryBaseline) + throw new InvalidOperationException($"Baseline '{baseline.Names[0]}' is not an external repository baseline."); + + await EnsureHubSupportAsync(); + + var spec = Config.GetResolvedCustomBaseline(baseline.CanonicalKey) + ?? throw new InvalidOperationException($"No resolved custom baseline spec exists for canonical key '{baseline.CanonicalKey}'."); + + Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); + + if (forceRedownload && File.Exists(destinationPath)) + File.Delete(destinationPath); + + string payloadPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_{Guid.NewGuid():N}.json"); + string scriptPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_{Guid.NewGuid():N}.py"); + string resultPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_result_{Guid.NewGuid():N}.json"); + + try + { + await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new + { + repo_id = spec.RepoId, + file_name = spec.SourceFileName, + target_path = destinationPath, + force_redownload = forceRedownload, + result_path = resultPath + }), ct); + + const string py = """ + import json + import os + import shutil + import sys + from huggingface_hub import hf_hub_download + + payload_path = sys.argv[1] + with open(payload_path, 'r', encoding='utf-8') as f: + payload = json.load(f) + + target_path = payload['target_path'] + result_path = payload['result_path'] + os.makedirs(os.path.dirname(target_path), exist_ok=True) + + try: + downloaded = hf_hub_download( + repo_id=payload['repo_id'], + filename=payload['file_name'], + local_dir=os.path.dirname(target_path), + force_download=payload.get('force_redownload', False), + ) + + if os.path.abspath(downloaded) != os.path.abspath(target_path): + shutil.copy2(downloaded, target_path) + + size = os.path.getsize(target_path) + result = {'success': True, 'path': target_path, 'size': size} + except Exception as ex: + result = {'success': False, 'error': str(ex)} + + with open(result_path, 'w', encoding='utf-8') as f: + json.dump(result, f) + """; + + await File.WriteAllTextAsync(scriptPath, py, ct); + await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); + + using var doc = JsonDocument.Parse(await File.ReadAllTextAsync(resultPath, ct)); + if (!doc.RootElement.TryGetProperty("success", out var successProp) || !successProp.GetBoolean()) + { + string error = doc.RootElement.TryGetProperty("error", out var errProp) ? errProp.GetString() ?? "unknown error" : "unknown error"; + throw new InvalidOperationException($"Failed downloading external baseline '{baseline.Names[0]}': {error}"); + } + + if (!File.Exists(destinationPath) || new FileInfo(destinationPath).Length == 0) + throw new InvalidOperationException($"External baseline download reported success but no valid file exists at '{destinationPath}'."); + + return destinationPath; + } + finally + { + TryDelete(payloadPath); + TryDelete(scriptPath); + TryDelete(resultPath); + } + } + + private async Task EnsureHubSupportAsync() + { + string? version = await _python.GetInstalledVersionAsync("huggingface_hub"); + if (version != null) + return; + + AnsiConsole.MarkupLine("[cyan]Installing huggingface_hub (includes Hub download/CLI support)...[/]"); + await _python.RunPipInstallAsync("--upgrade huggingface_hub"); + } + + private async Task> ListRepoFilesAsync(string repoId, CancellationToken ct) + { + string tempDir = Cache.ExternalBaselineCacheDirectory ?? Cache.MagicQuantDirectory ?? AppContext.BaseDirectory; + Directory.CreateDirectory(tempDir); + + string payloadPath = Path.Combine(tempDir, $"hf_repo_list_{Guid.NewGuid():N}.json"); + string resultPath = Path.Combine(tempDir, $"hf_repo_list_result_{Guid.NewGuid():N}.json"); + string scriptPath = Path.Combine(tempDir, $"hf_repo_list_{Guid.NewGuid():N}.py"); + + try + { + await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new { repo_id = repoId, result_path = resultPath }), ct); + + const string py = """ + import json + import sys + from huggingface_hub import HfApi + + payload_path = sys.argv[1] + with open(payload_path, 'r', encoding='utf-8') as f: + payload = json.load(f) + + result_path = payload['result_path'] + try: + files = HfApi().list_repo_files(repo_id=payload['repo_id']) + result = {'success': True, 'files': files} + except Exception as ex: + result = {'success': False, 'error': str(ex), 'files': []} + + with open(result_path, 'w', encoding='utf-8') as f: + json.dump(result, f) + """; + + await File.WriteAllTextAsync(scriptPath, py, ct); + await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); + + using var doc = JsonDocument.Parse(await File.ReadAllTextAsync(resultPath, ct)); + if (!doc.RootElement.TryGetProperty("success", out var successProp) || !successProp.GetBoolean()) + { + string error = doc.RootElement.TryGetProperty("error", out var errProp) ? errProp.GetString() ?? "unknown error" : "unknown error"; + throw new InvalidOperationException($"Failed listing files for Hugging Face repo '{repoId}': {error}"); + } + + return doc.RootElement.GetProperty("files") + .EnumerateArray() + .Select(x => x.GetString()) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Cast() + .ToList(); + } + finally + { + TryDelete(payloadPath); + TryDelete(scriptPath); + TryDelete(resultPath); + } + } + + private static string ResolveRepoFileName(IReadOnlyList repoFiles, CustomBaselineIncludeConfig include, BaselineQuants standardFamily) + { + if (!string.IsNullOrWhiteSpace(include.FileName)) + { + string explicitName = include.FileName!.Trim(); + var match = repoFiles.FirstOrDefault(x => string.Equals(x, explicitName, StringComparison.OrdinalIgnoreCase)); + if (match == null) + throw new InvalidOperationException($"Configured file '{explicitName}' was not found in the configured custom baseline repository."); + + if (!match.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)) + throw new InvalidOperationException($"Configured file '{explicitName}' is not a GGUF file."); + + return match; + } + + string family = NormalizeSuffixToken(standardFamily.Names[0]); + var matches = repoFiles + .Where(x => x.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)) + .Where(x => NormalizeSuffixToken(Path.GetFileNameWithoutExtension(x)).EndsWith(family, StringComparison.OrdinalIgnoreCase)) + .ToList(); + + if (matches.Count == 0) + { + throw new InvalidOperationException( + $"Could not auto-match a GGUF file for baseline family '{standardFamily.Names[0]}'. " + + "Specify file_name explicitly in the YAML."); + } + + if (matches.Count > 1) + { + throw new InvalidOperationException( + $"Auto-match for baseline family '{standardFamily.Names[0]}' returned multiple files: {string.Join(", ", matches)}. " + + "Specify file_name explicitly in the YAML."); + } + + return matches[0]; + } + + private static string BuildCanonicalKey(string repoId, string fileName, string family) + => $"hf:{repoId.Trim().ToLowerInvariant()}::{fileName.Trim().ToLowerInvariant()}::{family.Trim().ToLowerInvariant()}"; + + private static string DeriveShortSourceName(string repoId) + { + var owner = DeriveSourceOwner(repoId); + if (string.IsNullOrWhiteSpace(owner)) + return "Custom"; + + return char.ToUpperInvariant(owner[0]) + owner[1..]; + } + + private static string DeriveSourceOwner(string repoId) + { + if (string.IsNullOrWhiteSpace(repoId)) + return string.Empty; + + var parts = repoId.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + return parts.Length > 0 ? parts[0] : repoId; + } + + private static string NormalizeSuffixToken(string value) + { + return value.Trim() + .Replace("-", "_") + .Replace(" ", string.Empty) + .ToUpperInvariant(); + } + + private static void TryDelete(string path) + { + try + { + if (File.Exists(path)) + File.Delete(path); + } + catch + { + } + } +} diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 9dbeae2..2738af2 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -245,10 +245,14 @@ public async Task PrunePredictedLargerThanQ8Async( var kept = new List(rows.Count); + ulong sizeCeilingBytes = Config.ManualMaxPredictedSizeBytes > 0 + ? Config.ManualMaxPredictedSizeBytes + : predictionContext.PureQ8BaseSize; + foreach (var row in rows) { ulong predicted = predictionContext.Predict(row); - if (predicted <= predictionContext.PureQ8BaseSize) + if (predicted <= sizeCeilingBytes) kept.Add(row); } @@ -263,7 +267,11 @@ public async Task PrunePredictedLargerThanQ8Async( await RecreateTableAsync(connection, ct); await BulkAppendAsync(connection, kept, "predicted-size-prune", ct); - AnsiConsole.MarkupLine($"[yellow]Predicted-size pruning removed:[/] [red]{removed:N0}[/] combo(s) larger than pure Q8."); + string ceilingLabel = Config.ManualMaxPredictedSizeBytes > 0 + ? $"manual ceiling {Config.ManualMaxPredictedSizeBytes:N0} bytes" + : "pure Q8"; + + AnsiConsole.MarkupLine($"[yellow]Predicted-size pruning removed:[/] [red]{removed:N0}[/] combo(s) larger than {ceilingLabel}."); return removed; } @@ -668,4 +676,4 @@ private void AddDelta(byte groupId, byte candidateId, ref long total) total += delta; } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 3f71328..1f2d434 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -49,6 +49,7 @@ public class QuantizationService private readonly SemaphoreSlim _cpuQuantLock; private readonly int _maxConcurrentQuantizations; private readonly ImatrixService _imatrixService; + private readonly HuggingFaceBaselineService _huggingFaceBaselineService; private static readonly SemaphoreSlim BaseModelLock = new(1, 1); private const byte UnknownTensorGroupId = 255; @@ -74,6 +75,7 @@ public QuantizationService(BenchmarkService benchmarker) _ggufDir = Path.Combine(Cache.ModelMagicQuantDirectory, "GGUF"); _benchDir = Path.Combine(Cache.ModelMagicQuantDirectory, "Benchmarks"); _imatrixService = new ImatrixService(); + _huggingFaceBaselineService = new HuggingFaceBaselineService(_python); Directory.CreateDirectory(_ggufDir); Directory.CreateDirectory(_benchDir); @@ -306,134 +308,183 @@ await Parallel.ForEachAsync( return (comboId, benchmarkId == Guid.Empty ? null : benchmarkId); } - public async Task ProcessHybridQuantAsync( - HybridQuant quant, - CancellationToken ct = default) +public async Task ProcessHybridQuantAsync( + HybridQuant quant, + CancellationToken ct = default) +{ + string modelName = GenerateHybridName(quant); + string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); + string modelBenchDir = Path.Combine(_benchDir, modelName); + string baseLogitsDir = GetBaseLogitsDirectory(); + + DateTime startedUtc = DateTime.UtcNow; + var stopwatch = Stopwatch.StartNew(); + var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); + bool pureExternalBaseline = ShouldDownloadExternalBaselineInsteadOfQuantizing(quant); + string benchmarkModelPath = quantPath; + + if (!forceBaselineRelearn && await _benchmarker.TryReuseExistingBenchmarksAsync( + quantConfig: quant, + modelPath: quantPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + domainsOverride: new[] { "general" })) { - string modelName = GenerateHybridName(quant); - string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); - string modelBenchDir = Path.Combine(_benchDir, modelName); - string baseLogitsDir = GetBaseLogitsDirectory(); - - DateTime startedUtc = DateTime.UtcNow; - var stopwatch = Stopwatch.StartNew(); - var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); - - if (!forceBaselineRelearn && await _benchmarker.TryReuseExistingBenchmarksAsync( - quantConfig: quant, - modelPath: quantPath, - benchDir: modelBenchDir, - klLogitsDir: baseLogitsDir, - domainsOverride: new[] { "general" })) - { - AnsiConsole.MarkupLine($"[grey]Reused existing benchmark artifacts:[/] {Markup.Escape(modelName)}"); + AnsiConsole.MarkupLine($"[grey]Reused existing benchmark artifacts:[/] {Markup.Escape(modelName)}"); - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - return SampleProcessState.Skipped; - } + return SampleProcessState.Skipped; + } - if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) - { - AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); + if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) + { + AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - return SampleProcessState.Skipped; - } + return SampleProcessState.Skipped; + } + + try + { + string inputPath = await GetEffectiveInputModelPathAsync(quant, forceBaselineRelearn, ct); + QuantizationExecutionReport? quantizationReport = null; + await _cpuQuantLock.WaitAsync(ct); try { - string basePath = await EnsureBaseModelFileAsync(); - QuantizationExecutionReport? quantizationReport = null; - - await _cpuQuantLock.WaitAsync(ct); - try + if (pureExternalBaseline) + { + benchmarkModelPath = inputPath; + } + else { + benchmarkModelPath = quantPath; if (!File.Exists(quantPath) || forceBaselineRelearn) { AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); - quantizationReport = await RunLlamaQuantizeAsync(basePath, quantPath, quant); + quantizationReport = await RunLlamaQuantizeAsync(inputPath, quantPath, quant); } } - finally - { - _cpuQuantLock.Release(); - } - - if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) - { - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - - return SampleProcessState.Skipped; - } + } + finally + { + _cpuQuantLock.Release(); + } - AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); + if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) + { + if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - await _benchmarker.RunAllBenchmarksAsync( - quantConfig: quant, - modelPath: quantPath, - benchDir: modelBenchDir, - klLogitsDir: baseLogitsDir, - saveLogits: false, - domainsOverride: new[] { "general" }); + return SampleProcessState.Skipped; + } - stopwatch.Stop(); + AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); + await _benchmarker.RunAllBenchmarksAsync( + quantConfig: quant, + modelPath: benchmarkModelPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: false, + domainsOverride: new[] { "general" }); + + stopwatch.Stop(); + + await PersistQuantizationRunAsync( + quant: quant, + imatrixDefinitionId: null, + startedUtc: startedUtc, + completedUtc: DateTime.UtcNow, + succeeded: true, + outputModelPath: benchmarkModelPath, + error: null, + ct: ct); + + if (IsLearnableBaselineRun(quant)) + await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); + + return SampleProcessState.Completed; + } + catch (Exception ex) + { + stopwatch.Stop(); + try + { await PersistQuantizationRunAsync( quant: quant, imatrixDefinitionId: null, startedUtc: startedUtc, completedUtc: DateTime.UtcNow, - succeeded: true, - outputModelPath: quantPath, - error: null, + succeeded: false, + outputModelPath: benchmarkModelPath, + error: ex.ToString(), ct: ct); - - if (IsLearnableBaselineRun(quant)) - { - await LearnAndPersistBaselineTensorMapAsync(quant, quantPath, quantizationReport, ct); - } - - return SampleProcessState.Completed; } - catch (Exception ex) + catch { - stopwatch.Stop(); + } - try - { - await PersistQuantizationRunAsync( - quant: quant, - imatrixDefinitionId: null, - startedUtc: startedUtc, - completedUtc: DateTime.UtcNow, - succeeded: false, - outputModelPath: quantPath, - error: ex.ToString(), - ct: ct); - } - catch - { - // Never hide the original exception because timing persistence failed. - } + throw; + } + finally + { + if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + } +} - throw; - } - finally - { - if (!IsProtectedModel(modelName)) - { - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - } - } +private bool ShouldDownloadExternalBaselineInsteadOfQuantizing(HybridQuant quant) + => quant.BaseQuant.IsExternalRepositoryBaseline && quant.Tensors.Count == 0; + +private async Task GetEffectiveInputModelPathAsync(HybridQuant quant, bool forceRefresh, CancellationToken ct) +{ + string basePath = await EnsureBaseModelFileAsync(); + if (!quant.BaseQuant.IsExternalRepositoryBaseline) + return basePath; + + string externalPath = GetExternalBaselineCachePath(quant.BaseQuant); + await _huggingFaceBaselineService.DownloadBaselineAsync(quant.BaseQuant, externalPath, forceRefresh, ct); + await ValidateExternalBaselineTensorParityOrThrow(basePath, externalPath); + return externalPath; +} + +private string GetExternalBaselineCachePath(BaselineQuants baseline) +{ + string root = Cache.ExternalBaselineCacheDirectory ?? Path.Combine(Cache.ModelMagicQuantDirectory!, "ExternalBaselines"); + Directory.CreateDirectory(root); + string safe = string.Concat(baseline.CanonicalKey.Select(ch => Path.GetInvalidFileNameChars().Contains(ch) ? '_' : ch)); + string extension = Path.GetExtension(baseline.SourceFileName ?? string.Empty); + if (string.IsNullOrWhiteSpace(extension)) + extension = ".gguf"; + return Path.Combine(root, safe + extension); +} + +private async Task ValidateExternalBaselineTensorParityOrThrow(string baseModelPath, string externalBaselinePath) +{ + var baseMeta = await ReadTensorMetadataFromGgufAsync(baseModelPath, externalBaselinePath + ".nativecheck"); + var externalMeta = await ReadTensorMetadataFromGgufAsync(externalBaselinePath, externalBaselinePath + ".externalcheck"); + + var baseNames = baseMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); + var externalNames = externalMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); + + var missing = baseNames.Except(externalNames, StringComparer.Ordinal).Take(20).ToList(); + var unexpected = externalNames.Except(baseNames, StringComparer.Ordinal).Take(20).ToList(); + + if (missing.Count > 0 || unexpected.Count > 0 || baseNames.Count != externalNames.Count) + { + throw new InvalidOperationException( + $"External/custom baseline tensor mismatch detected. Missing=[{string.Join(", ", missing)}] Unexpected=[{string.Join(", ", unexpected)}]. " + + "MagicQuant will not persist or use a custom baseline whose tensor names do not exactly match the source model."); } +} - // ---------------------------------------------------------------- - // Benchmark/logit helpers +// ---------------------------------------------------------------- +// Benchmark/logit helpers // ---------------------------------------------------------------- private string GetBaseLogitsDirectory() @@ -877,7 +928,7 @@ private static string ResolveQuantizeBaseArgument( "Use a real carrier baseline (Q8_0 recommended) and apply only learned exact tensor overrides for the target configuration."); } - return ResolveBaseName(quant.BaseQuant); + return quant.BaseQuant.QuantizeBaseArgumentName; } private bool ShouldApplyImatrix(HybridQuant quant) @@ -1093,6 +1144,12 @@ private async Task LearnAndPersistBaselineTensorMapAsync( if (!IsLearnableBaselineRun(quant)) return; + if (quant.BaseQuant.IsExternalRepositoryBaseline) + { + string nativeBasePath = await EnsureBaseModelFileAsync(); + await ValidateExternalBaselineTensorParityOrThrow(nativeBasePath, quantizedModelPath); + } + var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; var parsed = ParseQuantizeLogForTensorTypes(report?.LogPath ?? (quantizedModelPath + ".quantize.log")); var ggufMetadata = await ReadTensorMetadataFromGgufAsync(quantizedModelPath, quantizedModelPath); @@ -1152,7 +1209,7 @@ private async Task LearnAndPersistBaselineTensorMapAsync( await db.LearnedBaselineTensorQuants .Where(x => x.AiModelHashId == model.Id && - x.BaselineQuantId == quant.BaseQuant.UniqueId && + x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && x.TensorWeightSchemeId == tensorScheme.UniqueId) .ExecuteDeleteAsync(ct); @@ -1170,6 +1227,10 @@ await db.LearnedBaselineTensorQuants BaselineQuantId = quant.BaseQuant.UniqueId, TensorWeightSchemeId = tensorScheme.UniqueId, TensorGroupId = match.PrimaryGroup?.UniqueId ?? UnknownTensorGroupId, + BaselineCanonicalKey = quant.BaseQuant.CanonicalKey, + BaselineSourceKind = quant.BaseQuant.SourceKind, + BaselineSourceRepository = quant.BaseQuant.SourceRepository, + BaselineSourceFileName = quant.BaseQuant.SourceFileName, TensorName = kv.Key, FinalQuantType = kv.Value.FinalQuantType }; @@ -1448,109 +1509,176 @@ private static HashSet GetExpectedTensorNamesForGroup( .ToHashSet(StringComparer.Ordinal); } - private List BuildRequestedTensorOverrides( - HybridQuant quant, - IReadOnlyCollection sourceTensorNames) - { - var result = new List(); - - if (quant.Tensors == null || quant.Tensors.Count == 0) - return result; - var baseScheme = TryResolveBaseTensorScheme(quant.BaseQuant); +private List BuildRequestedTensorOverrides( + HybridQuant quant, + IReadOnlyCollection sourceTensorNames) +{ + var result = new List(); - foreach (var hybrid in quant.Tensors) - { - if (hybrid?.TGroup == null) - continue; + if (quant.Tensors == null || quant.Tensors.Count == 0) + return result; - hybrid.ValidateOrThrow(); + var baseScheme = TryResolveBaseTensorScheme(quant.BaseQuant); - if (hybrid.MaterializedTensorScheme.UniqueId == TensorWeightScheme.NULL.UniqueId) - continue; + if (quant.BaseQuant.IsExternalRepositoryBaseline) + { + var blanket = TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: false); - var expectedForGroup = GetExpectedTensorNamesForGroup(hybrid.TGroup, sourceTensorNames); - if (expectedForGroup.Count == 0) - continue; + if (blanket.Count == 0) + { + throw new InvalidOperationException( + $"Missing blanket learned mapping for custom carrier baseline '{quant.BaseQuant.Names[0]}'. " + + "Custom carrier baselines must be learned once before they can participate in hybrid quantization."); + } - switch (hybrid.OverrideMode) + foreach (var kv in blanket.OrderBy(x => x.Key, StringComparer.Ordinal)) + { + result.Add(new RequestedTensorOverride { - case HybridTensorOverrideMode.ExactTensorScheme: - { - var exactScheme = hybrid.ExactTensorScheme!; + GroupName = "base_carrier", + TensorName = kv.Key, + SchemeName = kv.Value + }); + } + } - if (baseScheme != null && exactScheme.UniqueId == baseScheme.UniqueId) - continue; + foreach (var hybrid in quant.Tensors) + { + if (hybrid?.TGroup == null) + continue; - string schemeName = ResolveSchemeName(exactScheme); - foreach (var tensorName in expectedForGroup.OrderBy(x => x, StringComparer.Ordinal)) - { - result.Add(new RequestedTensorOverride - { - GroupName = hybrid.TGroup.Name, - TensorName = tensorName, - SchemeName = schemeName - }); - } + hybrid.ValidateOrThrow(); - break; - } + if (hybrid.MaterializedTensorScheme.UniqueId == TensorWeightScheme.NULL.UniqueId) + continue; - case HybridTensorOverrideMode.LearnedBaselineCandidate: + var expectedForGroup = GetExpectedTensorNamesForGroup(hybrid.TGroup, sourceTensorNames); + if (expectedForGroup.Count == 0) + continue; + + switch (hybrid.OverrideMode) + { + case HybridTensorOverrideMode.ExactTensorScheme: + { + var exactScheme = hybrid.ExactTensorScheme!; + if (!quant.BaseQuant.IsExternalRepositoryBaseline && baseScheme != null && exactScheme.UniqueId == baseScheme.UniqueId) + continue; + + string schemeName = ResolveSchemeName(exactScheme); + foreach (var tensorName in expectedForGroup.OrderBy(x => x, StringComparer.Ordinal)) { - var sourceBaseline = hybrid.CandidateBaseline!; - byte canonicalBaselineId = BaselineQuants.CanonicalLearningBaselineId(sourceBaseline); - var learned = TryLoadLearnedTensorMapping( - canonicalSourceBaselineId: canonicalBaselineId, - targetGroup: hybrid.TGroup, - preferredSourceScheme: sourceBaseline.DefaultTensorScheme, - allowDominantFallback: false); - - if (learned.Count == 0) + result.Add(new RequestedTensorOverride { - throw new InvalidOperationException( - $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}' (canonicalId={canonicalBaselineId}). " + - "Run with --relearn-baseline-mappings to regenerate."); - } + GroupName = hybrid.TGroup.Name, + TensorName = tensorName, + SchemeName = schemeName + }); + } - var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); - var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); - var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); + break; + } - if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) - { - var missingText = missingExpected.Count == 0 ? "none" : string.Join(", ", missingExpected.Take(15)); - var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); + case HybridTensorOverrideMode.LearnedBaselineCandidate: + { + var sourceBaseline = hybrid.CandidateBaseline!; + var learned = TryLoadLearnedTensorMapping( + sourceBaseline: sourceBaseline, + targetGroup: hybrid.TGroup, + preferredSourceScheme: sourceBaseline.DefaultTensorScheme, + allowDominantFallback: false); + + if (learned.Count == 0) + { + throw new InvalidOperationException( + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Run with --relearn-baseline-mappings to regenerate."); + } - throw new InvalidOperationException( - $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. " + - $"Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); - } + var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); + var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); + var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); - foreach (var kv in learned.OrderBy(x => x.Key, StringComparer.Ordinal)) - { - result.Add(new RequestedTensorOverride - { - GroupName = hybrid.TGroup.Name, - TensorName = kv.Key, - SchemeName = kv.Value - }); - } + if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) + { + var missingText = missingExpected.Count == 0 ? "none" : string.Join(", ", missingExpected.Take(15)); + var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); + throw new InvalidOperationException( + $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); + } - break; + foreach (var kv in learned.OrderBy(x => x.Key, StringComparer.Ordinal)) + { + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + TensorName = kv.Key, + SchemeName = kv.Value + }); } - default: - throw new InvalidOperationException( - $"Hybrid tensor for group '{hybrid.TGroup.Name}' has unsupported override mode '{hybrid.OverrideMode}'."); + break; } + + default: + throw new InvalidOperationException( + $"Hybrid tensor for group '{hybrid.TGroup.Name}' has unsupported override mode '{hybrid.OverrideMode}'."); } + } - return result; + return result; +} + +private Dictionary TryLoadAllLearnedTensorMappings( + string canonicalBaselineKey, + TensorWeightScheme? preferredSourceScheme = null, + bool allowDominantFallback = false) +{ + using var db = new MagicQuantContext(); + var model = db.AiModelHashes.AsNoTracking().FirstOrDefault(x => x.UniqueHash == Cache.CurrentModelId); + if (model == null) + return new Dictionary(StringComparer.Ordinal); + + var allRows = db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == model.Id) + .Where(x => x.BaselineCanonicalKey == canonicalBaselineKey) + .OrderBy(x => x.TensorName) + .ToList(); + + if (allRows.Count == 0) + return new Dictionary(StringComparer.Ordinal); + + var rows = allRows; + if (preferredSourceScheme != null) + { + var preferred = allRows.Where(x => x.TensorWeightSchemeId == preferredSourceScheme.UniqueId).ToList(); + if (preferred.Count > 0) + rows = preferred; + else if (!allowDominantFallback) + return new Dictionary(StringComparer.Ordinal); + } + + if (rows.Select(x => x.TensorWeightSchemeId).Distinct().Count() > 1) + { + if (!allowDominantFallback) + return new Dictionary(StringComparer.Ordinal); + + var dominantSchemeId = rows.GroupBy(x => x.TensorWeightSchemeId) + .OrderByDescending(g => g.Count()) + .ThenBy(g => g.Key) + .Select(g => g.Key) + .First(); + rows = rows.Where(x => x.TensorWeightSchemeId == dominantSchemeId).ToList(); } - private Dictionary TryLoadLearnedTensorMapping( - byte canonicalSourceBaselineId, + return rows.ToDictionary(x => x.TensorName, x => x.FinalQuantType, StringComparer.Ordinal); +} + +private Dictionary TryLoadLearnedTensorMapping( + BaselineQuants sourceBaseline, TensorGroup targetGroup, TensorWeightScheme? preferredSourceScheme = null, bool allowDominantFallback = false) @@ -1567,7 +1695,7 @@ private Dictionary TryLoadLearnedTensorMapping( var allRows = db.LearnedBaselineTensorQuants .AsNoTracking() .Where(x => x.AiModelHashId == model.Id) - .Where(x => x.BaselineQuantId == canonicalSourceBaselineId) + .Where(x => x.BaselineCanonicalKey == sourceBaseline.CanonicalKey) .Where(x => x.TensorGroupId == targetGroup.UniqueId) .OrderBy(x => x.TensorName) .ToList(); @@ -1614,52 +1742,42 @@ private Dictionary TryLoadLearnedTensorMapping( return rows.ToDictionary(x => x.TensorName, x => x.FinalQuantType, StringComparer.Ordinal); } - private List ResolveConcreteTensorOverrides( - IReadOnlyCollection allTensorNames, - List requestedOverrides) - { - if (requestedOverrides.Count == 0) - return new List(); - var nameSet = allTensorNames.ToHashSet(StringComparer.Ordinal); +private List ResolveConcreteTensorOverrides( + IReadOnlyCollection allTensorNames, + List requestedOverrides) +{ + if (requestedOverrides.Count == 0) + return new List(); - var missing = requestedOverrides - .Where(x => !nameSet.Contains(x.TensorName)) - .ToList(); + var nameSet = allTensorNames.ToHashSet(StringComparer.Ordinal); - if (missing.Count > 0) - { - throw new InvalidOperationException( - $"Required learned tensor mappings were missing in source GGUF ({missing.Count} tensors). " + - $"Examples: {string.Join(", ", missing.Take(10).Select(x => x.TensorName))}"); - } + var missing = requestedOverrides + .Where(x => !nameSet.Contains(x.TensorName)) + .ToList(); - var duplicates = requestedOverrides - .GroupBy(x => x.TensorName, StringComparer.Ordinal) - .Where(g => g.Select(x => x.SchemeName).Distinct(StringComparer.OrdinalIgnoreCase).Count() > 1) - .Select(g => g.Key) - .ToList(); + if (missing.Count > 0) + { + throw new InvalidOperationException( + $"Required learned tensor mappings were missing in source GGUF ({missing.Count} tensors). Examples: {string.Join(", ", missing.Take(10).Select(x => x.TensorName))}"); + } - if (duplicates.Count > 0) - { - throw new InvalidOperationException( - $"Conflicting learned mappings tried to assign multiple quant types to the same tensor: " + - $"{string.Join(", ", duplicates.Take(20))}"); - } + var lastWins = new Dictionary(StringComparer.Ordinal); + foreach (var item in requestedOverrides) + lastWins[item.TensorName] = item; - return requestedOverrides - .GroupBy(x => x.TensorName, StringComparer.Ordinal) - .Select(g => g.First()) - .Select(x => new ConcreteTensorOverride - { - GroupName = x.GroupName, - SchemeName = x.SchemeName, - TensorName = x.TensorName - }) - .ToList(); - } + return lastWins.Values + .Select(x => new ConcreteTensorOverride + { + GroupName = x.GroupName, + SchemeName = x.SchemeName, + TensorName = x.TensorName + }) + .OrderBy(x => x.TensorName, StringComparer.Ordinal) + .ToList(); +} - private async Task ReadTensorMetadataFromGgufAsync(string ggufPath, string outputFilePath) +private async Task ReadTensorMetadataFromGgufAsync(string ggufPath, string outputFilePath) { string workingDir = Path.GetDirectoryName(outputFilePath)!; string unique = Guid.NewGuid().ToString("N"); diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml new file mode 100644 index 0000000..b4b979e --- /dev/null +++ b/MagicQuant/config.default.yaml @@ -0,0 +1,64 @@ +# Production-friendly default runtime config. +# CLI flags still override anything here. + +paths: + magic_quant_root: + model_dir: + llama_root: + llama_bin: + convert_script: + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: false + force_imatrix_rebuild: false + force_relearn_baseline_tensor_mappings: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: + dataset_config: + dataset_local_file: + +evolution: + max_data_collected_per_category: 5 + max_survival_rounds: 4 + collapse_multiplier: 1.5 + brute_force_final_combination_threshold: 2000 + +isolation_pruning: + minimum_isolation_reduction_to_continue_ratio: 0.04 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + +prediction: + manual_max_predicted_size_bytes: 0 + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + custom_repositories: + # - repo_id: unsloth/Qwen3.6-35B-A3B-GGUF + # short_source_name: Unsloth + # enabled: true + # allow_as_learning_baseline: true + # allow_as_combination_carrier: true + # allow_as_explicit_group_candidate: true + # includes: + # - baseline_family: Q4_K + # file_name: Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf + # display_name: Unsloth-Q4_K_XL + # quantize_base_name: Q4_K + # requires_imatrix: false + # banned_group_ids: [] diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml new file mode 100644 index 0000000..8be7a40 --- /dev/null +++ b/MagicQuant/config.dev.yaml @@ -0,0 +1,50 @@ +# Dev config. This is auto-selected in DEBUG when --config is not supplied. + +paths: + magic_quant_root: + model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ + llama_root: + llama_bin: + convert_script: + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_relearn_baseline_tensor_mappings: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1m.jsonl + +evolution: + max_data_collected_per_category: 5 + max_survival_rounds: 4 + collapse_multiplier: 1.5 + brute_force_final_combination_threshold: 2000 + +isolation_pruning: + minimum_isolation_reduction_to_continue_ratio: 0.04 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + +prediction: + manual_max_predicted_size_bytes: 0 + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + custom_repositories: [] From f95c45b67e31f486962a05774e4a2d7492e4bb86 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 21 Apr 2026 16:37:28 -0400 Subject: [PATCH 108/258] new start to allowing new learned and yaml settings --- .../20260421190835_InitialCreate.Designer.cs | 665 ++++++++++++++++++ .../20260421190835_InitialCreate.cs | 500 +++++++++++++ .../MagicQuantContextModelSnapshot.cs | 662 +++++++++++++++++ .../Configuration/MagicQuantYamlConfig.cs | 183 +++++ .../Configuration/MagicQuantYamlLoader.cs | 175 +++++ 5 files changed, 2185 insertions(+) create mode 100644 MQ.DB/Migrations/20260421190835_InitialCreate.Designer.cs create mode 100644 MQ.DB/Migrations/20260421190835_InitialCreate.cs create mode 100644 MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs create mode 100644 MagicQuant/Configuration/MagicQuantYamlConfig.cs create mode 100644 MagicQuant/Configuration/MagicQuantYamlLoader.cs diff --git a/MQ.DB/Migrations/20260421190835_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260421190835_InitialCreate.Designer.cs new file mode 100644 index 0000000..7e8ef7c --- /dev/null +++ b/MQ.DB/Migrations/20260421190835_InitialCreate.Designer.cs @@ -0,0 +1,665 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260421190835_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("CanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ExplicitCandidateSortOrder") + .HasColumnType("INTEGER"); + + b.Property("IsCombinationCarrierCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsCustomBaseline") + .HasColumnType("INTEGER"); + + b.Property("IsExplicitGroupCombinationCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsLearningBaseline") + .HasColumnType("INTEGER"); + + b.Property("QuantizeBaseArgumentName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RequiresImatrix") + .HasColumnType("INTEGER"); + + b.Property("ShortSourceName") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceOwner") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("SourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("CanonicalKey") + .IsUnique(); + + b.HasIndex("SourceRepository", "SourceFileName"); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260421190835_InitialCreate.cs b/MQ.DB/Migrations/20260421190835_InitialCreate.cs new file mode 100644 index 0000000..0a09c7d --- /dev/null +++ b/MQ.DB/Migrations/20260421190835_InitialCreate.cs @@ -0,0 +1,500 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AiModelHashes", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + UniqueHash = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AiModelHashes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "BaselineQuantDefinitions", + columns: table => new + { + BaselineQuantId = table.Column(type: "INTEGER", nullable: false), + CanonicalKey = table.Column(type: "TEXT", maxLength: 256, nullable: false), + BaselineName = table.Column(type: "TEXT", maxLength: 128, nullable: false), + QuantizeBaseArgumentName = table.Column(type: "TEXT", maxLength: 64, nullable: false), + DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), + DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false), + SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), + SourceOwner = table.Column(type: "TEXT", maxLength: 128, nullable: true), + SourceRepository = table.Column(type: "TEXT", maxLength: 256, nullable: true), + SourceFileName = table.Column(type: "TEXT", maxLength: 512, nullable: true), + ShortSourceName = table.Column(type: "TEXT", maxLength: 64, nullable: true), + IsCustomBaseline = table.Column(type: "INTEGER", nullable: false), + IsLearningBaseline = table.Column(type: "INTEGER", nullable: false), + IsCombinationCarrierCandidate = table.Column(type: "INTEGER", nullable: false), + IsExplicitGroupCombinationCandidate = table.Column(type: "INTEGER", nullable: false), + RequiresImatrix = table.Column(type: "INTEGER", nullable: false), + ExplicitCandidateSortOrder = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.BaselineQuantId); + }); + + migrationBuilder.CreateTable( + name: "TensorCombos", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AttnKV = table.Column(type: "INTEGER", nullable: false), + AttnOutput = table.Column(type: "INTEGER", nullable: false), + AttnQ = table.Column(type: "INTEGER", nullable: false), + BaseQuant = table.Column(type: "INTEGER", nullable: false), + Embeddings = table.Column(type: "INTEGER", nullable: false), + FfnDown = table.Column(type: "INTEGER", nullable: false), + FfnUpGate = table.Column(type: "INTEGER", nullable: false), + LmHead = table.Column(type: "INTEGER", nullable: false), + MoeExperts = table.Column(type: "INTEGER", nullable: false), + MoeRouter = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TensorCombos", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ImatrixDefinitions", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + IdentityHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), + CanonicalPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true), + SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + MetadataJson = table.Column(type: "TEXT", maxLength: 8000, nullable: true), + TokenCount = table.Column(type: "INTEGER", nullable: true), + BuildFingerprint = table.Column(type: "TEXT", maxLength: 512, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ImatrixDefinitions", x => x.Id); + table.ForeignKey( + name: "FK_ImatrixDefinitions_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AiBenchmarks", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + Ngl = table.Column(type: "INTEGER", nullable: false), + SizeBytes = table.Column(type: "INTEGER", nullable: false), + TokensPerSecond = table.Column(type: "REAL", nullable: false), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AiBenchmarks", x => x.Id); + table.ForeignKey( + name: "FK_AiBenchmarks_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AiBenchmarks_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "ExecutionPlanProbeCaches", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + HardwareFingerprint = table.Column(type: "TEXT", maxLength: 1024, nullable: false), + QuantizedModelFingerprint = table.Column(type: "TEXT", maxLength: 2048, nullable: false), + QuantizationKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), + DiscoveryTokenTarget = table.Column(type: "INTEGER", nullable: false), + StaticNgl = table.Column(type: "INTEGER", nullable: false), + UsesGpu = table.Column(type: "INTEGER", nullable: false), + GroupSize = table.Column(type: "INTEGER", nullable: false), + SlotsJson = table.Column(type: "TEXT", maxLength: 8000, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + UpdatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ExecutionPlanProbeCaches", x => x.Id); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "CategoryBenchmark", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + Category = table.Column(type: "INTEGER", nullable: false), + Kld = table.Column(type: "REAL", nullable: false), + Ppl = table.Column(type: "REAL", nullable: false), + PplError = table.Column(type: "REAL", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CategoryBenchmark", x => x.Id); + table.ForeignKey( + name: "FK_CategoryBenchmark_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "LearnedBaselineTensorQuants", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + BaselineQuantId = table.Column(type: "INTEGER", nullable: false), + BaselineCanonicalKey = table.Column(type: "TEXT", maxLength: 256, nullable: false), + BaselineSourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), + BaselineSourceRepository = table.Column(type: "TEXT", maxLength: 256, nullable: true), + BaselineSourceFileName = table.Column(type: "TEXT", maxLength: 512, nullable: true), + TensorWeightSchemeId = table.Column(type: "INTEGER", nullable: false), + TensorGroupId = table.Column(type: "INTEGER", nullable: false), + TensorName = table.Column(type: "TEXT", maxLength: 512, nullable: false), + FinalQuantType = table.Column(type: "TEXT", maxLength: 32, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_LearnedBaselineTensorQuants", x => x.Id); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "QuantizationRuns", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: true), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: false), + DurationMs = table.Column(type: "INTEGER", nullable: false), + Succeeded = table.Column(type: "INTEGER", nullable: false), + Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true), + OutputModelPath = table.Column(type: "TEXT", maxLength: 2048, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_QuantizationRuns", x => x.Id); + table.ForeignKey( + name: "FK_QuantizationRuns_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_QuantizationRuns_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_QuantizationRuns_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "BenchmarkRuns", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + TensorComboId = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + CategoryBenchmarkId = table.Column(type: "TEXT", nullable: true), + Category = table.Column(type: "INTEGER", nullable: false), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: false), + DurationMs = table.Column(type: "INTEGER", nullable: false), + Succeeded = table.Column(type: "INTEGER", nullable: false), + Error = table.Column(type: "TEXT", maxLength: 4000, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_BenchmarkRuns", x => x.Id); + table.ForeignKey( + name: "FK_BenchmarkRuns_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_CategoryBenchmark_CategoryBenchmarkId", + column: x => x.CategoryBenchmarkId, + principalTable: "CategoryBenchmark", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_BenchmarkRuns_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_BenchmarkRuns_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", + table: "AiBenchmarks", + columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_ImatrixDefinitionId", + table: "AiBenchmarks", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_TensorComboId", + table: "AiBenchmarks", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_AiModelHashes_UniqueHash", + table: "AiModelHashes", + column: "UniqueHash"); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_CanonicalKey", + table: "BaselineQuantDefinitions", + column: "CanonicalKey", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_SourceRepository_SourceFileName", + table: "BaselineQuantDefinitions", + columns: new[] { "SourceRepository", "SourceFileName" }); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiBenchmarkId", + table: "BenchmarkRuns", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiBenchmarkId_Category", + table: "BenchmarkRuns", + columns: new[] { "AiBenchmarkId", "Category" }); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_AiModelHashId", + table: "BenchmarkRuns", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_CategoryBenchmarkId", + table: "BenchmarkRuns", + column: "CategoryBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_ImatrixDefinitionId", + table: "BenchmarkRuns", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_StartedUtc", + table: "BenchmarkRuns", + column: "StartedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_TensorComboId", + table: "BenchmarkRuns", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_CategoryBenchmark_AiBenchmarkId", + table: "CategoryBenchmark", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_AiModelHashId", + table: "ExecutionPlanProbeCaches", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", + table: "ExecutionPlanProbeCaches", + columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_ImatrixDefinitionId", + table: "ExecutionPlanProbeCaches", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_ImatrixDefinitions_AiModelHashId_IdentityHash", + table: "ImatrixDefinitions", + columns: new[] { "AiModelHashId", "IdentityHash" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiBenchmarkId", + table: "LearnedBaselineTensorQuants", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineCanonicalKey_TensorWeightSchemeId_TensorName", + table: "LearnedBaselineTensorQuants", + columns: new[] { "AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorGroupId", + table: "LearnedBaselineTensorQuants", + columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId" }); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_AiBenchmarkId", + table: "QuantizationRuns", + column: "AiBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_AiModelHashId", + table: "QuantizationRuns", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_ImatrixDefinitionId", + table: "QuantizationRuns", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_StartedUtc", + table: "QuantizationRuns", + column: "StartedUtc"); + + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_TensorComboId", + table: "QuantizationRuns", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", + table: "TensorCombos", + columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BaselineQuantDefinitions"); + + migrationBuilder.DropTable( + name: "BenchmarkRuns"); + + migrationBuilder.DropTable( + name: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropTable( + name: "LearnedBaselineTensorQuants"); + + migrationBuilder.DropTable( + name: "QuantizationRuns"); + + migrationBuilder.DropTable( + name: "CategoryBenchmark"); + + migrationBuilder.DropTable( + name: "AiBenchmarks"); + + migrationBuilder.DropTable( + name: "ImatrixDefinitions"); + + migrationBuilder.DropTable( + name: "TensorCombos"); + + migrationBuilder.DropTable( + name: "AiModelHashes"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs new file mode 100644 index 0000000..4174181 --- /dev/null +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -0,0 +1,662 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + partial class MagicQuantContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("CanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ExplicitCandidateSortOrder") + .HasColumnType("INTEGER"); + + b.Property("IsCombinationCarrierCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsCustomBaseline") + .HasColumnType("INTEGER"); + + b.Property("IsExplicitGroupCombinationCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsLearningBaseline") + .HasColumnType("INTEGER"); + + b.Property("QuantizeBaseArgumentName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RequiresImatrix") + .HasColumnType("INTEGER"); + + b.Property("ShortSourceName") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceOwner") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("SourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("CanonicalKey") + .IsUnique(); + + b.HasIndex("SourceRepository", "SourceFileName"); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs new file mode 100644 index 0000000..ccf26a5 --- /dev/null +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -0,0 +1,183 @@ +using YamlDotNet.Serialization; + +namespace MagicQuant.Configuration; + +public sealed class MagicQuantYamlConfig +{ + public RuntimePathConfig Paths { get; set; } = new(); + public RuntimeFlagConfig Flags { get; set; } = new(); + public RuntimeImatrixConfig Imatrix { get; set; } = new(); + public RuntimeEvolutionConfig Evolution { get; set; } = new(); + public RuntimeIsolationPruningConfig IsolationPruning { get; set; } = new(); + public RuntimePredictionConfig Prediction { get; set; } = new(); + public RuntimeBaselineConfig Baselines { get; set; } = new(); + + public List SensitivityProbeGroups { get; set; } = + [ + "embeddings", + "lm_head", + "attn_q", + "attn_kv", + "attn_output", + "ffn_up_gate", + "ffn_down" + ]; + + public List SensitivityProbeGroupsMoe { get; set; } = + [ + "moe_router", + "moe_experts" + ]; + + public List BrainLayers { get; set; } = + [ + "embeddings", + "lm_head", + "attn_output" + ]; + + public List CollapsePenaltySchemes { get; set; } = + [ + "MXFP4", + "IQ2_XXS", + "IQ2_XS", + "IQ2_S" + ]; + + public List MoeIndicatorTensors { get; set; } = + [ + "blk.*.ffn_up_expert_0.weight", + "blk.*.ffn_gate_expert_0.weight", + "blk.*.ffn_down_expert_0.weight", + "blk.*.ffn_up_exps.weight", + "blk.*.ffn_gate_exps.weight", + "blk.*.ffn_down_exps.weight", + "blk.*.ffn_gate_inp.weight", + "router.weight", + "gate.weight", + "blk.*.router.*", + "blk.*.gate_proj.*", + "blk.*.gate_inp.*", + "model.language_model.layers.*.mlp.experts.gate_up_proj", + "model.language_model.layers.*.mlp.experts.down_proj", + "model.language_model.layers.*.mlp.gate.weight", + "model.language_model.layers.*.mlp.shared_expert.gate_proj.weight", + "model.language_model.layers.*.mlp.shared_expert.up_proj.weight", + "model.language_model.layers.*.mlp.shared_expert.down_proj.weight", + "model.language_model.layers.*.experts.gate_up_proj", + "model.language_model.layers.*.experts.down_proj", + "model.language_model.layers.*.router.proj.weight", + "model.language_model.layers.*.router.per_expert_scale", + "model.language_model.layers.*.router.scale" + ]; + + public static MagicQuantYamlConfig CreateDefault() => new(); +} + +public sealed class RuntimePathConfig +{ + public string? MagicQuantRoot { get; set; } + public string? ModelDir { get; set; } + public string? LlamaRoot { get; set; } + public string? LlamaBin { get; set; } + public string? ConvertScript { get; set; } + public string ExternalBaselineCacheDirName { get; set; } = "ExternalBaselines"; +} + +public sealed class RuntimeFlagConfig +{ + public bool UseImatrix { get; set; } + public bool ForceImatrixRebuild { get; set; } + public bool ForceRelearnBaselineTensorMappings { get; set; } + public bool ForceRefreshHardwareProbe { get; set; } + public bool AllowHighPrecisionHybrids { get; set; } +} + +public sealed class RuntimeImatrixConfig +{ + public string? ImatrixUrl { get; set; } + public string? DatasetRepo { get; set; } + public string? DatasetSplit { get; set; } + public string? DatasetConfig { get; set; } + public string? DatasetLocalFile { get; set; } +} + +public sealed class RuntimeEvolutionConfig +{ + public int MaxDataCollectedPerCategory { get; set; } = 5; + public int MaxSurvivalRounds { get; set; } = 4; + public double CollapseMultiplier { get; set; } = 1.5d; + public int BruteForceFinalCombinationThreshold { get; set; } = 2_000; +} + +public sealed class RuntimeIsolationPruningConfig +{ + public double MinimumIsolationReductionToContinueRatio { get; set; } = 0.04d; + public double MinimumIsolationReductionToSuppressBf16Ratio { get; set; } = 0.10d; + public double MaximumIsolationPplDeltaPercent { get; set; } = 5.0d; + public double MaximumIsolationKld { get; set; } = 0.1d; + public double BadTradeMaxSizeDeltaPercent { get; set; } = 4.0d; + public double BadTradeKldMultiplier { get; set; } = 2.5d; + public double BadTradePplMultiplier { get; set; } = 3.5d; + public double FloatingPointEpsilon { get; set; } = 1e-8d; + public double MinimumMeaningfulBaseOnlyReductionRatio { get; set; } = 0.01d; +} + +public sealed class RuntimePredictionConfig +{ + public ulong ManualMaxPredictedSizeBytes { get; set; } = 0; +} + +public sealed class RuntimeBaselineConfig +{ + public string StandardBaselinesMode { get; set; } = "all"; + public List EnabledStandardLearningBaselines { get; set; } = new(); + public List EnabledStandardCombinationCarriers { get; set; } = new(); + public List EnabledStandardExplicitGroupCandidates { get; set; } = new(); + public List CustomRepositories { get; set; } = new(); + + [YamlIgnore] + public List ResolvedCustomBaselines { get; set; } = new(); +} + +public sealed class CustomBaselineRepositoryConfig +{ + public string RepoId { get; set; } = string.Empty; + public string? ShortSourceName { get; set; } + public bool Enabled { get; set; } = true; + public bool AllowAsCombinationCarrier { get; set; } + public bool AllowAsExplicitGroupCandidate { get; set; } = true; + public bool AllowAsLearningBaseline { get; set; } = true; + public List Includes { get; set; } = new(); +} + +public sealed class CustomBaselineIncludeConfig +{ + public string BaselineFamily { get; set; } = string.Empty; + public string? FileName { get; set; } + public string? DisplayName { get; set; } + public string? QuantizeBaseName { get; set; } + public bool? RequiresImatrix { get; set; } + public bool? AllowAsCombinationCarrier { get; set; } + public bool? AllowAsExplicitGroupCandidate { get; set; } + public bool? AllowAsLearningBaseline { get; set; } + public List BannedGroupIds { get; set; } = new(); +} + +public sealed class ResolvedCustomBaselineSpec +{ + public byte DynamicBaselineId { get; set; } + public string CanonicalKey { get; set; } = string.Empty; + public string DisplayName { get; set; } = string.Empty; + public string RepoId { get; set; } = string.Empty; + public string SourceOwner { get; set; } = string.Empty; + public string SourceFileName { get; set; } = string.Empty; + public string ShortSourceName { get; set; } = string.Empty; + public string BaselineFamily { get; set; } = string.Empty; + public string QuantizeBaseName { get; set; } = string.Empty; + public bool RequiresImatrix { get; set; } + public bool AllowAsLearningBaseline { get; set; } + public bool AllowAsCombinationCarrier { get; set; } + public bool AllowAsExplicitGroupCandidate { get; set; } + public IReadOnlyList BannedGroupIds { get; set; } = Array.Empty(); +} diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs new file mode 100644 index 0000000..daf744a --- /dev/null +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -0,0 +1,175 @@ +using System.Diagnostics; +using MagicQuant.Helpers; +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace MagicQuant.Configuration; + +public static class MagicQuantYamlLoader +{ + public static MagicQuantYamlConfig LoadAndApply(string commandName, IReadOnlyList args) + { + string configPath = ResolveConfigPath(args); + Cache.ActiveConfigPath = configPath; + + if (!File.Exists(configPath)) + { + throw new FileNotFoundException( + $"MagicQuant config file was not found at '{configPath}'. " + + "Ensure config.default.yaml or config.dev.yaml is copied next to the build output, or pass --config."); + } + + var deserializer = new DeserializerBuilder() + .IgnoreUnmatchedProperties() + .WithNamingConvention(UnderscoredNamingConvention.Instance) + .Build(); + + var yaml = File.ReadAllText(configPath); + var loaded = deserializer.Deserialize(yaml) ?? MagicQuantYamlConfig.CreateDefault(); + ApplyCliOverrides(loaded, args); + NormalizeAndApply(loaded); + + Config.Load(loaded); + + AnsiConsole.MarkupLine($"[grey]Using config:[/] {Markup.Escape(configPath)}"); + return loaded; + } + + public static string ResolveConfigPath(IReadOnlyList args) + { + string? explicitPath = args.FirstOrDefault(a => string.Equals(a.Name, "config", StringComparison.OrdinalIgnoreCase))?.Value; + if (!string.IsNullOrWhiteSpace(explicitPath)) + return Path.GetFullPath(explicitPath); + +#if DEBUG + string preferred = Path.Combine(AppContext.BaseDirectory, "config.dev.yaml"); + if (File.Exists(preferred)) + return preferred; +#endif + return Path.Combine(AppContext.BaseDirectory, "config.default.yaml"); + } + + private static void NormalizeAndApply(MagicQuantYamlConfig config) + { + config.Paths.MagicQuantRoot = ResolveMagicQuantRoot(config.Paths.MagicQuantRoot); + Cache.MagicQuantDirectory = config.Paths.MagicQuantRoot; + Cache.LlamaRoot = NormalizeNullOrFullPath(config.Paths.LlamaRoot); + Cache.LlamaBin = NormalizeNullOrFullPath(config.Paths.LlamaBin); + Cache.ConvertScript = NormalizeNullOrFullPath(config.Paths.ConvertScript); + Cache.ExternalBaselineCacheDirectory = Path.Combine( + config.Paths.MagicQuantRoot!, + string.IsNullOrWhiteSpace(config.Paths.ExternalBaselineCacheDirName) ? "ExternalBaselines" : config.Paths.ExternalBaselineCacheDirName); + + Directory.CreateDirectory(Cache.MagicQuantDirectory!); + Directory.CreateDirectory(Cache.ExternalBaselineCacheDirectory!); + + Cache.UseImatrix = config.Flags.UseImatrix; + Cache.ForceImatrixRebuild = config.Flags.ForceImatrixRebuild; + Cache.ForceRelearnBaselineTensorMappings = config.Flags.ForceRelearnBaselineTensorMappings; + Cache.ForceRefreshHardwareProbe = config.Flags.ForceRefreshHardwareProbe; + + RuntimeSearchSpace.AllowHighPrecisionHybrids = config.Flags.AllowHighPrecisionHybrids; + + ApplyStandardBaselineFilters(config.Baselines); + BaselineQuants.ResetDynamicCustomBaselines(); + } + + private static void ApplyStandardBaselineFilters(RuntimeBaselineConfig baselineConfig) + { + string mode = (baselineConfig.StandardBaselinesMode ?? "all").Trim().ToLowerInvariant(); + + HashSet? learning = null; + HashSet? carriers = null; + HashSet? explicitCandidates = null; + + if (mode == "none") + { + learning = new HashSet(); + carriers = new HashSet(); + explicitCandidates = new HashSet(); + } + else if (mode == "selected") + { + learning = ResolveStandardBaselineIds(baselineConfig.EnabledStandardLearningBaselines); + carriers = ResolveStandardBaselineIds(baselineConfig.EnabledStandardCombinationCarriers); + explicitCandidates = ResolveStandardBaselineIds(baselineConfig.EnabledStandardExplicitGroupCandidates); + } + + BaselineQuants.ConfigureStandardRoleFilters(learning, carriers, explicitCandidates); + } + + private static HashSet ResolveStandardBaselineIds(IEnumerable names) + { + var result = new HashSet(); + + foreach (var raw in names ?? Array.Empty()) + { + if (string.IsNullOrWhiteSpace(raw)) + continue; + + var baseline = BaselineQuants.ResolveBuiltInStandardBaseline(raw.Trim()); + if (baseline == null) + { + throw new InvalidOperationException( + $"Unknown built-in baseline '{raw}'. " + + $"Known values: {string.Join(", ", BaselineQuants.GetBuiltInStandardBaselines().Select(x => x.Names[0]))}"); + } + + result.Add(baseline.UniqueId); + } + + return result; + } + + private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList args) + { + string? Get(string name) => args.FirstOrDefault(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase))?.Value; + bool Has(string name) => args.Any(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase)); + + config.Paths.ModelDir = Prefer(Get("model-dir"), config.Paths.ModelDir); + config.Paths.LlamaRoot = Prefer(Get("llama-root"), config.Paths.LlamaRoot); + config.Paths.LlamaBin = Prefer(Get("llama-bin"), config.Paths.LlamaBin); + config.Paths.ConvertScript = Prefer(Get("convert-script"), config.Paths.ConvertScript); + + if (Has("use-imatrix")) config.Flags.UseImatrix = true; + if (Has("imatrix-force-rebuild")) config.Flags.ForceImatrixRebuild = true; + if (Has("relearn-baseline-mappings")) config.Flags.ForceRelearnBaselineTensorMappings = true; + if (Has("recheck-hardware-probe")) config.Flags.ForceRefreshHardwareProbe = true; + if (Has("allow-high-precision-hybrids")) config.Flags.AllowHighPrecisionHybrids = true; + + config.Imatrix.ImatrixUrl = Prefer(Get("imatrix-url"), config.Imatrix.ImatrixUrl); + config.Imatrix.DatasetRepo = Prefer(Get("imatrix-dataset-repo"), config.Imatrix.DatasetRepo); + config.Imatrix.DatasetSplit = Prefer(Get("imatrix-dataset-split"), config.Imatrix.DatasetSplit); + config.Imatrix.DatasetConfig = Prefer(Get("imatrix-dataset-config"), config.Imatrix.DatasetConfig); + config.Imatrix.DatasetLocalFile = Prefer(Get("imatrix-dataset-local-file"), config.Imatrix.DatasetLocalFile); + + if (int.TryParse(Get("brute-force-final-combination-threshold"), out var bruteForceThreshold) && bruteForceThreshold > 0) + config.Evolution.BruteForceFinalCombinationThreshold = bruteForceThreshold; + + if (ulong.TryParse(Get("manual-max-predicted-size-bytes"), out var manualBytes)) + config.Prediction.ManualMaxPredictedSizeBytes = manualBytes; + } + + private static string? Prefer(string? preferred, string? fallback) + => string.IsNullOrWhiteSpace(preferred) ? fallback : preferred; + + private static string ResolveMagicQuantRoot(string? configured) + { + if (!string.IsNullOrWhiteSpace(configured)) + return Path.GetFullPath(configured); + + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), MagicConstants.MagicQuantFolder); + } + + private static string? NormalizeNullOrFullPath(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return null; + + return Path.GetFullPath(value); + } +} From 9c5d96681afb7da1be11db214ce475c6b19b5909 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 22 Apr 2026 12:03:43 -0400 Subject: [PATCH 109/258] some repair, the full download and learn from unsloth not yet working --- MQ.DB/Models/BaselineQuants.cs | 191 ++++++------------ .../Helpers/NativePrecisionNormalization.cs | 70 +++++++ MagicQuant/MagicQuant.csproj | 3 + MagicQuant/Services/QuantizationService.cs | 38 +++- MagicQuant/config.dev.backup.yaml | 50 +++++ MagicQuant/config.dev.yaml | 67 +++++- 6 files changed, 286 insertions(+), 133 deletions(-) create mode 100644 MagicQuant/Helpers/NativePrecisionNormalization.cs create mode 100644 MagicQuant/config.dev.backup.yaml diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 9bcad79..30842ed 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -37,7 +37,10 @@ public record BaselineQuants( public ImmutableArray TensorWeightSchemes => LearnedMatchTensorWeightSchemes; public bool IsPureBaselineCandidate => IsLearningBaseline; public bool IsHighPrecisionExplicitCandidate => IsHighPrecisionExactAlias; - public bool IsExternalRepositoryBaseline => IsCustomBaseline && !string.IsNullOrWhiteSpace(SourceRepository) && !string.IsNullOrWhiteSpace(SourceFileName); + public bool IsExternalRepositoryBaseline => + IsCustomBaseline && + !string.IsNullOrWhiteSpace(SourceRepository) && + !string.IsNullOrWhiteSpace(SourceFileName); private static BaselineQuants Create( byte uniqueId, @@ -83,118 +86,47 @@ private static BaselineQuants Create( } public static readonly BaselineQuants Q8_0 = - Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], - isLearningBaseline: true, - isCombinationCarrierCandidate: true, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 11); + Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 11); public static readonly BaselineQuants Q6_K = - Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], - isLearningBaseline: true, - isCombinationCarrierCandidate: true, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 10); + Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, true, true, false, 10); public static readonly BaselineQuants Q5_K = - Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: true, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 9); + Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 9); public static readonly BaselineQuants Q4_K_M = - Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: true, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 8); + Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 8); public static readonly BaselineQuants IQ4_NL = - Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: true, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 7); + Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, true, true, false, 7); public static readonly BaselineQuants IQ4_XS = - Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: true, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 6); + Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, true, true, false, 6); public static readonly BaselineQuants IQ3_S = - Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 5); + Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 5); public static readonly BaselineQuants IQ3_XS = - Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 4); + Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 4); public static readonly BaselineQuants IQ3_XXS = - Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 3); + Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3); public static readonly BaselineQuants IQ2_S = - Create(10, true, "IQ2_S", "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 2); + Create(10, true, "IQ2_S", "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2); public static readonly BaselineQuants IQ2_XS = - Create(11, true, "IQ2_XS", "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 1); + Create(11, true, "IQ2_XS", "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 1); public static readonly BaselineQuants IQ2_XXS = - Create(12, true, "IQ2_XXS", "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], - isLearningBaseline: true, - isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: true, - isHighPrecisionExactAlias: false, - explicitCandidateSortOrder: 0); + Create(12, true, "IQ2_XXS", "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], true, false, true, false, 0); + public static readonly BaselineQuants BF16_Hybrid = - Create(201, false, "BF16", "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], - isLearningBaseline: false, - isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: false, - isHighPrecisionExactAlias: true, - canonicalKey: "alias:bf16", - sourceKind: "exact_alias"); + Create(201, false, "BF16", "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], false, false, false, true, int.MaxValue, false, "alias:bf16", "exact_alias", null, null, null, null); public static readonly BaselineQuants F16_Hybrid = - Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], - isLearningBaseline: false, - isCombinationCarrierCandidate: false, - isExplicitGroupCombinationCandidate: false, - isHighPrecisionExactAlias: true, - canonicalKey: "alias:f16", - sourceKind: "exact_alias"); + Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], false, false, false, true, int.MaxValue, false, "alias:f16", "exact_alias", null, null, null, null); private static readonly ImmutableArray StandardBaselines = [ @@ -250,15 +182,15 @@ public static BaselineQuants CreateDynamicCustomBaseline( isLearningBaseline, isCombinationCarrierCandidate, isExplicitGroupCombinationCandidate, - IsHighPrecisionExactAlias: false, - IsCustomBaseline: true, - CanonicalKey: canonicalKey, - SourceKind: sourceKind, - SourceOwner: sourceOwner, - SourceRepository: sourceRepository, - SourceFileName: sourceFileName, - ShortSourceName: shortSourceName, - ExplicitCandidateSortOrder: explicitCandidateSortOrder); + false, + true, + canonicalKey, + sourceKind, + sourceOwner, + sourceRepository, + sourceFileName, + shortSourceName, + explicitCandidateSortOrder); } public static void ResetDynamicCustomBaselines() @@ -308,8 +240,6 @@ public static void ConfigureStandardRoleFilters( EnabledStandardExplicitCandidateIds = enabledExplicitCandidateIds == null ? null : enabledExplicitCandidateIds.ToHashSet(); } - - public static void ConfigureStandardPolicy( bool includeStandardLearningBaselines, bool includeStandardCombinationCarriers, @@ -379,24 +309,28 @@ public sealed class ExternalBaselineRegistration public static BaselineQuants RegisterCustomExternalBaseline(ExternalBaselineRegistration registration) { + var sortOrder = StandardBaselines + .FirstOrDefault(x => string.Equals(x.Names[0], registration.BaselineFamilyName, StringComparison.OrdinalIgnoreCase)) + ?.ExplicitCandidateSortOrder ?? int.MaxValue; + var baseline = CreateDynamicCustomBaseline( - uniqueId: GetFirstAvailableDynamicBaselineId(), - displayName: registration.DisplayName, - quantizeBaseArgumentName: registration.QuantizeBaseArgumentName, - sourceRepository: registration.Repository, - sourceFileName: registration.RepositoryFileName, - shortSourceName: registration.OwnerShortName, - sourceOwner: registration.OwnerShortName, - sourceKind: "huggingface_repo", - canonicalKey: registration.CanonicalKey, - primaryTensorWeightScheme: registration.TensorScheme, - learnedMatchTensorWeightSchemes: [registration.TensorScheme], - bannedGroupIds: registration.BannedGroupIds, - requiresImatrix: registration.RequiresImatrix, - isLearningBaseline: registration.AddAsLearningBaseline, - isCombinationCarrierCandidate: registration.AddAsCombinationCarrier, - isExplicitGroupCombinationCandidate: registration.AddAsGroupCandidate, - explicitCandidateSortOrder: StandardBaselines.FirstOrDefault(x => string.Equals(x.Names[0], registration.BaselineFamilyName, StringComparison.OrdinalIgnoreCase))?.ExplicitCandidateSortOrder ?? int.MaxValue); + GetFirstAvailableDynamicBaselineId(), + registration.DisplayName, + registration.QuantizeBaseArgumentName, + registration.Repository, + registration.RepositoryFileName, + registration.OwnerShortName, + registration.OwnerShortName, + "huggingface_repo", + registration.CanonicalKey, + registration.TensorScheme, + [registration.TensorScheme], + registration.BannedGroupIds, + registration.RequiresImatrix, + registration.AddAsLearningBaseline, + registration.AddAsCombinationCarrier, + registration.AddAsGroupCandidate, + sortOrder); RegisterDynamicCustomBaseline(baseline); return baseline; @@ -414,18 +348,18 @@ public static BaselineQuants GetNativeQuant() nativeScheme, [nativeScheme], [], - IsLearningBaseline: false, - IsCombinationCarrierCandidate: false, - IsExplicitGroupCombinationCandidate: false, - IsHighPrecisionExactAlias: true, - IsCustomBaseline: false, - CanonicalKey: $"native:{nativeScheme.Names[0].ToLowerInvariant()}", - SourceKind: "native_exact_alias", - SourceOwner: null, - SourceRepository: null, - SourceFileName: null, - ShortSourceName: null, - ExplicitCandidateSortOrder: int.MaxValue); + false, + false, + false, + true, + false, + $"native:{nativeScheme.Names[0].ToLowerInvariant()}", + "native_exact_alias", + null, + null, + null, + null, + int.MaxValue); } public static BaselineQuants GetBF16Quant() => GetNativeQuant(); @@ -636,8 +570,7 @@ public static BaselineQuants FromTensorSchemeId(byte schemeId) if (schemeId == TensorWeightScheme.F16.UniqueId) return F16_Hybrid; - if (schemeId == TensorWeightScheme.GetCurrentNativePrecisionScheme().UniqueId && - schemeId == TensorWeightScheme.F32.UniqueId) + if (schemeId == TensorWeightScheme.GetCurrentNativePrecisionScheme().UniqueId) return GetNativeQuant(); var found = GetAllRecognizedBaselines() diff --git a/MagicQuant/Helpers/NativePrecisionNormalization.cs b/MagicQuant/Helpers/NativePrecisionNormalization.cs new file mode 100644 index 0000000..479aa38 --- /dev/null +++ b/MagicQuant/Helpers/NativePrecisionNormalization.cs @@ -0,0 +1,70 @@ +using MQ.DB.Models; + +namespace MagicQuant.Helpers; + +public static class NativePrecisionNormalization +{ + public static string NormalizeLearnedFinalQuantTypeForApplication(string? observedFinalQuantType) + { + if (string.IsNullOrWhiteSpace(observedFinalQuantType)) + return string.Empty; + + var canonical = Canonicalize(observedFinalQuantType); + var native = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + + // Keep raw F32 as-is. Do not silently collapse F32 to BF16/F16. + if (canonical == "F32") + return TensorWeightScheme.F32.Names[0]; + + // Treat F16/BF16 as "native high precision kept" when replaying + // learned behavior into the active source model. + if (canonical == "F16" || canonical == "BF16") + return native.Names[0]; + + return observedFinalQuantType.Trim(); + } + + public static IReadOnlyCollection ResolveSchemeIdsForLearnedFinalQuantType(string? observedFinalQuantType) + { + var result = new HashSet(); + + if (string.IsNullOrWhiteSpace(observedFinalQuantType)) + return result; + + var canonical = Canonicalize(observedFinalQuantType); + + // Keep F32 exact if observed. + if (canonical == "F32") + { + result.Add(TensorWeightScheme.F32.UniqueId); + return result; + } + + // Treat F16/BF16 as native high precision for pruning/application logic. + if (canonical == "F16" || canonical == "BF16") + { + result.Add(TensorWeightScheme.GetCurrentNativePrecisionScheme().UniqueId); + return result; + } + + foreach (var scheme in TensorWeightScheme.All) + { + if (scheme.Names.IsDefaultOrEmpty) + continue; + + if (scheme.Names.Any(x => Canonicalize(x) == canonical)) + result.Add(scheme.UniqueId); + } + + return result; + } + + private static string Canonicalize(string value) + { + return value + .Trim() + .Replace("-", "_") + .Replace(" ", string.Empty) + .ToUpperInvariant(); + } +} diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index ff111c1..da4a682 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -26,6 +26,9 @@ PreserveNewest + + PreserveNewest + diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 1f2d434..5991e8d 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1674,7 +1674,24 @@ private Dictionary TryLoadAllLearnedTensorMappings( rows = rows.Where(x => x.TensorWeightSchemeId == dominantSchemeId).ToList(); } - return rows.ToDictionary(x => x.TensorName, x => x.FinalQuantType, StringComparer.Ordinal); + var result = new Dictionary(StringComparer.Ordinal); + + foreach (var row in rows) + { + var appliedSchemeName = + NativePrecisionNormalization.NormalizeLearnedFinalQuantTypeForApplication(row.FinalQuantType); + + if (string.IsNullOrWhiteSpace(appliedSchemeName)) + { + throw new InvalidOperationException( + $"Learned tensor mapping for tensor '{row.TensorName}' on baseline key '{canonicalBaselineKey}' " + + $"returned an empty normalized scheme name. Observed FinalQuantType='{row.FinalQuantType}'."); + } + + result[row.TensorName] = appliedSchemeName; + } + + return result; } private Dictionary TryLoadLearnedTensorMapping( @@ -1739,7 +1756,24 @@ private Dictionary TryLoadLearnedTensorMapping( rows = rows.Where(x => x.TensorWeightSchemeId == dominantSchemeId).ToList(); } - return rows.ToDictionary(x => x.TensorName, x => x.FinalQuantType, StringComparer.Ordinal); + var result = new Dictionary(StringComparer.Ordinal); + + foreach (var row in rows) + { + var appliedSchemeName = + NativePrecisionNormalization.NormalizeLearnedFinalQuantTypeForApplication(row.FinalQuantType); + + if (string.IsNullOrWhiteSpace(appliedSchemeName)) + { + throw new InvalidOperationException( + $"Learned tensor mapping for tensor '{row.TensorName}' in group '{targetGroup.Name}' " + + $"returned an empty normalized scheme name. Observed FinalQuantType='{row.FinalQuantType}'."); + } + + result[row.TensorName] = appliedSchemeName; + } + + return result; } diff --git a/MagicQuant/config.dev.backup.yaml b/MagicQuant/config.dev.backup.yaml new file mode 100644 index 0000000..8be7a40 --- /dev/null +++ b/MagicQuant/config.dev.backup.yaml @@ -0,0 +1,50 @@ +# Dev config. This is auto-selected in DEBUG when --config is not supplied. + +paths: + magic_quant_root: + model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ + llama_root: + llama_bin: + convert_script: + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_relearn_baseline_tensor_mappings: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1m.jsonl + +evolution: + max_data_collected_per_category: 5 + max_survival_rounds: 4 + collapse_multiplier: 1.5 + brute_force_final_combination_threshold: 2000 + +isolation_pruning: + minimum_isolation_reduction_to_continue_ratio: 0.04 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + +prediction: + manual_max_predicted_size_bytes: 0 + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + custom_repositories: [] diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 8be7a40..9ed1432 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,4 +1,16 @@ -# Dev config. This is auto-selected in DEBUG when --config is not supplied. +# Dev config. Auto-selected in DEBUG when --config is not supplied. +# CLI flags should override values here when both are present. +# +# IMPORTANT: +# - standard_baselines_mode: all +# Keeps built-in llama.cpp learning baselines + allows custom repository baselines too. +# - custom repositories below are additional learned baseline sources. +# - include entries are explicit and safest because file naming can vary by repo. +# - attached_to_baseline tells MagicQuant which internal learned baseline identity this file should behave as. +# - display_name is just a friendly name for logs / DB / troubleshooting. +# - quantize_base_argument_name should normally be the internal baseline family name you want it associated with. +# - short_source_name is what can show up in naming/logic as the short source marker. +# - source_kind is just a source label so this is clearly not a normal llama.cpp-built baseline. paths: magic_quant_root: @@ -40,6 +52,7 @@ isolation_pruning: minimum_meaningful_base_only_reduction_ratio: 0.01 prediction: + # 0 = automatic current logic based on learned/default threshold behavior. manual_max_predicted_size_bytes: 0 baselines: @@ -47,4 +60,54 @@ baselines: enabled_standard_learning_baselines: [] enabled_standard_combination_carriers: [] enabled_standard_explicit_group_candidates: [] - custom_repositories: [] + + custom_repositories: + - repo_id: unsloth/Qwen3-4B-Instruct-2507-GGUF + enabled: true + short_source_name: Unsloth + source_kind: huggingface_gguf_repository + require_all_includes_to_resolve: true + validate_tensor_names_against_source_model: true + delete_partial_or_dirty_downloads: true + resume_or_retry_downloads: true + + include: + - file_name: Qwen3-4B-Instruct-2507-UD-Q4_K_XL.gguf + attached_to_baseline: Q4_K_M + quantize_base_argument_name: Q4_K_M + display_name: Unsloth_Q4_K_XL + enabled_for_learning: true + enabled_for_combination_carrier: true + enabled_for_explicit_group_candidate: true + + - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf + attached_to_baseline: Q5_K + quantize_base_argument_name: Q5_K + display_name: Unsloth_Q5_K_XL + enabled_for_learning: true + enabled_for_combination_carrier: true + enabled_for_explicit_group_candidate: true + + - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf + attached_to_baseline: Q6_K + quantize_base_argument_name: Q6_K + display_name: Unsloth_Q6_K_XL + enabled_for_learning: true + enabled_for_combination_carrier: true + enabled_for_explicit_group_candidate: true + + - file_name: Qwen3-4B-Instruct-2507-UD-Q3_K_XL.gguf + attached_to_baseline: IQ3_S + quantize_base_argument_name: IQ3_S + display_name: Unsloth_Q3_K_XL + enabled_for_learning: true + enabled_for_combination_carrier: false + enabled_for_explicit_group_candidate: true + + - file_name: Qwen3-4B-Instruct-2507-UD-IQ3_XS.gguf + attached_to_baseline: IQ3_XS + quantize_base_argument_name: IQ3_XS + display_name: Unsloth_IQ3_XS + enabled_for_learning: true + enabled_for_combination_carrier: false + enabled_for_explicit_group_candidate: true \ No newline at end of file From 91b2e2daedd89ceef70f2aae133319c9e9319424 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 22 Apr 2026 12:59:34 -0400 Subject: [PATCH 110/258] Getting further, may be working but baseline quant definitions is causing issues. --- MagicQuant/Commands/Evolution.cs | 100 +++--- .../Configuration/MagicQuantYamlLoader.cs | 2 + .../Services/HuggingFaceBaselineService.cs | 105 ++++--- .../Services/IsolationOptimizationService.cs | 82 +---- .../Services/LearnedBaselinePruningService.cs | 269 +--------------- MagicQuant/Services/QuantizationService.cs | 294 ++++++++++++++++-- MagicQuant/config.default.yaml | 187 ++++++++++- MagicQuant/config.dev.yaml | 75 ++--- 8 files changed, 603 insertions(+), 511 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 137837d..44baebe 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -87,7 +87,11 @@ public async Task Run(List args) var pyManager = new PythonManager(Cache.MagicQuantDirectory!); var customBaselineService = new HuggingFaceBaselineService(pyManager); -await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); +var resolvedCustomBaselines = await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); +if (Config.Current.Baselines.CustomRepositories.Any(x => x.Enabled) && resolvedCustomBaselines.Count == 0) +{ + throw new InvalidOperationException("Custom baseline repositories were enabled, but no custom baselines resolved into the runtime registry."); +} await EnsureSqliteReadyAsync(); var benchmarkService = new BenchmarkService(pyManager); @@ -194,47 +198,17 @@ await benchmarkService.RunAllBenchmarksAsync( RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; + PrintCustomBaselineRuntimeSummary(resolvedCustomBaselines, imatrixEnsureResult.Enabled); + CliHelpers.ValidateCombinationLogicWorks(true); var dbService = new QuantDatabaseService(); await dbService.InitializeAsync(); var comboCountBefore = ComboCounter.CountAll(); - var learnedBaselinePruner = new LearnedBaselinePruningService(); - var totalLearnedPruningResult = new LearnedBaselinePruningResult(); - if (!Cache.ForceRelearnBaselineTensorMappings) - { - var coverageStatus = await learnedBaselinePruner.GetCoverageStatusAsync(); - if (coverageStatus.SafeToApplyBeforeStartup) - { - AnsiConsole.Write(new Rule("[yellow]Pre-Startup Learned Baseline Pruning[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine( - $"[grey]Using existing learned baseline coverage before startup sampling:[/] [cyan]{coverageStatus.PresentCandidateGroupPairs:N0}[/]/[cyan]{coverageStatus.ExpectedCandidateGroupPairs:N0}[/] candidate-group pairs."); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Pre-Startup Learned-Baseline Pruning"); - var preStartupLearnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); - MergeLearnedPruningResults(totalLearnedPruningResult, preStartupLearnedPruningResult); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Pre-Startup Learned-Baseline Pruning"); - - foreach (var note in preStartupLearnedPruningResult.Notes) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); - } - else if (coverageStatus.HasAnyLearnedRows) - { - AnsiConsole.MarkupLine( - $"[grey]Skipping pre-startup learned pruning because learned coverage is incomplete for the current explicit candidate universe ({coverageStatus.PresentCandidateGroupPairs:N0}/{coverageStatus.ExpectedCandidateGroupPairs:N0} candidate-group pairs present).[/]"); - } - else - { - AnsiConsole.MarkupLine("[grey]Skipping pre-startup learned pruning because no learned baseline rows exist yet for this model.[/]"); - } - } - else - { - AnsiConsole.MarkupLine("[grey]Skipping pre-startup learned pruning because --relearn-baseline-mappings was requested.[/]"); - } + AnsiConsole.MarkupLine("[grey]Learned-baseline early pruning is disabled for this build. Startup sampling will proceed without learned-scheme candidate elimination.[/]"); AnsiConsole.Write(new Rule("[yellow]Initial Isolation Startup Samples[/]") { Justification = Justify.Left }); @@ -249,17 +223,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {initialSummary.Skipped:N0}"); AnsiConsole.MarkupLine($" [red]Failed:[/] {initialSummary.Failed:N0}"); - AnsiConsole.MarkupLine("[bold magenta]Evolution flow marker:[/] startup sampling finished, refreshing learned-baseline pruning before initial probe analysis."); - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Learned-Baseline Pruning Refresh"); - - AnsiConsole.Write(new Rule("[yellow]Learned Baseline Pruning Refresh[/]") { Justification = Justify.Left }); - var learnedPruningResult = await learnedBaselinePruner.AnalyzeAndApplyAsync(); - MergeLearnedPruningResults(totalLearnedPruningResult, learnedPruningResult); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Learned-Baseline Pruning Refresh"); - - foreach (var note in learnedPruningResult.Notes) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + AnsiConsole.MarkupLine("[bold magenta]Evolution flow marker:[/] startup sampling finished. Learned-baseline early pruning remains disabled for subsequent phases."); var isolationOptimizer = new IsolationOptimizationService(); @@ -331,8 +295,8 @@ await benchmarkService.RunAllBenchmarksAsync( long predictedSizePruned = await dbService.PrunePredictedLargerThanQ8Async(mergedPlan); long highPrecisionPruned = await dbService.PruneHighPrecisionHybridCandidatesAsync(); - AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {totalLearnedPruningResult.GroupCandidateEliminations:N0}"); - AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {totalLearnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0}"); + AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {totalLearnedPruningResult.GroupCandidateEliminations:N0} [grey](early pruning disabled)[/]"); + AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {totalLearnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0} [grey](early pruning disabled)[/]"); AnsiConsole.MarkupLine($"[green]Groups reduced to explicit-banned->Q8-fallback:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); @@ -385,15 +349,6 @@ await benchmarkService.RunAllBenchmarksAsync( } } - private static void MergeLearnedPruningResults(LearnedBaselinePruningResult target, LearnedBaselinePruningResult source) - { - target.GroupCandidateEliminations += source.GroupCandidateEliminations; - target.BaselinesSkippedWithoutLearnedRows += source.BaselinesSkippedWithoutLearnedRows; - - foreach (var note in source.Notes) - target.Notes.Add(note); - } - private static void PrintIsolationGroupDecisions(IEnumerable decisions) { foreach (var gd in decisions.OrderBy(x => x.GroupName)) @@ -414,6 +369,39 @@ private static void PrintIsolationGroupDecisions(IEnumerable resolvedCustomBaselines, + bool hasUsableImatrix) + { + AnsiConsole.Write(new Rule("[yellow]Custom Baseline Runtime Summary[/]") { Justification = Justify.Left }); + + var learning = BaselineQuants.GetLearningBaselines(hasUsableImatrix); + var carriers = BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix); + var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix, Config.Current.Flags.AllowHighPrecisionHybrids); + + AnsiConsole.MarkupLine($"[grey]Learning baselines in runtime registry:[/] [cyan]{learning.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Combination carriers in runtime registry:[/] [cyan]{carriers.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Explicit group candidates in runtime registry:[/] [cyan]{explicitCandidates.Count:N0}[/]"); + + if (resolvedCustomBaselines.Count == 0) + { + AnsiConsole.MarkupLine("[grey]No custom baselines were resolved for this run.[/]"); + return; + } + + AnsiConsole.MarkupLine($"[green]Custom baselines registered:[/] [cyan]{resolvedCustomBaselines.Count:N0}[/]"); + + foreach (var custom in resolvedCustomBaselines.OrderBy(x => x.DynamicBaselineId)) + { + bool inLearning = learning.Any(x => x.UniqueId == custom.DynamicBaselineId); + bool inCarriers = carriers.Any(x => x.UniqueId == custom.DynamicBaselineId); + bool inExplicit = explicitCandidates.Any(x => x.UniqueId == custom.DynamicBaselineId); + + AnsiConsole.MarkupLine($" [cyan]{custom.DynamicBaselineId}[/] [yellow]{Markup.Escape(custom.DisplayName)}[/] family={Markup.Escape(custom.BaselineFamily)} file={Markup.Escape(custom.SourceFileName)} learning={inLearning} carrier={inCarriers} explicit={inExplicit}"); + } + } + private void ShowEvolutionHelp() { AnsiConsole.MarkupLine("[bold yellow]Command: evolution[/]"); diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index daf744a..cd278fd 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Text.Json; using MagicQuant.Helpers; using MagicQuant.Models; using MQ.DB; @@ -30,6 +31,7 @@ public static MagicQuantYamlConfig LoadAndApply(string commandName, IReadOnlyLis var yaml = File.ReadAllText(configPath); var loaded = deserializer.Deserialize(yaml) ?? MagicQuantYamlConfig.CreateDefault(); + ApplyCliOverrides(loaded, args); NormalizeAndApply(loaded); diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index 4eb7955..f02eeb8 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -20,20 +20,40 @@ public async Task> PrecheckAndRegister { await EnsureHubSupportAsync(); + var enabledRepos = Config.Current.Baselines.CustomRepositories.Where(x => x.Enabled).ToList(); var resolved = new List(); BaselineQuants.ResetDynamicCustomBaselines(); + AnsiConsole.Write(new Rule("[yellow]Custom Baseline Precheck[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[grey]Enabled custom repositories:[/] [cyan]{enabledRepos.Count:N0}[/]"); + + if (enabledRepos.Count == 0) + { + AnsiConsole.MarkupLine("[grey]No enabled custom repositories were configured for this run.[/]"); + Config.SetResolvedCustomBaselines(Array.Empty()); + BaselineQuants.ValidateIntegrityOrThrow(); + return resolved; + } + byte nextId = BaselineQuants.GetFirstAvailableDynamicBaselineId(); - foreach (var repo in Config.Current.Baselines.CustomRepositories.Where(x => x.Enabled)) + foreach (var repo in enabledRepos) { if (string.IsNullOrWhiteSpace(repo.RepoId)) throw new InvalidOperationException("Custom baseline repository entry is missing repo_id."); + if (repo.Includes.Count == 0) + throw new InvalidOperationException($"Custom baseline repository '{repo.RepoId}' is enabled but has zero include entries."); + + AnsiConsole.MarkupLine($"[cyan]Repo:[/] {Markup.Escape(repo.RepoId)} [grey](includes={repo.Includes.Count})[/]"); + var repoFiles = await ListRepoFilesAsync(repo.RepoId, ct); if (repoFiles.Count == 0) throw new InvalidOperationException($"No files were returned from Hugging Face repo '{repo.RepoId}'."); + var ggufRepoFiles = repoFiles.Where(x => x.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)).ToList(); + AnsiConsole.MarkupLine($" [grey]GGUF files discovered:[/] [cyan]{ggufRepoFiles.Count:N0}[/]"); + string shortSourceName = string.IsNullOrWhiteSpace(repo.ShortSourceName) ? DeriveShortSourceName(repo.RepoId) : repo.ShortSourceName!.Trim(); @@ -86,41 +106,39 @@ public async Task> PrecheckAndRegister BaselineQuants.RegisterDynamicCustomBaseline(dynamicBaseline); - resolved.Add(new ResolvedCustomBaselineSpec + var spec = new ResolvedCustomBaselineSpec { - DynamicBaselineId = nextId, - CanonicalKey = canonicalKey, - DisplayName = displayName, + DynamicBaselineId = dynamicBaseline.UniqueId, + CanonicalKey = dynamicBaseline.CanonicalKey, + DisplayName = dynamicBaseline.Names[0], RepoId = repo.RepoId, - SourceOwner = DeriveSourceOwner(repo.RepoId), - SourceFileName = resolvedFileName, - ShortSourceName = shortSourceName, + SourceOwner = dynamicBaseline.SourceOwner ?? string.Empty, + SourceFileName = dynamicBaseline.SourceFileName ?? string.Empty, + ShortSourceName = dynamicBaseline.ShortSourceName ?? shortSourceName, BaselineFamily = standardFamily.Names[0], - QuantizeBaseName = quantizeBaseName, - RequiresImatrix = requiresImatrix, - AllowAsLearningBaseline = allowAsLearning, - AllowAsCombinationCarrier = allowAsCarrier, - AllowAsExplicitGroupCandidate = allowAsExplicit, - BannedGroupIds = bannedGroups - }); + QuantizeBaseName = dynamicBaseline.QuantizeBaseArgumentName, + RequiresImatrix = dynamicBaseline.RequiresImatrix, + AllowAsLearningBaseline = dynamicBaseline.IsLearningBaseline, + AllowAsCombinationCarrier = dynamicBaseline.IsCombinationCarrierCandidate, + AllowAsExplicitGroupCandidate = dynamicBaseline.IsExplicitGroupCombinationCandidate, + BannedGroupIds = dynamicBaseline.BannedGroupIds + }; + + resolved.Add(spec); + AnsiConsole.MarkupLine( + $" [green]Resolved:[/] id=[cyan]{dynamicBaseline.UniqueId}[/] family=[yellow]{Markup.Escape(standardFamily.Names[0])}[/] file=[blue]{Markup.Escape(resolvedFileName)}[/] learning={allowAsLearning} carrier={allowAsCarrier} explicit={allowAsExplicit}"); checked { nextId++; } } } + if (enabledRepos.Count > 0 && resolved.Count == 0) + throw new InvalidOperationException("Custom baseline repositories were enabled, but zero custom baselines resolved into the runtime registry. Check YAML property names and include entries."); + Config.SetResolvedCustomBaselines(resolved); BaselineQuants.ValidateIntegrityOrThrow(); - if (resolved.Count > 0) - { - AnsiConsole.MarkupLine($"[green]Resolved custom baselines:[/] {resolved.Count:N0}"); - foreach (var item in resolved) - { - AnsiConsole.MarkupLine( - $" [grey]- {Markup.Escape(item.DisplayName)}[/] => [cyan]{Markup.Escape(item.RepoId)}[/] / [yellow]{Markup.Escape(item.SourceFileName)}[/]"); - } - } - + AnsiConsole.MarkupLine($"[green]Custom baseline precheck complete:[/] [cyan]{resolved.Count:N0}[/] resolved custom baseline(s)."); return resolved; } @@ -136,9 +154,18 @@ public async Task DownloadBaselineAsync(BaselineQuants baseline, string Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); + if (File.Exists(destinationPath) && new FileInfo(destinationPath).Length == 0) + File.Delete(destinationPath); + if (forceRedownload && File.Exists(destinationPath)) File.Delete(destinationPath); + if (File.Exists(destinationPath) && new FileInfo(destinationPath).Length > 0) + { + AnsiConsole.MarkupLine($"[grey]Reusing cached external baseline:[/] {Markup.Escape(destinationPath)}"); + return destinationPath; + } + string payloadPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_{Guid.NewGuid():N}.json"); string scriptPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_{Guid.NewGuid():N}.py"); string resultPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_result_{Guid.NewGuid():N}.json"); @@ -178,12 +205,20 @@ with open(payload_path, 'r', encoding='utf-8') as f: ) if os.path.abspath(downloaded) != os.path.abspath(target_path): + if os.path.exists(target_path): + os.remove(target_path) shutil.copy2(downloaded, target_path) - size = os.path.getsize(target_path) - result = {'success': True, 'path': target_path, 'size': size} + result = { + 'ok': True, + 'downloaded_path': target_path, + 'size_bytes': os.path.getsize(target_path) if os.path.exists(target_path) else 0, + } except Exception as ex: - result = {'success': False, 'error': str(ex)} + result = { + 'ok': False, + 'error': str(ex), + } with open(result_path, 'w', encoding='utf-8') as f: json.dump(result, f) @@ -192,16 +227,14 @@ with open(result_path, 'w', encoding='utf-8') as f: await File.WriteAllTextAsync(scriptPath, py, ct); await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); - using var doc = JsonDocument.Parse(await File.ReadAllTextAsync(resultPath, ct)); - if (!doc.RootElement.TryGetProperty("success", out var successProp) || !successProp.GetBoolean()) - { - string error = doc.RootElement.TryGetProperty("error", out var errProp) ? errProp.GetString() ?? "unknown error" : "unknown error"; - throw new InvalidOperationException($"Failed downloading external baseline '{baseline.Names[0]}': {error}"); - } + var json = JsonDocument.Parse(await File.ReadAllTextAsync(resultPath, ct)).RootElement; + if (!json.GetProperty("ok").GetBoolean()) + throw new InvalidOperationException($"External baseline download failed: {json.GetProperty("error").GetString()}"); if (!File.Exists(destinationPath) || new FileInfo(destinationPath).Length == 0) - throw new InvalidOperationException($"External baseline download reported success but no valid file exists at '{destinationPath}'."); + throw new InvalidOperationException($"External baseline download completed but produced no file: {destinationPath}"); + AnsiConsole.MarkupLine($"[green]Downloaded external baseline:[/] {Markup.Escape(destinationPath)}"); return destinationPath; } finally @@ -358,4 +391,4 @@ private static void TryDelete(string path) { } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 463bef1..ec55168 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -137,8 +137,7 @@ public async Task AnalyzeInitialIsolationProbesA $"Suppressed BF16 explicit candidate for '{group.Name}' because smallest baseline-candidate probe already saved {reduction:P2}."); } - await ApplyEarlyCandidatePruningForContinuingGroupAsync(group, decision, ct); - AppendLearnedPrunedCandidates(group, decision); + result.Notes.Add($"Early learned-scheme continuation pruning is disabled for '{group.Name}'. All compatible candidates remain available for later pipeline stages."); result.GroupDetails.Add(decision); } @@ -146,85 +145,6 @@ public async Task AnalyzeInitialIsolationProbesA return result; } - private static async Task ApplyEarlyCandidatePruningForContinuingGroupAsync( - TensorGroup group, - IsolationGroupDecision decision, - CancellationToken ct) - { - if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) - return; - - await using var db = new MagicQuantContext(); - - var aiModelHashId = await db.AiModelHashes - .AsNoTracking() - .Where(x => x.UniqueHash == Cache.CurrentModelId) - .Select(x => (uint?)x.Id) - .FirstOrDefaultAsync(ct); - - if (aiModelHashId == null) - return; - - var learnedRows = await db.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == aiModelHashId.Value && x.TensorGroupId == group.UniqueId) - .Select(x => new LearnedBaselinePruningService.LearnedRow( - x.BaselineQuantId, - x.TensorWeightSchemeId, - x.TensorGroupId, - x.FinalQuantType)) - .ToListAsync(ct); - - if (learnedRows.Count == 0) - return; - - var aliasToSchemeIds = LearnedBaselinePruningService.BuildAliasToSchemeIds(); - var effectiveSchemesByCandidateAndGroup = LearnedBaselinePruningService.BuildEffectiveSchemesByBaselineAndGroup( - learnedRows, - aliasToSchemeIds); - - var candidates = BaselineQuants.GetGroupCombinationCandidatesSmallestFirst( - RuntimeSearchSpace.HasUsableImatrix(), - allowHighPrecisionHybrids: false) - .Where(x => !x.BannedGroupIds.Contains(group.UniqueId)) - .ToList(); - - foreach (var candidate in candidates) - { - if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) - continue; - - var expectedIds = candidate.LearnedMatchTensorWeightSchemes - .Select(x => x.UniqueId) - .Distinct() - .OrderBy(x => x) - .ToList(); - - if (expectedIds.Count == 0) - continue; - - var key = (candidate.UniqueId, group.UniqueId); - effectiveSchemesByCandidateAndGroup.TryGetValue(key, out var effectiveIdsSet); - effectiveIdsSet ??= new HashSet(); - - var matchedIds = expectedIds - .Where(effectiveIdsSet.Contains) - .OrderBy(x => x) - .ToList(); - - if (matchedIds.Count > 0) - continue; - - RuntimeSearchSpace.BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( - group, - candidate, - expectedTensorWeightSchemeIds: expectedIds, - matchedTensorWeightSchemeIds: matchedIds, - note: "Early continuation gate removed candidate because learned tensor schemes for this group do not match the candidate family."); - } - } - - public async Task AnalyzeAndApplyFinalAsync( RequiredSampleGenerationResult fullPlan, IsolationOptimizationOptions? options = null, diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index 5a11f51..0e950f2 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -1,13 +1,6 @@ -using System; using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; -using MagicQuant.Helpers; -using Microsoft.EntityFrameworkCore; -using MQ.DB; -using MQ.DB.Data; -using MQ.DB.Models; namespace MagicQuant.Services; @@ -18,7 +11,6 @@ public sealed class LearnedBaselinePruningResult public List Notes { get; } = new(); } - public sealed class LearnedBaselineCoverageStatus { public bool HasAnyLearnedRows { get; set; } @@ -30,257 +22,24 @@ public sealed class LearnedBaselineCoverageStatus public sealed class LearnedBaselinePruningService { - internal readonly record struct LearnedRow( - byte BaselineQuantId, - byte TensorWeightSchemeId, - byte TensorGroupId, - string FinalQuantType); - - - public async Task GetCoverageStatusAsync(CancellationToken ct = default) + public Task GetCoverageStatusAsync(CancellationToken ct = default) { - if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) - throw new InvalidOperationException("Cache.CurrentModelId is not set."); - - var status = new LearnedBaselineCoverageStatus(); - - await using var db = new MagicQuantContext(); - - var aiModelHash = await db.AiModelHashes - .AsNoTracking() - .Where(x => x.UniqueHash == Cache.CurrentModelId) - .Select(x => new { x.Id }) - .FirstOrDefaultAsync(ct); - - if (aiModelHash == null) - return status; - - var presentPairs = await db.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == aiModelHash.Id) - .Select(x => new { x.BaselineQuantId, x.TensorGroupId }) - .Distinct() - .ToListAsync(ct); - - if (presentPairs.Count == 0) - return status; - - status.HasAnyLearnedRows = true; - - var present = presentPairs - .Select(x => (x.BaselineQuantId, x.TensorGroupId)) - .ToHashSet(); - - var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); - var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates( - RuntimeSearchSpace.HasUsableImatrix(), - allowHighPrecisionHybrids: false) - .OrderBy(x => x.ExplicitCandidateSortOrder) - .ThenBy(x => x.UniqueId) - .ToList(); - - foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) + var status = new LearnedBaselineCoverageStatus { - if (unusedIds.Contains(group.UniqueId)) - continue; - - foreach (var candidate in explicitCandidates) - { - if (candidate.BannedGroupIds.Contains(group.UniqueId)) - continue; - - status.ExpectedCandidateGroupPairs++; - - if (present.Contains((candidate.UniqueId, group.UniqueId))) - { - status.PresentCandidateGroupPairs++; - continue; - } - - status.MissingPairs.Add($"{group.Name}:{candidate.Names[0]}"); - } - } - - status.SafeToApplyBeforeStartup = status.MissingPairs.Count == 0; - return status; + HasAnyLearnedRows = false, + SafeToApplyBeforeStartup = false, + ExpectedCandidateGroupPairs = 0, + PresentCandidateGroupPairs = 0 + }; + + status.MissingPairs.Add("Learned-baseline early pruning is disabled."); + return Task.FromResult(status); } - public async Task AnalyzeAndApplyAsync(CancellationToken ct = default) + public Task AnalyzeAndApplyAsync(CancellationToken ct = default) { - if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) - throw new InvalidOperationException("Cache.CurrentModelId is not set."); - - RuntimeSearchSpace.ClearLearnedBaselinePruneBookkeeping(); - var result = new LearnedBaselinePruningResult(); - - await using var db = new MagicQuantContext(); - - var aiModelHash = await db.AiModelHashes - .AsNoTracking() - .Where(x => x.UniqueHash == Cache.CurrentModelId) - .Select(x => new { x.Id, x.UniqueHash }) - .FirstOrDefaultAsync(ct); - - if (aiModelHash == null) - throw new InvalidOperationException( - $"AiModelHash row was not found for current model id '{Cache.CurrentModelId}'."); - - result.Notes.Add( - $"Learned-baseline pruning model resolution: Cache.CurrentModelId={Cache.CurrentModelId}, " + - $"AiModelHash.Id={aiModelHash.Id}, AiModelHash.UniqueHash={aiModelHash.UniqueHash}"); - - var learnedRows = await db.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == aiModelHash.Id) - .Select(x => new LearnedRow( - x.BaselineQuantId, - x.TensorWeightSchemeId, - x.TensorGroupId, - x.FinalQuantType)) - .ToListAsync(ct); - - if (learnedRows.Count == 0) - { - result.Notes.Add( - "Learned-baseline pruning skipped because no LearnedBaselineTensorQuants rows existed for the current model."); - return result; - } - - var unusedIds = Cache.UnusedTensorGroups - .Select(x => x.UniqueId) - .ToHashSet(); - - ApplyLearnedBaselinePruning(learnedRows, aiModelHash.Id, aiModelHash.UniqueHash, unusedIds, result); - return result; - } - - internal static void ApplyLearnedBaselinePruning( - IReadOnlyList learnedRows, - uint aiModelHashId, - string aiModelHashUniqueHash, - HashSet unusedGroupIds, - LearnedBaselinePruningResult result) - { - RuntimeSearchSpace.ClearLearnedBaselinePruneBookkeeping(); - - var aliasToSchemeIds = BuildAliasToSchemeIds(); - var effectiveSchemesByCandidateAndGroup = BuildEffectiveSchemesByBaselineAndGroup(learnedRows, aliasToSchemeIds); - - var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) - .OrderBy(x => x.UniqueId) - .ToList(); - - foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) - { - if (unusedGroupIds.Contains(group.UniqueId)) - continue; - - foreach (var candidate in explicitCandidates) - { - if (candidate.BannedGroupIds.Contains(group.UniqueId)) - continue; - - if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) - continue; - - var key = (candidate.UniqueId, group.UniqueId); - bool hasEffectiveSet = effectiveSchemesByCandidateAndGroup.TryGetValue(key, out var effectiveForGroup); - var expectedIds = candidate.LearnedMatchTensorWeightSchemes.Select(x => x.UniqueId).Distinct().OrderBy(x => x).ToList(); - var effectiveIdsSet = hasEffectiveSet ? effectiveForGroup! : new HashSet(); - var matchedIds = expectedIds.Where(effectiveIdsSet.Contains).OrderBy(x => x).ToList(); - bool allow = matchedIds.Count > 0; - string effectiveIds = hasEffectiveSet - ? string.Join(",", effectiveIdsSet.OrderBy(x => x)) - : ""; - string expected = string.Join(",", expectedIds); - string matched = matchedIds.Count > 0 ? string.Join(",", matchedIds) : ""; - - result.Notes.Add( - $"Learned-prune check: model={aiModelHashId}/{aiModelHashUniqueHash}, group={group.Name}, " + - $"candidate={candidate.Names[0]}, expected=[{expected}], effective=[{effectiveIds}], matched=[{matched}], decision={(allow ? "ALLOW" : "BAN")}"); - - if (!allow) - { - RuntimeSearchSpace.BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( - group, - candidate, - expectedTensorWeightSchemeIds: expectedIds, - matchedTensorWeightSchemeIds: matchedIds, - note: "No matching learned tensor-weight schemes for candidate/group."); - result.GroupCandidateEliminations++; - } - } - } - } - - internal static Dictionary<(byte BaselineId, byte GroupId), HashSet> BuildEffectiveSchemesByBaselineAndGroup( - IReadOnlyList learnedRows, - Dictionary> aliasToSchemeIds) - { - var effectiveSchemesByBaselineAndGroup = new Dictionary<(byte BaselineId, byte GroupId), HashSet>(); - - foreach (var row in learnedRows) - { - var key = (row.BaselineQuantId, row.TensorGroupId); - - if (!effectiveSchemesByBaselineAndGroup.TryGetValue(key, out var set)) - { - set = new HashSet(); - effectiveSchemesByBaselineAndGroup[key] = set; - } - - // The persisted TensorWeightSchemeId is the authoritative learned-family identity. - // FinalQuantType is useful extra metadata, but it cannot replace the stored scheme id - // because some learned baselines materialize tensors whose final emitted token differs - // from the baseline family we are learning from. - set.Add(row.TensorWeightSchemeId); - - if (aliasToSchemeIds.TryGetValue(CanonicalizeQuantToken(row.FinalQuantType), out var resolvedIds)) - { - foreach (var resolvedId in resolvedIds) - set.Add(resolvedId); - } - } - - return effectiveSchemesByBaselineAndGroup; - } - - internal static Dictionary> BuildAliasToSchemeIds() - { - var map = new Dictionary>(StringComparer.Ordinal); - - foreach (var scheme in TensorWeightScheme.All) - { - if (scheme.Names.IsDefaultOrEmpty) - continue; - - foreach (var alias in scheme.Names) - { - var token = CanonicalizeQuantToken(alias); - - if (!map.TryGetValue(token, out var ids)) - { - ids = new HashSet(); - map[token] = ids; - } - - ids.Add(scheme.UniqueId); - } - } - - return map; - } - - private static string CanonicalizeQuantToken(string value) - { - if (string.IsNullOrWhiteSpace(value)) - return "UNKNOWN"; - - return value - .Trim() - .Replace("-", "_") - .Replace(" ", string.Empty) - .ToUpperInvariant(); + result.Notes.Add("Learned-baseline early pruning is disabled. No candidates were removed from the search space."); + return Task.FromResult(result); } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 5991e8d..439c0dc 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -321,9 +321,12 @@ public async Task ProcessHybridQuantAsync( var stopwatch = Stopwatch.StartNew(); var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); bool pureExternalBaseline = ShouldDownloadExternalBaselineInsteadOfQuantizing(quant); + bool baselineLearnedTruthExists = !forceBaselineRelearn && await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); string benchmarkModelPath = quantPath; + PreparedExternalBaselineBuild? preparedExternalBaseline = null; + string? transientExternalDownloadPath = null; - if (!forceBaselineRelearn && await _benchmarker.TryReuseExistingBenchmarksAsync( + if (!forceBaselineRelearn && baselineLearnedTruthExists && await _benchmarker.TryReuseExistingBenchmarksAsync( quantConfig: quant, modelPath: quantPath, benchDir: modelBenchDir, @@ -338,7 +341,7 @@ public async Task ProcessHybridQuantAsync( return SampleProcessState.Skipped; } - if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) + if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) { AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); @@ -351,6 +354,9 @@ public async Task ProcessHybridQuantAsync( try { string inputPath = await GetEffectiveInputModelPathAsync(quant, forceBaselineRelearn, ct); + if (pureExternalBaseline) + transientExternalDownloadPath = inputPath; + QuantizationExecutionReport? quantizationReport = null; await _cpuQuantLock.WaitAsync(ct); @@ -358,7 +364,14 @@ public async Task ProcessHybridQuantAsync( { if (pureExternalBaseline) { - benchmarkModelPath = inputPath; + preparedExternalBaseline = await PrepareExternalBaselineRebuildAsync( + quant, + downloadedExternalBaselinePath: inputPath, + rebuiltOutputPath: quantPath, + forceBaselineRelearn: forceBaselineRelearn, + ct: ct); + + benchmarkModelPath = preparedExternalBaseline.BenchmarkModelPath; } else { @@ -375,7 +388,7 @@ public async Task ProcessHybridQuantAsync( _cpuQuantLock.Release(); } - if (!forceBaselineRelearn && await BenchmarkExistsAsync(quant, ct)) + if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) { if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); @@ -406,7 +419,12 @@ await PersistQuantizationRunAsync( ct: ct); if (IsLearnableBaselineRun(quant)) - await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); + { + if (preparedExternalBaseline?.HasPreparedLearningTruth == true) + await PersistLearnedBaselineTensorMapFromPreparedAsync(quant, preparedExternalBaseline, ct); + else if (!baselineLearnedTruthExists || forceBaselineRelearn) + await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); + } return SampleProcessState.Completed; } @@ -433,6 +451,9 @@ await PersistQuantizationRunAsync( } finally { + if (pureExternalBaseline && !string.IsNullOrWhiteSpace(transientExternalDownloadPath)) + await CleanupExternalBaselineDownloadArtifactsAsync(transientExternalDownloadPath); + if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); } @@ -483,6 +504,204 @@ private async Task ValidateExternalBaselineTensorParityOrThrow(string baseModelP } } +private async Task HasLearnedTruthForBaselineAsync(BaselineQuants baseline, CancellationToken ct = default) +{ + if (baseline.UniqueId == BaselineQuants.NativeSourceUniqueId) + return await HasNativeSourceLearnedTruthAsync(ct); + + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + return false; + + await using var db = new MagicQuantContext(); + var model = await db.AiModelHashes + .AsNoTracking() + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (model == null) + return false; + + var query = db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == model.Id) + .Where(x => x.BaselineCanonicalKey == baseline.CanonicalKey); + + if (baseline.DefaultTensorScheme != null) + query = query.Where(x => x.TensorWeightSchemeId == baseline.DefaultTensorScheme.UniqueId); + + return await query.AnyAsync(ct); +} + +private async Task PrepareExternalBaselineRebuildAsync( + HybridQuant quant, + string downloadedExternalBaselinePath, + string rebuiltOutputPath, + bool forceBaselineRelearn, + CancellationToken ct) +{ + if (!quant.BaseQuant.IsExternalRepositoryBaseline) + throw new InvalidOperationException("PrepareExternalBaselineRebuildAsync was called for a non-external baseline."); + + string nativeBasePath = await EnsureBaseModelFileAsync(); + bool canReuseLearnedTruth = !forceBaselineRelearn && await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); + + if (canReuseLearnedTruth) + { + var blanket = TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: false); + + if (blanket.Count == 0) + throw new InvalidOperationException($"Custom baseline '{quant.BaseQuant.Names[0]}' was marked as already learned, but no blanket learned tensor mapping could be loaded."); + + if (!File.Exists(rebuiltOutputPath) || forceBaselineRelearn) + { + AnsiConsole.MarkupLine($"[cyan]Rebuilding normalized custom baseline from learned truth:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); + await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, blanket); + } + + return new PreparedExternalBaselineBuild + { + BenchmarkModelPath = rebuiltOutputPath, + DownloadedExternalModelPath = downloadedExternalBaselinePath, + HasPreparedLearningTruth = false + }; + } + + AnsiConsole.MarkupLine($"[cyan]Learning external baseline truth from downloaded artifact:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); + await ValidateExternalBaselineTensorParityOrThrow(nativeBasePath, downloadedExternalBaselinePath); + + var ggufMetadata = await ReadTensorMetadataFromGgufAsync(downloadedExternalBaselinePath, rebuiltOutputPath + ".learn"); + var ggufTruth = ggufMetadata.TensorTypes + .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); + + if (ggufTruth.Count == 0) + throw new InvalidOperationException($"Downloaded external baseline '{quant.BaseQuant.Names[0]}' produced no readable GGUF tensor truth."); + + var truth = ggufTruth + .OrderBy(x => x.Key, StringComparer.Ordinal) + .ToDictionary( + x => x.Key, + x => new LearnedTensorTruth(x.Key, x.Value, LearningSource.GgufOnly), + StringComparer.Ordinal); + + var grouped = AssignGroups(truth.Keys); + var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); + var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).OrderBy(x => x, StringComparer.Ordinal).ToList(); + + var normalizedOverrides = truth.ToDictionary( + x => x.Key, + x => NativePrecisionNormalization.NormalizeLearnedFinalQuantTypeForApplication(x.Value.FinalQuantType), + StringComparer.Ordinal); + + if (normalizedOverrides.Values.Any(string.IsNullOrWhiteSpace)) + throw new InvalidOperationException($"External baseline '{quant.BaseQuant.Names[0]}' produced one or more empty normalized tensor scheme names."); + + AnsiConsole.MarkupLine($"[cyan]Rebuilding normalized benchmark artifact for custom baseline:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); + await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, normalizedOverrides); + + return new PreparedExternalBaselineBuild + { + BenchmarkModelPath = rebuiltOutputPath, + DownloadedExternalModelPath = downloadedExternalBaselinePath, + TruthByTensor = truth, + GroupedByTensor = grouped, + AllTensorNamesInDownloadedArtifact = ggufMetadata.TensorNames, + AmbiguousGroupingRows = ambiguous, + UnresolvedTensorNames = unresolved, + HasPreparedLearningTruth = true + }; +} + +private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( + HybridQuant quant, + PreparedExternalBaselineBuild prepared, + CancellationToken ct) +{ + if (!IsLearnableBaselineRun(quant) || !prepared.HasPreparedLearningTruth || prepared.TruthByTensor == null || prepared.GroupedByTensor == null) + return; + + var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; + + await using var db = new MagicQuantContext(); + + var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + if (model == null) + throw new InvalidOperationException("Unable to persist learned mappings because AiModelHash row was not found."); + + var combo = await db.TensorCombos + .AsNoTracking() + .FirstAsync(x => x.BaseQuant == quant.BaseQuant.UniqueId && + x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && + x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); + + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + + var benchmarkId = await db.AiBenchmarks + .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) + .OrderByDescending(x => x.Id) + .Select(x => (Guid?)x.Id) + .FirstOrDefaultAsync(ct); + + if (!benchmarkId.HasValue) + throw new InvalidOperationException($"Unable to persist learned mappings because no AiBenchmark exists for rebuilt baseline '{quant.BaseQuant.Names[0]}'."); + + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == model.Id && + x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && + x.TensorWeightSchemeId == tensorScheme.UniqueId) + .ExecuteDeleteAsync(ct); + + var rows = prepared.TruthByTensor + .OrderBy(x => x.Key, StringComparer.Ordinal) + .Select(kv => + { + var match = prepared.GroupedByTensor[kv.Key]; + + return new LearnedBaselineTensorQuant + { + Id = Guid.NewGuid(), + AiBenchmarkId = benchmarkId.Value, + AiModelHashId = model.Id, + BaselineQuantId = quant.BaseQuant.UniqueId, + TensorWeightSchemeId = tensorScheme.UniqueId, + TensorGroupId = match.PrimaryGroup?.UniqueId ?? UnknownTensorGroupId, + BaselineCanonicalKey = quant.BaseQuant.CanonicalKey, + BaselineSourceKind = quant.BaseQuant.SourceKind, + BaselineSourceRepository = quant.BaseQuant.SourceRepository, + BaselineSourceFileName = quant.BaseQuant.SourceFileName, + TensorName = kv.Key, + FinalQuantType = kv.Value.FinalQuantType + }; + }) + .ToList(); + + if (rows.Count == 0) + throw new InvalidOperationException($"Prepared learning truth for baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); + + db.LearnedBaselineTensorQuants.AddRange(rows); + await db.SaveChangesAsync(ct); + + await WriteLearningDiagnosticArtifactAsync( + baselineName: quant.BaseQuant.Names[0], + schemeName: tensorScheme.Names[0], + truthByTensor: prepared.TruthByTensor, + grouped: prepared.GroupedByTensor, + allTensorNamesInModel: prepared.AllTensorNamesInDownloadedArtifact ?? prepared.TruthByTensor.Keys.ToList(), + ambiguous: prepared.AmbiguousGroupingRows ?? new List>(), + unresolved: prepared.UnresolvedTensorNames ?? new List()); + + AnsiConsole.MarkupLine($"[green]Persisted rebuilt custom-baseline learning truth:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); +} + +private async Task CleanupExternalBaselineDownloadArtifactsAsync(string downloadedExternalBaselinePath) +{ + if (string.IsNullOrWhiteSpace(downloadedExternalBaselinePath)) + return; + + await HardDeleteHelper.DeleteFileIfExistsAsync(downloadedExternalBaselinePath); +} + // ---------------------------------------------------------------- // Benchmark/logit helpers // ---------------------------------------------------------------- @@ -833,7 +1052,7 @@ public async Task CleanupPureQ8ModelAsync() // Quantization // ---------------------------------------------------------------- - private async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant) + private async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant, IReadOnlyDictionary? temporaryCarrierOverrides = null) { if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); @@ -841,7 +1060,7 @@ private async Task RunLlamaQuantizeAsync(string inp Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, outputFile); - var requestedOverrides = BuildRequestedTensorOverrides(quant, inputTensorMetadata.TensorNames); + var requestedOverrides = BuildRequestedTensorOverrides(quant, inputTensorMetadata.TensorNames, temporaryCarrierOverrides); var concreteOverrides = ResolveConcreteTensorOverrides( allTensorNames: inputTensorMetadata.TensorNames, requestedOverrides: requestedOverrides); @@ -947,7 +1166,7 @@ public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = defaul { await ClearLearnedBaselineTensorMappingsAsync(ct); - foreach (var baseline in BaselineQuants.All) + foreach (var baseline in BaselineQuants.GetAllRecognizedBaselines()) { var pure = HybridQuant.CreatePureBaseline(baseline); var name = GenerateHybridName(pure); @@ -968,6 +1187,9 @@ public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = defaul if (Directory.Exists(debugDir)) Directory.Delete(debugDir, recursive: true); + if (!string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory) && Directory.Exists(Cache.ExternalBaselineCacheDirectory)) + Directory.Delete(Cache.ExternalBaselineCacheDirectory, recursive: true); + string nativeType = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; string nativeBaseFile = Path.Combine(_ggufDir, $"{modelName}-{nativeType}.gguf"); @@ -1512,28 +1734,31 @@ private static HashSet GetExpectedTensorNamesForGroup( private List BuildRequestedTensorOverrides( HybridQuant quant, - IReadOnlyCollection sourceTensorNames) + IReadOnlyCollection sourceTensorNames, + IReadOnlyDictionary? temporaryCarrierOverrides = null) { var result = new List(); - if (quant.Tensors == null || quant.Tensors.Count == 0) + bool hasTemporaryCarrierOverrides = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0; + bool hasExplicitGroupOverrides = quant.Tensors != null && quant.Tensors.Count > 0; + + if (!hasTemporaryCarrierOverrides && !hasExplicitGroupOverrides && !quant.BaseQuant.IsExternalRepositoryBaseline) return result; var baseScheme = TryResolveBaseTensorScheme(quant.BaseQuant); - if (quant.BaseQuant.IsExternalRepositoryBaseline) + if (quant.BaseQuant.IsExternalRepositoryBaseline || hasTemporaryCarrierOverrides) { - var blanket = TryLoadAllLearnedTensorMappings( - canonicalBaselineKey: quant.BaseQuant.CanonicalKey, - preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, - allowDominantFallback: false); - - if (blanket.Count == 0) - { + var blanket = hasTemporaryCarrierOverrides + ? new Dictionary(temporaryCarrierOverrides!, StringComparer.Ordinal) + : TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: false); + + if ((quant.BaseQuant.IsExternalRepositoryBaseline || hasTemporaryCarrierOverrides) && blanket.Count == 0) throw new InvalidOperationException( - $"Missing blanket learned mapping for custom carrier baseline '{quant.BaseQuant.Names[0]}'. " + - "Custom carrier baselines must be learned once before they can participate in hybrid quantization."); - } + $"Missing blanket learned mapping for custom carrier baseline '{quant.BaseQuant.Names[0]}'. Custom carrier baselines must be learned once before they can participate in hybrid quantization."); foreach (var kv in blanket.OrderBy(x => x.Key, StringComparer.Ordinal)) { @@ -1546,6 +1771,9 @@ private List BuildRequestedTensorOverrides( } } + if (!hasExplicitGroupOverrides) + return result; + foreach (var hybrid in quant.Tensors) { if (hybrid?.TGroup == null) @@ -1592,10 +1820,7 @@ private List BuildRequestedTensorOverrides( allowDominantFallback: false); if (learned.Count == 0) - { - throw new InvalidOperationException( - $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Run with --relearn-baseline-mappings to regenerate."); - } + throw new InvalidOperationException($"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Run with --relearn-baseline-mappings to regenerate."); var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); @@ -1605,8 +1830,7 @@ private List BuildRequestedTensorOverrides( { var missingText = missingExpected.Count == 0 ? "none" : string.Join(", ", missingExpected.Take(15)); var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); - throw new InvalidOperationException( - $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); + throw new InvalidOperationException($"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); } foreach (var kv in learned.OrderBy(x => x.Key, StringComparer.Ordinal)) @@ -1623,14 +1847,12 @@ private List BuildRequestedTensorOverrides( } default: - throw new InvalidOperationException( - $"Hybrid tensor for group '{hybrid.TGroup.Name}' has unsupported override mode '{hybrid.OverrideMode}'."); + throw new InvalidOperationException($"Hybrid tensor for group '{hybrid.TGroup.Name}' has unsupported override mode '{hybrid.OverrideMode}'."); } } return result; } - private Dictionary TryLoadAllLearnedTensorMappings( string canonicalBaselineKey, TensorWeightScheme? preferredSourceScheme = null, @@ -1940,6 +2162,18 @@ private static bool IsHighPrecisionType(string value) return normalized is "BF16" or "F16" or "F32"; } + private sealed class PreparedExternalBaselineBuild + { + public string BenchmarkModelPath { get; set; } = string.Empty; + public string? DownloadedExternalModelPath { get; set; } + public Dictionary? TruthByTensor { get; set; } + public Dictionary? GroupedByTensor { get; set; } + public IReadOnlyCollection? AllTensorNamesInDownloadedArtifact { get; set; } + public List>? AmbiguousGroupingRows { get; set; } + public List? UnresolvedTensorNames { get; set; } + public bool HasPreparedLearningTruth { get; set; } + } + private sealed class QuantizationExecutionReport { public string LogPath { get; set; } = string.Empty; diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index b4b979e..da2d60e 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -1,64 +1,231 @@ -# Production-friendly default runtime config. -# CLI flags still override anything here. +# ============================================================ +# MagicQuant - Default Production Configuration +# ============================================================ +# +# This file is intended to be the safe baseline config for production use. +# +# General rules: +# - CLI flags override values from this YAML. +# - In DEBUG, your dev config may be auto-selected instead. +# - Leave values blank when they must be provided per-machine or per-run. +# +# Notes: +# - "standard_baselines_mode: all" keeps the built-in baseline families active. +# - Custom repositories can add extra learned baseline sources, such as Unsloth. +# - External/custom baselines are LEARNED from, then rebuilt by MagicQuant +# under MagicQuant-controlled conditions before benchmarking. +# - The old early learned-baseline pruning logic is disabled in code now, +# so the isolation_pruning section mainly controls later tradeoff logic. +# ============================================================ paths: + # Root MagicQuant working directory. + # If blank, runtime may fall back to current working directory logic. magic_quant_root: + + # REQUIRED for real runs. + # This should point to the local source model folder containing safetensors. model_dir: + + # Optional if MagicQuant auto-discovers or bootstraps llama.cpp. llama_root: llama_bin: convert_script: + + # Folder name created under the MagicQuant root for external/custom GGUF downloads. external_baseline_cache_dir_name: ExternalBaselines flags: + # Whether MagicQuant should use an imatrix when supported and configured. use_imatrix: false + + # Force rebuilding the active imatrix artifact even if one already exists. force_imatrix_rebuild: false + + # Force relearning baseline tensor mappings even if learned truth already exists in SQLite. force_relearn_baseline_tensor_mappings: false + + # Force rerunning hardware execution-plan probing. force_refresh_hardware_probe: false + + # Whether exact high-precision hybrid aliases (BF16/F16-style explicit overrides) + # are allowed in hybrid generation logic. allow_high_precision_hybrids: false imatrix: + # Optional remote imatrix URL if your flow supports fetching one. imatrix_url: + + # Optional dataset repo for building an imatrix from a dataset source. dataset_repo: + + # Optional dataset split, for example: text, train, validation dataset_split: + + # Optional dataset config / subset name. dataset_config: + + # Optional local dataset file path for imatrix generation. + # Example: + # dataset_local_file: /data/datasets/imatrix-general-v1-1m.jsonl dataset_local_file: evolution: + # Maximum saved benchmark datapoints per category during survival/evolution stages. max_data_collected_per_category: 5 + + # Maximum survival rounds for evolution narrowing. max_survival_rounds: 4 + + # Collapse multiplier used in evolution logic. collapse_multiplier: 1.5 + + # If remaining combinations are <= this number, brute-force the end. brute_force_final_combination_threshold: 2000 isolation_pruning: + # NOTE: + # Early learned-baseline pruning has been removed from the code path. + # These values still matter for later isolation / bad-trade reasoning, + # not for the old "skip candidate because learned tensor usage looked redundant" path. + + # Minimum isolation reduction ratio required to continue considering the result meaningful. minimum_isolation_reduction_to_continue_ratio: 0.04 + + # Minimum reduction ratio before BF16 suppression logic is allowed to kick in. minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + + # Maximum allowed isolation PPL delta percent before considering the trade poor. maximum_isolation_ppl_delta_percent: 5.0 + + # Maximum allowed isolation KLD before considering the trade poor. maximum_isolation_kld: 0.1 + + # If size savings are below this percent, the trade may be treated as bad. bad_trade_max_size_delta_percent: 4.0 + + # Multiplier thresholds for bad-trade reasoning. bad_trade_kld_multiplier: 2.5 bad_trade_ppl_multiplier: 3.5 + + # Float comparison tolerance. floating_point_epsilon: 1.0e-8 + + # Minimum meaningful reduction ratio for base-only comparisons. minimum_meaningful_base_only_reduction_ratio: 0.01 prediction: + # 0 = automatic size ceiling behavior using current built-in logic. + # + # If > 0, this becomes a hard manual predicted-size ceiling in bytes. + # Any predicted combo larger than this is pruned out. + # + # Example for ~4 GiB: + # manual_max_predicted_size_bytes: 4294967296 manual_max_predicted_size_bytes: 0 baselines: + # ---------------------------------------------------------- + # standard_baselines_mode options + # ---------------------------------------------------------- + # all + # Keep built-in standard baselines active AND allow custom repositories. + # + # standard_only + # Use only built-in baselines. Ignore custom repositories. + # + # custom_only + # Use only custom repositories for learning/carrier/explicit-group roles, + # except for any internal anchors the runtime still requires. standard_baselines_mode: all + + # If empty, runtime uses normal built-in defaults for that category. + # + # Example: + # enabled_standard_learning_baselines: [Q8_0, Q6_K, Q5_K, Q4_K_M] enabled_standard_learning_baselines: [] + + # Example: + # enabled_standard_combination_carriers: [Q8_0, Q6_K, Q5_K] enabled_standard_combination_carriers: [] + + # Example: + # enabled_standard_explicit_group_candidates: [Q8_0, Q6_K, Q5_K, Q4_K_M, IQ4_NL, IQ4_XS] enabled_standard_explicit_group_candidates: [] + custom_repositories: - # - repo_id: unsloth/Qwen3.6-35B-A3B-GGUF - # short_source_name: Unsloth + # ======================================================== + # Example custom repository entry + # ======================================================== + # + # This is for external/custom GGUF baselines such as Unsloth. + # + # Flow: + # 1. MagicQuant resolves included files from the repo + # 2. downloads the external GGUF + # 3. validates tensor-name parity against the local source model + # 4. learns tensor behavior from that external GGUF + # 5. rebuilds a MagicQuant-controlled equivalent from the local source model + # 6. benchmarks the rebuilt version instead of trusting the original external artifact + # + # Important: + # - "includes" must be plural + # - "baseline_family" is the internal MagicQuant family being attached to + # - "quantize_base_name" is the quant/base family name used in rebuild logic + # + # - repo_id: unsloth/Qwen3-4B-Instruct-2507-GGUF # enabled: true + # short_source_name: Unsloth + # source_kind: huggingface_gguf_repository + # + # # Repository-level defaults: # allow_as_learning_baseline: true - # allow_as_combination_carrier: true - # allow_as_explicit_group_candidate: true + # allow_as_combination_carrier: false + # allow_as_explicit_group_candidate: false + # + # # Validation / download behavior: + # require_all_includes_to_resolve: true + # validate_tensor_names_against_source_model: true + # delete_partial_or_dirty_downloads: true + # resume_or_retry_downloads: true + # # includes: - # - baseline_family: Q4_K - # file_name: Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf - # display_name: Unsloth-Q4_K_XL - # quantize_base_name: Q4_K + # - file_name: Qwen3-4B-Instruct-2507-UD-Q4_K_XL.gguf + # baseline_family: Q4_K_M + # quantize_base_name: Q4_K_M + # display_name: Unsloth_Q4_K_XL + # allow_as_learning_baseline: true + # allow_as_combination_carrier: true + # allow_as_explicit_group_candidate: true # requires_imatrix: false # banned_group_ids: [] + # + # - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf + # baseline_family: Q5_K + # quantize_base_name: Q5_K + # display_name: Unsloth_Q5_K_XL + # allow_as_learning_baseline: true + # allow_as_combination_carrier: true + # allow_as_explicit_group_candidate: true + # + # - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf + # baseline_family: Q6_K + # quantize_base_name: Q6_K + # display_name: Unsloth_Q6_K_XL + # allow_as_learning_baseline: true + # allow_as_combination_carrier: true + # allow_as_explicit_group_candidate: true + # + # - file_name: Qwen3-4B-Instruct-2507-UD-Q3_K_XL.gguf + # baseline_family: IQ3_S + # quantize_base_name: IQ3_S + # display_name: Unsloth_Q3_K_XL + # allow_as_learning_baseline: true + # allow_as_combination_carrier: false + # allow_as_explicit_group_candidate: true + # + # # Example note: + # # If the repo does not actually contain IQ3_XS, do not reference it. + # # Use only filenames that truly exist in the repository. + # + [] \ No newline at end of file diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 9ed1432..d437279 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,17 +1,3 @@ -# Dev config. Auto-selected in DEBUG when --config is not supplied. -# CLI flags should override values here when both are present. -# -# IMPORTANT: -# - standard_baselines_mode: all -# Keeps built-in llama.cpp learning baselines + allows custom repository baselines too. -# - custom repositories below are additional learned baseline sources. -# - include entries are explicit and safest because file naming can vary by repo. -# - attached_to_baseline tells MagicQuant which internal learned baseline identity this file should behave as. -# - display_name is just a friendly name for logs / DB / troubleshooting. -# - quantize_base_argument_name should normally be the internal baseline family name you want it associated with. -# - short_source_name is what can show up in naming/logic as the short source marker. -# - source_kind is just a source label so this is clearly not a normal llama.cpp-built baseline. - paths: magic_quant_root: model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ @@ -52,7 +38,6 @@ isolation_pruning: minimum_meaningful_base_only_reduction_ratio: 0.01 prediction: - # 0 = automatic current logic based on learned/default threshold behavior. manual_max_predicted_size_bytes: 0 baselines: @@ -71,43 +56,47 @@ baselines: delete_partial_or_dirty_downloads: true resume_or_retry_downloads: true - include: + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: false + + includes: - file_name: Qwen3-4B-Instruct-2507-UD-Q4_K_XL.gguf - attached_to_baseline: Q4_K_M - quantize_base_argument_name: Q4_K_M + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M display_name: Unsloth_Q4_K_XL - enabled_for_learning: true - enabled_for_combination_carrier: true - enabled_for_explicit_group_candidate: true + allow_as_learning_baseline: true + allow_as_combination_carrier: true + allow_as_explicit_group_candidate: true - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf - attached_to_baseline: Q5_K - quantize_base_argument_name: Q5_K + baseline_family: Q5_K + quantize_base_name: Q5_K display_name: Unsloth_Q5_K_XL - enabled_for_learning: true - enabled_for_combination_carrier: true - enabled_for_explicit_group_candidate: true + allow_as_learning_baseline: true + allow_as_combination_carrier: true + allow_as_explicit_group_candidate: true - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf - attached_to_baseline: Q6_K - quantize_base_argument_name: Q6_K + baseline_family: Q6_K + quantize_base_name: Q6_K display_name: Unsloth_Q6_K_XL - enabled_for_learning: true - enabled_for_combination_carrier: true - enabled_for_explicit_group_candidate: true + allow_as_learning_baseline: true + allow_as_combination_carrier: true + allow_as_explicit_group_candidate: true - file_name: Qwen3-4B-Instruct-2507-UD-Q3_K_XL.gguf - attached_to_baseline: IQ3_S - quantize_base_argument_name: IQ3_S + baseline_family: IQ3_S + quantize_base_name: IQ3_S display_name: Unsloth_Q3_K_XL - enabled_for_learning: true - enabled_for_combination_carrier: false - enabled_for_explicit_group_candidate: true + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-Instruct-2507-UD-IQ3_XS.gguf - attached_to_baseline: IQ3_XS - quantize_base_argument_name: IQ3_XS - display_name: Unsloth_IQ3_XS - enabled_for_learning: true - enabled_for_combination_carrier: false - enabled_for_explicit_group_candidate: true \ No newline at end of file + - file_name: Qwen3-4B-Instruct-2507-UD-IQ3_XXS.gguf + baseline_family: IQ3_XS + quantize_base_name: IQ3_XS + display_name: Unsloth_IQ3_XXS_for_IQ3_XS + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true \ No newline at end of file From 58676a348817f86d0cb9639e647dc29b0b8cf7c8 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 22 Apr 2026 14:54:23 -0400 Subject: [PATCH 111/258] working external baselines like Unsloth. --- MQ.DB/Data/MagicQuantContext.cs | 107 +++++++++++++----- ... 20260422171457_InitialCreate.Designer.cs} | 2 +- ...ate.cs => 20260422171457_InitialCreate.cs} | 0 MQ.DB/Models/BaselineQuants.cs | 43 ++++--- .../Services/HuggingFaceBaselineService.cs | 69 ++++++++++- 5 files changed, 173 insertions(+), 48 deletions(-) rename MQ.DB/Migrations/{20260421190835_InitialCreate.Designer.cs => 20260422171457_InitialCreate.Designer.cs} (99%) rename MQ.DB/Migrations/{20260421190835_InitialCreate.cs => 20260422171457_InitialCreate.cs} (100%) diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index 5cdc668..eaf9afe 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -88,8 +88,6 @@ private void EnsureBaselineQuantDefinitions() .ToList(); var current = BaselineQuantDefinitions - .AsNoTracking() - .OrderBy(x => x.BaselineQuantId) .ToList(); if (current.Count == 0) @@ -99,33 +97,88 @@ private void EnsureBaselineQuantDefinitions() return; } - var mismatch = current.Count != expected.Count || - current.Zip(expected, (a, b) => - a.BaselineQuantId == b.BaselineQuantId && - a.DefaultTensorSchemeId == b.DefaultTensorSchemeId && - a.IsCustomBaseline == b.IsCustomBaseline && - a.IsLearningBaseline == b.IsLearningBaseline && - a.IsCombinationCarrierCandidate == b.IsCombinationCarrierCandidate && - a.IsExplicitGroupCombinationCandidate == b.IsExplicitGroupCombinationCandidate && - a.RequiresImatrix == b.RequiresImatrix && - a.ExplicitCandidateSortOrder == b.ExplicitCandidateSortOrder && - string.Equals(a.CanonicalKey, b.CanonicalKey, StringComparison.Ordinal) && - string.Equals(a.BaselineName, b.BaselineName, StringComparison.Ordinal) && - string.Equals(a.QuantizeBaseArgumentName, b.QuantizeBaseArgumentName, StringComparison.Ordinal) && - string.Equals(a.DefaultTensorSchemeName, b.DefaultTensorSchemeName, StringComparison.Ordinal) && - string.Equals(a.SourceKind, b.SourceKind, StringComparison.Ordinal) && - string.Equals(a.SourceOwner, b.SourceOwner, StringComparison.Ordinal) && - string.Equals(a.SourceRepository, b.SourceRepository, StringComparison.Ordinal) && - string.Equals(a.SourceFileName, b.SourceFileName, StringComparison.Ordinal) && - string.Equals(a.ShortSourceName, b.ShortSourceName, StringComparison.Ordinal)) - .Any(equal => !equal); - - if (mismatch) + var currentByCanonicalKey = current + .Where(x => !string.IsNullOrWhiteSpace(x.CanonicalKey)) + .GroupBy(x => x.CanonicalKey, StringComparer.Ordinal) + .ToDictionary( + g => g.Key, + g => g.OrderBy(x => x.BaselineQuantId).First(), + StringComparer.Ordinal); + + var currentById = current.ToDictionary(x => x.BaselineQuantId); + var changed = false; + + foreach (var expectedRow in expected) { - throw new InvalidOperationException( - "BaselineQuantDefinitions table is out of sync with the runtime baseline registry. " + - "Delete the SQLite DB, recreate migrations, and let MagicQuant reseed baseline definitions."); + BaselineQuantDefinition? target = null; + + if (!string.IsNullOrWhiteSpace(expectedRow.CanonicalKey) && + currentByCanonicalKey.TryGetValue(expectedRow.CanonicalKey, out var byCanonicalKey)) + { + target = byCanonicalKey; + } + else if (currentById.TryGetValue(expectedRow.BaselineQuantId, out var byId)) + { + target = byId; + } + + if (target == null) + { + BaselineQuantDefinitions.Add(expectedRow); + changed = true; + continue; + } + + if (!BaselineDefinitionEquals(target, expectedRow)) + { + ApplyBaselineDefinitionUpdate(target, expectedRow); + changed = true; + } } + + if (changed) + SaveChanges(); + } + + private static bool BaselineDefinitionEquals(BaselineQuantDefinition a, BaselineQuantDefinition b) + { + return a.BaselineQuantId == b.BaselineQuantId && + a.DefaultTensorSchemeId == b.DefaultTensorSchemeId && + a.IsCustomBaseline == b.IsCustomBaseline && + a.IsLearningBaseline == b.IsLearningBaseline && + a.IsCombinationCarrierCandidate == b.IsCombinationCarrierCandidate && + a.IsExplicitGroupCombinationCandidate == b.IsExplicitGroupCombinationCandidate && + a.RequiresImatrix == b.RequiresImatrix && + a.ExplicitCandidateSortOrder == b.ExplicitCandidateSortOrder && + string.Equals(a.CanonicalKey, b.CanonicalKey, StringComparison.Ordinal) && + string.Equals(a.BaselineName, b.BaselineName, StringComparison.Ordinal) && + string.Equals(a.QuantizeBaseArgumentName, b.QuantizeBaseArgumentName, StringComparison.Ordinal) && + string.Equals(a.DefaultTensorSchemeName, b.DefaultTensorSchemeName, StringComparison.Ordinal) && + string.Equals(a.SourceKind, b.SourceKind, StringComparison.Ordinal) && + string.Equals(a.SourceOwner, b.SourceOwner, StringComparison.Ordinal) && + string.Equals(a.SourceRepository, b.SourceRepository, StringComparison.Ordinal) && + string.Equals(a.SourceFileName, b.SourceFileName, StringComparison.Ordinal) && + string.Equals(a.ShortSourceName, b.ShortSourceName, StringComparison.Ordinal); + } + + private static void ApplyBaselineDefinitionUpdate(BaselineQuantDefinition target, BaselineQuantDefinition source) + { + target.CanonicalKey = source.CanonicalKey; + target.BaselineName = source.BaselineName; + target.QuantizeBaseArgumentName = source.QuantizeBaseArgumentName; + target.DefaultTensorSchemeId = source.DefaultTensorSchemeId; + target.DefaultTensorSchemeName = source.DefaultTensorSchemeName; + target.SourceKind = source.SourceKind; + target.SourceOwner = source.SourceOwner; + target.SourceRepository = source.SourceRepository; + target.SourceFileName = source.SourceFileName; + target.ShortSourceName = source.ShortSourceName; + target.IsCustomBaseline = source.IsCustomBaseline; + target.IsLearningBaseline = source.IsLearningBaseline; + target.IsCombinationCarrierCandidate = source.IsCombinationCarrierCandidate; + target.IsExplicitGroupCombinationCandidate = source.IsExplicitGroupCombinationCandidate; + target.RequiresImatrix = source.RequiresImatrix; + target.ExplicitCandidateSortOrder = source.ExplicitCandidateSortOrder; } private static bool IsDesignTime() diff --git a/MQ.DB/Migrations/20260421190835_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260422171457_InitialCreate.Designer.cs similarity index 99% rename from MQ.DB/Migrations/20260421190835_InitialCreate.Designer.cs rename to MQ.DB/Migrations/20260422171457_InitialCreate.Designer.cs index 7e8ef7c..4d387dc 100644 --- a/MQ.DB/Migrations/20260421190835_InitialCreate.Designer.cs +++ b/MQ.DB/Migrations/20260422171457_InitialCreate.Designer.cs @@ -11,7 +11,7 @@ namespace MQ.DB.Migrations { [DbContext(typeof(MagicQuantContext))] - [Migration("20260421190835_InitialCreate")] + [Migration("20260422171457_InitialCreate")] partial class InitialCreate { /// diff --git a/MQ.DB/Migrations/20260421190835_InitialCreate.cs b/MQ.DB/Migrations/20260422171457_InitialCreate.cs similarity index 100% rename from MQ.DB/Migrations/20260421190835_InitialCreate.cs rename to MQ.DB/Migrations/20260422171457_InitialCreate.cs diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 30842ed..ccafcf1 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -15,6 +15,7 @@ public record BaselineQuants( bool IsCombinationCarrierCandidate, bool IsExplicitGroupCombinationCandidate, bool IsHighPrecisionExactAlias, + byte BitRange, bool IsCustomBaseline = false, string CanonicalKey = "", string SourceKind = "standard", @@ -37,6 +38,7 @@ public record BaselineQuants( public ImmutableArray TensorWeightSchemes => LearnedMatchTensorWeightSchemes; public bool IsPureBaselineCandidate => IsLearningBaseline; public bool IsHighPrecisionExplicitCandidate => IsHighPrecisionExactAlias; + public bool IsExternalRepositoryBaseline => IsCustomBaseline && !string.IsNullOrWhiteSpace(SourceRepository) && @@ -54,6 +56,7 @@ private static BaselineQuants Create( bool isCombinationCarrierCandidate, bool isExplicitGroupCombinationCandidate, bool isHighPrecisionExactAlias, + byte bitRange, int explicitCandidateSortOrder = int.MaxValue, bool isCustomBaseline = false, string? canonicalKey = null, @@ -75,6 +78,7 @@ private static BaselineQuants Create( isCombinationCarrierCandidate, isExplicitGroupCombinationCandidate, isHighPrecisionExactAlias, + bitRange, isCustomBaseline, canonicalKey ?? $"standard:{name.ToLowerInvariant()}", sourceKind, @@ -86,47 +90,46 @@ private static BaselineQuants Create( } public static readonly BaselineQuants Q8_0 = - Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 11); + Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 8, 11); public static readonly BaselineQuants Q6_K = - Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, true, true, false, 10); + Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, true, true, false, 6, 10); public static readonly BaselineQuants Q5_K = - Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 9); + Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 5, 9); public static readonly BaselineQuants Q4_K_M = - Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 8); + Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 8); public static readonly BaselineQuants IQ4_NL = - Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, true, true, false, 7); + Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 7); public static readonly BaselineQuants IQ4_XS = - Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, true, true, false, 6); + Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 6); public static readonly BaselineQuants IQ3_S = - Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 5); + Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 5); public static readonly BaselineQuants IQ3_XS = - Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 4); + Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 4); public static readonly BaselineQuants IQ3_XXS = - Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3); + Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 3); public static readonly BaselineQuants IQ2_S = - Create(10, true, "IQ2_S", "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2); + Create(10, true, "IQ2_S", "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2, 2); public static readonly BaselineQuants IQ2_XS = - Create(11, true, "IQ2_XS", "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 1); + Create(11, true, "IQ2_XS", "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2, 1); public static readonly BaselineQuants IQ2_XXS = - Create(12, true, "IQ2_XXS", "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], true, false, true, false, 0); - + Create(12, true, "IQ2_XXS", "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], true, false, true, false, 2, 0); public static readonly BaselineQuants BF16_Hybrid = - Create(201, false, "BF16", "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], false, false, false, true, int.MaxValue, false, "alias:bf16", "exact_alias", null, null, null, null); + Create(201, false, "BF16", "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], false, false, false, true, 16, int.MaxValue, false, "alias:bf16", "exact_alias", null, null, null, null); public static readonly BaselineQuants F16_Hybrid = - Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], false, false, false, true, int.MaxValue, false, "alias:f16", "exact_alias", null, null, null, null); + Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], false, false, false, true, 16, int.MaxValue, false, "alias:f16", "exact_alias", null, null, null, null); private static readonly ImmutableArray StandardBaselines = [ @@ -169,6 +172,7 @@ public static BaselineQuants CreateDynamicCustomBaseline( bool isLearningBaseline, bool isCombinationCarrierCandidate, bool isExplicitGroupCombinationCandidate, + byte bitRange, int explicitCandidateSortOrder) { return new BaselineQuants( @@ -183,6 +187,7 @@ public static BaselineQuants CreateDynamicCustomBaseline( isCombinationCarrierCandidate, isExplicitGroupCombinationCandidate, false, + bitRange, true, canonicalKey, sourceKind, @@ -304,6 +309,7 @@ public sealed class ExternalBaselineRegistration public bool AddAsLearningBaseline { get; set; } public bool AddAsCombinationCarrier { get; set; } public bool AddAsGroupCandidate { get; set; } + public byte BitRange { get; set; } public IReadOnlyCollection BannedGroupIds { get; set; } = Array.Empty(); } @@ -330,6 +336,7 @@ public static BaselineQuants RegisterCustomExternalBaseline(ExternalBaselineRegi registration.AddAsLearningBaseline, registration.AddAsCombinationCarrier, registration.AddAsGroupCandidate, + registration.BitRange, sortOrder); RegisterDynamicCustomBaseline(baseline); @@ -352,6 +359,7 @@ public static BaselineQuants GetNativeQuant() false, false, true, + 16, false, $"native:{nativeScheme.Names[0].ToLowerInvariant()}", "native_exact_alias", @@ -434,7 +442,8 @@ public static IReadOnlyList GetGroupCombinationCandidates(bool h public static IReadOnlyList GetGroupCombinationCandidatesSmallestFirst(bool hasUsableImatrix, bool allowHighPrecisionHybrids) => GetGroupCombinationCandidates(hasUsableImatrix, allowHighPrecisionHybrids) - .OrderBy(x => x.ExplicitCandidateSortOrder) + .OrderBy(x => x.BitRange) + .ThenBy(x => x.ExplicitCandidateSortOrder) .ThenBy(x => x.UniqueId) .ToList(); @@ -584,4 +593,4 @@ public static BaselineQuants FromTensorSchemeId(byte schemeId) return found; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index f02eeb8..7d188cd 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -1,3 +1,5 @@ +using Microsoft.EntityFrameworkCore; +using MQ.DB.Data; using System.Text.Json; using MagicQuant.Configuration; using MagicQuant.Helpers; @@ -35,6 +37,14 @@ public async Task> PrecheckAndRegister return resolved; } + var existingDynamicIdsByCanonicalKey = LoadExistingDynamicBaselineIds(); + var reservedIds = BaselineQuants.GetAllRecognizedBaselines() + .Select(x => x.UniqueId) + .ToHashSet(); + + foreach (var persistedId in existingDynamicIdsByCanonicalKey.Values) + reservedIds.Add(persistedId); + byte nextId = BaselineQuants.GetFirstAvailableDynamicBaselineId(); foreach (var repo in enabledRepos) @@ -85,8 +95,14 @@ public async Task> PrecheckAndRegister ? include.BannedGroupIds.ToArray() : standardFamily.BannedGroupIds.ToArray(); + byte dynamicBaselineId = ResolveDynamicBaselineId( + canonicalKey, + existingDynamicIdsByCanonicalKey, + reservedIds, + ref nextId); + var dynamicBaseline = BaselineQuants.CreateDynamicCustomBaseline( - uniqueId: nextId, + uniqueId: dynamicBaselineId, displayName: displayName, quantizeBaseArgumentName: quantizeBaseName, sourceRepository: repo.RepoId, @@ -102,6 +118,7 @@ public async Task> PrecheckAndRegister isLearningBaseline: allowAsLearning, isCombinationCarrierCandidate: allowAsCarrier, isExplicitGroupCombinationCandidate: allowAsExplicit, + bitRange: standardFamily.BitRange, explicitCandidateSortOrder: standardFamily.ExplicitCandidateSortOrder); BaselineQuants.RegisterDynamicCustomBaseline(dynamicBaseline); @@ -127,8 +144,6 @@ public async Task> PrecheckAndRegister resolved.Add(spec); AnsiConsole.MarkupLine( $" [green]Resolved:[/] id=[cyan]{dynamicBaseline.UniqueId}[/] family=[yellow]{Markup.Escape(standardFamily.Names[0])}[/] file=[blue]{Markup.Escape(resolvedFileName)}[/] learning={allowAsLearning} carrier={allowAsCarrier} explicit={allowAsExplicit}"); - - checked { nextId++; } } } @@ -142,6 +157,54 @@ public async Task> PrecheckAndRegister return resolved; } + private static Dictionary LoadExistingDynamicBaselineIds() + { + try + { + using var db = new MagicQuantContext(); + + return db.BaselineQuantDefinitions + .AsNoTracking() + .Where(x => x.IsCustomBaseline && !string.IsNullOrWhiteSpace(x.CanonicalKey)) + .OrderBy(x => x.BaselineQuantId) + .ToDictionary(x => x.CanonicalKey, x => x.BaselineQuantId, StringComparer.Ordinal); + } + catch + { + return new Dictionary(StringComparer.Ordinal); + } + } + + private static byte ResolveDynamicBaselineId( + string canonicalKey, + IReadOnlyDictionary existingDynamicIdsByCanonicalKey, + HashSet reservedIds, + ref byte nextId) + { + if (!string.IsNullOrWhiteSpace(canonicalKey) && + existingDynamicIdsByCanonicalKey.TryGetValue(canonicalKey, out var existingId)) + { + reservedIds.Add(existingId); + return existingId; + } + + while (reservedIds.Contains(nextId)) + { + if (nextId >= 199) + throw new InvalidOperationException("No free dynamic baseline ids remain in the configured range."); + + nextId++; + } + + var allocated = nextId; + reservedIds.Add(allocated); + + if (nextId < 199) + nextId++; + + return allocated; + } + public async Task DownloadBaselineAsync(BaselineQuants baseline, string destinationPath, bool forceRedownload = false, CancellationToken ct = default) { if (!baseline.IsExternalRepositoryBaseline) From aac26e3b42b44e3dcc64a6d407bd734632db8ea6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 22 Apr 2026 15:02:19 -0400 Subject: [PATCH 112/258] BitRange logic added. --- MQ.DB/Data/MagicQuantContext.cs | 5 +- ...260422185552_AddByteRangeLogic.Designer.cs | 668 ++++++++++++++++++ .../20260422185552_AddByteRangeLogic.cs | 29 + .../MagicQuantContextModelSnapshot.cs | 3 + .../DbModels/BaselineQuantDefinition.cs | 1 + 5 files changed, 705 insertions(+), 1 deletion(-) create mode 100644 MQ.DB/Migrations/20260422185552_AddByteRangeLogic.Designer.cs create mode 100644 MQ.DB/Migrations/20260422185552_AddByteRangeLogic.cs diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index eaf9afe..0bfe688 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -82,6 +82,7 @@ private void EnsureBaselineQuantDefinitions() IsCombinationCarrierCandidate = x.IsCombinationCarrierCandidate, IsExplicitGroupCombinationCandidate = x.IsExplicitGroupCombinationCandidate, RequiresImatrix = x.RequiresImatrix, + BitRange = x.BitRange, ExplicitCandidateSortOrder = x.ExplicitCandidateSortOrder }) .OrderBy(x => x.BaselineQuantId) @@ -149,6 +150,7 @@ private static bool BaselineDefinitionEquals(BaselineQuantDefinition a, Baseline a.IsCombinationCarrierCandidate == b.IsCombinationCarrierCandidate && a.IsExplicitGroupCombinationCandidate == b.IsExplicitGroupCombinationCandidate && a.RequiresImatrix == b.RequiresImatrix && + a.BitRange == b.BitRange && a.ExplicitCandidateSortOrder == b.ExplicitCandidateSortOrder && string.Equals(a.CanonicalKey, b.CanonicalKey, StringComparison.Ordinal) && string.Equals(a.BaselineName, b.BaselineName, StringComparison.Ordinal) && @@ -178,6 +180,7 @@ private static void ApplyBaselineDefinitionUpdate(BaselineQuantDefinition target target.IsCombinationCarrierCandidate = source.IsCombinationCarrierCandidate; target.IsExplicitGroupCombinationCandidate = source.IsExplicitGroupCombinationCandidate; target.RequiresImatrix = source.RequiresImatrix; + target.BitRange = source.BitRange; target.ExplicitCandidateSortOrder = source.ExplicitCandidateSortOrder; } @@ -257,4 +260,4 @@ private void ValidateDbSetsImplementInterface() ); } } -} \ No newline at end of file +} diff --git a/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.Designer.cs b/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.Designer.cs new file mode 100644 index 0000000..d8f16af --- /dev/null +++ b/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.Designer.cs @@ -0,0 +1,668 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260422185552_AddByteRangeLogic")] + partial class AddByteRangeLogic + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("BitRange") + .HasColumnType("INTEGER"); + + b.Property("CanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ExplicitCandidateSortOrder") + .HasColumnType("INTEGER"); + + b.Property("IsCombinationCarrierCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsCustomBaseline") + .HasColumnType("INTEGER"); + + b.Property("IsExplicitGroupCombinationCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsLearningBaseline") + .HasColumnType("INTEGER"); + + b.Property("QuantizeBaseArgumentName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RequiresImatrix") + .HasColumnType("INTEGER"); + + b.Property("ShortSourceName") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceOwner") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("SourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("CanonicalKey") + .IsUnique(); + + b.HasIndex("SourceRepository", "SourceFileName"); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.cs b/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.cs new file mode 100644 index 0000000..cc47976 --- /dev/null +++ b/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class AddByteRangeLogic : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "BitRange", + table: "BaselineQuantDefinitions", + type: "INTEGER", + nullable: false, + defaultValue: (byte)0); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "BitRange", + table: "BaselineQuantDefinitions"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index 4174181..2904a7b 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -79,6 +79,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(128) .HasColumnType("TEXT"); + b.Property("BitRange") + .HasColumnType("INTEGER"); + b.Property("CanonicalKey") .IsRequired() .HasMaxLength(256) diff --git a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs index 5d61881..def8f7d 100644 --- a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs +++ b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs @@ -22,6 +22,7 @@ public class BaselineQuantDefinition : ISQLiteEntity public bool IsCombinationCarrierCandidate { get; set; } public bool IsExplicitGroupCombinationCandidate { get; set; } public bool RequiresImatrix { get; set; } + public byte BitRange { get; set; } public int ExplicitCandidateSortOrder { get; set; } public void Configure(EntityTypeBuilder builder) From 33eea3f947c69eda590e15fe4dcb85f09f91f5d4 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 22 Apr 2026 18:30:09 -0400 Subject: [PATCH 113/258] new architecture category identity. FIxed quantization errors. the system looks solid. --- MQ.DB/Cache.cs | 13 +- MQ.DB/Data/MagicQuantContext.cs | 4 +- .../20260422171457_InitialCreate.Designer.cs | 665 ------------------ .../20260422185552_AddByteRangeLogic.cs | 29 - ... 20260422202538_InitialCreate.Designer.cs} | 89 ++- ...ate.cs => 20260422202538_InitialCreate.cs} | 75 ++ .../MagicQuantContextModelSnapshot.cs | 85 +++ MQ.DB/Models/DbModels/ArchitectureFamily.cs | 24 + .../DbModels/ArchitectureFamilyModelHash.cs | 31 + .../DbModels/BaselineQuantDefinition.cs | 2 +- .../DbModels/ExecutionPlanProbeCache.cs | 2 +- MQ.DB/Models/DbModels/ImatrixDefinition.cs | 2 +- .../DbModels/LearnedBaselineTensorQuant.cs | 2 +- MagicQuant/Commands/Evolution.cs | 3 + .../Configuration/MagicQuantYamlConfig.cs | 9 +- .../Configuration/MagicQuantYamlLoader.cs | 12 +- MagicQuant/Helpers/CliHelpers.cs | 1 + MagicQuant/Helpers/TensorConfigGenerator.cs | 12 - MagicQuant/Program.cs | 10 +- .../Services/ArchitectureFamilyService.cs | 218 ++++++ MagicQuant/Services/BenchmarkService.cs | 25 +- MagicQuant/Services/ImatrixIdentityService.cs | 10 +- .../Services/IsolationOptimizationService.cs | 8 +- .../Services/IsolationPlanningService.cs | 2 +- .../Services/LearnedBaselinePruningService.cs | 2 +- MagicQuant/Services/QuantizationService.cs | 644 ++++++++++++----- config.backup.yaml | 53 ++ config.default.yaml | 53 ++ config.dev.yaml | 53 ++ 29 files changed, 1230 insertions(+), 908 deletions(-) delete mode 100644 MQ.DB/Migrations/20260422171457_InitialCreate.Designer.cs delete mode 100644 MQ.DB/Migrations/20260422185552_AddByteRangeLogic.cs rename MQ.DB/Migrations/{20260422185552_AddByteRangeLogic.Designer.cs => 20260422202538_InitialCreate.Designer.cs} (88%) rename MQ.DB/Migrations/{20260422171457_InitialCreate.cs => 20260422202538_InitialCreate.cs} (87%) create mode 100644 MQ.DB/Models/DbModels/ArchitectureFamily.cs create mode 100644 MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs create mode 100644 MagicQuant/Services/ArchitectureFamilyService.cs create mode 100644 config.backup.yaml create mode 100644 config.default.yaml create mode 100644 config.dev.yaml diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index c2e7071..6f5db02 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -71,6 +71,17 @@ public enum MainTorchType public static string CurrentModelId { get; set; } = string.Empty; + public static string CurrentArchitectureFamilyName { get; set; } = string.Empty; + + public static string CurrentArchitectureFamilyNormalizedName => + string.IsNullOrWhiteSpace(CurrentArchitectureFamilyName) + ? string.Empty + : CurrentArchitectureFamilyName.Trim().ToLowerInvariant(); + + public static bool AllowArchitectureFamilyAliasOverride { get; set; } + + public static int? CurrentArchitectureFamilyId { get; set; } + public static bool ForceRelearnBaselineTensorMappings { get; set; } public static bool ForceRefreshHardwareProbe { get; set; } @@ -84,4 +95,4 @@ public enum MainTorchType public static string? ActiveImatrixPath { get; set; } public static string? ActiveImatrixIdentityHash { get; set; } -} +} \ No newline at end of file diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index 0bfe688..e03ebb4 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -204,6 +204,8 @@ private static bool IsDesignTime() public DbSet BaselineQuantDefinitions { get; set; } public DbSet ExecutionPlanProbeCaches { get; set; } public DbSet ImatrixDefinitions { get; set; } + public DbSet ArchitectureFamilies { get; set; } + public DbSet ArchitectureFamilyModelHashes { get; set; } // -------------------------------------------------------- // Configuration @@ -260,4 +262,4 @@ private void ValidateDbSetsImplementInterface() ); } } -} +} \ No newline at end of file diff --git a/MQ.DB/Migrations/20260422171457_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260422171457_InitialCreate.Designer.cs deleted file mode 100644 index 4d387dc..0000000 --- a/MQ.DB/Migrations/20260422171457_InitialCreate.Designer.cs +++ /dev/null @@ -1,665 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260422171457_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("CanonicalKey") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("ExplicitCandidateSortOrder") - .HasColumnType("INTEGER"); - - b.Property("IsCombinationCarrierCandidate") - .HasColumnType("INTEGER"); - - b.Property("IsCustomBaseline") - .HasColumnType("INTEGER"); - - b.Property("IsExplicitGroupCombinationCandidate") - .HasColumnType("INTEGER"); - - b.Property("IsLearningBaseline") - .HasColumnType("INTEGER"); - - b.Property("QuantizeBaseArgumentName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("RequiresImatrix") - .HasColumnType("INTEGER"); - - b.Property("ShortSourceName") - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("SourceFileName") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("SourceOwner") - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("SourceRepository") - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("CanonicalKey") - .IsUnique(); - - b.HasIndex("SourceRepository", "SourceFileName"); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DiscoveryTokenTarget") - .HasColumnType("INTEGER"); - - b.Property("GroupSize") - .HasColumnType("INTEGER"); - - b.Property("HardwareFingerprint") - .IsRequired() - .HasMaxLength(1024) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("QuantizationKey") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("QuantizedModelFingerprint") - .IsRequired() - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("SlotsJson") - .IsRequired() - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("StaticNgl") - .HasColumnType("INTEGER"); - - b.Property("UpdatedUtc") - .HasColumnType("TEXT"); - - b.Property("UsesGpu") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") - .IsUnique(); - - b.ToTable("ExecutionPlanProbeCaches"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BuildFingerprint") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("CanonicalPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IdentityHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("MetadataJson") - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("TokenCount") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId", "IdentityHash") - .IsUnique(); - - b.ToTable("ImatrixDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineCanonicalKey") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineSourceFileName") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("BaselineSourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("BaselineSourceRepository") - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.cs b/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.cs deleted file mode 100644 index cc47976..0000000 --- a/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class AddByteRangeLogic : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AddColumn( - name: "BitRange", - table: "BaselineQuantDefinitions", - type: "INTEGER", - nullable: false, - defaultValue: (byte)0); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropColumn( - name: "BitRange", - table: "BaselineQuantDefinitions"); - } - } -} diff --git a/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.Designer.cs b/MQ.DB/Migrations/20260422202538_InitialCreate.Designer.cs similarity index 88% rename from MQ.DB/Migrations/20260422185552_AddByteRangeLogic.Designer.cs rename to MQ.DB/Migrations/20260422202538_InitialCreate.Designer.cs index d8f16af..00409c2 100644 --- a/MQ.DB/Migrations/20260422185552_AddByteRangeLogic.Designer.cs +++ b/MQ.DB/Migrations/20260422202538_InitialCreate.Designer.cs @@ -11,8 +11,8 @@ namespace MQ.DB.Migrations { [DbContext(typeof(MagicQuantContext))] - [Migration("20260422185552_AddByteRangeLogic")] - partial class AddByteRangeLogic + [Migration("20260422202538_InitialCreate")] + partial class InitialCreate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -72,6 +72,72 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.ToTable("AiModelHashes"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("NormalizedName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("TensorCount") + .HasColumnType("INTEGER"); + + b.Property("TensorSignatureHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique(); + + b.HasIndex("TensorSignatureHash", "TensorCount"); + + b.ToTable("ArchitectureFamilies"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IsCanonical") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "AiModelHashId") + .IsUnique(); + + b.ToTable("ArchitectureFamilyModelHashes"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => { b.Property("BaselineQuantId") @@ -525,6 +591,25 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Navigation("TensorCombo"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => { b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") diff --git a/MQ.DB/Migrations/20260422171457_InitialCreate.cs b/MQ.DB/Migrations/20260422202538_InitialCreate.cs similarity index 87% rename from MQ.DB/Migrations/20260422171457_InitialCreate.cs rename to MQ.DB/Migrations/20260422202538_InitialCreate.cs index 0a09c7d..3dde0e5 100644 --- a/MQ.DB/Migrations/20260422171457_InitialCreate.cs +++ b/MQ.DB/Migrations/20260422202538_InitialCreate.cs @@ -24,6 +24,23 @@ protected override void Up(MigrationBuilder migrationBuilder) table.PrimaryKey("PK_AiModelHashes", x => x.Id); }); + migrationBuilder.CreateTable( + name: "ArchitectureFamilies", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + NormalizedName = table.Column(type: "TEXT", maxLength: 256, nullable: false), + DisplayName = table.Column(type: "TEXT", maxLength: 256, nullable: false), + TensorSignatureHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), + TensorCount = table.Column(type: "INTEGER", nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ArchitectureFamilies", x => x.Id); + }); + migrationBuilder.CreateTable( name: "BaselineQuantDefinitions", columns: table => new @@ -44,6 +61,7 @@ protected override void Up(MigrationBuilder migrationBuilder) IsCombinationCarrierCandidate = table.Column(type: "INTEGER", nullable: false), IsExplicitGroupCombinationCandidate = table.Column(type: "INTEGER", nullable: false), RequiresImatrix = table.Column(type: "INTEGER", nullable: false), + BitRange = table.Column(type: "INTEGER", nullable: false), ExplicitCandidateSortOrder = table.Column(type: "INTEGER", nullable: false) }, constraints: table => @@ -98,6 +116,34 @@ protected override void Up(MigrationBuilder migrationBuilder) onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ArchitectureFamilyModelHashes", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + IsCanonical = table.Column(type: "INTEGER", nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ArchitectureFamilyModelHashes", x => x.Id); + table.ForeignKey( + name: "FK_ArchitectureFamilyModelHashes_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ArchitectureFamilyModelHashes_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "AiBenchmarks", columns: table => new @@ -342,6 +388,29 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "AiModelHashes", column: "UniqueHash"); + migrationBuilder.CreateIndex( + name: "IX_ArchitectureFamilies_NormalizedName", + table: "ArchitectureFamilies", + column: "NormalizedName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_ArchitectureFamilies_TensorSignatureHash_TensorCount", + table: "ArchitectureFamilies", + columns: new[] { "TensorSignatureHash", "TensorCount" }); + + migrationBuilder.CreateIndex( + name: "IX_ArchitectureFamilyModelHashes_AiModelHashId", + table: "ArchitectureFamilyModelHashes", + column: "AiModelHashId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_ArchitectureFamilyModelHashes_ArchitectureFamilyId_AiModelHashId", + table: "ArchitectureFamilyModelHashes", + columns: new[] { "ArchitectureFamilyId", "AiModelHashId" }, + unique: true); + migrationBuilder.CreateIndex( name: "IX_BaselineQuantDefinitions_CanonicalKey", table: "BaselineQuantDefinitions", @@ -466,6 +535,9 @@ protected override void Up(MigrationBuilder migrationBuilder) /// protected override void Down(MigrationBuilder migrationBuilder) { + migrationBuilder.DropTable( + name: "ArchitectureFamilyModelHashes"); + migrationBuilder.DropTable( name: "BaselineQuantDefinitions"); @@ -481,6 +553,9 @@ protected override void Down(MigrationBuilder migrationBuilder) migrationBuilder.DropTable( name: "QuantizationRuns"); + migrationBuilder.DropTable( + name: "ArchitectureFamilies"); + migrationBuilder.DropTable( name: "CategoryBenchmark"); diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index 2904a7b..b6d59f7 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -69,6 +69,72 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("AiModelHashes"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("NormalizedName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("TensorCount") + .HasColumnType("INTEGER"); + + b.Property("TensorSignatureHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique(); + + b.HasIndex("TensorSignatureHash", "TensorCount"); + + b.ToTable("ArchitectureFamilies"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IsCanonical") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "AiModelHashId") + .IsUnique(); + + b.ToTable("ArchitectureFamilyModelHashes"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => { b.Property("BaselineQuantId") @@ -522,6 +588,25 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("TensorCombo"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => { b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") diff --git a/MQ.DB/Models/DbModels/ArchitectureFamily.cs b/MQ.DB/Models/DbModels/ArchitectureFamily.cs new file mode 100644 index 0000000..93f7099 --- /dev/null +++ b/MQ.DB/Models/DbModels/ArchitectureFamily.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class ArchitectureFamily : ISQLiteEntity +{ + public int Id { get; set; } + public string NormalizedName { get; set; } = string.Empty; + public string DisplayName { get; set; } = string.Empty; + public string TensorSignatureHash { get; set; } = string.Empty; + public int TensorCount { get; set; } + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.HasIndex(x => x.NormalizedName).IsUnique(); + builder.HasIndex(x => new { x.TensorSignatureHash, x.TensorCount }); + builder.Property(x => x.NormalizedName).HasMaxLength(256).IsRequired(); + builder.Property(x => x.DisplayName).HasMaxLength(256).IsRequired(); + builder.Property(x => x.TensorSignatureHash).HasMaxLength(128).IsRequired(); + } +} diff --git a/MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs b/MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs new file mode 100644 index 0000000..1ea94ed --- /dev/null +++ b/MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class ArchitectureFamilyModelHash : ISQLiteEntity +{ + public int Id { get; set; } + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + public bool IsCanonical { get; set; } + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.AiModelHashId }).IsUnique(); + builder.HasIndex(x => x.AiModelHashId).IsUnique(); + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.AiModelHash) + .WithMany() + .HasForeignKey(x => x.AiModelHashId) + .OnDelete(DeleteBehavior.Cascade); + } +} diff --git a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs index def8f7d..4ced612 100644 --- a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs +++ b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs @@ -64,4 +64,4 @@ public void Configure(EntityTypeBuilder builder) builder.HasIndex(x => x.CanonicalKey).IsUnique(); builder.HasIndex(x => new { x.SourceRepository, x.SourceFileName }); } -} +} \ No newline at end of file diff --git a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs index 696a1f8..8094fe2 100644 --- a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs +++ b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs @@ -59,4 +59,4 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.ImatrixDefinitionId) .OnDelete(DeleteBehavior.Restrict); } -} +} \ No newline at end of file diff --git a/MQ.DB/Models/DbModels/ImatrixDefinition.cs b/MQ.DB/Models/DbModels/ImatrixDefinition.cs index 166e07a..23945f0 100644 --- a/MQ.DB/Models/DbModels/ImatrixDefinition.cs +++ b/MQ.DB/Models/DbModels/ImatrixDefinition.cs @@ -32,4 +32,4 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.AiModelHashId) .OnDelete(DeleteBehavior.Cascade); } -} +} \ No newline at end of file diff --git a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs index 8203151..b8c33c9 100644 --- a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs +++ b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs @@ -79,4 +79,4 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.AiBenchmarkId) .OnDelete(DeleteBehavior.Cascade); } -} +} \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 44baebe..d9dbe63 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -107,6 +107,9 @@ public async Task Run(List args) string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); + var architectureFamilyService = new ArchitectureFamilyService(pyManager); + await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(bf16ModelGgufPath); + var imatrixRequest = new ImatrixRequest { UseImatrix = Cache.UseImatrix, diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index ccf26a5..fb27609 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -10,6 +10,7 @@ public sealed class MagicQuantYamlConfig public RuntimeEvolutionConfig Evolution { get; set; } = new(); public RuntimeIsolationPruningConfig IsolationPruning { get; set; } = new(); public RuntimePredictionConfig Prediction { get; set; } = new(); + public RuntimeIdentityConfig Identity { get; set; } = new(); public RuntimeBaselineConfig Baselines { get; set; } = new(); public List SensitivityProbeGroups { get; set; } = @@ -128,6 +129,12 @@ public sealed class RuntimePredictionConfig public ulong ManualMaxPredictedSizeBytes { get; set; } = 0; } +public sealed class RuntimeIdentityConfig +{ + public string? ArchitectureFamilyName { get; set; } + public bool AllowArchitectureFamilyAliasOverride { get; set; } +} + public sealed class RuntimeBaselineConfig { public string StandardBaselinesMode { get; set; } = "all"; @@ -180,4 +187,4 @@ public sealed class ResolvedCustomBaselineSpec public bool AllowAsCombinationCarrier { get; set; } public bool AllowAsExplicitGroupCandidate { get; set; } public IReadOnlyList BannedGroupIds { get; set; } = Array.Empty(); -} +} \ No newline at end of file diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index cd278fd..da78ada 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -75,6 +75,13 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) Cache.ForceRefreshHardwareProbe = config.Flags.ForceRefreshHardwareProbe; RuntimeSearchSpace.AllowHighPrecisionHybrids = config.Flags.AllowHighPrecisionHybrids; + Cache.CurrentArchitectureFamilyName = + config.Identity.ArchitectureFamilyName?.Trim() ?? string.Empty; + + Cache.AllowArchitectureFamilyAliasOverride = + config.Identity.AllowArchitectureFamilyAliasOverride; + + Cache.CurrentArchitectureFamilyId = null; ApplyStandardBaselineFilters(config.Baselines); BaselineQuants.ResetDynamicCustomBaselines(); @@ -154,6 +161,9 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (ulong.TryParse(Get("manual-max-predicted-size-bytes"), out var manualBytes)) config.Prediction.ManualMaxPredictedSizeBytes = manualBytes; + + config.Identity.ArchitectureFamilyName = Prefer(Get("architecture-family"), config.Identity.ArchitectureFamilyName); + if (Has("allow-architecture-family-alias-override")) config.Identity.AllowArchitectureFamilyAliasOverride = true; } private static string? Prefer(string? preferred, string? fallback) @@ -174,4 +184,4 @@ private static string ResolveMagicQuantRoot(string? configured) return Path.GetFullPath(value); } -} +} \ No newline at end of file diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index 3044db7..c7f9df4 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -121,6 +121,7 @@ public static void ShowHelp(Dictionary [blue][[--option value]][/]"); AnsiConsole.MarkupLine("Config: [green]--config[/] [grey][/] (CLI flags override YAML)"); + AnsiConsole.MarkupLine("Identity: [green]--architecture-family[/] [grey][/] | [green]--allow-architecture-family-alias-override[/]"); AnsiConsole.WriteLine(); } } \ No newline at end of file diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 7fb94d8..7521e3c 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -151,15 +151,9 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample foreach (var candidate in candidates) { - if (candidate.BannedGroupIds.Contains(group.UniqueId)) - continue; - if (smallest != null && candidate.UniqueId == smallest.UniqueId) continue; - if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) - continue; - var quant = HybridQuant.CreateExactBlanket( baseQuant: carrier, groups: TReg.All.Where(x => !missingIds.Contains(x.UniqueId)), @@ -310,12 +304,6 @@ public static IEnumerable> GenerateTensorConfigBatches( RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false)) { - if (candidate.BannedGroupIds.Contains(group.UniqueId)) - continue; - - if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) - continue; - return candidate; } diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index cd1c210..fe1f3c6 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -9,7 +9,13 @@ #if DEBUG if (args.Length == 0) { - args = new[] { "evolution" }; + args = new[] { "evolution", "--architecture-family", @"""Qwen3-4B-Instruct-2507""" }; +} +else if (args.Length > 0 && + string.Equals(args[0], "evolution", StringComparison.OrdinalIgnoreCase) && + !args.Any(x => string.Equals(x, "--architecture-family", StringComparison.OrdinalIgnoreCase))) +{ + args = args.Concat(new[] { "--architecture-family", @"""Qwen3-4B-Instruct-2507""" }).ToArray(); } #endif @@ -79,4 +85,4 @@ await AnsiConsole.Status() catch (Exception ex) { AnsiConsole.WriteException(ex); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/ArchitectureFamilyService.cs b/MagicQuant/Services/ArchitectureFamilyService.cs new file mode 100644 index 0000000..1dba27f --- /dev/null +++ b/MagicQuant/Services/ArchitectureFamilyService.cs @@ -0,0 +1,218 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using MagicQuant.Helpers; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class ArchitectureFamilyService +{ + private readonly PythonManager _python; + + public ArchitectureFamilyService(PythonManager python) + { + _python = python; + } + + public async Task EnsureCurrentArchitectureFamilyAsync(string bf16GgufPath, CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentArchitectureFamilyName)) + throw new InvalidOperationException("Architecture family is required. Provide --architecture-family or set identity.architecture_family_name in YAML."); + + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + throw new InvalidOperationException("Cache.CurrentModelId is not set."); + + var tensorNames = await ReadTensorNamesFromGgufAsync(bf16GgufPath, ct); + if (tensorNames.Count == 0) + throw new InvalidOperationException("Architecture-family validation could not read any tensor names from the BF16 GGUF."); + + string signatureHash = ComputeTensorSignatureHash(tensorNames); + int tensorCount = tensorNames.Count; + string normalized = NormalizeFamilyName(Cache.CurrentArchitectureFamilyName); + + await using var db = new MagicQuantContext(); + + var aiModelHash = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + if (aiModelHash == null) + { + aiModelHash = new AiModelHash { UniqueHash = Cache.CurrentModelId }; + db.AiModelHashes.Add(aiModelHash); + await db.SaveChangesAsync(ct); + } + + var existingMapping = await db.Set() + .Include(x => x.ArchitectureFamily) + .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHash.Id, ct); + + if (existingMapping != null) + { + if (!string.Equals(existingMapping.ArchitectureFamily.NormalizedName, normalized, StringComparison.Ordinal)) + { + throw new InvalidOperationException($"Current model hash is already mapped to architecture family '{existingMapping.ArchitectureFamily.DisplayName}'."); + } + + Cache.CurrentArchitectureFamilyId = existingMapping.ArchitectureFamilyId; + Cache.CurrentArchitectureFamilyName = existingMapping.ArchitectureFamily.DisplayName; + return; + } + + var matchingName = await db.Set() + .FirstOrDefaultAsync(x => x.NormalizedName == normalized, ct); + + if (matchingName != null) + { + if (matchingName.TensorCount != tensorCount || !string.Equals(matchingName.TensorSignatureHash, signatureHash, StringComparison.Ordinal)) + { + throw new InvalidOperationException($"Architecture family '{matchingName.DisplayName}' already exists, but the current model tensor names/count do not match the previously registered architecture. Expected count={matchingName.TensorCount}, actual count={tensorCount}."); + } + + db.Add(new ArchitectureFamilyModelHash + { + ArchitectureFamilyId = matchingName.Id, + AiModelHashId = aiModelHash.Id, + IsCanonical = false + }); + await db.SaveChangesAsync(ct); + + Cache.CurrentArchitectureFamilyId = matchingName.Id; + Cache.CurrentArchitectureFamilyName = matchingName.DisplayName; + AnsiConsole.MarkupLine($"[green]Architecture family linked:[/] [cyan]{Markup.Escape(matchingName.DisplayName)}[/] -> model hash [grey]{Markup.Escape(Cache.CurrentModelId)}[/]"); + return; + } + + var sameSignatureFamilies = await db.Set() + .Where(x => x.TensorCount == tensorCount && x.TensorSignatureHash == signatureHash) + .OrderBy(x => x.DisplayName) + .ToListAsync(ct); + + if (sameSignatureFamilies.Count > 0 && !Cache.AllowArchitectureFamilyAliasOverride) + { + throw new InvalidOperationException( + $"The provided architecture family '{Cache.CurrentArchitectureFamilyName}' matches an existing architecture signature already registered under: {string.Join(", ", sameSignatureFamilies.Select(x => x.DisplayName))}. Use one of those names or rerun with --allow-architecture-family-alias-override if you intentionally want a separate family namespace."); + } + + var family = new ArchitectureFamily + { + NormalizedName = normalized, + DisplayName = Cache.CurrentArchitectureFamilyName.Trim(), + TensorSignatureHash = signatureHash, + TensorCount = tensorCount, + CreatedUtc = DateTime.UtcNow + }; + db.Add(family); + await db.SaveChangesAsync(ct); + + db.Add(new ArchitectureFamilyModelHash + { + ArchitectureFamilyId = family.Id, + AiModelHashId = aiModelHash.Id, + IsCanonical = true + }); + await db.SaveChangesAsync(ct); + + Cache.CurrentArchitectureFamilyId = family.Id; + Cache.CurrentArchitectureFamilyName = family.DisplayName; + AnsiConsole.MarkupLine($"[green]Architecture family created:[/] [cyan]{Markup.Escape(family.DisplayName)}[/] tensors={tensorCount:N0}"); + } + + public static async Task ResolveScopedAiModelHashIdOrNullAsync(MagicQuantContext db, CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + return null; + + var current = await db.AiModelHashes.AsNoTracking().FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + if (current == null) + return null; + + if (Cache.CurrentArchitectureFamilyId == null) + return current.Id; + + var canonical = await db.Set() + .AsNoTracking() + .Where(x => x.ArchitectureFamilyId == Cache.CurrentArchitectureFamilyId.Value) + .OrderByDescending(x => x.IsCanonical) + .ThenBy(x => x.AiModelHashId) + .Select(x => (uint?)x.AiModelHashId) + .FirstOrDefaultAsync(ct); + + return canonical ?? current.Id; + } + + public static async Task ResolveScopedAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct = default) + { + var id = await ResolveScopedAiModelHashIdOrNullAsync(db, ct); + if (id == null) + throw new InvalidOperationException("Unable to resolve the current scoped AiModelHashId."); + return id.Value; + } + + private static string NormalizeFamilyName(string value) => value.Trim().ToLowerInvariant(); + + private static string ComputeTensorSignatureHash(IReadOnlyCollection tensorNames) + { + using var sha = SHA256.Create(); + var payload = string.Join("\n", tensorNames.OrderBy(x => x, StringComparer.Ordinal)); + var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(payload)); + return Convert.ToHexString(hash).ToLowerInvariant(); + } + + private async Task> ReadTensorNamesFromGgufAsync(string ggufPath, CancellationToken ct) + { + string workingDir = Path.Combine(Cache.ModelMagicQuantDirectory ?? Path.GetTempPath(), "_architecture_family"); + Directory.CreateDirectory(workingDir); + string unique = Guid.NewGuid().ToString("N"); + string payloadPath = Path.Combine(workingDir, $"arch_payload_{unique}.json"); + string resultPath = Path.Combine(workingDir, $"arch_result_{unique}.json"); + string scriptPath = Path.Combine(workingDir, $"arch_script_{unique}.py"); + + try + { + await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new { gguf_path = ggufPath, output_path = resultPath }), ct); + const string py = """ +import json +import sys +payload_path = sys.argv[1] +with open(payload_path, 'r', encoding='utf-8') as f: + payload = json.load(f) +try: + import gguf + reader = gguf.GGUFReader(payload['gguf_path']) + names = [t.name for t in reader.tensors] + result = {'TensorNames': names, 'Error': None} +except Exception as e: + result = {'TensorNames': [], 'Error': str(e)} +with open(payload['output_path'], 'w', encoding='utf-8') as f: + json.dump(result, f, indent=2) +"""; + await File.WriteAllTextAsync(scriptPath, py, ct); + await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); + using var stream = File.OpenRead(resultPath); + using var doc = await JsonDocument.ParseAsync(stream, cancellationToken: ct); + var root = doc.RootElement; + var err = root.TryGetProperty("Error", out var e) && e.ValueKind != JsonValueKind.Null ? e.GetString() : null; + if (!string.IsNullOrWhiteSpace(err)) + throw new InvalidOperationException($"Failed to read GGUF tensor names for architecture family validation: {err}"); + return root.GetProperty("TensorNames").EnumerateArray() + .Select(x => x.GetString() ?? string.Empty) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + } + finally + { + TryDelete(payloadPath); + TryDelete(resultPath); + TryDelete(scriptPath); + } + } + + private static void TryDelete(string path) + { + try { if (File.Exists(path)) File.Delete(path); } catch { } + } +} diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 525dc71..45717bf 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -495,7 +495,8 @@ private static string BuildQuantizedModelFingerprint(string quantizationKey) throw new InvalidOperationException("Cache.CurrentModelId is not set."); string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixIdentityHash ?? "imatrix-unknown") : "no-imatrix"; - return $"model:{Cache.CurrentModelId}|imatrix:{imatrix}|quant:{quantizationKey}"; + string family = string.IsNullOrWhiteSpace(Cache.CurrentArchitectureFamilyName) ? Cache.CurrentModelId : Cache.CurrentArchitectureFamilyNormalizedName; + return $"family:{family}|model:{Cache.CurrentModelId}|imatrix:{imatrix}|quant:{quantizationKey}"; } private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct) @@ -504,12 +505,16 @@ private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext throw new InvalidOperationException("Cache.CurrentModelId is not set."); var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - if (model != null) - return model.Id; + if (model == null) + { + model = new AiModelHash { UniqueHash = Cache.CurrentModelId }; + db.AiModelHashes.Add(model); + await db.SaveChangesAsync(ct); + } + + if (Cache.CurrentArchitectureFamilyId != null) + return await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); - model = new AiModelHash { UniqueHash = Cache.CurrentModelId }; - db.AiModelHashes.Add(model); - await db.SaveChangesAsync(ct); return model.Id; } @@ -1033,6 +1038,12 @@ await SaveBenchmarkToDbAsync( await db.SaveChangesAsync(ct); } + if (Cache.CurrentArchitectureFamilyId != null) + { + uint scopedId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); + aiModelHash = await db.AiModelHashes.FirstAsync(x => x.Id == scopedId, ct); + } + var tensorCombo = await GetOrCreateTensorComboAsync(db, quantConfig, ct); var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHash.Id, createIfMissing: true, ct); @@ -2019,4 +2030,4 @@ private sealed record ExecutionPlanCacheKey( string QuantizationKey, int DiscoveryTokenTarget, string PlanModelPath); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/ImatrixIdentityService.cs b/MagicQuant/Services/ImatrixIdentityService.cs index ad1f51f..04ad325 100644 --- a/MagicQuant/Services/ImatrixIdentityService.cs +++ b/MagicQuant/Services/ImatrixIdentityService.cs @@ -35,8 +35,12 @@ public static class ImatrixIdentityService if (string.IsNullOrWhiteSpace(identityHash)) return null; + var scopedAiModelHashId = Cache.CurrentArchitectureFamilyId != null + ? await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct) + : aiModelHashId; + var existing = await db.ImatrixDefinitions - .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHashId && x.IdentityHash == identityHash, ct); + .FirstOrDefaultAsync(x => x.AiModelHashId == scopedAiModelHashId && x.IdentityHash == identityHash, ct); if (existing != null) return existing.Id; @@ -46,7 +50,7 @@ public static class ImatrixIdentityService var row = new ImatrixDefinition { - AiModelHashId = aiModelHashId, + AiModelHashId = scopedAiModelHashId, IdentityHash = identityHash, CanonicalPath = Cache.ActiveImatrixPath, SourceKind = "runtime-active", @@ -59,4 +63,4 @@ public static class ImatrixIdentityService await db.SaveChangesAsync(ct); return row.Id; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index ec55168..6ab790a 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -531,11 +531,11 @@ private static int GetCandidateSafetyScore(BaselineQuants candidate) { await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - if (model == null) + var scopedAiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) return null; - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); var lookup = (TensorConfig)quant; var row = await db.AiBenchmarks @@ -545,7 +545,7 @@ private static int GetCandidateSafetyScore(BaselineQuants candidate) c => c.Id, (b, c) => new { b, c }) .FirstOrDefaultAsync(x => - x.b.AiModelHashId == model.Id && + x.b.AiModelHashId == scopedAiModelHashId.Value && x.b.ImatrixDefinitionId == imatrixDefinitionId && x.c.BaseQuant == lookup.BaseQuant && x.c.Embeddings == lookup.Embeddings && diff --git a/MagicQuant/Services/IsolationPlanningService.cs b/MagicQuant/Services/IsolationPlanningService.cs index c862d60..35b3167 100644 --- a/MagicQuant/Services/IsolationPlanningService.cs +++ b/MagicQuant/Services/IsolationPlanningService.cs @@ -16,4 +16,4 @@ public RequiredSampleGenerationResult BuildContinuationPlan(IEnumerable gr public List BuildRequiredStartupCombos(List? missingTensorGroups = null) => TensorConfigGenerator.GenerateRequiredDataSampleCombos(missingTensorGroups); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/LearnedBaselinePruningService.cs b/MagicQuant/Services/LearnedBaselinePruningService.cs index 0e950f2..e29779c 100644 --- a/MagicQuant/Services/LearnedBaselinePruningService.cs +++ b/MagicQuant/Services/LearnedBaselinePruningService.cs @@ -42,4 +42,4 @@ public Task AnalyzeAndApplyAsync(CancellationToken result.Notes.Add("Learned-baseline early pruning is disabled. No candidates were removed from the search space."); return Task.FromResult(result); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 439c0dc..6580186 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -197,16 +197,15 @@ public async Task ProcessHybridBatchAsync( } var remainingPlans = plans.Except(learnableBaselinePlans).ToList(); + var equivalenceMap = await BuildIsolationDeduplicationPlanAsync(remainingPlans, ct); - await Parallel.ForEachAsync( - remainingPlans, - new ParallelOptions - { - MaxDegreeOfParallelism = _maxConcurrentQuantizations, - CancellationToken = ct - }, - async (plan, token) => + foreach (var plan in remainingPlans) + { + if (equivalenceMap.TryGetValue(plan.Key, out var cloneSourceKey) && + !string.IsNullOrWhiteSpace(cloneSourceKey) && + !string.Equals(cloneSourceKey, plan.Key, StringComparison.Ordinal)) { + var sourcePlan = remainingPlans.First(x => string.Equals(x.Key, cloneSourceKey, StringComparison.Ordinal)); var record = new SampleProcessingRecord { Plan = plan, @@ -215,32 +214,32 @@ await Parallel.ForEachAsync( try { - var state = await ProcessHybridQuantAsync(plan.Quant, token); - record.State = state; - - var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, token); - record.TensorComboId = identity.TensorComboId; - record.BenchmarkId = identity.BenchmarkId; - - switch (state) + bool cloned = await CloneEquivalentIsolationBenchmarkAsync(sourcePlan, plan, ct); + if (cloned) + { + var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, ct); + record.State = SampleProcessState.Completed; + record.TensorComboId = identity.TensorComboId; + record.BenchmarkId = identity.BenchmarkId; + completed++; + } + else { - case SampleProcessState.Completed: - Interlocked.Increment(ref completed); - break; - case SampleProcessState.Skipped: - Interlocked.Increment(ref skipped); - break; - default: - Interlocked.Increment(ref failed); - break; + var state = await ProcessHybridQuantAsync(plan.Quant, ct); + record.State = state; + var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, ct); + record.TensorComboId = identity.TensorComboId; + record.BenchmarkId = identity.BenchmarkId; + if (state == SampleProcessState.Completed) completed++; + else if (state == SampleProcessState.Skipped) skipped++; + else failed++; } } catch (Exception ex) { record.State = SampleProcessState.Failed; record.Error = ex.Message; - Interlocked.Increment(ref failed); - + failed++; AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(record.ModelName)}"); AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); } @@ -248,7 +247,42 @@ await Parallel.ForEachAsync( { records.Add(record); } - }); + + continue; + } + + var recordPrimary = new SampleProcessingRecord + { + Plan = plan, + ModelName = GenerateHybridName(plan.Quant) + }; + + try + { + var state = await ProcessHybridQuantAsync(plan.Quant, ct); + recordPrimary.State = state; + + var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, ct); + recordPrimary.TensorComboId = identity.TensorComboId; + recordPrimary.BenchmarkId = identity.BenchmarkId; + + if (state == SampleProcessState.Completed) completed++; + else if (state == SampleProcessState.Skipped) skipped++; + else failed++; + } + catch (Exception ex) + { + recordPrimary.State = SampleProcessState.Failed; + recordPrimary.Error = ex.Message; + failed++; + AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(recordPrimary.ModelName)}"); + AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); + } + finally + { + records.Add(recordPrimary); + } + } return new SampleProcessingSummary { @@ -271,14 +305,12 @@ await Parallel.ForEachAsync( await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes - .AsNoTracking() - .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (model == null) + if (scopedAiModelHashId == null) return (null, null); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); var comboId = await db.TensorCombos .AsNoTracking() @@ -301,7 +333,7 @@ await Parallel.ForEachAsync( var benchmarkId = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == comboId) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == comboId) .Select(x => x.Id) .FirstOrDefaultAsync(ct); @@ -378,8 +410,25 @@ public async Task ProcessHybridQuantAsync( benchmarkModelPath = quantPath; if (!File.Exists(quantPath) || forceBaselineRelearn) { - AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); - quantizationReport = await RunLlamaQuantizeAsync(inputPath, quantPath, quant); + var quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline + ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) + : quant; + + var effectiveInputPath = quant.BaseQuant.IsExternalRepositoryBaseline + ? await EnsureBaseModelFileAsync() + : inputPath; + + if (quant.BaseQuant.IsExternalRepositoryBaseline) + { + AnsiConsole.MarkupLine( + $"[cyan]Building sample:[/] {Markup.Escape(modelName)} [grey](native input, surrogate carrier={Markup.Escape(quantToExecute.BaseQuant.Names[0])})[/]"); + } + else + { + AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); + } + + quantizationReport = await RunLlamaQuantizeAsync(effectiveInputPath, quantPath, quantToExecute); } } } @@ -468,6 +517,12 @@ private async Task GetEffectiveInputModelPathAsync(HybridQuant quant, bo if (!quant.BaseQuant.IsExternalRepositoryBaseline) return basePath; + // Pure external baselines are downloaded so MagicQuant can learn their tensor truth. + // Any continuation / isolation / hybrid that uses that external baseline must rebuild + // from the native base GGUF instead of requantizing the staged external GGUF. + if (quant.Tensors.Count > 0) + return basePath; + string externalPath = GetExternalBaselineCachePath(quant.BaseQuant); await _huggingFaceBaselineService.DownloadBaselineAsync(quant.BaseQuant, externalPath, forceRefresh, ct); await ValidateExternalBaselineTensorParityOrThrow(basePath, externalPath); @@ -513,16 +568,14 @@ private async Task HasLearnedTruthForBaselineAsync(BaselineQuants baseline return false; await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes - .AsNoTracking() - .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (model == null) + if (scopedAiModelHashId == null) return false; var query = db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) .Where(x => x.BaselineCanonicalKey == baseline.CanonicalKey); if (baseline.DefaultTensorScheme != null) @@ -625,9 +678,9 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - if (model == null) - throw new InvalidOperationException("Unable to persist learned mappings because AiModelHash row was not found."); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + throw new InvalidOperationException("Unable to persist learned mappings because scoped AiModelHash row was not found."); var combo = await db.TensorCombos .AsNoTracking() @@ -635,10 +688,10 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); @@ -647,7 +700,7 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( throw new InvalidOperationException($"Unable to persist learned mappings because no AiBenchmark exists for rebuilt baseline '{quant.BaseQuant.Names[0]}'."); await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == model.Id && + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && x.TensorWeightSchemeId == tensorScheme.UniqueId) .ExecuteDeleteAsync(ct); @@ -662,7 +715,7 @@ await db.LearnedBaselineTensorQuants { Id = Guid.NewGuid(), AiBenchmarkId = benchmarkId.Value, - AiModelHashId = model.Id, + AiModelHashId = scopedAiModelHashId.Value, BaselineQuantId = quant.BaseQuant.UniqueId, TensorWeightSchemeId = tensorScheme.UniqueId, TensorGroupId = match.PrimaryGroup?.UniqueId ?? UnknownTensorGroupId, @@ -721,18 +774,16 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes - .AsNoTracking() - .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (model == null) + if (scopedAiModelHashId == null) return false; - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); var bench = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId) .Join( db.TensorCombos.AsNoTracking(), benchmark => benchmark.TensorComboId, @@ -767,6 +818,17 @@ private static TensorConfig BuildTensorLookup(HybridQuant quant) return (TensorConfig)quant; } + private static async Task ResolveCurrentScopedAiModelHashIdOrNullAsync(MagicQuantContext db, CancellationToken ct) + { + return await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); + } + + private static async Task ResolveCurrentScopedAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct) + { + return await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); + } + + private async Task PersistQuantizationRunAsync( HybridQuant quant, int? imatrixDefinitionId, @@ -798,6 +860,10 @@ private async Task PersistQuantizationRunAsync( await db.SaveChangesAsync(ct); } + uint scopedAiModelHashId = Cache.CurrentArchitectureFamilyId != null + ? await ResolveCurrentScopedAiModelHashIdAsync(db, ct) + : aiModelHash.Id; + var tensorCombo = await db.TensorCombos.FirstOrDefaultAsync(x => x.BaseQuant == lookup.BaseQuant && x.Embeddings == lookup.Embeddings && @@ -817,17 +883,17 @@ private async Task PersistQuantizationRunAsync( await db.SaveChangesAsync(ct); } - imatrixDefinitionId ??= await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHash.Id, createIfMissing: true, ct); + imatrixDefinitionId ??= await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId, createIfMissing: true, ct); Guid? aiBenchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == aiModelHash.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == tensorCombo.Id) + .Where(x => x.AiModelHashId == scopedAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == tensorCombo.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); var row = new QuantizationRun { Id = Guid.NewGuid(), - AiModelHashId = aiModelHash.Id, + AiModelHashId = scopedAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = tensorCombo.Id, AiBenchmarkId = aiBenchmarkId, @@ -1057,6 +1123,20 @@ private async Task RunLlamaQuantizeAsync(string inp if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); + if (!string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory)) + { + string fullInput = Path.GetFullPath(inputFile); + string fullExternalRoot = Path.GetFullPath(Cache.ExternalBaselineCacheDirectory); + + if (fullInput.StartsWith(fullExternalRoot, StringComparison.OrdinalIgnoreCase) && + (temporaryCarrierOverrides != null || quant.BaseQuant.IsExternalRepositoryBaseline)) + { + throw new InvalidOperationException( + $"Quantization attempted to use staged external GGUF '{inputFile}' as the carrier input. " + + "External/custom baselines must rebuild from the native base GGUF instead."); + } + } + Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, outputFile); @@ -1150,6 +1230,21 @@ private static string ResolveQuantizeBaseArgument( return quant.BaseQuant.QuantizeBaseArgumentName; } + private static HybridQuant CreateEquivalentStandardCarrierQuantForExternalRebuild(HybridQuant quant) + { + if (!quant.BaseQuant.IsExternalRepositoryBaseline) + return quant; + + var standardFamily = BaselineQuants.ResolveBuiltInStandardBaseline(quant.BaseQuant.QuantizeBaseArgumentName) + ?? BaselineQuants.ResolveBuiltInStandardBaseline(quant.BaseQuant.Names[0]) + ?? throw new InvalidOperationException( + $"Could not resolve a built-in carrier baseline for external baseline '{quant.BaseQuant.Names[0]}' using quantize base name '{quant.BaseQuant.QuantizeBaseArgumentName}'."); + + var clone = quant.Clone(); + clone.BaseQuant = standardFamily; + return clone; + } + private bool ShouldApplyImatrix(HybridQuant quant) { return _imatrixService.ShouldUseImatrixForQuant(quant); @@ -1212,143 +1307,145 @@ public async Task HasNativeSourceLearnedTruthAsync(CancellationToken ct = var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes - .AsNoTracking() - .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (model == null) + if (scopedAiModelHashId == null) return false; return await db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id && + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && x.TensorWeightSchemeId == nativeScheme.UniqueId) .AnyAsync(ct); } public async Task LearnNativeSourceTruthAsync( - string nativeGgufPath, - CancellationToken ct = default) - { - if (string.IsNullOrWhiteSpace(nativeGgufPath) || !File.Exists(nativeGgufPath)) - throw new FileNotFoundException($"Native GGUF path not found for learning: {nativeGgufPath}"); + string nativeGgufPath, + CancellationToken ct = default) +{ + if (string.IsNullOrWhiteSpace(nativeGgufPath) || !File.Exists(nativeGgufPath)) + throw new FileNotFoundException($"Native GGUF path not found for learning: {nativeGgufPath}"); - var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - if (!Cache.ForceRelearnBaselineTensorMappings) + if (!Cache.ForceRelearnBaselineTensorMappings) + { + await using var precheckDb = new MagicQuantContext(); + var precheckScopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(precheckDb, ct); + + if (precheckScopedAiModelHashId != null) { - await using var precheckDb = new MagicQuantContext(); - var existingModel = await precheckDb.AiModelHashes + int existingRows = await precheckDb.LearnedBaselineTensorQuants .AsNoTracking() - .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + .Where(x => x.AiModelHashId == precheckScopedAiModelHashId.Value && + x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + x.TensorWeightSchemeId == nativeScheme.UniqueId) + .CountAsync(ct); - if (existingModel != null) + if (existingRows > 0) { - int existingRows = await precheckDb.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == existingModel.Id && - x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && - x.TensorWeightSchemeId == nativeScheme.UniqueId) - .CountAsync(ct); - - if (existingRows > 0) - { - AnsiConsole.MarkupLine( - $"[grey]Native-source learned truth already exists:[/] [cyan]{existingRows:N0}[/] row(s) for [yellow]{Markup.Escape(nativeScheme.Names[0])}[/]. Skipping relearn. Use [green]--relearn-baseline-mappings[/] to regenerate."); - return; - } + AnsiConsole.MarkupLine( + $"[grey]Native-source learned truth already exists:[/] [cyan]{existingRows:N0}[/] row(s) for [yellow]{Markup.Escape(nativeScheme.Names[0])}[/]. Skipping relearn. Use [green]--relearn-baseline-mappings[/] to regenerate."); + return; } } + } - var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, nativeGgufPath); - var ggufTruth = metadata.TensorTypes - .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); - - var truth = BuildTruthMapWithVerification( - logTruth: new Dictionary(StringComparer.Ordinal), - ggufTruth: ggufTruth, - baselineName: "NATIVE"); + var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, nativeGgufPath); + var ggufTruth = metadata.TensorTypes + .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); - var grouped = AssignGroups(truth.Keys); - var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); - var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).ToList(); + var truth = BuildTruthMapWithVerification( + logTruth: new Dictionary(StringComparer.Ordinal), + ggufTruth: ggufTruth, + baselineName: "NATIVE"); - await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct) - ?? throw new InvalidOperationException("Could not persist native-source learning because AiModelHash row was missing."); + var grouped = AssignGroups(truth.Keys); + var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); + var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).ToList(); - var combo = await db.TensorCombos - .AsNoTracking() - .FirstOrDefaultAsync(x => x.BaseQuant == BaselineQuants.NativeSourceUniqueId && - x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && - x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && - x.MoeExperts == 0 && x.MoeRouter == 0, ct); + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct) + ?? throw new InvalidOperationException("Could not persist native-source learning because scoped AiModelHash row was missing."); - if (combo == null) - throw new InvalidOperationException("Native-source benchmark TensorCombo is missing; benchmark base model first."); + var combo = await db.TensorCombos + .AsNoTracking() + .FirstOrDefaultAsync(x => x.BaseQuant == BaselineQuants.NativeSourceUniqueId && + x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && + x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && + x.MoeExperts == 0 && x.MoeRouter == 0, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + if (combo == null) + throw new InvalidOperationException("Native-source benchmark TensorCombo is missing; benchmark base model first."); - var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) - .OrderByDescending(x => x.Id) - .Select(x => (Guid?)x.Id) - .FirstOrDefaultAsync(ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( + db, + scopedAiModelHashId, + createIfMissing: false, + ct); - if (!benchmarkId.HasValue) - throw new InvalidOperationException("Native-source benchmark row is missing; benchmark base model before native-source learning."); + var benchmarkId = await db.AiBenchmarks + .Where(x => x.AiModelHashId == scopedAiModelHashId && + x.ImatrixDefinitionId == imatrixDefinitionId && + x.TensorComboId == combo.Id) + .OrderByDescending(x => x.Id) + .Select(x => (Guid?)x.Id) + .FirstOrDefaultAsync(ct); - await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == model.Id && - x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && - x.TensorWeightSchemeId == nativeScheme.UniqueId) - .ExecuteDeleteAsync(ct); + if (!benchmarkId.HasValue) + throw new InvalidOperationException("Native-source benchmark row is missing; benchmark base model before native-source learning."); - var rows = truth - .OrderBy(x => x.Key, StringComparer.Ordinal) - .Select(x => - { - var primaryGroup = grouped[x.Key].PrimaryGroup; + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == scopedAiModelHashId && + x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + x.TensorWeightSchemeId == nativeScheme.UniqueId) + .ExecuteDeleteAsync(ct); - return new LearnedBaselineTensorQuant - { - Id = Guid.NewGuid(), - AiBenchmarkId = benchmarkId.Value, - AiModelHashId = model.Id, - BaselineQuantId = BaselineQuants.NativeSourceUniqueId, - TensorWeightSchemeId = nativeScheme.UniqueId, - TensorGroupId = primaryGroup?.UniqueId ?? UnknownTensorGroupId, - TensorName = x.Key, - FinalQuantType = x.Value.FinalQuantType - }; - }) - .ToList(); + var rows = truth + .OrderBy(x => x.Key, StringComparer.Ordinal) + .Select(x => + { + var primaryGroup = grouped[x.Key].PrimaryGroup; - if (rows.Count == 0) - throw new InvalidOperationException("Native-source learning produced no persistable rows."); + return new LearnedBaselineTensorQuant + { + Id = Guid.NewGuid(), + AiBenchmarkId = benchmarkId.Value, + AiModelHashId = scopedAiModelHashId, + BaselineQuantId = BaselineQuants.NativeSourceUniqueId, + TensorWeightSchemeId = nativeScheme.UniqueId, + TensorGroupId = primaryGroup?.UniqueId ?? UnknownTensorGroupId, + TensorName = x.Key, + FinalQuantType = x.Value.FinalQuantType + }; + }) + .ToList(); - db.LearnedBaselineTensorQuants.AddRange(rows); - await db.SaveChangesAsync(ct); + if (rows.Count == 0) + throw new InvalidOperationException("Native-source learning produced no persistable rows."); - await WriteLearningDiagnosticArtifactAsync( - baselineName: $"NATIVE_{nativeScheme.Names[0]}", - schemeName: nativeScheme.Names[0], - truthByTensor: truth, - grouped: grouped, - allTensorNamesInModel: metadata.TensorNames, - ambiguous: ambiguous, - unresolved: unresolved); + db.LearnedBaselineTensorQuants.AddRange(rows); + await db.SaveChangesAsync(ct); - var sourcePrecision = nativeScheme.Names[0]; - var distribution = rows.GroupBy(x => x.FinalQuantType) - .OrderByDescending(g => g.Count()) - .Select(g => $"{g.Key}:{g.Count()}") - .ToList(); + await WriteLearningDiagnosticArtifactAsync( + baselineName: $"NATIVE_{nativeScheme.Names[0]}", + schemeName: nativeScheme.Names[0], + truthByTensor: truth, + grouped: grouped, + allTensorNamesInModel: metadata.TensorNames, + ambiguous: ambiguous, + unresolved: unresolved); + + var sourcePrecision = nativeScheme.Names[0]; + var distribution = rows.GroupBy(x => x.FinalQuantType) + .OrderByDescending(g => g.Count()) + .Select(g => $"{g.Key}:{g.Count()}") + .ToList(); - AnsiConsole.MarkupLine( - $"[green]Native-source learned truth:[/] precision={Markup.Escape(sourcePrecision)}, tensors={rows.Count}, unresolved={unresolved.Count}, ambiguous={ambiguous.Count}, dist={Markup.Escape($"[{string.Join(", ", distribution)}]")}"); - } + AnsiConsole.MarkupLine( + $"[green]Native-source learned truth:[/] precision={Markup.Escape(sourcePrecision)}, tensors={rows.Count}, unresolved={unresolved.Count}, ambiguous={ambiguous.Count}, dist={Markup.Escape($"[{string.Join(", ", distribution)}]")}"); +} private static bool IsLearnableBaselineRun(HybridQuant quant) { @@ -1408,9 +1505,9 @@ private async Task LearnAndPersistBaselineTensorMapAsync( await using var db = new MagicQuantContext(); - var model = await db.AiModelHashes.FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - if (model == null) - throw new InvalidOperationException("Unable to persist learned mappings because AiModelHash row was not found."); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + throw new InvalidOperationException("Unable to persist learned mappings because scoped AiModelHash row was not found."); var combo = await db.TensorCombos .AsNoTracking() @@ -1418,10 +1515,10 @@ private async Task LearnAndPersistBaselineTensorMapAsync( x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, model.Id, createIfMissing: false, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); @@ -1430,7 +1527,7 @@ private async Task LearnAndPersistBaselineTensorMapAsync( throw new InvalidOperationException($"Unable to persist learned mappings because no AiBenchmark exists for baseline '{quant.BaseQuant.Names[0]}'."); await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == model.Id && + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && x.TensorWeightSchemeId == tensorScheme.UniqueId) .ExecuteDeleteAsync(ct); @@ -1445,7 +1542,7 @@ await db.LearnedBaselineTensorQuants { Id = Guid.NewGuid(), AiBenchmarkId = benchmarkId.Value, - AiModelHashId = model.Id, + AiModelHashId = scopedAiModelHashId.Value, BaselineQuantId = quant.BaseQuant.UniqueId, TensorWeightSchemeId = tensorScheme.UniqueId, TensorGroupId = match.PrimaryGroup?.UniqueId ?? UnknownTensorGroupId, @@ -1859,13 +1956,13 @@ private Dictionary TryLoadAllLearnedTensorMappings( bool allowDominantFallback = false) { using var db = new MagicQuantContext(); - var model = db.AiModelHashes.AsNoTracking().FirstOrDefault(x => x.UniqueHash == Cache.CurrentModelId); - if (model == null) + var scopedAiModelHashId = ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db).GetAwaiter().GetResult(); + if (scopedAiModelHashId == null) return new Dictionary(StringComparer.Ordinal); var allRows = db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) .Where(x => x.BaselineCanonicalKey == canonicalBaselineKey) .OrderBy(x => x.TensorName) .ToList(); @@ -1924,16 +2021,14 @@ private Dictionary TryLoadLearnedTensorMapping( { using var db = new MagicQuantContext(); - var model = db.AiModelHashes - .AsNoTracking() - .FirstOrDefault(x => x.UniqueHash == Cache.CurrentModelId); + var scopedAiModelHashId = ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db).GetAwaiter().GetResult(); - if (model == null) + if (scopedAiModelHashId == null) return new Dictionary(StringComparer.Ordinal); var allRows = db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == model.Id) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) .Where(x => x.BaselineCanonicalKey == sourceBaseline.CanonicalKey) .Where(x => x.TensorGroupId == targetGroup.UniqueId) .OrderBy(x => x.TensorName) @@ -2097,6 +2192,207 @@ with open(output_path, "w", encoding="utf-8") as f: } } + +private async Task> BuildIsolationDeduplicationPlanAsync( + IReadOnlyCollection plans, + CancellationToken ct) +{ + var result = new Dictionary(StringComparer.Ordinal); + var firstBySignature = new Dictionary(StringComparer.Ordinal); + + foreach (var plan in plans) + { + string? signature = await TryBuildIsolationEquivalenceKeyAsync(plan, ct); + if (string.IsNullOrWhiteSpace(signature)) + { + result[plan.Key] = plan.Key; + continue; + } + + if (!firstBySignature.TryGetValue(signature, out var firstKey)) + { + firstBySignature[signature] = plan.Key; + result[plan.Key] = plan.Key; + continue; + } + + result[plan.Key] = firstKey; + AnsiConsole.MarkupLine( + $"[grey]Isolation dedupe planned:[/] {Markup.Escape(plan.Key)} -> {Markup.Escape(firstKey)}"); + } + + return result; +} + +private async Task TryBuildIsolationEquivalenceKeyAsync( + RequiredSamplePlan plan, + CancellationToken ct) +{ + if (plan.Kind != RequiredSampleKind.GroupIsolationProbe && + plan.Kind != RequiredSampleKind.GroupIsolationContinuation) + return null; + + if (plan.TargetGroupId == null || string.IsNullOrWhiteSpace(plan.TestedCandidateCanonicalKey)) + return null; + + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + return null; + + var rows = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) + .Where(x => x.BaselineCanonicalKey == plan.TestedCandidateCanonicalKey) + .Where(x => x.TensorGroupId == plan.TargetGroupId.Value) + .OrderBy(x => x.TensorName) + .Select(x => new { x.TensorName, x.FinalQuantType }) + .ToListAsync(ct); + + if (rows.Count == 0) + return null; + + var sb = new StringBuilder(); + sb.Append("group=").Append(plan.TargetGroupId.Value).Append('|'); + foreach (var row in rows) + { + sb.Append(row.TensorName).Append('=') + .Append(NormalizeLearnedIsolationQuantToken(row.FinalQuantType)) + .Append(';'); + } + + return sb.ToString(); +} + +private static string NormalizeLearnedIsolationQuantToken(string value) +{ + if (string.IsNullOrWhiteSpace(value)) + return string.Empty; + + return value.Trim().Replace(" ", string.Empty).Replace("-", "_").ToUpperInvariant(); +} + +private async Task CloneEquivalentIsolationBenchmarkAsync( + RequiredSamplePlan sourcePlan, + RequiredSamplePlan duplicatePlan, + CancellationToken ct) +{ + var sourceIdentity = await ResolveBenchmarkIdentityAsync(sourcePlan.Quant, ct); + if (sourceIdentity.BenchmarkId == null) + return false; + + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + return false; + + var sourceBench = await db.AiBenchmarks + .Include(x => x.CategorBenchmarks) + .FirstOrDefaultAsync(x => x.Id == sourceIdentity.BenchmarkId.Value, ct); + + if (sourceBench == null) + return false; + + var duplicateLookup = BuildTensorLookup(duplicatePlan.Quant); + var duplicateCombo = await db.TensorCombos.FirstOrDefaultAsync(x => + x.BaseQuant == duplicateLookup.BaseQuant && + x.Embeddings == duplicateLookup.Embeddings && + x.LmHead == duplicateLookup.LmHead && + x.AttnQ == duplicateLookup.AttnQ && + x.AttnKV == duplicateLookup.AttnKV && + x.AttnOutput == duplicateLookup.AttnOutput && + x.FfnUpGate == duplicateLookup.FfnUpGate && + x.FfnDown == duplicateLookup.FfnDown && + x.MoeExperts == duplicateLookup.MoeExperts && + x.MoeRouter == duplicateLookup.MoeRouter, ct); + + if (duplicateCombo == null) + { + duplicateCombo = new TensorCombo(duplicateLookup); + db.TensorCombos.Add(duplicateCombo); + await db.SaveChangesAsync(ct); + } + + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( + db, + scopedAiModelHashId.Value, + createIfMissing: true, + ct); + + var existing = await db.AiBenchmarks + .FirstOrDefaultAsync(x => x.AiModelHashId == scopedAiModelHashId.Value && + x.ImatrixDefinitionId == imatrixDefinitionId && + x.TensorComboId == duplicateCombo.Id, ct); + + if (existing != null) + return true; + + var clonedBenchmark = new AiBenchmark + { + Id = Guid.NewGuid(), + Ngl = sourceBench.Ngl, + SizeBytes = sourceBench.SizeBytes, + TokensPerSecond = sourceBench.TokensPerSecond, + TensorComboId = duplicateCombo.Id, + AiModelHashId = scopedAiModelHashId.Value, + ImatrixDefinitionId = imatrixDefinitionId + }; + db.AiBenchmarks.Add(clonedBenchmark); + + var clonedCategories = sourceBench.CategorBenchmarks + .Select(x => new CategoryBenchmark + { + Id = Guid.NewGuid(), + AiBenchmarkId = clonedBenchmark.Id, + Category = x.Category, + Kld = x.Kld, + Ppl = x.Ppl, + PplError = x.PplError + }) + .ToList(); + db.AddRange(clonedCategories); + + db.QuantizationRuns.Add(new QuantizationRun + { + Id = Guid.NewGuid(), + AiModelHashId = scopedAiModelHashId.Value, + ImatrixDefinitionId = imatrixDefinitionId, + TensorComboId = duplicateCombo.Id, + AiBenchmarkId = clonedBenchmark.Id, + StartedUtc = DateTime.UtcNow, + CompletedUtc = DateTime.UtcNow, + DurationMs = 0, + Succeeded = true, + Error = $"Cloned from equivalent isolation benchmark '{sourcePlan.Key}'.", + OutputModelPath = null + }); + + foreach (var cat in clonedCategories) + { + db.BenchmarkRuns.Add(new BenchmarkRun + { + Id = Guid.NewGuid(), + AiModelHashId = scopedAiModelHashId.Value, + ImatrixDefinitionId = imatrixDefinitionId, + TensorComboId = duplicateCombo.Id, + AiBenchmarkId = clonedBenchmark.Id, + CategoryBenchmarkId = cat.Id, + Category = cat.Category, + StartedUtc = DateTime.UtcNow, + CompletedUtc = DateTime.UtcNow, + DurationMs = 0, + Succeeded = true, + Error = $"Cloned from equivalent isolation benchmark '{sourcePlan.Key}'." + }); + } + + await db.SaveChangesAsync(ct); + + AnsiConsole.MarkupLine( + $"[green]Isolation dedupe clone:[/] {Markup.Escape(duplicatePlan.Key)} reused benchmark data from {Markup.Escape(sourcePlan.Key)}"); + return true; +} + // ---------------------------------------------------------------- // Internal DTOs // ---------------------------------------------------------------- diff --git a/config.backup.yaml b/config.backup.yaml new file mode 100644 index 0000000..7def13a --- /dev/null +++ b/config.backup.yaml @@ -0,0 +1,53 @@ +# Default runtime config +identity: + architecture_family_name: + allow_architecture_family_alias_override: false + +paths: + magic_quant_root: + model_dir: + llama_root: + llama_bin: + convert_script: + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_relearn_baseline_tensor_mappings: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: + +evolution: + max_data_collected_per_category: 5 + max_survival_rounds: 4 + collapse_multiplier: 1.5 + brute_force_final_combination_threshold: 2000 + +isolation_pruning: + minimum_isolation_reduction_to_continue_ratio: 0.04 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + +prediction: + manual_max_predicted_size_bytes: 0 + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + custom_repositories: [] diff --git a/config.default.yaml b/config.default.yaml new file mode 100644 index 0000000..7def13a --- /dev/null +++ b/config.default.yaml @@ -0,0 +1,53 @@ +# Default runtime config +identity: + architecture_family_name: + allow_architecture_family_alias_override: false + +paths: + magic_quant_root: + model_dir: + llama_root: + llama_bin: + convert_script: + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_relearn_baseline_tensor_mappings: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: + +evolution: + max_data_collected_per_category: 5 + max_survival_rounds: 4 + collapse_multiplier: 1.5 + brute_force_final_combination_threshold: 2000 + +isolation_pruning: + minimum_isolation_reduction_to_continue_ratio: 0.04 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + +prediction: + manual_max_predicted_size_bytes: 0 + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + custom_repositories: [] diff --git a/config.dev.yaml b/config.dev.yaml new file mode 100644 index 0000000..5605b63 --- /dev/null +++ b/config.dev.yaml @@ -0,0 +1,53 @@ +# Default runtime config +identity: + architecture_family_name: Qwen3-4B-Instruct-2507 + allow_architecture_family_alias_override: false + +paths: + magic_quant_root: + model_dir: + llama_root: + llama_bin: + convert_script: + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_relearn_baseline_tensor_mappings: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: + +evolution: + max_data_collected_per_category: 5 + max_survival_rounds: 4 + collapse_multiplier: 1.5 + brute_force_final_combination_threshold: 2000 + +isolation_pruning: + minimum_isolation_reduction_to_continue_ratio: 0.04 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + +prediction: + manual_max_predicted_size_bytes: 0 + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + custom_repositories: [] From bffffa316cb051b3c8a8f2365e94a8fca520a164 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 22 Apr 2026 20:30:30 -0400 Subject: [PATCH 114/258] Adding in new prediction system. it is NOT working, but it's being honed in. --- MQ.DB/Cache.cs | 8 +- MQ.DB/Models/BaselineQuants.cs | 10 +- MagicQuant/Commands/BuildHybrids.cs | 10 +- MagicQuant/Commands/Evolution.cs | 74 ++--- MagicQuant/Config.cs | 15 + .../Configuration/MagicQuantYamlConfig.cs | 27 +- .../Configuration/MagicQuantYamlLoader.cs | 36 +++ MagicQuant/MagicQuant.csproj | 7 +- MagicQuant/Models/HybridFinalizationModels.cs | 207 +++++++++++++ .../Services/BitRangeBucketBuilderService.cs | 107 +++++++ .../Services/BucketLocalPruningService.cs | 135 +++++++++ .../CombinationSurvivalPipelineService.cs | 229 +++++++++++++++ .../EffectiveCandidateStateResolverService.cs | 153 ++++++++++ .../FinalRealBenchmarkEliminationService.cs | 45 +++ .../FinalSurvivorSelectionCliService.cs | 105 +++++++ .../Services/HybridArtifactExportService.cs | 248 ++++++++++++++++ .../Services/HybridBenchmarkRepository.cs | 271 ++++++++++++++++++ .../Services/HybridMapGenerationService.cs | 50 ++++ .../Services/IsolationOptimizationService.cs | 261 ++++++++++++++++- .../PredictedCandidateEvaluationService.cs | 163 +++++++++++ MagicQuant/Services/QuantizationService.cs | 68 +++++ .../Services/ReadmeGenerationService.cs | 93 ++++++ .../Services/RemainingCombinationStore.cs | 148 ++++++++++ MagicQuant/config.default.yaml | 44 ++- MagicQuant/config.dev.backup.yaml | 50 ---- MagicQuant/config.dev.yaml | 23 +- 26 files changed, 2471 insertions(+), 116 deletions(-) create mode 100644 MagicQuant/Models/HybridFinalizationModels.cs create mode 100644 MagicQuant/Services/BitRangeBucketBuilderService.cs create mode 100644 MagicQuant/Services/BucketLocalPruningService.cs create mode 100644 MagicQuant/Services/CombinationSurvivalPipelineService.cs create mode 100644 MagicQuant/Services/EffectiveCandidateStateResolverService.cs create mode 100644 MagicQuant/Services/FinalRealBenchmarkEliminationService.cs create mode 100644 MagicQuant/Services/FinalSurvivorSelectionCliService.cs create mode 100644 MagicQuant/Services/HybridArtifactExportService.cs create mode 100644 MagicQuant/Services/HybridBenchmarkRepository.cs create mode 100644 MagicQuant/Services/HybridMapGenerationService.cs create mode 100644 MagicQuant/Services/PredictedCandidateEvaluationService.cs create mode 100644 MagicQuant/Services/ReadmeGenerationService.cs create mode 100644 MagicQuant/Services/RemainingCombinationStore.cs delete mode 100644 MagicQuant/config.dev.backup.yaml diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index 6f5db02..7c636ed 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -95,4 +95,10 @@ public enum MainTorchType public static string? ActiveImatrixPath { get; set; } public static string? ActiveImatrixIdentityHash { get; set; } -} \ No newline at end of file + + + /// + /// Final export/output directory for selected survivor artifacts. + /// + public static string? OutputDirectory { get; set; } +} diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index ccafcf1..e14711a 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -93,19 +93,19 @@ private static BaselineQuants Create( Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 8, 11); public static readonly BaselineQuants Q6_K = - Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, true, true, false, 6, 10); + Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, true, false, false, 6, 10); public static readonly BaselineQuants Q5_K = - Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 5, 9); + Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, true, false, false, 5, 9); public static readonly BaselineQuants Q4_K_M = - Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 8); + Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, true, false, false, 4, 8); public static readonly BaselineQuants IQ4_NL = - Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 7); + Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, true, false, false, 4, 7); public static readonly BaselineQuants IQ4_XS = - Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 6); + Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, true, false, false, 4, 6); public static readonly BaselineQuants IQ3_S = Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 5); diff --git a/MagicQuant/Commands/BuildHybrids.cs b/MagicQuant/Commands/BuildHybrids.cs index de1ea75..37b9613 100644 --- a/MagicQuant/Commands/BuildHybrids.cs +++ b/MagicQuant/Commands/BuildHybrids.cs @@ -13,16 +13,14 @@ public async Task Run(List args) return; } - await Task.Yield(); - - throw new NotImplementedException( - "The build-hybrids command is currently disabled. Use `evolution` for active hybrid generation workflows."); + AnsiConsole.MarkupLine("[grey]build-hybrids now routes through the centralized evolution/survival/export pipeline.[/]"); + await new Evolution().Run(args); } private static void ShowHelp() { AnsiConsole.MarkupLine("[bold yellow]Command: build-hybrids[/]"); - AnsiConsole.MarkupLine("Builds/benchmarks remaining hybrid combinations from the current candidate-based search space."); - AnsiConsole.MarkupLine("Usage: mq build-hybrids --model-dir \"\" [--use-imatrix] [--allow-high-precision-hybrids]"); + AnsiConsole.MarkupLine("Runs the centralized survival/export flow over the active MagicQuant evolution pipeline."); + AnsiConsole.MarkupLine("Usage: mq build-hybrids --model-dir \"\" [--output-dir \"\"] [--output-name-prefix \"model\"] [--export-external-learned-baselines]"); } } diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index d9dbe63..396c47c 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -72,10 +72,13 @@ public async Task Run(List args) if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); + Cache.OutputDirectory = ResolveAndValidateOutputDirectory(); + AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); AnsiConsole.Write(new Rule("[yellow]Evolution Configuration[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"Model Path: [blue]{Markup.Escape(Cache.ModelDirectory)}[/]"); - AnsiConsole.MarkupLine($"Output Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); + AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); + AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length:N0}[/] safe tensors"); if (string.IsNullOrEmpty(Cache.LlamaBin)) @@ -316,40 +319,16 @@ await benchmarkService.RunAllBenchmarksAsync( long finalRemainingCombinationCount = await dbService.GetRemainingCombinationCountAsync(); - AnsiConsole.MarkupLine($"[green]Final surviving combinations:[/] {finalRemainingCombinationCount:N0}"); - - int bruteForceFinalCombinationThreshold = Config.BruteForceFinalCombinationThreshold; + AnsiConsole.MarkupLine($"[green]Final surviving combinations after stage-1 pruning:[/] {finalRemainingCombinationCount:N0}"); - if (finalRemainingCombinationCount <= bruteForceFinalCombinationThreshold) - { - AnsiConsole.Write(new Rule("[yellow]Final Brute Force Benchmark Phase[/]") { Justification = Justify.Left }); + var survivalPipeline = new CombinationSurvivalPipelineService(quantizationService); + var finalizationResult = await survivalPipeline.RunAsync(ct: default); - AnsiConsole.MarkupLine( - $"[green]Final combination count[/] [cyan]{finalRemainingCombinationCount:N0}[/] " + - $"is at or below the brute-force threshold of [yellow]{bruteForceFinalCombinationThreshold:N0}[/]."); - - var finalConfigs = await dbService.GetRemainingTensorConfigsAsync(); - var finalQuants = finalConfigs - .Select(x => (HybridQuant)x) - .ToList(); - - var finalSummary = await quantizationService.ProcessHybridBatchAsync(finalQuants); - - AnsiConsole.MarkupLine("[bold green]Final brute force benchmarking complete.[/]"); - AnsiConsole.MarkupLine($" [green]Requested:[/] {finalSummary.Requested:N0}"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {finalSummary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {finalSummary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {finalSummary.Failed:N0}"); - AnsiConsole.MarkupLine("[yellow]Note:[/] Final model creation/export functionality is still being implemented."); - } - else - { - AnsiConsole.MarkupLine("[yellow]Note:[/] Final model creation/export functionality is still being implemented."); - - throw new InvalidOperationException( - $"Prediction engine not created yet. Final surviving combinations were {finalRemainingCombinationCount:N0}, " + - $"which is above the brute-force threshold of {bruteForceFinalCombinationThreshold:N0}."); - } + AnsiConsole.Write(new Rule("[yellow]Export Summary[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[green]Export directory:[/] [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); + AnsiConsole.MarkupLine($"[green]Final brutal survivors:[/] [cyan]{finalizationResult.BrutalSurvivors.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[green]Selected survivors:[/] [cyan]{finalizationResult.SelectedRows.Count(x => x.Enabled):N0}[/]"); + AnsiConsole.MarkupLine($"[green]Exported/linkable artifacts:[/] [cyan]{finalizationResult.ExportedArtifacts.Count:N0}[/]"); } private static void PrintIsolationGroupDecisions(IEnumerable decisions) @@ -428,12 +407,41 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--imatrix-dataset-config[/] Optional dataset config name for HF datasets (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-dataset-local-file[/] Full path to local .json/.jsonl dataset source (Optional)"); AnsiConsole.MarkupLine(" [green]--manual-max-predicted-size-bytes[/] Override late predicted-size pruning ceiling (Optional; 0 = auto Q8 ceiling)"); + AnsiConsole.MarkupLine(" [green]--output-dir[/] Final export/output directory for selected survivor artifacts (Optional; default = /MagicQuant/Final_Outputs)"); + AnsiConsole.MarkupLine(" [green]--output-name-prefix[/] Output filename prefix for exported GGUF files (Optional; default = model)"); + AnsiConsole.MarkupLine(" [green]--export-external-learned-baselines[/] Also locally rebuild/export pure learned external baselines such as Unsloth (Optional; default false)"); + AnsiConsole.MarkupLine(" [green]--max-selected-choices-per-bucket[/] Hard cap for survivors retained per BitRange bucket before brute force (Optional; default = 5)"); AnsiConsole.MarkupLine(" [green]--config[/] Path to YAML runtime config. CLI flags override YAML values."); AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Example:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } + + private static string ResolveAndValidateOutputDirectory() + { + string resolved; + + if (!string.IsNullOrWhiteSpace(Config.Current.Output.OutputDir)) + { + resolved = Path.IsPathRooted(Config.Current.Output.OutputDir) + ? Path.GetFullPath(Config.Current.Output.OutputDir) + : Path.GetFullPath(Path.Combine(Cache.ModelMagicQuantDirectory!, Config.Current.Output.OutputDir)); + } + else + { + resolved = Path.Combine(Cache.ModelMagicQuantDirectory!, "Final_Outputs"); + } + + Directory.CreateDirectory(resolved); + + string probe = Path.Combine(resolved, $".write_test_{Guid.NewGuid():N}.tmp"); + File.WriteAllText(probe, "ok"); + File.Delete(probe); + + return resolved; + } + private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) { await using var db = new MagicQuantContext(); diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index c2cf8e3..863d9e8 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -28,6 +28,21 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.Evolution.BruteForceFinalCombinationThreshold; public static ulong ManualMaxPredictedSizeBytes => Current.Prediction.ManualMaxPredictedSizeBytes; + public static string? OutputDirectory => Current.Output.OutputDir; + public static string OutputNamePrefix => string.IsNullOrWhiteSpace(Current.Output.OutputNamePrefix) + ? "model" + : Current.Output.OutputNamePrefix.Trim(); + + public static bool ExportExternalLearnedBaselines => Current.Output.ExportExternalLearnedBaselines; + + public static int MaxSelectedChoicesPerBucket => Math.Max(1, Current.Survival.MaxSelectedChoicesPerBucket); + public static double SurvivalMeaningfulSizeBiasPercent => Current.Survival.MeaningfulSizeBiasPercent; + public static double SurvivalKldCloseCallAbsoluteEpsilon => Current.Survival.KldCloseCallAbsoluteEpsilon; + public static double SurvivalKldCloseCallRelativeFraction => Current.Survival.KldCloseCallRelativeFraction; + public static double SurvivalPplLargeDifferencePercent => Current.Survival.PplLargeDifferencePercent; + public static double SurvivalTradeScoreSizeBiasWeight => Current.Survival.TradeScoreSizeBiasWeight; + public static double SurvivalTradeScorePplWeight => Current.Survival.TradeScorePplWeight; + public static List SensitivityProbeGroups => Current.SensitivityProbeGroups; public static List SensitivityProbeGroupsMoe => Current.SensitivityProbeGroupsMoe; public static List BrainLayers => Current.BrainLayers; diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index fb27609..0b99982 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -12,6 +12,8 @@ public sealed class MagicQuantYamlConfig public RuntimePredictionConfig Prediction { get; set; } = new(); public RuntimeIdentityConfig Identity { get; set; } = new(); public RuntimeBaselineConfig Baselines { get; set; } = new(); + public RuntimeOutputConfig Output { get; set; } = new(); + public RuntimeSurvivalConfig Survival { get; set; } = new(); public List SensitivityProbeGroups { get; set; } = [ @@ -135,6 +137,24 @@ public sealed class RuntimeIdentityConfig public bool AllowArchitectureFamilyAliasOverride { get; set; } } +public sealed class RuntimeOutputConfig +{ + public string? OutputDir { get; set; } + public string OutputNamePrefix { get; set; } = "model"; + public bool ExportExternalLearnedBaselines { get; set; } = false; +} + +public sealed class RuntimeSurvivalConfig +{ + public int MaxSelectedChoicesPerBucket { get; set; } = 5; + public double MeaningfulSizeBiasPercent { get; set; } = 1.0d; + public double KldCloseCallAbsoluteEpsilon { get; set; } = 0.00075d; + public double KldCloseCallRelativeFraction { get; set; } = 0.02d; + public double PplLargeDifferencePercent { get; set; } = 0.75d; + public double TradeScoreSizeBiasWeight { get; set; } = 1.25d; + public double TradeScorePplWeight { get; set; } = 0.15d; +} + public sealed class RuntimeBaselineConfig { public string StandardBaselinesMode { get; set; } = "all"; @@ -151,7 +171,12 @@ public sealed class CustomBaselineRepositoryConfig { public string RepoId { get; set; } = string.Empty; public string? ShortSourceName { get; set; } + public string SourceKind { get; set; } = "huggingface_gguf_repository"; public bool Enabled { get; set; } = true; + public bool RequireAllIncludesToResolve { get; set; } = true; + public bool ValidateTensorNamesAgainstSourceModel { get; set; } = true; + public bool DeletePartialOrDirtyDownloads { get; set; } = true; + public bool ResumeOrRetryDownloads { get; set; } = true; public bool AllowAsCombinationCarrier { get; set; } public bool AllowAsExplicitGroupCandidate { get; set; } = true; public bool AllowAsLearningBaseline { get; set; } = true; @@ -187,4 +212,4 @@ public sealed class ResolvedCustomBaselineSpec public bool AllowAsCombinationCarrier { get; set; } public bool AllowAsExplicitGroupCandidate { get; set; } public IReadOnlyList BannedGroupIds { get; set; } = Array.Empty(); -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index da78ada..2e00638 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -83,6 +83,17 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) Cache.CurrentArchitectureFamilyId = null; + config.Output.OutputDir = string.IsNullOrWhiteSpace(config.Output.OutputDir) + ? null + : config.Output.OutputDir.Trim(); + + config.Output.OutputNamePrefix = string.IsNullOrWhiteSpace(config.Output.OutputNamePrefix) + ? "model" + : config.Output.OutputNamePrefix.Trim(); + + if (config.Survival.MaxSelectedChoicesPerBucket <= 0) + throw new InvalidOperationException("survival.max_selected_choices_per_bucket must be greater than 0."); + ApplyStandardBaselineFilters(config.Baselines); BaselineQuants.ResetDynamicCustomBaselines(); } @@ -162,6 +173,31 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (ulong.TryParse(Get("manual-max-predicted-size-bytes"), out var manualBytes)) config.Prediction.ManualMaxPredictedSizeBytes = manualBytes; + config.Output.OutputDir = Prefer(Get("output-dir"), config.Output.OutputDir); + config.Output.OutputNamePrefix = Prefer(Get("output-name-prefix"), config.Output.OutputNamePrefix); + if (Has("export-external-learned-baselines")) config.Output.ExportExternalLearnedBaselines = true; + + if (int.TryParse(Get("max-selected-choices-per-bucket"), out var maxSelectedChoicesPerBucket) && maxSelectedChoicesPerBucket > 0) + config.Survival.MaxSelectedChoicesPerBucket = maxSelectedChoicesPerBucket; + + if (double.TryParse(Get("survival-meaningful-size-bias-percent"), out var sizeBiasPercent) && sizeBiasPercent >= 0d) + config.Survival.MeaningfulSizeBiasPercent = sizeBiasPercent; + + if (double.TryParse(Get("survival-kld-close-call-absolute-epsilon"), out var kldCloseCallAbs) && kldCloseCallAbs >= 0d) + config.Survival.KldCloseCallAbsoluteEpsilon = kldCloseCallAbs; + + if (double.TryParse(Get("survival-kld-close-call-relative-fraction"), out var kldCloseCallRelative) && kldCloseCallRelative >= 0d) + config.Survival.KldCloseCallRelativeFraction = kldCloseCallRelative; + + if (double.TryParse(Get("survival-ppl-large-difference-percent"), out var pplLargeDiff) && pplLargeDiff >= 0d) + config.Survival.PplLargeDifferencePercent = pplLargeDiff; + + if (double.TryParse(Get("survival-trade-score-size-bias-weight"), out var sizeWeight) && sizeWeight >= 0d) + config.Survival.TradeScoreSizeBiasWeight = sizeWeight; + + if (double.TryParse(Get("survival-trade-score-ppl-weight"), out var pplWeight) && pplWeight >= 0d) + config.Survival.TradeScorePplWeight = pplWeight; + config.Identity.ArchitectureFamilyName = Prefer(Get("architecture-family"), config.Identity.ArchitectureFamilyName); if (Has("allow-architecture-family-alias-override")) config.Identity.AllowArchitectureFamilyAliasOverride = true; } diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index da4a682..8352882 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -21,13 +21,10 @@ PreserveNewest - PreserveNewest + Always - PreserveNewest - - - PreserveNewest + Always diff --git a/MagicQuant/Models/HybridFinalizationModels.cs b/MagicQuant/Models/HybridFinalizationModels.cs new file mode 100644 index 0000000..ba252a7 --- /dev/null +++ b/MagicQuant/Models/HybridFinalizationModels.cs @@ -0,0 +1,207 @@ +using System.Security.Cryptography; +using System.Text; +using MQ.DB.Models; + +namespace MagicQuant.Models; + +public static class TensorConfigIdentity +{ + public static string ToKey(TensorConfig config) + { + return string.Join(":", + config.BaseQuant, + config.Embeddings, + config.LmHead, + config.AttnQ, + config.AttnKV, + config.AttnOutput, + config.FfnUpGate, + config.FfnDown, + config.MoeExperts, + config.MoeRouter); + } + + public static bool IsPureBaseline(TensorConfig config) + { + return config.Embeddings == BaselineQuants.TensorConfigNullSlotValue && + config.LmHead == BaselineQuants.TensorConfigNullSlotValue && + config.AttnQ == BaselineQuants.TensorConfigNullSlotValue && + config.AttnKV == BaselineQuants.TensorConfigNullSlotValue && + config.AttnOutput == BaselineQuants.TensorConfigNullSlotValue && + config.FfnUpGate == BaselineQuants.TensorConfigNullSlotValue && + config.FfnDown == BaselineQuants.TensorConfigNullSlotValue && + config.MoeExperts == BaselineQuants.TensorConfigNullSlotValue && + config.MoeRouter == BaselineQuants.TensorConfigNullSlotValue; + } + + public static IReadOnlyList<(TensorGroup Group, byte StoredValue)> EnumerateGroupSlots(TensorConfig config) + { + return + [ + (TReg.Embeddings, config.Embeddings), + (TReg.LmHead, config.LmHead), + (TReg.AttnQ, config.AttnQ), + (TReg.AttnKV, config.AttnKV), + (TReg.AttnOutput, config.AttnOutput), + (TReg.FfnUpGate, config.FfnUpGate), + (TReg.FfnDown, config.FfnDown), + (TReg.MoeExperts, config.MoeExperts), + (TReg.MoeRouter, config.MoeRouter) + ]; + } + + public static string StableHash(string value) + { + var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(value)); + return Convert.ToHexString(bytes).ToLowerInvariant(); + } +} + +public sealed class EffectiveStateResolutionResult +{ + public TensorConfig Config { get; init; } + public string EffectiveStateKey { get; init; } = string.Empty; + public bool HasUnknownMappings { get; init; } + public IReadOnlyList Warnings { get; init; } = Array.Empty(); + public IReadOnlyDictionary GroupStates { get; init; } = new Dictionary(StringComparer.Ordinal); + public string BaseState { get; init; } = string.Empty; +} + +public sealed class PredictedCandidateEvaluation +{ + public TensorConfig Config { get; init; } + public HybridQuant Quant { get; init; } = default!; + public ulong PredictedSizeBytes { get; init; } + public double PredictedKldCost { get; init; } + public double PredictedPplCost { get; init; } + public double CompositeScore { get; init; } + public string EffectiveStateKey { get; init; } = string.Empty; + public bool HasUnknownMappings { get; init; } + public byte BaseBitRange { get; init; } + public bool IsPureBaseline { get; init; } + public List Notes { get; init; } = new(); +} + +public sealed class BitRangeBucketDefinition +{ + public string Key { get; init; } = string.Empty; + public byte LowerBitRange { get; init; } + public byte UpperBitRange { get; init; } + public ulong LowerAnchorSizeBytes { get; init; } + public ulong UpperAnchorSizeBytes { get; init; } + public bool WasSkipped { get; init; } + public string? SkipReason { get; init; } +} + +public sealed class BucketedCandidate +{ + public PredictedCandidateEvaluation Evaluation { get; init; } = default!; + public BitRangeBucketDefinition Bucket { get; init; } = default!; +} + +public sealed class BucketPruneDiagnostics +{ + public string BucketKey { get; init; } = string.Empty; + public ulong LowerAnchorSizeBytes { get; init; } + public ulong UpperAnchorSizeBytes { get; init; } + public int IncomingCount { get; set; } + public int RemovedCount { get; set; } + public int KeptCount { get; set; } + public bool Skipped { get; set; } + public string? SkipReason { get; set; } + public Dictionary RemovalReasons { get; } = new(StringComparer.OrdinalIgnoreCase); + public List Notes { get; } = new(); + + public void CountReason(string reason) + { + RemovalReasons.TryGetValue(reason, out var current); + RemovalReasons[reason] = current + 1; + } +} + +public sealed class SurvivalStageReport +{ + public long StartingCount { get; set; } + public long EndingCount { get; set; } + public long RemovedCount => StartingCount - EndingCount; + public Dictionary RemovalCounts { get; } = new(StringComparer.OrdinalIgnoreCase); + public List BucketDiagnostics { get; } = new(); + public List Notes { get; } = new(); + + public void AddRemoval(string reason, long count) + { + RemovalCounts.TryGetValue(reason, out var current); + RemovalCounts[reason] = current + count; + } +} + +public sealed class BenchmarkSnapshotRecord +{ + public TensorConfig Config { get; init; } + public HybridQuant Quant { get; init; } = default!; + public string DisplayName { get; init; } = string.Empty; + public string ProviderName { get; init; } = string.Empty; + public string BaselineFamily { get; init; } = string.Empty; + public bool IsHybrid { get; init; } + public bool IsExternalPureBaseline { get; init; } + public ulong SizeBytes { get; init; } + public double Kld { get; init; } + public double Ppl { get; init; } + public string? OutputModelPath { get; init; } + public string? ExternalRepositoryUrl { get; init; } +} + +public sealed class FinalSelectionRow +{ + public int Id { get; set; } + public bool Enabled { get; set; } = true; + public BenchmarkSnapshotRecord Snapshot { get; init; } = default!; +} + +public sealed class ExportedArtifactRecord +{ + public BenchmarkSnapshotRecord Snapshot { get; init; } = default!; + public string DisplayName { get; init; } = string.Empty; + public string ProviderName { get; init; } = string.Empty; + public string BaselineFamily { get; init; } = string.Empty; + public bool IsExternalReference { get; init; } + public string? FileName { get; init; } + public string? FullPath { get; init; } + public string DownloadTarget { get; init; } = string.Empty; + public ulong ExpectedSizeBytes { get; init; } + public ulong? ActualSizeBytes { get; init; } + public EffectiveStateResolutionResult? EffectiveState { get; init; } +} + +public sealed class HybridMapEntry +{ + public string ExportedFileName { get; set; } = string.Empty; + public string DisplayName { get; set; } = string.Empty; + public string ProviderSource { get; set; } = string.Empty; + public string BaselineFamily { get; set; } = string.Empty; + public string? OriginalReferenceBaseline { get; set; } + public Dictionary TensorGroups { get; set; } = new(StringComparer.Ordinal); + public string EffectiveQuantStateKey { get; set; } = string.Empty; + public bool HasUnknownMappings { get; set; } + public List Warnings { get; set; } = new(); + public bool UsedImatrix { get; set; } + public ulong ExpectedSizeBytes { get; set; } + public ulong? ActualSizeBytes { get; set; } + public string? OriginalExternalSource { get; set; } +} + +public sealed class FinalRealEliminationResult +{ + public IReadOnlyList Survivors { get; init; } = Array.Empty(); + public IReadOnlyList Eliminated { get; init; } = Array.Empty(); +} + +public sealed class CombinationSurvivalExecutionResult +{ + public IReadOnlyList BenchmarkedSnapshots { get; init; } = Array.Empty(); + public IReadOnlyList BrutalSurvivors { get; init; } = Array.Empty(); + public IReadOnlyList SelectedRows { get; init; } = Array.Empty(); + public IReadOnlyList ExportedArtifacts { get; init; } = Array.Empty(); + public IReadOnlyList BucketDiagnostics { get; init; } = Array.Empty(); + public SurvivalStageReport SurvivalReport { get; init; } = new(); +} diff --git a/MagicQuant/Services/BitRangeBucketBuilderService.cs b/MagicQuant/Services/BitRangeBucketBuilderService.cs new file mode 100644 index 0000000..9fa4487 --- /dev/null +++ b/MagicQuant/Services/BitRangeBucketBuilderService.cs @@ -0,0 +1,107 @@ +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class BitRangeBucketBuildResult +{ + public IReadOnlyList Buckets { get; init; } = Array.Empty(); + public IReadOnlyList BucketedCandidates { get; init; } = Array.Empty(); + public IReadOnlyList UnbucketedCandidates { get; init; } = Array.Empty(); +} + +public sealed class BitRangeBucketBuilderService +{ + private readonly HybridBenchmarkRepository _repository; + + public BitRangeBucketBuilderService(HybridBenchmarkRepository repository) + { + _repository = repository; + } + + public async Task BuildAsync( + IReadOnlyCollection candidates, + CancellationToken ct = default) + { + var pureBaselines = await _repository.LoadPureBaselineSnapshotsAsync(ct); + var baselineAnchors = pureBaselines + .GroupBy(x => x.Quant.BaseQuant.BitRange) + .Select(g => new + { + BitRange = g.Key, + Snapshot = g.OrderBy(x => x.SizeBytes).ThenBy(x => x.Kld).First() + }) + .OrderBy(x => x.BitRange) + .ToList(); + + var buckets = new List(); + + for (int i = 0; i < baselineAnchors.Count - 1; i++) + { + var lower = baselineAnchors[i]; + var upper = baselineAnchors[i + 1]; + + buckets.Add(new BitRangeBucketDefinition + { + Key = $"{lower.BitRange}->{upper.BitRange}", + LowerBitRange = lower.BitRange, + UpperBitRange = upper.BitRange, + LowerAnchorSizeBytes = lower.Snapshot.SizeBytes, + UpperAnchorSizeBytes = upper.Snapshot.SizeBytes + }); + } + + if (buckets.Count == 0) + { + AnsiConsole.MarkupLine("[yellow]No usable BitRange buckets could be built. Survival will fall back to global predicted sorting if required.[/]"); + return new BitRangeBucketBuildResult + { + Buckets = Array.Empty(), + BucketedCandidates = Array.Empty(), + UnbucketedCandidates = candidates.ToList() + }; + } + + var bucketed = new List(); + var unbucketed = new List(); + + foreach (var candidate in candidates) + { + BitRangeBucketDefinition? selected = null; + + foreach (var bucket in buckets) + { + ulong lowerBound = bucket == buckets[0] ? 0UL : bucket.LowerAnchorSizeBytes; + ulong upperBound = bucket.UpperAnchorSizeBytes; + + if (candidate.PredictedSizeBytes >= lowerBound && candidate.PredictedSizeBytes <= upperBound) + { + selected = bucket; + break; + } + } + + if (selected == null && candidate.PredictedSizeBytes > buckets[^1].UpperAnchorSizeBytes) + selected = buckets[^1]; + + if (selected == null) + { + unbucketed.Add(candidate); + continue; + } + + bucketed.Add(new BucketedCandidate + { + Evaluation = candidate, + Bucket = selected + }); + } + + return new BitRangeBucketBuildResult + { + Buckets = buckets, + BucketedCandidates = bucketed, + UnbucketedCandidates = unbucketed + }; + } +} diff --git a/MagicQuant/Services/BucketLocalPruningService.cs b/MagicQuant/Services/BucketLocalPruningService.cs new file mode 100644 index 0000000..8d78b3b --- /dev/null +++ b/MagicQuant/Services/BucketLocalPruningService.cs @@ -0,0 +1,135 @@ +using MagicQuant.Models; + +namespace MagicQuant.Services; + +public sealed class BucketLocalPruningResult +{ + public IReadOnlyList Survivors { get; init; } = Array.Empty(); + public IReadOnlyList Diagnostics { get; init; } = Array.Empty(); +} + +public sealed class BucketLocalPruningService +{ + private readonly PredictedTradeComparisonPolicy _policy = new(); + + public BucketLocalPruningResult Prune(BitRangeBucketBuildResult buildResult) + { + var diagnostics = new List(); + var survivors = new List(); + + foreach (var bucket in buildResult.Buckets) + { + var incoming = buildResult.BucketedCandidates + .Where(x => x.Bucket.Key == bucket.Key) + .Select(x => x.Evaluation) + .ToList(); + + var diag = new BucketPruneDiagnostics + { + BucketKey = bucket.Key, + LowerAnchorSizeBytes = bucket.LowerAnchorSizeBytes, + UpperAnchorSizeBytes = bucket.UpperAnchorSizeBytes, + IncomingCount = incoming.Count + }; + + if (incoming.Count == 0) + { + diag.KeptCount = 0; + diagnostics.Add(diag); + continue; + } + + var deduped = incoming + .GroupBy(x => x.EffectiveStateKey, StringComparer.Ordinal) + .Select(g => + { + var ordered = g.OrderBy(x => x, Comparer.Create(_policy.Compare)).ToList(); + int removed = ordered.Count - 1; + if (removed > 0) + diag.CountReason("effective-duplicate"); + return ordered[0]; + }) + .ToList(); + + var dominancePruned = new List(deduped); + + for (int i = dominancePruned.Count - 1; i >= 0; i--) + { + var current = dominancePruned[i]; + bool dominated = dominancePruned + .Where((_, index) => index != i) + .Any(other => _policy.Dominates(other, current)); + + if (dominated) + { + dominancePruned.RemoveAt(i); + diag.CountReason("dominance"); + } + } + + var orderedByPracticalTrade = dominancePruned + .OrderBy(x => x, Comparer.Create(_policy.Compare)) + .ToList(); + + var keptAfterPractical = new List(); + if (orderedByPracticalTrade.Count > 0) + { + var best = orderedByPracticalTrade[0]; + foreach (var candidate in orderedByPracticalTrade) + { + bool sameNeighborhood = + Math.Abs(candidate.PredictedKldCost - best.PredictedKldCost) <= Math.Max(Config.SurvivalKldCloseCallAbsoluteEpsilon, best.PredictedKldCost * Config.SurvivalKldCloseCallRelativeFraction) && + Math.Abs(candidate.PredictedPplCost - best.PredictedPplCost) <= Config.SurvivalPplLargeDifferencePercent && + PercentDifference(candidate.PredictedSizeBytes, best.PredictedSizeBytes) < Config.SurvivalMeaningfulSizeBiasPercent; + + bool obviouslyJunk = candidate.CompositeScore > best.CompositeScore * 1.65d && sameNeighborhood; + + if (obviouslyJunk) + { + diag.CountReason("practical-trade"); + continue; + } + + keptAfterPractical.Add(candidate); + } + } + + var capped = keptAfterPractical + .OrderBy(x => x, Comparer.Create(_policy.Compare)) + .Take(Config.MaxSelectedChoicesPerBucket) + .ToList(); + + int capRemoved = keptAfterPractical.Count - capped.Count; + if (capRemoved > 0) + diag.CountReason("bucket-cap"); + + diag.KeptCount = capped.Count; + diag.RemovedCount = diag.IncomingCount - diag.KeptCount; + survivors.AddRange(capped); + diagnostics.Add(diag); + } + + foreach (var candidate in buildResult.UnbucketedCandidates) + survivors.Add(candidate); + + survivors = survivors + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .ToList(); + + return new BucketLocalPruningResult + { + Survivors = survivors, + Diagnostics = diagnostics + }; + } + + private static double PercentDifference(ulong left, ulong right) + { + if (left == 0 || right == 0) + return 0d; + + double min = Math.Min(left, right); + double max = Math.Max(left, right); + return ((max - min) / min) * 100d; + } +} diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs new file mode 100644 index 0000000..e6fa126 --- /dev/null +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -0,0 +1,229 @@ +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class CombinationSurvivalPipelineService +{ + private readonly QuantizationService _quantizationService; + private readonly RemainingCombinationStore _combinationStore; + private readonly HybridBenchmarkRepository _benchmarkRepository; + private readonly EffectiveCandidateStateResolverService _effectiveResolver; + private readonly PredictedCandidateEvaluationService _predictionService; + private readonly BitRangeBucketBuilderService _bucketBuilder; + private readonly BucketLocalPruningService _bucketPruner; + private readonly FinalRealBenchmarkEliminationService _finalEliminator; + private readonly FinalSurvivorSelectionCliService _selectionCli; + private readonly HybridArtifactExportService _exportService; + private readonly ReadmeGenerationService _readmeService; + private readonly HybridMapGenerationService _hybridMapService; + + public CombinationSurvivalPipelineService(QuantizationService quantizationService) + { + _quantizationService = quantizationService; + _combinationStore = new RemainingCombinationStore(); + _benchmarkRepository = new HybridBenchmarkRepository(); + _effectiveResolver = new EffectiveCandidateStateResolverService(_benchmarkRepository); + _predictionService = new PredictedCandidateEvaluationService(_benchmarkRepository, _effectiveResolver); + _bucketBuilder = new BitRangeBucketBuilderService(_benchmarkRepository); + _bucketPruner = new BucketLocalPruningService(); + _finalEliminator = new FinalRealBenchmarkEliminationService(); + _selectionCli = new FinalSurvivorSelectionCliService(); + _exportService = new HybridArtifactExportService(_quantizationService, _effectiveResolver); + _readmeService = new ReadmeGenerationService(); + _hybridMapService = new HybridMapGenerationService(); + } + + public async Task RunAsync(CancellationToken ct = default) + { + var report = new SurvivalStageReport(); + report.StartingCount = await _combinationStore.CountAsync(ct); + + AnsiConsole.Write(new Rule("[yellow]Prediction / Survival Pipeline[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[green]Starting remaining combinations:[/] {report.StartingCount:N0}"); + + if (report.StartingCount > Config.BruteForceFinalCombinationThreshold) + { + var current = await _combinationStore.LoadAllAsync(ct); + var predicted = await _predictionService.EvaluateAsync(current, ct); + var bucketBuild = await _bucketBuilder.BuildAsync(predicted, ct); + + PrintBucketAnchors(bucketBuild.Buckets); + + var bucketPruneResult = _bucketPruner.Prune(bucketBuild); + foreach (var diag in bucketPruneResult.Diagnostics) + report.BucketDiagnostics.Add(diag); + + var survivors = bucketPruneResult.Survivors + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .ToList(); + + int beforeBalance = survivors.Count; + if (survivors.Count > Config.BruteForceFinalCombinationThreshold) + { + survivors = BalanceDownToThreshold(survivors, bucketBuild, Config.BruteForceFinalCombinationThreshold); + report.AddRemoval("bucket-balance", beforeBalance - survivors.Count); + } + + if (survivors.Count > Config.BruteForceFinalCombinationThreshold) + { + var globalCut = survivors + .OrderBy(x => x.CompositeScore) + .ThenBy(x => x.PredictedKldCost) + .ThenBy(x => x.PredictedSizeBytes) + .Take(Config.BruteForceFinalCombinationThreshold) + .ToList(); + + report.AddRemoval("stage-7-global-cut", survivors.Count - globalCut.Count); + survivors = globalCut; + } + + await _combinationStore.ReplaceAllAsync(survivors.Select(x => x.Config).ToList(), "prediction-survival", ct); + + foreach (var diag in report.BucketDiagnostics) + { + AnsiConsole.MarkupLine( + $"[grey]Bucket {Markup.Escape(diag.BucketKey)}:[/] anchors=({diag.LowerAnchorSizeBytes:N0}..{diag.UpperAnchorSizeBytes:N0}) " + + $"incoming=[cyan]{diag.IncomingCount:N0}[/] removed=[red]{diag.RemovedCount:N0}[/] kept=[green]{diag.KeptCount:N0}[/]"); + + foreach (var reason in diag.RemovalReasons.OrderByDescending(x => x.Value)) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(reason.Key)}:[/] {reason.Value:N0}"); + } + } + else + { + AnsiConsole.MarkupLine("[grey]Remaining combinations are already at or under threshold. Skipping additional predictive narrowing.[/]"); + } + + report.EndingCount = await _combinationStore.CountAsync(ct); + AnsiConsole.MarkupLine($"[green]Combinations after survival pipeline:[/] {report.EndingCount:N0}"); + + if (report.EndingCount > Config.BruteForceFinalCombinationThreshold) + { + throw new InvalidOperationException( + $"Survival pipeline completed but still left {report.EndingCount:N0} combinations, which is above the brute-force threshold of {Config.BruteForceFinalCombinationThreshold:N0}. Diagnostics were emitted above."); + } + + AnsiConsole.Write(new Rule("[yellow]Final Brute Force Benchmark Phase[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine( + $"[green]Remaining combination count[/] [cyan]{report.EndingCount:N0}[/] [grey]is at or below the brute-force threshold of[/] [yellow]{Config.BruteForceFinalCombinationThreshold:N0}[/]."); + + var finalConfigs = await _combinationStore.LoadAllAsync(ct); + var finalQuants = finalConfigs.Select(x => (HybridQuant)x).ToList(); + var finalSummary = await _quantizationService.ProcessHybridBatchAsync(finalQuants); + + AnsiConsole.MarkupLine("[bold green]Final brute force benchmarking complete.[/]"); + AnsiConsole.MarkupLine($" [green]Requested:[/] {finalSummary.Requested:N0}"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {finalSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {finalSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {finalSummary.Failed:N0}"); + + var benchmarkSnapshots = (await _benchmarkRepository.LoadBenchmarkSnapshotsAsync(finalConfigs, ct)).Values.ToList(); + var pureBaselines = await _benchmarkRepository.LoadPureBaselineSnapshotsAsync(ct); + + var brutalInput = benchmarkSnapshots + .Concat(pureBaselines) + .GroupBy(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal) + .Select(g => g.First()) + .ToList(); + + var brutal = _finalEliminator.Eliminate(brutalInput); + AnsiConsole.MarkupLine($"[green]Final brutal elimination removals:[/] [red]{brutal.Eliminated.Count:N0}[/]"); + + var selectedRows = _selectionCli.Prompt(brutal.Survivors); + + var exportedArtifacts = await _exportService.ExportAsync(selectedRows, ct); + + string modelName = string.IsNullOrWhiteSpace(Cache.ModelDirectory) + ? "model" + : new DirectoryInfo(Cache.ModelDirectory!).Name; + + await _readmeService.GenerateAsync(Cache.OutputDirectory!, modelName, exportedArtifacts, brutalInput, ct); + await _hybridMapService.GenerateAsync(Cache.OutputDirectory!, exportedArtifacts, ct); + + return new CombinationSurvivalExecutionResult + { + BenchmarkedSnapshots = benchmarkSnapshots, + BrutalSurvivors = brutal.Survivors, + SelectedRows = selectedRows, + ExportedArtifacts = exportedArtifacts, + BucketDiagnostics = report.BucketDiagnostics, + SurvivalReport = report + }; + } + + private static void PrintBucketAnchors(IReadOnlyList buckets) + { + foreach (var bucket in buckets) + { + AnsiConsole.MarkupLine( + $"[grey]BitRange bucket {Markup.Escape(bucket.Key)}[/] -> lower_anchor=[cyan]{bucket.LowerAnchorSizeBytes:N0}[/] upper_anchor=[cyan]{bucket.UpperAnchorSizeBytes:N0}[/]"); + } + } + + private static List BalanceDownToThreshold( + IReadOnlyList survivors, + BitRangeBucketBuildResult bucketBuild, + int threshold) + { + var byBucket = bucketBuild.Buckets + .ToDictionary( + bucket => bucket.Key, + bucket => survivors + .Where(x => bucketBuild.BucketedCandidates.Any(bc => bc.Bucket.Key == bucket.Key && TensorConfigIdentity.ToKey(bc.Evaluation.Config) == TensorConfigIdentity.ToKey(x.Config))) + .OrderBy(x => x.CompositeScore) + .ThenBy(x => x.PredictedKldCost) + .ThenBy(x => x.PredictedSizeBytes) + .ToList(), + StringComparer.Ordinal); + + var fallback = survivors + .Where(x => !bucketBuild.BucketedCandidates.Any(bc => TensorConfigIdentity.ToKey(bc.Evaluation.Config) == TensorConfigIdentity.ToKey(x.Config))) + .OrderBy(x => x.CompositeScore) + .ThenBy(x => x.PredictedKldCost) + .ThenBy(x => x.PredictedSizeBytes) + .ToList(); + + var balanced = new List(threshold); + int pass = 0; + while (balanced.Count < threshold) + { + bool addedAny = false; + + foreach (var bucket in byBucket.OrderBy(x => x.Key, StringComparer.Ordinal)) + { + if (pass >= bucket.Value.Count) + continue; + + balanced.Add(bucket.Value[pass]); + addedAny = true; + + if (balanced.Count >= threshold) + break; + } + + if (!addedAny) + break; + + pass++; + } + + foreach (var item in fallback) + { + if (balanced.Count >= threshold) + break; + + if (balanced.Any(x => TensorConfigIdentity.ToKey(x.Config) == TensorConfigIdentity.ToKey(item.Config))) + continue; + + balanced.Add(item); + } + + return balanced + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .Take(threshold) + .ToList(); + } +} diff --git a/MagicQuant/Services/EffectiveCandidateStateResolverService.cs b/MagicQuant/Services/EffectiveCandidateStateResolverService.cs new file mode 100644 index 0000000..1f027c8 --- /dev/null +++ b/MagicQuant/Services/EffectiveCandidateStateResolverService.cs @@ -0,0 +1,153 @@ +using MagicQuant.Helpers; +using System.Text; +using MagicQuant.Models; +using MQ.DB.Models; + +namespace MagicQuant.Services; + +public sealed class EffectiveCandidateStateResolverService +{ + private readonly HybridBenchmarkRepository _repository; + + public EffectiveCandidateStateResolverService(HybridBenchmarkRepository repository) + { + _repository = repository; + } + + public async Task ResolveAsync(TensorConfig config, CancellationToken ct = default) + { + return await ResolveAsync((HybridQuant)config, config, ct); + } + + public async Task ResolveAsync(HybridQuant quant, CancellationToken ct = default) + { + return await ResolveAsync(quant, (TensorConfig)quant, ct); + } + + private async Task ResolveAsync(HybridQuant quant, TensorConfig config, CancellationToken ct) + { + var warnings = new List(); + var groupStates = new Dictionary(StringComparer.Ordinal); + + string baseState = await ResolveBaseStateAsync(quant.BaseQuant, warnings, ct); + + foreach (var (group, storedValue) in TensorConfigIdentity.EnumerateGroupSlots(config)) + { + string state; + if (BaselineQuants.IsNullTensorConfigGroupSlot(storedValue)) + { + state = "base"; + } + else + { + byte baselineId = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedValue); + + if (BaselineQuants.IsNativeExactAlias(baselineId)) + { + var exactScheme = BaselineQuants.ResolveExactOverrideScheme(baselineId); + state = $"exact:{exactScheme.Names[0]}"; + } + else + { + var baseline = BaselineQuants.FromId(baselineId); + state = await ResolveGroupStateAsync(baseline, group, warnings, ct); + } + } + + groupStates[group.Name] = state; + } + + var keyBuilder = new StringBuilder(); + keyBuilder.Append("base=").Append(baseState); + foreach (var kv in groupStates.OrderBy(x => x.Key, StringComparer.Ordinal)) + keyBuilder.Append('|').Append(kv.Key).Append('=').Append(kv.Value); + + return new EffectiveStateResolutionResult + { + Config = config, + EffectiveStateKey = keyBuilder.ToString(), + HasUnknownMappings = warnings.Count > 0, + Warnings = warnings, + GroupStates = groupStates, + BaseState = baseState + }; + } + + private async Task ResolveBaseStateAsync(BaselineQuants baseline, List warnings, CancellationToken ct) + { + if (!baseline.IsExternalRepositoryBaseline) + return baseline.CanonicalKey; + + var blanket = await _repository.LoadLearnedTensorMappingsAsync( + canonicalBaselineKey: baseline.CanonicalKey, + groupId: null, + preferredSourceScheme: baseline.DefaultTensorScheme, + allowDominantFallback: true, + ct: ct); + + if (blanket.Count == 0) + { + warnings.Add($"No learned blanket mapping found for external baseline '{baseline.Names[0]}'. Falling back to canonical key."); + return baseline.CanonicalKey; + } + + var payload = string.Join("|", blanket.OrderBy(x => x.Key, StringComparer.Ordinal).Select(x => $"{x.Key}={NormalizeOrPreserveRaw(x.Value, warnings)}")); + return $"{baseline.CanonicalKey}:{TensorConfigIdentity.StableHash(payload)}"; + } + + private async Task ResolveGroupStateAsync( + BaselineQuants baseline, + TensorGroup group, + List warnings, + CancellationToken ct) + { + var mappings = await _repository.LoadLearnedTensorMappingsAsync( + canonicalBaselineKey: baseline.CanonicalKey, + groupId: group.UniqueId, + preferredSourceScheme: baseline.DefaultTensorScheme, + allowDominantFallback: true, + ct: ct); + + if (mappings.Count == 0) + { + warnings.Add($"No learned mapping found for group '{group.Name}' baseline '{baseline.Names[0]}'. Falling back to requested baseline identity."); + return $"requested:{baseline.CanonicalKey}"; + } + + var normalized = mappings + .OrderBy(x => x.Key, StringComparer.Ordinal) + .Select(x => $"{x.Key}={NormalizeOrPreserveRaw(x.Value, warnings)}") + .ToList(); + + if (normalized.Select(x => x.Split('=')[1]).Distinct(StringComparer.Ordinal).Count() == 1) + return $"effective:{normalized[0].Split('=')[1]}"; + + return $"effective-map:{TensorConfigIdentity.StableHash(string.Join("|", normalized))}"; + } + + private static string NormalizeOrPreserveRaw(string raw, List warnings) + { + string normalizedRaw = (raw ?? string.Empty).Trim(); + if (string.IsNullOrWhiteSpace(normalizedRaw)) + { + warnings.Add("Encountered an empty learned tensor state and preserved it as unknown metadata."); + return "unknown:"; + } + + var resolved = TensorWeightScheme.All.FirstOrDefault(x => + x.Names.Any(n => string.Equals(n, normalizedRaw, StringComparison.OrdinalIgnoreCase))); + if (resolved != null) + return resolved.Names[0]; + + var nativeResolved = NativePrecisionNormalization.ResolveSchemeIdsForLearnedFinalQuantType(normalizedRaw); + if (nativeResolved.Count > 0) + { + var nativeScheme = TensorWeightScheme.All.FirstOrDefault(x => x.UniqueId == nativeResolved.First()); + if (nativeScheme != null) + return nativeScheme.Names[0]; + } + + warnings.Add($"Unknown or partially unmapped learned tensor state '{normalizedRaw}' was preserved as raw metadata."); + return $"unknown:{normalizedRaw}"; + } +} diff --git a/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs b/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs new file mode 100644 index 0000000..6ddae84 --- /dev/null +++ b/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs @@ -0,0 +1,45 @@ +using MagicQuant.Models; + +namespace MagicQuant.Services; + +public sealed class FinalRealBenchmarkEliminationService +{ + public FinalRealEliminationResult Eliminate(IReadOnlyCollection snapshots) + { + var ordered = snapshots + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ThenBy(x => x.Ppl) + .ToList(); + + var survivors = new List(); + var eliminated = new List(); + + for (int i = 0; i < ordered.Count; i++) + { + var current = ordered[i]; + bool dominated = ordered + .Where((_, index) => index != i) + .Any(other => + other.SizeBytes <= current.SizeBytes && + other.Kld < current.Kld && + other.Ppl < current.Ppl); + + if (dominated) + eliminated.Add(current); + else + survivors.Add(current); + } + + return new FinalRealEliminationResult + { + Survivors = survivors + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ThenBy(x => x.Ppl) + .ToList(), + Eliminated = eliminated + }; + } +} diff --git a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs new file mode 100644 index 0000000..9787089 --- /dev/null +++ b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs @@ -0,0 +1,105 @@ +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class FinalSurvivorSelectionCliService +{ + public IReadOnlyList Prompt(IReadOnlyCollection survivors) + { + var rows = survivors + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .Select((snapshot, index) => new FinalSelectionRow + { + Id = index + 1, + Enabled = true, + Snapshot = snapshot + }) + .ToList(); + + if (rows.Count == 0) + { + AnsiConsole.MarkupLine("[yellow]No survivors remained after brutal real-truth elimination.[/]"); + return rows; + } + + while (true) + { + Render(rows); + + string input = AnsiConsole.Prompt( + new TextPrompt("Toggle [cyan]row number[/], or type [green]ready[/] to continue") + .AllowEmpty()) + .Trim(); + + if (string.IsNullOrWhiteSpace(input) || + string.Equals(input, "ready", StringComparison.OrdinalIgnoreCase) || + string.Equals(input, "continue", StringComparison.OrdinalIgnoreCase) || + string.Equals(input, "done", StringComparison.OrdinalIgnoreCase)) + { + if (rows.Any(x => x.Enabled)) + return rows; + + AnsiConsole.MarkupLine("[red]At least one survivor must remain enabled.[/]"); + continue; + } + + if (!int.TryParse(input, out var id)) + { + AnsiConsole.MarkupLine($"[yellow]Unknown selection command:[/] {Markup.Escape(input)}"); + continue; + } + + var row = rows.FirstOrDefault(x => x.Id == id); + if (row == null) + { + AnsiConsole.MarkupLine($"[yellow]No row exists with ID {id}.[/]"); + continue; + } + + row.Enabled = !row.Enabled; + } + } + + private static void Render(IReadOnlyCollection rows) + { + AnsiConsole.Clear(); + AnsiConsole.Write(new Rule("[yellow]Final Survivor Selection[/]") { Justification = Justify.Left }); + + var table = new Table().Border(TableBorder.Rounded).Expand(); + table.AddColumn("ID"); + table.AddColumn("State"); + table.AddColumn("Display / Model"); + table.AddColumn("Provider"); + table.AddColumn("Quant Family / Baseline"); + table.AddColumn("KLD"); + table.AddColumn("PPL"); + table.AddColumn("Size (GB)"); + + foreach (var row in rows) + { + var snap = row.Snapshot; + string state = row.Enabled ? "[green]ENABLED[/]" : "[red]DISABLED[/]"; + string display = row.Enabled ? Markup.Escape(snap.DisplayName) : $"[grey]{Markup.Escape(snap.DisplayName)}[/]"; + string provider = row.Enabled ? Markup.Escape(snap.ProviderName) : $"[grey]{Markup.Escape(snap.ProviderName)}[/]"; + string family = row.Enabled ? Markup.Escape(snap.BaselineFamily) : $"[grey]{Markup.Escape(snap.BaselineFamily)}[/]"; + string kld = row.Enabled ? $"[cyan]{snap.Kld:0.000000}[/]" : $"[grey]{snap.Kld:0.000000}[/]"; + string ppl = row.Enabled ? $"[cyan]{snap.Ppl:0.0000}[/]" : $"[grey]{snap.Ppl:0.0000}[/]"; + string sizeGb = (snap.SizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); + + table.AddRow( + row.Id.ToString(), + state, + display, + provider, + family, + kld, + ppl, + row.Enabled ? $"[cyan]{sizeGb}[/]" : $"[grey]{sizeGb}[/]"); + } + + AnsiConsole.Write(table); + AnsiConsole.MarkupLine("[grey]All rows start enabled. Enter a row number to toggle it, then type ready when done.[/]"); + } +} diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs new file mode 100644 index 0000000..eccbf41 --- /dev/null +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -0,0 +1,248 @@ +using System.Text.Json; +using MagicQuant.Models; +using MQ.DB; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class HybridArtifactExportService +{ + private static readonly string[] ModelAdjacentFiles = + [ + "generation_config.json", + "config.json", + "chat_template.jinja", + "added_tokenizer.json", + "LICENSE", + "merges.txt", + "model.safetensors.index.json", + "tokenizer.json", + "tokenizer_config.json", + "vocab.json" + ]; + + private readonly QuantizationService _quantizationService; + private readonly EffectiveCandidateStateResolverService _effectiveResolver; + + public HybridArtifactExportService( + QuantizationService quantizationService, + EffectiveCandidateStateResolverService effectiveResolver) + { + _quantizationService = quantizationService; + _effectiveResolver = effectiveResolver; + } + + public async Task> ExportAsync( + IReadOnlyCollection selectedRows, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(Cache.OutputDirectory)) + throw new InvalidOperationException("Cache.OutputDirectory is not set."); + + Directory.CreateDirectory(Cache.OutputDirectory); + + var output = new List(); + var hybridOrdinalByFamily = new Dictionary(StringComparer.Ordinal); + + foreach (var row in selectedRows.Where(x => x.Enabled).OrderBy(x => x.Snapshot.Kld).ThenBy(x => x.Snapshot.SizeBytes)) + { + var snap = row.Snapshot; + bool isHybrid = snap.IsHybrid; + bool exportLocally = isHybrid || !snap.IsExternalPureBaseline || Config.ExportExternalLearnedBaselines; + string provider = HybridBenchmarkRepository.ResolveProviderName(snap.Quant, exportNaming: exportLocally && isHybrid); + + if (!exportLocally) + { + AnsiConsole.MarkupLine($"[grey]Skipping local export for external learned baseline by default:[/] {Markup.Escape(snap.DisplayName)} [grey](enable with --export-external-learned-baselines or output.export_external_learned_baselines: true)[/]"); + + output.Add(new ExportedArtifactRecord + { + Snapshot = snap, + DisplayName = snap.DisplayName, + ProviderName = provider, + BaselineFamily = snap.BaselineFamily, + IsExternalReference = true, + DownloadTarget = snap.ExternalRepositoryUrl ?? string.Empty, + ExpectedSizeBytes = snap.SizeBytes, + EffectiveState = await _effectiveResolver.ResolveAsync(snap.Config, ct) + }); + + continue; + } + + if (snap.IsExternalPureBaseline && !snap.IsHybrid) + AnsiConsole.MarkupLine($"[yellow]Local export enabled for external learned baseline:[/] {Markup.Escape(snap.DisplayName)}"); + + string fileName = BuildFileName(snap, provider, hybridOrdinalByFamily); + string fullPath = Path.Combine(Cache.OutputDirectory!, fileName); + ulong expectedBytes = snap.SizeBytes; + + bool shouldBuild = true; + if (File.Exists(fullPath)) + { + ulong actual = (ulong)new FileInfo(fullPath).Length; + if (actual == expectedBytes) + { + shouldBuild = false; + AnsiConsole.MarkupLine($"[grey]Reusing existing exported artifact:[/] {Markup.Escape(fullPath)}"); + } + else + { + AnsiConsole.MarkupLine($"[yellow]Existing export byte size mismatch, rebuilding:[/] {Markup.Escape(fullPath)}"); + File.Delete(fullPath); + } + } + + if (shouldBuild) + await _quantizationService.BuildExportArtifactAsync(snap.Quant, fullPath, forceRebuild: false, ct: ct); + + ulong actualBytes = File.Exists(fullPath) ? (ulong)new FileInfo(fullPath).Length : 0UL; + if (actualBytes != expectedBytes) + { + AnsiConsole.MarkupLine($"[yellow]Export byte validation warning:[/] expected [cyan]{expectedBytes:N0}[/] but got [cyan]{actualBytes:N0}[/] for {Markup.Escape(fileName)}"); + } + + output.Add(new ExportedArtifactRecord + { + Snapshot = snap, + DisplayName = snap.DisplayName, + ProviderName = provider, + BaselineFamily = snap.BaselineFamily, + IsExternalReference = false, + FileName = fileName, + FullPath = fullPath, + DownloadTarget = $"./../../resolve/main/{fileName}?download=true", + ExpectedSizeBytes = expectedBytes, + ActualSizeBytes = actualBytes, + EffectiveState = await _effectiveResolver.ResolveAsync(snap.Config, ct) + }); + } + + await CopyModelAdjacentFilesAsync(Cache.OutputDirectory!, ct); + await CopyImatrixArtifactsAsync(Cache.OutputDirectory!, ct); + await CopyMmprojArtifactsAsync(Cache.OutputDirectory!, ct); + + return output; + } + + private static string BuildFileName( + BenchmarkSnapshotRecord snapshot, + string provider, + Dictionary hybridOrdinalByFamily) + { + string prefix = Sanitize(Config.OutputNamePrefix); + + if (!snapshot.IsHybrid) + return $"{prefix}-{Sanitize(provider)}-{Sanitize(snapshot.BaselineFamily)}.gguf"; + + hybridOrdinalByFamily.TryGetValue(snapshot.BaselineFamily, out var current); + current++; + hybridOrdinalByFamily[snapshot.BaselineFamily] = current; + + string special = $"H{current}"; + return $"{prefix}-{Sanitize(provider)}-{special}-{Sanitize(snapshot.BaselineFamily)}.gguf"; + } + + private static async Task CopyModelAdjacentFilesAsync(string outputDirectory, CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(Cache.ModelDirectory)) + return; + + foreach (var fileName in ModelAdjacentFiles) + { + string source = Path.Combine(Cache.ModelDirectory!, fileName); + string target = Path.Combine(outputDirectory, fileName); + + if (!File.Exists(source)) + { + AnsiConsole.MarkupLine($"[grey]Optional model-adjacent file missing:[/] {Markup.Escape(fileName)}"); + continue; + } + + File.Copy(source, target, overwrite: true); + await Task.Yield(); + AnsiConsole.MarkupLine($"[green]Copied model-adjacent file:[/] {Markup.Escape(fileName)}"); + } + } + + private static async Task CopyImatrixArtifactsAsync(string outputDirectory, CancellationToken ct) + { + if (!Cache.IsImatrixAvailable || string.IsNullOrWhiteSpace(Cache.ActiveImatrixPath)) + return; + + string source = Cache.ActiveImatrixPath!; + string target = Path.Combine(outputDirectory, "imatrix.dat"); + File.Copy(source, target, overwrite: true); + AnsiConsole.MarkupLine($"[green]Copied imatrix artifact:[/] {Markup.Escape(target)}"); + + string imatrixDir = Path.GetDirectoryName(source)!; + foreach (var optional in new[] { "imatrix.success.json", "imatrix.metadata.json", "imatrix.build.log" }) + { + string optionalSource = Path.Combine(imatrixDir, optional); + if (!File.Exists(optionalSource)) + continue; + + File.Copy(optionalSource, Path.Combine(outputDirectory, optional), overwrite: true); + await Task.Yield(); + AnsiConsole.MarkupLine($"[green]Copied imatrix sidecar:[/] {Markup.Escape(optional)}"); + } + } + + private static async Task CopyMmprojArtifactsAsync(string outputDirectory, CancellationToken ct) + { + var searchRoots = new List(); + if (!string.IsNullOrWhiteSpace(Cache.ModelDirectory)) + searchRoots.Add(Cache.ModelDirectory!); + if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + searchRoots.Add(Cache.ModelMagicQuantDirectory!); + + foreach (var root in searchRoots.Distinct(StringComparer.OrdinalIgnoreCase)) + { + var mmproj = Directory.EnumerateFiles(root, "*mmproj*.gguf", SearchOption.AllDirectories).FirstOrDefault(); + if (mmproj == null) + continue; + + string target = Path.Combine(outputDirectory, Path.GetFileName(mmproj)); + File.Copy(mmproj, target, overwrite: true); + AnsiConsole.MarkupLine($"[green]Copied mmproj artifact:[/] {Markup.Escape(target)}"); + return; + } + + if (!LooksVisionCapableModel()) + { + AnsiConsole.MarkupLine("[grey]No mmproj artifact was present, but no vision capability hints were detected. Continuing.[/]"); + return; + } + + throw new InvalidOperationException( + "This model appears to be vision-capable, but no mmproj GGUF could be found in the working/source artifacts."); + } + + private static bool LooksVisionCapableModel() + { + if (string.IsNullOrWhiteSpace(Cache.ModelDirectory)) + return false; + + string configPath = Path.Combine(Cache.ModelDirectory!, "config.json"); + if (!File.Exists(configPath)) + return false; + + string json = File.ReadAllText(configPath); + return json.Contains("vision_config", StringComparison.OrdinalIgnoreCase) || + json.Contains("vision_tower", StringComparison.OrdinalIgnoreCase) || + json.Contains("mm_vision_tower", StringComparison.OrdinalIgnoreCase) || + json.Contains("projector", StringComparison.OrdinalIgnoreCase); + } + + private static string Sanitize(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return "model"; + + var cleaned = value.Trim(); + foreach (char c in Path.GetInvalidFileNameChars()) + cleaned = cleaned.Replace(c, '-'); + + return cleaned.Replace(" ", "-"); + } +} diff --git a/MagicQuant/Services/HybridBenchmarkRepository.cs b/MagicQuant/Services/HybridBenchmarkRepository.cs new file mode 100644 index 0000000..7381ed2 --- /dev/null +++ b/MagicQuant/Services/HybridBenchmarkRepository.cs @@ -0,0 +1,271 @@ +using MagicQuant.Helpers; +using MagicQuant.Models; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; + +namespace MagicQuant.Services; + +public sealed class HybridBenchmarkRepository +{ + public async Task> LoadBenchmarkSnapshotsAsync( + IEnumerable configs, + CancellationToken ct = default) + { + var result = new Dictionary(StringComparer.Ordinal); + + foreach (var config in configs.DistinctBy(TensorConfigIdentity.ToKey)) + { + var snapshot = await LoadBenchmarkSnapshotAsync(config, ct); + if (snapshot != null) + result[TensorConfigIdentity.ToKey(config)] = snapshot; + } + + return result; + } + + public async Task LoadBenchmarkSnapshotAsync( + TensorConfig config, + CancellationToken ct = default) + { + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + return null; + + int? activeImatrixId = await ResolveActiveImatrixIdAsync(db, scopedAiModelHashId.Value, ct); + + var query = db.AiBenchmarks + .AsNoTracking() + .Include(x => x.TensorCombo) + .Include(x => x.CategorBenchmarks) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) + .Where(x => x.TensorCombo.BaseQuant == config.BaseQuant) + .Where(x => x.TensorCombo.Embeddings == config.Embeddings) + .Where(x => x.TensorCombo.LmHead == config.LmHead) + .Where(x => x.TensorCombo.AttnQ == config.AttnQ) + .Where(x => x.TensorCombo.AttnKV == config.AttnKV) + .Where(x => x.TensorCombo.AttnOutput == config.AttnOutput) + .Where(x => x.TensorCombo.FfnUpGate == config.FfnUpGate) + .Where(x => x.TensorCombo.FfnDown == config.FfnDown) + .Where(x => x.TensorCombo.MoeExperts == config.MoeExperts) + .Where(x => x.TensorCombo.MoeRouter == config.MoeRouter); + + var rows = await query.ToListAsync(ct); + if (rows.Count == 0) + return null; + + AiBenchmark chosen = rows + .OrderByDescending(x => activeImatrixId != null && x.ImatrixDefinitionId == activeImatrixId.Value) + .ThenByDescending(x => x.ImatrixDefinitionId != null) + .ThenBy(x => x.Id) + .First(); + + var general = chosen.CategorBenchmarks.FirstOrDefault(x => x.Category == (byte)BenchmarkCategory.General) + ?? chosen.CategorBenchmarks.OrderBy(x => x.Category).FirstOrDefault(); + + if (general == null) + return null; + + var quant = (HybridQuant)config; + var baseQuant = quant.BaseQuant; + + return new BenchmarkSnapshotRecord + { + Config = config, + Quant = quant, + DisplayName = BuildDisplayName(quant), + ProviderName = ResolveProviderName(quant, exportNaming: false), + BaselineFamily = baseQuant.Names[0], + IsHybrid = quant.Tensors.Count > 0, + IsExternalPureBaseline = quant.Tensors.Count == 0 && baseQuant.IsExternalRepositoryBaseline, + SizeBytes = chosen.SizeBytes, + Kld = general.Kld, + Ppl = general.Ppl, + OutputModelPath = await FindLatestSuccessfulOutputPathAsync(config, ct), + ExternalRepositoryUrl = BuildExternalRepositoryUrl(baseQuant) + }; + } + + public async Task> LoadPureBaselineSnapshotsAsync(CancellationToken ct = default) + { + var result = new List(); + + foreach (var baseline in BaselineQuants.GetAllRecognizedBaselines()) + { + var snapshot = await LoadBenchmarkSnapshotAsync((TensorConfig)HybridQuant.CreatePureBaseline(baseline), ct); + if (snapshot != null) + result.Add(snapshot); + } + + return result + .GroupBy(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal) + .Select(g => g.First()) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ToList(); + } + + public async Task FindLatestSuccessfulOutputPathAsync(TensorConfig config, CancellationToken ct = default) + { + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + return null; + + var tensorComboId = await db.TensorCombos + .AsNoTracking() + .Where(x => x.BaseQuant == config.BaseQuant) + .Where(x => x.Embeddings == config.Embeddings) + .Where(x => x.LmHead == config.LmHead) + .Where(x => x.AttnQ == config.AttnQ) + .Where(x => x.AttnKV == config.AttnKV) + .Where(x => x.AttnOutput == config.AttnOutput) + .Where(x => x.FfnUpGate == config.FfnUpGate) + .Where(x => x.FfnDown == config.FfnDown) + .Where(x => x.MoeExperts == config.MoeExperts) + .Where(x => x.MoeRouter == config.MoeRouter) + .Select(x => (Guid?)x.Id) + .FirstOrDefaultAsync(ct); + + if (tensorComboId == null) + return null; + + return await db.QuantizationRuns + .AsNoTracking() + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) + .Where(x => x.TensorComboId == tensorComboId.Value) + .Where(x => x.Succeeded) + .OrderByDescending(x => x.CompletedUtc) + .Select(x => x.OutputModelPath) + .FirstOrDefaultAsync(ct); + } + + public async Task> LoadLearnedTensorMappingsAsync( + string canonicalBaselineKey, + byte? groupId = null, + TensorWeightScheme? preferredSourceScheme = null, + bool allowDominantFallback = true, + CancellationToken ct = default) + { + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + return new Dictionary(StringComparer.Ordinal); + + var query = db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) + .Where(x => x.BaselineCanonicalKey == canonicalBaselineKey); + + if (groupId != null) + query = query.Where(x => x.TensorGroupId == groupId.Value); + + var allRows = await query.OrderBy(x => x.TensorName).ToListAsync(ct); + if (allRows.Count == 0) + return new Dictionary(StringComparer.Ordinal); + + var rows = allRows; + + if (preferredSourceScheme != null) + { + var preferred = allRows.Where(x => x.TensorWeightSchemeId == preferredSourceScheme.UniqueId).ToList(); + if (preferred.Count > 0) + rows = preferred; + else if (!allowDominantFallback) + return new Dictionary(StringComparer.Ordinal); + } + + if (rows.Select(x => x.TensorWeightSchemeId).Distinct().Count() > 1) + { + if (!allowDominantFallback) + return new Dictionary(StringComparer.Ordinal); + + var dominant = rows.GroupBy(x => x.TensorWeightSchemeId) + .OrderByDescending(g => g.Count()) + .ThenBy(g => g.Key) + .First() + .Key; + + rows = rows.Where(x => x.TensorWeightSchemeId == dominant).ToList(); + } + + return rows.ToDictionary( + x => x.TensorName, + x => + { + var normalized = NativePrecisionNormalization.NormalizeLearnedFinalQuantTypeForApplication(x.FinalQuantType); + return string.IsNullOrWhiteSpace(normalized) ? x.FinalQuantType : normalized; + }, + StringComparer.Ordinal); + } + + public static string ResolveProviderName(HybridQuant quant, bool exportNaming) + { + if (exportNaming && quant.Tensors.Count > 0) + return "MagicHybrid"; + + var baseline = quant.BaseQuant; + if (baseline.IsExternalRepositoryBaseline) + return string.IsNullOrWhiteSpace(baseline.ShortSourceName) + ? "External" + : baseline.ShortSourceName!; + + return string.IsNullOrWhiteSpace(baseline.ShortSourceName) + ? "llama.cpp" + : baseline.ShortSourceName!; + } + + public static string BuildDisplayName(HybridQuant quant) + { + string modelName = string.IsNullOrWhiteSpace(Cache.ModelDirectory) + ? "model" + : new DirectoryInfo(Cache.ModelDirectory!).Name; + + if (quant.Tensors.Count == 0) + return $"{modelName}-{quant.BaseQuant.Names[0]}"; + + var parts = quant.Tensors + .Where(x => x?.TGroup != null) + .OrderBy(x => x.TGroup.ShortCode) + .Select(x => + { + if (x.OverrideMode == HybridTensorOverrideMode.ExactTensorScheme) + return $"{x.TGroup.ShortCode}-{x.ExactTensorScheme!.Names[0]}"; + + return $"{x.TGroup.ShortCode}-{x.CandidateBaseline!.Names[0]}"; + }); + + return $"{modelName}-{quant.BaseQuant.Names[0]}-{string.Join("-", parts)}"; + } + + public static string? BuildExternalRepositoryUrl(BaselineQuants baseline) + { + if (!baseline.IsExternalRepositoryBaseline) + return null; + + var resolved = Config.GetResolvedCustomBaseline(baseline.CanonicalKey); + if (resolved != null && !string.IsNullOrWhiteSpace(resolved.RepoId)) + return $"https://huggingface.co/{resolved.RepoId}"; + + if (!string.IsNullOrWhiteSpace(baseline.SourceRepository)) + return $"https://huggingface.co/{baseline.SourceRepository}"; + + return null; + } + + private static async Task ResolveActiveImatrixIdAsync(MagicQuantContext db, uint scopedAiModelHashId, CancellationToken ct) + { + if (!Cache.IsImatrixAvailable || string.IsNullOrWhiteSpace(Cache.ActiveImatrixIdentityHash)) + return null; + + return await db.ImatrixDefinitions + .AsNoTracking() + .Where(x => x.AiModelHashId == scopedAiModelHashId) + .Where(x => x.IdentityHash == Cache.ActiveImatrixIdentityHash) + .Select(x => (int?)x.Id) + .FirstOrDefaultAsync(ct); + } +} diff --git a/MagicQuant/Services/HybridMapGenerationService.cs b/MagicQuant/Services/HybridMapGenerationService.cs new file mode 100644 index 0000000..2565971 --- /dev/null +++ b/MagicQuant/Services/HybridMapGenerationService.cs @@ -0,0 +1,50 @@ +using System.Text.Json; +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class HybridMapGenerationService +{ + public async Task GenerateAsync( + string outputDirectory, + IReadOnlyCollection exportedArtifacts, + CancellationToken ct = default) + { + Directory.CreateDirectory(outputDirectory); + string path = Path.Combine(outputDirectory, "magicquant.hybrid-map.json"); + + var entries = exportedArtifacts + .Where(x => !x.IsExternalReference) + .Where(x => x.Snapshot.IsHybrid) + .Select(x => new HybridMapEntry + { + ExportedFileName = x.FileName ?? string.Empty, + DisplayName = x.DisplayName, + ProviderSource = x.ProviderName, + BaselineFamily = x.BaselineFamily, + OriginalReferenceBaseline = x.Snapshot.Quant.BaseQuant.Names[0], + TensorGroups = x.Snapshot.Quant.Tensors.ToDictionary( + t => t.TGroup.Name, + t => t.OverrideMode == HybridTensorOverrideMode.ExactTensorScheme + ? t.ExactTensorScheme!.Names[0] + : t.CandidateBaseline!.Names[0], + StringComparer.Ordinal), + EffectiveQuantStateKey = x.EffectiveState?.EffectiveStateKey ?? string.Empty, + HasUnknownMappings = x.EffectiveState?.HasUnknownMappings ?? false, + Warnings = x.EffectiveState?.Warnings.ToList() ?? new List(), + UsedImatrix = Cache.UseImatrix && Cache.IsImatrixAvailable, + ExpectedSizeBytes = x.ExpectedSizeBytes, + ActualSizeBytes = x.ActualSizeBytes, + OriginalExternalSource = HybridBenchmarkRepository.BuildExternalRepositoryUrl(x.Snapshot.Quant.BaseQuant) + }) + .ToList(); + + var json = JsonSerializer.Serialize(entries, new JsonSerializerOptions { WriteIndented = true }); + await File.WriteAllTextAsync(path, json, ct); + AnsiConsole.MarkupLine($"[green]Hybrid map JSON generated:[/] {Markup.Escape(path)}"); + return path; + } +} diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 6ab790a..b281774 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -290,25 +290,32 @@ public async Task AnalyzeAndApplyFinalAsync( .Where(x => x.Key.StartsWith("baseonly:", StringComparison.Ordinal)) .ToList(); + var baseBaselineCandidates = new List(); + foreach (var item in baseOnlyPlans) { var snap = await LoadSnapshotAsync(item.Quant, ct); if (snap == null) continue; - double reduction = ComputeReductionRatio(nativeBaseline.SizeBytes, snap.SizeBytes); - if (reduction < options.MinMeaningfulBaseOnlyReductionRatio) + var baseline = BaselineQuants.FromId(item.TestedBaselineId!.Value); + if (!baseline.IsCombinationCarrierCandidate) + continue; + + baseBaselineCandidates.Add(new BaseBaselineEvaluation { - var baseline = BaselineQuants.FromId(item.TestedBaselineId!.Value); - if (RuntimeSearchSpace.DisableCombinationBaseline(baseline)) - { - result.DisabledBaselines++; - result.Notes.Add( - $"Disabled combination baseline '{baseline.Names[0]}' because uncovered-tensor reduction was only {reduction:P2}."); - } - } + Baseline = baseline, + SizeBytes = snap.SizeBytes, + SavingsRatio = ComputeReductionRatio(nativeBaseline.SizeBytes, snap.SizeBytes), + Kld = GetAggregateKld(snap), + PplDeltaPercent = GetAggregatePplDeltaPercent(snap, nativeBaseline) + }); } + ApplyBaseBaselineReductionPruning(baseBaselineCandidates, options, result); + ApplyBaseBaselineDominanceElimination(baseBaselineCandidates, result); + ApplyBaseBaselineBadTradeElimination(baseBaselineCandidates, result); + result.ExplicitQuantBannedGroups = RuntimeSearchSpace.GetGroupsWithExplicitQuantBanned().Count; result.Bf16SuppressedGroups = result.GroupDetails.Count(x => x.Bf16Suppressed); @@ -586,6 +593,231 @@ private static double ComputeReductionRatio(ulong baselineBytes, ulong candidate return delta / baselineBytes; } + + private static void ApplyBaseBaselineReductionPruning( + List candidates, + IsolationOptimizationOptions options, + IsolationOptimizationResult result) + { + var activeCandidates = GetActiveBaseBaselineCandidates(candidates); + if (activeCandidates.Count <= 1) + return; + + var belowThreshold = activeCandidates + .Where(x => x.SavingsRatio < options.MinMeaningfulBaseOnlyReductionRatio) + .OrderBy(x => x.Baseline.BitRange) + .ThenBy(x => x.SizeBytes) + .ThenBy(x => x.Baseline.Names[0], StringComparer.Ordinal) + .ToList(); + + if (belowThreshold.Count == 0) + return; + + if (belowThreshold.Count == activeCandidates.Count) + { + result.Notes.Add( + $"All active combination baselines had uncovered-tensor reduction below the meaningful threshold of {options.MinMeaningfulBaseOnlyReductionRatio:P2}. " + + "Deferring carrier pruning to tie/dominance/bad-trade comparison so the safest surviving carrier can be preserved."); + return; + } + + foreach (var candidate in belowThreshold) + { + if (!RuntimeSearchSpace.DisableCombinationBaseline(candidate.Baseline)) + continue; + + result.DisabledBaselines++; + result.Notes.Add( + $"Disabled combination baseline '{candidate.Baseline.Names[0]}' because uncovered-tensor reduction was only {candidate.SavingsRatio:P2}."); + } + } + + private static void ApplyBaseBaselineDominanceElimination( + List candidates, + IsolationOptimizationResult result) + { + var activeCandidates = GetActiveBaseBaselineCandidates(candidates); + if (activeCandidates.Count <= 1) + return; + + for (int i = 0; i < activeCandidates.Count; i++) + { + for (int j = 0; j < activeCandidates.Count; j++) + { + if (i == j) + continue; + + var a = activeCandidates[i]; + var b = activeCandidates[j]; + + bool sameSize = a.SizeBytes == b.SizeBytes; + bool sameOrSmaller = a.SizeBytes <= b.SizeBytes; + bool kldNoWorse = a.Kld <= b.Kld + IsolationPruningConfig.FloatingPointEpsilon; + bool pplNoWorse = Math.Abs(a.PplDeltaPercent) <= Math.Abs(b.PplDeltaPercent) + IsolationPruningConfig.FloatingPointEpsilon; + + bool effectivelyTied = + sameSize && + Math.Abs(a.Kld - b.Kld) <= IsolationPruningConfig.FloatingPointEpsilon && + Math.Abs(Math.Abs(a.PplDeltaPercent) - Math.Abs(b.PplDeltaPercent)) <= IsolationPruningConfig.FloatingPointEpsilon; + + bool saferTieWinner = effectivelyTied && a.Baseline.BitRange > b.Baseline.BitRange; + + bool strictlyBetter = + a.Kld + IsolationPruningConfig.FloatingPointEpsilon < b.Kld || + Math.Abs(a.PplDeltaPercent) + IsolationPruningConfig.FloatingPointEpsilon < Math.Abs(b.PplDeltaPercent) || + a.SizeBytes < b.SizeBytes || + saferTieWinner; + + if (!sameOrSmaller || !kldNoWorse || !pplNoWorse || !strictlyBetter) + continue; + + if (!RuntimeSearchSpace.DisableCombinationBaseline(b.Baseline)) + continue; + + result.DisabledBaselines++; + + if (saferTieWinner) + { + result.Notes.Add( + $"Disabled combination baseline '{b.Baseline.Names[0]}' because it tied '{a.Baseline.Names[0]}' on measured size/KLD/PPL, so the safer higher BitRange carrier was kept."); + } + else + { + result.Notes.Add( + $"Disabled combination baseline '{b.Baseline.Names[0]}' because '{a.Baseline.Names[0]}' was same-size-or-smaller and no worse on KLD/PPL."); + } + } + } + } + + private static void ApplyBaseBaselineBadTradeElimination( + List candidates, + IsolationOptimizationResult result) + { + var activeCandidates = GetActiveBaseBaselineCandidates(candidates); + if (activeCandidates.Count <= 1) + return; + + var sizeBuckets = BuildBaseBaselineSizeBuckets(activeCandidates); + if (sizeBuckets.Count == 0) + return; + + var acceptedAnchor = SelectBestBaseBaselineBucketSurvivor(sizeBuckets[0]); + if (acceptedAnchor == null) + return; + + for (int i = 1; i < sizeBuckets.Count; i++) + { + var bucketSurvivors = new List(); + + foreach (var candidate in sizeBuckets[i]) + { + if (RuntimeSearchSpace.IsCombinationBaselineDisabled(candidate.Baseline)) + continue; + + if (ShouldEliminateBaseBaselineAsBadTrade(acceptedAnchor, candidate, out var reason)) + { + if (RuntimeSearchSpace.DisableCombinationBaseline(candidate.Baseline)) + { + result.DisabledBaselines++; + result.Notes.Add( + $"Disabled combination baseline '{candidate.Baseline.Names[0]}' vs accepted carrier anchor '{acceptedAnchor.Baseline.Names[0]}'. {reason}"); + } + + continue; + } + + bucketSurvivors.Add(candidate); + } + + var promotedAnchor = SelectBestBaseBaselineBucketSurvivor(bucketSurvivors); + if (promotedAnchor != null) + acceptedAnchor = promotedAnchor; + } + } + + private static List GetActiveBaseBaselineCandidates(List candidates) + { + return candidates + .Where(x => !RuntimeSearchSpace.IsCombinationBaselineDisabled(x.Baseline)) + .OrderByDescending(x => x.Baseline.BitRange) + .ThenBy(x => x.SizeBytes) + .ThenBy(x => x.Kld) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) + .ToList(); + } + + private static List> BuildBaseBaselineSizeBuckets(List candidates) + { + return candidates + .GroupBy(x => x.SizeBytes) + .OrderByDescending(x => x.Key) + .Select(x => x + .OrderBy(c => c.Kld) + .ThenBy(c => Math.Abs(c.PplDeltaPercent)) + .ThenByDescending(c => c.Baseline.BitRange) + .ThenBy(c => c.Baseline.Names[0], StringComparer.Ordinal) + .ToList()) + .ToList(); + } + + private static BaseBaselineEvaluation? SelectBestBaseBaselineBucketSurvivor(List survivors) + { + return survivors + .OrderBy(x => x.Kld) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) + .ThenByDescending(x => x.Baseline.BitRange) + .ThenBy(x => x.Baseline.Names[0], StringComparer.Ordinal) + .FirstOrDefault(); + } + + private static bool ShouldEliminateBaseBaselineAsBadTrade( + BaseBaselineEvaluation anchor, + BaseBaselineEvaluation candidate, + out string reason) + { + reason = string.Empty; + + if (anchor.SizeBytes <= candidate.SizeBytes) + return false; + + double sizeDeltaPercent = ((double)anchor.SizeBytes - candidate.SizeBytes) / anchor.SizeBytes * 100.0; + if (sizeDeltaPercent > IsolationPruningConfig.BadTradeMaxSizeDeltaPercent) + return false; + + double anchorPplAbs = Math.Abs(anchor.PplDeltaPercent); + double candidatePplAbs = Math.Abs(candidate.PplDeltaPercent); + + double kldRatio = anchor.Kld <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : candidate.Kld / anchor.Kld; + + double pplRatio = anchorPplAbs <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : candidatePplAbs / anchorPplAbs; + + bool kldBadTrade = candidate.Kld > anchor.Kld * IsolationPruningConfig.BadTradeKldMultiplier; + bool pplBadTrade = candidatePplAbs > anchorPplAbs * IsolationPruningConfig.BadTradePplMultiplier; + + bool candidateMeaningfullyBetterKld = + candidate.Kld + IsolationPruningConfig.FloatingPointEpsilon < anchor.Kld * 0.90; + + bool candidateMeaningfullyBetterPpl = + candidatePplAbs + IsolationPruningConfig.FloatingPointEpsilon < anchorPplAbs * 0.90; + + bool mixedTradeoff = + (kldBadTrade && candidateMeaningfullyBetterPpl) || + (pplBadTrade && candidateMeaningfullyBetterKld); + + if (mixedTradeoff || (!kldBadTrade && !pplBadTrade)) + return false; + + reason = + $"Reason: small size gain ({sizeDeltaPercent:F2}%) but disproportionate damage (KLD x{kldRatio:F2}, |PPL| x{pplRatio:F2})."; + + return true; + } + private static double GetAggregateKld(BenchmarkSnapshot snapshot) { return snapshot.Benchmarks @@ -625,6 +857,15 @@ private sealed class GroupCandidateEvaluation public double PplDeltaPercent { get; set; } } + private sealed class BaseBaselineEvaluation + { + public BaselineQuants Baseline { get; set; } = default!; + public ulong SizeBytes { get; set; } + public double SavingsRatio { get; set; } + public double Kld { get; set; } + public double PplDeltaPercent { get; set; } + } + private sealed class BenchmarkSnapshot { public ulong SizeBytes { get; set; } diff --git a/MagicQuant/Services/PredictedCandidateEvaluationService.cs b/MagicQuant/Services/PredictedCandidateEvaluationService.cs new file mode 100644 index 0000000..b18af72 --- /dev/null +++ b/MagicQuant/Services/PredictedCandidateEvaluationService.cs @@ -0,0 +1,163 @@ +using MagicQuant.Models; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class PredictedTradeComparisonPolicy +{ + public int Compare(PredictedCandidateEvaluation left, PredictedCandidateEvaluation right) + { + if (!IsKldClose(left, right)) + return left.PredictedKldCost.CompareTo(right.PredictedKldCost); + + double sizeDeltaPercent = PercentDifference(left.PredictedSizeBytes, right.PredictedSizeBytes); + if (sizeDeltaPercent >= Config.SurvivalMeaningfulSizeBiasPercent && left.PredictedSizeBytes != right.PredictedSizeBytes) + return left.PredictedSizeBytes.CompareTo(right.PredictedSizeBytes); + + double pplDelta = Math.Abs(left.PredictedPplCost - right.PredictedPplCost); + if (pplDelta >= Config.SurvivalPplLargeDifferencePercent) + return left.PredictedPplCost.CompareTo(right.PredictedPplCost); + + return left.CompositeScore.CompareTo(right.CompositeScore); + } + + public bool Dominates(PredictedCandidateEvaluation better, PredictedCandidateEvaluation worse) + { + bool sizeOk = better.PredictedSizeBytes <= worse.PredictedSizeBytes; + bool kldBetter = better.PredictedKldCost <= worse.PredictedKldCost; + bool pplNotWorse = better.PredictedPplCost <= worse.PredictedPplCost + 1e-9; + + bool strict = better.PredictedSizeBytes < worse.PredictedSizeBytes || + better.PredictedKldCost < worse.PredictedKldCost || + better.PredictedPplCost < worse.PredictedPplCost; + + return sizeOk && kldBetter && pplNotWorse && strict; + } + + private static bool IsKldClose(PredictedCandidateEvaluation left, PredictedCandidateEvaluation right) + { + double diff = Math.Abs(left.PredictedKldCost - right.PredictedKldCost); + double absolute = Config.SurvivalKldCloseCallAbsoluteEpsilon; + double relative = Math.Min(left.PredictedKldCost, right.PredictedKldCost) * Config.SurvivalKldCloseCallRelativeFraction; + return diff <= Math.Max(absolute, relative); + } + + private static double PercentDifference(ulong left, ulong right) + { + if (left == 0 || right == 0) + return 0; + + double min = Math.Min(left, right); + double max = Math.Max(left, right); + return ((max - min) / min) * 100d; + } +} + +public sealed class PredictedCandidateEvaluationService +{ + private readonly HybridBenchmarkRepository _repository; + private readonly EffectiveCandidateStateResolverService _effectiveResolver; + + public PredictedCandidateEvaluationService( + HybridBenchmarkRepository repository, + EffectiveCandidateStateResolverService effectiveResolver) + { + _repository = repository; + _effectiveResolver = effectiveResolver; + } + + public async Task> EvaluateAsync( + IReadOnlyCollection configs, + CancellationToken ct = default) + { + var result = new List(configs.Count); + + var pureSnapshots = (await _repository.LoadPureBaselineSnapshotsAsync(ct)) + .GroupBy(x => x.BaselineFamily, StringComparer.Ordinal) + .ToDictionary(g => g.Key, g => g.OrderBy(x => x.SizeBytes).First(), StringComparer.Ordinal); + + var pureQ8 = pureSnapshots.TryGetValue(BaselineQuants.Q8_0.Names[0], out var q8Snap) ? q8Snap : null; + if (pureQ8 == null) + throw new InvalidOperationException("Prediction requires a learned pure Q8_0 benchmark anchor."); + + var isolationCache = new Dictionary(StringComparer.Ordinal); + + foreach (var config in configs) + { + var quant = (HybridQuant)config; + var effective = await _effectiveResolver.ResolveAsync(config, ct); + var notes = new List(effective.Warnings); + + BenchmarkSnapshotRecord baselineAnchor = pureSnapshots.TryGetValue(quant.BaseQuant.Names[0], out var baselineSnap) + ? baselineSnap + : pureQ8; + + ulong predictedSize = baselineAnchor.SizeBytes; + double predictedKld = baselineAnchor.Kld; + double predictedPpl = baselineAnchor.Ppl; + + foreach (var tensor in quant.Tensors) + { + tensor.ValidateOrThrow(); + + string isolationKey = $"{tensor.TGroup.UniqueId}:{tensor.OverrideMode}:{tensor.CandidateBaseline?.CanonicalKey}:{tensor.ExactTensorScheme?.Names[0]}"; + if (!isolationCache.TryGetValue(isolationKey, out var isolation)) + { + var isolationQuant = HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0); + + if (tensor.OverrideMode == HybridTensorOverrideMode.ExactTensorScheme) + isolationQuant.SetExactOverride(tensor.TGroup, tensor.ExactTensorScheme!); + else + isolationQuant.SetLearnedCandidateOverride(tensor.TGroup, tensor.CandidateBaseline!); + + isolation = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); + isolationCache[isolationKey] = isolation; + } + + if (isolation == null) + { + notes.Add($"Isolation benchmark missing for group '{tensor.TGroup.Name}'. Applied conservative penalty."); + predictedKld += 0.005d; + predictedPpl += 0.25d; + continue; + } + + long sizeDelta = (long)isolation.SizeBytes - (long)pureQ8.SizeBytes; + if (sizeDelta >= 0) + predictedSize += (ulong)sizeDelta; + else + predictedSize = predictedSize > (ulong)(-sizeDelta) ? predictedSize - (ulong)(-sizeDelta) : 0; + + predictedKld += Math.Max(0d, isolation.Kld - pureQ8.Kld); + predictedPpl += Math.Max(0d, isolation.Ppl - pureQ8.Ppl); + } + + if (Config.ManualMaxPredictedSizeBytes > 0 && predictedSize > Config.ManualMaxPredictedSizeBytes) + notes.Add($"Predicted size {predictedSize:N0} bytes exceeds configured manual ceiling {Config.ManualMaxPredictedSizeBytes:N0} bytes."); + + double sizeGb = predictedSize / 1024d / 1024d / 1024d; + double composite = (predictedKld * 10000d) + + (predictedPpl * Config.SurvivalTradeScorePplWeight) + + (sizeGb / Math.Max(0.01d, Config.SurvivalTradeScoreSizeBiasWeight)); + + result.Add(new PredictedCandidateEvaluation + { + Config = config, + Quant = quant, + PredictedSizeBytes = predictedSize, + PredictedKldCost = predictedKld, + PredictedPplCost = predictedPpl, + CompositeScore = composite, + EffectiveStateKey = effective.EffectiveStateKey, + HasUnknownMappings = effective.HasUnknownMappings, + BaseBitRange = quant.BaseQuant.BitRange, + IsPureBaseline = TensorConfigIdentity.IsPureBaseline(config), + Notes = notes + }); + } + + AnsiConsole.MarkupLine($"[grey]Prediction evaluation completed for[/] [cyan]{result.Count:N0}[/] [grey]remaining combinations.[/]"); + return result; + } +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 6580186..610835e 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1047,6 +1047,74 @@ public async Task EnsureBaseModelFileAsync(bool deleteProcess = false) } } + public async Task BuildExportArtifactAsync( + HybridQuant quant, + string outputPath, + bool forceRebuild = false, + CancellationToken ct = default) + { + if (quant == null) + throw new ArgumentNullException(nameof(quant)); + + if (string.IsNullOrWhiteSpace(outputPath)) + throw new InvalidOperationException("Export output path is required."); + + Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); + + if (!forceRebuild && File.Exists(outputPath) && new FileInfo(outputPath).Length > 0) + return outputPath; + + if (forceRebuild && File.Exists(outputPath)) + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + + await _cpuQuantLock.WaitAsync(ct); + try + { + string nativeBasePath = await EnsureBaseModelFileAsync(); + HybridQuant quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline + ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) + : quant; + + IReadOnlyDictionary? temporaryCarrierOverrides = null; + + if (quant.BaseQuant.IsExternalRepositoryBaseline) + { + string downloadedExternalBaselinePath = GetExternalBaselineCachePath(quant.BaseQuant); + await _huggingFaceBaselineService.DownloadBaselineAsync( + quant.BaseQuant, + downloadedExternalBaselinePath, + forceRedownload: false, + ct: ct); + + temporaryCarrierOverrides = TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: true); + + if (temporaryCarrierOverrides.Count == 0) + { + throw new InvalidOperationException( + $"Missing blanket learned mapping for external/custom baseline '{quant.BaseQuant.Names[0]}'. " + + "MagicQuant cannot export a hybrid from an external baseline until that baseline has been learned."); + } + } + + await RunLlamaQuantizeAsync( + inputFile: nativeBasePath, + outputFile: outputPath, + quant: quantToExecute, + temporaryCarrierOverrides: temporaryCarrierOverrides); + + await File.WriteAllTextAsync(outputPath + ".success.json", "{\"status\":\"success\"}", ct); + return outputPath; + } + finally + { + _cpuQuantLock.Release(); + } + } + + public async Task EnsurePureQ8ModelAsync() { string basePath = await EnsureBaseModelFileAsync(); diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs new file mode 100644 index 0000000..178c722 --- /dev/null +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -0,0 +1,93 @@ +using System.Text; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class ReadmeGenerationService +{ + public async Task GenerateAsync( + string outputDirectory, + string modelName, + IReadOnlyCollection exportedArtifacts, + IReadOnlyCollection benchmarkOverview, + CancellationToken ct = default) + { + Directory.CreateDirectory(outputDirectory); + string readmePath = Path.Combine(outputDirectory, "README.md"); + + var sb = new StringBuilder(); + sb.AppendLine($"# MagicQuant Hybrids (v2.0) - {modelName}"); + sb.AppendLine(); + sb.AppendLine("MagicQuant is **not** a quantization technique by itself."); + sb.AppendLine(); + sb.AppendLine("It is a search, judging, and hybrid-discovery system that learns from baseline families such as llama.cpp and external/custom baseline sources, then uses isolated empirical truth, pruning, and real benchmarking to keep the practical survivors."); + sb.AppendLine(); + sb.AppendLine("Sometimes a hybrid beats a pure baseline. Sometimes it does not. That is normal. The point is to pay the real benchmarking cost only where the trade looks genuinely worth it."); + sb.AppendLine(); + + sb.AppendLine("## Final surviving downloadable outputs"); + sb.AppendLine(); + AppendDownloadTable(sb, exportedArtifacts); + sb.AppendLine(); + + sb.AppendLine("## Benchmark overview"); + sb.AppendLine(); + AppendBenchmarkOverviewTable(sb, benchmarkOverview); + sb.AppendLine(); + + sb.AppendLine("## Dive Deeper"); + sb.AppendLine(); + sb.AppendLine("- Browse the project GitHub/Wiki for benchmark methodology, architecture notes, and planned pipeline improvements."); + sb.AppendLine("- If you spot a mistake, edge case, or a better practical trade, open an issue or share the artifact details so the comparison can be improved."); + sb.AppendLine(); + + sb.AppendLine("## Warning"); + sb.AppendLine(); + sb.AppendLine("External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under BF16 / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing."); + sb.AppendLine(); + sb.AppendLine("That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency, not as a universal judgment of the original creator's exact release artifact."); + sb.AppendLine(); + + sb.AppendLine("## Support"); + sb.AppendLine(); + sb.AppendLine("If this release helped you, a star, issue report, correction, or benchmark reproduction note is genuinely useful. Careful feedback matters more than hype, especially when a hybrid looks surprisingly good or surprisingly bad."); + + await File.WriteAllTextAsync(readmePath, sb.ToString(), ct); + AnsiConsole.MarkupLine($"[green]README generated:[/] {Markup.Escape(readmePath)}"); + return readmePath; + } + + private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection artifacts) + { + sb.AppendLine("| Name | Provider | Quant Family / Baseline | KLD | PPL | Size (GB) | Download |"); + sb.AppendLine("|---|---|---|---:|---:|---:|---|"); + + foreach (var artifact in artifacts.OrderBy(x => x.Snapshot.Kld).ThenBy(x => x.Snapshot.SizeBytes)) + { + string sizeGb = (artifact.Snapshot.SizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); + string download = artifact.IsExternalReference + ? $"[Link]({artifact.DownloadTarget})" + : $"[Link](./../../resolve/main/{artifact.FileName}?download=true)"; + + sb.AppendLine($"| {EscapePipe(artifact.DisplayName)} | {EscapePipe(artifact.ProviderName)} | {EscapePipe(artifact.BaselineFamily)} | {artifact.Snapshot.Kld:0.000000} | {artifact.Snapshot.Ppl:0.0000} | {sizeGb} | {download} |"); + } + } + + private static void AppendBenchmarkOverviewTable(StringBuilder sb, IReadOnlyCollection snapshots) + { + sb.AppendLine("| Name | Provider | Quant Family | KLD | PPL | Size (GB) |"); + sb.AppendLine("|---|---|---|---:|---:|---:|"); + + foreach (var snap in snapshots + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes)) + { + string sizeGb = (snap.SizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); + sb.AppendLine($"| {EscapePipe(snap.DisplayName)} | {EscapePipe(snap.ProviderName)} | {EscapePipe(snap.BaselineFamily)} | {snap.Kld:0.000000} | {snap.Ppl:0.0000} | {sizeGb} |"); + } + } + + private static string EscapePipe(string value) => value.Replace("|", "\\|"); +} diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs new file mode 100644 index 0000000..629db42 --- /dev/null +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -0,0 +1,148 @@ +using DuckDB.NET.Data; +using MagicQuant.Helpers; +using MQ.DB; +using MQ.DB.Models; + +namespace MagicQuant.Services; + +public sealed class RemainingCombinationStore +{ + private const string DbFileNamePrefix = "MagicQuant_Combinations"; + private const string TableName = "tensor_configs"; + + private static string ConnectionString => $"Data Source={Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName())}"; + + public string GetDatabaseFilePath() => Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName()); + + public async Task CountAsync(CancellationToken ct = default) + { + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); + + using var cmd = connection.CreateCommand(); + cmd.CommandText = $"SELECT COUNT(*) FROM {TableName};"; + return Convert.ToInt64(await cmd.ExecuteScalarAsync(ct) ?? 0L); + } + + public async Task> LoadAllAsync(CancellationToken ct = default) + { + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); + + var results = new List(); + + using var cmd = connection.CreateCommand(); + cmd.CommandText = $@" +SELECT BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter +FROM {TableName} +ORDER BY BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter;"; + + using var reader = await cmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + { + results.Add(new TensorConfig( + baseQuant: Convert.ToByte(reader.GetValue(0)), + embeddings: Convert.ToByte(reader.GetValue(1)), + lmHead: Convert.ToByte(reader.GetValue(2)), + attnQ: Convert.ToByte(reader.GetValue(3)), + attnKV: Convert.ToByte(reader.GetValue(4)), + attnOutput: Convert.ToByte(reader.GetValue(5)), + ffnUpGate: Convert.ToByte(reader.GetValue(6)), + ffnDown: Convert.ToByte(reader.GetValue(7)), + moeExperts: Convert.ToByte(reader.GetValue(8)), + moeRouter: Convert.ToByte(reader.GetValue(9)))); + } + + return results; + } + + public async Task ReplaceAllAsync(IReadOnlyCollection configs, string reason, CancellationToken ct = default) + { + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); + await RecreateTableAsync(connection, ct); + + using var tx = connection.BeginTransaction(); + using var insert = connection.CreateCommand(); + insert.CommandText = $@" +INSERT INTO {TableName} +(BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter) +VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + + foreach (var config in configs) + { + insert.Parameters.Clear(); + insert.Parameters.Add(new DuckDBParameter { Value = config.BaseQuant }); + insert.Parameters.Add(new DuckDBParameter { Value = config.Embeddings }); + insert.Parameters.Add(new DuckDBParameter { Value = config.LmHead }); + insert.Parameters.Add(new DuckDBParameter { Value = config.AttnQ }); + insert.Parameters.Add(new DuckDBParameter { Value = config.AttnKV }); + insert.Parameters.Add(new DuckDBParameter { Value = config.AttnOutput }); + insert.Parameters.Add(new DuckDBParameter { Value = config.FfnUpGate }); + insert.Parameters.Add(new DuckDBParameter { Value = config.FfnDown }); + insert.Parameters.Add(new DuckDBParameter { Value = config.MoeExperts }); + insert.Parameters.Add(new DuckDBParameter { Value = config.MoeRouter }); + await insert.ExecuteNonQueryAsync(ct); + } + + tx.Commit(); + } + + private static async Task ConfigureFastLoadSessionAsync(DuckDBConnection connection, CancellationToken ct) + { + using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = "SET preserve_insertion_order = false;"; + await cmd.ExecuteNonQueryAsync(ct); + } + + using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = $"SET threads = {Math.Max(1, Environment.ProcessorCount)};"; + await cmd.ExecuteNonQueryAsync(ct); + } + } + + private static async Task RecreateTableAsync(DuckDBConnection connection, CancellationToken ct) + { + using var createCmd = connection.CreateCommand(); + createCmd.CommandText = $@" +DROP TABLE IF EXISTS {TableName}; +CREATE TABLE {TableName} ( + BaseQuant UTINYINT, + Embeddings UTINYINT, + LmHead UTINYINT, + AttnQ UTINYINT, + AttnKV UTINYINT, + AttnOutput UTINYINT, + FfnUpGate UTINYINT, + FfnDown UTINYINT, + MoeExperts UTINYINT, + MoeRouter UTINYINT +);"; + await createCmd.ExecuteNonQueryAsync(ct); + } + + private static string GetDuckDbDirectory() + { + if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + return Cache.ModelMagicQuantDirectory!; + + if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) + return Cache.MagicQuantDirectory!; + + throw new InvalidOperationException( + "Neither Cache.ModelMagicQuantDirectory nor Cache.MagicQuantDirectory is set."); + } + + private static string BuildContextAwareDuckDbFileName() + { + string model = string.IsNullOrWhiteSpace(Cache.CurrentModelId) ? "unknown-model" : Cache.CurrentModelId; + string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixIdentityHash ?? "imatrix-unknown") : "no-imatrix"; + string hp = RuntimeSearchSpace.AllowHighPrecisionHybrids ? "hp-on" : "hp-off"; + return $"{DbFileNamePrefix}_{model}_{imatrix}_{hp}.duckdb"; + } +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index da2d60e..778580d 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -81,7 +81,7 @@ evolution: collapse_multiplier: 1.5 # If remaining combinations are <= this number, brute-force the end. - brute_force_final_combination_threshold: 2000 + brute_force_final_combination_threshold: 100 isolation_pruning: # NOTE: @@ -124,6 +124,45 @@ prediction: # manual_max_predicted_size_bytes: 4294967296 manual_max_predicted_size_bytes: 0 +output: + # Optional explicit output directory. + # If blank, MagicQuant will default to: + # /MagicQuant/Final_Outputs + output_dir: + + # Prefix used when generating exported GGUF file names. + output_name_prefix: model + + # By default MagicQuant will not locally export pure learned external/custom baselines + # such as Unsloth. They remain upstream references in the README/output unless enabled. + # + # Set true if you explicitly want MagicQuant to rebuild/export those external learned + # baselines locally under MagicQuant-controlled conditions (for example when testing a + # modified model where the upstream artifact does not really exist for your case). + export_external_learned_baselines: false + +survival: + # Hard cap for survivors retained per BitRange bucket before brute-force benchmarking. + max_selected_choices_per_bucket: 5 + + # Size advantage percentage used as a meaningful tie-bias during bucket-local trade scoring. + meaningful_size_bias_percent: 1.0 + + # Absolute and relative KLD closeness thresholds for close-call handling. + kld_close_call_absolute_epsilon: 0.00075 + kld_close_call_relative_fraction: 0.02 + + # PPL only matters more strongly when the difference is actually meaningful. + ppl_large_difference_percent: 0.75 + + # Centralized trade scoring weights. + trade_score_size_bias_weight: 1.25 + trade_score_ppl_weight: 0.15 + +identity: + architecture_family_name: + allow_architecture_family_alias_override: false + baselines: # ---------------------------------------------------------- # standard_baselines_mode options @@ -227,5 +266,4 @@ baselines: # # Example note: # # If the repo does not actually contain IQ3_XS, do not reference it. # # Use only filenames that truly exist in the repository. - # - [] \ No newline at end of file + [] diff --git a/MagicQuant/config.dev.backup.yaml b/MagicQuant/config.dev.backup.yaml deleted file mode 100644 index 8be7a40..0000000 --- a/MagicQuant/config.dev.backup.yaml +++ /dev/null @@ -1,50 +0,0 @@ -# Dev config. This is auto-selected in DEBUG when --config is not supplied. - -paths: - magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ - llama_root: - llama_bin: - convert_script: - external_baseline_cache_dir_name: ExternalBaselines - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_relearn_baseline_tensor_mappings: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -imatrix: - imatrix_url: - dataset_repo: - dataset_split: text - dataset_config: - dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1m.jsonl - -evolution: - max_data_collected_per_category: 5 - max_survival_rounds: 4 - collapse_multiplier: 1.5 - brute_force_final_combination_threshold: 2000 - -isolation_pruning: - minimum_isolation_reduction_to_continue_ratio: 0.04 - minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 - maximum_isolation_ppl_delta_percent: 5.0 - maximum_isolation_kld: 0.1 - bad_trade_max_size_delta_percent: 4.0 - bad_trade_kld_multiplier: 2.5 - bad_trade_ppl_multiplier: 3.5 - floating_point_epsilon: 1.0e-8 - minimum_meaningful_base_only_reduction_ratio: 0.01 - -prediction: - manual_max_predicted_size_bytes: 0 - -baselines: - standard_baselines_mode: all - enabled_standard_learning_baselines: [] - enabled_standard_combination_carriers: [] - enabled_standard_explicit_group_candidates: [] - custom_repositories: [] diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index d437279..81e3efb 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -24,7 +24,7 @@ evolution: max_data_collected_per_category: 5 max_survival_rounds: 4 collapse_multiplier: 1.5 - brute_force_final_combination_threshold: 2000 + brute_force_final_combination_threshold: 100 isolation_pruning: minimum_isolation_reduction_to_continue_ratio: 0.04 @@ -40,6 +40,25 @@ isolation_pruning: prediction: manual_max_predicted_size_bytes: 0 +output: + # Leave blank to default to /MagicQuant/Final_Outputs + output_dir: + output_name_prefix: model + export_external_learned_baselines: false + +survival: + max_selected_choices_per_bucket: 5 + meaningful_size_bias_percent: 1.0 + kld_close_call_absolute_epsilon: 0.00075 + kld_close_call_relative_fraction: 0.02 + ppl_large_difference_percent: 0.75 + trade_score_size_bias_weight: 1.25 + trade_score_ppl_weight: 0.15 + +identity: + architecture_family_name: Qwen3-4B-Instruct-2507 + allow_architecture_family_alias_override: false + baselines: standard_baselines_mode: all enabled_standard_learning_baselines: [] @@ -99,4 +118,4 @@ baselines: display_name: Unsloth_IQ3_XXS_for_IQ3_XS allow_as_learning_baseline: true allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true \ No newline at end of file + allow_as_explicit_group_candidate: true From 6eb452c1963b771388f51c2957941000c2848c41 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 22 Apr 2026 23:46:09 -0400 Subject: [PATCH 115/258] getting somewhere --- MQ.DB/Models/BaselineQuants.cs | 10 +- .../Services/BitRangeBucketBuilderService.cs | 47 +++++ .../Services/BucketLocalPruningService.cs | 123 ++++++++++- .../CombinationSurvivalPipelineService.cs | 9 +- .../Services/IsolationOptimizationService.cs | 42 +++- .../PredictedCandidateEvaluationService.cs | 199 +++++++++++++++--- MagicQuant/Services/QuantDatabaseService.cs | 111 ++++++++-- 7 files changed, 469 insertions(+), 72 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index e14711a..ccafcf1 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -93,19 +93,19 @@ private static BaselineQuants Create( Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 8, 11); public static readonly BaselineQuants Q6_K = - Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, true, false, false, 6, 10); + Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, true, true, false, 6, 10); public static readonly BaselineQuants Q5_K = - Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, true, false, false, 5, 9); + Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 5, 9); public static readonly BaselineQuants Q4_K_M = - Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, true, false, false, 4, 8); + Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 8); public static readonly BaselineQuants IQ4_NL = - Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, true, false, false, 4, 7); + Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 7); public static readonly BaselineQuants IQ4_XS = - Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, true, false, false, 4, 6); + Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 6); public static readonly BaselineQuants IQ3_S = Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 5); diff --git a/MagicQuant/Services/BitRangeBucketBuilderService.cs b/MagicQuant/Services/BitRangeBucketBuilderService.cs index 9fa4487..46ca1c5 100644 --- a/MagicQuant/Services/BitRangeBucketBuilderService.cs +++ b/MagicQuant/Services/BitRangeBucketBuilderService.cs @@ -51,6 +51,19 @@ public async Task BuildAsync( }); } + if (candidates.Count > 0) + { + ulong minPredicted = candidates.Min(x => x.PredictedSizeBytes); + ulong maxPredicted = candidates.Max(x => x.PredictedSizeBytes); + AnsiConsole.MarkupLine($"[grey]Predicted size spread:[/] [cyan]{FormatBytes(minPredicted)}[/] [grey]..[/] [cyan]{FormatBytes(maxPredicted)}[/]"); + + foreach (var baseBitRange in candidates.GroupBy(x => x.BaseBitRange).OrderBy(x => x.Key)) + { + AnsiConsole.MarkupLine( + $"[grey]Predicted candidates using base BitRange {baseBitRange.Key}:[/] [cyan]{baseBitRange.Count():N0}[/]"); + } + } + if (buckets.Count == 0) { AnsiConsole.MarkupLine("[yellow]No usable BitRange buckets could be built. Survival will fall back to global predicted sorting if required.[/]"); @@ -62,6 +75,12 @@ public async Task BuildAsync( }; } + foreach (var bucket in buckets) + { + AnsiConsole.MarkupLine( + $"[grey]BitRange bucket {bucket.Key} -> lower_anchor={bucket.LowerAnchorSizeBytes:N0} upper_anchor={bucket.UpperAnchorSizeBytes:N0}[/]"); + } + var bucketed = new List(); var unbucketed = new List(); @@ -97,6 +116,28 @@ public async Task BuildAsync( }); } + int populatedBucketCount = 0; + foreach (var bucket in buckets) + { + int count = bucketed.Count(x => x.Bucket.Key == bucket.Key); + if (count > 0) + populatedBucketCount++; + + AnsiConsole.MarkupLine( + $"[grey]Bucket assignment {bucket.Key}:[/] [cyan]{count:N0}[/] [grey]candidate(s)[/]"); + } + + if (unbucketed.Count > 0) + { + AnsiConsole.MarkupLine($"[yellow]Unbucketed predicted candidates:[/] [cyan]{unbucketed.Count:N0}[/]"); + } + + if (candidates.Count > 0 && populatedBucketCount <= 1) + { + AnsiConsole.MarkupLine( + "[bold yellow]Bucket diagnostic warning:[/] [grey]Only one BitRange bucket received predicted candidates. This usually means carrier pruning or predicted-size anchoring collapsed the search into one neighborhood.[/]"); + } + return new BitRangeBucketBuildResult { Buckets = buckets, @@ -104,4 +145,10 @@ public async Task BuildAsync( UnbucketedCandidates = unbucketed }; } + + private static string FormatBytes(ulong bytes) + { + double gb = bytes / 1024d / 1024d / 1024d; + return $"{gb:F2} GB ({bytes:N0} bytes)"; + } } diff --git a/MagicQuant/Services/BucketLocalPruningService.cs b/MagicQuant/Services/BucketLocalPruningService.cs index 8d78b3b..9b4c0eb 100644 --- a/MagicQuant/Services/BucketLocalPruningService.cs +++ b/MagicQuant/Services/BucketLocalPruningService.cs @@ -12,7 +12,9 @@ public sealed class BucketLocalPruningService { private readonly PredictedTradeComparisonPolicy _policy = new(); - public BucketLocalPruningResult Prune(BitRangeBucketBuildResult buildResult) + public BucketLocalPruningResult Prune( + BitRangeBucketBuildResult buildResult, + IReadOnlyCollection? pureBaselines = null) { var diagnostics = new List(); var survivors = new List(); @@ -24,6 +26,8 @@ public BucketLocalPruningResult Prune(BitRangeBucketBuildResult buildResult) .Select(x => x.Evaluation) .ToList(); + var bucketPureBaselines = GetPureBaselinesForBucket(bucket, pureBaselines); + var diag = new BucketPruneDiagnostics { BucketKey = bucket.Key, @@ -94,18 +98,63 @@ public BucketLocalPruningResult Prune(BitRangeBucketBuildResult buildResult) } } - var capped = keptAfterPractical + // Hybrids that beat a pure baseline in this bucket are bonus keeps and do not count + // against the configured bucket cap. + var bonusHybrids = keptAfterPractical + .Where(x => !x.IsPureBaseline) + .Where(x => BeatsAnyPureBaseline(x, bucketPureBaselines)) + .OrderBy(x => x, Comparer.Create(_policy.Compare)) + .ToList(); + + if (bonusHybrids.Count > 0) + AddReasonCount(diag, "bonus-hybrid-kept", bonusHybrids.Count); + + var regularPool = keptAfterPractical + .Where(x => !bonusHybrids.Any(b => TensorConfigIdentity.ToKey(b.Config) == TensorConfigIdentity.ToKey(x.Config))) .OrderBy(x => x, Comparer.Create(_policy.Compare)) + .ToList(); + + var viableRegular = new List(); + var shadowedRegular = new List(); + + foreach (var candidate in regularPool) + { + if (IsShadowedByPureBaseline(candidate, bucketPureBaselines)) + shadowedRegular.Add(candidate); + else + viableRegular.Add(candidate); + } + + var capped = viableRegular .Take(Config.MaxSelectedChoicesPerBucket) .ToList(); - int capRemoved = keptAfterPractical.Count - capped.Count; + // Refill from shadowed candidates only if we still have open slots in this bucket. + if (capped.Count < Config.MaxSelectedChoicesPerBucket) + { + capped.AddRange( + shadowedRegular + .Take(Config.MaxSelectedChoicesPerBucket - capped.Count)); + } + else if (shadowedRegular.Count > 0) + { + AddReasonCount(diag, "pure-baseline-shadowed", shadowedRegular.Count); + } + + int capRemoved = Math.Max(0, viableRegular.Count - Math.Min(viableRegular.Count, Config.MaxSelectedChoicesPerBucket)); if (capRemoved > 0) - diag.CountReason("bucket-cap"); + AddReasonCount(diag, "bucket-cap", capRemoved); - diag.KeptCount = capped.Count; - diag.RemovedCount = diag.IncomingCount - diag.KeptCount; - survivors.AddRange(capped); + var finalBucketSurvivors = bonusHybrids + .Concat(capped) + .GroupBy(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal) + .Select(g => g.First()) + .OrderBy(x => x, Comparer.Create(_policy.Compare)) + .ToList(); + + diag.KeptCount = finalBucketSurvivors.Count; + diag.RemovedCount = Math.Max(0, diag.IncomingCount - diag.KeptCount); + survivors.AddRange(finalBucketSurvivors); diagnostics.Add(diag); } @@ -123,6 +172,66 @@ public BucketLocalPruningResult Prune(BitRangeBucketBuildResult buildResult) }; } + private static List GetPureBaselinesForBucket( + BitRangeBucketDefinition bucket, + IReadOnlyCollection? pureBaselines) + { + if (pureBaselines == null || pureBaselines.Count == 0) + return new List(); + + ulong lowerBound = bucket.LowerAnchorSizeBytes; + ulong upperBound = bucket.UpperAnchorSizeBytes; + + return pureBaselines + .Where(x => x.SizeBytes >= lowerBound && x.SizeBytes <= upperBound) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ToList(); + } + + private static bool BeatsAnyPureBaseline( + PredictedCandidateEvaluation candidate, + IReadOnlyCollection pureBaselines) + { + foreach (var baseline in pureBaselines) + { + bool sameOrSmaller = candidate.PredictedSizeBytes <= baseline.SizeBytes; + bool betterKld = candidate.PredictedKldCost + 1e-9 < baseline.Kld; + bool betterPpl = candidate.PredictedPplCost + 1e-9 < baseline.Ppl; + + if (sameOrSmaller && (betterKld || betterPpl)) + return true; + } + + return false; + } + + private static bool IsShadowedByPureBaseline( + PredictedCandidateEvaluation candidate, + IReadOnlyCollection pureBaselines) + { + foreach (var baseline in pureBaselines) + { + bool sameOrSmaller = baseline.SizeBytes <= candidate.PredictedSizeBytes; + bool kldNoWorse = baseline.Kld <= candidate.PredictedKldCost + 1e-9; + bool pplNoWorse = baseline.Ppl <= candidate.PredictedPplCost + 1e-9; + bool strict = baseline.SizeBytes < candidate.PredictedSizeBytes || + baseline.Kld + 1e-9 < candidate.PredictedKldCost || + baseline.Ppl + 1e-9 < candidate.PredictedPplCost; + + if (sameOrSmaller && kldNoWorse && pplNoWorse && strict) + return true; + } + + return false; + } + + private static void AddReasonCount(BucketPruneDiagnostics diag, string reason, int count) + { + for (int i = 0; i < count; i++) + diag.CountReason(reason); + } + private static double PercentDifference(ulong left, ulong right) { if (left == 0 || right == 0) diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index e6fa126..a4835de 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -52,7 +52,9 @@ public async Task RunAsync(CancellationToken PrintBucketAnchors(bucketBuild.Buckets); - var bucketPruneResult = _bucketPruner.Prune(bucketBuild); + var pureBaselineSnapshots = await _benchmarkRepository.LoadPureBaselineSnapshotsAsync(ct); + AnsiConsole.MarkupLine($"[grey]Pure baseline context loaded for bucket pruning:[/] [cyan]{pureBaselineSnapshots.Count:N0}[/]"); + var bucketPruneResult = _bucketPruner.Prune(bucketBuild, pureBaselineSnapshots); foreach (var diag in bucketPruneResult.Diagnostics) report.BucketDiagnostics.Add(diag); @@ -80,6 +82,9 @@ public async Task RunAsync(CancellationToken survivors = globalCut; } + if (survivors.Count == 0) + AnsiConsole.MarkupLine("[yellow]Warning:[/] Survival pipeline produced zero kept candidates after bucket pruning. Check pure-baseline-shadowed / bonus-hybrid-kept diagnostics."); + await _combinationStore.ReplaceAllAsync(survivors.Select(x => x.Config).ToList(), "prediction-survival", ct); foreach (var diag in report.BucketDiagnostics) @@ -226,4 +231,4 @@ private static List BalanceDownToThreshold( .Take(threshold) .ToList(); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index b281774..eb0460a 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -294,14 +294,25 @@ public async Task AnalyzeAndApplyFinalAsync( foreach (var item in baseOnlyPlans) { - var snap = await LoadSnapshotAsync(item.Quant, ct); - if (snap == null) - continue; - var baseline = BaselineQuants.FromId(item.TestedBaselineId!.Value); if (!baseline.IsCombinationCarrierCandidate) continue; + // IMPORTANT: + // Combination carrier pruning must reason from the pure baseline artifact first, + // not the base-only isolation blanket. The blanket is useful to detect uncovered + // tensors, but when all meaningful tensors are covered by groups it will often tie + // across multiple carriers and collapse the entire search upward into Q8. + // + // For carrier dominance / bad-trade we therefore prefer the real pure-baseline + // benchmark, and only fall back to the base-only isolation snapshot if the pure + // artifact benchmark is missing for some reason. + var pureSnap = await LoadSnapshotAsync(HybridQuant.CreatePureBaseline(baseline), ct); + var baseOnlySnap = await LoadSnapshotAsync(item.Quant, ct); + var snap = pureSnap ?? baseOnlySnap; + if (snap == null) + continue; + baseBaselineCandidates.Add(new BaseBaselineEvaluation { Baseline = baseline, @@ -310,6 +321,12 @@ public async Task AnalyzeAndApplyFinalAsync( Kld = GetAggregateKld(snap), PplDeltaPercent = GetAggregatePplDeltaPercent(snap, nativeBaseline) }); + + if (pureSnap == null && baseOnlySnap != null) + { + result.Notes.Add( + $"Carrier '{baseline.Names[0]}' fell back to base-only isolation metrics because a pure baseline benchmark snapshot was not found."); + } } ApplyBaseBaselineReductionPruning(baseBaselineCandidates, options, result); @@ -650,6 +667,7 @@ private static void ApplyBaseBaselineDominanceElimination( var a = activeCandidates[i]; var b = activeCandidates[j]; + bool sameBitRange = a.Baseline.BitRange == b.Baseline.BitRange; bool sameSize = a.SizeBytes == b.SizeBytes; bool sameOrSmaller = a.SizeBytes <= b.SizeBytes; bool kldNoWorse = a.Kld <= b.Kld + IsolationPruningConfig.FloatingPointEpsilon; @@ -660,13 +678,21 @@ private static void ApplyBaseBaselineDominanceElimination( Math.Abs(a.Kld - b.Kld) <= IsolationPruningConfig.FloatingPointEpsilon && Math.Abs(Math.Abs(a.PplDeltaPercent) - Math.Abs(b.PplDeltaPercent)) <= IsolationPruningConfig.FloatingPointEpsilon; - bool saferTieWinner = effectivelyTied && a.Baseline.BitRange > b.Baseline.BitRange; + // Cross-BitRange ties must be preserved. Those ties are exactly what allows + // downstream range-aware prediction/bucketing to explore multiple size neighborhoods. + if (effectivelyTied && !sameBitRange) + continue; + + bool deterministicSameBucketTieWinner = + effectivelyTied && + sameBitRange && + string.Compare(a.Baseline.CanonicalKey, b.Baseline.CanonicalKey, StringComparison.Ordinal) < 0; bool strictlyBetter = a.Kld + IsolationPruningConfig.FloatingPointEpsilon < b.Kld || Math.Abs(a.PplDeltaPercent) + IsolationPruningConfig.FloatingPointEpsilon < Math.Abs(b.PplDeltaPercent) || a.SizeBytes < b.SizeBytes || - saferTieWinner; + deterministicSameBucketTieWinner; if (!sameOrSmaller || !kldNoWorse || !pplNoWorse || !strictlyBetter) continue; @@ -676,10 +702,10 @@ private static void ApplyBaseBaselineDominanceElimination( result.DisabledBaselines++; - if (saferTieWinner) + if (deterministicSameBucketTieWinner) { result.Notes.Add( - $"Disabled combination baseline '{b.Baseline.Names[0]}' because it tied '{a.Baseline.Names[0]}' on measured size/KLD/PPL, so the safer higher BitRange carrier was kept."); + $"Disabled same-BitRange tied combination baseline '{b.Baseline.Names[0]}' because '{a.Baseline.Names[0]}' was chosen as the deterministic representative for BitRange {a.Baseline.BitRange}."); } else { diff --git a/MagicQuant/Services/PredictedCandidateEvaluationService.cs b/MagicQuant/Services/PredictedCandidateEvaluationService.cs index b18af72..ef04a35 100644 --- a/MagicQuant/Services/PredictedCandidateEvaluationService.cs +++ b/MagicQuant/Services/PredictedCandidateEvaluationService.cs @@ -73,12 +73,12 @@ public async Task> EvaluateAsync( { var result = new List(configs.Count); - var pureSnapshots = (await _repository.LoadPureBaselineSnapshotsAsync(ct)) - .GroupBy(x => x.BaselineFamily, StringComparer.Ordinal) - .ToDictionary(g => g.Key, g => g.OrderBy(x => x.SizeBytes).First(), StringComparer.Ordinal); + var pureSnapshots = await _repository.LoadPureBaselineSnapshotsAsync(ct); + var pureByBaselineId = pureSnapshots + .GroupBy(x => x.Quant.BaseQuant.UniqueId) + .ToDictionary(g => g.Key, g => g.OrderBy(x => x.SizeBytes).ThenBy(x => x.Kld).First()); - var pureQ8 = pureSnapshots.TryGetValue(BaselineQuants.Q8_0.Names[0], out var q8Snap) ? q8Snap : null; - if (pureQ8 == null) + if (!pureByBaselineId.TryGetValue(BaselineQuants.Q8_0.UniqueId, out var pureQ8)) throw new InvalidOperationException("Prediction requires a learned pure Q8_0 benchmark anchor."); var isolationCache = new Dictionary(StringComparer.Ordinal); @@ -89,9 +89,12 @@ public async Task> EvaluateAsync( var effective = await _effectiveResolver.ResolveAsync(config, ct); var notes = new List(effective.Warnings); - BenchmarkSnapshotRecord baselineAnchor = pureSnapshots.TryGetValue(quant.BaseQuant.Names[0], out var baselineSnap) + byte normalizedBaseId = NormalizeBaselineIdForIsolation(quant.BaseQuant.UniqueId); + BenchmarkSnapshotRecord baselineAnchor = pureByBaselineId.TryGetValue(quant.BaseQuant.UniqueId, out var baselineSnap) ? baselineSnap - : pureQ8; + : pureByBaselineId.TryGetValue(normalizedBaseId, out var normalizedSnap) + ? normalizedSnap + : pureQ8; ulong predictedSize = baselineAnchor.SizeBytes; double predictedKld = baselineAnchor.Kld; @@ -101,44 +104,43 @@ public async Task> EvaluateAsync( { tensor.ValidateOrThrow(); - string isolationKey = $"{tensor.TGroup.UniqueId}:{tensor.OverrideMode}:{tensor.CandidateBaseline?.CanonicalKey}:{tensor.ExactTensorScheme?.Names[0]}"; - if (!isolationCache.TryGetValue(isolationKey, out var isolation)) + BenchmarkSnapshotRecord? targetIsolation = await LoadIsolationSnapshotAsync(tensor, isolationCache, ct); + if (targetIsolation == null) { - var isolationQuant = HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0); - - if (tensor.OverrideMode == HybridTensorOverrideMode.ExactTensorScheme) - isolationQuant.SetExactOverride(tensor.TGroup, tensor.ExactTensorScheme!); - else - isolationQuant.SetLearnedCandidateOverride(tensor.TGroup, tensor.CandidateBaseline!); - - isolation = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); - isolationCache[isolationKey] = isolation; - } - - if (isolation == null) - { - notes.Add($"Isolation benchmark missing for group '{tensor.TGroup.Name}'. Applied conservative penalty."); + notes.Add($"Isolation benchmark missing for target override on group '{tensor.TGroup.Name}'. Applied conservative penalty."); predictedKld += 0.005d; predictedPpl += 0.25d; continue; } - long sizeDelta = (long)isolation.SizeBytes - (long)pureQ8.SizeBytes; + BenchmarkSnapshotRecord? baseIsolation = await LoadBaseIsolationSnapshotAsync(quant.BaseQuant, tensor.TGroup, isolationCache, ct); + if (baseIsolation == null) + { + notes.Add($"Base-family isolation benchmark missing for '{quant.BaseQuant.Names[0]}' on group '{tensor.TGroup.Name}'. Using target isolation without relative improvement credit."); + baseIsolation = targetIsolation; + } + + long sizeDelta = (long)targetIsolation.SizeBytes - (long)baseIsolation.SizeBytes; if (sizeDelta >= 0) predictedSize += (ulong)sizeDelta; else predictedSize = predictedSize > (ulong)(-sizeDelta) ? predictedSize - (ulong)(-sizeDelta) : 0; - predictedKld += Math.Max(0d, isolation.Kld - pureQ8.Kld); - predictedPpl += Math.Max(0d, isolation.Ppl - pureQ8.Ppl); + predictedKld += (targetIsolation.Kld - baseIsolation.Kld); + predictedPpl += (targetIsolation.Ppl - baseIsolation.Ppl); + + ApplyProportionalTradeWeighting(baseIsolation, targetIsolation, ref predictedKld, ref predictedPpl); } + if (predictedKld < 0d) + predictedKld = 0d; + if (Config.ManualMaxPredictedSizeBytes > 0 && predictedSize > Config.ManualMaxPredictedSizeBytes) notes.Add($"Predicted size {predictedSize:N0} bytes exceeds configured manual ceiling {Config.ManualMaxPredictedSizeBytes:N0} bytes."); double sizeGb = predictedSize / 1024d / 1024d / 1024d; double composite = (predictedKld * 10000d) + - (predictedPpl * Config.SurvivalTradeScorePplWeight) + + (Math.Max(0d, predictedPpl) * Config.SurvivalTradeScorePplWeight) + (sizeGb / Math.Max(0.01d, Config.SurvivalTradeScoreSizeBiasWeight)); result.Add(new PredictedCandidateEvaluation @@ -157,7 +159,148 @@ public async Task> EvaluateAsync( }); } - AnsiConsole.MarkupLine($"[grey]Prediction evaluation completed for[/] [cyan]{result.Count:N0}[/] [grey]remaining combinations.[/]"); + PrintPredictionDiagnostics(result); return result; } + + private async Task LoadBaseIsolationSnapshotAsync( + BaselineQuants baseQuant, + TensorGroup group, + Dictionary cache, + CancellationToken ct) + { + byte normalizedId = NormalizeBaselineIdForIsolation(baseQuant.UniqueId); + var normalizedBaseline = BaselineQuants.FromId(normalizedId); + return await LoadLearnedCandidateIsolationAsync(group, normalizedBaseline, cache, ct); + } + + private async Task LoadIsolationSnapshotAsync( + HybridTensor tensor, + Dictionary cache, + CancellationToken ct) + { + return tensor.OverrideMode switch + { + HybridTensorOverrideMode.ExactTensorScheme => await LoadExactIsolationAsync(tensor.TGroup, tensor.ExactTensorScheme!, cache, ct), + _ => await LoadLearnedCandidateIsolationAsync(tensor.TGroup, tensor.CandidateBaseline!, cache, ct) + }; + } + + private async Task LoadLearnedCandidateIsolationAsync( + TensorGroup group, + BaselineQuants baseline, + Dictionary cache, + CancellationToken ct) + { + string cacheKey = $"learned:{group.UniqueId}:{baseline.CanonicalKey}"; + if (cache.TryGetValue(cacheKey, out var existing)) + return existing; + + var isolationQuant = HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0); + isolationQuant.SetLearnedCandidateOverride(group, baseline); + + var snapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); + cache[cacheKey] = snapshot; + return snapshot; + } + + private async Task LoadExactIsolationAsync( + TensorGroup group, + TensorWeightScheme exactScheme, + Dictionary cache, + CancellationToken ct) + { + string cacheKey = $"exact:{group.UniqueId}:{exactScheme.Names[0]}"; + if (cache.TryGetValue(cacheKey, out var existing)) + return existing; + + var isolationQuant = HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0); + isolationQuant.SetExactOverride(group, exactScheme); + + var snapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); + cache[cacheKey] = snapshot; + return snapshot; + } + + private static byte NormalizeBaselineIdForIsolation(byte baselineId) + { + var baseline = BaselineQuants.FromId(baselineId); + if (!baseline.IsExternalRepositoryBaseline) + return baselineId; + + var builtIn = BaselineQuants.ResolveBuiltInStandardBaseline(baseline.QuantizeBaseArgumentName) + ?? BaselineQuants.ResolveBuiltInStandardBaseline(baseline.Names[0]); + + return builtIn?.UniqueId ?? baselineId; + } + + private static void ApplyProportionalTradeWeighting( + BenchmarkSnapshotRecord baseIsolation, + BenchmarkSnapshotRecord targetIsolation, + ref double predictedKld, + ref double predictedPpl) + { + if (targetIsolation.SizeBytes >= baseIsolation.SizeBytes) + return; + + double savingsPercent = ((double)baseIsolation.SizeBytes - targetIsolation.SizeBytes) / baseIsolation.SizeBytes * 100d; + if (savingsPercent <= 0d) + return; + + double kldDelta = targetIsolation.Kld - baseIsolation.Kld; + double pplDelta = targetIsolation.Ppl - baseIsolation.Ppl; + + if (kldDelta <= 0d && pplDelta <= 0d) + return; + + double damage = Math.Max(0d, kldDelta) * 1000d + Math.Max(0d, pplDelta); + double damagePerSavings = damage / Math.Max(0.10d, savingsPercent); + + if (damagePerSavings <= 1.0d) + return; + + double multiplier = Math.Min(2.75d, 1.0d + ((damagePerSavings - 1.0d) * 0.20d)); + predictedKld += Math.Max(0d, kldDelta) * (multiplier - 1.0d); + predictedPpl += Math.Max(0d, pplDelta) * (multiplier - 1.0d); + } + + private static void PrintPredictionDiagnostics(IReadOnlyList evaluations) + { + int pureCount = evaluations.Count(x => x.IsPureBaseline); + int hybridCount = evaluations.Count - pureCount; + AnsiConsole.MarkupLine($"[grey]Prediction composition:[/] [cyan]{pureCount:N0}[/] [grey]pure[/] / [cyan]{hybridCount:N0}[/] [grey]hybrid[/]"); + + if (evaluations.Count == 0) + { + AnsiConsole.MarkupLine("[grey]Prediction evaluation completed for[/] [cyan]0[/] [grey]remaining combinations.[/]"); + return; + } + + ulong min = evaluations.Min(x => x.PredictedSizeBytes); + ulong max = evaluations.Max(x => x.PredictedSizeBytes); + AnsiConsole.MarkupLine($"[grey]Prediction size spread:[/] [cyan]{ToGb(min):F2}[/] [grey]GB ..[/] [cyan]{ToGb(max):F2}[/] [grey]GB[/]"); + + foreach (var byBitRange in evaluations.GroupBy(x => x.BaseBitRange).OrderBy(x => x.Key)) + { + ulong bitMin = byBitRange.Min(x => x.PredictedSizeBytes); + ulong bitMax = byBitRange.Max(x => x.PredictedSizeBytes); + AnsiConsole.MarkupLine( + $"[grey]Base BitRange {byBitRange.Key} prediction spread:[/] [cyan]{byBitRange.Count():N0}[/] [grey]candidate(s),[/] [cyan]{ToGb(bitMin):F2}[/] [grey]GB ..[/] [cyan]{ToGb(bitMax):F2}[/] [grey]GB[/]"); + + foreach (var sample in byBitRange.OrderBy(x => x.PredictedSizeBytes).ThenBy(x => x.PredictedKldCost).Take(3)) + { + AnsiConsole.MarkupLine( + $" [grey]- sample:[/] {Markup.Escape(sample.Quant.BaseQuant.Names[0])} [grey]| predicted[/] [cyan]{ToGb(sample.PredictedSizeBytes):F2}[/] [grey]GB | KLD[/] [cyan]{sample.PredictedKldCost:G6}[/] [grey]| PPL[/] [cyan]{sample.PredictedPplCost:F4}[/]"); + } + } + + if (min == max && evaluations.Select(x => x.BaseBitRange).Distinct().Count() > 1) + { + AnsiConsole.MarkupLine("[yellow]Prediction diagnostic warning:[/] all candidates resolved to the same predicted size even though multiple base BitRanges remain. This usually means the relative size predictor is still collapsing too aggressively.[/]"); + } + + AnsiConsole.MarkupLine($"[grey]Prediction evaluation completed for[/] [cyan]{evaluations.Count:N0}[/] [grey]remaining combinations.[/]"); + } + + private static double ToGb(ulong bytes) => bytes / 1024d / 1024d / 1024d; } diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 2738af2..d079f40 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -244,6 +244,7 @@ public async Task PrunePredictedLargerThanQ8Async( } var kept = new List(rows.Count); + var predictedByBase = new Dictionary>(); ulong sizeCeilingBytes = Config.ManualMaxPredictedSizeBytes > 0 ? Config.ManualMaxPredictedSizeBytes @@ -252,10 +253,35 @@ public async Task PrunePredictedLargerThanQ8Async( foreach (var row in rows) { ulong predicted = predictionContext.Predict(row); + + if (!predictedByBase.TryGetValue(row.BaseQuant, out var bucket)) + { + bucket = new List(); + predictedByBase[row.BaseQuant] = bucket; + } + + bucket.Add(predicted); + if (predicted <= sizeCeilingBytes) kept.Add(row); } + if (predictedByBase.Count > 0) + { + ulong globalMin = predictedByBase.Values.SelectMany(x => x).Min(); + ulong globalMax = predictedByBase.Values.SelectMany(x => x).Max(); + AnsiConsole.MarkupLine($"[grey]Stage-1 predicted size spread:[/] [cyan]{globalMin / 1024d / 1024d / 1024d:F2}[/] [grey]GB ..[/] [cyan]{globalMax / 1024d / 1024d / 1024d:F2}[/] [grey]GB[/]"); + + foreach (var kv in predictedByBase.OrderBy(x => BaselineQuants.FromId(x.Key).BitRange).ThenBy(x => x.Key)) + { + var baseline = BaselineQuants.FromId(kv.Key); + ulong min = kv.Value.Min(); + ulong max = kv.Value.Max(); + AnsiConsole.MarkupLine( + $"[grey]Stage-1 base {Markup.Escape(baseline.Names[0])} (BitRange {baseline.BitRange}) ->[/] [cyan]{kv.Value.Count:N0}[/] [grey]candidate(s),[/] [cyan]{min / 1024d / 1024d / 1024d:F2}[/] [grey]GB ..[/] [cyan]{max / 1024d / 1024d / 1024d:F2}[/] [grey]GB[/]"); + } + } + long removed = rows.Count - kept.Count; if (removed <= 0) @@ -554,7 +580,17 @@ private static void AppendRows( if (carrier == null) return null; - var deltaByGroupAndCandidate = new Dictionary<(byte GroupId, byte CandidateId), long>(); + var pureBaselineSizes = new Dictionary(); + foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) + { + var snap = await LoadSnapshotByQuantAsync(db, model.Id, imatrixDefinitionId, HybridQuant.CreatePureBaseline(baseline), ct); + if (snap != null) + pureBaselineSizes[baseline.UniqueId] = snap.SizeBytes; + } + + pureBaselineSizes[BaselineQuants.Q8_0.UniqueId] = pureQ8.SizeBytes; + + var sizeByGroupAndCandidate = new Dictionary<(byte GroupId, byte CandidateId), ulong>(); var groupPlans = fullPlan.Plans .Where(x => x.Kind == RequiredSampleKind.GroupIsolationProbe || x.Kind == RequiredSampleKind.GroupIsolationContinuation) @@ -570,14 +606,14 @@ private static void AppendRows( if (snap == null) continue; - long delta = (long)snap.SizeBytes - (long)carrier.SizeBytes; - deltaByGroupAndCandidate[(plan.TargetGroupId!.Value, plan.TestedCandidateId!.Value)] = delta; + sizeByGroupAndCandidate[(plan.TargetGroupId!.Value, plan.TestedCandidateId!.Value)] = snap.SizeBytes; } return new PredictionContext( pureQ8BaseSize: pureQ8.SizeBytes, + pureBaselineSizes: pureBaselineSizes, carrierBaseOnlySize: carrier.SizeBytes, - deltas: deltaByGroupAndCandidate); + sizesByGroupAndCandidate: sizeByGroupAndCandidate); } private static async Task LoadSnapshotByQuantAsync( @@ -632,34 +668,42 @@ private sealed class InsertProgress private sealed class PredictionContext { - private readonly Dictionary<(byte GroupId, byte CandidateId), long> _deltas; + private readonly Dictionary _pureBaselineSizes; + private readonly Dictionary<(byte GroupId, byte CandidateId), ulong> _sizesByGroupAndCandidate; public ulong PureQ8BaseSize { get; } public ulong CarrierBaseOnlySize { get; } public PredictionContext( ulong pureQ8BaseSize, + Dictionary pureBaselineSizes, ulong carrierBaseOnlySize, - Dictionary<(byte GroupId, byte CandidateId), long> deltas) + Dictionary<(byte GroupId, byte CandidateId), ulong> sizesByGroupAndCandidate) { PureQ8BaseSize = pureQ8BaseSize; CarrierBaseOnlySize = carrierBaseOnlySize; - _deltas = deltas; + _pureBaselineSizes = pureBaselineSizes; + _sizesByGroupAndCandidate = sizesByGroupAndCandidate; } public ulong Predict(TensorConfig config) { - long total = (long)CarrierBaseOnlySize; - - AddDelta(TReg.Embeddings.UniqueId, config.Embeddings, ref total); - AddDelta(TReg.LmHead.UniqueId, config.LmHead, ref total); - AddDelta(TReg.AttnQ.UniqueId, config.AttnQ, ref total); - AddDelta(TReg.AttnKV.UniqueId, config.AttnKV, ref total); - AddDelta(TReg.AttnOutput.UniqueId, config.AttnOutput, ref total); - AddDelta(TReg.FfnUpGate.UniqueId, config.FfnUpGate, ref total); - AddDelta(TReg.FfnDown.UniqueId, config.FfnDown, ref total); - AddDelta(TReg.MoeExperts.UniqueId, config.MoeExperts, ref total); - AddDelta(TReg.MoeRouter.UniqueId, config.MoeRouter, ref total); + byte normalizedBaseId = NormalizeBaselineIdForIsolation(config.BaseQuant); + long total = (long)(_pureBaselineSizes.TryGetValue(config.BaseQuant, out var directBase) + ? directBase + : _pureBaselineSizes.TryGetValue(normalizedBaseId, out var normalizedBase) + ? normalizedBase + : PureQ8BaseSize); + + ApplyRelativeDelta(TReg.Embeddings.UniqueId, normalizedBaseId, config.Embeddings, ref total); + ApplyRelativeDelta(TReg.LmHead.UniqueId, normalizedBaseId, config.LmHead, ref total); + ApplyRelativeDelta(TReg.AttnQ.UniqueId, normalizedBaseId, config.AttnQ, ref total); + ApplyRelativeDelta(TReg.AttnKV.UniqueId, normalizedBaseId, config.AttnKV, ref total); + ApplyRelativeDelta(TReg.AttnOutput.UniqueId, normalizedBaseId, config.AttnOutput, ref total); + ApplyRelativeDelta(TReg.FfnUpGate.UniqueId, normalizedBaseId, config.FfnUpGate, ref total); + ApplyRelativeDelta(TReg.FfnDown.UniqueId, normalizedBaseId, config.FfnDown, ref total); + ApplyRelativeDelta(TReg.MoeExperts.UniqueId, normalizedBaseId, config.MoeExperts, ref total); + ApplyRelativeDelta(TReg.MoeRouter.UniqueId, normalizedBaseId, config.MoeRouter, ref total); if (total < 0) total = 0; @@ -667,13 +711,36 @@ public ulong Predict(TensorConfig config) return (ulong)total; } - private void AddDelta(byte groupId, byte candidateId, ref long total) + private void ApplyRelativeDelta(byte groupId, byte baseCandidateId, byte candidateId, ref long total) { - if (candidateId == BaselineQuants.BF16_Hybrid.UniqueId || candidateId == BaselineQuants.F16_Hybrid.UniqueId) + if (BaselineQuants.IsNullTensorConfigGroupSlot(candidateId) || + candidateId == BaselineQuants.BF16_Hybrid.UniqueId || + candidateId == BaselineQuants.F16_Hybrid.UniqueId) + return; + + byte normalizedCandidateId = NormalizeBaselineIdForIsolation(candidateId); + if (normalizedCandidateId == baseCandidateId) + return; + + if (!_sizesByGroupAndCandidate.TryGetValue((groupId, normalizedCandidateId), out var candidateSize)) return; - if (_deltas.TryGetValue((groupId, candidateId), out long delta)) - total += delta; + if (!_sizesByGroupAndCandidate.TryGetValue((groupId, baseCandidateId), out var baseSize)) + return; + + total += (long)candidateSize - (long)baseSize; + } + + private static byte NormalizeBaselineIdForIsolation(byte baselineId) + { + var baseline = BaselineQuants.FromId(baselineId); + if (!baseline.IsExternalRepositoryBaseline) + return baselineId; + + var builtIn = BaselineQuants.ResolveBuiltInStandardBaseline(baseline.QuantizeBaseArgumentName) + ?? BaselineQuants.ResolveBuiltInStandardBaseline(baseline.Names[0]); + + return builtIn?.UniqueId ?? baselineId; } } } \ No newline at end of file From 5fc4e8db8a6f9647f949441fc0bc7a6d85f4e079 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 23 Apr 2026 11:34:07 -0400 Subject: [PATCH 116/258] Trying to fix predictive bucket logic --- .../Services/BucketLocalPruningService.cs | 175 +++++++++++++----- .../FinalRealBenchmarkEliminationService.cs | 115 +++++++++++- MagicQuant/config.dev.yaml | 2 +- 3 files changed, 231 insertions(+), 61 deletions(-) diff --git a/MagicQuant/Services/BucketLocalPruningService.cs b/MagicQuant/Services/BucketLocalPruningService.cs index 9b4c0eb..63d32ae 100644 --- a/MagicQuant/Services/BucketLocalPruningService.cs +++ b/MagicQuant/Services/BucketLocalPruningService.cs @@ -1,4 +1,5 @@ using MagicQuant.Models; +using MQ.DB.Models; namespace MagicQuant.Services; @@ -26,8 +27,6 @@ public BucketLocalPruningResult Prune( .Select(x => x.Evaluation) .ToList(); - var bucketPureBaselines = GetPureBaselinesForBucket(bucket, pureBaselines); - var diag = new BucketPruneDiagnostics { BucketKey = bucket.Key, @@ -50,13 +49,12 @@ public BucketLocalPruningResult Prune( var ordered = g.OrderBy(x => x, Comparer.Create(_policy.Compare)).ToList(); int removed = ordered.Count - 1; if (removed > 0) - diag.CountReason("effective-duplicate"); + AddReasonCount(diag, "effective-duplicate", removed); return ordered[0]; }) .ToList(); var dominancePruned = new List(deduped); - for (int i = dominancePruned.Count - 1; i >= 0; i--) { var current = dominancePruned[i]; @@ -82,12 +80,13 @@ public BucketLocalPruningResult Prune( foreach (var candidate in orderedByPracticalTrade) { bool sameNeighborhood = - Math.Abs(candidate.PredictedKldCost - best.PredictedKldCost) <= Math.Max(Config.SurvivalKldCloseCallAbsoluteEpsilon, best.PredictedKldCost * Config.SurvivalKldCloseCallRelativeFraction) && + Math.Abs(candidate.PredictedKldCost - best.PredictedKldCost) <= Math.Max( + Config.SurvivalKldCloseCallAbsoluteEpsilon, + best.PredictedKldCost * Config.SurvivalKldCloseCallRelativeFraction) && Math.Abs(candidate.PredictedPplCost - best.PredictedPplCost) <= Config.SurvivalPplLargeDifferencePercent && PercentDifference(candidate.PredictedSizeBytes, best.PredictedSizeBytes) < Config.SurvivalMeaningfulSizeBiasPercent; bool obviouslyJunk = candidate.CompositeScore > best.CompositeScore * 1.65d && sameNeighborhood; - if (obviouslyJunk) { diag.CountReason("practical-trade"); @@ -98,63 +97,55 @@ public BucketLocalPruningResult Prune( } } - // Hybrids that beat a pure baseline in this bucket are bonus keeps and do not count - // against the configured bucket cap. + var bucketPureBaselines = FilterPureBaselinesForBucket(bucket, pureBaselines); + + // Keep truly special hybrids as bonus survivors. They do not consume the bucket cap. var bonusHybrids = keptAfterPractical .Where(x => !x.IsPureBaseline) - .Where(x => BeatsAnyPureBaseline(x, bucketPureBaselines)) + .Where(x => bucketPureBaselines.Count == 0 || BeatsAnyPureBaseline(x, bucketPureBaselines)) + .Where(x => !IsShadowedByPureBaseline(x, bucketPureBaselines)) .OrderBy(x => x, Comparer.Create(_policy.Compare)) + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) .ToList(); if (bonusHybrids.Count > 0) - AddReasonCount(diag, "bonus-hybrid-kept", bonusHybrids.Count); - - var regularPool = keptAfterPractical - .Where(x => !bonusHybrids.Any(b => TensorConfigIdentity.ToKey(b.Config) == TensorConfigIdentity.ToKey(x.Config))) - .OrderBy(x => x, Comparer.Create(_policy.Compare)) - .ToList(); - - var viableRegular = new List(); - var shadowedRegular = new List(); - - foreach (var candidate in regularPool) { - if (IsShadowedByPureBaseline(candidate, bucketPureBaselines)) - shadowedRegular.Add(candidate); - else - viableRegular.Add(candidate); + survivors.AddRange(bonusHybrids); + diag.Notes.Add($"bonus-hybrid-kept={bonusHybrids.Count:N0}"); } - var capped = viableRegular - .Take(Config.MaxSelectedChoicesPerBucket) + var remainingPool = keptAfterPractical + .Where(x => bonusHybrids.All(b => TensorConfigIdentity.ToKey(b.Config) != TensorConfigIdentity.ToKey(x.Config))) .ToList(); - // Refill from shadowed candidates only if we still have open slots in this bucket. - if (capped.Count < Config.MaxSelectedChoicesPerBucket) + int shadowedRemoved = 0; + if (bucketPureBaselines.Count > 0) { - capped.AddRange( - shadowedRegular - .Take(Config.MaxSelectedChoicesPerBucket - capped.Count)); - } - else if (shadowedRegular.Count > 0) - { - AddReasonCount(diag, "pure-baseline-shadowed", shadowedRegular.Count); + remainingPool = remainingPool + .Where(x => + { + bool shadowed = IsShadowedByPureBaseline(x, bucketPureBaselines); + if (shadowed) + shadowedRemoved++; + return !shadowed; + }) + .ToList(); + + if (shadowedRemoved > 0) + AddReasonCount(diag, "pure-baseline-shadowed", shadowedRemoved); } - int capRemoved = Math.Max(0, viableRegular.Count - Math.Min(viableRegular.Count, Config.MaxSelectedChoicesPerBucket)); + int bucketBudget = Math.Max(0, Config.MaxSelectedChoicesPerBucket); + var capped = SelectDiversifiedBySizeBands(remainingPool, bucket, bucketBudget); + + int capRemoved = Math.Max(0, remainingPool.Count - capped.Count); if (capRemoved > 0) AddReasonCount(diag, "bucket-cap", capRemoved); - var finalBucketSurvivors = bonusHybrids - .Concat(capped) - .GroupBy(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal) - .Select(g => g.First()) - .OrderBy(x => x, Comparer.Create(_policy.Compare)) - .ToList(); + survivors.AddRange(capped); - diag.KeptCount = finalBucketSurvivors.Count; + diag.KeptCount = bonusHybrids.Count + capped.Count; diag.RemovedCount = Math.Max(0, diag.IncomingCount - diag.KeptCount); - survivors.AddRange(finalBucketSurvivors); diagnostics.Add(diag); } @@ -172,21 +163,103 @@ public BucketLocalPruningResult Prune( }; } - private static List GetPureBaselinesForBucket( + private List SelectDiversifiedBySizeBands( + IReadOnlyList candidates, + BitRangeBucketDefinition bucket, + int budget) + { + if (budget <= 0 || candidates.Count == 0) + return new List(); + + var ordered = candidates + .OrderBy(x => x.PredictedSizeBytes) + .ThenBy(x => x.PredictedKldCost) + .ThenBy(x => x.PredictedPplCost) + .ThenBy(x => x.CompositeScore) + .ToList(); + + // First pass: pull a Pareto-ish size frontier so obvious smaller-size wins get first dibs. + var frontier = new List(); + double bestKldSeen = double.PositiveInfinity; + foreach (var candidate in ordered) + { + bool materiallyBetterKld = candidate.PredictedKldCost + Config.SurvivalKldCloseCallAbsoluteEpsilon < bestKldSeen; + bool meaningfullySmallerThanLast = frontier.Count == 0 || + PercentDifference(candidate.PredictedSizeBytes, frontier[^1].PredictedSizeBytes) >= Config.SurvivalMeaningfulSizeBiasPercent; + + if (materiallyBetterKld || meaningfullySmallerThanLast) + { + frontier.Add(candidate); + if (candidate.PredictedKldCost < bestKldSeen) + bestKldSeen = candidate.PredictedKldCost; + } + } + + var selected = new List(); + ulong lower = bucket.LowerAnchorSizeBytes; + ulong upper = bucket.UpperAnchorSizeBytes > lower ? bucket.UpperAnchorSizeBytes : lower + 1UL; + double span = Math.Max(1d, upper - lower); + + // Second pass: reserve one slot per size band so we do not just take the five most Q8-adjacent items. + var bands = new Dictionary>(); + foreach (var candidate in frontier) + { + double normalized = Math.Clamp((candidate.PredictedSizeBytes - lower) / span, 0d, 0.999999d); + int band = Math.Min(budget - 1, (int)Math.Floor(normalized * budget)); + if (!bands.TryGetValue(band, out var list)) + { + list = new List(); + bands[band] = list; + } + + list.Add(candidate); + } + + foreach (var band in bands.OrderBy(x => x.Key)) + { + var best = band.Value + .OrderBy(x => x, Comparer.Create(_policy.Compare)) + .First(); + + if (selected.All(x => TensorConfigIdentity.ToKey(x.Config) != TensorConfigIdentity.ToKey(best.Config))) + selected.Add(best); + + if (selected.Count >= budget) + return selected; + } + + // Final fill: if we still have room, backfill from the overall frontier, then the full pool. + foreach (var candidate in frontier + .OrderBy(x => x, Comparer.Create(_policy.Compare)) + .Concat(ordered.OrderBy(x => x, Comparer.Create(_policy.Compare)))) + { + if (selected.Count >= budget) + break; + + if (selected.Any(x => TensorConfigIdentity.ToKey(x.Config) == TensorConfigIdentity.ToKey(candidate.Config))) + continue; + + selected.Add(candidate); + } + + return selected; + } + + private static List FilterPureBaselinesForBucket( BitRangeBucketDefinition bucket, IReadOnlyCollection? pureBaselines) { if (pureBaselines == null || pureBaselines.Count == 0) return new List(); - ulong lowerBound = bucket.LowerAnchorSizeBytes; - ulong upperBound = bucket.UpperAnchorSizeBytes; + ulong lower = bucket.LowerAnchorSizeBytes; + ulong upper = bucket.UpperAnchorSizeBytes; - return pureBaselines - .Where(x => x.SizeBytes >= lowerBound && x.SizeBytes <= upperBound) - .OrderBy(x => x.Kld) - .ThenBy(x => x.SizeBytes) + var inBucket = pureBaselines + .Where(x => x.SizeBytes >= lower && x.SizeBytes <= upper) .ToList(); + + return inBucket.Count > 0 ? inBucket : pureBaselines.ToList(); } private static bool BeatsAnyPureBaseline( diff --git a/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs b/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs index 6ddae84..3dac477 100644 --- a/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs +++ b/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs @@ -1,4 +1,6 @@ +using MagicQuant.Helpers; using MagicQuant.Models; +using MQ.DB.Models; namespace MagicQuant.Services; @@ -6,25 +8,25 @@ public sealed class FinalRealBenchmarkEliminationService { public FinalRealEliminationResult Eliminate(IReadOnlyCollection snapshots) { - var ordered = snapshots + var uniqueByConfig = snapshots .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) .OrderBy(x => x.Kld) .ThenBy(x => x.SizeBytes) .ThenBy(x => x.Ppl) + .ThenBy(x => x.DisplayName, StringComparer.Ordinal) .ToList(); - var survivors = new List(); var eliminated = new List(); + var collapsed = CollapseEquivalentTruths(uniqueByConfig, eliminated); - for (int i = 0; i < ordered.Count; i++) + var survivors = new List(); + + for (int i = 0; i < collapsed.Count; i++) { - var current = ordered[i]; - bool dominated = ordered + var current = collapsed[i]; + bool dominated = collapsed .Where((_, index) => index != i) - .Any(other => - other.SizeBytes <= current.SizeBytes && - other.Kld < current.Kld && - other.Ppl < current.Ppl); + .Any(other => Dominates(other, current)); if (dominated) eliminated.Add(current); @@ -35,11 +37,106 @@ public FinalRealEliminationResult Eliminate(IReadOnlyCollection TensorConfigIdentity.ToKey(x.Config)) .OrderBy(x => x.Kld) .ThenBy(x => x.SizeBytes) .ThenBy(x => x.Ppl) + .ThenByDescending(x => x.Quant.BaseQuant.BitRange) + .ThenByDescending(x => x.Quant.BaseQuant.ExplicitCandidateSortOrder) + .ThenBy(x => x.IsHybrid) + .ThenBy(x => x.IsExternalPureBaseline) + .ThenBy(x => x.DisplayName, StringComparer.Ordinal) .ToList(), Eliminated = eliminated + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .ToList() }; } + + private static List CollapseEquivalentTruths( + IReadOnlyList ordered, + List eliminated) + { + var kept = new List(); + var used = new bool[ordered.Count]; + + for (int i = 0; i < ordered.Count; i++) + { + if (used[i]) + continue; + + var seed = ordered[i]; + var tied = new List { seed }; + used[i] = true; + + for (int j = i + 1; j < ordered.Count; j++) + { + if (used[j]) + continue; + + if (!AreEquivalentTruths(seed, ordered[j])) + continue; + + tied.Add(ordered[j]); + used[j] = true; + } + + if (tied.Count == 1) + { + kept.Add(seed); + continue; + } + + var representative = tied + .OrderByDescending(GetSafetyRank) + .ThenBy(x => x.IsHybrid) + .ThenBy(x => x.IsExternalPureBaseline) + .ThenBy(x => x.ProviderName, StringComparer.Ordinal) + .ThenBy(x => x.DisplayName, StringComparer.Ordinal) + .First(); + + kept.Add(representative); + + foreach (var loser in tied) + { + if (!ReferenceEquals(loser, representative)) + eliminated.Add(loser); + } + } + + return kept; + } + + private static bool Dominates(BenchmarkSnapshotRecord better, BenchmarkSnapshotRecord worse) + { + bool sameOrSmaller = better.SizeBytes <= worse.SizeBytes; + bool strictlyBetterKld = better.Kld + IsolationPruningConfig.FloatingPointEpsilon < worse.Kld; + bool strictlyBetterPpl = better.Ppl + IsolationPruningConfig.FloatingPointEpsilon < worse.Ppl; + return sameOrSmaller && strictlyBetterKld && strictlyBetterPpl; + } + + private static bool AreEquivalentTruths(BenchmarkSnapshotRecord left, BenchmarkSnapshotRecord right) + { + if (left.SizeBytes != right.SizeBytes) + return false; + + return Math.Abs(left.Kld - right.Kld) <= IsolationPruningConfig.FloatingPointEpsilon && + Math.Abs(left.Ppl - right.Ppl) <= IsolationPruningConfig.FloatingPointEpsilon; + } + + private static int GetSafetyRank(BenchmarkSnapshotRecord snapshot) + { + var baseline = snapshot.Quant.BaseQuant; + + // Prefer the safest / most default representative when multiple rows have identical truth. + // 1) Higher BitRange is safer. + // 2) Higher ExplicitCandidateSortOrder wins ties inside the same BitRange. + // 3) Pure baseline beats hybrid when the measured truth is identical. + // 4) Internal/non-external beats external pure reference when still tied. + int rank = baseline.BitRange * 10_000; + rank += baseline.ExplicitCandidateSortOrder * 10; + rank += snapshot.IsHybrid ? 0 : 2; + rank += snapshot.IsExternalPureBaseline ? 0 : 1; + return rank; + } } diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 81e3efb..08b5e49 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -24,7 +24,7 @@ evolution: max_data_collected_per_category: 5 max_survival_rounds: 4 collapse_multiplier: 1.5 - brute_force_final_combination_threshold: 100 + brute_force_final_combination_threshold: 12000 isolation_pruning: minimum_isolation_reduction_to_continue_ratio: 0.04 From 2e2d44667882a8839e4f4218ae94df6290fbffd1 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 23 Apr 2026 13:09:54 -0400 Subject: [PATCH 117/258] Should be fixed with isolated base quants logic and pruning hopefully. --- MQ.DB/Models/BaselineQuants.cs | 10 +- .../Services/BitRangeBucketBuilderService.cs | 8 +- .../Services/HybridBenchmarkRepository.cs | 33 ++++- .../Services/IsolationOptimizationService.cs | 132 ++++++++++++++++-- .../PredictedCandidateEvaluationService.cs | 46 +++++- MagicQuant/Services/QuantDatabaseService.cs | 83 +++++++++-- MagicQuant/config.dev.yaml | 4 +- 7 files changed, 271 insertions(+), 45 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index ccafcf1..a48fcc1 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -93,19 +93,19 @@ private static BaselineQuants Create( Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 8, 11); public static readonly BaselineQuants Q6_K = - Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, true, true, false, 6, 10); + Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, false, true, false, 6, 10); public static readonly BaselineQuants Q5_K = - Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 5, 9); + Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, false, true, false, 5, 9); public static readonly BaselineQuants Q4_K_M = - Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 8); + Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 8); public static readonly BaselineQuants IQ4_NL = - Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 7); + Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 7); public static readonly BaselineQuants IQ4_XS = - Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, true, true, false, 4, 6); + Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 6); public static readonly BaselineQuants IQ3_S = Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 5); diff --git a/MagicQuant/Services/BitRangeBucketBuilderService.cs b/MagicQuant/Services/BitRangeBucketBuilderService.cs index 46ca1c5..d8dc13c 100644 --- a/MagicQuant/Services/BitRangeBucketBuilderService.cs +++ b/MagicQuant/Services/BitRangeBucketBuilderService.cs @@ -23,13 +23,13 @@ public async Task BuildAsync( IReadOnlyCollection candidates, CancellationToken ct = default) { - var pureBaselines = await _repository.LoadPureBaselineSnapshotsAsync(ct); - var baselineAnchors = pureBaselines + var baseOnlyCarriers = await _repository.LoadBaseOnlyCarrierSnapshotsAsync(ct); + var baselineAnchors = baseOnlyCarriers .GroupBy(x => x.Quant.BaseQuant.BitRange) .Select(g => new { BitRange = g.Key, - Snapshot = g.OrderBy(x => x.SizeBytes).ThenBy(x => x.Kld).First() + Snapshot = g.OrderBy(x => x.Kld).ThenBy(x => Math.Abs(x.Ppl)).ThenByDescending(x => x.Quant.BaseQuant.BitRange).First() }) .OrderBy(x => x.BitRange) .ToList(); @@ -151,4 +151,4 @@ private static string FormatBytes(ulong bytes) double gb = bytes / 1024d / 1024d / 1024d; return $"{gb:F2} GB ({bytes:N0} bytes)"; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/HybridBenchmarkRepository.cs b/MagicQuant/Services/HybridBenchmarkRepository.cs index 7381ed2..d48ad22 100644 --- a/MagicQuant/Services/HybridBenchmarkRepository.cs +++ b/MagicQuant/Services/HybridBenchmarkRepository.cs @@ -108,6 +108,37 @@ public async Task> LoadPureBaselineSnapshotsAsync( .ToList(); } + + public async Task> LoadBaseOnlyCarrierSnapshotsAsync(CancellationToken ct = default) + { + var result = new List(); + var activeGroups = TReg.All + .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + + foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) + { + var quant = HybridQuant.CreateExactBlanket( + baseQuant: baseline, + groups: activeGroups, + exactScheme: nativeExactScheme); + + var snapshot = await LoadBenchmarkSnapshotAsync((TensorConfig)quant, ct); + if (snapshot != null) + result.Add(snapshot); + } + + return result + .GroupBy(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal) + .Select(g => g.First()) + .OrderByDescending(x => x.Quant.BaseQuant.BitRange) + .ThenBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ToList(); + } + public async Task FindLatestSuccessfulOutputPathAsync(TensorConfig config, CancellationToken ct = default) { await using var db = new MagicQuantContext(); @@ -268,4 +299,4 @@ public static string BuildDisplayName(HybridQuant quant) .Select(x => (int?)x.Id) .FirstOrDefaultAsync(ct); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index eb0460a..13e8d16 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -299,39 +299,54 @@ public async Task AnalyzeAndApplyFinalAsync( continue; // IMPORTANT: - // Combination carrier pruning must reason from the pure baseline artifact first, - // not the base-only isolation blanket. The blanket is useful to detect uncovered - // tensors, but when all meaningful tensors are covered by groups it will often tie - // across multiple carriers and collapse the entire search upward into Q8. + // Base-combination carrier isolation must primarily reason from the base-only blanket + // snapshot for the tested carrier, where known groups are forced back to native/BF16 + // and only uncovered tensors remain exposed to the base quant choice. // - // For carrier dominance / bad-trade we therefore prefer the real pure-baseline - // benchmark, and only fall back to the base-only isolation snapshot if the pure - // artifact benchmark is missing for some reason. - var pureSnap = await LoadSnapshotAsync(HybridQuant.CreatePureBaseline(baseline), ct); + // If a model's group coverage effectively captures everything meaningful, these + // base-only snapshots should tie or nearly tie across carriers. Pure fully-quantized + // baseline artifacts are still useful as reference context, but they must not be used + // as the primary pruning metric here because that would conflate fully-quantized + // baseline quality/size with uncovered-tensor-only carrier isolation truth. var baseOnlySnap = await LoadSnapshotAsync(item.Quant, ct); - var snap = pureSnap ?? baseOnlySnap; + var pureSnap = await LoadSnapshotAsync(HybridQuant.CreatePureBaseline(baseline), ct); + var snap = baseOnlySnap ?? pureSnap; if (snap == null) continue; + var usedBaseOnly = baseOnlySnap != null; + var usedPureFallback = !usedBaseOnly && pureSnap != null; + baseBaselineCandidates.Add(new BaseBaselineEvaluation { Baseline = baseline, SizeBytes = snap.SizeBytes, SavingsRatio = ComputeReductionRatio(nativeBaseline.SizeBytes, snap.SizeBytes), Kld = GetAggregateKld(snap), - PplDeltaPercent = GetAggregatePplDeltaPercent(snap, nativeBaseline) + PplDeltaPercent = GetAggregatePplDeltaPercent(snap, nativeBaseline), + UsedBaseOnlySnapshot = usedBaseOnly, + UsedPureFallback = usedPureFallback, + PureBaselineSizeBytes = pureSnap?.SizeBytes, + PureBaselineKld = pureSnap == null ? null : GetAggregateKld(pureSnap), + PureBaselinePplDeltaPercent = pureSnap == null ? null : GetAggregatePplDeltaPercent(pureSnap, nativeBaseline) }); - if (pureSnap == null && baseOnlySnap != null) + if (usedPureFallback) + { + result.Notes.Add( + $"Carrier '{baseline.Names[0]}' fell back to pure-baseline metrics because a base-only isolation snapshot was not found."); + } + else if (pureSnap != null) { result.Notes.Add( - $"Carrier '{baseline.Names[0]}' fell back to base-only isolation metrics because a pure baseline benchmark snapshot was not found."); + $"Carrier '{baseline.Names[0]}' is using base-only isolation metrics for pruning/display; pure-baseline metrics are retained only as reference context."); } } ApplyBaseBaselineReductionPruning(baseBaselineCandidates, options, result); ApplyBaseBaselineDominanceElimination(baseBaselineCandidates, result); ApplyBaseBaselineBadTradeElimination(baseBaselineCandidates, result); + AppendBaseCombinationCarrierDecision(baseBaselineCandidates, result); result.ExplicitQuantBannedGroups = RuntimeSearchSpace.GetGroupsWithExplicitQuantBanned().Count; result.Bf16SuppressedGroups = result.GroupDetails.Count(x => x.Bf16Suppressed); @@ -353,6 +368,67 @@ private static void AppendLearnedPrunedCandidates(TensorGroup group, IsolationGr } } + private static void AppendBaseCombinationCarrierDecision( + List candidates, + IsolationOptimizationResult result) + { + if (candidates.Count == 0) + return; + + var decision = new IsolationGroupDecision + { + GroupName = "base_combination_carriers" + }; + + var ordered = candidates + .OrderByDescending(x => x.SavingsRatio) + .ThenBy(x => x.Kld) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) + .ThenByDescending(x => x.Baseline.BitRange) + .ThenBy(x => x.Baseline.Names[0], StringComparer.Ordinal) + .ToList(); + + var winner = ordered + .Where(x => !RuntimeSearchSpace.IsCombinationBaselineDisabled(x.Baseline)) + .OrderBy(x => x.Kld) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) + .ThenByDescending(x => x.Baseline.BitRange) + .ThenBy(x => x.Baseline.Names[0], StringComparer.Ordinal) + .FirstOrDefault(); + + if (winner != null) + { + decision.BestReductionRatio = winner.SavingsRatio; + decision.WinningCandidate = winner.Baseline.Names[0]; + decision.WinningSizeBytes = winner.SizeBytes; + decision.WinningKld = winner.Kld; + decision.WinningPplDelta = winner.PplDeltaPercent; + } + + foreach (var candidate in ordered) + { + var state = RuntimeSearchSpace.IsCombinationBaselineDisabled(candidate.Baseline) ? "DISABLED" : "ACTIVE"; + var source = candidate.UsedBaseOnlySnapshot ? "base-only" : (candidate.UsedPureFallback ? "pure-fallback" : "unknown"); + var line = + $"{candidate.Baseline.Names[0]} | size={(candidate.SizeBytes / 1024.0 / 1024.0):F2}MB | savings={candidate.SavingsRatio:P2} | kld={candidate.Kld:G6} | pplΔ={candidate.PplDeltaPercent:F4}% | source={source} | state={state}"; + + if (candidate.PureBaselineSizeBytes.HasValue && candidate.UsedBaseOnlySnapshot) + { + line += + $" | pure-ref={(candidate.PureBaselineSizeBytes.Value / 1024.0 / 1024.0):F2}MB / kld={candidate.PureBaselineKld:G6} / pplΔ={candidate.PureBaselinePplDeltaPercent:F4}%"; + } + + decision.Candidates.Add(line); + } + + foreach (var note in result.Notes.Where(x => x.Contains("combination baseline", StringComparison.OrdinalIgnoreCase) || x.Contains("carrier anchor", StringComparison.OrdinalIgnoreCase)).Distinct()) + { + decision.Candidates.Add($"[pruned-final] {note}"); + } + + result.GroupDetails.Add(decision); + } + private static string FormatSchemeNames(IEnumerable schemeIds) { var names = schemeIds @@ -606,8 +682,8 @@ private static double ComputeReductionRatio(ulong baselineBytes, ulong candidate if (baselineBytes == 0) return 0d; - double delta = baselineBytes - candidateBytes; - return delta / baselineBytes; + double delta = (double)baselineBytes - (double)candidateBytes; + return delta / (double)baselineBytes; } @@ -632,9 +708,30 @@ private static void ApplyBaseBaselineReductionPruning( if (belowThreshold.Count == activeCandidates.Count) { + var keeper = activeCandidates + .OrderBy(x => x.Kld) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) + .ThenByDescending(x => x.Baseline.BitRange) + .ThenBy(x => x.Baseline.Names[0], StringComparer.Ordinal) + .First(); + result.Notes.Add( $"All active combination baselines had uncovered-tensor reduction below the meaningful threshold of {options.MinMeaningfulBaseOnlyReductionRatio:P2}. " + - "Deferring carrier pruning to tie/dominance/bad-trade comparison so the safest surviving carrier can be preserved."); + $"Base-carrier influence was therefore treated as negligible, and the search was collapsed to the deterministic safe carrier '{keeper.Baseline.Names[0]}'."); + + foreach (var candidate in activeCandidates) + { + if (candidate.Baseline.UniqueId == keeper.Baseline.UniqueId) + continue; + + if (!RuntimeSearchSpace.DisableCombinationBaseline(candidate.Baseline)) + continue; + + result.DisabledBaselines++; + result.Notes.Add( + $"Disabled combination baseline '{candidate.Baseline.Names[0]}' because all surviving carriers were below the meaningful uncovered-tensor threshold and '{keeper.Baseline.Names[0]}' was selected as the deterministic safe representative."); + } + return; } @@ -890,6 +987,11 @@ private sealed class BaseBaselineEvaluation public double SavingsRatio { get; set; } public double Kld { get; set; } public double PplDeltaPercent { get; set; } + public bool UsedBaseOnlySnapshot { get; set; } + public bool UsedPureFallback { get; set; } + public ulong? PureBaselineSizeBytes { get; set; } + public double? PureBaselineKld { get; set; } + public double? PureBaselinePplDeltaPercent { get; set; } } private sealed class BenchmarkSnapshot diff --git a/MagicQuant/Services/PredictedCandidateEvaluationService.cs b/MagicQuant/Services/PredictedCandidateEvaluationService.cs index ef04a35..8e2d352 100644 --- a/MagicQuant/Services/PredictedCandidateEvaluationService.cs +++ b/MagicQuant/Services/PredictedCandidateEvaluationService.cs @@ -81,6 +81,14 @@ public async Task> EvaluateAsync( if (!pureByBaselineId.TryGetValue(BaselineQuants.Q8_0.UniqueId, out var pureQ8)) throw new InvalidOperationException("Prediction requires a learned pure Q8_0 benchmark anchor."); + var baseOnlySnapshots = await _repository.LoadBaseOnlyCarrierSnapshotsAsync(ct); + var baseOnlyByBaselineId = baseOnlySnapshots + .GroupBy(x => x.Quant.BaseQuant.UniqueId) + .ToDictionary(g => g.Key, g => g.OrderBy(x => x.Kld).ThenBy(x => Math.Abs(x.Ppl)).ThenByDescending(x => x.Quant.BaseQuant.BitRange).First()); + + if (!baseOnlyByBaselineId.TryGetValue(BaselineQuants.Q8_0.UniqueId, out var q8BaseOnly)) + throw new InvalidOperationException("Prediction requires a carrier base-only Q8_0 benchmark anchor."); + var isolationCache = new Dictionary(StringComparer.Ordinal); foreach (var config in configs) @@ -90,16 +98,42 @@ public async Task> EvaluateAsync( var notes = new List(effective.Warnings); byte normalizedBaseId = NormalizeBaselineIdForIsolation(quant.BaseQuant.UniqueId); - BenchmarkSnapshotRecord baselineAnchor = pureByBaselineId.TryGetValue(quant.BaseQuant.UniqueId, out var baselineSnap) - ? baselineSnap - : pureByBaselineId.TryGetValue(normalizedBaseId, out var normalizedSnap) - ? normalizedSnap - : pureQ8; + bool usedPureFallback = false; + + BenchmarkSnapshotRecord baselineAnchor; + if (baseOnlyByBaselineId.TryGetValue(quant.BaseQuant.UniqueId, out var baselineSnap)) + { + baselineAnchor = baselineSnap; + } + else if (baseOnlyByBaselineId.TryGetValue(normalizedBaseId, out var normalizedSnap)) + { + baselineAnchor = normalizedSnap; + } + else if (pureByBaselineId.TryGetValue(quant.BaseQuant.UniqueId, out var pureDirect)) + { + usedPureFallback = true; + baselineAnchor = pureDirect; + } + else if (pureByBaselineId.TryGetValue(normalizedBaseId, out var pureNormalized)) + { + usedPureFallback = true; + baselineAnchor = pureNormalized; + } + else + { + usedPureFallback = true; + baselineAnchor = q8BaseOnly; + } ulong predictedSize = baselineAnchor.SizeBytes; double predictedKld = baselineAnchor.Kld; double predictedPpl = baselineAnchor.Ppl; + if (usedPureFallback) + { + notes.Add($"Base-only carrier anchor was missing for '{quant.BaseQuant.Names[0]}'. Prediction fell back to pure-baseline context for the starting anchor."); + } + foreach (var tensor in quant.Tensors) { tensor.ValidateOrThrow(); @@ -303,4 +337,4 @@ private static void PrintPredictionDiagnostics(IReadOnlyList bytes / 1024d / 1024d / 1024d; -} +} \ No newline at end of file diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index d079f40..2f97182 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -217,6 +217,20 @@ public async Task PrunePredictedLargerThanQ8Async( return 0; } + if (Config.ManualMaxPredictedSizeBytes <= 0 && predictionContext.ShouldSkipPureQ8CeilingPruning) + { + if (!string.IsNullOrWhiteSpace(predictionContext.SkipPureQ8CeilingReason)) + { + AnsiConsole.MarkupLine($"[green]Predicted-size pruning removed 0 combinations.[/] [grey]{Markup.Escape(predictionContext.SkipPureQ8CeilingReason!)}[/]"); + } + else + { + AnsiConsole.MarkupLine("[green]Predicted-size pruning removed 0 combinations.[/]"); + } + + return 0; + } + var rows = new List(); using (var select = connection.CreateCommand()) @@ -568,17 +582,54 @@ private static void AppendRows( HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), ct); - var carrierBaseOnlyPlan = fullPlan.Plans.FirstOrDefault(x => - x.Kind == RequiredSampleKind.BaseOnlyIsolation && - x.TestedBaselineId == BaselineQuants.Q8_0.UniqueId && - x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal)); - - if (pureQ8 == null || carrierBaseOnlyPlan == null) + if (pureQ8 == null) return null; - var carrier = await LoadSnapshotByQuantAsync(db, model.Id, imatrixDefinitionId, carrierBaseOnlyPlan.Quant, ct); - if (carrier == null) - return null; + var activeCombinationBaselines = RuntimeSearchSpace.GetActiveCombinationBaselines().ToList(); + + var carrierBaseOnlyPlans = fullPlan.Plans + .Where(x => x.Kind == RequiredSampleKind.BaseOnlyIsolation) + .Where(x => x.TestedBaselineId.HasValue) + .Where(x => x.Key.StartsWith("carrier-baseonly:", StringComparison.Ordinal) || + x.Key.StartsWith("baseonly:", StringComparison.Ordinal)) + .Where(x => + { + var baseline = BaselineQuants.FromId(x.TestedBaselineId!.Value); + return baseline.IsCombinationCarrierCandidate; + }) + .GroupBy(x => x.TestedBaselineId!.Value) + .Select(g => g.First()) + .ToList(); + + var loadedCarrierSnapshots = new List<(byte BaselineId, BenchmarkRow Snapshot)>(); + foreach (var plan in carrierBaseOnlyPlans) + { + var snap = await LoadSnapshotByQuantAsync(db, model.Id, imatrixDefinitionId, plan.Quant, ct); + if (snap != null) + loadedCarrierSnapshots.Add((plan.TestedBaselineId!.Value, snap)); + } + + ulong representativeCarrierBaseOnlySize = loadedCarrierSnapshots.Count > 0 + ? loadedCarrierSnapshots + .OrderByDescending(x => BaselineQuants.FromId(x.BaselineId).BitRange) + .ThenByDescending(x => BaselineQuants.FromId(x.BaselineId).ExplicitCandidateSortOrder) + .Select(x => x.Snapshot.SizeBytes) + .First() + : pureQ8.SizeBytes; + + bool carrierBaseOnlyTruthCollapsed = loadedCarrierSnapshots.Count > 1 && + loadedCarrierSnapshots + .Select(x => x.Snapshot.SizeBytes) + .Distinct() + .Count() == 1; + + string? skipPureQ8PruneReason = null; + if (carrierBaseOnlyTruthCollapsed && activeCombinationBaselines.Count == 1 && Config.ManualMaxPredictedSizeBytes <= 0) + { + var safeCarrier = activeCombinationBaselines[0]; + skipPureQ8PruneReason = + $"Skipped pure-Q8 size pruning because base-carrier isolation truth collapsed across carriers and the search already resolved to the single deterministic safe carrier '{safeCarrier.Names[0]}'."; + } var pureBaselineSizes = new Dictionary(); foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) @@ -612,8 +663,10 @@ private static void AppendRows( return new PredictionContext( pureQ8BaseSize: pureQ8.SizeBytes, pureBaselineSizes: pureBaselineSizes, - carrierBaseOnlySize: carrier.SizeBytes, - sizesByGroupAndCandidate: sizeByGroupAndCandidate); + carrierBaseOnlySize: representativeCarrierBaseOnlySize, + sizesByGroupAndCandidate: sizeByGroupAndCandidate, + shouldSkipPureQ8CeilingPruning: !string.IsNullOrWhiteSpace(skipPureQ8PruneReason), + skipPureQ8CeilingReason: skipPureQ8PruneReason); } private static async Task LoadSnapshotByQuantAsync( @@ -673,15 +726,21 @@ private sealed class PredictionContext public ulong PureQ8BaseSize { get; } public ulong CarrierBaseOnlySize { get; } + public bool ShouldSkipPureQ8CeilingPruning { get; } + public string? SkipPureQ8CeilingReason { get; } public PredictionContext( ulong pureQ8BaseSize, Dictionary pureBaselineSizes, ulong carrierBaseOnlySize, - Dictionary<(byte GroupId, byte CandidateId), ulong> sizesByGroupAndCandidate) + Dictionary<(byte GroupId, byte CandidateId), ulong> sizesByGroupAndCandidate, + bool shouldSkipPureQ8CeilingPruning, + string? skipPureQ8CeilingReason) { PureQ8BaseSize = pureQ8BaseSize; CarrierBaseOnlySize = carrierBaseOnlySize; + ShouldSkipPureQ8CeilingPruning = shouldSkipPureQ8CeilingPruning; + SkipPureQ8CeilingReason = skipPureQ8CeilingReason; _pureBaselineSizes = pureBaselineSizes; _sizesByGroupAndCandidate = sizesByGroupAndCandidate; } diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 08b5e49..a60b5c0 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -85,7 +85,7 @@ baselines: quantize_base_name: Q4_K_M display_name: Unsloth_Q4_K_XL allow_as_learning_baseline: true - allow_as_combination_carrier: true + allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf @@ -93,7 +93,7 @@ baselines: quantize_base_name: Q5_K display_name: Unsloth_Q5_K_XL allow_as_learning_baseline: true - allow_as_combination_carrier: true + allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf From 1c358184ad9fdffc00bfee917b7fdd96e2200a2b Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 23 Apr 2026 17:58:19 -0400 Subject: [PATCH 118/258] all items get isolated and learned from now --- MagicQuant/Commands/Evolution.cs | 39 +++++++++++ MagicQuant/Helpers/TensorConfigGenerator.cs | 66 ++++++++++++++++++- .../Services/IsolationPlanningService.cs | 7 ++ 3 files changed, 110 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 396c47c..21879a6 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -269,6 +269,12 @@ await benchmarkService.RunAllBenchmarksAsync( } var mergedPlan = initialPlan.MergeWith(continuationPlan); + var archivalGroupIds = TReg.All + .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) + .Select(x => x.UniqueId) + .Except(initialAnalysis.GroupsToContinue) + .OrderBy(x => x) + .ToList(); SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Final Isolation Optimization"); @@ -321,6 +327,39 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Final surviving combinations after stage-1 pruning:[/] {finalRemainingCombinationCount:N0}"); + AnsiConsole.Write(new Rule("[yellow]Archival Isolation Coverage[/]") { Justification = Justify.Left }); + + var archivalCoveragePlan = isolationPlanner.BuildArchivalCoveragePlan( + groupIdsToArchive: archivalGroupIds, + existingPlanKeys: mergedPlan.Plans.Select(x => x.Key), + missingTensorGroups: Cache.UnusedTensorGroups); + + var archivalCoverageGroups = archivalCoveragePlan.Plans + .Where(x => x.TargetGroupId.HasValue) + .Select(x => x.TargetGroupId!.Value) + .Distinct() + .Count(); + + AnsiConsole.MarkupLine($"[grey]Groups queued for archival coverage:[/] [cyan]{archivalCoverageGroups:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Non-continuing groups targeted for archival fill:[/] [cyan]{archivalGroupIds.Count:N0}[/]"); + AnsiConsole.MarkupLine("[grey]This pass does not feed current-run pruning; it only fills missing isolated-sample coverage in the database for groups that were fixed/collapsed out of combo exploration.[/]"); + + if (archivalCoveragePlan.TotalCount > 0) + { + AnsiConsole.MarkupLine($"[grey]Queued archival isolation samples:[/] [cyan]{archivalCoveragePlan.TotalCount:N0}[/]"); + + var archivalCoverageSummary = await quantizationService.ProcessHybridBatchAsync(archivalCoveragePlan.Plans); + + AnsiConsole.MarkupLine("[bold green]Archival isolation coverage complete.[/]"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {archivalCoverageSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {archivalCoverageSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {archivalCoverageSummary.Failed:N0}"); + } + else + { + AnsiConsole.MarkupLine("[grey]No archival isolation coverage samples were required.[/]"); + } + var survivalPipeline = new CombinationSurvivalPipelineService(quantizationService); var finalizationResult = await survivalPipeline.RunAsync(ct: default); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 7521e3c..a7ce0cd 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -136,9 +136,72 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample .OrderBy(x => x.UniqueId) .ToList(); + var result = BuildIsolationCoverageContinuationPlan(activeGroups, missingIds); + + AnsiConsole.MarkupLine($"[bold green]Continuation isolation samples required:[/] {result.GroupIsolationCount:N0}"); + return result; + } + + /// + /// Builds archival-only isolation coverage for any continuation-style samples that were not part + /// of the live startup+continuation pruning plan. This is intentionally kept separate from the + /// current run's pruning inputs so search-space behavior stays unchanged while the database still + /// gains full isolated-sample coverage for future prediction/reporting flows. + /// + public static RequiredSampleGenerationResult GenerateArchivalIsolationCoverageSamplePlan( + IEnumerable? groupIdsToArchive = null, + IEnumerable? existingPlanKeys = null, + List? missingTensorGroups = null) + { + if (missingTensorGroups != null && !missingTensorGroups.Any()) + missingTensorGroups = null; + + var missingIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); + var archiveIds = groupIdsToArchive? + .Distinct() + .ToHashSet() + ?? new HashSet(); + + var existingKeys = existingPlanKeys? + .Where(x => !string.IsNullOrWhiteSpace(x)) + .ToHashSet(StringComparer.Ordinal) + ?? new HashSet(StringComparer.Ordinal); + + var activeGroups = TReg.All + .Where(x => !missingIds.Contains(x.UniqueId)) + .Where(x => archiveIds.Count == 0 || archiveIds.Contains(x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + + var result = BuildIsolationCoverageContinuationPlan(activeGroups, missingIds); + + if (existingKeys.Count > 0) + { + result.Plans = result.Plans + .Where(x => !existingKeys.Contains(x.Key)) + .ToList(); + } + + result.GroupIsolationCount = result.Plans.Count(x => + x.Kind == RequiredSampleKind.GroupIsolationProbe || + x.Kind == RequiredSampleKind.GroupIsolationContinuation); + + return result; + } + + private static RequiredSampleGenerationResult BuildIsolationCoverageContinuationPlan( + IReadOnlyCollection activeGroups, + HashSet missingIds) + { var result = new RequiredSampleGenerationResult(); + if (activeGroups.Count == 0) + return result; + var carrier = BaselineQuants.Q8_0; var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + var blanketGroups = TReg.All + .Where(x => !missingIds.Contains(x.UniqueId)) + .ToList(); var candidates = BaselineQuants.GetGroupCombinationCandidatesSmallestFirst( RuntimeSearchSpace.HasUsableImatrix(), @@ -156,7 +219,7 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var quant = HybridQuant.CreateExactBlanket( baseQuant: carrier, - groups: TReg.All.Where(x => !missingIds.Contains(x.UniqueId)), + groups: blanketGroups, exactScheme: nativeExactScheme); quant.SetLearnedCandidateOverride(group, candidate); @@ -178,7 +241,6 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample } } - AnsiConsole.MarkupLine($"[bold green]Continuation isolation samples required:[/] {result.GroupIsolationCount:N0}"); return result; } diff --git a/MagicQuant/Services/IsolationPlanningService.cs b/MagicQuant/Services/IsolationPlanningService.cs index 35b3167..ce4fe48 100644 --- a/MagicQuant/Services/IsolationPlanningService.cs +++ b/MagicQuant/Services/IsolationPlanningService.cs @@ -14,6 +14,13 @@ public RequiredSampleGenerationResult BuildInitialPlan(List? missin public RequiredSampleGenerationResult BuildContinuationPlan(IEnumerable groupIdsToContinue, List? missingTensorGroups = null) => TensorConfigGenerator.GenerateContinuationIsolationSamplePlan(groupIdsToContinue, missingTensorGroups); + + public RequiredSampleGenerationResult BuildArchivalCoveragePlan( + IEnumerable? groupIdsToArchive = null, + IEnumerable? existingPlanKeys = null, + List? missingTensorGroups = null) + => TensorConfigGenerator.GenerateArchivalIsolationCoverageSamplePlan(groupIdsToArchive, existingPlanKeys, missingTensorGroups); + public List BuildRequiredStartupCombos(List? missingTensorGroups = null) => TensorConfigGenerator.GenerateRequiredDataSampleCombos(missingTensorGroups); } \ No newline at end of file From 46d9a7773e395a6bc96bd7d132dfeb8244fb526d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 23 Apr 2026 18:54:33 -0400 Subject: [PATCH 119/258] significantly cleaner and proper combinations making it through now. --- .../Helpers/EquivalentTruthSelectionHelper.cs | 38 +++++++ .../FinalRealBenchmarkEliminationService.cs | 40 ++----- .../Services/IsolationOptimizationService.cs | 103 +++++++++++++++--- MagicQuant/config.default.yaml | 5 +- MagicQuant/config.dev.yaml | 3 +- 5 files changed, 144 insertions(+), 45 deletions(-) create mode 100644 MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs diff --git a/MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs b/MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs new file mode 100644 index 0000000..c2aa24d --- /dev/null +++ b/MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs @@ -0,0 +1,38 @@ +using MQ.DB.Models; + +namespace MagicQuant.Helpers; + +public static class EquivalentTruthSelectionHelper +{ + public static bool AreEquivalentTruths( + ulong leftSizeBytes, + double leftKld, + double leftPpl, + ulong rightSizeBytes, + double rightKld, + double rightPpl) + { + if (leftSizeBytes != rightSizeBytes) + return false; + + return Math.Abs(leftKld - rightKld) <= IsolationPruningConfig.FloatingPointEpsilon && + Math.Abs(leftPpl - rightPpl) <= IsolationPruningConfig.FloatingPointEpsilon; + } + + public static int GetBaselineSafetyRank( + BaselineQuants baseline, + bool isHybrid, + bool isExternalPureBaseline) + { + // Prefer the safest / most default representative when multiple rows have identical truth. + // 1) Higher BitRange is safer. + // 2) Higher ExplicitCandidateSortOrder wins ties inside the same BitRange. + // 3) Pure baseline beats hybrid when the measured truth is identical. + // 4) Internal/non-external beats external pure reference when still tied. + int rank = baseline.BitRange * 10_000; + rank += baseline.ExplicitCandidateSortOrder * 10; + rank += isHybrid ? 0 : 2; + rank += isExternalPureBaseline ? 0 : 1; + return rank; + } +} diff --git a/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs b/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs index 3dac477..f1e0c29 100644 --- a/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs +++ b/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs @@ -74,7 +74,13 @@ private static List CollapseEquivalentTruths( if (used[j]) continue; - if (!AreEquivalentTruths(seed, ordered[j])) + if (!EquivalentTruthSelectionHelper.AreEquivalentTruths( + seed.SizeBytes, + seed.Kld, + seed.Ppl, + ordered[j].SizeBytes, + ordered[j].Kld, + ordered[j].Ppl)) continue; tied.Add(ordered[j]); @@ -88,7 +94,10 @@ private static List CollapseEquivalentTruths( } var representative = tied - .OrderByDescending(GetSafetyRank) + .OrderByDescending(x => EquivalentTruthSelectionHelper.GetBaselineSafetyRank( + x.Quant.BaseQuant, + isHybrid: x.IsHybrid, + isExternalPureBaseline: x.IsExternalPureBaseline)) .ThenBy(x => x.IsHybrid) .ThenBy(x => x.IsExternalPureBaseline) .ThenBy(x => x.ProviderName, StringComparer.Ordinal) @@ -114,29 +123,4 @@ private static bool Dominates(BenchmarkSnapshotRecord better, BenchmarkSnapshotR bool strictlyBetterPpl = better.Ppl + IsolationPruningConfig.FloatingPointEpsilon < worse.Ppl; return sameOrSmaller && strictlyBetterKld && strictlyBetterPpl; } - - private static bool AreEquivalentTruths(BenchmarkSnapshotRecord left, BenchmarkSnapshotRecord right) - { - if (left.SizeBytes != right.SizeBytes) - return false; - - return Math.Abs(left.Kld - right.Kld) <= IsolationPruningConfig.FloatingPointEpsilon && - Math.Abs(left.Ppl - right.Ppl) <= IsolationPruningConfig.FloatingPointEpsilon; - } - - private static int GetSafetyRank(BenchmarkSnapshotRecord snapshot) - { - var baseline = snapshot.Quant.BaseQuant; - - // Prefer the safest / most default representative when multiple rows have identical truth. - // 1) Higher BitRange is safer. - // 2) Higher ExplicitCandidateSortOrder wins ties inside the same BitRange. - // 3) Pure baseline beats hybrid when the measured truth is identical. - // 4) Internal/non-external beats external pure reference when still tied. - int rank = baseline.BitRange * 10_000; - rank += baseline.ExplicitCandidateSortOrder * 10; - rank += snapshot.IsHybrid ? 0 : 2; - rank += snapshot.IsExternalPureBaseline ? 0 : 1; - return rank; - } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 13e8d16..e96e247 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -234,11 +234,18 @@ public async Task AnalyzeAndApplyFinalAsync( ApplyDominanceElimination(group, candidates, result); candidates = FilterSurvivors(group, candidates); ApplyBadTradeElimination(group, candidates, result); + candidates = FilterSurvivors(group, candidates); + ApplyEquivalentTruthElimination(group, candidates, result); candidates = FilterSurvivors(group, candidates) .OrderBy(x => x.Kld) - .ThenBy(x => x.PplDeltaPercent) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) .ThenByDescending(x => x.SavingsRatio) + .ThenByDescending(x => EquivalentTruthSelectionHelper.GetBaselineSafetyRank( + x.CandidateBaseline, + isHybrid: false, + isExternalPureBaseline: x.CandidateBaseline.IsExternalRepositoryBaseline)) + .ThenBy(x => x.CandidateBaseline.Names[0], StringComparer.Ordinal) .ToList(); if (candidates.Count == 0) @@ -543,6 +550,84 @@ private static void ApplyBadTradeElimination(TensorGroup group, List candidates, + IsolationOptimizationResult result) + { + var explicitCandidates = GetActiveExplicitCandidates(group, candidates); + if (explicitCandidates.Count <= 1) + return; + + var ordered = explicitCandidates + .OrderBy(x => x.SizeBytes) + .ThenBy(x => x.Kld) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) + .ThenByDescending(x => EquivalentTruthSelectionHelper.GetBaselineSafetyRank( + x.CandidateBaseline, + isHybrid: false, + isExternalPureBaseline: x.CandidateBaseline.IsExternalRepositoryBaseline)) + .ThenBy(x => x.CandidateBaseline.Names[0], StringComparer.Ordinal) + .ToList(); + + var used = new bool[ordered.Count]; + + for (int i = 0; i < ordered.Count; i++) + { + if (used[i]) + continue; + + var seed = ordered[i]; + var tied = new List { seed }; + used[i] = true; + + for (int j = i + 1; j < ordered.Count; j++) + { + if (used[j]) + continue; + + if (!EquivalentTruthSelectionHelper.AreEquivalentTruths( + seed.SizeBytes, + seed.Kld, + Math.Abs(seed.PplDeltaPercent), + ordered[j].SizeBytes, + ordered[j].Kld, + Math.Abs(ordered[j].PplDeltaPercent))) + continue; + + tied.Add(ordered[j]); + used[j] = true; + } + + if (tied.Count == 1) + continue; + + var representative = tied + .OrderByDescending(x => EquivalentTruthSelectionHelper.GetBaselineSafetyRank( + x.CandidateBaseline, + isHybrid: false, + isExternalPureBaseline: x.CandidateBaseline.IsExternalRepositoryBaseline)) + .ThenBy(x => x.CandidateBaseline.Names[0], StringComparer.Ordinal) + .First(); + + foreach (var loser in tied) + { + if (ReferenceEquals(loser, representative)) + continue; + + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, loser.CandidateBaseline)) + continue; + + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, loser.CandidateBaseline); + result.DominatedGroupCandidatesBanned++; + + result.Notes.Add( + $"Equivalent-truth elimination: '{loser.CandidateBaseline.Names[0]}' removed for '{group.Name}' because it had identical measured truth to safer representative '{representative.CandidateBaseline.Names[0]}'."); + } + } + } + private static List GetActiveExplicitCandidates(TensorGroup group, List candidates) { return candidates @@ -559,7 +644,7 @@ private static List> BuildSizeBuckets(List x .OrderBy(c => c.Kld) .ThenBy(c => Math.Abs(c.PplDeltaPercent)) - .ThenByDescending(c => GetCandidateSafetyScore(c.CandidateBaseline)) + .ThenByDescending(c => EquivalentTruthSelectionHelper.GetBaselineSafetyRank(c.CandidateBaseline, isHybrid: false, isExternalPureBaseline: c.CandidateBaseline.IsExternalRepositoryBaseline)) .ThenBy(c => c.CandidateBaseline.Names[0], StringComparer.Ordinal) .ToList()) .ToList(); @@ -609,23 +694,11 @@ private static bool ShouldEliminateAsBadTrade(GroupCandidateEvaluation anchor, G return survivors .OrderBy(x => x.Kld) .ThenBy(x => Math.Abs(x.PplDeltaPercent)) - .ThenByDescending(x => GetCandidateSafetyScore(x.CandidateBaseline)) + .ThenByDescending(x => EquivalentTruthSelectionHelper.GetBaselineSafetyRank(x.CandidateBaseline, isHybrid: false, isExternalPureBaseline: x.CandidateBaseline.IsExternalRepositoryBaseline)) .ThenBy(x => x.CandidateBaseline.Names[0], StringComparer.Ordinal) .FirstOrDefault(); } - private static int GetCandidateSafetyScore(BaselineQuants candidate) - { - string canonical = candidate.Names[0]; - - for (int i = 0; i < canonical.Length - 1; i++) - { - if ((canonical[i] == 'q' || canonical[i] == 'Q') && char.IsDigit(canonical[i + 1])) - return canonical[i + 1] - '0'; - } - - return 0; - } private async Task LoadSnapshotAsync(HybridQuant quant, CancellationToken ct) { diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 778580d..cb17ddd 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -90,7 +90,10 @@ isolation_pruning: # not for the old "skip candidate because learned tensor usage looked redundant" path. # Minimum isolation reduction ratio required to continue considering the result meaningful. - minimum_isolation_reduction_to_continue_ratio: 0.04 + # Currently broke, should be 0.04 but leave at 0 until fixed. Causes isolated samples not to be made + # which was once a requirement but need to go back and remove as all samples are needed for + # accurate predictions. DO not remove this comment till this is resolved. + minimum_isolation_reduction_to_continue_ratio: 0.00 # Minimum reduction ratio before BF16 suppression logic is allowed to kick in. minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index a60b5c0..fd41343 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -27,7 +27,8 @@ evolution: brute_force_final_combination_threshold: 12000 isolation_pruning: - minimum_isolation_reduction_to_continue_ratio: 0.04 + # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 + minimum_isolation_reduction_to_continue_ratio: 0.00 minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 maximum_isolation_ppl_delta_percent: 5.0 maximum_isolation_kld: 0.1 From 45e212e1e35d5428bea53bcc558daf88df8fbaa5 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 24 Apr 2026 16:30:09 -0400 Subject: [PATCH 120/258] Updated --- MQ.DB/Models/TensorWeightScheme.cs | 8 +++++ config.backup.yaml | 53 ------------------------------ config.default.yaml | 53 ------------------------------ config.dev.yaml | 53 ------------------------------ 4 files changed, 8 insertions(+), 159 deletions(-) delete mode 100644 config.backup.yaml delete mode 100644 config.default.yaml delete mode 100644 config.dev.yaml diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index e4ca2d2..38ef6b4 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -182,6 +182,14 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) ["Q4_K"], 32 ); + + public static readonly TensorWeightScheme Q2_K = + new( + 13, + true, + ["Q2_K"], + 32 + ); public static readonly TensorWeightScheme F16 = new(15, false, ["F16", "FLOAT16", "FP16", "HALF"], null, isEligibleForBaseline: false); diff --git a/config.backup.yaml b/config.backup.yaml deleted file mode 100644 index 7def13a..0000000 --- a/config.backup.yaml +++ /dev/null @@ -1,53 +0,0 @@ -# Default runtime config -identity: - architecture_family_name: - allow_architecture_family_alias_override: false - -paths: - magic_quant_root: - model_dir: - llama_root: - llama_bin: - convert_script: - external_baseline_cache_dir_name: ExternalBaselines - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_relearn_baseline_tensor_mappings: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -imatrix: - imatrix_url: - dataset_repo: - dataset_split: text - dataset_config: - dataset_local_file: - -evolution: - max_data_collected_per_category: 5 - max_survival_rounds: 4 - collapse_multiplier: 1.5 - brute_force_final_combination_threshold: 2000 - -isolation_pruning: - minimum_isolation_reduction_to_continue_ratio: 0.04 - minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 - maximum_isolation_ppl_delta_percent: 5.0 - maximum_isolation_kld: 0.1 - bad_trade_max_size_delta_percent: 4.0 - bad_trade_kld_multiplier: 2.5 - bad_trade_ppl_multiplier: 3.5 - floating_point_epsilon: 1.0e-8 - minimum_meaningful_base_only_reduction_ratio: 0.01 - -prediction: - manual_max_predicted_size_bytes: 0 - -baselines: - standard_baselines_mode: all - enabled_standard_learning_baselines: [] - enabled_standard_combination_carriers: [] - enabled_standard_explicit_group_candidates: [] - custom_repositories: [] diff --git a/config.default.yaml b/config.default.yaml deleted file mode 100644 index 7def13a..0000000 --- a/config.default.yaml +++ /dev/null @@ -1,53 +0,0 @@ -# Default runtime config -identity: - architecture_family_name: - allow_architecture_family_alias_override: false - -paths: - magic_quant_root: - model_dir: - llama_root: - llama_bin: - convert_script: - external_baseline_cache_dir_name: ExternalBaselines - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_relearn_baseline_tensor_mappings: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -imatrix: - imatrix_url: - dataset_repo: - dataset_split: text - dataset_config: - dataset_local_file: - -evolution: - max_data_collected_per_category: 5 - max_survival_rounds: 4 - collapse_multiplier: 1.5 - brute_force_final_combination_threshold: 2000 - -isolation_pruning: - minimum_isolation_reduction_to_continue_ratio: 0.04 - minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 - maximum_isolation_ppl_delta_percent: 5.0 - maximum_isolation_kld: 0.1 - bad_trade_max_size_delta_percent: 4.0 - bad_trade_kld_multiplier: 2.5 - bad_trade_ppl_multiplier: 3.5 - floating_point_epsilon: 1.0e-8 - minimum_meaningful_base_only_reduction_ratio: 0.01 - -prediction: - manual_max_predicted_size_bytes: 0 - -baselines: - standard_baselines_mode: all - enabled_standard_learning_baselines: [] - enabled_standard_combination_carriers: [] - enabled_standard_explicit_group_candidates: [] - custom_repositories: [] diff --git a/config.dev.yaml b/config.dev.yaml deleted file mode 100644 index 5605b63..0000000 --- a/config.dev.yaml +++ /dev/null @@ -1,53 +0,0 @@ -# Default runtime config -identity: - architecture_family_name: Qwen3-4B-Instruct-2507 - allow_architecture_family_alias_override: false - -paths: - magic_quant_root: - model_dir: - llama_root: - llama_bin: - convert_script: - external_baseline_cache_dir_name: ExternalBaselines - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_relearn_baseline_tensor_mappings: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -imatrix: - imatrix_url: - dataset_repo: - dataset_split: text - dataset_config: - dataset_local_file: - -evolution: - max_data_collected_per_category: 5 - max_survival_rounds: 4 - collapse_multiplier: 1.5 - brute_force_final_combination_threshold: 2000 - -isolation_pruning: - minimum_isolation_reduction_to_continue_ratio: 0.04 - minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 - maximum_isolation_ppl_delta_percent: 5.0 - maximum_isolation_kld: 0.1 - bad_trade_max_size_delta_percent: 4.0 - bad_trade_kld_multiplier: 2.5 - bad_trade_ppl_multiplier: 3.5 - floating_point_epsilon: 1.0e-8 - minimum_meaningful_base_only_reduction_ratio: 0.01 - -prediction: - manual_max_predicted_size_bytes: 0 - -baselines: - standard_baselines_mode: all - enabled_standard_learning_baselines: [] - enabled_standard_combination_carriers: [] - enabled_standard_explicit_group_candidates: [] - custom_repositories: [] From ff84dfd630e5cf5a8be9e8e73ed81fd425994d8e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 24 Apr 2026 16:30:32 -0400 Subject: [PATCH 121/258] push --- MQ.DB/Models/BaselineQuants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index a48fcc1..4dcd716 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -130,7 +130,7 @@ private static BaselineQuants Create( public static readonly BaselineQuants F16_Hybrid = Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], false, false, false, true, 16, int.MaxValue, false, "alias:f16", "exact_alias", null, null, null, null); - +//test private static readonly ImmutableArray StandardBaselines = [ Q8_0, From f04d1f61508b13c14e9312a20a2c1d4f1dbeeb15 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 24 Apr 2026 16:30:42 -0400 Subject: [PATCH 122/258] push --- MQ.DB/Models/BaselineQuants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 4dcd716..a48fcc1 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -130,7 +130,7 @@ private static BaselineQuants Create( public static readonly BaselineQuants F16_Hybrid = Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], false, false, false, true, 16, int.MaxValue, false, "alias:f16", "exact_alias", null, null, null, null); -//test + private static readonly ImmutableArray StandardBaselines = [ Q8_0, From 619f6bed2f8aaa9bad046040799c9fa56076b598 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 24 Apr 2026 16:51:23 -0400 Subject: [PATCH 123/258] Base of new prediction engine. Not working yet, but this is the idea --- MagicQuant/Commands/Evolution.cs | 15 +- MagicQuant/Config.cs | 38 +- .../Configuration/MagicQuantYamlConfig.cs | 71 +++- .../Configuration/MagicQuantYamlLoader.cs | 78 +++- MagicQuant/Models/HybridFinalizationModels.cs | 2 + MagicQuant/Program.cs | 1 + .../Services/BitRangeBucketBuilderService.cs | 154 -------- .../Services/BucketLocalPruningService.cs | 317 ---------------- .../CombinationSurvivalPipelineService.cs | 231 +++--------- .../FinalRealBenchmarkEliminationService.cs | 7 +- .../Services/HybridBenchmarkRepository.cs | 78 ++++ .../PredictedCandidateEvaluationService.cs | 340 ------------------ .../Services/ReadmeGenerationService.cs | 73 +++- MagicQuant/config.default.yaml | 94 +++-- MagicQuant/config.dev.yaml | 73 +++- 15 files changed, 522 insertions(+), 1050 deletions(-) delete mode 100644 MagicQuant/Services/BitRangeBucketBuilderService.cs delete mode 100644 MagicQuant/Services/BucketLocalPruningService.cs delete mode 100644 MagicQuant/Services/PredictedCandidateEvaluationService.cs diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 21879a6..ac4d596 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -304,7 +304,10 @@ await benchmarkService.RunAllBenchmarksAsync( await dbService.InitializeAsync(forceRebuild: true); - long predictedSizePruned = await dbService.PrunePredictedLargerThanQ8Async(mergedPlan); + // The old MDA/predicted-size ceiling pass is intentionally removed. + // DuckDB now stays as the allowed candidate universe, and the rank-safe + // isolation predictor chooses which candidates deserve real validation. + long predictedSizePruned = 0; long highPrecisionPruned = await dbService.PruneHighPrecisionHybridCandidatesAsync(); AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {totalLearnedPruningResult.GroupCandidateEliminations:N0} [grey](early pruning disabled)[/]"); @@ -317,7 +320,7 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); AnsiConsole.MarkupLine($"[green]Combination count after rule pruning:[/] {comboCountAfterRulePruning:N0}"); - AnsiConsole.MarkupLine($"[green]Predicted-size combo removals:[/] {predictedSizePruned:N0}"); + AnsiConsole.MarkupLine($"[green]Predicted-size combo removals:[/] {predictedSizePruned:N0} [grey](obsolete MDA ceiling pruning removed)[/]"); AnsiConsole.MarkupLine($"[green]Late-stage high-precision combo removals:[/] {highPrecisionPruned:N0}"); foreach (var note in isolationResult.Notes) @@ -426,7 +429,7 @@ private static void PrintCustomBaselineRuntimeSummary( private void ShowEvolutionHelp() { AnsiConsole.MarkupLine("[bold yellow]Command: evolution[/]"); - AnsiConsole.WriteLine("Runs the full evolutionary quantization search algorithm on a target model."); + AnsiConsole.WriteLine("Runs the full quantization search on a target model, then uses rank-safe isolation prediction to choose validated final hybrids."); AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Usage:[/]"); AnsiConsole.WriteLine(" mq evolution --model-dir \"\" [options]"); @@ -445,11 +448,13 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--imatrix-dataset-split[/] Dataset split for HF/local dataset source metadata/build (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-dataset-config[/] Optional dataset config name for HF datasets (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-dataset-local-file[/] Full path to local .json/.jsonl dataset source (Optional)"); - AnsiConsole.MarkupLine(" [green]--manual-max-predicted-size-bytes[/] Override late predicted-size pruning ceiling (Optional; 0 = auto Q8 ceiling)"); + AnsiConsole.MarkupLine(" [green]--selection-near-baseline-max-size-growth-percent[/] Phase-2 size premium for replacing a smaller/higher-damage anchor (Optional; default = 1.0)"); + AnsiConsole.MarkupLine(" [green]--selection-interior-window-fractions[/] Comma-separated phase-3 interior windows, e.g. 0.35,0.35 (Optional)"); + AnsiConsole.MarkupLine(" [green]--prediction-bit-stress-threshold-candidates[/] Comma-separated interaction-fit thresholds, e.g. 4,5,6,7,8,9,10,11,12 (Optional)"); AnsiConsole.MarkupLine(" [green]--output-dir[/] Final export/output directory for selected survivor artifacts (Optional; default = /MagicQuant/Final_Outputs)"); AnsiConsole.MarkupLine(" [green]--output-name-prefix[/] Output filename prefix for exported GGUF files (Optional; default = model)"); AnsiConsole.MarkupLine(" [green]--export-external-learned-baselines[/] Also locally rebuild/export pure learned external baselines such as Unsloth (Optional; default false)"); - AnsiConsole.MarkupLine(" [green]--max-selected-choices-per-bucket[/] Hard cap for survivors retained per BitRange bucket before brute force (Optional; default = 5)"); + AnsiConsole.MarkupLine(" [green]--selection-max-candidates-per-interior-window[/] Candidate count retained per interior window (Optional; default = 1)"); AnsiConsole.MarkupLine(" [green]--config[/] Path to YAML runtime config. CLI flags override YAML values."); AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Example:[/]"); diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 863d9e8..452f4fe 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -22,12 +22,48 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.Evolution.MaxDataCollectedPerCategory; public static int MaxSurvivalRounds => Current.Evolution.MaxSurvivalRounds; public static double CollapseMultiplier => Current.Evolution.CollapseMultiplier; public static int BruteForceFinalCombinationThreshold => Current.Evolution.BruteForceFinalCombinationThreshold; public static ulong ManualMaxPredictedSizeBytes => Current.Prediction.ManualMaxPredictedSizeBytes; + public static IReadOnlyList PredictionBitStressThresholdCandidates => + Current.Prediction.BitStressThresholdCandidates.Count == 0 + ? new[] { Current.Prediction.DefaultBitStressThreshold } + : Current.Prediction.BitStressThresholdCandidates; + + public static double PredictionDefaultBitStressThreshold => Current.Prediction.DefaultBitStressThreshold; + public static int PredictionMinimumFitRows => Math.Max(2, Current.Prediction.MinimumFitRows); + + public static double SelectionNearBaselineMaxSizeGrowthPercent => + Math.Max(0d, Current.CandidateSelection.NearBaselineMaxSizeGrowthPercent); + + public static IReadOnlyList SelectionInteriorWindowFractions => + Current.CandidateSelection.InteriorWindowFractions.Count == 0 + ? new[] { 0.35d, 0.35d } + : Current.CandidateSelection.InteriorWindowFractions; + + public static int SelectionMaxCandidatesPerInteriorWindow => + Math.Max(1, Current.CandidateSelection.MaxCandidatesPerInteriorWindow); + + public static int SelectionMaxFallbackAttemptsPerAnchor => + Math.Max(1, Current.CandidateSelection.MaxFallbackAttemptsPerAnchor); + + public static double SelectionMinimumKldImprovementEpsilon => + Math.Max(0d, Current.CandidateSelection.MinimumKldImprovementEpsilon); + + public static double SelectionMinimumNeighborGapFractionOfGlobalSpan => + Math.Clamp(Current.CandidateSelection.MinimumNeighborGapFractionOfGlobalSpan, 0d, 1d); + + public static double SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan => + Math.Clamp(Current.CandidateSelection.NearLowerAnchorBrutalZoneFractionOfPairSpan, 0d, 1d); + + public static double SelectionNearAnchorRequiredKldGainFractionOfPairGap => + Math.Max(0d, Current.CandidateSelection.NearAnchorRequiredKldGainFractionOfPairGap); + public static string? OutputDirectory => Current.Output.OutputDir; public static string OutputNamePrefix => string.IsNullOrWhiteSpace(Current.Output.OutputNamePrefix) ? "model" @@ -48,4 +84,4 @@ public static void SetResolvedCustomBaselines(IEnumerable BrainLayers => Current.BrainLayers; public static List CollapsePenaltySchemes => Current.CollapsePenaltySchemes; public static List MoeIndicatorTensors => Current.MoeIndicatorTensors; -} +} \ No newline at end of file diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 0b99982..9d05875 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -14,6 +14,7 @@ public sealed class MagicQuantYamlConfig public RuntimeBaselineConfig Baselines { get; set; } = new(); public RuntimeOutputConfig Output { get; set; } = new(); public RuntimeSurvivalConfig Survival { get; set; } = new(); + public RuntimeCandidateSelectionConfig CandidateSelection { get; set; } = new(); public List SensitivityProbeGroups { get; set; } = [ @@ -128,7 +129,30 @@ public sealed class RuntimeIsolationPruningConfig public sealed class RuntimePredictionConfig { + /// + /// Legacy emergency ceiling. Keep at 0 for the rank-safe isolation predictor. + /// public ulong ManualMaxPredictedSizeBytes { get; set; } = 0; + + /// + /// Candidate thresholds used while fitting the low-bit interaction correction. + /// The best threshold is selected by lowest MAE against existing general-category truth. + /// + public List BitStressThresholdCandidates { get; set; } = + [ + 4.0d, + 5.0d, + 6.0d, + 7.0d, + 8.0d, + 9.0d, + 10.0d, + 11.0d, + 12.0d + ]; + + public double DefaultBitStressThreshold { get; set; } = 8.0d; + public int MinimumFitRows { get; set; } = 12; } public sealed class RuntimeIdentityConfig @@ -155,6 +179,51 @@ public sealed class RuntimeSurvivalConfig public double TradeScorePplWeight { get; set; } = 0.15d; } +public sealed class RuntimeCandidateSelectionConfig +{ + /// + /// Phase 2 window. 1.0 means "up to one percent larger than the smaller/higher-damage anchor". + /// + public double NearBaselineMaxSizeGrowthPercent { get; set; } = 1.0d; + + /// + /// Phase 3 windows as fractions of each adjacent anchor-pair size span. + /// Example [0.35, 0.35] tests the first 35% and next 35% of the span. + /// + public List InteriorWindowFractions { get; set; } = + [ + 0.35d, + 0.35d + ]; + + public int MaxCandidatesPerInteriorWindow { get; set; } = 1; + public int MaxFallbackAttemptsPerAnchor { get; set; } = 5; + + /// + /// Strict epsilon for "lower KLD" claims. This is intentionally tiny because + /// the validator verifies the final relationship against real benchmark truth. + /// + public double MinimumKldImprovementEpsilon { get; set; } = 1e-9d; + + /// + /// Final spacing pass: candidates closer than this fraction of the global survivor + /// size span are collapsed to a single winner. + /// + public double MinimumNeighborGapFractionOfGlobalSpan { get; set; } = 0.03d; + + /// + /// Extra-brutal near-small-anchor zone. A candidate extremely close to the smaller + /// anchor must earn a larger KLD gain to survive. + /// + public double NearLowerAnchorBrutalZoneFractionOfPairSpan { get; set; } = 0.02d; + + /// + /// Required gain fraction of the adjacent-anchor KLD gap when a candidate sits in + /// the near-small-anchor brutal zone. + /// + public double NearAnchorRequiredKldGainFractionOfPairGap { get; set; } = 0.05d; +} + public sealed class RuntimeBaselineConfig { public string StandardBaselinesMode { get; set; } = "all"; @@ -212,4 +281,4 @@ public sealed class ResolvedCustomBaselineSpec public bool AllowAsCombinationCarrier { get; set; } public bool AllowAsExplicitGroupCandidate { get; set; } public IReadOnlyList BannedGroupIds { get; set; } = Array.Empty(); -} +} \ No newline at end of file diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 2e00638..851b363 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -92,7 +92,35 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) : config.Output.OutputNamePrefix.Trim(); if (config.Survival.MaxSelectedChoicesPerBucket <= 0) - throw new InvalidOperationException("survival.max_selected_choices_per_bucket must be greater than 0."); + config.Survival.MaxSelectedChoicesPerBucket = 1; + + if (config.Prediction.BitStressThresholdCandidates.Count == 0) + config.Prediction.BitStressThresholdCandidates.Add(config.Prediction.DefaultBitStressThreshold); + + config.Prediction.BitStressThresholdCandidates = config.Prediction.BitStressThresholdCandidates + .Where(x => x > 0d) + .Distinct() + .OrderBy(x => x) + .ToList(); + + if (config.Prediction.MinimumFitRows < 2) + config.Prediction.MinimumFitRows = 2; + + if (config.CandidateSelection.InteriorWindowFractions.Count == 0) + { + config.CandidateSelection.InteriorWindowFractions.Add(0.35d); + config.CandidateSelection.InteriorWindowFractions.Add(0.35d); + } + + config.CandidateSelection.InteriorWindowFractions = config.CandidateSelection.InteriorWindowFractions + .Select(x => Math.Clamp(x, 0d, 1d)) + .Where(x => x > 0d) + .ToList(); + + config.CandidateSelection.MaxCandidatesPerInteriorWindow = Math.Max(1, config.CandidateSelection.MaxCandidatesPerInteriorWindow); + config.CandidateSelection.MaxFallbackAttemptsPerAnchor = Math.Max(1, config.CandidateSelection.MaxFallbackAttemptsPerAnchor); + config.CandidateSelection.NearBaselineMaxSizeGrowthPercent = Math.Max(0d, config.CandidateSelection.NearBaselineMaxSizeGrowthPercent); + config.CandidateSelection.MinimumKldImprovementEpsilon = Math.Max(0d, config.CandidateSelection.MinimumKldImprovementEpsilon); ApplyStandardBaselineFilters(config.Baselines); BaselineQuants.ResetDynamicCustomBaselines(); @@ -173,6 +201,41 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (ulong.TryParse(Get("manual-max-predicted-size-bytes"), out var manualBytes)) config.Prediction.ManualMaxPredictedSizeBytes = manualBytes; + if (double.TryParse(Get("prediction-default-bit-stress-threshold"), out var defaultBitStress) && defaultBitStress > 0d) + config.Prediction.DefaultBitStressThreshold = defaultBitStress; + + if (int.TryParse(Get("prediction-minimum-fit-rows"), out var minFitRows) && minFitRows >= 2) + config.Prediction.MinimumFitRows = minFitRows; + + var bitStressCandidates = ParseDoubleList(Get("prediction-bit-stress-threshold-candidates")); + if (bitStressCandidates.Count > 0) + config.Prediction.BitStressThresholdCandidates = bitStressCandidates; + + if (double.TryParse(Get("selection-near-baseline-max-size-growth-percent"), out var nearPct) && nearPct >= 0d) + config.CandidateSelection.NearBaselineMaxSizeGrowthPercent = nearPct; + + var windows = ParseDoubleList(Get("selection-interior-window-fractions")); + if (windows.Count > 0) + config.CandidateSelection.InteriorWindowFractions = windows; + + if (int.TryParse(Get("selection-max-candidates-per-interior-window"), out var maxInterior) && maxInterior > 0) + config.CandidateSelection.MaxCandidatesPerInteriorWindow = maxInterior; + + if (int.TryParse(Get("selection-max-fallback-attempts-per-anchor"), out var maxFallbacks) && maxFallbacks > 0) + config.CandidateSelection.MaxFallbackAttemptsPerAnchor = maxFallbacks; + + if (double.TryParse(Get("selection-minimum-kld-improvement-epsilon"), out var minKldEpsilon) && minKldEpsilon >= 0d) + config.CandidateSelection.MinimumKldImprovementEpsilon = minKldEpsilon; + + if (double.TryParse(Get("selection-minimum-neighbor-gap-fraction"), out var neighborGap) && neighborGap >= 0d) + config.CandidateSelection.MinimumNeighborGapFractionOfGlobalSpan = neighborGap; + + if (double.TryParse(Get("selection-near-lower-anchor-brutal-zone-fraction"), out var brutalZone) && brutalZone >= 0d) + config.CandidateSelection.NearLowerAnchorBrutalZoneFractionOfPairSpan = brutalZone; + + if (double.TryParse(Get("selection-near-anchor-required-kld-gain-fraction"), out var brutalGain) && brutalGain >= 0d) + config.CandidateSelection.NearAnchorRequiredKldGainFractionOfPairGap = brutalGain; + config.Output.OutputDir = Prefer(Get("output-dir"), config.Output.OutputDir); config.Output.OutputNamePrefix = Prefer(Get("output-name-prefix"), config.Output.OutputNamePrefix); if (Has("export-external-learned-baselines")) config.Output.ExportExternalLearnedBaselines = true; @@ -202,6 +265,19 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (Has("allow-architecture-family-alias-override")) config.Identity.AllowArchitectureFamilyAliasOverride = true; } + private static List ParseDoubleList(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return new List(); + + return value + .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(x => double.TryParse(x, out var parsed) ? (double?)parsed : null) + .Where(x => x.HasValue) + .Select(x => x!.Value) + .ToList(); + } + private static string? Prefer(string? preferred, string? fallback) => string.IsNullOrWhiteSpace(preferred) ? fallback : preferred; diff --git a/MagicQuant/Models/HybridFinalizationModels.cs b/MagicQuant/Models/HybridFinalizationModels.cs index ba252a7..747f676 100644 --- a/MagicQuant/Models/HybridFinalizationModels.cs +++ b/MagicQuant/Models/HybridFinalizationModels.cs @@ -204,4 +204,6 @@ public sealed class CombinationSurvivalExecutionResult public IReadOnlyList ExportedArtifacts { get; init; } = Array.Empty(); public IReadOnlyList BucketDiagnostics { get; init; } = Array.Empty(); public SurvivalStageReport SurvivalReport { get; init; } = new(); + public IReadOnlyList Eliminations { get; init; } = Array.Empty(); + public IReadOnlyList ValidationFailures { get; init; } = Array.Empty(); } diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index fe1f3c6..71e3527 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -22,6 +22,7 @@ var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) { { "evolution", ("Run the full evolutionary quantization search", () => new Evolution()) }, + { "validate-predictions", ("Validate rank-safe KLD predictions against existing SQLite benchmarks", () => new ValidatePredictions()) }, { "build-hybrids", ("Export specific hybrid models with polished README", () => new BuildHybrids()) }, { "initialize-llama-cpp", ("Initialize or update llama.cpp", () => new InitializeLlamaCpp()) } }; diff --git a/MagicQuant/Services/BitRangeBucketBuilderService.cs b/MagicQuant/Services/BitRangeBucketBuilderService.cs deleted file mode 100644 index d8dc13c..0000000 --- a/MagicQuant/Services/BitRangeBucketBuilderService.cs +++ /dev/null @@ -1,154 +0,0 @@ -using MagicQuant.Models; -using Spectre.Console; - -namespace MagicQuant.Services; - -public sealed class BitRangeBucketBuildResult -{ - public IReadOnlyList Buckets { get; init; } = Array.Empty(); - public IReadOnlyList BucketedCandidates { get; init; } = Array.Empty(); - public IReadOnlyList UnbucketedCandidates { get; init; } = Array.Empty(); -} - -public sealed class BitRangeBucketBuilderService -{ - private readonly HybridBenchmarkRepository _repository; - - public BitRangeBucketBuilderService(HybridBenchmarkRepository repository) - { - _repository = repository; - } - - public async Task BuildAsync( - IReadOnlyCollection candidates, - CancellationToken ct = default) - { - var baseOnlyCarriers = await _repository.LoadBaseOnlyCarrierSnapshotsAsync(ct); - var baselineAnchors = baseOnlyCarriers - .GroupBy(x => x.Quant.BaseQuant.BitRange) - .Select(g => new - { - BitRange = g.Key, - Snapshot = g.OrderBy(x => x.Kld).ThenBy(x => Math.Abs(x.Ppl)).ThenByDescending(x => x.Quant.BaseQuant.BitRange).First() - }) - .OrderBy(x => x.BitRange) - .ToList(); - - var buckets = new List(); - - for (int i = 0; i < baselineAnchors.Count - 1; i++) - { - var lower = baselineAnchors[i]; - var upper = baselineAnchors[i + 1]; - - buckets.Add(new BitRangeBucketDefinition - { - Key = $"{lower.BitRange}->{upper.BitRange}", - LowerBitRange = lower.BitRange, - UpperBitRange = upper.BitRange, - LowerAnchorSizeBytes = lower.Snapshot.SizeBytes, - UpperAnchorSizeBytes = upper.Snapshot.SizeBytes - }); - } - - if (candidates.Count > 0) - { - ulong minPredicted = candidates.Min(x => x.PredictedSizeBytes); - ulong maxPredicted = candidates.Max(x => x.PredictedSizeBytes); - AnsiConsole.MarkupLine($"[grey]Predicted size spread:[/] [cyan]{FormatBytes(minPredicted)}[/] [grey]..[/] [cyan]{FormatBytes(maxPredicted)}[/]"); - - foreach (var baseBitRange in candidates.GroupBy(x => x.BaseBitRange).OrderBy(x => x.Key)) - { - AnsiConsole.MarkupLine( - $"[grey]Predicted candidates using base BitRange {baseBitRange.Key}:[/] [cyan]{baseBitRange.Count():N0}[/]"); - } - } - - if (buckets.Count == 0) - { - AnsiConsole.MarkupLine("[yellow]No usable BitRange buckets could be built. Survival will fall back to global predicted sorting if required.[/]"); - return new BitRangeBucketBuildResult - { - Buckets = Array.Empty(), - BucketedCandidates = Array.Empty(), - UnbucketedCandidates = candidates.ToList() - }; - } - - foreach (var bucket in buckets) - { - AnsiConsole.MarkupLine( - $"[grey]BitRange bucket {bucket.Key} -> lower_anchor={bucket.LowerAnchorSizeBytes:N0} upper_anchor={bucket.UpperAnchorSizeBytes:N0}[/]"); - } - - var bucketed = new List(); - var unbucketed = new List(); - - foreach (var candidate in candidates) - { - BitRangeBucketDefinition? selected = null; - - foreach (var bucket in buckets) - { - ulong lowerBound = bucket == buckets[0] ? 0UL : bucket.LowerAnchorSizeBytes; - ulong upperBound = bucket.UpperAnchorSizeBytes; - - if (candidate.PredictedSizeBytes >= lowerBound && candidate.PredictedSizeBytes <= upperBound) - { - selected = bucket; - break; - } - } - - if (selected == null && candidate.PredictedSizeBytes > buckets[^1].UpperAnchorSizeBytes) - selected = buckets[^1]; - - if (selected == null) - { - unbucketed.Add(candidate); - continue; - } - - bucketed.Add(new BucketedCandidate - { - Evaluation = candidate, - Bucket = selected - }); - } - - int populatedBucketCount = 0; - foreach (var bucket in buckets) - { - int count = bucketed.Count(x => x.Bucket.Key == bucket.Key); - if (count > 0) - populatedBucketCount++; - - AnsiConsole.MarkupLine( - $"[grey]Bucket assignment {bucket.Key}:[/] [cyan]{count:N0}[/] [grey]candidate(s)[/]"); - } - - if (unbucketed.Count > 0) - { - AnsiConsole.MarkupLine($"[yellow]Unbucketed predicted candidates:[/] [cyan]{unbucketed.Count:N0}[/]"); - } - - if (candidates.Count > 0 && populatedBucketCount <= 1) - { - AnsiConsole.MarkupLine( - "[bold yellow]Bucket diagnostic warning:[/] [grey]Only one BitRange bucket received predicted candidates. This usually means carrier pruning or predicted-size anchoring collapsed the search into one neighborhood.[/]"); - } - - return new BitRangeBucketBuildResult - { - Buckets = buckets, - BucketedCandidates = bucketed, - UnbucketedCandidates = unbucketed - }; - } - - private static string FormatBytes(ulong bytes) - { - double gb = bytes / 1024d / 1024d / 1024d; - return $"{gb:F2} GB ({bytes:N0} bytes)"; - } -} \ No newline at end of file diff --git a/MagicQuant/Services/BucketLocalPruningService.cs b/MagicQuant/Services/BucketLocalPruningService.cs deleted file mode 100644 index 63d32ae..0000000 --- a/MagicQuant/Services/BucketLocalPruningService.cs +++ /dev/null @@ -1,317 +0,0 @@ -using MagicQuant.Models; -using MQ.DB.Models; - -namespace MagicQuant.Services; - -public sealed class BucketLocalPruningResult -{ - public IReadOnlyList Survivors { get; init; } = Array.Empty(); - public IReadOnlyList Diagnostics { get; init; } = Array.Empty(); -} - -public sealed class BucketLocalPruningService -{ - private readonly PredictedTradeComparisonPolicy _policy = new(); - - public BucketLocalPruningResult Prune( - BitRangeBucketBuildResult buildResult, - IReadOnlyCollection? pureBaselines = null) - { - var diagnostics = new List(); - var survivors = new List(); - - foreach (var bucket in buildResult.Buckets) - { - var incoming = buildResult.BucketedCandidates - .Where(x => x.Bucket.Key == bucket.Key) - .Select(x => x.Evaluation) - .ToList(); - - var diag = new BucketPruneDiagnostics - { - BucketKey = bucket.Key, - LowerAnchorSizeBytes = bucket.LowerAnchorSizeBytes, - UpperAnchorSizeBytes = bucket.UpperAnchorSizeBytes, - IncomingCount = incoming.Count - }; - - if (incoming.Count == 0) - { - diag.KeptCount = 0; - diagnostics.Add(diag); - continue; - } - - var deduped = incoming - .GroupBy(x => x.EffectiveStateKey, StringComparer.Ordinal) - .Select(g => - { - var ordered = g.OrderBy(x => x, Comparer.Create(_policy.Compare)).ToList(); - int removed = ordered.Count - 1; - if (removed > 0) - AddReasonCount(diag, "effective-duplicate", removed); - return ordered[0]; - }) - .ToList(); - - var dominancePruned = new List(deduped); - for (int i = dominancePruned.Count - 1; i >= 0; i--) - { - var current = dominancePruned[i]; - bool dominated = dominancePruned - .Where((_, index) => index != i) - .Any(other => _policy.Dominates(other, current)); - - if (dominated) - { - dominancePruned.RemoveAt(i); - diag.CountReason("dominance"); - } - } - - var orderedByPracticalTrade = dominancePruned - .OrderBy(x => x, Comparer.Create(_policy.Compare)) - .ToList(); - - var keptAfterPractical = new List(); - if (orderedByPracticalTrade.Count > 0) - { - var best = orderedByPracticalTrade[0]; - foreach (var candidate in orderedByPracticalTrade) - { - bool sameNeighborhood = - Math.Abs(candidate.PredictedKldCost - best.PredictedKldCost) <= Math.Max( - Config.SurvivalKldCloseCallAbsoluteEpsilon, - best.PredictedKldCost * Config.SurvivalKldCloseCallRelativeFraction) && - Math.Abs(candidate.PredictedPplCost - best.PredictedPplCost) <= Config.SurvivalPplLargeDifferencePercent && - PercentDifference(candidate.PredictedSizeBytes, best.PredictedSizeBytes) < Config.SurvivalMeaningfulSizeBiasPercent; - - bool obviouslyJunk = candidate.CompositeScore > best.CompositeScore * 1.65d && sameNeighborhood; - if (obviouslyJunk) - { - diag.CountReason("practical-trade"); - continue; - } - - keptAfterPractical.Add(candidate); - } - } - - var bucketPureBaselines = FilterPureBaselinesForBucket(bucket, pureBaselines); - - // Keep truly special hybrids as bonus survivors. They do not consume the bucket cap. - var bonusHybrids = keptAfterPractical - .Where(x => !x.IsPureBaseline) - .Where(x => bucketPureBaselines.Count == 0 || BeatsAnyPureBaseline(x, bucketPureBaselines)) - .Where(x => !IsShadowedByPureBaseline(x, bucketPureBaselines)) - .OrderBy(x => x, Comparer.Create(_policy.Compare)) - .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) - .ToList(); - - if (bonusHybrids.Count > 0) - { - survivors.AddRange(bonusHybrids); - diag.Notes.Add($"bonus-hybrid-kept={bonusHybrids.Count:N0}"); - } - - var remainingPool = keptAfterPractical - .Where(x => bonusHybrids.All(b => TensorConfigIdentity.ToKey(b.Config) != TensorConfigIdentity.ToKey(x.Config))) - .ToList(); - - int shadowedRemoved = 0; - if (bucketPureBaselines.Count > 0) - { - remainingPool = remainingPool - .Where(x => - { - bool shadowed = IsShadowedByPureBaseline(x, bucketPureBaselines); - if (shadowed) - shadowedRemoved++; - return !shadowed; - }) - .ToList(); - - if (shadowedRemoved > 0) - AddReasonCount(diag, "pure-baseline-shadowed", shadowedRemoved); - } - - int bucketBudget = Math.Max(0, Config.MaxSelectedChoicesPerBucket); - var capped = SelectDiversifiedBySizeBands(remainingPool, bucket, bucketBudget); - - int capRemoved = Math.Max(0, remainingPool.Count - capped.Count); - if (capRemoved > 0) - AddReasonCount(diag, "bucket-cap", capRemoved); - - survivors.AddRange(capped); - - diag.KeptCount = bonusHybrids.Count + capped.Count; - diag.RemovedCount = Math.Max(0, diag.IncomingCount - diag.KeptCount); - diagnostics.Add(diag); - } - - foreach (var candidate in buildResult.UnbucketedCandidates) - survivors.Add(candidate); - - survivors = survivors - .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) - .ToList(); - - return new BucketLocalPruningResult - { - Survivors = survivors, - Diagnostics = diagnostics - }; - } - - private List SelectDiversifiedBySizeBands( - IReadOnlyList candidates, - BitRangeBucketDefinition bucket, - int budget) - { - if (budget <= 0 || candidates.Count == 0) - return new List(); - - var ordered = candidates - .OrderBy(x => x.PredictedSizeBytes) - .ThenBy(x => x.PredictedKldCost) - .ThenBy(x => x.PredictedPplCost) - .ThenBy(x => x.CompositeScore) - .ToList(); - - // First pass: pull a Pareto-ish size frontier so obvious smaller-size wins get first dibs. - var frontier = new List(); - double bestKldSeen = double.PositiveInfinity; - foreach (var candidate in ordered) - { - bool materiallyBetterKld = candidate.PredictedKldCost + Config.SurvivalKldCloseCallAbsoluteEpsilon < bestKldSeen; - bool meaningfullySmallerThanLast = frontier.Count == 0 || - PercentDifference(candidate.PredictedSizeBytes, frontier[^1].PredictedSizeBytes) >= Config.SurvivalMeaningfulSizeBiasPercent; - - if (materiallyBetterKld || meaningfullySmallerThanLast) - { - frontier.Add(candidate); - if (candidate.PredictedKldCost < bestKldSeen) - bestKldSeen = candidate.PredictedKldCost; - } - } - - var selected = new List(); - ulong lower = bucket.LowerAnchorSizeBytes; - ulong upper = bucket.UpperAnchorSizeBytes > lower ? bucket.UpperAnchorSizeBytes : lower + 1UL; - double span = Math.Max(1d, upper - lower); - - // Second pass: reserve one slot per size band so we do not just take the five most Q8-adjacent items. - var bands = new Dictionary>(); - foreach (var candidate in frontier) - { - double normalized = Math.Clamp((candidate.PredictedSizeBytes - lower) / span, 0d, 0.999999d); - int band = Math.Min(budget - 1, (int)Math.Floor(normalized * budget)); - if (!bands.TryGetValue(band, out var list)) - { - list = new List(); - bands[band] = list; - } - - list.Add(candidate); - } - - foreach (var band in bands.OrderBy(x => x.Key)) - { - var best = band.Value - .OrderBy(x => x, Comparer.Create(_policy.Compare)) - .First(); - - if (selected.All(x => TensorConfigIdentity.ToKey(x.Config) != TensorConfigIdentity.ToKey(best.Config))) - selected.Add(best); - - if (selected.Count >= budget) - return selected; - } - - // Final fill: if we still have room, backfill from the overall frontier, then the full pool. - foreach (var candidate in frontier - .OrderBy(x => x, Comparer.Create(_policy.Compare)) - .Concat(ordered.OrderBy(x => x, Comparer.Create(_policy.Compare)))) - { - if (selected.Count >= budget) - break; - - if (selected.Any(x => TensorConfigIdentity.ToKey(x.Config) == TensorConfigIdentity.ToKey(candidate.Config))) - continue; - - selected.Add(candidate); - } - - return selected; - } - - private static List FilterPureBaselinesForBucket( - BitRangeBucketDefinition bucket, - IReadOnlyCollection? pureBaselines) - { - if (pureBaselines == null || pureBaselines.Count == 0) - return new List(); - - ulong lower = bucket.LowerAnchorSizeBytes; - ulong upper = bucket.UpperAnchorSizeBytes; - - var inBucket = pureBaselines - .Where(x => x.SizeBytes >= lower && x.SizeBytes <= upper) - .ToList(); - - return inBucket.Count > 0 ? inBucket : pureBaselines.ToList(); - } - - private static bool BeatsAnyPureBaseline( - PredictedCandidateEvaluation candidate, - IReadOnlyCollection pureBaselines) - { - foreach (var baseline in pureBaselines) - { - bool sameOrSmaller = candidate.PredictedSizeBytes <= baseline.SizeBytes; - bool betterKld = candidate.PredictedKldCost + 1e-9 < baseline.Kld; - bool betterPpl = candidate.PredictedPplCost + 1e-9 < baseline.Ppl; - - if (sameOrSmaller && (betterKld || betterPpl)) - return true; - } - - return false; - } - - private static bool IsShadowedByPureBaseline( - PredictedCandidateEvaluation candidate, - IReadOnlyCollection pureBaselines) - { - foreach (var baseline in pureBaselines) - { - bool sameOrSmaller = baseline.SizeBytes <= candidate.PredictedSizeBytes; - bool kldNoWorse = baseline.Kld <= candidate.PredictedKldCost + 1e-9; - bool pplNoWorse = baseline.Ppl <= candidate.PredictedPplCost + 1e-9; - bool strict = baseline.SizeBytes < candidate.PredictedSizeBytes || - baseline.Kld + 1e-9 < candidate.PredictedKldCost || - baseline.Ppl + 1e-9 < candidate.PredictedPplCost; - - if (sameOrSmaller && kldNoWorse && pplNoWorse && strict) - return true; - } - - return false; - } - - private static void AddReasonCount(BucketPruneDiagnostics diag, string reason, int count) - { - for (int i = 0; i < count; i++) - diag.CountReason(reason); - } - - private static double PercentDifference(ulong left, ulong right) - { - if (left == 0 || right == 0) - return 0d; - - double min = Math.Min(left, right); - double max = Math.Max(left, right); - return ((max - min) / min) * 100d; - } -} diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index a4835de..9906c87 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -11,10 +11,9 @@ public sealed class CombinationSurvivalPipelineService private readonly RemainingCombinationStore _combinationStore; private readonly HybridBenchmarkRepository _benchmarkRepository; private readonly EffectiveCandidateStateResolverService _effectiveResolver; - private readonly PredictedCandidateEvaluationService _predictionService; - private readonly BitRangeBucketBuilderService _bucketBuilder; - private readonly BucketLocalPruningService _bucketPruner; + private readonly RankSafeKldPredictionService _predictionService; private readonly FinalRealBenchmarkEliminationService _finalEliminator; + private readonly PredictionGuidedHybridSelectionService _selectionEngine; private readonly FinalSurvivorSelectionCliService _selectionCli; private readonly HybridArtifactExportService _exportService; private readonly ReadmeGenerationService _readmeService; @@ -26,10 +25,9 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic _combinationStore = new RemainingCombinationStore(); _benchmarkRepository = new HybridBenchmarkRepository(); _effectiveResolver = new EffectiveCandidateStateResolverService(_benchmarkRepository); - _predictionService = new PredictedCandidateEvaluationService(_benchmarkRepository, _effectiveResolver); - _bucketBuilder = new BitRangeBucketBuilderService(_benchmarkRepository); - _bucketPruner = new BucketLocalPruningService(); + _predictionService = new RankSafeKldPredictionService(_benchmarkRepository, _effectiveResolver); _finalEliminator = new FinalRealBenchmarkEliminationService(); + _selectionEngine = new PredictionGuidedHybridSelectionService(_quantizationService, _benchmarkRepository, _finalEliminator); _selectionCli = new FinalSurvivorSelectionCliService(); _exportService = new HybridArtifactExportService(_quantizationService, _effectiveResolver); _readmeService = new ReadmeGenerationService(); @@ -38,106 +36,46 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic public async Task RunAsync(CancellationToken ct = default) { - var report = new SurvivalStageReport(); - report.StartingCount = await _combinationStore.CountAsync(ct); + var report = new SurvivalStageReport + { + StartingCount = await _combinationStore.CountAsync(ct) + }; - AnsiConsole.Write(new Rule("[yellow]Prediction / Survival Pipeline[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"[green]Starting remaining combinations:[/] {report.StartingCount:N0}"); + AnsiConsole.Write(new Rule("[yellow]Rank-Safe Prediction / Hybrid Selection Pipeline[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[green]Remaining DuckDB combinations available to score:[/] [cyan]{report.StartingCount:N0}[/]"); + AnsiConsole.MarkupLine("[grey]Old MDA bucket survival is disabled. DuckDB now defines the allowed search space; rank-safe isolation prediction selects what deserves real benchmarking.[/]"); - if (report.StartingCount > Config.BruteForceFinalCombinationThreshold) - { - var current = await _combinationStore.LoadAllAsync(ct); - var predicted = await _predictionService.EvaluateAsync(current, ct); - var bucketBuild = await _bucketBuilder.BuildAsync(predicted, ct); - - PrintBucketAnchors(bucketBuild.Buckets); - - var pureBaselineSnapshots = await _benchmarkRepository.LoadPureBaselineSnapshotsAsync(ct); - AnsiConsole.MarkupLine($"[grey]Pure baseline context loaded for bucket pruning:[/] [cyan]{pureBaselineSnapshots.Count:N0}[/]"); - var bucketPruneResult = _bucketPruner.Prune(bucketBuild, pureBaselineSnapshots); - foreach (var diag in bucketPruneResult.Diagnostics) - report.BucketDiagnostics.Add(diag); - - var survivors = bucketPruneResult.Survivors - .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) - .ToList(); - - int beforeBalance = survivors.Count; - if (survivors.Count > Config.BruteForceFinalCombinationThreshold) - { - survivors = BalanceDownToThreshold(survivors, bucketBuild, Config.BruteForceFinalCombinationThreshold); - report.AddRemoval("bucket-balance", beforeBalance - survivors.Count); - } - - if (survivors.Count > Config.BruteForceFinalCombinationThreshold) - { - var globalCut = survivors - .OrderBy(x => x.CompositeScore) - .ThenBy(x => x.PredictedKldCost) - .ThenBy(x => x.PredictedSizeBytes) - .Take(Config.BruteForceFinalCombinationThreshold) - .ToList(); - - report.AddRemoval("stage-7-global-cut", survivors.Count - globalCut.Count); - survivors = globalCut; - } - - if (survivors.Count == 0) - AnsiConsole.MarkupLine("[yellow]Warning:[/] Survival pipeline produced zero kept candidates after bucket pruning. Check pure-baseline-shadowed / bonus-hybrid-kept diagnostics."); - - await _combinationStore.ReplaceAllAsync(survivors.Select(x => x.Config).ToList(), "prediction-survival", ct); - - foreach (var diag in report.BucketDiagnostics) - { - AnsiConsole.MarkupLine( - $"[grey]Bucket {Markup.Escape(diag.BucketKey)}:[/] anchors=({diag.LowerAnchorSizeBytes:N0}..{diag.UpperAnchorSizeBytes:N0}) " + - $"incoming=[cyan]{diag.IncomingCount:N0}[/] removed=[red]{diag.RemovedCount:N0}[/] kept=[green]{diag.KeptCount:N0}[/]"); - - foreach (var reason in diag.RemovalReasons.OrderByDescending(x => x.Value)) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(reason.Key)}:[/] {reason.Value:N0}"); - } - } - else - { - AnsiConsole.MarkupLine("[grey]Remaining combinations are already at or under threshold. Skipping additional predictive narrowing.[/]"); - } + var remainingConfigs = await _combinationStore.LoadAllAsync(ct); + var pureBaselines = await _benchmarkRepository.LoadPureBaselineSnapshotsAsync(ct); - report.EndingCount = await _combinationStore.CountAsync(ct); - AnsiConsole.MarkupLine($"[green]Combinations after survival pipeline:[/] {report.EndingCount:N0}"); + if (pureBaselines.Count == 0) + throw new InvalidOperationException("No pure baseline benchmark snapshots were available. Run the baseline/isolation phases before final hybrid selection."); - if (report.EndingCount > Config.BruteForceFinalCombinationThreshold) - { - throw new InvalidOperationException( - $"Survival pipeline completed but still left {report.EndingCount:N0} combinations, which is above the brute-force threshold of {Config.BruteForceFinalCombinationThreshold:N0}. Diagnostics were emitted above."); - } + AnsiConsole.MarkupLine($"[green]Pure baseline snapshots loaded:[/] [cyan]{pureBaselines.Count:N0}[/]"); - AnsiConsole.Write(new Rule("[yellow]Final Brute Force Benchmark Phase[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine( - $"[green]Remaining combination count[/] [cyan]{report.EndingCount:N0}[/] [grey]is at or below the brute-force threshold of[/] [yellow]{Config.BruteForceFinalCombinationThreshold:N0}[/]."); + var predictionInput = remainingConfigs + .Concat(pureBaselines.Select(x => x.Config)) + .DistinctBy(TensorConfigIdentity.ToKey) + .ToList(); - var finalConfigs = await _combinationStore.LoadAllAsync(ct); - var finalQuants = finalConfigs.Select(x => (HybridQuant)x).ToList(); - var finalSummary = await _quantizationService.ProcessHybridBatchAsync(finalQuants); + var predictions = await _predictionService.PredictAsync(predictionInput, ct); - AnsiConsole.MarkupLine("[bold green]Final brute force benchmarking complete.[/]"); - AnsiConsole.MarkupLine($" [green]Requested:[/] {finalSummary.Requested:N0}"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {finalSummary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {finalSummary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {finalSummary.Failed:N0}"); + foreach (var note in predictions.Notes) + AnsiConsole.MarkupLine($"[grey]Prediction note:[/] {Markup.Escape(note)}"); - var benchmarkSnapshots = (await _benchmarkRepository.LoadBenchmarkSnapshotsAsync(finalConfigs, ct)).Values.ToList(); - var pureBaselines = await _benchmarkRepository.LoadPureBaselineSnapshotsAsync(ct); + var selection = await _selectionEngine.RunAsync( + predictions.PredictableRows.ToList(), + pureBaselines, + ct); - var brutalInput = benchmarkSnapshots - .Concat(pureBaselines) - .GroupBy(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal) - .Select(g => g.First()) - .ToList(); + report.EndingCount = selection.Survivors.Count; + report.AddRemoval("prediction-guided-non-selected", Math.Max(0L, report.StartingCount - report.EndingCount)); - var brutal = _finalEliminator.Eliminate(brutalInput); - AnsiConsole.MarkupLine($"[green]Final brutal elimination removals:[/] [red]{brutal.Eliminated.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[green]Final candidate/anchor survivors before manual enablement:[/] [cyan]{selection.Survivors.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[yellow]Recorded baseline/anchor eliminations:[/] [cyan]{selection.Eliminations.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[yellow]Prediction validation misses:[/] [cyan]{selection.ValidationFailures.Count:N0}[/]"); - var selectedRows = _selectionCli.Prompt(brutal.Survivors); + var selectedRows = _selectionCli.Prompt(selection.Survivors); var exportedArtifacts = await _exportService.ExportAsync(selectedRows, ct); @@ -145,90 +83,35 @@ public async Task RunAsync(CancellationToken ? "model" : new DirectoryInfo(Cache.ModelDirectory!).Name; - await _readmeService.GenerateAsync(Cache.OutputDirectory!, modelName, exportedArtifacts, brutalInput, ct); + var benchmarkOverview = selection.Survivors + .Concat(pureBaselines) + .Concat(selection.ValidationFailures.Select(x => x.Snapshot).OfType()) + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ToList(); + + await _readmeService.GenerateAsync( + Cache.OutputDirectory!, + modelName, + exportedArtifacts, + benchmarkOverview, + selection.Eliminations, + selection.ValidationFailures, + ct); + await _hybridMapService.GenerateAsync(Cache.OutputDirectory!, exportedArtifacts, ct); return new CombinationSurvivalExecutionResult { - BenchmarkedSnapshots = benchmarkSnapshots, - BrutalSurvivors = brutal.Survivors, + BenchmarkedSnapshots = benchmarkOverview, + BrutalSurvivors = selection.Survivors, SelectedRows = selectedRows, ExportedArtifacts = exportedArtifacts, - BucketDiagnostics = report.BucketDiagnostics, - SurvivalReport = report + BucketDiagnostics = Array.Empty(), + SurvivalReport = report, + Eliminations = selection.Eliminations, + ValidationFailures = selection.ValidationFailures }; } - - private static void PrintBucketAnchors(IReadOnlyList buckets) - { - foreach (var bucket in buckets) - { - AnsiConsole.MarkupLine( - $"[grey]BitRange bucket {Markup.Escape(bucket.Key)}[/] -> lower_anchor=[cyan]{bucket.LowerAnchorSizeBytes:N0}[/] upper_anchor=[cyan]{bucket.UpperAnchorSizeBytes:N0}[/]"); - } - } - - private static List BalanceDownToThreshold( - IReadOnlyList survivors, - BitRangeBucketBuildResult bucketBuild, - int threshold) - { - var byBucket = bucketBuild.Buckets - .ToDictionary( - bucket => bucket.Key, - bucket => survivors - .Where(x => bucketBuild.BucketedCandidates.Any(bc => bc.Bucket.Key == bucket.Key && TensorConfigIdentity.ToKey(bc.Evaluation.Config) == TensorConfigIdentity.ToKey(x.Config))) - .OrderBy(x => x.CompositeScore) - .ThenBy(x => x.PredictedKldCost) - .ThenBy(x => x.PredictedSizeBytes) - .ToList(), - StringComparer.Ordinal); - - var fallback = survivors - .Where(x => !bucketBuild.BucketedCandidates.Any(bc => TensorConfigIdentity.ToKey(bc.Evaluation.Config) == TensorConfigIdentity.ToKey(x.Config))) - .OrderBy(x => x.CompositeScore) - .ThenBy(x => x.PredictedKldCost) - .ThenBy(x => x.PredictedSizeBytes) - .ToList(); - - var balanced = new List(threshold); - int pass = 0; - while (balanced.Count < threshold) - { - bool addedAny = false; - - foreach (var bucket in byBucket.OrderBy(x => x.Key, StringComparer.Ordinal)) - { - if (pass >= bucket.Value.Count) - continue; - - balanced.Add(bucket.Value[pass]); - addedAny = true; - - if (balanced.Count >= threshold) - break; - } - - if (!addedAny) - break; - - pass++; - } - - foreach (var item in fallback) - { - if (balanced.Count >= threshold) - break; - - if (balanced.Any(x => TensorConfigIdentity.ToKey(x.Config) == TensorConfigIdentity.ToKey(item.Config))) - continue; - - balanced.Add(item); - } - - return balanced - .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) - .Take(threshold) - .ToList(); - } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs b/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs index f1e0c29..6042547 100644 --- a/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs +++ b/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs @@ -120,7 +120,10 @@ private static bool Dominates(BenchmarkSnapshotRecord better, BenchmarkSnapshotR { bool sameOrSmaller = better.SizeBytes <= worse.SizeBytes; bool strictlyBetterKld = better.Kld + IsolationPruningConfig.FloatingPointEpsilon < worse.Kld; - bool strictlyBetterPpl = better.Ppl + IsolationPruningConfig.FloatingPointEpsilon < worse.Ppl; - return sameOrSmaller && strictlyBetterKld && strictlyBetterPpl; + + // Final dominance intentionally follows the new survival rule: + // size must be same-or-smaller and KLD must be lower. PPL remains displayed + // and available for manual judgment, but it no longer prevents a KLD/size win. + return sameOrSmaller && strictlyBetterKld; } } \ No newline at end of file diff --git a/MagicQuant/Services/HybridBenchmarkRepository.cs b/MagicQuant/Services/HybridBenchmarkRepository.cs index d48ad22..627302b 100644 --- a/MagicQuant/Services/HybridBenchmarkRepository.cs +++ b/MagicQuant/Services/HybridBenchmarkRepository.cs @@ -164,9 +164,12 @@ public async Task> LoadBaseOnlyCarrierSnapshotsAsy if (tensorComboId == null) return null; + int? activeImatrixId = await ResolveActiveImatrixIdAsync(db, scopedAiModelHashId.Value, ct); + return await db.QuantizationRuns .AsNoTracking() .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) + .Where(x => x.ImatrixDefinitionId == activeImatrixId) .Where(x => x.TensorComboId == tensorComboId.Value) .Where(x => x.Succeeded) .OrderByDescending(x => x.CompletedUtc) @@ -233,6 +236,81 @@ public async Task> LoadLearnedTensorMappingsAsync( StringComparer.Ordinal); } + + public async Task> LoadAllBenchmarkSnapshotsForCurrentContextAsync( + byte category = (byte)BenchmarkCategory.General, + bool strictImatrixContext = true, + CancellationToken ct = default) + { + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + return new List(); + + int? activeImatrixId = await ResolveActiveImatrixIdAsync(db, scopedAiModelHashId.Value, ct); + + var query = db.AiBenchmarks + .AsNoTracking() + .Include(x => x.TensorCombo) + .Include(x => x.CategorBenchmarks) + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value); + + if (strictImatrixContext) + query = query.Where(x => x.ImatrixDefinitionId == activeImatrixId); + + var rows = await query.ToListAsync(ct); + var result = new List(); + + foreach (var benchmark in rows) + { + var metric = benchmark.CategorBenchmarks.FirstOrDefault(x => x.Category == category) + ?? benchmark.CategorBenchmarks.FirstOrDefault(x => x.Category == (byte)BenchmarkCategory.General) + ?? benchmark.CategorBenchmarks.OrderBy(x => x.Category).FirstOrDefault(); + + if (metric == null) + continue; + + var combo = benchmark.TensorCombo; + var config = new TensorConfig( + baseQuant: combo.BaseQuant, + embeddings: combo.Embeddings, + lmHead: combo.LmHead, + attnQ: combo.AttnQ, + attnKV: combo.AttnKV, + attnOutput: combo.AttnOutput, + ffnUpGate: combo.FfnUpGate, + ffnDown: combo.FfnDown, + moeExperts: combo.MoeExperts, + moeRouter: combo.MoeRouter); + + var quant = (HybridQuant)config; + var baseQuant = quant.BaseQuant; + + result.Add(new BenchmarkSnapshotRecord + { + Config = config, + Quant = quant, + DisplayName = BuildDisplayName(quant), + ProviderName = ResolveProviderName(quant, exportNaming: false), + BaselineFamily = baseQuant.Names[0], + IsHybrid = quant.Tensors.Count > 0, + IsExternalPureBaseline = quant.Tensors.Count == 0 && baseQuant.IsExternalRepositoryBaseline, + SizeBytes = benchmark.SizeBytes, + Kld = metric.Kld, + Ppl = metric.Ppl, + OutputModelPath = await FindLatestSuccessfulOutputPathAsync(config, ct), + ExternalRepositoryUrl = BuildExternalRepositoryUrl(baseQuant) + }); + } + + return result + .GroupBy(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal) + .Select(g => g.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes).First()) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ToList(); + } + public static string ResolveProviderName(HybridQuant quant, bool exportNaming) { if (exportNaming && quant.Tensors.Count > 0) diff --git a/MagicQuant/Services/PredictedCandidateEvaluationService.cs b/MagicQuant/Services/PredictedCandidateEvaluationService.cs deleted file mode 100644 index 8e2d352..0000000 --- a/MagicQuant/Services/PredictedCandidateEvaluationService.cs +++ /dev/null @@ -1,340 +0,0 @@ -using MagicQuant.Models; -using MQ.DB.Models; -using Spectre.Console; - -namespace MagicQuant.Services; - -public sealed class PredictedTradeComparisonPolicy -{ - public int Compare(PredictedCandidateEvaluation left, PredictedCandidateEvaluation right) - { - if (!IsKldClose(left, right)) - return left.PredictedKldCost.CompareTo(right.PredictedKldCost); - - double sizeDeltaPercent = PercentDifference(left.PredictedSizeBytes, right.PredictedSizeBytes); - if (sizeDeltaPercent >= Config.SurvivalMeaningfulSizeBiasPercent && left.PredictedSizeBytes != right.PredictedSizeBytes) - return left.PredictedSizeBytes.CompareTo(right.PredictedSizeBytes); - - double pplDelta = Math.Abs(left.PredictedPplCost - right.PredictedPplCost); - if (pplDelta >= Config.SurvivalPplLargeDifferencePercent) - return left.PredictedPplCost.CompareTo(right.PredictedPplCost); - - return left.CompositeScore.CompareTo(right.CompositeScore); - } - - public bool Dominates(PredictedCandidateEvaluation better, PredictedCandidateEvaluation worse) - { - bool sizeOk = better.PredictedSizeBytes <= worse.PredictedSizeBytes; - bool kldBetter = better.PredictedKldCost <= worse.PredictedKldCost; - bool pplNotWorse = better.PredictedPplCost <= worse.PredictedPplCost + 1e-9; - - bool strict = better.PredictedSizeBytes < worse.PredictedSizeBytes || - better.PredictedKldCost < worse.PredictedKldCost || - better.PredictedPplCost < worse.PredictedPplCost; - - return sizeOk && kldBetter && pplNotWorse && strict; - } - - private static bool IsKldClose(PredictedCandidateEvaluation left, PredictedCandidateEvaluation right) - { - double diff = Math.Abs(left.PredictedKldCost - right.PredictedKldCost); - double absolute = Config.SurvivalKldCloseCallAbsoluteEpsilon; - double relative = Math.Min(left.PredictedKldCost, right.PredictedKldCost) * Config.SurvivalKldCloseCallRelativeFraction; - return diff <= Math.Max(absolute, relative); - } - - private static double PercentDifference(ulong left, ulong right) - { - if (left == 0 || right == 0) - return 0; - - double min = Math.Min(left, right); - double max = Math.Max(left, right); - return ((max - min) / min) * 100d; - } -} - -public sealed class PredictedCandidateEvaluationService -{ - private readonly HybridBenchmarkRepository _repository; - private readonly EffectiveCandidateStateResolverService _effectiveResolver; - - public PredictedCandidateEvaluationService( - HybridBenchmarkRepository repository, - EffectiveCandidateStateResolverService effectiveResolver) - { - _repository = repository; - _effectiveResolver = effectiveResolver; - } - - public async Task> EvaluateAsync( - IReadOnlyCollection configs, - CancellationToken ct = default) - { - var result = new List(configs.Count); - - var pureSnapshots = await _repository.LoadPureBaselineSnapshotsAsync(ct); - var pureByBaselineId = pureSnapshots - .GroupBy(x => x.Quant.BaseQuant.UniqueId) - .ToDictionary(g => g.Key, g => g.OrderBy(x => x.SizeBytes).ThenBy(x => x.Kld).First()); - - if (!pureByBaselineId.TryGetValue(BaselineQuants.Q8_0.UniqueId, out var pureQ8)) - throw new InvalidOperationException("Prediction requires a learned pure Q8_0 benchmark anchor."); - - var baseOnlySnapshots = await _repository.LoadBaseOnlyCarrierSnapshotsAsync(ct); - var baseOnlyByBaselineId = baseOnlySnapshots - .GroupBy(x => x.Quant.BaseQuant.UniqueId) - .ToDictionary(g => g.Key, g => g.OrderBy(x => x.Kld).ThenBy(x => Math.Abs(x.Ppl)).ThenByDescending(x => x.Quant.BaseQuant.BitRange).First()); - - if (!baseOnlyByBaselineId.TryGetValue(BaselineQuants.Q8_0.UniqueId, out var q8BaseOnly)) - throw new InvalidOperationException("Prediction requires a carrier base-only Q8_0 benchmark anchor."); - - var isolationCache = new Dictionary(StringComparer.Ordinal); - - foreach (var config in configs) - { - var quant = (HybridQuant)config; - var effective = await _effectiveResolver.ResolveAsync(config, ct); - var notes = new List(effective.Warnings); - - byte normalizedBaseId = NormalizeBaselineIdForIsolation(quant.BaseQuant.UniqueId); - bool usedPureFallback = false; - - BenchmarkSnapshotRecord baselineAnchor; - if (baseOnlyByBaselineId.TryGetValue(quant.BaseQuant.UniqueId, out var baselineSnap)) - { - baselineAnchor = baselineSnap; - } - else if (baseOnlyByBaselineId.TryGetValue(normalizedBaseId, out var normalizedSnap)) - { - baselineAnchor = normalizedSnap; - } - else if (pureByBaselineId.TryGetValue(quant.BaseQuant.UniqueId, out var pureDirect)) - { - usedPureFallback = true; - baselineAnchor = pureDirect; - } - else if (pureByBaselineId.TryGetValue(normalizedBaseId, out var pureNormalized)) - { - usedPureFallback = true; - baselineAnchor = pureNormalized; - } - else - { - usedPureFallback = true; - baselineAnchor = q8BaseOnly; - } - - ulong predictedSize = baselineAnchor.SizeBytes; - double predictedKld = baselineAnchor.Kld; - double predictedPpl = baselineAnchor.Ppl; - - if (usedPureFallback) - { - notes.Add($"Base-only carrier anchor was missing for '{quant.BaseQuant.Names[0]}'. Prediction fell back to pure-baseline context for the starting anchor."); - } - - foreach (var tensor in quant.Tensors) - { - tensor.ValidateOrThrow(); - - BenchmarkSnapshotRecord? targetIsolation = await LoadIsolationSnapshotAsync(tensor, isolationCache, ct); - if (targetIsolation == null) - { - notes.Add($"Isolation benchmark missing for target override on group '{tensor.TGroup.Name}'. Applied conservative penalty."); - predictedKld += 0.005d; - predictedPpl += 0.25d; - continue; - } - - BenchmarkSnapshotRecord? baseIsolation = await LoadBaseIsolationSnapshotAsync(quant.BaseQuant, tensor.TGroup, isolationCache, ct); - if (baseIsolation == null) - { - notes.Add($"Base-family isolation benchmark missing for '{quant.BaseQuant.Names[0]}' on group '{tensor.TGroup.Name}'. Using target isolation without relative improvement credit."); - baseIsolation = targetIsolation; - } - - long sizeDelta = (long)targetIsolation.SizeBytes - (long)baseIsolation.SizeBytes; - if (sizeDelta >= 0) - predictedSize += (ulong)sizeDelta; - else - predictedSize = predictedSize > (ulong)(-sizeDelta) ? predictedSize - (ulong)(-sizeDelta) : 0; - - predictedKld += (targetIsolation.Kld - baseIsolation.Kld); - predictedPpl += (targetIsolation.Ppl - baseIsolation.Ppl); - - ApplyProportionalTradeWeighting(baseIsolation, targetIsolation, ref predictedKld, ref predictedPpl); - } - - if (predictedKld < 0d) - predictedKld = 0d; - - if (Config.ManualMaxPredictedSizeBytes > 0 && predictedSize > Config.ManualMaxPredictedSizeBytes) - notes.Add($"Predicted size {predictedSize:N0} bytes exceeds configured manual ceiling {Config.ManualMaxPredictedSizeBytes:N0} bytes."); - - double sizeGb = predictedSize / 1024d / 1024d / 1024d; - double composite = (predictedKld * 10000d) + - (Math.Max(0d, predictedPpl) * Config.SurvivalTradeScorePplWeight) + - (sizeGb / Math.Max(0.01d, Config.SurvivalTradeScoreSizeBiasWeight)); - - result.Add(new PredictedCandidateEvaluation - { - Config = config, - Quant = quant, - PredictedSizeBytes = predictedSize, - PredictedKldCost = predictedKld, - PredictedPplCost = predictedPpl, - CompositeScore = composite, - EffectiveStateKey = effective.EffectiveStateKey, - HasUnknownMappings = effective.HasUnknownMappings, - BaseBitRange = quant.BaseQuant.BitRange, - IsPureBaseline = TensorConfigIdentity.IsPureBaseline(config), - Notes = notes - }); - } - - PrintPredictionDiagnostics(result); - return result; - } - - private async Task LoadBaseIsolationSnapshotAsync( - BaselineQuants baseQuant, - TensorGroup group, - Dictionary cache, - CancellationToken ct) - { - byte normalizedId = NormalizeBaselineIdForIsolation(baseQuant.UniqueId); - var normalizedBaseline = BaselineQuants.FromId(normalizedId); - return await LoadLearnedCandidateIsolationAsync(group, normalizedBaseline, cache, ct); - } - - private async Task LoadIsolationSnapshotAsync( - HybridTensor tensor, - Dictionary cache, - CancellationToken ct) - { - return tensor.OverrideMode switch - { - HybridTensorOverrideMode.ExactTensorScheme => await LoadExactIsolationAsync(tensor.TGroup, tensor.ExactTensorScheme!, cache, ct), - _ => await LoadLearnedCandidateIsolationAsync(tensor.TGroup, tensor.CandidateBaseline!, cache, ct) - }; - } - - private async Task LoadLearnedCandidateIsolationAsync( - TensorGroup group, - BaselineQuants baseline, - Dictionary cache, - CancellationToken ct) - { - string cacheKey = $"learned:{group.UniqueId}:{baseline.CanonicalKey}"; - if (cache.TryGetValue(cacheKey, out var existing)) - return existing; - - var isolationQuant = HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0); - isolationQuant.SetLearnedCandidateOverride(group, baseline); - - var snapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); - cache[cacheKey] = snapshot; - return snapshot; - } - - private async Task LoadExactIsolationAsync( - TensorGroup group, - TensorWeightScheme exactScheme, - Dictionary cache, - CancellationToken ct) - { - string cacheKey = $"exact:{group.UniqueId}:{exactScheme.Names[0]}"; - if (cache.TryGetValue(cacheKey, out var existing)) - return existing; - - var isolationQuant = HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0); - isolationQuant.SetExactOverride(group, exactScheme); - - var snapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); - cache[cacheKey] = snapshot; - return snapshot; - } - - private static byte NormalizeBaselineIdForIsolation(byte baselineId) - { - var baseline = BaselineQuants.FromId(baselineId); - if (!baseline.IsExternalRepositoryBaseline) - return baselineId; - - var builtIn = BaselineQuants.ResolveBuiltInStandardBaseline(baseline.QuantizeBaseArgumentName) - ?? BaselineQuants.ResolveBuiltInStandardBaseline(baseline.Names[0]); - - return builtIn?.UniqueId ?? baselineId; - } - - private static void ApplyProportionalTradeWeighting( - BenchmarkSnapshotRecord baseIsolation, - BenchmarkSnapshotRecord targetIsolation, - ref double predictedKld, - ref double predictedPpl) - { - if (targetIsolation.SizeBytes >= baseIsolation.SizeBytes) - return; - - double savingsPercent = ((double)baseIsolation.SizeBytes - targetIsolation.SizeBytes) / baseIsolation.SizeBytes * 100d; - if (savingsPercent <= 0d) - return; - - double kldDelta = targetIsolation.Kld - baseIsolation.Kld; - double pplDelta = targetIsolation.Ppl - baseIsolation.Ppl; - - if (kldDelta <= 0d && pplDelta <= 0d) - return; - - double damage = Math.Max(0d, kldDelta) * 1000d + Math.Max(0d, pplDelta); - double damagePerSavings = damage / Math.Max(0.10d, savingsPercent); - - if (damagePerSavings <= 1.0d) - return; - - double multiplier = Math.Min(2.75d, 1.0d + ((damagePerSavings - 1.0d) * 0.20d)); - predictedKld += Math.Max(0d, kldDelta) * (multiplier - 1.0d); - predictedPpl += Math.Max(0d, pplDelta) * (multiplier - 1.0d); - } - - private static void PrintPredictionDiagnostics(IReadOnlyList evaluations) - { - int pureCount = evaluations.Count(x => x.IsPureBaseline); - int hybridCount = evaluations.Count - pureCount; - AnsiConsole.MarkupLine($"[grey]Prediction composition:[/] [cyan]{pureCount:N0}[/] [grey]pure[/] / [cyan]{hybridCount:N0}[/] [grey]hybrid[/]"); - - if (evaluations.Count == 0) - { - AnsiConsole.MarkupLine("[grey]Prediction evaluation completed for[/] [cyan]0[/] [grey]remaining combinations.[/]"); - return; - } - - ulong min = evaluations.Min(x => x.PredictedSizeBytes); - ulong max = evaluations.Max(x => x.PredictedSizeBytes); - AnsiConsole.MarkupLine($"[grey]Prediction size spread:[/] [cyan]{ToGb(min):F2}[/] [grey]GB ..[/] [cyan]{ToGb(max):F2}[/] [grey]GB[/]"); - - foreach (var byBitRange in evaluations.GroupBy(x => x.BaseBitRange).OrderBy(x => x.Key)) - { - ulong bitMin = byBitRange.Min(x => x.PredictedSizeBytes); - ulong bitMax = byBitRange.Max(x => x.PredictedSizeBytes); - AnsiConsole.MarkupLine( - $"[grey]Base BitRange {byBitRange.Key} prediction spread:[/] [cyan]{byBitRange.Count():N0}[/] [grey]candidate(s),[/] [cyan]{ToGb(bitMin):F2}[/] [grey]GB ..[/] [cyan]{ToGb(bitMax):F2}[/] [grey]GB[/]"); - - foreach (var sample in byBitRange.OrderBy(x => x.PredictedSizeBytes).ThenBy(x => x.PredictedKldCost).Take(3)) - { - AnsiConsole.MarkupLine( - $" [grey]- sample:[/] {Markup.Escape(sample.Quant.BaseQuant.Names[0])} [grey]| predicted[/] [cyan]{ToGb(sample.PredictedSizeBytes):F2}[/] [grey]GB | KLD[/] [cyan]{sample.PredictedKldCost:G6}[/] [grey]| PPL[/] [cyan]{sample.PredictedPplCost:F4}[/]"); - } - } - - if (min == max && evaluations.Select(x => x.BaseBitRange).Distinct().Count() > 1) - { - AnsiConsole.MarkupLine("[yellow]Prediction diagnostic warning:[/] all candidates resolved to the same predicted size even though multiple base BitRanges remain. This usually means the relative size predictor is still collapsing too aggressively.[/]"); - } - - AnsiConsole.MarkupLine($"[grey]Prediction evaluation completed for[/] [cyan]{evaluations.Count:N0}[/] [grey]remaining combinations.[/]"); - } - - private static double ToGb(ulong bytes) => bytes / 1024d / 1024d / 1024d; -} \ No newline at end of file diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 178c722..7007a51 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -11,6 +11,8 @@ public async Task GenerateAsync( string modelName, IReadOnlyCollection exportedArtifacts, IReadOnlyCollection benchmarkOverview, + IReadOnlyCollection? eliminatedBaselines = null, + IReadOnlyCollection? validationFailures = null, CancellationToken ct = default) { Directory.CreateDirectory(outputDirectory); @@ -21,7 +23,7 @@ public async Task GenerateAsync( sb.AppendLine(); sb.AppendLine("MagicQuant is **not** a quantization technique by itself."); sb.AppendLine(); - sb.AppendLine("It is a search, judging, and hybrid-discovery system that learns from baseline families such as llama.cpp and external/custom baseline sources, then uses isolated empirical truth, pruning, and real benchmarking to keep the practical survivors."); + sb.AppendLine("It is a search, judging, and hybrid-discovery system that learns from baseline families such as llama.cpp and external/custom baseline sources, then uses isolated empirical truth, rank-safe prediction, and real benchmarking to keep the practical survivors."); sb.AppendLine(); sb.AppendLine("Sometimes a hybrid beats a pure baseline. Sometimes it does not. That is normal. The point is to pay the real benchmarking cost only where the trade looks genuinely worth it."); sb.AppendLine(); @@ -31,11 +33,36 @@ public async Task GenerateAsync( AppendDownloadTable(sb, exportedArtifacts); sb.AppendLine(); + if (eliminatedBaselines is { Count: > 0 }) + { + sb.AppendLine("## Baselines / anchors removed from final download table"); + sb.AppendLine(); + sb.AppendLine("These rows are intentionally **not** part of the primary download table. They explain which pure baselines or previously-surviving anchors were beaten by another validated artifact."); + sb.AppendLine(); + AppendEliminationTable(sb, eliminatedBaselines); + sb.AppendLine(); + } + + if (validationFailures is { Count: > 0 }) + { + sb.AppendLine("## Predicted candidates that did not validate"); + sb.AppendLine(); + sb.AppendLine("The prediction engine is used for choosing what is worth building, but final survival still requires real benchmark validation. These candidates were predicted as interesting, built or checked, and then rejected because the real relationship did not hold."); + sb.AppendLine(); + AppendValidationFailureTable(sb, validationFailures); + sb.AppendLine(); + } + sb.AppendLine("## Benchmark overview"); sb.AppendLine(); AppendBenchmarkOverviewTable(sb, benchmarkOverview); sb.AppendLine(); + sb.AppendLine("## Method note"); + sb.AppendLine(); + sb.AppendLine("The final chooser uses rank-safe isolation prediction: Q8-carrier single-group isolation measurements provide the additive backbone, a low-bit interaction correction improves numeric KLD closeness, and an isotonic projection keeps the final predicted ordering monotone with the isolation backbone. Predicted candidates still have to validate against real benchmark truth before they can replace a baseline or remain as an interior hybrid."); + sb.AppendLine(); + sb.AppendLine("## Dive Deeper"); sb.AppendLine(); sb.AppendLine("- Browse the project GitHub/Wiki for benchmark methodology, architecture notes, and planned pipeline improvements."); @@ -44,7 +71,7 @@ public async Task GenerateAsync( sb.AppendLine("## Warning"); sb.AppendLine(); - sb.AppendLine("External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under BF16 / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing."); + sb.AppendLine("External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing."); sb.AppendLine(); sb.AppendLine("That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency, not as a universal judgment of the original creator's exact release artifact."); sb.AppendLine(); @@ -74,6 +101,42 @@ private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection eliminations) + { + sb.AppendLine("| Removed | Removed KLD | Removed Size (GB) | Winner | Winner KLD | Winner Size (GB) | Reason |"); + sb.AppendLine("|---|---:|---:|---|---:|---:|---|"); + + foreach (var row in eliminations + .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") + .OrderBy(x => x.Eliminated.Kld) + .ThenBy(x => x.Eliminated.SizeBytes)) + { + sb.AppendLine( + $"| {EscapePipe(row.Eliminated.DisplayName)} | {row.Eliminated.Kld:0.000000} | {ToGb(row.Eliminated.SizeBytes)} | " + + $"{EscapePipe(row.Eliminator.DisplayName)} | {row.Eliminator.Kld:0.000000} | {ToGb(row.Eliminator.SizeBytes)} | {EscapePipe(row.Reason)} |"); + } + } + + private static void AppendValidationFailureTable(StringBuilder sb, IReadOnlyCollection failures) + { + sb.AppendLine("| Candidate | Reason | Predicted KLD | Predicted Size (GB) | Actual KLD | Actual Size (GB) | Message |"); + sb.AppendLine("|---|---|---:|---:|---:|---:|---|"); + + foreach (var failure in failures + .Where(x => !x.Accepted) + .OrderBy(x => x.Candidate.Reason) + .ThenBy(x => x.Candidate.Prediction.PredictedKld) + .Take(100)) + { + var actualKld = failure.Snapshot == null ? "n/a" : failure.Snapshot.Kld.ToString("0.000000"); + var actualSize = failure.Snapshot == null ? "n/a" : ToGb(failure.Snapshot.SizeBytes); + sb.AppendLine( + $"| {EscapePipe(failure.Candidate.Prediction.Quant.BaseQuant.Names[0])} | {failure.Candidate.Reason} | " + + $"{failure.Candidate.Prediction.PredictedKld:0.000000} | {ToGb(failure.Candidate.Prediction.PredictedSizeBytes)} | " + + $"{actualKld} | {actualSize} | {EscapePipe(failure.Message)} |"); + } + } + private static void AppendBenchmarkOverviewTable(StringBuilder sb, IReadOnlyCollection snapshots) { sb.AppendLine("| Name | Provider | Quant Family | KLD | PPL | Size (GB) |"); @@ -84,10 +147,10 @@ private static void AppendBenchmarkOverviewTable(StringBuilder sb, IReadOnlyColl .OrderBy(x => x.Kld) .ThenBy(x => x.SizeBytes)) { - string sizeGb = (snap.SizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); - sb.AppendLine($"| {EscapePipe(snap.DisplayName)} | {EscapePipe(snap.ProviderName)} | {EscapePipe(snap.BaselineFamily)} | {snap.Kld:0.000000} | {snap.Ppl:0.0000} | {sizeGb} |"); + sb.AppendLine($"| {EscapePipe(snap.DisplayName)} | {EscapePipe(snap.ProviderName)} | {EscapePipe(snap.BaselineFamily)} | {snap.Kld:0.000000} | {snap.Ppl:0.0000} | {ToGb(snap.SizeBytes)} |"); } } - private static string EscapePipe(string value) => value.Replace("|", "\\|"); + private static string ToGb(ulong bytes) => (bytes / 1024d / 1024d / 1024d).ToString("0.00"); + private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); } diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index cb17ddd..b34bf3e 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -70,18 +70,8 @@ imatrix: # dataset_local_file: /data/datasets/imatrix-general-v1-1m.jsonl dataset_local_file: -evolution: - # Maximum saved benchmark datapoints per category during survival/evolution stages. - max_data_collected_per_category: 5 - - # Maximum survival rounds for evolution narrowing. - max_survival_rounds: 4 - - # Collapse multiplier used in evolution logic. - collapse_multiplier: 1.5 - - # If remaining combinations are <= this number, brute-force the end. - brute_force_final_combination_threshold: 100 +# Legacy evolution survivor knobs were removed from YAML. +# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. isolation_pruning: # NOTE: @@ -117,16 +107,63 @@ isolation_pruning: # Minimum meaningful reduction ratio for base-only comparisons. minimum_meaningful_base_only_reduction_ratio: 0.01 + prediction: - # 0 = automatic size ceiling behavior using current built-in logic. - # - # If > 0, this becomes a hard manual predicted-size ceiling in bytes. - # Any predicted combo larger than this is pruned out. + # Rank-safe isolation KLD predictor. # - # Example for ~4 GiB: - # manual_max_predicted_size_bytes: 4294967296 + # manual_max_predicted_size_bytes is retained only as an emergency compatibility + # field for older helper code. Leave it at 0 for the new chooser. manual_max_predicted_size_bytes: 0 + # Candidate bit-stress thresholds for the low-bit interaction correction. + # The predictor fits each candidate threshold against existing category=General + # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. + bit_stress_threshold_candidates: + - 4.0 + - 5.0 + - 6.0 + - 7.0 + - 8.0 + - 9.0 + - 10.0 + - 11.0 + - 12.0 + + # Fallback threshold when too few benchmark rows exist to fit the interaction model. + default_bit_stress_threshold: 8.0 + + # Minimum benchmark rows required before fitting the interaction correction. + minimum_fit_rows: 12 + +candidate_selection: + # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits + # inside this size premium and beats the real linear KLD improvement line. + near_baseline_max_size_growth_percent: 1.0 + + # Phase 3: interior windows between adjacent final anchors. + # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. + interior_window_fractions: + - 0.35 + - 0.35 + + # Number of predicted winners to keep per interior window. + max_candidates_per_interior_window: 1 + + # If the first predicted candidate fails real validation, try this many fallbacks. + max_fallback_attempts_per_anchor: 5 + + # Strict epsilon for lower-KLD comparisons after real benchmark validation. + minimum_kld_improvement_epsilon: 1.0e-9 + + # Final spacing pass: candidates closer than this fraction of the global survivor + # size span are collapsed unless one genuinely earns the slot. + minimum_neighbor_gap_fraction_of_global_span: 0.03 + + # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller + # anchor must provide a stronger KLD gain to justify its existence. + near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 + near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 + output: # Optional explicit output directory. # If blank, MagicQuant will default to: @@ -144,23 +181,8 @@ output: # modified model where the upstream artifact does not really exist for your case). export_external_learned_baselines: false -survival: - # Hard cap for survivors retained per BitRange bucket before brute-force benchmarking. - max_selected_choices_per_bucket: 5 - - # Size advantage percentage used as a meaningful tie-bias during bucket-local trade scoring. - meaningful_size_bias_percent: 1.0 - - # Absolute and relative KLD closeness thresholds for close-call handling. - kld_close_call_absolute_epsilon: 0.00075 - kld_close_call_relative_fraction: 0.02 - - # PPL only matters more strongly when the difference is actually meaningful. - ppl_large_difference_percent: 0.75 - - # Centralized trade scoring weights. - trade_score_size_bias_weight: 1.25 - trade_score_ppl_weight: 0.15 +# Legacy bit-range bucket survival settings were removed. +# See candidate_selection above for the active final chooser settings. identity: architecture_family_name: @@ -269,4 +291,4 @@ baselines: # # Example note: # # If the repo does not actually contain IQ3_XS, do not reference it. # # Use only filenames that truly exist in the repository. - [] + [] \ No newline at end of file diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index fd41343..5bf4abe 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -20,11 +20,8 @@ imatrix: dataset_config: dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1m.jsonl -evolution: - max_data_collected_per_category: 5 - max_survival_rounds: 4 - collapse_multiplier: 1.5 - brute_force_final_combination_threshold: 12000 +# Legacy evolution survivor knobs were removed from YAML. +# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. isolation_pruning: # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 @@ -38,23 +35,71 @@ isolation_pruning: floating_point_epsilon: 1.0e-8 minimum_meaningful_base_only_reduction_ratio: 0.01 + prediction: + # Rank-safe isolation KLD predictor. + # + # manual_max_predicted_size_bytes is retained only as an emergency compatibility + # field for older helper code. Leave it at 0 for the new chooser. manual_max_predicted_size_bytes: 0 + # Candidate bit-stress thresholds for the low-bit interaction correction. + # The predictor fits each candidate threshold against existing category=General + # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. + bit_stress_threshold_candidates: + - 4.0 + - 5.0 + - 6.0 + - 7.0 + - 8.0 + - 9.0 + - 10.0 + - 11.0 + - 12.0 + + # Fallback threshold when too few benchmark rows exist to fit the interaction model. + default_bit_stress_threshold: 8.0 + + # Minimum benchmark rows required before fitting the interaction correction. + minimum_fit_rows: 12 + +candidate_selection: + # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits + # inside this size premium and beats the real linear KLD improvement line. + near_baseline_max_size_growth_percent: 1.0 + + # Phase 3: interior windows between adjacent final anchors. + # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. + interior_window_fractions: + - 0.35 + - 0.35 + + # Number of predicted winners to keep per interior window. + max_candidates_per_interior_window: 1 + + # If the first predicted candidate fails real validation, try this many fallbacks. + max_fallback_attempts_per_anchor: 5 + + # Strict epsilon for lower-KLD comparisons after real benchmark validation. + minimum_kld_improvement_epsilon: 1.0e-9 + + # Final spacing pass: candidates closer than this fraction of the global survivor + # size span are collapsed unless one genuinely earns the slot. + minimum_neighbor_gap_fraction_of_global_span: 0.03 + + # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller + # anchor must provide a stronger KLD gain to justify its existence. + near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 + near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 + output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: output_name_prefix: model export_external_learned_baselines: false -survival: - max_selected_choices_per_bucket: 5 - meaningful_size_bias_percent: 1.0 - kld_close_call_absolute_epsilon: 0.00075 - kld_close_call_relative_fraction: 0.02 - ppl_large_difference_percent: 0.75 - trade_score_size_bias_weight: 1.25 - trade_score_ppl_weight: 0.15 +# Legacy bit-range bucket survival settings were removed. +# See candidate_selection above for the active final chooser settings. identity: architecture_family_name: Qwen3-4B-Instruct-2507 @@ -119,4 +164,4 @@ baselines: display_name: Unsloth_IQ3_XXS_for_IQ3_XS allow_as_learning_baseline: true allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true + allow_as_explicit_group_candidate: true \ No newline at end of file From e997f11c32a264dec3f53aad78bfe2494e148113 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 24 Apr 2026 18:45:00 -0400 Subject: [PATCH 124/258] not working fully but getting there --- MagicQuant/Commands/ValidatePredictions.cs | 147 ++++ .../Models/PredictionSelectionModels.cs | 143 ++++ .../PredictionGuidedHybridSelectionService.cs | 638 ++++++++++++++++ .../Services/PredictionValidationService.cs | 333 +++++++++ .../Services/RankSafeKldPredictionService.cs | 689 ++++++++++++++++++ 5 files changed, 1950 insertions(+) create mode 100644 MagicQuant/Commands/ValidatePredictions.cs create mode 100644 MagicQuant/Models/PredictionSelectionModels.cs create mode 100644 MagicQuant/Services/PredictionGuidedHybridSelectionService.cs create mode 100644 MagicQuant/Services/PredictionValidationService.cs create mode 100644 MagicQuant/Services/RankSafeKldPredictionService.cs diff --git a/MagicQuant/Commands/ValidatePredictions.cs b/MagicQuant/Commands/ValidatePredictions.cs new file mode 100644 index 0000000..0d6b982 --- /dev/null +++ b/MagicQuant/Commands/ValidatePredictions.cs @@ -0,0 +1,147 @@ +using MagicQuant.Helpers; +using MagicQuant.Models; +using MagicQuant.Services; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using Spectre.Console; + +namespace MagicQuant.Commands; + +public sealed class ValidatePredictions : ICommand +{ + public async Task Run(List args) + { + if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) + { + ShowHelp(); + return; + } + + string? modelDirRaw = args.FirstOrDefault(a => + string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; + + modelDirRaw = string.IsNullOrWhiteSpace(modelDirRaw) + ? Config.Current.Paths.ModelDir + : modelDirRaw; + + if (string.IsNullOrWhiteSpace(modelDirRaw)) + throw new InvalidOperationException("Missing model directory. Provide --model-dir or set paths.model_dir in YAML."); + + string modelDir = Path.GetFullPath(modelDirRaw); + if (!Directory.Exists(modelDir)) + throw new DirectoryNotFoundException($"Model directory does not exist: {modelDir}"); + + Cache.ModelDirectory = modelDir; + Cache.ModelMagicQuantDirectory = Path.Combine(modelDir, "MagicQuant"); + Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); + + Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(modelDir); + JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); + + await ResolveArchitectureFamilyFromConfigAsync(); + + ApplyOptionalImatrixContext(args); + + string outputDir = ResolveOutputDirectory(args); + Directory.CreateDirectory(outputDir); + + var repository = new HybridBenchmarkRepository(); + var effectiveResolver = new EffectiveCandidateStateResolverService(repository); + var prediction = new RankSafeKldPredictionService(repository, effectiveResolver); + var validator = new PredictionValidationService(repository, prediction); + + await validator.ExportAsync(outputDir); + } + + private static async Task ResolveArchitectureFamilyFromConfigAsync() + { + Cache.CurrentArchitectureFamilyId = null; + + if (string.IsNullOrWhiteSpace(Cache.CurrentArchitectureFamilyName)) + return; + + string normalized = Cache.CurrentArchitectureFamilyNormalizedName; + + await using var db = new MagicQuantContext(); + var family = await db.ArchitectureFamilies + .AsNoTracking() + .FirstOrDefaultAsync(x => x.NormalizedName == normalized); + + if (family == null) + { + AnsiConsole.MarkupLine($"[yellow]Warning:[/] Architecture family '{Markup.Escape(Cache.CurrentArchitectureFamilyName)}' was configured but not found in SQLite. Validation will use the raw model hash scope."); + return; + } + + Cache.CurrentArchitectureFamilyId = family.Id; + AnsiConsole.MarkupLine($"[green]Architecture family scope:[/] {Markup.Escape(family.DisplayName)} (Id={family.Id})"); + } + + private static void ApplyOptionalImatrixContext(IReadOnlyList args) + { + string? imatrixPath = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-path", StringComparison.OrdinalIgnoreCase))?.Value; + string? imatrixHash = args.FirstOrDefault(a => string.Equals(a.Name, "imatrix-identity-hash", StringComparison.OrdinalIgnoreCase))?.Value; + + if (!string.IsNullOrWhiteSpace(imatrixPath)) + { + string fullPath = Path.GetFullPath(imatrixPath); + if (!File.Exists(fullPath)) + throw new FileNotFoundException($"Imatrix path does not exist: {fullPath}"); + + Cache.IsImatrixAvailable = true; + Cache.ActiveImatrixPath = fullPath; + Cache.ActiveImatrixIdentityHash = null; + ImatrixIdentityService.EnsureActiveImatrixIdentityHashAsync().GetAwaiter().GetResult(); + AnsiConsole.MarkupLine($"[green]Validation imatrix path:[/] {Markup.Escape(fullPath)}"); + return; + } + + if (!string.IsNullOrWhiteSpace(imatrixHash)) + { + Cache.IsImatrixAvailable = true; + Cache.ActiveImatrixPath = null; + Cache.ActiveImatrixIdentityHash = imatrixHash.Trim().ToLowerInvariant(); + AnsiConsole.MarkupLine($"[green]Validation imatrix identity:[/] {Markup.Escape(Cache.ActiveImatrixIdentityHash)}"); + return; + } + + Cache.IsImatrixAvailable = false; + Cache.ActiveImatrixPath = null; + Cache.ActiveImatrixIdentityHash = null; + + if (Config.Current.Flags.UseImatrix) + { + AnsiConsole.MarkupLine("[yellow]Warning:[/] flags.use_imatrix is true, but validate-predictions was not given --imatrix-path or --imatrix-identity-hash. Strict validation will use the no-imatrix bucket."); + } + } + + private static string ResolveOutputDirectory(IReadOnlyList args) + { + string? explicitOutput = args.FirstOrDefault(a => string.Equals(a.Name, "output-dir", StringComparison.OrdinalIgnoreCase))?.Value; + if (!string.IsNullOrWhiteSpace(explicitOutput)) + return Path.GetFullPath(explicitOutput); + + if (!string.IsNullOrWhiteSpace(Config.OutputDirectory)) + return Path.Combine(Path.GetFullPath(Config.OutputDirectory!), "PredictionValidation"); + + return Path.Combine(Cache.ModelMagicQuantDirectory!, "PredictionValidation"); + } + + private static void ShowHelp() + { + AnsiConsole.MarkupLine("[bold]validate-predictions[/]"); + AnsiConsole.MarkupLine("Validates rank-safe isolation KLD predictions against existing SQLite category=General benchmark truth."); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[yellow]Required:[/]"); + AnsiConsole.MarkupLine(" --model-dir HuggingFace source model directory, or set paths.model_dir in YAML"); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[yellow]Optional:[/]"); + AnsiConsole.MarkupLine(" --architecture-family Uses configured family scope if present in SQLite"); + AnsiConsole.MarkupLine(" --imatrix-path Hash this imatrix and validate that exact bucket"); + AnsiConsole.MarkupLine(" --imatrix-identity-hash Validate an already-known imatrix bucket"); + AnsiConsole.MarkupLine(" --output-dir Report output directory"); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[grey]Outputs prediction_validation_general.csv and prediction_validation_general.md.[/]"); + } +} diff --git a/MagicQuant/Models/PredictionSelectionModels.cs b/MagicQuant/Models/PredictionSelectionModels.cs new file mode 100644 index 0000000..18a6de4 --- /dev/null +++ b/MagicQuant/Models/PredictionSelectionModels.cs @@ -0,0 +1,143 @@ +using MQ.DB.Models; + +namespace MagicQuant.Models; + +public sealed class RankSafePredictionRow +{ + public TensorConfig Config { get; init; } + public HybridQuant Quant { get; init; } = default!; + public ulong PredictedSizeBytes { get; set; } + public bool IsSizePredictable { get; set; } = true; + public double AdditiveKld { get; set; } + public double InteractionKld { get; set; } + public double PredictedKld { get; set; } + public double PredictedPpl { get; set; } + public double CrossTerm { get; set; } + public bool IsPureBaseline { get; init; } + public bool IsHybrid => !IsPureBaseline; + public bool IsPredictable { get; set; } = true; + public bool HasUnknownMappings { get; set; } + public string EffectiveStateKey { get; init; } = string.Empty; + public List Notes { get; init; } = new(); + + public double ActualKld { get; set; } = double.NaN; + public double ActualPpl { get; set; } = double.NaN; + public ulong? ActualSizeBytes { get; set; } + public int? ActualRank { get; set; } + public int? PredictedRank { get; set; } + + public double AbsoluteKldError => + double.IsNaN(ActualKld) ? double.NaN : Math.Abs(PredictedKld - ActualKld); + + public double SignedKldError => + double.IsNaN(ActualKld) ? double.NaN : PredictedKld - ActualKld; +} + +public sealed class RankSafePredictionSet +{ + public IReadOnlyList Rows { get; init; } = Array.Empty(); + public RankSafePredictionFit Fit { get; init; } = new(); + public IReadOnlyList Notes { get; init; } = Array.Empty(); + + public IReadOnlyList PredictableRows => + Rows.Where(x => x.IsPredictable).ToList(); +} + +public sealed class RankSafePredictionFit +{ + public double Alpha { get; init; } = 1.0d; + public double Beta { get; init; } = 0.0d; + public double BitStressThreshold { get; init; } = 8.0d; + public int FitRowCount { get; init; } + public double FitMae { get; init; } + public bool UsedFallback { get; init; } +} + +public sealed class HybridSelectionAnchor +{ + public BenchmarkSnapshotRecord Snapshot { get; init; } = default!; + public string Key => TensorConfigIdentity.ToKey(Snapshot.Config); +} + +public enum HybridSelectionReason +{ + StrictDominanceReplacement = 1, + NearBaselineOnePercentReplacement = 2, + InteriorSubspaceDiscovery = 3 +} + +public sealed class HybridSelectionCandidate +{ + public RankSafePredictionRow Prediction { get; init; } = default!; + public HybridSelectionReason Reason { get; init; } + public BenchmarkSnapshotRecord LowerDamageAnchor { get; init; } = default!; + public BenchmarkSnapshotRecord HigherDamageAnchor { get; init; } = default!; + public ulong WindowMinSizeBytes { get; init; } + public ulong WindowMaxSizeBytes { get; init; } + public double LinearExpectedKld { get; init; } + public double PredictedGainOverLine { get; init; } + public int AttemptOrder { get; init; } + public string WindowLabel { get; init; } = string.Empty; +} + +public sealed class CandidateValidationResult +{ + public HybridSelectionCandidate Candidate { get; init; } = default!; + public BenchmarkSnapshotRecord? Snapshot { get; init; } + public bool Accepted { get; init; } + public string Message { get; init; } = string.Empty; +} + +public sealed class BaselineEliminationRecord +{ + public BenchmarkSnapshotRecord Eliminated { get; init; } = default!; + public BenchmarkSnapshotRecord Eliminator { get; init; } = default!; + public string Reason { get; init; } = string.Empty; + public bool EliminatorIsHybrid => Eliminator.IsHybrid; + public double EliminatedKld => Eliminated.Kld; + public double EliminatorKld => Eliminator.Kld; + public ulong EliminatedSizeBytes => Eliminated.SizeBytes; + public ulong EliminatorSizeBytes => Eliminator.SizeBytes; +} + +public sealed class RankSafeValidationSummary +{ + public int RowCount { get; init; } + public int PredictableCount { get; init; } + public double Mae { get; init; } + public double Rmse { get; init; } + public double MaxAbsoluteError { get; init; } + public double MeanSignedError { get; init; } + public double PairwiseAccuracyPercent { get; init; } + public long ConcordantPairs { get; init; } + public long DiscordantPairs { get; init; } + public long TiedPredictedPairs { get; init; } + public int ExactRankMatches { get; init; } + public int WithinOneRank { get; init; } + public int WithinTwoRanks { get; init; } + public int WithinFiveRanks { get; init; } + public int WithinTenRanks { get; init; } + public int WithinTwentyRanks { get; init; } +} + +public sealed class PredictionValidationExportResult +{ + public RankSafeValidationSummary Summary { get; init; } = new(); + public string CsvPath { get; init; } = string.Empty; + public string MarkdownPath { get; init; } = string.Empty; + public IReadOnlyList Rows { get; init; } = Array.Empty(); +} + + +public sealed class PhaseValidationResult +{ + public IReadOnlyList AcceptedSnapshots { get; init; } = Array.Empty(); + public IReadOnlyList Attempts { get; init; } = Array.Empty(); +} + +public sealed class PredictionGuidedSelectionResult +{ + public IReadOnlyList Survivors { get; init; } = Array.Empty(); + public IReadOnlyList Eliminations { get; init; } = Array.Empty(); + public IReadOnlyList ValidationFailures { get; init; } = Array.Empty(); +} diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs new file mode 100644 index 0000000..a33d9f0 --- /dev/null +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -0,0 +1,638 @@ +using MagicQuant.Models; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +/// +/// Final hybrid chooser driven by the rank-safe isolation prediction engine. +/// +/// This service intentionally does not brute-force the whole remaining DuckDB space. +/// It only validates candidates whose predicted outcome proves one of the user-defined +/// survival claims: +/// 1. strict dominance over a pure/current anchor: lower KLD at same-or-smaller size +/// 2. near-baseline replacement: <= configured small size premium and better-than-linear KLD +/// 3. interior subspace discovery: better-than-linear KLD inside configurable size windows +/// +public sealed class PredictionGuidedHybridSelectionService +{ + private readonly QuantizationService _quantizationService; + private readonly HybridBenchmarkRepository _repository; + private readonly FinalRealBenchmarkEliminationService _finalEliminator; + + public PredictionGuidedHybridSelectionService( + QuantizationService quantizationService, + HybridBenchmarkRepository repository, + FinalRealBenchmarkEliminationService finalEliminator) + { + _quantizationService = quantizationService; + _repository = repository; + _finalEliminator = finalEliminator; + } + + public async Task RunAsync( + IReadOnlyList predictions, + IReadOnlyList pureBaselineSnapshots, + CancellationToken ct = default) + { + var eliminationRecords = new List(); + var validationFailures = new List(); + + var current = _finalEliminator.Eliminate(pureBaselineSnapshots).Survivors.ToList(); + AnsiConsole.MarkupLine($"[green]Pure/current anchor survivors after dominance:[/] [cyan]{current.Count:N0}[/]"); + + var strict = await RunStrictDominanceReplacementAsync(predictions, current, eliminationRecords, validationFailures, ct); + current = MergeAndDominanceFilter(current, strict.AcceptedSnapshots, eliminationRecords, "strict predicted hybrid dominance validated by real benchmark"); + + var near = await RunNearBaselineReplacementAsync(predictions, current, eliminationRecords, validationFailures, ct); + current = MergeAndDominanceFilter(current, near.AcceptedSnapshots, eliminationRecords, "near-baseline size-premium replacement validated by real benchmark"); + + var interior = await RunInteriorSubspaceDiscoveryAsync(predictions, current, validationFailures, ct); + current = MergeAndDominanceFilter(current, interior.AcceptedSnapshots, eliminationRecords, "interior subspace discovery dominated by real benchmark truth"); + + current = ApplyMeaningfulSpacing(current, eliminationRecords); + + var finalDominance = _finalEliminator.Eliminate(current); + foreach (var eliminated in finalDominance.Eliminated) + { + var eliminator = finalDominance.Survivors + .FirstOrDefault(x => Dominates(x, eliminated)); + + if (eliminator != null) + { + eliminationRecords.Add(new BaselineEliminationRecord + { + Eliminated = eliminated, + Eliminator = eliminator, + Reason = "final dominance pass" + }); + } + } + + return new PredictionGuidedSelectionResult + { + Survivors = finalDominance.Survivors.ToList(), + Eliminations = eliminationRecords + .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") + .ToList(), + ValidationFailures = validationFailures + }; + } + + private async Task RunStrictDominanceReplacementAsync( + IReadOnlyList predictions, + IReadOnlyList currentAnchors, + List eliminations, + List validationFailures, + CancellationToken ct) + { + AnsiConsole.Write(new Rule("[yellow]Prediction Phase 1: Strict Hybrid Dominance[/]") { Justification = Justify.Left }); + + var accepted = new List(); + var hybridPredictions = predictions + .Where(x => x.IsPredictable && x.IsSizePredictable && x.IsHybrid) + .ToList(); + + foreach (var anchor in currentAnchors.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes)) + { + var candidates = hybridPredictions + .Where(x => x.PredictedSizeBytes <= anchor.SizeBytes) + .Where(x => x.PredictedKld + Config.SelectionMinimumKldImprovementEpsilon < anchor.Kld) + .OrderBy(x => x.PredictedSizeBytes) + .ThenBy(x => x.PredictedKld) + .Take(Config.SelectionMaxFallbackAttemptsPerAnchor) + .Select((x, i) => new HybridSelectionCandidate + { + Prediction = x, + Reason = HybridSelectionReason.StrictDominanceReplacement, + LowerDamageAnchor = anchor, + HigherDamageAnchor = anchor, + WindowMinSizeBytes = 0, + WindowMaxSizeBytes = anchor.SizeBytes, + LinearExpectedKld = anchor.Kld, + PredictedGainOverLine = anchor.Kld - x.PredictedKld, + AttemptOrder = i + 1, + WindowLabel = $"strict <= {anchor.DisplayName}" + }) + .ToList(); + + if (candidates.Count == 0) + continue; + + CandidateValidationResult? acceptedForAnchor = null; + foreach (var candidate in candidates) + { + var validation = await BuildAndValidateSingleAsync( + candidate, + snapshot => snapshot.SizeBytes <= anchor.SizeBytes && + snapshot.Kld + Config.SelectionMinimumKldImprovementEpsilon < anchor.Kld, + $"must be <= {anchor.SizeBytes:N0} bytes and lower KLD than {anchor.DisplayName}", + ct); + + if (validation.Accepted && validation.Snapshot != null) + { + acceptedForAnchor = validation; + accepted.Add(validation.Snapshot); + eliminations.Add(new BaselineEliminationRecord + { + Eliminated = anchor, + Eliminator = validation.Snapshot, + Reason = "strict hybrid dominance: lower KLD at same-or-smaller real size" + }); + break; + } + + validationFailures.Add(validation); + } + + if (acceptedForAnchor == null) + { + AnsiConsole.MarkupLine($"[grey]No strict predicted replacement validated for anchor:[/] {Markup.Escape(anchor.DisplayName)}"); + } + } + + return new PhaseValidationResult { AcceptedSnapshots = accepted }; + } + + private async Task RunNearBaselineReplacementAsync( + IReadOnlyList predictions, + IReadOnlyList currentAnchors, + List eliminations, + List validationFailures, + CancellationToken ct) + { + AnsiConsole.Write(new Rule("[yellow]Prediction Phase 2: Near-Baseline Replacement[/]") { Justification = Justify.Left }); + + var accepted = new List(); + var pairs = BuildAdjacentPairs(currentAnchors); + + foreach (var pair in pairs) + { + var lowerSizeHigherDamage = pair.HigherDamageSmaller; + var upperSizeLowerDamage = pair.LowerDamageLarger; + + ulong min = lowerSizeHigherDamage.SizeBytes; + ulong max = AddPercent(min, Config.SelectionNearBaselineMaxSizeGrowthPercent); + + if (max > upperSizeLowerDamage.SizeBytes) + max = upperSizeLowerDamage.SizeBytes; + + var candidates = FindBetterThanLinearCandidates( + predictions, + lowerSizeHigherDamage, + upperSizeLowerDamage, + min, + max, + HybridSelectionReason.NearBaselineOnePercentReplacement, + $"near-baseline +{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}% {lowerSizeHigherDamage.DisplayName}") + .Take(Config.SelectionMaxFallbackAttemptsPerAnchor) + .ToList(); + + if (candidates.Count == 0) + continue; + + foreach (var candidate in candidates) + { + var validation = await BuildAndValidateSingleAsync( + candidate, + snapshot => snapshot.SizeBytes >= min && + snapshot.SizeBytes <= max && + BeatsLinearKldLine(snapshot.SizeBytes, snapshot.Kld, lowerSizeHigherDamage, upperSizeLowerDamage), + $"must land inside {min:N0}..{max:N0} bytes and beat the real linear KLD line", + ct); + + if (validation.Accepted && validation.Snapshot != null) + { + accepted.Add(validation.Snapshot); + eliminations.Add(new BaselineEliminationRecord + { + Eliminated = lowerSizeHigherDamage, + Eliminator = validation.Snapshot, + Reason = $"near-baseline replacement within +{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}% size premium" + }); + break; + } + + validationFailures.Add(validation); + } + } + + return new PhaseValidationResult { AcceptedSnapshots = accepted }; + } + + private async Task RunInteriorSubspaceDiscoveryAsync( + IReadOnlyList predictions, + IReadOnlyList currentAnchors, + List validationFailures, + CancellationToken ct) + { + AnsiConsole.Write(new Rule("[yellow]Prediction Phase 3: Interior Subspace Discovery[/]") { Justification = Justify.Left }); + + var accepted = new List(); + var pairs = BuildAdjacentPairs(currentAnchors); + + var allCandidates = new List(); + + foreach (var pair in pairs) + { + ulong lowSize = pair.HigherDamageSmaller.SizeBytes; + ulong highSize = pair.LowerDamageLarger.SizeBytes; + + if (highSize <= lowSize) + continue; + + ulong span = highSize - lowSize; + ulong cursor = lowSize; + + for (int i = 0; i < Config.SelectionInteriorWindowFractions.Count; i++) + { + double fraction = Config.SelectionInteriorWindowFractions[i]; + if (fraction <= 0d) + continue; + + ulong width = (ulong)Math.Round(span * fraction, MidpointRounding.AwayFromZero); + if (width == 0) + continue; + + ulong min = cursor; + ulong max = i == Config.SelectionInteriorWindowFractions.Count - 1 + ? Math.Min(highSize, cursor + width) + : Math.Min(highSize, cursor + width); + + if (max <= min) + continue; + + allCandidates.AddRange( + FindBetterThanLinearCandidates( + predictions, + pair.HigherDamageSmaller, + pair.LowerDamageLarger, + min, + max, + HybridSelectionReason.InteriorSubspaceDiscovery, + $"interior {i + 1}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}") + .Take(Config.SelectionMaxCandidatesPerInteriorWindow)); + + cursor = max; + + if (cursor >= highSize) + break; + } + } + + var deduped = allCandidates + .GroupBy(x => TensorConfigIdentity.ToKey(x.Prediction.Config), StringComparer.Ordinal) + .Select(g => g.OrderByDescending(x => x.PredictedGainOverLine).ThenBy(x => x.Prediction.PredictedSizeBytes).First()) + .OrderByDescending(x => x.PredictedGainOverLine) + .ThenBy(x => x.Prediction.PredictedSizeBytes) + .ToList(); + + if (deduped.Count == 0) + { + AnsiConsole.MarkupLine("[grey]No predicted interior candidates beat their local linear KLD lines.[/]"); + return new PhaseValidationResult(); + } + + AnsiConsole.MarkupLine($"[grey]Interior candidates selected for batch validation:[/] [cyan]{deduped.Count:N0}[/]"); + + var quantBatch = deduped.Select(x => x.Prediction.Quant).DistinctBy(x => TensorConfigIdentity.ToKey((TensorConfig)x)).ToList(); + var summary = await _quantizationService.ProcessHybridBatchAsync(quantBatch, ct); + AnsiConsole.MarkupLine($"[grey]Interior validation batch:[/] requested={summary.Requested:N0} completed={summary.Completed:N0} skipped={summary.Skipped:N0} failed={summary.Failed:N0}"); + + foreach (var candidate in deduped) + { + var snapshot = await _repository.LoadBenchmarkSnapshotAsync(candidate.Prediction.Config, ct); + bool acceptedCandidate = snapshot != null && + snapshot.SizeBytes >= candidate.WindowMinSizeBytes && + snapshot.SizeBytes <= candidate.WindowMaxSizeBytes && + BeatsLinearKldLine(snapshot.SizeBytes, snapshot.Kld, candidate.HigherDamageAnchor, candidate.LowerDamageAnchor); + + if (acceptedCandidate && snapshot != null) + { + accepted.Add(snapshot); + continue; + } + + validationFailures.Add(new CandidateValidationResult + { + Candidate = candidate, + Snapshot = snapshot, + Accepted = false, + Message = snapshot == null + ? "benchmark snapshot was not found after batch build" + : "real benchmark did not beat the local linear KLD line inside the requested size window" + }); + } + + return new PhaseValidationResult { AcceptedSnapshots = accepted }; + } + + private async Task BuildAndValidateSingleAsync( + HybridSelectionCandidate candidate, + Func accept, + string expectation, + CancellationToken ct) + { + AnsiConsole.MarkupLine( + $"[grey]Validating candidate:[/] {Markup.Escape(candidate.Prediction.Quant.BaseQuant.Names[0])} " + + $"[grey]| reason=[/] {candidate.Reason} [grey]| window=[/] {Markup.Escape(candidate.WindowLabel)}"); + + var summary = await _quantizationService.ProcessHybridBatchAsync(new[] { candidate.Prediction.Quant }, ct); + var snapshot = await _repository.LoadBenchmarkSnapshotAsync(candidate.Prediction.Config, ct); + + bool accepted = snapshot != null && accept(snapshot); + string message = accepted + ? "validated" + : snapshot == null + ? $"no benchmark snapshot was available after build attempt (completed={summary.Completed}, skipped={summary.Skipped}, failed={summary.Failed})" + : $"failed expectation: {expectation}; actual size={snapshot.SizeBytes:N0}, actual KLD={snapshot.Kld:0.000000}"; + + if (accepted && snapshot != null) + { + AnsiConsole.MarkupLine($"[green]Validated:[/] {Markup.Escape(snapshot.DisplayName)} size={snapshot.SizeBytes:N0} KLD={snapshot.Kld:0.000000}"); + } + else + { + AnsiConsole.MarkupLine($"[yellow]Rejected predicted candidate:[/] {Markup.Escape(message)}"); + } + + return new CandidateValidationResult + { + Candidate = candidate, + Snapshot = snapshot, + Accepted = accepted, + Message = message + }; + } + + private List FindBetterThanLinearCandidates( + IReadOnlyList predictions, + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger, + ulong minSize, + ulong maxSize, + HybridSelectionReason reason, + string windowLabel) + { + if (maxSize < minSize) + return new List(); + + var result = predictions + .Where(x => x.IsPredictable && x.IsSizePredictable && x.IsHybrid) + .Where(x => x.PredictedSizeBytes >= minSize && x.PredictedSizeBytes <= maxSize) + .Select(x => + { + double line = InterpolateKldLine(x.PredictedSizeBytes, higherDamageSmaller, lowerDamageLarger); + double gain = line - x.PredictedKld; + return new HybridSelectionCandidate + { + Prediction = x, + Reason = reason, + HigherDamageAnchor = higherDamageSmaller, + LowerDamageAnchor = lowerDamageLarger, + WindowMinSizeBytes = minSize, + WindowMaxSizeBytes = maxSize, + LinearExpectedKld = line, + PredictedGainOverLine = gain, + WindowLabel = windowLabel + }; + }) + .Where(x => x.PredictedGainOverLine > Config.SelectionMinimumKldImprovementEpsilon) + .Where(x => PassesNearLowerAnchorBrutality(x)) + .OrderByDescending(x => x.PredictedGainOverLine) + .ThenBy(x => x.Prediction.PredictedSizeBytes) + .ThenBy(x => x.Prediction.PredictedKld) + .Select((x, i) => + { + x = new HybridSelectionCandidate + { + Prediction = x.Prediction, + Reason = x.Reason, + HigherDamageAnchor = x.HigherDamageAnchor, + LowerDamageAnchor = x.LowerDamageAnchor, + WindowMinSizeBytes = x.WindowMinSizeBytes, + WindowMaxSizeBytes = x.WindowMaxSizeBytes, + LinearExpectedKld = x.LinearExpectedKld, + PredictedGainOverLine = x.PredictedGainOverLine, + WindowLabel = x.WindowLabel, + AttemptOrder = i + 1 + }; + return x; + }) + .ToList(); + + return result; + } + + private static bool PassesNearLowerAnchorBrutality(HybridSelectionCandidate candidate) + { + ulong span = candidate.LowerDamageAnchor.SizeBytes > candidate.HigherDamageAnchor.SizeBytes + ? candidate.LowerDamageAnchor.SizeBytes - candidate.HigherDamageAnchor.SizeBytes + : 0; + + if (span == 0) + return true; + + ulong distanceFromSmall = candidate.Prediction.PredictedSizeBytes > candidate.HigherDamageAnchor.SizeBytes + ? candidate.Prediction.PredictedSizeBytes - candidate.HigherDamageAnchor.SizeBytes + : 0; + + double fraction = distanceFromSmall / (double)span; + if (fraction > Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan) + return true; + + double requiredGain = Math.Max( + Config.SelectionMinimumKldImprovementEpsilon, + Math.Abs(candidate.HigherDamageAnchor.Kld - candidate.LowerDamageAnchor.Kld) * + Config.SelectionNearAnchorRequiredKldGainFractionOfPairGap); + + return candidate.PredictedGainOverLine >= requiredGain; + } + + private List ApplyMeaningfulSpacing( + IReadOnlyList snapshots, + List eliminations) + { + if (snapshots.Count <= 2) + return snapshots.ToList(); + + var ordered = snapshots + .OrderBy(x => x.SizeBytes) + .ThenBy(x => x.Kld) + .ToList(); + + ulong minSize = ordered.Min(x => x.SizeBytes); + ulong maxSize = ordered.Max(x => x.SizeBytes); + ulong globalSpan = maxSize > minSize ? maxSize - minSize : 0; + + if (globalSpan == 0) + return _finalEliminator.Eliminate(ordered).Survivors.ToList(); + + ulong minGap = (ulong)Math.Round(globalSpan * Config.SelectionMinimumNeighborGapFractionOfGlobalSpan, MidpointRounding.AwayFromZero); + if (minGap == 0) + return _finalEliminator.Eliminate(ordered).Survivors.ToList(); + + var kept = new List(); + + foreach (var snap in ordered) + { + var tooClose = kept + .Where(x => Distance(x.SizeBytes, snap.SizeBytes) < minGap) + .OrderBy(x => Distance(x.SizeBytes, snap.SizeBytes)) + .FirstOrDefault(); + + if (tooClose == null) + { + kept.Add(snap); + continue; + } + + var winner = ChooseSpacingWinner(tooClose, snap); + var loser = ReferenceEquals(winner, tooClose) ? snap : tooClose; + + if (!ReferenceEquals(winner, tooClose)) + { + kept.Remove(tooClose); + kept.Add(winner); + } + + eliminations.Add(new BaselineEliminationRecord + { + Eliminated = loser, + Eliminator = winner, + Reason = $"meaningful spacing collapse; size gap below {minGap:N0} bytes" + }); + } + + return _finalEliminator.Eliminate(kept).Survivors.ToList(); + } + + private static BenchmarkSnapshotRecord ChooseSpacingWinner(BenchmarkSnapshotRecord left, BenchmarkSnapshotRecord right) + { + if (Dominates(left, right)) + return left; + + if (Dominates(right, left)) + return right; + + return left.Kld.CompareTo(right.Kld) switch + { + < 0 => left, + > 0 => right, + _ => left.SizeBytes <= right.SizeBytes ? left : right + }; + } + + private List MergeAndDominanceFilter( + IReadOnlyList current, + IReadOnlyList additions, + List eliminations, + string reason) + { + if (additions.Count == 0) + return current.ToList(); + + var merged = current + .Concat(additions) + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .ToList(); + + var result = _finalEliminator.Eliminate(merged); + + foreach (var eliminated in result.Eliminated) + { + var eliminator = result.Survivors.FirstOrDefault(x => Dominates(x, eliminated)); + if (eliminator == null) + continue; + + eliminations.Add(new BaselineEliminationRecord + { + Eliminated = eliminated, + Eliminator = eliminator, + Reason = reason + }); + } + + return result.Survivors.ToList(); + } + + private static List BuildAdjacentPairs(IReadOnlyList anchors) + { + var ordered = anchors + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ToList(); + + var result = new List(); + + for (int i = 0; i < ordered.Count - 1; i++) + { + var lowerDamage = ordered[i]; + var higherDamage = ordered[i + 1]; + + if (higherDamage.SizeBytes >= lowerDamage.SizeBytes) + continue; + + result.Add(new AdjacentAnchorPair + { + LowerDamageLarger = lowerDamage, + HigherDamageSmaller = higherDamage + }); + } + + return result; + } + + private static bool BeatsLinearKldLine( + ulong candidateSize, + double candidateKld, + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger) + { + double expected = InterpolateKldLine(candidateSize, higherDamageSmaller, lowerDamageLarger); + return candidateKld + Config.SelectionMinimumKldImprovementEpsilon < expected; + } + + private static double InterpolateKldLine( + ulong candidateSize, + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger) + { + ulong smallSize = higherDamageSmaller.SizeBytes; + ulong largeSize = lowerDamageLarger.SizeBytes; + + if (largeSize <= smallSize) + return Math.Min(higherDamageSmaller.Kld, lowerDamageLarger.Kld); + + double t = Math.Clamp((candidateSize - smallSize) / (double)(largeSize - smallSize), 0d, 1d); + return higherDamageSmaller.Kld + ((lowerDamageLarger.Kld - higherDamageSmaller.Kld) * t); + } + + private static ulong AddPercent(ulong bytes, double percent) + { + if (percent <= 0d) + return bytes; + + double multiplier = 1d + (percent / 100d); + double result = bytes * multiplier; + if (result >= ulong.MaxValue) + return ulong.MaxValue; + + return (ulong)Math.Round(result, MidpointRounding.AwayFromZero); + } + + private static ulong Distance(ulong left, ulong right) => left >= right ? left - right : right - left; + + private static bool Dominates(BenchmarkSnapshotRecord better, BenchmarkSnapshotRecord worse) + { + bool sameOrSmaller = better.SizeBytes <= worse.SizeBytes; + bool strictlyLowerKld = better.Kld + Config.SelectionMinimumKldImprovementEpsilon < worse.Kld; + return sameOrSmaller && strictlyLowerKld; + } + + private sealed class AdjacentAnchorPair + { + public BenchmarkSnapshotRecord LowerDamageLarger { get; init; } = default!; + public BenchmarkSnapshotRecord HigherDamageSmaller { get; init; } = default!; + } +} diff --git a/MagicQuant/Services/PredictionValidationService.cs b/MagicQuant/Services/PredictionValidationService.cs new file mode 100644 index 0000000..547a98a --- /dev/null +++ b/MagicQuant/Services/PredictionValidationService.cs @@ -0,0 +1,333 @@ +using System.Globalization; +using System.Text; +using MagicQuant.Models; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +/// +/// Offline validator for the rank-safe isolation predictor. +/// It scores every currently benchmarked general-category combo in the active +/// scoped-model/imatrix bucket, compares predicted KLD to real KLD, and reports +/// rank/order accuracy. +/// +public sealed class PredictionValidationService +{ + private readonly HybridBenchmarkRepository _repository; + private readonly RankSafeKldPredictionService _predictionService; + + public PredictionValidationService( + HybridBenchmarkRepository repository, + RankSafeKldPredictionService predictionService) + { + _repository = repository; + _predictionService = predictionService; + } + + public async Task ExportAsync( + string outputDirectory, + CancellationToken ct = default) + { + Directory.CreateDirectory(outputDirectory); + + var actual = await _repository.LoadAllBenchmarkSnapshotsForCurrentContextAsync( + category: (byte)BenchmarkCategory.General, + strictImatrixContext: true, + ct: ct); + + if (actual.Count == 0) + throw new InvalidOperationException("No category=General benchmark snapshots were found for the active scoped model/imatrix context."); + + var predictions = await _predictionService.PredictAsync(actual.Select(x => x.Config).ToList(), ct); + + var actualByKey = actual.ToDictionary(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal); + var rows = predictions.Rows + .Where(x => x.IsPredictable) + .Where(x => actualByKey.ContainsKey(TensorConfigIdentity.ToKey(x.Config))) + .ToList(); + + foreach (var row in rows) + { + var snap = actualByKey[TensorConfigIdentity.ToKey(row.Config)]; + row.ActualKld = snap.Kld; + row.ActualPpl = snap.Ppl; + row.ActualSizeBytes = snap.SizeBytes; + } + + AssignRanks(rows); + + var summary = BuildSummary(rows); + string csvPath = Path.Combine(outputDirectory, "prediction_validation_general.csv"); + string markdownPath = Path.Combine(outputDirectory, "prediction_validation_general.md"); + + await File.WriteAllTextAsync(csvPath, BuildCsv(rows), ct); + await File.WriteAllTextAsync(markdownPath, BuildMarkdown(summary, rows, predictions.Notes), ct); + + PrintSummary(summary, csvPath, markdownPath); + + return new PredictionValidationExportResult + { + Summary = summary, + CsvPath = csvPath, + MarkdownPath = markdownPath, + Rows = rows + .OrderByDescending(x => x.AbsoluteKldError) + .ThenByDescending(x => Math.Abs((x.PredictedRank ?? 0) - (x.ActualRank ?? 0))) + .ToList() + }; + } + + private static void AssignRanks(IReadOnlyList rows) + { + int actualRank = 1; + foreach (var row in rows.OrderBy(x => x.ActualKld).ThenBy(x => x.ActualSizeBytes ?? ulong.MaxValue)) + row.ActualRank = actualRank++; + + int predictedRank = 1; + foreach (var row in rows.OrderBy(x => x.PredictedKld).ThenBy(x => x.PredictedSizeBytes)) + row.PredictedRank = predictedRank++; + } + + private static RankSafeValidationSummary BuildSummary(IReadOnlyList rows) + { + if (rows.Count == 0) + return new RankSafeValidationSummary(); + + double mae = rows.Average(x => x.AbsoluteKldError); + double rmse = Math.Sqrt(rows.Average(x => x.SignedKldError * x.SignedKldError)); + double maxAbs = rows.Max(x => x.AbsoluteKldError); + double meanSigned = rows.Average(x => x.SignedKldError); + + long concordant = 0; + long discordant = 0; + long tiedPred = 0; + + for (int i = 0; i < rows.Count; i++) + { + for (int j = i + 1; j < rows.Count; j++) + { + double actualDiff = rows[i].ActualKld - rows[j].ActualKld; + double predDiff = rows[i].PredictedKld - rows[j].PredictedKld; + + int actualSign = Math.Sign(actualDiff); + int predSign = Math.Sign(predDiff); + + if (predSign == 0) + { + tiedPred++; + continue; + } + + if (actualSign == 0 || actualSign == predSign) + concordant++; + else + discordant++; + } + } + + long denominator = concordant + discordant; + double pairwise = denominator == 0 ? 100d : concordant * 100d / denominator; + + int ShiftWithin(int maxShift) => + rows.Count(x => x.ActualRank.HasValue && x.PredictedRank.HasValue && + Math.Abs(x.PredictedRank.Value - x.ActualRank.Value) <= maxShift); + + return new RankSafeValidationSummary + { + RowCount = rows.Count, + PredictableCount = rows.Count, + Mae = mae, + Rmse = rmse, + MaxAbsoluteError = maxAbs, + MeanSignedError = meanSigned, + PairwiseAccuracyPercent = pairwise, + ConcordantPairs = concordant, + DiscordantPairs = discordant, + TiedPredictedPairs = tiedPred, + ExactRankMatches = ShiftWithin(0), + WithinOneRank = ShiftWithin(1), + WithinTwoRanks = ShiftWithin(2), + WithinFiveRanks = ShiftWithin(5), + WithinTenRanks = ShiftWithin(10), + WithinTwentyRanks = ShiftWithin(20) + }; + } + + private static string BuildCsv(IReadOnlyList rows) + { + var sb = new StringBuilder(); + sb.AppendLine("config_key,display_name,is_hybrid,base_quant,is_size_predictable,predicted_kld,actual_kld,abs_error,signed_error,predicted_rank,actual_rank,rank_shift,predicted_size_bytes,actual_size_bytes,size_abs_error_bytes,size_abs_error_percent,predicted_ppl,actual_ppl,effective_groups,notes"); + + foreach (var row in rows + .OrderByDescending(x => x.AbsoluteKldError) + .ThenByDescending(x => Math.Abs((x.PredictedRank ?? 0) - (x.ActualRank ?? 0)))) + { + int shift = (row.PredictedRank ?? 0) - (row.ActualRank ?? 0); + sb.Append(Csv(TensorConfigIdentity.ToKey(row.Config))).Append(','); + sb.Append(Csv(HybridBenchmarkRepository.BuildDisplayName(row.Quant))).Append(','); + sb.Append(row.IsHybrid ? "true" : "false").Append(','); + sb.Append(Csv(row.Quant.BaseQuant.Names[0])).Append(','); + sb.Append(row.IsSizePredictable ? "true" : "false").Append(','); + sb.Append(Format(row.PredictedKld)).Append(','); + sb.Append(Format(row.ActualKld)).Append(','); + sb.Append(Format(row.AbsoluteKldError)).Append(','); + sb.Append(Format(row.SignedKldError)).Append(','); + sb.Append(row.PredictedRank?.ToString(CultureInfo.InvariantCulture) ?? "").Append(','); + sb.Append(row.ActualRank?.ToString(CultureInfo.InvariantCulture) ?? "").Append(','); + sb.Append(shift.ToString(CultureInfo.InvariantCulture)).Append(','); + sb.Append(row.PredictedSizeBytes.ToString(CultureInfo.InvariantCulture)).Append(','); + sb.Append(row.ActualSizeBytes?.ToString(CultureInfo.InvariantCulture) ?? "").Append(','); + long sizeError = row.ActualSizeBytes.HasValue ? (long)row.PredictedSizeBytes - (long)row.ActualSizeBytes.Value : 0L; + double sizeErrorPct = row.ActualSizeBytes.HasValue && row.ActualSizeBytes.Value > 0 + ? Math.Abs(sizeError) * 100d / row.ActualSizeBytes.Value + : double.NaN; + sb.Append(row.ActualSizeBytes.HasValue ? Math.Abs(sizeError).ToString(CultureInfo.InvariantCulture) : "").Append(','); + sb.Append(row.ActualSizeBytes.HasValue ? Format(sizeErrorPct) : "").Append(','); + sb.Append(Format(row.PredictedPpl)).Append(','); + sb.Append(Format(row.ActualPpl)).Append(','); + sb.Append(Csv(BuildEffectiveGroupSummary(row.Config))).Append(','); + sb.Append(Csv(string.Join(" | ", row.Notes))); + sb.AppendLine(); + } + + return sb.ToString(); + } + + private static string BuildMarkdown( + RankSafeValidationSummary summary, + IReadOnlyList rows, + IReadOnlyList notes) + { + var sb = new StringBuilder(); + sb.AppendLine("# MagicQuant Rank-Safe Prediction Validation"); + sb.AppendLine(); + sb.AppendLine("This report compares predicted KLD to real category=General benchmark KLD for the active scoped model/imatrix bucket."); + sb.AppendLine(); + + sb.AppendLine("## Summary"); + sb.AppendLine(); + sb.AppendLine("| Metric | Value |"); + sb.AppendLine("|---|---:|"); + sb.AppendLine($"| Rows | {summary.RowCount:N0} |"); + sb.AppendLine($"| MAE | {summary.Mae:0.000000} |"); + sb.AppendLine($"| RMSE | {summary.Rmse:0.000000} |"); + sb.AppendLine($"| Max abs error | {summary.MaxAbsoluteError:0.000000} |"); + sb.AppendLine($"| Mean signed error | {summary.MeanSignedError:0.000000} |"); + sb.AppendLine($"| Pairwise order accuracy | {summary.PairwiseAccuracyPercent:0.0000}% |"); + sb.AppendLine($"| Concordant pairs | {summary.ConcordantPairs:N0} |"); + sb.AppendLine($"| Discordant pairs | {summary.DiscordantPairs:N0} |"); + sb.AppendLine($"| Tied predicted pairs | {summary.TiedPredictedPairs:N0} |"); + + var sizeRows = rows.Where(x => x.ActualSizeBytes.HasValue && x.IsSizePredictable).ToList(); + if (sizeRows.Count > 0) + { + double sizeMaePercent = sizeRows.Average(x => Math.Abs((long)x.PredictedSizeBytes - (long)x.ActualSizeBytes!.Value) * 100d / x.ActualSizeBytes!.Value); + double sizeMaxPercent = sizeRows.Max(x => Math.Abs((long)x.PredictedSizeBytes - (long)x.ActualSizeBytes!.Value) * 100d / x.ActualSizeBytes!.Value); + sb.AppendLine($"| Size-safe rows | {sizeRows.Count:N0} |"); + sb.AppendLine($"| Size MAE % | {sizeMaePercent:0.0000}% |"); + sb.AppendLine($"| Size max abs % | {sizeMaxPercent:0.0000}% |"); + } + sb.AppendLine(); + + sb.AppendLine("## Rank movement"); + sb.AppendLine(); + sb.AppendLine("| Window | Count | Percent |"); + sb.AppendLine("|---|---:|---:|"); + AppendRankWindow(sb, "Exact", summary.ExactRankMatches, summary.RowCount); + AppendRankWindow(sb, "Within 1", summary.WithinOneRank, summary.RowCount); + AppendRankWindow(sb, "Within 2", summary.WithinTwoRanks, summary.RowCount); + AppendRankWindow(sb, "Within 5", summary.WithinFiveRanks, summary.RowCount); + AppendRankWindow(sb, "Within 10", summary.WithinTenRanks, summary.RowCount); + AppendRankWindow(sb, "Within 20", summary.WithinTwentyRanks, summary.RowCount); + sb.AppendLine(); + + if (notes.Count > 0) + { + sb.AppendLine("## Prediction notes"); + sb.AppendLine(); + foreach (var note in notes) + sb.AppendLine($"- {note}"); + sb.AppendLine(); + } + + sb.AppendLine("## Largest size misses"); + sb.AppendLine(); + sb.AppendLine("| Name | Size Safe | Pred Size GB | Actual Size GB | Abs Error MB | Error % | Groups |"); + sb.AppendLine("|---|---:|---:|---:|---:|---:|---|"); + foreach (var row in rows + .Where(x => x.ActualSizeBytes.HasValue) + .OrderByDescending(x => Math.Abs((long)x.PredictedSizeBytes - (long)x.ActualSizeBytes!.Value)) + .Take(50)) + { + long absBytes = Math.Abs((long)row.PredictedSizeBytes - (long)row.ActualSizeBytes!.Value); + double pct = row.ActualSizeBytes.Value == 0 ? 0d : absBytes * 100d / row.ActualSizeBytes.Value; + double mb = absBytes / 1024d / 1024d; + sb.AppendLine($"| {EscapePipe(HybridBenchmarkRepository.BuildDisplayName(row.Quant))} | {(row.IsSizePredictable ? "yes" : "no")} | {ToGb(row.PredictedSizeBytes)} | {ToGb(row.ActualSizeBytes.Value)} | {mb:0.00} | {pct:0.0000}% | {EscapePipe(BuildEffectiveGroupSummary(row.Config))} |"); + } + sb.AppendLine(); + + sb.AppendLine("## Largest KLD misses"); + sb.AppendLine(); + sb.AppendLine("| Name | Predicted KLD | Actual KLD | Abs Error | Pred Rank | Actual Rank | Shift | Size Pred GB | Size Actual GB | Groups |"); + sb.AppendLine("|---|---:|---:|---:|---:|---:|---:|---:|---:|---|"); + + foreach (var row in rows + .OrderByDescending(x => x.AbsoluteKldError) + .ThenByDescending(x => Math.Abs((x.PredictedRank ?? 0) - (x.ActualRank ?? 0))) + .Take(100)) + { + int shift = (row.PredictedRank ?? 0) - (row.ActualRank ?? 0); + sb.AppendLine( + $"| {EscapePipe(HybridBenchmarkRepository.BuildDisplayName(row.Quant))} | {row.PredictedKld:0.000000} | {row.ActualKld:0.000000} | {row.AbsoluteKldError:0.000000} | " + + $"{row.PredictedRank} | {row.ActualRank} | {shift:+#;-#;0} | {ToGb(row.PredictedSizeBytes)} | {ToGb(row.ActualSizeBytes ?? 0)} | {EscapePipe(BuildEffectiveGroupSummary(row.Config))} |"); + } + + return sb.ToString(); + } + + private static void PrintSummary(RankSafeValidationSummary summary, string csvPath, string markdownPath) + { + AnsiConsole.Write(new Rule("[yellow]Prediction Validation[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[green]Rows:[/] [cyan]{summary.RowCount:N0}[/]"); + AnsiConsole.MarkupLine($"[green]MAE:[/] [cyan]{summary.Mae:0.000000}[/] [green]RMSE:[/] [cyan]{summary.Rmse:0.000000}[/] [green]MaxAbs:[/] [cyan]{summary.MaxAbsoluteError:0.000000}[/]"); + AnsiConsole.MarkupLine($"[green]Pairwise order accuracy:[/] [cyan]{summary.PairwiseAccuracyPercent:0.0000}%[/] [grey]discordant={summary.DiscordantPairs:N0} tied-pred={summary.TiedPredictedPairs:N0}[/]"); + AnsiConsole.MarkupLine($"[green]CSV:[/] [blue]{Markup.Escape(csvPath)}[/]"); + AnsiConsole.MarkupLine($"[green]Markdown:[/] [blue]{Markup.Escape(markdownPath)}[/]"); + } + + private static void AppendRankWindow(StringBuilder sb, string label, int count, int total) + { + double pct = total == 0 ? 0d : count * 100d / total; + sb.AppendLine($"| {label} | {count:N0} | {pct:0.00}% |"); + } + + private static string BuildEffectiveGroupSummary(TensorConfig config) + { + return string.Join("; ", + RankSafeKldPredictionService.EnumerateEffectiveBaselines(config) + .Select(x => + { + var baseline = BaselineQuants.FromId(x.EffectiveBaselineId); + return $"{x.Group.Name}={baseline.Names[0]}"; + })); + } + + private static string Format(double value) => + double.IsNaN(value) || double.IsInfinity(value) + ? "" + : value.ToString("0.000000########", CultureInfo.InvariantCulture); + + private static string Csv(string value) + { + value ??= string.Empty; + if (!value.Contains(',') && !value.Contains('"') && !value.Contains('\n') && !value.Contains('\r')) + return value; + + return "\"" + value.Replace("\"", "\"\"") + "\""; + } + + private static string ToGb(ulong bytes) => (bytes / 1024d / 1024d / 1024d).ToString("0.00", CultureInfo.InvariantCulture); + private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); +} diff --git a/MagicQuant/Services/RankSafeKldPredictionService.cs b/MagicQuant/Services/RankSafeKldPredictionService.cs new file mode 100644 index 0000000..ce83254 --- /dev/null +++ b/MagicQuant/Services/RankSafeKldPredictionService.cs @@ -0,0 +1,689 @@ +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +/// +/// Central prediction authority for MagicQuant's isolation-truth KLD estimator. +/// +/// This intentionally replaces the old MDA / bucket-survival prediction path. +/// It combines: +/// - Q8-carrier, native-exact blanket, single-group isolation measurements +/// - additive isolation KLD +/// - bit-stress interaction correction +/// - rank-safe isotonic projection over the additive backbone +/// +public sealed class RankSafeKldPredictionService +{ + private readonly HybridBenchmarkRepository _repository; + private readonly EffectiveCandidateStateResolverService _effectiveResolver; + + public RankSafeKldPredictionService( + HybridBenchmarkRepository repository, + EffectiveCandidateStateResolverService effectiveResolver) + { + _repository = repository; + _effectiveResolver = effectiveResolver; + } + + public async Task PredictAsync( + IReadOnlyCollection configs, + CancellationToken ct = default) + { + if (configs == null) + throw new ArgumentNullException(nameof(configs)); + + var context = await BuildContextAsync(ct); + var uniqueConfigs = configs + .DistinctBy(TensorConfigIdentity.ToKey) + .ToList(); + + var rows = new List(uniqueConfigs.Count); + + foreach (var config in uniqueConfigs) + { + ct.ThrowIfCancellationRequested(); + var row = await PredictSingleAsync(config, context, ct); + rows.Add(row); + } + + var fitRows = await LoadFitRowsAsync(context, rows, ct); + var fit = FitInteractionModel(fitRows, context); + + foreach (var row in rows.Where(x => x.IsPredictable)) + { + row.CrossTerm = ComputeCrossTerm(row.Config, context, fit.BitStressThreshold); + row.InteractionKld = Math.Max(0d, (fit.Alpha * row.AdditiveKld) + (fit.Beta * row.CrossTerm)); + } + + ApplyRankSafeProjection(rows); + + var notes = new List(context.Notes); + notes.Add($"Prediction fit rows: {fit.FitRowCount:N0}; alpha={fit.Alpha:G6}; beta={fit.Beta:G6}; bit-stress-threshold={fit.BitStressThreshold:G4}; fallback={fit.UsedFallback}."); + + PrintPredictionDiagnostics(rows, fit); + return new RankSafePredictionSet + { + Rows = rows + .OrderBy(x => x.PredictedKld) + .ThenBy(x => x.IsSizePredictable ? 0 : 1) + .ThenBy(x => x.PredictedSizeBytes) + .ToList(), + Fit = fit, + Notes = notes + }; + } + + private async Task PredictSingleAsync( + TensorConfig config, + PredictionContext context, + CancellationToken ct) + { + var quant = (HybridQuant)config; + var effective = await _effectiveResolver.ResolveAsync(config, ct); + + var row = new RankSafePredictionRow + { + Config = config, + Quant = quant, + IsPureBaseline = TensorConfigIdentity.IsPureBaseline(config), + EffectiveStateKey = effective.EffectiveStateKey, + HasUnknownMappings = effective.HasUnknownMappings, + Notes = effective.Warnings.ToList() + }; + + byte normalizedBaseId = NormalizeBaselineIdForIsolation(config.BaseQuant); + + if (row.IsPureBaseline && context.PureSnapshotsByBaselineId.TryGetValue(config.BaseQuant, out var pureDirect)) + { + row.PredictedSizeBytes = pureDirect.SizeBytes; + row.AdditiveKld = pureDirect.Kld; + row.InteractionKld = pureDirect.Kld; + row.PredictedKld = pureDirect.Kld; + row.PredictedPpl = pureDirect.Ppl; + return row; + } + + if (row.IsPureBaseline && context.PureSnapshotsByBaselineId.TryGetValue(normalizedBaseId, out var pureNormalized)) + { + row.PredictedSizeBytes = pureNormalized.SizeBytes; + row.AdditiveKld = pureNormalized.Kld; + row.InteractionKld = pureNormalized.Kld; + row.PredictedKld = pureNormalized.Kld; + row.PredictedPpl = pureNormalized.Ppl; + row.Notes.Add($"Pure baseline '{quant.BaseQuant.Names[0]}' was normalized to '{pureNormalized.Quant.BaseQuant.Names[0]}' for prediction."); + return row; + } + + row.PredictedSizeBytes = PredictSize(config, context, row.Notes, out bool canPredictSize); + row.IsSizePredictable = canPredictSize; + row.AdditiveKld = PredictAdditiveKld(config, context, row.Notes, out bool canPredict); + row.PredictedPpl = PredictPpl(config, context, row.Notes); + + if (!canPredict) + { + row.IsPredictable = false; + row.InteractionKld = double.PositiveInfinity; + row.PredictedKld = double.PositiveInfinity; + return row; + } + + row.InteractionKld = row.AdditiveKld; + row.PredictedKld = row.AdditiveKld; + return row; + } + + private async Task BuildContextAsync(CancellationToken ct) + { + var activeGroups = TReg.All + .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + + if (activeGroups.Count == 0) + throw new InvalidOperationException("No active tensor groups were available for prediction."); + + var notes = new List(); + var pureSnapshots = await _repository.LoadPureBaselineSnapshotsAsync(ct); + var pureByBaselineId = pureSnapshots + .GroupBy(x => x.Quant.BaseQuant.UniqueId) + .ToDictionary( + g => g.Key, + g => g.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes).First()); + + if (!pureByBaselineId.TryGetValue(BaselineQuants.Q8_0.UniqueId, out var pureQ8)) + throw new InvalidOperationException("Rank-safe prediction requires a pure Q8_0 benchmark snapshot."); + + var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + var q8BaseOnlyQuant = HybridQuant.CreateExactBlanket( + baseQuant: BaselineQuants.Q8_0, + groups: activeGroups, + exactScheme: nativeExactScheme); + + var q8BaseOnly = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)q8BaseOnlyQuant, ct); + if (q8BaseOnly == null) + { + notes.Add("Q8 native-exact base-only anchor was missing. Size fallback will use pure Q8; KLD exact/Q8 contributions remain zero."); + q8BaseOnly = pureQ8; + } + + var baseOnlyByBaselineId = new Dictionary + { + [BaselineQuants.Q8_0.UniqueId] = q8BaseOnly + }; + + foreach (var baseline in BaselineQuants.GetAllRecognizedBaselines() + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .OrderBy(x => x.UniqueId)) + { + byte normalizedBaselineId = NormalizeBaselineIdForIsolation(baseline.UniqueId); + if (baseOnlyByBaselineId.ContainsKey(normalizedBaselineId)) + continue; + + var normalizedBaseline = BaselineQuants.FromId(normalizedBaselineId); + var baseOnlyQuant = HybridQuant.CreateExactBlanket( + baseQuant: normalizedBaseline, + groups: activeGroups, + exactScheme: nativeExactScheme); + + var baseOnlySnapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)baseOnlyQuant, ct); + if (baseOnlySnapshot != null) + baseOnlyByBaselineId[normalizedBaselineId] = baseOnlySnapshot; + } + + var isolationByGroupAndBaseline = new Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord>(); + + foreach (var group in activeGroups) + { + foreach (var baseline in BaselineQuants.GetAllRecognizedBaselines() + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .OrderBy(x => x.UniqueId)) + { + var normalizedBaselineId = NormalizeBaselineIdForIsolation(baseline.UniqueId); + + if (isolationByGroupAndBaseline.ContainsKey((group.UniqueId, normalizedBaselineId))) + continue; + + var isolationQuant = HybridQuant.CreateExactBlanket( + baseQuant: BaselineQuants.Q8_0, + groups: activeGroups, + exactScheme: nativeExactScheme); + + isolationQuant.SetLearnedCandidateOverride(group, BaselineQuants.FromId(normalizedBaselineId)); + var snapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); + + if (snapshot != null) + isolationByGroupAndBaseline[(group.UniqueId, normalizedBaselineId)] = snapshot; + } + } + + return new PredictionContext( + activeGroups: activeGroups, + pureQ8: pureQ8, + q8BaseOnly: q8BaseOnly, + pureSnapshotsByBaselineId: pureByBaselineId, + baseOnlySnapshotsByBaselineId: baseOnlyByBaselineId, + isolationByGroupAndBaseline: isolationByGroupAndBaseline, + notes: notes); + } + + private async Task> LoadFitRowsAsync( + PredictionContext context, + IReadOnlyList alreadyPredicted, + CancellationToken ct) + { + var allBenchmarkRows = await _repository.LoadAllBenchmarkSnapshotsForCurrentContextAsync( + category: (byte)BenchmarkCategory.General, + strictImatrixContext: true, + ct: ct); + + var alreadyByKey = alreadyPredicted.ToDictionary(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal); + var fitRows = new List(); + + foreach (var snapshot in allBenchmarkRows) + { + ct.ThrowIfCancellationRequested(); + + RankSafePredictionRow predicted; + if (!alreadyByKey.TryGetValue(TensorConfigIdentity.ToKey(snapshot.Config), out predicted!)) + { + predicted = await PredictSingleAsync(snapshot.Config, context, ct); + } + + if (!predicted.IsPredictable || double.IsInfinity(predicted.AdditiveKld) || double.IsNaN(predicted.AdditiveKld)) + continue; + + fitRows.Add(new FitObservation + { + Config = snapshot.Config, + ActualKld = Math.Max(0d, snapshot.Kld), + AdditiveKld = predicted.AdditiveKld + }); + } + + return fitRows; + } + + private RankSafePredictionFit FitInteractionModel( + IReadOnlyList observations, + PredictionContext context) + { + var usable = observations + .Where(x => x.ActualKld >= 0d) + .Where(x => !double.IsNaN(x.AdditiveKld) && !double.IsInfinity(x.AdditiveKld)) + .ToList(); + + if (usable.Count < Math.Max(3, Config.PredictionMinimumFitRows)) + { + return new RankSafePredictionFit + { + Alpha = 1.0d, + Beta = 0.0d, + BitStressThreshold = Config.PredictionDefaultBitStressThreshold, + FitRowCount = usable.Count, + UsedFallback = true + }; + } + + RankSafePredictionFit? best = null; + + foreach (double threshold in Config.PredictionBitStressThresholdCandidates) + { + double s11 = 0d; + double s12 = 0d; + double s22 = 0d; + double y1 = 0d; + double y2 = 0d; + + var crossTerms = new Dictionary(StringComparer.Ordinal); + foreach (var row in usable) + { + double x1 = row.AdditiveKld; + double x2 = ComputeCrossTerm(row.Config, context, threshold); + double y = row.ActualKld; + + s11 += x1 * x1; + s12 += x1 * x2; + s22 += x2 * x2; + y1 += x1 * y; + y2 += x2 * y; + crossTerms[TensorConfigIdentity.ToKey(row.Config)] = x2; + } + + double det = (s11 * s22) - (s12 * s12); + double alpha; + double beta; + + if (Math.Abs(det) <= 1e-18d) + { + alpha = s11 <= 1e-18d ? 1.0d : y1 / s11; + beta = 0.0d; + } + else + { + alpha = ((y1 * s22) - (y2 * s12)) / det; + beta = ((s11 * y2) - (s12 * y1)) / det; + } + + if (double.IsNaN(alpha) || double.IsInfinity(alpha)) + alpha = 1.0d; + + if (double.IsNaN(beta) || double.IsInfinity(beta)) + beta = 0.0d; + + // Keep the correction sane. The fit can be noisy when the only benchmarked + // rows are pure baselines and isolation probes. + alpha = Math.Clamp(alpha, 0.05d, 10.0d); + beta = Math.Clamp(beta, -1_000_000d, 1_000_000d); + + double mae = usable + .Select(x => + { + double cross = crossTerms[TensorConfigIdentity.ToKey(x.Config)]; + double pred = Math.Max(0d, (alpha * x.AdditiveKld) + (beta * cross)); + return Math.Abs(pred - x.ActualKld); + }) + .Average(); + + var candidate = new RankSafePredictionFit + { + Alpha = alpha, + Beta = beta, + BitStressThreshold = threshold, + FitRowCount = usable.Count, + FitMae = mae, + UsedFallback = false + }; + + if (best == null || candidate.FitMae < best.FitMae) + best = candidate; + } + + return best ?? new RankSafePredictionFit + { + Alpha = 1.0d, + Beta = 0.0d, + BitStressThreshold = Config.PredictionDefaultBitStressThreshold, + FitRowCount = usable.Count, + UsedFallback = true + }; + } + + private static void ApplyRankSafeProjection(IReadOnlyList rows) + { + var predictable = rows + .Where(x => x.IsPredictable) + .OrderBy(x => x.AdditiveKld) + .ThenBy(x => x.InteractionKld) + .ThenBy(x => x.PredictedSizeBytes) + .ToList(); + + if (predictable.Count == 0) + return; + + double[] projected = Pava(predictable.Select(x => x.InteractionKld).ToArray()); + + for (int i = 0; i < predictable.Count; i++) + predictable[i].PredictedKld = Math.Max(0d, projected[i]); + + int rank = 1; + foreach (var row in rows + .Where(x => x.IsPredictable) + .OrderBy(x => x.PredictedKld) + .ThenBy(x => x.PredictedSizeBytes)) + { + row.PredictedRank = rank++; + } + } + + private static double[] Pava(double[] values) + { + var blocks = new List(); + + foreach (double value in values) + { + blocks.Add(new PavaBlock { Sum = value, Weight = 1d, Count = 1 }); + + while (blocks.Count >= 2) + { + var right = blocks[^1]; + var left = blocks[^2]; + + if (left.Mean <= right.Mean) + break; + + left.Sum += right.Sum; + left.Weight += right.Weight; + left.Count += right.Count; + blocks[^2] = left; + blocks.RemoveAt(blocks.Count - 1); + } + } + + var result = new double[values.Length]; + int index = 0; + foreach (var block in blocks) + { + double mean = block.Mean; + for (int i = 0; i < block.Count; i++) + result[index++] = mean; + } + + return result; + } + + private double PredictAdditiveKld( + TensorConfig config, + PredictionContext context, + List notes, + out bool canPredict) + { + canPredict = true; + double total = 0d; + + foreach (var (group, effectiveBaselineId) in EnumerateEffectiveBaselines(config, context.ActiveGroups)) + { + if (IsZeroDamageAlias(effectiveBaselineId)) + continue; + + byte normalized = NormalizeBaselineIdForIsolation(effectiveBaselineId); + if (IsZeroDamageAlias(normalized)) + continue; + + if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalized), out var isolation)) + { + notes.Add($"Missing KLD isolation snapshot for group '{group.Name}' and baseline id '{normalized}'."); + canPredict = false; + continue; + } + + total += Math.Max(0d, isolation.Kld); + } + + return Math.Max(0d, total); + } + + private double PredictPpl( + TensorConfig config, + PredictionContext context, + List notes) + { + double total = 0d; + + foreach (var (group, effectiveBaselineId) in EnumerateEffectiveBaselines(config, context.ActiveGroups)) + { + if (IsZeroDamageAlias(effectiveBaselineId)) + continue; + + byte normalized = NormalizeBaselineIdForIsolation(effectiveBaselineId); + if (context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalized), out var isolation)) + total += isolation.Ppl; + } + + return total; + } + + private ulong PredictSize( + TensorConfig config, + PredictionContext context, + List notes, + out bool canPredictSize) + { + canPredictSize = true; + byte normalizedBaseId = NormalizeBaselineIdForIsolation(config.BaseQuant); + + if (!context.BaseOnlySnapshotsByBaselineId.TryGetValue(normalizedBaseId, out var baseOnlyAnchor)) + { + notes.Add($"Missing base-only size anchor for base baseline id '{normalizedBaseId}'. Size prediction is not safe for selection."); + canPredictSize = false; + return 0; + } + + long total = (long)baseOnlyAnchor.SizeBytes; + long q8ExactBlanketSize = (long)context.Q8BaseOnly.SizeBytes; + + foreach (var (group, effectiveBaselineId) in EnumerateEffectiveBaselines(config, context.ActiveGroups)) + { + byte normalizedTargetId = NormalizeBaselineIdForIsolation(effectiveBaselineId); + + // Base-only anchors already hold every active group at native exact precision. + // Exact aliases therefore contribute no size delta. + if (BaselineQuants.IsNativeExactAlias(normalizedTargetId)) + continue; + + if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalizedTargetId), out var targetIsolation)) + { + notes.Add($"Missing group size-isolation snapshot for group '{group.Name}' and effective baseline id '{normalizedTargetId}'. Size prediction is not safe for selection."); + canPredictSize = false; + continue; + } + + total += (long)targetIsolation.SizeBytes - q8ExactBlanketSize; + } + + if (total <= 0) + { + notes.Add($"Predicted size collapsed to {total:N0} bytes. Size prediction is not safe for selection."); + canPredictSize = false; + return 0; + } + + return (ulong)total; + } + + private double ComputeCrossTerm(TensorConfig config, PredictionContext context, double threshold) + { + var contributions = new List<(double Kld, double Bits)>(); + + foreach (var (group, effectiveBaselineId) in EnumerateEffectiveBaselines(config, context.ActiveGroups)) + { + if (IsZeroDamageAlias(effectiveBaselineId)) + continue; + + byte normalized = NormalizeBaselineIdForIsolation(effectiveBaselineId); + if (IsZeroDamageAlias(normalized)) + continue; + + if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalized), out var isolation)) + continue; + + var baseline = BaselineQuants.FromId(normalized); + contributions.Add((Math.Max(0d, isolation.Kld), baseline.BitRange)); + } + + double cross = 0d; + for (int i = 0; i < contributions.Count; i++) + { + for (int j = i + 1; j < contributions.Count; j++) + { + double stressI = Math.Max(0d, threshold - contributions[i].Bits); + double stressJ = Math.Max(0d, threshold - contributions[j].Bits); + if (stressI <= 0d || stressJ <= 0d) + continue; + + cross += contributions[i].Kld * contributions[j].Kld * stressI * stressJ; + } + } + + return cross; + } + + public static IReadOnlyList<(TensorGroup Group, byte EffectiveBaselineId)> EnumerateEffectiveBaselines( + TensorConfig config, + IReadOnlyList? activeGroups = null) + { + activeGroups ??= TReg.All + .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + + var result = new List<(TensorGroup Group, byte EffectiveBaselineId)>(activeGroups.Count); + + foreach (var (group, storedValue) in TensorConfigIdentity.EnumerateGroupSlots(config)) + { + if (!activeGroups.Any(x => x.UniqueId == group.UniqueId)) + continue; + + byte effective = BaselineQuants.IsNullTensorConfigGroupSlot(storedValue) + ? config.BaseQuant + : BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedValue); + + result.Add((group, effective)); + } + + return result; + } + + public static byte NormalizeBaselineIdForIsolation(byte baselineId) + { + if (BaselineQuants.IsNativeExactAlias(baselineId)) + return baselineId; + + var baseline = BaselineQuants.FromId(baselineId); + if (!baseline.IsExternalRepositoryBaseline) + return baselineId; + + var builtIn = BaselineQuants.ResolveBuiltInStandardBaseline(baseline.QuantizeBaseArgumentName) + ?? BaselineQuants.ResolveBuiltInStandardBaseline(baseline.Names[0]); + + return builtIn?.UniqueId ?? baselineId; + } + + private static bool IsZeroDamageAlias(byte baselineId) + { + return baselineId == BaselineQuants.Q8_0.UniqueId || + BaselineQuants.IsNativeExactAlias(baselineId); + } + + private static void PrintPredictionDiagnostics(IReadOnlyCollection rows, RankSafePredictionFit fit) + { + int predictable = rows.Count(x => x.IsPredictable); + int sizePredictable = rows.Count(x => x.IsPredictable && x.IsSizePredictable); + int skipped = rows.Count - predictable; + int unsafeSize = predictable - sizePredictable; + + AnsiConsole.MarkupLine($"[grey]Rank-safe prediction rows:[/] [cyan]{predictable:N0}[/] KLD-predictable / [cyan]{sizePredictable:N0}[/] size-safe / [yellow]{skipped:N0}[/] KLD-incomplete / [yellow]{unsafeSize:N0}[/] unsafe-size"); + AnsiConsole.MarkupLine($"[grey]Interaction fit:[/] alpha=[cyan]{fit.Alpha:G6}[/] beta=[cyan]{fit.Beta:G6}[/] bit-stress=[cyan]{fit.BitStressThreshold:G4}[/] fit-rows=[cyan]{fit.FitRowCount:N0}[/] fallback=[cyan]{fit.UsedFallback}[/]"); + + if (predictable == 0) + return; + + var sizeSafeRows = rows.Where(x => x.IsPredictable && x.IsSizePredictable).ToList(); + ulong minSize = sizeSafeRows.Count == 0 ? 0UL : sizeSafeRows.Min(x => x.PredictedSizeBytes); + ulong maxSize = sizeSafeRows.Count == 0 ? 0UL : sizeSafeRows.Max(x => x.PredictedSizeBytes); + double minKld = rows.Where(x => x.IsPredictable).Min(x => x.PredictedKld); + double maxKld = rows.Where(x => x.IsPredictable).Max(x => x.PredictedKld); + + AnsiConsole.MarkupLine($"[grey]Predicted size spread, size-safe rows only:[/] [cyan]{ToGb(minSize):0.00}[/] GB .. [cyan]{ToGb(maxSize):0.00}[/] GB"); + AnsiConsole.MarkupLine($"[grey]Predicted KLD spread:[/] [cyan]{minKld:0.000000}[/] .. [cyan]{maxKld:0.000000}[/]"); + } + + private static double ToGb(ulong bytes) => bytes / 1024d / 1024d / 1024d; + + private sealed class PredictionContext + { + public PredictionContext( + IReadOnlyList activeGroups, + BenchmarkSnapshotRecord pureQ8, + BenchmarkSnapshotRecord q8BaseOnly, + Dictionary pureSnapshotsByBaselineId, + Dictionary baseOnlySnapshotsByBaselineId, + Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord> isolationByGroupAndBaseline, + IReadOnlyList notes) + { + ActiveGroups = activeGroups; + PureQ8 = pureQ8; + Q8BaseOnly = q8BaseOnly; + PureSnapshotsByBaselineId = pureSnapshotsByBaselineId; + BaseOnlySnapshotsByBaselineId = baseOnlySnapshotsByBaselineId; + IsolationByGroupAndBaseline = isolationByGroupAndBaseline; + Notes = notes; + } + + public IReadOnlyList ActiveGroups { get; } + public BenchmarkSnapshotRecord PureQ8 { get; } + public BenchmarkSnapshotRecord Q8BaseOnly { get; } + public Dictionary PureSnapshotsByBaselineId { get; } + public Dictionary BaseOnlySnapshotsByBaselineId { get; } + public Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord> IsolationByGroupAndBaseline { get; } + public IReadOnlyList Notes { get; } + } + + private sealed class FitObservation + { + public TensorConfig Config { get; init; } + public double ActualKld { get; init; } + public double AdditiveKld { get; init; } + } + + private struct PavaBlock + { + public double Sum; + public double Weight; + public int Count; + public double Mean => Weight <= 0d ? 0d : Sum / Weight; + } +} From 811404b258f001d49d1ef7f81b0ed42c990e2eaf Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 25 Apr 2026 12:50:06 -0400 Subject: [PATCH 125/258] Working selection system and output so far! Still a bit of bugs but this is solid! --- MQ.DB/Models/HybridQuant.cs | 2 +- MagicQuant/Commands/BuildHybrids.cs | 2 +- MagicQuant/Commands/Evolution.cs | 5 +- MagicQuant/Config.cs | 7 +- .../Configuration/MagicQuantYamlConfig.cs | 11 +- .../Configuration/MagicQuantYamlLoader.cs | 7 +- MagicQuant/Models/HybridFinalizationModels.cs | 2 +- .../CombinationSurvivalPipelineService.cs | 53 ++- .../Services/FinalArtifactNamingService.cs | 318 ++++++++++++++++++ .../Services/HybridArtifactExportService.cs | 210 +++++++----- .../Services/HybridBenchmarkRepository.cs | 4 +- .../PredictionGuidedHybridSelectionService.cs | 18 +- .../Services/ReadmeGenerationService.cs | 183 +++++++--- .../SelectionDiagnosticsLogService.cs | 170 ++++++++++ MagicQuant/config.default.yaml | 19 +- MagicQuant/config.dev.yaml | 19 +- 16 files changed, 858 insertions(+), 172 deletions(-) create mode 100644 MagicQuant/Services/FinalArtifactNamingService.cs create mode 100644 MagicQuant/Services/SelectionDiagnosticsLogService.cs diff --git a/MQ.DB/Models/HybridQuant.cs b/MQ.DB/Models/HybridQuant.cs index b9d4789..cf1ce63 100644 --- a/MQ.DB/Models/HybridQuant.cs +++ b/MQ.DB/Models/HybridQuant.cs @@ -3,7 +3,7 @@ namespace MQ.DB.Models; public enum HybridTensorOverrideMode { LearnedBaselineCandidate = 1, - ExactTensorScheme = 2 + ExactTensorScheme = 2, } public class HybridQuant diff --git a/MagicQuant/Commands/BuildHybrids.cs b/MagicQuant/Commands/BuildHybrids.cs index 37b9613..1118b94 100644 --- a/MagicQuant/Commands/BuildHybrids.cs +++ b/MagicQuant/Commands/BuildHybrids.cs @@ -21,6 +21,6 @@ private static void ShowHelp() { AnsiConsole.MarkupLine("[bold yellow]Command: build-hybrids[/]"); AnsiConsole.MarkupLine("Runs the centralized survival/export flow over the active MagicQuant evolution pipeline."); - AnsiConsole.MarkupLine("Usage: mq build-hybrids --model-dir \"\" [--output-dir \"\"] [--output-name-prefix \"model\"] [--export-external-learned-baselines]"); + AnsiConsole.MarkupLine("Usage: mq build-hybrids --model-dir \"\" [--output-dir \"\"] [--output-name-prefix \"Model\"] [--export-external-learned-baselines]"); } } diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index ac4d596..c51cc74 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -452,7 +452,8 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--selection-interior-window-fractions[/] Comma-separated phase-3 interior windows, e.g. 0.35,0.35 (Optional)"); AnsiConsole.MarkupLine(" [green]--prediction-bit-stress-threshold-candidates[/] Comma-separated interaction-fit thresholds, e.g. 4,5,6,7,8,9,10,11,12 (Optional)"); AnsiConsole.MarkupLine(" [green]--output-dir[/] Final export/output directory for selected survivor artifacts (Optional; default = /MagicQuant/Final_Outputs)"); - AnsiConsole.MarkupLine(" [green]--output-name-prefix[/] Output filename prefix for exported GGUF files (Optional; default = model)"); + AnsiConsole.MarkupLine(" [green]--allow-eight-bit-anchor-replacements[/] Permit final prediction to try replacing 8-bit anchors like Q8_0 (Optional; default false)"); + AnsiConsole.MarkupLine(" [green]--output-name-prefix[/] Output filename prefix for exported GGUF files (Optional; default = Model)"); AnsiConsole.MarkupLine(" [green]--export-external-learned-baselines[/] Also locally rebuild/export pure learned external baselines such as Unsloth (Optional; default false)"); AnsiConsole.MarkupLine(" [green]--selection-max-candidates-per-interior-window[/] Candidate count retained per interior window (Optional; default = 1)"); AnsiConsole.MarkupLine(" [green]--config[/] Path to YAML runtime config. CLI flags override YAML values."); @@ -500,4 +501,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} \ No newline at end of file +} diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 452f4fe..f95ba7d 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -64,9 +64,12 @@ public static void SetResolvedCustomBaselines(IEnumerable Math.Max(0d, Current.CandidateSelection.NearAnchorRequiredKldGainFractionOfPairGap); + public static bool SelectionAllowEightBitAnchorReplacements => + Current.CandidateSelection.AllowEightBitAnchorReplacements; + public static string? OutputDirectory => Current.Output.OutputDir; public static string OutputNamePrefix => string.IsNullOrWhiteSpace(Current.Output.OutputNamePrefix) - ? "model" + ? "Model" : Current.Output.OutputNamePrefix.Trim(); public static bool ExportExternalLearnedBaselines => Current.Output.ExportExternalLearnedBaselines; @@ -84,4 +87,4 @@ public static void SetResolvedCustomBaselines(IEnumerable BrainLayers => Current.BrainLayers; public static List CollapsePenaltySchemes => Current.CollapsePenaltySchemes; public static List MoeIndicatorTensors => Current.MoeIndicatorTensors; -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 9d05875..235869b 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -164,7 +164,7 @@ public sealed class RuntimeIdentityConfig public sealed class RuntimeOutputConfig { public string? OutputDir { get; set; } - public string OutputNamePrefix { get; set; } = "model"; + public string OutputNamePrefix { get; set; } = "Model"; public bool ExportExternalLearnedBaselines { get; set; } = false; } @@ -222,6 +222,13 @@ public sealed class RuntimeCandidateSelectionConfig /// the near-small-anchor brutal zone. /// public double NearAnchorRequiredKldGainFractionOfPairGap { get; set; } = 0.05d; + + /// + /// When false, the prediction selector does not spend build/benchmark attempts trying + /// to replace 8-bit anchors such as Q8_0 during strict dominance or near-anchor checks. + /// Q8 remains the highest-fidelity practical anchor unless this is explicitly enabled. + /// + public bool AllowEightBitAnchorReplacements { get; set; } = false; } public sealed class RuntimeBaselineConfig @@ -281,4 +288,4 @@ public sealed class ResolvedCustomBaselineSpec public bool AllowAsCombinationCarrier { get; set; } public bool AllowAsExplicitGroupCandidate { get; set; } public IReadOnlyList BannedGroupIds { get; set; } = Array.Empty(); -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 851b363..0a790ee 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -88,7 +88,7 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) : config.Output.OutputDir.Trim(); config.Output.OutputNamePrefix = string.IsNullOrWhiteSpace(config.Output.OutputNamePrefix) - ? "model" + ? "Model" : config.Output.OutputNamePrefix.Trim(); if (config.Survival.MaxSelectedChoicesPerBucket <= 0) @@ -236,6 +236,9 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (double.TryParse(Get("selection-near-anchor-required-kld-gain-fraction"), out var brutalGain) && brutalGain >= 0d) config.CandidateSelection.NearAnchorRequiredKldGainFractionOfPairGap = brutalGain; + if (Has("allow-eight-bit-anchor-replacements")) + config.CandidateSelection.AllowEightBitAnchorReplacements = true; + config.Output.OutputDir = Prefer(Get("output-dir"), config.Output.OutputDir); config.Output.OutputNamePrefix = Prefer(Get("output-name-prefix"), config.Output.OutputNamePrefix); if (Has("export-external-learned-baselines")) config.Output.ExportExternalLearnedBaselines = true; @@ -296,4 +299,4 @@ private static string ResolveMagicQuantRoot(string? configured) return Path.GetFullPath(value); } -} \ No newline at end of file +} diff --git a/MagicQuant/Models/HybridFinalizationModels.cs b/MagicQuant/Models/HybridFinalizationModels.cs index 747f676..c72f77e 100644 --- a/MagicQuant/Models/HybridFinalizationModels.cs +++ b/MagicQuant/Models/HybridFinalizationModels.cs @@ -169,7 +169,7 @@ public sealed class ExportedArtifactRecord public string? FullPath { get; init; } public string DownloadTarget { get; init; } = string.Empty; public ulong ExpectedSizeBytes { get; init; } - public ulong? ActualSizeBytes { get; init; } + public ulong? ActualSizeBytes { get; set; } public EffectiveStateResolutionResult? EffectiveState { get; init; } } diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index 9906c87..be6468e 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -18,6 +18,7 @@ public sealed class CombinationSurvivalPipelineService private readonly HybridArtifactExportService _exportService; private readonly ReadmeGenerationService _readmeService; private readonly HybridMapGenerationService _hybridMapService; + private readonly SelectionDiagnosticsLogService _diagnosticsLogService; public CombinationSurvivalPipelineService(QuantizationService quantizationService) { @@ -32,6 +33,7 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic _exportService = new HybridArtifactExportService(_quantizationService, _effectiveResolver); _readmeService = new ReadmeGenerationService(); _hybridMapService = new HybridMapGenerationService(); + _diagnosticsLogService = new SelectionDiagnosticsLogService(); } public async Task RunAsync(CancellationToken ct = default) @@ -74,10 +76,11 @@ public async Task RunAsync(CancellationToken AnsiConsole.MarkupLine($"[green]Final candidate/anchor survivors before manual enablement:[/] [cyan]{selection.Survivors.Count:N0}[/]"); AnsiConsole.MarkupLine($"[yellow]Recorded baseline/anchor eliminations:[/] [cyan]{selection.Eliminations.Count:N0}[/]"); AnsiConsole.MarkupLine($"[yellow]Prediction validation misses:[/] [cyan]{selection.ValidationFailures.Count:N0}[/]"); + RenderEliminationSummary(selection.Eliminations); var selectedRows = _selectionCli.Prompt(selection.Survivors); - var exportedArtifacts = await _exportService.ExportAsync(selectedRows, ct); + var exportedArtifacts = await _exportService.ExportAsync(selectedRows, pureBaselines, ct); string modelName = string.IsNullOrWhiteSpace(Cache.ModelDirectory) ? "model" @@ -91,13 +94,19 @@ public async Task RunAsync(CancellationToken .ThenBy(x => x.SizeBytes) .ToList(); + var nativeReference = await _benchmarkRepository.LoadBenchmarkSnapshotAsync( + (TensorConfig)HybridQuant.CreatePureBaseline(BaselineQuants.GetNativeQuant()), + ct); + + await _diagnosticsLogService.WriteAsync(benchmarkOverview, selection.ValidationFailures, ct); + await _readmeService.GenerateAsync( Cache.OutputDirectory!, modelName, exportedArtifacts, - benchmarkOverview, + pureBaselines, selection.Eliminations, - selection.ValidationFailures, + nativeReference, ct); await _hybridMapService.GenerateAsync(Cache.OutputDirectory!, exportedArtifacts, ct); @@ -114,4 +123,42 @@ await _readmeService.GenerateAsync( ValidationFailures = selection.ValidationFailures }; } + + private static void RenderEliminationSummary(IReadOnlyCollection eliminations) + { + if (eliminations.Count == 0) + return; + + AnsiConsole.Write(new Rule("[yellow]Baseline / Anchor Eliminations[/]") { Justification = Justify.Left }); + + var table = new Table().Border(TableBorder.Rounded); + table.AddColumn("Removed"); + table.AddColumn("Winner"); + table.AddColumn("KLD Δ"); + table.AddColumn("Size Δ (GB)"); + table.AddColumn("Reason"); + + foreach (var row in eliminations + .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") + .OrderBy(x => x.Eliminated.Kld) + .ThenBy(x => x.Eliminated.SizeBytes) + .Take(25)) + { + double kldDelta = row.Eliminated.Kld - row.Eliminator.Kld; + double sizeDeltaGb = (row.Eliminated.SizeBytes - (double)row.Eliminator.SizeBytes) / 1024d / 1024d / 1024d; + + table.AddRow( + Markup.Escape(row.Eliminated.DisplayName), + Markup.Escape(row.Eliminator.DisplayName), + kldDelta.ToString("0.000000"), + sizeDeltaGb.ToString("0.00"), + Markup.Escape(row.Reason)); + } + + AnsiConsole.Write(table); + + if (eliminations.Count > 25) + AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in README/logs.[/]"); + } + } diff --git a/MagicQuant/Services/FinalArtifactNamingService.cs b/MagicQuant/Services/FinalArtifactNamingService.cs new file mode 100644 index 0000000..2eeba4d --- /dev/null +++ b/MagicQuant/Services/FinalArtifactNamingService.cs @@ -0,0 +1,318 @@ +using System.Text.RegularExpressions; +using MagicQuant.Models; +using MQ.DB.Models; + +namespace MagicQuant.Services; + +/// +/// Centralizes the public naming rules used by exported GGUF files, README rows, +/// links, and diagnostic logs. Internal tensor-combo display names stay internal. +/// +public sealed class FinalArtifactNamingService +{ + private static readonly Regex UnsafeFileChars = new(@"[^A-Za-z0-9._-]+", RegexOptions.Compiled); + + public FinalArtifactNamingContext CreateContext( + IReadOnlyCollection pureBaselineSnapshots) + { + var ranges = BuildRanges(pureBaselineSnapshots); + return new FinalArtifactNamingContext(ranges); + } + + public FinalArtifactName BuildName( + BenchmarkSnapshotRecord snapshot, + FinalArtifactNamingContext context, + ISet? reservedFileNames = null) + { + string prefix = SanitizeToken(Config.OutputNamePrefix); + if (string.IsNullOrWhiteSpace(prefix)) + prefix = "Model"; + + string tag; + string providerToken; + string quantFamily; + + if (snapshot.IsHybrid) + { + providerToken = "MQ"; + quantFamily = ResolveHybridRangeFamily(snapshot, context); + int ordinal = context.NextHybridOrdinal(quantFamily); + tag = $"{providerToken}-{SanitizeToken(quantFamily)}_{ordinal}"; + } + else if (snapshot.Quant.BaseQuant.IsExternalRepositoryBaseline) + { + providerToken = ResolveExternalProviderToken(snapshot.Quant.BaseQuant); + quantFamily = NormalizeExternalDisplayName(snapshot.Quant.BaseQuant.Names[0], providerToken); + tag = SanitizeToken(quantFamily); + } + else + { + providerToken = "LM"; + quantFamily = snapshot.Quant.BaseQuant.Names[0]; + tag = $"{providerToken}-{SanitizeToken(quantFamily)}"; + } + + string stem = $"{prefix}-{tag}"; + string fileName = MakeUniqueFileName($"{stem}.gguf", reservedFileNames); + + return new FinalArtifactName + { + FileName = fileName, + DisplayName = Path.GetFileNameWithoutExtension(fileName), + ProviderToken = providerToken, + QuantFamilyOrBaseline = quantFamily + }; + } + + public string BuildDisplayLabel( + BenchmarkSnapshotRecord snapshot, + FinalArtifactNamingContext context) + { + string prefix = SanitizeToken(Config.OutputNamePrefix); + if (string.IsNullOrWhiteSpace(prefix)) + prefix = "Model"; + + if (snapshot.IsHybrid) + return $"{prefix}-MQ-{SanitizeToken(ResolveHybridRangeFamily(snapshot, context))}"; + + if (snapshot.Quant.BaseQuant.IsExternalRepositoryBaseline) + { + string providerToken = ResolveExternalProviderToken(snapshot.Quant.BaseQuant); + return $"{prefix}-{SanitizeToken(NormalizeExternalDisplayName(snapshot.Quant.BaseQuant.Names[0], providerToken))}"; + } + + return $"{prefix}-LM-{SanitizeToken(snapshot.Quant.BaseQuant.Names[0])}"; + } + + public IReadOnlyList BuildProviderCredits( + IReadOnlyCollection artifacts) + { + var result = new Dictionary(StringComparer.OrdinalIgnoreCase); + + result["llama.cpp"] = new ProviderCredit + { + Name = "llama.cpp", + Url = "https://github.com/ggml-org/llama.cpp", + Note = "Baseline quantization formats and llama.cpp tooling." + }; + + foreach (var artifact in artifacts) + { + foreach (var baseline in EnumerateBaselinesUsedBy(artifact.Snapshot.Quant)) + { + if (!baseline.IsExternalRepositoryBaseline) + continue; + + string providerName = string.IsNullOrWhiteSpace(baseline.ShortSourceName) + ? "External provider" + : baseline.ShortSourceName!; + + string providerToken = ResolveExternalProviderToken(baseline); + if (string.Equals(providerName, "Unsloth", StringComparison.OrdinalIgnoreCase)) + providerName = "Unsloth"; + + string? url = HybridBenchmarkRepository.BuildExternalRepositoryUrl(baseline); + if (string.IsNullOrWhiteSpace(url) && !string.IsNullOrWhiteSpace(baseline.SourceRepository)) + url = $"https://huggingface.co/{baseline.SourceRepository}"; + + string key = !string.IsNullOrWhiteSpace(url) ? url : providerName; + result[key] = new ProviderCredit + { + Name = providerName, + Url = url ?? string.Empty, + Note = $"External learned baseline source ({providerToken})." + }; + } + } + + return result.Values + .OrderBy(x => x.Name, StringComparer.OrdinalIgnoreCase) + .ThenBy(x => x.Url, StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + private static IEnumerable EnumerateBaselinesUsedBy(HybridQuant quant) + { + yield return quant.BaseQuant; + + foreach (var tensor in quant.Tensors) + { + if (tensor.OverrideMode == HybridTensorOverrideMode.LearnedBaselineCandidate && tensor.CandidateBaseline != null) + yield return tensor.CandidateBaseline; + } + } + + private static IReadOnlyList BuildRanges(IReadOnlyCollection pureBaselineSnapshots) + { + var anchors = pureBaselineSnapshots + .Where(x => !x.IsHybrid) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ToList(); + + var ranges = new List(); + + for (int i = 0; i < anchors.Count - 1; i++) + { + var lowerDamageLarger = anchors[i]; + var higherDamageSmaller = anchors[i + 1]; + + if (higherDamageSmaller.SizeBytes >= lowerDamageLarger.SizeBytes) + continue; + + ranges.Add(new FinalArtifactRange + { + MinSizeBytes = higherDamageSmaller.SizeBytes, + MaxSizeBytes = lowerDamageLarger.SizeBytes, + RangeFamily = NormalizeRangeFamily(higherDamageSmaller.Quant.BaseQuant), + SmallerHigherDamageAnchor = higherDamageSmaller, + LargerLowerDamageAnchor = lowerDamageLarger + }); + } + + return ranges + .OrderBy(x => x.MinSizeBytes) + .ThenBy(x => x.MaxSizeBytes) + .ToList(); + } + + private static string ResolveHybridRangeFamily( + BenchmarkSnapshotRecord snapshot, + FinalArtifactNamingContext context) + { + var match = context.Ranges + .Where(x => snapshot.SizeBytes >= x.MinSizeBytes && snapshot.SizeBytes <= x.MaxSizeBytes) + .OrderBy(x => x.MaxSizeBytes - x.MinSizeBytes) + .FirstOrDefault(); + + if (match != null) + return match.RangeFamily; + + var nearestSmallerAnchor = context.Ranges + .Select(x => x.SmallerHigherDamageAnchor) + .OrderBy(x => Distance(snapshot.SizeBytes, x.SizeBytes)) + .FirstOrDefault(); + + if (nearestSmallerAnchor != null) + return NormalizeRangeFamily(nearestSmallerAnchor.Quant.BaseQuant); + + return NormalizeRangeFamily(snapshot.Quant.BaseQuant); + } + + private static string NormalizeRangeFamily(BaselineQuants baseline) + { + if (!string.IsNullOrWhiteSpace(baseline.QuantizeBaseArgumentName)) + return baseline.QuantizeBaseArgumentName; + + return !baseline.Names.IsDefaultOrEmpty ? baseline.Names[0] : "Unknown"; + } + + private static string ResolveExternalProviderToken(BaselineQuants baseline) + { + string joined = $"{baseline.ShortSourceName} {baseline.SourceOwner} {baseline.SourceRepository} {baseline.Names[0]}"; + if (joined.Contains("unsloth", StringComparison.OrdinalIgnoreCase)) + return "UD"; + + if (!string.IsNullOrWhiteSpace(baseline.ShortSourceName)) + return SanitizeToken(baseline.ShortSourceName!); + + return "EXT"; + } + + private static string NormalizeExternalDisplayName(string displayName, string providerToken) + { + if (string.IsNullOrWhiteSpace(displayName)) + return providerToken; + + var value = displayName.Trim(); + + if (value.StartsWith("Unsloth_", StringComparison.OrdinalIgnoreCase)) + value = providerToken + value["Unsloth".Length..]; + + if (!value.StartsWith(providerToken + "_", StringComparison.OrdinalIgnoreCase) && + !value.StartsWith(providerToken + "-", StringComparison.OrdinalIgnoreCase)) + { + value = $"{providerToken}_{value}"; + } + + return value; + } + + private static string MakeUniqueFileName(string desiredFileName, ISet? reservedFileNames) + { + if (reservedFileNames == null) + return desiredFileName; + + string candidate = desiredFileName; + string stem = Path.GetFileNameWithoutExtension(desiredFileName); + string ext = Path.GetExtension(desiredFileName); + int i = 1; + + while (!reservedFileNames.Add(candidate)) + { + i++; + candidate = $"{stem}_{i}{ext}"; + } + + return candidate; + } + + public static string SanitizeToken(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return string.Empty; + + value = value.Trim().Replace(' ', '_'); + value = UnsafeFileChars.Replace(value, "_"); + while (value.Contains("__", StringComparison.Ordinal)) + value = value.Replace("__", "_", StringComparison.Ordinal); + return value.Trim('_', '-'); + } + + private static ulong Distance(ulong left, ulong right) => left >= right ? left - right : right - left; +} + +public sealed class FinalArtifactNamingContext +{ + private readonly Dictionary _hybridOrdinalByFamily = new(StringComparer.OrdinalIgnoreCase); + + public FinalArtifactNamingContext(IReadOnlyList ranges) + { + Ranges = ranges; + } + + public IReadOnlyList Ranges { get; } + + public int NextHybridOrdinal(string rangeFamily) + { + string key = string.IsNullOrWhiteSpace(rangeFamily) ? "Unknown" : rangeFamily; + _hybridOrdinalByFamily.TryGetValue(key, out int current); + current++; + _hybridOrdinalByFamily[key] = current; + return current; + } +} + +public sealed class FinalArtifactRange +{ + public ulong MinSizeBytes { get; init; } + public ulong MaxSizeBytes { get; init; } + public string RangeFamily { get; init; } = string.Empty; + public BenchmarkSnapshotRecord SmallerHigherDamageAnchor { get; init; } = default!; + public BenchmarkSnapshotRecord LargerLowerDamageAnchor { get; init; } = default!; +} + +public sealed class FinalArtifactName +{ + public string FileName { get; init; } = string.Empty; + public string DisplayName { get; init; } = string.Empty; + public string ProviderToken { get; init; } = string.Empty; + public string QuantFamilyOrBaseline { get; init; } = string.Empty; +} + +public sealed class ProviderCredit +{ + public string Name { get; init; } = string.Empty; + public string Url { get; init; } = string.Empty; + public string Note { get; init; } = string.Empty; +} diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index eccbf41..e460530 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -1,6 +1,7 @@ -using System.Text.Json; +using MagicQuant.Helpers; using MagicQuant.Models; using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Services; @@ -23,6 +24,7 @@ public sealed class HybridArtifactExportService private readonly QuantizationService _quantizationService; private readonly EffectiveCandidateStateResolverService _effectiveResolver; + private readonly FinalArtifactNamingService _namingService; public HybridArtifactExportService( QuantizationService quantizationService, @@ -30,37 +32,50 @@ public HybridArtifactExportService( { _quantizationService = quantizationService; _effectiveResolver = effectiveResolver; + _namingService = new FinalArtifactNamingService(); } public async Task> ExportAsync( IReadOnlyCollection selectedRows, + IReadOnlyCollection pureBaselineSnapshots, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(Cache.OutputDirectory)) throw new InvalidOperationException("Cache.OutputDirectory is not set."); Directory.CreateDirectory(Cache.OutputDirectory); + await CleanOutputDirectoryAsync(Cache.OutputDirectory!, ct); var output = new List(); - var hybridOrdinalByFamily = new Dictionary(StringComparer.Ordinal); + var reservedFileNames = new HashSet(StringComparer.OrdinalIgnoreCase); + var namingContext = _namingService.CreateContext(pureBaselineSnapshots); - foreach (var row in selectedRows.Where(x => x.Enabled).OrderBy(x => x.Snapshot.Kld).ThenBy(x => x.Snapshot.SizeBytes)) + var enabledRows = selectedRows + .Where(x => x.Enabled) + .OrderBy(x => x.Snapshot.Kld) + .ThenBy(x => x.Snapshot.SizeBytes) + .ToList(); + + var localBuilds = new List<(ExportedArtifactRecord Record, HybridQuant Quant, string FullPath, ulong ExpectedBytes)>(); + + foreach (var row in enabledRows) { var snap = row.Snapshot; bool isHybrid = snap.IsHybrid; bool exportLocally = isHybrid || !snap.IsExternalPureBaseline || Config.ExportExternalLearnedBaselines; - string provider = HybridBenchmarkRepository.ResolveProviderName(snap.Quant, exportNaming: exportLocally && isHybrid); + var name = _namingService.BuildName(snap, namingContext, reservedFileNames); + string provider = ResolveReadmeProviderName(snap, isHybrid); if (!exportLocally) { - AnsiConsole.MarkupLine($"[grey]Skipping local export for external learned baseline by default:[/] {Markup.Escape(snap.DisplayName)} [grey](enable with --export-external-learned-baselines or output.export_external_learned_baselines: true)[/]"); + AnsiConsole.MarkupLine($"[grey]Skipping local export for external learned baseline by default:[/] {Markup.Escape(name.DisplayName)} [grey](enable with --export-external-learned-baselines or output.export_external_learned_baselines: true)[/]"); output.Add(new ExportedArtifactRecord { Snapshot = snap, - DisplayName = snap.DisplayName, + DisplayName = name.DisplayName, ProviderName = provider, - BaselineFamily = snap.BaselineFamily, + BaselineFamily = name.QuantFamilyOrBaseline, IsExternalReference = true, DownloadTarget = snap.ExternalRepositoryUrl ?? string.Empty, ExpectedSizeBytes = snap.SizeBytes, @@ -71,76 +86,122 @@ public async Task> ExportAsync( } if (snap.IsExternalPureBaseline && !snap.IsHybrid) - AnsiConsole.MarkupLine($"[yellow]Local export enabled for external learned baseline:[/] {Markup.Escape(snap.DisplayName)}"); - - string fileName = BuildFileName(snap, provider, hybridOrdinalByFamily); - string fullPath = Path.Combine(Cache.OutputDirectory!, fileName); - ulong expectedBytes = snap.SizeBytes; - - bool shouldBuild = true; - if (File.Exists(fullPath)) - { - ulong actual = (ulong)new FileInfo(fullPath).Length; - if (actual == expectedBytes) - { - shouldBuild = false; - AnsiConsole.MarkupLine($"[grey]Reusing existing exported artifact:[/] {Markup.Escape(fullPath)}"); - } - else - { - AnsiConsole.MarkupLine($"[yellow]Existing export byte size mismatch, rebuilding:[/] {Markup.Escape(fullPath)}"); - File.Delete(fullPath); - } - } + AnsiConsole.MarkupLine($"[yellow]Local export enabled for external learned baseline:[/] {Markup.Escape(name.DisplayName)}"); - if (shouldBuild) - await _quantizationService.BuildExportArtifactAsync(snap.Quant, fullPath, forceRebuild: false, ct: ct); - - ulong actualBytes = File.Exists(fullPath) ? (ulong)new FileInfo(fullPath).Length : 0UL; - if (actualBytes != expectedBytes) - { - AnsiConsole.MarkupLine($"[yellow]Export byte validation warning:[/] expected [cyan]{expectedBytes:N0}[/] but got [cyan]{actualBytes:N0}[/] for {Markup.Escape(fileName)}"); - } - - output.Add(new ExportedArtifactRecord + string fullPath = Path.Combine(Cache.OutputDirectory!, name.FileName); + var record = new ExportedArtifactRecord { Snapshot = snap, - DisplayName = snap.DisplayName, + DisplayName = name.DisplayName, ProviderName = provider, - BaselineFamily = snap.BaselineFamily, + BaselineFamily = name.QuantFamilyOrBaseline, IsExternalReference = false, - FileName = fileName, + FileName = name.FileName, FullPath = fullPath, - DownloadTarget = $"./../../resolve/main/{fileName}?download=true", - ExpectedSizeBytes = expectedBytes, - ActualSizeBytes = actualBytes, + DownloadTarget = $"./../../resolve/main/{name.FileName}?download=true", + ExpectedSizeBytes = snap.SizeBytes, EffectiveState = await _effectiveResolver.ResolveAsync(snap.Config, ct) - }); + }; + + output.Add(record); + localBuilds.Add((record, snap.Quant, fullPath, snap.SizeBytes)); } + // Kick off all exports together. QuantizationService owns the real concurrency gates, + // so this trusts that service to self-regulate CPU/GPU/process pressure. + var buildTasks = localBuilds.Select(async item => + { + await _quantizationService.BuildExportArtifactAsync(item.Quant, item.FullPath, forceRebuild: true, ct: ct); + + ulong actualBytes = File.Exists(item.FullPath) ? (ulong)new FileInfo(item.FullPath).Length : 0UL; + item.Record.ActualSizeBytes = actualBytes; + + if (actualBytes != item.ExpectedBytes) + { + AnsiConsole.MarkupLine($"[yellow]Export byte validation warning:[/] expected [cyan]{item.ExpectedBytes:N0}[/] but got [cyan]{actualBytes:N0}[/] for {Markup.Escape(Path.GetFileName(item.FullPath))}"); + } + }); + + await Task.WhenAll(buildTasks); + await CopyModelAdjacentFilesAsync(Cache.OutputDirectory!, ct); await CopyImatrixArtifactsAsync(Cache.OutputDirectory!, ct); await CopyMmprojArtifactsAsync(Cache.OutputDirectory!, ct); + await CleanExportSidecarsAsync(Cache.OutputDirectory!, ct); return output; } - private static string BuildFileName( - BenchmarkSnapshotRecord snapshot, - string provider, - Dictionary hybridOrdinalByFamily) + private static string ResolveReadmeProviderName(BenchmarkSnapshotRecord snapshot, bool isHybrid) { - string prefix = Sanitize(Config.OutputNamePrefix); + if (isHybrid) + return "MagicQuant"; - if (!snapshot.IsHybrid) - return $"{prefix}-{Sanitize(provider)}-{Sanitize(snapshot.BaselineFamily)}.gguf"; + return HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); + } - hybridOrdinalByFamily.TryGetValue(snapshot.BaselineFamily, out var current); - current++; - hybridOrdinalByFamily[snapshot.BaselineFamily] = current; + private static async Task CleanOutputDirectoryAsync(string outputDirectory, CancellationToken ct) + { + if (!Directory.Exists(outputDirectory)) + { + Directory.CreateDirectory(outputDirectory); + return; + } + + foreach (var file in Directory.EnumerateFiles(outputDirectory, "*", SearchOption.TopDirectoryOnly)) + { + ct.ThrowIfCancellationRequested(); + await HardDeleteHelper.DeleteFileIfExistsAsync(file); + } + + foreach (var directory in Directory.EnumerateDirectories(outputDirectory, "*", SearchOption.TopDirectoryOnly)) + { + ct.ThrowIfCancellationRequested(); + await HardDeleteDirectoryAsync(directory); + } - string special = $"H{current}"; - return $"{prefix}-{Sanitize(provider)}-{special}-{Sanitize(snapshot.BaselineFamily)}.gguf"; + AnsiConsole.MarkupLine($"[grey]Cleaned final export directory:[/] {Markup.Escape(outputDirectory)}"); + } + + private static async Task HardDeleteDirectoryAsync(string directory) + { + if (!Directory.Exists(directory)) + return; + + foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)) + await HardDeleteHelper.DeleteFileIfExistsAsync(file); + + foreach (var sub in Directory.EnumerateDirectories(directory, "*", SearchOption.AllDirectories) + .OrderByDescending(x => x.Length)) + { + if (Directory.Exists(sub)) + Directory.Delete(sub, recursive: false); + } + + if (Directory.Exists(directory)) + Directory.Delete(directory, recursive: false); + } + + private static async Task CleanExportSidecarsAsync(string outputDirectory, CancellationToken ct) + { + string[] patterns = + [ + "*.success.json", + "*.quantize.log", + "*.convert.log", + "imatrix.success.json", + "imatrix.metadata.json", + "imatrix.build.log" + ]; + + foreach (var pattern in patterns) + { + foreach (var file in Directory.EnumerateFiles(outputDirectory, pattern, SearchOption.TopDirectoryOnly)) + { + ct.ThrowIfCancellationRequested(); + await HardDeleteHelper.DeleteFileIfExistsAsync(file); + } + } } private static async Task CopyModelAdjacentFilesAsync(string outputDirectory, CancellationToken ct) @@ -165,30 +226,19 @@ private static async Task CopyModelAdjacentFilesAsync(string outputDirectory, Ca } } - private static async Task CopyImatrixArtifactsAsync(string outputDirectory, CancellationToken ct) + private static Task CopyImatrixArtifactsAsync(string outputDirectory, CancellationToken ct) { if (!Cache.IsImatrixAvailable || string.IsNullOrWhiteSpace(Cache.ActiveImatrixPath)) - return; + return Task.CompletedTask; string source = Cache.ActiveImatrixPath!; string target = Path.Combine(outputDirectory, "imatrix.dat"); File.Copy(source, target, overwrite: true); AnsiConsole.MarkupLine($"[green]Copied imatrix artifact:[/] {Markup.Escape(target)}"); - - string imatrixDir = Path.GetDirectoryName(source)!; - foreach (var optional in new[] { "imatrix.success.json", "imatrix.metadata.json", "imatrix.build.log" }) - { - string optionalSource = Path.Combine(imatrixDir, optional); - if (!File.Exists(optionalSource)) - continue; - - File.Copy(optionalSource, Path.Combine(outputDirectory, optional), overwrite: true); - await Task.Yield(); - AnsiConsole.MarkupLine($"[green]Copied imatrix sidecar:[/] {Markup.Escape(optional)}"); - } + return Task.CompletedTask; } - private static async Task CopyMmprojArtifactsAsync(string outputDirectory, CancellationToken ct) + private static Task CopyMmprojArtifactsAsync(string outputDirectory, CancellationToken ct) { var searchRoots = new List(); if (!string.IsNullOrWhiteSpace(Cache.ModelDirectory)) @@ -205,13 +255,13 @@ private static async Task CopyMmprojArtifactsAsync(string outputDirectory, Cance string target = Path.Combine(outputDirectory, Path.GetFileName(mmproj)); File.Copy(mmproj, target, overwrite: true); AnsiConsole.MarkupLine($"[green]Copied mmproj artifact:[/] {Markup.Escape(target)}"); - return; + return Task.CompletedTask; } if (!LooksVisionCapableModel()) { AnsiConsole.MarkupLine("[grey]No mmproj artifact was present, but no vision capability hints were detected. Continuing.[/]"); - return; + return Task.CompletedTask; } throw new InvalidOperationException( @@ -233,16 +283,4 @@ private static bool LooksVisionCapableModel() json.Contains("mm_vision_tower", StringComparison.OrdinalIgnoreCase) || json.Contains("projector", StringComparison.OrdinalIgnoreCase); } - - private static string Sanitize(string value) - { - if (string.IsNullOrWhiteSpace(value)) - return "model"; - - var cleaned = value.Trim(); - foreach (char c in Path.GetInvalidFileNameChars()) - cleaned = cleaned.Replace(c, '-'); - - return cleaned.Replace(" ", "-"); - } } diff --git a/MagicQuant/Services/HybridBenchmarkRepository.cs b/MagicQuant/Services/HybridBenchmarkRepository.cs index 627302b..6f2738e 100644 --- a/MagicQuant/Services/HybridBenchmarkRepository.cs +++ b/MagicQuant/Services/HybridBenchmarkRepository.cs @@ -314,7 +314,7 @@ public async Task> LoadAllBenchmarkSnapshotsForCur public static string ResolveProviderName(HybridQuant quant, bool exportNaming) { if (exportNaming && quant.Tensors.Count > 0) - return "MagicHybrid"; + return "MQ"; var baseline = quant.BaseQuant; if (baseline.IsExternalRepositoryBaseline) @@ -377,4 +377,4 @@ public static string BuildDisplayName(HybridQuant quant) .Select(x => (int?)x.Id) .FirstOrDefaultAsync(ct); } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index a33d9f0..8f28875 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -95,6 +95,12 @@ private async Task RunStrictDominanceReplacementAsync( foreach (var anchor in currentAnchors.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes)) { + if (ShouldSkipAnchorReplacement(anchor)) + { + AnsiConsole.MarkupLine($"[grey]Skipping 8-bit anchor replacement attempts:[/] {Markup.Escape(anchor.DisplayName)}"); + continue; + } + var candidates = hybridPredictions .Where(x => x.PredictedSizeBytes <= anchor.SizeBytes) .Where(x => x.PredictedKld + Config.SelectionMinimumKldImprovementEpsilon < anchor.Kld) @@ -171,6 +177,9 @@ private async Task RunNearBaselineReplacementAsync( var lowerSizeHigherDamage = pair.HigherDamageSmaller; var upperSizeLowerDamage = pair.LowerDamageLarger; + if (ShouldSkipAnchorReplacement(lowerSizeHigherDamage)) + continue; + ulong min = lowerSizeHigherDamage.SizeBytes; ulong max = AddPercent(min, Config.SelectionNearBaselineMaxSizeGrowthPercent); @@ -334,7 +343,7 @@ private async Task BuildAndValidateSingleAsync( CancellationToken ct) { AnsiConsole.MarkupLine( - $"[grey]Validating candidate:[/] {Markup.Escape(candidate.Prediction.Quant.BaseQuant.Names[0])} " + + $"[grey]Validating candidate:[/] {Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(candidate.Prediction.Quant))} " + $"[grey]| reason=[/] {candidate.Reason} [grey]| window=[/] {Markup.Escape(candidate.WindowLabel)}"); var summary = await _quantizationService.ProcessHybridBatchAsync(new[] { candidate.Prediction.Quant }, ct); @@ -556,6 +565,13 @@ private List MergeAndDominanceFilter( return result.Survivors.ToList(); } + private static bool ShouldSkipAnchorReplacement(BenchmarkSnapshotRecord anchor) + { + return !Config.SelectionAllowEightBitAnchorReplacements && + anchor.Quant.BaseQuant.BitRange >= 8 && + !anchor.Quant.BaseQuant.IsHighPrecisionExactAlias; + } + private static List BuildAdjacentPairs(IReadOnlyList anchors) { var ordered = anchors diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 7007a51..4c90984 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -6,58 +6,52 @@ namespace MagicQuant.Services; public sealed class ReadmeGenerationService { + private readonly FinalArtifactNamingService _namingService = new(); + public async Task GenerateAsync( string outputDirectory, string modelName, IReadOnlyCollection exportedArtifacts, - IReadOnlyCollection benchmarkOverview, + IReadOnlyCollection pureBaselineSnapshots, IReadOnlyCollection? eliminatedBaselines = null, - IReadOnlyCollection? validationFailures = null, + BenchmarkSnapshotRecord? pplReference = null, CancellationToken ct = default) { Directory.CreateDirectory(outputDirectory); string readmePath = Path.Combine(outputDirectory, "README.md"); + var namingContext = _namingService.CreateContext(pureBaselineSnapshots); + double? referencePpl = ResolveReferencePpl(pplReference, pureBaselineSnapshots, exportedArtifacts); + var sb = new StringBuilder(); - sb.AppendLine($"# MagicQuant Hybrids (v2.0) - {modelName}"); + sb.AppendLine($"# MagicQuant Hybrids (v2.1) - {modelName}"); sb.AppendLine(); sb.AppendLine("MagicQuant is **not** a quantization technique by itself."); sb.AppendLine(); sb.AppendLine("It is a search, judging, and hybrid-discovery system that learns from baseline families such as llama.cpp and external/custom baseline sources, then uses isolated empirical truth, rank-safe prediction, and real benchmarking to keep the practical survivors."); sb.AppendLine(); - sb.AppendLine("Sometimes a hybrid beats a pure baseline. Sometimes it does not. That is normal. The point is to pay the real benchmarking cost only where the trade looks genuinely worth it."); + sb.AppendLine("Sometimes a hybrid beats a pure baseline. Sometimes it does not. The point is to pay the real benchmarking cost only where the trade is genuinely worth keeping."); sb.AppendLine(); sb.AppendLine("## Final surviving downloadable outputs"); sb.AppendLine(); - AppendDownloadTable(sb, exportedArtifacts); + AppendDownloadTable(sb, exportedArtifacts, referencePpl); + sb.AppendLine(); + sb.AppendLine("> **PPL Δ % note:** negative is better. Larger positive values are worse. The percentage is measured against the native/reference PPL when available; otherwise it falls back to the best available reference in this release set."); sb.AppendLine(); if (eliminatedBaselines is { Count: > 0 }) { sb.AppendLine("## Baselines / anchors removed from final download table"); sb.AppendLine(); - sb.AppendLine("These rows are intentionally **not** part of the primary download table. They explain which pure baselines or previously-surviving anchors were beaten by another validated artifact."); - sb.AppendLine(); - AppendEliminationTable(sb, eliminatedBaselines); - sb.AppendLine(); - } - - if (validationFailures is { Count: > 0 }) - { - sb.AppendLine("## Predicted candidates that did not validate"); + sb.AppendLine("These rows are intentionally **not** part of the primary download table. They explain which pure baselines or previously-surviving anchors were beaten, collapsed, or made redundant by a validated artifact."); sb.AppendLine(); - sb.AppendLine("The prediction engine is used for choosing what is worth building, but final survival still requires real benchmark validation. These candidates were predicted as interesting, built or checked, and then rejected because the real relationship did not hold."); + AppendEliminationLegend(sb); sb.AppendLine(); - AppendValidationFailureTable(sb, validationFailures); + AppendEliminationTable(sb, eliminatedBaselines, exportedArtifacts, namingContext); sb.AppendLine(); } - sb.AppendLine("## Benchmark overview"); - sb.AppendLine(); - AppendBenchmarkOverviewTable(sb, benchmarkOverview); - sb.AppendLine(); - sb.AppendLine("## Method note"); sb.AppendLine(); sb.AppendLine("The final chooser uses rank-safe isolation prediction: Q8-carrier single-group isolation measurements provide the additive backbone, a low-bit interaction correction improves numeric KLD closeness, and an isotonic projection keeps the final predicted ordering monotone with the isolation backbone. Predicted candidates still have to validate against real benchmark truth before they can replace a baseline or remain as an interior hybrid."); @@ -69,6 +63,8 @@ public async Task GenerateAsync( sb.AppendLine("- If you spot a mistake, edge case, or a better practical trade, open an issue or share the artifact details so the comparison can be improved."); sb.AppendLine(); + AppendProviderCredits(sb, exportedArtifacts); + sb.AppendLine("## Warning"); sb.AppendLine(); sb.AppendLine("External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing."); @@ -85,70 +81,147 @@ public async Task GenerateAsync( return readmePath; } - private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection artifacts) + private static void AppendDownloadTable( + StringBuilder sb, + IReadOnlyCollection artifacts, + double? referencePpl) { - sb.AppendLine("| Name | Provider | Quant Family / Baseline | KLD | PPL | Size (GB) | Download |"); + sb.AppendLine("| Name | Provider | Quant Family / Baseline | KLD | PPL Δ % | Size (GB) | Download |"); sb.AppendLine("|---|---|---|---:|---:|---:|---|"); foreach (var artifact in artifacts.OrderBy(x => x.Snapshot.Kld).ThenBy(x => x.Snapshot.SizeBytes)) { - string sizeGb = (artifact.Snapshot.SizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); + string sizeGb = ToGb(artifact.Snapshot.SizeBytes); string download = artifact.IsExternalReference ? $"[Link]({artifact.DownloadTarget})" : $"[Link](./../../resolve/main/{artifact.FileName}?download=true)"; - sb.AppendLine($"| {EscapePipe(artifact.DisplayName)} | {EscapePipe(artifact.ProviderName)} | {EscapePipe(artifact.BaselineFamily)} | {artifact.Snapshot.Kld:0.000000} | {artifact.Snapshot.Ppl:0.0000} | {sizeGb} | {download} |"); + sb.AppendLine( + $"| {EscapePipe(artifact.DisplayName)} | {EscapePipe(artifact.ProviderName)} | {EscapePipe(artifact.BaselineFamily)} | " + + $"{artifact.Snapshot.Kld:0.000000} | {FormatPplDeltaPercent(artifact.Snapshot.Ppl, referencePpl)} | {sizeGb} | {download} |"); } } - private static void AppendEliminationTable(StringBuilder sb, IReadOnlyCollection eliminations) + private static void AppendEliminationLegend(StringBuilder sb) + { + sb.AppendLine("**Reason legend:** 🏆 strict dominance, 📈 near-baseline premium, 🧩 useful interior discovery, 📏 spacing collapse, 🔪 final dominance."); + } + + private void AppendEliminationTable( + StringBuilder sb, + IReadOnlyCollection eliminations, + IReadOnlyCollection artifacts, + FinalArtifactNamingContext namingContext) { - sb.AppendLine("| Removed | Removed KLD | Removed Size (GB) | Winner | Winner KLD | Winner Size (GB) | Reason |"); - sb.AppendLine("|---|---:|---:|---|---:|---:|---|"); + var exportedByKey = artifacts + .GroupBy(x => TensorConfigIdentity.ToKey(x.Snapshot.Config), StringComparer.Ordinal) + .ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal); + + sb.AppendLine("| Removed | Winner | KLD Δ | Size Δ (GB) | Why |"); + sb.AppendLine("|---|---|---:|---:|---|"); foreach (var row in eliminations .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") .OrderBy(x => x.Eliminated.Kld) .ThenBy(x => x.Eliminated.SizeBytes)) { + string removed = GetPublicName(row.Eliminated, exportedByKey, namingContext); + string winner = GetPublicName(row.Eliminator, exportedByKey, namingContext); + + double kldDelta = row.Eliminated.Kld - row.Eliminator.Kld; + double sizeDeltaGb = (row.Eliminated.SizeBytes - (double)row.Eliminator.SizeBytes) / 1024d / 1024d / 1024d; + sb.AppendLine( - $"| {EscapePipe(row.Eliminated.DisplayName)} | {row.Eliminated.Kld:0.000000} | {ToGb(row.Eliminated.SizeBytes)} | " + - $"{EscapePipe(row.Eliminator.DisplayName)} | {row.Eliminator.Kld:0.000000} | {ToGb(row.Eliminator.SizeBytes)} | {EscapePipe(row.Reason)} |"); + $"| {EscapePipe(removed)} | {EscapePipe(winner)} | {kldDelta:0.000000} | {sizeDeltaGb:0.00} | {ReasonEmoji(row.Reason)} |"); } } - private static void AppendValidationFailureTable(StringBuilder sb, IReadOnlyCollection failures) + private static string GetPublicName( + BenchmarkSnapshotRecord snapshot, + IReadOnlyDictionary exportedByKey, + FinalArtifactNamingContext namingContext) { - sb.AppendLine("| Candidate | Reason | Predicted KLD | Predicted Size (GB) | Actual KLD | Actual Size (GB) | Message |"); - sb.AppendLine("|---|---|---:|---:|---:|---:|---|"); - - foreach (var failure in failures - .Where(x => !x.Accepted) - .OrderBy(x => x.Candidate.Reason) - .ThenBy(x => x.Candidate.Prediction.PredictedKld) - .Take(100)) - { - var actualKld = failure.Snapshot == null ? "n/a" : failure.Snapshot.Kld.ToString("0.000000"); - var actualSize = failure.Snapshot == null ? "n/a" : ToGb(failure.Snapshot.SizeBytes); - sb.AppendLine( - $"| {EscapePipe(failure.Candidate.Prediction.Quant.BaseQuant.Names[0])} | {failure.Candidate.Reason} | " + - $"{failure.Candidate.Prediction.PredictedKld:0.000000} | {ToGb(failure.Candidate.Prediction.PredictedSizeBytes)} | " + - $"{actualKld} | {actualSize} | {EscapePipe(failure.Message)} |"); - } + string key = TensorConfigIdentity.ToKey(snapshot.Config); + if (exportedByKey.TryGetValue(key, out var artifact)) + return artifact.DisplayName; + + return new FinalArtifactNamingService().BuildDisplayLabel(snapshot, namingContext); } - private static void AppendBenchmarkOverviewTable(StringBuilder sb, IReadOnlyCollection snapshots) + private void AppendProviderCredits(StringBuilder sb, IReadOnlyCollection artifacts) { - sb.AppendLine("| Name | Provider | Quant Family | KLD | PPL | Size (GB) |"); - sb.AppendLine("|---|---|---|---:|---:|---:|"); + var credits = _namingService.BuildProviderCredits(artifacts); + if (credits.Count == 0) + return; + + sb.AppendLine(); + sb.AppendLine("### Provider credits"); + sb.AppendLine(); - foreach (var snap in snapshots - .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) - .OrderBy(x => x.Kld) - .ThenBy(x => x.SizeBytes)) + foreach (var credit in credits) { - sb.AppendLine($"| {EscapePipe(snap.DisplayName)} | {EscapePipe(snap.ProviderName)} | {EscapePipe(snap.BaselineFamily)} | {snap.Kld:0.000000} | {snap.Ppl:0.0000} | {ToGb(snap.SizeBytes)} |"); + string name = EscapePipe(credit.Name); + string note = EscapePipe(credit.Note); + + if (!string.IsNullOrWhiteSpace(credit.Url)) + sb.AppendLine($"- [{name}]({credit.Url}) — {note}"); + else + sb.AppendLine($"- {name} — {note}"); } + + sb.AppendLine(); + } + + private static string ReasonEmoji(string reason) + { + if (reason.Contains("strict", StringComparison.OrdinalIgnoreCase)) + return "🏆"; + if (reason.Contains("near-baseline", StringComparison.OrdinalIgnoreCase) || + reason.Contains("size premium", StringComparison.OrdinalIgnoreCase)) + return "📈"; + if (reason.Contains("interior", StringComparison.OrdinalIgnoreCase)) + return "🧩"; + if (reason.Contains("spacing", StringComparison.OrdinalIgnoreCase) || + reason.Contains("collapse", StringComparison.OrdinalIgnoreCase)) + return "📏"; + if (reason.Contains("dominance", StringComparison.OrdinalIgnoreCase)) + return "🔪"; + + return "✅"; + } + + private static double? ResolveReferencePpl( + BenchmarkSnapshotRecord? pplReference, + IReadOnlyCollection pureBaselineSnapshots, + IReadOnlyCollection artifacts) + { + if (pplReference is { Ppl: > 0d }) + return pplReference.Ppl; + + var bestPure = pureBaselineSnapshots + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .ThenByDescending(x => x.SizeBytes) + .FirstOrDefault(); + + if (bestPure != null) + return bestPure.Ppl; + + return artifacts + .Select(x => x.Snapshot) + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .FirstOrDefault() + ?.Ppl; + } + + private static string FormatPplDeltaPercent(double ppl, double? referencePpl) + { + if (referencePpl is null or <= 0d || ppl <= 0d) + return "n/a"; + + double delta = ((ppl - referencePpl.Value) / referencePpl.Value) * 100d; + return delta.ToString("0.000"); } private static string ToGb(ulong bytes) => (bytes / 1024d / 1024d / 1024d).ToString("0.00"); diff --git a/MagicQuant/Services/SelectionDiagnosticsLogService.cs b/MagicQuant/Services/SelectionDiagnosticsLogService.cs new file mode 100644 index 0000000..4e400a5 --- /dev/null +++ b/MagicQuant/Services/SelectionDiagnosticsLogService.cs @@ -0,0 +1,170 @@ +using System.Text.Json; +using MagicQuant.Models; +using MQ.DB; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class SelectionDiagnosticsLogService +{ + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = true + }; + + public async Task WriteAsync( + IReadOnlyCollection benchmarkOverview, + IReadOnlyCollection validationFailures, + CancellationToken ct = default) + { + string directory = ResolveGgufDirectory(); + Directory.CreateDirectory(directory); + + string overviewPath = Path.Combine(directory, "magicquant-benchmark-overview.json"); + string missesPath = Path.Combine(directory, "magicquant-selection-validation-misses.json"); + + var overview = benchmarkOverview + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Config)) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .Select(ToSnapshotLog) + .ToList(); + + var misses = validationFailures + .Where(x => !x.Accepted) + .Select(ToFailureLog) + .ToList(); + + await File.WriteAllTextAsync(overviewPath, JsonSerializer.Serialize(overview, JsonOptions), ct); + await File.WriteAllTextAsync(missesPath, JsonSerializer.Serialize(misses, JsonOptions), ct); + + AnsiConsole.MarkupLine($"[green]Benchmark overview log:[/] {Markup.Escape(overviewPath)}"); + AnsiConsole.MarkupLine($"[green]Prediction miss log:[/] {Markup.Escape(missesPath)}"); + } + + private static object ToSnapshotLog(BenchmarkSnapshotRecord snap) + { + return new + { + key = TensorConfigIdentity.ToKey(snap.Config), + displayName = snap.DisplayName, + provider = snap.ProviderName, + baselineFamily = snap.BaselineFamily, + isHybrid = snap.IsHybrid, + isExternalPureBaseline = snap.IsExternalPureBaseline, + sizeBytes = snap.SizeBytes, + sizeGiB = ToGb(snap.SizeBytes), + kld = snap.Kld, + ppl = snap.Ppl, + outputModelPath = snap.OutputModelPath, + externalRepositoryUrl = snap.ExternalRepositoryUrl + }; + } + + private static object ToFailureLog(CandidateValidationResult failure) + { + var c = failure.Candidate; + var snap = failure.Snapshot; + + double? actualLine = null; + double? actualGainOverLine = null; + long? sizeMissBytes = null; + double? kldMiss = null; + + if (snap != null) + { + actualLine = InterpolateKldLine(snap.SizeBytes, c.HigherDamageAnchor, c.LowerDamageAnchor); + actualGainOverLine = actualLine.Value - snap.Kld; + + if (snap.SizeBytes < c.WindowMinSizeBytes) + sizeMissBytes = (long)c.WindowMinSizeBytes - (long)snap.SizeBytes; + else if (snap.SizeBytes > c.WindowMaxSizeBytes) + sizeMissBytes = (long)snap.SizeBytes - (long)c.WindowMaxSizeBytes; + else + sizeMissBytes = 0; + + kldMiss = snap.Kld - actualLine.Value; + } + + return new + { + reason = c.Reason.ToString(), + attemptOrder = c.AttemptOrder, + windowLabel = c.WindowLabel, + candidateKey = TensorConfigIdentity.ToKey(c.Prediction.Config), + candidateInternalName = HybridBenchmarkRepository.BuildDisplayName(c.Prediction.Quant), + predicted = new + { + sizeBytes = c.Prediction.PredictedSizeBytes, + sizeGiB = ToGb(c.Prediction.PredictedSizeBytes), + kld = c.Prediction.PredictedKld, + lineKldAtPredictedSize = c.LinearExpectedKld, + gainOverLine = c.PredictedGainOverLine + }, + actual = snap == null + ? null + : new + { + displayName = snap.DisplayName, + sizeBytes = snap.SizeBytes, + sizeGiB = ToGb(snap.SizeBytes), + kld = snap.Kld, + ppl = snap.Ppl, + lineKldAtActualSize = actualLine, + gainOverLine = actualGainOverLine, + sizeMissBytes, + kldMiss + }, + anchors = new + { + higherDamageSmaller = ToAnchorLog(c.HigherDamageAnchor), + lowerDamageLarger = ToAnchorLog(c.LowerDamageAnchor) + }, + accepted = failure.Accepted, + message = failure.Message + }; + } + + private static object ToAnchorLog(BenchmarkSnapshotRecord anchor) + { + return new + { + key = TensorConfigIdentity.ToKey(anchor.Config), + displayName = anchor.DisplayName, + sizeBytes = anchor.SizeBytes, + sizeGiB = ToGb(anchor.SizeBytes), + kld = anchor.Kld, + ppl = anchor.Ppl, + bitRange = anchor.Quant.BaseQuant.BitRange, + quantizeBase = anchor.Quant.BaseQuant.QuantizeBaseArgumentName + }; + } + + private static double InterpolateKldLine( + ulong candidateSize, + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger) + { + ulong smallSize = higherDamageSmaller.SizeBytes; + ulong largeSize = lowerDamageLarger.SizeBytes; + + if (largeSize <= smallSize) + return Math.Min(higherDamageSmaller.Kld, lowerDamageLarger.Kld); + + double t = Math.Clamp((candidateSize - smallSize) / (double)(largeSize - smallSize), 0d, 1d); + return higherDamageSmaller.Kld + ((lowerDamageLarger.Kld - higherDamageSmaller.Kld) * t); + } + + private static string ResolveGgufDirectory() + { + if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + return Path.Combine(Cache.ModelMagicQuantDirectory!, "GGUF"); + + if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) + return Path.Combine(Cache.MagicQuantDirectory!, "GGUF"); + + return Path.Combine(Directory.GetCurrentDirectory(), "GGUF"); + } + + private static string ToGb(ulong bytes) => (bytes / 1024d / 1024d / 1024d).ToString("0.00"); +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index b34bf3e..2d40f57 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -164,6 +164,11 @@ candidate_selection: near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 + # Default false: do not spend final prediction/build attempts trying to replace + # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. + # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. + allow_eight_bit_anchor_replacements: false + output: # Optional explicit output directory. # If blank, MagicQuant will default to: @@ -171,7 +176,7 @@ output: output_dir: # Prefix used when generating exported GGUF file names. - output_name_prefix: model + output_name_prefix: Model # By default MagicQuant will not locally export pure learned external/custom baselines # such as Unsloth. They remain upstream references in the README/output unless enabled. @@ -239,7 +244,7 @@ baselines: # # - repo_id: unsloth/Qwen3-4B-Instruct-2507-GGUF # enabled: true - # short_source_name: Unsloth + # short_source_name: UD # source_kind: huggingface_gguf_repository # # # Repository-level defaults: @@ -257,7 +262,7 @@ baselines: # - file_name: Qwen3-4B-Instruct-2507-UD-Q4_K_XL.gguf # baseline_family: Q4_K_M # quantize_base_name: Q4_K_M - # display_name: Unsloth_Q4_K_XL + # display_name: UD_Q4_K_XL # allow_as_learning_baseline: true # allow_as_combination_carrier: true # allow_as_explicit_group_candidate: true @@ -267,7 +272,7 @@ baselines: # - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf # baseline_family: Q5_K # quantize_base_name: Q5_K - # display_name: Unsloth_Q5_K_XL + # display_name: UD_Q5_K_XL # allow_as_learning_baseline: true # allow_as_combination_carrier: true # allow_as_explicit_group_candidate: true @@ -275,7 +280,7 @@ baselines: # - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf # baseline_family: Q6_K # quantize_base_name: Q6_K - # display_name: Unsloth_Q6_K_XL + # display_name: UD_Q6_K_XL # allow_as_learning_baseline: true # allow_as_combination_carrier: true # allow_as_explicit_group_candidate: true @@ -283,7 +288,7 @@ baselines: # - file_name: Qwen3-4B-Instruct-2507-UD-Q3_K_XL.gguf # baseline_family: IQ3_S # quantize_base_name: IQ3_S - # display_name: Unsloth_Q3_K_XL + # display_name: UD_Q3_K_XL # allow_as_learning_baseline: true # allow_as_combination_carrier: false # allow_as_explicit_group_candidate: true @@ -291,4 +296,4 @@ baselines: # # Example note: # # If the repo does not actually contain IQ3_XS, do not reference it. # # Use only filenames that truly exist in the repository. - [] \ No newline at end of file + [] diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 5bf4abe..f2d2066 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -92,10 +92,15 @@ candidate_selection: near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 + # Default false: do not spend final prediction/build attempts trying to replace + # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. + # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. + allow_eight_bit_anchor_replacements: false + output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: - output_name_prefix: model + output_name_prefix: Model export_external_learned_baselines: false # Legacy bit-range bucket survival settings were removed. @@ -129,7 +134,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-Q4_K_XL.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M - display_name: Unsloth_Q4_K_XL + display_name: UD_Q4_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true @@ -137,7 +142,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf baseline_family: Q5_K quantize_base_name: Q5_K - display_name: Unsloth_Q5_K_XL + display_name: UD_Q5_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true @@ -145,7 +150,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf baseline_family: Q6_K quantize_base_name: Q6_K - display_name: Unsloth_Q6_K_XL + display_name: UD_Q6_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: true allow_as_explicit_group_candidate: true @@ -153,7 +158,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-Q3_K_XL.gguf baseline_family: IQ3_S quantize_base_name: IQ3_S - display_name: Unsloth_Q3_K_XL + display_name: UD_Q3_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true @@ -161,7 +166,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-IQ3_XXS.gguf baseline_family: IQ3_XS quantize_base_name: IQ3_XS - display_name: Unsloth_IQ3_XXS_for_IQ3_XS + display_name: UD_IQ3_XXS allow_as_learning_baseline: true allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true \ No newline at end of file + allow_as_explicit_group_candidate: true From d26680198e385b8ce2658ccc2afe14f463e89014 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 25 Apr 2026 15:53:09 -0400 Subject: [PATCH 126/258] Workout output, readme, and more. --- MagicQuant/Models/HybridFinalizationModels.cs | 8 + .../Models/PredictionSelectionModels.cs | 2 +- .../CombinationSurvivalPipelineService.cs | 47 ++- .../Services/FinalArtifactNamingService.cs | 91 +++++- .../Services/FinalReleaseMetadataService.cs | 268 ++++++++++++++++++ .../FinalSurvivorSelectionCliService.cs | 94 +++++- .../Services/HybridArtifactExportService.cs | 34 ++- .../Services/ReadmeGenerationService.cs | 196 ++++++------- MagicQuant/config.dev.yaml | 10 +- 9 files changed, 585 insertions(+), 165 deletions(-) create mode 100644 MagicQuant/Services/FinalReleaseMetadataService.cs diff --git a/MagicQuant/Models/HybridFinalizationModels.cs b/MagicQuant/Models/HybridFinalizationModels.cs index c72f77e..f937da2 100644 --- a/MagicQuant/Models/HybridFinalizationModels.cs +++ b/MagicQuant/Models/HybridFinalizationModels.cs @@ -156,6 +156,14 @@ public sealed class FinalSelectionRow public int Id { get; set; } public bool Enabled { get; set; } = true; public BenchmarkSnapshotRecord Snapshot { get; init; } = default!; + + // Planned public/export identity. The CLI previews these names and the export + // service reuses them, so a user never sees one name in the selection UI and + // a different name in the produced GGUF/README. + public string PlannedFileName { get; set; } = string.Empty; + public string PlannedDisplayName { get; set; } = string.Empty; + public string PlannedProviderName { get; set; } = string.Empty; + public string PlannedQuantFamily { get; set; } = string.Empty; } public sealed class ExportedArtifactRecord diff --git a/MagicQuant/Models/PredictionSelectionModels.cs b/MagicQuant/Models/PredictionSelectionModels.cs index 18a6de4..e46dcdd 100644 --- a/MagicQuant/Models/PredictionSelectionModels.cs +++ b/MagicQuant/Models/PredictionSelectionModels.cs @@ -128,7 +128,6 @@ public sealed class PredictionValidationExportResult public IReadOnlyList Rows { get; init; } = Array.Empty(); } - public sealed class PhaseValidationResult { public IReadOnlyList AcceptedSnapshots { get; init; } = Array.Empty(); @@ -141,3 +140,4 @@ public sealed class PredictionGuidedSelectionResult public IReadOnlyList Eliminations { get; init; } = Array.Empty(); public IReadOnlyList ValidationFailures { get; init; } = Array.Empty(); } + diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index be6468e..cf8123b 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -19,6 +19,8 @@ public sealed class CombinationSurvivalPipelineService private readonly ReadmeGenerationService _readmeService; private readonly HybridMapGenerationService _hybridMapService; private readonly SelectionDiagnosticsLogService _diagnosticsLogService; + private readonly FinalReleaseMetadataService _releaseMetadataService; + private readonly FinalArtifactNamingService _namingService; public CombinationSurvivalPipelineService(QuantizationService quantizationService) { @@ -34,6 +36,8 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic _readmeService = new ReadmeGenerationService(); _hybridMapService = new HybridMapGenerationService(); _diagnosticsLogService = new SelectionDiagnosticsLogService(); + _releaseMetadataService = new FinalReleaseMetadataService(); + _namingService = new FinalArtifactNamingService(); } public async Task RunAsync(CancellationToken ct = default) @@ -76,9 +80,13 @@ public async Task RunAsync(CancellationToken AnsiConsole.MarkupLine($"[green]Final candidate/anchor survivors before manual enablement:[/] [cyan]{selection.Survivors.Count:N0}[/]"); AnsiConsole.MarkupLine($"[yellow]Recorded baseline/anchor eliminations:[/] [cyan]{selection.Eliminations.Count:N0}[/]"); AnsiConsole.MarkupLine($"[yellow]Prediction validation misses:[/] [cyan]{selection.ValidationFailures.Count:N0}[/]"); - RenderEliminationSummary(selection.Eliminations); + RenderEliminationSummary(selection.Eliminations, pureBaselines); - var selectedRows = _selectionCli.Prompt(selection.Survivors); + var nativeReference = await _benchmarkRepository.LoadBenchmarkSnapshotAsync( + (TensorConfig)HybridQuant.CreatePureBaseline(BaselineQuants.GetNativeQuant()), + ct); + + var selectedRows = _selectionCli.Prompt(selection.Survivors, pureBaselines, nativeReference); var exportedArtifacts = await _exportService.ExportAsync(selectedRows, pureBaselines, ct); @@ -94,12 +102,18 @@ public async Task RunAsync(CancellationToken .ThenBy(x => x.SizeBytes) .ToList(); - var nativeReference = await _benchmarkRepository.LoadBenchmarkSnapshotAsync( - (TensorConfig)HybridQuant.CreatePureBaseline(BaselineQuants.GetNativeQuant()), - ct); - await _diagnosticsLogService.WriteAsync(benchmarkOverview, selection.ValidationFailures, ct); + await _hybridMapService.GenerateAsync(Cache.OutputDirectory!, exportedArtifacts, ct); + + await _releaseMetadataService.GenerateAsync( + Cache.OutputDirectory!, + exportedArtifacts, + selection.Eliminations, + pureBaselines, + nativeReference, + ct); + await _readmeService.GenerateAsync( Cache.OutputDirectory!, modelName, @@ -109,8 +123,6 @@ await _readmeService.GenerateAsync( nativeReference, ct); - await _hybridMapService.GenerateAsync(Cache.OutputDirectory!, exportedArtifacts, ct); - return new CombinationSurvivalExecutionResult { BenchmarkedSnapshots = benchmarkOverview, @@ -124,11 +136,15 @@ await _readmeService.GenerateAsync( }; } - private static void RenderEliminationSummary(IReadOnlyCollection eliminations) + private void RenderEliminationSummary( + IReadOnlyCollection eliminations, + IReadOnlyCollection pureBaselineSnapshots) { if (eliminations.Count == 0) return; + var namingContext = _namingService.CreateContext(pureBaselineSnapshots); + AnsiConsole.Write(new Rule("[yellow]Baseline / Anchor Eliminations[/]") { Justification = Justify.Left }); var table = new Table().Border(TableBorder.Rounded); @@ -136,7 +152,7 @@ private static void RenderEliminationSummary(IReadOnlyCollection $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") @@ -146,19 +162,22 @@ private static void RenderEliminationSummary(IReadOnlyCollection 25) - AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in README/logs.[/]"); + AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in magicquant.replacements.json.[/]"); } } diff --git a/MagicQuant/Services/FinalArtifactNamingService.cs b/MagicQuant/Services/FinalArtifactNamingService.cs index 2eeba4d..8eba0cc 100644 --- a/MagicQuant/Services/FinalArtifactNamingService.cs +++ b/MagicQuant/Services/FinalArtifactNamingService.cs @@ -6,7 +6,7 @@ namespace MagicQuant.Services; /// /// Centralizes the public naming rules used by exported GGUF files, README rows, -/// links, and diagnostic logs. Internal tensor-combo display names stay internal. +/// CLI previews, links, and diagnostic logs. Internal tensor-combo display names stay internal. /// public sealed class FinalArtifactNamingService { @@ -24,9 +24,7 @@ public FinalArtifactName BuildName( FinalArtifactNamingContext context, ISet? reservedFileNames = null) { - string prefix = SanitizeToken(Config.OutputNamePrefix); - if (string.IsNullOrWhiteSpace(prefix)) - prefix = "Model"; + string prefix = ResolveModelPrefix(); string tag; string providerToken; @@ -41,9 +39,23 @@ public FinalArtifactName BuildName( } else if (snapshot.Quant.BaseQuant.IsExternalRepositoryBaseline) { - providerToken = ResolveExternalProviderToken(snapshot.Quant.BaseQuant); - quantFamily = NormalizeExternalDisplayName(snapshot.Quant.BaseQuant.Names[0], providerToken); - tag = SanitizeToken(quantFamily); + string externalProviderToken = ResolveExternalProviderToken(snapshot.Quant.BaseQuant); + string externalFamily = NormalizeExternalDisplayName(snapshot.Quant.BaseQuant.Names[0], externalProviderToken); + + if (Config.ExportExternalLearnedBaselines) + { + // This is a MagicQuant rebuilt/re-uploaded copy of an external learned baseline. + // Keep the external source tag, but mark the artifact as MQ-owned. + providerToken = "MQ"; + quantFamily = $"MQ-{SanitizeToken(externalFamily)}"; + tag = quantFamily; + } + else + { + providerToken = externalProviderToken; + quantFamily = SanitizeToken(externalFamily); + tag = quantFamily; + } } else { @@ -59,6 +71,7 @@ public FinalArtifactName BuildName( { FileName = fileName, DisplayName = Path.GetFileNameWithoutExtension(fileName), + ShortDisplayName = ToShortDisplayName(Path.GetFileNameWithoutExtension(fileName)), ProviderToken = providerToken, QuantFamilyOrBaseline = quantFamily }; @@ -68,9 +81,7 @@ public string BuildDisplayLabel( BenchmarkSnapshotRecord snapshot, FinalArtifactNamingContext context) { - string prefix = SanitizeToken(Config.OutputNamePrefix); - if (string.IsNullOrWhiteSpace(prefix)) - prefix = "Model"; + string prefix = ResolveModelPrefix(); if (snapshot.IsHybrid) return $"{prefix}-MQ-{SanitizeToken(ResolveHybridRangeFamily(snapshot, context))}"; @@ -78,12 +89,30 @@ public string BuildDisplayLabel( if (snapshot.Quant.BaseQuant.IsExternalRepositoryBaseline) { string providerToken = ResolveExternalProviderToken(snapshot.Quant.BaseQuant); - return $"{prefix}-{SanitizeToken(NormalizeExternalDisplayName(snapshot.Quant.BaseQuant.Names[0], providerToken))}"; + string family = SanitizeToken(NormalizeExternalDisplayName(snapshot.Quant.BaseQuant.Names[0], providerToken)); + return Config.ExportExternalLearnedBaselines + ? $"{prefix}-MQ-{family}" + : $"{prefix}-{family}"; } return $"{prefix}-LM-{SanitizeToken(snapshot.Quant.BaseQuant.Names[0])}"; } + public string ToShortDisplayName(string displayNameOrFileName) + { + if (string.IsNullOrWhiteSpace(displayNameOrFileName)) + return string.Empty; + + string value = Path.GetFileNameWithoutExtension(displayNameOrFileName.Trim()); + string prefix = ResolveModelPrefix(); + string fullPrefix = prefix + "-"; + + if (value.StartsWith(fullPrefix, StringComparison.OrdinalIgnoreCase)) + return value[fullPrefix.Length..]; + + return value; + } + public IReadOnlyList BuildProviderCredits( IReadOnlyCollection artifacts) { @@ -131,6 +160,37 @@ public IReadOnlyList BuildProviderCredits( .ToList(); } + public static string ReasonCode(string reason) + { + if (reason.Contains("strict", StringComparison.OrdinalIgnoreCase)) + return "STRICT_DOMINANCE"; + if (reason.Contains("near-baseline", StringComparison.OrdinalIgnoreCase) || + reason.Contains("size premium", StringComparison.OrdinalIgnoreCase)) + return "NEAR_BASELINE_PREMIUM"; + if (reason.Contains("interior", StringComparison.OrdinalIgnoreCase)) + return "INTERIOR_DISCOVERY"; + if (reason.Contains("spacing", StringComparison.OrdinalIgnoreCase) || + reason.Contains("collapse", StringComparison.OrdinalIgnoreCase)) + return "SPACING_COLLAPSE"; + if (reason.Contains("dominance", StringComparison.OrdinalIgnoreCase)) + return "FINAL_DOMINANCE"; + + return "VALIDATED_REPLACEMENT"; + } + + public static string ReasonDescription(string code) + { + return code switch + { + "STRICT_DOMINANCE" => "The winner was no larger and had lower real KLD than the removed anchor.", + "NEAR_BASELINE_PREMIUM" => "The winner used only the configured near-baseline size premium and beat the real linear KLD trade line.", + "INTERIOR_DISCOVERY" => "The winner was selected as a useful interior point inside a size/KLD gap between anchors.", + "SPACING_COLLAPSE" => "Two candidates were too close in practical output space; the stronger one was kept.", + "FINAL_DOMINANCE" => "A later validated survivor dominated this artifact in final real benchmark comparison.", + _ => "A validated survivor replaced or made this artifact redundant." + }; + } + private static IEnumerable EnumerateBaselinesUsedBy(HybridQuant quant) { yield return quant.BaseQuant; @@ -235,7 +295,7 @@ private static string NormalizeExternalDisplayName(string displayName, string pr value = $"{providerToken}_{value}"; } - return value; + return SanitizeToken(value); } private static string MakeUniqueFileName(string desiredFileName, ISet? reservedFileNames) @@ -257,6 +317,12 @@ private static string MakeUniqueFileName(string desiredFileName, ISet? r return candidate; } + private static string ResolveModelPrefix() + { + string prefix = SanitizeToken(Config.OutputNamePrefix); + return string.IsNullOrWhiteSpace(prefix) ? "Model" : prefix; + } + public static string SanitizeToken(string value) { if (string.IsNullOrWhiteSpace(value)) @@ -306,6 +372,7 @@ public sealed class FinalArtifactName { public string FileName { get; init; } = string.Empty; public string DisplayName { get; init; } = string.Empty; + public string ShortDisplayName { get; init; } = string.Empty; public string ProviderToken { get; init; } = string.Empty; public string QuantFamilyOrBaseline { get; init; } = string.Empty; } diff --git a/MagicQuant/Services/FinalReleaseMetadataService.cs b/MagicQuant/Services/FinalReleaseMetadataService.cs new file mode 100644 index 0000000..ea44a5c --- /dev/null +++ b/MagicQuant/Services/FinalReleaseMetadataService.cs @@ -0,0 +1,268 @@ +using System.Text.Json; +using MagicQuant.Models; +using MQ.DB; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class FinalReleaseMetadataService +{ + public const string FinalSurvivorsFileName = "magicquant.final-survivors.json"; + public const string ReplacementsFileName = "magicquant.replacements.json"; + + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = true + }; + + private readonly FinalArtifactNamingService _namingService = new(); + + public async Task GenerateAsync( + string outputDirectory, + IReadOnlyCollection exportedArtifacts, + IReadOnlyCollection eliminations, + IReadOnlyCollection pureBaselineSnapshots, + BenchmarkSnapshotRecord? pplReference = null, + CancellationToken ct = default) + { + Directory.CreateDirectory(outputDirectory); + + double? referencePpl = ResolveReferencePpl(pplReference, pureBaselineSnapshots, exportedArtifacts.Select(x => x.Snapshot).ToList()); + var namingContext = _namingService.CreateContext(pureBaselineSnapshots); + var exportedByKey = exportedArtifacts + .GroupBy(x => TensorConfigIdentity.ToKey(x.Snapshot.Config), StringComparer.Ordinal) + .ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal); + + var replacementMap = BuildReplacementMap(eliminations); + + string finalPath = Path.Combine(outputDirectory, FinalSurvivorsFileName); + var survivors = exportedArtifacts + .OrderBy(x => x.Snapshot.Kld) + .ThenBy(x => x.Snapshot.SizeBytes) + .Select(x => ToSurvivorJson(x, referencePpl, replacementMap)) + .ToList(); + await File.WriteAllTextAsync(finalPath, JsonSerializer.Serialize(survivors, JsonOptions), ct); + + string replacementsPath = Path.Combine(outputDirectory, ReplacementsFileName); + var replacements = eliminations + .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") + .OrderBy(x => x.Eliminated.Kld) + .ThenBy(x => x.Eliminated.SizeBytes) + .Select(x => ToReplacementJson(x, exportedByKey, namingContext, referencePpl)) + .ToList(); + await File.WriteAllTextAsync(replacementsPath, JsonSerializer.Serialize(replacements, JsonOptions), ct); + + AnsiConsole.MarkupLine($"[green]Final survivor metrics JSON generated:[/] {Markup.Escape(finalPath)}"); + AnsiConsole.MarkupLine($"[green]Replacement detail JSON generated:[/] {Markup.Escape(replacementsPath)}"); + } + + private object ToSurvivorJson( + ExportedArtifactRecord artifact, + double? referencePpl, + IReadOnlyDictionary> replacementMap) + { + string key = TensorConfigIdentity.ToKey(artifact.Snapshot.Config); + var replacements = ResolveTransitiveReplacements(key, replacementMap) + .Select(x => new + { + key = TensorConfigIdentity.ToKey(x.Eliminated.Config), + shortName = _namingService.ToShortDisplayName(x.Eliminated.DisplayName), + internalDisplayName = x.Eliminated.DisplayName, + kld = x.Eliminated.Kld, + ppl = x.Eliminated.Ppl, + pplDeltaPercent = CalculatePplDeltaPercent(x.Eliminated.Ppl, referencePpl), + sizeBytes = x.Eliminated.SizeBytes, + sizeGiB = ToGiBNumber(x.Eliminated.SizeBytes), + reasonCode = FinalArtifactNamingService.ReasonCode(x.Reason), + reason = x.Reason + }) + .ToList(); + + return new + { + key, + fileName = artifact.IsExternalReference + ? EnsureGgufExtension(artifact.DisplayName) + : artifact.FileName, + displayName = artifact.DisplayName, + shortName = _namingService.ToShortDisplayName(artifact.DisplayName), + provider = artifact.ProviderName, + quantFamily = artifact.BaselineFamily, + isHybrid = artifact.Snapshot.IsHybrid, + isExternalReference = artifact.IsExternalReference, + downloadTarget = artifact.DownloadTarget, + kld = artifact.Snapshot.Kld, + ppl = artifact.Snapshot.Ppl, + pplDeltaPercent = CalculatePplDeltaPercent(artifact.Snapshot.Ppl, referencePpl), + sizeBytes = artifact.Snapshot.SizeBytes, + sizeGiB = ToGiBNumber(artifact.Snapshot.SizeBytes), + expectedSizeBytes = artifact.ExpectedSizeBytes, + actualSizeBytes = artifact.ActualSizeBytes, + usedImatrix = Cache.UseImatrix && Cache.IsImatrixAvailable, + replacedArtifacts = replacements + }; + } + + private object ToReplacementJson( + BaselineEliminationRecord row, + IReadOnlyDictionary exportedByKey, + FinalArtifactNamingContext namingContext, + double? referencePpl) + { + string reasonCode = FinalArtifactNamingService.ReasonCode(row.Reason); + double kldDelta = row.Eliminated.Kld - row.Eliminator.Kld; + long sizeDeltaBytes = (long)row.Eliminated.SizeBytes - (long)row.Eliminator.SizeBytes; + double? pplDeltaPercentRemoved = CalculatePplDeltaPercent(row.Eliminated.Ppl, referencePpl); + double? pplDeltaPercentWinner = CalculatePplDeltaPercent(row.Eliminator.Ppl, referencePpl); + double? pplDeltaPercentImprovement = pplDeltaPercentRemoved.HasValue && pplDeltaPercentWinner.HasValue + ? pplDeltaPercentRemoved.Value - pplDeltaPercentWinner.Value + : null; + + return new + { + reasonCode, + reasonDescription = FinalArtifactNamingService.ReasonDescription(reasonCode), + rawReason = row.Reason, + removed = ToReplacementSideJson(row.Eliminated, exportedByKey, namingContext, referencePpl), + winner = ToReplacementSideJson(row.Eliminator, exportedByKey, namingContext, referencePpl), + deltas = new + { + kld = kldDelta, + sizeBytes = sizeDeltaBytes, + sizeGiB = sizeDeltaBytes / 1024d / 1024d / 1024d, + removedPplDeltaPercent = pplDeltaPercentRemoved, + winnerPplDeltaPercent = pplDeltaPercentWinner, + pplDeltaPercentImprovement = pplDeltaPercentImprovement + } + }; + } + + private object ToReplacementSideJson( + BenchmarkSnapshotRecord snapshot, + IReadOnlyDictionary exportedByKey, + FinalArtifactNamingContext namingContext, + double? referencePpl) + { + string key = TensorConfigIdentity.ToKey(snapshot.Config); + string displayName; + string fileName; + string shortName; + string provider; + string quantFamily; + + if (exportedByKey.TryGetValue(key, out var artifact)) + { + displayName = artifact.DisplayName; + shortName = _namingService.ToShortDisplayName(artifact.DisplayName); + fileName = artifact.IsExternalReference ? EnsureGgufExtension(artifact.DisplayName) : artifact.FileName ?? EnsureGgufExtension(artifact.DisplayName); + provider = artifact.ProviderName; + quantFamily = artifact.BaselineFamily; + } + else + { + displayName = _namingService.BuildDisplayLabel(snapshot, namingContext); + shortName = _namingService.ToShortDisplayName(displayName); + fileName = EnsureGgufExtension(displayName); + provider = snapshot.IsHybrid ? "MagicQuant" : HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); + quantFamily = snapshot.BaselineFamily; + } + + return new + { + key, + fileName, + displayName, + shortName, + provider, + quantFamily, + isHybrid = snapshot.IsHybrid, + isExternalPureBaseline = snapshot.IsExternalPureBaseline, + kld = snapshot.Kld, + ppl = snapshot.Ppl, + pplDeltaPercent = CalculatePplDeltaPercent(snapshot.Ppl, referencePpl), + sizeBytes = snapshot.SizeBytes, + sizeGiB = ToGiBNumber(snapshot.SizeBytes) + }; + } + + public static IReadOnlyDictionary> BuildReplacementMap( + IReadOnlyCollection eliminations) + { + return eliminations + .GroupBy(x => TensorConfigIdentity.ToKey(x.Eliminator.Config), StringComparer.Ordinal) + .ToDictionary(g => g.Key, g => g.ToList(), StringComparer.Ordinal); + } + + public static IReadOnlyList ResolveTransitiveReplacements( + string finalWinnerKey, + IReadOnlyDictionary> replacementMap) + { + var output = new List(); + var visited = new HashSet(StringComparer.Ordinal); + + void Visit(string winnerKey) + { + if (!visited.Add(winnerKey)) + return; + + if (!replacementMap.TryGetValue(winnerKey, out var direct)) + return; + + foreach (var row in direct) + { + output.Add(row); + Visit(TensorConfigIdentity.ToKey(row.Eliminated.Config)); + } + } + + Visit(finalWinnerKey); + + return output + .DistinctBy(x => TensorConfigIdentity.ToKey(x.Eliminated.Config), StringComparer.Ordinal) + .ToList(); + } + + private static double? ResolveReferencePpl( + BenchmarkSnapshotRecord? pplReference, + IReadOnlyCollection pureBaselineSnapshots, + IReadOnlyCollection snapshots) + { + if (pplReference is { Ppl: > 0d }) + return pplReference.Ppl; + + var bestPure = pureBaselineSnapshots + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .ThenByDescending(x => x.SizeBytes) + .FirstOrDefault(); + + if (bestPure != null) + return bestPure.Ppl; + + return snapshots + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .FirstOrDefault() + ?.Ppl; + } + + public static double? CalculatePplDeltaPercent(double ppl, double? referencePpl) + { + if (referencePpl is null or <= 0d || ppl <= 0d) + return null; + + return ((ppl - referencePpl.Value) / referencePpl.Value) * 100d; + } + + private static string EnsureGgufExtension(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return string.Empty; + + return value.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase) + ? value + : value + ".gguf"; + } + + private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; +} diff --git a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs index 9787089..1ecc59d 100644 --- a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs +++ b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs @@ -5,16 +5,33 @@ namespace MagicQuant.Services; public sealed class FinalSurvivorSelectionCliService { - public IReadOnlyList Prompt(IReadOnlyCollection survivors) + private readonly FinalArtifactNamingService _namingService = new(); + + public IReadOnlyList Prompt( + IReadOnlyCollection survivors, + IReadOnlyCollection pureBaselineSnapshots, + BenchmarkSnapshotRecord? pplReference = null) { + var namingContext = _namingService.CreateContext(pureBaselineSnapshots); + var reservedFileNames = new HashSet(StringComparer.OrdinalIgnoreCase); + double? referencePpl = ResolveReferencePpl(pplReference, pureBaselineSnapshots, survivors); + var rows = survivors .OrderBy(x => x.Kld) .ThenBy(x => x.SizeBytes) - .Select((snapshot, index) => new FinalSelectionRow + .Select((snapshot, index) => { - Id = index + 1, - Enabled = true, - Snapshot = snapshot + var name = _namingService.BuildName(snapshot, namingContext, reservedFileNames); + return new FinalSelectionRow + { + Id = index + 1, + Enabled = true, + Snapshot = snapshot, + PlannedFileName = name.FileName, + PlannedDisplayName = name.DisplayName, + PlannedProviderName = ResolveProviderName(snapshot, name), + PlannedQuantFamily = name.QuantFamilyOrBaseline + }; }) .ToList(); @@ -26,7 +43,7 @@ public IReadOnlyList Prompt(IReadOnlyCollection("Toggle [cyan]row number[/], or type [green]ready[/] to continue") @@ -62,7 +79,7 @@ public IReadOnlyList Prompt(IReadOnlyCollection rows) + private static void Render(IReadOnlyCollection rows, double? referencePpl) { AnsiConsole.Clear(); AnsiConsole.Write(new Rule("[yellow]Final Survivor Selection[/]") { Justification = Justify.Left }); @@ -72,20 +89,25 @@ private static void Render(IReadOnlyCollection rows) table.AddColumn("State"); table.AddColumn("Display / Model"); table.AddColumn("Provider"); - table.AddColumn("Quant Family / Baseline"); + table.AddColumn("Quant Family"); table.AddColumn("KLD"); - table.AddColumn("PPL"); + table.AddColumn("PPL Δ %"); table.AddColumn("Size (GB)"); foreach (var row in rows) { var snap = row.Snapshot; string state = row.Enabled ? "[green]ENABLED[/]" : "[red]DISABLED[/]"; - string display = row.Enabled ? Markup.Escape(snap.DisplayName) : $"[grey]{Markup.Escape(snap.DisplayName)}[/]"; - string provider = row.Enabled ? Markup.Escape(snap.ProviderName) : $"[grey]{Markup.Escape(snap.ProviderName)}[/]"; - string family = row.Enabled ? Markup.Escape(snap.BaselineFamily) : $"[grey]{Markup.Escape(snap.BaselineFamily)}[/]"; + string displayValue = string.IsNullOrWhiteSpace(row.PlannedDisplayName) ? snap.DisplayName : row.PlannedDisplayName; + string providerValue = string.IsNullOrWhiteSpace(row.PlannedProviderName) ? snap.ProviderName : row.PlannedProviderName; + string familyValue = string.IsNullOrWhiteSpace(row.PlannedQuantFamily) ? snap.BaselineFamily : row.PlannedQuantFamily; + + string display = row.Enabled ? Markup.Escape(displayValue) : $"[grey]{Markup.Escape(displayValue)}[/]"; + string provider = row.Enabled ? Markup.Escape(providerValue) : $"[grey]{Markup.Escape(providerValue)}[/]"; + string family = row.Enabled ? Markup.Escape(familyValue) : $"[grey]{Markup.Escape(familyValue)}[/]"; string kld = row.Enabled ? $"[cyan]{snap.Kld:0.000000}[/]" : $"[grey]{snap.Kld:0.000000}[/]"; - string ppl = row.Enabled ? $"[cyan]{snap.Ppl:0.0000}[/]" : $"[grey]{snap.Ppl:0.0000}[/]"; + string pplDelta = FormatPplDeltaPercent(snap.Ppl, referencePpl); + string ppl = row.Enabled ? $"[cyan]{pplDelta}[/]" : $"[grey]{pplDelta}[/]"; string sizeGb = (snap.SizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); table.AddRow( @@ -100,6 +122,50 @@ private static void Render(IReadOnlyCollection rows) } AnsiConsole.Write(table); - AnsiConsole.MarkupLine("[grey]All rows start enabled. Enter a row number to toggle it, then type ready when done.[/]"); + AnsiConsole.MarkupLine("[grey]PPL Δ % is measured against the native/reference PPL when available. Negative is better; larger positive values are worse.[/]"); + } + + private static string ResolveProviderName(BenchmarkSnapshotRecord snapshot, FinalArtifactName name) + { + if (snapshot.IsHybrid) + return "MagicQuant"; + + if (string.Equals(name.ProviderToken, "MQ", StringComparison.OrdinalIgnoreCase)) + return "MagicQuant"; + + return HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); + } + + private static double? ResolveReferencePpl( + BenchmarkSnapshotRecord? pplReference, + IReadOnlyCollection pureBaselineSnapshots, + IReadOnlyCollection survivors) + { + if (pplReference is { Ppl: > 0d }) + return pplReference.Ppl; + + var bestPure = pureBaselineSnapshots + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .ThenByDescending(x => x.SizeBytes) + .FirstOrDefault(); + + if (bestPure != null) + return bestPure.Ppl; + + return survivors + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .FirstOrDefault() + ?.Ppl; + } + + private static string FormatPplDeltaPercent(double ppl, double? referencePpl) + { + if (referencePpl is null or <= 0d || ppl <= 0d) + return "n/a"; + + double delta = ((ppl - referencePpl.Value) / referencePpl.Value) * 100d; + return $"{delta:0.000}%"; } } diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index e460530..cdd4abb 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -63,8 +63,10 @@ public async Task> ExportAsync( var snap = row.Snapshot; bool isHybrid = snap.IsHybrid; bool exportLocally = isHybrid || !snap.IsExternalPureBaseline || Config.ExportExternalLearnedBaselines; - var name = _namingService.BuildName(snap, namingContext, reservedFileNames); - string provider = ResolveReadmeProviderName(snap, isHybrid); + var name = ResolvePlannedOrBuildName(row, snap, namingContext, reservedFileNames); + string provider = !string.IsNullOrWhiteSpace(row.PlannedProviderName) + ? row.PlannedProviderName + : ResolveReadmeProviderName(snap, isHybrid, name); if (!exportLocally) { @@ -132,11 +134,37 @@ public async Task> ExportAsync( return output; } - private static string ResolveReadmeProviderName(BenchmarkSnapshotRecord snapshot, bool isHybrid) + private FinalArtifactName ResolvePlannedOrBuildName( + FinalSelectionRow row, + BenchmarkSnapshotRecord snapshot, + FinalArtifactNamingContext namingContext, + ISet reservedFileNames) + { + if (!string.IsNullOrWhiteSpace(row.PlannedFileName) && + !string.IsNullOrWhiteSpace(row.PlannedDisplayName)) + { + reservedFileNames.Add(row.PlannedFileName); + return new FinalArtifactName + { + FileName = row.PlannedFileName, + DisplayName = row.PlannedDisplayName, + ShortDisplayName = _namingService.ToShortDisplayName(row.PlannedDisplayName), + ProviderToken = row.PlannedProviderName, + QuantFamilyOrBaseline = row.PlannedQuantFamily + }; + } + + return _namingService.BuildName(snapshot, namingContext, reservedFileNames); + } + + private static string ResolveReadmeProviderName(BenchmarkSnapshotRecord snapshot, bool isHybrid, FinalArtifactName name) { if (isHybrid) return "MagicQuant"; + if (string.Equals(name.ProviderToken, "MQ", StringComparison.OrdinalIgnoreCase)) + return "MagicQuant"; + return HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); } diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 4c90984..542ee00 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -20,11 +20,14 @@ public async Task GenerateAsync( Directory.CreateDirectory(outputDirectory); string readmePath = Path.Combine(outputDirectory, "README.md"); + var replacementMap = FinalReleaseMetadataService.BuildReplacementMap(eliminatedBaselines ?? Array.Empty()); var namingContext = _namingService.CreateContext(pureBaselineSnapshots); - double? referencePpl = ResolveReferencePpl(pplReference, pureBaselineSnapshots, exportedArtifacts); + var exportedByKey = exportedArtifacts + .GroupBy(x => TensorConfigIdentity.ToKey(x.Snapshot.Config), StringComparer.Ordinal) + .ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal); var sb = new StringBuilder(); - sb.AppendLine($"# MagicQuant Hybrids (v2.1) - {modelName}"); + sb.AppendLine($"# MagicQuant Hybrids (v2.2) - {modelName}"); sb.AppendLine(); sb.AppendLine("MagicQuant is **not** a quantization technique by itself."); sb.AppendLine(); @@ -35,117 +38,117 @@ public async Task GenerateAsync( sb.AppendLine("## Final surviving downloadable outputs"); sb.AppendLine(); - AppendDownloadTable(sb, exportedArtifacts, referencePpl); - sb.AppendLine(); - sb.AppendLine("> **PPL Δ % note:** negative is better. Larger positive values are worse. The percentage is measured against the native/reference PPL when available; otherwise it falls back to the best available reference in this release set."); + AppendDownloadTable(sb, exportedArtifacts, replacementMap, exportedByKey, namingContext); sb.AppendLine(); - if (eliminatedBaselines is { Count: > 0 }) - { - sb.AppendLine("## Baselines / anchors removed from final download table"); - sb.AppendLine(); - sb.AppendLine("These rows are intentionally **not** part of the primary download table. They explain which pure baselines or previously-surviving anchors were beaten, collapsed, or made redundant by a validated artifact."); - sb.AppendLine(); - AppendEliminationLegend(sb); - sb.AppendLine(); - AppendEliminationTable(sb, eliminatedBaselines, exportedArtifacts, namingContext); - sb.AppendLine(); - } - - sb.AppendLine("## Method note"); + sb.AppendLine("## Release metadata"); sb.AppendLine(); - sb.AppendLine("The final chooser uses rank-safe isolation prediction: Q8-carrier single-group isolation measurements provide the additive backbone, a low-bit interaction correction improves numeric KLD closeness, and an isotonic projection keeps the final predicted ordering monotone with the isolation backbone. Predicted candidates still have to validate against real benchmark truth before they can replace a baseline or remain as an interior hybrid."); + sb.AppendLine("- [Final survivor metrics](./../../resolve/main/magicquant.final-survivors.json?download=true) — full file names, KLD, PPL delta %, byte sizes, download targets, and replacement lineage. PPL delta % is measured against the native/reference PPL when available; negative is better and larger positive values are worse."); + sb.AppendLine("- [Hybrid tensor map](./../../resolve/main/magicquant.hybrid-map.json?download=true) — tensor-group assignments and effective-state details for MagicQuant hybrid GGUFs."); + sb.AppendLine("- [Replacement details](./../../resolve/main/magicquant.replacements.json?download=true) — structured details for baselines or anchors removed from the final download table, including reason codes, KLD deltas, PPL delta %, and size deltas."); sb.AppendLine(); - - sb.AppendLine("## Dive Deeper"); + AppendReasonCodeDetails(sb); sb.AppendLine(); - sb.AppendLine("- Browse the project GitHub/Wiki for benchmark methodology, architecture notes, and planned pipeline improvements."); - sb.AppendLine("- If you spot a mistake, edge case, or a better practical trade, open an issue or share the artifact details so the comparison can be improved."); + + AppendCollapsible(sb, "Method note", "The final chooser uses rank-safe isolation prediction: Q8-carrier single-group isolation measurements provide the additive backbone, a low-bit interaction correction improves numeric KLD closeness, and an isotonic projection keeps the final predicted ordering monotone with the isolation backbone. Predicted candidates still have to validate against real benchmark truth before they can replace a baseline or remain as an interior hybrid."); sb.AppendLine(); AppendProviderCredits(sb, exportedArtifacts); - - sb.AppendLine("## Warning"); sb.AppendLine(); - sb.AppendLine("External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing."); - sb.AppendLine(); - sb.AppendLine("That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency, not as a universal judgment of the original creator's exact release artifact."); + + AppendCollapsible(sb, "Warning", "External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing. That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency, not as a universal judgment of the original creator's exact release artifact."); sb.AppendLine(); - sb.AppendLine("## Support"); + AppendCollapsible(sb, "Dive deeper", "Browse the project GitHub/Wiki for benchmark methodology, architecture notes, and planned pipeline improvements. If you spot a mistake, edge case, or a better practical trade, open an issue or share the artifact details so the comparison can be improved."); sb.AppendLine(); - sb.AppendLine("If this release helped you, a star, issue report, correction, or benchmark reproduction note is genuinely useful. Careful feedback matters more than hype, especially when a hybrid looks surprisingly good or surprisingly bad."); + + AppendCollapsible(sb, "Support", "If this release helped you, a star, issue report, correction, or benchmark reproduction note is genuinely useful. Careful feedback matters more than hype, especially when a hybrid looks surprisingly good or surprisingly bad."); await File.WriteAllTextAsync(readmePath, sb.ToString(), ct); AnsiConsole.MarkupLine($"[green]README generated:[/] {Markup.Escape(readmePath)}"); return readmePath; } - private static void AppendDownloadTable( + private void AppendDownloadTable( StringBuilder sb, IReadOnlyCollection artifacts, - double? referencePpl) + IReadOnlyDictionary> replacementMap, + IReadOnlyDictionary exportedByKey, + FinalArtifactNamingContext namingContext) { - sb.AppendLine("| Name | Provider | Quant Family / Baseline | KLD | PPL Δ % | Size (GB) | Download |"); - sb.AppendLine("|---|---|---|---:|---:|---:|---|"); + sb.AppendLine("| Name | Provider | Quant Family | KLD | Size (GB) | Download |"); + sb.AppendLine("|---|---|---|---:|---:|---|"); foreach (var artifact in artifacts.OrderBy(x => x.Snapshot.Kld).ThenBy(x => x.Snapshot.SizeBytes)) { + string key = TensorConfigIdentity.ToKey(artifact.Snapshot.Config); + string shortName = _namingService.ToShortDisplayName(artifact.DisplayName); + var replacements = FinalReleaseMetadataService.ResolveTransitiveReplacements(key, replacementMap); + string nameCell = BuildNameCell(shortName, replacements, exportedByKey, namingContext); string sizeGb = ToGb(artifact.Snapshot.SizeBytes); string download = artifact.IsExternalReference ? $"[Link]({artifact.DownloadTarget})" : $"[Link](./../../resolve/main/{artifact.FileName}?download=true)"; sb.AppendLine( - $"| {EscapePipe(artifact.DisplayName)} | {EscapePipe(artifact.ProviderName)} | {EscapePipe(artifact.BaselineFamily)} | " + - $"{artifact.Snapshot.Kld:0.000000} | {FormatPplDeltaPercent(artifact.Snapshot.Ppl, referencePpl)} | {sizeGb} | {download} |"); + $"| {nameCell} | {EscapePipe(artifact.ProviderName)} | {EscapePipe(artifact.BaselineFamily)} | " + + $"{artifact.Snapshot.Kld:0.000000} | {sizeGb} | {download} |"); } } - private static void AppendEliminationLegend(StringBuilder sb) - { - sb.AppendLine("**Reason legend:** 🏆 strict dominance, 📈 near-baseline premium, 🧩 useful interior discovery, 📏 spacing collapse, 🔪 final dominance."); - } - - private void AppendEliminationTable( - StringBuilder sb, - IReadOnlyCollection eliminations, - IReadOnlyCollection artifacts, + private string BuildNameCell( + string shortName, + IReadOnlyList replacements, + IReadOnlyDictionary exportedByKey, FinalArtifactNamingContext namingContext) { - var exportedByKey = artifacts - .GroupBy(x => TensorConfigIdentity.ToKey(x.Snapshot.Config), StringComparer.Ordinal) - .ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal); + if (replacements.Count == 0) + return EscapePipe(shortName); - sb.AppendLine("| Removed | Winner | KLD Δ | Size Δ (GB) | Why |"); - sb.AppendLine("|---|---|---:|---:|---|"); + var replacedNames = replacements + .Select(x => GetPublicShortName(x.Eliminated, exportedByKey, namingContext)) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Take(5) + .ToList(); - foreach (var row in eliminations - .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") - .OrderBy(x => x.Eliminated.Kld) - .ThenBy(x => x.Eliminated.SizeBytes)) - { - string removed = GetPublicName(row.Eliminated, exportedByKey, namingContext); - string winner = GetPublicName(row.Eliminator, exportedByKey, namingContext); + string tooltip = replacedNames.Count == 0 + ? "Replaced one or more dominated artifacts. See magicquant.replacements.json." + : $"Replaced: {string.Join(", ", replacedNames)}"; - double kldDelta = row.Eliminated.Kld - row.Eliminator.Kld; - double sizeDeltaGb = (row.Eliminated.SizeBytes - (double)row.Eliminator.SizeBytes) / 1024d / 1024d / 1024d; + if (replacements.Count > replacedNames.Count) + tooltip += $" + {replacements.Count - replacedNames.Count} more"; - sb.AppendLine( - $"| {EscapePipe(removed)} | {EscapePipe(winner)} | {kldDelta:0.000000} | {sizeDeltaGb:0.00} | {ReasonEmoji(row.Reason)} |"); - } + return $"[{EscapePipe(shortName)}](#winner-notes \"{EscapeTooltip(tooltip)}\")"; } - private static string GetPublicName( + private string GetPublicShortName( BenchmarkSnapshotRecord snapshot, IReadOnlyDictionary exportedByKey, FinalArtifactNamingContext namingContext) { string key = TensorConfigIdentity.ToKey(snapshot.Config); if (exportedByKey.TryGetValue(key, out var artifact)) - return artifact.DisplayName; + return _namingService.ToShortDisplayName(artifact.DisplayName); - return new FinalArtifactNamingService().BuildDisplayLabel(snapshot, namingContext); + return _namingService.ToShortDisplayName(_namingService.BuildDisplayLabel(snapshot, namingContext)); + } + + private static void AppendReasonCodeDetails(StringBuilder sb) + { + sb.AppendLine("
"); + sb.AppendLine("Replacement reason codes"); + sb.AppendLine(); + sb.AppendLine("- `STRICT_DOMINANCE` — the winner was no larger and had lower real KLD than the removed anchor."); + sb.AppendLine("- `NEAR_BASELINE_PREMIUM` — the winner used only the configured near-baseline size premium and beat the real linear KLD trade line."); + sb.AppendLine("- `INTERIOR_DISCOVERY` — the winner was selected as a useful interior point inside a size/KLD gap between anchors."); + sb.AppendLine("- `SPACING_COLLAPSE` — two candidates were too close in practical output space, so the stronger one was kept."); + sb.AppendLine("- `FINAL_DOMINANCE` — a later validated survivor dominated this artifact in final real benchmark comparison."); + sb.AppendLine(); + sb.AppendLine(""); + sb.AppendLine("Underlined names in the table replaced or ultimately inherited the replacement of another artifact. Hover the name for the short replacement summary, or inspect `magicquant.replacements.json` for exact KLD/PPL/size deltas."); + sb.AppendLine(); + sb.AppendLine("
"); } private void AppendProviderCredits(StringBuilder sb, IReadOnlyCollection artifacts) @@ -154,8 +157,8 @@ private void AppendProviderCredits(StringBuilder sb, IReadOnlyCollection"); + sb.AppendLine("Provider credits"); sb.AppendLine(); foreach (var credit in credits) @@ -170,60 +173,21 @@ private void AppendProviderCredits(StringBuilder sb, IReadOnlyCollection"); } - private static string ReasonEmoji(string reason) + private static void AppendCollapsible(StringBuilder sb, string summary, string body) { - if (reason.Contains("strict", StringComparison.OrdinalIgnoreCase)) - return "🏆"; - if (reason.Contains("near-baseline", StringComparison.OrdinalIgnoreCase) || - reason.Contains("size premium", StringComparison.OrdinalIgnoreCase)) - return "📈"; - if (reason.Contains("interior", StringComparison.OrdinalIgnoreCase)) - return "🧩"; - if (reason.Contains("spacing", StringComparison.OrdinalIgnoreCase) || - reason.Contains("collapse", StringComparison.OrdinalIgnoreCase)) - return "📏"; - if (reason.Contains("dominance", StringComparison.OrdinalIgnoreCase)) - return "🔪"; - - return "✅"; - } - - private static double? ResolveReferencePpl( - BenchmarkSnapshotRecord? pplReference, - IReadOnlyCollection pureBaselineSnapshots, - IReadOnlyCollection artifacts) - { - if (pplReference is { Ppl: > 0d }) - return pplReference.Ppl; - - var bestPure = pureBaselineSnapshots - .Where(x => x.Ppl > 0d) - .OrderBy(x => x.Kld) - .ThenByDescending(x => x.SizeBytes) - .FirstOrDefault(); - - if (bestPure != null) - return bestPure.Ppl; - - return artifacts - .Select(x => x.Snapshot) - .Where(x => x.Ppl > 0d) - .OrderBy(x => x.Kld) - .FirstOrDefault() - ?.Ppl; - } - - private static string FormatPplDeltaPercent(double ppl, double? referencePpl) - { - if (referencePpl is null or <= 0d || ppl <= 0d) - return "n/a"; - - double delta = ((ppl - referencePpl.Value) / referencePpl.Value) * 100d; - return delta.ToString("0.000"); + sb.AppendLine("
"); + sb.AppendLine($"{EscapeHtml(summary)}"); + sb.AppendLine(); + sb.AppendLine(body); + sb.AppendLine(); + sb.AppendLine("
"); } private static string ToGb(ulong bytes) => (bytes / 1024d / 1024d / 1024d).ToString("0.00"); private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); + private static string EscapeTooltip(string value) => (value ?? string.Empty).Replace("\"", """).Replace("|", " "); + private static string EscapeHtml(string value) => (value ?? string.Empty).Replace("&", "&").Replace("<", "<").Replace(">", ">"); } diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index f2d2066..aa7b38b 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -134,7 +134,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-Q4_K_XL.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M - display_name: UD_Q4_K_XL + display_name: UD-Q4_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true @@ -142,7 +142,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf baseline_family: Q5_K quantize_base_name: Q5_K - display_name: UD_Q5_K_XL + display_name: UD-Q5_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true @@ -150,7 +150,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf baseline_family: Q6_K quantize_base_name: Q6_K - display_name: UD_Q6_K_XL + display_name: UD-Q6_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: true allow_as_explicit_group_candidate: true @@ -158,7 +158,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-Q3_K_XL.gguf baseline_family: IQ3_S quantize_base_name: IQ3_S - display_name: UD_Q3_K_XL + display_name: UD-Q3_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true @@ -166,7 +166,7 @@ baselines: - file_name: Qwen3-4B-Instruct-2507-UD-IQ3_XXS.gguf baseline_family: IQ3_XS quantize_base_name: IQ3_XS - display_name: UD_IQ3_XXS + display_name: UD-IQ3_XXS allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true From f857a506a1827eb439a6cc6c891280f8c87b98e2 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 26 Apr 2026 14:56:49 -0400 Subject: [PATCH 127/258] lots of updates --- MQ.DB/Models/BaselineQuants.cs | 42 +++++++++++++++---- MQ.DB/Models/TensorWeightScheme.cs | 26 +++++++++++- .../Services/ReadmeGenerationService.cs | 30 ++++++++----- 3 files changed, 76 insertions(+), 22 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index a48fcc1..c0010bf 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -90,32 +90,51 @@ private static BaselineQuants Create( } public static readonly BaselineQuants Q8_0 = - Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 8, 11); + Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 8, 16); public static readonly BaselineQuants Q6_K = - Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, false, true, false, 6, 10); + Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, false, true, false, 6, 15); public static readonly BaselineQuants Q5_K = - Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, false, true, false, 5, 9); + Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, false, true, false, 5, 14); + public static readonly BaselineQuants Q5_K_S = + Create(13, false, "Q5_K_S", "Q5_K_S", TensorWeightScheme.Q5_K_S, [TensorWeightScheme.Q5_K_S], [TReg.MoeRouter.UniqueId], true, false, true, false, 5, 13); + + public static readonly BaselineQuants Q4_K_M = - Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 8); + Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 12); + + public static readonly BaselineQuants Q4_K_S = + Create(14, false, "Q4_K_S", "Q4_K_S", TensorWeightScheme.Q4_K_S, [TensorWeightScheme.Q4_K_S], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 11); + public static readonly BaselineQuants IQ4_NL = - Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 7); + Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 10); public static readonly BaselineQuants IQ4_XS = - Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 6); + Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 9); + + public static readonly BaselineQuants MXFP4_MOE = + Create(15, false, "MXFP4_MOE", "MXFP4_MOE", TensorWeightScheme.MXFP4, [TensorWeightScheme.MXFP4, TensorWeightScheme.IQ3_S, TensorWeightScheme.IQ3_XS], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 8); + public static readonly BaselineQuants IQ3_M = + Create(17, true, "IQ3_M", "IQ3_M", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 7); + + public static readonly BaselineQuants IQ3_S = - Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 5); + Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 6); public static readonly BaselineQuants IQ3_XS = - Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 4); + Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 5); public static readonly BaselineQuants IQ3_XXS = - Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 3); + Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 4); + + public static readonly BaselineQuants IQ2_M = + Create(16, true, "IQ2_M", "IQ2_M", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2, 3); + public static readonly BaselineQuants IQ2_S = Create(10, true, "IQ2_S", "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2, 2); @@ -136,12 +155,17 @@ private static BaselineQuants Create( Q8_0, Q6_K, Q5_K, + Q5_K_S, Q4_K_M, + Q4_K_S, IQ4_NL, IQ4_XS, + MXFP4_MOE, + IQ3_M, IQ3_S, IQ3_XS, IQ3_XXS, + IQ2_M, IQ2_S, IQ2_XS, IQ2_XXS diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 38ef6b4..ae14084 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -120,6 +120,9 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) public static readonly TensorWeightScheme Q5_K = new(5, false, ["Q5_K"], 256); + + public static readonly TensorWeightScheme Q5_K_S = + new(18, false, ["Q5_K_S"], 256); public static readonly TensorWeightScheme IQ4_XS = new(6, false, ["IQ4_XS"], 32); @@ -183,6 +186,23 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) 32 ); + public static readonly TensorWeightScheme Q4_K_S = + new( + 17, + false, + ["Q4_K_S"], + 32 + ); + + public static readonly TensorWeightScheme MXFP4 = + new( + 19, + false, + ["MXFP4"], + 32 + ); + + public static readonly TensorWeightScheme Q2_K = new( 13, @@ -203,7 +223,7 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) NULL, BF16, //F16, - //MXFP4, + MXFP4, Q8_0, Q6_K, Q5_K, @@ -215,7 +235,9 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) IQ2_S, IQ2_XS, IQ2_XXS, - Q4_K + Q4_K, + Q4_K_S, + Q5_K_S ]; // This is the true registry of everything known. diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 542ee00..1de38b4 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -27,19 +27,26 @@ public async Task GenerateAsync( .ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal); var sb = new StringBuilder(); - sb.AppendLine($"# MagicQuant Hybrids (v2.2) - {modelName}"); + sb.AppendLine($"# MagicQuant Hybrids (v2.0) - {modelName}"); sb.AppendLine(); sb.AppendLine("MagicQuant is **not** a quantization technique by itself."); sb.AppendLine(); - sb.AppendLine("It is a search, judging, and hybrid-discovery system that learns from baseline families such as llama.cpp and external/custom baseline sources, then uses isolated empirical truth, rank-safe prediction, and real benchmarking to keep the practical survivors."); + sb.AppendLine("It is a search, judging, and hybrid-discovery system that learns from baseline families such as llama.cpp and external/custom baseline sources, then uses isolated samples, rank-safe prediction, and real benchmarking to keep the practical survivors."); sb.AppendLine(); - sb.AppendLine("Sometimes a hybrid beats a pure baseline. Sometimes it does not. The point is to pay the real benchmarking cost only where the trade is genuinely worth keeping."); + sb.AppendLine("Sometimes a hybrid beats a pure baseline. Sometimes it does not. MagicQuant finds non linear good trades to discover potential better hybrids, good sub spaces between anchor baselines and more."); + sb.AppendLine(); + sb.AppendLine(); + sb.AppendLine("Read more on the [MagicQuant Wiki Here](https://github.com/magiccodingman/MagicQuant-Wiki)."); + sb.AppendLine("_The GitHub links is also a great place to make a request, bring up issues, share ideas, or anything else._"); + sb.AppendLine(); + sb.AppendLine("---"); sb.AppendLine(); - sb.AppendLine("## Final surviving downloadable outputs"); sb.AppendLine(); AppendDownloadTable(sb, exportedArtifacts, replacementMap, exportedByKey, namingContext); sb.AppendLine(); + sb.AppendLine("---"); + sb.AppendLine(); sb.AppendLine("## Release metadata"); sb.AppendLine(); @@ -47,10 +54,10 @@ public async Task GenerateAsync( sb.AppendLine("- [Hybrid tensor map](./../../resolve/main/magicquant.hybrid-map.json?download=true) — tensor-group assignments and effective-state details for MagicQuant hybrid GGUFs."); sb.AppendLine("- [Replacement details](./../../resolve/main/magicquant.replacements.json?download=true) — structured details for baselines or anchors removed from the final download table, including reason codes, KLD deltas, PPL delta %, and size deltas."); sb.AppendLine(); - AppendReasonCodeDetails(sb); + sb.AppendLine("---"); sb.AppendLine(); - - AppendCollapsible(sb, "Method note", "The final chooser uses rank-safe isolation prediction: Q8-carrier single-group isolation measurements provide the additive backbone, a low-bit interaction correction improves numeric KLD closeness, and an isotonic projection keeps the final predicted ordering monotone with the isolation backbone. Predicted candidates still have to validate against real benchmark truth before they can replace a baseline or remain as an interior hybrid."); + + AppendReasonCodeDetails(sb); sb.AppendLine(); AppendProviderCredits(sb, exportedArtifacts); @@ -59,11 +66,12 @@ public async Task GenerateAsync( AppendCollapsible(sb, "Warning", "External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing. That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency, not as a universal judgment of the original creator's exact release artifact."); sb.AppendLine(); - AppendCollapsible(sb, "Dive deeper", "Browse the project GitHub/Wiki for benchmark methodology, architecture notes, and planned pipeline improvements. If you spot a mistake, edge case, or a better practical trade, open an issue or share the artifact details so the comparison can be improved."); + sb.AppendLine("## Support"); + sb.AppendLine("I’m a solo developer working full time for myself to achieve my dream. I build open source code on the side. If you like any of my work, buying me a coffee is always appreciated. Otherwise, I hope you enjoy, maybe give me a star or something. Or just send me good vibes. Either way, thank you!"); sb.AppendLine(); - - AppendCollapsible(sb, "Support", "If this release helped you, a star, issue report, correction, or benchmark reproduction note is genuinely useful. Careful feedback matters more than hype, especially when a hybrid looks surprisingly good or surprisingly bad."); - + sb.AppendLine("[Click here to see ways to support](https://sayou.biz/support) - BTC, Paypal, GitHub sponsors."); + sb.AppendLine(); + await File.WriteAllTextAsync(readmePath, sb.ToString(), ct); AnsiConsole.MarkupLine($"[green]README generated:[/] {Markup.Escape(readmePath)}"); return readmePath; From e85b72364e7f37963af0f2241a067d1d6a24c3af Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 27 Apr 2026 12:27:25 -0400 Subject: [PATCH 128/258] new tensor groups method with YAML and exceptions --- MQ.DB/MQ.DB.csproj | 8 + MQ.DB/Models/TensorGroup.cs | 526 +++++++++++++++++++++++++----------- MQ.DB/tensor_groups.yaml | 259 ++++++++++++++++++ 3 files changed, 631 insertions(+), 162 deletions(-) create mode 100644 MQ.DB/tensor_groups.yaml diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj index e0c67e6..6e40d18 100644 --- a/MQ.DB/MQ.DB.csproj +++ b/MQ.DB/MQ.DB.csproj @@ -14,6 +14,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -21,4 +22,11 @@ + + + + Always + + + diff --git a/MQ.DB/Models/TensorGroup.cs b/MQ.DB/Models/TensorGroup.cs index 29216db..06f81dd 100644 --- a/MQ.DB/Models/TensorGroup.cs +++ b/MQ.DB/Models/TensorGroup.cs @@ -1,6 +1,7 @@ using System.Collections.Immutable; -using System.Linq; - +using System.Text.RegularExpressions; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; namespace MQ.DB.Models; @@ -11,6 +12,13 @@ public class TensorGroupInfo /// /// Represents a categorized group of tensors with a unique name and matching patterns. +/// +/// Important: +/// The group identity itself is intentionally owned by C#. +/// The regex patterns are loaded from tensor_groups.yaml. +/// +/// This keeps MagicQuant's benchmark identity stable while allowing tensor-name +/// matching rules to evolve without recompiling the application. /// public record TensorGroup(byte UniqueId, string Name, ImmutableArray Tensors) { @@ -33,177 +41,371 @@ public record TensorGroup(byte UniqueId, string Name, ImmutableArray Ten } /// -/// Tensor Registry +/// Tensor Registry. +/// +/// The semantic tensor groups are fixed here on purpose. +/// The matching regex patterns are loaded from tensor_groups.yaml and cached on first use. +/// +/// Design rule: +/// MagicQuant should not silently invent tensor-group behavior at runtime. +/// New groups should be deliberate architecture/benchmark decisions. +/// Pattern changes, however, are config/schema-level changes and belong in YAML. /// public static class TReg { - public static readonly TensorGroup Embeddings = new(0, "embeddings", [ - "token_embd\\.weight", - "model\\.embed_tokens\\.weight", - "embed_tokens\\.weight", - "tok_embeddings\\.weight", - "word_embeddings\\.weight", - "transformer\\.wte\\.weight", - "wte\\.weight" - ]); - - public static readonly TensorGroup LmHead = new(1, "lm_head", [ - "output\\.weight", - "lm_head\\.weight", - "final_logits_proj\\.weight", - "model\\.embed_out\\.weight", - "lm_head\\.decoder\\.weight" - ]); - - public static readonly TensorGroup AttnQ = new(2, "attn_q", [ - // Matches blk.0.attn_q.weight - "blk\\..*\\.attn_q\\.weight", - ".*q_proj.*weight", - ".*query\\.weight", - ".*self_attn\\.q_proj\\.weight", - ".*attention\\.self\\.query\\.weight", - ".*SelfAttention\\.q\\.weight", - ".*c_attn\\.weight", - ".*query_key_value\\.weight" - ]); - - public static readonly TensorGroup AttnKV = new(3, "attn_kv", [ - "blk\\..*\\.attn_k\\.weight", - "blk\\..*\\.attn_v\\.weight", - ".*k_proj.*weight", - ".*v_proj.*weight", - ".*key\\.weight", - ".*value\\.weight", - ".*self_attn\\.k_proj\\.weight", - ".*self_attn\\.v_proj\\.weight", - ".*attention\\.self\\.key\\.weight", - ".*attention\\.self\\.value\\.weight", - ".*SelfAttention\\.k\\.weight", - ".*SelfAttention\\.v\\.weight", - ".*EncDecAttention\\.k\\.weight", - ".*EncDecAttention\\.v\\.weight" - ]); - - public static readonly TensorGroup AttnOutput = new(4, "attn_output", [ - "blk\\..*\\.attn_output\\.weight", - ".*out_proj.*weight", - ".*o_proj.*weight", - ".*c_proj\\.weight", - ".*attention\\.output\\.dense\\.weight", - ".*self_attn\\.out_proj\\.weight", - ".*SelfAttention\\.o\\.weight", - ".*self_attention\\.dense\\.weight", - ".*attention\\.proj\\.weight" - ]); - - public static readonly TensorGroup FfnUpGate = new(5, "ffn_up_gate", [ - "blk\\..*\\.ffn_up\\.weight", - "blk\\..*\\.ffn_gate\\.weight", - - ".*intermediate\\.dense\\.weight", - ".*c_fc\\.weight", - ".*fc1\\.weight", - ".*fc_in\\.weight", - ".*dense_h_to_4h\\.weight", - ".*wi\\.weight", - ".*wi_0\\.weight", - ".*wi_1\\.weight", - ".*mlp\\.up_proj\\.weight", - ".*mlp\\.gate_proj\\.weight", - ".*DenseReluDense\\.wi_0\\.weight", - ".*DenseReluDense\\.wi_1\\.weight", - ".*experts.*wi_0\\.weight", - ".*experts.*wi_1\\.weight", - "blk\\..*\\.ffn_up_exps\\.weight", - "blk\\..*\\.ffn_gate_exps\\.weight", - - // Qwen3.5 MoE / modern expert forms - ".*mlp\\.experts\\.gate_up_proj.*", - ".*mlp\\.shared_expert\\.gate_proj\\.weight", - ".*mlp\\.shared_expert\\.up_proj\\.weight", - - // Gemma 4 MoE - ".*layers\\..*\\.experts\\.gate_up_proj.*" - ]); - - public static readonly TensorGroup FfnDown = new(6, "ffn_down", [ - "blk\\..*\\.ffn_down\\.weight", - ".*output\\.dense\\.weight", - ".*c_proj\\.weight", - ".*fc2\\.weight", - ".*fc_out\\.weight", - ".*wo\\.weight", - ".*dense_4h_to_h\\.weight", - ".*mlp\\.down_proj\\.weight", - ".*DenseReluDense\\.wo\\.weight", - ".*experts.*wo\\.weight", - "blk\\..*\\.ffn_down_exps\\.weight", - - // Qwen3.5 MoE / modern expert forms - ".*mlp\\.experts\\.down_proj.*", - ".*mlp\\.shared_expert\\.down_proj\\.weight", - - // Gemma 4 MoE - ".*layers\\..*\\.experts\\.down_proj.*" - ]); - - public static readonly TensorGroup MoeExperts = new(7, "moe_experts", [ - "blk\\..*\\.ffn_.*expert.*", - "blk\\..*\\.ffn_.*exps.*", - ".*experts?\\..*wi_0.*", - ".*experts?\\..*wi_1.*", - ".*experts?\\..*wo.*", - ".*experts?\\..*fc1.*", - ".*experts?\\..*fc2.*", - ".*experts?\\..*dense_h_to_4h.*", - ".*experts?\\..*dense_4h_to_h.*", - - // Qwen3.5 native HF MoE - ".*mlp\\.experts\\.gate_up_proj.*", - ".*mlp\\.experts\\.down_proj.*", - ".*mlp\\.shared_expert\\.gate_proj\\.weight", - ".*mlp\\.shared_expert\\.up_proj\\.weight", - ".*mlp\\.shared_expert\\.down_proj\\.weight", - - // Gemma 4 MoE - ".*layers\\..*\\.experts\\.gate_up_proj.*", - ".*layers\\..*\\.experts\\.down_proj.*" - ]); - - public static readonly TensorGroup MoeRouter = new(8, "moe_router", [ - "router.*", - "gating.*", - "routing.*", - ".*(? GroupCache = + new(StringComparer.OrdinalIgnoreCase); + + private static readonly Dictionary> GroupRegexCache = + new(StringComparer.OrdinalIgnoreCase); + + private static ImmutableArray? _baseQuantExceptionPatternsCache; + + private static ImmutableArray? _baseQuantExceptionRegexCache; + + /// + /// Future extension point. + /// + /// Right now this can remain null and the loader will resolve tensor_groups.yaml + /// from the application output directory. + /// + /// Later, CLI/config code can set this before first access if users provide + /// an override location. + /// + public static string? TensorGroupsYamlPathOverride { get; set; } + + public static TensorGroup Embeddings => GetRequiredGroup(0, "embeddings"); + + public static TensorGroup LmHead => GetRequiredGroup(1, "lm_head"); + + public static TensorGroup AttnQ => GetRequiredGroup(2, "attn_q"); + + public static TensorGroup AttnKV => GetRequiredGroup(3, "attn_kv"); + + public static TensorGroup AttnOutput => GetRequiredGroup(4, "attn_output"); + + public static TensorGroup FfnUpGate => GetRequiredGroup(5, "ffn_up_gate"); + + public static TensorGroup FfnDown => GetRequiredGroup(6, "ffn_down"); + + public static TensorGroup MoeExperts => GetRequiredGroup(7, "moe_experts"); + + public static TensorGroup MoeRouter => GetRequiredGroup(8, "moe_router"); /// /// Provides a complete list of all registered tensor groups. + /// + /// This remains fixed by design. The regex patterns inside each group come + /// from tensor_groups.yaml. /// - public static readonly ImmutableArray All = + public static ImmutableArray All => [ - Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, - FfnUpGate, FfnDown, MoeExperts, MoeRouter + Embeddings, + LmHead, + AttnQ, + AttnKV, + AttnOutput, + FfnUpGate, + FfnDown, + MoeExperts, + MoeRouter ]; /// - /// Look up a group by its string name (useful when parsing external configs). + /// Look up a group by its string name, useful when parsing external configs. /// public static TensorGroup? GetByName(string name) => - All.FirstOrDefault(g => g.Name.Equals(name, System.StringComparison.OrdinalIgnoreCase)); + All.FirstOrDefault(g => g.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + + /// + /// Gets compiled regexes for a semantic tensor group. + /// + /// This is useful when matching many tensors repeatedly and avoids recompiling + /// the same patterns over and over. + /// + public static ImmutableArray GetRegexesForGroup(TensorGroup group) + { + lock (CacheLock) + { + if (GroupRegexCache.TryGetValue(group.Name, out var cached)) + return cached; + + var regexes = group.Tensors + .Select(CreateRegex) + .ToImmutableArray(); + + GroupRegexCache[group.Name] = regexes; + return regexes; + } + } + + /// + /// Gets regex patterns for tensors that are explicitly allowed to fall back to BaseQuant. + /// + /// These are not semantic tensor groups. They are only checked after a tensor fails to + /// match any registered semantic group. + /// + public static ImmutableArray GetBaseQuantExceptionPatterns() + { + lock (CacheLock) + { + if (_baseQuantExceptionPatternsCache is not null) + return _baseQuantExceptionPatternsCache.Value; + + var yaml = LoadYamlIfNeeded(); + + var patterns = yaml.BaseQuantExceptions?.Patterns ?? []; + + var cleanedPatterns = patterns + .Where(p => !string.IsNullOrWhiteSpace(p)) + .Select(p => p.Trim()) + .Distinct(StringComparer.Ordinal) + .ToImmutableArray(); + + _baseQuantExceptionPatternsCache = cleanedPatterns; + return cleanedPatterns; + } + } + + /// + /// Gets compiled regexes for tensors that are explicitly allowed to fall back to BaseQuant. + /// + public static ImmutableArray GetBaseQuantExceptionRegexes() + { + lock (CacheLock) + { + if (_baseQuantExceptionRegexCache is not null) + return _baseQuantExceptionRegexCache.Value; + + var regexes = GetBaseQuantExceptionPatterns() + .Select(CreateRegex) + .ToImmutableArray(); + + _baseQuantExceptionRegexCache = regexes; + return regexes; + } + } + + /// + /// Returns true when the tensor is explicitly allowed to remain outside all semantic + /// tensor groups and fall back to the artifact BaseQuant. + /// + /// Important: + /// This should only be called after normal group matching returns zero matches. + /// It must not be used to resolve group collisions. + /// + public static bool IsBaseQuantException(string tensorName) + { + foreach (var regex in GetBaseQuantExceptionRegexes()) + { + if (regex.IsMatch(tensorName)) + return true; + } + + return false; + } + + /// + /// Finds all semantic tensor groups that match the provided tensor name. + /// + /// If this returns: + /// - 0 groups: caller may then check IsBaseQuantException. + /// - 1 group: tensor is safely categorized. + /// - 2+ groups: caller should treat this as an ambiguity/collision error. + /// + public static ImmutableArray FindMatchingGroups(string tensorName) + { + var matches = ImmutableArray.CreateBuilder(); + + foreach (var group in All) + { + var regexes = GetRegexesForGroup(group); + + foreach (var regex in regexes) + { + if (!regex.IsMatch(tensorName)) + continue; + + matches.Add(group); + break; + } + } + + return matches.ToImmutable(); + } + + /// + /// Clears the loaded YAML and materialized group/regex caches. + /// + /// This is mainly useful for tests or future reload behavior. + /// Normal production runs should not need to call this. + /// + public static void ClearCache() + { + lock (CacheLock) + { + _yamlCache = null; + GroupCache.Clear(); + GroupRegexCache.Clear(); + _baseQuantExceptionPatternsCache = null; + _baseQuantExceptionRegexCache = null; + } + } + + private static TensorGroup GetRequiredGroup(byte uniqueId, string name) + { + lock (CacheLock) + { + if (GroupCache.TryGetValue(name, out var cached)) + return cached; + + var yaml = LoadYamlIfNeeded(); + + if (yaml.Groups is null || yaml.Groups.Count == 0) + { + throw new InvalidOperationException( + $"Tensor group YAML did not define any groups. File: {ResolveTensorGroupsYamlPath()}"); + } + + if (!yaml.Groups.TryGetValue(name, out var groupDef)) + { + throw new InvalidOperationException( + $"Required tensor group '{name}' was not found in {ResolveTensorGroupsYamlPath()}."); + } + + if (groupDef.Patterns is null || groupDef.Patterns.Count == 0) + { + throw new InvalidOperationException( + $"Tensor group '{name}' exists in {ResolveTensorGroupsYamlPath()}, but it has no patterns."); + } + + var cleanedPatterns = groupDef.Patterns + .Where(p => !string.IsNullOrWhiteSpace(p)) + .Select(p => p.Trim()) + .Distinct(StringComparer.Ordinal) + .ToImmutableArray(); + + if (cleanedPatterns.Length == 0) + { + throw new InvalidOperationException( + $"Tensor group '{name}' exists in {ResolveTensorGroupsYamlPath()}, but all patterns were empty."); + } + + var group = new TensorGroup(uniqueId, name, cleanedPatterns); + GroupCache[name] = group; + + return group; + } + } + + private static TensorGroupYamlFile LoadYamlIfNeeded() + { + if (_yamlCache is not null) + return _yamlCache; + + var path = ResolveTensorGroupsYamlPath(); + + if (!File.Exists(path)) + { + throw new FileNotFoundException( + $"Could not find {DefaultYamlFileName}. Expected it at: {path}", + path); + } + + var yamlText = File.ReadAllText(path); + + if (string.IsNullOrWhiteSpace(yamlText)) + { + throw new InvalidOperationException( + $"Tensor group YAML file is empty: {path}"); + } + + var deserializer = new DeserializerBuilder() + .WithNamingConvention(UnderscoredNamingConvention.Instance) + .IgnoreUnmatchedProperties() + .Build(); + + TensorGroupYamlFile? parsed; + + try + { + parsed = deserializer.Deserialize(yamlText); + } + catch (Exception ex) + { + throw new InvalidOperationException( + $"Failed to parse tensor group YAML file: {path}", + ex); + } + + if (parsed is null) + { + throw new InvalidOperationException( + $"Tensor group YAML parsed to null: {path}"); + } + + if (parsed.SchemaVersion <= 0) + { + throw new InvalidOperationException( + $"Tensor group YAML must define a positive schema_version. File: {path}"); + } + + _yamlCache = parsed; + return _yamlCache; + } + + private static string ResolveTensorGroupsYamlPath() + { + if (!string.IsNullOrWhiteSpace(TensorGroupsYamlPathOverride)) + return Path.GetFullPath(TensorGroupsYamlPathOverride); + + return Path.Combine(AppContext.BaseDirectory, DefaultYamlFileName); + } + + private static Regex CreateRegex(string pattern) + { + try + { + return new Regex( + pattern, + RegexOptions.Compiled | + RegexOptions.CultureInvariant); + } + catch (Exception ex) + { + throw new InvalidOperationException( + $"Invalid tensor group regex pattern: {pattern}", + ex); + } + } + + private sealed class TensorGroupYamlFile + { + public int SchemaVersion { get; set; } + + public Dictionary Groups { get; set; } = + new(StringComparer.OrdinalIgnoreCase); + + public BaseQuantExceptionYamlDefinition? BaseQuantExceptions { get; set; } + } + + private sealed class TensorGroupYamlDefinition + { + public string? Description { get; set; } + + public List Patterns { get; set; } = []; + } + + private sealed class BaseQuantExceptionYamlDefinition + { + public string? Description { get; set; } + + public List Patterns { get; set; } = []; + } } \ No newline at end of file diff --git a/MQ.DB/tensor_groups.yaml b/MQ.DB/tensor_groups.yaml new file mode 100644 index 0000000..81b6214 --- /dev/null +++ b/MQ.DB/tensor_groups.yaml @@ -0,0 +1,259 @@ +schema_version: 1 + +groups: + embeddings: + description: "Token embedding matrices." + patterns: + - "token_embd\\.weight" + - "model\\.embed_tokens\\.weight" + - "embed_tokens\\.weight" + - "tok_embeddings\\.weight" + - "word_embeddings\\.weight" + - "transformer\\.wte\\.weight" + - "wte\\.weight" + + lm_head: + description: "Final output/logit projection tensors." + patterns: + - "output\\.weight" + - "lm_head\\.weight" + - "final_logits_proj\\.weight" + - "model\\.embed_out\\.weight" + - "lm_head\\.decoder\\.weight" + + attn_q: + description: "Attention query projection tensors." + patterns: + # llama.cpp GGUF form, e.g. blk.0.attn_q.weight + - "blk\\..*\\.attn_q\\.weight" + + # LLaMA / Qwen / Mistral / modern HF forms. + - ".*q_proj.*weight" + - ".*self_attn\\.q_proj\\.weight" + + # BERT / encoder style forms. + - ".*query\\.weight" + - ".*attention\\.self\\.query\\.weight" + + # T5 / miscellaneous forms. + - ".*SelfAttention\\.q\\.weight" + + # Fused QKV forms. + # These may intentionally match broader attention tensors and should be + # handled carefully by the learning/ambiguity layer. + - ".*c_attn\\.weight" + - ".*query_key_value\\.weight" + + attn_kv: + description: "Attention key/value projection tensors." + patterns: + # llama.cpp GGUF forms. + - "blk\\..*\\.attn_k\\.weight" + - "blk\\..*\\.attn_v\\.weight" + + # LLaMA / Qwen / Mistral / modern HF forms. + - ".*k_proj.*weight" + - ".*v_proj.*weight" + - ".*self_attn\\.k_proj\\.weight" + - ".*self_attn\\.v_proj\\.weight" + + # BERT / encoder style forms. + - ".*key\\.weight" + - ".*value\\.weight" + - ".*attention\\.self\\.key\\.weight" + - ".*attention\\.self\\.value\\.weight" + + # T5 / encoder-decoder style forms. + - ".*SelfAttention\\.k\\.weight" + - ".*SelfAttention\\.v\\.weight" + - ".*EncDecAttention\\.k\\.weight" + - ".*EncDecAttention\\.v\\.weight" + + attn_output: + description: "Attention output projection tensors." + patterns: + # llama.cpp GGUF form. + - "blk\\..*\\.attn_output\\.weight" + + # LLaMA / Qwen / Mistral / modern HF forms. + - ".*out_proj.*weight" + - ".*o_proj.*weight" + - ".*self_attn\\.out_proj\\.weight" + + # GPT-style attention output. + # Note: c_proj can also appear in MLPs on some architectures, so this may + # require architecture-aware disambiguation or strict ambiguity reporting. + - ".*c_proj\\.weight" + + # BERT / encoder style forms. + - ".*attention\\.output\\.dense\\.weight" + - ".*self_attention\\.dense\\.weight" + - ".*attention\\.proj\\.weight" + + # T5 / miscellaneous forms. + - ".*SelfAttention\\.o\\.weight" + + ffn_up_gate: + description: "Dense FFN up/gate tensors. Expert-path tensors should generally be owned by moe_experts." + patterns: + # llama.cpp GGUF dense FFN forms. + - "blk\\..*\\.ffn_up\\.weight" + - "blk\\..*\\.ffn_gate\\.weight" + + # BERT / encoder style forms. + - ".*intermediate\\.dense\\.weight" + + # GPT / MLP style forms. + - ".*c_fc\\.weight" + - ".*fc1\\.weight" + - ".*fc_in\\.weight" + - ".*dense_h_to_4h\\.weight" + + # T5 / gated FFN style forms. + - ".*wi\\.weight" + - ".*wi_0\\.weight" + - ".*wi_1\\.weight" + - ".*DenseReluDense\\.wi_0\\.weight" + - ".*DenseReluDense\\.wi_1\\.weight" + + # LLaMA / Qwen / Mistral / modern dense MLP forms. + - ".*mlp\\.up_proj\\.weight" + - ".*mlp\\.gate_proj\\.weight" + + # Qwen3.5 / Qwen3.6 / modern expert forms. + # These were added for newer MoE architectures, but if strict collision + # detection is enabled, moe_experts should usually own these instead. + - ".*experts.*wi_0\\.weight" + - ".*experts.*wi_1\\.weight" + - "blk\\..*\\.ffn_up_exps\\.weight" + - "blk\\..*\\.ffn_gate_exps\\.weight" + - ".*mlp\\.experts\\.gate_up_proj.*" + - ".*mlp\\.shared_expert\\.gate_proj\\.weight" + - ".*mlp\\.shared_expert\\.up_proj\\.weight" + + # Gemma-style MoE forms. + - ".*layers\\..*\\.experts\\.gate_up_proj.*" + + ffn_down: + description: "Dense FFN down-projection tensors. Expert-path tensors should generally be owned by moe_experts." + patterns: + # llama.cpp GGUF dense FFN form. + - "blk\\..*\\.ffn_down\\.weight" + + # BERT / encoder style form. + - ".*output\\.dense\\.weight" + + # GPT / MLP style forms. + # Note: c_proj can also appear as attention output on some architectures. + # Let the matching layer report ambiguity if the model family cannot + # distinguish it cleanly. + - ".*c_proj\\.weight" + - ".*fc2\\.weight" + - ".*fc_out\\.weight" + - ".*dense_4h_to_h\\.weight" + + # T5 / gated FFN style forms. + - ".*wo\\.weight" + - ".*DenseReluDense\\.wo\\.weight" + + # LLaMA / Qwen / Mistral / modern dense MLP form. + - ".*mlp\\.down_proj\\.weight" + + # Qwen3.5 / Qwen3.6 / modern expert forms. + # These were added for newer MoE architectures, but if strict collision + # detection is enabled, moe_experts should usually own these instead. + - ".*experts.*wo\\.weight" + - "blk\\..*\\.ffn_down_exps\\.weight" + - ".*mlp\\.experts\\.down_proj.*" + - ".*mlp\\.shared_expert\\.down_proj\\.weight" + + # Gemma-style MoE forms. + - ".*layers\\..*\\.experts\\.down_proj.*" + + moe_experts: + description: "MoE expert-path tensors, including shared experts." + patterns: + # llama.cpp GGUF MoE expert tensors. + # These intentionally own the *_exps forms so they do not collide with + # dense ffn_up_gate / ffn_down groups. + - "blk\\..*\\.ffn_.*expert.*" + - "blk\\..*\\.ffn_.*exps.*" + - "blk\\..*\\.ffn_up_exps\\.weight" + - "blk\\..*\\.ffn_gate_exps\\.weight" + - "blk\\..*\\.ffn_down_exps\\.weight" + + # Generic expert container forms used by several HF architectures. + - ".*experts?\\..*wi_0.*" + - ".*experts?\\..*wi_1.*" + - ".*experts?\\..*wo.*" + - ".*experts?\\..*fc1.*" + - ".*experts?\\..*fc2.*" + - ".*experts?\\..*dense_h_to_4h.*" + - ".*experts?\\..*dense_4h_to_h.*" + - ".*experts?\\..*up_proj.*" + - ".*experts?\\..*gate_proj.*" + - ".*experts?\\..*down_proj.*" + + # Qwen3.5 / Qwen3.6 / modern HF MoE forms. + - ".*mlp\\.experts\\.gate_up_proj.*" + - ".*mlp\\.experts\\.gate_proj.*" + - ".*mlp\\.experts\\.up_proj.*" + - ".*mlp\\.experts\\.down_proj.*" + + # Shared experts are still MoE expert-path tensors, not normal dense FFN. + # Keeping them here prevents them from double-counting as generic FFN. + - ".*mlp\\.shared_expert\\.gate_proj\\.weight" + - ".*mlp\\.shared_expert\\.up_proj\\.weight" + - ".*mlp\\.shared_expert\\.down_proj\\.weight" + + # Gemma-style MoE forms. + - ".*layers\\..*\\.experts\\.gate_up_proj.*" + - ".*layers\\..*\\.experts\\.gate_proj.*" + - ".*layers\\..*\\.experts\\.up_proj.*" + - ".*layers\\..*\\.experts\\.down_proj.*" + + moe_router: + description: "MoE router/gating tensors." + patterns: + - "router.*" + - "gating.*" + - "routing.*" + + # Generic gate/router forms. + # Negative lookbehind prevents ffn_gate.weight from being treated as router. + - ".*(? Date: Mon, 27 Apr 2026 12:28:39 -0400 Subject: [PATCH 129/258] tensor weights adding MXFP4 --- MQ.DB/Models/TensorWeightScheme.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index ae14084..29669a6 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -223,12 +223,12 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) NULL, BF16, //F16, - MXFP4, Q8_0, Q6_K, Q5_K, IQ4_XS, IQ4_NL, + MXFP4, IQ3_S, IQ3_XS, IQ3_XXS, @@ -247,7 +247,7 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) BF16, F16, F32, - //MXFP4, + MXFP4, Q8_0, Q6_K, Q5_K, From 08edc8b04f1dffcb728b1e165a75dd76080edc56 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 27 Apr 2026 12:29:01 -0400 Subject: [PATCH 130/258] better imatrix support, cloning configuration started, and read me generation changes --- MQ.DB/Cache.cs | 15 +- MQ.DB/Data/MagicQuantContext.cs | 74 ++ ...8_EnforceImatrixExactOwnership.Designer.cs | 753 ++++++++++++++++++ ...0426194528_EnforceImatrixExactOwnership.cs | 22 + MagicQuant/Commands/CloneRepositoryQuants.cs | 310 +++++++ MagicQuant/Models/RepositoryCloneModels.cs | 50 ++ MagicQuant/Program.cs | 23 +- .../Services/ArchitectureFamilyService.cs | 33 +- MagicQuant/Services/BenchmarkService.cs | 133 +++- .../CloneConfigManifestGenerationService.cs | 115 +++ .../Services/CloneReadmeGenerationService.cs | 75 ++ .../CombinationSurvivalPipelineService.cs | 10 +- .../Services/HuggingFaceBaselineService.cs | 97 +++ MagicQuant/Services/ImatrixIdentityService.cs | 34 +- MagicQuant/Services/ImatrixService.cs | 12 +- .../Services/IsolationOptimizationService.cs | 8 +- MagicQuant/Services/QuantizationService.cs | 223 +++++- .../Services/ReadmeGenerationService.cs | 3 +- .../RepositoryCloneManifestService.cs | 120 +++ MagicQuant/config.dev.yaml | 164 +++- 20 files changed, 2190 insertions(+), 84 deletions(-) create mode 100644 MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.Designer.cs create mode 100644 MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.cs create mode 100644 MagicQuant/Commands/CloneRepositoryQuants.cs create mode 100644 MagicQuant/Models/RepositoryCloneModels.cs create mode 100644 MagicQuant/Services/CloneConfigManifestGenerationService.cs create mode 100644 MagicQuant/Services/CloneReadmeGenerationService.cs create mode 100644 MagicQuant/Services/RepositoryCloneManifestService.cs diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index 7c636ed..1d0590b 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -97,8 +97,21 @@ public enum MainTorchType public static string? ActiveImatrixIdentityHash { get; set; } + /// + /// When false, long-running llama.cpp child processes write their full stdout/stderr + /// to log files only. This keeps the CLI readable during large export/clone runs. + /// + public static bool VerboseProcessOutput { get; set; } + + /// + /// Clone/export-only flows may benchmark for release metadata without polluting the + /// learning/evolution SQLite truth tables. + /// + public static bool SuppressBenchmarkPersistence { get; set; } + + /// /// Final export/output directory for selected survivor artifacts. /// public static string? OutputDirectory { get; set; } -} +} \ No newline at end of file diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index e03ebb4..57ddba8 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -207,6 +207,80 @@ private static bool IsDesignTime() public DbSet ArchitectureFamilies { get; set; } public DbSet ArchitectureFamilyModelHashes { get; set; } + + // -------------------------------------------------------- + // Imatrix Ownership Guard + // -------------------------------------------------------- + + public override int SaveChanges() + { + ValidateImatrixOwnershipBeforeSaveAsync(CancellationToken.None).GetAwaiter().GetResult(); + return base.SaveChanges(); + } + + public override int SaveChanges(bool acceptAllChangesOnSuccess) + { + ValidateImatrixOwnershipBeforeSaveAsync(CancellationToken.None).GetAwaiter().GetResult(); + return base.SaveChanges(acceptAllChangesOnSuccess); + } + + public override async Task SaveChangesAsync(CancellationToken cancellationToken = default) + { + await ValidateImatrixOwnershipBeforeSaveAsync(cancellationToken); + return await base.SaveChangesAsync(cancellationToken); + } + + public override async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) + { + await ValidateImatrixOwnershipBeforeSaveAsync(cancellationToken); + return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); + } + + private async Task ValidateImatrixOwnershipBeforeSaveAsync(CancellationToken ct) + { + var pairs = ChangeTracker.Entries() + .Where(e => e.State is EntityState.Added or EntityState.Modified) + .Select(e => e.Entity) + .Select(entity => entity switch + { + AiBenchmark x => (EntityName: nameof(AiBenchmark), x.AiModelHashId, x.ImatrixDefinitionId), + BenchmarkRun x => (EntityName: nameof(BenchmarkRun), x.AiModelHashId, x.ImatrixDefinitionId), + QuantizationRun x => (EntityName: nameof(QuantizationRun), x.AiModelHashId, x.ImatrixDefinitionId), + ExecutionPlanProbeCache x => (EntityName: nameof(ExecutionPlanProbeCache), x.AiModelHashId, x.ImatrixDefinitionId), + _ => default + }) + .Where(x => !string.IsNullOrWhiteSpace(x.EntityName) && x.ImatrixDefinitionId.HasValue) + .Distinct() + .ToList(); + + if (pairs.Count == 0) + return; + + var ids = pairs + .Select(x => x.ImatrixDefinitionId!.Value) + .Distinct() + .ToList(); + + var owners = await ImatrixDefinitions + .AsNoTracking() + .Where(x => ids.Contains(x.Id)) + .Select(x => new { x.Id, x.AiModelHashId }) + .ToDictionaryAsync(x => x.Id, x => x.AiModelHashId, ct); + + foreach (var pair in pairs) + { + if (!owners.TryGetValue(pair.ImatrixDefinitionId!.Value, out var ownerHashId) || + ownerHashId != pair.AiModelHashId) + { + throw new InvalidOperationException( + $"{pair.EntityName} attempted to save AiModelHashId={pair.AiModelHashId} with " + + $"ImatrixDefinitionId={pair.ImatrixDefinitionId.Value}, but that imatrix belongs to " + + $"AiModelHashId={(owners.TryGetValue(pair.ImatrixDefinitionId.Value, out var found) ? found.ToString() : "missing")}. " + + "ImatrixDefinition ownership is exact-model-hash scoped."); + } + } + } + // -------------------------------------------------------- // Configuration // -------------------------------------------------------- diff --git a/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.Designer.cs b/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.Designer.cs new file mode 100644 index 0000000..9345ece --- /dev/null +++ b/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.Designer.cs @@ -0,0 +1,753 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260426194528_EnforceImatrixExactOwnership")] + partial class EnforceImatrixExactOwnership + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("NormalizedName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("TensorCount") + .HasColumnType("INTEGER"); + + b.Property("TensorSignatureHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique(); + + b.HasIndex("TensorSignatureHash", "TensorCount"); + + b.ToTable("ArchitectureFamilies"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IsCanonical") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "AiModelHashId") + .IsUnique(); + + b.ToTable("ArchitectureFamilyModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("BitRange") + .HasColumnType("INTEGER"); + + b.Property("CanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ExplicitCandidateSortOrder") + .HasColumnType("INTEGER"); + + b.Property("IsCombinationCarrierCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsCustomBaseline") + .HasColumnType("INTEGER"); + + b.Property("IsExplicitGroupCombinationCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsLearningBaseline") + .HasColumnType("INTEGER"); + + b.Property("QuantizeBaseArgumentName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RequiresImatrix") + .HasColumnType("INTEGER"); + + b.Property("ShortSourceName") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceOwner") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("SourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("CanonicalKey") + .IsUnique(); + + b.HasIndex("SourceRepository", "SourceFileName"); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.cs b/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.cs new file mode 100644 index 0000000..9d5420d --- /dev/null +++ b/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class EnforceImatrixExactOwnership : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs new file mode 100644 index 0000000..0740235 --- /dev/null +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -0,0 +1,310 @@ +using System.Text.Json; +using MagicQuant.Configuration; +using MagicQuant.Helpers; +using MagicQuant.Models; +using MagicQuant.Services; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Commands; + +public sealed class CloneRepositoryQuants : ICommand +{ + private static readonly string[] ModelAdjacentFiles = + [ + "generation_config.json", + "config.json", + "chat_template.jinja", + "added_tokenizer.json", + "LICENSE", + "merges.txt", + "model.safetensors.index.json", + "tokenizer.json", + "tokenizer_config.json", + "vocab.json" + ]; + + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = true + }; + + public async Task Run(List args) + { + if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) + { + ShowHelp(); + return; + } + + string? modelDirRaw = Get(args, "model-dir"); + if (string.IsNullOrWhiteSpace(modelDirRaw)) + modelDirRaw = Config.Current.Paths.ModelDir; + + if (string.IsNullOrWhiteSpace(modelDirRaw)) + throw new InvalidOperationException("Clone mode requires --model-dir or paths.model_dir in YAML."); + + string fullModelPath = Path.GetFullPath(modelDirRaw); + if (!Directory.Exists(fullModelPath)) + throw new DirectoryNotFoundException($"Model directory does not exist: {fullModelPath}"); + + var safeTensorFiles = Directory.GetFiles(fullModelPath, "*.safetensors", SearchOption.TopDirectoryOnly); + if (safeTensorFiles.Length == 0) + throw new InvalidOperationException($"No .safetensors files were found in model directory: {fullModelPath}"); + + Cache.ModelDirectory = fullModelPath; + Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + Cache.ForceRelearnBaselineTensorMappings = false; + Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; + Cache.UseImatrix = Config.Current.Flags.UseImatrix; + Cache.ForceImatrixRebuild = Config.Current.Flags.ForceImatrixRebuild; + Cache.SuppressBenchmarkPersistence = true; + + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(false); + RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; + + JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); + Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); + + Cache.OutputDirectory = ResolveAndValidateOutputDirectory(args); + + AnsiConsole.Write(new Rule("[yellow]Repository Quant Clone Mode[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"Model Path: [blue]{Markup.Escape(Cache.ModelDirectory)}[/]"); + AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); + AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); + + Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); + AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); + + await EnsureSqliteReadyAsync(); + + var pyManager = new PythonManager(Cache.MagicQuantDirectory!); + var hf = new HuggingFaceBaselineService(pyManager); + var manifestService = new RepositoryCloneManifestService(hf); + + string? sourceRepo = Get(args, "source-repo") ?? Get(args, "clone-repo"); + string? sourceJson = Get(args, "source-json") ?? Get(args, "clone-json"); + + var (manifest, manifestLocalPath, sourceDescription) = await manifestService.ResolveAsync( + sourceRepo, + sourceJson, + Cache.ModelMagicQuantDirectory!, + CancellationToken.None); + + var benchmarkService = new BenchmarkService(pyManager); + var quantizationService = new QuantizationService(benchmarkService); + var imatrixService = new ImatrixService(); + + string baseModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); + + var architectureFamilyService = new ArchitectureFamilyService(pyManager); + await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(baseModelGgufPath); + + var imatrixRequest = new ImatrixRequest + { + UseImatrix = Cache.UseImatrix, + ForceRebuild = Cache.ForceImatrixRebuild, + ImatrixUrl = Config.Current.Imatrix.ImatrixUrl, + DatasetRepo = Config.Current.Imatrix.DatasetRepo, + DatasetSplit = Config.Current.Imatrix.DatasetSplit, + DatasetConfig = Config.Current.Imatrix.DatasetConfig, + LocalDatasetFile = Config.Current.Imatrix.DatasetLocalFile, + ModelDirectory = Cache.ModelDirectory!, + MagicQuantDirectory = Cache.ModelMagicQuantDirectory! + }; + + var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest); + RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Available); + + await CleanOutputDirectoryAsync(Cache.OutputDirectory!); + + // Always place the clone source manifest in the output, but stamp it with this clone source. + manifest.SourceRepository = sourceRepo; + manifest.SourceJson = string.IsNullOrWhiteSpace(sourceRepo) ? sourceDescription : manifest.SourceJson; + await File.WriteAllTextAsync( + Path.Combine(Cache.OutputDirectory!, CloneConfigManifestGenerationService.FileName), + JsonSerializer.Serialize(manifest, JsonOptions)); + + string q8Path = await quantizationService.EnsurePureQ8ModelAsync(); + await benchmarkService.EnsureExecutionPlanAsync( + q8ModelPath: q8Path, + discoveryTokenTarget: 8192, + quantizationKey: "Q8_0", + forceRediscovery: Cache.ForceRefreshHardwareProbe); + + var q8Reference = await benchmarkService.RunAllBenchmarksAsync( + quantConfig: HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), + modelPath: q8Path, + benchDir: Path.Combine(Cache.ModelMagicQuantDirectory!, "CloneBenchmarks", "_reference_q8"), + domainsOverride: new[] { "general" }); + + double? referencePpl = q8Reference.Perplexity.TryGetValue("general", out var q8Ppl) && q8Ppl.Ppl > 0 + ? q8Ppl.Ppl + : null; + + var records = new List(); + + foreach (var artifact in manifest.Artifacts) + { + string outputFile = Path.Combine(Cache.OutputDirectory!, artifact.FileName); + string baseQuantName = string.IsNullOrWhiteSpace(artifact.BaseQuant) + ? artifact.QuantFamily + : artifact.BaseQuant; + + AnsiConsole.Write(new Rule($"[yellow]Clone Artifact: {Markup.Escape(artifact.FileName)}[/]") { Justification = Justify.Left }); + + await quantizationService.BuildExportArtifactFromExactTensorMapAsync( + tensorTypes: artifact.TensorTypes, + outputPath: outputFile, + baseQuantName: baseQuantName, + forceRebuild: true); + + var quantForBenchmark = HybridQuant.CreatePureBaseline( + BaselineQuants.ResolveBuiltInStandardBaseline(baseQuantName) + ?? BaselineQuants.ResolveBuiltInStandardBaseline(artifact.QuantFamily) + ?? BaselineQuants.Q8_0); + + var bench = await benchmarkService.RunAllBenchmarksAsync( + quantConfig: quantForBenchmark, + modelPath: outputFile, + benchDir: Path.Combine(Cache.ModelMagicQuantDirectory!, "CloneBenchmarks", Path.GetFileNameWithoutExtension(artifact.FileName)), + domainsOverride: new[] { "general" }); + + var general = bench.Perplexity.TryGetValue("general", out var ppl) ? ppl : null; + + records.Add(new CloneArtifactBuildRecord + { + ManifestArtifact = artifact, + OutputPath = outputFile, + ActualSizeBytes = File.Exists(outputFile) ? (ulong)new FileInfo(outputFile).Length : 0UL, + Kld = general?.Kld, + Ppl = general?.Ppl, + PplDeltaPercent = general != null && referencePpl is > 0d + ? FinalReleaseMetadataService.CalculatePplDeltaPercent(general.Ppl, referencePpl) + : null + }); + } + + await CopyModelAdjacentFilesAsync(Cache.OutputDirectory!); + await CopyImatrixArtifactsAsync(Cache.OutputDirectory!); + + await new CloneReadmeGenerationService().GenerateAsync( + Cache.OutputDirectory!, + new DirectoryInfo(Cache.ModelDirectory!).Name, + sourceDescription, + !string.IsNullOrWhiteSpace(sourceRepo), + records); + + await WriteCloneBenchmarkSummaryAsync(Cache.OutputDirectory!, records); + + AnsiConsole.MarkupLine("[bold green]Repository quant clone complete.[/]"); + } + + private static async Task WriteCloneBenchmarkSummaryAsync(string outputDirectory, IReadOnlyCollection records) + { + var payload = records + .OrderBy(x => x.Kld ?? double.MaxValue) + .ThenBy(x => x.ActualSizeBytes) + .Select(x => new + { + fileName = x.ManifestArtifact.FileName, + displayName = x.ManifestArtifact.DisplayName, + provider = x.ManifestArtifact.Provider, + quantFamily = x.ManifestArtifact.QuantFamily, + baseQuant = x.ManifestArtifact.BaseQuant, + kld = x.Kld, + ppl = x.Ppl, + pplDeltaPercent = x.PplDeltaPercent, + sizeBytes = x.ActualSizeBytes, + sizeGiB = x.ActualSizeBytes / 1024d / 1024d / 1024d, + sourceKld = x.ManifestArtifact.SourceKld, + sourcePpl = x.ManifestArtifact.SourcePpl, + sourceSizeBytes = x.ManifestArtifact.SourceSizeBytes + }) + .ToList(); + + string path = Path.Combine(outputDirectory, "magicquant.clone-benchmarks.json"); + await File.WriteAllTextAsync(path, JsonSerializer.Serialize(payload, JsonOptions)); + AnsiConsole.MarkupLine($"[green]Clone benchmark summary generated:[/] {Markup.Escape(path)}"); + } + + private static async Task CopyModelAdjacentFilesAsync(string outputDirectory) + { + foreach (var fileName in ModelAdjacentFiles) + { + string source = Path.Combine(Cache.ModelDirectory!, fileName); + string target = Path.Combine(outputDirectory, fileName); + + if (!File.Exists(source)) + continue; + + File.Copy(source, target, overwrite: true); + await Task.Yield(); + AnsiConsole.MarkupLine($"[green]Copied model-adjacent file:[/] {Markup.Escape(fileName)}"); + } + } + + private static Task CopyImatrixArtifactsAsync(string outputDirectory) + { + if (!Cache.IsImatrixAvailable || string.IsNullOrWhiteSpace(Cache.ActiveImatrixPath)) + return Task.CompletedTask; + + string target = Path.Combine(outputDirectory, "imatrix.dat"); + File.Copy(Cache.ActiveImatrixPath!, target, overwrite: true); + AnsiConsole.MarkupLine($"[green]Copied imatrix artifact:[/] {Markup.Escape(target)}"); + return Task.CompletedTask; + } + + private static async Task CleanOutputDirectoryAsync(string outputDirectory) + { + Directory.CreateDirectory(outputDirectory); + + foreach (var file in Directory.EnumerateFiles(outputDirectory, "*", SearchOption.TopDirectoryOnly)) + await HardDeleteHelper.DeleteFileIfExistsAsync(file); + + foreach (var directory in Directory.EnumerateDirectories(outputDirectory, "*", SearchOption.TopDirectoryOnly)) + Directory.Delete(directory, recursive: true); + + AnsiConsole.MarkupLine($"[grey]Cleaned clone export directory:[/] {Markup.Escape(outputDirectory)}"); + } + + private static async Task EnsureSqliteReadyAsync() + { + await using var db = new MagicQuantContext(); + await db.Database.MigrateAsync(); + } + + private static string ResolveAndValidateOutputDirectory(IReadOnlyCollection args) + { + string? explicitOutput = Get(args, "output-dir"); + string outputDir = !string.IsNullOrWhiteSpace(explicitOutput) + ? explicitOutput! + : !string.IsNullOrWhiteSpace(Config.OutputDirectory) + ? Config.OutputDirectory! + : Path.Combine(Cache.ModelMagicQuantDirectory!, "FinalOutput"); + + outputDir = Path.GetFullPath(outputDir); + Directory.CreateDirectory(outputDir); + return outputDir; + } + + private static string? Get(IReadOnlyCollection args, string name) + => args.FirstOrDefault(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase))?.Value; + + private static void ShowHelp() + { + AnsiConsole.MarkupLine("[bold yellow]Command: clone-repository-quants[/]"); + AnsiConsole.MarkupLine("Rebuilds the final GGUF list from a MagicQuant-compatible tensor config manifest without running the evolution/search pipeline."); + AnsiConsole.MarkupLine("Usage:"); + AnsiConsole.MarkupLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-repo \"owner/repo\" [--output-dir \"\"]"); + AnsiConsole.MarkupLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-json \"\" [--output-dir \"\"]"); + AnsiConsole.MarkupLine("Options:"); + AnsiConsole.MarkupLine(" --source-repo Hugging Face repo containing magicquant.clone-configs.json"); + AnsiConsole.MarkupLine(" --source-json Local or http(s) path to magicquant.clone-configs.json"); + AnsiConsole.MarkupLine(" --use-imatrix Use configured/provided imatrix for the cloned model"); + } +} diff --git a/MagicQuant/Models/RepositoryCloneModels.cs b/MagicQuant/Models/RepositoryCloneModels.cs new file mode 100644 index 0000000..6b61a40 --- /dev/null +++ b/MagicQuant/Models/RepositoryCloneModels.cs @@ -0,0 +1,50 @@ +using System.Text.Json.Serialization; + +namespace MagicQuant.Models; + +public sealed class MagicQuantCloneManifest +{ + public int SchemaVersion { get; set; } = 1; + public DateTime GeneratedUtc { get; set; } = DateTime.UtcNow; + public string Generator { get; set; } = "MagicQuant"; + public string? SourceRepository { get; set; } + public string? SourceJson { get; set; } + public string? SourceModelId { get; set; } + public string? SourceArchitectureFamily { get; set; } + public string? Notes { get; set; } + + public List Artifacts { get; set; } = new(); +} + +public sealed class MagicQuantCloneArtifact +{ + public string FileName { get; set; } = string.Empty; + public string DisplayName { get; set; } = string.Empty; + public string ShortName { get; set; } = string.Empty; + public string Provider { get; set; } = string.Empty; + public string QuantFamily { get; set; } = string.Empty; + public string BaseQuant { get; set; } = "Q8_0"; + public bool IsHybrid { get; set; } + public bool UsedImatrix { get; set; } + + public double? SourceKld { get; set; } + public double? SourcePpl { get; set; } + public double? SourcePplDeltaPercent { get; set; } + public ulong? SourceSizeBytes { get; set; } + + /// + /// Exact tensor-name -> final GGUF quant type map read from the exported artifact. + /// This is the real clone payload. + /// + public Dictionary TensorTypes { get; set; } = new(StringComparer.Ordinal); +} + +public sealed class CloneArtifactBuildRecord +{ + public MagicQuantCloneArtifact ManifestArtifact { get; init; } = default!; + public string OutputPath { get; init; } = string.Empty; + public ulong ActualSizeBytes { get; set; } + public double? Kld { get; set; } + public double? Ppl { get; set; } + public double? PplDeltaPercent { get; set; } +} diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 71e3527..516ea08 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -9,13 +9,29 @@ #if DEBUG if (args.Length == 0) { - args = new[] { "evolution", "--architecture-family", @"""Qwen3-4B-Instruct-2507""" }; + const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. + + if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) + { + args = + [ + "clone-repository-quants", + "--architecture-family", @"""Qwen3-4B-Instruct-2507""", + "--source-repo", @"""magiccodingman/Qwen3-4B-Instruct-2507-Unsloth-MagicQuant-v2-GGUF""" + ]; + } + else + { + // Previous DEBUG harness kept intact for quick full-pipeline testing. + args = ["evolution", "--architecture-family", @"""Qwen3.6-35B-A3B"""]; + } } else if (args.Length > 0 && - string.Equals(args[0], "evolution", StringComparison.OrdinalIgnoreCase) && + (string.Equals(args[0], "evolution", StringComparison.OrdinalIgnoreCase) || + string.Equals(args[0], "clone-repository-quants", StringComparison.OrdinalIgnoreCase)) && !args.Any(x => string.Equals(x, "--architecture-family", StringComparison.OrdinalIgnoreCase))) { - args = args.Concat(new[] { "--architecture-family", @"""Qwen3-4B-Instruct-2507""" }).ToArray(); + args = args.Concat(["--architecture-family", @"""Qwen3-4B-Instruct-2507"""]).ToArray(); } #endif @@ -24,6 +40,7 @@ { "evolution", ("Run the full evolutionary quantization search", () => new Evolution()) }, { "validate-predictions", ("Validate rank-safe KLD predictions against existing SQLite benchmarks", () => new ValidatePredictions()) }, { "build-hybrids", ("Export specific hybrid models with polished README", () => new BuildHybrids()) }, + { "clone-repository-quants", ("Clone final MagicQuant tensor configurations from a compatible repository/json", () => new CloneRepositoryQuants()) }, { "initialize-llama-cpp", ("Initialize or update llama.cpp", () => new InitializeLlamaCpp()) } }; diff --git a/MagicQuant/Services/ArchitectureFamilyService.cs b/MagicQuant/Services/ArchitectureFamilyService.cs index 1dba27f..a6f18f1 100644 --- a/MagicQuant/Services/ArchitectureFamilyService.cs +++ b/MagicQuant/Services/ArchitectureFamilyService.cs @@ -71,6 +71,17 @@ public async Task EnsureCurrentArchitectureFamilyAsync(string bf16GgufPath, Canc throw new InvalidOperationException($"Architecture family '{matchingName.DisplayName}' already exists, but the current model tensor names/count do not match the previously registered architecture. Expected count={matchingName.TensorCount}, actual count={tensorCount}."); } + bool familyAlreadyHasHashes = await db.Set() + .AnyAsync(x => x.ArchitectureFamilyId == matchingName.Id, ct); + + if (familyAlreadyHasHashes && !Cache.AllowArchitectureFamilyAliasOverride) + { + throw new InvalidOperationException( + $"Architecture family '{matchingName.DisplayName}' already has one or more model hashes attached. " + + "Adding the current hash means you are manually asserting these different model hashes share the same tensor architecture/truth. " + + "Rerun with --allow-architecture-family-alias-override only if you intentionally approve this shared-family linkage."); + } + db.Add(new ArchitectureFamilyModelHash { ArchitectureFamilyId = matchingName.Id, @@ -120,6 +131,26 @@ public async Task EnsureCurrentArchitectureFamilyAsync(string bf16GgufPath, Canc AnsiConsole.MarkupLine($"[green]Architecture family created:[/] [cyan]{Markup.Escape(family.DisplayName)}[/] tensors={tensorCount:N0}"); } + public static async Task ResolveExactCurrentAiModelHashIdOrNullAsync(MagicQuantContext db, CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + return null; + + return await db.AiModelHashes + .AsNoTracking() + .Where(x => x.UniqueHash == Cache.CurrentModelId) + .Select(x => (uint?)x.Id) + .FirstOrDefaultAsync(ct); + } + + public static async Task ResolveExactCurrentAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct = default) + { + var id = await ResolveExactCurrentAiModelHashIdOrNullAsync(db, ct); + if (id == null) + throw new InvalidOperationException("Unable to resolve the exact current AiModelHashId."); + return id.Value; + } + public static async Task ResolveScopedAiModelHashIdOrNullAsync(MagicQuantContext db, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) @@ -215,4 +246,4 @@ private static void TryDelete(string path) { try { if (File.Exists(path)) File.Delete(path); } catch { } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 45717bf..c912a57 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -512,9 +512,6 @@ private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext await db.SaveChangesAsync(ct); } - if (Cache.CurrentArchitectureFamilyId != null) - return await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); - return model.Id; } @@ -808,6 +805,19 @@ public async Task RunAllBenchmarksAsync( var requestedDomains = ResolveRequestedDomains(quantConfig, domainsOverride); bool requireKld = RequiresKld(quantConfig); + if (Cache.SuppressBenchmarkPersistence) + { + return await RunAllBenchmarksTransientAsync( + quantConfig: quantConfig, + modelPath: modelPath, + benchDir: benchDir, + tokenTarget: tokenTarget, + klLogitsDir: klLogitsDir, + saveLogits: saveLogits, + requestedDomains: requestedDomains, + requireKld: requireKld); + } + using var db = new MagicQuantContext(); var identity = await GetOrCreateBenchmarkIdentityAsync(db, quantConfig); @@ -986,16 +996,19 @@ await SaveBenchmarkToDbAsync( { sw.Stop(); - await PersistFailedBenchmarkRunAsync( - db: db, - aiModelHashId: aiModelHash.Id, - tensorComboId: tensorCombo.Id, - aiBenchmarkId: trackedBench.Id, - imatrixDefinitionId: identity.ImatrixDefinitionId, - category: DomainToCategory(domain), - startedUtc: startedUtc, - completedUtc: DateTime.UtcNow, - error: ex.ToString()); + if (!Cache.SuppressBenchmarkPersistence) + { + await PersistFailedBenchmarkRunAsync( + db: db, + aiModelHashId: aiModelHash.Id, + tensorComboId: tensorCombo.Id, + aiBenchmarkId: trackedBench.Id, + imatrixDefinitionId: identity.ImatrixDefinitionId, + category: DomainToCategory(domain), + startedUtc: startedUtc, + completedUtc: DateTime.UtcNow, + error: ex.ToString()); + } throw; } @@ -1015,6 +1028,100 @@ await SaveBenchmarkToDbAsync( return result; } + + private async Task RunAllBenchmarksTransientAsync( + HybridQuant quantConfig, + string modelPath, + string benchDir, + int tokenTarget, + string? klLogitsDir, + bool saveLogits, + IReadOnlyCollection requestedDomains, + bool requireKld) + { + if (TryReadExistingBenchmarkArtifacts(benchDir, requestedDomains, requireKld, out var reused)) + { + reused.ModelSizeBytes ??= TryGetModelSize(modelPath); + await WriteMetricsJsonAsync(benchDir, reused); + return reused; + } + + if (_currentPlan == null) + { + throw new InvalidOperationException( + "No benchmark execution plan has been discovered yet. " + + "You must call EnsureExecutionPlanAsync() with the pure Q8 model first."); + } + + await using var slotLease = await AcquireBenchmarkSlotAsync(); + var slot = slotLease.Slot; + + int effectiveNgl = slot.UsesGpu + ? _currentPlan.StaticNgl + : 0; + + var result = new BenchmarkResult + { + ModelSizeBytes = TryGetModelSize(modelPath), + LlamaBench = new LlamaBenchMetrics + { + LogPath = null, + Backend = slot.UsesGpu ? "disabled" : "cpu-disabled", + Ngl = effectiveNgl, + Test = "disabled", + Tps = 0 + } + }; + + var corporaRoot = Path.Combine(Path.GetDirectoryName(benchDir)!, "_ppl_corpora"); + Directory.CreateDirectory(corporaRoot); + + if (saveLogits && !string.IsNullOrEmpty(klLogitsDir)) + Directory.CreateDirectory(klLogitsDir); + + foreach (var domain in requestedDomains) + { + if (TryReadExistingPplLog( + benchDir: benchDir, + domain: domain, + allowMissingKld: !requireKld, + requirePositiveKld: requireKld, + metrics: out var existingPpl)) + { + result.Perplexity[domain] = existingPpl; + continue; + } + + string corpusPath = Path.Combine(corporaRoot, $"ppl_corpus_{domain}.txt"); + await PreparePplCorpusAsync(domain, corpusPath, tokenTarget); + + AnsiConsole.MarkupLine( + $"[yellow]Running transient Perplexity ({Markup.Escape(domain)})[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); + + var metrics = await RunPplBenchmarkAsync( + modelPath: modelPath, + benchDir: benchDir, + domain: domain, + corpusPath: corpusPath, + fixedNgl: effectiveNgl, + slot: slot, + klLogitsDir: klLogitsDir, + saveLogits: saveLogits); + + if (requireKld && !HasMeaningfulKld(metrics.Kld)) + { + throw new InvalidOperationException( + $"Non-base transient benchmark produced invalid KLD for domain '{domain}'. " + + $"KLD must exist and be > 0. Parsed value: {(metrics.Kld.HasValue ? metrics.Kld.Value.ToString(CultureInfo.InvariantCulture) : "null")}"); + } + + result.Perplexity[domain] = metrics; + } + + await WriteMetricsJsonAsync(benchDir, result); + return result; + } + // ---------------------------------------------------------------- // Database helpers // ---------------------------------------------------------------- diff --git a/MagicQuant/Services/CloneConfigManifestGenerationService.cs b/MagicQuant/Services/CloneConfigManifestGenerationService.cs new file mode 100644 index 0000000..9baf168 --- /dev/null +++ b/MagicQuant/Services/CloneConfigManifestGenerationService.cs @@ -0,0 +1,115 @@ +using System.Text.Json; +using MagicQuant.Models; +using MQ.DB; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class CloneConfigManifestGenerationService +{ + public const string FileName = "magicquant.clone-configs.json"; + + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = true + }; + + private readonly QuantizationService _quantizationService; + private readonly FinalArtifactNamingService _namingService = new(); + + public CloneConfigManifestGenerationService(QuantizationService quantizationService) + { + _quantizationService = quantizationService; + } + + public async Task GenerateAsync( + string outputDirectory, + IReadOnlyCollection exportedArtifacts, + BenchmarkSnapshotRecord? pplReference = null, + string? sourceRepository = null, + string? sourceJson = null, + CancellationToken ct = default) + { + Directory.CreateDirectory(outputDirectory); + + var manifest = new MagicQuantCloneManifest + { + SchemaVersion = 1, + GeneratedUtc = DateTime.UtcNow, + Generator = "MagicQuant", + SourceRepository = sourceRepository, + SourceJson = sourceJson, + SourceModelId = Cache.CurrentModelId, + SourceArchitectureFamily = Cache.CurrentArchitectureFamilyName, + Notes = "Exact GGUF tensor quantization map for repository clone/reproducibility mode. This file is not a proof that another cloned model went through the full MagicQuant evolution pipeline." + }; + + double? referencePpl = ResolveReferencePpl(pplReference, exportedArtifacts.Select(x => x.Snapshot)); + + foreach (var artifact in exportedArtifacts + .Where(x => !x.IsExternalReference) + .Where(x => !string.IsNullOrWhiteSpace(x.FullPath)) + .OrderBy(x => x.Snapshot.Kld) + .ThenBy(x => x.Snapshot.SizeBytes)) + { + ct.ThrowIfCancellationRequested(); + + string fullPath = artifact.FullPath!; + if (!File.Exists(fullPath)) + { + AnsiConsole.MarkupLine($"[yellow]Skipping clone config for missing artifact:[/] {Markup.Escape(fullPath)}"); + continue; + } + + var tensorTypes = await _quantizationService.ReadExactTensorTypesAsync(fullPath, ct); + + manifest.Artifacts.Add(new MagicQuantCloneArtifact + { + FileName = artifact.FileName ?? Path.GetFileName(fullPath), + DisplayName = artifact.DisplayName, + ShortName = _namingService.ToShortDisplayName(artifact.DisplayName), + Provider = artifact.ProviderName, + QuantFamily = artifact.BaselineFamily, + BaseQuant = ResolveBaseQuantName(artifact), + IsHybrid = artifact.Snapshot.IsHybrid, + UsedImatrix = Cache.UseImatrix && Cache.IsImatrixAvailable, + SourceKld = artifact.Snapshot.Kld, + SourcePpl = artifact.Snapshot.Ppl, + SourcePplDeltaPercent = FinalReleaseMetadataService.CalculatePplDeltaPercent(artifact.Snapshot.Ppl, referencePpl), + SourceSizeBytes = artifact.ActualSizeBytes ?? artifact.ExpectedSizeBytes, + TensorTypes = tensorTypes.ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal) + }); + } + + string path = Path.Combine(outputDirectory, FileName); + await File.WriteAllTextAsync(path, JsonSerializer.Serialize(manifest, JsonOptions), ct); + AnsiConsole.MarkupLine($"[green]Clone configuration JSON generated:[/] {Markup.Escape(path)}"); + return path; + } + + private static string ResolveBaseQuantName(ExportedArtifactRecord artifact) + { + var quant = artifact.Snapshot.Quant; + if (!string.IsNullOrWhiteSpace(quant.BaseQuant.QuantizeBaseArgumentName)) + return quant.BaseQuant.QuantizeBaseArgumentName; + + if (!quant.BaseQuant.Names.IsDefaultOrEmpty) + return quant.BaseQuant.Names[0]; + + return "Q8_0"; + } + + private static double? ResolveReferencePpl( + BenchmarkSnapshotRecord? pplReference, + IEnumerable snapshots) + { + if (pplReference is { Ppl: > 0d }) + return pplReference.Ppl; + + return snapshots + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .FirstOrDefault() + ?.Ppl; + } +} diff --git a/MagicQuant/Services/CloneReadmeGenerationService.cs b/MagicQuant/Services/CloneReadmeGenerationService.cs new file mode 100644 index 0000000..1b7e300 --- /dev/null +++ b/MagicQuant/Services/CloneReadmeGenerationService.cs @@ -0,0 +1,75 @@ +using System.Text; +using MagicQuant.Models; +using MQ.DB; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class CloneReadmeGenerationService +{ + public async Task GenerateAsync( + string outputDirectory, + string modelName, + string sourceDescription, + bool sourceWasHuggingFaceRepo, + IReadOnlyCollection records, + CancellationToken ct = default) + { + Directory.CreateDirectory(outputDirectory); + string path = Path.Combine(outputDirectory, "README.md"); + + var sb = new StringBuilder(); + + sb.AppendLine($"# {modelName} - MagicQuant Clone Build"); + sb.AppendLine(); + sb.AppendLine("This repository was built in **MagicQuant repository clone mode**."); + sb.AppendLine(); + sb.AppendLine("That means these GGUF files copied exact tensor quantization configurations from an existing MagicQuant-compatible release, then rebuilt and benchmarked those tensor maps against this model locally."); + sb.AppendLine(); + sb.AppendLine("> Important: this model did **not** run through the full MagicQuant probing/evolution/search pipeline by itself. It reused tensor configurations from another MagicQuant release and then generated fresh local benchmark metadata for this output."); + sb.AppendLine(); + sb.AppendLine("## Clone source"); + sb.AppendLine(); + if (sourceWasHuggingFaceRepo) + sb.AppendLine($"- Source Hugging Face repository: `{sourceDescription}`"); + else + sb.AppendLine($"- Source clone JSON: `{sourceDescription}`"); + sb.AppendLine($"- Clone config file: [`{CloneConfigManifestGenerationService.FileName}`](./../../resolve/main/{CloneConfigManifestGenerationService.FileName}?download=true)"); + sb.AppendLine(); + sb.AppendLine("## Downloadable outputs"); + sb.AppendLine(); + sb.AppendLine("| Name | Provider | Quant Family | KLD | PPL Δ % | Size (GB) | Download |"); + sb.AppendLine("|---|---|---|---:|---:|---:|---|"); + + foreach (var record in records.OrderBy(x => x.Kld ?? double.MaxValue).ThenBy(x => x.ActualSizeBytes)) + { + var a = record.ManifestArtifact; + string name = EscapePipe(string.IsNullOrWhiteSpace(a.ShortName) ? Path.GetFileNameWithoutExtension(a.FileName) : a.ShortName); + string provider = EscapePipe(string.IsNullOrWhiteSpace(a.Provider) ? "Cloned config" : a.Provider); + string family = EscapePipe(string.IsNullOrWhiteSpace(a.QuantFamily) ? a.BaseQuant : a.QuantFamily); + string kld = record.Kld.HasValue ? record.Kld.Value.ToString("0.000000") : "n/a"; + string ppl = record.PplDeltaPercent.HasValue ? $"{record.PplDeltaPercent.Value:0.000}%" : "n/a"; + string size = (record.ActualSizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); + sb.AppendLine($"| {name} | {provider} | {family} | {kld} | {ppl} | {size} | [Link](./../../resolve/main/{Uri.EscapeDataString(a.FileName)}?download=true) |"); + } + + sb.AppendLine(); + sb.AppendLine("## Reproducibility"); + sb.AppendLine(); + sb.AppendLine($"The file `{CloneConfigManifestGenerationService.FileName}` stores the exact `tensor name -> quant type` map used to rebuild each GGUF. A future clone run can use that JSON directly with `--source-json`, or a Hugging Face repository containing that file with `--source-repo`."); + sb.AppendLine(); + sb.AppendLine("## Support"); + sb.AppendLine(); + sb.AppendLine("I’m a solo developer working full time for myself to achieve my dream. If you like any of my work, buying me a coffee is always appreciated. Otherwise, good vibes are also accepted as legal tender."); + sb.AppendLine(); + sb.AppendLine("[Click here to see ways to support](https://sayou.biz/support) - BTC, Paypal, GitHub sponsors."); + sb.AppendLine(); + + await File.WriteAllTextAsync(path, sb.ToString(), ct); + AnsiConsole.MarkupLine($"[green]Clone README generated:[/] {Markup.Escape(path)}"); + return path; + } + + private static string EscapePipe(string value) + => (value ?? string.Empty).Replace("|", "\\|"); +} diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index cf8123b..3e0a18b 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -20,6 +20,7 @@ public sealed class CombinationSurvivalPipelineService private readonly HybridMapGenerationService _hybridMapService; private readonly SelectionDiagnosticsLogService _diagnosticsLogService; private readonly FinalReleaseMetadataService _releaseMetadataService; + private readonly CloneConfigManifestGenerationService _cloneConfigManifestService; private readonly FinalArtifactNamingService _namingService; public CombinationSurvivalPipelineService(QuantizationService quantizationService) @@ -37,6 +38,7 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic _hybridMapService = new HybridMapGenerationService(); _diagnosticsLogService = new SelectionDiagnosticsLogService(); _releaseMetadataService = new FinalReleaseMetadataService(); + _cloneConfigManifestService = new CloneConfigManifestGenerationService(_quantizationService); _namingService = new FinalArtifactNamingService(); } @@ -114,6 +116,12 @@ await _releaseMetadataService.GenerateAsync( nativeReference, ct); + await _cloneConfigManifestService.GenerateAsync( + Cache.OutputDirectory!, + exportedArtifacts, + nativeReference, + ct: ct); + await _readmeService.GenerateAsync( Cache.OutputDirectory!, modelName, @@ -180,4 +188,4 @@ private void RenderEliminationSummary( AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in magicquant.replacements.json.[/]"); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index 7d188cd..fd53759 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -308,6 +308,103 @@ with open(result_path, 'w', encoding='utf-8') as f: } } + + public async Task DownloadRepositoryFileAsync( + string repoId, + string fileName, + string destinationPath, + bool forceRedownload = true, + CancellationToken ct = default) + { + await EnsureHubSupportAsync(); + + if (string.IsNullOrWhiteSpace(repoId)) + throw new ArgumentException("Hugging Face repo id is required.", nameof(repoId)); + if (string.IsNullOrWhiteSpace(fileName)) + throw new ArgumentException("Hugging Face file name is required.", nameof(fileName)); + if (string.IsNullOrWhiteSpace(destinationPath)) + throw new ArgumentException("Destination path is required.", nameof(destinationPath)); + + Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); + + string tempDir = Cache.ExternalBaselineCacheDirectory ?? Cache.MagicQuantDirectory ?? AppContext.BaseDirectory; + Directory.CreateDirectory(tempDir); + + string payloadPath = Path.Combine(tempDir, $"hf_download_file_{Guid.NewGuid():N}.json"); + string resultPath = Path.Combine(tempDir, $"hf_download_file_result_{Guid.NewGuid():N}.json"); + string scriptPath = Path.Combine(tempDir, $"hf_download_file_{Guid.NewGuid():N}.py"); + + try + { + await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new + { + repo_id = repoId, + file_name = fileName, + destination_path = destinationPath, + force_redownload = forceRedownload + }), ct); + + const string py = """ + import json + import os + import shutil + import sys + from huggingface_hub import hf_hub_download + + payload_path = sys.argv[1] + with open(payload_path, 'r', encoding='utf-8') as f: + payload = json.load(f) + + target_path = payload['destination_path'] + result_path = target_path + '.download_result.json' + os.makedirs(os.path.dirname(target_path), exist_ok=True) + + try: + downloaded = hf_hub_download( + repo_id=payload['repo_id'], + filename=payload['file_name'], + local_dir=os.path.dirname(target_path), + force_download=payload.get('force_redownload', True), + ) + + if os.path.abspath(downloaded) != os.path.abspath(target_path): + if os.path.exists(target_path): + os.remove(target_path) + shutil.copy2(downloaded, target_path) + + result = {'ok': True, 'downloaded_path': target_path, 'size_bytes': os.path.getsize(target_path)} + except Exception as ex: + result = {'ok': False, 'error': str(ex)} + + with open(result_path, 'w', encoding='utf-8') as f: + json.dump(result, f) + """; + + await File.WriteAllTextAsync(scriptPath, py, ct); + await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); + + string pythonResultPath = destinationPath + ".download_result.json"; + File.Move(pythonResultPath, resultPath, overwrite: true); + + var json = JsonDocument.Parse(await File.ReadAllTextAsync(resultPath, ct)).RootElement; + if (!json.GetProperty("ok").GetBoolean()) + throw new InvalidOperationException($"Hugging Face file download failed: {json.GetProperty("error").GetString()}"); + + if (!File.Exists(destinationPath) || new FileInfo(destinationPath).Length == 0) + throw new InvalidOperationException($"Hugging Face file download completed but produced no file: {destinationPath}"); + + AnsiConsole.MarkupLine($"[green]Downloaded repository file:[/] {Markup.Escape(repoId)}/{Markup.Escape(fileName)} -> {Markup.Escape(destinationPath)}"); + return destinationPath; + } + finally + { + TryDelete(payloadPath); + TryDelete(scriptPath); + TryDelete(resultPath); + TryDelete(destinationPath + ".download_result.json"); + } + } + private async Task EnsureHubSupportAsync() { string? version = await _python.GetInstalledVersionAsync("huggingface_hub"); diff --git a/MagicQuant/Services/ImatrixIdentityService.cs b/MagicQuant/Services/ImatrixIdentityService.cs index 04ad325..d4b8f8a 100644 --- a/MagicQuant/Services/ImatrixIdentityService.cs +++ b/MagicQuant/Services/ImatrixIdentityService.cs @@ -35,12 +35,8 @@ public static class ImatrixIdentityService if (string.IsNullOrWhiteSpace(identityHash)) return null; - var scopedAiModelHashId = Cache.CurrentArchitectureFamilyId != null - ? await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct) - : aiModelHashId; - var existing = await db.ImatrixDefinitions - .FirstOrDefaultAsync(x => x.AiModelHashId == scopedAiModelHashId && x.IdentityHash == identityHash, ct); + .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHashId && x.IdentityHash == identityHash, ct); if (existing != null) return existing.Id; @@ -50,7 +46,7 @@ public static class ImatrixIdentityService var row = new ImatrixDefinition { - AiModelHashId = scopedAiModelHashId, + AiModelHashId = aiModelHashId, IdentityHash = identityHash, CanonicalPath = Cache.ActiveImatrixPath, SourceKind = "runtime-active", @@ -63,4 +59,28 @@ public static class ImatrixIdentityService await db.SaveChangesAsync(ct); return row.Id; } -} \ No newline at end of file + + public static async Task ValidateOwnershipAsync( + MagicQuantContext db, + uint aiModelHashId, + int? imatrixDefinitionId, + CancellationToken ct = default) + { + if (!imatrixDefinitionId.HasValue) + return; + + var ownerHashId = await db.ImatrixDefinitions + .AsNoTracking() + .Where(x => x.Id == imatrixDefinitionId.Value) + .Select(x => (uint?)x.AiModelHashId) + .FirstOrDefaultAsync(ct); + + if (ownerHashId == null || ownerHashId.Value != aiModelHashId) + { + throw new InvalidOperationException( + $"ImatrixDefinitionId {imatrixDefinitionId.Value} does not belong to AiModelHashId {aiModelHashId}. " + + $"Owner AiModelHashId={(ownerHashId.HasValue ? ownerHashId.Value.ToString() : "missing")}. " + + "Imatrix identity is exact-model-hash scoped and must not be resolved through architecture family scope."); + } + } +} diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index b351f66..b14a464 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -469,7 +469,7 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s var psi = new System.Diagnostics.ProcessStartInfo { FileName = imatrixBin, - Arguments = $"-m \"{baseModelPath}\" -f \"{datasetPath}\" -o \"{datPath}\"", + Arguments = $"--no-mmap -m \"{baseModelPath}\" -f \"{datasetPath}\" -o \"{datPath}\"", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false @@ -487,7 +487,7 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s using var writeLock = new SemaphoreSlim(1, 1); var startedUtc = DateTime.UtcNow; - var maxRuntime = TimeSpan.FromHours(2); + var maxRuntime = TimeSpan.FromHours(168); int outputLineCount = 0; bool datDetected = false; long lastDatSize = -1; @@ -495,13 +495,15 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s Task stdoutTask = PumpProcessStreamAsync(p.StandardOutput, "stdout", buildLog, writeLock, line => { outputLineCount++; - AnsiConsole.MarkupLine($"[grey]llama-imatrix stdout:[/] {Markup.Escape(line)}"); + if (Cache.VerboseProcessOutput) + AnsiConsole.MarkupLine($"[grey]llama-imatrix stdout:[/] {Markup.Escape(line)}"); }, ct); Task stderrTask = PumpProcessStreamAsync(p.StandardError, "stderr", buildLog, writeLock, line => { outputLineCount++; - AnsiConsole.MarkupLine($"[grey]llama-imatrix stderr:[/] {Markup.Escape(line)}"); + if (Cache.VerboseProcessOutput) + AnsiConsole.MarkupLine($"[grey]llama-imatrix stderr:[/] {Markup.Escape(line)}"); }, ct); while (!p.HasExited) @@ -904,4 +906,4 @@ private static string ResolvePythonExecutableOrThrow() return pythonExe; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index e96e247..88ee9b4 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -704,11 +704,11 @@ private static bool ShouldEliminateAsBadTrade(GroupCandidateEvaluation anchor, G { await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); - if (scopedAiModelHashId == null) + var exactAiModelHashId = await ArchitectureFamilyService.ResolveExactCurrentAiModelHashIdOrNullAsync(db, ct); + if (exactAiModelHashId == null) return null; - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, createIfMissing: false, ct); var lookup = (TensorConfig)quant; var row = await db.AiBenchmarks @@ -718,7 +718,7 @@ private static bool ShouldEliminateAsBadTrade(GroupCandidateEvaluation anchor, G c => c.Id, (b, c) => new { b, c }) .FirstOrDefaultAsync(x => - x.b.AiModelHashId == scopedAiModelHashId.Value && + x.b.AiModelHashId == exactAiModelHashId.Value && x.b.ImatrixDefinitionId == imatrixDefinitionId && x.c.BaseQuant == lookup.BaseQuant && x.c.Embeddings == lookup.Embeddings && diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 610835e..19d12fc 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -305,12 +305,12 @@ public async Task ProcessHybridBatchAsync( await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdOrNullAsync(db, ct); - if (scopedAiModelHashId == null) + if (exactAiModelHashId == null) return (null, null); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, createIfMissing: false, ct); var comboId = await db.TensorCombos .AsNoTracking() @@ -333,7 +333,7 @@ public async Task ProcessHybridBatchAsync( var benchmarkId = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == comboId) + .Where(x => x.AiModelHashId == exactAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == comboId) .Select(x => x.Id) .FirstOrDefaultAsync(ct); @@ -688,10 +688,11 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId, createIfMissing: false, ct); var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) + .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); @@ -774,16 +775,16 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdOrNullAsync(db, ct); - if (scopedAiModelHashId == null) + if (exactAiModelHashId == null) return false; - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, createIfMissing: false, ct); var bench = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId) + .Where(x => x.AiModelHashId == exactAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId) .Join( db.TensorCombos.AsNoTracking(), benchmark => benchmark.TensorComboId, @@ -828,6 +829,16 @@ private static async Task ResolveCurrentScopedAiModelHashIdAsync(MagicQuan return await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); } + private static async Task ResolveCurrentExactAiModelHashIdOrNullAsync(MagicQuantContext db, CancellationToken ct) + { + return await ArchitectureFamilyService.ResolveExactCurrentAiModelHashIdOrNullAsync(db, ct); + } + + private static async Task ResolveCurrentExactAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct) + { + return await ArchitectureFamilyService.ResolveExactCurrentAiModelHashIdAsync(db, ct); + } + private async Task PersistQuantizationRunAsync( HybridQuant quant, @@ -860,9 +871,7 @@ private async Task PersistQuantizationRunAsync( await db.SaveChangesAsync(ct); } - uint scopedAiModelHashId = Cache.CurrentArchitectureFamilyId != null - ? await ResolveCurrentScopedAiModelHashIdAsync(db, ct) - : aiModelHash.Id; + uint persistenceAiModelHashId = aiModelHash.Id; var tensorCombo = await db.TensorCombos.FirstOrDefaultAsync(x => x.BaseQuant == lookup.BaseQuant && @@ -883,17 +892,18 @@ private async Task PersistQuantizationRunAsync( await db.SaveChangesAsync(ct); } - imatrixDefinitionId ??= await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId, createIfMissing: true, ct); + imatrixDefinitionId ??= await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, persistenceAiModelHashId, createIfMissing: true, ct); + await ImatrixIdentityService.ValidateOwnershipAsync(db, persistenceAiModelHashId, imatrixDefinitionId, ct); Guid? aiBenchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == scopedAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == tensorCombo.Id) + .Where(x => x.AiModelHashId == persistenceAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == tensorCombo.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); var row = new QuantizationRun { Id = Guid.NewGuid(), - AiModelHashId = scopedAiModelHashId, + AiModelHashId = persistenceAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = tensorCombo.Id, AiBenchmarkId = aiBenchmarkId, @@ -1047,6 +1057,51 @@ public async Task EnsureBaseModelFileAsync(bool deleteProcess = false) } } + + public async Task BuildExportArtifactFromExactTensorMapAsync( + IReadOnlyDictionary tensorTypes, + string outputPath, + string baseQuantName, + bool forceRebuild = false, + CancellationToken ct = default) + { + if (tensorTypes == null || tensorTypes.Count == 0) + throw new ArgumentException("A clone tensor map must contain at least one tensor entry.", nameof(tensorTypes)); + + if (string.IsNullOrWhiteSpace(outputPath)) + throw new InvalidOperationException("Export output path is required."); + + var baseQuant = BaselineQuants.ResolveBuiltInStandardBaseline(baseQuantName) + ?? BaselineQuants.Q8_0; + + Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); + + if (!forceRebuild && File.Exists(outputPath) && new FileInfo(outputPath).Length > 0) + return outputPath; + + if (forceRebuild && File.Exists(outputPath)) + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + + await _cpuQuantLock.WaitAsync(ct); + try + { + string nativeBasePath = await EnsureBaseModelFileAsync(); + await RunLlamaQuantizeWithExactTensorMapAsync( + inputFile: nativeBasePath, + outputFile: outputPath, + tensorTypes: tensorTypes, + baseQuant: baseQuant, + ct: ct); + + await File.WriteAllTextAsync(outputPath + ".success.json", "{\"status\":\"success\"}", ct); + return outputPath; + } + finally + { + _cpuQuantLock.Release(); + } + } + public async Task BuildExportArtifactAsync( HybridQuant quant, string outputPath, @@ -1186,6 +1241,107 @@ public async Task CleanupPureQ8ModelAsync() // Quantization // ---------------------------------------------------------------- + + private async Task RunLlamaQuantizeWithExactTensorMapAsync( + string inputFile, + string outputFile, + IReadOnlyDictionary tensorTypes, + BaselineQuants baseQuant, + CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) + throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); + + Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); + + var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, outputFile); + var requestedOverrides = tensorTypes + .OrderBy(x => x.Key, StringComparer.Ordinal) + .Select(x => new RequestedTensorOverride + { + GroupName = "clone_exact_tensor_map", + TensorName = x.Key, + SchemeName = NormalizeQuantName(x.Value) + }) + .ToList(); + + var concreteOverrides = ResolveConcreteTensorOverrides( + allTensorNames: inputTensorMetadata.TensorNames, + requestedOverrides: requestedOverrides); + + var missingInManifest = inputTensorMetadata.TensorNames + .Except(tensorTypes.Keys, StringComparer.Ordinal) + .Take(20) + .ToList(); + + var unexpectedInManifest = tensorTypes.Keys + .Except(inputTensorMetadata.TensorNames, StringComparer.Ordinal) + .Take(20) + .ToList(); + + if (missingInManifest.Count > 0 || unexpectedInManifest.Count > 0 || inputTensorMetadata.TensorNames.Count != tensorTypes.Count) + { + throw new InvalidOperationException( + $"Clone tensor manifest does not exactly match this model architecture. " + + $"MissingInManifest=[{string.Join(", ", missingInManifest)}] UnexpectedInManifest=[{string.Join(", ", unexpectedInManifest)}] " + + $"ModelTensorCount={inputTensorMetadata.TensorNames.Count} ManifestTensorCount={tensorTypes.Count}."); + } + + var args = new List(capacity: concreteOverrides.Count + 8); + + foreach (var overrideItem in concreteOverrides) + args.Add($"--tensor-type \"{overrideItem.TensorName}={overrideItem.SchemeName}\""); + + if (_imatrixService.ShouldUseImatrixForQuant(HybridQuant.CreatePureBaseline(baseQuant))) + { + string imatrixPath = _imatrixService.GetCanonicalImatrixPath(); + if (!File.Exists(imatrixPath)) + throw new InvalidOperationException($"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); + + args.Add($"--imatrix \"{imatrixPath}\""); + } + + args.Add($"\"{inputFile}\""); + args.Add($"\"{outputFile}\""); + args.Add(baseQuant.QuantizeBaseArgumentName); + args.Add("8"); + + string bin = Path.Combine( + Cache.LlamaBin!, + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "llama-quantize.exe" : "llama-quantize"); + + string quantizeLogPath = outputFile + ".quantize.log"; + AnsiConsole.MarkupLine($"[cyan]Quantizing clone artifact:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); + + var result = await RunLoggedProcessAsync(new ProcessStartInfo + { + FileName = bin, + Arguments = string.Join(" ", args) + }, quantizeLogPath, ct); + + if (result.ExitCode != 0) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputFile); + throw new InvalidOperationException( + $"Clone quantization failed for '{outputFile}'. ExitCode={result.ExitCode}. See '{quantizeLogPath}'."); + } + + if (!File.Exists(outputFile) || new FileInfo(outputFile).Length == 0) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputFile); + throw new InvalidOperationException( + $"Clone quantization exited successfully but produced no valid GGUF output: {outputFile}"); + } + + AnsiConsole.MarkupLine($"[green]Clone quantized model ready:[/] {Markup.Escape(outputFile)}"); + + return new QuantizationExecutionReport + { + LogPath = quantizeLogPath, + ResolvedOverrides = concreteOverrides + }; + } + private async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant, IReadOnlyDictionary? temporaryCarrierOverrides = null) { if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) @@ -1255,6 +1411,7 @@ private async Task RunLlamaQuantizeAsync(string inp Arguments = arguments }; + AnsiConsole.MarkupLine($"[cyan]Quantizing:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); var result = await RunLoggedProcessAsync(psi, quantizeLogPath); if (result.ExitCode != 0) @@ -1318,6 +1475,16 @@ private bool ShouldApplyImatrix(HybridQuant quant) return _imatrixService.ShouldUseImatrixForQuant(quant); } + public async Task> ReadExactTensorTypesAsync( + string ggufPath, + CancellationToken ct = default) + { + var meta = await ReadTensorMetadataFromGgufAsync(ggufPath, ggufPath); + return meta.TensorTypes + .OrderBy(x => x.Key, StringComparer.Ordinal) + .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); + } + public async Task ClearLearnedBaselineTensorMappingsAsync(CancellationToken ct = default) { await using var db = new MagicQuantContext(); @@ -1447,14 +1614,15 @@ public async Task LearnNativeSourceTruthAsync( if (combo == null) throw new InvalidOperationException("Native-source benchmark TensorCombo is missing; benchmark base model first."); + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( db, - scopedAiModelHashId, + exactAiModelHashId, createIfMissing: false, ct); var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == scopedAiModelHashId && + .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) @@ -1583,10 +1751,11 @@ private async Task LearnAndPersistBaselineTensorMapAsync( x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId, createIfMissing: false, ct); var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) + .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); @@ -2381,14 +2550,15 @@ private async Task CloneEquivalentIsolationBenchmarkAsync( await db.SaveChangesAsync(ct); } + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( db, - scopedAiModelHashId.Value, + exactAiModelHashId, createIfMissing: true, ct); var existing = await db.AiBenchmarks - .FirstOrDefaultAsync(x => x.AiModelHashId == scopedAiModelHashId.Value && + .FirstOrDefaultAsync(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == duplicateCombo.Id, ct); @@ -2402,7 +2572,7 @@ private async Task CloneEquivalentIsolationBenchmarkAsync( SizeBytes = sourceBench.SizeBytes, TokensPerSecond = sourceBench.TokensPerSecond, TensorComboId = duplicateCombo.Id, - AiModelHashId = scopedAiModelHashId.Value, + AiModelHashId = exactAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId }; db.AiBenchmarks.Add(clonedBenchmark); @@ -2423,7 +2593,7 @@ private async Task CloneEquivalentIsolationBenchmarkAsync( db.QuantizationRuns.Add(new QuantizationRun { Id = Guid.NewGuid(), - AiModelHashId = scopedAiModelHashId.Value, + AiModelHashId = exactAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = duplicateCombo.Id, AiBenchmarkId = clonedBenchmark.Id, @@ -2440,7 +2610,7 @@ private async Task CloneEquivalentIsolationBenchmarkAsync( db.BenchmarkRuns.Add(new BenchmarkRun { Id = Guid.NewGuid(), - AiModelHashId = scopedAiModelHashId.Value, + AiModelHashId = exactAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = duplicateCombo.Id, AiBenchmarkId = clonedBenchmark.Id, @@ -2729,7 +2899,8 @@ void HandleLine(string? line, bool isError) logWriter?.WriteLine(line); } - AnsiConsole.WriteLine(line); + if (Cache.VerboseProcessOutput) + AnsiConsole.WriteLine(line); } process.OutputDataReceived += (_, e) => HandleLine(e.Data, isError: false); diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 1de38b4..0ea82b5 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -53,6 +53,7 @@ public async Task GenerateAsync( sb.AppendLine("- [Final survivor metrics](./../../resolve/main/magicquant.final-survivors.json?download=true) — full file names, KLD, PPL delta %, byte sizes, download targets, and replacement lineage. PPL delta % is measured against the native/reference PPL when available; negative is better and larger positive values are worse."); sb.AppendLine("- [Hybrid tensor map](./../../resolve/main/magicquant.hybrid-map.json?download=true) — tensor-group assignments and effective-state details for MagicQuant hybrid GGUFs."); sb.AppendLine("- [Replacement details](./../../resolve/main/magicquant.replacements.json?download=true) — structured details for baselines or anchors removed from the final download table, including reason codes, KLD deltas, PPL delta %, and size deltas."); + sb.AppendLine("- [Clone tensor configs](./../../resolve/main/magicquant.clone-configs.json?download=true) — exact per-GGUF tensor quantization maps for reproducing this final output list in repository clone mode."); sb.AppendLine(); sb.AppendLine("---"); sb.AppendLine(); @@ -198,4 +199,4 @@ private static void AppendCollapsible(StringBuilder sb, string summary, string b private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); private static string EscapeTooltip(string value) => (value ?? string.Empty).Replace("\"", """).Replace("|", " "); private static string EscapeHtml(string value) => (value ?? string.Empty).Replace("&", "&").Replace("<", "<").Replace(">", ">"); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/RepositoryCloneManifestService.cs b/MagicQuant/Services/RepositoryCloneManifestService.cs new file mode 100644 index 0000000..1549d42 --- /dev/null +++ b/MagicQuant/Services/RepositoryCloneManifestService.cs @@ -0,0 +1,120 @@ +using System.Text.Json; +using MagicQuant.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class RepositoryCloneManifestService +{ + private static readonly JsonSerializerOptions JsonOptions = new() + { + PropertyNameCaseInsensitive = true, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true + }; + + private readonly HuggingFaceBaselineService _huggingFace; + + public RepositoryCloneManifestService(HuggingFaceBaselineService huggingFace) + { + _huggingFace = huggingFace; + } + + public async Task<(MagicQuantCloneManifest Manifest, string LocalPath, string SourceDescription)> ResolveAsync( + string? sourceRepo, + string? sourceJson, + string modelMagicQuantDirectory, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(sourceRepo) && string.IsNullOrWhiteSpace(sourceJson)) + throw new InvalidOperationException("Clone mode requires --source-repo or --source-json ."); + + string cloneDir = Path.Combine(modelMagicQuantDirectory, "CloneSource"); + Directory.CreateDirectory(cloneDir); + + string localPath; + string sourceDescription; + + if (!string.IsNullOrWhiteSpace(sourceRepo)) + { + localPath = Path.Combine(cloneDir, CloneConfigManifestGenerationService.FileName); + sourceDescription = sourceRepo.Trim(); + await _huggingFace.DownloadRepositoryFileAsync( + repoId: sourceRepo.Trim(), + fileName: CloneConfigManifestGenerationService.FileName, + destinationPath: localPath, + forceRedownload: true, + ct: ct); + } + else + { + string raw = sourceJson!.Trim(); + + if (raw.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || + raw.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + { + localPath = Path.Combine(cloneDir, CloneConfigManifestGenerationService.FileName); + sourceDescription = raw; + + using var http = new HttpClient(); + var json = await http.GetStringAsync(raw, ct); + await File.WriteAllTextAsync(localPath, json, ct); + } + else + { + localPath = Path.GetFullPath(raw); + sourceDescription = localPath; + + if (!File.Exists(localPath)) + throw new FileNotFoundException($"Clone JSON file does not exist: {localPath}"); + + string copied = Path.Combine(cloneDir, CloneConfigManifestGenerationService.FileName); + File.Copy(localPath, copied, overwrite: true); + localPath = copied; + } + } + + var manifest = JsonSerializer.Deserialize( + await File.ReadAllTextAsync(localPath, ct), + JsonOptions); + + if (manifest == null) + throw new InvalidOperationException($"Clone manifest could not be parsed: {localPath}"); + + ValidateManifest(manifest, localPath); + + AnsiConsole.MarkupLine($"[green]Clone manifest loaded:[/] {Markup.Escape(localPath)} artifacts={manifest.Artifacts.Count:N0}"); + return (manifest, localPath, sourceDescription); + } + + private static void ValidateManifest(MagicQuantCloneManifest manifest, string localPath) + { + if (manifest.SchemaVersion <= 0) + throw new InvalidOperationException($"Clone manifest has invalid schemaVersion in {localPath}."); + + if (manifest.Artifacts.Count == 0) + throw new InvalidOperationException($"Clone manifest contains zero artifacts: {localPath}"); + + var duplicateFiles = manifest.Artifacts + .Where(x => !string.IsNullOrWhiteSpace(x.FileName)) + .GroupBy(x => x.FileName, StringComparer.OrdinalIgnoreCase) + .Where(g => g.Count() > 1) + .Select(g => g.Key) + .ToList(); + + if (duplicateFiles.Count > 0) + throw new InvalidOperationException($"Clone manifest contains duplicate file names: {string.Join(", ", duplicateFiles)}"); + + foreach (var artifact in manifest.Artifacts) + { + if (string.IsNullOrWhiteSpace(artifact.FileName)) + throw new InvalidOperationException("Clone manifest artifact is missing fileName."); + + if (!artifact.FileName.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)) + throw new InvalidOperationException($"Clone manifest artifact fileName must end with .gguf: {artifact.FileName}"); + + if (artifact.TensorTypes.Count == 0) + throw new InvalidOperationException($"Clone manifest artifact '{artifact.FileName}' has no tensorTypes map."); + } + } +} diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index aa7b38b..97ee4d1 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ + model_dir: /mnt/world8/AI/Models/Qwen3.6-35B-A3B-Qwen/ llama_root: llama_bin: convert_script: @@ -9,7 +9,7 @@ paths: flags: use_imatrix: true force_imatrix_rebuild: false - force_relearn_baseline_tensor_mappings: false + force_relearn_baseline_tensor_mappings: true force_refresh_hardware_probe: false allow_high_precision_hybrids: false @@ -18,7 +18,7 @@ imatrix: dataset_repo: dataset_split: text dataset_config: - dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1m.jsonl + dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1_5m.jsonl # Legacy evolution survivor knobs were removed from YAML. # Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. @@ -100,14 +100,14 @@ candidate_selection: output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: - output_name_prefix: Model + output_name_prefix: Qwen3.6-35B-A3B export_external_learned_baselines: false # Legacy bit-range bucket survival settings were removed. # See candidate_selection above for the active final chooser settings. identity: - architecture_family_name: Qwen3-4B-Instruct-2507 + architecture_family_name: Qwen3.6-35B-A3B allow_architecture_family_alias_override: false baselines: @@ -117,7 +117,7 @@ baselines: enabled_standard_explicit_group_candidates: [] custom_repositories: - - repo_id: unsloth/Qwen3-4B-Instruct-2507-GGUF + - repo_id: unsloth/Qwen3.6-35B-A3B-GGUF enabled: true short_source_name: Unsloth source_kind: huggingface_gguf_repository @@ -131,7 +131,119 @@ baselines: allow_as_explicit_group_candidate: false includes: - - file_name: Qwen3-4B-Instruct-2507-UD-Q4_K_XL.gguf + - file_name: Qwen3.6-35B-A3B-MXFP4_MOE.gguf + baseline_family: MXFP4_MOE + quantize_base_name: MXFP4_MOE + display_name: UD-MXFP4_MOE + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ2_M.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: UD-IQ2_M + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ2_XXS.gguf + baseline_family: IQ2_XXS + quantize_base_name: IQ2_XXS + display_name: UD-IQ2_XXS + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ3_S.gguf + baseline_family: IQ3_S + quantize_base_name: IQ3_S + display_name: UD-IQ3_S + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ3_XXS.gguf + baseline_family: IQ3_XXS + quantize_base_name: IQ3_XXS + display_name: UD-IQ3_XXS + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ4_NL.gguf + baseline_family: IQ4_NL + quantize_base_name: IQ4_NL + display_name: UD-IQ4_NL + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ4_NL_XL.gguf + baseline_family: IQ4_NL + quantize_base_name: IQ4_NL + display_name: UD-IQ4_NL_XL + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ4_XS.gguf + baseline_family: IQ4_XS + quantize_base_name: IQ4_XS + display_name: UD-IQ4_XS + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q2_K_XL.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: UD-Q2_K_XL + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q3_K_M.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_M + display_name: UD-Q3_K_M + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q3_K_S.gguf + baseline_family: IQ3_S + quantize_base_name: IQ3_S + display_name: UD-Q3_K_S + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q3_K_XL.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_M + display_name: UD-Q3_K_XL + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q4_K_M.gguf + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M + display_name: UD-Q4_K_M + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q4_K_S.gguf + baseline_family: Q4_K_S + quantize_base_name: Q4_K_S + display_name: UD-Q4_K_S + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M display_name: UD-Q4_K_XL @@ -139,34 +251,42 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf + - file_name: Qwen3.6-35B-A3B-UD-Q5_K_M.gguf baseline_family: Q5_K quantize_base_name: Q5_K - display_name: UD-Q5_K_XL + display_name: UD-Q5_K_M allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: UD-Q6_K_XL + - file_name: Qwen3.6-35B-A3B-UD-Q5_K_S.gguf + baseline_family: Q5_K_S + quantize_base_name: Q5_K_S + display_name: UD-Q5_K_S allow_as_learning_baseline: true - allow_as_combination_carrier: true + allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-Instruct-2507-UD-Q3_K_XL.gguf - baseline_family: IQ3_S - quantize_base_name: IQ3_S - display_name: UD-Q3_K_XL + - file_name: Qwen3.6-35B-A3B-UD-Q5_K_XL.gguf + baseline_family: Q5_K + quantize_base_name: Q5_K + display_name: UD-Q5_K_XL allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-Instruct-2507-UD-IQ3_XXS.gguf - baseline_family: IQ3_XS - quantize_base_name: IQ3_XS - display_name: UD-IQ3_XXS + - file_name: Qwen3.6-35B-A3B-UD-Q6_K.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: UD-Q6_K allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q6_K_XL.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: UD-Q6_K_XL + allow_as_learning_baseline: true + allow_as_combination_carrier: true + allow_as_explicit_group_candidate: true \ No newline at end of file From 6d6fbfb2e1cf42f19fa9f9f1f642efd468fd8a76 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 27 Apr 2026 12:41:43 -0400 Subject: [PATCH 131/258] disable relearning tensor map on config --- MagicQuant/config.dev.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 97ee4d1..f765b1c 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -9,7 +9,7 @@ paths: flags: use_imatrix: true force_imatrix_rebuild: false - force_relearn_baseline_tensor_mappings: true + force_relearn_baseline_tensor_mappings: false force_refresh_hardware_probe: false allow_high_precision_hybrids: false From 081011e21deeef2be7fc00cf7476af927f2783cb Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 13:10:40 -0400 Subject: [PATCH 132/258] Enforce strict tensor-group learning validation --- MagicQuant/Helpers/HardDeleteHelper.cs | 27 +- .../Models/Learning/TensorLearningModels.cs | 60 +++++ .../Services/HybridArtifactExportService.cs | 21 +- .../Learning/TensorGroupingAuditService.cs | 113 ++++++++ .../TensorLearningDiagnosticWriter.cs | 135 ++++++++++ MagicQuant/Services/QuantizationService.cs | 250 +++++++++++------- 6 files changed, 496 insertions(+), 110 deletions(-) create mode 100644 MagicQuant/Models/Learning/TensorLearningModels.cs create mode 100644 MagicQuant/Services/Learning/TensorGroupingAuditService.cs create mode 100644 MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs diff --git a/MagicQuant/Helpers/HardDeleteHelper.cs b/MagicQuant/Helpers/HardDeleteHelper.cs index d988248..9615c84 100644 --- a/MagicQuant/Helpers/HardDeleteHelper.cs +++ b/MagicQuant/Helpers/HardDeleteHelper.cs @@ -2,6 +2,31 @@ namespace MagicQuant.Helpers; public static class HardDeleteHelper { + public static async Task DeleteDirectoryIfExistsAsync( + string? directory, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(directory) || !Directory.Exists(directory)) + return; + + foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)) + { + ct.ThrowIfCancellationRequested(); + await DeleteFileIfExistsAsync(file); + } + + foreach (var sub in Directory.EnumerateDirectories(directory, "*", SearchOption.AllDirectories) + .OrderByDescending(x => x.Length)) + { + ct.ThrowIfCancellationRequested(); + if (Directory.Exists(sub)) + Directory.Delete(sub, recursive: false); + } + + if (Directory.Exists(directory)) + Directory.Delete(directory, recursive: false); + } + public static async Task DeleteFileIfExistsAsync( string? path, int maxAttempts = 6, @@ -45,4 +70,4 @@ public static async Task DeleteFileIfExistsAsync( $"Failed to hard delete file '{path}' after {maxAttempts} attempts.", lastError); } -} \ No newline at end of file +} diff --git a/MagicQuant/Models/Learning/TensorLearningModels.cs b/MagicQuant/Models/Learning/TensorLearningModels.cs new file mode 100644 index 0000000..e120b18 --- /dev/null +++ b/MagicQuant/Models/Learning/TensorLearningModels.cs @@ -0,0 +1,60 @@ +using MQ.DB.Models; + +namespace MagicQuant.Models.Learning; + +public sealed class TensorGroupingResult +{ + public TensorGroup? PrimaryGroup { get; init; } + public IReadOnlyList MatchedGroups { get; init; } = []; + public bool IsBaseQuantException { get; init; } + public string? MatchedExceptionPattern { get; init; } +} + +public sealed class TensorGroupingAuditIssue +{ + public required string TensorName { get; init; } + public required string IssueKind { get; init; } + public IReadOnlyList MatchedGroups { get; init; } = []; + public string? MatchedExceptionPattern { get; init; } + public string? FinalQuantType { get; init; } + public string? LearningSource { get; init; } +} + +public sealed class TensorGroupingAuditResult +{ + public required IReadOnlyDictionary GroupedByTensor { get; init; } + public required IReadOnlyList Ambiguous { get; init; } + public required IReadOnlyList IllegalUnresolved { get; init; } + public required IReadOnlyList BaseQuantExceptions { get; init; } + + public bool HasFatalIssues => Ambiguous.Count > 0 || IllegalUnresolved.Count > 0; + public int FatalIssueCount => Ambiguous.Count + IllegalUnresolved.Count; +} + +public sealed record LearnedTensorTruth(string TensorName, string FinalQuantType, LearningSource Source); + +public enum LearningSource +{ + LogOnly = 1, + GgufOnly = 2, + Both = 3, + BothWithMismatch = 4 +} + +public sealed class TensorTruthMismatch +{ + public required string TensorName { get; init; } + public required string LogQuantType { get; init; } + public required string GgufQuantType { get; init; } + public bool IsHighSeverity { get; init; } +} + +public sealed class TensorTruthVerificationResult +{ + public required IReadOnlyDictionary TruthByTensor { get; init; } + public IReadOnlyList HardMismatches { get; init; } = []; + public IReadOnlyList SoftMismatches { get; init; } = []; + public IReadOnlyList LogOnly { get; init; } = []; + + public bool HasFatalIssues => HardMismatches.Count > 0; +} diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index cdd4abb..6f125ea 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -185,31 +185,12 @@ private static async Task CleanOutputDirectoryAsync(string outputDirectory, Canc foreach (var directory in Directory.EnumerateDirectories(outputDirectory, "*", SearchOption.TopDirectoryOnly)) { ct.ThrowIfCancellationRequested(); - await HardDeleteDirectoryAsync(directory); + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(directory, ct); } AnsiConsole.MarkupLine($"[grey]Cleaned final export directory:[/] {Markup.Escape(outputDirectory)}"); } - private static async Task HardDeleteDirectoryAsync(string directory) - { - if (!Directory.Exists(directory)) - return; - - foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)) - await HardDeleteHelper.DeleteFileIfExistsAsync(file); - - foreach (var sub in Directory.EnumerateDirectories(directory, "*", SearchOption.AllDirectories) - .OrderByDescending(x => x.Length)) - { - if (Directory.Exists(sub)) - Directory.Delete(sub, recursive: false); - } - - if (Directory.Exists(directory)) - Directory.Delete(directory, recursive: false); - } - private static async Task CleanExportSidecarsAsync(string outputDirectory, CancellationToken ct) { string[] patterns = diff --git a/MagicQuant/Services/Learning/TensorGroupingAuditService.cs b/MagicQuant/Services/Learning/TensorGroupingAuditService.cs new file mode 100644 index 0000000..289593a --- /dev/null +++ b/MagicQuant/Services/Learning/TensorGroupingAuditService.cs @@ -0,0 +1,113 @@ +using MagicQuant.Models.Learning; +using MQ.DB.Models; + +namespace MagicQuant.Services.Learning; + +public sealed class TensorGroupingAuditService +{ + public TensorGroupingAuditResult Audit( + IReadOnlyCollection tensorNames, + IReadOnlyDictionary truthByTensor) + { + var grouped = new Dictionary(StringComparer.Ordinal); + var ambiguous = new List(); + var illegalUnresolved = new List(); + var baseQuantExceptions = new List(); + + foreach (var tensorName in tensorNames.OrderBy(x => x, StringComparer.Ordinal)) + { + var matchedGroups = TReg.FindMatchingGroups(tensorName); + + if (matchedGroups.Length == 1) + { + grouped[tensorName] = new TensorGroupingResult + { + PrimaryGroup = matchedGroups[0], + MatchedGroups = [matchedGroups[0].Name] + }; + + continue; + } + + if (matchedGroups.Length > 1) + { + var names = matchedGroups.Select(x => x.Name).ToList(); + grouped[tensorName] = new TensorGroupingResult + { + PrimaryGroup = null, + MatchedGroups = names + }; + + ambiguous.Add(new TensorGroupingAuditIssue + { + TensorName = tensorName, + IssueKind = "AmbiguousSemanticGroupCollision", + MatchedGroups = names, + FinalQuantType = truthByTensor.TryGetValue(tensorName, out var truth) ? truth.FinalQuantType : null, + LearningSource = truthByTensor.TryGetValue(tensorName, out truth) ? truth.Source.ToString() : null + }); + + continue; + } + + var matchedPattern = FindMatchingBaseQuantExceptionPattern(tensorName); + if (matchedPattern != null) + { + grouped[tensorName] = new TensorGroupingResult + { + PrimaryGroup = null, + MatchedGroups = [], + IsBaseQuantException = true, + MatchedExceptionPattern = matchedPattern + }; + + baseQuantExceptions.Add(new TensorGroupingAuditIssue + { + TensorName = tensorName, + IssueKind = "BaseQuantExceptionFallback", + MatchedExceptionPattern = matchedPattern, + FinalQuantType = truthByTensor.TryGetValue(tensorName, out var truth) ? truth.FinalQuantType : null, + LearningSource = truthByTensor.TryGetValue(tensorName, out truth) ? truth.Source.ToString() : null + }); + + continue; + } + + grouped[tensorName] = new TensorGroupingResult + { + PrimaryGroup = null, + MatchedGroups = [] + }; + + illegalUnresolved.Add(new TensorGroupingAuditIssue + { + TensorName = tensorName, + IssueKind = "IllegalUnresolvedTensor", + FinalQuantType = truthByTensor.TryGetValue(tensorName, out var truth) ? truth.FinalQuantType : null, + LearningSource = truthByTensor.TryGetValue(tensorName, out truth) ? truth.Source.ToString() : null + }); + } + + return new TensorGroupingAuditResult + { + GroupedByTensor = grouped, + Ambiguous = ambiguous, + IllegalUnresolved = illegalUnresolved, + BaseQuantExceptions = baseQuantExceptions + }; + } + + private static string? FindMatchingBaseQuantExceptionPattern(string tensorName) + { + var patterns = TReg.GetBaseQuantExceptionPatterns(); + var regexes = TReg.GetBaseQuantExceptionRegexes(); + + for (int i = 0; i < regexes.Length; i++) + { + if (regexes[i].IsMatch(tensorName)) + return i < patterns.Length ? patterns[i] : regexes[i].ToString(); + } + + return null; + } +} diff --git a/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs b/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs new file mode 100644 index 0000000..0593ca4 --- /dev/null +++ b/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs @@ -0,0 +1,135 @@ +using System.Text; +using System.Text.Json; +using MagicQuant.Helpers; +using MagicQuant.Models.Learning; +using MQ.DB; + +namespace MagicQuant.Services.Learning; + +public sealed class TensorLearningDiagnosticWriter +{ + public async Task WriteFailureAsync( + string baselineName, + string schemeName, + string sourceKind, + string? sourceRepository, + string? sourceFileName, + IReadOnlyDictionary truthByTensor, + TensorGroupingAuditResult audit, + TensorTruthVerificationResult verification, + CancellationToken ct = default) + { + string dir = GetTensorConfigLogDirectory(); + Directory.CreateDirectory(Path.Combine(Cache.ModelMagicQuantDirectory!, "Logs")); + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(dir, ct); + Directory.CreateDirectory(dir); + + string safeBaseline = SanitizeForFileName(baselineName); + string safeScheme = SanitizeForFileName(schemeName); + string baseName = $"tensor-group-learning-failure-{safeBaseline}-{safeScheme}"; + string jsonPath = Path.Combine(dir, baseName + ".json"); + string txtPath = Path.Combine(dir, baseName + ".txt"); + + var payload = new + { + GeneratedUtc = DateTime.UtcNow, + ModelDirectory = Cache.ModelDirectory, + ModelMagicQuantDirectory = Cache.ModelMagicQuantDirectory, + ActiveConfigPath = TReg.TensorGroupsYamlPathOverride, + Baseline = baselineName, + Scheme = schemeName, + SourceKind = sourceKind, + SourceRepository = sourceRepository, + SourceFileName = sourceFileName, + TotalTruthTensors = truthByTensor.Count, + SemanticMatchedCount = audit.GroupedByTensor.Count(x => x.Value.PrimaryGroup != null), + BaseQuantExceptionCount = audit.BaseQuantExceptions.Count, + IllegalUnresolvedCount = audit.IllegalUnresolved.Count, + AmbiguousCount = audit.Ambiguous.Count, + MismatchCount = verification.HardMismatches.Count + verification.SoftMismatches.Count, + HighSeverityMismatchCount = verification.HardMismatches.Count, + BaseQuantExceptions = audit.BaseQuantExceptions, + IllegalUnresolved = audit.IllegalUnresolved, + Ambiguous = audit.Ambiguous, + HardMismatches = verification.HardMismatches, + SoftMismatches = verification.SoftMismatches, + LogOnly = verification.LogOnly + }; + + await File.WriteAllTextAsync(jsonPath, JsonSerializer.Serialize(payload, new JsonSerializerOptions { WriteIndented = true }), ct); + + var sb = new StringBuilder(); + sb.AppendLine("MagicQuant Tensor Group Learning Failure"); + sb.AppendLine($"GeneratedUtc: {DateTime.UtcNow:O}"); + sb.AppendLine($"ModelDirectory: {Cache.ModelDirectory}"); + sb.AppendLine($"ModelMagicQuantDirectory: {Cache.ModelMagicQuantDirectory}"); + sb.AppendLine($"ActiveConfigPath: {TReg.TensorGroupsYamlPathOverride}"); + sb.AppendLine($"Baseline: {baselineName}"); + sb.AppendLine($"Scheme: {schemeName}"); + sb.AppendLine($"SourceKind: {sourceKind}"); + sb.AppendLine($"SourceRepository: {sourceRepository}"); + sb.AppendLine($"SourceFileName: {sourceFileName}"); + sb.AppendLine($"TotalTruthTensors: {truthByTensor.Count}"); + sb.AppendLine($"SemanticMatchedCount: {audit.GroupedByTensor.Count(x => x.Value.PrimaryGroup != null)}"); + sb.AppendLine($"BaseQuantExceptionCount: {audit.BaseQuantExceptions.Count}"); + sb.AppendLine($"IllegalUnresolvedCount: {audit.IllegalUnresolved.Count}"); + sb.AppendLine($"AmbiguousCount: {audit.Ambiguous.Count}"); + sb.AppendLine($"MismatchCount: {verification.HardMismatches.Count + verification.SoftMismatches.Count}"); + sb.AppendLine($"HighSeverityMismatchCount: {verification.HardMismatches.Count}"); + sb.AppendLine(); + + AppendIssues(sb, "Base Quant Exceptions", audit.BaseQuantExceptions); + AppendIssues(sb, "Illegal Unresolved", audit.IllegalUnresolved); + AppendIssues(sb, "Ambiguous", audit.Ambiguous); + + sb.AppendLine("Mismatches:"); + foreach (var mismatch in verification.HardMismatches.Concat(verification.SoftMismatches)) + sb.AppendLine($"- {mismatch.TensorName}: log={mismatch.LogQuantType}, gguf={mismatch.GgufQuantType}, severity={(mismatch.IsHighSeverity ? "high" : "soft")}, source decision=GGUF"); + + if (verification.LogOnly.Count > 0) + { + sb.AppendLine(); + sb.AppendLine("Log-only tensors ignored:"); + foreach (var entry in verification.LogOnly) + sb.AppendLine($"- {entry}"); + } + + await File.WriteAllTextAsync(txtPath, sb.ToString(), ct); + return txtPath; + } + + private static void AppendIssues(StringBuilder sb, string heading, IReadOnlyList issues) + { + sb.AppendLine(heading + ":"); + if (issues.Count == 0) + { + sb.AppendLine("- none"); + sb.AppendLine(); + return; + } + + foreach (var issue in issues) + { + var groups = issue.MatchedGroups.Count > 0 ? $", matchedGroups=[{string.Join(", ", issue.MatchedGroups)}]" : string.Empty; + var pattern = string.IsNullOrWhiteSpace(issue.MatchedExceptionPattern) ? string.Empty : $", exceptionPattern={issue.MatchedExceptionPattern}"; + sb.AppendLine($"- {issue.TensorName}: quant={issue.FinalQuantType}, source={issue.LearningSource}{groups}{pattern}"); + } + + sb.AppendLine(); + } + + private static string GetTensorConfigLogDirectory() + { + if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + throw new InvalidOperationException("Cache.ModelMagicQuantDirectory is not set."); + + return Path.Combine(Cache.ModelMagicQuantDirectory, "Logs", "TensorConfigs"); + } + + private static string SanitizeForFileName(string value) + { + var invalidChars = Path.GetInvalidFileNameChars(); + var cleaned = new string(value.Select(ch => invalidChars.Contains(ch) ? '-' : ch).ToArray()); + return string.IsNullOrWhiteSpace(cleaned) ? "unknown" : cleaned; + } +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 19d12fc..d9f6651 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -5,6 +5,8 @@ using System.Text.Json; using System.Text.RegularExpressions; using MagicQuant.Helpers; +using MagicQuant.Models.Learning; +using MagicQuant.Services.Learning; using MQ.DB; using MQ.DB.Data; using MQ.DB.Models; @@ -50,6 +52,8 @@ public class QuantizationService private readonly int _maxConcurrentQuantizations; private readonly ImatrixService _imatrixService; private readonly HuggingFaceBaselineService _huggingFaceBaselineService; + private readonly TensorGroupingAuditService _tensorGroupingAuditService; + private readonly TensorLearningDiagnosticWriter _tensorLearningDiagnosticWriter; private static readonly SemaphoreSlim BaseModelLock = new(1, 1); private const byte UnknownTensorGroupId = 255; @@ -76,6 +80,8 @@ public QuantizationService(BenchmarkService benchmarker) _benchDir = Path.Combine(Cache.ModelMagicQuantDirectory, "Benchmarks"); _imatrixService = new ImatrixService(); _huggingFaceBaselineService = new HuggingFaceBaselineService(_python); + _tensorGroupingAuditService = new TensorGroupingAuditService(); + _tensorLearningDiagnosticWriter = new TensorLearningDiagnosticWriter(); Directory.CreateDirectory(_ggufDir); Directory.CreateDirectory(_benchDir); @@ -638,9 +644,32 @@ private async Task PrepareExternalBaselineRebuild x => new LearnedTensorTruth(x.Key, x.Value, LearningSource.GgufOnly), StringComparer.Ordinal); - var grouped = AssignGroups(truth.Keys); - var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); - var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).OrderBy(x => x, StringComparer.Ordinal).ToList(); + var verification = new TensorTruthVerificationResult + { + TruthByTensor = truth + }; + var audit = _tensorGroupingAuditService.Audit(truth.Keys.ToList(), truth); + + if (audit.HasFatalIssues) + { + var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( + baselineName: quant.BaseQuant.Names[0], + schemeName: quant.BaseQuant.DefaultTensorScheme?.Names[0] ?? "external", + sourceKind: quant.BaseQuant.SourceKind.ToString(), + sourceRepository: quant.BaseQuant.SourceRepository, + sourceFileName: quant.BaseQuant.SourceFileName, + truthByTensor: truth, + audit: audit, + verification: verification, + ct: ct); + + AnsiConsole.MarkupLine($"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); + throw new InvalidOperationException( + $"Strict tensor-group learning validation failed for external baseline '{quant.BaseQuant.Names[0]}' " + + $"from '{quant.BaseQuant.SourceRepository}/{quant.BaseQuant.SourceFileName}'. " + + $"No normalized rebuilt baseline was produced and no learned tensor mappings were persisted. " + + $"Diagnostic log: {diagnosticPath}"); + } var normalizedOverrides = truth.ToDictionary( x => x.Key, @@ -658,10 +687,12 @@ private async Task PrepareExternalBaselineRebuild BenchmarkModelPath = rebuiltOutputPath, DownloadedExternalModelPath = downloadedExternalBaselinePath, TruthByTensor = truth, - GroupedByTensor = grouped, + GroupedByTensor = audit.GroupedByTensor, AllTensorNamesInDownloadedArtifact = ggufMetadata.TensorNames, - AmbiguousGroupingRows = ambiguous, - UnresolvedTensorNames = unresolved, + AmbiguousGroupingRows = audit.Ambiguous, + UnresolvedTensorNames = audit.IllegalUnresolved.Select(x => x.TensorName).ToList(), + BaseQuantExceptionRows = audit.BaseQuantExceptions, + Verification = verification, HasPreparedLearningTruth = true }; } @@ -675,6 +706,39 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( return; var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; + var verification = prepared.Verification ?? new TensorTruthVerificationResult { TruthByTensor = prepared.TruthByTensor }; + var audit = new TensorGroupingAuditResult + { + GroupedByTensor = prepared.GroupedByTensor, + Ambiguous = prepared.AmbiguousGroupingRows ?? [], + IllegalUnresolved = (prepared.UnresolvedTensorNames ?? []).Select(x => new TensorGroupingAuditIssue + { + TensorName = x, + IssueKind = "IllegalUnresolvedTensor" + }).ToList(), + BaseQuantExceptions = prepared.BaseQuantExceptionRows ?? [] + }; + + if (audit.HasFatalIssues || verification.HasFatalIssues) + { + var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( + baselineName: quant.BaseQuant.Names[0], + schemeName: tensorScheme.Names[0], + sourceKind: quant.BaseQuant.SourceKind.ToString(), + sourceRepository: quant.BaseQuant.SourceRepository, + sourceFileName: quant.BaseQuant.SourceFileName, + truthByTensor: prepared.TruthByTensor, + audit: audit, + verification: verification, + ct: ct); + + AnsiConsole.MarkupLine($"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); + throw new InvalidOperationException( + $"Strict tensor-group learning validation failed for external baseline '{quant.BaseQuant.Names[0]}' " + + $"from '{quant.BaseQuant.SourceRepository}/{quant.BaseQuant.SourceFileName}'. " + + $"No normalized rebuilt baseline was produced and no learned tensor mappings were persisted. " + + $"Diagnostic log: {diagnosticPath}"); + } await using var db = new MagicQuantContext(); @@ -742,7 +806,7 @@ await WriteLearningDiagnosticArtifactAsync( truthByTensor: prepared.TruthByTensor, grouped: prepared.GroupedByTensor, allTensorNamesInModel: prepared.AllTensorNamesInDownloadedArtifact ?? prepared.TruthByTensor.Keys.ToList(), - ambiguous: prepared.AmbiguousGroupingRows ?? new List>(), + ambiguous: audit.Ambiguous, unresolved: prepared.UnresolvedTensorNames ?? new List()); AnsiConsole.MarkupLine($"[green]Persisted rebuilt custom-baseline learning truth:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); @@ -1591,14 +1655,34 @@ public async Task LearnNativeSourceTruthAsync( var ggufTruth = metadata.TensorTypes .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); - var truth = BuildTruthMapWithVerification( + var verification = BuildTruthMapWithVerification( logTruth: new Dictionary(StringComparer.Ordinal), ggufTruth: ggufTruth, baselineName: "NATIVE"); + var truth = verification.TruthByTensor; - var grouped = AssignGroups(truth.Keys); - var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); - var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).ToList(); + var audit = _tensorGroupingAuditService.Audit(truth.Keys.ToList(), truth); + + if (audit.HasFatalIssues || verification.HasFatalIssues) + { + var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( + baselineName: $"NATIVE_{nativeScheme.Names[0]}", + schemeName: nativeScheme.Names[0], + sourceKind: "NativeSource", + sourceRepository: null, + sourceFileName: Path.GetFileName(nativeGgufPath), + truthByTensor: truth, + audit: audit, + verification: verification, + ct: ct); + + AnsiConsole.MarkupLine($"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); + throw new InvalidOperationException( + $"Strict tensor-group learning validation failed for baseline 'NATIVE_{nativeScheme.Names[0]}'. " + + $"Ambiguous={audit.Ambiguous.Count}, IllegalUnresolved={audit.IllegalUnresolved.Count}, " + + $"AllowedBaseQuantFallback={audit.BaseQuantExceptions.Count}. " + + $"No learned tensor mappings were persisted. Diagnostic log: {diagnosticPath}"); + } await using var db = new MagicQuantContext(); var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct) @@ -1642,7 +1726,7 @@ await db.LearnedBaselineTensorQuants .OrderBy(x => x.Key, StringComparer.Ordinal) .Select(x => { - var primaryGroup = grouped[x.Key].PrimaryGroup; + var primaryGroup = audit.GroupedByTensor[x.Key].PrimaryGroup; return new LearnedBaselineTensorQuant { @@ -1668,10 +1752,10 @@ await WriteLearningDiagnosticArtifactAsync( baselineName: $"NATIVE_{nativeScheme.Names[0]}", schemeName: nativeScheme.Names[0], truthByTensor: truth, - grouped: grouped, + grouped: audit.GroupedByTensor, allTensorNamesInModel: metadata.TensorNames, - ambiguous: ambiguous, - unresolved: unresolved); + ambiguous: audit.Ambiguous, + unresolved: audit.IllegalUnresolved.Select(x => x.TensorName).ToList()); var sourcePrecision = nativeScheme.Names[0]; var distribution = rows.GroupBy(x => x.FinalQuantType) @@ -1680,7 +1764,7 @@ await WriteLearningDiagnosticArtifactAsync( .ToList(); AnsiConsole.MarkupLine( - $"[green]Native-source learned truth:[/] precision={Markup.Escape(sourcePrecision)}, tensors={rows.Count}, unresolved={unresolved.Count}, ambiguous={ambiguous.Count}, dist={Markup.Escape($"[{string.Join(", ", distribution)}]")}"); + $"[green]Native-source learned truth:[/] precision={Markup.Escape(sourcePrecision)}, tensors={rows.Count}, unresolved={audit.IllegalUnresolved.Count}, ambiguous={audit.Ambiguous.Count}, baseFallback={audit.BaseQuantExceptions.Count}, dist={Markup.Escape($"[{string.Join(", ", distribution)}]")}"); } private static bool IsLearnableBaselineRun(HybridQuant quant) @@ -1718,25 +1802,33 @@ private async Task LearnAndPersistBaselineTensorMapAsync( return; } - var truth = BuildTruthMapWithVerification(parsed, ggufTruth, quant.BaseQuant.Names[0]); + var verification = BuildTruthMapWithVerification(parsed, ggufTruth, quant.BaseQuant.Names[0]); + var truth = verification.TruthByTensor; if (truth.Count == 0) throw new InvalidOperationException($"No verified tensor truth entries were available for baseline '{quant.BaseQuant.Names[0]}'."); - var grouped = AssignGroups(truth.Keys); - var ambiguous = grouped.Where(x => x.Value.MatchedGroups.Count > 1).ToList(); - if (ambiguous.Count > 0) - { - AnsiConsole.MarkupLine( - $"[red]WARNING:[/] {ambiguous.Count} tensor(s) matched multiple groups while learning baseline {Markup.Escape(quant.BaseQuant.Names[0])}."); - AnsiConsole.MarkupLine($"[grey]Example: {Markup.Escape(ambiguous[0].Key)} => {Markup.Escape(string.Join(", ", ambiguous[0].Value.MatchedGroups))}[/]"); - } + var audit = _tensorGroupingAuditService.Audit(truth.Keys.ToList(), truth); + if (audit.HasFatalIssues || verification.HasFatalIssues) + { + var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( + baselineName: quant.BaseQuant.Names[0], + schemeName: tensorScheme.Names[0], + sourceKind: quant.BaseQuant.SourceKind.ToString(), + sourceRepository: quant.BaseQuant.SourceRepository, + sourceFileName: quant.BaseQuant.SourceFileName, + truthByTensor: truth, + audit: audit, + verification: verification, + ct: ct); - var unresolved = grouped.Where(x => x.Value.PrimaryGroup == null).Select(x => x.Key).ToList(); - if (unresolved.Count > 0) - { - AnsiConsole.MarkupLine( - $"[yellow]WARNING:[/] {unresolved.Count} tensor(s) had no tensor-group match while learning baseline {Markup.Escape(quant.BaseQuant.Names[0])}. " + - $"They will still be saved with TensorGroupId={UnknownTensorGroupId}."); + AnsiConsole.MarkupLine($"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); + throw new InvalidOperationException( + $"Strict tensor-group learning validation failed for baseline '{quant.BaseQuant.Names[0]}'. " + + $"Ambiguous={audit.Ambiguous.Count}, " + + $"IllegalUnresolved={audit.IllegalUnresolved.Count}, " + + $"AllowedBaseQuantFallback={audit.BaseQuantExceptions.Count}. " + + $"No learned tensor mappings were persisted. " + + $"Diagnostic log: {diagnosticPath}"); } await using var db = new MagicQuantContext(); @@ -1773,7 +1865,7 @@ await db.LearnedBaselineTensorQuants .OrderBy(x => x.Key, StringComparer.Ordinal) .Select(kv => { - var match = grouped[kv.Key]; + var match = audit.GroupedByTensor[kv.Key]; return new LearnedBaselineTensorQuant { @@ -1803,10 +1895,10 @@ await WriteLearningDiagnosticArtifactAsync( baselineName: quant.BaseQuant.Names[0], schemeName: tensorScheme.Names[0], truthByTensor: truth, - grouped: grouped, + grouped: audit.GroupedByTensor, allTensorNamesInModel: ggufMetadata.TensorNames, - ambiguous: ambiguous, - unresolved: unresolved); + ambiguous: audit.Ambiguous, + unresolved: audit.IllegalUnresolved.Select(x => x.TensorName).ToList()); AnsiConsole.MarkupLine( $"[green]Learned baseline tensor mapping persisted:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); @@ -1842,7 +1934,7 @@ private Dictionary ParseQuantizeLogForTensorTypes(string logPath return byTensor; } - private Dictionary BuildTruthMapWithVerification( + private TensorTruthVerificationResult BuildTruthMapWithVerification( IReadOnlyDictionary logTruth, IReadOnlyDictionary ggufTruth, string baselineName) @@ -1854,8 +1946,8 @@ private Dictionary BuildTruthMapWithVerification( .ToList(); var result = new Dictionary(StringComparer.Ordinal); - var hardMismatches = new List(); - var softMismatches = new List(); + var hardMismatches = new List(); + var softMismatches = new List(); var logOnly = new List(); foreach (var name in allNames) @@ -1874,9 +1966,21 @@ private Dictionary BuildTruthMapWithVerification( result[name] = new LearnedTensorTruth(name, ggufType!, LearningSource.BothWithMismatch); if (IsHighSeverityMismatch(logType!, ggufType!)) - hardMismatches.Add($"{name}: log={logType} gguf={ggufType}"); + hardMismatches.Add(new TensorTruthMismatch + { + TensorName = name, + LogQuantType = logType!, + GgufQuantType = ggufType!, + IsHighSeverity = true + }); else - softMismatches.Add($"{name}: log={logType} gguf={ggufType}"); + softMismatches.Add(new TensorTruthMismatch + { + TensorName = name, + LogQuantType = logType!, + GgufQuantType = ggufType!, + IsHighSeverity = false + }); } } else if (inGguf) @@ -1893,14 +1997,14 @@ private Dictionary BuildTruthMapWithVerification( { AnsiConsole.MarkupLine( $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {hardMismatches.Count} high-severity GGUF/log mismatches; GGUF truth was used."); - AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", hardMismatches.Take(6)))}[/]"); + AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", hardMismatches.Take(6).Select(x => $\"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}\")))}[/]"); } if (softMismatches.Count > 0) { AnsiConsole.MarkupLine( $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {softMismatches.Count} GGUF/log mismatches; GGUF truth was used."); - AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", softMismatches.Take(6)))}[/]"); + AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", softMismatches.Take(6).Select(x => $\"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}\")))}[/]"); } if (logOnly.Count > 0) @@ -1910,7 +2014,13 @@ private Dictionary BuildTruthMapWithVerification( AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", logOnly.Take(6)))}[/]"); } - return result; + return new TensorTruthVerificationResult + { + TruthByTensor = result, + HardMismatches = hardMismatches, + SoftMismatches = softMismatches, + LogOnly = logOnly + }; } private static bool IsHighSeverityMismatch(string logType, string ggufType) @@ -1926,7 +2036,7 @@ private async Task WriteLearningDiagnosticArtifactAsync( IReadOnlyDictionary truthByTensor, IReadOnlyDictionary grouped, IReadOnlyCollection allTensorNamesInModel, - IReadOnlyCollection> ambiguous, + IReadOnlyCollection ambiguous, IReadOnlyCollection unresolved) { var summaries = new List(); @@ -1966,7 +2076,7 @@ private async Task WriteLearningDiagnosticArtifactAsync( LearnedTensorCount = learned.Count, UnmatchedExpected = unmatched, UnexpectedLearned = unexpected, - Ambiguous = ambiguous.Where(x => x.Value.MatchedGroups.Contains(group.Name)).Select(x => x.Key).Take(20).ToList(), + Ambiguous = ambiguous.Where(x => x.MatchedGroups.Contains(group.Name)).Select(x => x.TensorName).Take(20).ToList(), QuantDistribution = distribution, SourceDistribution = sourceCounts }); @@ -1986,7 +2096,7 @@ private async Task WriteLearningDiagnosticArtifactAsync( $"expected={expected.Count} " + $"learned={learned.Count} " + $"unmatched={unmatched.Count} " + - $"ambiguous={ambiguous.Count(x => x.Value.MatchedGroups.Contains(group.Name))} " + + $"ambiguous={ambiguous.Count(x => x.MatchedGroups.Contains(group.Name))} " + $"dist={Markup.Escape($"[{distShort}]")} " + $"src={Markup.Escape($"[{srcShort}]")}[/]"); @@ -2024,30 +2134,6 @@ private async Task WriteLearningDiagnosticArtifactAsync( } } - private Dictionary AssignGroups(IEnumerable tensorNames) - { - var dict = new Dictionary(StringComparer.Ordinal); - - foreach (var tensorName in tensorNames) - { - var matched = new List(); - - foreach (var group in TReg.All) - { - if (group.Tensors.Any(pattern => Regex.IsMatch(tensorName, $"^{pattern}$"))) - matched.Add(group); - } - - dict[tensorName] = new TensorGroupingResult - { - MatchedGroups = matched.Select(x => x.Name).ToList(), - PrimaryGroup = matched.FirstOrDefault() - }; - } - - return dict; - } - private static TensorWeightScheme? TryResolveBaseTensorScheme(BaselineQuants baseQuant) { if (baseQuant.UniqueId == BaselineQuants.NativeSourceUniqueId) @@ -2701,10 +2787,12 @@ private sealed class PreparedExternalBaselineBuild public string BenchmarkModelPath { get; set; } = string.Empty; public string? DownloadedExternalModelPath { get; set; } public Dictionary? TruthByTensor { get; set; } - public Dictionary? GroupedByTensor { get; set; } + public IReadOnlyDictionary? GroupedByTensor { get; set; } public IReadOnlyCollection? AllTensorNamesInDownloadedArtifact { get; set; } - public List>? AmbiguousGroupingRows { get; set; } + public List? AmbiguousGroupingRows { get; set; } public List? UnresolvedTensorNames { get; set; } + public List? BaseQuantExceptionRows { get; set; } + public TensorTruthVerificationResult? Verification { get; set; } public bool HasPreparedLearningTruth { get; set; } } @@ -2728,12 +2816,6 @@ private sealed class ConcreteTensorOverride public string GroupName { get; set; } = string.Empty; } - private sealed class TensorGroupingResult - { - public TensorGroup? PrimaryGroup { get; set; } - public List MatchedGroups { get; set; } = new(); - } - private sealed class GgufTensorReadResult { public string? Error { get; set; } @@ -2741,16 +2823,6 @@ private sealed class GgufTensorReadResult public Dictionary TensorTypes { get; set; } = new(StringComparer.Ordinal); } - private sealed record LearnedTensorTruth(string TensorName, string FinalQuantType, LearningSource Source); - - private enum LearningSource - { - LogOnly = 1, - GgufOnly = 2, - Both = 3, - BothWithMismatch = 4 - } - // ---------------------------------------------------------------- // Naming helpers // ---------------------------------------------------------------- @@ -2937,4 +3009,4 @@ void HandleLine(string? line, bool isError) StdErr = stderrBuilder.ToString() }; } -} \ No newline at end of file +} From 3327d3ad377b2186cd0f2c030e00ccc6760413b4 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 13:14:33 -0400 Subject: [PATCH 133/258] Fix strict-learning compile regressions --- .../Learning/TensorGroupingAuditService.cs | 15 +++++++++------ .../Learning/TensorLearningDiagnosticWriter.cs | 1 + MagicQuant/Services/QuantizationService.cs | 10 +++++----- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/MagicQuant/Services/Learning/TensorGroupingAuditService.cs b/MagicQuant/Services/Learning/TensorGroupingAuditService.cs index 289593a..ba59bb2 100644 --- a/MagicQuant/Services/Learning/TensorGroupingAuditService.cs +++ b/MagicQuant/Services/Learning/TensorGroupingAuditService.cs @@ -32,6 +32,7 @@ public TensorGroupingAuditResult Audit( if (matchedGroups.Length > 1) { var names = matchedGroups.Select(x => x.Name).ToList(); + truthByTensor.TryGetValue(tensorName, out var truth); grouped[tensorName] = new TensorGroupingResult { PrimaryGroup = null, @@ -43,8 +44,8 @@ public TensorGroupingAuditResult Audit( TensorName = tensorName, IssueKind = "AmbiguousSemanticGroupCollision", MatchedGroups = names, - FinalQuantType = truthByTensor.TryGetValue(tensorName, out var truth) ? truth.FinalQuantType : null, - LearningSource = truthByTensor.TryGetValue(tensorName, out truth) ? truth.Source.ToString() : null + FinalQuantType = truth?.FinalQuantType, + LearningSource = truth?.Source.ToString() }); continue; @@ -53,6 +54,7 @@ public TensorGroupingAuditResult Audit( var matchedPattern = FindMatchingBaseQuantExceptionPattern(tensorName); if (matchedPattern != null) { + truthByTensor.TryGetValue(tensorName, out var truth); grouped[tensorName] = new TensorGroupingResult { PrimaryGroup = null, @@ -66,8 +68,8 @@ public TensorGroupingAuditResult Audit( TensorName = tensorName, IssueKind = "BaseQuantExceptionFallback", MatchedExceptionPattern = matchedPattern, - FinalQuantType = truthByTensor.TryGetValue(tensorName, out var truth) ? truth.FinalQuantType : null, - LearningSource = truthByTensor.TryGetValue(tensorName, out truth) ? truth.Source.ToString() : null + FinalQuantType = truth?.FinalQuantType, + LearningSource = truth?.Source.ToString() }); continue; @@ -79,12 +81,13 @@ public TensorGroupingAuditResult Audit( MatchedGroups = [] }; + truthByTensor.TryGetValue(tensorName, out var unresolvedTruth); illegalUnresolved.Add(new TensorGroupingAuditIssue { TensorName = tensorName, IssueKind = "IllegalUnresolvedTensor", - FinalQuantType = truthByTensor.TryGetValue(tensorName, out var truth) ? truth.FinalQuantType : null, - LearningSource = truthByTensor.TryGetValue(tensorName, out truth) ? truth.Source.ToString() : null + FinalQuantType = unresolvedTruth?.FinalQuantType, + LearningSource = unresolvedTruth?.Source.ToString() }); } diff --git a/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs b/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs index 0593ca4..f1ff4d9 100644 --- a/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs +++ b/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs @@ -3,6 +3,7 @@ using MagicQuant.Helpers; using MagicQuant.Models.Learning; using MQ.DB; +using MQ.DB.Models; namespace MagicQuant.Services.Learning; diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index d9f6651..1208f9d 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1997,14 +1997,14 @@ private TensorTruthVerificationResult BuildTruthMapWithVerification( { AnsiConsole.MarkupLine( $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {hardMismatches.Count} high-severity GGUF/log mismatches; GGUF truth was used."); - AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", hardMismatches.Take(6).Select(x => $\"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}\")))}[/]"); + AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", hardMismatches.Take(6).Select(x => $"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}")))}[/]"); } if (softMismatches.Count > 0) { AnsiConsole.MarkupLine( $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {softMismatches.Count} GGUF/log mismatches; GGUF truth was used."); - AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", softMismatches.Take(6).Select(x => $\"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}\")))}[/]"); + AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", softMismatches.Take(6).Select(x => $"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}")))}[/]"); } if (logOnly.Count > 0) @@ -2789,9 +2789,9 @@ private sealed class PreparedExternalBaselineBuild public Dictionary? TruthByTensor { get; set; } public IReadOnlyDictionary? GroupedByTensor { get; set; } public IReadOnlyCollection? AllTensorNamesInDownloadedArtifact { get; set; } - public List? AmbiguousGroupingRows { get; set; } - public List? UnresolvedTensorNames { get; set; } - public List? BaseQuantExceptionRows { get; set; } + public IReadOnlyList? AmbiguousGroupingRows { get; set; } + public IReadOnlyList? UnresolvedTensorNames { get; set; } + public IReadOnlyList? BaseQuantExceptionRows { get; set; } public TensorTruthVerificationResult? Verification { get; set; } public bool HasPreparedLearningTruth { get; set; } } From c9466a18dcf54f7a3778ded58fb3fc8f8f3c697f Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 13:27:50 -0400 Subject: [PATCH 134/258] Refine external persistence failure messaging --- MagicQuant/Services/QuantizationService.cs | 40 +++++++++++----------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 1208f9d..b2019aa 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -736,7 +736,7 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( throw new InvalidOperationException( $"Strict tensor-group learning validation failed for external baseline '{quant.BaseQuant.Names[0]}' " + $"from '{quant.BaseQuant.SourceRepository}/{quant.BaseQuant.SourceFileName}'. " + - $"No normalized rebuilt baseline was produced and no learned tensor mappings were persisted. " + + $"Prepared external baseline learning truth was invalid. No learned tensor mappings were persisted. " + $"Diagnostic log: {diagnosticPath}"); } @@ -764,12 +764,6 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( if (!benchmarkId.HasValue) throw new InvalidOperationException($"Unable to persist learned mappings because no AiBenchmark exists for rebuilt baseline '{quant.BaseQuant.Names[0]}'."); - await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && - x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && - x.TensorWeightSchemeId == tensorScheme.UniqueId) - .ExecuteDeleteAsync(ct); - var rows = prepared.TruthByTensor .OrderBy(x => x.Key, StringComparer.Ordinal) .Select(kv => @@ -797,6 +791,12 @@ await db.LearnedBaselineTensorQuants if (rows.Count == 0) throw new InvalidOperationException($"Prepared learning truth for baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && + x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && + x.TensorWeightSchemeId == tensorScheme.UniqueId) + .ExecuteDeleteAsync(ct); + db.LearnedBaselineTensorQuants.AddRange(rows); await db.SaveChangesAsync(ct); @@ -1716,12 +1716,6 @@ public async Task LearnNativeSourceTruthAsync( if (!benchmarkId.HasValue) throw new InvalidOperationException("Native-source benchmark row is missing; benchmark base model before native-source learning."); - await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == scopedAiModelHashId && - x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && - x.TensorWeightSchemeId == nativeScheme.UniqueId) - .ExecuteDeleteAsync(ct); - var rows = truth .OrderBy(x => x.Key, StringComparer.Ordinal) .Select(x => @@ -1745,6 +1739,12 @@ await db.LearnedBaselineTensorQuants if (rows.Count == 0) throw new InvalidOperationException("Native-source learning produced no persistable rows."); + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == scopedAiModelHashId && + x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + x.TensorWeightSchemeId == nativeScheme.UniqueId) + .ExecuteDeleteAsync(ct); + db.LearnedBaselineTensorQuants.AddRange(rows); await db.SaveChangesAsync(ct); @@ -1855,12 +1855,6 @@ private async Task LearnAndPersistBaselineTensorMapAsync( if (!benchmarkId.HasValue) throw new InvalidOperationException($"Unable to persist learned mappings because no AiBenchmark exists for baseline '{quant.BaseQuant.Names[0]}'."); - await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && - x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && - x.TensorWeightSchemeId == tensorScheme.UniqueId) - .ExecuteDeleteAsync(ct); - var rows = truth .OrderBy(x => x.Key, StringComparer.Ordinal) .Select(kv => @@ -1888,6 +1882,12 @@ await db.LearnedBaselineTensorQuants if (rows.Count == 0) throw new InvalidOperationException($"Learning baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && + x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && + x.TensorWeightSchemeId == tensorScheme.UniqueId) + .ExecuteDeleteAsync(ct); + db.LearnedBaselineTensorQuants.AddRange(rows); await db.SaveChangesAsync(ct); @@ -1996,7 +1996,7 @@ private TensorTruthVerificationResult BuildTruthMapWithVerification( if (hardMismatches.Count > 0) { AnsiConsole.MarkupLine( - $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {hardMismatches.Count} high-severity GGUF/log mismatches; GGUF truth was used."); + $"[red]STRICT VALIDATION:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] has {hardMismatches.Count} high-severity GGUF/log mismatches. A failure diagnostic will be written and learning will stop."); AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", hardMismatches.Take(6).Select(x => $"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}")))}[/]"); } From a5107b7f11d309f83ade5434828ca8dfc491b6fc Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 13:38:50 -0400 Subject: [PATCH 135/258] Make empty log+GGUF tensor truth a strict failure --- MagicQuant/Services/QuantizationService.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index b2019aa..f0028d0 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1790,16 +1790,17 @@ private async Task LearnAndPersistBaselineTensorMapAsync( } var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; - var parsed = ParseQuantizeLogForTensorTypes(report?.LogPath ?? (quantizedModelPath + ".quantize.log")); + string logPath = report?.LogPath ?? (quantizedModelPath + ".quantize.log"); + var parsed = ParseQuantizeLogForTensorTypes(logPath); var ggufMetadata = await ReadTensorMetadataFromGgufAsync(quantizedModelPath, quantizedModelPath); var ggufTruth = ggufMetadata.TensorTypes .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); if (parsed.Count == 0 && ggufTruth.Count == 0) { - AnsiConsole.MarkupLine( - $"[red]WARNING:[/] learned mapping parse returned no tensors from logs and GGUF for baseline [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); - return; + throw new InvalidOperationException( + $"Strict tensor learning failed for baseline '{quant.BaseQuant.Names[0]}': no tensor truth could be read from either the quantize log or GGUF metadata. " + + $"QuantizedModelPath={quantizedModelPath}; LogPath={logPath}"); } var verification = BuildTruthMapWithVerification(parsed, ggufTruth, quant.BaseQuant.Names[0]); From 56251896ee0abb8a3cf881f4460173ee514e5be1 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 27 Apr 2026 14:02:15 -0400 Subject: [PATCH 136/258] updated regex --- MQ.DB/tensor_groups.yaml | 296 ++++++++++++++++++++++----------------- 1 file changed, 170 insertions(+), 126 deletions(-) diff --git a/MQ.DB/tensor_groups.yaml b/MQ.DB/tensor_groups.yaml index 81b6214..d9ae5e1 100644 --- a/MQ.DB/tensor_groups.yaml +++ b/MQ.DB/tensor_groups.yaml @@ -4,183 +4,196 @@ groups: embeddings: description: "Token embedding matrices." patterns: - - "token_embd\\.weight" - - "model\\.embed_tokens\\.weight" - - "embed_tokens\\.weight" - - "tok_embeddings\\.weight" - - "word_embeddings\\.weight" - - "transformer\\.wte\\.weight" - - "wte\\.weight" + # Top-level GGUF / llama.cpp forms. + - "^token_embd\\.weight$" + + # Common HF / framework forms. + - "^model\\.embed_tokens\\.weight$" + - "^embed_tokens\\.weight$" + - "^tok_embeddings\\.weight$" + - "^word_embeddings\\.weight$" + - "^transformer\\.wte\\.weight$" + - "^wte\\.weight$" lm_head: - description: "Final output/logit projection tensors." + description: "Final output/logit projection tensors. Keep these anchored so blk.N.attn_output.weight never collides with lm_head." patterns: - - "output\\.weight" - - "lm_head\\.weight" - - "final_logits_proj\\.weight" - - "model\\.embed_out\\.weight" - - "lm_head\\.decoder\\.weight" + # Top-level GGUF output head only. + # Important: do NOT use unanchored output\.weight here, because it also + # matches blk.N.attn_output.weight when Regex.IsMatch is used. + - "^output\\.weight$" + + # Common HF / framework forms. + - "^lm_head\\.weight$" + - "^final_logits_proj\\.weight$" + - "^model\\.embed_out\\.weight$" + - "^lm_head\\.decoder\\.weight$" attn_q: - description: "Attention query projection tensors." + description: "Attention query projection tensors. Fused QKV tensors are assigned here as the representative attention-projection owner because MagicQuant currently has no separate attn_qkv group." patterns: - # llama.cpp GGUF form, e.g. blk.0.attn_q.weight - - "blk\\..*\\.attn_q\\.weight" + # llama.cpp GGUF query form. + - "^blk\\..*\\.attn_q\\.weight$" + + # Qwen3.6 / hybrid attention form. + # This is a fused query/key/value tensor. It is material and should not be + # an exception. If MagicQuant later gains a dedicated attn_qkv group, move + # this pattern there. + - "^blk\\..*\\.attn_qkv\\.weight$" # LLaMA / Qwen / Mistral / modern HF forms. - - ".*q_proj.*weight" - - ".*self_attn\\.q_proj\\.weight" + - ".*q_proj.*weight$" + - ".*self_attn\\.q_proj\\.weight$" # BERT / encoder style forms. - - ".*query\\.weight" - - ".*attention\\.self\\.query\\.weight" + - ".*query\\.weight$" + - ".*attention\\.self\\.query\\.weight$" # T5 / miscellaneous forms. - - ".*SelfAttention\\.q\\.weight" + - ".*SelfAttention\\.q\\.weight$" - # Fused QKV forms. + # Fused QKV HF forms. # These may intentionally match broader attention tensors and should be # handled carefully by the learning/ambiguity layer. - - ".*c_attn\\.weight" - - ".*query_key_value\\.weight" + - ".*c_attn\\.weight$" + - ".*query_key_value\\.weight$" attn_kv: description: "Attention key/value projection tensors." patterns: # llama.cpp GGUF forms. - - "blk\\..*\\.attn_k\\.weight" - - "blk\\..*\\.attn_v\\.weight" + - "^blk\\..*\\.attn_k\\.weight$" + - "^blk\\..*\\.attn_v\\.weight$" # LLaMA / Qwen / Mistral / modern HF forms. - - ".*k_proj.*weight" - - ".*v_proj.*weight" - - ".*self_attn\\.k_proj\\.weight" - - ".*self_attn\\.v_proj\\.weight" + - ".*k_proj.*weight$" + - ".*v_proj.*weight$" + - ".*self_attn\\.k_proj\\.weight$" + - ".*self_attn\\.v_proj\\.weight$" # BERT / encoder style forms. - - ".*key\\.weight" - - ".*value\\.weight" - - ".*attention\\.self\\.key\\.weight" - - ".*attention\\.self\\.value\\.weight" + - ".*key\\.weight$" + - ".*value\\.weight$" + - ".*attention\\.self\\.key\\.weight$" + - ".*attention\\.self\\.value\\.weight$" # T5 / encoder-decoder style forms. - - ".*SelfAttention\\.k\\.weight" - - ".*SelfAttention\\.v\\.weight" - - ".*EncDecAttention\\.k\\.weight" - - ".*EncDecAttention\\.v\\.weight" + - ".*SelfAttention\\.k\\.weight$" + - ".*SelfAttention\\.v\\.weight$" + - ".*EncDecAttention\\.k\\.weight$" + - ".*EncDecAttention\\.v\\.weight$" attn_output: description: "Attention output projection tensors." patterns: # llama.cpp GGUF form. - - "blk\\..*\\.attn_output\\.weight" + - "^blk\\..*\\.attn_output\\.weight$" # LLaMA / Qwen / Mistral / modern HF forms. - - ".*out_proj.*weight" - - ".*o_proj.*weight" - - ".*self_attn\\.out_proj\\.weight" + - ".*out_proj.*weight$" + - ".*o_proj.*weight$" + - ".*self_attn\\.out_proj\\.weight$" # GPT-style attention output. # Note: c_proj can also appear in MLPs on some architectures, so this may # require architecture-aware disambiguation or strict ambiguity reporting. - - ".*c_proj\\.weight" + - ".*attn.*c_proj\\.weight$" + - ".*attention.*c_proj\\.weight$" # BERT / encoder style forms. - - ".*attention\\.output\\.dense\\.weight" - - ".*self_attention\\.dense\\.weight" - - ".*attention\\.proj\\.weight" + - ".*attention\\.output\\.dense\\.weight$" + - ".*self_attention\\.dense\\.weight$" + - ".*attention\\.proj\\.weight$" # T5 / miscellaneous forms. - - ".*SelfAttention\\.o\\.weight" + - ".*SelfAttention\\.o\\.weight$" ffn_up_gate: - description: "Dense FFN up/gate tensors. Expert-path tensors should generally be owned by moe_experts." + description: "Dense FFN up/gate tensors only. Expert-path tensors are intentionally excluded and should be owned by moe_experts." patterns: # llama.cpp GGUF dense FFN forms. - - "blk\\..*\\.ffn_up\\.weight" - - "blk\\..*\\.ffn_gate\\.weight" + - "^blk\\..*\\.ffn_up\\.weight$" + - "^blk\\..*\\.ffn_gate\\.weight$" # BERT / encoder style forms. - - ".*intermediate\\.dense\\.weight" + - ".*intermediate\\.dense\\.weight$" # GPT / MLP style forms. - - ".*c_fc\\.weight" - - ".*fc1\\.weight" - - ".*fc_in\\.weight" - - ".*dense_h_to_4h\\.weight" + - ".*mlp.*c_fc\\.weight$" + - ".*mlp.*fc1\\.weight$" + - ".*fc1\\.weight$" + - ".*fc_in\\.weight$" + - ".*dense_h_to_4h\\.weight$" # T5 / gated FFN style forms. - - ".*wi\\.weight" - - ".*wi_0\\.weight" - - ".*wi_1\\.weight" - - ".*DenseReluDense\\.wi_0\\.weight" - - ".*DenseReluDense\\.wi_1\\.weight" + - ".*wi\\.weight$" + - ".*wi_0\\.weight$" + - ".*wi_1\\.weight$" + - ".*DenseReluDense\\.wi_0\\.weight$" + - ".*DenseReluDense\\.wi_1\\.weight$" # LLaMA / Qwen / Mistral / modern dense MLP forms. - - ".*mlp\\.up_proj\\.weight" - - ".*mlp\\.gate_proj\\.weight" - - # Qwen3.5 / Qwen3.6 / modern expert forms. - # These were added for newer MoE architectures, but if strict collision - # detection is enabled, moe_experts should usually own these instead. - - ".*experts.*wi_0\\.weight" - - ".*experts.*wi_1\\.weight" - - "blk\\..*\\.ffn_up_exps\\.weight" - - "blk\\..*\\.ffn_gate_exps\\.weight" - - ".*mlp\\.experts\\.gate_up_proj.*" - - ".*mlp\\.shared_expert\\.gate_proj\\.weight" - - ".*mlp\\.shared_expert\\.up_proj\\.weight" + - ".*mlp\\.up_proj\\.weight$" + - ".*mlp\\.gate_proj\\.weight$" - # Gemma-style MoE forms. - - ".*layers\\..*\\.experts\\.gate_up_proj.*" + # Do NOT add expert/shared-expert patterns here. + # Qwen3.5 / Qwen3.6 expert forms such as: + # blk.N.ffn_up_exps.weight + # blk.N.ffn_gate_exps.weight + # blk.N.ffn_up_shexp.weight + # blk.N.ffn_gate_shexp.weight + # are MoE expert-path tensors and belong to moe_experts. ffn_down: - description: "Dense FFN down-projection tensors. Expert-path tensors should generally be owned by moe_experts." + description: "Dense FFN down-projection tensors only. Expert-path tensors are intentionally excluded and should be owned by moe_experts." patterns: # llama.cpp GGUF dense FFN form. - - "blk\\..*\\.ffn_down\\.weight" + - "^blk\\..*\\.ffn_down\\.weight$" # BERT / encoder style form. - - ".*output\\.dense\\.weight" + # Keep this scoped to avoid catching attention.output.dense if that should + # be owned by attn_output. + - ".*mlp.*output\\.dense\\.weight$" + - ".*ffn.*output\\.dense\\.weight$" # GPT / MLP style forms. - # Note: c_proj can also appear as attention output on some architectures. - # Let the matching layer report ambiguity if the model family cannot - # distinguish it cleanly. - - ".*c_proj\\.weight" - - ".*fc2\\.weight" - - ".*fc_out\\.weight" - - ".*dense_4h_to_h\\.weight" + # c_proj can also appear as attention output on some architectures. + # Scope it toward MLP where possible to avoid attn_output ambiguity. + - ".*mlp.*c_proj\\.weight$" + - ".*mlp.*fc2\\.weight$" + - ".*fc2\\.weight$" + - ".*fc_out\\.weight$" + - ".*dense_4h_to_h\\.weight$" # T5 / gated FFN style forms. - - ".*wo\\.weight" - - ".*DenseReluDense\\.wo\\.weight" + - ".*wo\\.weight$" + - ".*DenseReluDense\\.wo\\.weight$" # LLaMA / Qwen / Mistral / modern dense MLP form. - - ".*mlp\\.down_proj\\.weight" + - ".*mlp\\.down_proj\\.weight$" - # Qwen3.5 / Qwen3.6 / modern expert forms. - # These were added for newer MoE architectures, but if strict collision - # detection is enabled, moe_experts should usually own these instead. - - ".*experts.*wo\\.weight" - - "blk\\..*\\.ffn_down_exps\\.weight" - - ".*mlp\\.experts\\.down_proj.*" - - ".*mlp\\.shared_expert\\.down_proj\\.weight" - - # Gemma-style MoE forms. - - ".*layers\\..*\\.experts\\.down_proj.*" + # Do NOT add expert/shared-expert patterns here. + # Qwen3.5 / Qwen3.6 expert forms such as: + # blk.N.ffn_down_exps.weight + # blk.N.ffn_down_shexp.weight + # are MoE expert-path tensors and belong to moe_experts. moe_experts: - description: "MoE expert-path tensors, including shared experts." + description: "MoE expert-path tensors, including routed experts and shared experts. These own *_exps and *_shexp forms so they do not collide with dense FFN groups." patterns: - # llama.cpp GGUF MoE expert tensors. - # These intentionally own the *_exps forms so they do not collide with - # dense ffn_up_gate / ffn_down groups. - - "blk\\..*\\.ffn_.*expert.*" - - "blk\\..*\\.ffn_.*exps.*" - - "blk\\..*\\.ffn_up_exps\\.weight" - - "blk\\..*\\.ffn_gate_exps\\.weight" - - "blk\\..*\\.ffn_down_exps\\.weight" + # llama.cpp GGUF MoE routed expert tensors. + - "^blk\\..*\\.ffn_.*expert.*$" + - "^blk\\..*\\.ffn_.*exps.*$" + - "^blk\\..*\\.ffn_up_exps\\.weight$" + - "^blk\\..*\\.ffn_gate_exps\\.weight$" + - "^blk\\..*\\.ffn_down_exps\\.weight$" + + # Qwen3.6 shared-expert GGUF tensors. + # These are material expert-path matrices and should not fall back through + # base_quant_exceptions. + - "^blk\\..*\\.ffn_up_shexp\\.weight$" + - "^blk\\..*\\.ffn_gate_shexp\\.weight$" + - "^blk\\..*\\.ffn_down_shexp\\.weight$" # Generic expert container forms used by several HF architectures. - ".*experts?\\..*wi_0.*" @@ -202,9 +215,9 @@ groups: # Shared experts are still MoE expert-path tensors, not normal dense FFN. # Keeping them here prevents them from double-counting as generic FFN. - - ".*mlp\\.shared_expert\\.gate_proj\\.weight" - - ".*mlp\\.shared_expert\\.up_proj\\.weight" - - ".*mlp\\.shared_expert\\.down_proj\\.weight" + - ".*mlp\\.shared_expert\\.gate_proj\\.weight$" + - ".*mlp\\.shared_expert\\.up_proj\\.weight$" + - ".*mlp\\.shared_expert\\.down_proj\\.weight$" # Gemma-style MoE forms. - ".*layers\\..*\\.experts\\.gate_up_proj.*" @@ -215,30 +228,53 @@ groups: moe_router: description: "MoE router/gating tensors." patterns: - - "router.*" - - "gating.*" - - "routing.*" + - "^router.*" + - "^gating.*" + - "^routing.*" # Generic gate/router forms. # Negative lookbehind prevents ffn_gate.weight from being treated as router. - - ".*(? Date: Mon, 27 Apr 2026 14:15:21 -0400 Subject: [PATCH 137/258] Apply learned base-carrier blanket before group overrides --- MagicQuant/Services/QuantizationService.cs | 87 ++++++++++++++++------ 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index f0028d0..dc92041 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1432,6 +1432,7 @@ private async Task RunLlamaQuantizeAsync(string inp var concreteOverrides = ResolveConcreteTensorOverrides( allTensorNames: inputTensorMetadata.TensorNames, requestedOverrides: requestedOverrides); + bool shouldRequireFullLearnedCoverage = ShouldApplyLearnedBaseCarrierBlanket(quant, temporaryCarrierOverrides); if (requestedOverrides.Count > 0 && concreteOverrides.Count == 0) { @@ -1440,6 +1441,25 @@ private async Task RunLlamaQuantizeAsync(string inp "This means the requested tensor selectors did not match the input GGUF."); } + if (shouldRequireFullLearnedCoverage) + { + var concreteNames = concreteOverrides + .Select(x => x.TensorName) + .ToHashSet(StringComparer.Ordinal); + var missing = inputTensorMetadata.TensorNames + .Except(concreteNames, StringComparer.Ordinal) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + if (missing.Count > 0) + { + throw new InvalidOperationException( + $"Full learned base-carrier coverage is incomplete for baseline '{quant.BaseQuant.Names[0]}'. " + + $"Missing={missing.Count}. Examples=[{string.Join(", ", missing.Take(15))}]. " + + "Run with --relearn-baseline-mappings."); + } + } + var args = new List(capacity: 256); foreach (var overrideItem in concreteOverrides) @@ -2162,34 +2182,25 @@ private List BuildRequestedTensorOverrides( bool hasTemporaryCarrierOverrides = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0; bool hasExplicitGroupOverrides = quant.Tensors != null && quant.Tensors.Count > 0; + bool shouldApplyBaseCarrierBlanket = ShouldApplyLearnedBaseCarrierBlanket(quant, temporaryCarrierOverrides); - if (!hasTemporaryCarrierOverrides && !hasExplicitGroupOverrides && !quant.BaseQuant.IsExternalRepositoryBaseline) + if (!shouldApplyBaseCarrierBlanket) return result; var baseScheme = TryResolveBaseTensorScheme(quant.BaseQuant); + var blanket = LoadBaseCarrierTensorMappingsOrThrow( + quant: quant, + temporaryCarrierOverrides: temporaryCarrierOverrides, + requireFullCoverage: shouldApplyBaseCarrierBlanket); - if (quant.BaseQuant.IsExternalRepositoryBaseline || hasTemporaryCarrierOverrides) + foreach (var kv in blanket.OrderBy(x => x.Key, StringComparer.Ordinal)) { - var blanket = hasTemporaryCarrierOverrides - ? new Dictionary(temporaryCarrierOverrides!, StringComparer.Ordinal) - : TryLoadAllLearnedTensorMappings( - canonicalBaselineKey: quant.BaseQuant.CanonicalKey, - preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, - allowDominantFallback: false); - - if ((quant.BaseQuant.IsExternalRepositoryBaseline || hasTemporaryCarrierOverrides) && blanket.Count == 0) - throw new InvalidOperationException( - $"Missing blanket learned mapping for custom carrier baseline '{quant.BaseQuant.Names[0]}'. Custom carrier baselines must be learned once before they can participate in hybrid quantization."); - - foreach (var kv in blanket.OrderBy(x => x.Key, StringComparer.Ordinal)) + result.Add(new RequestedTensorOverride { - result.Add(new RequestedTensorOverride - { - GroupName = "base_carrier", - TensorName = kv.Key, - SchemeName = kv.Value - }); - } + GroupName = "base_carrier", + TensorName = kv.Key, + SchemeName = kv.Value + }); } if (!hasExplicitGroupOverrides) @@ -2274,6 +2285,40 @@ private List BuildRequestedTensorOverrides( return result; } + +private bool ShouldApplyLearnedBaseCarrierBlanket( + HybridQuant quant, + IReadOnlyDictionary? temporaryCarrierOverrides = null) +{ + bool hasTemporaryCarrierOverrides = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0; + bool hasExplicitGroupOverrides = quant.Tensors != null && quant.Tensors.Count > 0; + + return hasTemporaryCarrierOverrides || + quant.BaseQuant.IsExternalRepositoryBaseline || + hasExplicitGroupOverrides; +} + +private Dictionary LoadBaseCarrierTensorMappingsOrThrow( + HybridQuant quant, + IReadOnlyDictionary? temporaryCarrierOverrides, + bool requireFullCoverage) +{ + var blanket = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0 + ? new Dictionary(temporaryCarrierOverrides, StringComparer.Ordinal) + : TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: false); + + if (requireFullCoverage && blanket.Count == 0) + { + throw new InvalidOperationException( + $"Missing full learned base-carrier mapping for baseline '{quant.BaseQuant.Names[0]}'. " + + "Run with --relearn-baseline-mappings before applying learned tensor configurations."); + } + + return blanket; +} private Dictionary TryLoadAllLearnedTensorMappings( string canonicalBaselineKey, TensorWeightScheme? preferredSourceScheme = null, From c82584897fc621fc8f6f4845f8432fb3a126a292 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 27 Apr 2026 14:22:29 -0400 Subject: [PATCH 138/258] yaml update --- MagicQuant/config.dev.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index f765b1c..8018325 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -224,7 +224,7 @@ baselines: quantize_base_name: IQ3_M display_name: UD-Q3_K_XL allow_as_learning_baseline: true - allow_as_combination_carrier: false + allow_as_combination_carrier: true allow_as_explicit_group_candidate: true - file_name: Qwen3.6-35B-A3B-UD-Q4_K_M.gguf @@ -248,7 +248,7 @@ baselines: quantize_base_name: Q4_K_M display_name: UD-Q4_K_XL allow_as_learning_baseline: true - allow_as_combination_carrier: false + allow_as_combination_carrier: true allow_as_explicit_group_candidate: true - file_name: Qwen3.6-35B-A3B-UD-Q5_K_M.gguf From 2c2a5ce0cb7ef636d4792b083282ebecc0239419 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 27 Apr 2026 15:42:53 -0400 Subject: [PATCH 139/258] fixed some logging logic. Also fixed a logic bug where if BF16 didn't finish logits or other tests, but saved that it was done. Additional re-runs didn't validate this, cached logic said it was done, and it would fail future kld down pipeline tests because there was no logits to compare too. This is resolved, it now actually validates the files are needed at startup. --- MagicQuant/Commands/Evolution.cs | 338 +++- MagicQuant/Services/QuantizationService.cs | 1961 ++++++++++---------- 2 files changed, 1301 insertions(+), 998 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index c51cc74..dc8ef38 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -13,6 +13,7 @@ namespace MagicQuant.Commands; public class Evolution : ICommand { + private static readonly string[] RequiredNativeKldDomains = ["general", "code", "math"]; public async Task Run(List args) { @@ -23,20 +24,20 @@ public async Task Run(List args) } string? modelDirRaw = args.FirstOrDefault(a => - string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; + string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; -if (string.IsNullOrWhiteSpace(modelDirRaw)) - modelDirRaw = Config.Current.Paths.ModelDir; + if (string.IsNullOrWhiteSpace(modelDirRaw)) + modelDirRaw = Config.Current.Paths.ModelDir; -if (string.IsNullOrWhiteSpace(modelDirRaw)) -{ - const string msg = "[red]Error:[/] Missing required model directory. Provide [yellow]--model-dir[/] or set [yellow]paths.model_dir[/] in YAML."; - AnsiConsole.MarkupLine(msg); - ShowEvolutionHelp(); - throw new InvalidOperationException("Missing required model directory."); -} + if (string.IsNullOrWhiteSpace(modelDirRaw)) + { + const string msg = "[red]Error:[/] Missing required model directory. Provide [yellow]--model-dir[/] or set [yellow]paths.model_dir[/] in YAML."; + AnsiConsole.MarkupLine(msg); + ShowEvolutionHelp(); + throw new InvalidOperationException("Missing required model directory."); + } -string fullModelPath = Path.GetFullPath(modelDirRaw); + string fullModelPath = Path.GetFullPath(modelDirRaw); if (!Directory.Exists(fullModelPath)) { @@ -59,13 +60,14 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); -Cache.ForceRelearnBaselineTensorMappings = Config.Current.Flags.ForceRelearnBaselineTensorMappings; -Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; -Cache.UseImatrix = Config.Current.Flags.UseImatrix; -Cache.ForceImatrixRebuild = Config.Current.Flags.ForceImatrixRebuild; -RuntimeSearchSpace.ResetForNewModel(); -RuntimeSearchSpace.SetImatrixAvailability(false); -RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; + Cache.ForceRelearnBaselineTensorMappings = Config.Current.Flags.ForceRelearnBaselineTensorMappings; + Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; + Cache.UseImatrix = Config.Current.Flags.UseImatrix; + Cache.ForceImatrixRebuild = Config.Current.Flags.ForceImatrixRebuild; + + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(false); + RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); @@ -86,18 +88,22 @@ public async Task Run(List args) AnsiConsole.MarkupLine("[grey]Acquiring unique model ID...[/]"); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); -AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); + AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); -var pyManager = new PythonManager(Cache.MagicQuantDirectory!); -var customBaselineService = new HuggingFaceBaselineService(pyManager); -var resolvedCustomBaselines = await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); -if (Config.Current.Baselines.CustomRepositories.Any(x => x.Enabled) && resolvedCustomBaselines.Count == 0) -{ - throw new InvalidOperationException("Custom baseline repositories were enabled, but no custom baselines resolved into the runtime registry."); -} -await EnsureSqliteReadyAsync(); + var pyManager = new PythonManager(Cache.MagicQuantDirectory!); -var benchmarkService = new BenchmarkService(pyManager); + var customBaselineService = new HuggingFaceBaselineService(pyManager); + var resolvedCustomBaselines = await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); + + if (Config.Current.Baselines.CustomRepositories.Any(x => x.Enabled) && resolvedCustomBaselines.Count == 0) + { + throw new InvalidOperationException( + "Custom baseline repositories were enabled, but no custom baselines resolved into the runtime registry."); + } + + await EnsureSqliteReadyAsync(); + + var benchmarkService = new BenchmarkService(pyManager); var quantizationService = new QuantizationService(benchmarkService); var imatrixService = new ImatrixService(); @@ -117,16 +123,17 @@ public async Task Run(List args) { UseImatrix = Cache.UseImatrix, ForceRebuild = Cache.ForceImatrixRebuild, -ImatrixUrl = Config.Current.Imatrix.ImatrixUrl, -DatasetRepo = Config.Current.Imatrix.DatasetRepo, -DatasetSplit = Config.Current.Imatrix.DatasetSplit, -DatasetConfig = Config.Current.Imatrix.DatasetConfig, -LocalDatasetFile = Config.Current.Imatrix.DatasetLocalFile, + ImatrixUrl = Config.Current.Imatrix.ImatrixUrl, + DatasetRepo = Config.Current.Imatrix.DatasetRepo, + DatasetSplit = Config.Current.Imatrix.DatasetSplit, + DatasetConfig = Config.Current.Imatrix.DatasetConfig, + LocalDatasetFile = Config.Current.Imatrix.DatasetLocalFile, ModelDirectory = Cache.ModelDirectory!, MagicQuantDirectory = Cache.ModelMagicQuantDirectory! }; var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest, ct: default); + if (imatrixEnsureResult.Enabled) { string canonicalPath = imatrixEnsureResult.CanonicalImatrixPath ?? "n/a"; @@ -151,14 +158,16 @@ await benchmarkService.TryInitializeExecutionPlanFromCacheAsync( { AnsiConsole.MarkupLine("[grey]Cache not usable, preparing probe-only Q8 baseline...[/]"); var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); + await benchmarkService.EnsureExecutionPlanAsync( q8ModelGgufPath, quantizationKey: q8QuantizationKey, forceRediscovery: Cache.ForceRefreshHardwareProbe); } - bool nativeTruthAlreadyLearned = !Cache.ForceRelearnBaselineTensorMappings && - await quantizationService.HasNativeSourceLearnedTruthAsync(); + bool nativeTruthAlreadyLearned = + !Cache.ForceRelearnBaselineTensorMappings && + await quantizationService.HasNativeSourceLearnedTruthAsync(); if (!nativeTruthAlreadyLearned || !loadedPlanFromCache) { @@ -173,28 +182,22 @@ await benchmarkService.EnsureExecutionPlanAsync( await quantizationService.CleanupPureQ8ModelAsync(); var baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); - var baseBenchDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks", baseTypeName); + var benchmarkRootDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks"); + var baseBenchDir = Path.Combine(benchmarkRootDir, baseTypeName); var baseLogitsDir = Path.Combine(baseBenchDir, "logits"); + var pplCorporaDir = Path.Combine(benchmarkRootDir, "_ppl_corpora"); var baseModelQuant = HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()); - if (!nativeTruthAlreadyLearned) - { - await benchmarkService.RunAllBenchmarksAsync( - quantConfig: baseModelQuant, - modelPath: bf16ModelGgufPath, - benchDir: baseBenchDir, - klLogitsDir: baseLogitsDir, - saveLogits: true, - domainsOverride: new[] { "general", "code", "math" }); - - await quantizationService.LearnNativeSourceTruthAsync(bf16ModelGgufPath); - } - else - { - AnsiConsole.MarkupLine( - "[grey]Skipping native BF16 baseline benchmark + relearn because learned native-source truth already exists. Use --relearn-baseline-mappings to force rebuild.[/]"); - } + await EnsureNativeBenchmarkEnvironmentReadyAsync( + benchmarkService: benchmarkService, + quantizationService: quantizationService, + baseModelQuant: baseModelQuant, + bf16ModelGgufPath: bf16ModelGgufPath, + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir, + pplCorporaDir: pplCorporaDir, + nativeTruthAlreadyLearned: nativeTruthAlreadyLearned); var compatibilityService = new ModelCompatibilityService(pyManager); await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); @@ -220,6 +223,7 @@ await benchmarkService.RunAllBenchmarksAsync( var isolationPlanner = new IsolationPlanningService(); var initialPlan = isolationPlanner.BuildInitialPlan(Cache.UnusedTensorGroups); + AnsiConsole.MarkupLine($"[grey]Queued initial startup samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); var initialSummary = await quantizationService.ProcessHybridBatchAsync(initialPlan.Plans); @@ -269,6 +273,7 @@ await benchmarkService.RunAllBenchmarksAsync( } var mergedPlan = initialPlan.MergeWith(continuationPlan); + var archivalGroupIds = TReg.All .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) .Select(x => x.UniqueId) @@ -373,6 +378,225 @@ await benchmarkService.RunAllBenchmarksAsync( AnsiConsole.MarkupLine($"[green]Exported/linkable artifacts:[/] [cyan]{finalizationResult.ExportedArtifacts.Count:N0}[/]"); } + private static async Task EnsureNativeBenchmarkEnvironmentReadyAsync( + BenchmarkService benchmarkService, + QuantizationService quantizationService, + HybridQuant baseModelQuant, + string bf16ModelGgufPath, + string baseBenchDir, + string baseLogitsDir, + string pplCorporaDir, + bool nativeTruthAlreadyLearned) + { + var status = ValidateNativeBenchmarkEnvironment( + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir, + pplCorporaDir: pplCorporaDir, + requiredDomains: RequiredNativeKldDomains); + + bool mustRegenerateNativeBenchmarkArtifacts = + Cache.ForceRelearnBaselineTensorMappings || + !status.IsValid; + + if (mustRegenerateNativeBenchmarkArtifacts) + { + AnsiConsole.Write(new Rule("[yellow]Native BF16 Benchmark/KLD Artifact Validation[/]") { Justification = Justify.Left }); + + if (Cache.ForceRelearnBaselineTensorMappings) + { + AnsiConsole.MarkupLine("[yellow]Forced relearn is ON:[/] native BF16 benchmark/logit artifacts will be regenerated."); + } + else + { + AnsiConsole.MarkupLine("[yellow]Native BF16 benchmark/KLD artifacts are missing or incomplete.[/] Regenerating required artifacts."); + } + + PrintNativeBenchmarkEnvironmentIssues(status); + + await ForceRegenerateNativeBenchmarkArtifactsAsync( + benchmarkService: benchmarkService, + baseModelQuant: baseModelQuant, + bf16ModelGgufPath: bf16ModelGgufPath, + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir); + + status = ValidateNativeBenchmarkEnvironment( + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir, + pplCorporaDir: pplCorporaDir, + requiredDomains: RequiredNativeKldDomains); + + if (!status.IsValid) + { + var details = string.Join( + Environment.NewLine, + status.MissingOrInvalidArtifacts.Select(x => $"- {x}")); + + throw new InvalidOperationException( + "Native BF16 benchmark/logit generation completed, but required native benchmark artifacts are still missing or invalid. " + + "This is fatal because every non-base benchmark requires complete native KLD logits." + + Environment.NewLine + + details); + } + + AnsiConsole.MarkupLine("[green]Native BF16 benchmark/KLD artifacts validated.[/]"); + } + else + { + AnsiConsole.MarkupLine("[grey]Native BF16 benchmark/KLD artifacts already exist and passed validation.[/]"); + } + + if (!nativeTruthAlreadyLearned) + { + await quantizationService.LearnNativeSourceTruthAsync(bf16ModelGgufPath); + } + else + { + AnsiConsole.MarkupLine( + "[grey]Skipping native-source tensor relearn because learned native-source truth already exists.[/]"); + } + } + + private static async Task ForceRegenerateNativeBenchmarkArtifactsAsync( + BenchmarkService benchmarkService, + HybridQuant baseModelQuant, + string bf16ModelGgufPath, + string baseBenchDir, + string baseLogitsDir) + { + if (Directory.Exists(baseBenchDir)) + { + AnsiConsole.MarkupLine( + $"[grey]Clearing incomplete/stale native benchmark directory:[/] {Markup.Escape(baseBenchDir)}"); + + Directory.Delete(baseBenchDir, recursive: true); + } + + Directory.CreateDirectory(baseBenchDir); + Directory.CreateDirectory(baseLogitsDir); + + bool previousSuppressBenchmarkPersistence = Cache.SuppressBenchmarkPersistence; + + try + { + // This is intentional. + // + // If persisted native BF16 benchmark rows already exist in SQLite, the normal + // BenchmarkService path may return DB truth without actually running llama-perplexity, + // which means missing KLD logits would stay missing forever. + // + // Transient mode forces this artifact-repair pass to rely on disk execution instead + // of DB benchmark truth. The native tensor truth is learned separately below. + Cache.SuppressBenchmarkPersistence = true; + + await benchmarkService.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, + modelPath: bf16ModelGgufPath, + benchDir: baseBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: true, + domainsOverride: RequiredNativeKldDomains); + } + finally + { + Cache.SuppressBenchmarkPersistence = previousSuppressBenchmarkPersistence; + } + } + + private static NativeBenchmarkEnvironmentStatus ValidateNativeBenchmarkEnvironment( + string baseBenchDir, + string baseLogitsDir, + string pplCorporaDir, + IReadOnlyCollection requiredDomains) + { + var issues = new List(); + + if (string.IsNullOrWhiteSpace(baseBenchDir)) + { + issues.Add("Native benchmark directory path is null/empty."); + } + else if (!Directory.Exists(baseBenchDir)) + { + issues.Add($"Native benchmark directory does not exist: {baseBenchDir}"); + } + + if (string.IsNullOrWhiteSpace(baseLogitsDir)) + { + issues.Add("Native KLD logits directory path is null/empty."); + } + else if (!Directory.Exists(baseLogitsDir)) + { + issues.Add($"Native KLD logits directory does not exist: {baseLogitsDir}"); + } + + if (string.IsNullOrWhiteSpace(pplCorporaDir)) + { + issues.Add("_ppl_corpora directory path is null/empty."); + } + else if (!Directory.Exists(pplCorporaDir)) + { + issues.Add($"_ppl_corpora directory does not exist: {pplCorporaDir}"); + } + else if (!Directory.EnumerateFiles(pplCorporaDir, "*", SearchOption.AllDirectories).Any()) + { + issues.Add($"_ppl_corpora directory exists but contains no files: {pplCorporaDir}"); + } + + foreach (var domain in requiredDomains.OrderBy(x => x, StringComparer.Ordinal)) + { + if (!string.IsNullOrWhiteSpace(baseBenchDir) && Directory.Exists(baseBenchDir)) + { + var pplLog = Path.Combine(baseBenchDir, $"perplexity_{domain}.log"); + + if (!File.Exists(pplLog)) + { + issues.Add($"Missing native BF16 perplexity log for domain '{domain}': {pplLog}"); + } + else if (new FileInfo(pplLog).Length <= 0) + { + issues.Add($"Native BF16 perplexity log is empty for domain '{domain}': {pplLog}"); + } + } + + if (!string.IsNullOrWhiteSpace(baseLogitsDir) && Directory.Exists(baseLogitsDir)) + { + var logitsFile = Path.Combine(baseLogitsDir, $"kld_logits_{domain}.bin"); + + if (!File.Exists(logitsFile)) + { + issues.Add($"Missing native KLD logits for domain '{domain}': {logitsFile}"); + } + else if (new FileInfo(logitsFile).Length <= 0) + { + issues.Add($"Native KLD logits file is empty for domain '{domain}': {logitsFile}"); + } + } + } + + return new NativeBenchmarkEnvironmentStatus( + IsValid: issues.Count == 0, + MissingOrInvalidArtifacts: issues); + } + + private static void PrintNativeBenchmarkEnvironmentIssues(NativeBenchmarkEnvironmentStatus status) + { + if (status.IsValid) + return; + + foreach (var issue in status.MissingOrInvalidArtifacts.Take(20)) + AnsiConsole.MarkupLine($"[grey]- {Markup.Escape(issue)}[/]"); + + if (status.MissingOrInvalidArtifacts.Count > 20) + { + AnsiConsole.MarkupLine( + $"[grey]- ...and {status.MissingOrInvalidArtifacts.Count - 20:N0} more issue(s).[/]"); + } + } + + private sealed record NativeBenchmarkEnvironmentStatus( + bool IsValid, + IReadOnlyList MissingOrInvalidArtifacts); + private static void PrintIsolationGroupDecisions(IEnumerable decisions) { foreach (var gd in decisions.OrderBy(x => x.GroupName)) @@ -393,7 +617,6 @@ private static void PrintIsolationGroupDecisions(IEnumerable resolvedCustomBaselines, bool hasUsableImatrix) @@ -422,7 +645,8 @@ private static void PrintCustomBaselineRuntimeSummary( bool inCarriers = carriers.Any(x => x.UniqueId == custom.DynamicBaselineId); bool inExplicit = explicitCandidates.Any(x => x.UniqueId == custom.DynamicBaselineId); - AnsiConsole.MarkupLine($" [cyan]{custom.DynamicBaselineId}[/] [yellow]{Markup.Escape(custom.DisplayName)}[/] family={Markup.Escape(custom.BaselineFamily)} file={Markup.Escape(custom.SourceFileName)} learning={inLearning} carrier={inCarriers} explicit={inExplicit}"); + AnsiConsole.MarkupLine( + $" [cyan]{custom.DynamicBaselineId}[/] [yellow]{Markup.Escape(custom.DisplayName)}[/] family={Markup.Escape(custom.BaselineFamily)} file={Markup.Escape(custom.SourceFileName)} learning={inLearning} carrier={inCarriers} explicit={inExplicit}"); } } @@ -441,7 +665,6 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); AnsiConsole.MarkupLine(" [green]--use-imatrix[/] Enable imatrix acquisition/build and allow imatrix-required search candidates (Optional)"); AnsiConsole.MarkupLine(" [green]--allow-high-precision-hybrids[/] Keep BF16/F16 explicit group candidates in final surviving combos (Optional, default false)"); - AnsiConsole.MarkupLine(" [green]--imatrix-force-rebuild[/] Delete/rebuild canonical imatrix artifacts before run (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-url[/] HTTPS URL for direct imatrix artifact download (Optional)"); AnsiConsole.MarkupLine(" [green]--imatrix-dataset-repo[/] Hugging Face dataset repo ID for imatrix generation (Optional)"); @@ -462,7 +685,6 @@ private void ShowEvolutionHelp() AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); } - private static string ResolveAndValidateOutputDirectory() { string resolved; @@ -501,4 +723,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index dc92041..1a4b9ba 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -211,7 +211,8 @@ public async Task ProcessHybridBatchAsync( !string.IsNullOrWhiteSpace(cloneSourceKey) && !string.Equals(cloneSourceKey, plan.Key, StringComparison.Ordinal)) { - var sourcePlan = remainingPlans.First(x => string.Equals(x.Key, cloneSourceKey, StringComparison.Ordinal)); + var sourcePlan = + remainingPlans.First(x => string.Equals(x.Key, cloneSourceKey, StringComparison.Ordinal)); var record = new SampleProcessingRecord { Plan = plan, @@ -316,7 +317,9 @@ public async Task ProcessHybridBatchAsync( if (exactAiModelHashId == null) return (null, null); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, createIfMissing: false, ct); + var imatrixDefinitionId = + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, + createIfMissing: false, ct); var comboId = await db.TensorCombos .AsNoTracking() @@ -339,486 +342,512 @@ public async Task ProcessHybridBatchAsync( var benchmarkId = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == exactAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == comboId) + .Where(x => x.AiModelHashId == exactAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && + x.TensorComboId == comboId) .Select(x => x.Id) .FirstOrDefaultAsync(ct); return (comboId, benchmarkId == Guid.Empty ? null : benchmarkId); } -public async Task ProcessHybridQuantAsync( - HybridQuant quant, - CancellationToken ct = default) -{ - string modelName = GenerateHybridName(quant); - string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); - string modelBenchDir = Path.Combine(_benchDir, modelName); - string baseLogitsDir = GetBaseLogitsDirectory(); - - DateTime startedUtc = DateTime.UtcNow; - var stopwatch = Stopwatch.StartNew(); - var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); - bool pureExternalBaseline = ShouldDownloadExternalBaselineInsteadOfQuantizing(quant); - bool baselineLearnedTruthExists = !forceBaselineRelearn && await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); - string benchmarkModelPath = quantPath; - PreparedExternalBaselineBuild? preparedExternalBaseline = null; - string? transientExternalDownloadPath = null; - - if (!forceBaselineRelearn && baselineLearnedTruthExists && await _benchmarker.TryReuseExistingBenchmarksAsync( - quantConfig: quant, - modelPath: quantPath, - benchDir: modelBenchDir, - klLogitsDir: baseLogitsDir, - domainsOverride: new[] { "general" })) - { - AnsiConsole.MarkupLine($"[grey]Reused existing benchmark artifacts:[/] {Markup.Escape(modelName)}"); - - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - - return SampleProcessState.Skipped; - } - - if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) + public async Task ProcessHybridQuantAsync( + HybridQuant quant, + CancellationToken ct = default) { - AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); + string modelName = GenerateHybridName(quant); + string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); + string modelBenchDir = Path.Combine(_benchDir, modelName); + string baseLogitsDir = GetBaseLogitsDirectory(); + + DateTime startedUtc = DateTime.UtcNow; + var stopwatch = Stopwatch.StartNew(); + var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); + bool pureExternalBaseline = ShouldDownloadExternalBaselineInsteadOfQuantizing(quant); + bool baselineLearnedTruthExists = + !forceBaselineRelearn && await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); + string benchmarkModelPath = quantPath; + PreparedExternalBaselineBuild? preparedExternalBaseline = null; + string? transientExternalDownloadPath = null; + + if (!forceBaselineRelearn && baselineLearnedTruthExists && await _benchmarker.TryReuseExistingBenchmarksAsync( + quantConfig: quant, + modelPath: quantPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + domainsOverride: new[] { "general" })) + { + AnsiConsole.MarkupLine($"[grey]Reused existing benchmark artifacts:[/] {Markup.Escape(modelName)}"); + + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + return SampleProcessState.Skipped; + } - return SampleProcessState.Skipped; - } + if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) + { + AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); - try - { - string inputPath = await GetEffectiveInputModelPathAsync(quant, forceBaselineRelearn, ct); - if (pureExternalBaseline) - transientExternalDownloadPath = inputPath; + if (!IsProtectedModel(modelName)) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - QuantizationExecutionReport? quantizationReport = null; + return SampleProcessState.Skipped; + } - await _cpuQuantLock.WaitAsync(ct); try { + string inputPath = await GetEffectiveInputModelPathAsync(quant, forceBaselineRelearn, ct); if (pureExternalBaseline) - { - preparedExternalBaseline = await PrepareExternalBaselineRebuildAsync( - quant, - downloadedExternalBaselinePath: inputPath, - rebuiltOutputPath: quantPath, - forceBaselineRelearn: forceBaselineRelearn, - ct: ct); + transientExternalDownloadPath = inputPath; - benchmarkModelPath = preparedExternalBaseline.BenchmarkModelPath; - } - else + QuantizationExecutionReport? quantizationReport = null; + + await _cpuQuantLock.WaitAsync(ct); + try { - benchmarkModelPath = quantPath; - if (!File.Exists(quantPath) || forceBaselineRelearn) + if (pureExternalBaseline) { - var quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline - ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) - : quant; + preparedExternalBaseline = await PrepareExternalBaselineRebuildAsync( + quant, + downloadedExternalBaselinePath: inputPath, + rebuiltOutputPath: quantPath, + forceBaselineRelearn: forceBaselineRelearn, + ct: ct); + + benchmarkModelPath = preparedExternalBaseline.BenchmarkModelPath; + } + else + { + benchmarkModelPath = quantPath; + if (!File.Exists(quantPath) || forceBaselineRelearn) + { + var quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline + ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) + : quant; - var effectiveInputPath = quant.BaseQuant.IsExternalRepositoryBaseline - ? await EnsureBaseModelFileAsync() - : inputPath; + var effectiveInputPath = quant.BaseQuant.IsExternalRepositoryBaseline + ? await EnsureBaseModelFileAsync() + : inputPath; - if (quant.BaseQuant.IsExternalRepositoryBaseline) - { - AnsiConsole.MarkupLine( - $"[cyan]Building sample:[/] {Markup.Escape(modelName)} [grey](native input, surrogate carrier={Markup.Escape(quantToExecute.BaseQuant.Names[0])})[/]"); - } - else - { - AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); - } + if (quant.BaseQuant.IsExternalRepositoryBaseline) + { + AnsiConsole.MarkupLine( + $"[cyan]Building sample:[/] {Markup.Escape(modelName)} [grey](native input, surrogate carrier={Markup.Escape(quantToExecute.BaseQuant.Names[0])})[/]"); + } + else + { + AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); + } - quantizationReport = await RunLlamaQuantizeAsync(effectiveInputPath, quantPath, quantToExecute); + quantizationReport = await RunLlamaQuantizeAsync(effectiveInputPath, quantPath, quantToExecute); + } } } - } - finally - { - _cpuQuantLock.Release(); - } - - if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) - { - if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - - return SampleProcessState.Skipped; - } + finally + { + _cpuQuantLock.Release(); + } - AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); + if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) + { + if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - await _benchmarker.RunAllBenchmarksAsync( - quantConfig: quant, - modelPath: benchmarkModelPath, - benchDir: modelBenchDir, - klLogitsDir: baseLogitsDir, - saveLogits: false, - domainsOverride: new[] { "general" }); + return SampleProcessState.Skipped; + } - stopwatch.Stop(); + AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); - await PersistQuantizationRunAsync( - quant: quant, - imatrixDefinitionId: null, - startedUtc: startedUtc, - completedUtc: DateTime.UtcNow, - succeeded: true, - outputModelPath: benchmarkModelPath, - error: null, - ct: ct); + await _benchmarker.RunAllBenchmarksAsync( + quantConfig: quant, + modelPath: benchmarkModelPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: false, + domainsOverride: new[] { "general" }); - if (IsLearnableBaselineRun(quant)) - { - if (preparedExternalBaseline?.HasPreparedLearningTruth == true) - await PersistLearnedBaselineTensorMapFromPreparedAsync(quant, preparedExternalBaseline, ct); - else if (!baselineLearnedTruthExists || forceBaselineRelearn) - await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); - } + stopwatch.Stop(); - return SampleProcessState.Completed; - } - catch (Exception ex) - { - stopwatch.Stop(); - try - { await PersistQuantizationRunAsync( quant: quant, imatrixDefinitionId: null, startedUtc: startedUtc, completedUtc: DateTime.UtcNow, - succeeded: false, + succeeded: true, outputModelPath: benchmarkModelPath, - error: ex.ToString(), + error: null, ct: ct); + + if (IsLearnableBaselineRun(quant)) + { + if (preparedExternalBaseline?.HasPreparedLearningTruth == true) + await PersistLearnedBaselineTensorMapFromPreparedAsync(quant, preparedExternalBaseline, ct); + else if (!baselineLearnedTruthExists || forceBaselineRelearn) + await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); + } + + return SampleProcessState.Completed; } - catch + catch (Exception ex) { + stopwatch.Stop(); + try + { + await PersistQuantizationRunAsync( + quant: quant, + imatrixDefinitionId: null, + startedUtc: startedUtc, + completedUtc: DateTime.UtcNow, + succeeded: false, + outputModelPath: benchmarkModelPath, + error: ex.ToString(), + ct: ct); + } + catch + { + } + + throw; } + finally + { + if (pureExternalBaseline && !string.IsNullOrWhiteSpace(transientExternalDownloadPath)) + await CleanupExternalBaselineDownloadArtifactsAsync(transientExternalDownloadPath); - throw; + if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) + await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); + } } - finally + + private bool ShouldDownloadExternalBaselineInsteadOfQuantizing(HybridQuant quant) + => quant.BaseQuant.IsExternalRepositoryBaseline && quant.Tensors.Count == 0; + + private async Task GetEffectiveInputModelPathAsync(HybridQuant quant, bool forceRefresh, + CancellationToken ct) { - if (pureExternalBaseline && !string.IsNullOrWhiteSpace(transientExternalDownloadPath)) - await CleanupExternalBaselineDownloadArtifactsAsync(transientExternalDownloadPath); + string basePath = await EnsureBaseModelFileAsync(); + if (!quant.BaseQuant.IsExternalRepositoryBaseline) + return basePath; - if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - } -} + // Pure external baselines are downloaded so MagicQuant can learn their tensor truth. + // Any continuation / isolation / hybrid that uses that external baseline must rebuild + // from the native base GGUF instead of requantizing the staged external GGUF. + if (quant.Tensors.Count > 0) + return basePath; -private bool ShouldDownloadExternalBaselineInsteadOfQuantizing(HybridQuant quant) - => quant.BaseQuant.IsExternalRepositoryBaseline && quant.Tensors.Count == 0; + string externalPath = GetExternalBaselineCachePath(quant.BaseQuant); + await _huggingFaceBaselineService.DownloadBaselineAsync(quant.BaseQuant, externalPath, forceRefresh, ct); + await ValidateExternalBaselineTensorParityOrThrow(basePath, externalPath); + return externalPath; + } -private async Task GetEffectiveInputModelPathAsync(HybridQuant quant, bool forceRefresh, CancellationToken ct) -{ - string basePath = await EnsureBaseModelFileAsync(); - if (!quant.BaseQuant.IsExternalRepositoryBaseline) - return basePath; - - // Pure external baselines are downloaded so MagicQuant can learn their tensor truth. - // Any continuation / isolation / hybrid that uses that external baseline must rebuild - // from the native base GGUF instead of requantizing the staged external GGUF. - if (quant.Tensors.Count > 0) - return basePath; - - string externalPath = GetExternalBaselineCachePath(quant.BaseQuant); - await _huggingFaceBaselineService.DownloadBaselineAsync(quant.BaseQuant, externalPath, forceRefresh, ct); - await ValidateExternalBaselineTensorParityOrThrow(basePath, externalPath); - return externalPath; -} + private string GetExternalBaselineCachePath(BaselineQuants baseline) + { + string root = Cache.ExternalBaselineCacheDirectory ?? + Path.Combine(Cache.ModelMagicQuantDirectory!, "ExternalBaselines"); + Directory.CreateDirectory(root); + string safe = + string.Concat(baseline.CanonicalKey.Select(ch => Path.GetInvalidFileNameChars().Contains(ch) ? '_' : ch)); + string extension = Path.GetExtension(baseline.SourceFileName ?? string.Empty); + if (string.IsNullOrWhiteSpace(extension)) + extension = ".gguf"; + return Path.Combine(root, safe + extension); + } -private string GetExternalBaselineCachePath(BaselineQuants baseline) -{ - string root = Cache.ExternalBaselineCacheDirectory ?? Path.Combine(Cache.ModelMagicQuantDirectory!, "ExternalBaselines"); - Directory.CreateDirectory(root); - string safe = string.Concat(baseline.CanonicalKey.Select(ch => Path.GetInvalidFileNameChars().Contains(ch) ? '_' : ch)); - string extension = Path.GetExtension(baseline.SourceFileName ?? string.Empty); - if (string.IsNullOrWhiteSpace(extension)) - extension = ".gguf"; - return Path.Combine(root, safe + extension); -} + private async Task ValidateExternalBaselineTensorParityOrThrow(string baseModelPath, string externalBaselinePath) + { + var baseMeta = await ReadTensorMetadataFromGgufAsync(baseModelPath, externalBaselinePath + ".nativecheck"); + var externalMeta = + await ReadTensorMetadataFromGgufAsync(externalBaselinePath, externalBaselinePath + ".externalcheck"); -private async Task ValidateExternalBaselineTensorParityOrThrow(string baseModelPath, string externalBaselinePath) -{ - var baseMeta = await ReadTensorMetadataFromGgufAsync(baseModelPath, externalBaselinePath + ".nativecheck"); - var externalMeta = await ReadTensorMetadataFromGgufAsync(externalBaselinePath, externalBaselinePath + ".externalcheck"); + var baseNames = baseMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); + var externalNames = externalMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); - var baseNames = baseMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); - var externalNames = externalMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); + var missing = baseNames.Except(externalNames, StringComparer.Ordinal).Take(20).ToList(); + var unexpected = externalNames.Except(baseNames, StringComparer.Ordinal).Take(20).ToList(); - var missing = baseNames.Except(externalNames, StringComparer.Ordinal).Take(20).ToList(); - var unexpected = externalNames.Except(baseNames, StringComparer.Ordinal).Take(20).ToList(); + if (missing.Count > 0 || unexpected.Count > 0 || baseNames.Count != externalNames.Count) + { + throw new InvalidOperationException( + $"External/custom baseline tensor mismatch detected. Missing=[{string.Join(", ", missing)}] Unexpected=[{string.Join(", ", unexpected)}]. " + + "MagicQuant will not persist or use a custom baseline whose tensor names do not exactly match the source model."); + } + } - if (missing.Count > 0 || unexpected.Count > 0 || baseNames.Count != externalNames.Count) + private async Task HasLearnedTruthForBaselineAsync(BaselineQuants baseline, CancellationToken ct = default) { - throw new InvalidOperationException( - $"External/custom baseline tensor mismatch detected. Missing=[{string.Join(", ", missing)}] Unexpected=[{string.Join(", ", unexpected)}]. " + - "MagicQuant will not persist or use a custom baseline whose tensor names do not exactly match the source model."); + if (baseline.UniqueId == BaselineQuants.NativeSourceUniqueId) + return await HasNativeSourceLearnedTruthAsync(ct); + + if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) + return false; + + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + + if (scopedAiModelHashId == null) + return false; + + var query = db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) + .Where(x => x.BaselineCanonicalKey == baseline.CanonicalKey); + + if (baseline.DefaultTensorScheme != null) + query = query.Where(x => x.TensorWeightSchemeId == baseline.DefaultTensorScheme.UniqueId); + + return await query.AnyAsync(ct); } -} -private async Task HasLearnedTruthForBaselineAsync(BaselineQuants baseline, CancellationToken ct = default) -{ - if (baseline.UniqueId == BaselineQuants.NativeSourceUniqueId) - return await HasNativeSourceLearnedTruthAsync(ct); + private async Task PrepareExternalBaselineRebuildAsync( + HybridQuant quant, + string downloadedExternalBaselinePath, + string rebuiltOutputPath, + bool forceBaselineRelearn, + CancellationToken ct) + { + if (!quant.BaseQuant.IsExternalRepositoryBaseline) + throw new InvalidOperationException( + "PrepareExternalBaselineRebuildAsync was called for a non-external baseline."); - if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) - return false; + string nativeBasePath = await EnsureBaseModelFileAsync(); + bool canReuseLearnedTruth = !forceBaselineRelearn && await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); - await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + if (canReuseLearnedTruth) + { + var blanket = TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: false); - if (scopedAiModelHashId == null) - return false; + if (blanket.Count == 0) + throw new InvalidOperationException( + $"Custom baseline '{quant.BaseQuant.Names[0]}' was marked as already learned, but no blanket learned tensor mapping could be loaded."); - var query = db.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) - .Where(x => x.BaselineCanonicalKey == baseline.CanonicalKey); + if (!File.Exists(rebuiltOutputPath) || forceBaselineRelearn) + { + AnsiConsole.MarkupLine( + $"[cyan]Rebuilding normalized custom baseline from learned truth:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); + await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, blanket); + } - if (baseline.DefaultTensorScheme != null) - query = query.Where(x => x.TensorWeightSchemeId == baseline.DefaultTensorScheme.UniqueId); + return new PreparedExternalBaselineBuild + { + BenchmarkModelPath = rebuiltOutputPath, + DownloadedExternalModelPath = downloadedExternalBaselinePath, + HasPreparedLearningTruth = false + }; + } - return await query.AnyAsync(ct); -} + AnsiConsole.MarkupLine( + $"[cyan]Learning external baseline truth from downloaded artifact:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); + await ValidateExternalBaselineTensorParityOrThrow(nativeBasePath, downloadedExternalBaselinePath); -private async Task PrepareExternalBaselineRebuildAsync( - HybridQuant quant, - string downloadedExternalBaselinePath, - string rebuiltOutputPath, - bool forceBaselineRelearn, - CancellationToken ct) -{ - if (!quant.BaseQuant.IsExternalRepositoryBaseline) - throw new InvalidOperationException("PrepareExternalBaselineRebuildAsync was called for a non-external baseline."); + var ggufMetadata = + await ReadTensorMetadataFromGgufAsync(downloadedExternalBaselinePath, rebuiltOutputPath + ".learn"); + var ggufTruth = ggufMetadata.TensorTypes + .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); - string nativeBasePath = await EnsureBaseModelFileAsync(); - bool canReuseLearnedTruth = !forceBaselineRelearn && await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); + if (ggufTruth.Count == 0) + throw new InvalidOperationException( + $"Downloaded external baseline '{quant.BaseQuant.Names[0]}' produced no readable GGUF tensor truth."); - if (canReuseLearnedTruth) - { - var blanket = TryLoadAllLearnedTensorMappings( - canonicalBaselineKey: quant.BaseQuant.CanonicalKey, - preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, - allowDominantFallback: false); + var truth = ggufTruth + .OrderBy(x => x.Key, StringComparer.Ordinal) + .ToDictionary( + x => x.Key, + x => new LearnedTensorTruth(x.Key, x.Value, LearningSource.GgufOnly), + StringComparer.Ordinal); - if (blanket.Count == 0) - throw new InvalidOperationException($"Custom baseline '{quant.BaseQuant.Names[0]}' was marked as already learned, but no blanket learned tensor mapping could be loaded."); + var verification = new TensorTruthVerificationResult + { + TruthByTensor = truth + }; + var audit = _tensorGroupingAuditService.Audit(truth.Keys.ToList(), truth); - if (!File.Exists(rebuiltOutputPath) || forceBaselineRelearn) + if (audit.HasFatalIssues) { - AnsiConsole.MarkupLine($"[cyan]Rebuilding normalized custom baseline from learned truth:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); - await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, blanket); + var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( + baselineName: quant.BaseQuant.Names[0], + schemeName: quant.BaseQuant.DefaultTensorScheme?.Names[0] ?? "external", + sourceKind: quant.BaseQuant.SourceKind.ToString(), + sourceRepository: quant.BaseQuant.SourceRepository, + sourceFileName: quant.BaseQuant.SourceFileName, + truthByTensor: truth, + audit: audit, + verification: verification, + ct: ct); + + AnsiConsole.MarkupLine( + $"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); + throw new InvalidOperationException( + $"Strict tensor-group learning validation failed for external baseline '{quant.BaseQuant.Names[0]}' " + + $"from '{quant.BaseQuant.SourceRepository}/{quant.BaseQuant.SourceFileName}'. " + + $"No normalized rebuilt baseline was produced and no learned tensor mappings were persisted. " + + $"Diagnostic log: {diagnosticPath}"); } + var normalizedOverrides = truth.ToDictionary( + x => x.Key, + x => NativePrecisionNormalization.NormalizeLearnedFinalQuantTypeForApplication(x.Value.FinalQuantType), + StringComparer.Ordinal); + + if (normalizedOverrides.Values.Any(string.IsNullOrWhiteSpace)) + throw new InvalidOperationException( + $"External baseline '{quant.BaseQuant.Names[0]}' produced one or more empty normalized tensor scheme names."); + + AnsiConsole.MarkupLine( + $"[cyan]Rebuilding normalized benchmark artifact for custom baseline:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); + await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, normalizedOverrides); + return new PreparedExternalBaselineBuild { BenchmarkModelPath = rebuiltOutputPath, DownloadedExternalModelPath = downloadedExternalBaselinePath, - HasPreparedLearningTruth = false + TruthByTensor = truth, + GroupedByTensor = audit.GroupedByTensor, + AllTensorNamesInDownloadedArtifact = ggufMetadata.TensorNames, + AmbiguousGroupingRows = audit.Ambiguous, + UnresolvedTensorNames = audit.IllegalUnresolved.Select(x => x.TensorName).ToList(), + BaseQuantExceptionRows = audit.BaseQuantExceptions, + Verification = verification, + HasPreparedLearningTruth = true }; } - AnsiConsole.MarkupLine($"[cyan]Learning external baseline truth from downloaded artifact:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); - await ValidateExternalBaselineTensorParityOrThrow(nativeBasePath, downloadedExternalBaselinePath); - - var ggufMetadata = await ReadTensorMetadataFromGgufAsync(downloadedExternalBaselinePath, rebuiltOutputPath + ".learn"); - var ggufTruth = ggufMetadata.TensorTypes - .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); + private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( + HybridQuant quant, + PreparedExternalBaselineBuild prepared, + CancellationToken ct) + { + if (!IsLearnableBaselineRun(quant) || !prepared.HasPreparedLearningTruth || prepared.TruthByTensor == null || + prepared.GroupedByTensor == null) + return; - if (ggufTruth.Count == 0) - throw new InvalidOperationException($"Downloaded external baseline '{quant.BaseQuant.Names[0]}' produced no readable GGUF tensor truth."); + var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; + var verification = prepared.Verification ?? new TensorTruthVerificationResult + { TruthByTensor = prepared.TruthByTensor }; + var audit = new TensorGroupingAuditResult + { + GroupedByTensor = prepared.GroupedByTensor, + Ambiguous = prepared.AmbiguousGroupingRows ?? [], + IllegalUnresolved = (prepared.UnresolvedTensorNames ?? []).Select(x => new TensorGroupingAuditIssue + { + TensorName = x, + IssueKind = "IllegalUnresolvedTensor" + }).ToList(), + BaseQuantExceptions = prepared.BaseQuantExceptionRows ?? [] + }; - var truth = ggufTruth - .OrderBy(x => x.Key, StringComparer.Ordinal) - .ToDictionary( - x => x.Key, - x => new LearnedTensorTruth(x.Key, x.Value, LearningSource.GgufOnly), - StringComparer.Ordinal); + if (audit.HasFatalIssues || verification.HasFatalIssues) + { + var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( + baselineName: quant.BaseQuant.Names[0], + schemeName: tensorScheme.Names[0], + sourceKind: quant.BaseQuant.SourceKind.ToString(), + sourceRepository: quant.BaseQuant.SourceRepository, + sourceFileName: quant.BaseQuant.SourceFileName, + truthByTensor: prepared.TruthByTensor, + audit: audit, + verification: verification, + ct: ct); - var verification = new TensorTruthVerificationResult - { - TruthByTensor = truth - }; - var audit = _tensorGroupingAuditService.Audit(truth.Keys.ToList(), truth); + AnsiConsole.MarkupLine( + $"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); + throw new InvalidOperationException( + $"Strict tensor-group learning validation failed for external baseline '{quant.BaseQuant.Names[0]}' " + + $"from '{quant.BaseQuant.SourceRepository}/{quant.BaseQuant.SourceFileName}'. " + + $"Prepared external baseline learning truth was invalid. No learned tensor mappings were persisted. " + + $"Diagnostic log: {diagnosticPath}"); + } - if (audit.HasFatalIssues) - { - var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( - baselineName: quant.BaseQuant.Names[0], - schemeName: quant.BaseQuant.DefaultTensorScheme?.Names[0] ?? "external", - sourceKind: quant.BaseQuant.SourceKind.ToString(), - sourceRepository: quant.BaseQuant.SourceRepository, - sourceFileName: quant.BaseQuant.SourceFileName, - truthByTensor: truth, - audit: audit, - verification: verification, - ct: ct); - - AnsiConsole.MarkupLine($"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); - throw new InvalidOperationException( - $"Strict tensor-group learning validation failed for external baseline '{quant.BaseQuant.Names[0]}' " + - $"from '{quant.BaseQuant.SourceRepository}/{quant.BaseQuant.SourceFileName}'. " + - $"No normalized rebuilt baseline was produced and no learned tensor mappings were persisted. " + - $"Diagnostic log: {diagnosticPath}"); - } - - var normalizedOverrides = truth.ToDictionary( - x => x.Key, - x => NativePrecisionNormalization.NormalizeLearnedFinalQuantTypeForApplication(x.Value.FinalQuantType), - StringComparer.Ordinal); - - if (normalizedOverrides.Values.Any(string.IsNullOrWhiteSpace)) - throw new InvalidOperationException($"External baseline '{quant.BaseQuant.Names[0]}' produced one or more empty normalized tensor scheme names."); - - AnsiConsole.MarkupLine($"[cyan]Rebuilding normalized benchmark artifact for custom baseline:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); - await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, normalizedOverrides); - - return new PreparedExternalBaselineBuild - { - BenchmarkModelPath = rebuiltOutputPath, - DownloadedExternalModelPath = downloadedExternalBaselinePath, - TruthByTensor = truth, - GroupedByTensor = audit.GroupedByTensor, - AllTensorNamesInDownloadedArtifact = ggufMetadata.TensorNames, - AmbiguousGroupingRows = audit.Ambiguous, - UnresolvedTensorNames = audit.IllegalUnresolved.Select(x => x.TensorName).ToList(), - BaseQuantExceptionRows = audit.BaseQuantExceptions, - Verification = verification, - HasPreparedLearningTruth = true - }; -} + await using var db = new MagicQuantContext(); -private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( - HybridQuant quant, - PreparedExternalBaselineBuild prepared, - CancellationToken ct) -{ - if (!IsLearnableBaselineRun(quant) || !prepared.HasPreparedLearningTruth || prepared.TruthByTensor == null || prepared.GroupedByTensor == null) - return; + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + throw new InvalidOperationException( + "Unable to persist learned mappings because scoped AiModelHash row was not found."); - var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; - var verification = prepared.Verification ?? new TensorTruthVerificationResult { TruthByTensor = prepared.TruthByTensor }; - var audit = new TensorGroupingAuditResult - { - GroupedByTensor = prepared.GroupedByTensor, - Ambiguous = prepared.AmbiguousGroupingRows ?? [], - IllegalUnresolved = (prepared.UnresolvedTensorNames ?? []).Select(x => new TensorGroupingAuditIssue - { - TensorName = x, - IssueKind = "IllegalUnresolvedTensor" - }).ToList(), - BaseQuantExceptions = prepared.BaseQuantExceptionRows ?? [] - }; + var combo = await db.TensorCombos + .AsNoTracking() + .FirstAsync(x => x.BaseQuant == quant.BaseQuant.UniqueId && + x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && + x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && + x.MoeRouter == 0, ct); - if (audit.HasFatalIssues || verification.HasFatalIssues) - { - var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( - baselineName: quant.BaseQuant.Names[0], - schemeName: tensorScheme.Names[0], - sourceKind: quant.BaseQuant.SourceKind.ToString(), - sourceRepository: quant.BaseQuant.SourceRepository, - sourceFileName: quant.BaseQuant.SourceFileName, - truthByTensor: prepared.TruthByTensor, - audit: audit, - verification: verification, - ct: ct); + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var imatrixDefinitionId = + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId, + createIfMissing: false, ct); - AnsiConsole.MarkupLine($"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); - throw new InvalidOperationException( - $"Strict tensor-group learning validation failed for external baseline '{quant.BaseQuant.Names[0]}' " + - $"from '{quant.BaseQuant.SourceRepository}/{quant.BaseQuant.SourceFileName}'. " + - $"Prepared external baseline learning truth was invalid. No learned tensor mappings were persisted. " + - $"Diagnostic log: {diagnosticPath}"); - } + var benchmarkId = await db.AiBenchmarks + .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && + x.TensorComboId == combo.Id) + .OrderByDescending(x => x.Id) + .Select(x => (Guid?)x.Id) + .FirstOrDefaultAsync(ct); - await using var db = new MagicQuantContext(); + if (!benchmarkId.HasValue) + throw new InvalidOperationException( + $"Unable to persist learned mappings because no AiBenchmark exists for rebuilt baseline '{quant.BaseQuant.Names[0]}'."); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (scopedAiModelHashId == null) - throw new InvalidOperationException("Unable to persist learned mappings because scoped AiModelHash row was not found."); + var rows = prepared.TruthByTensor + .OrderBy(x => x.Key, StringComparer.Ordinal) + .Select(kv => + { + var match = prepared.GroupedByTensor[kv.Key]; - var combo = await db.TensorCombos - .AsNoTracking() - .FirstAsync(x => x.BaseQuant == quant.BaseQuant.UniqueId && - x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && - x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); + return new LearnedBaselineTensorQuant + { + Id = Guid.NewGuid(), + AiBenchmarkId = benchmarkId.Value, + AiModelHashId = scopedAiModelHashId.Value, + BaselineQuantId = quant.BaseQuant.UniqueId, + TensorWeightSchemeId = tensorScheme.UniqueId, + TensorGroupId = match.PrimaryGroup?.UniqueId ?? UnknownTensorGroupId, + BaselineCanonicalKey = quant.BaseQuant.CanonicalKey, + BaselineSourceKind = quant.BaseQuant.SourceKind, + BaselineSourceRepository = quant.BaseQuant.SourceRepository, + BaselineSourceFileName = quant.BaseQuant.SourceFileName, + TensorName = kv.Key, + FinalQuantType = kv.Value.FinalQuantType + }; + }) + .ToList(); - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId, createIfMissing: false, ct); + if (rows.Count == 0) + throw new InvalidOperationException( + $"Prepared learning truth for baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); - var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) - .OrderByDescending(x => x.Id) - .Select(x => (Guid?)x.Id) - .FirstOrDefaultAsync(ct); + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && + x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && + x.TensorWeightSchemeId == tensorScheme.UniqueId) + .ExecuteDeleteAsync(ct); - if (!benchmarkId.HasValue) - throw new InvalidOperationException($"Unable to persist learned mappings because no AiBenchmark exists for rebuilt baseline '{quant.BaseQuant.Names[0]}'."); + db.LearnedBaselineTensorQuants.AddRange(rows); + await db.SaveChangesAsync(ct); - var rows = prepared.TruthByTensor - .OrderBy(x => x.Key, StringComparer.Ordinal) - .Select(kv => - { - var match = prepared.GroupedByTensor[kv.Key]; + await WriteLearningDiagnosticArtifactAsync( + baselineName: quant.BaseQuant.Names[0], + schemeName: tensorScheme.Names[0], + truthByTensor: prepared.TruthByTensor, + grouped: prepared.GroupedByTensor, + allTensorNamesInModel: prepared.AllTensorNamesInDownloadedArtifact ?? prepared.TruthByTensor.Keys.ToList(), + ambiguous: audit.Ambiguous, + unresolved: prepared.UnresolvedTensorNames ?? new List()); - return new LearnedBaselineTensorQuant - { - Id = Guid.NewGuid(), - AiBenchmarkId = benchmarkId.Value, - AiModelHashId = scopedAiModelHashId.Value, - BaselineQuantId = quant.BaseQuant.UniqueId, - TensorWeightSchemeId = tensorScheme.UniqueId, - TensorGroupId = match.PrimaryGroup?.UniqueId ?? UnknownTensorGroupId, - BaselineCanonicalKey = quant.BaseQuant.CanonicalKey, - BaselineSourceKind = quant.BaseQuant.SourceKind, - BaselineSourceRepository = quant.BaseQuant.SourceRepository, - BaselineSourceFileName = quant.BaseQuant.SourceFileName, - TensorName = kv.Key, - FinalQuantType = kv.Value.FinalQuantType - }; - }) - .ToList(); - - if (rows.Count == 0) - throw new InvalidOperationException($"Prepared learning truth for baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); - - await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && - x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && - x.TensorWeightSchemeId == tensorScheme.UniqueId) - .ExecuteDeleteAsync(ct); - - db.LearnedBaselineTensorQuants.AddRange(rows); - await db.SaveChangesAsync(ct); - - await WriteLearningDiagnosticArtifactAsync( - baselineName: quant.BaseQuant.Names[0], - schemeName: tensorScheme.Names[0], - truthByTensor: prepared.TruthByTensor, - grouped: prepared.GroupedByTensor, - allTensorNamesInModel: prepared.AllTensorNamesInDownloadedArtifact ?? prepared.TruthByTensor.Keys.ToList(), - ambiguous: audit.Ambiguous, - unresolved: prepared.UnresolvedTensorNames ?? new List()); - - AnsiConsole.MarkupLine($"[green]Persisted rebuilt custom-baseline learning truth:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); -} + AnsiConsole.MarkupLine( + $"[green]Persisted rebuilt custom-baseline learning truth:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); + } -private async Task CleanupExternalBaselineDownloadArtifactsAsync(string downloadedExternalBaselinePath) -{ - if (string.IsNullOrWhiteSpace(downloadedExternalBaselinePath)) - return; + private async Task CleanupExternalBaselineDownloadArtifactsAsync(string downloadedExternalBaselinePath) + { + if (string.IsNullOrWhiteSpace(downloadedExternalBaselinePath)) + return; - await HardDeleteHelper.DeleteFileIfExistsAsync(downloadedExternalBaselinePath); -} + await HardDeleteHelper.DeleteFileIfExistsAsync(downloadedExternalBaselinePath); + } // ---------------------------------------------------------------- // Benchmark/logit helpers @@ -844,7 +873,9 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok if (exactAiModelHashId == null) return false; - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, createIfMissing: false, ct); + var imatrixDefinitionId = + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, + createIfMissing: false, ct); var bench = await db.AiBenchmarks .AsNoTracking() @@ -883,7 +914,8 @@ private static TensorConfig BuildTensorLookup(HybridQuant quant) return (TensorConfig)quant; } - private static async Task ResolveCurrentScopedAiModelHashIdOrNullAsync(MagicQuantContext db, CancellationToken ct) + private static async Task ResolveCurrentScopedAiModelHashIdOrNullAsync(MagicQuantContext db, + CancellationToken ct) { return await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); } @@ -893,7 +925,8 @@ private static async Task ResolveCurrentScopedAiModelHashIdAsync(MagicQuan return await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); } - private static async Task ResolveCurrentExactAiModelHashIdOrNullAsync(MagicQuantContext db, CancellationToken ct) + private static async Task ResolveCurrentExactAiModelHashIdOrNullAsync(MagicQuantContext db, + CancellationToken ct) { return await ArchitectureFamilyService.ResolveExactCurrentAiModelHashIdOrNullAsync(db, ct); } @@ -956,11 +989,14 @@ private async Task PersistQuantizationRunAsync( await db.SaveChangesAsync(ct); } - imatrixDefinitionId ??= await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, persistenceAiModelHashId, createIfMissing: true, ct); + imatrixDefinitionId ??= + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, persistenceAiModelHashId, + createIfMissing: true, ct); await ImatrixIdentityService.ValidateOwnershipAsync(db, persistenceAiModelHashId, imatrixDefinitionId, ct); Guid? aiBenchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == persistenceAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == tensorCombo.Id) + .Where(x => x.AiModelHashId == persistenceAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && + x.TensorComboId == tensorCombo.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); @@ -1130,13 +1166,14 @@ public async Task BuildExportArtifactFromExactTensorMapAsync( CancellationToken ct = default) { if (tensorTypes == null || tensorTypes.Count == 0) - throw new ArgumentException("A clone tensor map must contain at least one tensor entry.", nameof(tensorTypes)); + throw new ArgumentException("A clone tensor map must contain at least one tensor entry.", + nameof(tensorTypes)); if (string.IsNullOrWhiteSpace(outputPath)) throw new InvalidOperationException("Export output path is required."); var baseQuant = BaselineQuants.ResolveBuiltInStandardBaseline(baseQuantName) - ?? BaselineQuants.Q8_0; + ?? BaselineQuants.Q8_0; Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); @@ -1343,7 +1380,8 @@ private async Task RunLlamaQuantizeWithExactTensorM .Take(20) .ToList(); - if (missingInManifest.Count > 0 || unexpectedInManifest.Count > 0 || inputTensorMetadata.TensorNames.Count != tensorTypes.Count) + if (missingInManifest.Count > 0 || unexpectedInManifest.Count > 0 || + inputTensorMetadata.TensorNames.Count != tensorTypes.Count) { throw new InvalidOperationException( $"Clone tensor manifest does not exactly match this model architecture. " + @@ -1360,7 +1398,8 @@ private async Task RunLlamaQuantizeWithExactTensorM { string imatrixPath = _imatrixService.GetCanonicalImatrixPath(); if (!File.Exists(imatrixPath)) - throw new InvalidOperationException($"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); + throw new InvalidOperationException( + $"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); args.Add($"--imatrix \"{imatrixPath}\""); } @@ -1375,7 +1414,8 @@ private async Task RunLlamaQuantizeWithExactTensorM RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "llama-quantize.exe" : "llama-quantize"); string quantizeLogPath = outputFile + ".quantize.log"; - AnsiConsole.MarkupLine($"[cyan]Quantizing clone artifact:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); + AnsiConsole.MarkupLine( + $"[cyan]Quantizing clone artifact:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); var result = await RunLoggedProcessAsync(new ProcessStartInfo { @@ -1406,7 +1446,8 @@ private async Task RunLlamaQuantizeWithExactTensorM }; } - private async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, HybridQuant quant, IReadOnlyDictionary? temporaryCarrierOverrides = null) + private async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, + HybridQuant quant, IReadOnlyDictionary? temporaryCarrierOverrides = null) { if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); @@ -1428,7 +1469,8 @@ private async Task RunLlamaQuantizeAsync(string inp Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, outputFile); - var requestedOverrides = BuildRequestedTensorOverrides(quant, inputTensorMetadata.TensorNames, temporaryCarrierOverrides); + var requestedOverrides = + BuildRequestedTensorOverrides(quant, inputTensorMetadata.TensorNames, temporaryCarrierOverrides); var concreteOverrides = ResolveConcreteTensorOverrides( allTensorNames: inputTensorMetadata.TensorNames, requestedOverrides: requestedOverrides); @@ -1471,7 +1513,8 @@ private async Task RunLlamaQuantizeAsync(string inp { string imatrixPath = _imatrixService.GetCanonicalImatrixPath(); if (!File.Exists(imatrixPath)) - throw new InvalidOperationException($"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); + throw new InvalidOperationException( + $"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); args.Add($"--imatrix \"{imatrixPath}\""); } @@ -1495,7 +1538,8 @@ private async Task RunLlamaQuantizeAsync(string inp Arguments = arguments }; - AnsiConsole.MarkupLine($"[cyan]Quantizing:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); + AnsiConsole.MarkupLine( + $"[cyan]Quantizing:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); var result = await RunLoggedProcessAsync(psi, quantizeLogPath); if (result.ExitCode != 0) @@ -1545,9 +1589,9 @@ private static HybridQuant CreateEquivalentStandardCarrierQuantForExternalRebuil return quant; var standardFamily = BaselineQuants.ResolveBuiltInStandardBaseline(quant.BaseQuant.QuantizeBaseArgumentName) - ?? BaselineQuants.ResolveBuiltInStandardBaseline(quant.BaseQuant.Names[0]) - ?? throw new InvalidOperationException( - $"Could not resolve a built-in carrier baseline for external baseline '{quant.BaseQuant.Names[0]}' using quantize base name '{quant.BaseQuant.QuantizeBaseArgumentName}'."); + ?? BaselineQuants.ResolveBuiltInStandardBaseline(quant.BaseQuant.Names[0]) + ?? throw new InvalidOperationException( + $"Could not resolve a built-in carrier baseline for external baseline '{quant.BaseQuant.Names[0]}' using quantize base name '{quant.BaseQuant.QuantizeBaseArgumentName}'."); var clone = quant.Clone(); clone.BaseQuant = standardFamily; @@ -1573,7 +1617,8 @@ public async Task ClearLearnedBaselineTensorMappingsAsync(CancellationToken ct = { await using var db = new MagicQuantContext(); int removed = await db.LearnedBaselineTensorQuants.ExecuteDeleteAsync(ct); - AnsiConsole.MarkupLine($"[yellow]Relearn requested:[/] removed [red]{removed:N0}[/] learned baseline tensor mapping rows."); + AnsiConsole.MarkupLine( + $"[yellow]Relearn requested:[/] removed [red]{removed:N0}[/] learned baseline tensor mapping rows."); } public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = default) @@ -1601,7 +1646,8 @@ public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = defaul if (Directory.Exists(debugDir)) Directory.Delete(debugDir, recursive: true); - if (!string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory) && Directory.Exists(Cache.ExternalBaselineCacheDirectory)) + if (!string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory) && + Directory.Exists(Cache.ExternalBaselineCacheDirectory)) Directory.Delete(Cache.ExternalBaselineCacheDirectory, recursive: true); string nativeType = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); @@ -1615,7 +1661,8 @@ public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = defaul if (Directory.Exists(nativeBenchDir)) Directory.Delete(nativeBenchDir, recursive: true); - AnsiConsole.MarkupLine("[yellow]Relearn requested:[/] baseline artifacts, benchmark caches, and learning diagnostics were invalidated."); + AnsiConsole.MarkupLine( + "[yellow]Relearn requested:[/] baseline artifacts, benchmark caches, and learning diagnostics were invalidated."); } public async Task HasNativeSourceLearnedTruthAsync(CancellationToken ct = default) @@ -1640,152 +1687,156 @@ public async Task HasNativeSourceLearnedTruthAsync(CancellationToken ct = } public async Task LearnNativeSourceTruthAsync( - string nativeGgufPath, - CancellationToken ct = default) -{ - if (string.IsNullOrWhiteSpace(nativeGgufPath) || !File.Exists(nativeGgufPath)) - throw new FileNotFoundException($"Native GGUF path not found for learning: {nativeGgufPath}"); - - var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - - if (!Cache.ForceRelearnBaselineTensorMappings) + string nativeGgufPath, + CancellationToken ct = default) { - await using var precheckDb = new MagicQuantContext(); - var precheckScopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(precheckDb, ct); + if (string.IsNullOrWhiteSpace(nativeGgufPath) || !File.Exists(nativeGgufPath)) + throw new FileNotFoundException($"Native GGUF path not found for learning: {nativeGgufPath}"); - if (precheckScopedAiModelHashId != null) + var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + + if (!Cache.ForceRelearnBaselineTensorMappings) { - int existingRows = await precheckDb.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == precheckScopedAiModelHashId.Value && - x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && - x.TensorWeightSchemeId == nativeScheme.UniqueId) - .CountAsync(ct); + await using var precheckDb = new MagicQuantContext(); + var precheckScopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(precheckDb, ct); - if (existingRows > 0) + if (precheckScopedAiModelHashId != null) { - AnsiConsole.MarkupLine( - $"[grey]Native-source learned truth already exists:[/] [cyan]{existingRows:N0}[/] row(s) for [yellow]{Markup.Escape(nativeScheme.Names[0])}[/]. Skipping relearn. Use [green]--relearn-baseline-mappings[/] to regenerate."); - return; + int existingRows = await precheckDb.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == precheckScopedAiModelHashId.Value && + x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + x.TensorWeightSchemeId == nativeScheme.UniqueId) + .CountAsync(ct); + + if (existingRows > 0) + { + AnsiConsole.MarkupLine( + $"[grey]Native-source learned truth already exists:[/] [cyan]{existingRows:N0}[/] row(s) for [yellow]{Markup.Escape(nativeScheme.Names[0])}[/]. Skipping relearn. Use [green]--relearn-baseline-mappings[/] to regenerate."); + return; + } } } - } - var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, nativeGgufPath); - var ggufTruth = metadata.TensorTypes - .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); + var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, nativeGgufPath); + var ggufTruth = metadata.TensorTypes + .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); + + var verification = BuildTruthMapWithVerification( + logTruth: new Dictionary(StringComparer.Ordinal), + ggufTruth: ggufTruth, + baselineName: "NATIVE"); + var truth = verification.TruthByTensor; + + var audit = _tensorGroupingAuditService.Audit(truth.Keys.ToList(), truth); + + if (audit.HasFatalIssues || verification.HasFatalIssues) + { + var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( + baselineName: $"NATIVE_{nativeScheme.Names[0]}", + schemeName: nativeScheme.Names[0], + sourceKind: "NativeSource", + sourceRepository: null, + sourceFileName: Path.GetFileName(nativeGgufPath), + truthByTensor: truth, + audit: audit, + verification: verification, + ct: ct); + + AnsiConsole.MarkupLine( + $"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); + throw new InvalidOperationException( + $"Strict tensor-group learning validation failed for baseline 'NATIVE_{nativeScheme.Names[0]}'. " + + $"Ambiguous={audit.Ambiguous.Count}, IllegalUnresolved={audit.IllegalUnresolved.Count}, " + + $"AllowedBaseQuantFallback={audit.BaseQuantExceptions.Count}. " + + $"No learned tensor mappings were persisted. Diagnostic log: {diagnosticPath}"); + } + + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct) + ?? throw new InvalidOperationException( + "Could not persist native-source learning because scoped AiModelHash row was missing."); + + var combo = await db.TensorCombos + .AsNoTracking() + .FirstOrDefaultAsync(x => x.BaseQuant == BaselineQuants.NativeSourceUniqueId && + x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && + x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && + x.MoeExperts == 0 && x.MoeRouter == 0, ct); + + if (combo == null) + throw new InvalidOperationException( + "Native-source benchmark TensorCombo is missing; benchmark base model first."); + + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( + db, + exactAiModelHashId, + createIfMissing: false, + ct); + + var benchmarkId = await db.AiBenchmarks + .Where(x => x.AiModelHashId == exactAiModelHashId && + x.ImatrixDefinitionId == imatrixDefinitionId && + x.TensorComboId == combo.Id) + .OrderByDescending(x => x.Id) + .Select(x => (Guid?)x.Id) + .FirstOrDefaultAsync(ct); + + if (!benchmarkId.HasValue) + throw new InvalidOperationException( + "Native-source benchmark row is missing; benchmark base model before native-source learning."); + + var rows = truth + .OrderBy(x => x.Key, StringComparer.Ordinal) + .Select(x => + { + var primaryGroup = audit.GroupedByTensor[x.Key].PrimaryGroup; + + return new LearnedBaselineTensorQuant + { + Id = Guid.NewGuid(), + AiBenchmarkId = benchmarkId.Value, + AiModelHashId = scopedAiModelHashId, + BaselineQuantId = BaselineQuants.NativeSourceUniqueId, + TensorWeightSchemeId = nativeScheme.UniqueId, + TensorGroupId = primaryGroup?.UniqueId ?? UnknownTensorGroupId, + TensorName = x.Key, + FinalQuantType = x.Value.FinalQuantType + }; + }) + .ToList(); + + if (rows.Count == 0) + throw new InvalidOperationException("Native-source learning produced no persistable rows."); - var verification = BuildTruthMapWithVerification( - logTruth: new Dictionary(StringComparer.Ordinal), - ggufTruth: ggufTruth, - baselineName: "NATIVE"); - var truth = verification.TruthByTensor; + await db.LearnedBaselineTensorQuants + .Where(x => x.AiModelHashId == scopedAiModelHashId && + x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + x.TensorWeightSchemeId == nativeScheme.UniqueId) + .ExecuteDeleteAsync(ct); - var audit = _tensorGroupingAuditService.Audit(truth.Keys.ToList(), truth); + db.LearnedBaselineTensorQuants.AddRange(rows); + await db.SaveChangesAsync(ct); - if (audit.HasFatalIssues || verification.HasFatalIssues) - { - var diagnosticPath = await _tensorLearningDiagnosticWriter.WriteFailureAsync( + await WriteLearningDiagnosticArtifactAsync( baselineName: $"NATIVE_{nativeScheme.Names[0]}", schemeName: nativeScheme.Names[0], - sourceKind: "NativeSource", - sourceRepository: null, - sourceFileName: Path.GetFileName(nativeGgufPath), truthByTensor: truth, - audit: audit, - verification: verification, - ct: ct); - - AnsiConsole.MarkupLine($"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); - throw new InvalidOperationException( - $"Strict tensor-group learning validation failed for baseline 'NATIVE_{nativeScheme.Names[0]}'. " + - $"Ambiguous={audit.Ambiguous.Count}, IllegalUnresolved={audit.IllegalUnresolved.Count}, " + - $"AllowedBaseQuantFallback={audit.BaseQuantExceptions.Count}. " + - $"No learned tensor mappings were persisted. Diagnostic log: {diagnosticPath}"); - } - - await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct) - ?? throw new InvalidOperationException("Could not persist native-source learning because scoped AiModelHash row was missing."); - - var combo = await db.TensorCombos - .AsNoTracking() - .FirstOrDefaultAsync(x => x.BaseQuant == BaselineQuants.NativeSourceUniqueId && - x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && - x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && - x.MoeExperts == 0 && x.MoeRouter == 0, ct); - - if (combo == null) - throw new InvalidOperationException("Native-source benchmark TensorCombo is missing; benchmark base model first."); - - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( - db, - exactAiModelHashId, - createIfMissing: false, - ct); - - var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == exactAiModelHashId && - x.ImatrixDefinitionId == imatrixDefinitionId && - x.TensorComboId == combo.Id) - .OrderByDescending(x => x.Id) - .Select(x => (Guid?)x.Id) - .FirstOrDefaultAsync(ct); - - if (!benchmarkId.HasValue) - throw new InvalidOperationException("Native-source benchmark row is missing; benchmark base model before native-source learning."); - - var rows = truth - .OrderBy(x => x.Key, StringComparer.Ordinal) - .Select(x => - { - var primaryGroup = audit.GroupedByTensor[x.Key].PrimaryGroup; - - return new LearnedBaselineTensorQuant - { - Id = Guid.NewGuid(), - AiBenchmarkId = benchmarkId.Value, - AiModelHashId = scopedAiModelHashId, - BaselineQuantId = BaselineQuants.NativeSourceUniqueId, - TensorWeightSchemeId = nativeScheme.UniqueId, - TensorGroupId = primaryGroup?.UniqueId ?? UnknownTensorGroupId, - TensorName = x.Key, - FinalQuantType = x.Value.FinalQuantType - }; - }) - .ToList(); - - if (rows.Count == 0) - throw new InvalidOperationException("Native-source learning produced no persistable rows."); - - await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == scopedAiModelHashId && - x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && - x.TensorWeightSchemeId == nativeScheme.UniqueId) - .ExecuteDeleteAsync(ct); - - db.LearnedBaselineTensorQuants.AddRange(rows); - await db.SaveChangesAsync(ct); - - await WriteLearningDiagnosticArtifactAsync( - baselineName: $"NATIVE_{nativeScheme.Names[0]}", - schemeName: nativeScheme.Names[0], - truthByTensor: truth, - grouped: audit.GroupedByTensor, - allTensorNamesInModel: metadata.TensorNames, - ambiguous: audit.Ambiguous, - unresolved: audit.IllegalUnresolved.Select(x => x.TensorName).ToList()); - - var sourcePrecision = nativeScheme.Names[0]; - var distribution = rows.GroupBy(x => x.FinalQuantType) - .OrderByDescending(g => g.Count()) - .Select(g => $"{g.Key}:{g.Count()}") - .ToList(); - - AnsiConsole.MarkupLine( - $"[green]Native-source learned truth:[/] precision={Markup.Escape(sourcePrecision)}, tensors={rows.Count}, unresolved={audit.IllegalUnresolved.Count}, ambiguous={audit.Ambiguous.Count}, baseFallback={audit.BaseQuantExceptions.Count}, dist={Markup.Escape($"[{string.Join(", ", distribution)}]")}"); -} + grouped: audit.GroupedByTensor, + allTensorNamesInModel: metadata.TensorNames, + ambiguous: audit.Ambiguous, + unresolved: audit.IllegalUnresolved.Select(x => x.TensorName).ToList()); + + var sourcePrecision = nativeScheme.Names[0]; + var distribution = rows.GroupBy(x => x.FinalQuantType) + .OrderByDescending(g => g.Count()) + .Select(g => $"{g.Key}:{g.Count()}") + .ToList(); + + AnsiConsole.MarkupLine( + $"[green]Native-source learned truth:[/] precision={Markup.Escape(sourcePrecision)}, tensors={rows.Count}, unresolved={audit.IllegalUnresolved.Count}, ambiguous={audit.Ambiguous.Count}, baseFallback={audit.BaseQuantExceptions.Count}, dist={Markup.Escape($"[{string.Join(", ", distribution)}]")}"); + } private static bool IsLearnableBaselineRun(HybridQuant quant) { @@ -1826,7 +1877,8 @@ private async Task LearnAndPersistBaselineTensorMapAsync( var verification = BuildTruthMapWithVerification(parsed, ggufTruth, quant.BaseQuant.Names[0]); var truth = verification.TruthByTensor; if (truth.Count == 0) - throw new InvalidOperationException($"No verified tensor truth entries were available for baseline '{quant.BaseQuant.Names[0]}'."); + throw new InvalidOperationException( + $"No verified tensor truth entries were available for baseline '{quant.BaseQuant.Names[0]}'."); var audit = _tensorGroupingAuditService.Audit(truth.Keys.ToList(), truth); if (audit.HasFatalIssues || verification.HasFatalIssues) @@ -1842,7 +1894,8 @@ private async Task LearnAndPersistBaselineTensorMapAsync( verification: verification, ct: ct); - AnsiConsole.MarkupLine($"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); + AnsiConsole.MarkupLine( + $"[red]Tensor group learning failed.[/] See diagnostic log: [yellow]{Markup.Escape(diagnosticPath)}[/]"); throw new InvalidOperationException( $"Strict tensor-group learning validation failed for baseline '{quant.BaseQuant.Names[0]}'. " + $"Ambiguous={audit.Ambiguous.Count}, " + @@ -1856,25 +1909,31 @@ private async Task LearnAndPersistBaselineTensorMapAsync( var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); if (scopedAiModelHashId == null) - throw new InvalidOperationException("Unable to persist learned mappings because scoped AiModelHash row was not found."); + throw new InvalidOperationException( + "Unable to persist learned mappings because scoped AiModelHash row was not found."); var combo = await db.TensorCombos .AsNoTracking() .FirstAsync(x => x.BaseQuant == quant.BaseQuant.UniqueId && x.Embeddings == 0 && x.LmHead == 0 && x.AttnQ == 0 && x.AttnKV == 0 && - x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); + x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && + x.MoeRouter == 0, ct); var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId, createIfMissing: false, ct); + var imatrixDefinitionId = + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId, + createIfMissing: false, ct); var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) + .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && + x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); if (!benchmarkId.HasValue) - throw new InvalidOperationException($"Unable to persist learned mappings because no AiBenchmark exists for baseline '{quant.BaseQuant.Names[0]}'."); + throw new InvalidOperationException( + $"Unable to persist learned mappings because no AiBenchmark exists for baseline '{quant.BaseQuant.Names[0]}'."); var rows = truth .OrderBy(x => x.Key, StringComparer.Ordinal) @@ -1901,7 +1960,8 @@ private async Task LearnAndPersistBaselineTensorMapAsync( .ToList(); if (rows.Count == 0) - throw new InvalidOperationException($"Learning baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); + throw new InvalidOperationException( + $"Learning baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); await db.LearnedBaselineTensorQuants .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && @@ -1929,7 +1989,8 @@ private Dictionary ParseQuantizeLogForTensorTypes(string logPath { if (!File.Exists(logPath)) { - AnsiConsole.MarkupLine($"[red]WARNING:[/] quantization log does not exist, cannot learn tensor mapping: {Markup.Escape(logPath)}"); + AnsiConsole.MarkupLine( + $"[red]WARNING:[/] quantization log does not exist, cannot learn tensor mapping: {Markup.Escape(logPath)}"); return new Dictionary(StringComparer.Ordinal); } @@ -1973,8 +2034,8 @@ private TensorTruthVerificationResult BuildTruthMapWithVerification( foreach (var name in allNames) { - bool inLog = logTruth.TryGetValue(name, out var logType); - bool inGguf = ggufTruth.TryGetValue(name, out var ggufType); + var inLog = logTruth.TryGetValue(name, out var logType); + var inGguf = ggufTruth.TryGetValue(name, out var ggufType); if (inLog && inGguf) { @@ -1984,24 +2045,18 @@ private TensorTruthVerificationResult BuildTruthMapWithVerification( } else { + // GGUF is the final artifact truth. The log is secondary evidence only. + // A log/GGUF disagreement is useful diagnostic information, but it is + // not fatal as long as GGUF truth exists. result[name] = new LearnedTensorTruth(name, ggufType!, LearningSource.BothWithMismatch); - if (IsHighSeverityMismatch(logType!, ggufType!)) - hardMismatches.Add(new TensorTruthMismatch - { - TensorName = name, - LogQuantType = logType!, - GgufQuantType = ggufType!, - IsHighSeverity = true - }); - else - softMismatches.Add(new TensorTruthMismatch - { - TensorName = name, - LogQuantType = logType!, - GgufQuantType = ggufType!, - IsHighSeverity = false - }); + softMismatches.Add(new TensorTruthMismatch + { + TensorName = name, + LogQuantType = logType!, + GgufQuantType = ggufType!, + IsHighSeverity = IsHighSeverityMismatch(logType!, ggufType!) + }); } } else if (inGguf) @@ -2010,35 +2065,47 @@ private TensorTruthVerificationResult BuildTruthMapWithVerification( } else if (inLog) { + // Log-only entries are not reliable enough to learn from because there is no + // final GGUF artifact truth confirming them. logOnly.Add($"{name}:{logType}"); } } - if (hardMismatches.Count > 0) + if (softMismatches.Count > 0) { + var highSeverityCount = softMismatches.Count(x => x.IsHighSeverity); + var lowSeverityCount = softMismatches.Count - highSeverityCount; + + var severitySummary = highSeverityCount > 0 && lowSeverityCount > 0 + ? $"{highSeverityCount} high-severity, {lowSeverityCount} low-severity" + : highSeverityCount > 0 + ? $"{highSeverityCount} high-severity" + : $"{lowSeverityCount} low-severity"; + AnsiConsole.MarkupLine( - $"[red]STRICT VALIDATION:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] has {hardMismatches.Count} high-severity GGUF/log mismatches. A failure diagnostic will be written and learning will stop."); - AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", hardMismatches.Take(6).Select(x => $"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}")))}[/]"); - } + $"[yellow]GGUF/log mismatch:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {softMismatches.Count} tensor type disagreement(s) ({severitySummary}). GGUF artifact truth was used."); - if (softMismatches.Count > 0) - { AnsiConsole.MarkupLine( - $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] had {softMismatches.Count} GGUF/log mismatches; GGUF truth was used."); - AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", softMismatches.Take(6).Select(x => $"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}")))}[/]"); + $"[grey]Examples: {Markup.Escape(string.Join(" | ", softMismatches.Take(6).Select(x => $"{x.TensorName}: log={x.LogQuantType} gguf={x.GgufQuantType}")))}[/]"); } if (logOnly.Count > 0) { AnsiConsole.MarkupLine( - $"[yellow]WARNING:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] produced {logOnly.Count} log-only tensor mapping(s) with no GGUF truth. They were ignored."); - AnsiConsole.MarkupLine($"[grey]Examples: {Markup.Escape(string.Join(" | ", logOnly.Take(6)))}[/]"); + $"[yellow]GGUF/log mismatch:[/] Baseline [yellow]{Markup.Escape(baselineName)}[/] produced {logOnly.Count} log-only tensor mapping(s) with no GGUF artifact truth. They were ignored."); + + AnsiConsole.MarkupLine( + $"[grey]Examples: {Markup.Escape(string.Join(" | ", logOnly.Take(6)))}[/]"); } return new TensorTruthVerificationResult { TruthByTensor = result, + + // Deliberately empty for log-vs-GGUF disagreements where GGUF truth exists. + // GGUF wins, so these are diagnostics, not fatal validation failures. HardMismatches = hardMismatches, + SoftMismatches = softMismatches, LogOnly = logOnly }; @@ -2097,7 +2164,8 @@ private async Task WriteLearningDiagnosticArtifactAsync( LearnedTensorCount = learned.Count, UnmatchedExpected = unmatched, UnexpectedLearned = unexpected, - Ambiguous = ambiguous.Where(x => x.MatchedGroups.Contains(group.Name)).Select(x => x.TensorName).Take(20).ToList(), + Ambiguous = ambiguous.Where(x => x.MatchedGroups.Contains(group.Name)).Select(x => x.TensorName) + .Take(20).ToList(), QuantDistribution = distribution, SourceDistribution = sourceCounts }); @@ -2142,7 +2210,8 @@ private async Task WriteLearningDiagnosticArtifactAsync( string debugDir = Path.Combine(_benchDir, "_learning_debug"); Directory.CreateDirectory(debugDir); string path = Path.Combine(debugDir, $"{baselineName}_{schemeName}_learned_map.json"); - await File.WriteAllTextAsync(path, JsonSerializer.Serialize(artifact, new JsonSerializerOptions { WriteIndented = true })); + await File.WriteAllTextAsync(path, + JsonSerializer.Serialize(artifact, new JsonSerializerOptions { WriteIndented = true })); AnsiConsole.MarkupLine( $"[grey]Learned mapping diagnostic written:[/] {Markup.Escape(path)}"); @@ -2173,216 +2242,226 @@ private static HashSet GetExpectedTensorNamesForGroup( } -private List BuildRequestedTensorOverrides( - HybridQuant quant, - IReadOnlyCollection sourceTensorNames, - IReadOnlyDictionary? temporaryCarrierOverrides = null) -{ - var result = new List(); + private List BuildRequestedTensorOverrides( + HybridQuant quant, + IReadOnlyCollection sourceTensorNames, + IReadOnlyDictionary? temporaryCarrierOverrides = null) + { + var result = new List(); - bool hasTemporaryCarrierOverrides = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0; - bool hasExplicitGroupOverrides = quant.Tensors != null && quant.Tensors.Count > 0; - bool shouldApplyBaseCarrierBlanket = ShouldApplyLearnedBaseCarrierBlanket(quant, temporaryCarrierOverrides); + bool hasTemporaryCarrierOverrides = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0; + bool hasExplicitGroupOverrides = quant.Tensors != null && quant.Tensors.Count > 0; + bool shouldApplyBaseCarrierBlanket = ShouldApplyLearnedBaseCarrierBlanket(quant, temporaryCarrierOverrides); - if (!shouldApplyBaseCarrierBlanket) - return result; + if (!shouldApplyBaseCarrierBlanket) + return result; - var baseScheme = TryResolveBaseTensorScheme(quant.BaseQuant); - var blanket = LoadBaseCarrierTensorMappingsOrThrow( - quant: quant, - temporaryCarrierOverrides: temporaryCarrierOverrides, - requireFullCoverage: shouldApplyBaseCarrierBlanket); + var baseScheme = TryResolveBaseTensorScheme(quant.BaseQuant); + var blanket = LoadBaseCarrierTensorMappingsOrThrow( + quant: quant, + temporaryCarrierOverrides: temporaryCarrierOverrides, + requireFullCoverage: shouldApplyBaseCarrierBlanket); - foreach (var kv in blanket.OrderBy(x => x.Key, StringComparer.Ordinal)) - { - result.Add(new RequestedTensorOverride + foreach (var kv in blanket.OrderBy(x => x.Key, StringComparer.Ordinal)) { - GroupName = "base_carrier", - TensorName = kv.Key, - SchemeName = kv.Value - }); - } + result.Add(new RequestedTensorOverride + { + GroupName = "base_carrier", + TensorName = kv.Key, + SchemeName = kv.Value + }); + } - if (!hasExplicitGroupOverrides) - return result; + if (!hasExplicitGroupOverrides) + return result; - foreach (var hybrid in quant.Tensors) - { - if (hybrid?.TGroup == null) - continue; + foreach (var hybrid in quant.Tensors) + { + if (hybrid?.TGroup == null) + continue; - hybrid.ValidateOrThrow(); + hybrid.ValidateOrThrow(); - if (hybrid.MaterializedTensorScheme.UniqueId == TensorWeightScheme.NULL.UniqueId) - continue; + if (hybrid.MaterializedTensorScheme.UniqueId == TensorWeightScheme.NULL.UniqueId) + continue; - var expectedForGroup = GetExpectedTensorNamesForGroup(hybrid.TGroup, sourceTensorNames); - if (expectedForGroup.Count == 0) - continue; + var expectedForGroup = GetExpectedTensorNamesForGroup(hybrid.TGroup, sourceTensorNames); + if (expectedForGroup.Count == 0) + continue; - switch (hybrid.OverrideMode) - { - case HybridTensorOverrideMode.ExactTensorScheme: + switch (hybrid.OverrideMode) { - var exactScheme = hybrid.ExactTensorScheme!; - if (!quant.BaseQuant.IsExternalRepositoryBaseline && baseScheme != null && exactScheme.UniqueId == baseScheme.UniqueId) - continue; - - string schemeName = ResolveSchemeName(exactScheme); - foreach (var tensorName in expectedForGroup.OrderBy(x => x, StringComparer.Ordinal)) + case HybridTensorOverrideMode.ExactTensorScheme: { - result.Add(new RequestedTensorOverride - { - GroupName = hybrid.TGroup.Name, - TensorName = tensorName, - SchemeName = schemeName - }); - } + var exactScheme = hybrid.ExactTensorScheme!; + if (!quant.BaseQuant.IsExternalRepositoryBaseline && baseScheme != null && + exactScheme.UniqueId == baseScheme.UniqueId) + continue; - break; - } + string schemeName = ResolveSchemeName(exactScheme); + foreach (var tensorName in expectedForGroup.OrderBy(x => x, StringComparer.Ordinal)) + { + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + TensorName = tensorName, + SchemeName = schemeName + }); + } - case HybridTensorOverrideMode.LearnedBaselineCandidate: - { - var sourceBaseline = hybrid.CandidateBaseline!; - var learned = TryLoadLearnedTensorMapping( - sourceBaseline: sourceBaseline, - targetGroup: hybrid.TGroup, - preferredSourceScheme: sourceBaseline.DefaultTensorScheme, - allowDominantFallback: false); + break; + } - if (learned.Count == 0) - throw new InvalidOperationException($"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Run with --relearn-baseline-mappings to regenerate."); + case HybridTensorOverrideMode.LearnedBaselineCandidate: + { + var sourceBaseline = hybrid.CandidateBaseline!; + var learned = TryLoadLearnedTensorMapping( + sourceBaseline: sourceBaseline, + targetGroup: hybrid.TGroup, + preferredSourceScheme: sourceBaseline.DefaultTensorScheme, + allowDominantFallback: false); + + if (learned.Count == 0) + throw new InvalidOperationException( + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Run with --relearn-baseline-mappings to regenerate."); - var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); - var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); - var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); + var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); + var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); + var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); - if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) - { - var missingText = missingExpected.Count == 0 ? "none" : string.Join(", ", missingExpected.Take(15)); - var unexpectedText = unexpectedLearned.Count == 0 ? "none" : string.Join(", ", unexpectedLearned.Take(15)); - throw new InvalidOperationException($"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); - } + if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) + { + var missingText = missingExpected.Count == 0 + ? "none" + : string.Join(", ", missingExpected.Take(15)); + var unexpectedText = unexpectedLearned.Count == 0 + ? "none" + : string.Join(", ", unexpectedLearned.Take(15)); + throw new InvalidOperationException( + $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); + } - foreach (var kv in learned.OrderBy(x => x.Key, StringComparer.Ordinal)) - { - result.Add(new RequestedTensorOverride + foreach (var kv in learned.OrderBy(x => x.Key, StringComparer.Ordinal)) { - GroupName = hybrid.TGroup.Name, - TensorName = kv.Key, - SchemeName = kv.Value - }); + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + TensorName = kv.Key, + SchemeName = kv.Value + }); + } + + break; } - break; + default: + throw new InvalidOperationException( + $"Hybrid tensor for group '{hybrid.TGroup.Name}' has unsupported override mode '{hybrid.OverrideMode}'."); } - - default: - throw new InvalidOperationException($"Hybrid tensor for group '{hybrid.TGroup.Name}' has unsupported override mode '{hybrid.OverrideMode}'."); } + + return result; } - return result; -} + private bool ShouldApplyLearnedBaseCarrierBlanket( + HybridQuant quant, + IReadOnlyDictionary? temporaryCarrierOverrides = null) + { + bool hasTemporaryCarrierOverrides = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0; + bool hasExplicitGroupOverrides = quant.Tensors != null && quant.Tensors.Count > 0; -private bool ShouldApplyLearnedBaseCarrierBlanket( - HybridQuant quant, - IReadOnlyDictionary? temporaryCarrierOverrides = null) -{ - bool hasTemporaryCarrierOverrides = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0; - bool hasExplicitGroupOverrides = quant.Tensors != null && quant.Tensors.Count > 0; + return hasTemporaryCarrierOverrides || + quant.BaseQuant.IsExternalRepositoryBaseline || + hasExplicitGroupOverrides; + } - return hasTemporaryCarrierOverrides || - quant.BaseQuant.IsExternalRepositoryBaseline || - hasExplicitGroupOverrides; -} + private Dictionary LoadBaseCarrierTensorMappingsOrThrow( + HybridQuant quant, + IReadOnlyDictionary? temporaryCarrierOverrides, + bool requireFullCoverage) + { + var blanket = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0 + ? new Dictionary(temporaryCarrierOverrides, StringComparer.Ordinal) + : TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: false); -private Dictionary LoadBaseCarrierTensorMappingsOrThrow( - HybridQuant quant, - IReadOnlyDictionary? temporaryCarrierOverrides, - bool requireFullCoverage) -{ - var blanket = temporaryCarrierOverrides != null && temporaryCarrierOverrides.Count > 0 - ? new Dictionary(temporaryCarrierOverrides, StringComparer.Ordinal) - : TryLoadAllLearnedTensorMappings( - canonicalBaselineKey: quant.BaseQuant.CanonicalKey, - preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, - allowDominantFallback: false); + if (requireFullCoverage && blanket.Count == 0) + { + throw new InvalidOperationException( + $"Missing full learned base-carrier mapping for baseline '{quant.BaseQuant.Names[0]}'. " + + "Run with --relearn-baseline-mappings before applying learned tensor configurations."); + } - if (requireFullCoverage && blanket.Count == 0) - { - throw new InvalidOperationException( - $"Missing full learned base-carrier mapping for baseline '{quant.BaseQuant.Names[0]}'. " + - "Run with --relearn-baseline-mappings before applying learned tensor configurations."); + return blanket; } - return blanket; -} -private Dictionary TryLoadAllLearnedTensorMappings( - string canonicalBaselineKey, - TensorWeightScheme? preferredSourceScheme = null, - bool allowDominantFallback = false) -{ - using var db = new MagicQuantContext(); - var scopedAiModelHashId = ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db).GetAwaiter().GetResult(); - if (scopedAiModelHashId == null) - return new Dictionary(StringComparer.Ordinal); - - var allRows = db.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) - .Where(x => x.BaselineCanonicalKey == canonicalBaselineKey) - .OrderBy(x => x.TensorName) - .ToList(); - - if (allRows.Count == 0) - return new Dictionary(StringComparer.Ordinal); - - var rows = allRows; - if (preferredSourceScheme != null) - { - var preferred = allRows.Where(x => x.TensorWeightSchemeId == preferredSourceScheme.UniqueId).ToList(); - if (preferred.Count > 0) - rows = preferred; - else if (!allowDominantFallback) + private Dictionary TryLoadAllLearnedTensorMappings( + string canonicalBaselineKey, + TensorWeightScheme? preferredSourceScheme = null, + bool allowDominantFallback = false) + { + using var db = new MagicQuantContext(); + var scopedAiModelHashId = + ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db).GetAwaiter().GetResult(); + if (scopedAiModelHashId == null) return new Dictionary(StringComparer.Ordinal); - } - if (rows.Select(x => x.TensorWeightSchemeId).Distinct().Count() > 1) - { - if (!allowDominantFallback) + var allRows = db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) + .Where(x => x.BaselineCanonicalKey == canonicalBaselineKey) + .OrderBy(x => x.TensorName) + .ToList(); + + if (allRows.Count == 0) return new Dictionary(StringComparer.Ordinal); - var dominantSchemeId = rows.GroupBy(x => x.TensorWeightSchemeId) - .OrderByDescending(g => g.Count()) - .ThenBy(g => g.Key) - .Select(g => g.Key) - .First(); - rows = rows.Where(x => x.TensorWeightSchemeId == dominantSchemeId).ToList(); - } + var rows = allRows; + if (preferredSourceScheme != null) + { + var preferred = allRows.Where(x => x.TensorWeightSchemeId == preferredSourceScheme.UniqueId).ToList(); + if (preferred.Count > 0) + rows = preferred; + else if (!allowDominantFallback) + return new Dictionary(StringComparer.Ordinal); + } - var result = new Dictionary(StringComparer.Ordinal); + if (rows.Select(x => x.TensorWeightSchemeId).Distinct().Count() > 1) + { + if (!allowDominantFallback) + return new Dictionary(StringComparer.Ordinal); - foreach (var row in rows) - { - var appliedSchemeName = - NativePrecisionNormalization.NormalizeLearnedFinalQuantTypeForApplication(row.FinalQuantType); + var dominantSchemeId = rows.GroupBy(x => x.TensorWeightSchemeId) + .OrderByDescending(g => g.Count()) + .ThenBy(g => g.Key) + .Select(g => g.Key) + .First(); + rows = rows.Where(x => x.TensorWeightSchemeId == dominantSchemeId).ToList(); + } + + var result = new Dictionary(StringComparer.Ordinal); - if (string.IsNullOrWhiteSpace(appliedSchemeName)) + foreach (var row in rows) { - throw new InvalidOperationException( - $"Learned tensor mapping for tensor '{row.TensorName}' on baseline key '{canonicalBaselineKey}' " + - $"returned an empty normalized scheme name. Observed FinalQuantType='{row.FinalQuantType}'."); + var appliedSchemeName = + NativePrecisionNormalization.NormalizeLearnedFinalQuantTypeForApplication(row.FinalQuantType); + + if (string.IsNullOrWhiteSpace(appliedSchemeName)) + { + throw new InvalidOperationException( + $"Learned tensor mapping for tensor '{row.TensorName}' on baseline key '{canonicalBaselineKey}' " + + $"returned an empty normalized scheme name. Observed FinalQuantType='{row.FinalQuantType}'."); + } + + result[row.TensorName] = appliedSchemeName; } - result[row.TensorName] = appliedSchemeName; + return result; } - return result; -} - -private Dictionary TryLoadLearnedTensorMapping( + private Dictionary TryLoadLearnedTensorMapping( BaselineQuants sourceBaseline, TensorGroup targetGroup, TensorWeightScheme? preferredSourceScheme = null, @@ -2390,7 +2469,8 @@ private Dictionary TryLoadLearnedTensorMapping( { using var db = new MagicQuantContext(); - var scopedAiModelHashId = ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db).GetAwaiter().GetResult(); + var scopedAiModelHashId = + ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db).GetAwaiter().GetResult(); if (scopedAiModelHashId == null) return new Dictionary(StringComparer.Ordinal); @@ -2463,41 +2543,41 @@ private Dictionary TryLoadLearnedTensorMapping( } -private List ResolveConcreteTensorOverrides( - IReadOnlyCollection allTensorNames, - List requestedOverrides) -{ - if (requestedOverrides.Count == 0) - return new List(); + private List ResolveConcreteTensorOverrides( + IReadOnlyCollection allTensorNames, + List requestedOverrides) + { + if (requestedOverrides.Count == 0) + return new List(); - var nameSet = allTensorNames.ToHashSet(StringComparer.Ordinal); + var nameSet = allTensorNames.ToHashSet(StringComparer.Ordinal); - var missing = requestedOverrides - .Where(x => !nameSet.Contains(x.TensorName)) - .ToList(); + var missing = requestedOverrides + .Where(x => !nameSet.Contains(x.TensorName)) + .ToList(); - if (missing.Count > 0) - { - throw new InvalidOperationException( - $"Required learned tensor mappings were missing in source GGUF ({missing.Count} tensors). Examples: {string.Join(", ", missing.Take(10).Select(x => x.TensorName))}"); - } + if (missing.Count > 0) + { + throw new InvalidOperationException( + $"Required learned tensor mappings were missing in source GGUF ({missing.Count} tensors). Examples: {string.Join(", ", missing.Take(10).Select(x => x.TensorName))}"); + } - var lastWins = new Dictionary(StringComparer.Ordinal); - foreach (var item in requestedOverrides) - lastWins[item.TensorName] = item; + var lastWins = new Dictionary(StringComparer.Ordinal); + foreach (var item in requestedOverrides) + lastWins[item.TensorName] = item; - return lastWins.Values - .Select(x => new ConcreteTensorOverride - { - GroupName = x.GroupName, - SchemeName = x.SchemeName, - TensorName = x.TensorName - }) - .OrderBy(x => x.TensorName, StringComparer.Ordinal) - .ToList(); -} + return lastWins.Values + .Select(x => new ConcreteTensorOverride + { + GroupName = x.GroupName, + SchemeName = x.SchemeName, + TensorName = x.TensorName + }) + .OrderBy(x => x.TensorName, StringComparer.Ordinal) + .ToList(); + } -private async Task ReadTensorMetadataFromGgufAsync(string ggufPath, string outputFilePath) + private async Task ReadTensorMetadataFromGgufAsync(string ggufPath, string outputFilePath) { string workingDir = Path.GetDirectoryName(outputFilePath)!; string unique = Guid.NewGuid().ToString("N"); @@ -2507,7 +2587,8 @@ private async Task ReadTensorMetadataFromGgufAsync(string try { - await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new { gguf_path = ggufPath, output_path = resultPath })); + await File.WriteAllTextAsync(payloadPath, + JsonSerializer.Serialize(new { gguf_path = ggufPath, output_path = resultPath })); const string py = """ import json @@ -2562,206 +2643,206 @@ with open(output_path, "w", encoding="utf-8") as f: } -private async Task> BuildIsolationDeduplicationPlanAsync( - IReadOnlyCollection plans, - CancellationToken ct) -{ - var result = new Dictionary(StringComparer.Ordinal); - var firstBySignature = new Dictionary(StringComparer.Ordinal); - - foreach (var plan in plans) + private async Task> BuildIsolationDeduplicationPlanAsync( + IReadOnlyCollection plans, + CancellationToken ct) { - string? signature = await TryBuildIsolationEquivalenceKeyAsync(plan, ct); - if (string.IsNullOrWhiteSpace(signature)) - { - result[plan.Key] = plan.Key; - continue; - } + var result = new Dictionary(StringComparer.Ordinal); + var firstBySignature = new Dictionary(StringComparer.Ordinal); - if (!firstBySignature.TryGetValue(signature, out var firstKey)) + foreach (var plan in plans) { - firstBySignature[signature] = plan.Key; - result[plan.Key] = plan.Key; - continue; + string? signature = await TryBuildIsolationEquivalenceKeyAsync(plan, ct); + if (string.IsNullOrWhiteSpace(signature)) + { + result[plan.Key] = plan.Key; + continue; + } + + if (!firstBySignature.TryGetValue(signature, out var firstKey)) + { + firstBySignature[signature] = plan.Key; + result[plan.Key] = plan.Key; + continue; + } + + result[plan.Key] = firstKey; + AnsiConsole.MarkupLine( + $"[grey]Isolation dedupe planned:[/] {Markup.Escape(plan.Key)} -> {Markup.Escape(firstKey)}"); } - result[plan.Key] = firstKey; - AnsiConsole.MarkupLine( - $"[grey]Isolation dedupe planned:[/] {Markup.Escape(plan.Key)} -> {Markup.Escape(firstKey)}"); + return result; } - return result; -} + private async Task TryBuildIsolationEquivalenceKeyAsync( + RequiredSamplePlan plan, + CancellationToken ct) + { + if (plan.Kind != RequiredSampleKind.GroupIsolationProbe && + plan.Kind != RequiredSampleKind.GroupIsolationContinuation) + return null; -private async Task TryBuildIsolationEquivalenceKeyAsync( - RequiredSamplePlan plan, - CancellationToken ct) -{ - if (plan.Kind != RequiredSampleKind.GroupIsolationProbe && - plan.Kind != RequiredSampleKind.GroupIsolationContinuation) - return null; + if (plan.TargetGroupId == null || string.IsNullOrWhiteSpace(plan.TestedCandidateCanonicalKey)) + return null; - if (plan.TargetGroupId == null || string.IsNullOrWhiteSpace(plan.TestedCandidateCanonicalKey)) - return null; + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + return null; + + var rows = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) + .Where(x => x.BaselineCanonicalKey == plan.TestedCandidateCanonicalKey) + .Where(x => x.TensorGroupId == plan.TargetGroupId.Value) + .OrderBy(x => x.TensorName) + .Select(x => new { x.TensorName, x.FinalQuantType }) + .ToListAsync(ct); - await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (scopedAiModelHashId == null) - return null; + if (rows.Count == 0) + return null; - var rows = await db.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) - .Where(x => x.BaselineCanonicalKey == plan.TestedCandidateCanonicalKey) - .Where(x => x.TensorGroupId == plan.TargetGroupId.Value) - .OrderBy(x => x.TensorName) - .Select(x => new { x.TensorName, x.FinalQuantType }) - .ToListAsync(ct); + var sb = new StringBuilder(); + sb.Append("group=").Append(plan.TargetGroupId.Value).Append('|'); + foreach (var row in rows) + { + sb.Append(row.TensorName).Append('=') + .Append(NormalizeLearnedIsolationQuantToken(row.FinalQuantType)) + .Append(';'); + } - if (rows.Count == 0) - return null; + return sb.ToString(); + } - var sb = new StringBuilder(); - sb.Append("group=").Append(plan.TargetGroupId.Value).Append('|'); - foreach (var row in rows) + private static string NormalizeLearnedIsolationQuantToken(string value) { - sb.Append(row.TensorName).Append('=') - .Append(NormalizeLearnedIsolationQuantToken(row.FinalQuantType)) - .Append(';'); + if (string.IsNullOrWhiteSpace(value)) + return string.Empty; + + return value.Trim().Replace(" ", string.Empty).Replace("-", "_").ToUpperInvariant(); } - return sb.ToString(); -} + private async Task CloneEquivalentIsolationBenchmarkAsync( + RequiredSamplePlan sourcePlan, + RequiredSamplePlan duplicatePlan, + CancellationToken ct) + { + var sourceIdentity = await ResolveBenchmarkIdentityAsync(sourcePlan.Quant, ct); + if (sourceIdentity.BenchmarkId == null) + return false; -private static string NormalizeLearnedIsolationQuantToken(string value) -{ - if (string.IsNullOrWhiteSpace(value)) - return string.Empty; + await using var db = new MagicQuantContext(); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); + if (scopedAiModelHashId == null) + return false; - return value.Trim().Replace(" ", string.Empty).Replace("-", "_").ToUpperInvariant(); -} + var sourceBench = await db.AiBenchmarks + .Include(x => x.CategorBenchmarks) + .FirstOrDefaultAsync(x => x.Id == sourceIdentity.BenchmarkId.Value, ct); -private async Task CloneEquivalentIsolationBenchmarkAsync( - RequiredSamplePlan sourcePlan, - RequiredSamplePlan duplicatePlan, - CancellationToken ct) -{ - var sourceIdentity = await ResolveBenchmarkIdentityAsync(sourcePlan.Quant, ct); - if (sourceIdentity.BenchmarkId == null) - return false; - - await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (scopedAiModelHashId == null) - return false; - - var sourceBench = await db.AiBenchmarks - .Include(x => x.CategorBenchmarks) - .FirstOrDefaultAsync(x => x.Id == sourceIdentity.BenchmarkId.Value, ct); - - if (sourceBench == null) - return false; - - var duplicateLookup = BuildTensorLookup(duplicatePlan.Quant); - var duplicateCombo = await db.TensorCombos.FirstOrDefaultAsync(x => - x.BaseQuant == duplicateLookup.BaseQuant && - x.Embeddings == duplicateLookup.Embeddings && - x.LmHead == duplicateLookup.LmHead && - x.AttnQ == duplicateLookup.AttnQ && - x.AttnKV == duplicateLookup.AttnKV && - x.AttnOutput == duplicateLookup.AttnOutput && - x.FfnUpGate == duplicateLookup.FfnUpGate && - x.FfnDown == duplicateLookup.FfnDown && - x.MoeExperts == duplicateLookup.MoeExperts && - x.MoeRouter == duplicateLookup.MoeRouter, ct); - - if (duplicateCombo == null) - { - duplicateCombo = new TensorCombo(duplicateLookup); - db.TensorCombos.Add(duplicateCombo); - await db.SaveChangesAsync(ct); - } + if (sourceBench == null) + return false; - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); - var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( - db, - exactAiModelHashId, - createIfMissing: true, - ct); + var duplicateLookup = BuildTensorLookup(duplicatePlan.Quant); + var duplicateCombo = await db.TensorCombos.FirstOrDefaultAsync(x => + x.BaseQuant == duplicateLookup.BaseQuant && + x.Embeddings == duplicateLookup.Embeddings && + x.LmHead == duplicateLookup.LmHead && + x.AttnQ == duplicateLookup.AttnQ && + x.AttnKV == duplicateLookup.AttnKV && + x.AttnOutput == duplicateLookup.AttnOutput && + x.FfnUpGate == duplicateLookup.FfnUpGate && + x.FfnDown == duplicateLookup.FfnDown && + x.MoeExperts == duplicateLookup.MoeExperts && + x.MoeRouter == duplicateLookup.MoeRouter, ct); + + if (duplicateCombo == null) + { + duplicateCombo = new TensorCombo(duplicateLookup); + db.TensorCombos.Add(duplicateCombo); + await db.SaveChangesAsync(ct); + } - var existing = await db.AiBenchmarks - .FirstOrDefaultAsync(x => x.AiModelHashId == exactAiModelHashId && - x.ImatrixDefinitionId == imatrixDefinitionId && - x.TensorComboId == duplicateCombo.Id, ct); + var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( + db, + exactAiModelHashId, + createIfMissing: true, + ct); - if (existing != null) - return true; + var existing = await db.AiBenchmarks + .FirstOrDefaultAsync(x => x.AiModelHashId == exactAiModelHashId && + x.ImatrixDefinitionId == imatrixDefinitionId && + x.TensorComboId == duplicateCombo.Id, ct); - var clonedBenchmark = new AiBenchmark - { - Id = Guid.NewGuid(), - Ngl = sourceBench.Ngl, - SizeBytes = sourceBench.SizeBytes, - TokensPerSecond = sourceBench.TokensPerSecond, - TensorComboId = duplicateCombo.Id, - AiModelHashId = exactAiModelHashId, - ImatrixDefinitionId = imatrixDefinitionId - }; - db.AiBenchmarks.Add(clonedBenchmark); + if (existing != null) + return true; - var clonedCategories = sourceBench.CategorBenchmarks - .Select(x => new CategoryBenchmark + var clonedBenchmark = new AiBenchmark { Id = Guid.NewGuid(), - AiBenchmarkId = clonedBenchmark.Id, - Category = x.Category, - Kld = x.Kld, - Ppl = x.Ppl, - PplError = x.PplError - }) - .ToList(); - db.AddRange(clonedCategories); - - db.QuantizationRuns.Add(new QuantizationRun - { - Id = Guid.NewGuid(), - AiModelHashId = exactAiModelHashId, - ImatrixDefinitionId = imatrixDefinitionId, - TensorComboId = duplicateCombo.Id, - AiBenchmarkId = clonedBenchmark.Id, - StartedUtc = DateTime.UtcNow, - CompletedUtc = DateTime.UtcNow, - DurationMs = 0, - Succeeded = true, - Error = $"Cloned from equivalent isolation benchmark '{sourcePlan.Key}'.", - OutputModelPath = null - }); - - foreach (var cat in clonedCategories) - { - db.BenchmarkRuns.Add(new BenchmarkRun + Ngl = sourceBench.Ngl, + SizeBytes = sourceBench.SizeBytes, + TokensPerSecond = sourceBench.TokensPerSecond, + TensorComboId = duplicateCombo.Id, + AiModelHashId = exactAiModelHashId, + ImatrixDefinitionId = imatrixDefinitionId + }; + db.AiBenchmarks.Add(clonedBenchmark); + + var clonedCategories = sourceBench.CategorBenchmarks + .Select(x => new CategoryBenchmark + { + Id = Guid.NewGuid(), + AiBenchmarkId = clonedBenchmark.Id, + Category = x.Category, + Kld = x.Kld, + Ppl = x.Ppl, + PplError = x.PplError + }) + .ToList(); + db.AddRange(clonedCategories); + + db.QuantizationRuns.Add(new QuantizationRun { Id = Guid.NewGuid(), AiModelHashId = exactAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = duplicateCombo.Id, AiBenchmarkId = clonedBenchmark.Id, - CategoryBenchmarkId = cat.Id, - Category = cat.Category, StartedUtc = DateTime.UtcNow, CompletedUtc = DateTime.UtcNow, DurationMs = 0, Succeeded = true, - Error = $"Cloned from equivalent isolation benchmark '{sourcePlan.Key}'." + Error = $"Cloned from equivalent isolation benchmark '{sourcePlan.Key}'.", + OutputModelPath = null }); - } - await db.SaveChangesAsync(ct); + foreach (var cat in clonedCategories) + { + db.BenchmarkRuns.Add(new BenchmarkRun + { + Id = Guid.NewGuid(), + AiModelHashId = exactAiModelHashId, + ImatrixDefinitionId = imatrixDefinitionId, + TensorComboId = duplicateCombo.Id, + AiBenchmarkId = clonedBenchmark.Id, + CategoryBenchmarkId = cat.Id, + Category = cat.Category, + StartedUtc = DateTime.UtcNow, + CompletedUtc = DateTime.UtcNow, + DurationMs = 0, + Succeeded = true, + Error = $"Cloned from equivalent isolation benchmark '{sourcePlan.Key}'." + }); + } + + await db.SaveChangesAsync(ct); - AnsiConsole.MarkupLine( - $"[green]Isolation dedupe clone:[/] {Markup.Escape(duplicatePlan.Key)} reused benchmark data from {Markup.Escape(sourcePlan.Key)}"); - return true; -} + AnsiConsole.MarkupLine( + $"[green]Isolation dedupe clone:[/] {Markup.Escape(duplicatePlan.Key)} reused benchmark data from {Markup.Escape(sourcePlan.Key)}"); + return true; + } // ---------------------------------------------------------------- // Internal DTOs @@ -3055,4 +3136,4 @@ void HandleLine(string? line, bool isError) StdErr = stderrBuilder.ToString() }; } -} +} \ No newline at end of file From a5e6a9e2b4c8871f2b5309c28274424ef5361376 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 16:15:46 -0400 Subject: [PATCH 140/258] Add stage progress tracking and parallel batch orchestration --- MagicQuant/Commands/Evolution.cs | 37 ++- MagicQuant/Services/BenchmarkService.cs | 5 + .../Services/HybridArtifactExportService.cs | 32 ++- .../PredictionGuidedHybridSelectionService.cs | 24 +- .../Services/Progress/StageProgressOptions.cs | 13 + .../Progress/StageProgressSnapshot.cs | 13 + .../Services/Progress/StageProgressTracker.cs | 157 +++++++++++ MagicQuant/Services/QuantizationService.cs | 261 ++++++++++-------- 8 files changed, 413 insertions(+), 129 deletions(-) create mode 100644 MagicQuant/Services/Progress/StageProgressOptions.cs create mode 100644 MagicQuant/Services/Progress/StageProgressSnapshot.cs create mode 100644 MagicQuant/Services/Progress/StageProgressTracker.cs diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index dc8ef38..74f683e 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -2,6 +2,7 @@ using MagicQuant.Helpers; using MagicQuant.Models; using MagicQuant.Services; +using MagicQuant.Services.Progress; using MQ.DB; using MQ.DB.Data; using MQ.DB.Models; @@ -226,7 +227,17 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( AnsiConsole.MarkupLine($"[grey]Queued initial startup samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); - var initialSummary = await quantizationService.ProcessHybridBatchAsync(initialPlan.Plans); + var initialSummary = await quantizationService.ProcessHybridBatchAsync( + initialPlan.Plans, + new StageProgressOptions + { + StageName = "Initial isolation startup samples", + Total = initialPlan.TotalCount, + MinimumNonSkippedSamplesBeforeEta = 2, + ShowEta = true, + CountSkippedForEta = false + }, + default); AnsiConsole.MarkupLine("[bold green]Initial startup sampling complete.[/]"); AnsiConsole.MarkupLine($" [green]Completed:[/] {initialSummary.Completed:N0}"); @@ -260,7 +271,17 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( { AnsiConsole.MarkupLine($"[grey]Queued continuation samples:[/] [cyan]{continuationPlan.TotalCount:N0}[/]"); - var continuationSummary = await quantizationService.ProcessHybridBatchAsync(continuationPlan.Plans); + var continuationSummary = await quantizationService.ProcessHybridBatchAsync( + continuationPlan.Plans, + new StageProgressOptions + { + StageName = "Continuation isolation samples", + Total = continuationPlan.TotalCount, + MinimumNonSkippedSamplesBeforeEta = 2, + ShowEta = true, + CountSkippedForEta = false + }, + default); AnsiConsole.MarkupLine("[bold green]Continuation sampling complete.[/]"); AnsiConsole.MarkupLine($" [green]Completed:[/] {continuationSummary.Completed:N0}"); @@ -356,7 +377,17 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( { AnsiConsole.MarkupLine($"[grey]Queued archival isolation samples:[/] [cyan]{archivalCoveragePlan.TotalCount:N0}[/]"); - var archivalCoverageSummary = await quantizationService.ProcessHybridBatchAsync(archivalCoveragePlan.Plans); + var archivalCoverageSummary = await quantizationService.ProcessHybridBatchAsync( + archivalCoveragePlan.Plans, + new StageProgressOptions + { + StageName = "Archival isolation coverage samples", + Total = archivalCoveragePlan.TotalCount, + MinimumNonSkippedSamplesBeforeEta = 2, + ShowEta = true, + CountSkippedForEta = false + }, + default); AnsiConsole.MarkupLine("[bold green]Archival isolation coverage complete.[/]"); AnsiConsole.MarkupLine($" [green]Completed:[/] {archivalCoverageSummary.Completed:N0}"); diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index c912a57..133cc58 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -46,6 +46,11 @@ public class BenchmarkService private static Queue _availableSlots = new(); private static SemaphoreSlim? _slotSemaphore; + public int CurrentParallelSlotCount + { + get { lock (SlotSync) return _currentPlan?.Slots.Count ?? 1; } + } + // ---------------------------------------------------------------- // Construction // ---------------------------------------------------------------- diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index 6f125ea..7638352 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -1,5 +1,6 @@ using MagicQuant.Helpers; using MagicQuant.Models; +using MagicQuant.Services.Progress; using MQ.DB; using MQ.DB.Models; using Spectre.Console; @@ -109,18 +110,39 @@ public async Task> ExportAsync( localBuilds.Add((record, snap.Quant, fullPath, snap.SizeBytes)); } + StageProgressTracker? exportProgress = localBuilds.Count > 0 + ? new StageProgressTracker(new StageProgressOptions + { + StageName = "Final artifact export", + Total = localBuilds.Count, + ShowEta = false, + MinimumPrintInterval = TimeSpan.FromSeconds(5) + }) + : null; + // Kick off all exports together. QuantizationService owns the real concurrency gates, // so this trusts that service to self-regulate CPU/GPU/process pressure. var buildTasks = localBuilds.Select(async item => { - await _quantizationService.BuildExportArtifactAsync(item.Quant, item.FullPath, forceRebuild: true, ct: ct); + string fileName = Path.GetFileName(item.FullPath); + try + { + await _quantizationService.BuildExportArtifactAsync(item.Quant, item.FullPath, forceRebuild: true, ct: ct); - ulong actualBytes = File.Exists(item.FullPath) ? (ulong)new FileInfo(item.FullPath).Length : 0UL; - item.Record.ActualSizeBytes = actualBytes; + ulong actualBytes = File.Exists(item.FullPath) ? (ulong)new FileInfo(item.FullPath).Length : 0UL; + item.Record.ActualSizeBytes = actualBytes; - if (actualBytes != item.ExpectedBytes) + if (actualBytes != item.ExpectedBytes) + { + AnsiConsole.MarkupLine($"[yellow]Export byte validation warning:[/] expected [cyan]{item.ExpectedBytes:N0}[/] but got [cyan]{actualBytes:N0}[/] for {Markup.Escape(fileName)}"); + } + + exportProgress?.ReportFinished(SampleProcessState.Completed, fileName); + } + catch { - AnsiConsole.MarkupLine($"[yellow]Export byte validation warning:[/] expected [cyan]{item.ExpectedBytes:N0}[/] but got [cyan]{actualBytes:N0}[/] for {Markup.Escape(Path.GetFileName(item.FullPath))}"); + exportProgress?.ReportFinished(SampleProcessState.Failed, fileName); + throw; } }); diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index 8f28875..3ff434c 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -1,4 +1,5 @@ using MagicQuant.Models; +using MagicQuant.Services.Progress; using MQ.DB.Models; using Spectre.Console; @@ -305,7 +306,17 @@ private async Task RunInteriorSubspaceDiscoveryAsync( AnsiConsole.MarkupLine($"[grey]Interior candidates selected for batch validation:[/] [cyan]{deduped.Count:N0}[/]"); var quantBatch = deduped.Select(x => x.Prediction.Quant).DistinctBy(x => TensorConfigIdentity.ToKey((TensorConfig)x)).ToList(); - var summary = await _quantizationService.ProcessHybridBatchAsync(quantBatch, ct); + var summary = await _quantizationService.ProcessHybridBatchAsync( + quantBatch, + new StageProgressOptions + { + StageName = "Interior candidate validation batch", + Total = quantBatch.Count, + MinimumNonSkippedSamplesBeforeEta = 2, + ShowEta = true, + CountSkippedForEta = false + }, + ct); AnsiConsole.MarkupLine($"[grey]Interior validation batch:[/] requested={summary.Requested:N0} completed={summary.Completed:N0} skipped={summary.Skipped:N0} failed={summary.Failed:N0}"); foreach (var candidate in deduped) @@ -346,7 +357,16 @@ private async Task BuildAndValidateSingleAsync( $"[grey]Validating candidate:[/] {Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(candidate.Prediction.Quant))} " + $"[grey]| reason=[/] {candidate.Reason} [grey]| window=[/] {Markup.Escape(candidate.WindowLabel)}"); - var summary = await _quantizationService.ProcessHybridBatchAsync(new[] { candidate.Prediction.Quant }, ct); + var summary = await _quantizationService.ProcessHybridBatchAsync( + new[] { candidate.Prediction.Quant }, + new StageProgressOptions + { + StageName = "Single candidate validation", + Total = 1, + ShowEta = false, + MinimumPrintInterval = TimeSpan.FromSeconds(5) + }, + ct); var snapshot = await _repository.LoadBenchmarkSnapshotAsync(candidate.Prediction.Config, ct); bool accepted = snapshot != null && accept(snapshot); diff --git a/MagicQuant/Services/Progress/StageProgressOptions.cs b/MagicQuant/Services/Progress/StageProgressOptions.cs new file mode 100644 index 0000000..51cd2ec --- /dev/null +++ b/MagicQuant/Services/Progress/StageProgressOptions.cs @@ -0,0 +1,13 @@ +namespace MagicQuant.Services.Progress; + +public sealed class StageProgressOptions +{ + public string StageName { get; init; } = string.Empty; + public int Total { get; init; } + public int MinimumNonSkippedSamplesBeforeEta { get; init; } = 2; + public TimeSpan MinimumPrintInterval { get; init; } = TimeSpan.FromSeconds(15); + public int PrintEveryNFinished { get; init; } = 1; + public bool ShowEta { get; init; } = true; + public bool CountSkippedForEta { get; init; } = false; + public bool PrintFinalSummary { get; init; } = true; +} diff --git a/MagicQuant/Services/Progress/StageProgressSnapshot.cs b/MagicQuant/Services/Progress/StageProgressSnapshot.cs new file mode 100644 index 0000000..d9a78ba --- /dev/null +++ b/MagicQuant/Services/Progress/StageProgressSnapshot.cs @@ -0,0 +1,13 @@ +namespace MagicQuant.Services.Progress; + +public readonly record struct StageProgressSnapshot( + string StageName, + int Total, + DateTime StartedUtc, + int Completed, + int Skipped, + int Failed, + int Finished, + DateTime CapturedUtc, + DateTime LastPrintedUtc, + int LastPrintedFinished); diff --git a/MagicQuant/Services/Progress/StageProgressTracker.cs b/MagicQuant/Services/Progress/StageProgressTracker.cs new file mode 100644 index 0000000..06a9990 --- /dev/null +++ b/MagicQuant/Services/Progress/StageProgressTracker.cs @@ -0,0 +1,157 @@ +using Spectre.Console; + +namespace MagicQuant.Services.Progress; + +public sealed class StageProgressTracker +{ + private readonly StageProgressOptions _options; + private readonly object _printSync = new(); + + private int _completed; + private int _skipped; + private int _failed; + private int _lastPrintedFinished; + private DateTime _lastPrintedUtc; + + public StageProgressTracker(StageProgressOptions options) + { + _options = options ?? throw new ArgumentNullException(nameof(options)); + + if (_options.Total < 0) + throw new ArgumentOutOfRangeException(nameof(options.Total), "Total cannot be negative."); + + StageName = string.IsNullOrWhiteSpace(_options.StageName) ? "Stage" : _options.StageName.Trim(); + Total = _options.Total; + StartedUtc = DateTime.UtcNow; + _lastPrintedUtc = StartedUtc; + } + + public string StageName { get; } + public int Total { get; } + public DateTime StartedUtc { get; } + + public StageProgressSnapshot Snapshot + { + get + { + int completed = Volatile.Read(ref _completed); + int skipped = Volatile.Read(ref _skipped); + int failed = Volatile.Read(ref _failed); + int finished = completed + skipped + failed; + return new StageProgressSnapshot( + StageName, + Total, + StartedUtc, + completed, + skipped, + failed, + finished, + DateTime.UtcNow, + _lastPrintedUtc, + Volatile.Read(ref _lastPrintedFinished)); + } + } + + public void ReportFinished(SampleProcessState state, string? itemName = null) + { + switch (state) + { + case SampleProcessState.Completed: + Interlocked.Increment(ref _completed); + break; + case SampleProcessState.Skipped: + Interlocked.Increment(ref _skipped); + break; + default: + Interlocked.Increment(ref _failed); + break; + } + + MaybePrint(state, itemName); + } + + private void MaybePrint(SampleProcessState justFinishedState, string? itemName) + { + var now = DateTime.UtcNow; + + lock (_printSync) + { + int completed = Volatile.Read(ref _completed); + int skipped = Volatile.Read(ref _skipped); + int failed = Volatile.Read(ref _failed); + int finished = completed + skipped + failed; + + bool isFinal = Total > 0 && finished >= Total; + bool intervalElapsed = now - _lastPrintedUtc >= _options.MinimumPrintInterval; + bool countThresholdHit = finished - _lastPrintedFinished >= Math.Max(1, _options.PrintEveryNFinished); + bool rapidSkipStorm = justFinishedState == SampleProcessState.Skipped && + finished - _lastPrintedFinished < Math.Max(1, _options.PrintEveryNFinished) && + !intervalElapsed && + !isFinal; + + if (!isFinal && !intervalElapsed && (!countThresholdHit || rapidSkipStorm)) + return; + + if (!isFinal && finished == _lastPrintedFinished) + return; + + string escapedStage = Markup.Escape(StageName); + + if (!_options.ShowEta) + { + AnsiConsole.MarkupLine($"[grey][progress][/]{escapedStage}: [cyan]{finished}[/]/[cyan]{Total}[/] local GGUF outputs built"); + } + else + { + var elapsed = now - StartedUtc; + string elapsedText = FormatDuration(elapsed); + + int etaSampleCount = _options.CountSkippedForEta ? finished : completed + failed; + string etaText = "ETA warming up..."; + string estFinishText = "est finish UTC n/a"; + + if (elapsed.TotalSeconds > 0 && etaSampleCount >= Math.Max(1, _options.MinimumNonSkippedSamplesBeforeEta)) + { + double rate = etaSampleCount / elapsed.TotalSeconds; + int remaining = Math.Max(0, Total - finished); + if (rate > 0) + { + var estimatedRemaining = TimeSpan.FromSeconds(remaining / rate); + var estimatedFinishUtc = now + estimatedRemaining; + etaText = $"ETA {FormatEta(estimatedRemaining)}"; + estFinishText = $"est finish UTC {estimatedFinishUtc:yyyy-MM-dd HH:mm}"; + } + } + + string maybeItem = string.IsNullOrWhiteSpace(itemName) + ? string.Empty + : $" | item={Markup.Escape(itemName)}"; + + AnsiConsole.MarkupLine( + $"[grey][progress][/]{escapedStage}: [cyan]{finished}[/]/[cyan]{Total}[/] done | completed=[green]{completed}[/] skipped=[yellow]{skipped}[/] failed=[red]{failed}[/] | elapsed={elapsedText} | {etaText} | {estFinishText}{maybeItem}"); + } + + _lastPrintedUtc = now; + _lastPrintedFinished = finished; + } + } + + private static string FormatDuration(TimeSpan duration) + { + if (duration.TotalDays >= 1) + return $"{(int)duration.TotalDays}d {duration.Hours:00}h {duration.Minutes:00}m"; + + return $"{duration.Hours:00}h {duration.Minutes:00}m {duration.Seconds:00}s"; + } + + private static string FormatEta(TimeSpan duration) + { + if (duration.TotalDays >= 1) + return $"{(int)duration.TotalDays}d {duration.Hours:00}h {duration.Minutes:00}m"; + + if (duration.TotalHours >= 1) + return $"{duration.Hours:00}h {duration.Minutes:00}m"; + + return $"{duration.Minutes:00}m"; + } +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 1a4b9ba..30e1bc9 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -7,6 +7,7 @@ using MagicQuant.Helpers; using MagicQuant.Models.Learning; using MagicQuant.Services.Learning; +using MagicQuant.Services.Progress; using MQ.DB; using MQ.DB.Data; using MQ.DB.Models; @@ -138,17 +139,32 @@ public async Task ProcessHybridBatchAsync( return await ProcessHybridBatchAsync(shimmedPlans, ct); } + public Task ProcessHybridBatchAsync( + IReadOnlyCollection plans, + CancellationToken ct = default) + => ProcessHybridBatchAsync(plans, progressOptions: null, ct); + public async Task ProcessHybridBatchAsync( IReadOnlyCollection plans, + StageProgressOptions? progressOptions, CancellationToken ct = default) { if (plans == null) throw new ArgumentNullException(nameof(plans)); - int completed = 0; - int skipped = 0; - int failed = 0; + if (plans.Count == 0) + { + return new SampleProcessingSummary + { + Requested = 0, + Records = new List() + }; + } + var records = new ConcurrentBag(); + var stageProgress = progressOptions != null && progressOptions.Total > 0 + ? new StageProgressTracker(progressOptions) + : null; await EnsureBaseModelFileAsync(false); @@ -159,146 +175,153 @@ public async Task ProcessHybridBatchAsync( foreach (var baselinePlan in learnableBaselinePlans) { - var baselineRecord = new SampleProcessingRecord - { - Plan = baselinePlan, - ModelName = GenerateHybridName(baselinePlan.Quant) - }; + ct.ThrowIfCancellationRequested(); + records.Add(await ExecutePlanAsync(baselinePlan, stageProgress, ct)); + } - try - { - var state = await ProcessHybridQuantAsync(baselinePlan.Quant, ct); - baselineRecord.State = state; + var remainingPlans = plans.Except(learnableBaselinePlans).ToList(); + var equivalenceMap = await BuildIsolationDeduplicationPlanAsync(remainingPlans, ct); - var identity = await ResolveBenchmarkIdentityAsync(baselinePlan.Quant, ct); - baselineRecord.TensorComboId = identity.TensorComboId; - baselineRecord.BenchmarkId = identity.BenchmarkId; + var planByKey = remainingPlans.ToDictionary(p => p.Key, StringComparer.Ordinal); + var primaryGroups = new List<(RequiredSamplePlan Source, List Duplicates)>(); - switch (state) - { - case SampleProcessState.Completed: - completed++; - break; - case SampleProcessState.Skipped: - skipped++; - break; - default: - failed++; - break; - } - } - catch (Exception ex) - { - baselineRecord.State = SampleProcessState.Failed; - baselineRecord.Error = ex.Message; - failed++; + foreach (var plan in remainingPlans) + { + ct.ThrowIfCancellationRequested(); - AnsiConsole.MarkupLine($"[red]Baseline sample failed:[/] {Markup.Escape(baselineRecord.ModelName)}"); - AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); + if (!equivalenceMap.TryGetValue(plan.Key, out var sourceKey) || + string.IsNullOrWhiteSpace(sourceKey) || + string.Equals(sourceKey, plan.Key, StringComparison.Ordinal)) + { + primaryGroups.Add((plan, new List())); + continue; } - finally + + if (!planByKey.TryGetValue(sourceKey, out _)) { - records.Add(baselineRecord); + primaryGroups.Add((plan, new List())); } } - var remainingPlans = plans.Except(learnableBaselinePlans).ToList(); - var equivalenceMap = await BuildIsolationDeduplicationPlanAsync(remainingPlans, ct); - + var groupBySource = primaryGroups.ToDictionary(g => g.Source.Key, g => g, StringComparer.Ordinal); foreach (var plan in remainingPlans) { - if (equivalenceMap.TryGetValue(plan.Key, out var cloneSourceKey) && - !string.IsNullOrWhiteSpace(cloneSourceKey) && - !string.Equals(cloneSourceKey, plan.Key, StringComparison.Ordinal)) + if (!equivalenceMap.TryGetValue(plan.Key, out var sourceKey) || + string.IsNullOrWhiteSpace(sourceKey) || + string.Equals(sourceKey, plan.Key, StringComparison.Ordinal)) { - var sourcePlan = - remainingPlans.First(x => string.Equals(x.Key, cloneSourceKey, StringComparison.Ordinal)); - var record = new SampleProcessingRecord - { - Plan = plan, - ModelName = GenerateHybridName(plan.Quant) - }; - - try - { - bool cloned = await CloneEquivalentIsolationBenchmarkAsync(sourcePlan, plan, ct); - if (cloned) - { - var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, ct); - record.State = SampleProcessState.Completed; - record.TensorComboId = identity.TensorComboId; - record.BenchmarkId = identity.BenchmarkId; - completed++; - } - else - { - var state = await ProcessHybridQuantAsync(plan.Quant, ct); - record.State = state; - var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, ct); - record.TensorComboId = identity.TensorComboId; - record.BenchmarkId = identity.BenchmarkId; - if (state == SampleProcessState.Completed) completed++; - else if (state == SampleProcessState.Skipped) skipped++; - else failed++; - } - } - catch (Exception ex) - { - record.State = SampleProcessState.Failed; - record.Error = ex.Message; - failed++; - AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(record.ModelName)}"); - AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); - } - finally - { - records.Add(record); - } - continue; } - var recordPrimary = new SampleProcessingRecord - { - Plan = plan, - ModelName = GenerateHybridName(plan.Quant) - }; + if (groupBySource.TryGetValue(sourceKey, out var group)) + group.Duplicates.Add(plan); + } - try + int workerCount = Math.Max(1, Math.Min(primaryGroups.Count, + _maxConcurrentQuantizations + _benchmarker.CurrentParallelSlotCount)); + + await Parallel.ForEachAsync( + primaryGroups, + new ParallelOptions { MaxDegreeOfParallelism = workerCount, CancellationToken = ct }, + async (group, token) => { - var state = await ProcessHybridQuantAsync(plan.Quant, ct); - recordPrimary.State = state; + records.Add(await ExecutePlanAsync(group.Source, stageProgress, token)); - var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, ct); - recordPrimary.TensorComboId = identity.TensorComboId; - recordPrimary.BenchmarkId = identity.BenchmarkId; + foreach (var duplicatePlan in group.Duplicates) + { + token.ThrowIfCancellationRequested(); + records.Add(await ExecuteDuplicatePlanAsync(group.Source, duplicatePlan, stageProgress, token)); + } + }); - if (state == SampleProcessState.Completed) completed++; - else if (state == SampleProcessState.Skipped) skipped++; - else failed++; - } - catch (Exception ex) - { - recordPrimary.State = SampleProcessState.Failed; - recordPrimary.Error = ex.Message; - failed++; - AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(recordPrimary.ModelName)}"); - AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); - } - finally - { - records.Add(recordPrimary); - } - } + var finalRecords = records.OrderBy(x => x.Plan.Key, StringComparer.Ordinal).ToList(); return new SampleProcessingSummary { Requested = plans.Count, - Completed = completed, - Skipped = skipped, - Failed = failed, - Records = records.OrderBy(x => x.Plan.Key).ToList() + Completed = finalRecords.Count(x => x.State == SampleProcessState.Completed), + Skipped = finalRecords.Count(x => x.State == SampleProcessState.Skipped), + Failed = finalRecords.Count(x => x.State == SampleProcessState.Failed), + Records = finalRecords + }; + } + + + private async Task ExecutePlanAsync( + RequiredSamplePlan plan, + StageProgressTracker? progress, + CancellationToken ct) + { + var record = new SampleProcessingRecord + { + Plan = plan, + ModelName = GenerateHybridName(plan.Quant) + }; + + try + { + var state = await ProcessHybridQuantAsync(plan.Quant, ct); + record.State = state; + + var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, ct); + record.TensorComboId = identity.TensorComboId; + record.BenchmarkId = identity.BenchmarkId; + + progress?.ReportFinished(state, record.ModelName); + return record; + } + catch (Exception ex) + { + record.State = SampleProcessState.Failed; + record.Error = ex.Message; + + AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(record.ModelName)}"); + AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); + + progress?.ReportFinished(SampleProcessState.Failed, record.ModelName); + return record; + } + } + + private async Task ExecuteDuplicatePlanAsync( + RequiredSamplePlan sourcePlan, + RequiredSamplePlan duplicatePlan, + StageProgressTracker? progress, + CancellationToken ct) + { + var record = new SampleProcessingRecord + { + Plan = duplicatePlan, + ModelName = GenerateHybridName(duplicatePlan.Quant) }; + + try + { + bool cloned = await CloneEquivalentIsolationBenchmarkAsync(sourcePlan, duplicatePlan, ct); + + if (cloned) + { + var identity = await ResolveBenchmarkIdentityAsync(duplicatePlan.Quant, ct); + record.State = SampleProcessState.Completed; + record.TensorComboId = identity.TensorComboId; + record.BenchmarkId = identity.BenchmarkId; + progress?.ReportFinished(SampleProcessState.Completed, record.ModelName); + return record; + } + + return await ExecutePlanAsync(duplicatePlan, progress, ct); + } + catch (Exception ex) + { + record.State = SampleProcessState.Failed; + record.Error = ex.Message; + + AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(record.ModelName)}"); + AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); + + progress?.ReportFinished(SampleProcessState.Failed, record.ModelName); + return record; + } } private async Task<(Guid? TensorComboId, Guid? BenchmarkId)> ResolveBenchmarkIdentityAsync( From f47eb48f8e0de2ac8996a7a374abfb2a7d9acaf4 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 16:19:31 -0400 Subject: [PATCH 141/258] Add HybridQuant progress overload for ProcessHybridBatchAsync --- MagicQuant/Services/QuantizationService.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 30e1bc9..8c307f1 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -119,8 +119,14 @@ public static void ValidateQuantNameNormalizationOrThrow() // Batch processing // ---------------------------------------------------------------- + public Task ProcessHybridBatchAsync( + IReadOnlyCollection quants, + CancellationToken ct = default) + => ProcessHybridBatchAsync(quants, progressOptions: null, ct); + public async Task ProcessHybridBatchAsync( IReadOnlyCollection quants, + StageProgressOptions? progressOptions, CancellationToken ct = default) { if (quants == null) @@ -136,7 +142,7 @@ public async Task ProcessHybridBatchAsync( }) .ToList(); - return await ProcessHybridBatchAsync(shimmedPlans, ct); + return await ProcessHybridBatchAsync(shimmedPlans, progressOptions, ct); } public Task ProcessHybridBatchAsync( From 46f5a8d716832b31008d2481fd5123349d7c8309 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 16:23:56 -0400 Subject: [PATCH 142/258] Generalize non-ETA progress labels and remove single-item tracker --- MagicQuant/Services/HybridArtifactExportService.cs | 3 ++- .../PredictionGuidedHybridSelectionService.cs | 11 +---------- MagicQuant/Services/Progress/StageProgressOptions.cs | 1 + MagicQuant/Services/Progress/StageProgressTracker.cs | 6 +++++- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index 7638352..a19fc1f 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -116,7 +116,8 @@ public async Task> ExportAsync( StageName = "Final artifact export", Total = localBuilds.Count, ShowEta = false, - MinimumPrintInterval = TimeSpan.FromSeconds(5) + MinimumPrintInterval = TimeSpan.FromSeconds(5), + UnitLabel = "local GGUF outputs built" }) : null; diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index 3ff434c..afe02c0 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -357,16 +357,7 @@ private async Task BuildAndValidateSingleAsync( $"[grey]Validating candidate:[/] {Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(candidate.Prediction.Quant))} " + $"[grey]| reason=[/] {candidate.Reason} [grey]| window=[/] {Markup.Escape(candidate.WindowLabel)}"); - var summary = await _quantizationService.ProcessHybridBatchAsync( - new[] { candidate.Prediction.Quant }, - new StageProgressOptions - { - StageName = "Single candidate validation", - Total = 1, - ShowEta = false, - MinimumPrintInterval = TimeSpan.FromSeconds(5) - }, - ct); + var summary = await _quantizationService.ProcessHybridBatchAsync(new[] { candidate.Prediction.Quant }, ct); var snapshot = await _repository.LoadBenchmarkSnapshotAsync(candidate.Prediction.Config, ct); bool accepted = snapshot != null && accept(snapshot); diff --git a/MagicQuant/Services/Progress/StageProgressOptions.cs b/MagicQuant/Services/Progress/StageProgressOptions.cs index 51cd2ec..009defe 100644 --- a/MagicQuant/Services/Progress/StageProgressOptions.cs +++ b/MagicQuant/Services/Progress/StageProgressOptions.cs @@ -10,4 +10,5 @@ public sealed class StageProgressOptions public bool ShowEta { get; init; } = true; public bool CountSkippedForEta { get; init; } = false; public bool PrintFinalSummary { get; init; } = true; + public string? UnitLabel { get; init; } } diff --git a/MagicQuant/Services/Progress/StageProgressTracker.cs b/MagicQuant/Services/Progress/StageProgressTracker.cs index 06a9990..108f839 100644 --- a/MagicQuant/Services/Progress/StageProgressTracker.cs +++ b/MagicQuant/Services/Progress/StageProgressTracker.cs @@ -99,7 +99,11 @@ private void MaybePrint(SampleProcessState justFinishedState, string? itemName) if (!_options.ShowEta) { - AnsiConsole.MarkupLine($"[grey][progress][/]{escapedStage}: [cyan]{finished}[/]/[cyan]{Total}[/] local GGUF outputs built"); + var unitLabel = string.IsNullOrWhiteSpace(_options.UnitLabel) + ? "items finished" + : _options.UnitLabel.Trim(); + + AnsiConsole.MarkupLine($"[grey][progress][/]{escapedStage}: [cyan]{finished}[/]/[cyan]{Total}[/] {Markup.Escape(unitLabel)}"); } else { From 7443f635fb35846ef789d0173246f969b7c5bc4a Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 16:27:32 -0400 Subject: [PATCH 143/258] Escape literal [progress] label in Spectre markup --- MagicQuant/Services/Progress/StageProgressTracker.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Services/Progress/StageProgressTracker.cs b/MagicQuant/Services/Progress/StageProgressTracker.cs index 108f839..3337ff5 100644 --- a/MagicQuant/Services/Progress/StageProgressTracker.cs +++ b/MagicQuant/Services/Progress/StageProgressTracker.cs @@ -103,7 +103,7 @@ private void MaybePrint(SampleProcessState justFinishedState, string? itemName) ? "items finished" : _options.UnitLabel.Trim(); - AnsiConsole.MarkupLine($"[grey][progress][/]{escapedStage}: [cyan]{finished}[/]/[cyan]{Total}[/] {Markup.Escape(unitLabel)}"); + AnsiConsole.MarkupLine($"[grey][[progress]][/] {escapedStage}: [cyan]{finished}[/]/[cyan]{Total}[/] {Markup.Escape(unitLabel)}"); } else { @@ -132,7 +132,7 @@ private void MaybePrint(SampleProcessState justFinishedState, string? itemName) : $" | item={Markup.Escape(itemName)}"; AnsiConsole.MarkupLine( - $"[grey][progress][/]{escapedStage}: [cyan]{finished}[/]/[cyan]{Total}[/] done | completed=[green]{completed}[/] skipped=[yellow]{skipped}[/] failed=[red]{failed}[/] | elapsed={elapsedText} | {etaText} | {estFinishText}{maybeItem}"); + $"[grey][[progress]][/] {escapedStage}: [cyan]{finished}[/]/[cyan]{Total}[/] done | completed=[green]{completed}[/] skipped=[yellow]{skipped}[/] failed=[red]{failed}[/] | elapsed={elapsedText} | {etaText} | {estFinishText}{maybeItem}"); } _lastPrintedUtc = now; From e3f8a279299b13ccc73c1face1756cff0f91ac38 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 16:33:43 -0400 Subject: [PATCH 144/258] Parallelize learnable baseline batch phase with bounded workers --- MagicQuant/Services/QuantizationService.cs | 42 +++++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 8c307f1..d804ad3 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -179,12 +179,34 @@ public async Task ProcessHybridBatchAsync( .OrderBy(p => p.Quant.BaseQuant.UniqueId) .ToList(); - foreach (var baselinePlan in learnableBaselinePlans) + var duplicateLearnableNames = learnableBaselinePlans + .Select(p => GenerateHybridName(p.Quant)) + .GroupBy(x => x, StringComparer.Ordinal) + .Where(g => g.Count() > 1) + .Select(g => g.Key) + .ToList(); + + if (duplicateLearnableNames.Count > 0) { - ct.ThrowIfCancellationRequested(); - records.Add(await ExecutePlanAsync(baselinePlan, stageProgress, ct)); + throw new InvalidOperationException( + "Duplicate learnable baseline output names were queued in the same batch: " + + string.Join(", ", duplicateLearnableNames)); } + int baselineWorkerCount = CalculateBatchWorkerCount(learnableBaselinePlans.Count); + + await Parallel.ForEachAsync( + learnableBaselinePlans, + new ParallelOptions + { + MaxDegreeOfParallelism = baselineWorkerCount, + CancellationToken = ct + }, + async (baselinePlan, token) => + { + records.Add(await ExecutePlanAsync(baselinePlan, stageProgress, token)); + }); + var remainingPlans = plans.Except(learnableBaselinePlans).ToList(); var equivalenceMap = await BuildIsolationDeduplicationPlanAsync(remainingPlans, ct); @@ -223,8 +245,7 @@ public async Task ProcessHybridBatchAsync( group.Duplicates.Add(plan); } - int workerCount = Math.Max(1, Math.Min(primaryGroups.Count, - _maxConcurrentQuantizations + _benchmarker.CurrentParallelSlotCount)); + int workerCount = CalculateBatchWorkerCount(primaryGroups.Count); await Parallel.ForEachAsync( primaryGroups, @@ -253,6 +274,17 @@ await Parallel.ForEachAsync( } + private int CalculateBatchWorkerCount(int itemCount) + { + if (itemCount <= 0) + return 1; + + return Math.Max(1, Math.Min( + itemCount, + _maxConcurrentQuantizations + _benchmarker.CurrentParallelSlotCount)); + } + + private async Task ExecutePlanAsync( RequiredSamplePlan plan, StageProgressTracker? progress, From bfecd22129d014c1af46c77ab2e897bee5208df2 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 17:00:28 -0400 Subject: [PATCH 145/258] Implement dynamic two-anchor NGL planning and tensor-split config --- MQ.DB/Cache.cs | 2 + .../DbModels/ExecutionPlanProbeCache.cs | 11 + MagicQuant/Commands/Evolution.cs | 42 +- .../Configuration/MagicQuantYamlConfig.cs | 6 + .../Configuration/MagicQuantYamlLoader.cs | 9 + MagicQuant/Services/BenchmarkService.cs | 479 +++++++++++++++--- MagicQuant/config.default.yaml | 12 + MagicQuant/config.dev.yaml | 5 + 8 files changed, 468 insertions(+), 98 deletions(-) diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index 1d0590b..c2b664f 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -90,6 +90,8 @@ public enum MainTorchType public static bool ForceImatrixRebuild { get; set; } + public static Dictionary GpuMemoryLimitsGb { get; set; } = new(); + public static bool IsImatrixAvailable { get; set; } public static string? ActiveImatrixPath { get; set; } diff --git a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs index 8094fe2..7beeabd 100644 --- a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs +++ b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs @@ -24,6 +24,15 @@ public class ExecutionPlanProbeCache : ISQLiteEntity public int GroupSize { get; set; } public string SlotsJson { get; set; } = "[]"; + public int ProbeSchemaVersion { get; set; } = 2; + public ulong Q8ModelSizeBytes { get; set; } + public int Q8StableNgl { get; set; } + public ulong NativeModelSizeBytes { get; set; } + public int NativeStableNgl { get; set; } + public int MaxCandidateNgl { get; set; } + public string GpuMemoryLimitsJson { get; set; } = "{}"; + public string TensorSplitJson { get; set; } = "{}"; + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; public DateTime UpdatedUtc { get; set; } = DateTime.UtcNow; @@ -36,6 +45,8 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.QuantizedModelFingerprint).HasMaxLength(2048); builder.Property(x => x.QuantizationKey).HasMaxLength(128); builder.Property(x => x.SlotsJson).HasMaxLength(8000); + builder.Property(x => x.GpuMemoryLimitsJson).HasMaxLength(4000); + builder.Property(x => x.TensorSplitJson).HasMaxLength(4000); builder.HasIndex(x => x.AiModelHashId); builder.HasIndex(x => x.ImatrixDefinitionId); diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 74f683e..fbbc456 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -151,38 +151,40 @@ public async Task Run(List args) // cannot accidentally inherit a stale default. RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); + string baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && - await benchmarkService.TryInitializeExecutionPlanFromCacheAsync( - quantizationKey: q8QuantizationKey); + await benchmarkService.TryInitializeDynamicExecutionPlanFromCacheAsync( + q8QuantizationKey: q8QuantizationKey); if (!loadedPlanFromCache) { - AnsiConsole.MarkupLine("[grey]Cache not usable, preparing probe-only Q8 baseline...[/]"); - var q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); + AnsiConsole.MarkupLine("[grey]Dynamic execution-plan cache not usable; probing Q8 + native anchors...[/]"); + string? q8ModelGgufPath = null; - await benchmarkService.EnsureExecutionPlanAsync( - q8ModelGgufPath, - quantizationKey: q8QuantizationKey, - forceRediscovery: Cache.ForceRefreshHardwareProbe); + try + { + q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); + await benchmarkService.EnsureDynamicExecutionPlanAsync( + q8ModelPath: q8ModelGgufPath, + nativeModelPath: bf16ModelGgufPath, + q8QuantizationKey: q8QuantizationKey, + nativeQuantizationKey: baseTypeName, + forceRediscovery: Cache.ForceRefreshHardwareProbe); + } + finally + { + await quantizationService.CleanupPureQ8ModelAsync(); + } } bool nativeTruthAlreadyLearned = !Cache.ForceRelearnBaselineTensorMappings && await quantizationService.HasNativeSourceLearnedTruthAsync(); - - if (!nativeTruthAlreadyLearned || !loadedPlanFromCache) - { - await benchmarkService.ClampStaticNglWithBaseModelAsync(bf16ModelGgufPath); - } - else + if (nativeTruthAlreadyLearned && loadedPlanFromCache) { AnsiConsole.MarkupLine( - "[grey]Skipping base-model ngl clamp because native-source truth already exists and execution plan cache was loaded.[/]"); + "[grey]Native-source truth already exists and dynamic plan loaded from cache.[/]"); } - - await quantizationService.CleanupPureQ8ModelAsync(); - - var baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); var benchmarkRootDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks"); var baseBenchDir = Path.Combine(benchmarkRootDir, baseTypeName); var baseLogitsDir = Path.Combine(baseBenchDir, "logits"); @@ -754,4 +756,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 235869b..2a90398 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -15,6 +15,7 @@ public sealed class MagicQuantYamlConfig public RuntimeOutputConfig Output { get; set; } = new(); public RuntimeSurvivalConfig Survival { get; set; } = new(); public RuntimeCandidateSelectionConfig CandidateSelection { get; set; } = new(); + public RuntimeHardwareConfig Hardware { get; set; } = new(); public List SensitivityProbeGroups { get; set; } = [ @@ -289,3 +290,8 @@ public sealed class ResolvedCustomBaselineSpec public bool AllowAsExplicitGroupCandidate { get; set; } public IReadOnlyList BannedGroupIds { get; set; } = Array.Empty(); } + +public sealed class RuntimeHardwareConfig +{ + public Dictionary GpuMemoryLimitsGb { get; set; } = new(); +} diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 0a790ee..36cd5d1 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -74,6 +74,15 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) Cache.ForceRelearnBaselineTensorMappings = config.Flags.ForceRelearnBaselineTensorMappings; Cache.ForceRefreshHardwareProbe = config.Flags.ForceRefreshHardwareProbe; + config.Hardware.GpuMemoryLimitsGb ??= new Dictionary(); + config.Hardware.GpuMemoryLimitsGb = config.Hardware.GpuMemoryLimitsGb + .Where(x => x.Key >= 0 && x.Value > 0d) + .ToDictionary(x => x.Key, x => x.Value); + + Cache.GpuMemoryLimitsGb = config.Hardware.GpuMemoryLimitsGb + .Where(x => x.Key >= 0 && x.Value > 0d) + .ToDictionary(x => x.Key, x => x.Value); + RuntimeSearchSpace.AllowHighPrecisionHybrids = config.Flags.AllowHighPrecisionHybrids; Cache.CurrentArchitectureFamilyName = config.Identity.ArchitectureFamilyName?.Trim() ?? string.Empty; diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 133cc58..eb4bfe6 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -33,6 +33,7 @@ public class BenchmarkService }; private static readonly int[] NglCandidates = { 35, 30, 24, 20, 16, 12, 8, 4 }; + private const int DynamicProbeSchemaVersion = 2; // ---------------------------------------------------------------- // Static execution-plan state @@ -72,14 +73,37 @@ public async Task EnsureExecutionPlanAsync( string quantizationKey = "Q8_0", bool forceRediscovery = false, CancellationToken ct = default) + { + await EnsureDynamicExecutionPlanAsync( + q8ModelPath: q8ModelPath, + nativeModelPath: q8ModelPath, + q8QuantizationKey: quantizationKey, + nativeQuantizationKey: quantizationKey, + discoveryTokenTarget: discoveryTokenTarget, + forceRediscovery: forceRediscovery, + ct: ct); + } + + public async Task EnsureDynamicExecutionPlanAsync( + string q8ModelPath, + string nativeModelPath, + string q8QuantizationKey = "Q8_0", + string nativeQuantizationKey = "BF16", + int discoveryTokenTarget = 8192, + bool forceRediscovery = false, + CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(q8ModelPath)) throw new ArgumentException("Q8 model path was null or empty.", nameof(q8ModelPath)); - if (string.IsNullOrWhiteSpace(quantizationKey)) - throw new ArgumentException("Quantization key was null or empty.", nameof(quantizationKey)); + if (string.IsNullOrWhiteSpace(nativeModelPath)) + throw new ArgumentException("Native model path was null or empty.", nameof(nativeModelPath)); + if (string.IsNullOrWhiteSpace(q8QuantizationKey)) + throw new ArgumentException("Quantization key was null or empty.", nameof(q8QuantizationKey)); string normalizedPath = Path.GetFullPath(q8ModelPath); - string normalizedQuantizationKey = quantizationKey.Trim().ToUpperInvariant(); + string normalizedNativePath = Path.GetFullPath(nativeModelPath); + string normalizedQuantizationKey = q8QuantizationKey.Trim().ToUpperInvariant(); + string normalizedNativeQuantizationKey = nativeQuantizationKey.Trim().ToUpperInvariant(); if (!forceRediscovery && _currentPlan != null && @@ -112,7 +136,13 @@ public async Task EnsureExecutionPlanAsync( if (plan == null) { - plan = await BuildExecutionPlanAsync(normalizedPath, discoveryTokenTarget, ct); + plan = await BuildDynamicExecutionPlanAsync( + q8ModelPath: normalizedPath, + nativeModelPath: normalizedNativePath, + q8QuantizationKey: normalizedQuantizationKey, + nativeQuantizationKey: normalizedNativeQuantizationKey, + discoveryTokenTarget: discoveryTokenTarget, + ct: ct); await UpsertCachedExecutionPlanAsync(cacheKey, plan, ct); } @@ -126,14 +156,30 @@ public async Task EnsureExecutionPlanAsync( AnsiConsole.Write(new Rule("[yellow]Benchmark Execution Plan[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"[green]Static ngl:[/] [cyan]{plan.StaticNgl}[/]"); + AnsiConsole.MarkupLine($"[green]Q8 anchor:[/] [cyan]{(plan.Q8ModelSizeBytes / 1024d / 1024d / 1024d):F2} GB @ ngl={plan.Q8StableNgl}[/]"); + AnsiConsole.MarkupLine($"[green]Native anchor:[/] [cyan]{(plan.NativeModelSizeBytes / 1024d / 1024d / 1024d):F2} GB @ ngl={plan.NativeStableNgl}[/]"); AnsiConsole.MarkupLine($"[green]Uses GPU:[/] [cyan]{plan.UsesGpu}[/]"); AnsiConsole.MarkupLine($"[green]GPU group size:[/] [cyan]{plan.GroupSize}[/]"); - AnsiConsole.MarkupLine($"[green]Parallel benchmark slots:[/] [cyan]{plan.Slots.Count}[/]"); + AnsiConsole.MarkupLine($"[green]Parallel benchmark Slots:[/] [cyan]{plan.Slots.Count}[/]"); AnsiConsole.MarkupLine($"[green]Quantization key:[/] [cyan]{Markup.Escape(normalizedQuantizationKey)}[/]"); + if (Cache.GpuMemoryLimitsGb.Count == 0) + { + AnsiConsole.MarkupLine("[green]GPU memory limits:[/] [grey]none[/]"); + } + else + { + string limits = string.Join(", ", Cache.GpuMemoryLimitsGb.OrderBy(x => x.Key).Select(x => $"GPU {x.Key}={x.Value:0.###} GB")); + AnsiConsole.MarkupLine($"[green]GPU memory limits:[/] [cyan]{Markup.Escape(limits)}[/]"); + } foreach (var slot in plan.Slots) { AnsiConsole.MarkupLine($" [grey]Slot {slot.SlotId}:[/] {Markup.Escape(slot.DisplayName)}"); + string tensorSplit = BuildTensorSplitArgs(slot); + if (!string.IsNullOrWhiteSpace(tensorSplit)) + { + AnsiConsole.MarkupLine($" [grey]tensor split:[/] {Markup.Escape(tensorSplit.Trim())}"); + } } } finally @@ -147,11 +193,22 @@ public async Task TryInitializeExecutionPlanFromCacheAsync( string quantizationKey = "Q8_0", string? preferredPlanModelPath = null, CancellationToken ct = default) + => await TryInitializeDynamicExecutionPlanFromCacheAsync( + discoveryTokenTarget: discoveryTokenTarget, + q8QuantizationKey: quantizationKey, + preferredPlanModelPath: preferredPlanModelPath, + ct: ct); + + public async Task TryInitializeDynamicExecutionPlanFromCacheAsync( + int discoveryTokenTarget = 8192, + string q8QuantizationKey = "Q8_0", + string? preferredPlanModelPath = null, + CancellationToken ct = default) { - if (string.IsNullOrWhiteSpace(quantizationKey)) - throw new ArgumentException("Quantization key was null or empty.", nameof(quantizationKey)); + if (string.IsNullOrWhiteSpace(q8QuantizationKey)) + throw new ArgumentException("Quantization key was null or empty.", nameof(q8QuantizationKey)); - string normalizedQuantizationKey = quantizationKey.Trim().ToUpperInvariant(); + string normalizedQuantizationKey = q8QuantizationKey.Trim().ToUpperInvariant(); string planModelPath = string.IsNullOrWhiteSpace(preferredPlanModelPath) ? $"cached://{normalizedQuantizationKey}" : Path.GetFullPath(preferredPlanModelPath); @@ -269,11 +326,11 @@ public async Task ClampStaticNglWithBaseModelAsync( if (chosen.Value != _currentPlan.StaticNgl) { var updated = new BenchmarkExecutionPlan( - planModelPath: _currentPlan.PlanModelPath, - staticNgl: chosen.Value, - usesGpu: _currentPlan.UsesGpu, - groupSize: _currentPlan.GroupSize, - slots: _currentPlan.Slots); + PlanModelPath: _currentPlan.PlanModelPath, + StaticNgl: chosen.Value, + UsesGpu: _currentPlan.UsesGpu, + GroupSize: _currentPlan.GroupSize, + Slots: _currentPlan.Slots); lock (SlotSync) { @@ -297,6 +354,67 @@ public async Task ClampStaticNglWithBaseModelAsync( } } + private async Task BuildDynamicExecutionPlanAsync( + string q8ModelPath, + string nativeModelPath, + string q8QuantizationKey, + string nativeQuantizationKey, + int discoveryTokenTarget, + CancellationToken ct) + { + var plan = await BuildExecutionPlanAsync(q8ModelPath, discoveryTokenTarget, ct); + if (!plan.UsesGpu) + { + return plan with + { + ProbeSchemaVersion = DynamicProbeSchemaVersion, + Q8ModelSizeBytes = TryGetModelSize(q8ModelPath), + Q8StableNgl = 0, + NativeModelSizeBytes = TryGetModelSize(nativeModelPath), + NativeStableNgl = 0, + MaxCandidateNgl = NglCandidates.Max(), + GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), + TensorSplitJson = SerializeTensorSplitMap(plan.Slots) + }; + } + + var probeSlot = plan.Slots[0]; + int? nativeStableNgl = await ProbeHighestStableNglAsync( + nativeModelPath, + probeSlot, + Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe_native"), + discoveryTokenTarget, + ct); + + if (!nativeStableNgl.HasValue || nativeStableNgl.Value <= 0) + { + AnsiConsole.MarkupLine("[yellow]Native/BF16 anchor probe failed; falling back to CPU plan.[/]"); + return BenchmarkExecutionPlan.CreateCpuPlan(q8ModelPath) with + { + ProbeSchemaVersion = DynamicProbeSchemaVersion, + Q8ModelSizeBytes = TryGetModelSize(q8ModelPath), + Q8StableNgl = 0, + NativeModelSizeBytes = TryGetModelSize(nativeModelPath), + NativeStableNgl = 0, + MaxCandidateNgl = NglCandidates.Max(), + GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), + TensorSplitJson = "{}" + }; + } + + return plan with + { + ProbeSchemaVersion = DynamicProbeSchemaVersion, + Q8ModelSizeBytes = TryGetModelSize(q8ModelPath), + Q8StableNgl = plan.StaticNgl, + NativeModelSizeBytes = TryGetModelSize(nativeModelPath), + NativeStableNgl = nativeStableNgl.Value, + MaxCandidateNgl = NglCandidates.Max(), + GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), + TensorSplitJson = SerializeTensorSplitMap(plan.Slots) + }; + } + private async Task BuildExecutionPlanAsync( string q8ModelPath, int discoveryTokenTarget, @@ -312,6 +430,7 @@ private async Task BuildExecutionPlanAsync( var allGpuIndices = Enumerable.Range(0, gpuCount).ToArray(); var allGpuSlot = new BenchmarkSlot(0, allGpuIndices); + _ = BuildTensorSplitArgs(allGpuSlot); string probeRoot = Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe"); Directory.CreateDirectory(probeRoot); @@ -339,6 +458,7 @@ private async Task BuildExecutionPlanAsync( for (int i = 0; i < groups.Count; i++) { var slot = new BenchmarkSlot(i, groups[i]); + _ = BuildTensorSplitArgs(slot); bool ok = await ValidateSlotForFixedPlanAsync( q8ModelPath, @@ -360,20 +480,20 @@ private async Task BuildExecutionPlanAsync( if (allGroupsPass && slots.Count > 0) { return new BenchmarkExecutionPlan( - planModelPath: q8ModelPath, - staticNgl: targetNgl.Value, - usesGpu: true, - groupSize: groupSize, - slots: slots); + PlanModelPath: q8ModelPath, + StaticNgl: targetNgl.Value, + UsesGpu: true, + GroupSize: groupSize, + Slots: slots); } } return new BenchmarkExecutionPlan( - planModelPath: q8ModelPath, - staticNgl: targetNgl.Value, - usesGpu: true, - groupSize: gpuCount, - slots: new List { allGpuSlot }); + PlanModelPath: q8ModelPath, + StaticNgl: targetNgl.Value, + UsesGpu: true, + GroupSize: gpuCount, + Slots: new List { allGpuSlot }); } private async Task TryLoadCachedExecutionPlanAsync( @@ -397,6 +517,18 @@ private async Task BuildExecutionPlanAsync( if (row == null) return null; + if (row.ProbeSchemaVersion < DynamicProbeSchemaVersion) + { + AnsiConsole.MarkupLine("[yellow]Execution-plan cache row uses old probe schema; re-probing.[/]"); + return null; + } + + if (row.GpuMemoryLimitsJson != SerializeGpuMemoryLimits()) + { + AnsiConsole.MarkupLine("[yellow]Execution-plan cache row GPU memory limits differ from current config; re-probing.[/]"); + return null; + } + List slotDevices; try { @@ -415,12 +547,32 @@ private async Task BuildExecutionPlanAsync( .Select((devices, idx) => new BenchmarkSlot(idx, devices ?? Array.Empty())) .ToList(); + foreach (var slot in slots) + { + _ = BuildTensorSplitArgs(slot); + } + + if (row.UsesGpu && + (row.Q8ModelSizeBytes == 0 || row.NativeModelSizeBytes == 0 || row.Q8StableNgl <= 0 || row.NativeStableNgl <= 0)) + { + AnsiConsole.MarkupLine("[yellow]Execution-plan cache row is missing dynamic anchor metadata; re-probing.[/]"); + return null; + } + return new BenchmarkExecutionPlan( - planModelPath: key.PlanModelPath, - staticNgl: row.StaticNgl, - usesGpu: row.UsesGpu, - groupSize: row.GroupSize, - slots: slots); + PlanModelPath: key.PlanModelPath, + StaticNgl: row.StaticNgl, + UsesGpu: row.UsesGpu, + GroupSize: row.GroupSize, + Slots: slots, + ProbeSchemaVersion: row.ProbeSchemaVersion, + Q8ModelSizeBytes: row.Q8ModelSizeBytes, + Q8StableNgl: row.Q8StableNgl, + NativeModelSizeBytes: row.NativeModelSizeBytes, + NativeStableNgl: row.NativeStableNgl, + MaxCandidateNgl: row.MaxCandidateNgl > 0 ? row.MaxCandidateNgl : NglCandidates.Max(), + GpuMemoryLimitsJson: row.GpuMemoryLimitsJson ?? "{}", + TensorSplitJson: row.TensorSplitJson ?? "{}"); } private async Task UpsertCachedExecutionPlanAsync( @@ -464,6 +616,14 @@ private async Task UpsertCachedExecutionPlanAsync( existing.UsesGpu = plan.UsesGpu; existing.GroupSize = plan.GroupSize; existing.SlotsJson = slotsJson; + existing.ProbeSchemaVersion = plan.ProbeSchemaVersion; + existing.Q8ModelSizeBytes = plan.Q8ModelSizeBytes; + existing.Q8StableNgl = plan.Q8StableNgl; + existing.NativeModelSizeBytes = plan.NativeModelSizeBytes; + existing.NativeStableNgl = plan.NativeStableNgl; + existing.MaxCandidateNgl = plan.MaxCandidateNgl; + existing.GpuMemoryLimitsJson = plan.GpuMemoryLimitsJson; + existing.TensorSplitJson = plan.TensorSplitJson; existing.UpdatedUtc = now; await db.SaveChangesAsync(ct); @@ -504,6 +664,49 @@ private static string BuildQuantizedModelFingerprint(string quantizationKey) return $"family:{family}|model:{Cache.CurrentModelId}|imatrix:{imatrix}|quant:{quantizationKey}"; } + private static string SerializeGpuMemoryLimits() + { + var ordered = Cache.GpuMemoryLimitsGb + .OrderBy(x => x.Key) + .ToDictionary(x => x.Key, x => x.Value); + return JsonSerializer.Serialize(ordered); + } + + private static string SerializeTensorSplitMap(IReadOnlyList slots) + { + var map = slots + .Where(s => s.UsesGpu && s.DeviceIndices.Length > 1) + .ToDictionary( + s => s.DisplayName, + s => BuildTensorSplitArgs(s).Trim(), + StringComparer.Ordinal); + return JsonSerializer.Serialize(map); + } + + private static string BuildTensorSplitArgs(BenchmarkSlot slot) + { + if (!slot.UsesGpu || slot.DeviceIndices.Length <= 1) + return string.Empty; + + if (Cache.GpuMemoryLimitsGb.Count == 0) + return string.Empty; + + var missing = slot.DeviceIndices + .Where(i => !Cache.GpuMemoryLimitsGb.ContainsKey(i)) + .ToArray(); + + if (missing.Length > 0) + { + throw new InvalidOperationException( + $"GPU memory limits were configured, but slot {slot.DisplayName} is missing limits for GPU(s): {string.Join(", ", missing)}."); + } + + string split = string.Join(",", slot.DeviceIndices.Select(i => + Cache.GpuMemoryLimitsGb[i].ToString("0.###", CultureInfo.InvariantCulture))); + + return $" --tensor-split {split}"; + } + private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct) { if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) @@ -594,7 +797,7 @@ private async Task ProbeLlamaBenchAtFixedNglAsync( $"probe_llamabench_slot{slot.SlotId}_g{slot.DeviceCount}_ngl{fixedNgl}.md"); string cmd = slot.UsesGpu - ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl} -o md" + ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl}{BuildTensorSplitArgs(slot)} -o md" : $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); @@ -625,7 +828,7 @@ private async Task ProbePerplexityAtFixedNglAsync( $"probe_ppl_general_slot{slot.SlotId}_g{slot.DeviceCount}_ngl{fixedNgl}.log"); string cmd = slot.UsesGpu - ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl} -t 4 -c 2048 --file \"{corpusPath}\"" + ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot)} -t 4 -c 2048 --file \"{corpusPath}\"" : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\""; var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); @@ -742,6 +945,58 @@ private static ulong TryGetModelSize(string modelPath) return File.Exists(modelPath) ? (ulong)new FileInfo(modelPath).Length : 0UL; } + private int ResolveDynamicNglForModel(ulong modelSizeBytes, BenchmarkSlot slot) + { + if (_currentPlan == null || !_currentPlan.UsesGpu || !slot.UsesGpu) + return 0; + + if (modelSizeBytes == 0) + return Math.Max(0, _currentPlan.StaticNgl); + + if (_currentPlan.Q8ModelSizeBytes == 0 || _currentPlan.Q8StableNgl <= 0) + return Math.Max(0, _currentPlan.StaticNgl); + + int maxNgl = _currentPlan.MaxCandidateNgl > 0 ? _currentPlan.MaxCandidateNgl : NglCandidates.Max(); + + double estimateRaw; + if (modelSizeBytes <= _currentPlan.Q8ModelSizeBytes) + { + estimateRaw = Math.Floor(_currentPlan.Q8StableNgl * (_currentPlan.Q8ModelSizeBytes / (double)modelSizeBytes)); + } + else if (_currentPlan.NativeModelSizeBytes > _currentPlan.Q8ModelSizeBytes && _currentPlan.NativeStableNgl > 0 && modelSizeBytes < _currentPlan.NativeModelSizeBytes) + { + double t = (modelSizeBytes - _currentPlan.Q8ModelSizeBytes) / (double)(_currentPlan.NativeModelSizeBytes - _currentPlan.Q8ModelSizeBytes); + estimateRaw = Math.Floor(_currentPlan.Q8StableNgl + ((_currentPlan.NativeStableNgl - _currentPlan.Q8StableNgl) * t)); + } + else if (_currentPlan.NativeModelSizeBytes > 0 && _currentPlan.NativeStableNgl > 0) + { + estimateRaw = Math.Floor(_currentPlan.NativeStableNgl * (_currentPlan.NativeModelSizeBytes / (double)modelSizeBytes)); + estimateRaw = Math.Min(estimateRaw, _currentPlan.NativeStableNgl); + } + else + { + estimateRaw = _currentPlan.StaticNgl; + } + + int estimate = (int)Math.Clamp(estimateRaw, 0, maxNgl); + int chosen = NglCandidates + .Where(x => x <= estimate && x <= maxNgl) + .DefaultIfEmpty(0) + .Max(); + + return Math.Max(0, chosen); + } + + private static List BuildNglFallbackList(int startNgl) + { + var result = new List { Math.Max(0, startNgl) }; + result.AddRange(NglCandidates.Where(x => x < startNgl).OrderByDescending(x => x)); + if (!result.Contains(0)) + result.Add(0); + + return result.Distinct().ToList(); + } + private const double KldEpsilon = 1e-8; private static bool HasMeaningfulKld(double? kld) @@ -915,13 +1170,19 @@ await SaveBenchmarkToDbAsync( await using var slotLease = await AcquireBenchmarkSlotAsync(); var slot = slotLease.Slot; - int effectiveNgl = slot.UsesGpu - ? _currentPlan.StaticNgl - : 0; + ulong modelSizeBytes = TryGetModelSize(modelPath); + int initialNgl = ResolveDynamicNglForModel(modelSizeBytes, slot); + var runtimeNgl = new RuntimeNglState + { + CurrentNgl = initialNgl, + LastSuccessfulNgl = initialNgl + }; + AnsiConsole.MarkupLine( + $"[grey]Dynamic NGL:[/] model={Markup.Escape(Path.GetFileName(modelPath))}, size={(modelSizeBytes / 1024d / 1024d / 1024d):F2} GB, q8={( _currentPlan.Q8ModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.Q8StableNgl}, native={(_currentPlan.NativeModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.NativeStableNgl}, slot={Markup.Escape(slot.DisplayName)}, chosen={initialNgl}"); var result = new BenchmarkResult { - ModelSizeBytes = TryGetModelSize(modelPath) + ModelSizeBytes = modelSizeBytes }; var executedRunTimings = new List(); @@ -931,7 +1192,7 @@ await SaveBenchmarkToDbAsync( { LogPath = null, Backend = slot.UsesGpu ? "disabled" : "cpu-disabled", - Ngl = effectiveNgl, + Ngl = runtimeNgl.CurrentNgl, Test = "disabled", Tps = 0 }; @@ -964,15 +1225,15 @@ await SaveBenchmarkToDbAsync( try { AnsiConsole.MarkupLine( - $"[yellow]Running Perplexity ({Markup.Escape(domain)})[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); + $"[yellow]Running Perplexity ({Markup.Escape(domain)})[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={runtimeNgl.CurrentNgl})[/]"); - var metrics = await RunPplBenchmarkAsync( + var metrics = await RunPplBenchmarkWithNglFallbackAsync( modelPath: modelPath, benchDir: benchDir, domain: domain, corpusPath: corpusPath, - fixedNgl: effectiveNgl, slot: slot, + runtimeNgl: runtimeNgl, klLogitsDir: klLogitsDir, saveLogits: saveLogits); @@ -986,6 +1247,7 @@ await SaveBenchmarkToDbAsync( sw.Stop(); result.Perplexity[domain] = metrics; + result.LlamaBench.Ngl = runtimeNgl.LastSuccessfulNgl; executedRunTimings.Add(new PendingBenchmarkRunTiming { @@ -1061,18 +1323,24 @@ private async Task RunAllBenchmarksTransientAsync( await using var slotLease = await AcquireBenchmarkSlotAsync(); var slot = slotLease.Slot; - int effectiveNgl = slot.UsesGpu - ? _currentPlan.StaticNgl - : 0; + ulong modelSizeBytes = TryGetModelSize(modelPath); + int initialNgl = ResolveDynamicNglForModel(modelSizeBytes, slot); + var runtimeNgl = new RuntimeNglState + { + CurrentNgl = initialNgl, + LastSuccessfulNgl = initialNgl + }; + AnsiConsole.MarkupLine( + $"[grey]Dynamic NGL:[/] model={Markup.Escape(Path.GetFileName(modelPath))}, size={(modelSizeBytes / 1024d / 1024d / 1024d):F2} GB, q8={( _currentPlan.Q8ModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.Q8StableNgl}, native={(_currentPlan.NativeModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.NativeStableNgl}, slot={Markup.Escape(slot.DisplayName)}, chosen={initialNgl}"); var result = new BenchmarkResult { - ModelSizeBytes = TryGetModelSize(modelPath), + ModelSizeBytes = modelSizeBytes, LlamaBench = new LlamaBenchMetrics { LogPath = null, Backend = slot.UsesGpu ? "disabled" : "cpu-disabled", - Ngl = effectiveNgl, + Ngl = runtimeNgl.CurrentNgl, Test = "disabled", Tps = 0 } @@ -1101,15 +1369,15 @@ private async Task RunAllBenchmarksTransientAsync( await PreparePplCorpusAsync(domain, corpusPath, tokenTarget); AnsiConsole.MarkupLine( - $"[yellow]Running transient Perplexity ({Markup.Escape(domain)})[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={effectiveNgl})[/]"); + $"[yellow]Running transient Perplexity ({Markup.Escape(domain)})[/] [grey]({Markup.Escape(slot.DisplayName)}, ngl={runtimeNgl.CurrentNgl})[/]"); - var metrics = await RunPplBenchmarkAsync( + var metrics = await RunPplBenchmarkWithNglFallbackAsync( modelPath: modelPath, benchDir: benchDir, domain: domain, corpusPath: corpusPath, - fixedNgl: effectiveNgl, slot: slot, + runtimeNgl: runtimeNgl, klLogitsDir: klLogitsDir, saveLogits: saveLogits); @@ -1121,6 +1389,7 @@ private async Task RunAllBenchmarksTransientAsync( } result.Perplexity[domain] = metrics; + result.LlamaBench.Ngl = runtimeNgl.LastSuccessfulNgl; } await WriteMetricsJsonAsync(benchDir, result); @@ -1634,7 +1903,7 @@ private async Task RunLlamaBenchAsync( string logFile = Path.Combine(benchDir, "llamabench.md"); string cmd = slot.UsesGpu - ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl} -o md" + ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl}{BuildTensorSplitArgs(slot)} -o md" : $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; await RunFixedCommandWithRetryAsync( @@ -1687,7 +1956,7 @@ private async Task RunPplBenchmarkAsync( } string cmd = slot.UsesGpu - ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}" + ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot)} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}" : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}"; await RunFixedCommandWithRetryAsync( @@ -1710,6 +1979,55 @@ await RunFixedCommandWithRetryAsync( return parsed; } + private async Task RunPplBenchmarkWithNglFallbackAsync( + string modelPath, + string benchDir, + string domain, + string corpusPath, + BenchmarkSlot slot, + RuntimeNglState runtimeNgl, + string? klLogitsDir, + bool saveLogits) + { + var fallbackNgls = BuildNglFallbackList(runtimeNgl.CurrentNgl); + Exception? lastException = null; + + for (int i = 0; i < fallbackNgls.Count; i++) + { + int ngl = fallbackNgls[i]; + try + { + var metrics = await RunPplBenchmarkAsync( + modelPath: modelPath, + benchDir: benchDir, + domain: domain, + corpusPath: corpusPath, + fixedNgl: ngl, + slot: slot, + klLogitsDir: klLogitsDir, + saveLogits: saveLogits); + + runtimeNgl.CurrentNgl = ngl; + runtimeNgl.LastSuccessfulNgl = ngl; + return metrics; + } + catch (Exception ex) + { + lastException = ex; + string content = ex.ToString(); + bool retryable = LooksLikeRetryableGpuFailure(content); + if (!retryable || i == fallbackNgls.Count - 1) + throw; + + int next = fallbackNgls[i + 1]; + AnsiConsole.MarkupLine( + $"[yellow]GPU failure at ngl={ngl} for model {Markup.Escape(Path.GetFileName(modelPath))}; retrying at ngl={next}.[/]"); + } + } + + throw lastException ?? new InvalidOperationException("Perplexity benchmark failed after NGL fallbacks."); + } + private async Task RunFixedCommandWithRetryAsync( string label, string cmd, @@ -2053,37 +2371,42 @@ private string GetRelativePath(string fullPath) // Internal plan / slot types // ---------------------------------------------------------------- - private sealed class BenchmarkExecutionPlan + private sealed record BenchmarkExecutionPlan( + string PlanModelPath, + int StaticNgl, + bool UsesGpu, + int GroupSize, + IReadOnlyList Slots, + int ProbeSchemaVersion = DynamicProbeSchemaVersion, + ulong Q8ModelSizeBytes = 0, + int Q8StableNgl = 0, + ulong NativeModelSizeBytes = 0, + int NativeStableNgl = 0, + int MaxCandidateNgl = 35, + string GpuMemoryLimitsJson = "{}", + string TensorSplitJson = "{}") { - public string PlanModelPath { get; } - public int StaticNgl { get; } - public bool UsesGpu { get; } - public int GroupSize { get; } - public IReadOnlyList Slots { get; } - - public BenchmarkExecutionPlan( - string planModelPath, - int staticNgl, - bool usesGpu, - int groupSize, - IReadOnlyList slots) - { - PlanModelPath = planModelPath; - StaticNgl = staticNgl; - UsesGpu = usesGpu; - GroupSize = groupSize; - Slots = slots; - } - public static BenchmarkExecutionPlan CreateCpuPlan(string q8ModelPath) - { - return new BenchmarkExecutionPlan( - planModelPath: q8ModelPath, - staticNgl: 0, - usesGpu: false, - groupSize: 0, - slots: new List { new(0, Array.Empty()) }); - } + => new( + PlanModelPath: q8ModelPath, + StaticNgl: 0, + UsesGpu: false, + GroupSize: 0, + Slots: new List { new(0, Array.Empty()) }, + ProbeSchemaVersion: DynamicProbeSchemaVersion, + Q8ModelSizeBytes: 0, + Q8StableNgl: 0, + NativeModelSizeBytes: 0, + NativeStableNgl: 0, + MaxCandidateNgl: NglCandidates.Max(), + GpuMemoryLimitsJson: SerializeGpuMemoryLimits(), + TensorSplitJson: "{}"); + } + + public sealed class RuntimeNglState + { + public int CurrentNgl { get; set; } + public int LastSuccessfulNgl { get; set; } } private sealed class BenchmarkSlot @@ -2142,4 +2465,4 @@ private sealed record ExecutionPlanCacheKey( string QuantizationKey, int DiscoveryTokenTarget, string PlanModelPath); -} \ No newline at end of file +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 2d40f57..4aa916e 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -52,6 +52,18 @@ flags: # are allowed in hybrid generation logic. allow_high_precision_hybrids: false +hardware: + # Optional per-GPU usable VRAM limits in GB. + # Leave empty for automatic/default llama.cpp placement. + # When provided and a benchmark slot uses multiple GPUs, MagicQuant will pass + # --tensor-split in visible GPU order. + # + # Example: + # gpu_memory_limits_gb: + # 0: 19 + # 1: 23 + gpu_memory_limits_gb: {} + imatrix: # Optional remote imatrix URL if your flow supports fetching one. imatrix_url: diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 8018325..417d673 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -13,6 +13,11 @@ flags: force_refresh_hardware_probe: false allow_high_precision_hybrids: false +hardware: + gpu_memory_limits_gb: + 0: 19 + 1: 23 + imatrix: imatrix_url: dataset_repo: From 3c0cde7a16e217b9ebe7c9fa2af5305045dd786c Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 17:12:02 -0400 Subject: [PATCH 146/258] Fix native anchor caching and preserve Q8 GPU plan on native probe failure --- .../DbModels/ExecutionPlanProbeCache.cs | 4 +- MagicQuant/Commands/Evolution.cs | 4 +- MagicQuant/Services/BenchmarkService.cs | 101 +++++++++++++++--- 3 files changed, 91 insertions(+), 18 deletions(-) diff --git a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs index 7beeabd..d1485c2 100644 --- a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs +++ b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs @@ -29,6 +29,7 @@ public class ExecutionPlanProbeCache : ISQLiteEntity public int Q8StableNgl { get; set; } public ulong NativeModelSizeBytes { get; set; } public int NativeStableNgl { get; set; } + public string NativeQuantizationKey { get; set; } = string.Empty; public int MaxCandidateNgl { get; set; } public string GpuMemoryLimitsJson { get; set; } = "{}"; public string TensorSplitJson { get; set; } = "{}"; @@ -44,6 +45,7 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.HardwareFingerprint).HasMaxLength(1024); builder.Property(x => x.QuantizedModelFingerprint).HasMaxLength(2048); builder.Property(x => x.QuantizationKey).HasMaxLength(128); + builder.Property(x => x.NativeQuantizationKey).HasMaxLength(128); builder.Property(x => x.SlotsJson).HasMaxLength(8000); builder.Property(x => x.GpuMemoryLimitsJson).HasMaxLength(4000); builder.Property(x => x.TensorSplitJson).HasMaxLength(4000); @@ -70,4 +72,4 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.ImatrixDefinitionId) .OnDelete(DeleteBehavior.Restrict); } -} \ No newline at end of file +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index fbbc456..7ec2a94 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -154,7 +154,9 @@ public async Task Run(List args) string baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && await benchmarkService.TryInitializeDynamicExecutionPlanFromCacheAsync( - q8QuantizationKey: q8QuantizationKey); + q8QuantizationKey: q8QuantizationKey, + nativeModelPath: bf16ModelGgufPath, + nativeQuantizationKey: baseTypeName); if (!loadedPlanFromCache) { diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index eb4bfe6..615cc46 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -129,7 +129,11 @@ public async Task EnsureDynamicExecutionPlanAsync( BenchmarkExecutionPlan? plan = null; if (!forceRediscovery) { - plan = await TryLoadCachedExecutionPlanAsync(cacheKey, ct); + plan = await TryLoadCachedExecutionPlanAsync( + key: cacheKey, + nativeModelPath: normalizedNativePath, + nativeQuantizationKey: normalizedNativeQuantizationKey, + ct: ct); if (plan != null) AnsiConsole.MarkupLine("[green]Loaded benchmark execution plan from SQLite cache.[/]"); } @@ -191,17 +195,23 @@ public async Task EnsureDynamicExecutionPlanAsync( public async Task TryInitializeExecutionPlanFromCacheAsync( int discoveryTokenTarget = 8192, string quantizationKey = "Q8_0", + string? nativeModelPath = null, + string nativeQuantizationKey = "BF16", string? preferredPlanModelPath = null, CancellationToken ct = default) => await TryInitializeDynamicExecutionPlanFromCacheAsync( discoveryTokenTarget: discoveryTokenTarget, q8QuantizationKey: quantizationKey, + nativeModelPath: nativeModelPath, + nativeQuantizationKey: nativeQuantizationKey, preferredPlanModelPath: preferredPlanModelPath, ct: ct); public async Task TryInitializeDynamicExecutionPlanFromCacheAsync( int discoveryTokenTarget = 8192, string q8QuantizationKey = "Q8_0", + string? nativeModelPath = null, + string nativeQuantizationKey = "BF16", string? preferredPlanModelPath = null, CancellationToken ct = default) { @@ -217,7 +227,11 @@ public async Task TryInitializeDynamicExecutionPlanFromCacheAsync( AnsiConsole.MarkupLine( $"[grey]Checking execution-plan cache:[/] quant={Markup.Escape(normalizedQuantizationKey)}, tokens={discoveryTokenTarget}"); - var plan = await TryLoadCachedExecutionPlanAsync(cacheKey, ct); + var plan = await TryLoadCachedExecutionPlanAsync( + key: cacheKey, + nativeModelPath: nativeModelPath, + nativeQuantizationKey: nativeQuantizationKey, + ct: ct); if (plan == null) { AnsiConsole.MarkupLine("[yellow]Execution-plan cache miss:[/] full Q8 probe will run."); @@ -305,7 +319,18 @@ public async Task ClampStaticNglWithBaseModelAsync( AnsiConsole.MarkupLine( "[yellow]Base model could not sustain the discovered GPU ngl. Falling back to a CPU benchmark plan.[/]"); - var cpuPlan = BenchmarkExecutionPlan.CreateCpuPlan(_currentPlan.PlanModelPath); + var cpuPlan = BenchmarkExecutionPlan.CreateCpuPlan(_currentPlan.PlanModelPath) with + { + ProbeSchemaVersion = _currentPlan.ProbeSchemaVersion, + Q8ModelSizeBytes = _currentPlan.Q8ModelSizeBytes, + Q8StableNgl = 0, + NativeModelSizeBytes = _currentPlan.NativeModelSizeBytes, + NativeStableNgl = 0, + NativeQuantizationKey = _currentPlan.NativeQuantizationKey, + MaxCandidateNgl = _currentPlan.MaxCandidateNgl, + GpuMemoryLimitsJson = _currentPlan.GpuMemoryLimitsJson, + TensorSplitJson = "{}" + }; lock (SlotSync) { @@ -325,12 +350,7 @@ public async Task ClampStaticNglWithBaseModelAsync( if (chosen.Value != _currentPlan.StaticNgl) { - var updated = new BenchmarkExecutionPlan( - PlanModelPath: _currentPlan.PlanModelPath, - StaticNgl: chosen.Value, - UsesGpu: _currentPlan.UsesGpu, - GroupSize: _currentPlan.GroupSize, - Slots: _currentPlan.Slots); + var updated = _currentPlan with { StaticNgl = chosen.Value }; lock (SlotSync) { @@ -378,7 +398,7 @@ private async Task BuildDynamicExecutionPlanAsync( }; } - var probeSlot = plan.Slots[0]; + var probeSlot = BuildAllGpuSlotFromSystemInfo(); int? nativeStableNgl = await ProbeHighestStableNglAsync( nativeModelPath, probeSlot, @@ -388,17 +408,19 @@ private async Task BuildDynamicExecutionPlanAsync( if (!nativeStableNgl.HasValue || nativeStableNgl.Value <= 0) { - AnsiConsole.MarkupLine("[yellow]Native/BF16 anchor probe failed; falling back to CPU plan.[/]"); - return BenchmarkExecutionPlan.CreateCpuPlan(q8ModelPath) with + AnsiConsole.MarkupLine( + "[yellow]Native anchor unavailable; keeping Q8 GPU plan and using conservative Q8-only dynamic NGL fallback.[/]"); + return plan with { ProbeSchemaVersion = DynamicProbeSchemaVersion, Q8ModelSizeBytes = TryGetModelSize(q8ModelPath), - Q8StableNgl = 0, + Q8StableNgl = plan.StaticNgl, NativeModelSizeBytes = TryGetModelSize(nativeModelPath), NativeStableNgl = 0, + NativeQuantizationKey = nativeQuantizationKey, MaxCandidateNgl = NglCandidates.Max(), GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), - TensorSplitJson = "{}" + TensorSplitJson = SerializeTensorSplitMap(plan.Slots) }; } @@ -409,6 +431,7 @@ private async Task BuildDynamicExecutionPlanAsync( Q8StableNgl = plan.StaticNgl, NativeModelSizeBytes = TryGetModelSize(nativeModelPath), NativeStableNgl = nativeStableNgl.Value, + NativeQuantizationKey = nativeQuantizationKey, MaxCandidateNgl = NglCandidates.Max(), GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), TensorSplitJson = SerializeTensorSplitMap(plan.Slots) @@ -496,8 +519,23 @@ private async Task BuildExecutionPlanAsync( Slots: new List { allGpuSlot }); } + private static BenchmarkSlot BuildAllGpuSlotFromSystemInfo() + { + int gpuCount = Cache.SysInfo?.GpuInfo? + .Count(x => x.GpuVendor != GpuVendor.Cpu && x.GpuVendor != GpuVendor.Unknown) ?? 0; + + if (gpuCount <= 0) + return new BenchmarkSlot(0, Array.Empty()); + + var slot = new BenchmarkSlot(0, Enumerable.Range(0, gpuCount).ToArray()); + _ = BuildTensorSplitArgs(slot); + return slot; + } + private async Task TryLoadCachedExecutionPlanAsync( ExecutionPlanCacheKey key, + string? nativeModelPath, + string nativeQuantizationKey, CancellationToken ct) { await using var db = new MagicQuantContext(); @@ -529,6 +567,23 @@ private async Task BuildExecutionPlanAsync( return null; } + string normalizedNativeQuantizationKey = (nativeQuantizationKey ?? string.Empty).Trim().ToUpperInvariant(); + if (!string.Equals((row.NativeQuantizationKey ?? string.Empty).Trim().ToUpperInvariant(), normalizedNativeQuantizationKey, StringComparison.Ordinal)) + { + AnsiConsole.MarkupLine("[yellow]Execution-plan cache native quantization key changed; re-probing.[/]"); + return null; + } + + if (!string.IsNullOrWhiteSpace(nativeModelPath)) + { + ulong nativeSize = TryGetModelSize(Path.GetFullPath(nativeModelPath)); + if (nativeSize > 0 && row.NativeModelSizeBytes != nativeSize) + { + AnsiConsole.MarkupLine("[yellow]Execution-plan cache native model size changed; re-probing.[/]"); + return null; + } + } + List slotDevices; try { @@ -570,6 +625,7 @@ private async Task BuildExecutionPlanAsync( Q8StableNgl: row.Q8StableNgl, NativeModelSizeBytes: row.NativeModelSizeBytes, NativeStableNgl: row.NativeStableNgl, + NativeQuantizationKey: row.NativeQuantizationKey ?? string.Empty, MaxCandidateNgl: row.MaxCandidateNgl > 0 ? row.MaxCandidateNgl : NglCandidates.Max(), GpuMemoryLimitsJson: row.GpuMemoryLimitsJson ?? "{}", TensorSplitJson: row.TensorSplitJson ?? "{}"); @@ -621,6 +677,7 @@ private async Task UpsertCachedExecutionPlanAsync( existing.Q8StableNgl = plan.Q8StableNgl; existing.NativeModelSizeBytes = plan.NativeModelSizeBytes; existing.NativeStableNgl = plan.NativeStableNgl; + existing.NativeQuantizationKey = plan.NativeQuantizationKey; existing.MaxCandidateNgl = plan.MaxCandidateNgl; existing.GpuMemoryLimitsJson = plan.GpuMemoryLimitsJson; existing.TensorSplitJson = plan.TensorSplitJson; @@ -975,7 +1032,7 @@ private int ResolveDynamicNglForModel(ulong modelSizeBytes, BenchmarkSlot slot) } else { - estimateRaw = _currentPlan.StaticNgl; + estimateRaw = Math.Floor(_currentPlan.Q8StableNgl * (_currentPlan.Q8ModelSizeBytes / (double)modelSizeBytes)); } int estimate = (int)Math.Clamp(estimateRaw, 0, maxNgl); @@ -2060,8 +2117,9 @@ private async Task RunFixedCommandWithRetryAsync( if (attempt < attempts && retryable) { + int retryNgl = ExtractNglFromCommand(cmd); AnsiConsole.MarkupLine( - $"[yellow]Transient benchmark failure detected on slot {slot.SlotId} ({Markup.Escape(slot.DisplayName)}). Retrying same fixed plan...[/]"); + $"[yellow]Transient benchmark failure detected at ngl={retryNgl} on slot {slot.SlotId} ({Markup.Escape(slot.DisplayName)}); retrying same NGL once before fallback.[/]"); await Task.Delay(1500); continue; } @@ -2109,6 +2167,15 @@ private static bool LooksLikeRetryableGpuFailure(string logContent) return false; } + private static int ExtractNglFromCommand(string cmd) + { + var match = Regex.Match(cmd, @"(?:\s-ngl\s+)(\d+)", RegexOptions.IgnoreCase); + if (match.Success && int.TryParse(match.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int ngl)) + return ngl; + + return 0; + } + // ---------------------------------------------------------------- // Parsers // ---------------------------------------------------------------- @@ -2382,6 +2449,7 @@ private sealed record BenchmarkExecutionPlan( int Q8StableNgl = 0, ulong NativeModelSizeBytes = 0, int NativeStableNgl = 0, + string NativeQuantizationKey = "", int MaxCandidateNgl = 35, string GpuMemoryLimitsJson = "{}", string TensorSplitJson = "{}") @@ -2398,6 +2466,7 @@ public static BenchmarkExecutionPlan CreateCpuPlan(string q8ModelPath) Q8StableNgl: 0, NativeModelSizeBytes: 0, NativeStableNgl: 0, + NativeQuantizationKey: string.Empty, MaxCandidateNgl: NglCandidates.Max(), GpuMemoryLimitsJson: SerializeGpuMemoryLimits(), TensorSplitJson: "{}"); From e160b7c74d0e0b0c908f2239028659bfa1da245e Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 17:28:07 -0400 Subject: [PATCH 147/258] Allow cached Q8-only plans when native anchor is unavailable --- MagicQuant/Services/BenchmarkService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 615cc46..fb0516e 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -607,8 +607,10 @@ private static BenchmarkSlot BuildAllGpuSlotFromSystemInfo() _ = BuildTensorSplitArgs(slot); } + // NativeStableNgl == 0 is valid and represents "native anchor unavailable" + // while still running a Q8-based GPU plan. if (row.UsesGpu && - (row.Q8ModelSizeBytes == 0 || row.NativeModelSizeBytes == 0 || row.Q8StableNgl <= 0 || row.NativeStableNgl <= 0)) + (row.Q8ModelSizeBytes == 0 || row.NativeModelSizeBytes == 0 || row.Q8StableNgl <= 0)) { AnsiConsole.MarkupLine("[yellow]Execution-plan cache row is missing dynamic anchor metadata; re-probing.[/]"); return null; From a244c20489013e1d629e6916b3b0a4f02a2d85e2 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 27 Apr 2026 17:33:43 -0400 Subject: [PATCH 148/258] Set native quant key for CPU dynamic-plan cache rows --- MagicQuant/Services/BenchmarkService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index fb0516e..01395db 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -392,6 +392,7 @@ private async Task BuildDynamicExecutionPlanAsync( Q8StableNgl = 0, NativeModelSizeBytes = TryGetModelSize(nativeModelPath), NativeStableNgl = 0, + NativeQuantizationKey = nativeQuantizationKey, MaxCandidateNgl = NglCandidates.Max(), GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), TensorSplitJson = SerializeTensorSplitMap(plan.Slots) From 6bff9409f453708fd445e37e2f70ec8f86074776 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 27 Apr 2026 18:42:18 -0400 Subject: [PATCH 149/258] new ngl update and benchmarking. --- ...35_BenchmarkPerformanceUpgrade.Designer.cs | 786 ++++++++++++++++++ ...60427213435_BenchmarkPerformanceUpgrade.cs | 120 +++ .../MagicQuantContextModelSnapshot.cs | 33 + MagicQuant/Services/QuantizationService.cs | 57 +- 4 files changed, 992 insertions(+), 4 deletions(-) create mode 100644 MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.Designer.cs create mode 100644 MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.cs diff --git a/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.Designer.cs b/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.Designer.cs new file mode 100644 index 0000000..8ad3ef7 --- /dev/null +++ b/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.Designer.cs @@ -0,0 +1,786 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260427213435_BenchmarkPerformanceUpgrade")] + partial class BenchmarkPerformanceUpgrade + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("NormalizedName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("TensorCount") + .HasColumnType("INTEGER"); + + b.Property("TensorSignatureHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique(); + + b.HasIndex("TensorSignatureHash", "TensorCount"); + + b.ToTable("ArchitectureFamilies"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IsCanonical") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "AiModelHashId") + .IsUnique(); + + b.ToTable("ArchitectureFamilyModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("BitRange") + .HasColumnType("INTEGER"); + + b.Property("CanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ExplicitCandidateSortOrder") + .HasColumnType("INTEGER"); + + b.Property("IsCombinationCarrierCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsCustomBaseline") + .HasColumnType("INTEGER"); + + b.Property("IsExplicitGroupCombinationCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsLearningBaseline") + .HasColumnType("INTEGER"); + + b.Property("QuantizeBaseArgumentName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RequiresImatrix") + .HasColumnType("INTEGER"); + + b.Property("ShortSourceName") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceOwner") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("SourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("BaselineQuantId"); + + b.HasIndex("CanonicalKey") + .IsUnique(); + + b.HasIndex("SourceRepository", "SourceFileName"); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GpuMemoryLimitsJson") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("MaxCandidateNgl") + .HasColumnType("INTEGER"); + + b.Property("NativeModelSizeBytes") + .HasColumnType("INTEGER"); + + b.Property("NativeQuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("NativeStableNgl") + .HasColumnType("INTEGER"); + + b.Property("ProbeSchemaVersion") + .HasColumnType("INTEGER"); + + b.Property("Q8ModelSizeBytes") + .HasColumnType("INTEGER"); + + b.Property("Q8StableNgl") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("TensorSplitJson") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.cs b/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.cs new file mode 100644 index 0000000..2136de2 --- /dev/null +++ b/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.cs @@ -0,0 +1,120 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class BenchmarkPerformanceUpgrade : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "GpuMemoryLimitsJson", + table: "ExecutionPlanProbeCaches", + type: "TEXT", + maxLength: 4000, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "MaxCandidateNgl", + table: "ExecutionPlanProbeCaches", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "NativeModelSizeBytes", + table: "ExecutionPlanProbeCaches", + type: "INTEGER", + nullable: false, + defaultValue: 0ul); + + migrationBuilder.AddColumn( + name: "NativeQuantizationKey", + table: "ExecutionPlanProbeCaches", + type: "TEXT", + maxLength: 128, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "NativeStableNgl", + table: "ExecutionPlanProbeCaches", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "ProbeSchemaVersion", + table: "ExecutionPlanProbeCaches", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Q8ModelSizeBytes", + table: "ExecutionPlanProbeCaches", + type: "INTEGER", + nullable: false, + defaultValue: 0ul); + + migrationBuilder.AddColumn( + name: "Q8StableNgl", + table: "ExecutionPlanProbeCaches", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "TensorSplitJson", + table: "ExecutionPlanProbeCaches", + type: "TEXT", + maxLength: 4000, + nullable: false, + defaultValue: ""); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "GpuMemoryLimitsJson", + table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn( + name: "MaxCandidateNgl", + table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn( + name: "NativeModelSizeBytes", + table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn( + name: "NativeQuantizationKey", + table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn( + name: "NativeStableNgl", + table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn( + name: "ProbeSchemaVersion", + table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn( + name: "Q8ModelSizeBytes", + table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn( + name: "Q8StableNgl", + table: "ExecutionPlanProbeCaches"); + + migrationBuilder.DropColumn( + name: "TensorSplitJson", + table: "ExecutionPlanProbeCaches"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index b6d59f7..c058062 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -314,6 +314,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("DiscoveryTokenTarget") .HasColumnType("INTEGER"); + b.Property("GpuMemoryLimitsJson") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + b.Property("GroupSize") .HasColumnType("INTEGER"); @@ -325,6 +330,29 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("ImatrixDefinitionId") .HasColumnType("INTEGER"); + b.Property("MaxCandidateNgl") + .HasColumnType("INTEGER"); + + b.Property("NativeModelSizeBytes") + .HasColumnType("INTEGER"); + + b.Property("NativeQuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("NativeStableNgl") + .HasColumnType("INTEGER"); + + b.Property("ProbeSchemaVersion") + .HasColumnType("INTEGER"); + + b.Property("Q8ModelSizeBytes") + .HasColumnType("INTEGER"); + + b.Property("Q8StableNgl") + .HasColumnType("INTEGER"); + b.Property("QuantizationKey") .IsRequired() .HasMaxLength(128) @@ -343,6 +371,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("StaticNgl") .HasColumnType("INTEGER"); + b.Property("TensorSplitJson") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + b.Property("UpdatedUtc") .HasColumnType("TEXT"); diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index d804ad3..c163121 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -50,6 +50,7 @@ public class QuantizationService private readonly string _benchDir; private readonly PythonManager _python; private readonly SemaphoreSlim _cpuQuantLock; + private readonly int _quantThreadsPerProcess; private readonly int _maxConcurrentQuantizations; private readonly ImatrixService _imatrixService; private readonly HuggingFaceBaselineService _huggingFaceBaselineService; @@ -88,8 +89,56 @@ public QuantizationService(BenchmarkService benchmarker) Directory.CreateDirectory(_benchDir); int threadCount = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; - _maxConcurrentQuantizations = Math.Max(1, threadCount / 8); - _cpuQuantLock = new SemaphoreSlim(_maxConcurrentQuantizations, _maxConcurrentQuantizations); + +// Minimum desired threads per llama-quantize process. +// This is used to decide the natural concurrency first. + const int minimumQuantThreadsPerProcess = 8; + +// Hard safety cap for large GGUF quantization. +// More than 2 concurrent 35B quantizers can overwhelm the output NVMe queue. + const int maxConcurrentQuantizationCap = 2; + +// Keep a little workstation breathing room. + int reservedThreads = threadCount switch + { + >= 16 => 2, + >= 8 => 2, + >= 4 => 1, + _ => 0 + }; + + int usableThreads = Math.Max(1, threadCount - reservedThreads); + +// First decide how many quantization processes the CPU budget would naturally allow. + int naturalConcurrentQuantizations = Math.Max( + 1, + usableThreads / minimumQuantThreadsPerProcess); + +// Then cap it to avoid hammering the output drive with too many giant writers. + _maxConcurrentQuantizations = Math.Max( + 1, + Math.Min(maxConcurrentQuantizationCap, naturalConcurrentQuantizations)); + +// Divide the usable thread budget evenly across the allowed quantization processes. +// Example on 7950X3D: +// 32 total - 2 reserved = 30 usable +// natural = 30 / 8 = 3 +// capped = min(2, 3) = 2 +// threads/process = 30 / 2 = 15 + _quantThreadsPerProcess = Math.Max( + 1, + usableThreads / _maxConcurrentQuantizations); + + _cpuQuantLock = new SemaphoreSlim( + _maxConcurrentQuantizations, + _maxConcurrentQuantizations); + + AnsiConsole.MarkupLine( + $"[grey]Quantization CPU plan:[/] " + + $"threads={threadCount}, reserved={reservedThreads}, usable={usableThreads}, " + + $"naturalConcurrent={naturalConcurrentQuantizations}, " + + $"concurrent={_maxConcurrentQuantizations}, " + + $"threads/process={_quantThreadsPerProcess}"); } public static void ValidateQuantNameNormalizationOrThrow() @@ -1468,7 +1517,7 @@ private async Task RunLlamaQuantizeWithExactTensorM args.Add($"\"{inputFile}\""); args.Add($"\"{outputFile}\""); args.Add(baseQuant.QuantizeBaseArgumentName); - args.Add("8"); + args.Add(_quantThreadsPerProcess.ToString()); string bin = Path.Combine( Cache.LlamaBin!, @@ -1583,7 +1632,7 @@ private async Task RunLlamaQuantizeAsync(string inp args.Add($"\"{inputFile}\""); args.Add($"\"{outputFile}\""); args.Add(ResolveQuantizeBaseArgument(quant, concreteOverrides)); - args.Add("8"); + args.Add(_quantThreadsPerProcess.ToString()); string arguments = string.Join(" ", args); From 09c34c98efc719a63768a9dea9bd0ba679762d08 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 27 Apr 2026 23:53:18 -0400 Subject: [PATCH 150/258] starting better pipeline for quantization --- MagicQuant/Services/QuantizationService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index c163121..4131fd1 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -96,7 +96,7 @@ public QuantizationService(BenchmarkService benchmarker) // Hard safety cap for large GGUF quantization. // More than 2 concurrent 35B quantizers can overwhelm the output NVMe queue. - const int maxConcurrentQuantizationCap = 2; + const int maxConcurrentQuantizationCap = 1; // Keep a little workstation breathing room. int reservedThreads = threadCount switch From 43308ed1c560a8189a9286e70fdbf1223dd0c4f0 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 28 Apr 2026 00:04:55 -0400 Subject: [PATCH 151/258] Clean and isolate external baseline staging artifacts --- MagicQuant/Program.cs | 64 +++++++++++++++++- MagicQuant/Services/QuantizationService.cs | 77 +++++++++++++++++++--- 2 files changed, 131 insertions(+), 10 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 516ea08..9be3f39 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -3,6 +3,7 @@ using MagicQuant.Helpers; using MagicQuant.Models; using MagicQuant.Services; +using MQ.DB; using MQ.DB.Models; using Spectre.Console; @@ -65,6 +66,7 @@ try { var loadedConfig = MagicQuantYamlLoader.LoadAndApply(commandInput, parsedArgs); + await CleanupExternalBaselineCacheDirectoryAsync(); TensorWeightScheme.ValidateSmallestConfiguration(); BaselineQuants.ValidateIntegrityOrThrow(); @@ -103,4 +105,64 @@ await AnsiConsole.Status() catch (Exception ex) { AnsiConsole.WriteException(ex); -} \ No newline at end of file +} + +static async Task CleanupExternalBaselineCacheDirectoryAsync() +{ + var root = Cache.ExternalBaselineCacheDirectory; + if (string.IsNullOrWhiteSpace(root)) + return; + + var fullRoot = Path.GetFullPath(root); + if (!IsSafeExternalBaselineCacheRoot(fullRoot)) + return; + + Directory.CreateDirectory(fullRoot); + + foreach (var file in Directory.EnumerateFiles(fullRoot)) + await HardDeleteHelper.DeleteFileIfExistsAsync(file); + + foreach (var directory in Directory.EnumerateDirectories(fullRoot)) + Directory.Delete(directory, recursive: true); + + Directory.CreateDirectory(fullRoot); +} + +static bool IsSafeExternalBaselineCacheRoot(string fullRoot) +{ + if (string.IsNullOrWhiteSpace(fullRoot)) + return false; + + var normalizedRoot = Path.GetFullPath(fullRoot) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + if (Path.GetPathRoot(normalizedRoot)?.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + .Equals(normalizedRoot, StringComparison.OrdinalIgnoreCase) == true) + { + return false; + } + + if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) + { + var magicRoot = Path.GetFullPath(Cache.MagicQuantDirectory) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + if (string.Equals(normalizedRoot, magicRoot, StringComparison.OrdinalIgnoreCase)) + return false; + + if (!IsPathInside(normalizedRoot, magicRoot)) + return false; + } + + return true; +} + +static bool IsPathInside(string childPath, string parentPath) +{ + var child = Path.GetFullPath(childPath) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + var parent = Path.GetFullPath(parentPath) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + return child.StartsWith(parent + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase); +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 4131fd1..4d2c18c 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -506,8 +506,12 @@ public async Task ProcessHybridQuantAsync( try { - string inputPath = await GetEffectiveInputModelPathAsync(quant, forceBaselineRelearn, ct); - if (pureExternalBaseline) + string inputPath = await GetEffectiveInputModelPathAsync( + quant, + forceBaselineRelearn, + baselineLearnedTruthExists, + ct); + if (pureExternalBaseline && IsPathInsideExternalBaselineCacheRoot(inputPath)) transientExternalDownloadPath = inputPath; QuantizationExecutionReport? quantizationReport = null; @@ -632,7 +636,10 @@ await PersistQuantizationRunAsync( private bool ShouldDownloadExternalBaselineInsteadOfQuantizing(HybridQuant quant) => quant.BaseQuant.IsExternalRepositoryBaseline && quant.Tensors.Count == 0; - private async Task GetEffectiveInputModelPathAsync(HybridQuant quant, bool forceRefresh, + private async Task GetEffectiveInputModelPathAsync( + HybridQuant quant, + bool forceRefresh, + bool baselineLearnedTruthExists, CancellationToken ct) { string basePath = await EnsureBaseModelFileAsync(); @@ -645,6 +652,9 @@ private async Task GetEffectiveInputModelPathAsync(HybridQuant quant, bo if (quant.Tensors.Count > 0) return basePath; + if (!forceRefresh && baselineLearnedTruthExists) + return basePath; + string externalPath = GetExternalBaselineCachePath(quant.BaseQuant); await _huggingFaceBaselineService.DownloadBaselineAsync(quant.BaseQuant, externalPath, forceRefresh, ct); await ValidateExternalBaselineTensorParityOrThrow(basePath, externalPath); @@ -661,7 +671,10 @@ private string GetExternalBaselineCachePath(BaselineQuants baseline) string extension = Path.GetExtension(baseline.SourceFileName ?? string.Empty); if (string.IsNullOrWhiteSpace(extension)) extension = ".gguf"; - return Path.Combine(root, safe + extension); + + string stagingDirectory = Path.Combine(root, $"{safe}-{Guid.NewGuid():N}"); + Directory.CreateDirectory(stagingDirectory); + return Path.Combine(stagingDirectory, safe + extension); } private async Task ValidateExternalBaselineTensorParityOrThrow(string baseModelPath, string externalBaselinePath) @@ -951,12 +964,51 @@ await WriteLearningDiagnosticArtifactAsync( $"[green]Persisted rebuilt custom-baseline learning truth:[/] [cyan]{rows.Count:N0}[/] row(s) for [yellow]{Markup.Escape(quant.BaseQuant.Names[0])}[/]."); } - private async Task CleanupExternalBaselineDownloadArtifactsAsync(string downloadedExternalBaselinePath) + private async Task CleanupExternalBaselineDownloadArtifactsAsync(string? downloadedExternalBaselinePath) { if (string.IsNullOrWhiteSpace(downloadedExternalBaselinePath)) return; - await HardDeleteHelper.DeleteFileIfExistsAsync(downloadedExternalBaselinePath); + string fullFile = Path.GetFullPath(downloadedExternalBaselinePath); + string? root = Cache.ExternalBaselineCacheDirectory; + + if (!string.IsNullOrWhiteSpace(root)) + { + string fullRoot = Path.GetFullPath(root); + string? stagingDir = Path.GetDirectoryName(fullFile); + + if (!string.IsNullOrWhiteSpace(stagingDir)) + { + string fullStagingDir = Path.GetFullPath(stagingDir); + + if (IsPathInside(fullStagingDir, fullRoot) && + !string.Equals(fullStagingDir, fullRoot, StringComparison.OrdinalIgnoreCase) && + Directory.Exists(fullStagingDir)) + { + Directory.Delete(fullStagingDir, recursive: true); + return; + } + } + } + + await HardDeleteHelper.DeleteFileIfExistsAsync(fullFile); + await HardDeleteHelper.DeleteFileIfExistsAsync(fullFile + ".nativecheck"); + await HardDeleteHelper.DeleteFileIfExistsAsync(fullFile + ".externalcheck"); + } + + private bool IsPathInsideExternalBaselineCacheRoot(string path) + { + if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory)) + return false; + + return IsPathInside(path, Cache.ExternalBaselineCacheDirectory); + } + + private static bool IsPathInside(string childPath, string parentPath) + { + var child = Path.GetFullPath(childPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + var parent = Path.GetFullPath(parentPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + return child.StartsWith(parent + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase); } // ---------------------------------------------------------------- @@ -1293,6 +1345,8 @@ public async Task BuildExportArtifactFromExactTensorMapAsync( if (forceRebuild && File.Exists(outputPath)) await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + string? transientExternalDownloadPath = null; + await _cpuQuantLock.WaitAsync(ct); try { @@ -1333,6 +1387,8 @@ public async Task BuildExportArtifactAsync( if (forceRebuild && File.Exists(outputPath)) await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + string? transientExternalDownloadPath = null; + await _cpuQuantLock.WaitAsync(ct); try { @@ -1345,10 +1401,10 @@ public async Task BuildExportArtifactAsync( if (quant.BaseQuant.IsExternalRepositoryBaseline) { - string downloadedExternalBaselinePath = GetExternalBaselineCachePath(quant.BaseQuant); + transientExternalDownloadPath = GetExternalBaselineCachePath(quant.BaseQuant); await _huggingFaceBaselineService.DownloadBaselineAsync( quant.BaseQuant, - downloadedExternalBaselinePath, + transientExternalDownloadPath, forceRedownload: false, ct: ct); @@ -1377,6 +1433,9 @@ await RunLlamaQuantizeAsync( finally { _cpuQuantLock.Release(); + + if (!string.IsNullOrWhiteSpace(transientExternalDownloadPath)) + await CleanupExternalBaselineDownloadArtifactsAsync(transientExternalDownloadPath); } } @@ -3246,4 +3305,4 @@ void HandleLine(string? line, bool isError) StdErr = stderrBuilder.ToString() }; } -} \ No newline at end of file +} From 7414ba4a2ce9d9eb6f685af5655c8cd0fb24a2cd Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 28 Apr 2026 00:08:36 -0400 Subject: [PATCH 152/258] Use HardDeleteHelper for external baseline directory cleanup --- MagicQuant/Program.cs | 2 +- MagicQuant/Services/QuantizationService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 9be3f39..4e4979a 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -123,7 +123,7 @@ static async Task CleanupExternalBaselineCacheDirectoryAsync() await HardDeleteHelper.DeleteFileIfExistsAsync(file); foreach (var directory in Directory.EnumerateDirectories(fullRoot)) - Directory.Delete(directory, recursive: true); + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(directory); Directory.CreateDirectory(fullRoot); } diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 4d2c18c..b995720 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -985,7 +985,7 @@ private async Task CleanupExternalBaselineDownloadArtifactsAsync(string? downloa !string.Equals(fullStagingDir, fullRoot, StringComparison.OrdinalIgnoreCase) && Directory.Exists(fullStagingDir)) { - Directory.Delete(fullStagingDir, recursive: true); + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(fullStagingDir); return; } } From b9c5f5339184165b25044cb7a9d6a27535e81d8e Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 28 Apr 2026 00:27:36 -0400 Subject: [PATCH 153/258] Cleanup failed external baseline staging before rethrow --- MagicQuant/Services/QuantizationService.cs | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index b995720..8f21186 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -656,9 +656,26 @@ private async Task GetEffectiveInputModelPathAsync( return basePath; string externalPath = GetExternalBaselineCachePath(quant.BaseQuant); - await _huggingFaceBaselineService.DownloadBaselineAsync(quant.BaseQuant, externalPath, forceRefresh, ct); - await ValidateExternalBaselineTensorParityOrThrow(basePath, externalPath); - return externalPath; + try + { + await _huggingFaceBaselineService.DownloadBaselineAsync(quant.BaseQuant, externalPath, forceRefresh, ct); + await ValidateExternalBaselineTensorParityOrThrow(basePath, externalPath); + return externalPath; + } + catch + { + try + { + await CleanupExternalBaselineDownloadArtifactsAsync(externalPath); + } + catch (Exception cleanupEx) + { + AnsiConsole.MarkupLine( + $"[yellow]Warning:[/] failed to clean external baseline staging after failed download/validation: {Markup.Escape(cleanupEx.Message)}"); + } + + throw; + } } private string GetExternalBaselineCachePath(BaselineQuants baseline) From a5474a424b709a482fb8868a9c933128bbe7e421 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 28 Apr 2026 00:29:15 -0400 Subject: [PATCH 154/258] removed MXFP4 beause it's causing issues. --- MQ.DB/Models/BaselineQuants.cs | 2 +- MagicQuant/config.dev.yaml | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index c0010bf..67d1cbe 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -116,7 +116,7 @@ private static BaselineQuants Create( Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 9); public static readonly BaselineQuants MXFP4_MOE = - Create(15, false, "MXFP4_MOE", "MXFP4_MOE", TensorWeightScheme.MXFP4, [TensorWeightScheme.MXFP4, TensorWeightScheme.IQ3_S, TensorWeightScheme.IQ3_XS], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 8); + Create(15, false, "MXFP4_MOE", "MXFP4_MOE", TensorWeightScheme.MXFP4, [TensorWeightScheme.MXFP4, TensorWeightScheme.IQ3_S, TensorWeightScheme.IQ3_XS], [TReg.MoeRouter.UniqueId], false, false, false, false, 4, 8); public static readonly BaselineQuants IQ3_M = Create(17, true, "IQ3_M", "IQ3_M", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 7); diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 417d673..7cdc2ee 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -136,13 +136,7 @@ baselines: allow_as_explicit_group_candidate: false includes: - - file_name: Qwen3.6-35B-A3B-MXFP4_MOE.gguf - baseline_family: MXFP4_MOE - quantize_base_name: MXFP4_MOE - display_name: UD-MXFP4_MOE - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true + - file_name: Qwen3.6-35B-A3B-UD-IQ2_M.gguf baseline_family: IQ2_M From 4da733cbea4eae4644b484769bc0aaa9f34bbe7a Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 28 Apr 2026 11:38:07 -0400 Subject: [PATCH 155/258] Add scratch lease storage and model-scoped external baseline paths --- MQ.DB/Cache.cs | 7 + .../ScratchStorageServiceTests.cs | 53 ++++ MagicQuant/Commands/CloneRepositoryQuants.cs | 7 +- MagicQuant/Commands/Evolution.cs | 24 +- MagicQuant/Commands/ValidatePredictions.cs | 1 + .../Configuration/MagicQuantYamlConfig.cs | 1 + .../Configuration/MagicQuantYamlLoader.cs | 17 +- MagicQuant/Program.cs | 62 +--- .../Services/ModelArtifactPathService.cs | 79 +++++ .../Services/ModelRuntimePathService.cs | 18 ++ MagicQuant/Services/QuantizationService.cs | 292 +++++++----------- MagicQuant/Services/ScratchStorageService.cs | 196 ++++++++++++ MagicQuant/config.default.yaml | 8 +- MagicQuant/config.dev.yaml | 4 + 14 files changed, 497 insertions(+), 272 deletions(-) create mode 100644 MagicQuant.Tests/ScratchStorageServiceTests.cs create mode 100644 MagicQuant/Services/ModelArtifactPathService.cs create mode 100644 MagicQuant/Services/ModelRuntimePathService.cs create mode 100644 MagicQuant/Services/ScratchStorageService.cs diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index c2b664f..ca61897 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -51,6 +51,13 @@ public class Cache /// public static string? ExternalBaselineCacheDirectory { get; set; } + + /// + /// Normalized configured scratch roots for transient heavy GGUF writes. + /// + public static List ScratchRoots { get; set; } = new(); + + /// /// Aka BF16, F16, or F32 /// diff --git a/MagicQuant.Tests/ScratchStorageServiceTests.cs b/MagicQuant.Tests/ScratchStorageServiceTests.cs new file mode 100644 index 0000000..45dafca --- /dev/null +++ b/MagicQuant.Tests/ScratchStorageServiceTests.cs @@ -0,0 +1,53 @@ +using MagicQuant.Services; +using MQ.DB; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class ScratchStorageServiceTests +{ + [Fact] + public async Task EmptyScratchRoots_FallsBackToSingleWriterCapacity() + { + var temp = Path.Combine(Path.GetTempPath(), "mq-tests", Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(temp); + + Cache.MagicQuantDirectory = temp; + Cache.ModelDirectory = temp; + Cache.ModelMagicQuantDirectory = Path.Combine(temp, "MagicQuant"); + Cache.CurrentModelId = "test-model"; + Cache.ScratchRoots = new List(); + + var paths = new ModelArtifactPathService(); + var service = new ScratchStorageService(paths); + + Assert.Equal(1, service.WriterCapacity); + + await service.CleanupStaleScratchArtifactsAsync(); + } + + [Fact] + public async Task PreserveOutput_PreventsLeaseDeletion() + { + var temp = Path.Combine(Path.GetTempPath(), "mq-tests", Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(temp); + + Cache.MagicQuantDirectory = temp; + Cache.ModelDirectory = temp; + Cache.ModelMagicQuantDirectory = Path.Combine(temp, "MagicQuant"); + Cache.CurrentModelId = "test-model"; + Cache.ScratchRoots = new List { temp }; + + var paths = new ModelArtifactPathService(); + var service = new ScratchStorageService(paths); + + var lease = await service.AcquireAsync(ScratchArtifactKind.Other, "artifact"); + await File.WriteAllTextAsync(lease.GgufPath, "x"); + lease.PreserveOutput(); + await lease.DisposeAsync(); + + Assert.True(Directory.Exists(lease.LeaseDirectory)); + + await service.CleanupStaleScratchArtifactsAsync(); + } +} diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 0740235..2a94d54 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -57,6 +57,7 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + ModelRuntimePathService.InitializeForCurrentModel(); Cache.ForceRelearnBaselineTensorMappings = false; Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; @@ -129,16 +130,16 @@ await File.WriteAllTextAsync( Path.Combine(Cache.OutputDirectory!, CloneConfigManifestGenerationService.FileName), JsonSerializer.Serialize(manifest, JsonOptions)); - string q8Path = await quantizationService.EnsurePureQ8ModelAsync(); + await using var q8Lease = await quantizationService.BuildPureQ8ProbeLeaseAsync(); await benchmarkService.EnsureExecutionPlanAsync( - q8ModelPath: q8Path, + q8ModelPath: q8Lease.GgufPath, discoveryTokenTarget: 8192, quantizationKey: "Q8_0", forceRediscovery: Cache.ForceRefreshHardwareProbe); var q8Reference = await benchmarkService.RunAllBenchmarksAsync( quantConfig: HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), - modelPath: q8Path, + modelPath: q8Lease.GgufPath, benchDir: Path.Combine(Cache.ModelMagicQuantDirectory!, "CloneBenchmarks", "_reference_q8"), domainsOverride: new[] { "general" }); diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 7ec2a94..b8adc17 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -61,6 +61,7 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + ModelRuntimePathService.InitializeForCurrentModel(); Cache.ForceRelearnBaselineTensorMappings = Config.Current.Flags.ForceRelearnBaselineTensorMappings; Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; @@ -161,22 +162,13 @@ await benchmarkService.TryInitializeDynamicExecutionPlanFromCacheAsync( if (!loadedPlanFromCache) { AnsiConsole.MarkupLine("[grey]Dynamic execution-plan cache not usable; probing Q8 + native anchors...[/]"); - string? q8ModelGgufPath = null; - - try - { - q8ModelGgufPath = await quantizationService.EnsurePureQ8ModelAsync(); - await benchmarkService.EnsureDynamicExecutionPlanAsync( - q8ModelPath: q8ModelGgufPath, - nativeModelPath: bf16ModelGgufPath, - q8QuantizationKey: q8QuantizationKey, - nativeQuantizationKey: baseTypeName, - forceRediscovery: Cache.ForceRefreshHardwareProbe); - } - finally - { - await quantizationService.CleanupPureQ8ModelAsync(); - } + await using var q8Lease = await quantizationService.BuildPureQ8ProbeLeaseAsync(); + await benchmarkService.EnsureDynamicExecutionPlanAsync( + q8ModelPath: q8Lease.GgufPath, + nativeModelPath: bf16ModelGgufPath, + q8QuantizationKey: q8QuantizationKey, + nativeQuantizationKey: baseTypeName, + forceRediscovery: Cache.ForceRefreshHardwareProbe); } bool nativeTruthAlreadyLearned = diff --git a/MagicQuant/Commands/ValidatePredictions.cs b/MagicQuant/Commands/ValidatePredictions.cs index 0d6b982..a331995 100644 --- a/MagicQuant/Commands/ValidatePredictions.cs +++ b/MagicQuant/Commands/ValidatePredictions.cs @@ -34,6 +34,7 @@ public async Task Run(List args) Cache.ModelDirectory = modelDir; Cache.ModelMagicQuantDirectory = Path.Combine(modelDir, "MagicQuant"); + ModelRuntimePathService.InitializeForCurrentModel(); Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(modelDir); diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 2a90398..bbe4516 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -86,6 +86,7 @@ public sealed class RuntimePathConfig public string? LlamaRoot { get; set; } public string? LlamaBin { get; set; } public string? ConvertScript { get; set; } + public List ScratchRoots { get; set; } = new(); public string ExternalBaselineCacheDirName { get; set; } = "ExternalBaselines"; } diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 36cd5d1..e81177d 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -62,12 +62,9 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) Cache.LlamaRoot = NormalizeNullOrFullPath(config.Paths.LlamaRoot); Cache.LlamaBin = NormalizeNullOrFullPath(config.Paths.LlamaBin); Cache.ConvertScript = NormalizeNullOrFullPath(config.Paths.ConvertScript); - Cache.ExternalBaselineCacheDirectory = Path.Combine( - config.Paths.MagicQuantRoot!, - string.IsNullOrWhiteSpace(config.Paths.ExternalBaselineCacheDirName) ? "ExternalBaselines" : config.Paths.ExternalBaselineCacheDirName); + Cache.ScratchRoots = NormalizeScratchRoots(config.Paths.ScratchRoots); Directory.CreateDirectory(Cache.MagicQuantDirectory!); - Directory.CreateDirectory(Cache.ExternalBaselineCacheDirectory!); Cache.UseImatrix = config.Flags.UseImatrix; Cache.ForceImatrixRebuild = config.Flags.ForceImatrixRebuild; @@ -301,6 +298,18 @@ private static string ResolveMagicQuantRoot(string? configured) return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), MagicConstants.MagicQuantFolder); } + + private static List NormalizeScratchRoots(IEnumerable? roots) + { + if (roots == null) + return new List(); + + return roots + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Select(x => Path.GetFullPath(x.Trim())) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } private static string? NormalizeNullOrFullPath(string? value) { if (string.IsNullOrWhiteSpace(value)) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 4e4979a..5737bb2 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -66,7 +66,8 @@ try { var loadedConfig = MagicQuantYamlLoader.LoadAndApply(commandInput, parsedArgs); - await CleanupExternalBaselineCacheDirectoryAsync(); + var startupScratch = new ScratchStorageService(); + await startupScratch.CleanupStaleScratchArtifactsAsync(); TensorWeightScheme.ValidateSmallestConfiguration(); BaselineQuants.ValidateIntegrityOrThrow(); @@ -107,62 +108,3 @@ await AnsiConsole.Status() AnsiConsole.WriteException(ex); } -static async Task CleanupExternalBaselineCacheDirectoryAsync() -{ - var root = Cache.ExternalBaselineCacheDirectory; - if (string.IsNullOrWhiteSpace(root)) - return; - - var fullRoot = Path.GetFullPath(root); - if (!IsSafeExternalBaselineCacheRoot(fullRoot)) - return; - - Directory.CreateDirectory(fullRoot); - - foreach (var file in Directory.EnumerateFiles(fullRoot)) - await HardDeleteHelper.DeleteFileIfExistsAsync(file); - - foreach (var directory in Directory.EnumerateDirectories(fullRoot)) - await HardDeleteHelper.DeleteDirectoryIfExistsAsync(directory); - - Directory.CreateDirectory(fullRoot); -} - -static bool IsSafeExternalBaselineCacheRoot(string fullRoot) -{ - if (string.IsNullOrWhiteSpace(fullRoot)) - return false; - - var normalizedRoot = Path.GetFullPath(fullRoot) - .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - - if (Path.GetPathRoot(normalizedRoot)?.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) - .Equals(normalizedRoot, StringComparison.OrdinalIgnoreCase) == true) - { - return false; - } - - if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) - { - var magicRoot = Path.GetFullPath(Cache.MagicQuantDirectory) - .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - - if (string.Equals(normalizedRoot, magicRoot, StringComparison.OrdinalIgnoreCase)) - return false; - - if (!IsPathInside(normalizedRoot, magicRoot)) - return false; - } - - return true; -} - -static bool IsPathInside(string childPath, string parentPath) -{ - var child = Path.GetFullPath(childPath) - .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - var parent = Path.GetFullPath(parentPath) - .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - - return child.StartsWith(parent + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase); -} diff --git a/MagicQuant/Services/ModelArtifactPathService.cs b/MagicQuant/Services/ModelArtifactPathService.cs new file mode 100644 index 0000000..475386d --- /dev/null +++ b/MagicQuant/Services/ModelArtifactPathService.cs @@ -0,0 +1,79 @@ +using System.Text; +using MQ.DB; +using MQ.DB.Models; + +namespace MagicQuant.Services; + +public sealed class ModelArtifactPathService +{ + public string ModelDirectory => Cache.ModelDirectory + ?? throw new InvalidOperationException("Cache.ModelDirectory is not set."); + + public string ModelMagicQuantDirectory => Cache.ModelMagicQuantDirectory + ?? throw new InvalidOperationException("Cache.ModelMagicQuantDirectory is not set."); + + public string GgufDir => Path.Combine(ModelMagicQuantDirectory, "GGUF"); + public string BenchDir => Path.Combine(ModelMagicQuantDirectory, "Benchmarks"); + public string LogsDir => Path.Combine(ModelMagicQuantDirectory, "Logs"); + public string QuantizationLogsDir => Path.Combine(LogsDir, "Quantization"); + public string ExternalBaselinesDir => Cache.ExternalBaselineCacheDirectory + ?? Path.Combine(ModelMagicQuantDirectory, "ExternalBaselines"); + + public string ScratchModelNamespace + { + get + { + var modelName = new DirectoryInfo(ModelDirectory).Name; + if (string.IsNullOrWhiteSpace(modelName)) + modelName = "model"; + + var modelId = string.IsNullOrWhiteSpace(Cache.CurrentModelId) ? "unknown" : Cache.CurrentModelId; + return MakeSafeFileComponent($"{modelName}_{modelId}"); + } + } + + public string GetBenchmarkDir(string modelName) => Path.Combine(BenchDir, modelName); + + public string GetBaseLogitsDirectory() + { + string typeStr = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + return Path.Combine(BenchDir, typeStr, "logits"); + } + + public string GetNativeBaseGgufPath() + { + string modelName = new DirectoryInfo(ModelDirectory).Name; + var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; + string typeStr = torchType.ToString(); + return Path.Combine(GgufDir, $"{modelName}-{typeStr}.gguf"); + } + + public string GetExternalBaselineDurablePath(BaselineQuants baseline) + { + string safe = MakeSafeFileComponent(baseline.CanonicalKey); + string extension = Path.GetExtension(baseline.SourceFileName ?? string.Empty); + if (string.IsNullOrWhiteSpace(extension)) + extension = ".gguf"; + + return Path.Combine(ExternalBaselinesDir, safe + extension); + } + + public string GetQuantizationLogPath(string artifactName, Guid leaseId) + { + string safeName = MakeSafeFileComponent(artifactName); + return Path.Combine(QuantizationLogsDir, $"{safeName}-{leaseId:N}.quantize.log"); + } + + public static string MakeSafeFileComponent(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return "artifact"; + + var invalid = Path.GetInvalidFileNameChars().ToHashSet(); + var sb = new StringBuilder(value.Length); + foreach (var ch in value) + sb.Append(invalid.Contains(ch) ? '_' : ch); + + return sb.ToString(); + } +} diff --git a/MagicQuant/Services/ModelRuntimePathService.cs b/MagicQuant/Services/ModelRuntimePathService.cs new file mode 100644 index 0000000..aa84e70 --- /dev/null +++ b/MagicQuant/Services/ModelRuntimePathService.cs @@ -0,0 +1,18 @@ +using MQ.DB; + +namespace MagicQuant.Services; + +public static class ModelRuntimePathService +{ + public static void InitializeForCurrentModel() + { + if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + throw new InvalidOperationException("Cache.ModelMagicQuantDirectory must be set before initializing runtime model paths."); + + string externalName = Config.Current.Paths.ExternalBaselineCacheDirName; + if (string.IsNullOrWhiteSpace(externalName)) + externalName = "ExternalBaselines"; + + Cache.ExternalBaselineCacheDirectory = Path.Combine(Cache.ModelMagicQuantDirectory, externalName); + } +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 8f21186..449cba3 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -46,8 +46,8 @@ public sealed class SampleProcessingSummary public class QuantizationService { private readonly BenchmarkService _benchmarker; - private readonly string _ggufDir; - private readonly string _benchDir; + private readonly ModelArtifactPathService _paths; + private readonly ScratchStorageService _scratchStorage; private readonly PythonManager _python; private readonly SemaphoreSlim _cpuQuantLock; private readonly int _quantThreadsPerProcess; @@ -78,25 +78,22 @@ public QuantizationService(BenchmarkService benchmarker) if (string.IsNullOrWhiteSpace(Cache.LlamaBin)) throw new Exception("Cache.LlamaBin not set. Initialization must complete before quantization starts."); - _ggufDir = Path.Combine(Cache.ModelMagicQuantDirectory, "GGUF"); - _benchDir = Path.Combine(Cache.ModelMagicQuantDirectory, "Benchmarks"); + _paths = new ModelArtifactPathService(); + _scratchStorage = new ScratchStorageService(_paths); _imatrixService = new ImatrixService(); _huggingFaceBaselineService = new HuggingFaceBaselineService(_python); _tensorGroupingAuditService = new TensorGroupingAuditService(); _tensorLearningDiagnosticWriter = new TensorLearningDiagnosticWriter(); - Directory.CreateDirectory(_ggufDir); - Directory.CreateDirectory(_benchDir); + Directory.CreateDirectory(_paths.GgufDir); + Directory.CreateDirectory(_paths.BenchDir); + Directory.CreateDirectory(_paths.QuantizationLogsDir); int threadCount = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; // Minimum desired threads per llama-quantize process. // This is used to decide the natural concurrency first. - const int minimumQuantThreadsPerProcess = 8; - -// Hard safety cap for large GGUF quantization. -// More than 2 concurrent 35B quantizers can overwhelm the output NVMe queue. - const int maxConcurrentQuantizationCap = 1; + const int minimumQuantThreadsPerProcess = 4; // Keep a little workstation breathing room. int reservedThreads = threadCount switch @@ -115,9 +112,10 @@ public QuantizationService(BenchmarkService benchmarker) usableThreads / minimumQuantThreadsPerProcess); // Then cap it to avoid hammering the output drive with too many giant writers. + int scratchWriterCapacity = _scratchStorage.WriterCapacity; _maxConcurrentQuantizations = Math.Max( 1, - Math.Min(maxConcurrentQuantizationCap, naturalConcurrentQuantizations)); + Math.Min(naturalConcurrentQuantizations, scratchWriterCapacity)); // Divide the usable thread budget evenly across the allowed quantization processes. // Example on 7950X3D: @@ -137,6 +135,7 @@ public QuantizationService(BenchmarkService benchmarker) $"[grey]Quantization CPU plan:[/] " + $"threads={threadCount}, reserved={reservedThreads}, usable={usableThreads}, " + $"naturalConcurrent={naturalConcurrentQuantizations}, " + + $"scratchWriterCapacity={scratchWriterCapacity}, " + $"concurrent={_maxConcurrentQuantizations}, " + $"threads/process={_quantThreadsPerProcess}"); } @@ -465,45 +464,35 @@ public async Task ProcessHybridQuantAsync( CancellationToken ct = default) { string modelName = GenerateHybridName(quant); - string quantPath = Path.Combine(_ggufDir, $"{modelName}.gguf"); - string modelBenchDir = Path.Combine(_benchDir, modelName); + string modelBenchDir = _paths.GetBenchmarkDir(modelName); string baseLogitsDir = GetBaseLogitsDirectory(); DateTime startedUtc = DateTime.UtcNow; - var stopwatch = Stopwatch.StartNew(); var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); bool pureExternalBaseline = ShouldDownloadExternalBaselineInsteadOfQuantizing(quant); bool baselineLearnedTruthExists = !forceBaselineRelearn && await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); - string benchmarkModelPath = quantPath; - PreparedExternalBaselineBuild? preparedExternalBaseline = null; - string? transientExternalDownloadPath = null; if (!forceBaselineRelearn && baselineLearnedTruthExists && await _benchmarker.TryReuseExistingBenchmarksAsync( quantConfig: quant, - modelPath: quantPath, + modelPath: string.Empty, benchDir: modelBenchDir, klLogitsDir: baseLogitsDir, domainsOverride: new[] { "general" })) { AnsiConsole.MarkupLine($"[grey]Reused existing benchmark artifacts:[/] {Markup.Escape(modelName)}"); - - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - return SampleProcessState.Skipped; } if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) { AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); - - if (!IsProtectedModel(modelName)) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - return SampleProcessState.Skipped; } + await using var lease = await _scratchStorage.AcquireAsync(ScratchArtifactKind.QuantizedSample, modelName, ct: ct); + string benchmarkModelPath = lease.GgufPath; + try { string inputPath = await GetEffectiveInputModelPathAsync( @@ -511,10 +500,9 @@ public async Task ProcessHybridQuantAsync( forceBaselineRelearn, baselineLearnedTruthExists, ct); - if (pureExternalBaseline && IsPathInsideExternalBaselineCacheRoot(inputPath)) - transientExternalDownloadPath = inputPath; QuantizationExecutionReport? quantizationReport = null; + PreparedExternalBaselineBuild? preparedExternalBaseline = null; await _cpuQuantLock.WaitAsync(ct); try @@ -524,37 +512,28 @@ public async Task ProcessHybridQuantAsync( preparedExternalBaseline = await PrepareExternalBaselineRebuildAsync( quant, downloadedExternalBaselinePath: inputPath, - rebuiltOutputPath: quantPath, + rebuiltOutputPath: lease.GgufPath, forceBaselineRelearn: forceBaselineRelearn, ct: ct); - benchmarkModelPath = preparedExternalBaseline.BenchmarkModelPath; } else { - benchmarkModelPath = quantPath; - if (!File.Exists(quantPath) || forceBaselineRelearn) - { - var quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline - ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) - : quant; - - var effectiveInputPath = quant.BaseQuant.IsExternalRepositoryBaseline - ? await EnsureBaseModelFileAsync() - : inputPath; - - if (quant.BaseQuant.IsExternalRepositoryBaseline) - { - AnsiConsole.MarkupLine( - $"[cyan]Building sample:[/] {Markup.Escape(modelName)} [grey](native input, surrogate carrier={Markup.Escape(quantToExecute.BaseQuant.Names[0])})[/]"); - } - else - { - AnsiConsole.MarkupLine($"[cyan]Building sample:[/] {Markup.Escape(modelName)}"); - } - - quantizationReport = await RunLlamaQuantizeAsync(effectiveInputPath, quantPath, quantToExecute); - } + var quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline + ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) + : quant; + + var effectiveInputPath = quant.BaseQuant.IsExternalRepositoryBaseline + ? await EnsureBaseModelFileAsync() + : inputPath; + + quantizationReport = await RunLlamaQuantizeAsync( + effectiveInputPath, + lease.GgufPath, + quantToExecute, + logPath: lease.PrimaryLogPath, + metadataWorkingDirectory: lease.LeaseDirectory, + ct: ct); } } finally @@ -562,14 +541,6 @@ public async Task ProcessHybridQuantAsync( _cpuQuantLock.Release(); } - if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) - { - if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - - return SampleProcessState.Skipped; - } - AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); await _benchmarker.RunAllBenchmarksAsync( @@ -580,8 +551,6 @@ await _benchmarker.RunAllBenchmarksAsync( saveLogits: false, domainsOverride: new[] { "general" }); - stopwatch.Stop(); - await PersistQuantizationRunAsync( quant: quant, imatrixDefinitionId: null, @@ -604,7 +573,6 @@ await PersistQuantizationRunAsync( } catch (Exception ex) { - stopwatch.Stop(); try { await PersistQuantizationRunAsync( @@ -623,14 +591,6 @@ await PersistQuantizationRunAsync( throw; } - finally - { - if (pureExternalBaseline && !string.IsNullOrWhiteSpace(transientExternalDownloadPath)) - await CleanupExternalBaselineDownloadArtifactsAsync(transientExternalDownloadPath); - - if (!IsProtectedModel(modelName) && benchmarkModelPath == quantPath) - await HardDeleteHelper.DeleteFileIfExistsAsync(quantPath); - } } private bool ShouldDownloadExternalBaselineInsteadOfQuantizing(HybridQuant quant) @@ -680,25 +640,16 @@ private async Task GetEffectiveInputModelPathAsync( private string GetExternalBaselineCachePath(BaselineQuants baseline) { - string root = Cache.ExternalBaselineCacheDirectory ?? - Path.Combine(Cache.ModelMagicQuantDirectory!, "ExternalBaselines"); + string root = _paths.ExternalBaselinesDir; Directory.CreateDirectory(root); - string safe = - string.Concat(baseline.CanonicalKey.Select(ch => Path.GetInvalidFileNameChars().Contains(ch) ? '_' : ch)); - string extension = Path.GetExtension(baseline.SourceFileName ?? string.Empty); - if (string.IsNullOrWhiteSpace(extension)) - extension = ".gguf"; - - string stagingDirectory = Path.Combine(root, $"{safe}-{Guid.NewGuid():N}"); - Directory.CreateDirectory(stagingDirectory); - return Path.Combine(stagingDirectory, safe + extension); + return _paths.GetExternalBaselineDurablePath(baseline); } private async Task ValidateExternalBaselineTensorParityOrThrow(string baseModelPath, string externalBaselinePath) { - var baseMeta = await ReadTensorMetadataFromGgufAsync(baseModelPath, externalBaselinePath + ".nativecheck"); + var baseMeta = await ReadTensorMetadataFromGgufAsync(baseModelPath, Path.GetDirectoryName(externalBaselinePath)!); var externalMeta = - await ReadTensorMetadataFromGgufAsync(externalBaselinePath, externalBaselinePath + ".externalcheck"); + await ReadTensorMetadataFromGgufAsync(externalBaselinePath, Path.GetDirectoryName(externalBaselinePath)!); var baseNames = baseMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); var externalNames = externalMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); @@ -768,7 +719,7 @@ private async Task PrepareExternalBaselineRebuild { AnsiConsole.MarkupLine( $"[cyan]Rebuilding normalized custom baseline from learned truth:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); - await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, blanket); + await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, blanket, metadataWorkingDirectory: Path.GetDirectoryName(rebuiltOutputPath), ct: ct); } return new PreparedExternalBaselineBuild @@ -784,7 +735,7 @@ private async Task PrepareExternalBaselineRebuild await ValidateExternalBaselineTensorParityOrThrow(nativeBasePath, downloadedExternalBaselinePath); var ggufMetadata = - await ReadTensorMetadataFromGgufAsync(downloadedExternalBaselinePath, rebuiltOutputPath + ".learn"); + await ReadTensorMetadataFromGgufAsync(downloadedExternalBaselinePath, Path.GetDirectoryName(rebuiltOutputPath)!); var ggufTruth = ggufMetadata.TensorTypes .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); @@ -838,7 +789,7 @@ private async Task PrepareExternalBaselineRebuild AnsiConsole.MarkupLine( $"[cyan]Rebuilding normalized benchmark artifact for custom baseline:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); - await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, normalizedOverrides); + await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, normalizedOverrides, metadataWorkingDirectory: Path.GetDirectoryName(rebuiltOutputPath), ct: ct); return new PreparedExternalBaselineBuild { @@ -1032,11 +983,7 @@ private static bool IsPathInside(string childPath, string parentPath) // Benchmark/logit helpers // ---------------------------------------------------------------- - private string GetBaseLogitsDirectory() - { - string typeStr = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); - return Path.Combine(_benchDir, typeStr, "logits"); - } + private string GetBaseLogitsDirectory() => _paths.GetBaseLogitsDirectory(); private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationToken ct) { @@ -1215,7 +1162,7 @@ public async Task EnsureBaseModelAsync(bool deleteProcess = false) string outputPath = await EnsureBaseModelFileAsync(deleteProcess); string typeStr = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); - string benchPath = Path.Combine(_benchDir, typeStr); + string benchPath = Path.Combine(_paths.BenchDir, typeStr); string logitsDir = Path.Combine(benchPath, "logits"); AnsiConsole.MarkupLine($"[bold yellow]Benchmarking Base {Markup.Escape(typeStr)} (Saving Logits)...[/]"); @@ -1248,21 +1195,21 @@ public async Task EnsureBaseModelFileAsync(bool deleteProcess = false) string typeStr = torchType.ToString(); string fileName = $"{modelName}-{typeStr}.gguf"; - string outputPath = Path.Combine(_ggufDir, fileName); - string successFile = Path.Combine(_ggufDir, $"{fileName}.success.json"); + string outputPath = Path.Combine(_paths.GgufDir, fileName); + string successFile = Path.Combine(_paths.GgufDir, $"{fileName}.success.json"); string convertLogPath = outputPath + ".convert.log"; if (deleteProcess) { - if (!Directory.Exists(_ggufDir)) - Directory.CreateDirectory(_ggufDir); + if (!Directory.Exists(_paths.GgufDir)) + Directory.CreateDirectory(_paths.GgufDir); var normalizedFileName = Path.GetFileName(fileName); var successFileName = normalizedFileName + ".success.json"; - var successFilePath = Path.Combine(_ggufDir, successFileName); + var successFilePath = Path.Combine(_paths.GgufDir, successFileName); bool isImmune = File.Exists(successFilePath); - foreach (var filePath in Directory.EnumerateFiles(_ggufDir, "*.gguf", SearchOption.TopDirectoryOnly)) + foreach (var filePath in Directory.EnumerateFiles(_paths.GgufDir, "*.gguf", SearchOption.TopDirectoryOnly)) { var currentFileName = Path.GetFileName(filePath); var currentModelName = Path.GetFileNameWithoutExtension(currentFileName); @@ -1362,8 +1309,6 @@ public async Task BuildExportArtifactFromExactTensorMapAsync( if (forceRebuild && File.Exists(outputPath)) await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - string? transientExternalDownloadPath = null; - await _cpuQuantLock.WaitAsync(ct); try { @@ -1404,8 +1349,6 @@ public async Task BuildExportArtifactAsync( if (forceRebuild && File.Exists(outputPath)) await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - string? transientExternalDownloadPath = null; - await _cpuQuantLock.WaitAsync(ct); try { @@ -1418,10 +1361,10 @@ public async Task BuildExportArtifactAsync( if (quant.BaseQuant.IsExternalRepositoryBaseline) { - transientExternalDownloadPath = GetExternalBaselineCachePath(quant.BaseQuant); + string durableExternalPath = GetExternalBaselineCachePath(quant.BaseQuant); await _huggingFaceBaselineService.DownloadBaselineAsync( quant.BaseQuant, - transientExternalDownloadPath, + durableExternalPath, forceRedownload: false, ct: ct); @@ -1442,7 +1385,8 @@ await RunLlamaQuantizeAsync( inputFile: nativeBasePath, outputFile: outputPath, quant: quantToExecute, - temporaryCarrierOverrides: temporaryCarrierOverrides); + temporaryCarrierOverrides: temporaryCarrierOverrides, + ct: ct); await File.WriteAllTextAsync(outputPath + ".success.json", "{\"status\":\"success\"}", ct); return outputPath; @@ -1450,14 +1394,11 @@ await RunLlamaQuantizeAsync( finally { _cpuQuantLock.Release(); - - if (!string.IsNullOrWhiteSpace(transientExternalDownloadPath)) - await CleanupExternalBaselineDownloadArtifactsAsync(transientExternalDownloadPath); } } - public async Task EnsurePureQ8ModelAsync() + public async Task BuildPureQ8ProbeLeaseAsync(CancellationToken ct = default) { string basePath = await EnsureBaseModelFileAsync(); @@ -1468,65 +1409,33 @@ public async Task EnsurePureQ8ModelAsync() }; string modelName = GenerateHybridName(pureQ8); - string q8Path = Path.Combine(_ggufDir, $"{modelName}.gguf"); - string successFile = Path.Combine(_ggufDir, $"{Path.GetFileName(q8Path)}.success.json"); + var lease = await _scratchStorage.AcquireAsync(ScratchArtifactKind.PureQ8Probe, modelName, ct: ct); - if (!File.Exists(q8Path) || !File.Exists(successFile)) + await _cpuQuantLock.WaitAsync(ct); + try { - await _cpuQuantLock.WaitAsync(); - try - { - if (!File.Exists(q8Path)) - { - AnsiConsole.MarkupLine($"[cyan]Building pure Q8 baseline:[/] {Markup.Escape(modelName)}"); - await RunLlamaQuantizeAsync(basePath, q8Path, pureQ8); - AnsiConsole.MarkupLine( - $"[green]Pure Q8 baseline quantization finished:[/] {Markup.Escape(q8Path)}"); - } - } - finally - { - _cpuQuantLock.Release(); - } - - await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); + AnsiConsole.MarkupLine($"[cyan]Building pure Q8 probe baseline:[/] {Markup.Escape(modelName)}"); + await RunLlamaQuantizeAsync( + basePath, + lease.GgufPath, + pureQ8, + logPath: lease.PrimaryLogPath, + metadataWorkingDirectory: lease.LeaseDirectory, + ct: ct); } - else + finally { - AnsiConsole.MarkupLine($"[grey]Pure Q8 baseline already exists:[/] {Markup.Escape(q8Path)}"); + _cpuQuantLock.Release(); } - return q8Path; - } - - public async Task CleanupPureQ8ModelAsync() - { - var pureQ8 = new HybridQuant - { - BaseQuant = BaselineQuants.Q8_0, - Tensors = new List() - }; - - string modelName = GenerateHybridName(pureQ8); - string q8Path = Path.Combine(_ggufDir, $"{modelName}.gguf"); - string successFile = Path.Combine(_ggufDir, $"{Path.GetFileName(q8Path)}.success.json"); - string quantLog = q8Path + ".quantize.log"; - - bool hadQ8 = File.Exists(q8Path) || File.Exists(successFile) || File.Exists(quantLog); - - await HardDeleteHelper.DeleteFileIfExistsAsync(q8Path); - await HardDeleteHelper.DeleteFileIfExistsAsync(successFile); - await HardDeleteHelper.DeleteFileIfExistsAsync(quantLog); - - if (hadQ8) - AnsiConsole.MarkupLine($"[grey]Removed probe-only Q8 artifacts:[/] {Markup.Escape(modelName)}"); - else - AnsiConsole.MarkupLine("[grey]No probe-only Q8 artifacts to clean up.[/]"); + return lease; } // ---------------------------------------------------------------- // Quantization // ---------------------------------------------------------------- + // Quantization + // ---------------------------------------------------------------- private async Task RunLlamaQuantizeWithExactTensorMapAsync( @@ -1534,14 +1443,16 @@ private async Task RunLlamaQuantizeWithExactTensorM string outputFile, IReadOnlyDictionary tensorTypes, BaselineQuants baseQuant, - CancellationToken ct) + string? logPath = null, + string? metadataWorkingDirectory = null, + CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); - var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, outputFile); + var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, metadataWorkingDirectory ?? Path.GetDirectoryName(outputFile)!); var requestedOverrides = tensorTypes .OrderBy(x => x.Key, StringComparer.Ordinal) .Select(x => new RequestedTensorOverride @@ -1599,7 +1510,8 @@ private async Task RunLlamaQuantizeWithExactTensorM Cache.LlamaBin!, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "llama-quantize.exe" : "llama-quantize"); - string quantizeLogPath = outputFile + ".quantize.log"; + string quantizeLogPath = string.IsNullOrWhiteSpace(logPath) ? outputFile + ".quantize.log" : logPath; + Directory.CreateDirectory(Path.GetDirectoryName(quantizeLogPath)!); AnsiConsole.MarkupLine( $"[cyan]Quantizing clone artifact:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); @@ -1632,29 +1544,32 @@ private async Task RunLlamaQuantizeWithExactTensorM }; } - private async Task RunLlamaQuantizeAsync(string inputFile, string outputFile, - HybridQuant quant, IReadOnlyDictionary? temporaryCarrierOverrides = null) + private async Task RunLlamaQuantizeAsync( + string inputFile, + string outputFile, + HybridQuant quant, + IReadOnlyDictionary? temporaryCarrierOverrides = null, + string? logPath = null, + string? metadataWorkingDirectory = null, + CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); - if (!string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory)) + if (temporaryCarrierOverrides != null || quant.BaseQuant.IsExternalRepositoryBaseline) { - string fullInput = Path.GetFullPath(inputFile); - string fullExternalRoot = Path.GetFullPath(Cache.ExternalBaselineCacheDirectory); - - if (fullInput.StartsWith(fullExternalRoot, StringComparison.OrdinalIgnoreCase) && - (temporaryCarrierOverrides != null || quant.BaseQuant.IsExternalRepositoryBaseline)) + string nativeBase = await EnsureBaseModelFileAsync(); + if (!string.Equals(Path.GetFullPath(inputFile), Path.GetFullPath(nativeBase), StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException( - $"Quantization attempted to use staged external GGUF '{inputFile}' as the carrier input. " + - "External/custom baselines must rebuild from the native base GGUF instead."); + $"Quantization attempted to use non-native carrier input '{inputFile}' for external/override execution. " + + "External/custom baseline rebuilds must use the native base GGUF as input."); } } Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); - var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, outputFile); + var inputTensorMetadata = await ReadTensorMetadataFromGgufAsync(inputFile, metadataWorkingDirectory ?? Path.GetDirectoryName(outputFile)!); var requestedOverrides = BuildRequestedTensorOverrides(quant, inputTensorMetadata.TensorNames, temporaryCarrierOverrides); var concreteOverrides = ResolveConcreteTensorOverrides( @@ -1716,7 +1631,8 @@ private async Task RunLlamaQuantizeAsync(string inp Cache.LlamaBin!, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "llama-quantize.exe" : "llama-quantize"); - string quantizeLogPath = outputFile + ".quantize.log"; + string quantizeLogPath = string.IsNullOrWhiteSpace(logPath) ? outputFile + ".quantize.log" : logPath; + Directory.CreateDirectory(Path.GetDirectoryName(quantizeLogPath)!); var psi = new ProcessStartInfo { @@ -1726,7 +1642,7 @@ private async Task RunLlamaQuantizeAsync(string inp AnsiConsole.MarkupLine( $"[cyan]Quantizing:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); - var result = await RunLoggedProcessAsync(psi, quantizeLogPath); + var result = await RunLoggedProcessAsync(psi, quantizeLogPath, ct); if (result.ExitCode != 0) { @@ -1793,7 +1709,7 @@ public async Task> ReadExactTensorTypesAsync string ggufPath, CancellationToken ct = default) { - var meta = await ReadTensorMetadataFromGgufAsync(ggufPath, ggufPath); + var meta = await ReadTensorMetadataFromGgufAsync(ggufPath, Path.GetDirectoryName(ggufPath)!); return meta.TensorTypes .OrderBy(x => x.Key, StringComparer.Ordinal) .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); @@ -1815,20 +1731,20 @@ public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = defaul { var pure = HybridQuant.CreatePureBaseline(baseline); var name = GenerateHybridName(pure); - var ggufPath = Path.Combine(_ggufDir, $"{name}.gguf"); - var success = Path.Combine(_ggufDir, $"{name}.gguf.success.json"); + var ggufPath = Path.Combine(_paths.GgufDir, $"{name}.gguf"); + var success = Path.Combine(_paths.GgufDir, $"{name}.gguf.success.json"); var log = ggufPath + ".quantize.log"; await HardDeleteHelper.DeleteFileIfExistsAsync(ggufPath); await HardDeleteHelper.DeleteFileIfExistsAsync(success); await HardDeleteHelper.DeleteFileIfExistsAsync(log); - string benchDir = Path.Combine(_benchDir, name); + string benchDir = Path.Combine(_paths.BenchDir, name); if (Directory.Exists(benchDir)) Directory.Delete(benchDir, recursive: true); } - string debugDir = Path.Combine(_benchDir, "_learning_debug"); + string debugDir = Path.Combine(_paths.BenchDir, "_learning_debug"); if (Directory.Exists(debugDir)) Directory.Delete(debugDir, recursive: true); @@ -1838,12 +1754,12 @@ public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = defaul string nativeType = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; - string nativeBaseFile = Path.Combine(_ggufDir, $"{modelName}-{nativeType}.gguf"); + string nativeBaseFile = Path.Combine(_paths.GgufDir, $"{modelName}-{nativeType}.gguf"); await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile); await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile + ".success.json"); await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile + ".convert.log"); - string nativeBenchDir = Path.Combine(_benchDir, nativeType); + string nativeBenchDir = Path.Combine(_paths.BenchDir, nativeType); if (Directory.Exists(nativeBenchDir)) Directory.Delete(nativeBenchDir, recursive: true); @@ -1904,7 +1820,7 @@ public async Task LearnNativeSourceTruthAsync( } } - var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, nativeGgufPath); + var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, Path.GetDirectoryName(nativeGgufPath)!); var ggufTruth = metadata.TensorTypes .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); @@ -2049,7 +1965,7 @@ private async Task LearnAndPersistBaselineTensorMapAsync( var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; string logPath = report?.LogPath ?? (quantizedModelPath + ".quantize.log"); var parsed = ParseQuantizeLogForTensorTypes(logPath); - var ggufMetadata = await ReadTensorMetadataFromGgufAsync(quantizedModelPath, quantizedModelPath); + var ggufMetadata = await ReadTensorMetadataFromGgufAsync(quantizedModelPath, Path.GetDirectoryName(quantizedModelPath)!); var ggufTruth = ggufMetadata.TensorTypes .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); @@ -2393,7 +2309,7 @@ private async Task WriteLearningDiagnosticArtifactAsync( Groups = summaries }; - string debugDir = Path.Combine(_benchDir, "_learning_debug"); + string debugDir = Path.Combine(_paths.BenchDir, "_learning_debug"); Directory.CreateDirectory(debugDir); string path = Path.Combine(debugDir, $"{baselineName}_{schemeName}_learned_map.json"); await File.WriteAllTextAsync(path, @@ -2763,9 +2679,9 @@ private List ResolveConcreteTensorOverrides( .ToList(); } - private async Task ReadTensorMetadataFromGgufAsync(string ggufPath, string outputFilePath) + private async Task ReadTensorMetadataFromGgufAsync(string ggufPath, string workingDirectory) { - string workingDir = Path.GetDirectoryName(outputFilePath)!; + string workingDir = workingDirectory; string unique = Guid.NewGuid().ToString("N"); string payloadPath = Path.Combine(workingDir, $"read_gguf_tensors_{unique}.json"); string resultPath = Path.Combine(workingDir, $"read_gguf_tensors_result_{unique}.json"); diff --git a/MagicQuant/Services/ScratchStorageService.cs b/MagicQuant/Services/ScratchStorageService.cs new file mode 100644 index 0000000..106e3a9 --- /dev/null +++ b/MagicQuant/Services/ScratchStorageService.cs @@ -0,0 +1,196 @@ +using System.Text.Json; +using MagicQuant.Helpers; +using MQ.DB; + +namespace MagicQuant.Services; + +public enum ScratchArtifactKind +{ + QuantizedSample, + PureQ8Probe, + ExternalBaselineRebuild, + ExternalBaselineNormalizedSample, + ExportTemp, + MetadataRead, + Other +} + +public sealed class ScratchArtifactLease : IAsyncDisposable +{ + private readonly Func _dispose; + private int _disposed; + + internal ScratchArtifactLease( + Guid leaseId, + ScratchArtifactKind kind, + string scratchRoot, + string leaseDirectory, + string ggufPath, + string primaryLogPath, + Func dispose) + { + LeaseId = leaseId; + Kind = kind; + ScratchRoot = scratchRoot; + LeaseDirectory = leaseDirectory; + GgufPath = ggufPath; + PrimaryLogPath = primaryLogPath; + OwnsGgufLifecycle = true; + _dispose = dispose; + } + + public Guid LeaseId { get; } + public ScratchArtifactKind Kind { get; } + public string ScratchRoot { get; } + public string LeaseDirectory { get; } + public string GgufPath { get; } + public string PrimaryLogPath { get; } + public bool OwnsGgufLifecycle { get; private set; } + + public void PreserveOutput() => OwnsGgufLifecycle = false; + + public async ValueTask DisposeAsync() + { + if (Interlocked.Exchange(ref _disposed, 1) != 0) + return; + + await _dispose(this); + } +} + +public sealed class ScratchStorageService +{ + private const string ScratchFolderName = ".MagicQuant_tmp"; + private readonly ModelArtifactPathService? _paths; + private readonly string _modelNamespace; + private readonly string _quantLogDir; + private readonly IReadOnlyList _roots; + private readonly SemaphoreSlim[] _rootLocks; + private readonly SemaphoreSlim _availableRoots; + private int _cursor = -1; + + public ScratchStorageService(ModelArtifactPathService? paths = null) + { + _paths = paths; + _modelNamespace = paths?.ScratchModelNamespace ?? "global"; + _quantLogDir = paths?.QuantizationLogsDir ?? Path.Combine(Cache.MagicQuantDirectory ?? Path.GetTempPath(), "Logs", "Quantization"); + var configured = Cache.ScratchRoots ?? []; + + var fallbackRoot = paths != null + ? Path.Combine(paths.ModelMagicQuantDirectory, ScratchFolderName) + : Path.Combine(Cache.MagicQuantDirectory ?? Path.GetTempPath(), ScratchFolderName); + + _roots = configured.Count == 0 + ? [fallbackRoot] + : configured.Select(x => Path.GetFullPath(x)).ToList(); + + _rootLocks = _roots.Select(_ => new SemaphoreSlim(1, 1)).ToArray(); + _availableRoots = new SemaphoreSlim(_roots.Count, _roots.Count); + } + + public int WriterCapacity => _roots.Count; + public IReadOnlyList ConfiguredScratchRoots => _roots; + + public async Task AcquireAsync( + ScratchArtifactKind kind, + string artifactBaseName, + string extension = ".gguf", + CancellationToken ct = default) + { + await _availableRoots.WaitAsync(ct); + + int rootIndex = -1; + try + { + while (rootIndex < 0) + { + int start = (Interlocked.Increment(ref _cursor) % _roots.Count + _roots.Count) % _roots.Count; + for (int i = 0; i < _roots.Count; i++) + { + int idx = (start + i) % _roots.Count; + if (_rootLocks[idx].Wait(0)) + { + rootIndex = idx; + break; + } + } + + if (rootIndex < 0) + await Task.Delay(20, ct); + } + + var leaseId = Guid.NewGuid(); + string root = _roots[rootIndex]; + string tmpRoot = root.EndsWith(ScratchFolderName, StringComparison.OrdinalIgnoreCase) + ? root + : Path.Combine(root, ScratchFolderName); + + string leaseDir = Path.Combine(tmpRoot, _modelNamespace, leaseId.ToString("N")); + Directory.CreateDirectory(leaseDir); + Directory.CreateDirectory(_quantLogDir); + + string safeBase = ModelArtifactPathService.MakeSafeFileComponent(artifactBaseName); + string ggufPath = Path.Combine(leaseDir, safeBase + extension); + string logPath = _paths?.GetQuantizationLogPath(safeBase, leaseId) ?? Path.Combine(_quantLogDir, $"{safeBase}-{leaseId:N}.quantize.log"); + + var marker = new + { + lease_id = leaseId, + process_id = Environment.ProcessId, + started_utc = DateTime.UtcNow, + kind = kind.ToString(), + artifact_name = artifactBaseName, + gguf_path = ggufPath + }; + await File.WriteAllTextAsync(Path.Combine(leaseDir, "lease.json"), JsonSerializer.Serialize(marker), ct); + + return new ScratchArtifactLease( + leaseId, + kind, + root, + leaseDir, + ggufPath, + logPath, + async lease => + { + try + { + if (lease.OwnsGgufLifecycle && Directory.Exists(lease.LeaseDirectory)) + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(lease.LeaseDirectory); + } + finally + { + _rootLocks[rootIndex].Release(); + _availableRoots.Release(); + } + }); + } + catch + { + if (rootIndex >= 0) + _rootLocks[rootIndex].Release(); + _availableRoots.Release(); + throw; + } + } + + public async Task CleanupStaleScratchArtifactsAsync(CancellationToken ct = default) + { + foreach (var root in _roots) + { + ct.ThrowIfCancellationRequested(); + string tmpRoot = root.EndsWith(ScratchFolderName, StringComparison.OrdinalIgnoreCase) + ? root + : Path.Combine(root, ScratchFolderName); + + if (!Directory.Exists(tmpRoot)) + continue; + + foreach (var child in Directory.EnumerateDirectories(tmpRoot)) + { + ct.ThrowIfCancellationRequested(); + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(child); + } + } + } +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 4aa916e..ad134fc 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -32,7 +32,13 @@ paths: llama_bin: convert_script: - # Folder name created under the MagicQuant root for external/custom GGUF downloads. + # Scratch roots for temporary heavy GGUF writes. + # MagicQuant creates .MagicQuant_tmp under each root and enforces one heavy writer per root. + # Roots are not validated as separate physical disks; choose paths intentionally. + # When blank, MagicQuant falls back to single-root model-local scratch behavior. + scratch_roots: [] + + # Folder name created under the current model's MagicQuant directory for durable external/custom GGUF downloads. external_baseline_cache_dir_name: ExternalBaselines flags: diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 7cdc2ee..db6ce06 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -4,6 +4,10 @@ paths: llama_root: llama_bin: convert_script: + scratch_roots: + - /mnt/world8/ + - /tmp/ + - /mnt/world7/ external_baseline_cache_dir_name: ExternalBaselines flags: From f8d1ddf4137862357a758aaf525fef75bb5d6a48 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 28 Apr 2026 11:46:56 -0400 Subject: [PATCH 156/258] Fix external baseline hybrid layering and scratch cleanup safety --- MagicQuant/Commands/CloneRepositoryQuants.cs | 1 + MagicQuant/Commands/Evolution.cs | 1 + MagicQuant/Commands/ValidatePredictions.cs | 1 + MagicQuant/Services/QuantizationService.cs | 77 +++++++++++++++----- MagicQuant/Services/ScratchStorageService.cs | 40 +++++++++- 5 files changed, 102 insertions(+), 18 deletions(-) diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 2a94d54..17a5451 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -58,6 +58,7 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Cache.ForceRelearnBaselineTensorMappings = false; Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index b8adc17..28cdb56 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -62,6 +62,7 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Cache.ForceRelearnBaselineTensorMappings = Config.Current.Flags.ForceRelearnBaselineTensorMappings; Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; diff --git a/MagicQuant/Commands/ValidatePredictions.cs b/MagicQuant/Commands/ValidatePredictions.cs index a331995..cd5f35c 100644 --- a/MagicQuant/Commands/ValidatePredictions.cs +++ b/MagicQuant/Commands/ValidatePredictions.cs @@ -35,6 +35,7 @@ public async Task Run(List args) Cache.ModelDirectory = modelDir; Cache.ModelMagicQuantDirectory = Path.Combine(modelDir, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(modelDir); diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 449cba3..ad5fd3e 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -490,17 +490,23 @@ public async Task ProcessHybridQuantAsync( return SampleProcessState.Skipped; } - await using var lease = await _scratchStorage.AcquireAsync(ScratchArtifactKind.QuantizedSample, modelName, ct: ct); + string inputPath = await GetEffectiveInputModelPathAsync( + quant, + forceBaselineRelearn, + baselineLearnedTruthExists, + ct); + + ScratchArtifactKind leaseKind = pureExternalBaseline + ? ScratchArtifactKind.ExternalBaselineRebuild + : quant.BaseQuant.IsExternalRepositoryBaseline + ? ScratchArtifactKind.ExternalBaselineNormalizedSample + : ScratchArtifactKind.QuantizedSample; + + await using var lease = await _scratchStorage.AcquireAsync(leaseKind, modelName, ct: ct); string benchmarkModelPath = lease.GgufPath; try { - string inputPath = await GetEffectiveInputModelPathAsync( - quant, - forceBaselineRelearn, - baselineLearnedTruthExists, - ct); - QuantizationExecutionReport? quantizationReport = null; PreparedExternalBaselineBuild? preparedExternalBaseline = null; @@ -513,6 +519,8 @@ public async Task ProcessHybridQuantAsync( quant, downloadedExternalBaselinePath: inputPath, rebuiltOutputPath: lease.GgufPath, + logPath: lease.PrimaryLogPath, + metadataWorkingDirectory: lease.LeaseDirectory, forceBaselineRelearn: forceBaselineRelearn, ct: ct); benchmarkModelPath = preparedExternalBaseline.BenchmarkModelPath; @@ -523,6 +531,24 @@ public async Task ProcessHybridQuantAsync( ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) : quant; + IReadOnlyDictionary? temporaryCarrierOverrides = null; + + if (quant.BaseQuant.IsExternalRepositoryBaseline) + { + temporaryCarrierOverrides = TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: true); + + if (temporaryCarrierOverrides.Count == 0) + { + throw new InvalidOperationException( + $"Missing blanket learned mapping for external/custom baseline '{quant.BaseQuant.Names[0]}'. " + + "External baseline hybrids require learned tensor mappings before sampling. " + + "Run with --relearn-baseline-mappings."); + } + } + var effectiveInputPath = quant.BaseQuant.IsExternalRepositoryBaseline ? await EnsureBaseModelFileAsync() : inputPath; @@ -531,6 +557,7 @@ public async Task ProcessHybridQuantAsync( effectiveInputPath, lease.GgufPath, quantToExecute, + temporaryCarrierOverrides: temporaryCarrierOverrides, logPath: lease.PrimaryLogPath, metadataWorkingDirectory: lease.LeaseDirectory, ct: ct); @@ -551,6 +578,14 @@ await _benchmarker.RunAllBenchmarksAsync( saveLogits: false, domainsOverride: new[] { "general" }); + if (IsLearnableBaselineRun(quant)) + { + if (preparedExternalBaseline?.HasPreparedLearningTruth == true) + await PersistLearnedBaselineTensorMapFromPreparedAsync(quant, preparedExternalBaseline, ct); + else if (!baselineLearnedTruthExists || forceBaselineRelearn) + await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); + } + await PersistQuantizationRunAsync( quant: quant, imatrixDefinitionId: null, @@ -561,14 +596,6 @@ await PersistQuantizationRunAsync( error: null, ct: ct); - if (IsLearnableBaselineRun(quant)) - { - if (preparedExternalBaseline?.HasPreparedLearningTruth == true) - await PersistLearnedBaselineTensorMapFromPreparedAsync(quant, preparedExternalBaseline, ct); - else if (!baselineLearnedTruthExists || forceBaselineRelearn) - await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); - } - return SampleProcessState.Completed; } catch (Exception ex) @@ -694,6 +721,8 @@ private async Task PrepareExternalBaselineRebuild HybridQuant quant, string downloadedExternalBaselinePath, string rebuiltOutputPath, + string logPath, + string metadataWorkingDirectory, bool forceBaselineRelearn, CancellationToken ct) { @@ -719,7 +748,14 @@ private async Task PrepareExternalBaselineRebuild { AnsiConsole.MarkupLine( $"[cyan]Rebuilding normalized custom baseline from learned truth:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); - await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, blanket, metadataWorkingDirectory: Path.GetDirectoryName(rebuiltOutputPath), ct: ct); + await RunLlamaQuantizeAsync( + nativeBasePath, + rebuiltOutputPath, + quant, + blanket, + logPath: logPath, + metadataWorkingDirectory: metadataWorkingDirectory, + ct: ct); } return new PreparedExternalBaselineBuild @@ -789,7 +825,14 @@ private async Task PrepareExternalBaselineRebuild AnsiConsole.MarkupLine( $"[cyan]Rebuilding normalized benchmark artifact for custom baseline:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); - await RunLlamaQuantizeAsync(nativeBasePath, rebuiltOutputPath, quant, normalizedOverrides, metadataWorkingDirectory: Path.GetDirectoryName(rebuiltOutputPath), ct: ct); + await RunLlamaQuantizeAsync( + nativeBasePath, + rebuiltOutputPath, + quant, + normalizedOverrides, + logPath: logPath, + metadataWorkingDirectory: metadataWorkingDirectory, + ct: ct); return new PreparedExternalBaselineBuild { diff --git a/MagicQuant/Services/ScratchStorageService.cs b/MagicQuant/Services/ScratchStorageService.cs index 106e3a9..9622f52 100644 --- a/MagicQuant/Services/ScratchStorageService.cs +++ b/MagicQuant/Services/ScratchStorageService.cs @@ -189,8 +189,46 @@ public async Task CleanupStaleScratchArtifactsAsync(CancellationToken ct = defau foreach (var child in Directory.EnumerateDirectories(tmpRoot)) { ct.ThrowIfCancellationRequested(); - await HardDeleteHelper.DeleteDirectoryIfExistsAsync(child); + // Legacy single-level lease folder support. + if (IsLeaseDirectory(child)) + { + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(child); + continue; + } + + // Model namespace folder: only remove known lease children. + bool containsOnlyLeaseDirs = !Directory.EnumerateFiles(child).Any(); + foreach (var leaseDir in Directory.EnumerateDirectories(child)) + { + ct.ThrowIfCancellationRequested(); + if (!IsLeaseDirectory(leaseDir)) + { + containsOnlyLeaseDirs = false; + continue; + } + + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(leaseDir); + } + + if (containsOnlyLeaseDirs && + !Directory.EnumerateDirectories(child).Any() && + !Directory.EnumerateFiles(child).Any()) + { + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(child); + } } } } + + private static bool IsLeaseDirectory(string directoryPath) + { + if (!Directory.Exists(directoryPath)) + return false; + + string name = Path.GetFileName(directoryPath); + if (!Guid.TryParseExact(name, "N", out _)) + return false; + + return File.Exists(Path.Combine(directoryPath, "lease.json")); + } } From 88e5c88fdfa46504df588f774aaec3c96c958c11 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 28 Apr 2026 11:55:58 -0400 Subject: [PATCH 157/258] Harden Q8 probe lease failure cleanup and benchmark reuse size gating --- MagicQuant/Services/BenchmarkService.cs | 7 ++ MagicQuant/Services/QuantizationService.cs | 87 ++++++++++++---------- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 01395db..950bb58 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -1093,6 +1093,13 @@ public async Task TryReuseExistingBenchmarksAsync( reused.ModelSizeBytes = actualSize; } + if (!reused.ModelSizeBytes.HasValue || reused.ModelSizeBytes.Value == 0) + { + // Reuse cannot safely persist DB truth with unknown size. + // This is expected for transient scratch samples where modelPath may be intentionally empty. + return false; + } + using var db = new MagicQuantContext(); var identity = await GetOrCreateBenchmarkIdentityAsync(db, quantConfig); diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index ad5fd3e..514a73f 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -1392,37 +1392,38 @@ public async Task BuildExportArtifactAsync( if (forceRebuild && File.Exists(outputPath)) await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - await _cpuQuantLock.WaitAsync(ct); - try - { - string nativeBasePath = await EnsureBaseModelFileAsync(); - HybridQuant quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline - ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) - : quant; + HybridQuant quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline + ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) + : quant; - IReadOnlyDictionary? temporaryCarrierOverrides = null; + IReadOnlyDictionary? temporaryCarrierOverrides = null; - if (quant.BaseQuant.IsExternalRepositoryBaseline) - { - string durableExternalPath = GetExternalBaselineCachePath(quant.BaseQuant); - await _huggingFaceBaselineService.DownloadBaselineAsync( - quant.BaseQuant, - durableExternalPath, - forceRedownload: false, - ct: ct); + if (quant.BaseQuant.IsExternalRepositoryBaseline) + { + string durableExternalPath = GetExternalBaselineCachePath(quant.BaseQuant); + await _huggingFaceBaselineService.DownloadBaselineAsync( + quant.BaseQuant, + durableExternalPath, + forceRedownload: false, + ct: ct); - temporaryCarrierOverrides = TryLoadAllLearnedTensorMappings( - canonicalBaselineKey: quant.BaseQuant.CanonicalKey, - preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, - allowDominantFallback: true); + temporaryCarrierOverrides = TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: true); - if (temporaryCarrierOverrides.Count == 0) - { - throw new InvalidOperationException( - $"Missing blanket learned mapping for external/custom baseline '{quant.BaseQuant.Names[0]}'. " + - "MagicQuant cannot export a hybrid from an external baseline until that baseline has been learned."); - } + if (temporaryCarrierOverrides.Count == 0) + { + throw new InvalidOperationException( + $"Missing blanket learned mapping for external/custom baseline '{quant.BaseQuant.Names[0]}'. " + + "MagicQuant cannot export a hybrid from an external baseline until that baseline has been learned."); } + } + + await _cpuQuantLock.WaitAsync(ct); + try + { + string nativeBasePath = await EnsureBaseModelFileAsync(); await RunLlamaQuantizeAsync( inputFile: nativeBasePath, @@ -1454,24 +1455,32 @@ public async Task BuildPureQ8ProbeLeaseAsync(CancellationT string modelName = GenerateHybridName(pureQ8); var lease = await _scratchStorage.AcquireAsync(ScratchArtifactKind.PureQ8Probe, modelName, ct: ct); - await _cpuQuantLock.WaitAsync(ct); try { - AnsiConsole.MarkupLine($"[cyan]Building pure Q8 probe baseline:[/] {Markup.Escape(modelName)}"); - await RunLlamaQuantizeAsync( - basePath, - lease.GgufPath, - pureQ8, - logPath: lease.PrimaryLogPath, - metadataWorkingDirectory: lease.LeaseDirectory, - ct: ct); + await _cpuQuantLock.WaitAsync(ct); + try + { + AnsiConsole.MarkupLine($"[cyan]Building pure Q8 probe baseline:[/] {Markup.Escape(modelName)}"); + await RunLlamaQuantizeAsync( + basePath, + lease.GgufPath, + pureQ8, + logPath: lease.PrimaryLogPath, + metadataWorkingDirectory: lease.LeaseDirectory, + ct: ct); + } + finally + { + _cpuQuantLock.Release(); + } + + return lease; } - finally + catch { - _cpuQuantLock.Release(); + await lease.DisposeAsync(); + throw; } - - return lease; } // ---------------------------------------------------------------- From f2be2d2d2e9ba0b795687a7f5f09b2bdac021a81 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 28 Apr 2026 12:48:47 -0400 Subject: [PATCH 158/258] don't use tmp --- MagicQuant/config.dev.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index db6ce06..6757737 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -6,7 +6,7 @@ paths: convert_script: scratch_roots: - /mnt/world8/ - - /tmp/ + - /home/slurp/ - /mnt/world7/ external_baseline_cache_dir_name: ExternalBaselines From 6d7c44368365acd9ef3b3bc88e0c80896ce58257 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 28 Apr 2026 13:09:30 -0400 Subject: [PATCH 159/258] Fix ETA sampling to ignore cloned/near-zero completions --- .../Services/Progress/StageProgressOptions.cs | 1 + .../Services/Progress/StageProgressTracker.cs | 27 +++++++++++++++++-- MagicQuant/Services/QuantizationService.cs | 19 ++++++++++--- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/MagicQuant/Services/Progress/StageProgressOptions.cs b/MagicQuant/Services/Progress/StageProgressOptions.cs index 009defe..e761ef7 100644 --- a/MagicQuant/Services/Progress/StageProgressOptions.cs +++ b/MagicQuant/Services/Progress/StageProgressOptions.cs @@ -9,6 +9,7 @@ public sealed class StageProgressOptions public int PrintEveryNFinished { get; init; } = 1; public bool ShowEta { get; init; } = true; public bool CountSkippedForEta { get; init; } = false; + public TimeSpan MinimumEtaSampleDuration { get; init; } = TimeSpan.FromSeconds(1); public bool PrintFinalSummary { get; init; } = true; public string? UnitLabel { get; init; } } diff --git a/MagicQuant/Services/Progress/StageProgressTracker.cs b/MagicQuant/Services/Progress/StageProgressTracker.cs index 3337ff5..ebaffa6 100644 --- a/MagicQuant/Services/Progress/StageProgressTracker.cs +++ b/MagicQuant/Services/Progress/StageProgressTracker.cs @@ -10,6 +10,7 @@ public sealed class StageProgressTracker private int _completed; private int _skipped; private int _failed; + private int _etaSamples; private int _lastPrintedFinished; private DateTime _lastPrintedUtc; @@ -52,7 +53,11 @@ public StageProgressSnapshot Snapshot } } - public void ReportFinished(SampleProcessState state, string? itemName = null) + public void ReportFinished( + SampleProcessState state, + string? itemName = null, + TimeSpan? duration = null, + bool? countForEtaOverride = null) { switch (state) { @@ -67,9 +72,27 @@ public void ReportFinished(SampleProcessState state, string? itemName = null) break; } + bool countForEta = countForEtaOverride ?? ShouldCountForEta(state, duration); + if (countForEta) + Interlocked.Increment(ref _etaSamples); + MaybePrint(state, itemName); } + private bool ShouldCountForEta(SampleProcessState state, TimeSpan? duration) + { + if (!duration.HasValue || duration.Value < _options.MinimumEtaSampleDuration) + return false; + + return state switch + { + SampleProcessState.Completed => true, + SampleProcessState.Failed => true, + SampleProcessState.Skipped => _options.CountSkippedForEta, + _ => false + }; + } + private void MaybePrint(SampleProcessState justFinishedState, string? itemName) { var now = DateTime.UtcNow; @@ -110,7 +133,7 @@ private void MaybePrint(SampleProcessState justFinishedState, string? itemName) var elapsed = now - StartedUtc; string elapsedText = FormatDuration(elapsed); - int etaSampleCount = _options.CountSkippedForEta ? finished : completed + failed; + int etaSampleCount = Volatile.Read(ref _etaSamples); string etaText = "ETA warming up..."; string estFinishText = "est finish UTC n/a"; diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 514a73f..0c4c56d 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -343,28 +343,31 @@ private async Task ExecutePlanAsync( Plan = plan, ModelName = GenerateHybridName(plan.Quant) }; + var sw = Stopwatch.StartNew(); try { var state = await ProcessHybridQuantAsync(plan.Quant, ct); + sw.Stop(); record.State = state; var identity = await ResolveBenchmarkIdentityAsync(plan.Quant, ct); record.TensorComboId = identity.TensorComboId; record.BenchmarkId = identity.BenchmarkId; - progress?.ReportFinished(state, record.ModelName); + progress?.ReportFinished(state, record.ModelName, sw.Elapsed); return record; } catch (Exception ex) { + sw.Stop(); record.State = SampleProcessState.Failed; record.Error = ex.Message; AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(record.ModelName)}"); AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); - progress?.ReportFinished(SampleProcessState.Failed, record.ModelName); + progress?.ReportFinished(SampleProcessState.Failed, record.ModelName, sw.Elapsed); return record; } } @@ -380,6 +383,7 @@ private async Task ExecuteDuplicatePlanAsync( Plan = duplicatePlan, ModelName = GenerateHybridName(duplicatePlan.Quant) }; + var sw = Stopwatch.StartNew(); try { @@ -391,21 +395,28 @@ private async Task ExecuteDuplicatePlanAsync( record.State = SampleProcessState.Completed; record.TensorComboId = identity.TensorComboId; record.BenchmarkId = identity.BenchmarkId; - progress?.ReportFinished(SampleProcessState.Completed, record.ModelName); + progress?.ReportFinished( + SampleProcessState.Completed, + record.ModelName, + duration: TimeSpan.Zero, + countForEtaOverride: false); + sw.Stop(); return record; } + sw.Stop(); return await ExecutePlanAsync(duplicatePlan, progress, ct); } catch (Exception ex) { + sw.Stop(); record.State = SampleProcessState.Failed; record.Error = ex.Message; AnsiConsole.MarkupLine($"[red]Sample failed:[/] {Markup.Escape(record.ModelName)}"); AnsiConsole.MarkupLine($"[grey]{Markup.Escape(ex.Message)}[/]"); - progress?.ReportFinished(SampleProcessState.Failed, record.ModelName); + progress?.ReportFinished(SampleProcessState.Failed, record.ModelName, sw.Elapsed); return record; } } From 3056c017cb22831beec116cf2a8c4b442ccc4e00 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 29 Apr 2026 11:52:09 -0400 Subject: [PATCH 160/258] Add verbose isolation pruning diagnostics and lifecycle audit logs --- MagicQuant/Helpers/MagicQuantDiagnostics.cs | 56 +++++++++++++++++++ MagicQuant/Helpers/RuntimeSearchSpace.cs | 27 ++++++--- MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 18 ++++++ MagicQuant/Helpers/TensorConfigGenerator.cs | 32 ++++++++++- .../Services/IsolationOptimizationService.cs | 48 +++++++++++----- 5 files changed, 158 insertions(+), 23 deletions(-) create mode 100644 MagicQuant/Helpers/MagicQuantDiagnostics.cs diff --git a/MagicQuant/Helpers/MagicQuantDiagnostics.cs b/MagicQuant/Helpers/MagicQuantDiagnostics.cs new file mode 100644 index 0000000..916640f --- /dev/null +++ b/MagicQuant/Helpers/MagicQuantDiagnostics.cs @@ -0,0 +1,56 @@ +using System.Runtime.CompilerServices; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Helpers; + +public static class MagicQuantDiagnostics +{ + private const string VerboseEnv = "MAGICQUANT_DIAG_VERBOSE_ISOLATION_PRUNING"; + private const string FocusGroupEnv = "MAGICQUANT_DIAG_GROUP"; + + public static bool VerboseIsolationPruning => + string.Equals(Environment.GetEnvironmentVariable(VerboseEnv), "1", StringComparison.OrdinalIgnoreCase) || + string.Equals(Environment.GetEnvironmentVariable(VerboseEnv), "true", StringComparison.OrdinalIgnoreCase); + + public static string? FocusGroup => Environment.GetEnvironmentVariable(FocusGroupEnv); + + public static bool ShouldLogGroup(TensorGroup group) + { + if (!VerboseIsolationPruning) + return false; + + if (string.IsNullOrWhiteSpace(FocusGroup)) + return true; + + return string.Equals(group.Name, FocusGroup, StringComparison.OrdinalIgnoreCase) + || string.Equals(group.UniqueId.ToString(), FocusGroup, StringComparison.OrdinalIgnoreCase); + } + + public static void Log(string tag, string message) + { + if (!VerboseIsolationPruning) + return; + AnsiConsole.MarkupLine($"[grey][diag:{Markup.Escape(tag)}][/]: {Markup.Escape(message)}"); + } + + public static string CandidateLabel(BaselineQuants candidate) => $"{candidate.Names[0]}(id={candidate.UniqueId})"; + + public static void LogRuntimeMutation( + string phase, + TensorGroup group, + BaselineQuants? candidate, + string reason, + int before, + int after, + [CallerMemberName] string caller = "") + { + if (!ShouldLogGroup(group)) + return; + + var candidateText = candidate == null ? "" : CandidateLabel(candidate); + Log("runtime-ban", + $"phase={phase} group={group.Name}(id={group.UniqueId}) candidate={candidateText} reason=\"{reason}\" allowedBefore={before} allowedAfter={after} caller={caller}"); + } +} diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 32fc67b..78f70da 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -44,8 +44,13 @@ public static void ResetForCompatibilityPass() public static bool HasUsableImatrix() => _imatrixAvailable; - public static void BanCombinationCandidateForGroup(TensorGroup group, BaselineQuants candidate) + public static void BanCombinationCandidateForGroup( + TensorGroup group, + BaselineQuants candidate, + string phase = "Unknown", + string reason = "unspecified") { + int before = GetAllowedRealExplicitCombinationCandidatesForGroup(group).Count; if (!ExplicitCandidateBansByGroup.TryGetValue(group.UniqueId, out var set)) { set = new HashSet(); @@ -53,6 +58,8 @@ public static void BanCombinationCandidateForGroup(TensorGroup group, BaselineQu } set.Add(candidate.UniqueId); + int after = GetAllowedRealExplicitCombinationCandidatesForGroup(group).Count; + MagicQuantDiagnostics.LogRuntimeMutation(phase, group, candidate, reason, before, after); } public static void BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( @@ -62,7 +69,7 @@ public static void BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( IReadOnlyCollection matchedTensorWeightSchemeIds, string note) { - BanCombinationCandidateForGroup(group, candidate); + BanCombinationCandidateForGroup(group, candidate, phase: "LearnedPrune", reason: note); if (!LearnedPrunesByGroupAndCandidate.TryGetValue(group.UniqueId, out var byCandidate)) { @@ -102,10 +109,10 @@ public static void ClearLearnedBaselinePruneForGroupCandidate(TensorGroup group, LearnedPrunesByGroupAndCandidate.Remove(group.UniqueId); } - public static void BanAllExplicitCombinationCandidatesForGroup(TensorGroup group) + public static void BanAllExplicitCombinationCandidatesForGroup(TensorGroup group, string phase = "Unknown", string reason = "ban-all") { foreach (var candidate in GetRealExplicitCombinationCandidatesForGroup(group)) - BanCombinationCandidateForGroup(group, candidate); + BanCombinationCandidateForGroup(group, candidate, phase, reason); } public static IReadOnlyList GetRuntimeExplicitCandidateBansForGroup(TensorGroup group) @@ -163,7 +170,12 @@ public static IReadOnlyList GetLearnedBaselineMis .ToList(); } - public static void SuppressBf16TensorChoice(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); + public static void SuppressBf16TensorChoice(TensorGroup group, string phase = "Unknown", string reason = "suppressed") + { + Bf16SuppressedTensorChoiceGroupIds.Add(group.UniqueId); + if (MagicQuantDiagnostics.ShouldLogGroup(group)) + MagicQuantDiagnostics.Log("runtime-ban", $"phase={phase} group={group.Name}(id={group.UniqueId}) bf16Suppressed=true reason=\"{reason}\""); + } public static bool IsBf16TensorChoiceSuppressed(TensorGroup group) => Bf16SuppressedTensorChoiceGroupIds.Contains(group.UniqueId) && HasAnyExplicitCombinationCandidateAllowed(group); @@ -208,7 +220,7 @@ public static IReadOnlyList GetActiveCombinationBaselines() return active; } - public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allowDisablingLast = false) + public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allowDisablingLast = false, string phase = "Unknown", string reason = "disabled") { if (!baseline.IsCombinationCarrierCandidate || DisabledCombinationBaselineIds.Contains(baseline.UniqueId)) return false; @@ -218,6 +230,7 @@ public static bool DisableCombinationBaseline(BaselineQuants baseline, bool allo return false; DisabledCombinationBaselineIds.Add(baseline.UniqueId); + MagicQuantDiagnostics.Log("runtime-ban", $"phase={phase} baseline={baseline.Names[0]}(id={baseline.UniqueId}) reason=\"{reason}\""); return true; } @@ -248,4 +261,4 @@ public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeight [Obsolete("Use IsGroupExplicitCandidateBanned.")] public static bool IsGroupExplicitQuantBanned(TensorGroup group) => IsGroupExplicitCandidateBanned(group); -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index 8c04e9b..3421c0e 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -95,6 +95,24 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc AnsiConsole.MarkupLine( $" [cyan]{Markup.Escape(group.Name)}[/] => [green]{ids.Length}[/] choice(s) " + $"[grey][[{Markup.Escape(state)}]][/] :: {Markup.Escape(string.Join(", ", names))}"); + + if (MagicQuantDiagnostics.ShouldLogGroup(group)) + { + var raw = RuntimeSearchSpace.GetRealExplicitCombinationCandidatesForGroup(group); + var runtimeBanned = raw.Where(x => RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x)).ToList(); + var staticBanned = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), false) + .Where(x => x.BannedGroupIds.Contains(group.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + var allowedReal = RuntimeSearchSpace.GetAllowedRealExplicitCombinationCandidatesForGroup(group); + var why = ids.Length == 1 ? "single final choice after bans/suppression" : "multi-choice"; + MagicQuantDiagnostics.Log("search-space", + $"group={group.Name}(id={group.UniqueId}) unused={Cache.UnusedTensorGroups.Any(x=>x.UniqueId==group.UniqueId)} explicitBanned={RuntimeSearchSpace.IsGroupExplicitCandidateBanned(group)} bf16Suppressed={RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)} rawExplicit={raw.Count} staticBanned={staticBanned.Count} runtimeBanned={runtimeBanned.Count} allowedExplicit={allowedReal.Count} finalChoices={string.Join(",", names)} why={why}"); + if (runtimeBanned.Count > 0) + MagicQuantDiagnostics.Log("search-space", $"group={group.Name} runtimeBanned: {string.Join(", ", runtimeBanned.Select(x => $"{x.Names[0]}(id={x.UniqueId})"))}"); + if (staticBanned.Count > 0) + MagicQuantDiagnostics.Log("search-space", $"group={group.Name} staticBanned: {string.Join(", ", staticBanned.Select(x => $"{x.Names[0]}(id={x.UniqueId})"))}"); + } } AnsiConsole.MarkupLine($" [bold green]Base total:[/] {baseCount:N0}"); diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index a7ce0cd..286d920 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -116,6 +116,7 @@ void AddPureBaselinePlan(BaselineQuants baseline) AnsiConsole.MarkupLine($"[bold green]Base-only isolation samples required:[/] {result.BaseOnlyIsolationCount:N0}"); AnsiConsole.MarkupLine($"[bold green]Smallest-probe isolation samples required:[/] {result.GroupIsolationCount:N0}"); AnsiConsole.MarkupLine($"[bold green]Total initial startup samples:[/] {result.TotalCount:N0}"); + EmitSamplePlanDiagnostics("initial", result.Plans); return result; } @@ -139,6 +140,7 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var result = BuildIsolationCoverageContinuationPlan(activeGroups, missingIds); AnsiConsole.MarkupLine($"[bold green]Continuation isolation samples required:[/] {result.GroupIsolationCount:N0}"); + EmitSamplePlanDiagnostics("continuation", result.Plans); return result; } @@ -244,6 +246,34 @@ private static RequiredSampleGenerationResult BuildIsolationCoverageContinuation return result; } + private static void EmitSamplePlanDiagnostics(string phase, IReadOnlyCollection plans) + { + if (!MagicQuantDiagnostics.VerboseIsolationPruning) + return; + + var byGroup = plans.Where(x => x.TargetGroupId.HasValue).GroupBy(x => x.TargetGroupId!.Value); + foreach (var set in byGroup) + { + var group = TReg.All.First(x => x.UniqueId == set.Key); + if (!MagicQuantDiagnostics.ShouldLogGroup(group)) + continue; + var planned = set.Select(x => BaselineQuants.FromId(x.TestedCandidateId!.Value)).ToList(); + var smallest = GetSmallestAllowedProbeCandidateForGroup(group); + var allowed = RuntimeSearchSpace.GetAllowedRealExplicitCombinationCandidatesForGroup(group); + var raw = RuntimeSearchSpace.GetRealExplicitCombinationCandidatesForGroup(group); + var staticBanned = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), false) + .Where(x => x.BannedGroupIds.Contains(group.UniqueId)).ToList(); + var runtimeBanned = raw.Where(x => RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x)).ToList(); + var notAllowed = planned.Where(x => allowed.All(a => a.UniqueId != x.UniqueId)).ToList(); + MagicQuantDiagnostics.Log("sample-plan", $"phase={phase} group={group.Name}(id={group.UniqueId}) plannedCount={planned.Count} smallestProbe={(smallest == null ? "" : MagicQuantDiagnostics.CandidateLabel(smallest))}"); + MagicQuantDiagnostics.Log("sample-plan", $"planned={string.Join(", ", planned.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", $"allowedAtPlan={string.Join(", ", allowed.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", $"staticBanned={string.Join(", ", staticBanned.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", $"runtimeBanned={string.Join(", ", runtimeBanned.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", $"plannedButNotAllowed={string.Join(", ", notAllowed.Select(MagicQuantDiagnostics.CandidateLabel))}"); + } + } + public static List GenerateRequiredDataSampleCombos(List? missingTensorGroups = null) { return GenerateInitialIsolationSamplePlan(missingTensorGroups) @@ -390,4 +420,4 @@ private static int ComputeWorkerThreads(int threadCount) return Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 88ee9b4..d73811a 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -113,7 +113,7 @@ public async Task AnalyzeInitialIsolationProbesA if (reduction < options.MinMeaningfulGroupReductionRatio) { - RuntimeSearchSpace.BanAllExplicitCombinationCandidatesForGroup(group); + RuntimeSearchSpace.BanAllExplicitCombinationCandidatesForGroup(group, phase: "InitialProbe", reason: "smallest probe savings below threshold"); decision.ExplicitQuantBanned = true; result.Notes.Add( @@ -130,7 +130,7 @@ public async Task AnalyzeInitialIsolationProbesA if (reduction >= IsolationPruningConfig.MinimumIsolationReductionToSuppressBf16Ratio) { - RuntimeSearchSpace.SuppressBf16TensorChoice(group); + RuntimeSearchSpace.SuppressBf16TensorChoice(group, phase: "InitialProbe", reason: "smallest probe savings exceeded BF16 suppression threshold"); decision.Bf16Suppressed = true; result.Notes.Add( @@ -185,7 +185,11 @@ public async Task AnalyzeAndApplyFinalAsync( { var snap = await LoadSnapshotAsync(item.Quant, ct); if (snap == null) + { + if (MagicQuantDiagnostics.ShouldLogGroup(group)) + MagicQuantDiagnostics.Log("final-load", $"group={group.Name}(id={group.UniqueId}) candidate={BaselineQuants.FromId(item.TestedCandidateId!.Value).Names[0]} key={item.Key} loaded=no"); continue; + } var candidateBaseline = BaselineQuants.FromId(item.TestedCandidateId!.Value); RuntimeSearchSpace.ClearLearnedBaselinePruneForGroupCandidate(group, candidateBaseline); @@ -199,6 +203,12 @@ public async Task AnalyzeAndApplyFinalAsync( Kld = GetAggregateKld(snap), PplDeltaPercent = GetAggregatePplDeltaPercent(snap, nativeBaseline) }); + if (MagicQuantDiagnostics.ShouldLogGroup(group)) + { + var cb = candidates[^1]; + var allowedNow = RuntimeSearchSpace.GetAllowedRealExplicitCombinationCandidatesForGroup(group).Any(x => x.UniqueId == cb.CandidateBaseline.UniqueId); + MagicQuantDiagnostics.Log("final-load", $"group={group.Name}(id={group.UniqueId}) candidate={cb.CandidateBaseline.Names[0]}(id={cb.CandidateBaseline.UniqueId}) loaded=yes sizeGB={(cb.SizeBytes / 1024d / 1024d / 1024d):F3} savings={cb.SavingsRatio:P2} kld={cb.Kld:G6} pplDelta={cb.PplDeltaPercent:F4}% highPrecision={IsHighPrecisionCandidate(cb.CandidateBaseline)} runtimeBanned={RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, cb.CandidateBaseline)} allowedNow={allowedNow} key={item.Key}"); + } } if (candidates.Count == 0) @@ -222,7 +232,7 @@ public async Task AnalyzeAndApplyFinalAsync( if (!hardFail) continue; - RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate.CandidateBaseline); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate.CandidateBaseline, phase: "HardDamage", reason: $"kld={candidate.Kld:G6}, pplDelta={candidate.PplDeltaPercent:F4}%"); result.HardDamageEliminations++; result.Notes.Add( @@ -473,7 +483,7 @@ private static List FilterSurvivors(TensorGroup group, private static void ApplyDominanceElimination(TensorGroup group, List candidates, IsolationOptimizationResult result) { - var explicitCandidates = GetActiveExplicitCandidates(group, candidates); + var explicitCandidates = GetActiveExplicitCandidates(group, candidates, phase: "Dominance"); for (int i = 0; i < explicitCandidates.Count; i++) { @@ -498,7 +508,7 @@ private static void ApplyDominanceElimination(TensorGroup group, List candidates, IsolationOptimizationResult result) { - var activeCandidates = GetActiveExplicitCandidates(group, candidates); + var activeCandidates = GetActiveExplicitCandidates(group, candidates, phase: "BadTrade"); if (activeCandidates.Count <= 1) return; @@ -534,7 +544,7 @@ private static void ApplyBadTradeElimination(TensorGroup group, List candidates, IsolationOptimizationResult result) { - var explicitCandidates = GetActiveExplicitCandidates(group, candidates); + var explicitCandidates = GetActiveExplicitCandidates(group, candidates, phase: "EquivalentTruth"); if (explicitCandidates.Count <= 1) return; @@ -619,7 +629,7 @@ private static void ApplyEquivalentTruthElimination( if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, loser.CandidateBaseline)) continue; - RuntimeSearchSpace.BanCombinationCandidateForGroup(group, loser.CandidateBaseline); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, loser.CandidateBaseline, phase: "EquivalentTruth", reason: $"equivalent to {representative.CandidateBaseline.Names[0]}"); result.DominatedGroupCandidatesBanned++; result.Notes.Add( @@ -628,12 +638,20 @@ private static void ApplyEquivalentTruthElimination( } } - private static List GetActiveExplicitCandidates(TensorGroup group, List candidates) + private static List GetActiveExplicitCandidates(TensorGroup group, List candidates, string phase = "Unknown") { - return candidates - .Where(x => !IsHighPrecisionCandidate(x.CandidateBaseline)) - .Where(x => !RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x.CandidateBaseline)) - .ToList(); + var active = new List(); + foreach (var candidate in candidates) + { + bool hp = IsHighPrecisionCandidate(candidate.CandidateBaseline); + bool rb = RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate.CandidateBaseline); + bool include = !hp && !rb; + if (include) + active.Add(candidate); + if (MagicQuantDiagnostics.ShouldLogGroup(group)) + MagicQuantDiagnostics.Log("active-filter", $"phase={phase} group={group.Name}(id={group.UniqueId}) candidate={candidate.CandidateBaseline.Names[0]}(id={candidate.CandidateBaseline.UniqueId}) included={include} highPrecision={hp} runtimeBanned={rb}"); + } + return active; } private static List> BuildSizeBuckets(List candidates) @@ -1080,4 +1098,4 @@ private sealed class CategorySnapshot public double Ppl { get; set; } public double PplError { get; set; } } -} \ No newline at end of file +} From 05d23f7572efd50bf294d4caf5a8e76857223d99 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 29 Apr 2026 12:06:12 -0400 Subject: [PATCH 161/258] Add focused tensor compatibility diagnostics for block alignment bans --- MagicQuant/Helpers/RuntimeSearchSpace.cs | 16 +++ MagicQuant/Helpers/SearchSpaceDebugPrinter.cs | 8 ++ .../Services/ModelCompatibilityService.cs | 111 +++++++++++++++++- 3 files changed, 131 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index 78f70da..dcd1e0d 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -15,6 +15,7 @@ public sealed class RuntimeLearnedBaselineBanInfo public static class RuntimeSearchSpace { private static readonly Dictionary> ExplicitCandidateBansByGroup = new(); + private static readonly Dictionary> ExplicitCandidateBanReasonsByGroup = new(); private static readonly Dictionary> LearnedPrunesByGroupAndCandidate = new(); private static readonly HashSet DisabledCombinationBaselineIds = new(); private static readonly HashSet Bf16SuppressedTensorChoiceGroupIds = new(); @@ -25,6 +26,7 @@ public static class RuntimeSearchSpace public static void ResetForNewModel() { ExplicitCandidateBansByGroup.Clear(); + ExplicitCandidateBanReasonsByGroup.Clear(); LearnedPrunesByGroupAndCandidate.Clear(); DisabledCombinationBaselineIds.Clear(); Bf16SuppressedTensorChoiceGroupIds.Clear(); @@ -37,6 +39,7 @@ public static void ResetForNewModel() public static void ResetForCompatibilityPass() { ExplicitCandidateBansByGroup.Clear(); + ExplicitCandidateBanReasonsByGroup.Clear(); LearnedPrunesByGroupAndCandidate.Clear(); DisabledCombinationBaselineIds.Clear(); Bf16SuppressedTensorChoiceGroupIds.Clear(); @@ -58,6 +61,12 @@ public static void BanCombinationCandidateForGroup( } set.Add(candidate.UniqueId); + if (!ExplicitCandidateBanReasonsByGroup.TryGetValue(group.UniqueId, out var reasonMap)) + { + reasonMap = new Dictionary(); + ExplicitCandidateBanReasonsByGroup[group.UniqueId] = reasonMap; + } + reasonMap[candidate.UniqueId] = reason; int after = GetAllowedRealExplicitCombinationCandidatesForGroup(group).Count; MagicQuantDiagnostics.LogRuntimeMutation(phase, group, candidate, reason, before, after); } @@ -126,6 +135,13 @@ public static IReadOnlyList GetRuntimeExplicitCandidateBansForGr .ToList(); } + public static IReadOnlyDictionary GetRuntimeExplicitCandidateBanReasonsForGroup(TensorGroup group) + { + if (!ExplicitCandidateBanReasonsByGroup.TryGetValue(group.UniqueId, out var reasons)) + return new Dictionary(); + return reasons; + } + public static bool IsCombinationCandidateRuntimeBannedForGroup(TensorGroup group, BaselineQuants candidate) => ExplicitCandidateBansByGroup.TryGetValue(group.UniqueId, out var set) && set.Contains(candidate.UniqueId); diff --git a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs index 3421c0e..e900bed 100644 --- a/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs +++ b/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs @@ -105,11 +105,19 @@ public static void PrintCurrentSearchSpace(string title = "Current Runtime Searc .OrderBy(x => x.UniqueId) .ToList(); var allowedReal = RuntimeSearchSpace.GetAllowedRealExplicitCombinationCandidatesForGroup(group); + var reasonMap = RuntimeSearchSpace.GetRuntimeExplicitCandidateBanReasonsForGroup(group); var why = ids.Length == 1 ? "single final choice after bans/suppression" : "multi-choice"; MagicQuantDiagnostics.Log("search-space", $"group={group.Name}(id={group.UniqueId}) unused={Cache.UnusedTensorGroups.Any(x=>x.UniqueId==group.UniqueId)} explicitBanned={RuntimeSearchSpace.IsGroupExplicitCandidateBanned(group)} bf16Suppressed={RuntimeSearchSpace.IsBf16TensorChoiceSuppressed(group)} rawExplicit={raw.Count} staticBanned={staticBanned.Count} runtimeBanned={runtimeBanned.Count} allowedExplicit={allowedReal.Count} finalChoices={string.Join(",", names)} why={why}"); if (runtimeBanned.Count > 0) MagicQuantDiagnostics.Log("search-space", $"group={group.Name} runtimeBanned: {string.Join(", ", runtimeBanned.Select(x => $"{x.Names[0]}(id={x.UniqueId})"))}"); + if (reasonMap.Count > 0) + { + var grouped = runtimeBanned + .GroupBy(x => reasonMap.TryGetValue(x.UniqueId, out var r) ? r : "unspecified") + .Select(g => $"{g.Key}: {string.Join(", ", g.Select(x => x.Names[0]))}"); + MagicQuantDiagnostics.Log("search-space", $"group={group.Name} restrictionReasons={string.Join(" | ", grouped)}"); + } if (staticBanned.Count > 0) MagicQuantDiagnostics.Log("search-space", $"group={group.Name} staticBanned: {string.Join(", ", staticBanned.Select(x => $"{x.Names[0]}(id={x.UniqueId})"))}"); } diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 32a8231..3694b10 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -1,13 +1,17 @@ using System.Text.Json; using MagicQuant.Helpers; using MQ.DB; +using MQ.DB.Data; using MQ.DB.Models; +using Microsoft.EntityFrameworkCore; using Spectre.Console; namespace MagicQuant.Services; public class ModelCompatibilityService { + private const string CompatVerboseEnv = "MAGICQUANT_DIAG_VERBOSE_TENSOR_COMPATIBILITY"; + private const string CompatFocusCandidatesEnv = "MAGICQUANT_DIAG_FOCUS_CANDIDATES"; private readonly PythonManager _pyManager; public ModelCompatibilityService(PythonManager pyManager) @@ -70,6 +74,9 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) if (result == null) return; + bool compatVerbose = IsCompatVerbose(); + var focusCandidates = GetFocusCandidates(); + var runtimeCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false).ToList(); int unusedCount = 0; int usedCount = 0; int shapeBanCount = 0; @@ -93,26 +100,44 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) unusedCount++; Cache.UnusedTensorGroups.Add(group); - RuntimeSearchSpace.BanAllExplicitCombinationCandidatesForGroup(group); + RuntimeSearchSpace.BanAllExplicitCombinationCandidatesForGroup(group, phase: "TensorCompatibilityCheck", reason: "group missing in GGUF"); } + var failuresByGroupAndScheme = result.Failures + .GroupBy(x => (x.Group, x.Scheme), StringComparer.OrdinalIgnoreCase) + .ToDictionary(x => x.Key, x => x.ToList()); + foreach (var failure in result.Incompatible) { var group = TReg.GetByName(failure.Group); - var candidate = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false) - .FirstOrDefault(c => c.Names.Any(n => n.Equals(failure.Scheme, StringComparison.OrdinalIgnoreCase))); + var candidate = runtimeCandidates.FirstOrDefault(c => c.Names.Any(n => n.Equals(failure.Scheme, StringComparison.OrdinalIgnoreCase))); if (group == null || candidate == null) continue; if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate)) continue; - RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate); + var beforeRuntimeBan = RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate); + RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate, phase: "TensorCompatibilityCheck", reason: "Block Alignment"); shapeBanCount++; shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{candidate.Names[0]}[/]", "[grey]Block Alignment[/]"); + + if (ShouldLogCompatDetail(compatVerbose, group, candidate, focusCandidates)) + { + MagicQuantDiagnostics.Log("compat:decision", + $"group={group.Name}(id={group.UniqueId}) candidate={candidate.Names[0]}(id={candidate.UniqueId}) scheme={candidate.DefaultTensorScheme?.Names[0] ?? ""} block={candidate.DefaultTensorScheme?.BlockNeo?.ToString() ?? ""} staticBanned={candidate.BannedGroupIds.Contains(group.UniqueId)} runtimeBannedBefore={beforeRuntimeBan} result=restricted reason=Block Alignment"); + } + + if (failuresByGroupAndScheme.TryGetValue((failure.Group, failure.Scheme), out var details) && details.Count > 0) + { + LogFailureSummary(group, candidate, details); + } } + if (compatVerbose) + await LogGroupCompatibilityOutcomeAndTruthCrossCheckAsync(result, runtimeCandidates, focusCandidates, ct: CancellationToken.None); + foreach (var group in TReg.All.Except(Cache.UnusedTensorGroups)) { if (RuntimeSearchSpace.IsGroupExplicitCandidateBanned(group)) @@ -160,6 +185,53 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) } } + private static bool IsCompatVerbose() + => string.Equals(Environment.GetEnvironmentVariable(CompatVerboseEnv), "1", StringComparison.OrdinalIgnoreCase) + || string.Equals(Environment.GetEnvironmentVariable(CompatVerboseEnv), "true", StringComparison.OrdinalIgnoreCase); + + private static HashSet GetFocusCandidates() + => (Environment.GetEnvironmentVariable(CompatFocusCandidatesEnv) ?? string.Empty) + .Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) + .ToHashSet(StringComparer.OrdinalIgnoreCase); + + private static bool ShouldLogCompatDetail(bool compatVerbose, TensorGroup group, BaselineQuants candidate, HashSet focusCandidates) + => compatVerbose && (MagicQuantDiagnostics.ShouldLogGroup(group) || focusCandidates.Count == 0 || candidate.Names.Any(x => focusCandidates.Contains(x))); + + private static void LogFailureSummary(TensorGroup group, BaselineQuants candidate, List details) + { + var failCount = details.Count; + var firstFive = details.Take(5).ToList(); + var shapes = details.GroupBy(x => $"[{string.Join(",", x.Shape)}]").Select(x => $"{x.Key} x{x.Count()}").ToList(); + var remainders = details.Select(x => x.Remainder).Distinct().OrderBy(x => x).ToList(); + MagicQuantDiagnostics.Log("compat:summary", $"group={group.Name} candidate={candidate.Names[0]} checkedTensors={details.Max(x => x.CheckedTensorCount)} failingTensors={failCount} shapePatterns={string.Join("; ", shapes)} remainders={string.Join(",", remainders)}"); + foreach (var f in firstFive) + { + MagicQuantDiagnostics.Log("compat:block-alignment-fail", + $"group={group.Name}(id={group.UniqueId}) candidate={candidate.Names[0]}(id={candidate.UniqueId}) tensor={f.Tensor} tensorClass={f.TensorClass} dims=[{string.Join(",", f.Shape)}] nDims={f.Shape.Count} checkedDimension={f.CheckedDimension} checkedValue={f.CheckedValue} requiredMultiple={f.RequiredMultiple} remainder={f.Remainder} pass=false reason=Block Alignment"); + } + } + + private static async Task LogGroupCompatibilityOutcomeAndTruthCrossCheckAsync(CompatResult result, List candidates, HashSet focusCandidates, CancellationToken ct) + { + await using var db = new MagicQuantContext(); + var modelHashId = await ArchitectureFamilyService.ResolveExactCurrentAiModelHashIdOrNullAsync(db, ct); + var imatrixId = modelHashId == null ? (long?)null : await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, modelHashId.Value, createIfMissing: false, ct); + foreach (var group in TReg.All.Except(Cache.UnusedTensorGroups)) + { + if (!MagicQuantDiagnostics.ShouldLogGroup(group)) + continue; + var raw = RuntimeSearchSpace.GetRealExplicitCombinationCandidatesForGroup(group); + var allowed = RuntimeSearchSpace.GetAllowedRealExplicitCombinationCandidatesForGroup(group); + var restricted = raw.Where(x => RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x)).ToList(); + MagicQuantDiagnostics.Log("compat:group-result", $"group={group.Name}(id={group.UniqueId}) before={raw.Count} after={allowed.Count} allowed={string.Join(", ", allowed.Select(MagicQuantDiagnostics.CandidateLabel))} restricted={string.Join(", ", restricted.Select(MagicQuantDiagnostics.CandidateLabel))}"); + if (raw.Count > 5 && allowed.Count == 1 && allowed[0].UniqueId == BaselineQuants.Q8_0.UniqueId) + { + var focusRestricted = restricted.Where(x => x.Names.Any(n => focusCandidates.Contains(n)) || x.Names[0] is "Q6_K" or "Q5_K" or "Q4_K_M").Select(x => x.Names[0]); + MagicQuantDiagnostics.Log("compat:collapse-warning", $"group={group.Name} collapsed to Q8_0 only restrictions={restricted.Count} topReason=Block Alignment focusCandidatesRestricted={string.Join(",", focusRestricted)}"); + } + } + } + private string GeneratePythonScript(string jsonPayload) { return $@" @@ -192,6 +264,7 @@ import gguf found_groups = [] failures = [] +failure_details = [] debug_lines = [] debug_lines.append('Inspecting ' + str(len(tensor_names)) + ' tensors against ' + str(len(config[""schemes""])) + ' block requirements.') @@ -232,6 +305,26 @@ import gguf if ne0 % block_size != 0: is_valid = False + tclass = 'unknown' + lower_name = w_name.lower() + if 'exps' in lower_name: + tclass = 'routed_expert' + elif 'router' in lower_name: + tclass = 'router' + elif 'ffn_' in lower_name: + tclass = 'dense_ffn' + failure_details.append({{ + ""Group"": g_name, + ""Scheme"": scheme, + ""Tensor"": w_name, + ""Shape"": list(t_obj.shape), + ""CheckedDimension"": 0, + ""CheckedValue"": int(ne0), + ""RequiredMultiple"": int(block_size), + ""Remainder"": int(ne0 % block_size), + ""TensorClass"": tclass, + ""CheckedTensorCount"": len(weights) + }}) debug_lines.append("" [FAIL] "" + g_name + "" vs "" + scheme + "" (Block "" + str(block_size) + ""): "" + w_name + "" ne0="" + str(ne0) + "". Remainder="" + str(ne0 % block_size)) break @@ -250,6 +343,7 @@ with open(output_path, 'w') as f: json.dump({{ ""FoundGroups"": found_groups, ""Incompatible"": failures, + ""Failures"": failure_details, ""Error"": None }}, f, indent=2) "; @@ -259,6 +353,7 @@ private class CompatResult { public List FoundGroups { get; set; } = new(); public List Incompatible { get; set; } = new(); + public List Failures { get; set; } = new(); public string? Error { get; set; } } @@ -266,5 +361,13 @@ private class CompatFailure { public string Group { get; set; } = string.Empty; public string Scheme { get; set; } = string.Empty; + public string Tensor { get; set; } = string.Empty; + public List Shape { get; set; } = new(); + public int CheckedDimension { get; set; } + public long CheckedValue { get; set; } + public int RequiredMultiple { get; set; } + public long Remainder { get; set; } + public string TensorClass { get; set; } = "unknown"; + public int CheckedTensorCount { get; set; } } } From 13c29ced9fefcfcffb76d6f016b360f46e27a981 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 29 Apr 2026 12:08:14 -0400 Subject: [PATCH 162/258] Fix compatibility diagnostics grouping key compile errors --- MagicQuant/Services/ModelCompatibilityService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 3694b10..7f1962f 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -104,8 +104,8 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) } var failuresByGroupAndScheme = result.Failures - .GroupBy(x => (x.Group, x.Scheme), StringComparer.OrdinalIgnoreCase) - .ToDictionary(x => x.Key, x => x.ToList()); + .GroupBy(x => $"{x.Group}::{x.Scheme}", StringComparer.OrdinalIgnoreCase) + .ToDictionary(x => x.Key, x => x.ToList(), StringComparer.OrdinalIgnoreCase); foreach (var failure in result.Incompatible) { @@ -129,7 +129,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) $"group={group.Name}(id={group.UniqueId}) candidate={candidate.Names[0]}(id={candidate.UniqueId}) scheme={candidate.DefaultTensorScheme?.Names[0] ?? ""} block={candidate.DefaultTensorScheme?.BlockNeo?.ToString() ?? ""} staticBanned={candidate.BannedGroupIds.Contains(group.UniqueId)} runtimeBannedBefore={beforeRuntimeBan} result=restricted reason=Block Alignment"); } - if (failuresByGroupAndScheme.TryGetValue((failure.Group, failure.Scheme), out var details) && details.Count > 0) + if (failuresByGroupAndScheme.TryGetValue($"{failure.Group}::{failure.Scheme}", out var details) && details.Count > 0) { LogFailureSummary(group, candidate, details); } From 8169a58abd84d954a5a1a8d7a95d624781d0cfe7 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 29 Apr 2026 12:36:53 -0400 Subject: [PATCH 163/258] Removing incompatible tensors --- MagicQuant/Helpers/CliHelpers.cs | 11 +++++++++-- MagicQuant/Services/ModelCompatibilityService.cs | 4 +++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index c7f9df4..c286ab8 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -73,13 +73,20 @@ public static void ValidateCombinationLogicWorks(bool realResults = false) public static void PrintTotalCombinationCount() { - const long MaxSupported = 4_000_000_000L; + /*const long MaxSupported = 4_000_000_000L; BigInteger total = ComboCounter.CountAll(); if (total > MaxSupported) throw new InvalidOperationException( - $"Total combinations ({total:N0}) exceed database primary ID limit ({MaxSupported:N0})."); + $"Total combinations ({total:N0}) exceed database primary ID limit ({MaxSupported:N0}).");*/ + + BigInteger total = ComboCounter.CountAll(); + + AnsiConsole.MarkupLine( + total > long.MaxValue + ? $"[red]Total potential combinations exceed Int64 range:[/] [bold yellow]{total:N0}[/]" + : $"[green]Total potential combinations:[/] [bold yellow]{total:N0}[/]"); AnsiConsole.MarkupLine($"[green]Total potential combinations:[/] [bold yellow]{total:N0}[/]"); } diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 7f1962f..7c1be96 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -118,7 +118,9 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) continue; var beforeRuntimeBan = RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate); - RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate, phase: "TensorCompatibilityCheck", reason: "Block Alignment"); + + // something is wrong with this. It's not working and this is a luxury not requirement. It's causing down stream issues on moe_experts for Qwen3.6 35B A3B + //RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate, phase: "TensorCompatibilityCheck", reason: "Block Alignment"); shapeBanCount++; shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{candidate.Names[0]}[/]", "[grey]Block Alignment[/]"); From d15ba5f5a861d0b0291a5f93498fd13941b98c1a Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 29 Apr 2026 12:37:18 -0400 Subject: [PATCH 164/258] update --- MagicQuant/Services/ModelCompatibilityService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index 7c1be96..d44d7cc 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -119,7 +119,7 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) var beforeRuntimeBan = RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate); - // something is wrong with this. It's not working and this is a luxury not requirement. It's causing down stream issues on moe_experts for Qwen3.6 35B A3B + // something is wrong with this. It's not working and this is a luxury not requirement. It's causing down stream issues on moe_experts for Qwen3.6-35B-A3B //RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate, phase: "TensorCompatibilityCheck", reason: "Block Alignment"); shapeBanCount++; shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{candidate.Names[0]}[/]", From c228b6d5c9493c72ed17f63626bbf38fe628e1e8 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 29 Apr 2026 12:57:37 -0400 Subject: [PATCH 165/258] Refactor DuckDB combo staging and pruning to SQL-native paths --- MagicQuant/Config.cs | 1 + .../Configuration/MagicQuantYamlConfig.cs | 1 + .../CombinationSurvivalPipelineService.cs | 5 +- MagicQuant/Services/QuantDatabaseService.cs | 431 ++++++++---------- .../Services/RemainingCombinationStore.cs | 31 ++ 5 files changed, 227 insertions(+), 242 deletions(-) diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index f95ba7d..be1af0d 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -37,6 +37,7 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.Prediction.DefaultBitStressThreshold; public static int PredictionMinimumFitRows => Math.Max(2, Current.Prediction.MinimumFitRows); + public static long MaxInMemoryCombinationLoadRows => Math.Max(1L, Current.Prediction.MaxInMemoryCombinationLoadRows); public static double SelectionNearBaselineMaxSizeGrowthPercent => Math.Max(0d, Current.CandidateSelection.NearBaselineMaxSizeGrowthPercent); diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index bbe4516..61806bf 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -155,6 +155,7 @@ public sealed class RuntimePredictionConfig public double DefaultBitStressThreshold { get; set; } = 8.0d; public int MinimumFitRows { get; set; } = 12; + public long MaxInMemoryCombinationLoadRows { get; set; } = 5_000_000; } public sealed class RuntimeIdentityConfig diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index 3e0a18b..be3e58d 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -53,6 +53,9 @@ public async Task RunAsync(CancellationToken AnsiConsole.MarkupLine($"[green]Remaining DuckDB combinations available to score:[/] [cyan]{report.StartingCount:N0}[/]"); AnsiConsole.MarkupLine("[grey]Old MDA bucket survival is disabled. DuckDB now defines the allowed search space; rank-safe isolation prediction selects what deserves real benchmarking.[/]"); + if (report.StartingCount > Config.MaxInMemoryCombinationLoadRows) + throw new InvalidOperationException($"Final prediction selection still requires DuckDB-backed prediction materialization. Refusing to load {report.StartingCount:N0} combinations into memory."); + var remainingConfigs = await _combinationStore.LoadAllAsync(ct); var pureBaselines = await _benchmarkRepository.LoadPureBaselineSnapshotsAsync(ct); @@ -188,4 +191,4 @@ private void RenderEliminationSummary( AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in magicquant.replacements.json.[/]"); } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 2f97182..87f0c16 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -15,12 +15,6 @@ public class QuantDatabaseService private const string DbFileNamePrefix = "MagicQuant_Combinations"; private const string TableName = "tensor_configs"; - // Keep this moderate so generation still yields often enough for progress. - private const int GeneratorBatchSize = 250_000; - - // Appender heartbeat. Lower = chattier. - private const long InsertProgressLogEveryRows = 50_000; - private static readonly string[] ExpectedColumnTypes = [ "utinyint", @@ -90,6 +84,10 @@ public async Task GetRemainingCombinationCountAsync(CancellationToken ct = public async Task> GetRemainingTensorConfigsAsync(CancellationToken ct = default) { + long count = await GetRemainingCombinationCountAsync(ct); + if (count > Config.MaxInMemoryCombinationLoadRows) + throw new InvalidOperationException($"Refusing to load {count:N0} DuckDB tensor configs into memory. Use SQL-native filtering/streaming instead."); + using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); await ConfigureFastLoadSessionAsync(connection, ct); @@ -231,81 +229,41 @@ public async Task PrunePredictedLargerThanQ8Async( return 0; } - var rows = new List(); - - using (var select = connection.CreateCommand()) - { - select.CommandText = $@" - SELECT BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter - FROM {TableName};"; - - using var reader = await select.ExecuteReaderAsync(ct); - while (await reader.ReadAsync(ct)) - { - rows.Add(new TensorConfig( - baseQuant: Convert.ToByte(reader.GetValue(0)), - embeddings: Convert.ToByte(reader.GetValue(1)), - lmHead: Convert.ToByte(reader.GetValue(2)), - attnQ: Convert.ToByte(reader.GetValue(3)), - attnKV: Convert.ToByte(reader.GetValue(4)), - attnOutput: Convert.ToByte(reader.GetValue(5)), - ffnUpGate: Convert.ToByte(reader.GetValue(6)), - ffnDown: Convert.ToByte(reader.GetValue(7)), - moeExperts: Convert.ToByte(reader.GetValue(8)), - moeRouter: Convert.ToByte(reader.GetValue(9)) - )); - } - } - - var kept = new List(rows.Count); - var predictedByBase = new Dictionary>(); + long beforeCount = await GetRowCountAsync(connection, ct); ulong sizeCeilingBytes = Config.ManualMaxPredictedSizeBytes > 0 ? Config.ManualMaxPredictedSizeBytes : predictionContext.PureQ8BaseSize; + await BuildPredictedSizeLookupTablesAsync(connection, predictionContext, ct); - foreach (var row in rows) - { - ulong predicted = predictionContext.Predict(row); - - if (!predictedByBase.TryGetValue(row.BaseQuant, out var bucket)) - { - bucket = new List(); - predictedByBase[row.BaseQuant] = bucket; - } - - bucket.Add(predicted); - - if (predicted <= sizeCeilingBytes) - kept.Add(row); - } - - if (predictedByBase.Count > 0) - { - ulong globalMin = predictedByBase.Values.SelectMany(x => x).Min(); - ulong globalMax = predictedByBase.Values.SelectMany(x => x).Max(); - AnsiConsole.MarkupLine($"[grey]Stage-1 predicted size spread:[/] [cyan]{globalMin / 1024d / 1024d / 1024d:F2}[/] [grey]GB ..[/] [cyan]{globalMax / 1024d / 1024d / 1024d:F2}[/] [grey]GB[/]"); - - foreach (var kv in predictedByBase.OrderBy(x => BaselineQuants.FromId(x.Key).BitRange).ThenBy(x => x.Key)) - { - var baseline = BaselineQuants.FromId(kv.Key); - ulong min = kv.Value.Min(); - ulong max = kv.Value.Max(); - AnsiConsole.MarkupLine( - $"[grey]Stage-1 base {Markup.Escape(baseline.Names[0])} (BitRange {baseline.BitRange}) ->[/] [cyan]{kv.Value.Count:N0}[/] [grey]candidate(s),[/] [cyan]{min / 1024d / 1024d / 1024d:F2}[/] [grey]GB ..[/] [cyan]{max / 1024d / 1024d / 1024d:F2}[/] [grey]GB[/]"); - } - } - - long removed = rows.Count - kept.Count; - - if (removed <= 0) + using (var pruneCmd = connection.CreateCommand()) { - AnsiConsole.MarkupLine("[green]Predicted-size pruning removed 0 combinations.[/]"); - return 0; + pruneCmd.CommandText = $@" +CREATE TABLE tensor_configs_pruned AS +SELECT t.* +FROM {TableName} t +JOIN temp_base_predicted_size b ON b.BaseQuant = t.BaseQuant +LEFT JOIN temp_group_size_delta de ON de.BaseQuant = t.BaseQuant AND de.GroupName = 'Embeddings' AND de.StoredSlot = t.Embeddings +LEFT JOIN temp_group_size_delta dl ON dl.BaseQuant = t.BaseQuant AND dl.GroupName = 'LmHead' AND dl.StoredSlot = t.LmHead +LEFT JOIN temp_group_size_delta daq ON daq.BaseQuant = t.BaseQuant AND daq.GroupName = 'AttnQ' AND daq.StoredSlot = t.AttnQ +LEFT JOIN temp_group_size_delta dakv ON dakv.BaseQuant = t.BaseQuant AND dakv.GroupName = 'AttnKV' AND dakv.StoredSlot = t.AttnKV +LEFT JOIN temp_group_size_delta dao ON dao.BaseQuant = t.BaseQuant AND dao.GroupName = 'AttnOutput' AND dao.StoredSlot = t.AttnOutput +LEFT JOIN temp_group_size_delta dfu ON dfu.BaseQuant = t.BaseQuant AND dfu.GroupName = 'FfnUpGate' AND dfu.StoredSlot = t.FfnUpGate +LEFT JOIN temp_group_size_delta dfd ON dfd.BaseQuant = t.BaseQuant AND dfd.GroupName = 'FfnDown' AND dfd.StoredSlot = t.FfnDown +LEFT JOIN temp_group_size_delta dme ON dme.BaseQuant = t.BaseQuant AND dme.GroupName = 'MoeExperts' AND dme.StoredSlot = t.MoeExperts +LEFT JOIN temp_group_size_delta dmr ON dmr.BaseQuant = t.BaseQuant AND dmr.GroupName = 'MoeRouter' AND dmr.StoredSlot = t.MoeRouter +WHERE CAST(b.BaseSizeBytes AS BIGINT) + + COALESCE(de.DeltaBytes, 0) + COALESCE(dl.DeltaBytes, 0) + COALESCE(daq.DeltaBytes, 0) + + COALESCE(dakv.DeltaBytes, 0) + COALESCE(dao.DeltaBytes, 0) + COALESCE(dfu.DeltaBytes, 0) + + COALESCE(dfd.DeltaBytes, 0) + COALESCE(dme.DeltaBytes, 0) + COALESCE(dmr.DeltaBytes, 0) + <= CAST({sizeCeilingBytes} AS BIGINT); +DROP TABLE {TableName}; +ALTER TABLE tensor_configs_pruned RENAME TO {TableName};"; + await pruneCmd.ExecuteNonQueryAsync(ct); } - await RecreateTableAsync(connection, ct); - await BulkAppendAsync(connection, kept, "predicted-size-prune", ct); + long afterCount = await GetRowCountAsync(connection, ct); + long removed = beforeCount - afterCount; string ceilingLabel = Config.ManualMaxPredictedSizeBytes > 0 ? $"manual ceiling {Config.ManualMaxPredictedSizeBytes:N0} bytes" @@ -324,26 +282,30 @@ public async Task PruneHighPrecisionHybridCandidatesAsync(CancellationToke await connection.OpenAsync(ct); await ConfigureFastLoadSessionAsync(connection, ct); - var rows = await GetRemainingTensorConfigsAsync(ct); - var kept = rows.Where(x => - x.Embeddings != BaselineQuants.BF16_Hybrid.UniqueId && x.Embeddings != BaselineQuants.F16_Hybrid.UniqueId && - x.LmHead != BaselineQuants.BF16_Hybrid.UniqueId && x.LmHead != BaselineQuants.F16_Hybrid.UniqueId && - x.AttnQ != BaselineQuants.BF16_Hybrid.UniqueId && x.AttnQ != BaselineQuants.F16_Hybrid.UniqueId && - x.AttnKV != BaselineQuants.BF16_Hybrid.UniqueId && x.AttnKV != BaselineQuants.F16_Hybrid.UniqueId && - x.AttnOutput != BaselineQuants.BF16_Hybrid.UniqueId && x.AttnOutput != BaselineQuants.F16_Hybrid.UniqueId && - x.FfnUpGate != BaselineQuants.BF16_Hybrid.UniqueId && x.FfnUpGate != BaselineQuants.F16_Hybrid.UniqueId && - x.FfnDown != BaselineQuants.BF16_Hybrid.UniqueId && x.FfnDown != BaselineQuants.F16_Hybrid.UniqueId && - x.MoeExperts != BaselineQuants.BF16_Hybrid.UniqueId && x.MoeExperts != BaselineQuants.F16_Hybrid.UniqueId && - x.MoeRouter != BaselineQuants.BF16_Hybrid.UniqueId && x.MoeRouter != BaselineQuants.F16_Hybrid.UniqueId).ToList(); - - long removed = rows.Count - kept.Count; - if (removed <= 0) - return 0; - - await RecreateTableAsync(connection, ct); - await BulkAppendAsync(connection, kept, "high-precision-prune", ct); - - return removed; + long beforeCount = await GetRowCountAsync(connection, ct); + byte bf16Stored = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(BaselineQuants.BF16_Hybrid.UniqueId); + byte f16Stored = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(BaselineQuants.F16_Hybrid.UniqueId); + using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = $@" +CREATE TABLE tensor_configs_pruned AS +SELECT * +FROM {TableName} +WHERE Embeddings NOT IN ({bf16Stored}, {f16Stored}) + AND LmHead NOT IN ({bf16Stored}, {f16Stored}) + AND AttnQ NOT IN ({bf16Stored}, {f16Stored}) + AND AttnKV NOT IN ({bf16Stored}, {f16Stored}) + AND AttnOutput NOT IN ({bf16Stored}, {f16Stored}) + AND FfnUpGate NOT IN ({bf16Stored}, {f16Stored}) + AND FfnDown NOT IN ({bf16Stored}, {f16Stored}) + AND MoeExperts NOT IN ({bf16Stored}, {f16Stored}) + AND MoeRouter NOT IN ({bf16Stored}, {f16Stored}); +DROP TABLE {TableName}; +ALTER TABLE tensor_configs_pruned RENAME TO {TableName};"; + await cmd.ExecuteNonQueryAsync(ct); + } + long afterCount = await GetRowCountAsync(connection, ct); + return beforeCount - afterCount; } private async Task HasExpectedTableShapeAsync(DuckDBConnection connection, CancellationToken ct) @@ -394,89 +356,47 @@ private async Task RebuildDatabaseAsync( BigInteger expectedTotal, CancellationToken ct) { - long totalTarget = (long)expectedTotal; - - AnsiConsole.MarkupLine($"[yellow]Starting bulk insert of {totalTarget:N0} rows...[/]"); - AnsiConsole.MarkupLine( - $"[grey]Generator batch size:[/] {GeneratorBatchSize:N0} [grey]| Appender heartbeat:[/] every {InsertProgressLogEveryRows:N0} rows"); + AnsiConsole.MarkupLine($"[yellow]Starting SQL-native tensor combination generation for {expectedTotal:N0} rows...[/]"); await RecreateTableAsync(connection, ct); await ConfigureFastLoadSessionAsync(connection, ct); - long insertedGrandTotal = 0; + BigInteger insertedGrandTotal = BigInteger.Zero; var overallSw = Stopwatch.StartNew(); - using DuckDBAppender appender = connection.CreateAppender(TableName); - foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) { ct.ThrowIfCancellationRequested(); - long baseInserted = 0; - int baseBatchNumber = 0; var baseSw = Stopwatch.StartNew(); string baseName = baseline.Names.FirstOrDefault() ?? baseline.UniqueId.ToString(); - - AnsiConsole.MarkupLine($"[cyan]Generating + inserting base:[/] [bold]{Markup.Escape(baseName)}[/]"); - - foreach (var batch in TensorConfigGenerator.GenerateTensorConfigBatches(baseline, batchSize: GeneratorBatchSize)) - { - ct.ThrowIfCancellationRequested(); - - baseBatchNumber++; - int batchCount = batch.Count; - var batchSw = Stopwatch.StartNew(); - - AnsiConsole.MarkupLine( - $" [grey]Base batch #{baseBatchNumber} generated:[/] {batchCount:N0} rows [grey]| Base inserted before batch:[/] {baseInserted:N0}"); - - var progress = new InsertProgress - { - InsertedTotal = insertedGrandTotal, - LastLoggedTotal = insertedGrandTotal, - ProgressLogEveryRows = InsertProgressLogEveryRows, - TotalTarget = totalTarget, - BaseName = baseName, - BatchNumber = baseBatchNumber - }; - - AppendRows(appender, batch, progress, overallSw, ct); - - insertedGrandTotal = progress.InsertedTotal; - baseInserted += batchCount; - - batchSw.Stop(); - - double grandPct = totalTarget == 0 ? 100d : insertedGrandTotal * 100d / totalTarget; - - AnsiConsole.MarkupLine( - $" [green]Base batch #{baseBatchNumber} done:[/] {batchCount:N0} rows in {batchSw.Elapsed.TotalSeconds:N1}s " + - $"[grey]| Base running:[/] {baseInserted:N0} [grey]| Grand total:[/] {insertedGrandTotal:N0}/{totalTarget:N0} ({grandPct:N2}%)"); - - batch.Clear(); - } + long before = await GetRowCountAsync(connection, ct); + BigInteger expectedForBase = await InsertBaselineCombinationsSqlAsync(connection, baseline, ct); + long after = await GetRowCountAsync(connection, ct); + BigInteger delta = new(after - before); + if (delta != expectedForBase) + throw new InvalidOperationException($"Baseline {baseName} inserted {delta} rows, expected {expectedForBase}."); + insertedGrandTotal += delta; baseSw.Stop(); double rowsPerSec = baseSw.Elapsed.TotalSeconds <= 0 - ? 0 - : baseInserted / baseSw.Elapsed.TotalSeconds; + ? 0 : (double)(long)expectedForBase / baseSw.Elapsed.TotalSeconds; AnsiConsole.MarkupLine( $"[bold green]Base complete:[/] {Markup.Escape(baseName)} " + - $"[grey]| Inserted:[/] {baseInserted:N0} rows " + + $"[grey]| Inserted:[/] {expectedForBase:N0} rows " + $"[grey]| Time:[/] {baseSw.Elapsed.TotalMinutes:N2} min " + $"[grey]| Rate:[/] {rowsPerSec:N0} rows/sec"); } - appender.Close(); overallSw.Stop(); long finalCount = await GetRowCountAsync(connection, ct); double finalRate = overallSw.Elapsed.TotalSeconds <= 0 ? 0 - : insertedGrandTotal / overallSw.Elapsed.TotalSeconds; + : (double)(long)insertedGrandTotal / overallSw.Elapsed.TotalSeconds; AnsiConsole.MarkupLine( $"[bold green]DuckDB rebuild complete.[/] " + @@ -484,6 +404,8 @@ private async Task RebuildDatabaseAsync( $"[grey]| Final row count:[/] {finalCount:N0} " + $"[grey]| Time:[/] {overallSw.Elapsed.TotalMinutes:N2} min " + $"[grey]| Avg rate:[/] {finalRate:N0} rows/sec"); + if (new BigInteger(finalCount) != expectedTotal) + throw new InvalidOperationException($"Final tensor_configs row count mismatch. actual={finalCount:N0}, expected={expectedTotal:N0}."); } private async Task BulkAppendAsync( @@ -492,6 +414,7 @@ private async Task BulkAppendAsync( string label, CancellationToken ct) { + // Emergency/small debug use only. Do NOT use for full search-space generation or trillion-scale pruning. if (rows.Count == 0) return; @@ -499,66 +422,16 @@ private async Task BulkAppendAsync( using DuckDBAppender appender = connection.CreateAppender(TableName); - var progress = new InsertProgress - { - InsertedTotal = 0, - LastLoggedTotal = 0, - ProgressLogEveryRows = InsertProgressLogEveryRows, - TotalTarget = rows.Count, - BaseName = label, - BatchNumber = 1 - }; - - AppendRows(appender, rows, progress, Stopwatch.StartNew(), ct); - appender.Close(); - } - - private static void AppendRows( - DuckDBAppender appender, - IReadOnlyCollection rows, - InsertProgress progress, - Stopwatch overallSw, - CancellationToken ct) - { foreach (var row in rows) { ct.ThrowIfCancellationRequested(); - appender.CreateRow() - .AppendValue(row.BaseQuant) - .AppendValue(row.Embeddings) - .AppendValue(row.LmHead) - .AppendValue(row.AttnQ) - .AppendValue(row.AttnKV) - .AppendValue(row.AttnOutput) - .AppendValue(row.FfnUpGate) - .AppendValue(row.FfnDown) - .AppendValue(row.MoeExperts) - .AppendValue(row.MoeRouter) - .EndRow(); - - progress.InsertedTotal++; - - if (progress.InsertedTotal - progress.LastLoggedTotal >= progress.ProgressLogEveryRows) - { - double elapsedSeconds = Math.Max(0.001, overallSw.Elapsed.TotalSeconds); - double rowsPerSecond = progress.InsertedTotal / elapsedSeconds; - double pct = progress.TotalTarget <= 0 ? 100d : progress.InsertedTotal * 100d / progress.TotalTarget; - - long remaining = Math.Max(0, progress.TotalTarget - progress.InsertedTotal); - double etaSeconds = rowsPerSecond <= 0 ? 0 : remaining / rowsPerSecond; - var eta = TimeSpan.FromSeconds(etaSeconds); - - AnsiConsole.MarkupLine( - $" [grey]Progress[/] [green]{progress.InsertedTotal:N0}[/]/[yellow]{progress.TotalTarget:N0}[/] " + - $"({pct:N2}%) [grey]| Rate:[/] {rowsPerSecond:N0}/sec " + - $"[grey]| ETA:[/] {eta:hh\\:mm\\:ss} " + - $"[grey]| Label:[/] {Markup.Escape(progress.BaseName)} " + - $"[grey]| Batch:[/] {progress.BatchNumber}"); - - progress.LastLoggedTotal = progress.InsertedTotal; - } + .AppendValue(row.BaseQuant).AppendValue(row.Embeddings).AppendValue(row.LmHead) + .AppendValue(row.AttnQ).AppendValue(row.AttnKV).AppendValue(row.AttnOutput) + .AppendValue(row.FfnUpGate).AppendValue(row.FfnDown).AppendValue(row.MoeExperts) + .AppendValue(row.MoeRouter).EndRow(); } + appender.Close(); } private async Task BuildPredictionContextAsync( @@ -704,19 +577,93 @@ private static void AppendRows( return new BenchmarkRow { SizeBytes = row.b.SizeBytes }; } + private static async Task InsertBaselineCombinationsSqlAsync(DuckDBConnection connection, BaselineQuants baseline, CancellationToken ct) + { + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseline); + if (allowed.Length != 9 || allowed.Any(x => x.IsDefaultOrEmpty)) + throw new InvalidOperationException($"Invalid allowed candidate dimensions for baseline {baseline.Names[0]}."); + await CreateTempDimensionTableAsync(connection, "temp_dim_embeddings", allowed[0], ct); + await CreateTempDimensionTableAsync(connection, "temp_dim_lm_head", allowed[1], ct); + await CreateTempDimensionTableAsync(connection, "temp_dim_attn_q", allowed[2], ct); + await CreateTempDimensionTableAsync(connection, "temp_dim_attn_kv", allowed[3], ct); + await CreateTempDimensionTableAsync(connection, "temp_dim_attn_output", allowed[4], ct); + await CreateTempDimensionTableAsync(connection, "temp_dim_ffn_up_gate", allowed[5], ct); + await CreateTempDimensionTableAsync(connection, "temp_dim_ffn_down", allowed[6], ct); + await CreateTempDimensionTableAsync(connection, "temp_dim_moe_experts", allowed[7], ct); + await CreateTempDimensionTableAsync(connection, "temp_dim_moe_router", allowed[8], ct); + using var cmd = connection.CreateCommand(); + cmd.CommandText = $@" +INSERT INTO {TableName} (BaseQuant,Embeddings,LmHead,AttnQ,AttnKV,AttnOutput,FfnUpGate,FfnDown,MoeExperts,MoeRouter) +SELECT CAST({baseline.UniqueId} AS UTINYINT), e.v, lh.v, aq.v, akv.v, ao.v, fu.v, fd.v, me.v, mr.v +FROM temp_dim_embeddings e +CROSS JOIN temp_dim_lm_head lh +CROSS JOIN temp_dim_attn_q aq +CROSS JOIN temp_dim_attn_kv akv +CROSS JOIN temp_dim_attn_output ao +CROSS JOIN temp_dim_ffn_up_gate fu +CROSS JOIN temp_dim_ffn_down fd +CROSS JOIN temp_dim_moe_experts me +CROSS JOIN temp_dim_moe_router mr;"; + await cmd.ExecuteNonQueryAsync(ct); + return ProductOfDimensionLengths(allowed); + } + + private static async Task BuildPredictedSizeLookupTablesAsync(DuckDBConnection connection, PredictionContext predictionContext, CancellationToken ct) + { + using (var create = connection.CreateCommand()) + { + create.CommandText = @"DROP TABLE IF EXISTS temp_base_predicted_size; +DROP TABLE IF EXISTS temp_group_size_delta; +CREATE TEMP TABLE temp_base_predicted_size (BaseQuant UTINYINT, BaseSizeBytes UBIGINT); +CREATE TEMP TABLE temp_group_size_delta (BaseQuant UTINYINT, GroupName VARCHAR, StoredSlot UTINYINT, DeltaBytes BIGINT);"; + await create.ExecuteNonQueryAsync(ct); + } + string[] groupNames = ["Embeddings","LmHead","AttnQ","AttnKV","AttnOutput","FfnUpGate","FfnDown","MoeExperts","MoeRouter"]; + foreach (var baseline in RuntimeSearchSpace.GetActiveCombinationBaselines()) + { + using (var b = connection.CreateCommand()) + { + b.CommandText = $"INSERT INTO temp_base_predicted_size VALUES ({baseline.UniqueId}, {predictionContext.GetBaseSizeForSql(baseline.UniqueId)});"; + await b.ExecuteNonQueryAsync(ct); + } + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseline); + for (int i = 0; i < groupNames.Length; i++) + foreach (byte slot in allowed[i]) + { + long delta = predictionContext.GetRelativeSizeDeltaForSql((byte)(i + 1), baseline.UniqueId, slot); + using var d = connection.CreateCommand(); + d.CommandText = $"INSERT INTO temp_group_size_delta VALUES ({baseline.UniqueId}, '{groupNames[i]}', {slot}, {delta});"; + await d.ExecuteNonQueryAsync(ct); + } + } + } + private sealed class BenchmarkRow { public ulong SizeBytes { get; set; } } - private sealed class InsertProgress + private static string BuildValuesSql(IReadOnlyList values) => + $"(VALUES {string.Join(", ", values.Select(v => $"({v})"))}) AS t(v)"; + + private static async Task CreateTempDimensionTableAsync(DuckDBConnection connection, string tableName, IReadOnlyList values, CancellationToken ct) + { + if (values.Count == 0) + throw new InvalidOperationException($"Dimension {tableName} had zero candidates."); + using var cmd = connection.CreateCommand(); + cmd.CommandText = $@"DROP TABLE IF EXISTS {tableName}; +CREATE TEMP TABLE {tableName} AS +SELECT CAST(v AS UTINYINT) AS v +FROM {BuildValuesSql(values)};"; + await cmd.ExecuteNonQueryAsync(ct); + } + + private static BigInteger ProductOfDimensionLengths(ImmutableArray[] allowed) { - public long InsertedTotal { get; set; } - public long LastLoggedTotal { get; set; } - public long ProgressLogEveryRows { get; set; } - public long TotalTarget { get; set; } - public string BaseName { get; set; } = string.Empty; - public int BatchNumber { get; set; } + BigInteger product = BigInteger.One; + foreach (var dim in allowed) + product *= dim.Length; + return product; } private sealed class PredictionContext @@ -747,22 +694,16 @@ public PredictionContext( public ulong Predict(TensorConfig config) { - byte normalizedBaseId = NormalizeBaselineIdForIsolation(config.BaseQuant); - long total = (long)(_pureBaselineSizes.TryGetValue(config.BaseQuant, out var directBase) - ? directBase - : _pureBaselineSizes.TryGetValue(normalizedBaseId, out var normalizedBase) - ? normalizedBase - : PureQ8BaseSize); - - ApplyRelativeDelta(TReg.Embeddings.UniqueId, normalizedBaseId, config.Embeddings, ref total); - ApplyRelativeDelta(TReg.LmHead.UniqueId, normalizedBaseId, config.LmHead, ref total); - ApplyRelativeDelta(TReg.AttnQ.UniqueId, normalizedBaseId, config.AttnQ, ref total); - ApplyRelativeDelta(TReg.AttnKV.UniqueId, normalizedBaseId, config.AttnKV, ref total); - ApplyRelativeDelta(TReg.AttnOutput.UniqueId, normalizedBaseId, config.AttnOutput, ref total); - ApplyRelativeDelta(TReg.FfnUpGate.UniqueId, normalizedBaseId, config.FfnUpGate, ref total); - ApplyRelativeDelta(TReg.FfnDown.UniqueId, normalizedBaseId, config.FfnDown, ref total); - ApplyRelativeDelta(TReg.MoeExperts.UniqueId, normalizedBaseId, config.MoeExperts, ref total); - ApplyRelativeDelta(TReg.MoeRouter.UniqueId, normalizedBaseId, config.MoeRouter, ref total); + long total = (long)GetBaseSizeForSql(config.BaseQuant); + total += GetRelativeSizeDeltaForSql(TReg.Embeddings.UniqueId, config.BaseQuant, config.Embeddings); + total += GetRelativeSizeDeltaForSql(TReg.LmHead.UniqueId, config.BaseQuant, config.LmHead); + total += GetRelativeSizeDeltaForSql(TReg.AttnQ.UniqueId, config.BaseQuant, config.AttnQ); + total += GetRelativeSizeDeltaForSql(TReg.AttnKV.UniqueId, config.BaseQuant, config.AttnKV); + total += GetRelativeSizeDeltaForSql(TReg.AttnOutput.UniqueId, config.BaseQuant, config.AttnOutput); + total += GetRelativeSizeDeltaForSql(TReg.FfnUpGate.UniqueId, config.BaseQuant, config.FfnUpGate); + total += GetRelativeSizeDeltaForSql(TReg.FfnDown.UniqueId, config.BaseQuant, config.FfnDown); + total += GetRelativeSizeDeltaForSql(TReg.MoeExperts.UniqueId, config.BaseQuant, config.MoeExperts); + total += GetRelativeSizeDeltaForSql(TReg.MoeRouter.UniqueId, config.BaseQuant, config.MoeRouter); if (total < 0) total = 0; @@ -770,24 +711,32 @@ public ulong Predict(TensorConfig config) return (ulong)total; } - private void ApplyRelativeDelta(byte groupId, byte baseCandidateId, byte candidateId, ref long total) + public ulong GetBaseSizeForSql(byte baseQuant) { - if (BaselineQuants.IsNullTensorConfigGroupSlot(candidateId) || - candidateId == BaselineQuants.BF16_Hybrid.UniqueId || - candidateId == BaselineQuants.F16_Hybrid.UniqueId) - return; - - byte normalizedCandidateId = NormalizeBaselineIdForIsolation(candidateId); - if (normalizedCandidateId == baseCandidateId) - return; + byte normalizedBaseId = NormalizeBaselineIdForIsolation(baseQuant); + return _pureBaselineSizes.TryGetValue(baseQuant, out var directBase) + ? directBase + : _pureBaselineSizes.TryGetValue(normalizedBaseId, out var normalizedBase) + ? normalizedBase + : PureQ8BaseSize; + } + public long GetRelativeSizeDeltaForSql(byte groupId, byte baseQuant, byte storedSlot) + { + if (storedSlot == 0) + return 0; + byte decoded = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedSlot); + if (decoded == BaselineQuants.BF16_Hybrid.UniqueId || decoded == BaselineQuants.F16_Hybrid.UniqueId) + return 0; + byte normalizedBase = NormalizeBaselineIdForIsolation(baseQuant); + byte normalizedCandidateId = NormalizeBaselineIdForIsolation(decoded); + if (normalizedCandidateId == normalizedBase) + return 0; if (!_sizesByGroupAndCandidate.TryGetValue((groupId, normalizedCandidateId), out var candidateSize)) - return; - - if (!_sizesByGroupAndCandidate.TryGetValue((groupId, baseCandidateId), out var baseSize)) - return; - - total += (long)candidateSize - (long)baseSize; + return 0; + if (!_sizesByGroupAndCandidate.TryGetValue((groupId, normalizedBase), out var baseSize)) + return 0; + return (long)candidateSize - (long)baseSize; } private static byte NormalizeBaselineIdForIsolation(byte baselineId) @@ -802,4 +751,4 @@ private static byte NormalizeBaselineIdForIsolation(byte baselineId) return builtIn?.UniqueId ?? baselineId; } } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 629db42..e54e7f7 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -2,6 +2,7 @@ using MagicQuant.Helpers; using MQ.DB; using MQ.DB.Models; +using System.Runtime.CompilerServices; namespace MagicQuant.Services; @@ -27,6 +28,10 @@ public async Task CountAsync(CancellationToken ct = default) public async Task> LoadAllAsync(CancellationToken ct = default) { + long count = await CountAsync(ct); + if (count > Config.MaxInMemoryCombinationLoadRows) + throw new InvalidOperationException($"Refusing to load {count:N0} DuckDB tensor configs into memory. Use SQL-native filtering/streaming instead."); + using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); await ConfigureFastLoadSessionAsync(connection, ct); @@ -58,6 +63,32 @@ public async Task> LoadAllAsync(CancellationToken ct = defaul return results; } + public async IAsyncEnumerable StreamAsync( + string? whereSql = null, + string? orderBySql = null, + long? limit = null, + [EnumeratorCancellation] CancellationToken ct = default) + { + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); + string sql = $@"SELECT BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter FROM {TableName}"; + if (!string.IsNullOrWhiteSpace(whereSql)) sql += $" WHERE {whereSql}"; + if (!string.IsNullOrWhiteSpace(orderBySql)) sql += $" ORDER BY {orderBySql}"; + if (limit.HasValue) sql += $" LIMIT {limit.Value}"; + using var cmd = connection.CreateCommand(); + cmd.CommandText = sql; + using var reader = await cmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + { + yield return new TensorConfig( + Convert.ToByte(reader.GetValue(0)), Convert.ToByte(reader.GetValue(1)), Convert.ToByte(reader.GetValue(2)), + Convert.ToByte(reader.GetValue(3)), Convert.ToByte(reader.GetValue(4)), Convert.ToByte(reader.GetValue(5)), + Convert.ToByte(reader.GetValue(6)), Convert.ToByte(reader.GetValue(7)), Convert.ToByte(reader.GetValue(8)), + Convert.ToByte(reader.GetValue(9))); + } + } + public async Task ReplaceAllAsync(IReadOnlyCollection configs, string reason, CancellationToken ct = default) { using var connection = new DuckDBConnection(ConnectionString); From 2b4326d40535378349ff46a10d2b01dc5612ae10 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 29 Apr 2026 13:04:07 -0400 Subject: [PATCH 166/258] Fix guards and CTAS safety for DuckDB pruning --- .../Services/CombinationSurvivalPipelineService.cs | 1 + MagicQuant/Services/QuantDatabaseService.cs | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index be3e58d..747da6b 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -52,6 +52,7 @@ public async Task RunAsync(CancellationToken AnsiConsole.Write(new Rule("[yellow]Rank-Safe Prediction / Hybrid Selection Pipeline[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"[green]Remaining DuckDB combinations available to score:[/] [cyan]{report.StartingCount:N0}[/]"); AnsiConsole.MarkupLine("[grey]Old MDA bucket survival is disabled. DuckDB now defines the allowed search space; rank-safe isolation prediction selects what deserves real benchmarking.[/]"); + AnsiConsole.MarkupLine("[grey]Note: final prediction/selection is currently guarded for small in-memory runs only; trillion-scale support requires DuckDB-backed prediction materialization + projection.[/]"); if (report.StartingCount > Config.MaxInMemoryCombinationLoadRows) throw new InvalidOperationException($"Final prediction selection still requires DuckDB-backed prediction materialization. Refusing to load {report.StartingCount:N0} combinations into memory."); diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 87f0c16..c0aed24 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -84,13 +84,12 @@ public async Task GetRemainingCombinationCountAsync(CancellationToken ct = public async Task> GetRemainingTensorConfigsAsync(CancellationToken ct = default) { - long count = await GetRemainingCombinationCountAsync(ct); - if (count > Config.MaxInMemoryCombinationLoadRows) - throw new InvalidOperationException($"Refusing to load {count:N0} DuckDB tensor configs into memory. Use SQL-native filtering/streaming instead."); - using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); await ConfigureFastLoadSessionAsync(connection, ct); + long count = await GetRowCountAsync(connection, ct); + if (count > Config.MaxInMemoryCombinationLoadRows) + throw new InvalidOperationException($"Refusing to load {count:N0} DuckDB tensor configs into memory. Use SQL-native filtering/streaming instead."); var results = new List(); @@ -239,6 +238,7 @@ public async Task PrunePredictedLargerThanQ8Async( using (var pruneCmd = connection.CreateCommand()) { pruneCmd.CommandText = $@" +DROP TABLE IF EXISTS tensor_configs_pruned; CREATE TABLE tensor_configs_pruned AS SELECT t.* FROM {TableName} t @@ -288,6 +288,7 @@ public async Task PruneHighPrecisionHybridCandidatesAsync(CancellationToke using (var cmd = connection.CreateCommand()) { cmd.CommandText = $@" +DROP TABLE IF EXISTS tensor_configs_pruned; CREATE TABLE tensor_configs_pruned AS SELECT * FROM {TableName} @@ -580,7 +581,7 @@ private async Task BulkAppendAsync( private static async Task InsertBaselineCombinationsSqlAsync(DuckDBConnection connection, BaselineQuants baseline, CancellationToken ct) { var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseline); - if (allowed.Length != 9 || allowed.Any(x => x.IsDefaultOrEmpty)) + if (allowed.Length != 9 || allowed.Any(x => x == null || x.Length == 0)) throw new InvalidOperationException($"Invalid allowed candidate dimensions for baseline {baseline.Names[0]}."); await CreateTempDimensionTableAsync(connection, "temp_dim_embeddings", allowed[0], ct); await CreateTempDimensionTableAsync(connection, "temp_dim_lm_head", allowed[1], ct); @@ -658,7 +659,7 @@ SELECT CAST(v AS UTINYINT) AS v await cmd.ExecuteNonQueryAsync(ct); } - private static BigInteger ProductOfDimensionLengths(ImmutableArray[] allowed) + private static BigInteger ProductOfDimensionLengths(ImmutableArray allowed) { BigInteger product = BigInteger.One; foreach (var dim in allowed) From 402e74caf5602ca362d26f28d89297baccd4cbf9 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 29 Apr 2026 15:13:10 -0400 Subject: [PATCH 167/258] big upgrade to speed --- MagicQuant/Services/QuantDatabaseService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index c0aed24..3d1780e 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -1,3 +1,4 @@ +using System.Collections.Immutable; using System.Diagnostics; using System.Numerics; using DuckDB.NET.Data; From 543a037b611863c18016a25ed23dd0866247416b Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 29 Apr 2026 18:36:48 -0400 Subject: [PATCH 168/258] Handle mmproj sidecars early and make export non-fatal --- MagicQuant/Commands/CloneRepositoryQuants.cs | 4 + MagicQuant/Commands/Evolution.cs | 3 + MagicQuant/Config.cs | 2 + .../Configuration/MagicQuantYamlConfig.cs | 2 + .../CombinationSurvivalPipelineService.cs | 5 +- .../Services/HybridArtifactExportService.cs | 64 +---- .../Services/ModelSidecarArtifactService.cs | 242 ++++++++++++++++++ 7 files changed, 271 insertions(+), 51 deletions(-) create mode 100644 MagicQuant/Services/ModelSidecarArtifactService.cs diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 17a5451..1e33d21 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -103,6 +103,9 @@ public async Task Run(List args) string baseModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); + var sidecarService = new ModelSidecarArtifactService(pyManager); + await sidecarService.EnsureMmprojArtifactAvailableAsync(); + var architectureFamilyService = new ArchitectureFamilyService(pyManager); await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(baseModelGgufPath); @@ -193,6 +196,7 @@ await quantizationService.BuildExportArtifactFromExactTensorMapAsync( await CopyModelAdjacentFilesAsync(Cache.OutputDirectory!); await CopyImatrixArtifactsAsync(Cache.OutputDirectory!); + await sidecarService.CopyMmprojArtifactsAsync(Cache.OutputDirectory!); await new CloneReadmeGenerationService().GenerateAsync( Cache.OutputDirectory!, diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 28cdb56..aa871a4 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -119,6 +119,9 @@ public async Task Run(List args) string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); + var sidecarService = new ModelSidecarArtifactService(pyManager); + await sidecarService.EnsureMmprojArtifactAvailableAsync(); + var architectureFamilyService = new ArchitectureFamilyService(pyManager); await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(bf16ModelGgufPath); diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index be1af0d..3179171 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -74,6 +74,8 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.Output.ExportExternalLearnedBaselines; + public static bool AttemptMmprojBuild => Current.Output.AttemptMmprojBuild; + public static bool RequireMmprojForVisionModels => Current.Output.RequireMmprojForVisionModels; public static int MaxSelectedChoicesPerBucket => Math.Max(1, Current.Survival.MaxSelectedChoicesPerBucket); public static double SurvivalMeaningfulSizeBiasPercent => Current.Survival.MeaningfulSizeBiasPercent; diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 61806bf..7d355aa 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -169,6 +169,8 @@ public sealed class RuntimeOutputConfig public string? OutputDir { get; set; } public string OutputNamePrefix { get; set; } = "Model"; public bool ExportExternalLearnedBaselines { get; set; } = false; + public bool AttemptMmprojBuild { get; set; } = true; + public bool RequireMmprojForVisionModels { get; set; } = false; } public sealed class RuntimeSurvivalConfig diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index 747da6b..ad35f35 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -1,3 +1,4 @@ +using MagicQuant.Helpers; using MagicQuant.Models; using MQ.DB; using MQ.DB.Models; @@ -33,7 +34,9 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic _finalEliminator = new FinalRealBenchmarkEliminationService(); _selectionEngine = new PredictionGuidedHybridSelectionService(_quantizationService, _benchmarkRepository, _finalEliminator); _selectionCli = new FinalSurvivorSelectionCliService(); - _exportService = new HybridArtifactExportService(_quantizationService, _effectiveResolver); + var pyManager = new PythonManager(Cache.MagicQuantDirectory!); + var sidecarService = new ModelSidecarArtifactService(pyManager); + _exportService = new HybridArtifactExportService(_quantizationService, _effectiveResolver, sidecarService); _readmeService = new ReadmeGenerationService(); _hybridMapService = new HybridMapGenerationService(); _diagnosticsLogService = new SelectionDiagnosticsLogService(); diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index a19fc1f..1a2738f 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -26,14 +26,17 @@ public sealed class HybridArtifactExportService private readonly QuantizationService _quantizationService; private readonly EffectiveCandidateStateResolverService _effectiveResolver; private readonly FinalArtifactNamingService _namingService; + private readonly ModelSidecarArtifactService _sidecarService; public HybridArtifactExportService( QuantizationService quantizationService, - EffectiveCandidateStateResolverService effectiveResolver) + EffectiveCandidateStateResolverService effectiveResolver, + ModelSidecarArtifactService sidecarService) { _quantizationService = quantizationService; _effectiveResolver = effectiveResolver; _namingService = new FinalArtifactNamingService(); + _sidecarService = sidecarService; } public async Task> ExportAsync( @@ -149,10 +152,16 @@ public async Task> ExportAsync( await Task.WhenAll(buildTasks); - await CopyModelAdjacentFilesAsync(Cache.OutputDirectory!, ct); - await CopyImatrixArtifactsAsync(Cache.OutputDirectory!, ct); - await CopyMmprojArtifactsAsync(Cache.OutputDirectory!, ct); - await CleanExportSidecarsAsync(Cache.OutputDirectory!, ct); + try + { + await CopyModelAdjacentFilesAsync(Cache.OutputDirectory!, ct); + await CopyImatrixArtifactsAsync(Cache.OutputDirectory!, ct); + await _sidecarService.CopyMmprojArtifactsAsync(Cache.OutputDirectory!, ct); + } + finally + { + await CleanExportSidecarsAsync(Cache.OutputDirectory!, ct); + } return output; } @@ -270,49 +279,4 @@ private static Task CopyImatrixArtifactsAsync(string outputDirectory, Cancellati return Task.CompletedTask; } - private static Task CopyMmprojArtifactsAsync(string outputDirectory, CancellationToken ct) - { - var searchRoots = new List(); - if (!string.IsNullOrWhiteSpace(Cache.ModelDirectory)) - searchRoots.Add(Cache.ModelDirectory!); - if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) - searchRoots.Add(Cache.ModelMagicQuantDirectory!); - - foreach (var root in searchRoots.Distinct(StringComparer.OrdinalIgnoreCase)) - { - var mmproj = Directory.EnumerateFiles(root, "*mmproj*.gguf", SearchOption.AllDirectories).FirstOrDefault(); - if (mmproj == null) - continue; - - string target = Path.Combine(outputDirectory, Path.GetFileName(mmproj)); - File.Copy(mmproj, target, overwrite: true); - AnsiConsole.MarkupLine($"[green]Copied mmproj artifact:[/] {Markup.Escape(target)}"); - return Task.CompletedTask; - } - - if (!LooksVisionCapableModel()) - { - AnsiConsole.MarkupLine("[grey]No mmproj artifact was present, but no vision capability hints were detected. Continuing.[/]"); - return Task.CompletedTask; - } - - throw new InvalidOperationException( - "This model appears to be vision-capable, but no mmproj GGUF could be found in the working/source artifacts."); - } - - private static bool LooksVisionCapableModel() - { - if (string.IsNullOrWhiteSpace(Cache.ModelDirectory)) - return false; - - string configPath = Path.Combine(Cache.ModelDirectory!, "config.json"); - if (!File.Exists(configPath)) - return false; - - string json = File.ReadAllText(configPath); - return json.Contains("vision_config", StringComparison.OrdinalIgnoreCase) || - json.Contains("vision_tower", StringComparison.OrdinalIgnoreCase) || - json.Contains("mm_vision_tower", StringComparison.OrdinalIgnoreCase) || - json.Contains("projector", StringComparison.OrdinalIgnoreCase); - } } diff --git a/MagicQuant/Services/ModelSidecarArtifactService.cs b/MagicQuant/Services/ModelSidecarArtifactService.cs new file mode 100644 index 0000000..193961d --- /dev/null +++ b/MagicQuant/Services/ModelSidecarArtifactService.cs @@ -0,0 +1,242 @@ +using System.Diagnostics; +using System.Text.Json; +using MagicQuant.Helpers; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed record VisionCapabilityDetection( + bool IsLikelyVisionCapable, + bool IsStrongSignal, + List Reasons, + List Warnings); + +public sealed record MmprojArtifactResult +{ + public bool IsVisionCapable { get; init; } + public bool ExistingFound { get; init; } + public bool Built { get; init; } + public bool Copied { get; init; } + public string? SourcePath { get; init; } + public string? OutputPath { get; init; } + public List Warnings { get; init; } = new(); +} + +public sealed class ModelSidecarArtifactService +{ + private static readonly string[] MultimodalTokens = + [ + "llava", "qwen2_vl", "qwen2_5_vl", "qwen3_vl", "gemma3", "internvl", "minicpm", "phi4mm", "glmv", "mllama", "idefics", "florence", "paligemma" + ]; + + private readonly PythonManager _pythonManager; + + public ModelSidecarArtifactService(PythonManager pythonManager) + { + _pythonManager = pythonManager; + } + + public async Task EnsureMmprojArtifactAvailableAsync(CancellationToken ct = default) + { + var detection = DetectVisionCapability(); + var warnings = new List(detection.Warnings); + string? existing = FindExistingMmprojArtifact(); + if (existing != null) + { + AnsiConsole.MarkupLine($"[green]mmproj artifact ready:[/] {Markup.Escape(existing)}"); + return new MmprojArtifactResult { IsVisionCapable = detection.IsLikelyVisionCapable, ExistingFound = true, SourcePath = existing, Warnings = warnings }; + } + + if (!detection.IsLikelyVisionCapable) + { + AnsiConsole.MarkupLine("[grey]No mmproj artifact present and no strong vision capability hints detected. Continuing.[/]"); + return new MmprojArtifactResult { IsVisionCapable = false, Warnings = warnings }; + } + + if (!Config.AttemptMmprojBuild || !detection.IsStrongSignal) + { + string warning = "Model has vision-capability hints, but no mmproj GGUF was found or generated. Continuing without mmproj sidecar. Vision inference may require a separate --mmproj file."; + warnings.Add(warning); + AnsiConsole.MarkupLine($"[yellow]{Markup.Escape(warning)}[/]"); + return HandleStrictRequirement(new MmprojArtifactResult { IsVisionCapable = true, Warnings = warnings }, warning); + } + + var built = await BuildMmprojArtifactAsync(warnings, ct); + if (!built.Built) + return HandleStrictRequirement(built, warnings.LastOrDefault() ?? "mmproj build failed."); + + return built; + } + + public async Task CopyMmprojArtifactsAsync(string outputDirectory, CancellationToken ct = default) + { + var detection = DetectVisionCapability(); + var warnings = new List(detection.Warnings); + string? source = FindExistingMmprojArtifact(); + if (source == null) + { + if (detection.IsLikelyVisionCapable) + { + string warning = "Model has vision-capability hints, but no mmproj GGUF was found or generated. Continuing without mmproj sidecar. Vision inference may require a separate --mmproj file."; + warnings.Add(warning); + AnsiConsole.MarkupLine($"[yellow]{Markup.Escape(warning)}[/]"); + } + else + { + AnsiConsole.MarkupLine("[grey]No mmproj artifact present and no strong vision capability hints detected. Continuing.[/]"); + } + + return new MmprojArtifactResult { IsVisionCapable = detection.IsLikelyVisionCapable, Warnings = warnings }; + } + + Directory.CreateDirectory(outputDirectory); + string target = Path.Combine(outputDirectory, Path.GetFileName(source)); + File.Copy(source, target, overwrite: true); + await Task.Yield(); + AnsiConsole.MarkupLine($"[green]Copied mmproj artifact:[/] {Markup.Escape(target)}"); + + return new MmprojArtifactResult { IsVisionCapable = detection.IsLikelyVisionCapable, ExistingFound = true, Copied = true, SourcePath = source, OutputPath = target, Warnings = warnings }; + } + + public string? FindExistingMmprojArtifact() + { + if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory) || string.IsNullOrWhiteSpace(Cache.ModelDirectory)) + return null; + + var roots = new[] + { + Cache.ModelDirectory!, + Cache.ModelMagicQuantDirectory!, + Path.Combine(Cache.ModelMagicQuantDirectory!, "Sidecars"), + Path.Combine(Cache.ModelMagicQuantDirectory!, "GGUF") + }; + + foreach (var root in roots.Distinct(StringComparer.OrdinalIgnoreCase)) + { + if (!Directory.Exists(root)) + continue; + + var found = Directory.EnumerateFiles(root, "*mmproj*.gguf", SearchOption.TopDirectoryOnly) + .FirstOrDefault(path => new FileInfo(path).Length > 0); + if (found != null) + return found; + } + + return null; + } + + public VisionCapabilityDetection DetectVisionCapability() + { + var reasons = new List(); + var warnings = new List(); + if (string.IsNullOrWhiteSpace(Cache.ModelDirectory)) + return new VisionCapabilityDetection(false, false, reasons, warnings); + + string configPath = Path.Combine(Cache.ModelDirectory!, "config.json"); + if (!File.Exists(configPath)) + { + warnings.Add("config.json not found; unable to evaluate vision capability hints."); + return new VisionCapabilityDetection(false, false, reasons, warnings); + } + + try + { + using var doc = JsonDocument.Parse(File.ReadAllText(configPath)); + var root = doc.RootElement; + string[] strongKeys = ["vision_config", "vision_tower", "mm_vision_tower", "visual", "image_token_id", "video_token_id", "vision_start_token_id", "vision_end_token_id"]; + foreach (var key in strongKeys) + { + if (root.TryGetProperty(key, out _)) + reasons.Add($"config has '{key}'"); + } + + if (root.TryGetProperty("model_type", out var modelType) && modelType.ValueKind == JsonValueKind.String) + { + string value = modelType.GetString() ?? string.Empty; + if (MultimodalTokens.Any(t => value.Contains(t, StringComparison.OrdinalIgnoreCase))) + reasons.Add($"model_type suggests multimodal: {value}"); + } + + if (root.TryGetProperty("architectures", out var archs) && archs.ValueKind == JsonValueKind.Array) + { + foreach (var arch in archs.EnumerateArray()) + { + var value = arch.GetString() ?? string.Empty; + if (MultimodalTokens.Any(t => value.Contains(t, StringComparison.OrdinalIgnoreCase))) + reasons.Add($"architecture suggests multimodal: {value}"); + } + } + } + catch (Exception ex) + { + warnings.Add($"Failed to parse config.json for vision capability hints: {ex.Message}"); + return new VisionCapabilityDetection(false, false, reasons, warnings); + } + + bool likely = reasons.Count > 0; + bool strong = reasons.Any(r => r.Contains("config has", StringComparison.OrdinalIgnoreCase)); + return new VisionCapabilityDetection(likely, strong, reasons, warnings); + } + + private async Task BuildMmprojArtifactAsync(List warnings, CancellationToken ct) + { + string sidecarDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Sidecars"); + Directory.CreateDirectory(sidecarDir); + string safeName = new DirectoryInfo(Cache.ModelDirectory!).Name.Replace(' ', '-'); + string targetPath = Path.Combine(sidecarDir, $"mmproj-{safeName}-f16.gguf"); + string successPath = targetPath + ".success.json"; + string logPath = targetPath + ".convert.log"; + + if (File.Exists(targetPath) && new FileInfo(targetPath).Length > 0 && File.Exists(successPath)) + { + AnsiConsole.MarkupLine($"[green]mmproj artifact ready:[/] {Markup.Escape(targetPath)}"); + return new MmprojArtifactResult { IsVisionCapable = true, ExistingFound = true, SourcePath = targetPath, Warnings = warnings }; + } + + var psi = new ProcessStartInfo + { + FileName = _pythonManager.GetPythonExecutable(), + WorkingDirectory = Cache.LlamaRoot, + RedirectStandardError = true, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + }; + psi.ArgumentList.Add("convert_hf_to_gguf.py"); + psi.ArgumentList.Add(Cache.ModelDirectory!); + psi.ArgumentList.Add("--mmproj"); + psi.ArgumentList.Add("--outtype"); + psi.ArgumentList.Add("f16"); + psi.ArgumentList.Add("--outfile"); + psi.ArgumentList.Add(targetPath); + + using var proc = Process.Start(psi) ?? throw new InvalidOperationException("Failed to start mmproj conversion process."); + string stdout = await proc.StandardOutput.ReadToEndAsync(); + string stderr = await proc.StandardError.ReadToEndAsync(); + await proc.WaitForExitAsync(ct); + await File.WriteAllTextAsync(logPath, stdout + Environment.NewLine + stderr, ct); + + if (proc.ExitCode != 0 || !File.Exists(targetPath) || new FileInfo(targetPath).Length == 0) + { + if (File.Exists(targetPath)) + await HardDeleteHelper.DeleteFileIfExistsAsync(targetPath); + + string warning = $"Model has vision-capability hints, but no mmproj GGUF was found or generated. Continuing without mmproj sidecar. Vision inference may require a separate --mmproj file. Log: {logPath}"; + warnings.Add(warning); + AnsiConsole.MarkupLine($"[yellow]{Markup.Escape(warning)}[/]"); + return new MmprojArtifactResult { IsVisionCapable = true, Warnings = warnings }; + } + + await File.WriteAllTextAsync(successPath, "{\"status\":\"success\"}", ct); + AnsiConsole.MarkupLine($"[green]mmproj artifact ready:[/] {Markup.Escape(targetPath)}"); + return new MmprojArtifactResult { IsVisionCapable = true, Built = true, SourcePath = targetPath, Warnings = warnings }; + } + + private static MmprojArtifactResult HandleStrictRequirement(MmprojArtifactResult result, string message) + { + if (Config.RequireMmprojForVisionModels && result.IsVisionCapable && !result.ExistingFound && !result.Built) + throw new InvalidOperationException(message); + + return result; + } +} From 3f7186d2d9613239706d1ce934915affa13fb6ba Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 29 Apr 2026 20:08:13 -0400 Subject: [PATCH 169/258] df --- MagicQuant/Services/ModelSidecarArtifactService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/MagicQuant/Services/ModelSidecarArtifactService.cs b/MagicQuant/Services/ModelSidecarArtifactService.cs index 193961d..2c6c1e6 100644 --- a/MagicQuant/Services/ModelSidecarArtifactService.cs +++ b/MagicQuant/Services/ModelSidecarArtifactService.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Text.Json; using MagicQuant.Helpers; +using MQ.DB; using Spectre.Console; namespace MagicQuant.Services; From 332bb707bbf4e6a819a2e67d3e5cb6ee83b05d37 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Wed, 29 Apr 2026 20:13:16 -0400 Subject: [PATCH 170/258] Fix public artifact short names in README and release metadata --- .../Services/FinalArtifactNamingService.cs | 100 ++++++++++++++++++ .../Services/FinalReleaseMetadataService.cs | 54 ++++++++-- .../Services/ReadmeGenerationService.cs | 28 ++++- 3 files changed, 170 insertions(+), 12 deletions(-) diff --git a/MagicQuant/Services/FinalArtifactNamingService.cs b/MagicQuant/Services/FinalArtifactNamingService.cs index 8eba0cc..486a777 100644 --- a/MagicQuant/Services/FinalArtifactNamingService.cs +++ b/MagicQuant/Services/FinalArtifactNamingService.cs @@ -113,6 +113,32 @@ public string ToShortDisplayName(string displayNameOrFileName) return value; } + public string ToPublicArtifactShortName( + string? displayNameOrFileName, + string? fileName = null, + string? providerName = null, + string? quantFamily = null, + BenchmarkSnapshotRecord? snapshot = null, + FinalArtifactNamingContext? context = null) + { + string prefix = ResolveModelPrefix(); + string? preferred = !string.IsNullOrWhiteSpace(fileName) ? fileName : displayNameOrFileName; + + if (!string.IsNullOrWhiteSpace(preferred)) + { + string candidate = Path.GetFileNameWithoutExtension(preferred.Trim()); + string fullPrefix = prefix + "-"; + + if (candidate.StartsWith(fullPrefix, StringComparison.OrdinalIgnoreCase)) + candidate = candidate[fullPrefix.Length..]; + + if (!LooksLikeModelOnlyLabel(candidate, prefix)) + return candidate; + } + + return BuildProviderQuantFallback(fileName, providerName, quantFamily, snapshot, context); + } + public IReadOnlyList BuildProviderCredits( IReadOnlyCollection artifacts) { @@ -336,6 +362,80 @@ public static string SanitizeToken(string value) } private static ulong Distance(ulong left, ulong right) => left >= right ? left - right : right - left; + + private static bool LooksLikeModelOnlyLabel(string value, string modelPrefix) + { + if (string.IsNullOrWhiteSpace(value)) + return true; + + string normalized = value.Trim(); + if (string.Equals(normalized, modelPrefix, StringComparison.OrdinalIgnoreCase)) + return true; + + string alnum = new(normalized.Where(char.IsLetterOrDigit).ToArray()); + if (string.IsNullOrWhiteSpace(alnum)) + return true; + + return Regex.IsMatch(alnum, @"^[A-Za-z]+\d*(?:\d)?$", RegexOptions.IgnoreCase); + } + + private string BuildProviderQuantFallback( + string? fileName, + string? providerName, + string? quantFamily, + BenchmarkSnapshotRecord? snapshot, + FinalArtifactNamingContext? context) + { + string resolvedProvider = providerName ?? (snapshot != null + ? (snapshot.IsHybrid ? "MagicQuant" : HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false)) + : string.Empty); + + string resolvedFamily = quantFamily; + if (string.IsNullOrWhiteSpace(resolvedFamily) && snapshot != null) + resolvedFamily = snapshot.BaselineFamily; + if (string.IsNullOrWhiteSpace(resolvedFamily) && snapshot != null && context != null) + resolvedFamily = ResolveHybridRangeFamily(snapshot, context); + + string sanitizedFamily = SanitizeToken(resolvedFamily ?? string.Empty); + if (string.IsNullOrWhiteSpace(sanitizedFamily)) + return string.Empty; + + if (snapshot?.IsHybrid == true) + { + string ordinal = ExtractOrdinalFromFileName(fileName); + string baseName = sanitizedFamily.StartsWith("MQ-", StringComparison.OrdinalIgnoreCase) + ? sanitizedFamily + : $"MQ-{sanitizedFamily}"; + return string.IsNullOrWhiteSpace(ordinal) ? baseName : $"{baseName}_{ordinal}"; + } + + if (string.Equals(resolvedProvider, "MagicQuant", StringComparison.OrdinalIgnoreCase)) + return sanitizedFamily.StartsWith("MQ-", StringComparison.OrdinalIgnoreCase) ? sanitizedFamily : $"MQ-{sanitizedFamily}"; + + if (string.Equals(resolvedProvider, "llama.cpp", StringComparison.OrdinalIgnoreCase)) + return sanitizedFamily.StartsWith("LM-", StringComparison.OrdinalIgnoreCase) ? sanitizedFamily : $"LM-{sanitizedFamily}"; + + if (string.Equals(resolvedProvider, "Unsloth", StringComparison.OrdinalIgnoreCase)) + { + if (sanitizedFamily.StartsWith("UD-", StringComparison.OrdinalIgnoreCase) || + sanitizedFamily.StartsWith("Unsloth", StringComparison.OrdinalIgnoreCase)) + return sanitizedFamily; + + return $"UD-{sanitizedFamily}"; + } + + return sanitizedFamily; + } + + private static string ExtractOrdinalFromFileName(string? fileName) + { + if (string.IsNullOrWhiteSpace(fileName)) + return string.Empty; + + string stem = Path.GetFileNameWithoutExtension(fileName.Trim()); + var match = Regex.Match(stem, @"_(\d+)$", RegexOptions.CultureInvariant); + return match.Success ? match.Groups[1].Value : string.Empty; + } } public sealed class FinalArtifactNamingContext diff --git a/MagicQuant/Services/FinalReleaseMetadataService.cs b/MagicQuant/Services/FinalReleaseMetadataService.cs index ea44a5c..9975af8 100644 --- a/MagicQuant/Services/FinalReleaseMetadataService.cs +++ b/MagicQuant/Services/FinalReleaseMetadataService.cs @@ -39,7 +39,7 @@ public async Task GenerateAsync( var survivors = exportedArtifacts .OrderBy(x => x.Snapshot.Kld) .ThenBy(x => x.Snapshot.SizeBytes) - .Select(x => ToSurvivorJson(x, referencePpl, replacementMap)) + .Select(x => ToSurvivorJson(x, referencePpl, replacementMap, namingContext)) .ToList(); await File.WriteAllTextAsync(finalPath, JsonSerializer.Serialize(survivors, JsonOptions), ct); @@ -59,14 +59,15 @@ public async Task GenerateAsync( private object ToSurvivorJson( ExportedArtifactRecord artifact, double? referencePpl, - IReadOnlyDictionary> replacementMap) + IReadOnlyDictionary> replacementMap, + FinalArtifactNamingContext namingContext) { string key = TensorConfigIdentity.ToKey(artifact.Snapshot.Config); var replacements = ResolveTransitiveReplacements(key, replacementMap) .Select(x => new { key = TensorConfigIdentity.ToKey(x.Eliminated.Config), - shortName = _namingService.ToShortDisplayName(x.Eliminated.DisplayName), + shortName = ToSnapshotShortName(x.Eliminated, null, namingContext), internalDisplayName = x.Eliminated.DisplayName, kld = x.Eliminated.Kld, ppl = x.Eliminated.Ppl, @@ -85,7 +86,13 @@ private object ToSurvivorJson( ? EnsureGgufExtension(artifact.DisplayName) : artifact.FileName, displayName = artifact.DisplayName, - shortName = _namingService.ToShortDisplayName(artifact.DisplayName), + shortName = _namingService.ToPublicArtifactShortName( + artifact.DisplayName, + artifact.FileName, + artifact.ProviderName, + artifact.BaselineFamily, + artifact.Snapshot, + namingContext), provider = artifact.ProviderName, quantFamily = artifact.BaselineFamily, isHybrid = artifact.Snapshot.IsHybrid, @@ -153,7 +160,13 @@ private object ToReplacementSideJson( if (exportedByKey.TryGetValue(key, out var artifact)) { displayName = artifact.DisplayName; - shortName = _namingService.ToShortDisplayName(artifact.DisplayName); + shortName = _namingService.ToPublicArtifactShortName( + artifact.DisplayName, + artifact.FileName, + artifact.ProviderName, + artifact.BaselineFamily, + artifact.Snapshot, + namingContext); fileName = artifact.IsExternalReference ? EnsureGgufExtension(artifact.DisplayName) : artifact.FileName ?? EnsureGgufExtension(artifact.DisplayName); provider = artifact.ProviderName; quantFamily = artifact.BaselineFamily; @@ -161,10 +174,16 @@ private object ToReplacementSideJson( else { displayName = _namingService.BuildDisplayLabel(snapshot, namingContext); - shortName = _namingService.ToShortDisplayName(displayName); - fileName = EnsureGgufExtension(displayName); provider = snapshot.IsHybrid ? "MagicQuant" : HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); quantFamily = snapshot.BaselineFamily; + shortName = _namingService.ToPublicArtifactShortName( + displayName, + null, + provider, + quantFamily, + snapshot, + namingContext); + fileName = EnsureGgufExtension(displayName); } return new @@ -264,5 +283,26 @@ private static string EnsureGgufExtension(string value) : value + ".gguf"; } + private string ToSnapshotShortName( + BenchmarkSnapshotRecord snapshot, + ExportedArtifactRecord? artifact, + FinalArtifactNamingContext namingContext) + { + if (artifact != null) + { + return _namingService.ToPublicArtifactShortName( + artifact.DisplayName, + artifact.FileName, + artifact.ProviderName, + artifact.BaselineFamily, + artifact.Snapshot, + namingContext); + } + + string provider = snapshot.IsHybrid ? "MagicQuant" : HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); + string display = _namingService.BuildDisplayLabel(snapshot, namingContext); + return _namingService.ToPublicArtifactShortName(display, null, provider, snapshot.BaselineFamily, snapshot, namingContext); + } + private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; } diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 0ea82b5..8c9d416 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -91,7 +91,13 @@ private void AppendDownloadTable( foreach (var artifact in artifacts.OrderBy(x => x.Snapshot.Kld).ThenBy(x => x.Snapshot.SizeBytes)) { string key = TensorConfigIdentity.ToKey(artifact.Snapshot.Config); - string shortName = _namingService.ToShortDisplayName(artifact.DisplayName); + string shortName = _namingService.ToPublicArtifactShortName( + artifact.DisplayName, + artifact.FileName, + artifact.ProviderName, + artifact.BaselineFamily, + artifact.Snapshot, + namingContext); var replacements = FinalReleaseMetadataService.ResolveTransitiveReplacements(key, replacementMap); string nameCell = BuildNameCell(shortName, replacements, exportedByKey, namingContext); string sizeGb = ToGb(artifact.Snapshot.SizeBytes); @@ -138,9 +144,21 @@ private string GetPublicShortName( { string key = TensorConfigIdentity.ToKey(snapshot.Config); if (exportedByKey.TryGetValue(key, out var artifact)) - return _namingService.ToShortDisplayName(artifact.DisplayName); - - return _namingService.ToShortDisplayName(_namingService.BuildDisplayLabel(snapshot, namingContext)); + return _namingService.ToPublicArtifactShortName( + artifact.DisplayName, + artifact.FileName, + artifact.ProviderName, + artifact.BaselineFamily, + artifact.Snapshot, + namingContext); + + return _namingService.ToPublicArtifactShortName( + _namingService.BuildDisplayLabel(snapshot, namingContext), + null, + snapshot.IsHybrid ? "MagicQuant" : HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false), + snapshot.BaselineFamily, + snapshot, + namingContext); } private static void AppendReasonCodeDetails(StringBuilder sb) @@ -199,4 +217,4 @@ private static void AppendCollapsible(StringBuilder sb, string summary, string b private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); private static string EscapeTooltip(string value) => (value ?? string.Empty).Replace("\"", """).Replace("|", " "); private static string EscapeHtml(string value) => (value ?? string.Empty).Replace("&", "&").Replace("<", "<").Replace(">", ">"); -} \ No newline at end of file +} From d0a4574685cdd7516cf8013d3528de2983a55c1c Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 30 Apr 2026 12:23:33 -0400 Subject: [PATCH 171/258] fixed naming scheme system for readme --- MagicQuant/Services/FinalArtifactNamingService.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/MagicQuant/Services/FinalArtifactNamingService.cs b/MagicQuant/Services/FinalArtifactNamingService.cs index 486a777..8414f93 100644 --- a/MagicQuant/Services/FinalArtifactNamingService.cs +++ b/MagicQuant/Services/FinalArtifactNamingService.cs @@ -103,7 +103,17 @@ public string ToShortDisplayName(string displayNameOrFileName) if (string.IsNullOrWhiteSpace(displayNameOrFileName)) return string.Empty; - string value = Path.GetFileNameWithoutExtension(displayNameOrFileName.Trim()); + string value = displayNameOrFileName.Trim(); + + // Only strip directories. Do NOT blindly call GetFileNameWithoutExtension on + // extensionless display names like Qwen3.6-35B-A3B-LM-Q8_0, because .NET will + // treat ".6-35B-A3B-LM-Q8_0" as the extension and return only "Qwen3". + value = Path.GetFileName(value); + + // Only remove the extension when it is a real GGUF artifact filename. + if (value.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)) + value = value[..^".gguf".Length]; + string prefix = ResolveModelPrefix(); string fullPrefix = prefix + "-"; From 81aa36c401fecba49e48f2ec6e6f31d008414d14 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Thu, 30 Apr 2026 13:10:44 -0400 Subject: [PATCH 172/258] Defer DuckDB build until post-isolation pruning --- MagicQuant/Commands/Evolution.cs | 4 +--- .../Services/ModelCompatibilityService.cs | 23 +++++++------------ 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index aa871a4..3b74fb4 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -212,9 +212,6 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( CliHelpers.ValidateCombinationLogicWorks(true); - var dbService = new QuantDatabaseService(); - await dbService.InitializeAsync(); - var comboCountBefore = ComboCounter.CountAll(); var totalLearnedPruningResult = new LearnedBaselinePruningResult(); @@ -328,6 +325,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( var comboCountAfterRulePruning = ComboCounter.CountAll(); + var dbService = new QuantDatabaseService(); await dbService.InitializeAsync(forceRebuild: true); // The old MDA/predicted-size ceiling pass is intentionally removed. diff --git a/MagicQuant/Services/ModelCompatibilityService.cs b/MagicQuant/Services/ModelCompatibilityService.cs index d44d7cc..44fff3e 100644 --- a/MagicQuant/Services/ModelCompatibilityService.cs +++ b/MagicQuant/Services/ModelCompatibilityService.cs @@ -79,14 +79,9 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) var runtimeCandidates = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), allowHighPrecisionHybrids: false).ToList(); int unusedCount = 0; int usedCount = 0; - int shapeBanCount = 0; + int observedShapeIncompatibilityCount = 0; int explicitQuantBannedCount = 0; - var shapeTable = new Table().Border(TableBorder.Rounded).Title("[red]Shape Incompatibilities[/]"); - shapeTable.AddColumn("Group"); - shapeTable.AddColumn("Candidate"); - shapeTable.AddColumn("Reason"); - foreach (var group in TReg.All) { bool exists = result.FoundGroups.Contains(group.Name, StringComparer.OrdinalIgnoreCase); @@ -119,16 +114,15 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) var beforeRuntimeBan = RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate); - // something is wrong with this. It's not working and this is a luxury not requirement. It's causing down stream issues on moe_experts for Qwen3.6-35B-A3B + // BlockNeo compatibility validation is currently observed-only and under review for removal. + // It is not reliable for some architectures (especially MoE), so runtime bans remain disabled. //RuntimeSearchSpace.BanCombinationCandidateForGroup(group, candidate, phase: "TensorCompatibilityCheck", reason: "Block Alignment"); - shapeBanCount++; - shapeTable.AddRow($"[blue]{group.Name}[/]", $"[yellow]{candidate.Names[0]}[/]", - "[grey]Block Alignment[/]"); + observedShapeIncompatibilityCount++; if (ShouldLogCompatDetail(compatVerbose, group, candidate, focusCandidates)) { MagicQuantDiagnostics.Log("compat:decision", - $"group={group.Name}(id={group.UniqueId}) candidate={candidate.Names[0]}(id={candidate.UniqueId}) scheme={candidate.DefaultTensorScheme?.Names[0] ?? ""} block={candidate.DefaultTensorScheme?.BlockNeo?.ToString() ?? ""} staticBanned={candidate.BannedGroupIds.Contains(group.UniqueId)} runtimeBannedBefore={beforeRuntimeBan} result=restricted reason=Block Alignment"); + $"group={group.Name}(id={group.UniqueId}) candidate={candidate.Names[0]}(id={candidate.UniqueId}) scheme={candidate.DefaultTensorScheme?.Names[0] ?? ""} block={candidate.DefaultTensorScheme?.BlockNeo?.ToString() ?? ""} staticBanned={candidate.BannedGroupIds.Contains(group.UniqueId)} runtimeBannedBefore={beforeRuntimeBan} result=observed-only reason=Block Alignment restriction=disabled"); } if (failuresByGroupAndScheme.TryGetValue($"{failure.Group}::{failure.Scheme}", out var details) && details.Count > 0) @@ -167,14 +161,13 @@ public async Task RunCompatibilityCheckAsync(string ggufPath) AnsiConsole.MarkupLine("[green]No groups were reduced to explicit-banned/NULL-only by compatibility checks.[/]"); } - if (shapeBanCount > 0) + if (observedShapeIncompatibilityCount > 0) { - AnsiConsole.Write(shapeTable); - AnsiConsole.MarkupLine($"[yellow]Applied {shapeBanCount} shape-based restrictions.[/]"); + AnsiConsole.MarkupLine($"[yellow]Observed {observedShapeIncompatibilityCount:N0} BlockNeo/shape incompatibilities; runtime restrictions are currently disabled while compatibility validation is under review.[/]"); } else { - AnsiConsole.MarkupLine("[green]No shape-based restrictions found.[/]"); + AnsiConsole.MarkupLine("[green]No BlockNeo/shape incompatibilities observed.[/]"); } AnsiConsole.WriteLine(); From 9041079bde9f8632604c5130589893dea1415658 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 30 Apr 2026 13:20:44 -0400 Subject: [PATCH 173/258] cleaned up unecessary validation logging and work at startup. --- MagicQuant/Commands/Evolution.cs | 5 +++-- MagicQuant/Helpers/CliHelpers.cs | 2 -- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 3b74fb4..f03327c 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -209,8 +209,9 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; PrintCustomBaselineRuntimeSummary(resolvedCustomBaselines, imatrixEnsureResult.Enabled); - - CliHelpers.ValidateCombinationLogicWorks(true); + + // No longer needed + //CliHelpers.ValidateCombinationLogicWorks(true); var comboCountBefore = ComboCounter.CountAll(); var totalLearnedPruningResult = new LearnedBaselinePruningResult(); diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index c286ab8..57d1330 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -87,8 +87,6 @@ public static void PrintTotalCombinationCount() total > long.MaxValue ? $"[red]Total potential combinations exceed Int64 range:[/] [bold yellow]{total:N0}[/]" : $"[green]Total potential combinations:[/] [bold yellow]{total:N0}[/]"); - - AnsiConsole.MarkupLine($"[green]Total potential combinations:[/] [bold yellow]{total:N0}[/]"); } public static List ParseArguments(string input) From 566d2a0c2e2745d310ea59db3ac6339cc533208e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 30 Apr 2026 14:58:02 -0400 Subject: [PATCH 174/258] Massively upgraded DuckDB use and prediction speed. --- .../Models/PredictionSelectionModels.cs | 4 +- .../Services/CombinationDuckDbSchema.cs | 76 +++ .../CombinationSurvivalPipelineService.cs | 25 +- .../DuckDbPredictionMaterializationService.cs | 540 ++++++++++++++++++ .../PredictionGuidedHybridSelectionService.cs | 123 +--- .../Services/PredictionValidationService.cs | 39 +- MagicQuant/Services/QuantDatabaseService.cs | 67 +-- .../Services/RankSafeKldPredictionService.cs | 63 +- .../Services/RemainingCombinationStore.cs | 314 ++++++++-- 9 files changed, 995 insertions(+), 256 deletions(-) create mode 100644 MagicQuant/Services/CombinationDuckDbSchema.cs create mode 100644 MagicQuant/Services/DuckDbPredictionMaterializationService.cs diff --git a/MagicQuant/Models/PredictionSelectionModels.cs b/MagicQuant/Models/PredictionSelectionModels.cs index e46dcdd..24498f0 100644 --- a/MagicQuant/Models/PredictionSelectionModels.cs +++ b/MagicQuant/Models/PredictionSelectionModels.cs @@ -11,6 +11,7 @@ public sealed class RankSafePredictionRow public double AdditiveKld { get; set; } public double InteractionKld { get; set; } public double PredictedKld { get; set; } + public double PredictionConfidence { get; set; } = 1.0d; public double PredictedPpl { get; set; } public double CrossTerm { get; set; } public bool IsPureBaseline { get; init; } @@ -24,7 +25,7 @@ public sealed class RankSafePredictionRow public double ActualPpl { get; set; } = double.NaN; public ulong? ActualSizeBytes { get; set; } public int? ActualRank { get; set; } - public int? PredictedRank { get; set; } + public ulong? PredictedRank { get; set; } public double AbsoluteKldError => double.IsNaN(ActualKld) ? double.NaN : Math.Abs(PredictedKld - ActualKld); @@ -140,4 +141,3 @@ public sealed class PredictionGuidedSelectionResult public IReadOnlyList Eliminations { get; init; } = Array.Empty(); public IReadOnlyList ValidationFailures { get; init; } = Array.Empty(); } - diff --git a/MagicQuant/Services/CombinationDuckDbSchema.cs b/MagicQuant/Services/CombinationDuckDbSchema.cs new file mode 100644 index 0000000..77a4e06 --- /dev/null +++ b/MagicQuant/Services/CombinationDuckDbSchema.cs @@ -0,0 +1,76 @@ +using System.Linq; + +namespace MagicQuant.Services; + +internal static class CombinationDuckDbSchema +{ + public const string TableName = "tensor_configs"; + public const string SlotColumnList = "BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter"; + public const string PredictionColumnList = "PredictedKld, PredictedSizeBytes, PredictionConfidence, PredictionRank"; + public const string HybridPredicateSql = "(Embeddings <> 0 OR LmHead <> 0 OR AttnQ <> 0 OR AttnKV <> 0 OR AttnOutput <> 0 OR FfnUpGate <> 0 OR FfnDown <> 0 OR MoeExperts <> 0 OR MoeRouter <> 0)"; + + public static readonly string[] SlotColumns = + [ + "BaseQuant", + "Embeddings", + "LmHead", + "AttnQ", + "AttnKV", + "AttnOutput", + "FfnUpGate", + "FfnDown", + "MoeExperts", + "MoeRouter" + ]; + + public static readonly string[] ExpectedColumnTypes = + [ + "utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint", + "double","ubigint","double","ubigint" + ]; + + public static string CreateTableSql => $@" +DROP TABLE IF EXISTS {TableName}; +CREATE TABLE {TableName} ( + BaseQuant UTINYINT, + Embeddings UTINYINT, + LmHead UTINYINT, + AttnQ UTINYINT, + AttnKV UTINYINT, + AttnOutput UTINYINT, + FfnUpGate UTINYINT, + FfnDown UTINYINT, + MoeExperts UTINYINT, + MoeRouter UTINYINT, + + -- Transient DuckDB-only prediction metadata. + -- SQLite remains the real benchmark truth source. + PredictedKld DOUBLE, + PredictedSizeBytes UBIGINT, + PredictionConfidence DOUBLE, + PredictionRank UBIGINT +);"; + + public static string BuildSlotEqualityPredicate(string leftAlias, string rightAlias) + { + return string.Join(" AND ", SlotColumns.Select(c => $"{leftAlias}.{c} = {rightAlias}.{c}")); + } + + public static string QualifySlotColumnList(string alias) + { + return string.Join(", ", SlotColumns.Select(c => $"{alias}.{c}")); + } + + public static string QualifyHybridPredicate(string alias) + { + return HybridPredicateSql.Replace("Embeddings", $"{alias}.Embeddings") + .Replace("LmHead", $"{alias}.LmHead") + .Replace("AttnQ", $"{alias}.AttnQ") + .Replace("AttnKV", $"{alias}.AttnKV") + .Replace("AttnOutput", $"{alias}.AttnOutput") + .Replace("FfnUpGate", $"{alias}.FfnUpGate") + .Replace("FfnDown", $"{alias}.FfnDown") + .Replace("MoeExperts", $"{alias}.MoeExperts") + .Replace("MoeRouter", $"{alias}.MoeRouter"); + } +} diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index ad35f35..99e3ef5 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -13,6 +13,7 @@ public sealed class CombinationSurvivalPipelineService private readonly HybridBenchmarkRepository _benchmarkRepository; private readonly EffectiveCandidateStateResolverService _effectiveResolver; private readonly RankSafeKldPredictionService _predictionService; + private readonly DuckDbPredictionMaterializationService _materializationService; private readonly FinalRealBenchmarkEliminationService _finalEliminator; private readonly PredictionGuidedHybridSelectionService _selectionEngine; private readonly FinalSurvivorSelectionCliService _selectionCli; @@ -32,7 +33,8 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic _effectiveResolver = new EffectiveCandidateStateResolverService(_benchmarkRepository); _predictionService = new RankSafeKldPredictionService(_benchmarkRepository, _effectiveResolver); _finalEliminator = new FinalRealBenchmarkEliminationService(); - _selectionEngine = new PredictionGuidedHybridSelectionService(_quantizationService, _benchmarkRepository, _finalEliminator); + _materializationService = new DuckDbPredictionMaterializationService(_combinationStore, _predictionService); + _selectionEngine = new PredictionGuidedHybridSelectionService(_quantizationService, _benchmarkRepository, _finalEliminator, _combinationStore); _selectionCli = new FinalSurvivorSelectionCliService(); var pyManager = new PythonManager(Cache.MagicQuantDirectory!); var sidecarService = new ModelSidecarArtifactService(pyManager); @@ -55,12 +57,7 @@ public async Task RunAsync(CancellationToken AnsiConsole.Write(new Rule("[yellow]Rank-Safe Prediction / Hybrid Selection Pipeline[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"[green]Remaining DuckDB combinations available to score:[/] [cyan]{report.StartingCount:N0}[/]"); AnsiConsole.MarkupLine("[grey]Old MDA bucket survival is disabled. DuckDB now defines the allowed search space; rank-safe isolation prediction selects what deserves real benchmarking.[/]"); - AnsiConsole.MarkupLine("[grey]Note: final prediction/selection is currently guarded for small in-memory runs only; trillion-scale support requires DuckDB-backed prediction materialization + projection.[/]"); - - if (report.StartingCount > Config.MaxInMemoryCombinationLoadRows) - throw new InvalidOperationException($"Final prediction selection still requires DuckDB-backed prediction materialization. Refusing to load {report.StartingCount:N0} combinations into memory."); - - var remainingConfigs = await _combinationStore.LoadAllAsync(ct); + AnsiConsole.MarkupLine("[grey]DuckDB prediction materialization is enabled; final selection will query pre-ranked candidates instead of loading the full search space into memory.[/]"); var pureBaselines = await _benchmarkRepository.LoadPureBaselineSnapshotsAsync(ct); if (pureBaselines.Count == 0) @@ -68,18 +65,10 @@ public async Task RunAsync(CancellationToken AnsiConsole.MarkupLine($"[green]Pure baseline snapshots loaded:[/] [cyan]{pureBaselines.Count:N0}[/]"); - var predictionInput = remainingConfigs - .Concat(pureBaselines.Select(x => x.Config)) - .DistinctBy(TensorConfigIdentity.ToKey) - .ToList(); - - var predictions = await _predictionService.PredictAsync(predictionInput, ct); - - foreach (var note in predictions.Notes) - AnsiConsole.MarkupLine($"[grey]Prediction note:[/] {Markup.Escape(note)}"); + var materialization = await _materializationService.MaterializeAsync(ct); + AnsiConsole.MarkupLine($"[green]DuckDB predicted rows:[/] [cyan]{materialization.PredictedRows:N0}[/] / [cyan]{materialization.TotalRows:N0}[/] (ranked: {materialization.RankedRows:N0})"); var selection = await _selectionEngine.RunAsync( - predictions.PredictableRows.ToList(), pureBaselines, ct); @@ -195,4 +184,4 @@ private void RenderEliminationSummary( AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in magicquant.replacements.json.[/]"); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs new file mode 100644 index 0000000..eccdb69 --- /dev/null +++ b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs @@ -0,0 +1,540 @@ +using System.Globalization; +using DuckDB.NET.Data; +using MagicQuant.Helpers; +using MagicQuant.Models; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +/// +/// Materializes transient prediction/ranking metadata into DuckDB. +/// SQLite remains the long-term benchmark truth source; these columns only order +/// candidates before real quantization/benchmark validation. +/// +public sealed class DuckDbPredictionMaterializationService +{ + private readonly RemainingCombinationStore _store; + private readonly RankSafeKldPredictionService _predictionService; + + private static readonly GroupSlot[] GroupSlots = + [ + new(TReg.Embeddings, "Embeddings"), + new(TReg.LmHead, "LmHead"), + new(TReg.AttnQ, "AttnQ"), + new(TReg.AttnKV, "AttnKV"), + new(TReg.AttnOutput, "AttnOutput"), + new(TReg.FfnUpGate, "FfnUpGate"), + new(TReg.FfnDown, "FfnDown"), + new(TReg.MoeExperts, "MoeExperts"), + new(TReg.MoeRouter, "MoeRouter") + ]; + + public DuckDbPredictionMaterializationService( + RemainingCombinationStore store, + RankSafeKldPredictionService predictionService) + { + _store = store; + _predictionService = predictionService; + } + + public async Task MaterializeAsync(CancellationToken ct = default) + { + var model = await _predictionService.BuildModelAsync(ct); + + using var c = new DuckDBConnection($"Data Source={_store.GetDatabaseFilePath()}"); + await c.OpenAsync(ct); + await ConfigureSessionAsync(c, ct); + + await ExecuteAsync(c, $@" +UPDATE {CombinationDuckDbSchema.TableName} +SET PredictedKld = NULL, + PredictedSizeBytes = NULL, + PredictionConfidence = NULL, + PredictionRank = NULL;", ct); + + await BuildLookupTablesAsync(c, model, ct); + await BuildPredictionWorkTablesAsync(c, model, ct); + await BuildPavaBlocksAsync(c, ct); + await PersistProjectedPredictionsAsync(c, model, ct); + + var status = await _store.GetPredictionStatusAsync(ct); + + foreach (var note in model.Notes) + AnsiConsole.MarkupLine($"[grey]Prediction materialization note:[/] {Markup.Escape(note)}"); + + return status; + } + + private static async Task BuildLookupTablesAsync( + DuckDBConnection c, + RankSafeKldPredictionService.RankSafePredictionModel model, + CancellationToken ct) + { + await ExecuteAsync(c, @" +DROP TABLE IF EXISTS temp_effective_group_prediction; +DROP TABLE IF EXISTS temp_base_predicted_size; +DROP TABLE IF EXISTS temp_group_size_delta; + +CREATE TEMP TABLE temp_effective_group_prediction ( + BaseQuant UTINYINT, + GroupName VARCHAR, + GroupId UTINYINT, + StoredSlot UTINYINT, + EffectiveBaselineId UTINYINT, + NormalizedBaselineId UTINYINT, + KldContribution DOUBLE, + PplContribution DOUBLE, + BitRange DOUBLE, + IsZeroDamage BOOLEAN, + IsKldPredictable BOOLEAN +); + +CREATE TEMP TABLE temp_base_predicted_size ( + BaseQuant UTINYINT, + BaseSizeBytes UBIGINT, + IsSizePredictable BOOLEAN +); + +CREATE TEMP TABLE temp_group_size_delta ( + BaseQuant UTINYINT, + GroupName VARCHAR, + GroupId UTINYINT, + StoredSlot UTINYINT, + DeltaBytes BIGINT, + IsSizePredictable BOOLEAN +);", ct); + + var activeBaselines = RuntimeSearchSpace.GetActiveCombinationBaselines() + .OrderBy(x => x.UniqueId) + .ToList(); + + var activeGroups = model.ActiveGroups + .Select(g => GroupSlots.First(x => x.Group.UniqueId == g.UniqueId)) + .OrderBy(x => x.Group.UniqueId) + .ToList(); + + using var tx = c.BeginTransaction(); + + foreach (var baseline in activeBaselines) + { + byte normalizedBase = RankSafeKldPredictionService.NormalizeBaselineIdForIsolation(baseline.UniqueId); + bool hasBaseSize = model.BaseOnlySnapshotsByBaselineId.TryGetValue(normalizedBase, out var baseOnly); + await ExecuteAsync(c, + $"INSERT INTO temp_base_predicted_size VALUES ({baseline.UniqueId}, {SqlULong(hasBaseSize ? baseOnly!.SizeBytes : 0UL)}, {SqlBool(hasBaseSize)});", + ct); + + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseline); + + foreach (var slot in activeGroups) + { + var allowedForGroup = allowed[slot.Group.UniqueId]; + foreach (byte storedSlot in allowedForGroup) + { + var effectiveBaselineId = GetEffectiveBaselineId(baseline.UniqueId, storedSlot); + var normalizedBaselineId = RankSafeKldPredictionService.NormalizeBaselineIdForIsolation(effectiveBaselineId); + bool zeroDamage = IsZeroDamageAlias(effectiveBaselineId) || IsZeroDamageAlias(normalizedBaselineId); + + double kldContribution = 0d; + double pplContribution = 0d; + double bitRange = zeroDamage ? 99d : GetBitRange(normalizedBaselineId); + bool kldPredictable = true; + + if (!zeroDamage) + { + if (model.IsolationByGroupAndBaseline.TryGetValue((slot.Group.UniqueId, normalizedBaselineId), out var isolation)) + { + kldContribution = Math.Max(0d, isolation.Kld); + pplContribution = isolation.Ppl; + } + else + { + kldPredictable = false; + } + } + + await ExecuteAsync(c, $@" +INSERT INTO temp_effective_group_prediction VALUES ( + {baseline.UniqueId}, + '{slot.ColumnName}', + {slot.Group.UniqueId}, + {storedSlot}, + {effectiveBaselineId}, + {normalizedBaselineId}, + {SqlDouble(kldContribution)}, + {SqlDouble(pplContribution)}, + {SqlDouble(bitRange)}, + {SqlBool(zeroDamage)}, + {SqlBool(kldPredictable)} +);", ct); + + long deltaBytes = 0L; + bool sizePredictable = true; + + // This mirrors RankSafeKldPredictionService.PredictSize: + // base-only anchor starts with native-exact groups, then every active + // effective group contributes its measured isolation size delta. + if (!BaselineQuants.IsNativeExactAlias(normalizedBaselineId)) + { + if (model.IsolationByGroupAndBaseline.TryGetValue((slot.Group.UniqueId, normalizedBaselineId), out var targetIsolation)) + { + deltaBytes = (long)targetIsolation.SizeBytes - (long)model.Q8BaseOnly.SizeBytes; + } + else + { + sizePredictable = false; + } + } + + await ExecuteAsync(c, $@" +INSERT INTO temp_group_size_delta VALUES ( + {baseline.UniqueId}, + '{slot.ColumnName}', + {slot.Group.UniqueId}, + {storedSlot}, + {deltaBytes.ToString(CultureInfo.InvariantCulture)}, + {SqlBool(sizePredictable)} +);", ct); + } + } + } + + tx.Commit(); + } + + private static async Task BuildPredictionWorkTablesAsync( + DuckDBConnection c, + RankSafeKldPredictionService.RankSafePredictionModel model, + CancellationToken ct) + { + var active = model.ActiveGroups + .Select(g => GroupSlots.First(x => x.Group.UniqueId == g.UniqueId)) + .OrderBy(x => x.Group.UniqueId) + .ToList(); + + string JoinEffective(GroupSlot slot, string alias) => + $"LEFT JOIN temp_effective_group_prediction {alias} ON {alias}.BaseQuant = t.BaseQuant AND {alias}.GroupName = '{slot.ColumnName}' AND {alias}.StoredSlot = t.{slot.ColumnName}"; + + string JoinDelta(GroupSlot slot, string alias) => + $"LEFT JOIN temp_group_size_delta {alias} ON {alias}.BaseQuant = t.BaseQuant AND {alias}.GroupName = '{slot.ColumnName}' AND {alias}.StoredSlot = t.{slot.ColumnName}"; + + string kldSum = active.Count == 0 + ? "0.0" + : string.Join(" + ", active.Select((_, i) => $"COALESCE(e{i}.KldContribution, 0.0)")); + + string sizeSum = active.Count == 0 + ? "0" + : string.Join(" + ", active.Select((_, i) => $"COALESCE(d{i}.DeltaBytes, 0)")); + + string kldPredictable = active.Count == 0 + ? "TRUE" + : string.Join(" AND ", active.Select((_, i) => $"COALESCE(e{i}.IsKldPredictable, FALSE)")); + + string sizePredictable = active.Count == 0 + ? "b.IsSizePredictable" + : "b.IsSizePredictable AND " + string.Join(" AND ", active.Select((_, i) => $"COALESCE(d{i}.IsSizePredictable, FALSE)")); + + string joins = string.Join(Environment.NewLine, active.Select((slot, i) => JoinEffective(slot, $"e{i}"))) + + Environment.NewLine + + string.Join(Environment.NewLine, active.Select((slot, i) => JoinDelta(slot, $"d{i}"))); + + await ExecuteAsync(c, $@" +DROP TABLE IF EXISTS temp_prediction_work; +CREATE TEMP TABLE temp_prediction_work AS +SELECT + CAST(ROW_NUMBER() OVER () AS UBIGINT) AS PredictionWorkId, + t.{CombinationDuckDbSchema.SlotColumnList.Replace(", ", ", t.")}, + ({kldSum})::DOUBLE AS AdditiveKld, + GREATEST(CAST(b.BaseSizeBytes AS BIGINT) + {sizeSum}, 0)::UBIGINT AS PredictedSizeBytesRaw, + ({kldPredictable})::BOOLEAN AS IsKldPredictable, + ({sizePredictable})::BOOLEAN AS IsSizePredictable +FROM {CombinationDuckDbSchema.TableName} t +JOIN temp_base_predicted_size b ON b.BaseQuant = t.BaseQuant +{joins};", ct); + + string contribUnions = string.Join(Environment.NewLine + "UNION ALL" + Environment.NewLine, + active.Select((slot, i) => $@" +SELECT + w.PredictionWorkId, + CAST({i} AS UTINYINT) AS GroupOrder, + e.KldContribution, + e.BitRange, + GREATEST(0.0, {SqlDouble(model.Fit.BitStressThreshold)} - e.BitRange) AS Stress +FROM temp_prediction_work w +JOIN temp_effective_group_prediction e + ON e.BaseQuant = w.BaseQuant + AND e.GroupName = '{slot.ColumnName}' + AND e.StoredSlot = w.{slot.ColumnName} +WHERE w.IsKldPredictable")); + + await ExecuteAsync(c, $@" +DROP TABLE IF EXISTS temp_work_group_contrib; +CREATE TEMP TABLE temp_work_group_contrib AS +{contribUnions};", ct); + + await ExecuteAsync(c, @" +DROP TABLE IF EXISTS temp_work_cross_term; +CREATE TEMP TABLE temp_work_cross_term AS +SELECT + a.PredictionWorkId, + SUM(a.KldContribution * b.KldContribution * a.Stress * b.Stress) AS CrossTerm +FROM temp_work_group_contrib a +JOIN temp_work_group_contrib b + ON a.PredictionWorkId = b.PredictionWorkId + AND a.GroupOrder < b.GroupOrder +GROUP BY a.PredictionWorkId;", ct); + + await ExecuteAsync(c, $@" +DROP TABLE IF EXISTS temp_projection; +CREATE TEMP TABLE temp_projection AS +SELECT + w.PredictionWorkId, + w.AdditiveKld, + GREATEST(0.0, ({SqlDouble(model.Fit.Alpha)} * w.AdditiveKld) + ({SqlDouble(model.Fit.Beta)} * COALESCE(x.CrossTerm, 0.0))) AS InteractionKld, + w.PredictedSizeBytesRaw AS PredictedSizeBytes, + COALESCE(x.CrossTerm, 0.0) AS CrossTerm +FROM temp_prediction_work w +LEFT JOIN temp_work_cross_term x ON x.PredictionWorkId = w.PredictionWorkId +WHERE w.IsKldPredictable + AND w.IsSizePredictable + AND w.PredictedSizeBytesRaw > 0;", ct); + + // PAVA is still the same rank-safe projection. It is just applied once + // over the DuckDB-ordered work table instead of over a giant C# object list. + await ExecuteAsync(c, @" +DROP TABLE IF EXISTS temp_prediction_order; +CREATE TEMP TABLE temp_prediction_order AS +SELECT + CAST(ROW_NUMBER() OVER ( + ORDER BY AdditiveKld ASC, + InteractionKld ASC, + PredictedSizeBytes ASC + ) AS UBIGINT) AS PredictionOrdinal, + PredictionWorkId, + AdditiveKld, + InteractionKld, + PredictedSizeBytes +FROM temp_projection;", ct); + } + + private static async Task BuildPavaBlocksAsync(DuckDBConnection c, CancellationToken ct) + { + await ExecuteAsync(c, @" +DROP TABLE IF EXISTS temp_pava_blocks; +CREATE TEMP TABLE temp_pava_blocks ( + StartOrdinal UBIGINT, + EndOrdinal UBIGINT, + ProjectedKld DOUBLE, + BlockCount UBIGINT, + MeanAdjustment DOUBLE +);", ct); + + var blocks = new List(); + + using (var cmd = c.CreateCommand()) + { + cmd.CommandText = @" +SELECT PredictionOrdinal, InteractionKld +FROM temp_prediction_order +ORDER BY PredictionOrdinal ASC;"; + + using var r = await cmd.ExecuteReaderAsync(ct); + while (await r.ReadAsync(ct)) + { + ct.ThrowIfCancellationRequested(); + + ulong ordinal = Convert.ToUInt64(r.GetValue(0)); + double value = Math.Max(0d, Convert.ToDouble(r.GetValue(1), CultureInfo.InvariantCulture)); + + blocks.Add(new PavaBlock + { + StartOrdinal = ordinal, + EndOrdinal = ordinal, + Sum = value, + Count = 1 + }); + + while (blocks.Count >= 2 && blocks[^2].Mean > blocks[^1].Mean) + { + var right = blocks[^1]; + var left = blocks[^2]; + + left.EndOrdinal = right.EndOrdinal; + left.Sum += right.Sum; + left.Count += right.Count; + + blocks[^2] = left; + blocks.RemoveAt(blocks.Count - 1); + } + } + } + + if (blocks.Count == 0) + return; + + using var tx = c.BeginTransaction(); + using var insert = c.CreateCommand(); + insert.CommandText = "INSERT INTO temp_pava_blocks VALUES (?, ?, ?, ?, ?);"; + + foreach (var block in blocks) + { + insert.Parameters.Clear(); + double projected = Math.Max(0d, block.Mean); + insert.Parameters.Add(new DuckDBParameter { Value = block.StartOrdinal }); + insert.Parameters.Add(new DuckDBParameter { Value = block.EndOrdinal }); + insert.Parameters.Add(new DuckDBParameter { Value = projected }); + insert.Parameters.Add(new DuckDBParameter { Value = block.Count }); + insert.Parameters.Add(new DuckDBParameter { Value = Math.Abs(projected - block.Mean) }); + await insert.ExecuteNonQueryAsync(ct); + } + + tx.Commit(); + } + + private static async Task PersistProjectedPredictionsAsync( + DuckDBConnection c, + RankSafeKldPredictionService.RankSafePredictionModel model, + CancellationToken ct) + { + double baseConfidence = ComputeBaseConfidence(model.Fit); + + await ExecuteAsync(c, $@" +DROP TABLE IF EXISTS temp_projected_prediction; +CREATE TEMP TABLE temp_projected_prediction AS +SELECT + o.PredictionWorkId, + b.ProjectedKld AS PredictedKld, + o.InteractionKld, + o.PredictedSizeBytes, + b.BlockCount, + ABS(b.ProjectedKld - o.InteractionKld) / GREATEST(b.ProjectedKld, 1e-9) AS AdjustmentRatio, + GREATEST(0.25, 1.0 / SQRT(GREATEST(CAST(b.BlockCount AS DOUBLE), 1.0))) AS PlateauPenalty +FROM temp_prediction_order o +JOIN temp_pava_blocks b + ON o.PredictionOrdinal BETWEEN b.StartOrdinal AND b.EndOrdinal;", ct); + + await ExecuteAsync(c, $@" +DROP TABLE IF EXISTS temp_ranked_prediction; +CREATE TEMP TABLE temp_ranked_prediction AS +SELECT + w.{CombinationDuckDbSchema.SlotColumnList.Replace(", ", ", w.")}, + p.PredictedKld, + p.PredictedSizeBytes, + LEAST(1.0, GREATEST(0.0, + CASE WHEN NOT {CombinationDuckDbSchema.QualifyHybridPredicate("w")} + THEN 1.0 + ELSE {SqlDouble(baseConfidence)} * (1.0 / (1.0 + p.AdjustmentRatio)) * p.PlateauPenalty + END + )) AS PredictionConfidence +FROM temp_prediction_work w +JOIN temp_projected_prediction p ON p.PredictionWorkId = w.PredictionWorkId;", ct); + + await ExecuteAsync(c, $@" +DROP TABLE IF EXISTS temp_ranked_prediction_with_rank; +CREATE TEMP TABLE temp_ranked_prediction_with_rank AS +SELECT + *, + CAST(ROW_NUMBER() OVER ( + ORDER BY PredictedKld ASC, + PredictedSizeBytes ASC, + PredictionConfidence DESC, + BaseQuant ASC, + Embeddings ASC, + LmHead ASC, + AttnQ ASC, + AttnKV ASC, + AttnOutput ASC, + FfnUpGate ASC, + FfnDown ASC, + MoeExperts ASC, + MoeRouter ASC + ) AS UBIGINT) AS PredictionRank +FROM temp_ranked_prediction;", ct); + + await ExecuteAsync(c, $@" +UPDATE {CombinationDuckDbSchema.TableName} t +SET PredictedKld = r.PredictedKld, + PredictedSizeBytes = r.PredictedSizeBytes, + PredictionConfidence = r.PredictionConfidence, + PredictionRank = r.PredictionRank +FROM temp_ranked_prediction_with_rank r +WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "r")};", ct); + } + + private static double ComputeBaseConfidence(RankSafePredictionFit fit) + { + if (fit.UsedFallback) + return 0.65d; + + double denom = Math.Max(Config.PredictionMinimumFitRows * 4.0d, 1.0d); + return Math.Clamp(fit.FitRowCount / denom, 0.35d, 1.0d); + } + + private static byte GetEffectiveBaselineId(byte baseQuant, byte storedSlot) + { + return BaselineQuants.IsNullTensorConfigGroupSlot(storedSlot) + ? baseQuant + : BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedSlot); + } + + private static bool IsZeroDamageAlias(byte baselineId) + { + return baselineId == BaselineQuants.Q8_0.UniqueId || + BaselineQuants.IsNativeExactAlias(baselineId); + } + + private static double GetBitRange(byte baselineId) + { + if (IsZeroDamageAlias(baselineId)) + return 99d; + + return BaselineQuants.FromId(baselineId).BitRange; + } + + private static async Task ConfigureSessionAsync(DuckDBConnection connection, CancellationToken ct) + { + await ExecuteAsync(connection, "SET preserve_insertion_order = false;", ct); + await ExecuteAsync(connection, $"SET threads = {Math.Max(1, Environment.ProcessorCount)};", ct); + } + + private static async Task ExecuteAsync(DuckDBConnection c, string sql, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + await cmd.ExecuteNonQueryAsync(ct); + } + + private static string SqlDouble(double value) + { + if (double.IsNaN(value) || double.IsInfinity(value)) + return "0.0"; + + return value.ToString("R", CultureInfo.InvariantCulture); + } + + private static string SqlULong(ulong value) => value.ToString(CultureInfo.InvariantCulture); + private static string SqlBool(bool value) => value ? "TRUE" : "FALSE"; + + private readonly record struct GroupSlot(TensorGroup Group, string ColumnName); + + private struct PavaBlock + { + public ulong StartOrdinal; + public ulong EndOrdinal; + public double Sum; + public ulong Count; + public double Mean => Count == 0 ? 0d : Sum / Count; + } +} + +public sealed class PredictionMaterializationStatus +{ + public long TotalRows { get; init; } + public long PredictedRows { get; init; } + public long MissingPredictionRows { get; init; } + public long RankedRows { get; init; } + public double? MinPredictedKld { get; init; } + public double? MaxPredictedKld { get; init; } + public ulong? MinPredictedSizeBytes { get; init; } + public ulong? MaxPredictedSizeBytes { get; init; } +} diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index afe02c0..aa6f694 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -20,19 +20,21 @@ public sealed class PredictionGuidedHybridSelectionService private readonly QuantizationService _quantizationService; private readonly HybridBenchmarkRepository _repository; private readonly FinalRealBenchmarkEliminationService _finalEliminator; + private readonly RemainingCombinationStore _predictedStore; public PredictionGuidedHybridSelectionService( QuantizationService quantizationService, HybridBenchmarkRepository repository, - FinalRealBenchmarkEliminationService finalEliminator) + FinalRealBenchmarkEliminationService finalEliminator, + RemainingCombinationStore predictedStore) { _quantizationService = quantizationService; _repository = repository; _finalEliminator = finalEliminator; + _predictedStore = predictedStore; } public async Task RunAsync( - IReadOnlyList predictions, IReadOnlyList pureBaselineSnapshots, CancellationToken ct = default) { @@ -42,13 +44,13 @@ public async Task RunAsync( var current = _finalEliminator.Eliminate(pureBaselineSnapshots).Survivors.ToList(); AnsiConsole.MarkupLine($"[green]Pure/current anchor survivors after dominance:[/] [cyan]{current.Count:N0}[/]"); - var strict = await RunStrictDominanceReplacementAsync(predictions, current, eliminationRecords, validationFailures, ct); + var strict = await RunStrictDominanceReplacementAsync(current, eliminationRecords, validationFailures, ct); current = MergeAndDominanceFilter(current, strict.AcceptedSnapshots, eliminationRecords, "strict predicted hybrid dominance validated by real benchmark"); - var near = await RunNearBaselineReplacementAsync(predictions, current, eliminationRecords, validationFailures, ct); + var near = await RunNearBaselineReplacementAsync(current, eliminationRecords, validationFailures, ct); current = MergeAndDominanceFilter(current, near.AcceptedSnapshots, eliminationRecords, "near-baseline size-premium replacement validated by real benchmark"); - var interior = await RunInteriorSubspaceDiscoveryAsync(predictions, current, validationFailures, ct); + var interior = await RunInteriorSubspaceDiscoveryAsync(current, validationFailures, ct); current = MergeAndDominanceFilter(current, interior.AcceptedSnapshots, eliminationRecords, "interior subspace discovery dominated by real benchmark truth"); current = ApplyMeaningfulSpacing(current, eliminationRecords); @@ -81,7 +83,6 @@ public async Task RunAsync( } private async Task RunStrictDominanceReplacementAsync( - IReadOnlyList predictions, IReadOnlyList currentAnchors, List eliminations, List validationFailures, @@ -90,9 +91,6 @@ private async Task RunStrictDominanceReplacementAsync( AnsiConsole.Write(new Rule("[yellow]Prediction Phase 1: Strict Hybrid Dominance[/]") { Justification = Justify.Left }); var accepted = new List(); - var hybridPredictions = predictions - .Where(x => x.IsPredictable && x.IsSizePredictable && x.IsHybrid) - .ToList(); foreach (var anchor in currentAnchors.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes)) { @@ -102,26 +100,8 @@ private async Task RunStrictDominanceReplacementAsync( continue; } - var candidates = hybridPredictions - .Where(x => x.PredictedSizeBytes <= anchor.SizeBytes) - .Where(x => x.PredictedKld + Config.SelectionMinimumKldImprovementEpsilon < anchor.Kld) - .OrderBy(x => x.PredictedSizeBytes) - .ThenBy(x => x.PredictedKld) - .Take(Config.SelectionMaxFallbackAttemptsPerAnchor) - .Select((x, i) => new HybridSelectionCandidate - { - Prediction = x, - Reason = HybridSelectionReason.StrictDominanceReplacement, - LowerDamageAnchor = anchor, - HigherDamageAnchor = anchor, - WindowMinSizeBytes = 0, - WindowMaxSizeBytes = anchor.SizeBytes, - LinearExpectedKld = anchor.Kld, - PredictedGainOverLine = anchor.Kld - x.PredictedKld, - AttemptOrder = i + 1, - WindowLabel = $"strict <= {anchor.DisplayName}" - }) - .ToList(); + var strictRows = await _predictedStore.QueryStrictDominanceCandidatesAsync(anchor, Config.SelectionMaxFallbackAttemptsPerAnchor, ct); + var candidates = strictRows.Select((x, i) => new HybridSelectionCandidate { Prediction = x, Reason = HybridSelectionReason.StrictDominanceReplacement, LowerDamageAnchor = anchor, HigherDamageAnchor = anchor, WindowMinSizeBytes = 0, WindowMaxSizeBytes = anchor.SizeBytes, LinearExpectedKld = anchor.Kld, PredictedGainOverLine = anchor.Kld - x.PredictedKld, AttemptOrder = i + 1, WindowLabel = $"strict <= {anchor.DisplayName}" }).ToList(); if (candidates.Count == 0) continue; @@ -162,7 +142,6 @@ private async Task RunStrictDominanceReplacementAsync( } private async Task RunNearBaselineReplacementAsync( - IReadOnlyList predictions, IReadOnlyList currentAnchors, List eliminations, List validationFailures, @@ -187,16 +166,7 @@ private async Task RunNearBaselineReplacementAsync( if (max > upperSizeLowerDamage.SizeBytes) max = upperSizeLowerDamage.SizeBytes; - var candidates = FindBetterThanLinearCandidates( - predictions, - lowerSizeHigherDamage, - upperSizeLowerDamage, - min, - max, - HybridSelectionReason.NearBaselineOnePercentReplacement, - $"near-baseline +{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}% {lowerSizeHigherDamage.DisplayName}") - .Take(Config.SelectionMaxFallbackAttemptsPerAnchor) - .ToList(); + var candidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync(lowerSizeHigherDamage, upperSizeLowerDamage, min, max, HybridSelectionReason.NearBaselineOnePercentReplacement, $"near-baseline +{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}% {lowerSizeHigherDamage.DisplayName}", Config.SelectionMaxFallbackAttemptsPerAnchor * 3, ct)).Where(PassesNearLowerAnchorBrutality).Take(Config.SelectionMaxFallbackAttemptsPerAnchor).ToList(); if (candidates.Count == 0) continue; @@ -231,7 +201,6 @@ private async Task RunNearBaselineReplacementAsync( } private async Task RunInteriorSubspaceDiscoveryAsync( - IReadOnlyList predictions, IReadOnlyList currentAnchors, List validationFailures, CancellationToken ct) @@ -272,16 +241,7 @@ private async Task RunInteriorSubspaceDiscoveryAsync( if (max <= min) continue; - allCandidates.AddRange( - FindBetterThanLinearCandidates( - predictions, - pair.HigherDamageSmaller, - pair.LowerDamageLarger, - min, - max, - HybridSelectionReason.InteriorSubspaceDiscovery, - $"interior {i + 1}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}") - .Take(Config.SelectionMaxCandidatesPerInteriorWindow)); + allCandidates.AddRange((await _predictedStore.QueryBetterThanLinearCandidatesAsync(pair.HigherDamageSmaller, pair.LowerDamageLarger, min, max, HybridSelectionReason.InteriorSubspaceDiscovery, $"interior {i + 1}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}", Config.SelectionMaxCandidatesPerInteriorWindow, ct)).Where(PassesNearLowerAnchorBrutality)); cursor = max; @@ -385,65 +345,6 @@ private async Task BuildAndValidateSingleAsync( }; } - private List FindBetterThanLinearCandidates( - IReadOnlyList predictions, - BenchmarkSnapshotRecord higherDamageSmaller, - BenchmarkSnapshotRecord lowerDamageLarger, - ulong minSize, - ulong maxSize, - HybridSelectionReason reason, - string windowLabel) - { - if (maxSize < minSize) - return new List(); - - var result = predictions - .Where(x => x.IsPredictable && x.IsSizePredictable && x.IsHybrid) - .Where(x => x.PredictedSizeBytes >= minSize && x.PredictedSizeBytes <= maxSize) - .Select(x => - { - double line = InterpolateKldLine(x.PredictedSizeBytes, higherDamageSmaller, lowerDamageLarger); - double gain = line - x.PredictedKld; - return new HybridSelectionCandidate - { - Prediction = x, - Reason = reason, - HigherDamageAnchor = higherDamageSmaller, - LowerDamageAnchor = lowerDamageLarger, - WindowMinSizeBytes = minSize, - WindowMaxSizeBytes = maxSize, - LinearExpectedKld = line, - PredictedGainOverLine = gain, - WindowLabel = windowLabel - }; - }) - .Where(x => x.PredictedGainOverLine > Config.SelectionMinimumKldImprovementEpsilon) - .Where(x => PassesNearLowerAnchorBrutality(x)) - .OrderByDescending(x => x.PredictedGainOverLine) - .ThenBy(x => x.Prediction.PredictedSizeBytes) - .ThenBy(x => x.Prediction.PredictedKld) - .Select((x, i) => - { - x = new HybridSelectionCandidate - { - Prediction = x.Prediction, - Reason = x.Reason, - HigherDamageAnchor = x.HigherDamageAnchor, - LowerDamageAnchor = x.LowerDamageAnchor, - WindowMinSizeBytes = x.WindowMinSizeBytes, - WindowMaxSizeBytes = x.WindowMaxSizeBytes, - LinearExpectedKld = x.LinearExpectedKld, - PredictedGainOverLine = x.PredictedGainOverLine, - WindowLabel = x.WindowLabel, - AttemptOrder = i + 1 - }; - return x; - }) - .ToList(); - - return result; - } - private static bool PassesNearLowerAnchorBrutality(HybridSelectionCandidate candidate) { ulong span = candidate.LowerDamageAnchor.SizeBytes > candidate.HigherDamageAnchor.SizeBytes @@ -662,4 +563,4 @@ private sealed class AdjacentAnchorPair public BenchmarkSnapshotRecord LowerDamageLarger { get; init; } = default!; public BenchmarkSnapshotRecord HigherDamageSmaller { get; init; } = default!; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/PredictionValidationService.cs b/MagicQuant/Services/PredictionValidationService.cs index 547a98a..1d16d1d 100644 --- a/MagicQuant/Services/PredictionValidationService.cs +++ b/MagicQuant/Services/PredictionValidationService.cs @@ -74,7 +74,7 @@ public async Task ExportAsync( MarkdownPath = markdownPath, Rows = rows .OrderByDescending(x => x.AbsoluteKldError) - .ThenByDescending(x => Math.Abs((x.PredictedRank ?? 0) - (x.ActualRank ?? 0))) + .ThenByDescending(x => RankDistance(x)) .ToList() }; } @@ -85,7 +85,7 @@ private static void AssignRanks(IReadOnlyList rows) foreach (var row in rows.OrderBy(x => x.ActualKld).ThenBy(x => x.ActualSizeBytes ?? ulong.MaxValue)) row.ActualRank = actualRank++; - int predictedRank = 1; + ulong predictedRank = 1; foreach (var row in rows.OrderBy(x => x.PredictedKld).ThenBy(x => x.PredictedSizeBytes)) row.PredictedRank = predictedRank++; } @@ -132,7 +132,7 @@ private static RankSafeValidationSummary BuildSummary(IReadOnlyList rows.Count(x => x.ActualRank.HasValue && x.PredictedRank.HasValue && - Math.Abs(x.PredictedRank.Value - x.ActualRank.Value) <= maxShift); + RankDistance(x) <= (ulong)maxShift); return new RankSafeValidationSummary { @@ -162,9 +162,9 @@ private static string BuildCsv(IReadOnlyList rows) foreach (var row in rows .OrderByDescending(x => x.AbsoluteKldError) - .ThenByDescending(x => Math.Abs((x.PredictedRank ?? 0) - (x.ActualRank ?? 0)))) + .ThenByDescending(x => RankDistance(x))) { - int shift = (row.PredictedRank ?? 0) - (row.ActualRank ?? 0); + long shift = RankShift(row); sb.Append(Csv(TensorConfigIdentity.ToKey(row.Config))).Append(','); sb.Append(Csv(HybridBenchmarkRepository.BuildDisplayName(row.Quant))).Append(','); sb.Append(row.IsHybrid ? "true" : "false").Append(','); @@ -275,10 +275,10 @@ private static string BuildMarkdown( foreach (var row in rows .OrderByDescending(x => x.AbsoluteKldError) - .ThenByDescending(x => Math.Abs((x.PredictedRank ?? 0) - (x.ActualRank ?? 0))) + .ThenByDescending(x => RankDistance(x)) .Take(100)) { - int shift = (row.PredictedRank ?? 0) - (row.ActualRank ?? 0); + long shift = RankShift(row); sb.AppendLine( $"| {EscapePipe(HybridBenchmarkRepository.BuildDisplayName(row.Quant))} | {row.PredictedKld:0.000000} | {row.ActualKld:0.000000} | {row.AbsoluteKldError:0.000000} | " + $"{row.PredictedRank} | {row.ActualRank} | {shift:+#;-#;0} | {ToGb(row.PredictedSizeBytes)} | {ToGb(row.ActualSizeBytes ?? 0)} | {EscapePipe(BuildEffectiveGroupSummary(row.Config))} |"); @@ -314,6 +314,29 @@ private static string BuildEffectiveGroupSummary(TensorConfig config) })); } + private static ulong RankDistance(RankSafePredictionRow row) + { + if (!row.PredictedRank.HasValue || !row.ActualRank.HasValue) + return 0UL; + + ulong actual = (ulong)Math.Max(0, row.ActualRank.Value); + return row.PredictedRank.Value >= actual + ? row.PredictedRank.Value - actual + : actual - row.PredictedRank.Value; + } + + private static long RankShift(RankSafePredictionRow row) + { + if (!row.PredictedRank.HasValue || !row.ActualRank.HasValue) + return 0L; + + long predicted = row.PredictedRank.Value > long.MaxValue + ? long.MaxValue + : (long)row.PredictedRank.Value; + + return predicted - row.ActualRank.Value; + } + private static string Format(double value) => double.IsNaN(value) || double.IsInfinity(value) ? "" @@ -330,4 +353,4 @@ private static string Csv(string value) private static string ToGb(ulong bytes) => (bytes / 1024d / 1024d / 1024d).ToString("0.00", CultureInfo.InvariantCulture); private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 3d1780e..021f416 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -14,36 +14,11 @@ namespace MagicQuant.Services; public class QuantDatabaseService { private const string DbFileNamePrefix = "MagicQuant_Combinations"; - private const string TableName = "tensor_configs"; - - private static readonly string[] ExpectedColumnTypes = - [ - "utinyint", - "utinyint", - "utinyint", - "utinyint", - "utinyint", - "utinyint", - "utinyint", - "utinyint", - "utinyint", - "utinyint" - ]; - - private static string CreateTableSql => $@" - DROP TABLE IF EXISTS {TableName}; - CREATE TABLE {TableName} ( - BaseQuant UTINYINT, - Embeddings UTINYINT, - LmHead UTINYINT, - AttnQ UTINYINT, - AttnKV UTINYINT, - AttnOutput UTINYINT, - FfnUpGate UTINYINT, - FfnDown UTINYINT, - MoeExperts UTINYINT, - MoeRouter UTINYINT - );"; + private const string TableName = CombinationDuckDbSchema.TableName; + + private static readonly string[] ExpectedColumnTypes = CombinationDuckDbSchema.ExpectedColumnTypes; + + private static string CreateTableSql => CombinationDuckDbSchema.CreateTableSql; private static async Task ConfigureFastLoadSessionAsync(DuckDBConnection connection, CancellationToken ct) { @@ -417,23 +392,39 @@ private async Task BulkAppendAsync( CancellationToken ct) { // Emergency/small debug use only. Do NOT use for full search-space generation or trillion-scale pruning. + // Insert only the ten tensor slot columns; DuckDB prediction columns intentionally remain NULL + // until DuckDbPredictionMaterializationService scores/ranks the transient search space. if (rows.Count == 0) return; await ConfigureFastLoadSessionAsync(connection, ct); - using DuckDBAppender appender = connection.CreateAppender(TableName); + using var tx = connection.BeginTransaction(); + using var insert = connection.CreateCommand(); + insert.CommandText = $@" +INSERT INTO {TableName} ({CombinationDuckDbSchema.SlotColumnList}) +VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; foreach (var row in rows) { ct.ThrowIfCancellationRequested(); - appender.CreateRow() - .AppendValue(row.BaseQuant).AppendValue(row.Embeddings).AppendValue(row.LmHead) - .AppendValue(row.AttnQ).AppendValue(row.AttnKV).AppendValue(row.AttnOutput) - .AppendValue(row.FfnUpGate).AppendValue(row.FfnDown).AppendValue(row.MoeExperts) - .AppendValue(row.MoeRouter).EndRow(); + + insert.Parameters.Clear(); + insert.Parameters.Add(new DuckDBParameter { Value = row.BaseQuant }); + insert.Parameters.Add(new DuckDBParameter { Value = row.Embeddings }); + insert.Parameters.Add(new DuckDBParameter { Value = row.LmHead }); + insert.Parameters.Add(new DuckDBParameter { Value = row.AttnQ }); + insert.Parameters.Add(new DuckDBParameter { Value = row.AttnKV }); + insert.Parameters.Add(new DuckDBParameter { Value = row.AttnOutput }); + insert.Parameters.Add(new DuckDBParameter { Value = row.FfnUpGate }); + insert.Parameters.Add(new DuckDBParameter { Value = row.FfnDown }); + insert.Parameters.Add(new DuckDBParameter { Value = row.MoeExperts }); + insert.Parameters.Add(new DuckDBParameter { Value = row.MoeRouter }); + + await insert.ExecuteNonQueryAsync(ct); } - appender.Close(); + + tx.Commit(); } private async Task BuildPredictionContextAsync( @@ -753,4 +744,4 @@ private static byte NormalizeBaselineIdForIsolation(byte baselineId) return builtIn?.UniqueId ?? baselineId; } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/RankSafeKldPredictionService.cs b/MagicQuant/Services/RankSafeKldPredictionService.cs index ce83254..5ca4807 100644 --- a/MagicQuant/Services/RankSafeKldPredictionService.cs +++ b/MagicQuant/Services/RankSafeKldPredictionService.cs @@ -29,6 +29,22 @@ public RankSafeKldPredictionService( _effectiveResolver = effectiveResolver; } + internal async Task BuildModelAsync(CancellationToken ct = default) + { + var context = await BuildContextAsync(ct); + var fitRows = await LoadFitRowsAsync(context, Array.Empty(), ct); + var fit = FitInteractionModel(fitRows, context); + + var notes = new List(context.Notes) + { + $"Prediction fit rows: {fit.FitRowCount:N0}; alpha={fit.Alpha:G6}; beta={fit.Beta:G6}; bit-stress-threshold={fit.BitStressThreshold:G4}; fallback={fit.UsedFallback}." + }; + + context.Fit = fit; + context.Notes = notes; + return context; + } + public async Task PredictAsync( IReadOnlyCollection configs, CancellationToken ct = default) @@ -36,7 +52,7 @@ public async Task PredictAsync( if (configs == null) throw new ArgumentNullException(nameof(configs)); - var context = await BuildContextAsync(ct); + var context = await BuildModelAsync(ct); var uniqueConfigs = configs .DistinctBy(TensorConfigIdentity.ToKey) .ToList(); @@ -50,21 +66,15 @@ public async Task PredictAsync( rows.Add(row); } - var fitRows = await LoadFitRowsAsync(context, rows, ct); - var fit = FitInteractionModel(fitRows, context); - foreach (var row in rows.Where(x => x.IsPredictable)) { - row.CrossTerm = ComputeCrossTerm(row.Config, context, fit.BitStressThreshold); - row.InteractionKld = Math.Max(0d, (fit.Alpha * row.AdditiveKld) + (fit.Beta * row.CrossTerm)); + row.CrossTerm = ComputeCrossTerm(row.Config, context, context.Fit.BitStressThreshold); + row.InteractionKld = Math.Max(0d, (context.Fit.Alpha * row.AdditiveKld) + (context.Fit.Beta * row.CrossTerm)); } ApplyRankSafeProjection(rows); - var notes = new List(context.Notes); - notes.Add($"Prediction fit rows: {fit.FitRowCount:N0}; alpha={fit.Alpha:G6}; beta={fit.Beta:G6}; bit-stress-threshold={fit.BitStressThreshold:G4}; fallback={fit.UsedFallback}."); - - PrintPredictionDiagnostics(rows, fit); + PrintPredictionDiagnostics(rows, context.Fit); return new RankSafePredictionSet { Rows = rows @@ -72,14 +82,14 @@ public async Task PredictAsync( .ThenBy(x => x.IsSizePredictable ? 0 : 1) .ThenBy(x => x.PredictedSizeBytes) .ToList(), - Fit = fit, - Notes = notes + Fit = context.Fit, + Notes = context.Notes }; } private async Task PredictSingleAsync( TensorConfig config, - PredictionContext context, + RankSafePredictionModel context, CancellationToken ct) { var quant = (HybridQuant)config; @@ -136,7 +146,7 @@ private async Task PredictSingleAsync( return row; } - private async Task BuildContextAsync(CancellationToken ct) + private async Task BuildContextAsync(CancellationToken ct) { var activeGroups = TReg.All .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) @@ -220,7 +230,7 @@ private async Task BuildContextAsync(CancellationToken ct) } } - return new PredictionContext( + return new RankSafePredictionModel( activeGroups: activeGroups, pureQ8: pureQ8, q8BaseOnly: q8BaseOnly, @@ -231,7 +241,7 @@ private async Task BuildContextAsync(CancellationToken ct) } private async Task> LoadFitRowsAsync( - PredictionContext context, + RankSafePredictionModel context, IReadOnlyList alreadyPredicted, CancellationToken ct) { @@ -269,7 +279,7 @@ private async Task> LoadFitRowsAsync( private RankSafePredictionFit FitInteractionModel( IReadOnlyList observations, - PredictionContext context) + RankSafePredictionModel context) { var usable = observations .Where(x => x.ActualKld >= 0d) @@ -389,7 +399,7 @@ private static void ApplyRankSafeProjection(IReadOnlyList for (int i = 0; i < predictable.Count; i++) predictable[i].PredictedKld = Math.Max(0d, projected[i]); - int rank = 1; + ulong rank = 1; foreach (var row in rows .Where(x => x.IsPredictable) .OrderBy(x => x.PredictedKld) @@ -437,7 +447,7 @@ private static double[] Pava(double[] values) private double PredictAdditiveKld( TensorConfig config, - PredictionContext context, + RankSafePredictionModel context, List notes, out bool canPredict) { @@ -468,7 +478,7 @@ private double PredictAdditiveKld( private double PredictPpl( TensorConfig config, - PredictionContext context, + RankSafePredictionModel context, List notes) { double total = 0d; @@ -488,7 +498,7 @@ private double PredictPpl( private ulong PredictSize( TensorConfig config, - PredictionContext context, + RankSafePredictionModel context, List notes, out bool canPredictSize) { @@ -534,7 +544,7 @@ private ulong PredictSize( return (ulong)total; } - private double ComputeCrossTerm(TensorConfig config, PredictionContext context, double threshold) + private double ComputeCrossTerm(TensorConfig config, RankSafePredictionModel context, double threshold) { var contributions = new List<(double Kld, double Bits)>(); @@ -643,9 +653,9 @@ private static void PrintPredictionDiagnostics(IReadOnlyCollection bytes / 1024d / 1024d / 1024d; - private sealed class PredictionContext + internal sealed class RankSafePredictionModel { - public PredictionContext( + public RankSafePredictionModel( IReadOnlyList activeGroups, BenchmarkSnapshotRecord pureQ8, BenchmarkSnapshotRecord q8BaseOnly, @@ -669,7 +679,8 @@ public PredictionContext( public Dictionary PureSnapshotsByBaselineId { get; } public Dictionary BaseOnlySnapshotsByBaselineId { get; } public Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord> IsolationByGroupAndBaseline { get; } - public IReadOnlyList Notes { get; } + public IReadOnlyList Notes { get; set; } + public RankSafePredictionFit Fit { get; set; } = new(); } private sealed class FitObservation @@ -686,4 +697,4 @@ private struct PavaBlock public int Count; public double Mean => Weight <= 0d ? 0d : Sum / Weight; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index e54e7f7..8028292 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -1,5 +1,6 @@ using DuckDB.NET.Data; using MagicQuant.Helpers; +using MagicQuant.Models; using MQ.DB; using MQ.DB.Models; using System.Runtime.CompilerServices; @@ -9,7 +10,7 @@ namespace MagicQuant.Services; public sealed class RemainingCombinationStore { private const string DbFileNamePrefix = "MagicQuant_Combinations"; - private const string TableName = "tensor_configs"; + private const string TableName = CombinationDuckDbSchema.TableName; private static string ConnectionString => $"Data Source={Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName())}"; @@ -40,25 +41,13 @@ public async Task> LoadAllAsync(CancellationToken ct = defaul using var cmd = connection.CreateCommand(); cmd.CommandText = $@" -SELECT BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter +SELECT {CombinationDuckDbSchema.SlotColumnList} FROM {TableName} -ORDER BY BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter;"; +ORDER BY {CombinationDuckDbSchema.SlotColumnList};"; using var reader = await cmd.ExecuteReaderAsync(ct); while (await reader.ReadAsync(ct)) - { - results.Add(new TensorConfig( - baseQuant: Convert.ToByte(reader.GetValue(0)), - embeddings: Convert.ToByte(reader.GetValue(1)), - lmHead: Convert.ToByte(reader.GetValue(2)), - attnQ: Convert.ToByte(reader.GetValue(3)), - attnKV: Convert.ToByte(reader.GetValue(4)), - attnOutput: Convert.ToByte(reader.GetValue(5)), - ffnUpGate: Convert.ToByte(reader.GetValue(6)), - ffnDown: Convert.ToByte(reader.GetValue(7)), - moeExperts: Convert.ToByte(reader.GetValue(8)), - moeRouter: Convert.ToByte(reader.GetValue(9)))); - } + results.Add(ReadTensorConfig(reader)); return results; } @@ -72,24 +61,27 @@ public async IAsyncEnumerable StreamAsync( using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); await ConfigureFastLoadSessionAsync(connection, ct); - string sql = $@"SELECT BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter FROM {TableName}"; - if (!string.IsNullOrWhiteSpace(whereSql)) sql += $" WHERE {whereSql}"; - if (!string.IsNullOrWhiteSpace(orderBySql)) sql += $" ORDER BY {orderBySql}"; - if (limit.HasValue) sql += $" LIMIT {limit.Value}"; + + string sql = $@"SELECT {CombinationDuckDbSchema.SlotColumnList} FROM {TableName}"; + if (!string.IsNullOrWhiteSpace(whereSql)) + sql += $" WHERE {whereSql}"; + if (!string.IsNullOrWhiteSpace(orderBySql)) + sql += $" ORDER BY {orderBySql}"; + if (limit.HasValue) + sql += $" LIMIT {limit.Value}"; + using var cmd = connection.CreateCommand(); cmd.CommandText = sql; + using var reader = await cmd.ExecuteReaderAsync(ct); while (await reader.ReadAsync(ct)) - { - yield return new TensorConfig( - Convert.ToByte(reader.GetValue(0)), Convert.ToByte(reader.GetValue(1)), Convert.ToByte(reader.GetValue(2)), - Convert.ToByte(reader.GetValue(3)), Convert.ToByte(reader.GetValue(4)), Convert.ToByte(reader.GetValue(5)), - Convert.ToByte(reader.GetValue(6)), Convert.ToByte(reader.GetValue(7)), Convert.ToByte(reader.GetValue(8)), - Convert.ToByte(reader.GetValue(9))); - } + yield return ReadTensorConfig(reader); } - public async Task ReplaceAllAsync(IReadOnlyCollection configs, string reason, CancellationToken ct = default) + public async Task ReplaceAllAsync( + IReadOnlyCollection configs, + string reason, + CancellationToken ct = default) { using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); @@ -100,28 +92,257 @@ public async Task ReplaceAllAsync(IReadOnlyCollection configs, str using var insert = connection.CreateCommand(); insert.CommandText = $@" INSERT INTO {TableName} -(BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter) +({CombinationDuckDbSchema.SlotColumnList}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; foreach (var config in configs) { insert.Parameters.Clear(); - insert.Parameters.Add(new DuckDBParameter { Value = config.BaseQuant }); - insert.Parameters.Add(new DuckDBParameter { Value = config.Embeddings }); - insert.Parameters.Add(new DuckDBParameter { Value = config.LmHead }); - insert.Parameters.Add(new DuckDBParameter { Value = config.AttnQ }); - insert.Parameters.Add(new DuckDBParameter { Value = config.AttnKV }); - insert.Parameters.Add(new DuckDBParameter { Value = config.AttnOutput }); - insert.Parameters.Add(new DuckDBParameter { Value = config.FfnUpGate }); - insert.Parameters.Add(new DuckDBParameter { Value = config.FfnDown }); - insert.Parameters.Add(new DuckDBParameter { Value = config.MoeExperts }); - insert.Parameters.Add(new DuckDBParameter { Value = config.MoeRouter }); + AddSlotParameters(insert, config); await insert.ExecuteNonQueryAsync(ct); } tx.Commit(); } + public async Task GetPredictionStatusAsync(CancellationToken ct = default) + { + using var connection = new DuckDBConnection(ConnectionString); + await connection.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(connection, ct); + + using var cmd = connection.CreateCommand(); + cmd.CommandText = $@" +SELECT + COUNT(*) AS TotalRows, + COUNT(PredictedKld) AS PredictedRows, + COUNT(PredictionRank) AS RankedRows, + MIN(PredictedKld) AS MinPredictedKld, + MAX(PredictedKld) AS MaxPredictedKld, + MIN(PredictedSizeBytes) AS MinPredictedSizeBytes, + MAX(PredictedSizeBytes) AS MaxPredictedSizeBytes +FROM {TableName};"; + + using var r = await cmd.ExecuteReaderAsync(ct); + await r.ReadAsync(ct); + + long total = Convert.ToInt64(r.GetValue(0)); + long predicted = Convert.ToInt64(r.GetValue(1)); + long ranked = Convert.ToInt64(r.GetValue(2)); + + return new PredictionMaterializationStatus + { + TotalRows = total, + PredictedRows = predicted, + MissingPredictionRows = Math.Max(0, total - predicted), + RankedRows = ranked, + MinPredictedKld = r.IsDBNull(3) ? null : Convert.ToDouble(r.GetValue(3)), + MaxPredictedKld = r.IsDBNull(4) ? null : Convert.ToDouble(r.GetValue(4)), + MinPredictedSizeBytes = r.IsDBNull(5) ? null : Convert.ToUInt64(r.GetValue(5)), + MaxPredictedSizeBytes = r.IsDBNull(6) ? null : Convert.ToUInt64(r.GetValue(6)) + }; + } + + public async Task> QueryStrictDominanceCandidatesAsync( + BenchmarkSnapshotRecord anchor, + int limit, + CancellationToken ct = default) + { + string sql = $@" +SELECT {CombinationDuckDbSchema.SlotColumnList}, + PredictedKld, + PredictedSizeBytes, + PredictionConfidence, + PredictionRank +FROM {TableName} +WHERE PredictedKld IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.HybridPredicateSql} + AND PredictedSizeBytes <= ? + AND PredictedKld + ? < ? +ORDER BY PredictedSizeBytes ASC, + PredictedKld ASC, + PredictionRank ASC, + PredictionConfidence DESC +LIMIT ?;"; + + return await QueryPredictedRowsAsync( + sql, + new object[] { anchor.SizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.Kld, limit }, + ct); + } + + public async Task> QueryBetterThanLinearCandidatesAsync( + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger, + ulong minSize, + ulong maxSize, + HybridSelectionReason reason, + string windowLabel, + int limit, + CancellationToken ct = default) + { + string sql = $@" +WITH scored AS ( + SELECT {CombinationDuckDbSchema.SlotColumnList}, + PredictedKld, + PredictedSizeBytes, + PredictionConfidence, + PredictionRank, + (CAST(? AS DOUBLE) + + ((CAST(PredictedSizeBytes AS DOUBLE) - CAST(? AS DOUBLE)) / GREATEST(CAST(? AS DOUBLE), 1.0)) + * (CAST(? AS DOUBLE) - CAST(? AS DOUBLE))) AS LinearExpectedKld + FROM {TableName} + WHERE PredictedKld IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.HybridPredicateSql} + AND PredictedSizeBytes BETWEEN ? AND ? +), +ranked AS ( + SELECT *, + LinearExpectedKld - PredictedKld AS Gain + FROM scored +) +SELECT {CombinationDuckDbSchema.SlotColumnList}, + PredictedKld, + PredictedSizeBytes, + PredictionConfidence, + PredictionRank, + LinearExpectedKld, + Gain +FROM ranked +WHERE Gain > ? +ORDER BY Gain DESC, + PredictionConfidence DESC, + PredictedSizeBytes ASC, + PredictedKld ASC, + PredictionRank ASC +LIMIT ?;"; + + double denominator = Math.Max( + (double)lowerDamageLarger.SizeBytes - higherDamageSmaller.SizeBytes, + 1d); + + using var c = new DuckDBConnection(ConnectionString); + await c.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(c, ct); + + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + + foreach (var value in new object[] + { + higherDamageSmaller.Kld, + (double)higherDamageSmaller.SizeBytes, + denominator, + lowerDamageLarger.Kld, + higherDamageSmaller.Kld, + minSize, + maxSize, + Config.SelectionMinimumKldImprovementEpsilon, + limit + }) + { + cmd.Parameters.Add(new DuckDBParameter { Value = value }); + } + + var list = new List(); + using var r = await cmd.ExecuteReaderAsync(ct); + int attempt = 0; + while (await r.ReadAsync(ct)) + { + var prediction = MapPredictedRow(r); + double line = Convert.ToDouble(r.GetValue(14)); + double gain = Convert.ToDouble(r.GetValue(15)); + + list.Add(new HybridSelectionCandidate + { + Prediction = prediction, + Reason = reason, + HigherDamageAnchor = higherDamageSmaller, + LowerDamageAnchor = lowerDamageLarger, + WindowMinSizeBytes = minSize, + WindowMaxSizeBytes = maxSize, + LinearExpectedKld = line, + PredictedGainOverLine = gain, + WindowLabel = windowLabel, + AttemptOrder = ++attempt + }); + } + + return list; + } + + private async Task> QueryPredictedRowsAsync( + string sql, + object[] args, + CancellationToken ct) + { + using var c = new DuckDBConnection(ConnectionString); + await c.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(c, ct); + + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + foreach (var arg in args) + cmd.Parameters.Add(new DuckDBParameter { Value = arg }); + + using var r = await cmd.ExecuteReaderAsync(ct); + var list = new List(); + while (await r.ReadAsync(ct)) + list.Add(MapPredictedRow(r)); + + return list; + } + + private static RankSafePredictionRow MapPredictedRow(System.Data.Common.DbDataReader r) + { + var config = ReadTensorConfig(r); + + return new RankSafePredictionRow + { + Config = config, + Quant = (HybridQuant)config, + PredictedKld = Convert.ToDouble(r.GetValue(10)), + PredictedSizeBytes = Convert.ToUInt64(r.GetValue(11)), + PredictionConfidence = Convert.ToDouble(r.GetValue(12)), + PredictedRank = Convert.ToUInt64(r.GetValue(13)), + IsPredictable = true, + IsSizePredictable = true + }; + } + + private static TensorConfig ReadTensorConfig(System.Data.Common.DbDataReader r) + { + return new TensorConfig( + Convert.ToByte(r.GetValue(0)), + Convert.ToByte(r.GetValue(1)), + Convert.ToByte(r.GetValue(2)), + Convert.ToByte(r.GetValue(3)), + Convert.ToByte(r.GetValue(4)), + Convert.ToByte(r.GetValue(5)), + Convert.ToByte(r.GetValue(6)), + Convert.ToByte(r.GetValue(7)), + Convert.ToByte(r.GetValue(8)), + Convert.ToByte(r.GetValue(9))); + } + + private static void AddSlotParameters(DuckDBCommand command, TensorConfig config) + { + command.Parameters.Add(new DuckDBParameter { Value = config.BaseQuant }); + command.Parameters.Add(new DuckDBParameter { Value = config.Embeddings }); + command.Parameters.Add(new DuckDBParameter { Value = config.LmHead }); + command.Parameters.Add(new DuckDBParameter { Value = config.AttnQ }); + command.Parameters.Add(new DuckDBParameter { Value = config.AttnKV }); + command.Parameters.Add(new DuckDBParameter { Value = config.AttnOutput }); + command.Parameters.Add(new DuckDBParameter { Value = config.FfnUpGate }); + command.Parameters.Add(new DuckDBParameter { Value = config.FfnDown }); + command.Parameters.Add(new DuckDBParameter { Value = config.MoeExperts }); + command.Parameters.Add(new DuckDBParameter { Value = config.MoeRouter }); + } + private static async Task ConfigureFastLoadSessionAsync(DuckDBConnection connection, CancellationToken ct) { using (var cmd = connection.CreateCommand()) @@ -140,20 +361,7 @@ private static async Task ConfigureFastLoadSessionAsync(DuckDBConnection connect private static async Task RecreateTableAsync(DuckDBConnection connection, CancellationToken ct) { using var createCmd = connection.CreateCommand(); - createCmd.CommandText = $@" -DROP TABLE IF EXISTS {TableName}; -CREATE TABLE {TableName} ( - BaseQuant UTINYINT, - Embeddings UTINYINT, - LmHead UTINYINT, - AttnQ UTINYINT, - AttnKV UTINYINT, - AttnOutput UTINYINT, - FfnUpGate UTINYINT, - FfnDown UTINYINT, - MoeExperts UTINYINT, - MoeRouter UTINYINT -);"; + createCmd.CommandText = CombinationDuckDbSchema.CreateTableSql; await createCmd.ExecuteNonQueryAsync(ct); } From 7ecc6f6790377f8bcd02b8a7629c020f8bf9f937 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 30 Apr 2026 15:59:12 -0400 Subject: [PATCH 175/258] new output flag for faster rebuilds of final output. --- MagicQuant/Commands/BuildHybrids.cs | 4 +- MagicQuant/Commands/Evolution.cs | 5 +- MagicQuant/Config.cs | 3 +- .../Configuration/MagicQuantYamlConfig.cs | 13 +- .../Configuration/MagicQuantYamlLoader.cs | 36 +++- MagicQuant/Program.cs | 11 +- .../Services/HybridArtifactExportService.cs | 58 +++++- .../Services/ModelSidecarArtifactService.cs | 36 +++- .../Services/ReadmeGenerationService.cs | 165 +++++++++++++++++- MagicQuant/config.default.yaml | 33 +++- MagicQuant/config.dev.yaml | 31 ++++ 11 files changed, 366 insertions(+), 29 deletions(-) diff --git a/MagicQuant/Commands/BuildHybrids.cs b/MagicQuant/Commands/BuildHybrids.cs index 1118b94..5ebff27 100644 --- a/MagicQuant/Commands/BuildHybrids.cs +++ b/MagicQuant/Commands/BuildHybrids.cs @@ -21,6 +21,6 @@ private static void ShowHelp() { AnsiConsole.MarkupLine("[bold yellow]Command: build-hybrids[/]"); AnsiConsole.MarkupLine("Runs the centralized survival/export flow over the active MagicQuant evolution pipeline."); - AnsiConsole.MarkupLine("Usage: mq build-hybrids --model-dir \"\" [--output-dir \"\"] [--output-name-prefix \"Model\"] [--export-external-learned-baselines]"); + AnsiConsole.MarkupLine("Usage: mq build-hybrids --model-dir \"\" [--config \"./config.default.yaml\"] [--output-dir \"\"] [--output-name-prefix \"Model\"] [--reuse-existing-final-artifacts] [--export-external-learned-baselines]"); } -} +} \ No newline at end of file diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index f03327c..2b49c83 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -705,8 +705,9 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--selection-interior-window-fractions[/] Comma-separated phase-3 interior windows, e.g. 0.35,0.35 (Optional)"); AnsiConsole.MarkupLine(" [green]--prediction-bit-stress-threshold-candidates[/] Comma-separated interaction-fit thresholds, e.g. 4,5,6,7,8,9,10,11,12 (Optional)"); AnsiConsole.MarkupLine(" [green]--output-dir[/] Final export/output directory for selected survivor artifacts (Optional; default = /MagicQuant/Final_Outputs)"); - AnsiConsole.MarkupLine(" [green]--allow-eight-bit-anchor-replacements[/] Permit final prediction to try replacing 8-bit anchors like Q8_0 (Optional; default false)"); AnsiConsole.MarkupLine(" [green]--output-name-prefix[/] Output filename prefix for exported GGUF files (Optional; default = Model)"); + AnsiConsole.MarkupLine(" [green]--reuse-existing-final-artifacts[/] Reuse valid final GGUFs only when exact file name + benchmark byte size match (Optional; default false)"); + AnsiConsole.MarkupLine(" [green]--allow-eight-bit-anchor-replacements[/] Permit final prediction to try replacing 8-bit anchors like Q8_0 (Optional; default false)"); AnsiConsole.MarkupLine(" [green]--export-external-learned-baselines[/] Also locally rebuild/export pure learned external baselines such as Unsloth (Optional; default false)"); AnsiConsole.MarkupLine(" [green]--selection-max-candidates-per-interior-window[/] Candidate count retained per interior window (Optional; default = 1)"); AnsiConsole.MarkupLine(" [green]--config[/] Path to YAML runtime config. CLI flags override YAML values."); @@ -753,4 +754,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} +} \ No newline at end of file diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 3179171..39656cf 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -76,6 +76,7 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.Output.ExportExternalLearnedBaselines; public static bool AttemptMmprojBuild => Current.Output.AttemptMmprojBuild; public static bool RequireMmprojForVisionModels => Current.Output.RequireMmprojForVisionModels; + public static bool ReuseExistingFinalArtifacts => Current.Output.ReuseExistingFinalArtifacts; public static int MaxSelectedChoicesPerBucket => Math.Max(1, Current.Survival.MaxSelectedChoicesPerBucket); public static double SurvivalMeaningfulSizeBiasPercent => Current.Survival.MeaningfulSizeBiasPercent; @@ -90,4 +91,4 @@ public static void SetResolvedCustomBaselines(IEnumerable BrainLayers => Current.BrainLayers; public static List CollapsePenaltySchemes => Current.CollapsePenaltySchemes; public static List MoeIndicatorTensors => Current.MoeIndicatorTensors; -} +} \ No newline at end of file diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 7d355aa..d8e293f 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -6,6 +6,7 @@ public sealed class MagicQuantYamlConfig { public RuntimePathConfig Paths { get; set; } = new(); public RuntimeFlagConfig Flags { get; set; } = new(); + public RuntimeReadmeConfig Readme { get; set; } = new(); public RuntimeImatrixConfig Imatrix { get; set; } = new(); public RuntimeEvolutionConfig Evolution { get; set; } = new(); public RuntimeIsolationPruningConfig IsolationPruning { get; set; } = new(); @@ -99,6 +100,15 @@ public sealed class RuntimeFlagConfig public bool AllowHighPrecisionHybrids { get; set; } } +public sealed class RuntimeReadmeConfig +{ + public string? TitleModelNameOverride { get; set; } + + // Flexible by design: Hugging Face frontmatter can grow without requiring + // new strongly typed C# properties for every key. + public Dictionary Frontmatter { get; set; } = new(StringComparer.OrdinalIgnoreCase); +} + public sealed class RuntimeImatrixConfig { public string? ImatrixUrl { get; set; } @@ -171,6 +181,7 @@ public sealed class RuntimeOutputConfig public bool ExportExternalLearnedBaselines { get; set; } = false; public bool AttemptMmprojBuild { get; set; } = true; public bool RequireMmprojForVisionModels { get; set; } = false; + public bool ReuseExistingFinalArtifacts { get; set; } = false; } public sealed class RuntimeSurvivalConfig @@ -298,4 +309,4 @@ public sealed class ResolvedCustomBaselineSpec public sealed class RuntimeHardwareConfig { public Dictionary GpuMemoryLimitsGb { get; set; } = new(); -} +} \ No newline at end of file diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index e81177d..1bce22f 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -97,6 +97,17 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) ? "Model" : config.Output.OutputNamePrefix.Trim(); + config.Readme ??= new RuntimeReadmeConfig(); + + config.Readme.TitleModelNameOverride = string.IsNullOrWhiteSpace(config.Readme.TitleModelNameOverride) + ? null + : config.Readme.TitleModelNameOverride.Trim(); + + config.Readme.Frontmatter ??= new Dictionary(StringComparer.OrdinalIgnoreCase); + config.Readme.Frontmatter = config.Readme.Frontmatter + .Where(x => !string.IsNullOrWhiteSpace(x.Key) && !IsEmptyFrontmatterValue(x.Value)) + .ToDictionary(x => x.Key.Trim(), x => x.Value, StringComparer.OrdinalIgnoreCase); + if (config.Survival.MaxSelectedChoicesPerBucket <= 0) config.Survival.MaxSelectedChoicesPerBucket = 1; @@ -248,6 +259,7 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList config.Output.OutputDir = Prefer(Get("output-dir"), config.Output.OutputDir); config.Output.OutputNamePrefix = Prefer(Get("output-name-prefix"), config.Output.OutputNamePrefix); if (Has("export-external-learned-baselines")) config.Output.ExportExternalLearnedBaselines = true; + if (Has("reuse-existing-final-artifacts")) config.Output.ReuseExistingFinalArtifacts = true; if (int.TryParse(Get("max-selected-choices-per-bucket"), out var maxSelectedChoicesPerBucket) && maxSelectedChoicesPerBucket > 0) config.Survival.MaxSelectedChoicesPerBucket = maxSelectedChoicesPerBucket; @@ -290,6 +302,28 @@ private static List ParseDoubleList(string? value) private static string? Prefer(string? preferred, string? fallback) => string.IsNullOrWhiteSpace(preferred) ? fallback : preferred; + private static bool IsEmptyFrontmatterValue(object? value) + { + if (value == null) + return true; + + if (value is string text) + return string.IsNullOrWhiteSpace(text); + + if (value is System.Collections.IEnumerable sequence && value is not string) + { + foreach (var item in sequence) + { + if (!IsEmptyFrontmatterValue(item)) + return false; + } + + return true; + } + + return false; + } + private static string ResolveMagicQuantRoot(string? configured) { if (!string.IsNullOrWhiteSpace(configured)) @@ -317,4 +351,4 @@ private static List NormalizeScratchRoots(IEnumerable? roots) return Path.GetFullPath(value); } -} +} \ No newline at end of file diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 5737bb2..62e6a91 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -24,7 +24,15 @@ else { // Previous DEBUG harness kept intact for quick full-pipeline testing. - args = ["evolution", "--architecture-family", @"""Qwen3.6-35B-A3B"""]; + // --reuse-existing-final-artifacts preserves/reuses valid existing final GGUFs by exact file name + byte size. + // Omit --reuse-existing-final-artifacts to force normal full rebuild behavior. + // "--config", @"/path/to/config.dev.yaml", + args = + [ + "evolution", + "--architecture-family", @"""Qwen3.6-35B-A3B""" + ,"--reuse-existing-final-artifacts" + ]; } } else if (args.Length > 0 && @@ -107,4 +115,3 @@ await AnsiConsole.Status() { AnsiConsole.WriteException(ex); } - diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index 1a2738f..30ee725 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -48,7 +48,7 @@ public async Task> ExportAsync( throw new InvalidOperationException("Cache.OutputDirectory is not set."); Directory.CreateDirectory(Cache.OutputDirectory); - await CleanOutputDirectoryAsync(Cache.OutputDirectory!, ct); + await CleanOutputDirectoryAsync(Cache.OutputDirectory!, Config.ReuseExistingFinalArtifacts, ct); var output = new List(); var reservedFileNames = new HashSet(StringComparer.OrdinalIgnoreCase); @@ -109,6 +109,15 @@ public async Task> ExportAsync( EffectiveState = await _effectiveResolver.ResolveAsync(snap.Config, ct) }; + if (Config.ReuseExistingFinalArtifacts && + TryReuseExistingFinalArtifact(Cache.OutputDirectory!, row, name.FileName, out var existingFullPath, out var actualSizeBytes)) + { + record.ActualSizeBytes = actualSizeBytes; + AnsiConsole.MarkupLine($"[green]Reused existing final GGUF:[/] {Markup.Escape(existingFullPath)} [grey]({actualSizeBytes:N0} bytes matched benchmark truth)[/]"); + output.Add(record); + continue; + } + output.Add(record); localBuilds.Add((record, snap.Quant, fullPath, snap.SizeBytes)); } @@ -200,7 +209,38 @@ private static string ResolveReadmeProviderName(BenchmarkSnapshotRecord snapshot return HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); } - private static async Task CleanOutputDirectoryAsync(string outputDirectory, CancellationToken ct) + private static bool TryReuseExistingFinalArtifact( + string outputDirectory, + FinalSelectionRow row, + string plannedFileName, + out string fullPath, + out ulong actualSizeBytes) + { + actualSizeBytes = 0UL; + fullPath = Path.Combine(outputDirectory, plannedFileName); + + if (string.IsNullOrWhiteSpace(plannedFileName)) + return false; + + string expectedFileName = string.IsNullOrWhiteSpace(row.PlannedFileName) + ? plannedFileName + : row.PlannedFileName.Trim(); + + if (!string.Equals(Path.GetFileName(fullPath), expectedFileName, StringComparison.OrdinalIgnoreCase)) + return false; + + if (!File.Exists(fullPath)) + return false; + + var info = new FileInfo(fullPath); + if (info.Length <= 0) + return false; + + actualSizeBytes = (ulong)info.Length; + return actualSizeBytes == row.Snapshot.SizeBytes; + } + + private static async Task CleanOutputDirectoryAsync(string outputDirectory, bool preserveReusableGgufs, CancellationToken ct) { if (!Directory.Exists(outputDirectory)) { @@ -211,6 +251,14 @@ private static async Task CleanOutputDirectoryAsync(string outputDirectory, Canc foreach (var file in Directory.EnumerateFiles(outputDirectory, "*", SearchOption.TopDirectoryOnly)) { ct.ThrowIfCancellationRequested(); + + if (preserveReusableGgufs && + string.Equals(Path.GetExtension(file), ".gguf", StringComparison.OrdinalIgnoreCase) && + new FileInfo(file).Length > 0) + { + continue; + } + await HardDeleteHelper.DeleteFileIfExistsAsync(file); } @@ -220,7 +268,9 @@ private static async Task CleanOutputDirectoryAsync(string outputDirectory, Canc await HardDeleteHelper.DeleteDirectoryIfExistsAsync(directory, ct); } - AnsiConsole.MarkupLine($"[grey]Cleaned final export directory:[/] {Markup.Escape(outputDirectory)}"); + AnsiConsole.MarkupLine(preserveReusableGgufs + ? $"[grey]Cleaned final export directory metadata/non-GGUF files; preserved existing non-empty GGUFs for reuse validation:[/] {Markup.Escape(outputDirectory)}" + : $"[grey]Cleaned final export directory:[/] {Markup.Escape(outputDirectory)}"); } private static async Task CleanExportSidecarsAsync(string outputDirectory, CancellationToken ct) @@ -279,4 +329,4 @@ private static Task CopyImatrixArtifactsAsync(string outputDirectory, Cancellati return Task.CompletedTask; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/ModelSidecarArtifactService.cs b/MagicQuant/Services/ModelSidecarArtifactService.cs index 2c6c1e6..8e79d73 100644 --- a/MagicQuant/Services/ModelSidecarArtifactService.cs +++ b/MagicQuant/Services/ModelSidecarArtifactService.cs @@ -25,6 +25,8 @@ public sealed record MmprojArtifactResult public sealed class ModelSidecarArtifactService { + public const string CanonicalMmprojFileName = "mmproj-BF16.gguf"; + private static readonly string[] MultimodalTokens = [ "llava", "qwen2_vl", "qwen2_5_vl", "qwen3_vl", "gemma3", "internvl", "minicpm", "phi4mm", "glmv", "mllama", "idefics", "florence", "paligemma" @@ -73,6 +75,15 @@ public async Task CopyMmprojArtifactsAsync(string outputDi { var detection = DetectVisionCapability(); var warnings = new List(detection.Warnings); + Directory.CreateDirectory(outputDirectory); + string target = Path.Combine(outputDirectory, CanonicalMmprojFileName); + + if (Config.ReuseExistingFinalArtifacts && File.Exists(target) && new FileInfo(target).Length > 0) + { + AnsiConsole.MarkupLine($"[green]Reused existing mmproj artifact:[/] {Markup.Escape(target)}"); + return new MmprojArtifactResult { IsVisionCapable = detection.IsLikelyVisionCapable, ExistingFound = true, Copied = false, SourcePath = target, OutputPath = target, Warnings = warnings }; + } + string? source = FindExistingMmprojArtifact(); if (source == null) { @@ -90,9 +101,9 @@ public async Task CopyMmprojArtifactsAsync(string outputDi return new MmprojArtifactResult { IsVisionCapable = detection.IsLikelyVisionCapable, Warnings = warnings }; } - Directory.CreateDirectory(outputDirectory); - string target = Path.Combine(outputDirectory, Path.GetFileName(source)); - File.Copy(source, target, overwrite: true); + if (!string.Equals(Path.GetFullPath(source), Path.GetFullPath(target), StringComparison.OrdinalIgnoreCase)) + File.Copy(source, target, overwrite: true); + await Task.Yield(); AnsiConsole.MarkupLine($"[green]Copied mmproj artifact:[/] {Markup.Escape(target)}"); @@ -112,7 +123,19 @@ public async Task CopyMmprojArtifactsAsync(string outputDi Path.Combine(Cache.ModelMagicQuantDirectory!, "GGUF") }; - foreach (var root in roots.Distinct(StringComparer.OrdinalIgnoreCase)) + var distinctRoots = roots.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); + + foreach (var root in distinctRoots) + { + if (!Directory.Exists(root)) + continue; + + string canonical = Path.Combine(root, CanonicalMmprojFileName); + if (File.Exists(canonical) && new FileInfo(canonical).Length > 0) + return canonical; + } + + foreach (var root in distinctRoots) { if (!Directory.Exists(root)) continue; @@ -183,8 +206,7 @@ private async Task BuildMmprojArtifactAsync(List w { string sidecarDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Sidecars"); Directory.CreateDirectory(sidecarDir); - string safeName = new DirectoryInfo(Cache.ModelDirectory!).Name.Replace(' ', '-'); - string targetPath = Path.Combine(sidecarDir, $"mmproj-{safeName}-f16.gguf"); + string targetPath = Path.Combine(sidecarDir, CanonicalMmprojFileName); string successPath = targetPath + ".success.json"; string logPath = targetPath + ".convert.log"; @@ -240,4 +262,4 @@ private static MmprojArtifactResult HandleStrictRequirement(MmprojArtifactResult return result; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 8c9d416..4619e65 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -1,5 +1,7 @@ +using System.Globalization; using System.Text; using MagicQuant.Models; +using MQ.DB; using Spectre.Console; namespace MagicQuant.Services; @@ -27,17 +29,18 @@ public async Task GenerateAsync( .ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal); var sb = new StringBuilder(); - sb.AppendLine($"# MagicQuant Hybrids (v2.0) - {modelName}"); - sb.AppendLine(); - sb.AppendLine("MagicQuant is **not** a quantization technique by itself."); + AppendHuggingFaceFrontmatter(sb); + + string resolvedModelName = ResolveReadmeTitleModelName(modelName); + sb.AppendLine($"# MagicQuant Hybrids (v2.0) - {resolvedModelName}"); sb.AppendLine(); - sb.AppendLine("It is a search, judging, and hybrid-discovery system that learns from baseline families such as llama.cpp and external/custom baseline sources, then uses isolated samples, rank-safe prediction, and real benchmarking to keep the practical survivors."); + sb.AppendLine("MagicQuant is a benchmark driven GGUF hybrid discovery and validation system focused on finding real, practical GGUF quants specific to each architecture."); sb.AppendLine(); - sb.AppendLine("Sometimes a hybrid beats a pure baseline. Sometimes it does not. MagicQuant finds non linear good trades to discover potential better hybrids, good sub spaces between anchor baselines and more."); + sb.AppendLine("Whether it's a pure baseline model built by llama.cpp, learned tensor configurations from Unsloth, or a custom built MagicQuant hybrid, the model table below shows quants that have won dominance checks, survived collapse spaces, and/or were found to be nonlinearly better. Instead of dumping every quant type possible, MagicQuant tests, validates, and brutally murders anything deemed unworthy."); sb.AppendLine(); + sb.AppendLine("You can learn more [from the MagicQuant Wiki](https://github.com/magiccodingman/MagicQuant-Wiki). It covers things like nonlinear winners, prediction systems, imatrix generation philosophy, isolated tensor analysis, and more."); sb.AppendLine(); - sb.AppendLine("Read more on the [MagicQuant Wiki Here](https://github.com/magiccodingman/MagicQuant-Wiki)."); - sb.AppendLine("_The GitHub links is also a great place to make a request, bring up issues, share ideas, or anything else._"); + sb.AppendLine("By default, if an external provider like Unsloth is deemed the winner, the repo will generally link directly to the original provider instead of re-hosting the quant. External GGUFs are normally only re-uploaded when a specific winning variant does not already exist (e.g. Heretic models or similar)."); sb.AppendLine(); sb.AppendLine("---"); sb.AppendLine(); @@ -78,6 +81,152 @@ public async Task GenerateAsync( return readmePath; } + + private static void AppendHuggingFaceFrontmatter(StringBuilder sb) + { + var entries = OrderedFrontmatterEntries().ToList(); + if (entries.Count == 0) + return; + + sb.AppendLine("---"); + foreach (var (key, value) in entries) + { + if (TryGetSequence(value, out var values)) + { + var rendered = values + .Where(x => !IsEmptyFrontmatterValue(x)) + .Select(FormatYamlScalar) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .ToList(); + + if (rendered.Count == 0) + continue; + + sb.AppendLine($"{key}:"); + foreach (var item in rendered) + sb.AppendLine($"- {item}"); + } + else + { + if (IsEmptyFrontmatterValue(value)) + continue; + + sb.AppendLine($"{key}: {FormatYamlScalar(value)}"); + } + } + + sb.AppendLine("---"); + sb.AppendLine(); + } + + private static IEnumerable> OrderedFrontmatterEntries() + { + var frontmatter = Config.Current.Readme.Frontmatter; + if (frontmatter == null || frontmatter.Count == 0) + yield break; + + if (frontmatter.TryGetValue("license", out var license) && !IsEmptyFrontmatterValue(license)) + yield return new KeyValuePair("license", license); + + foreach (var entry in frontmatter) + { + if (string.IsNullOrWhiteSpace(entry.Key) || + string.Equals(entry.Key, "license", StringComparison.OrdinalIgnoreCase) || + IsEmptyFrontmatterValue(entry.Value)) + { + continue; + } + + yield return new KeyValuePair(entry.Key.Trim(), entry.Value); + } + } + + private static string ResolveReadmeTitleModelName(string fallbackModelName) + { + if (!string.IsNullOrWhiteSpace(Config.Current.Readme.TitleModelNameOverride)) + return Config.Current.Readme.TitleModelNameOverride.Trim(); + + if (!string.IsNullOrWhiteSpace(Config.Current.Identity.ArchitectureFamilyName)) + return Config.Current.Identity.ArchitectureFamilyName.Trim(); + + if (!string.IsNullOrWhiteSpace(Cache.CurrentArchitectureFamilyName)) + return Cache.CurrentArchitectureFamilyName.Trim(); + + return string.IsNullOrWhiteSpace(fallbackModelName) ? "model" : fallbackModelName.Trim(); + } + + private static bool TryGetSequence(object? value, out IReadOnlyList values) + { + values = Array.Empty(); + + if (value is string || value == null) + return false; + + if (value is System.Collections.IEnumerable sequence) + { + values = sequence.Cast().ToList(); + return true; + } + + return false; + } + + private static bool IsEmptyFrontmatterValue(object? value) + { + if (value == null) + return true; + + if (value is string text) + return string.IsNullOrWhiteSpace(text); + + if (TryGetSequence(value, out var values)) + return values.All(IsEmptyFrontmatterValue); + + return false; + } + + private static string FormatYamlScalar(object? value) + { + if (value == null) + return string.Empty; + + if (value is bool boolean) + return boolean ? "true" : "false"; + + if (value is IFormattable formattable && value is not string) + return formattable.ToString(null, CultureInfo.InvariantCulture) ?? string.Empty; + + string text = value.ToString() ?? string.Empty; + if (!NeedsYamlQuotes(text)) + return text; + + return "\"" + text + .Replace("\\", "\\\\", StringComparison.Ordinal) + .Replace("\"", "\\\"", StringComparison.Ordinal) + .Replace("\r", "\\r", StringComparison.Ordinal) + .Replace("\n", "\\n", StringComparison.Ordinal) + "\""; + } + + private static bool NeedsYamlQuotes(string text) + { + if (text.Length == 0) + return true; + + if (!string.Equals(text, text.Trim(), StringComparison.Ordinal)) + return true; + + if (text.Contains(": ", StringComparison.Ordinal) || + text.Contains("#", StringComparison.Ordinal) || + text.Contains("\n", StringComparison.Ordinal) || + text.Contains("\r", StringComparison.Ordinal)) + { + return true; + } + + char first = text[0]; + return first is '-' or '?' or ':' or '@' or '!' or '&' or '*' or '[' or ']' or '{' or '}' or '|' or '>' or '%' or '`' or ','; + } + private void AppendDownloadTable( StringBuilder sb, IReadOnlyCollection artifacts, @@ -217,4 +366,4 @@ private static void AppendCollapsible(StringBuilder sb, string summary, string b private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); private static string EscapeTooltip(string value) => (value ?? string.Empty).Replace("\"", """).Replace("|", " "); private static string EscapeHtml(string value) => (value ?? string.Empty).Replace("&", "&").Replace("<", "<").Replace(">", ">"); -} +} \ No newline at end of file diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index ad134fc..3874918 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -58,6 +58,31 @@ flags: # are allowed in hybrid generation logic. allow_high_precision_hybrids: false +readme: + # Optional title model name override used in: + # # MagicQuant Hybrids (v2.0) - + # If blank, MagicQuant uses identity.architecture_family_name. + title_model_name_override: + + # Hugging Face README frontmatter. + # Scalars render as: + # license: apache-2.0 + # Arrays render as: + # tags: + # - gguf + # - text-generation + # + # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. + frontmatter: + license: apache-2.0 + tags: + - gguf + - text-generation + - magicquant + - conversational + base_model: + - Username/Model_Name + hardware: # Optional per-GPU usable VRAM limits in GB. # Leave empty for automatic/default llama.cpp placement. @@ -204,6 +229,12 @@ output: # modified model where the upstream artifact does not really exist for your case). export_external_learned_baselines: false + # false = normal behavior; delete/rebuild final outputs from scratch. + # true = preserve valid existing GGUFs and skip rebuilding them only when + # exact file name + byte size match benchmark truth. + # CLI --reuse-existing-final-artifacts overrides YAML. + reuse_existing_final_artifacts: false + # Legacy bit-range bucket survival settings were removed. # See candidate_selection above for the active final chooser settings. @@ -314,4 +345,4 @@ baselines: # # Example note: # # If the repo does not actually contain IQ3_XS, do not reference it. # # Use only filenames that truly exist in the repository. - [] + [] \ No newline at end of file diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 6757737..4fa01f2 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -17,6 +17,31 @@ flags: force_refresh_hardware_probe: false allow_high_precision_hybrids: false +readme: + # Optional title model name override used in: + # # MagicQuant Hybrids (v2.0) - + # If blank, MagicQuant uses identity.architecture_family_name. + title_model_name_override: + + # Hugging Face README frontmatter. + # Scalars render as: + # license: apache-2.0 + # Arrays render as: + # tags: + # - gguf + # - text-generation + # + # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. + frontmatter: + license: apache-2.0 + tags: + - gguf + - text-generation + - magicquant + - conversational + base_model: + - Qwen/Qwen3.6-35B-A3B + hardware: gpu_memory_limits_gb: 0: 19 @@ -112,6 +137,12 @@ output: output_name_prefix: Qwen3.6-35B-A3B export_external_learned_baselines: false + # false = normal behavior; delete/rebuild final outputs from scratch. + # true = preserve valid existing GGUFs and skip rebuilding them only when + # exact file name + byte size match benchmark truth. + # CLI --reuse-existing-final-artifacts overrides YAML. + reuse_existing_final_artifacts: false + # Legacy bit-range bucket survival settings were removed. # See candidate_selection above for the active final chooser settings. From 3e8cabd14eda4a6282d244b5f5c903a2c3dc178e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 30 Apr 2026 16:27:27 -0400 Subject: [PATCH 176/258] Fixed GB representation to match HF --- MagicQuant/Models/HybridFinalizationModels.cs | 4 ++++ MagicQuant/Models/RepositoryCloneModels.cs | 2 ++ .../Services/CloneConfigManifestGenerationService.cs | 5 +++++ MagicQuant/Services/FinalReleaseMetadataService.cs | 9 ++++++++- MagicQuant/Services/FinalSurvivorSelectionCliService.cs | 2 +- MagicQuant/Services/HybridMapGenerationService.cs | 7 +++++++ MagicQuant/Services/ReadmeGenerationService.cs | 4 ++-- 7 files changed, 29 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Models/HybridFinalizationModels.cs b/MagicQuant/Models/HybridFinalizationModels.cs index f937da2..5aa347b 100644 --- a/MagicQuant/Models/HybridFinalizationModels.cs +++ b/MagicQuant/Models/HybridFinalizationModels.cs @@ -194,7 +194,11 @@ public sealed class HybridMapEntry public List Warnings { get; set; } = new(); public bool UsedImatrix { get; set; } public ulong ExpectedSizeBytes { get; set; } + public double ExpectedSizeGB { get; set; } + public double ExpectedSizeGiB { get; set; } public ulong? ActualSizeBytes { get; set; } + public double? ActualSizeGB { get; set; } + public double? ActualSizeGiB { get; set; } public string? OriginalExternalSource { get; set; } } diff --git a/MagicQuant/Models/RepositoryCloneModels.cs b/MagicQuant/Models/RepositoryCloneModels.cs index 6b61a40..c2dd59e 100644 --- a/MagicQuant/Models/RepositoryCloneModels.cs +++ b/MagicQuant/Models/RepositoryCloneModels.cs @@ -31,6 +31,8 @@ public sealed class MagicQuantCloneArtifact public double? SourcePpl { get; set; } public double? SourcePplDeltaPercent { get; set; } public ulong? SourceSizeBytes { get; set; } + public double? SourceSizeGB { get; set; } + public double? SourceSizeGiB { get; set; } /// /// Exact tensor-name -> final GGUF quant type map read from the exported artifact. diff --git a/MagicQuant/Services/CloneConfigManifestGenerationService.cs b/MagicQuant/Services/CloneConfigManifestGenerationService.cs index 9baf168..084c2ab 100644 --- a/MagicQuant/Services/CloneConfigManifestGenerationService.cs +++ b/MagicQuant/Services/CloneConfigManifestGenerationService.cs @@ -77,6 +77,8 @@ public async Task GenerateAsync( SourcePpl = artifact.Snapshot.Ppl, SourcePplDeltaPercent = FinalReleaseMetadataService.CalculatePplDeltaPercent(artifact.Snapshot.Ppl, referencePpl), SourceSizeBytes = artifact.ActualSizeBytes ?? artifact.ExpectedSizeBytes, + SourceSizeGB = ToGBNumber(artifact.ActualSizeBytes ?? artifact.ExpectedSizeBytes), + SourceSizeGiB = ToGiBNumber(artifact.ActualSizeBytes ?? artifact.ExpectedSizeBytes), TensorTypes = tensorTypes.ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal) }); } @@ -87,6 +89,9 @@ public async Task GenerateAsync( return path; } + private static double ToGBNumber(ulong bytes) => bytes / 1000d / 1000d / 1000d; + private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; + private static string ResolveBaseQuantName(ExportedArtifactRecord artifact) { var quant = artifact.Snapshot.Quant; diff --git a/MagicQuant/Services/FinalReleaseMetadataService.cs b/MagicQuant/Services/FinalReleaseMetadataService.cs index 9975af8..190dc33 100644 --- a/MagicQuant/Services/FinalReleaseMetadataService.cs +++ b/MagicQuant/Services/FinalReleaseMetadataService.cs @@ -73,6 +73,7 @@ private object ToSurvivorJson( ppl = x.Eliminated.Ppl, pplDeltaPercent = CalculatePplDeltaPercent(x.Eliminated.Ppl, referencePpl), sizeBytes = x.Eliminated.SizeBytes, + sizeGB = ToGBNumber(x.Eliminated.SizeBytes), sizeGiB = ToGiBNumber(x.Eliminated.SizeBytes), reasonCode = FinalArtifactNamingService.ReasonCode(x.Reason), reason = x.Reason @@ -102,6 +103,7 @@ private object ToSurvivorJson( ppl = artifact.Snapshot.Ppl, pplDeltaPercent = CalculatePplDeltaPercent(artifact.Snapshot.Ppl, referencePpl), sizeBytes = artifact.Snapshot.SizeBytes, + sizeGB = ToGBNumber(artifact.Snapshot.SizeBytes), sizeGiB = ToGiBNumber(artifact.Snapshot.SizeBytes), expectedSizeBytes = artifact.ExpectedSizeBytes, actualSizeBytes = artifact.ActualSizeBytes, @@ -136,7 +138,8 @@ private object ToReplacementJson( { kld = kldDelta, sizeBytes = sizeDeltaBytes, - sizeGiB = sizeDeltaBytes / 1024d / 1024d / 1024d, + sizeGB = ToGBNumber(sizeDeltaBytes), + sizeGiB = ToGiBNumber(sizeDeltaBytes), removedPplDeltaPercent = pplDeltaPercentRemoved, winnerPplDeltaPercent = pplDeltaPercentWinner, pplDeltaPercentImprovement = pplDeltaPercentImprovement @@ -200,6 +203,7 @@ private object ToReplacementSideJson( ppl = snapshot.Ppl, pplDeltaPercent = CalculatePplDeltaPercent(snapshot.Ppl, referencePpl), sizeBytes = snapshot.SizeBytes, + sizeGB = ToGBNumber(snapshot.SizeBytes), sizeGiB = ToGiBNumber(snapshot.SizeBytes) }; } @@ -304,5 +308,8 @@ private string ToSnapshotShortName( return _namingService.ToPublicArtifactShortName(display, null, provider, snapshot.BaselineFamily, snapshot, namingContext); } + private static double ToGBNumber(ulong bytes) => bytes / 1000d / 1000d / 1000d; private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; + private static double ToGBNumber(long bytes) => bytes / 1000d / 1000d / 1000d; + private static double ToGiBNumber(long bytes) => bytes / 1024d / 1024d / 1024d; } diff --git a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs index 1ecc59d..bfa45d7 100644 --- a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs +++ b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs @@ -108,7 +108,7 @@ private static void Render(IReadOnlyCollection rows, double? string kld = row.Enabled ? $"[cyan]{snap.Kld:0.000000}[/]" : $"[grey]{snap.Kld:0.000000}[/]"; string pplDelta = FormatPplDeltaPercent(snap.Ppl, referencePpl); string ppl = row.Enabled ? $"[cyan]{pplDelta}[/]" : $"[grey]{pplDelta}[/]"; - string sizeGb = (snap.SizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); + string sizeGb = (snap.SizeBytes / 1000d / 1000d / 1000d).ToString("0.00"); table.AddRow( row.Id.ToString(), diff --git a/MagicQuant/Services/HybridMapGenerationService.cs b/MagicQuant/Services/HybridMapGenerationService.cs index 2565971..aa21860 100644 --- a/MagicQuant/Services/HybridMapGenerationService.cs +++ b/MagicQuant/Services/HybridMapGenerationService.cs @@ -37,7 +37,11 @@ public async Task GenerateAsync( Warnings = x.EffectiveState?.Warnings.ToList() ?? new List(), UsedImatrix = Cache.UseImatrix && Cache.IsImatrixAvailable, ExpectedSizeBytes = x.ExpectedSizeBytes, + ExpectedSizeGB = ToGBNumber(x.ExpectedSizeBytes), + ExpectedSizeGiB = ToGiBNumber(x.ExpectedSizeBytes), ActualSizeBytes = x.ActualSizeBytes, + ActualSizeGB = x.ActualSizeBytes.HasValue ? ToGBNumber(x.ActualSizeBytes.Value) : null, + ActualSizeGiB = x.ActualSizeBytes.HasValue ? ToGiBNumber(x.ActualSizeBytes.Value) : null, OriginalExternalSource = HybridBenchmarkRepository.BuildExternalRepositoryUrl(x.Snapshot.Quant.BaseQuant) }) .ToList(); @@ -47,4 +51,7 @@ public async Task GenerateAsync( AnsiConsole.MarkupLine($"[green]Hybrid map JSON generated:[/] {Markup.Escape(path)}"); return path; } + + private static double ToGBNumber(ulong bytes) => bytes / 1000d / 1000d / 1000d; + private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; } diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 4619e65..b075fd9 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -44,7 +44,7 @@ public async Task GenerateAsync( sb.AppendLine(); sb.AppendLine("---"); sb.AppendLine(); - sb.AppendLine("## Final surviving downloadable outputs"); + sb.AppendLine("## Final survivors"); sb.AppendLine(); AppendDownloadTable(sb, exportedArtifacts, replacementMap, exportedByKey, namingContext); sb.AppendLine(); @@ -362,7 +362,7 @@ private static void AppendCollapsible(StringBuilder sb, string summary, string b sb.AppendLine(""); } - private static string ToGb(ulong bytes) => (bytes / 1024d / 1024d / 1024d).ToString("0.00"); + private static string ToGb(ulong bytes) => (bytes / 1000d / 1000d / 1000d).ToString("0.00", CultureInfo.InvariantCulture); private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); private static string EscapeTooltip(string value) => (value ?? string.Empty).Replace("\"", """).Replace("|", " "); private static string EscapeHtml(string value) => (value ?? string.Empty).Replace("&", "&").Replace("<", "<").Replace(">", ">"); From 05ed6c2273aaf0f3e947f7367c5da93927eb6130 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 1 May 2026 00:43:16 -0400 Subject: [PATCH 177/258] Tons of fixed stuff for cloning and building clone config and external config clones that don't exist in final output --- MagicQuant/Commands/CloneRepositoryQuants.cs | 279 +++++++++++++++++- MagicQuant/Program.cs | 6 +- .../CloneConfigManifestGenerationService.cs | 211 ++++++++++++- .../CombinationSurvivalPipelineService.cs | 94 ++++-- .../Services/HybridArtifactExportService.cs | 4 +- 5 files changed, 538 insertions(+), 56 deletions(-) diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 1e33d21..d1a9f8f 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -27,6 +27,8 @@ public sealed class CloneRepositoryQuants : ICommand "vocab.json" ]; + private static readonly string[] CloneBenchmarkDomains = ["general"]; + private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true @@ -78,7 +80,8 @@ public async Task Run(List args) AnsiConsole.MarkupLine($"Model Path: [blue]{Markup.Escape(Cache.ModelDirectory)}[/]"); AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); - + + AnsiConsole.MarkupLine($"Getting safetensors hash. This may take a bit, please wait..."); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); @@ -134,18 +137,36 @@ await File.WriteAllTextAsync( Path.Combine(Cache.OutputDirectory!, CloneConfigManifestGenerationService.FileName), JsonSerializer.Serialize(manifest, JsonOptions)); + string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; + string nativeQuantizationKey = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + string cloneBenchmarkRootDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "CloneBenchmarks"); + string nativeBenchDir = Path.Combine(cloneBenchmarkRootDir, nativeQuantizationKey); + string nativeLogitsDir = Path.Combine(nativeBenchDir, "logits"); + string pplCorporaDir = Path.Combine(cloneBenchmarkRootDir, "_ppl_corpora"); + await using var q8Lease = await quantizationService.BuildPureQ8ProbeLeaseAsync(); - await benchmarkService.EnsureExecutionPlanAsync( + await benchmarkService.EnsureDynamicExecutionPlanAsync( q8ModelPath: q8Lease.GgufPath, + nativeModelPath: baseModelGgufPath, + q8QuantizationKey: q8QuantizationKey, + nativeQuantizationKey: nativeQuantizationKey, discoveryTokenTarget: 8192, - quantizationKey: "Q8_0", forceRediscovery: Cache.ForceRefreshHardwareProbe); + await EnsureCloneNativeBenchmarkArtifactsReadyAsync( + benchmarkService: benchmarkService, + nativeModelQuant: HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), + nativeModelPath: baseModelGgufPath, + nativeBenchDir: nativeBenchDir, + nativeLogitsDir: nativeLogitsDir, + pplCorporaDir: pplCorporaDir); + var q8Reference = await benchmarkService.RunAllBenchmarksAsync( quantConfig: HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), modelPath: q8Lease.GgufPath, - benchDir: Path.Combine(Cache.ModelMagicQuantDirectory!, "CloneBenchmarks", "_reference_q8"), - domainsOverride: new[] { "general" }); + benchDir: Path.Combine(cloneBenchmarkRootDir, "_reference_q8"), + klLogitsDir: nativeLogitsDir, + domainsOverride: CloneBenchmarkDomains); double? referencePpl = q8Reference.Perplexity.TryGetValue("general", out var q8Ppl) && q8Ppl.Ppl > 0 ? q8Ppl.Ppl @@ -168,16 +189,16 @@ await quantizationService.BuildExportArtifactFromExactTensorMapAsync( baseQuantName: baseQuantName, forceRebuild: true); - var quantForBenchmark = HybridQuant.CreatePureBaseline( - BaselineQuants.ResolveBuiltInStandardBaseline(baseQuantName) - ?? BaselineQuants.ResolveBuiltInStandardBaseline(artifact.QuantFamily) - ?? BaselineQuants.Q8_0); + var benchmarkBaseline = ResolveCloneBenchmarkBaseline(baseQuantName, artifact.QuantFamily); + var quantForBenchmark = HybridQuant.CreatePureBaseline(benchmarkBaseline); + bool benchmarkRequiresKld = benchmarkBaseline.UniqueId != BaselineQuants.NativeSourceUniqueId; var bench = await benchmarkService.RunAllBenchmarksAsync( quantConfig: quantForBenchmark, modelPath: outputFile, - benchDir: Path.Combine(Cache.ModelMagicQuantDirectory!, "CloneBenchmarks", Path.GetFileNameWithoutExtension(artifact.FileName)), - domainsOverride: new[] { "general" }); + benchDir: Path.Combine(cloneBenchmarkRootDir, Path.GetFileNameWithoutExtension(artifact.FileName)), + klLogitsDir: benchmarkRequiresKld ? nativeLogitsDir : null, + domainsOverride: CloneBenchmarkDomains); var general = bench.Perplexity.TryGetValue("general", out var ppl) ? ppl : null; @@ -210,6 +231,240 @@ await quantizationService.BuildExportArtifactFromExactTensorMapAsync( AnsiConsole.MarkupLine("[bold green]Repository quant clone complete.[/]"); } + private static async Task EnsureCloneNativeBenchmarkArtifactsReadyAsync( + BenchmarkService benchmarkService, + HybridQuant nativeModelQuant, + string nativeModelPath, + string nativeBenchDir, + string nativeLogitsDir, + string pplCorporaDir) + { + var status = ValidateCloneNativeBenchmarkEnvironment( + nativeBenchDir: nativeBenchDir, + nativeLogitsDir: nativeLogitsDir, + pplCorporaDir: pplCorporaDir); + + if (status.IsValid) + { + AnsiConsole.MarkupLine("[grey]Clone native benchmark/KLD artifacts already exist and passed validation.[/]"); + return; + } + + AnsiConsole.Write(new Rule("[yellow]Clone Native Benchmark/KLD Artifact Validation[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine("[yellow]Clone native benchmark/KLD artifacts are missing or incomplete.[/] Regenerating required artifacts."); + PrintCloneNativeBenchmarkEnvironmentIssues(status); + + await ForceRegenerateCloneNativeBenchmarkArtifactsAsync( + benchmarkService: benchmarkService, + nativeModelQuant: nativeModelQuant, + nativeModelPath: nativeModelPath, + nativeBenchDir: nativeBenchDir, + nativeLogitsDir: nativeLogitsDir); + + status = ValidateCloneNativeBenchmarkEnvironment( + nativeBenchDir: nativeBenchDir, + nativeLogitsDir: nativeLogitsDir, + pplCorporaDir: pplCorporaDir); + + if (!status.IsValid) + { + var details = string.Join( + Environment.NewLine, + status.MissingOrInvalidArtifacts.Select(x => $"- {x}")); + + throw new InvalidOperationException( + "Clone native benchmark/logit generation completed, but required native benchmark artifacts are still missing or invalid. " + + "This is fatal because every cloned non-native benchmark requires complete native KLD logits." + + Environment.NewLine + + details); + } + + AnsiConsole.MarkupLine("[green]Clone native benchmark/KLD artifacts validated.[/]"); + } + + private static async Task ForceRegenerateCloneNativeBenchmarkArtifactsAsync( + BenchmarkService benchmarkService, + HybridQuant nativeModelQuant, + string nativeModelPath, + string nativeBenchDir, + string nativeLogitsDir) + { + if (Directory.Exists(nativeBenchDir)) + { + AnsiConsole.MarkupLine( + $"[grey]Clearing incomplete/stale clone native benchmark directory:[/] {Markup.Escape(nativeBenchDir)}"); + + Directory.Delete(nativeBenchDir, recursive: true); + } + + Directory.CreateDirectory(nativeBenchDir); + Directory.CreateDirectory(nativeLogitsDir); + + bool previousSuppressBenchmarkPersistence = Cache.SuppressBenchmarkPersistence; + + try + { + // Clone/export mode must not contaminate SQLite benchmark truth, but it still + // needs the same native KLD base-logit artifacts that evolution prepares. + Cache.SuppressBenchmarkPersistence = true; + + await benchmarkService.RunAllBenchmarksAsync( + quantConfig: nativeModelQuant, + modelPath: nativeModelPath, + benchDir: nativeBenchDir, + klLogitsDir: nativeLogitsDir, + saveLogits: true, + domainsOverride: CloneBenchmarkDomains); + } + finally + { + Cache.SuppressBenchmarkPersistence = previousSuppressBenchmarkPersistence; + } + } + + private static CloneNativeBenchmarkEnvironmentStatus ValidateCloneNativeBenchmarkEnvironment( + string nativeBenchDir, + string nativeLogitsDir, + string pplCorporaDir) + { + var issues = new List(); + + if (string.IsNullOrWhiteSpace(nativeBenchDir)) + { + issues.Add("Clone native benchmark directory path is null/empty."); + } + else if (!Directory.Exists(nativeBenchDir)) + { + issues.Add($"Clone native benchmark directory does not exist: {nativeBenchDir}"); + } + + if (string.IsNullOrWhiteSpace(nativeLogitsDir)) + { + issues.Add("Clone native KLD logits directory path is null/empty."); + } + else if (!Directory.Exists(nativeLogitsDir)) + { + issues.Add($"Clone native KLD logits directory does not exist: {nativeLogitsDir}"); + } + + if (string.IsNullOrWhiteSpace(pplCorporaDir)) + { + issues.Add("Clone _ppl_corpora directory path is null/empty."); + } + else if (!Directory.Exists(pplCorporaDir)) + { + issues.Add($"Clone _ppl_corpora directory does not exist: {pplCorporaDir}"); + } + else if (!Directory.EnumerateFiles(pplCorporaDir, "*", SearchOption.AllDirectories).Any()) + { + issues.Add($"Clone _ppl_corpora directory exists but contains no files: {pplCorporaDir}"); + } + + foreach (var domain in CloneBenchmarkDomains.OrderBy(x => x, StringComparer.Ordinal)) + { + if (!string.IsNullOrWhiteSpace(nativeBenchDir) && Directory.Exists(nativeBenchDir)) + { + var pplLog = Path.Combine(nativeBenchDir, $"perplexity_{domain}.log"); + + if (!File.Exists(pplLog)) + { + issues.Add($"Missing clone native perplexity log for domain '{domain}': {pplLog}"); + } + else if (new FileInfo(pplLog).Length <= 0) + { + issues.Add($"Clone native perplexity log is empty for domain '{domain}': {pplLog}"); + } + } + + if (!string.IsNullOrWhiteSpace(nativeLogitsDir) && Directory.Exists(nativeLogitsDir)) + { + var logitsFile = Path.Combine(nativeLogitsDir, $"kld_logits_{domain}.bin"); + + if (!File.Exists(logitsFile)) + { + issues.Add($"Missing clone native KLD logits for domain '{domain}': {logitsFile}"); + } + else if (new FileInfo(logitsFile).Length <= 0) + { + issues.Add($"Clone native KLD logits file is empty for domain '{domain}': {logitsFile}"); + } + } + } + + return new CloneNativeBenchmarkEnvironmentStatus( + IsValid: issues.Count == 0, + MissingOrInvalidArtifacts: issues); + } + + private static void PrintCloneNativeBenchmarkEnvironmentIssues(CloneNativeBenchmarkEnvironmentStatus status) + { + if (status.IsValid) + return; + + foreach (var issue in status.MissingOrInvalidArtifacts.Take(20)) + AnsiConsole.MarkupLine($"[grey]- {Markup.Escape(issue)}[/]"); + + if (status.MissingOrInvalidArtifacts.Count > 20) + { + AnsiConsole.MarkupLine( + $"[grey]- ...and {status.MissingOrInvalidArtifacts.Count - 20:N0} more issue(s).[/]"); + } + } + + private static BaselineQuants ResolveCloneBenchmarkBaseline(string? baseQuantName, string? quantFamily) + { + var native = BaselineQuants.GetBF16Quant(); + + foreach (var raw in new[] { baseQuantName, quantFamily }) + { + if (string.IsNullOrWhiteSpace(raw)) + continue; + + string name = raw.Trim(); + + if (IsNativeBaselineName(name, native)) + return native; + + var standard = BaselineQuants.ResolveBuiltInStandardBaseline(name); + if (standard != null) + return standard; + + var recognized = BaselineQuants.GetAllRecognizedBaselines() + .FirstOrDefault(x => BaselineNameMatches(x, name)); + + if (recognized != null) + return recognized; + } + + return BaselineQuants.Q8_0; + } + + private static bool IsNativeBaselineName(string name, BaselineQuants native) + { + if (string.IsNullOrWhiteSpace(name)) + return false; + + string normalized = name.Trim(); + + return string.Equals(normalized, "native", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, "native_source", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, "source", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(), StringComparison.OrdinalIgnoreCase) || + BaselineNameMatches(native, normalized); + } + + private static bool BaselineNameMatches(BaselineQuants baseline, string name) + { + return baseline.Names.Any(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase)) || + string.Equals(baseline.QuantizeBaseArgumentName, name, StringComparison.OrdinalIgnoreCase) || + string.Equals(baseline.PrimaryTensorWeightScheme.Names[0], name, StringComparison.OrdinalIgnoreCase) || + string.Equals(baseline.CanonicalKey, name, StringComparison.OrdinalIgnoreCase); + } + + private sealed record CloneNativeBenchmarkEnvironmentStatus( + bool IsValid, + IReadOnlyList MissingOrInvalidArtifacts); + private static async Task WriteCloneBenchmarkSummaryAsync(string outputDirectory, IReadOnlyCollection records) { var payload = records @@ -313,4 +568,4 @@ private static void ShowHelp() AnsiConsole.MarkupLine(" --source-json Local or http(s) path to magicquant.clone-configs.json"); AnsiConsole.MarkupLine(" --use-imatrix Use configured/provided imatrix for the cloned model"); } -} +} \ No newline at end of file diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 62e6a91..7a701ff 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -10,6 +10,7 @@ #if DEBUG if (args.Length == 0) { + // Use: "clone" or "evolution" const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) @@ -17,8 +18,9 @@ args = [ "clone-repository-quants", - "--architecture-family", @"""Qwen3-4B-Instruct-2507""", - "--source-repo", @"""magiccodingman/Qwen3-4B-Instruct-2507-Unsloth-MagicQuant-v2-GGUF""" + "--architecture-family", @"""Qwen3.6-35B-A3B""", + "--source-repo", @"""magiccodingman/Qwen3.6-35B-A3B-MagicQuant-GGUF""" + ,"--allow-architecture-family-alias-override" ]; } else diff --git a/MagicQuant/Services/CloneConfigManifestGenerationService.cs b/MagicQuant/Services/CloneConfigManifestGenerationService.cs index 084c2ab..e9598ae 100644 --- a/MagicQuant/Services/CloneConfigManifestGenerationService.cs +++ b/MagicQuant/Services/CloneConfigManifestGenerationService.cs @@ -1,6 +1,8 @@ +using System.Diagnostics; using System.Text.Json; using MagicQuant.Models; using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Services; @@ -15,6 +17,7 @@ public sealed class CloneConfigManifestGenerationService }; private readonly QuantizationService _quantizationService; + private readonly HybridBenchmarkRepository _benchmarkRepository = new(); private readonly FinalArtifactNamingService _namingService = new(); public CloneConfigManifestGenerationService(QuantizationService quantizationService) @@ -41,31 +44,51 @@ public async Task GenerateAsync( SourceJson = sourceJson, SourceModelId = Cache.CurrentModelId, SourceArchitectureFamily = Cache.CurrentArchitectureFamilyName, - Notes = "Exact GGUF tensor quantization map for repository clone/reproducibility mode. This file is not a proof that another cloned model went through the full MagicQuant evolution pipeline." + Notes = "Exact GGUF tensor quantization map for repository clone/reproducibility mode. This file is not a proof that another cloned model went through the full MagicQuant evolution pipeline. External reference finalists use persisted SQLite learned tensor truth when no local final GGUF was exported." }; double? referencePpl = ResolveReferencePpl(pplReference, exportedArtifacts.Select(x => x.Snapshot)); - foreach (var artifact in exportedArtifacts - .Where(x => !x.IsExternalReference) - .Where(x => !string.IsNullOrWhiteSpace(x.FullPath)) - .OrderBy(x => x.Snapshot.Kld) - .ThenBy(x => x.Snapshot.SizeBytes)) + var orderedArtifacts = exportedArtifacts + .OrderBy(x => x.Snapshot.Kld) + .ThenBy(x => x.Snapshot.SizeBytes) + .ThenBy(x => x.DisplayName, StringComparer.Ordinal) + .ToList(); + + WriteCloneLog($"Starting clone manifest generation for {orderedArtifacts.Count:N0} finalist artifacts."); + + int index = 0; + foreach (var artifact in orderedArtifacts) { ct.ThrowIfCancellationRequested(); + index++; - string fullPath = artifact.FullPath!; - if (!File.Exists(fullPath)) + var artifactSw = Stopwatch.StartNew(); + WriteCloneLog( + $"[{index:N0}/{orderedArtifacts.Count:N0}] Resolving tensor map for '{artifact.DisplayName}' " + + $"provider='{artifact.ProviderName}' family='{artifact.BaselineFamily}' externalReference={artifact.IsExternalReference} externalPureBaseline={artifact.Snapshot.IsExternalPureBaseline} hybrid={artifact.Snapshot.IsHybrid}."); + + CloneTensorMapResolution resolution; + try + { + resolution = await ResolveTensorTypesForCloneAsync(artifact, ct); + } + catch (Exception ex) { - AnsiConsole.MarkupLine($"[yellow]Skipping clone config for missing artifact:[/] {Markup.Escape(fullPath)}"); - continue; + artifactSw.Stop(); + WriteCloneLog( + $"[{index:N0}/{orderedArtifacts.Count:N0}] FAILED resolving tensor map for '{artifact.DisplayName}' after {FormatDuration(artifactSw.Elapsed)}: {ex.GetType().Name}: {ex.Message}", + isError: true); + throw; } - var tensorTypes = await _quantizationService.ReadExactTensorTypesAsync(fullPath, ct); + artifactSw.Stop(); + WriteCloneLog( + $"[{index:N0}/{orderedArtifacts.Count:N0}] Resolved '{artifact.DisplayName}' via {resolution.SourceDescription} in {FormatDuration(artifactSw.Elapsed)}; tensors={resolution.TensorTypes.Count:N0}."); manifest.Artifacts.Add(new MagicQuantCloneArtifact { - FileName = artifact.FileName ?? Path.GetFileName(fullPath), + FileName = ResolveManifestFileName(artifact), DisplayName = artifact.DisplayName, ShortName = _namingService.ToShortDisplayName(artifact.DisplayName), Provider = artifact.ProviderName, @@ -79,16 +102,107 @@ public async Task GenerateAsync( SourceSizeBytes = artifact.ActualSizeBytes ?? artifact.ExpectedSizeBytes, SourceSizeGB = ToGBNumber(artifact.ActualSizeBytes ?? artifact.ExpectedSizeBytes), SourceSizeGiB = ToGiBNumber(artifact.ActualSizeBytes ?? artifact.ExpectedSizeBytes), - TensorTypes = tensorTypes.ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal) + TensorTypes = resolution.TensorTypes.ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal) }); } string path = Path.Combine(outputDirectory, FileName); await File.WriteAllTextAsync(path, JsonSerializer.Serialize(manifest, JsonOptions), ct); - AnsiConsole.MarkupLine($"[green]Clone configuration JSON generated:[/] {Markup.Escape(path)}"); + WriteCloneLog($"Clone configuration JSON generated: {path} | artifacts={manifest.Artifacts.Count:N0}"); return path; } + private async Task ResolveTensorTypesForCloneAsync( + ExportedArtifactRecord artifact, + CancellationToken ct) + { + // Critical: an external reference means the final output directory intentionally does not contain + // a local GGUF for this finalist. Do not re-download the upstream GGUF here. The exact learned + // tensor truth was already captured in SQLite during learning/benchmarking, and that is the correct + // source for clone reproducibility metadata. + if (artifact.IsExternalReference) + { + var learned = await LoadExternalPureBaselineTensorTruthAsync(artifact, ct); + return new CloneTensorMapResolution(learned, "SQLite learned tensor truth for external reference"); + } + + if (!string.IsNullOrWhiteSpace(artifact.FullPath) && File.Exists(artifact.FullPath)) + { + var tensorTypes = await _quantizationService.ReadExactTensorTypesAsync(artifact.FullPath, ct); + return new CloneTensorMapResolution( + tensorTypes.ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal), + $"local final GGUF '{artifact.FullPath}'"); + } + + if (!string.IsNullOrWhiteSpace(artifact.Snapshot.OutputModelPath) && File.Exists(artifact.Snapshot.OutputModelPath)) + { + var tensorTypes = await _quantizationService.ReadExactTensorTypesAsync(artifact.Snapshot.OutputModelPath, ct); + return new CloneTensorMapResolution( + tensorTypes.ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal), + $"existing benchmark GGUF '{artifact.Snapshot.OutputModelPath}'"); + } + + if (artifact.Snapshot.IsExternalPureBaseline && !artifact.Snapshot.IsHybrid) + { + var learned = await LoadExternalPureBaselineTensorTruthAsync(artifact, ct); + return new CloneTensorMapResolution(learned, "SQLite learned tensor truth fallback for external pure baseline"); + } + + throw new InvalidOperationException( + $"Cannot generate clone tensor map for '{artifact.DisplayName}'. No local final GGUF exists at '{artifact.FullPath ?? ""}', " + + $"no existing benchmark GGUF exists at '{artifact.Snapshot.OutputModelPath ?? ""}', and the artifact is not an external pure baseline with persisted learned tensor truth."); + } + + private async Task> LoadExternalPureBaselineTensorTruthAsync( + ExportedArtifactRecord artifact, + CancellationToken ct) + { + var baseline = artifact.Snapshot.Quant.BaseQuant; + + if (!baseline.IsExternalRepositoryBaseline && !artifact.Snapshot.IsExternalPureBaseline) + { + throw new InvalidOperationException( + $"Artifact '{artifact.DisplayName}' was marked as an external reference, but its base quant '{baseline.Names[0]}' is not an external repository baseline and the snapshot is not marked as an external pure baseline."); + } + + if (string.IsNullOrWhiteSpace(baseline.CanonicalKey)) + { + throw new InvalidOperationException( + $"External baseline artifact '{artifact.DisplayName}' has no canonical baseline key, so SQLite learned tensor truth cannot be loaded."); + } + + WriteCloneLog( + $"Loading SQLite learned tensor truth for external baseline '{baseline.Names[0]}' canonicalKey='{baseline.CanonicalKey}' preferredScheme='{baseline.DefaultTensorScheme?.Names[0] ?? ""}'."); + + var strict = await _benchmarkRepository.LoadLearnedTensorMappingsAsync( + canonicalBaselineKey: baseline.CanonicalKey, + groupId: null, + preferredSourceScheme: baseline.DefaultTensorScheme, + allowDominantFallback: false, + ct: ct); + + if (strict.Count > 0) + return strict; + + WriteCloneLog( + $"Strict SQLite learned tensor truth lookup returned 0 rows for '{baseline.Names[0]}'. Trying dominant-scheme fallback for legacy/mixed rows.", + isWarning: true); + + var fallback = await _benchmarkRepository.LoadLearnedTensorMappingsAsync( + canonicalBaselineKey: baseline.CanonicalKey, + groupId: null, + preferredSourceScheme: baseline.DefaultTensorScheme, + allowDominantFallback: true, + ct: ct); + + if (fallback.Count > 0) + return fallback; + + throw new InvalidOperationException( + $"No SQLite learned tensor truth exists for external baseline '{baseline.Names[0]}' canonicalKey='{baseline.CanonicalKey}'. " + + "Final clone manifest generation will not re-download external GGUFs. Re-run the external baseline learning/benchmark stage for this model/context so the tensor truth is present in SQLite."); + } + private static double ToGBNumber(ulong bytes) => bytes / 1000d / 1000d / 1000d; private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; @@ -104,6 +218,54 @@ private static string ResolveBaseQuantName(ExportedArtifactRecord artifact) return "Q8_0"; } + private static string ResolveManifestFileName(ExportedArtifactRecord artifact) + { + if (!string.IsNullOrWhiteSpace(artifact.FileName)) + return artifact.FileName; + + if (!string.IsNullOrWhiteSpace(artifact.FullPath)) + return Path.GetFileName(artifact.FullPath); + + if (!string.IsNullOrWhiteSpace(artifact.Snapshot.OutputModelPath)) + return Path.GetFileName(artifact.Snapshot.OutputModelPath); + + string? targetName = TryGetFileNameFromDownloadTarget(artifact.DownloadTarget); + if (!string.IsNullOrWhiteSpace(targetName)) + return targetName; + + return ToSafeGgufFileName(artifact.DisplayName); + } + + private static string? TryGetFileNameFromDownloadTarget(string? downloadTarget) + { + if (string.IsNullOrWhiteSpace(downloadTarget)) + return null; + + string value = downloadTarget.Trim(); + int queryIndex = value.IndexOf('?', StringComparison.Ordinal); + if (queryIndex >= 0) + value = value[..queryIndex]; + + value = value.TrimEnd('/'); + string fileName = Path.GetFileName(value.Replace('\\', '/')); + return string.IsNullOrWhiteSpace(fileName) ? null : fileName; + } + + private static string ToSafeGgufFileName(string value) + { + string safe = new string((value ?? string.Empty) + .Select(ch => char.IsLetterOrDigit(ch) || ch is '.' or '_' or '-' ? ch : '_') + .ToArray()) + .Trim('_', '.', '-'); + + if (string.IsNullOrWhiteSpace(safe)) + safe = "external-baseline"; + + return safe.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase) + ? safe + : $"{safe}.gguf"; + } + private static double? ResolveReferencePpl( BenchmarkSnapshotRecord? pplReference, IEnumerable snapshots) @@ -117,4 +279,25 @@ private static string ResolveBaseQuantName(ExportedArtifactRecord artifact) .FirstOrDefault() ?.Ppl; } + + private static string FormatDuration(TimeSpan value) => value.ToString(@"hh\:mm\:ss"); + + private static void WriteCloneLog(string message, bool isWarning = false, bool isError = false) + { + string color = isError ? "red" : isWarning ? "yellow" : "grey"; + string line = $"[{DateTime.Now:HH:mm:ss}] Clone manifest: {message}"; + AnsiConsole.MarkupLine($"[{color}]{Markup.Escape(line)}[/]"); + } + + private sealed class CloneTensorMapResolution + { + public CloneTensorMapResolution(Dictionary tensorTypes, string sourceDescription) + { + TensorTypes = tensorTypes; + SourceDescription = sourceDescription; + } + + public Dictionary TensorTypes { get; } + public string SourceDescription { get; } + } } diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index 99e3ef5..46e4c00 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using MagicQuant.Helpers; using MagicQuant.Models; using MQ.DB; @@ -100,32 +101,42 @@ public async Task RunAsync(CancellationToken .ThenBy(x => x.SizeBytes) .ToList(); - await _diagnosticsLogService.WriteAsync(benchmarkOverview, selection.ValidationFailures, ct); - - await _hybridMapService.GenerateAsync(Cache.OutputDirectory!, exportedArtifacts, ct); - - await _releaseMetadataService.GenerateAsync( - Cache.OutputDirectory!, - exportedArtifacts, - selection.Eliminations, - pureBaselines, - nativeReference, - ct); - - await _cloneConfigManifestService.GenerateAsync( - Cache.OutputDirectory!, - exportedArtifacts, - nativeReference, - ct: ct); - - await _readmeService.GenerateAsync( - Cache.OutputDirectory!, - modelName, - exportedArtifacts, - pureBaselines, - selection.Eliminations, - nativeReference, - ct); + await RunFinalOutputStageAsync( + "selection diagnostics log", + () => _diagnosticsLogService.WriteAsync(benchmarkOverview, selection.ValidationFailures, ct)); + + await RunFinalOutputStageAsync( + "hybrid map JSON", + () => _hybridMapService.GenerateAsync(Cache.OutputDirectory!, exportedArtifacts, ct)); + + await RunFinalOutputStageAsync( + "final survivor / replacement metadata JSON", + () => _releaseMetadataService.GenerateAsync( + Cache.OutputDirectory!, + exportedArtifacts, + selection.Eliminations, + pureBaselines, + nativeReference, + ct)); + + await RunFinalOutputStageAsync( + "clone configuration manifest JSON", + () => _cloneConfigManifestService.GenerateAsync( + Cache.OutputDirectory!, + exportedArtifacts, + nativeReference, + ct: ct)); + + await RunFinalOutputStageAsync( + "README", + () => _readmeService.GenerateAsync( + Cache.OutputDirectory!, + modelName, + exportedArtifacts, + pureBaselines, + selection.Eliminations, + nativeReference, + ct)); return new CombinationSurvivalExecutionResult { @@ -140,6 +151,35 @@ await _readmeService.GenerateAsync( }; } + + private static async Task RunFinalOutputStageAsync(string stageName, Func action) + { + var sw = Stopwatch.StartNew(); + WriteFinalOutputLog($"START {stageName}"); + + try + { + await action(); + sw.Stop(); + WriteFinalOutputLog($"DONE {stageName} in {FormatDuration(sw.Elapsed)}"); + } + catch (Exception ex) + { + sw.Stop(); + WriteFinalOutputLog($"FAILED {stageName} after {FormatDuration(sw.Elapsed)}: {ex.GetType().Name}: {ex.Message}", isError: true); + throw; + } + } + + private static string FormatDuration(TimeSpan value) => value.ToString(@"hh\:mm\:ss"); + + private static void WriteFinalOutputLog(string message, bool isError = false) + { + string color = isError ? "red" : "grey"; + string line = $"[{DateTime.Now:HH:mm:ss}] Final output: {message}"; + AnsiConsole.MarkupLine($"[{color}]{Markup.Escape(line)}[/]"); + } + private void RenderEliminationSummary( IReadOnlyCollection eliminations, IReadOnlyCollection pureBaselineSnapshots) @@ -184,4 +224,4 @@ private void RenderEliminationSummary( AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in magicquant.replacements.json.[/]"); } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index 30ee725..707e460 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -83,6 +83,8 @@ public async Task> ExportAsync( ProviderName = provider, BaselineFamily = name.QuantFamilyOrBaseline, IsExternalReference = true, + FileName = name.FileName, + FullPath = null, DownloadTarget = snap.ExternalRepositoryUrl ?? string.Empty, ExpectedSizeBytes = snap.SizeBytes, EffectiveState = await _effectiveResolver.ResolveAsync(snap.Config, ct) @@ -329,4 +331,4 @@ private static Task CopyImatrixArtifactsAsync(string outputDirectory, Cancellati return Task.CompletedTask; } -} \ No newline at end of file +} From 5c9cb919ed0e16ca3af6ff7cd39fdbfdbe3f9cd8 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 1 May 2026 15:28:23 -0400 Subject: [PATCH 178/258] changed readme and cloning logic to unify more of the process. still some issues but working better. --- MagicQuant/Commands/CloneRepositoryQuants.cs | 589 ++++++++++++++---- .../Configuration/MagicQuantYamlLoader.cs | 2 +- MagicQuant/Program.cs | 3 +- .../CloneConfigManifestGenerationService.cs | 6 +- .../Services/CloneReadmeGenerationService.cs | 76 +-- .../CombinationSurvivalPipelineService.cs | 31 +- .../Services/FinalReleaseMetadataService.cs | 12 +- .../Services/HybridMapGenerationService.cs | 6 +- .../IsolationDiagnosticsManifestService.cs | 271 ++++++++ .../Services/IsolationOptimizationService.cs | 102 ++- .../Services/MagicQuantManifestPathService.cs | 97 +++ .../Services/ReadmeGenerationService.cs | 389 ++++++++++-- .../RepositoryCloneManifestService.cs | 62 +- 13 files changed, 1372 insertions(+), 274 deletions(-) create mode 100644 MagicQuant/Services/IsolationDiagnosticsManifestService.cs create mode 100644 MagicQuant/Services/MagicQuantManifestPathService.cs diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index d1a9f8f..ebe575a 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -31,7 +31,10 @@ public sealed class CloneRepositoryQuants : ICommand private static readonly JsonSerializerOptions JsonOptions = new() { - WriteIndented = true + WriteIndented = true, + PropertyNameCaseInsensitive = true, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true }; public async Task Run(List args) @@ -80,8 +83,9 @@ public async Task Run(List args) AnsiConsole.MarkupLine($"Model Path: [blue]{Markup.Escape(Cache.ModelDirectory)}[/]"); AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); - - AnsiConsole.MarkupLine($"Getting safetensors hash. This may take a bit, please wait..."); + AnsiConsole.MarkupLine($"Reuse final artifacts: {(Config.ReuseExistingFinalArtifacts ? "[green]yes[/]" : "[grey]no[/]")}"); + + AnsiConsole.MarkupLine("Getting safetensors hash. This may take a bit, please wait..."); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); @@ -128,105 +132,160 @@ public async Task Run(List args) var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest); RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Available); - await CleanOutputDirectoryAsync(Cache.OutputDirectory!); + var preCleanBenchmarkCache = Config.ReuseExistingFinalArtifacts + ? LoadReusableCloneBenchmarkRows(Cache.OutputDirectory!) + : new Dictionary(StringComparer.OrdinalIgnoreCase); - // Always place the clone source manifest in the output, but stamp it with this clone source. - manifest.SourceRepository = sourceRepo; - manifest.SourceJson = string.IsNullOrWhiteSpace(sourceRepo) ? sourceDescription : manifest.SourceJson; - await File.WriteAllTextAsync( - Path.Combine(Cache.OutputDirectory!, CloneConfigManifestGenerationService.FileName), - JsonSerializer.Serialize(manifest, JsonOptions)); - - string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; - string nativeQuantizationKey = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); - string cloneBenchmarkRootDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "CloneBenchmarks"); - string nativeBenchDir = Path.Combine(cloneBenchmarkRootDir, nativeQuantizationKey); - string nativeLogitsDir = Path.Combine(nativeBenchDir, "logits"); - string pplCorporaDir = Path.Combine(cloneBenchmarkRootDir, "_ppl_corpora"); - - await using var q8Lease = await quantizationService.BuildPureQ8ProbeLeaseAsync(); - await benchmarkService.EnsureDynamicExecutionPlanAsync( - q8ModelPath: q8Lease.GgufPath, - nativeModelPath: baseModelGgufPath, - q8QuantizationKey: q8QuantizationKey, - nativeQuantizationKey: nativeQuantizationKey, - discoveryTokenTarget: 8192, - forceRediscovery: Cache.ForceRefreshHardwareProbe); - - await EnsureCloneNativeBenchmarkArtifactsReadyAsync( - benchmarkService: benchmarkService, - nativeModelQuant: HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), - nativeModelPath: baseModelGgufPath, - nativeBenchDir: nativeBenchDir, - nativeLogitsDir: nativeLogitsDir, - pplCorporaDir: pplCorporaDir); + bool canReuseEverything = TryLoadFullyReusableCloneRecords( + outputDirectory: Cache.OutputDirectory!, + manifest: manifest, + benchmarkCache: preCleanBenchmarkCache, + records: out var reusableRecords); + + await CleanOutputDirectoryAsync(Cache.OutputDirectory!, Config.ReuseExistingFinalArtifacts); - var q8Reference = await benchmarkService.RunAllBenchmarksAsync( - quantConfig: HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), - modelPath: q8Lease.GgufPath, - benchDir: Path.Combine(cloneBenchmarkRootDir, "_reference_q8"), - klLogitsDir: nativeLogitsDir, - domainsOverride: CloneBenchmarkDomains); + var archivedManifestFiles = await CopySourceManifestFilesAsync( + outputDirectory: Cache.OutputDirectory!, + sourceManifestLocalPath: manifestLocalPath, + sourceRepo: sourceRepo, + sourceJson: sourceJson, + huggingFace: hf, + ct: CancellationToken.None); - double? referencePpl = q8Reference.Perplexity.TryGetValue("general", out var q8Ppl) && q8Ppl.Ppl > 0 - ? q8Ppl.Ppl - : null; + // Always place the clone source manifest in the output manifest folder, stamped with this clone source. + manifest.SourceRepository = sourceRepo; + manifest.SourceJson = string.IsNullOrWhiteSpace(sourceRepo) ? sourceDescription : manifest.SourceJson; + string outputCloneManifestPath = MagicQuantManifestPathService.GetManifestFilePath(Cache.OutputDirectory!, MagicQuantManifestPathService.CloneConfigsFileName); + await File.WriteAllTextAsync(outputCloneManifestPath, JsonSerializer.Serialize(manifest, JsonOptions)); + archivedManifestFiles.Add(MagicQuantManifestPathService.CloneConfigsFileName); - var records = new List(); + var records = canReuseEverything + ? reusableRecords + : new List(); - foreach (var artifact in manifest.Artifacts) + if (records.Count == manifest.Artifacts.Count) { - string outputFile = Path.Combine(Cache.OutputDirectory!, artifact.FileName); - string baseQuantName = string.IsNullOrWhiteSpace(artifact.BaseQuant) - ? artifact.QuantFamily - : artifact.BaseQuant; - - AnsiConsole.Write(new Rule($"[yellow]Clone Artifact: {Markup.Escape(artifact.FileName)}[/]") { Justification = Justify.Left }); - - await quantizationService.BuildExportArtifactFromExactTensorMapAsync( - tensorTypes: artifact.TensorTypes, - outputPath: outputFile, - baseQuantName: baseQuantName, - forceRebuild: true); - - var benchmarkBaseline = ResolveCloneBenchmarkBaseline(baseQuantName, artifact.QuantFamily); - var quantForBenchmark = HybridQuant.CreatePureBaseline(benchmarkBaseline); - bool benchmarkRequiresKld = benchmarkBaseline.UniqueId != BaselineQuants.NativeSourceUniqueId; - - var bench = await benchmarkService.RunAllBenchmarksAsync( - quantConfig: quantForBenchmark, - modelPath: outputFile, - benchDir: Path.Combine(cloneBenchmarkRootDir, Path.GetFileNameWithoutExtension(artifact.FileName)), - klLogitsDir: benchmarkRequiresKld ? nativeLogitsDir : null, - domainsOverride: CloneBenchmarkDomains); + AnsiConsole.MarkupLine($"[green]Reused clone artifacts and benchmark summary:[/] all {records.Count:N0} artifact(s) matched existing GGUF byte sizes and {MagicQuantManifestPathService.CloneBenchmarksFileName}."); + } + else + { + records.Clear(); + var benchmarkCache = preCleanBenchmarkCache; + + string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; + string nativeQuantizationKey = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + string cloneBenchmarkRootDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "CloneBenchmarks"); + string nativeBenchDir = Path.Combine(cloneBenchmarkRootDir, nativeQuantizationKey); + string nativeLogitsDir = Path.Combine(nativeBenchDir, "logits"); + string pplCorporaDir = Path.Combine(cloneBenchmarkRootDir, "_ppl_corpora"); + + bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && + await benchmarkService.TryInitializeDynamicExecutionPlanFromCacheAsync( + q8QuantizationKey: q8QuantizationKey, + nativeModelPath: baseModelGgufPath, + nativeQuantizationKey: nativeQuantizationKey); + + if (loadedPlanFromCache) + { + AnsiConsole.MarkupLine("[grey]Clone mode reused the DB-backed hardware execution plan; no Q8 probe rebuild was needed.[/]"); + } + else + { + AnsiConsole.MarkupLine(Cache.ForceRefreshHardwareProbe + ? "[yellow]Hardware probe refresh requested; rebuilding Q8 probe and updating SQLite execution-plan cache.[/]" + : "[grey]No reusable hardware execution-plan cache row found; building one Q8 probe and saving it to SQLite.[/]"); + + await using var q8Lease = await quantizationService.BuildPureQ8ProbeLeaseAsync(); + await benchmarkService.EnsureDynamicExecutionPlanAsync( + q8ModelPath: q8Lease.GgufPath, + nativeModelPath: baseModelGgufPath, + q8QuantizationKey: q8QuantizationKey, + nativeQuantizationKey: nativeQuantizationKey, + discoveryTokenTarget: 8192, + forceRediscovery: Cache.ForceRefreshHardwareProbe); + } - var general = bench.Perplexity.TryGetValue("general", out var ppl) ? ppl : null; + await EnsureCloneNativeBenchmarkArtifactsReadyAsync( + benchmarkService: benchmarkService, + nativeModelQuant: HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), + nativeModelPath: baseModelGgufPath, + nativeBenchDir: nativeBenchDir, + nativeLogitsDir: nativeLogitsDir, + pplCorporaDir: pplCorporaDir); - records.Add(new CloneArtifactBuildRecord + foreach (var artifact in manifest.Artifacts) { - ManifestArtifact = artifact, - OutputPath = outputFile, - ActualSizeBytes = File.Exists(outputFile) ? (ulong)new FileInfo(outputFile).Length : 0UL, - Kld = general?.Kld, - Ppl = general?.Ppl, - PplDeltaPercent = general != null && referencePpl is > 0d - ? FinalReleaseMetadataService.CalculatePplDeltaPercent(general.Ppl, referencePpl) - : null - }); + string outputFile = Path.Combine(Cache.OutputDirectory!, artifact.FileName); + string baseQuantName = string.IsNullOrWhiteSpace(artifact.BaseQuant) + ? artifact.QuantFamily + : artifact.BaseQuant; + + AnsiConsole.Write(new Rule($"[yellow]Clone Artifact: {Markup.Escape(artifact.FileName)}[/]") { Justification = Justify.Left }); + + if (TryReuseExistingCloneArtifactAndBenchmark(outputFile, artifact, benchmarkCache, out var cachedRecord)) + { + AnsiConsole.MarkupLine($"[green]Reused existing clone artifact + benchmark:[/] {Markup.Escape(outputFile)}"); + records.Add(cachedRecord); + continue; + } + + bool artifactExists = Config.ReuseExistingFinalArtifacts && File.Exists(outputFile) && new FileInfo(outputFile).Length > 0; + if (artifactExists) + { + AnsiConsole.MarkupLine($"[green]Reused existing clone GGUF:[/] {Markup.Escape(outputFile)} [grey](benchmark cache missing/stale; rebenchmarking only)[/]"); + } + else + { + await quantizationService.BuildExportArtifactFromExactTensorMapAsync( + tensorTypes: artifact.TensorTypes, + outputPath: outputFile, + baseQuantName: baseQuantName, + forceRebuild: true); + } + + var benchmarkBaseline = ResolveCloneBenchmarkBaseline(baseQuantName, artifact.QuantFamily); + var quantForBenchmark = HybridQuant.CreatePureBaseline(benchmarkBaseline); + bool benchmarkRequiresKld = benchmarkBaseline.UniqueId != BaselineQuants.NativeSourceUniqueId; + + var bench = await benchmarkService.RunAllBenchmarksAsync( + quantConfig: quantForBenchmark, + modelPath: outputFile, + benchDir: Path.Combine(cloneBenchmarkRootDir, Path.GetFileNameWithoutExtension(artifact.FileName)), + klLogitsDir: benchmarkRequiresKld ? nativeLogitsDir : null, + domainsOverride: CloneBenchmarkDomains); + + var general = bench.Perplexity.TryGetValue("general", out var ppl) ? ppl : null; + + records.Add(new CloneArtifactBuildRecord + { + ManifestArtifact = artifact, + OutputPath = outputFile, + ActualSizeBytes = File.Exists(outputFile) ? (ulong)new FileInfo(outputFile).Length : 0UL, + Kld = general?.Kld, + Ppl = general?.Ppl, + PplDeltaPercent = null + }); + } } + ApplyCloneReferencePplDeltas(records); + await CopyModelAdjacentFilesAsync(Cache.OutputDirectory!); await CopyImatrixArtifactsAsync(Cache.OutputDirectory!); await sidecarService.CopyMmprojArtifactsAsync(Cache.OutputDirectory!); + await WriteCloneBenchmarkSummaryAsync(Cache.OutputDirectory!, records); + archivedManifestFiles.Add(MagicQuantManifestPathService.CloneBenchmarksFileName); + await new CloneReadmeGenerationService().GenerateAsync( Cache.OutputDirectory!, new DirectoryInfo(Cache.ModelDirectory!).Name, sourceDescription, !string.IsNullOrWhiteSpace(sourceRepo), - records); + records, + archivedManifestFiles); - await WriteCloneBenchmarkSummaryAsync(Cache.OutputDirectory!, records); + await CleanCloneExportSidecarsAsync(Cache.OutputDirectory!); AnsiConsole.MarkupLine("[bold green]Repository quant clone complete.[/]"); } @@ -304,8 +363,6 @@ private static async Task ForceRegenerateCloneNativeBenchmarkArtifactsAsync( try { - // Clone/export mode must not contaminate SQLite benchmark truth, but it still - // needs the same native KLD base-logit artifacts that evolution prepares. Cache.SuppressBenchmarkPersistence = true; await benchmarkService.RunAllBenchmarksAsync( @@ -330,35 +387,21 @@ private static CloneNativeBenchmarkEnvironmentStatus ValidateCloneNativeBenchmar var issues = new List(); if (string.IsNullOrWhiteSpace(nativeBenchDir)) - { issues.Add("Clone native benchmark directory path is null/empty."); - } else if (!Directory.Exists(nativeBenchDir)) - { issues.Add($"Clone native benchmark directory does not exist: {nativeBenchDir}"); - } if (string.IsNullOrWhiteSpace(nativeLogitsDir)) - { issues.Add("Clone native KLD logits directory path is null/empty."); - } else if (!Directory.Exists(nativeLogitsDir)) - { issues.Add($"Clone native KLD logits directory does not exist: {nativeLogitsDir}"); - } if (string.IsNullOrWhiteSpace(pplCorporaDir)) - { issues.Add("Clone _ppl_corpora directory path is null/empty."); - } else if (!Directory.Exists(pplCorporaDir)) - { issues.Add($"Clone _ppl_corpora directory does not exist: {pplCorporaDir}"); - } else if (!Directory.EnumerateFiles(pplCorporaDir, "*", SearchOption.AllDirectories).Any()) - { issues.Add($"Clone _ppl_corpora directory exists but contains no files: {pplCorporaDir}"); - } foreach (var domain in CloneBenchmarkDomains.OrderBy(x => x, StringComparer.Ordinal)) { @@ -367,13 +410,9 @@ private static CloneNativeBenchmarkEnvironmentStatus ValidateCloneNativeBenchmar var pplLog = Path.Combine(nativeBenchDir, $"perplexity_{domain}.log"); if (!File.Exists(pplLog)) - { issues.Add($"Missing clone native perplexity log for domain '{domain}': {pplLog}"); - } else if (new FileInfo(pplLog).Length <= 0) - { issues.Add($"Clone native perplexity log is empty for domain '{domain}': {pplLog}"); - } } if (!string.IsNullOrWhiteSpace(nativeLogitsDir) && Directory.Exists(nativeLogitsDir)) @@ -381,13 +420,9 @@ private static CloneNativeBenchmarkEnvironmentStatus ValidateCloneNativeBenchmar var logitsFile = Path.Combine(nativeLogitsDir, $"kld_logits_{domain}.bin"); if (!File.Exists(logitsFile)) - { issues.Add($"Missing clone native KLD logits for domain '{domain}': {logitsFile}"); - } else if (new FileInfo(logitsFile).Length <= 0) - { issues.Add($"Clone native KLD logits file is empty for domain '{domain}': {logitsFile}"); - } } } @@ -455,40 +490,270 @@ private static bool IsNativeBaselineName(string name, BaselineQuants native) private static bool BaselineNameMatches(BaselineQuants baseline, string name) { - return baseline.Names.Any(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase)) || - string.Equals(baseline.QuantizeBaseArgumentName, name, StringComparison.OrdinalIgnoreCase) || - string.Equals(baseline.PrimaryTensorWeightScheme.Names[0], name, StringComparison.OrdinalIgnoreCase) || - string.Equals(baseline.CanonicalKey, name, StringComparison.OrdinalIgnoreCase); + if (string.IsNullOrWhiteSpace(name)) + return false; + + string normalized = name.Trim(); + + if (baseline.Names.Any(x => string.Equals(x, normalized, StringComparison.OrdinalIgnoreCase))) + return true; + + if (string.Equals(baseline.QuantizeBaseArgumentName, normalized, StringComparison.OrdinalIgnoreCase)) + return true; + + if (string.Equals(baseline.CanonicalKey, normalized, StringComparison.OrdinalIgnoreCase)) + return true; + + if (!string.IsNullOrWhiteSpace(baseline.ShortSourceName) && + string.Equals(baseline.ShortSourceName, normalized, StringComparison.OrdinalIgnoreCase)) + return true; + + if (!string.IsNullOrWhiteSpace(baseline.SourceFileName) && + string.Equals(Path.GetFileNameWithoutExtension(baseline.SourceFileName), normalized, StringComparison.OrdinalIgnoreCase)) + return true; + + if (baseline.PrimaryTensorWeightScheme.Names.Any(x => string.Equals(x, normalized, StringComparison.OrdinalIgnoreCase))) + return true; + + return false; + } + + private static void ApplyCloneReferencePplDeltas(IReadOnlyList records) + { + if (records.Count == 0) + return; + + var reference = records.FirstOrDefault(IsCloneQ8ReferenceRecord); + if (reference == null || !reference.Ppl.HasValue || reference.Ppl.Value <= 0d) + { + AnsiConsole.MarkupLine("[grey]Clone PPL delta reference unavailable; keeping any cached/source PPL delta values as-is.[/]"); + return; + } + + double referencePpl = reference.Ppl.Value; + foreach (var record in records) + { + if (record.Ppl.HasValue && record.Ppl.Value > 0d) + record.PplDeltaPercent = FinalReleaseMetadataService.CalculatePplDeltaPercent(record.Ppl.Value, referencePpl); + } + + AnsiConsole.MarkupLine( + $"[grey]Clone PPL deltas calculated from final Q8 artifact:[/] {Markup.Escape(reference.ManifestArtifact.FileName)}"); } - private sealed record CloneNativeBenchmarkEnvironmentStatus( - bool IsValid, - IReadOnlyList MissingOrInvalidArtifacts); + private static bool IsCloneQ8ReferenceRecord(CloneArtifactBuildRecord record) + { + var artifact = record.ManifestArtifact; + + foreach (var raw in new[] + { + artifact.BaseQuant, + artifact.QuantFamily, + artifact.DisplayName, + Path.GetFileNameWithoutExtension(artifact.FileName) + }) + { + if (string.IsNullOrWhiteSpace(raw)) + continue; + + if (BaselineNameMatches(BaselineQuants.Q8_0, raw.Trim())) + return true; + } + + return false; + } + + private static bool TryLoadFullyReusableCloneRecords( + string outputDirectory, + MagicQuantCloneManifest manifest, + IReadOnlyDictionary benchmarkCache, + out List records) + { + records = new List(); + + if (!Config.ReuseExistingFinalArtifacts || benchmarkCache.Count == 0) + return false; + + foreach (var artifact in manifest.Artifacts) + { + string outputFile = Path.Combine(outputDirectory, artifact.FileName); + if (!TryReuseExistingCloneArtifactAndBenchmark(outputFile, artifact, benchmarkCache, out var record)) + { + records.Clear(); + return false; + } + + records.Add(record); + } + + return records.Count == manifest.Artifacts.Count; + } + + private static bool TryReuseExistingCloneArtifactAndBenchmark( + string outputFile, + MagicQuantCloneArtifact artifact, + IReadOnlyDictionary benchmarkCache, + out CloneArtifactBuildRecord record) + { + record = default!; + + if (!Config.ReuseExistingFinalArtifacts) + return false; + + if (!File.Exists(outputFile)) + return false; + + var info = new FileInfo(outputFile); + if (info.Length <= 0) + return false; + + if (!benchmarkCache.TryGetValue(artifact.FileName, out var cached)) + return false; + + if (cached.SizeBytes != (ulong)info.Length) + return false; + + record = new CloneArtifactBuildRecord + { + ManifestArtifact = artifact, + OutputPath = outputFile, + ActualSizeBytes = cached.SizeBytes, + Kld = cached.Kld, + Ppl = cached.Ppl, + PplDeltaPercent = cached.PplDeltaPercent + }; + + return true; + } + + private static Dictionary LoadReusableCloneBenchmarkRows(string outputDirectory) + { + string path = MagicQuantManifestPathService.GetManifestFilePath(outputDirectory, MagicQuantManifestPathService.CloneBenchmarksFileName); + if (!File.Exists(path)) + return new Dictionary(StringComparer.OrdinalIgnoreCase); + + try + { + var rows = JsonSerializer.Deserialize>(File.ReadAllText(path), JsonOptions) + ?? new List(); + + return rows + .Where(x => !string.IsNullOrWhiteSpace(x.FileName) && x.SizeBytes > 0) + .GroupBy(x => x.FileName, StringComparer.OrdinalIgnoreCase) + .ToDictionary(g => g.Key, g => g.First(), StringComparer.OrdinalIgnoreCase); + } + catch (Exception ex) + { + AnsiConsole.MarkupLine($"[yellow]Existing clone benchmark summary could not be reused:[/] {Markup.Escape(ex.Message)}"); + return new Dictionary(StringComparer.OrdinalIgnoreCase); + } + } + + private static async Task> CopySourceManifestFilesAsync( + string outputDirectory, + string sourceManifestLocalPath, + string? sourceRepo, + string? sourceJson, + HuggingFaceBaselineService huggingFace, + CancellationToken ct) + { + string targetManifestDir = MagicQuantManifestPathService.EnsureManifestDirectory(outputDirectory); + var copied = new HashSet(StringComparer.OrdinalIgnoreCase); + + if (!string.IsNullOrWhiteSpace(sourceRepo)) + { + foreach (var fileName in MagicQuantManifestPathService.KnownManifestFileNames) + { + if (string.Equals(fileName, MagicQuantManifestPathService.CloneBenchmarksFileName, StringComparison.OrdinalIgnoreCase)) + continue; + + if (await TryDownloadOptionalSourceManifestFileAsync(sourceRepo.Trim(), fileName, Path.Combine(targetManifestDir, fileName), huggingFace, ct)) + copied.Add(fileName); + } + + return copied; + } + + string sourceDir = Path.GetDirectoryName(sourceManifestLocalPath) ?? string.Empty; + if (Directory.Exists(sourceDir)) + { + foreach (var file in Directory.EnumerateFiles(sourceDir, "magicquant*.json", SearchOption.TopDirectoryOnly)) + { + string fileName = Path.GetFileName(file); + if (string.Equals(fileName, MagicQuantManifestPathService.CloneBenchmarksFileName, StringComparison.OrdinalIgnoreCase)) + continue; + + File.Copy(file, Path.Combine(targetManifestDir, fileName), overwrite: true); + copied.Add(fileName); + } + } + + return copied; + } + + private static async Task TryDownloadOptionalSourceManifestFileAsync( + string repoId, + string fileName, + string destinationPath, + HuggingFaceBaselineService huggingFace, + CancellationToken ct) + { + var candidates = new[] + { + MagicQuantManifestPathService.RelativeManifestPath(fileName), + fileName + }; + + foreach (var candidate in candidates) + { + try + { + await huggingFace.DownloadRepositoryFileAsync( + repoId: repoId, + fileName: candidate, + destinationPath: destinationPath, + forceRedownload: true, + ct: ct); + + AnsiConsole.MarkupLine($"[green]Archived source manifest file:[/] {Markup.Escape(candidate)}"); + return true; + } + catch + { + // Optional source manifest sidecars may not exist, especially in older repos. + } + } + + AnsiConsole.MarkupLine($"[grey]Optional source manifest file unavailable:[/] {Markup.Escape(fileName)}"); + return false; + } private static async Task WriteCloneBenchmarkSummaryAsync(string outputDirectory, IReadOnlyCollection records) { + string path = MagicQuantManifestPathService.GetManifestFilePath(outputDirectory, MagicQuantManifestPathService.CloneBenchmarksFileName); + var payload = records .OrderBy(x => x.Kld ?? double.MaxValue) .ThenBy(x => x.ActualSizeBytes) - .Select(x => new + .Select(x => new CloneBenchmarkCacheRow { - fileName = x.ManifestArtifact.FileName, - displayName = x.ManifestArtifact.DisplayName, - provider = x.ManifestArtifact.Provider, - quantFamily = x.ManifestArtifact.QuantFamily, - baseQuant = x.ManifestArtifact.BaseQuant, - kld = x.Kld, - ppl = x.Ppl, - pplDeltaPercent = x.PplDeltaPercent, - sizeBytes = x.ActualSizeBytes, - sizeGiB = x.ActualSizeBytes / 1024d / 1024d / 1024d, - sourceKld = x.ManifestArtifact.SourceKld, - sourcePpl = x.ManifestArtifact.SourcePpl, - sourceSizeBytes = x.ManifestArtifact.SourceSizeBytes + FileName = x.ManifestArtifact.FileName, + DisplayName = x.ManifestArtifact.DisplayName, + Provider = x.ManifestArtifact.Provider, + QuantFamily = x.ManifestArtifact.QuantFamily, + BaseQuant = x.ManifestArtifact.BaseQuant, + Kld = x.Kld, + Ppl = x.Ppl, + PplDeltaPercent = x.PplDeltaPercent, + SizeBytes = x.ActualSizeBytes, + SizeGB = x.ActualSizeBytes / 1000d / 1000d / 1000d, + SizeGiB = x.ActualSizeBytes / 1024d / 1024d / 1024d, + SourceKld = x.ManifestArtifact.SourceKld, + SourcePpl = x.ManifestArtifact.SourcePpl, + SourceSizeBytes = x.ManifestArtifact.SourceSizeBytes }) .ToList(); - string path = Path.Combine(outputDirectory, "magicquant.clone-benchmarks.json"); await File.WriteAllTextAsync(path, JsonSerializer.Serialize(payload, JsonOptions)); AnsiConsole.MarkupLine($"[green]Clone benchmark summary generated:[/] {Markup.Escape(path)}"); } @@ -520,17 +785,49 @@ private static Task CopyImatrixArtifactsAsync(string outputDirectory) return Task.CompletedTask; } - private static async Task CleanOutputDirectoryAsync(string outputDirectory) + private static async Task CleanOutputDirectoryAsync(string outputDirectory, bool preserveReusableGgufs) { Directory.CreateDirectory(outputDirectory); foreach (var file in Directory.EnumerateFiles(outputDirectory, "*", SearchOption.TopDirectoryOnly)) + { + if (preserveReusableGgufs && + string.Equals(Path.GetExtension(file), ".gguf", StringComparison.OrdinalIgnoreCase) && + new FileInfo(file).Length > 0) + { + continue; + } + await HardDeleteHelper.DeleteFileIfExistsAsync(file); + } foreach (var directory in Directory.EnumerateDirectories(outputDirectory, "*", SearchOption.TopDirectoryOnly)) - Directory.Delete(directory, recursive: true); + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(directory, CancellationToken.None); + + AnsiConsole.MarkupLine(preserveReusableGgufs + ? $"[grey]Cleaned clone export metadata/non-GGUF files; preserved existing non-empty GGUFs for reuse validation:[/] {Markup.Escape(outputDirectory)}" + : $"[grey]Cleaned clone export directory:[/] {Markup.Escape(outputDirectory)}"); + } - AnsiConsole.MarkupLine($"[grey]Cleaned clone export directory:[/] {Markup.Escape(outputDirectory)}"); + private static async Task CleanCloneExportSidecarsAsync(string outputDirectory) + { + string[] patterns = + [ + "*.success.json", + "*.quantize.log", + "*.convert.log", + "imatrix.success.json", + "imatrix.metadata.json", + "imatrix.build.log" + ]; + + foreach (var pattern in patterns) + { + foreach (var file in Directory.EnumerateFiles(outputDirectory, pattern, SearchOption.TopDirectoryOnly)) + await HardDeleteHelper.DeleteFileIfExistsAsync(file); + } + + AnsiConsole.MarkupLine($"[grey]Cleaned clone export sidecar success/log files:[/] {Markup.Escape(outputDirectory)}"); } private static async Task EnsureSqliteReadyAsync() @@ -561,11 +858,35 @@ private static void ShowHelp() AnsiConsole.MarkupLine("[bold yellow]Command: clone-repository-quants[/]"); AnsiConsole.MarkupLine("Rebuilds the final GGUF list from a MagicQuant-compatible tensor config manifest without running the evolution/search pipeline."); AnsiConsole.MarkupLine("Usage:"); - AnsiConsole.MarkupLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-repo \"owner/repo\" [--output-dir \"\"]"); - AnsiConsole.MarkupLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-json \"\" [--output-dir \"\"]"); + AnsiConsole.MarkupLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-repo \"owner/repo\" [--output-dir \"\"] [--reuse-existing-final-artifacts]"); + AnsiConsole.MarkupLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-json \"\" [--output-dir \"\"] [--reuse-existing-final-artifacts]"); AnsiConsole.MarkupLine("Options:"); - AnsiConsole.MarkupLine(" --source-repo Hugging Face repo containing magicquant.clone-configs.json"); + AnsiConsole.MarkupLine($" --source-repo Hugging Face repo containing {MagicQuantManifestPathService.RelativeManifestPath(MagicQuantManifestPathService.CloneConfigsFileName)} or legacy root {MagicQuantManifestPathService.CloneConfigsFileName}"); AnsiConsole.MarkupLine(" --source-json Local or http(s) path to magicquant.clone-configs.json"); AnsiConsole.MarkupLine(" --use-imatrix Use configured/provided imatrix for the cloned model"); + AnsiConsole.MarkupLine(" --reuse-existing-final-artifacts Reuse matching existing GGUFs and matching clone benchmark JSON rows"); + AnsiConsole.MarkupLine(" --recheck-hardware-probe / --force-refresh-hardware-probe Force Q8/native hardware probe and refresh the SQLite execution-plan cache"); + } + + private sealed record CloneNativeBenchmarkEnvironmentStatus( + bool IsValid, + IReadOnlyList MissingOrInvalidArtifacts); + + private sealed class CloneBenchmarkCacheRow + { + public string FileName { get; set; } = string.Empty; + public string DisplayName { get; set; } = string.Empty; + public string Provider { get; set; } = string.Empty; + public string QuantFamily { get; set; } = string.Empty; + public string BaseQuant { get; set; } = string.Empty; + public double? Kld { get; set; } + public double? Ppl { get; set; } + public double? PplDeltaPercent { get; set; } + public ulong SizeBytes { get; set; } + public double SizeGB { get; set; } + public double SizeGiB { get; set; } + public double? SourceKld { get; set; } + public double? SourcePpl { get; set; } + public ulong? SourceSizeBytes { get; set; } } -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 1bce22f..9669b1c 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -203,7 +203,7 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (Has("use-imatrix")) config.Flags.UseImatrix = true; if (Has("imatrix-force-rebuild")) config.Flags.ForceImatrixRebuild = true; if (Has("relearn-baseline-mappings")) config.Flags.ForceRelearnBaselineTensorMappings = true; - if (Has("recheck-hardware-probe")) config.Flags.ForceRefreshHardwareProbe = true; + if (Has("recheck-hardware-probe") || Has("force-refresh-hardware-probe") || Has("force_refresh_hardware_probe")) config.Flags.ForceRefreshHardwareProbe = true; if (Has("allow-high-precision-hybrids")) config.Flags.AllowHighPrecisionHybrids = true; config.Imatrix.ImatrixUrl = Prefer(Get("imatrix-url"), config.Imatrix.ImatrixUrl); diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 7a701ff..53ba34e 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -11,7 +11,7 @@ if (args.Length == 0) { // Use: "clone" or "evolution" - const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. + const string debugMode = "clone"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) { @@ -21,6 +21,7 @@ "--architecture-family", @"""Qwen3.6-35B-A3B""", "--source-repo", @"""magiccodingman/Qwen3.6-35B-A3B-MagicQuant-GGUF""" ,"--allow-architecture-family-alias-override" + , "--reuse-existing-final-artifacts" ]; } else diff --git a/MagicQuant/Services/CloneConfigManifestGenerationService.cs b/MagicQuant/Services/CloneConfigManifestGenerationService.cs index e9598ae..9761fe4 100644 --- a/MagicQuant/Services/CloneConfigManifestGenerationService.cs +++ b/MagicQuant/Services/CloneConfigManifestGenerationService.cs @@ -9,7 +9,7 @@ namespace MagicQuant.Services; public sealed class CloneConfigManifestGenerationService { - public const string FileName = "magicquant.clone-configs.json"; + public const string FileName = MagicQuantManifestPathService.CloneConfigsFileName; private static readonly JsonSerializerOptions JsonOptions = new() { @@ -33,7 +33,7 @@ public async Task GenerateAsync( string? sourceJson = null, CancellationToken ct = default) { - Directory.CreateDirectory(outputDirectory); + string manifestDirectory = MagicQuantManifestPathService.EnsureManifestDirectory(outputDirectory); var manifest = new MagicQuantCloneManifest { @@ -106,7 +106,7 @@ public async Task GenerateAsync( }); } - string path = Path.Combine(outputDirectory, FileName); + string path = Path.Combine(manifestDirectory, FileName); await File.WriteAllTextAsync(path, JsonSerializer.Serialize(manifest, JsonOptions), ct); WriteCloneLog($"Clone configuration JSON generated: {path} | artifacts={manifest.Artifacts.Count:N0}"); return path; diff --git a/MagicQuant/Services/CloneReadmeGenerationService.cs b/MagicQuant/Services/CloneReadmeGenerationService.cs index 1b7e300..bc7e1c2 100644 --- a/MagicQuant/Services/CloneReadmeGenerationService.cs +++ b/MagicQuant/Services/CloneReadmeGenerationService.cs @@ -1,75 +1,31 @@ -using System.Text; using MagicQuant.Models; -using MQ.DB; -using Spectre.Console; namespace MagicQuant.Services; +/// +/// Compatibility wrapper kept so existing call sites can move over gradually. +/// The actual README body/table/frontmatter logic is centralized in ReadmeGenerationService. +/// public sealed class CloneReadmeGenerationService { - public async Task GenerateAsync( + private readonly ReadmeGenerationService _readmeGenerationService = new(); + + public Task GenerateAsync( string outputDirectory, string modelName, string sourceDescription, bool sourceWasHuggingFaceRepo, IReadOnlyCollection records, + IReadOnlyCollection? archivedManifestFileNames = null, CancellationToken ct = default) { - Directory.CreateDirectory(outputDirectory); - string path = Path.Combine(outputDirectory, "README.md"); - - var sb = new StringBuilder(); - - sb.AppendLine($"# {modelName} - MagicQuant Clone Build"); - sb.AppendLine(); - sb.AppendLine("This repository was built in **MagicQuant repository clone mode**."); - sb.AppendLine(); - sb.AppendLine("That means these GGUF files copied exact tensor quantization configurations from an existing MagicQuant-compatible release, then rebuilt and benchmarked those tensor maps against this model locally."); - sb.AppendLine(); - sb.AppendLine("> Important: this model did **not** run through the full MagicQuant probing/evolution/search pipeline by itself. It reused tensor configurations from another MagicQuant release and then generated fresh local benchmark metadata for this output."); - sb.AppendLine(); - sb.AppendLine("## Clone source"); - sb.AppendLine(); - if (sourceWasHuggingFaceRepo) - sb.AppendLine($"- Source Hugging Face repository: `{sourceDescription}`"); - else - sb.AppendLine($"- Source clone JSON: `{sourceDescription}`"); - sb.AppendLine($"- Clone config file: [`{CloneConfigManifestGenerationService.FileName}`](./../../resolve/main/{CloneConfigManifestGenerationService.FileName}?download=true)"); - sb.AppendLine(); - sb.AppendLine("## Downloadable outputs"); - sb.AppendLine(); - sb.AppendLine("| Name | Provider | Quant Family | KLD | PPL Δ % | Size (GB) | Download |"); - sb.AppendLine("|---|---|---|---:|---:|---:|---|"); - - foreach (var record in records.OrderBy(x => x.Kld ?? double.MaxValue).ThenBy(x => x.ActualSizeBytes)) - { - var a = record.ManifestArtifact; - string name = EscapePipe(string.IsNullOrWhiteSpace(a.ShortName) ? Path.GetFileNameWithoutExtension(a.FileName) : a.ShortName); - string provider = EscapePipe(string.IsNullOrWhiteSpace(a.Provider) ? "Cloned config" : a.Provider); - string family = EscapePipe(string.IsNullOrWhiteSpace(a.QuantFamily) ? a.BaseQuant : a.QuantFamily); - string kld = record.Kld.HasValue ? record.Kld.Value.ToString("0.000000") : "n/a"; - string ppl = record.PplDeltaPercent.HasValue ? $"{record.PplDeltaPercent.Value:0.000}%" : "n/a"; - string size = (record.ActualSizeBytes / 1024d / 1024d / 1024d).ToString("0.00"); - sb.AppendLine($"| {name} | {provider} | {family} | {kld} | {ppl} | {size} | [Link](./../../resolve/main/{Uri.EscapeDataString(a.FileName)}?download=true) |"); - } - - sb.AppendLine(); - sb.AppendLine("## Reproducibility"); - sb.AppendLine(); - sb.AppendLine($"The file `{CloneConfigManifestGenerationService.FileName}` stores the exact `tensor name -> quant type` map used to rebuild each GGUF. A future clone run can use that JSON directly with `--source-json`, or a Hugging Face repository containing that file with `--source-repo`."); - sb.AppendLine(); - sb.AppendLine("## Support"); - sb.AppendLine(); - sb.AppendLine("I’m a solo developer working full time for myself to achieve my dream. If you like any of my work, buying me a coffee is always appreciated. Otherwise, good vibes are also accepted as legal tender."); - sb.AppendLine(); - sb.AppendLine("[Click here to see ways to support](https://sayou.biz/support) - BTC, Paypal, GitHub sponsors."); - sb.AppendLine(); - - await File.WriteAllTextAsync(path, sb.ToString(), ct); - AnsiConsole.MarkupLine($"[green]Clone README generated:[/] {Markup.Escape(path)}"); - return path; + return _readmeGenerationService.GenerateCloneAsync( + outputDirectory, + modelName, + sourceDescription, + sourceWasHuggingFaceRepo, + records, + archivedManifestFileNames, + ct); } - - private static string EscapePipe(string value) - => (value ?? string.Empty).Replace("|", "\\|"); } diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index 46e4c00..93c6218 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -25,6 +25,7 @@ public sealed class CombinationSurvivalPipelineService private readonly FinalReleaseMetadataService _releaseMetadataService; private readonly CloneConfigManifestGenerationService _cloneConfigManifestService; private readonly FinalArtifactNamingService _namingService; + private readonly IsolationDiagnosticsManifestService _isolationDiagnosticsManifestService; public CombinationSurvivalPipelineService(QuantizationService quantizationService) { @@ -46,9 +47,13 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic _releaseMetadataService = new FinalReleaseMetadataService(); _cloneConfigManifestService = new CloneConfigManifestGenerationService(_quantizationService); _namingService = new FinalArtifactNamingService(); + _isolationDiagnosticsManifestService = new IsolationDiagnosticsManifestService(); } - public async Task RunAsync(CancellationToken ct = default) + public async Task RunAsync( + RequiredSampleGenerationResult? isolationSamplePlan = null, + IsolationOptimizationResult? isolationOptimizationResult = null, + CancellationToken ct = default) { var report = new SurvivalStageReport { @@ -127,6 +132,26 @@ await RunFinalOutputStageAsync( nativeReference, ct: ct)); + if (isolationSamplePlan != null) + { + await RunFinalOutputStageAsync( + "isolation sample manifest JSON", + () => _isolationDiagnosticsManifestService.GenerateIsolationSamplesAsync( + Cache.OutputDirectory!, + isolationSamplePlan, + ct)); + } + + if (isolationOptimizationResult != null) + { + await RunFinalOutputStageAsync( + "bad trade manifest JSON", + () => _isolationDiagnosticsManifestService.GenerateBadTradesAsync( + Cache.OutputDirectory!, + isolationOptimizationResult, + ct)); + } + await RunFinalOutputStageAsync( "README", () => _readmeService.GenerateAsync( @@ -205,7 +230,7 @@ private void RenderEliminationSummary( .Take(25)) { double kldDelta = row.Eliminated.Kld - row.Eliminator.Kld; - double sizeDeltaGb = (row.Eliminated.SizeBytes - (double)row.Eliminator.SizeBytes) / 1024d / 1024d / 1024d; + double sizeDeltaGb = (row.Eliminated.SizeBytes - (double)row.Eliminator.SizeBytes) / 1000d / 1000d / 1000d; string removed = _namingService.ToShortDisplayName(_namingService.BuildDisplayLabel(row.Eliminated, namingContext)); string winner = _namingService.ToShortDisplayName(_namingService.BuildDisplayLabel(row.Eliminator, namingContext)); string code = FinalArtifactNamingService.ReasonCode(row.Reason); @@ -221,7 +246,7 @@ private void RenderEliminationSummary( AnsiConsole.Write(table); if (eliminations.Count > 25) - AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in magicquant.replacements.json.[/]"); + AnsiConsole.MarkupLine($"[grey]Showing first 25 of {eliminations.Count:N0} elimination records. Full details are in magicquant-manifest/magicquant.replacements.json.[/]"); } } diff --git a/MagicQuant/Services/FinalReleaseMetadataService.cs b/MagicQuant/Services/FinalReleaseMetadataService.cs index 190dc33..2377332 100644 --- a/MagicQuant/Services/FinalReleaseMetadataService.cs +++ b/MagicQuant/Services/FinalReleaseMetadataService.cs @@ -7,8 +7,8 @@ namespace MagicQuant.Services; public sealed class FinalReleaseMetadataService { - public const string FinalSurvivorsFileName = "magicquant.final-survivors.json"; - public const string ReplacementsFileName = "magicquant.replacements.json"; + public const string FinalSurvivorsFileName = MagicQuantManifestPathService.FinalSurvivorsFileName; + public const string ReplacementsFileName = MagicQuantManifestPathService.ReplacementsFileName; private static readonly JsonSerializerOptions JsonOptions = new() { @@ -25,7 +25,7 @@ public async Task GenerateAsync( BenchmarkSnapshotRecord? pplReference = null, CancellationToken ct = default) { - Directory.CreateDirectory(outputDirectory); + string manifestDirectory = MagicQuantManifestPathService.EnsureManifestDirectory(outputDirectory); double? referencePpl = ResolveReferencePpl(pplReference, pureBaselineSnapshots, exportedArtifacts.Select(x => x.Snapshot).ToList()); var namingContext = _namingService.CreateContext(pureBaselineSnapshots); @@ -35,7 +35,7 @@ public async Task GenerateAsync( var replacementMap = BuildReplacementMap(eliminations); - string finalPath = Path.Combine(outputDirectory, FinalSurvivorsFileName); + string finalPath = Path.Combine(manifestDirectory, FinalSurvivorsFileName); var survivors = exportedArtifacts .OrderBy(x => x.Snapshot.Kld) .ThenBy(x => x.Snapshot.SizeBytes) @@ -43,7 +43,7 @@ public async Task GenerateAsync( .ToList(); await File.WriteAllTextAsync(finalPath, JsonSerializer.Serialize(survivors, JsonOptions), ct); - string replacementsPath = Path.Combine(outputDirectory, ReplacementsFileName); + string replacementsPath = Path.Combine(manifestDirectory, ReplacementsFileName); var replacements = eliminations .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") .OrderBy(x => x.Eliminated.Kld) @@ -312,4 +312,4 @@ private string ToSnapshotShortName( private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; private static double ToGBNumber(long bytes) => bytes / 1000d / 1000d / 1000d; private static double ToGiBNumber(long bytes) => bytes / 1024d / 1024d / 1024d; -} +} \ No newline at end of file diff --git a/MagicQuant/Services/HybridMapGenerationService.cs b/MagicQuant/Services/HybridMapGenerationService.cs index aa21860..3e82870 100644 --- a/MagicQuant/Services/HybridMapGenerationService.cs +++ b/MagicQuant/Services/HybridMapGenerationService.cs @@ -13,8 +13,8 @@ public async Task GenerateAsync( IReadOnlyCollection exportedArtifacts, CancellationToken ct = default) { - Directory.CreateDirectory(outputDirectory); - string path = Path.Combine(outputDirectory, "magicquant.hybrid-map.json"); + string manifestDirectory = MagicQuantManifestPathService.EnsureManifestDirectory(outputDirectory); + string path = Path.Combine(manifestDirectory, MagicQuantManifestPathService.HybridMapFileName); var entries = exportedArtifacts .Where(x => !x.IsExternalReference) @@ -54,4 +54,4 @@ public async Task GenerateAsync( private static double ToGBNumber(ulong bytes) => bytes / 1000d / 1000d / 1000d; private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationDiagnosticsManifestService.cs b/MagicQuant/Services/IsolationDiagnosticsManifestService.cs new file mode 100644 index 0000000..c82871f --- /dev/null +++ b/MagicQuant/Services/IsolationDiagnosticsManifestService.cs @@ -0,0 +1,271 @@ +using System.Text.Json; +using MagicQuant.Helpers; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class IsolationDiagnosticsManifestService +{ + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = true + }; + + public async Task GenerateIsolationSamplesAsync( + string outputDirectory, + RequiredSampleGenerationResult samplePlan, + CancellationToken ct = default) + { + string manifestDirectory = MagicQuantManifestPathService.EnsureManifestDirectory(outputDirectory); + string path = Path.Combine(manifestDirectory, MagicQuantManifestPathService.IsolationSamplesFileName); + + var samples = await BuildIsolationSamplePayloadAsync(samplePlan, ct); + await File.WriteAllTextAsync(path, JsonSerializer.Serialize(samples, JsonOptions), ct); + AnsiConsole.MarkupLine($"[green]Isolation sample JSON generated:[/] {Markup.Escape(path)} samples={samples.Count:N0}"); + return path; + } + + public async Task GenerateBadTradesAsync( + string outputDirectory, + IsolationOptimizationResult isolationResult, + CancellationToken ct = default) + { + string manifestDirectory = MagicQuantManifestPathService.EnsureManifestDirectory(outputDirectory); + string path = Path.Combine(manifestDirectory, MagicQuantManifestPathService.BadTradesFileName); + + var payload = new + { + generatedUtc = DateTime.UtcNow, + modelId = Cache.CurrentModelId, + architectureFamily = Cache.CurrentArchitectureFamilyName, + summary = new + { + badTradeEliminations = isolationResult.BadTradeEliminations, + disabledBaselines = isolationResult.DisabledBaselines, + structuredBadTradeRows = isolationResult.BadTradeDetails.Count + }, + thresholds = new + { + maxSizeDeltaPercent = IsolationPruningConfig.BadTradeMaxSizeDeltaPercent, + kldMultiplier = IsolationPruningConfig.BadTradeKldMultiplier, + pplMultiplier = IsolationPruningConfig.BadTradePplMultiplier, + floatingPointEpsilon = IsolationPruningConfig.FloatingPointEpsilon + }, + badTrades = isolationResult.BadTradeDetails, + notes = isolationResult.Notes + .Where(x => x.Contains("bad trade", StringComparison.OrdinalIgnoreCase) || + x.Contains("carrier anchor", StringComparison.OrdinalIgnoreCase) || + x.Contains("combination baseline", StringComparison.OrdinalIgnoreCase)) + .Distinct(StringComparer.Ordinal) + .ToList() + }; + + await File.WriteAllTextAsync(path, JsonSerializer.Serialize(payload, JsonOptions), ct); + AnsiConsole.MarkupLine($"[green]Bad trade JSON generated:[/] {Markup.Escape(path)} records={isolationResult.BadTradeDetails.Count:N0}"); + return path; + } + + private static async Task> BuildIsolationSamplePayloadAsync( + RequiredSampleGenerationResult samplePlan, + CancellationToken ct) + { + await using var db = new MagicQuantContext(); + + var exactAiModelHashId = await ArchitectureFamilyService.ResolveExactCurrentAiModelHashIdOrNullAsync(db, ct); + if (exactAiModelHashId == null) + throw new InvalidOperationException("Cannot export isolation sample manifest because the current exact AiModelHashId could not be resolved."); + + var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( + db, + exactAiModelHashId.Value, + createIfMissing: false, + ct: ct); + + var nativeSnapshot = await LoadSnapshotAsync(db, exactAiModelHashId.Value, imatrixDefinitionId, (TensorConfig)HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()), ct); + var nativeByCategory = nativeSnapshot?.Categories.ToDictionary(x => x.CategoryId) ?? new Dictionary(); + + var output = new List(); + + foreach (var plan in samplePlan.Plans + .Where(x => x.Kind is RequiredSampleKind.BaseOnlyIsolation or RequiredSampleKind.GroupIsolationProbe or RequiredSampleKind.GroupIsolationContinuation) + .OrderBy(x => x.Kind) + .ThenBy(x => x.TargetGroupId ?? 0) + .ThenBy(x => x.TestedBaselineId ?? 0) + .ThenBy(x => x.TestedCandidateId ?? 0) + .ThenBy(x => x.Key, StringComparer.Ordinal)) + { + ct.ThrowIfCancellationRequested(); + + var config = (TensorConfig)plan.Quant; + var snapshot = await LoadSnapshotAsync(db, exactAiModelHashId.Value, imatrixDefinitionId, config, ct); + var categories = snapshot?.Categories ?? new List(); + + double? kld = categories.Where(x => x.Kld.HasValue).Select(x => x.Kld!.Value).AverageOrNull(); + double? ppl = categories.Select(x => x.Ppl).AverageOrNull(); + double? pplDelta = CalculateAggregatePplDelta(categories, nativeByCategory); + + output.Add(new + { + key = plan.Key, + description = plan.Description, + kind = plan.Kind.ToString(), + tensorConfigKey = MagicQuant.Models.TensorConfigIdentity.ToKey(config), + group = ResolveGroupName(plan.TargetGroupId), + testedBaseline = ResolveBaselineName(plan.TestedBaselineId), + testedCandidate = ResolveBaselineName(plan.TestedCandidateId), + testedBaselineCanonicalKey = plan.TestedBaselineCanonicalKey, + testedCandidateCanonicalKey = plan.TestedCandidateCanonicalKey, + isSmallestProbe = plan.IsSmallestProbe, + sizeBytes = snapshot?.SizeBytes, + sizeGB = snapshot?.SizeBytes is { } bytes ? ToGBNumber(bytes) : (double?)null, + sizeGiB = snapshot?.SizeBytes is { } gibBytes ? ToGiBNumber(gibBytes) : (double?)null, + kld, + ppl, + pplDeltaPercent = pplDelta, + foundBenchmark = snapshot != null, + categories, + config = new + { + config.BaseQuant, + config.Embeddings, + config.LmHead, + config.AttnQ, + config.AttnKV, + config.AttnOutput, + config.FfnUpGate, + config.FfnDown, + config.MoeExperts, + config.MoeRouter + } + }); + } + + return output; + } + + private static async Task LoadSnapshotAsync( + MagicQuantContext db, + uint aiModelHashId, + int? imatrixDefinitionId, + TensorConfig lookup, + CancellationToken ct) + { + var row = await db.AiBenchmarks + .AsNoTracking() + .Include(x => x.CategorBenchmarks) + .Join(db.TensorCombos, + b => b.TensorComboId, + c => c.Id, + (b, c) => new { b, c }) + .FirstOrDefaultAsync(x => + x.b.AiModelHashId == aiModelHashId && + x.b.ImatrixDefinitionId == imatrixDefinitionId && + x.c.BaseQuant == lookup.BaseQuant && + x.c.Embeddings == lookup.Embeddings && + x.c.LmHead == lookup.LmHead && + x.c.AttnQ == lookup.AttnQ && + x.c.AttnKV == lookup.AttnKV && + x.c.AttnOutput == lookup.AttnOutput && + x.c.FfnUpGate == lookup.FfnUpGate && + x.c.FfnDown == lookup.FfnDown && + x.c.MoeExperts == lookup.MoeExperts && + x.c.MoeRouter == lookup.MoeRouter, + ct); + + if (row == null) + return null; + + return new BenchmarkPayload + { + SizeBytes = row.b.SizeBytes, + Categories = row.b.CategorBenchmarks + .OrderBy(x => x.Category) + .Select(x => new CategoryPayload + { + CategoryId = x.Category, + Category = Enum.IsDefined(typeof(BenchmarkCategory), (int)x.Category) + ? ((BenchmarkCategory)x.Category).ToString() + : x.Category.ToString(), + Kld = x.Kld, + Ppl = x.Ppl, + PplError = x.PplError + }) + .ToList() + }; + } + + private static double? CalculateAggregatePplDelta( + IReadOnlyCollection categories, + IReadOnlyDictionary nativeByCategory) + { + var deltas = new List(); + + foreach (var category in categories) + { + if (!nativeByCategory.TryGetValue(category.CategoryId, out var native)) + continue; + + if (native.Ppl <= 0d || category.Ppl <= 0d) + continue; + + deltas.Add(((category.Ppl - native.Ppl) / native.Ppl) * 100d); + } + + return deltas.Count == 0 ? null : deltas.Average(); + } + + private static string? ResolveGroupName(byte? groupId) + { + if (groupId == null) + return null; + + return TReg.All.FirstOrDefault(x => x.UniqueId == groupId.Value)?.Name ?? groupId.Value.ToString(); + } + + private static string? ResolveBaselineName(byte? baselineId) + { + if (baselineId == null) + return null; + + try + { + return BaselineQuants.FromId(baselineId.Value).Names[0]; + } + catch + { + return baselineId.Value.ToString(); + } + } + + private static double ToGBNumber(ulong bytes) => bytes / 1000d / 1000d / 1000d; + private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; + + private sealed class BenchmarkPayload + { + public ulong SizeBytes { get; init; } + public List Categories { get; init; } = new(); + } + + private sealed class CategoryPayload + { + public byte CategoryId { get; init; } + public string Category { get; init; } = string.Empty; + public double? Kld { get; init; } + public double Ppl { get; init; } + public double PplError { get; init; } + } +} + +internal static class MagicQuantEnumerableExtensions +{ + public static double? AverageOrNull(this IEnumerable values) + { + var list = values.Where(x => !double.IsNaN(x) && !double.IsInfinity(x)).ToList(); + return list.Count == 0 ? null : list.Average(); + } +} diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index d73811a..afc910e 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -49,6 +49,29 @@ public sealed class IsolationOptimizationResult public List Notes { get; set; } = new(); public List GroupDetails { get; set; } = new(); + public List BadTradeDetails { get; set; } = new(); +} + +public sealed class IsolationBadTradeRecord +{ + public string Scope { get; set; } = string.Empty; + public string? GroupName { get; set; } + public string RemovedCandidate { get; set; } = string.Empty; + public string AcceptedAnchor { get; set; } = string.Empty; + public string Reason { get; set; } = string.Empty; + public ulong RemovedSizeBytes { get; set; } + public double RemovedSizeGB { get; set; } + public double RemovedSizeGiB { get; set; } + public double RemovedKld { get; set; } + public double RemovedPplDeltaPercent { get; set; } + public ulong AnchorSizeBytes { get; set; } + public double AnchorSizeGB { get; set; } + public double AnchorSizeGiB { get; set; } + public double AnchorKld { get; set; } + public double AnchorPplDeltaPercent { get; set; } + public double SizeDeltaPercent { get; set; } + public double KldRatio { get; set; } + public double PplAbsRatio { get; set; } } public class IsolationOptimizationService @@ -546,6 +569,18 @@ private static void ApplyBadTradeElimination(TensorGroup group, List> BuildSizeBuckets(List 0 && anchorSizeBytes > removedSizeBytes + ? ((double)anchorSizeBytes - removedSizeBytes) / anchorSizeBytes * 100.0 + : 0.0; + + double kldRatio = anchorKld <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : removedKld / anchorKld; + + double anchorPplAbs = Math.Abs(anchorPplDeltaPercent); + double removedPplAbs = Math.Abs(removedPplDeltaPercent); + double pplRatio = anchorPplAbs <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : removedPplAbs / anchorPplAbs; + + return new IsolationBadTradeRecord + { + Scope = scope, + GroupName = groupName, + RemovedCandidate = removedName, + AcceptedAnchor = anchorName, + Reason = reason, + RemovedSizeBytes = removedSizeBytes, + RemovedSizeGB = ToGBNumber(removedSizeBytes), + RemovedSizeGiB = ToGiBNumber(removedSizeBytes), + RemovedKld = removedKld, + RemovedPplDeltaPercent = removedPplDeltaPercent, + AnchorSizeBytes = anchorSizeBytes, + AnchorSizeGB = ToGBNumber(anchorSizeBytes), + AnchorSizeGiB = ToGiBNumber(anchorSizeBytes), + AnchorKld = anchorKld, + AnchorPplDeltaPercent = anchorPplDeltaPercent, + SizeDeltaPercent = sizeDeltaPercent, + KldRatio = kldRatio, + PplAbsRatio = pplRatio + }; + } + private static bool ShouldEliminateAsBadTrade(GroupCandidateEvaluation anchor, GroupCandidateEvaluation candidate, out string reason) { reason = string.Empty; @@ -934,6 +1019,18 @@ private static void ApplyBaseBaselineBadTradeElimination( if (RuntimeSearchSpace.DisableCombinationBaseline(candidate.Baseline)) { result.DisabledBaselines++; + result.BadTradeDetails.Add(CreateBadTradeRecord( + scope: "base-baseline", + groupName: null, + removedName: candidate.Baseline.Names[0], + anchorName: acceptedAnchor.Baseline.Names[0], + reason: reason, + removedSizeBytes: candidate.SizeBytes, + removedKld: candidate.Kld, + removedPplDeltaPercent: candidate.PplDeltaPercent, + anchorSizeBytes: acceptedAnchor.SizeBytes, + anchorKld: acceptedAnchor.Kld, + anchorPplDeltaPercent: acceptedAnchor.PplDeltaPercent)); result.Notes.Add( $"Disabled combination baseline '{candidate.Baseline.Names[0]}' vs accepted carrier anchor '{acceptedAnchor.Baseline.Names[0]}'. {reason}"); } @@ -1032,6 +1129,9 @@ private static bool ShouldEliminateBaseBaselineAsBadTrade( return true; } + private static double ToGBNumber(ulong bytes) => bytes / 1000d / 1000d / 1000d; + private static double ToGiBNumber(ulong bytes) => bytes / 1024d / 1024d / 1024d; + private static double GetAggregateKld(BenchmarkSnapshot snapshot) { return snapshot.Benchmarks @@ -1098,4 +1198,4 @@ private sealed class CategorySnapshot public double Ppl { get; set; } public double PplError { get; set; } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/MagicQuantManifestPathService.cs b/MagicQuant/Services/MagicQuantManifestPathService.cs new file mode 100644 index 0000000..a46540b --- /dev/null +++ b/MagicQuant/Services/MagicQuantManifestPathService.cs @@ -0,0 +1,97 @@ +using Spectre.Console; + +namespace MagicQuant.Services; + +public static class MagicQuantManifestPathService +{ + public const string ManifestDirectoryName = "magicquant-manifest"; + + public const string CloneConfigsFileName = "magicquant.clone-configs.json"; + public const string FinalSurvivorsFileName = "magicquant.final-survivors.json"; + public const string ReplacementsFileName = "magicquant.replacements.json"; + public const string HybridMapFileName = "magicquant.hybrid-map.json"; + public const string CloneBenchmarksFileName = "magicquant.clone-benchmarks.json"; + public const string IsolationSamplesFileName = "magicquant.isolation-samples.json"; + public const string BadTradesFileName = "magicquant.bad-trades.json"; + + public static readonly IReadOnlyList KnownManifestFileNames = + [ + CloneConfigsFileName, + FinalSurvivorsFileName, + ReplacementsFileName, + HybridMapFileName, + CloneBenchmarksFileName, + IsolationSamplesFileName, + BadTradesFileName + ]; + + public static string EnsureManifestDirectory(string outputDirectory) + { + if (string.IsNullOrWhiteSpace(outputDirectory)) + throw new ArgumentException("Output directory is required.", nameof(outputDirectory)); + + string normalizedOutput = NormalizeDirectoryPath(outputDirectory); + + // Idempotency guard: callers sometimes already pass the manifest directory itself. + // Do not create magicquant-manifest/magicquant-manifest. + if (string.Equals(Path.GetFileName(normalizedOutput), ManifestDirectoryName, StringComparison.OrdinalIgnoreCase)) + { + Directory.CreateDirectory(normalizedOutput); + return normalizedOutput; + } + + string path = Path.Combine(normalizedOutput, ManifestDirectoryName); + Directory.CreateDirectory(path); + return path; + } + + public static string GetManifestFilePath(string outputDirectory, string fileName) + => Path.Combine(EnsureManifestDirectory(outputDirectory), NormalizeManifestFileName(fileName)); + + public static string RelativeManifestPath(string fileName) + { + string normalized = NormalizeManifestFileName(fileName).Replace('\\', '/').TrimStart('/'); + + if (normalized.StartsWith(ManifestDirectoryName + "/", StringComparison.OrdinalIgnoreCase)) + return normalized; + + return $"{ManifestDirectoryName}/{normalized}"; + } + + public static string HuggingFaceResolvePath(string fileName, bool download = false) + { + string path = $"./../../resolve/main/{RelativeManifestPath(fileName)}"; + return download ? path + "?download=true" : path; + } + + public static string HuggingFaceGgufResolvePath(string fileName) + => $"./../../resolve/main/{Uri.EscapeDataString(fileName)}?download=true"; + + public static void WriteManifestLog(string message, bool isWarning = false, bool isError = false) + { + string color = isError ? "red" : isWarning ? "yellow" : "grey"; + string line = $"[{DateTime.Now:HH:mm:ss}] manifest: {message}"; + AnsiConsole.MarkupLine($"[{color}]{Markup.Escape(line)}[/]"); + } + + private static string NormalizeDirectoryPath(string path) + { + string full = Path.GetFullPath(path); + string root = Path.GetPathRoot(full) ?? string.Empty; + string trimmed = full.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + return string.IsNullOrWhiteSpace(trimmed) ? root : trimmed; + } + + private static string NormalizeManifestFileName(string fileName) + { + if (string.IsNullOrWhiteSpace(fileName)) + throw new ArgumentException("Manifest file name is required.", nameof(fileName)); + + string normalized = fileName.Trim().Replace('\\', '/').TrimStart('/'); + + if (normalized.StartsWith(ManifestDirectoryName + "/", StringComparison.OrdinalIgnoreCase)) + normalized = normalized[(ManifestDirectoryName.Length + 1)..]; + + return normalized; + } +} diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index b075fd9..0e545cd 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text.Json; using System.Text; using MagicQuant.Models; using MQ.DB; @@ -19,16 +20,127 @@ public async Task GenerateAsync( BenchmarkSnapshotRecord? pplReference = null, CancellationToken ct = default) { - Directory.CreateDirectory(outputDirectory); - string readmePath = Path.Combine(outputDirectory, "README.md"); - var replacementMap = FinalReleaseMetadataService.BuildReplacementMap(eliminatedBaselines ?? Array.Empty()); var namingContext = _namingService.CreateContext(pureBaselineSnapshots); var exportedByKey = exportedArtifacts .GroupBy(x => TensorConfigIdentity.ToKey(x.Snapshot.Config), StringComparer.Ordinal) .ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal); + double? referencePpl = ResolveReferencePpl(pplReference, pureBaselineSnapshots, exportedArtifacts.Select(x => x.Snapshot)); + + var rows = exportedArtifacts + .OrderBy(x => x.Snapshot.Kld) + .ThenBy(x => x.Snapshot.SizeBytes) + .Select(artifact => + { + string key = TensorConfigIdentity.ToKey(artifact.Snapshot.Config); + string shortName = _namingService.ToPublicArtifactShortName( + artifact.DisplayName, + artifact.FileName, + artifact.ProviderName, + artifact.BaselineFamily, + artifact.Snapshot, + namingContext); + + var replacements = FinalReleaseMetadataService.ResolveTransitiveReplacements(key, replacementMap); + string nameCell = BuildNameCell(shortName, replacements, exportedByKey, namingContext); + string download = artifact.IsExternalReference + ? artifact.DownloadTarget + : MagicQuantManifestPathService.HuggingFaceGgufResolvePath(artifact.FileName ?? string.Empty); + + return new ReadmeArtifactRow + { + NameCell = nameCell, + Provider = artifact.ProviderName, + QuantFamily = artifact.BaselineFamily, + Kld = artifact.Snapshot.Kld, + Ppl = artifact.Snapshot.Ppl, + PplDeltaPercent = FinalReleaseMetadataService.CalculatePplDeltaPercent(artifact.Snapshot.Ppl, referencePpl), + SizeBytes = artifact.Snapshot.SizeBytes, + DownloadTarget = download + }; + }) + .ToList(); + + return await GenerateCoreAsync( + outputDirectory, + modelName, + rows, + hasReplacementDetails: (eliminatedBaselines?.Count ?? 0) > 0, + cloneContext: null, + exportedArtifacts: exportedArtifacts, + ct: ct); + } + + public async Task GenerateCloneAsync( + string outputDirectory, + string modelName, + string sourceDescription, + bool sourceWasHuggingFaceRepo, + IReadOnlyCollection records, + IReadOnlyCollection? archivedManifestFileNames = null, + CancellationToken ct = default) + { + var cloneReplacementHints = LoadCloneReplacementHints(outputDirectory); + + var rows = records + .OrderBy(x => x.Kld ?? double.MaxValue) + .ThenBy(x => x.ActualSizeBytes) + .Select(record => + { + var artifact = record.ManifestArtifact; + string rawName = string.IsNullOrWhiteSpace(artifact.ShortName) + ? Path.GetFileNameWithoutExtension(artifact.FileName) + : artifact.ShortName; + string name = BuildCloneNameCell(rawName, artifact.FileName, cloneReplacementHints); + + return new ReadmeArtifactRow + { + NameCell = name, + Provider = string.IsNullOrWhiteSpace(artifact.Provider) ? "Cloned config" : artifact.Provider, + QuantFamily = string.IsNullOrWhiteSpace(artifact.QuantFamily) ? artifact.BaseQuant : artifact.QuantFamily, + Kld = record.Kld, + Ppl = record.Ppl, + PplDeltaPercent = record.PplDeltaPercent, + SizeBytes = record.ActualSizeBytes, + DownloadTarget = MagicQuantManifestPathService.HuggingFaceGgufResolvePath(artifact.FileName) + }; + }) + .ToList(); + + var cloneContext = new ReadmeCloneContext + { + SourceDescription = sourceDescription, + SourceWasHuggingFaceRepo = sourceWasHuggingFaceRepo, + ArchivedManifestFileNames = archivedManifestFileNames?.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.OrdinalIgnoreCase).OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList() + ?? new List() + }; + + return await GenerateCoreAsync( + outputDirectory, + modelName, + rows, + hasReplacementDetails: cloneContext.ArchivedManifestFileNames.Contains(MagicQuantManifestPathService.ReplacementsFileName, StringComparer.OrdinalIgnoreCase), + cloneContext: cloneContext, + exportedArtifacts: Array.Empty(), + ct: ct); + } + + private async Task GenerateCoreAsync( + string outputDirectory, + string modelName, + IReadOnlyCollection rows, + bool hasReplacementDetails, + ReadmeCloneContext? cloneContext, + IReadOnlyCollection exportedArtifacts, + CancellationToken ct) + { + Directory.CreateDirectory(outputDirectory); + MagicQuantManifestPathService.EnsureManifestDirectory(outputDirectory); + + string readmePath = Path.Combine(outputDirectory, "README.md"); var sb = new StringBuilder(); + AppendHuggingFaceFrontmatter(sb); string resolvedModelName = ResolveReadmeTitleModelName(modelName); @@ -42,45 +154,200 @@ public async Task GenerateAsync( sb.AppendLine(); sb.AppendLine("By default, if an external provider like Unsloth is deemed the winner, the repo will generally link directly to the original provider instead of re-hosting the quant. External GGUFs are normally only re-uploaded when a specific winning variant does not already exist (e.g. Heretic models or similar)."); sb.AppendLine(); + + if (cloneContext != null) + AppendCloneNotice(sb, cloneContext); + sb.AppendLine("---"); sb.AppendLine(); sb.AppendLine("## Final survivors"); sb.AppendLine(); - AppendDownloadTable(sb, exportedArtifacts, replacementMap, exportedByKey, namingContext); + AppendDownloadTable(sb, rows); sb.AppendLine(); sb.AppendLine("---"); sb.AppendLine(); - sb.AppendLine("## Release metadata"); - sb.AppendLine(); - sb.AppendLine("- [Final survivor metrics](./../../resolve/main/magicquant.final-survivors.json?download=true) — full file names, KLD, PPL delta %, byte sizes, download targets, and replacement lineage. PPL delta % is measured against the native/reference PPL when available; negative is better and larger positive values are worse."); - sb.AppendLine("- [Hybrid tensor map](./../../resolve/main/magicquant.hybrid-map.json?download=true) — tensor-group assignments and effective-state details for MagicQuant hybrid GGUFs."); - sb.AppendLine("- [Replacement details](./../../resolve/main/magicquant.replacements.json?download=true) — structured details for baselines or anchors removed from the final download table, including reason codes, KLD deltas, PPL delta %, and size deltas."); - sb.AppendLine("- [Clone tensor configs](./../../resolve/main/magicquant.clone-configs.json?download=true) — exact per-GGUF tensor quantization maps for reproducing this final output list in repository clone mode."); + AppendReleaseMetadata(sb, cloneContext); sb.AppendLine(); sb.AppendLine("---"); sb.AppendLine(); - - AppendReasonCodeDetails(sb); - sb.AppendLine(); - AppendProviderCredits(sb, exportedArtifacts); - sb.AppendLine(); + if (hasReplacementDetails) + { + AppendReasonCodeDetails(sb); + sb.AppendLine(); + } - AppendCollapsible(sb, "Warning", "External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing. That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency, not as a universal judgment of the original creator's exact release artifact."); - sb.AppendLine(); + if (cloneContext == null) + { + AppendProviderCredits(sb, exportedArtifacts); + sb.AppendLine(); + + AppendCollapsible(sb, "Warning", "External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing. That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency, not as a universal judgment of the original creator's exact release artifact."); + sb.AppendLine(); + } sb.AppendLine("## Support"); sb.AppendLine("I’m a solo developer working full time for myself to achieve my dream. I build open source code on the side. If you like any of my work, buying me a coffee is always appreciated. Otherwise, I hope you enjoy, maybe give me a star or something. Or just send me good vibes. Either way, thank you!"); sb.AppendLine(); sb.AppendLine("[Click here to see ways to support](https://sayou.biz/support) - BTC, Paypal, GitHub sponsors."); sb.AppendLine(); - + await File.WriteAllTextAsync(readmePath, sb.ToString(), ct); AnsiConsole.MarkupLine($"[green]README generated:[/] {Markup.Escape(readmePath)}"); return readmePath; } + private static void AppendCloneNotice(StringBuilder sb, ReadmeCloneContext clone) + { + sb.AppendLine("## Clone notice"); + sb.AppendLine(); + + string source = clone.SourceWasHuggingFaceRepo + ? BuildHuggingFaceRepoLink(clone.SourceDescription) + : $"`{EscapePipe(clone.SourceDescription)}`"; + + sb.AppendLine($"This repository did not run through the full MagicQuant evolution/search pipeline. It is a clone of the final survivor tensor configurations from {source}, rebuilt and benchmarked locally for this model."); + sb.AppendLine(); + sb.AppendLine("The archived MagicQuant JSON files in `magicquant-manifest/` are copied from the source release for durability. The clone benchmark JSON and the table below are from this clone run, so those metrics reflect the rebuilt outputs in this repository."); + sb.AppendLine(); + } + private static string BuildCloneNameCell(string rawName, string fileName, IReadOnlyDictionary> replacementHints) + { + string safeName = EscapePipe(rawName); + + if (!TryGetReplacementHint(replacementHints, fileName, out var replaced) && + !TryGetReplacementHint(replacementHints, rawName, out replaced)) + { + return safeName; + } + + var shown = replaced.Take(5).Where(x => !string.IsNullOrWhiteSpace(x)).Select(EscapePipe).ToList(); + string tooltip = shown.Count == 0 + ? $"Replaced one or more source artifacts. See {MagicQuantManifestPathService.RelativeManifestPath(MagicQuantManifestPathService.ReplacementsFileName)}." + : $"Replaced: {string.Join(", ", shown)}"; + + if (replaced.Count > shown.Count) + tooltip += $" + {replaced.Count - shown.Count} more"; + + return $"[{safeName}](#winner-notes \"{EscapeTooltip(tooltip)}\")"; + } + + private static bool TryGetReplacementHint(IReadOnlyDictionary> replacementHints, string? key, out IReadOnlyList replaced) + { + replaced = Array.Empty(); + if (string.IsNullOrWhiteSpace(key)) + return false; + + return replacementHints.TryGetValue(key.Trim(), out replaced) && replaced.Count > 0; + } + + private static Dictionary> LoadCloneReplacementHints(string outputDirectory) + { + var output = new Dictionary>(StringComparer.OrdinalIgnoreCase); + string path = MagicQuantManifestPathService.GetManifestFilePath(outputDirectory, MagicQuantManifestPathService.FinalSurvivorsFileName); + if (!File.Exists(path)) + return output; + + try + { + using var doc = JsonDocument.Parse(File.ReadAllText(path)); + if (doc.RootElement.ValueKind != JsonValueKind.Array) + return output; + + foreach (var row in doc.RootElement.EnumerateArray()) + { + var replaced = ReadReplacedShortNames(row); + if (replaced.Count == 0) + continue; + + AddReplacementHint(output, TryGetString(row, "fileName"), replaced); + AddReplacementHint(output, TryGetString(row, "shortName"), replaced); + AddReplacementHint(output, TryGetString(row, "displayName"), replaced); + } + } + catch (Exception ex) + { + AnsiConsole.MarkupLine($"[yellow]Could not read clone replacement hints from archived final-survivors JSON:[/] {Markup.Escape(ex.Message)}"); + } + + return output; + } + + private static List ReadReplacedShortNames(JsonElement survivorRow) + { + if (!survivorRow.TryGetProperty("replacedArtifacts", out var replacedArtifacts) || + replacedArtifacts.ValueKind != JsonValueKind.Array) + { + return new List(); + } + + return replacedArtifacts.EnumerateArray() + .Select(x => TryGetString(x, "shortName") ?? TryGetString(x, "displayName") ?? TryGetString(x, "fileName")) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Select(x => x!) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + + private static void AddReplacementHint(Dictionary> output, string? key, IReadOnlyList replaced) + { + if (string.IsNullOrWhiteSpace(key) || replaced.Count == 0) + return; + + output[key.Trim()] = replaced; + } + + private static string? TryGetString(JsonElement row, string propertyName) + { + return row.TryGetProperty(propertyName, out var value) && value.ValueKind == JsonValueKind.String + ? value.GetString() + : null; + } + + + private static string BuildHuggingFaceRepoLink(string repoId) + { + string clean = (repoId ?? string.Empty).Trim().Trim('/'); + if (string.IsNullOrWhiteSpace(clean)) + return "the source Hugging Face repository"; + + return $"[{EscapePipe(clean)}](https://huggingface.co/{clean})"; + } + + private static void AppendReleaseMetadata(StringBuilder sb, ReadmeCloneContext? cloneContext) + { + sb.AppendLine("## Release metadata"); + sb.AppendLine(); + if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.FinalSurvivorsFileName)) + sb.AppendLine($"- [Final survivor metrics]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.FinalSurvivorsFileName)}) — full file names, KLD, PPL, PPL delta %, byte sizes, download targets, and replacement lineage. PPL delta % is measured against the native/reference PPL when available; negative is better and larger positive values are worse."); + + if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.HybridMapFileName)) + sb.AppendLine($"- [Hybrid tensor map]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.HybridMapFileName)}) — tensor-group assignments and effective-state details for MagicQuant hybrid GGUFs."); + + if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.ReplacementsFileName)) + sb.AppendLine($"- [Replacement details]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.ReplacementsFileName)}) — structured details for baselines or anchors removed from the final download table, including reason codes, KLD deltas, PPL delta %, and size deltas."); + + sb.AppendLine($"- [Clone tensor configs]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.CloneConfigsFileName)}) — exact per-GGUF tensor quantization maps for reproducing this final output list in repository clone mode."); + + if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.IsolationSamplesFileName)) + sb.AppendLine($"- [Isolation samples]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.IsolationSamplesFileName)}) — isolated base/group probe samples with KLD, PPL, PPL delta %, and size truth."); + + if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.BadTradesFileName)) + sb.AppendLine($"- [Bad trade details]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.BadTradesFileName)}) — structured bad-trade pruning decisions from the isolation optimizer."); + + if (cloneContext != null) + sb.AppendLine($"- [Clone benchmark summary]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.CloneBenchmarksFileName)}) — fresh benchmark results from this clone run."); + } + + private static bool ShouldLinkManifestFile(ReadmeCloneContext? cloneContext, string fileName) + { + return cloneContext == null || + cloneContext.ArchivedManifestFileNames.Contains(fileName, StringComparer.OrdinalIgnoreCase) || + string.Equals(fileName, MagicQuantManifestPathService.CloneConfigsFileName, StringComparison.OrdinalIgnoreCase) || + string.Equals(fileName, MagicQuantManifestPathService.CloneBenchmarksFileName, StringComparison.OrdinalIgnoreCase); + } + private static void AppendHuggingFaceFrontmatter(StringBuilder sb) { @@ -227,36 +494,21 @@ private static bool NeedsYamlQuotes(string text) return first is '-' or '?' or ':' or '@' or '!' or '&' or '*' or '[' or ']' or '{' or '}' or '|' or '>' or '%' or '`' or ','; } - private void AppendDownloadTable( - StringBuilder sb, - IReadOnlyCollection artifacts, - IReadOnlyDictionary> replacementMap, - IReadOnlyDictionary exportedByKey, - FinalArtifactNamingContext namingContext) + private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection rows) { - sb.AppendLine("| Name | Provider | Quant Family | KLD | Size (GB) | Download |"); - sb.AppendLine("|---|---|---|---:|---:|---|"); + sb.AppendLine("| Name | Provider | Quant Family | KLD | PPL | PPL Δ % | Size (GB) | Download |"); + sb.AppendLine("|---|---|---|---:|---:|---:|---:|---|"); - foreach (var artifact in artifacts.OrderBy(x => x.Snapshot.Kld).ThenBy(x => x.Snapshot.SizeBytes)) + foreach (var row in rows.OrderBy(x => x.Kld ?? double.MaxValue).ThenBy(x => x.SizeBytes)) { - string key = TensorConfigIdentity.ToKey(artifact.Snapshot.Config); - string shortName = _namingService.ToPublicArtifactShortName( - artifact.DisplayName, - artifact.FileName, - artifact.ProviderName, - artifact.BaselineFamily, - artifact.Snapshot, - namingContext); - var replacements = FinalReleaseMetadataService.ResolveTransitiveReplacements(key, replacementMap); - string nameCell = BuildNameCell(shortName, replacements, exportedByKey, namingContext); - string sizeGb = ToGb(artifact.Snapshot.SizeBytes); - string download = artifact.IsExternalReference - ? $"[Link]({artifact.DownloadTarget})" - : $"[Link](./../../resolve/main/{artifact.FileName}?download=true)"; + string kld = row.Kld.HasValue ? row.Kld.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; + string ppl = row.Ppl.HasValue ? row.Ppl.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; + string pplDelta = row.PplDeltaPercent.HasValue ? row.PplDeltaPercent.Value.ToString("0.000", CultureInfo.InvariantCulture) + "%" : "n/a"; + string sizeGb = ToGB(row.SizeBytes); + string download = string.IsNullOrWhiteSpace(row.DownloadTarget) ? "n/a" : $"[Link]({row.DownloadTarget})"; sb.AppendLine( - $"| {nameCell} | {EscapePipe(artifact.ProviderName)} | {EscapePipe(artifact.BaselineFamily)} | " + - $"{artifact.Snapshot.Kld:0.000000} | {sizeGb} | {download} |"); + $"| {row.NameCell} | {EscapePipe(row.Provider)} | {EscapePipe(row.QuantFamily)} | {kld} | {ppl} | {pplDelta} | {sizeGb} | {download} |"); } } @@ -277,7 +529,7 @@ private string BuildNameCell( .ToList(); string tooltip = replacedNames.Count == 0 - ? "Replaced one or more dominated artifacts. See magicquant.replacements.json." + ? $"Replaced one or more dominated artifacts. See {MagicQuantManifestPathService.RelativeManifestPath(MagicQuantManifestPathService.ReplacementsFileName)}." : $"Replaced: {string.Join(", ", replacedNames)}"; if (replacements.Count > replacedNames.Count) @@ -322,7 +574,7 @@ private static void AppendReasonCodeDetails(StringBuilder sb) sb.AppendLine("- `FINAL_DOMINANCE` — a later validated survivor dominated this artifact in final real benchmark comparison."); sb.AppendLine(); sb.AppendLine(""); - sb.AppendLine("Underlined names in the table replaced or ultimately inherited the replacement of another artifact. Hover the name for the short replacement summary, or inspect `magicquant.replacements.json` for exact KLD/PPL/size deltas."); + sb.AppendLine($"Underlined names in the table replaced or ultimately inherited the replacement of another artifact. Hover the name for the short replacement summary, or inspect `{MagicQuantManifestPathService.RelativeManifestPath(MagicQuantManifestPathService.ReplacementsFileName)}` for exact KLD/PPL/size deltas."); sb.AppendLine(); sb.AppendLine(""); } @@ -362,8 +614,51 @@ private static void AppendCollapsible(StringBuilder sb, string summary, string b sb.AppendLine(""); } - private static string ToGb(ulong bytes) => (bytes / 1000d / 1000d / 1000d).ToString("0.00", CultureInfo.InvariantCulture); + private static double? ResolveReferencePpl( + BenchmarkSnapshotRecord? pplReference, + IReadOnlyCollection pureBaselineSnapshots, + IEnumerable snapshots) + { + if (pplReference is { Ppl: > 0d }) + return pplReference.Ppl; + + var bestPure = pureBaselineSnapshots + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .ThenByDescending(x => x.SizeBytes) + .FirstOrDefault(); + + if (bestPure != null) + return bestPure.Ppl; + + return snapshots + .Where(x => x.Ppl > 0d) + .OrderBy(x => x.Kld) + .FirstOrDefault() + ?.Ppl; + } + + private static string ToGB(ulong bytes) => (bytes / 1000d / 1000d / 1000d).ToString("0.00", CultureInfo.InvariantCulture); private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); private static string EscapeTooltip(string value) => (value ?? string.Empty).Replace("\"", """).Replace("|", " "); private static string EscapeHtml(string value) => (value ?? string.Empty).Replace("&", "&").Replace("<", "<").Replace(">", ">"); -} \ No newline at end of file + + private sealed class ReadmeArtifactRow + { + public string NameCell { get; init; } = string.Empty; + public string Provider { get; init; } = string.Empty; + public string QuantFamily { get; init; } = string.Empty; + public double? Kld { get; init; } + public double? Ppl { get; init; } + public double? PplDeltaPercent { get; init; } + public ulong SizeBytes { get; init; } + public string DownloadTarget { get; init; } = string.Empty; + } + + private sealed class ReadmeCloneContext + { + public string SourceDescription { get; init; } = string.Empty; + public bool SourceWasHuggingFaceRepo { get; init; } + public IReadOnlyList ArchivedManifestFileNames { get; init; } = Array.Empty(); + } +} diff --git a/MagicQuant/Services/RepositoryCloneManifestService.cs b/MagicQuant/Services/RepositoryCloneManifestService.cs index 1549d42..15092ec 100644 --- a/MagicQuant/Services/RepositoryCloneManifestService.cs +++ b/MagicQuant/Services/RepositoryCloneManifestService.cs @@ -37,14 +37,9 @@ public RepositoryCloneManifestService(HuggingFaceBaselineService huggingFace) if (!string.IsNullOrWhiteSpace(sourceRepo)) { - localPath = Path.Combine(cloneDir, CloneConfigManifestGenerationService.FileName); - sourceDescription = sourceRepo.Trim(); - await _huggingFace.DownloadRepositoryFileAsync( - repoId: sourceRepo.Trim(), - fileName: CloneConfigManifestGenerationService.FileName, - destinationPath: localPath, - forceRedownload: true, - ct: ct); + string repo = sourceRepo.Trim(); + sourceDescription = repo; + localPath = await DownloadRequiredCloneManifestFromRepoAsync(repo, cloneDir, ct); } else { @@ -53,7 +48,7 @@ await _huggingFace.DownloadRepositoryFileAsync( if (raw.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || raw.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { - localPath = Path.Combine(cloneDir, CloneConfigManifestGenerationService.FileName); + localPath = Path.Combine(MagicQuantManifestPathService.EnsureManifestDirectory(cloneDir), MagicQuantManifestPathService.CloneConfigsFileName); sourceDescription = raw; using var http = new HttpClient(); @@ -62,14 +57,14 @@ await _huggingFace.DownloadRepositoryFileAsync( } else { - localPath = Path.GetFullPath(raw); - sourceDescription = localPath; + string resolved = Path.GetFullPath(raw); + sourceDescription = resolved; - if (!File.Exists(localPath)) - throw new FileNotFoundException($"Clone JSON file does not exist: {localPath}"); + if (!File.Exists(resolved)) + throw new FileNotFoundException($"Clone JSON file does not exist: {resolved}"); - string copied = Path.Combine(cloneDir, CloneConfigManifestGenerationService.FileName); - File.Copy(localPath, copied, overwrite: true); + string copied = Path.Combine(MagicQuantManifestPathService.EnsureManifestDirectory(cloneDir), MagicQuantManifestPathService.CloneConfigsFileName); + File.Copy(resolved, copied, overwrite: true); localPath = copied; } } @@ -87,6 +82,43 @@ await File.ReadAllTextAsync(localPath, ct), return (manifest, localPath, sourceDescription); } + private async Task DownloadRequiredCloneManifestFromRepoAsync(string repoId, string cloneDir, CancellationToken ct) + { + string manifestDir = MagicQuantManifestPathService.EnsureManifestDirectory(cloneDir); + string localPath = Path.Combine(manifestDir, MagicQuantManifestPathService.CloneConfigsFileName); + + var candidates = new[] + { + MagicQuantManifestPathService.RelativeManifestPath(MagicQuantManifestPathService.CloneConfigsFileName), + MagicQuantManifestPathService.CloneConfigsFileName + }; + + var errors = new List(); + foreach (var candidate in candidates) + { + try + { + await _huggingFace.DownloadRepositoryFileAsync( + repoId: repoId, + fileName: candidate, + destinationPath: localPath, + forceRedownload: true, + ct: ct); + + AnsiConsole.MarkupLine($"[green]Downloaded clone manifest:[/] {Markup.Escape(repoId)}/{Markup.Escape(candidate)}"); + return localPath; + } + catch (Exception ex) + { + errors.Add($"{candidate}: {ex.Message}"); + } + } + + throw new InvalidOperationException( + $"Could not download clone manifest from Hugging Face repo '{repoId}'. Tried new manifest folder path and legacy root path." + + Environment.NewLine + string.Join(Environment.NewLine, errors.Select(x => "- " + x))); + } + private static void ValidateManifest(MagicQuantCloneManifest manifest, string localPath) { if (manifest.SchemaVersion <= 0) From 7410c66751a827e250db03be410a43db52568aa0 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 1 May 2026 15:30:14 -0400 Subject: [PATCH 179/258] new manifest and better debug program startup commands --- MagicQuant/Commands/Evolution.cs | 7 ++++++- MagicQuant/Program.cs | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 2b49c83..c8132ed 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -398,8 +398,13 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( AnsiConsole.MarkupLine("[grey]No archival isolation coverage samples were required.[/]"); } + var finalIsolationManifestPlan = mergedPlan.MergeWith(archivalCoveragePlan); + var survivalPipeline = new CombinationSurvivalPipelineService(quantizationService); - var finalizationResult = await survivalPipeline.RunAsync(ct: default); + var finalizationResult = await survivalPipeline.RunAsync( + isolationSamplePlan: finalIsolationManifestPlan, + isolationOptimizationResult: isolationResult, + ct: default); AnsiConsole.Write(new Rule("[yellow]Export Summary[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"[green]Export directory:[/] [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 53ba34e..2494904 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -11,7 +11,7 @@ if (args.Length == 0) { // Use: "clone" or "evolution" - const string debugMode = "clone"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. + const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) { From 2b3a91d18c70b8802bffd144682a7b38ee56bbee Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 2 May 2026 13:16:04 -0400 Subject: [PATCH 180/258] Significant refactor for how the DB relates learned tensors and how it relates. --- MQ.DB/Cache.cs | 6 +- MQ.DB/Data/MagicQuantContext.cs | 259 +++--- .../20260422202538_InitialCreate.Designer.cs | 753 ------------------ ...8_EnforceImatrixExactOwnership.Designer.cs | 753 ------------------ ...0426194528_EnforceImatrixExactOwnership.cs | 22 - ...60427213435_BenchmarkPerformanceUpgrade.cs | 120 --- ... 20260501195554_InitialCreate.Designer.cs} | 393 ++++++++- ...ate.cs => 20260501195554_InitialCreate.cs} | 410 ++++++++-- .../MagicQuantContextModelSnapshot.cs | 389 ++++++++- MQ.DB/Models/BaselineQuants.cs | 20 +- MQ.DB/Models/DbModels/AiBenchmark.cs | 28 +- .../DbModels/AiBenchmarkLearnedSource.cs | 74 ++ .../DbModels/BaselineQuantDefinition.cs | 82 +- MQ.DB/Models/DbModels/BenchmarkRun.cs | 20 +- .../DbModels/ExecutionPlanProbeCache.cs | 20 + .../DbModels/LearnedBaselineTensorQuant.cs | 90 ++- MQ.DB/Models/DbModels/QuantizationRun.cs | 20 +- MQ.DB/Models/DbModels/TensorGroupProfile.cs | 30 + MQ.DB/Models/TensorWeightScheme.cs | 13 +- MQ.DB/tensor_groups.yaml | 44 +- MagicQuant/Commands/CloneRepositoryQuants.cs | 7 +- MagicQuant/Commands/Evolution.cs | 85 +- .../Configuration/MagicQuantYamlConfig.cs | 14 +- .../Configuration/MagicQuantYamlLoader.cs | 19 +- MagicQuant/Helpers/LlamaBuilder.cs | 366 +++++++-- MagicQuant/Program.cs | 2 +- .../Services/BaselineDefinitionResolver.cs | 111 +++ MagicQuant/Services/BenchmarkService.cs | 93 ++- .../Services/HuggingFaceBaselineService.cs | 388 +++++---- .../Services/HybridBenchmarkRepository.cs | 31 +- MagicQuant/Services/ImatrixService.cs | 27 +- .../IsolationDiagnosticsManifestService.cs | 5 + .../Services/IsolationOptimizationService.cs | 6 +- MagicQuant/Services/QuantDatabaseService.cs | 6 +- MagicQuant/Services/QuantizationService.cs | 309 +++---- .../Services/ReadmeGenerationService.cs | 203 +++-- MagicQuant/Services/TargetedRelearnService.cs | 222 ++++++ .../Services/TensorGroupProfileService.cs | 104 +++ MagicQuant/config.default.yaml | 31 +- MagicQuant/config.dev.yaml | 150 +--- 40 files changed, 3212 insertions(+), 2513 deletions(-) delete mode 100644 MQ.DB/Migrations/20260422202538_InitialCreate.Designer.cs delete mode 100644 MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.Designer.cs delete mode 100644 MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.cs delete mode 100644 MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.cs rename MQ.DB/Migrations/{20260427213435_BenchmarkPerformanceUpgrade.Designer.cs => 20260501195554_InitialCreate.Designer.cs} (64%) rename MQ.DB/Migrations/{20260422202538_InitialCreate.cs => 20260501195554_InitialCreate.cs} (58%) create mode 100644 MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs create mode 100644 MQ.DB/Models/DbModels/TensorGroupProfile.cs create mode 100644 MagicQuant/Services/BaselineDefinitionResolver.cs create mode 100644 MagicQuant/Services/TargetedRelearnService.cs create mode 100644 MagicQuant/Services/TensorGroupProfileService.cs diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index ca61897..60c3a3f 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -89,7 +89,9 @@ public enum MainTorchType public static int? CurrentArchitectureFamilyId { get; set; } - public static bool ForceRelearnBaselineTensorMappings { get; set; } + public static int? CurrentTensorGroupProfileId { get; set; } + + public static string? CurrentTensorGroupProfileFingerprintHash { get; set; } public static bool ForceRefreshHardwareProbe { get; set; } @@ -123,4 +125,4 @@ public enum MainTorchType /// Final export/output directory for selected survivor artifacts. /// public static string? OutputDirectory { get; set; } -} \ No newline at end of file +} diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index 57ddba8..c9fca1a 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -61,128 +61,163 @@ private void InitializeDatabase() EnsureBaselineQuantDefinitions(); } - private void EnsureBaselineQuantDefinitions() - { - var expected = BaselineQuants.All - .Select(x => new BaselineQuantDefinition - { - BaselineQuantId = x.UniqueId, - CanonicalKey = x.CanonicalKey, - BaselineName = x.Names[0], - QuantizeBaseArgumentName = x.QuantizeBaseArgumentName, - DefaultTensorSchemeId = x.DefaultTensorScheme!.UniqueId, - DefaultTensorSchemeName = x.DefaultTensorScheme.Names[0], - SourceKind = x.SourceKind, - SourceOwner = x.SourceOwner, - SourceRepository = x.SourceRepository, - SourceFileName = x.SourceFileName, - ShortSourceName = x.ShortSourceName, - IsCustomBaseline = x.IsCustomBaseline, - IsLearningBaseline = x.IsLearningBaseline, - IsCombinationCarrierCandidate = x.IsCombinationCarrierCandidate, - IsExplicitGroupCombinationCandidate = x.IsExplicitGroupCombinationCandidate, - RequiresImatrix = x.RequiresImatrix, - BitRange = x.BitRange, - ExplicitCandidateSortOrder = x.ExplicitCandidateSortOrder - }) - .OrderBy(x => x.BaselineQuantId) - .ToList(); - var current = BaselineQuantDefinitions - .ToList(); +private void EnsureBaselineQuantDefinitions() +{ + var now = DateTime.UtcNow; + var expected = BaselineQuants.GetBuiltInStandardBaselines() + .Concat(BaselineQuants.GetExactHighPrecisionAliases(allowHighPrecisionHybrids: true)) + .Append(BaselineQuants.GetNativeQuant()) + .Select(x => new BaselineQuantDefinition + { + ArchitectureFamilyId = null, + RuntimeBaselineId = x.UniqueId, + CanonicalKey = x.CanonicalKey, + NormalizedCanonicalKey = NormalizeKey(x.CanonicalKey), + BaselineName = x.Names[0], + DisplayName = x.Names[0], + QuantizeBaseArgumentName = x.QuantizeBaseArgumentName, + DefaultTensorSchemeId = x.DefaultTensorScheme!.UniqueId, + DefaultTensorSchemeName = x.DefaultTensorScheme.Names[0], + SourceKind = x.SourceKind, + SourceOwner = x.SourceOwner, + SourceRepository = x.SourceRepository, + NormalizedSourceRepository = NormalizeNullable(x.SourceRepository), + SourceFileName = x.SourceFileName, + NormalizedSourceFileName = NormalizeFileNullable(x.SourceFileName), + ShortSourceName = x.ShortSourceName, + BaselineFamily = x.Names[0], + IsCustomBaseline = x.IsCustomBaseline, + IsLearningBaseline = x.IsLearningBaseline, + IsCombinationCarrierCandidate = x.IsCombinationCarrierCandidate, + IsExplicitGroupCombinationCandidate = x.IsExplicitGroupCombinationCandidate, + RequiresImatrix = x.RequiresImatrix, + BitRange = x.BitRange, + ExplicitCandidateSortOrder = x.ExplicitCandidateSortOrder, + IsActiveInCurrentConfig = true, + FirstSeenUtc = now, + LastSeenUtc = now, + LastUpdatedUtc = now + }) + .OrderBy(x => x.RuntimeBaselineId) + .ToList(); + + var current = BaselineQuantDefinitions + .Where(x => x.ArchitectureFamilyId == null) + .ToList(); + + var currentByCanonicalKey = current + .Where(x => !string.IsNullOrWhiteSpace(x.NormalizedCanonicalKey)) + .GroupBy(x => x.NormalizedCanonicalKey, StringComparer.Ordinal) + .ToDictionary(g => g.Key, g => g.OrderBy(x => x.RuntimeBaselineId).First(), StringComparer.Ordinal); + + var currentByRuntimeId = current.ToDictionary(x => x.RuntimeBaselineId); + var changed = false; + + foreach (var expectedRow in expected) + { + BaselineQuantDefinition? target = null; - if (current.Count == 0) + if (!string.IsNullOrWhiteSpace(expectedRow.NormalizedCanonicalKey) && + currentByCanonicalKey.TryGetValue(expectedRow.NormalizedCanonicalKey, out var byCanonicalKey)) { - BaselineQuantDefinitions.AddRange(expected); - SaveChanges(); - return; + target = byCanonicalKey; } - - var currentByCanonicalKey = current - .Where(x => !string.IsNullOrWhiteSpace(x.CanonicalKey)) - .GroupBy(x => x.CanonicalKey, StringComparer.Ordinal) - .ToDictionary( - g => g.Key, - g => g.OrderBy(x => x.BaselineQuantId).First(), - StringComparer.Ordinal); - - var currentById = current.ToDictionary(x => x.BaselineQuantId); - var changed = false; - - foreach (var expectedRow in expected) + else if (currentByRuntimeId.TryGetValue(expectedRow.RuntimeBaselineId, out var byId)) { - BaselineQuantDefinition? target = null; - - if (!string.IsNullOrWhiteSpace(expectedRow.CanonicalKey) && - currentByCanonicalKey.TryGetValue(expectedRow.CanonicalKey, out var byCanonicalKey)) - { - target = byCanonicalKey; - } - else if (currentById.TryGetValue(expectedRow.BaselineQuantId, out var byId)) - { - target = byId; - } + target = byId; + } - if (target == null) - { - BaselineQuantDefinitions.Add(expectedRow); - changed = true; - continue; - } + if (target == null) + { + BaselineQuantDefinitions.Add(expectedRow); + changed = true; + continue; + } - if (!BaselineDefinitionEquals(target, expectedRow)) - { - ApplyBaselineDefinitionUpdate(target, expectedRow); - changed = true; - } + if (!BaselineDefinitionEquals(target, expectedRow)) + { + ApplyBaselineDefinitionUpdate(target, expectedRow, preserveFirstSeen: true); + target.LastUpdatedUtc = now; + changed = true; } - if (changed) - SaveChanges(); + target.IsActiveInCurrentConfig = true; + target.LastSeenUtc = now; } - private static bool BaselineDefinitionEquals(BaselineQuantDefinition a, BaselineQuantDefinition b) - { - return a.BaselineQuantId == b.BaselineQuantId && - a.DefaultTensorSchemeId == b.DefaultTensorSchemeId && - a.IsCustomBaseline == b.IsCustomBaseline && - a.IsLearningBaseline == b.IsLearningBaseline && - a.IsCombinationCarrierCandidate == b.IsCombinationCarrierCandidate && - a.IsExplicitGroupCombinationCandidate == b.IsExplicitGroupCombinationCandidate && - a.RequiresImatrix == b.RequiresImatrix && - a.BitRange == b.BitRange && - a.ExplicitCandidateSortOrder == b.ExplicitCandidateSortOrder && - string.Equals(a.CanonicalKey, b.CanonicalKey, StringComparison.Ordinal) && - string.Equals(a.BaselineName, b.BaselineName, StringComparison.Ordinal) && - string.Equals(a.QuantizeBaseArgumentName, b.QuantizeBaseArgumentName, StringComparison.Ordinal) && - string.Equals(a.DefaultTensorSchemeName, b.DefaultTensorSchemeName, StringComparison.Ordinal) && - string.Equals(a.SourceKind, b.SourceKind, StringComparison.Ordinal) && - string.Equals(a.SourceOwner, b.SourceOwner, StringComparison.Ordinal) && - string.Equals(a.SourceRepository, b.SourceRepository, StringComparison.Ordinal) && - string.Equals(a.SourceFileName, b.SourceFileName, StringComparison.Ordinal) && - string.Equals(a.ShortSourceName, b.ShortSourceName, StringComparison.Ordinal); - } + if (changed) + SaveChanges(); +} - private static void ApplyBaselineDefinitionUpdate(BaselineQuantDefinition target, BaselineQuantDefinition source) - { - target.CanonicalKey = source.CanonicalKey; - target.BaselineName = source.BaselineName; - target.QuantizeBaseArgumentName = source.QuantizeBaseArgumentName; - target.DefaultTensorSchemeId = source.DefaultTensorSchemeId; - target.DefaultTensorSchemeName = source.DefaultTensorSchemeName; - target.SourceKind = source.SourceKind; - target.SourceOwner = source.SourceOwner; - target.SourceRepository = source.SourceRepository; - target.SourceFileName = source.SourceFileName; - target.ShortSourceName = source.ShortSourceName; - target.IsCustomBaseline = source.IsCustomBaseline; - target.IsLearningBaseline = source.IsLearningBaseline; - target.IsCombinationCarrierCandidate = source.IsCombinationCarrierCandidate; - target.IsExplicitGroupCombinationCandidate = source.IsExplicitGroupCombinationCandidate; - target.RequiresImatrix = source.RequiresImatrix; - target.BitRange = source.BitRange; - target.ExplicitCandidateSortOrder = source.ExplicitCandidateSortOrder; - } +private static bool BaselineDefinitionEquals(BaselineQuantDefinition a, BaselineQuantDefinition b) +{ + return a.ArchitectureFamilyId == b.ArchitectureFamilyId && + a.RuntimeBaselineId == b.RuntimeBaselineId && + a.DefaultTensorSchemeId == b.DefaultTensorSchemeId && + a.IsCustomBaseline == b.IsCustomBaseline && + a.IsLearningBaseline == b.IsLearningBaseline && + a.IsCombinationCarrierCandidate == b.IsCombinationCarrierCandidate && + a.IsExplicitGroupCombinationCandidate == b.IsExplicitGroupCombinationCandidate && + a.RequiresImatrix == b.RequiresImatrix && + a.BitRange == b.BitRange && + a.ExplicitCandidateSortOrder == b.ExplicitCandidateSortOrder && + a.IsActiveInCurrentConfig == b.IsActiveInCurrentConfig && + string.Equals(a.CanonicalKey, b.CanonicalKey, StringComparison.Ordinal) && + string.Equals(a.NormalizedCanonicalKey, b.NormalizedCanonicalKey, StringComparison.Ordinal) && + string.Equals(a.BaselineName, b.BaselineName, StringComparison.Ordinal) && + string.Equals(a.DisplayName, b.DisplayName, StringComparison.Ordinal) && + string.Equals(a.QuantizeBaseArgumentName, b.QuantizeBaseArgumentName, StringComparison.Ordinal) && + string.Equals(a.DefaultTensorSchemeName, b.DefaultTensorSchemeName, StringComparison.Ordinal) && + string.Equals(a.SourceKind, b.SourceKind, StringComparison.Ordinal) && + string.Equals(a.SourceOwner, b.SourceOwner, StringComparison.Ordinal) && + string.Equals(a.SourceRepository, b.SourceRepository, StringComparison.Ordinal) && + string.Equals(a.NormalizedSourceRepository, b.NormalizedSourceRepository, StringComparison.Ordinal) && + string.Equals(a.SourceFileName, b.SourceFileName, StringComparison.Ordinal) && + string.Equals(a.NormalizedSourceFileName, b.NormalizedSourceFileName, StringComparison.Ordinal) && + string.Equals(a.ShortSourceName, b.ShortSourceName, StringComparison.Ordinal) && + string.Equals(a.BaselineFamily, b.BaselineFamily, StringComparison.Ordinal); +} + +public static void ApplyBaselineDefinitionUpdate(BaselineQuantDefinition target, BaselineQuantDefinition source, bool preserveFirstSeen = true) +{ + var firstSeen = target.FirstSeenUtc; + target.ArchitectureFamilyId = source.ArchitectureFamilyId; + target.RuntimeBaselineId = source.RuntimeBaselineId; + target.CanonicalKey = source.CanonicalKey; + target.NormalizedCanonicalKey = source.NormalizedCanonicalKey; + target.BaselineName = source.BaselineName; + target.DisplayName = source.DisplayName; + target.QuantizeBaseArgumentName = source.QuantizeBaseArgumentName; + target.DefaultTensorSchemeId = source.DefaultTensorSchemeId; + target.DefaultTensorSchemeName = source.DefaultTensorSchemeName; + target.SourceKind = source.SourceKind; + target.SourceOwner = source.SourceOwner; + target.SourceRepository = source.SourceRepository; + target.NormalizedSourceRepository = source.NormalizedSourceRepository; + target.SourceFileName = source.SourceFileName; + target.NormalizedSourceFileName = source.NormalizedSourceFileName; + target.ShortSourceName = source.ShortSourceName; + target.BaselineFamily = source.BaselineFamily; + target.IsCustomBaseline = source.IsCustomBaseline; + target.IsLearningBaseline = source.IsLearningBaseline; + target.IsCombinationCarrierCandidate = source.IsCombinationCarrierCandidate; + target.IsExplicitGroupCombinationCandidate = source.IsExplicitGroupCombinationCandidate; + target.RequiresImatrix = source.RequiresImatrix; + target.BitRange = source.BitRange; + target.ExplicitCandidateSortOrder = source.ExplicitCandidateSortOrder; + target.IsActiveInCurrentConfig = source.IsActiveInCurrentConfig; + target.LastSeenUtc = source.LastSeenUtc; + target.LastUpdatedUtc = source.LastUpdatedUtc; + if (!preserveFirstSeen) + target.FirstSeenUtc = source.FirstSeenUtc; + else if (firstSeen != default) + target.FirstSeenUtc = firstSeen; +} + +private static string NormalizeKey(string value) => (value ?? string.Empty).Trim().ToLowerInvariant(); +private static string? NormalizeNullable(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim().ToLowerInvariant(); +private static string? NormalizeFileNullable(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim().Replace('\\', '/').ToLowerInvariant(); private static bool IsDesignTime() { @@ -202,6 +237,8 @@ private static bool IsDesignTime() public DbSet BenchmarkRuns { get; set; } public DbSet LearnedBaselineTensorQuants { get; set; } public DbSet BaselineQuantDefinitions { get; set; } + public DbSet TensorGroupProfiles { get; set; } + public DbSet AiBenchmarkLearnedSources { get; set; } public DbSet ExecutionPlanProbeCaches { get; set; } public DbSet ImatrixDefinitions { get; set; } public DbSet ArchitectureFamilies { get; set; } @@ -336,4 +373,4 @@ private void ValidateDbSetsImplementInterface() ); } } -} \ No newline at end of file +} diff --git a/MQ.DB/Migrations/20260422202538_InitialCreate.Designer.cs b/MQ.DB/Migrations/20260422202538_InitialCreate.Designer.cs deleted file mode 100644 index 00409c2..0000000 --- a/MQ.DB/Migrations/20260422202538_InitialCreate.Designer.cs +++ /dev/null @@ -1,753 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260422202538_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DisplayName") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("NormalizedName") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("TensorCount") - .HasColumnType("INTEGER"); - - b.Property("TensorSignatureHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("NormalizedName") - .IsUnique(); - - b.HasIndex("TensorSignatureHash", "TensorCount"); - - b.ToTable("ArchitectureFamilies"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ArchitectureFamilyId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IsCanonical") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId") - .IsUnique(); - - b.HasIndex("ArchitectureFamilyId", "AiModelHashId") - .IsUnique(); - - b.ToTable("ArchitectureFamilyModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("BitRange") - .HasColumnType("INTEGER"); - - b.Property("CanonicalKey") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("ExplicitCandidateSortOrder") - .HasColumnType("INTEGER"); - - b.Property("IsCombinationCarrierCandidate") - .HasColumnType("INTEGER"); - - b.Property("IsCustomBaseline") - .HasColumnType("INTEGER"); - - b.Property("IsExplicitGroupCombinationCandidate") - .HasColumnType("INTEGER"); - - b.Property("IsLearningBaseline") - .HasColumnType("INTEGER"); - - b.Property("QuantizeBaseArgumentName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("RequiresImatrix") - .HasColumnType("INTEGER"); - - b.Property("ShortSourceName") - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("SourceFileName") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("SourceOwner") - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("SourceRepository") - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("CanonicalKey") - .IsUnique(); - - b.HasIndex("SourceRepository", "SourceFileName"); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DiscoveryTokenTarget") - .HasColumnType("INTEGER"); - - b.Property("GroupSize") - .HasColumnType("INTEGER"); - - b.Property("HardwareFingerprint") - .IsRequired() - .HasMaxLength(1024) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("QuantizationKey") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("QuantizedModelFingerprint") - .IsRequired() - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("SlotsJson") - .IsRequired() - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("StaticNgl") - .HasColumnType("INTEGER"); - - b.Property("UpdatedUtc") - .HasColumnType("TEXT"); - - b.Property("UsesGpu") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") - .IsUnique(); - - b.ToTable("ExecutionPlanProbeCaches"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BuildFingerprint") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("CanonicalPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IdentityHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("MetadataJson") - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("TokenCount") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId", "IdentityHash") - .IsUnique(); - - b.ToTable("ImatrixDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineCanonicalKey") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineSourceFileName") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("BaselineSourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("BaselineSourceRepository") - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") - .WithMany() - .HasForeignKey("ArchitectureFamilyId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ArchitectureFamily"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.Designer.cs b/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.Designer.cs deleted file mode 100644 index 9345ece..0000000 --- a/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.Designer.cs +++ /dev/null @@ -1,753 +0,0 @@ -// -using System; -using MQ.DB.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace MQ.DB.Migrations -{ - [DbContext(typeof(MagicQuantContext))] - [Migration("20260426194528_EnforceImatrixExactOwnership")] - partial class EnforceImatrixExactOwnership - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("Ngl") - .HasColumnType("INTEGER"); - - b.Property("SizeBytes") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.Property("TokensPerSecond") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") - .IsUnique(); - - b.ToTable("AiBenchmarks"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("UniqueHash") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("UniqueHash"); - - b.ToTable("AiModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DisplayName") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("NormalizedName") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("TensorCount") - .HasColumnType("INTEGER"); - - b.Property("TensorSignatureHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("NormalizedName") - .IsUnique(); - - b.HasIndex("TensorSignatureHash", "TensorCount"); - - b.ToTable("ArchitectureFamilies"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("ArchitectureFamilyId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IsCanonical") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId") - .IsUnique(); - - b.HasIndex("ArchitectureFamilyId", "AiModelHashId") - .IsUnique(); - - b.ToTable("ArchitectureFamilyModelHashes"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => - { - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineName") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("BitRange") - .HasColumnType("INTEGER"); - - b.Property("CanonicalKey") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("DefaultTensorSchemeId") - .HasColumnType("INTEGER"); - - b.Property("DefaultTensorSchemeName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("ExplicitCandidateSortOrder") - .HasColumnType("INTEGER"); - - b.Property("IsCombinationCarrierCandidate") - .HasColumnType("INTEGER"); - - b.Property("IsCustomBaseline") - .HasColumnType("INTEGER"); - - b.Property("IsExplicitGroupCombinationCandidate") - .HasColumnType("INTEGER"); - - b.Property("IsLearningBaseline") - .HasColumnType("INTEGER"); - - b.Property("QuantizeBaseArgumentName") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("RequiresImatrix") - .HasColumnType("INTEGER"); - - b.Property("ShortSourceName") - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("SourceFileName") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("SourceOwner") - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("SourceRepository") - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.HasKey("BaselineQuantId"); - - b.HasIndex("CanonicalKey") - .IsUnique(); - - b.HasIndex("SourceRepository", "SourceFileName"); - - b.ToTable("BaselineQuantDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("CategoryBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("CategoryBenchmarkId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.HasIndex("AiBenchmarkId", "Category"); - - b.ToTable("BenchmarkRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("Category") - .HasColumnType("INTEGER"); - - b.Property("Kld") - .HasColumnType("REAL"); - - b.Property("Ppl") - .HasColumnType("REAL"); - - b.Property("PplError") - .HasColumnType("REAL"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.ToTable("CategoryBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("DiscoveryTokenTarget") - .HasColumnType("INTEGER"); - - b.Property("GroupSize") - .HasColumnType("INTEGER"); - - b.Property("HardwareFingerprint") - .IsRequired() - .HasMaxLength(1024) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("QuantizationKey") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("QuantizedModelFingerprint") - .IsRequired() - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("SlotsJson") - .IsRequired() - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("StaticNgl") - .HasColumnType("INTEGER"); - - b.Property("UpdatedUtc") - .HasColumnType("TEXT"); - - b.Property("UsesGpu") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") - .IsUnique(); - - b.ToTable("ExecutionPlanProbeCaches"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BuildFingerprint") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("CanonicalPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("CreatedUtc") - .HasColumnType("TEXT"); - - b.Property("IdentityHash") - .IsRequired() - .HasMaxLength(128) - .HasColumnType("TEXT"); - - b.Property("MetadataJson") - .HasMaxLength(8000) - .HasColumnType("TEXT"); - - b.Property("SourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("TokenCount") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiModelHashId", "IdentityHash") - .IsUnique(); - - b.ToTable("ImatrixDefinitions"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("BaselineCanonicalKey") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("BaselineQuantId") - .HasColumnType("INTEGER"); - - b.Property("BaselineSourceFileName") - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("BaselineSourceKind") - .IsRequired() - .HasMaxLength(64) - .HasColumnType("TEXT"); - - b.Property("BaselineSourceRepository") - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("FinalQuantType") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("TEXT"); - - b.Property("TensorGroupId") - .HasColumnType("INTEGER"); - - b.Property("TensorName") - .IsRequired() - .HasMaxLength(512) - .HasColumnType("TEXT"); - - b.Property("TensorWeightSchemeId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") - .IsUnique(); - - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); - - b.ToTable("LearnedBaselineTensorQuants"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AiBenchmarkId") - .HasColumnType("TEXT"); - - b.Property("AiModelHashId") - .HasColumnType("INTEGER"); - - b.Property("CompletedUtc") - .HasColumnType("TEXT"); - - b.Property("DurationMs") - .HasColumnType("INTEGER"); - - b.Property("Error") - .HasMaxLength(4000) - .HasColumnType("TEXT"); - - b.Property("ImatrixDefinitionId") - .HasColumnType("INTEGER"); - - b.Property("OutputModelPath") - .HasMaxLength(2048) - .HasColumnType("TEXT"); - - b.Property("StartedUtc") - .HasColumnType("TEXT"); - - b.Property("Succeeded") - .HasColumnType("INTEGER"); - - b.Property("TensorComboId") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.HasIndex("AiBenchmarkId"); - - b.HasIndex("AiModelHashId"); - - b.HasIndex("ImatrixDefinitionId"); - - b.HasIndex("StartedUtc"); - - b.HasIndex("TensorComboId"); - - b.ToTable("QuantizationRuns"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => - { - b.Property("Id") - .HasColumnType("TEXT"); - - b.Property("AttnKV") - .HasColumnType("INTEGER"); - - b.Property("AttnOutput") - .HasColumnType("INTEGER"); - - b.Property("AttnQ") - .HasColumnType("INTEGER"); - - b.Property("BaseQuant") - .HasColumnType("INTEGER"); - - b.Property("Embeddings") - .HasColumnType("INTEGER"); - - b.Property("FfnDown") - .HasColumnType("INTEGER"); - - b.Property("FfnUpGate") - .HasColumnType("INTEGER"); - - b.Property("LmHead") - .HasColumnType("INTEGER"); - - b.Property("MoeExperts") - .HasColumnType("INTEGER"); - - b.Property("MoeRouter") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") - .IsUnique(); - - b.ToTable("TensorCombos"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") - .WithMany() - .HasForeignKey("ArchitectureFamilyId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - - b.Navigation("ArchitectureFamily"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") - .WithMany() - .HasForeignKey("CategoryBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("CategoryBenchmark"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany("CategorBenchmarks") - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => - { - b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") - .WithMany() - .HasForeignKey("AiBenchmarkId") - .OnDelete(DeleteBehavior.SetNull); - - b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") - .WithMany() - .HasForeignKey("AiModelHashId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") - .WithMany() - .HasForeignKey("ImatrixDefinitionId") - .OnDelete(DeleteBehavior.Restrict); - - b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") - .WithMany() - .HasForeignKey("TensorComboId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("AiBenchmark"); - - b.Navigation("AiModelHash"); - - b.Navigation("ImatrixDefinition"); - - b.Navigation("TensorCombo"); - }); - - modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => - { - b.Navigation("CategorBenchmarks"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.cs b/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.cs deleted file mode 100644 index 9d5420d..0000000 --- a/MQ.DB/Migrations/20260426194528_EnforceImatrixExactOwnership.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class EnforceImatrixExactOwnership : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - - } - } -} diff --git a/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.cs b/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.cs deleted file mode 100644 index 2136de2..0000000 --- a/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.cs +++ /dev/null @@ -1,120 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MQ.DB.Migrations -{ - /// - public partial class BenchmarkPerformanceUpgrade : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AddColumn( - name: "GpuMemoryLimitsJson", - table: "ExecutionPlanProbeCaches", - type: "TEXT", - maxLength: 4000, - nullable: false, - defaultValue: ""); - - migrationBuilder.AddColumn( - name: "MaxCandidateNgl", - table: "ExecutionPlanProbeCaches", - type: "INTEGER", - nullable: false, - defaultValue: 0); - - migrationBuilder.AddColumn( - name: "NativeModelSizeBytes", - table: "ExecutionPlanProbeCaches", - type: "INTEGER", - nullable: false, - defaultValue: 0ul); - - migrationBuilder.AddColumn( - name: "NativeQuantizationKey", - table: "ExecutionPlanProbeCaches", - type: "TEXT", - maxLength: 128, - nullable: false, - defaultValue: ""); - - migrationBuilder.AddColumn( - name: "NativeStableNgl", - table: "ExecutionPlanProbeCaches", - type: "INTEGER", - nullable: false, - defaultValue: 0); - - migrationBuilder.AddColumn( - name: "ProbeSchemaVersion", - table: "ExecutionPlanProbeCaches", - type: "INTEGER", - nullable: false, - defaultValue: 0); - - migrationBuilder.AddColumn( - name: "Q8ModelSizeBytes", - table: "ExecutionPlanProbeCaches", - type: "INTEGER", - nullable: false, - defaultValue: 0ul); - - migrationBuilder.AddColumn( - name: "Q8StableNgl", - table: "ExecutionPlanProbeCaches", - type: "INTEGER", - nullable: false, - defaultValue: 0); - - migrationBuilder.AddColumn( - name: "TensorSplitJson", - table: "ExecutionPlanProbeCaches", - type: "TEXT", - maxLength: 4000, - nullable: false, - defaultValue: ""); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropColumn( - name: "GpuMemoryLimitsJson", - table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn( - name: "MaxCandidateNgl", - table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn( - name: "NativeModelSizeBytes", - table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn( - name: "NativeQuantizationKey", - table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn( - name: "NativeStableNgl", - table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn( - name: "ProbeSchemaVersion", - table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn( - name: "Q8ModelSizeBytes", - table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn( - name: "Q8StableNgl", - table: "ExecutionPlanProbeCaches"); - - migrationBuilder.DropColumn( - name: "TensorSplitJson", - table: "ExecutionPlanProbeCaches"); - } - } -} diff --git a/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.Designer.cs b/MQ.DB/Migrations/20260501195554_InitialCreate.Designer.cs similarity index 64% rename from MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.Designer.cs rename to MQ.DB/Migrations/20260501195554_InitialCreate.Designer.cs index 8ad3ef7..4e8e213 100644 --- a/MQ.DB/Migrations/20260427213435_BenchmarkPerformanceUpgrade.Designer.cs +++ b/MQ.DB/Migrations/20260501195554_InitialCreate.Designer.cs @@ -11,8 +11,8 @@ namespace MQ.DB.Migrations { [DbContext(typeof(MagicQuantContext))] - [Migration("20260427213435_BenchmarkPerformanceUpgrade")] - partial class BenchmarkPerformanceUpgrade + [Migration("20260501195554_InitialCreate")] + partial class InitialCreate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -28,6 +28,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("ImatrixDefinitionId") .HasColumnType("INTEGER"); @@ -40,21 +43,82 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("TensorComboId") .HasColumnType("TEXT"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.Property("TokensPerSecond") .HasColumnType("REAL"); b.HasKey("Id"); + b.HasIndex("AiModelHashId"); + b.HasIndex("ImatrixDefinitionId"); b.HasIndex("TensorComboId"); - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "TensorComboId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "TensorComboId") .IsUnique(); b.ToTable("AiBenchmarks"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmarkLearnedSource", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("SourceLearningBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaselineQuantDefinitionId"); + + b.HasIndex("SourceLearningBenchmarkId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("AiBenchmarkId", "TensorGroupId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId"); + + b.ToTable("AiBenchmarkLearnedSources"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => { b.Property("Id") @@ -140,9 +204,17 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => { - b.Property("BaselineQuantId") + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") .HasColumnType("INTEGER"); + b.Property("BaselineFamily") + .HasMaxLength(128) + .HasColumnType("TEXT"); + b.Property("BaselineName") .IsRequired() .HasMaxLength(128) @@ -153,7 +225,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("CanonicalKey") .IsRequired() - .HasMaxLength(256) + .HasMaxLength(512) .HasColumnType("TEXT"); b.Property("DefaultTensorSchemeId") @@ -164,9 +236,20 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasMaxLength(64) .HasColumnType("TEXT"); + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + b.Property("ExplicitCandidateSortOrder") .HasColumnType("INTEGER"); + b.Property("FirstSeenUtc") + .HasColumnType("TEXT"); + + b.Property("IsActiveInCurrentConfig") + .HasColumnType("INTEGER"); + b.Property("IsCombinationCarrierCandidate") .HasColumnType("INTEGER"); @@ -179,6 +262,25 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("IsLearningBaseline") .HasColumnType("INTEGER"); + b.Property("LastSeenUtc") + .HasColumnType("TEXT"); + + b.Property("LastUpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("NormalizedCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("NormalizedSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("NormalizedSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + b.Property("QuantizeBaseArgumentName") .IsRequired() .HasMaxLength(64) @@ -187,6 +289,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("RequiresImatrix") .HasColumnType("INTEGER"); + b.Property("RuntimeBaselineId") + .HasColumnType("INTEGER"); + b.Property("ShortSourceName") .HasMaxLength(64) .HasColumnType("TEXT"); @@ -208,12 +313,20 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasMaxLength(256) .HasColumnType("TEXT"); - b.HasKey("BaselineQuantId"); + b.HasKey("Id"); + + b.HasIndex("IsActiveInCurrentConfig"); + + b.HasIndex("ArchitectureFamilyId", "NormalizedCanonicalKey") + .IsUnique(); - b.HasIndex("CanonicalKey") + b.HasIndex("ArchitectureFamilyId", "RuntimeBaselineId") .IsUnique(); - b.HasIndex("SourceRepository", "SourceFileName"); + b.HasIndex("RuntimeBaselineId", "ArchitectureFamilyId"); + + b.HasIndex("ArchitectureFamilyId", "NormalizedSourceRepository", "NormalizedSourceFileName") + .IsUnique(); b.ToTable("BaselineQuantDefinitions"); }); @@ -229,6 +342,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("Category") .HasColumnType("INTEGER"); @@ -257,12 +373,17 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("TensorComboId") .HasColumnType("TEXT"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.HasKey("Id"); b.HasIndex("AiBenchmarkId"); b.HasIndex("AiModelHashId"); + b.HasIndex("ArchitectureFamilyId"); + b.HasIndex("CategoryBenchmarkId"); b.HasIndex("ImatrixDefinitionId"); @@ -271,6 +392,8 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("TensorComboId"); + b.HasIndex("TensorGroupProfileId"); + b.HasIndex("AiBenchmarkId", "Category"); b.ToTable("BenchmarkRuns"); @@ -311,6 +434,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("CreatedUtc") .HasColumnType("TEXT"); @@ -374,6 +500,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("StaticNgl") .HasColumnType("INTEGER"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.Property("TensorSplitJson") .IsRequired() .HasMaxLength(4000) @@ -389,9 +518,13 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); + b.HasIndex("ArchitectureFamilyId"); + b.HasIndex("ImatrixDefinitionId"); - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") .IsUnique(); b.ToTable("ExecutionPlanProbeCaches"); @@ -445,7 +578,6 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => { b.Property("Id") - .ValueGeneratedOnAdd() .HasColumnType("TEXT"); b.Property("AiBenchmarkId") @@ -454,11 +586,17 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("BaselineCanonicalKey") .IsRequired() - .HasMaxLength(256) + .HasMaxLength(512) .HasColumnType("TEXT"); + b.Property("BaselineQuantDefinitionId") + .HasColumnType("INTEGER"); + b.Property("BaselineQuantId") .HasColumnType("INTEGER"); @@ -480,9 +618,15 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasMaxLength(32) .HasColumnType("TEXT"); + b.Property("TensorComboId") + .HasColumnType("TEXT"); + b.Property("TensorGroupId") .HasColumnType("INTEGER"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.Property("TensorName") .IsRequired() .HasMaxLength(512) @@ -495,10 +639,18 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasIndex("AiBenchmarkId"); - b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") - .IsUnique(); + b.HasIndex("AiModelHashId"); - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + b.HasIndex("BaselineQuantDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorGroupId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); b.ToTable("LearnedBaselineTensorQuants"); }); @@ -514,6 +666,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("CompletedUtc") .HasColumnType("TEXT"); @@ -540,18 +695,25 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("TensorComboId") .HasColumnType("TEXT"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.HasKey("Id"); b.HasIndex("AiBenchmarkId"); b.HasIndex("AiModelHashId"); + b.HasIndex("ArchitectureFamilyId"); + b.HasIndex("ImatrixDefinitionId"); b.HasIndex("StartedUtc"); b.HasIndex("TensorComboId"); + b.HasIndex("TensorGroupProfileId"); + b.ToTable("QuantizationRuns"); }); @@ -598,6 +760,40 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.ToTable("TensorCombos"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorGroupProfile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("FingerprintHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("IsActive") + .HasColumnType("INTEGER"); + + b.Property("SnapshotJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ArchitectureFamilyId", "FingerprintHash") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "IsActive"); + + b.ToTable("TensorGroupProfiles"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => { b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") @@ -606,6 +802,12 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") .WithMany() .HasForeignKey("ImatrixDefinitionId") @@ -617,11 +819,71 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Restrict) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiModelHash"); + b.Navigation("ArchitectureFamily"); + b.Navigation("ImatrixDefinition"); b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmarkLearnedSource", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("LearnedSources") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.BaselineQuantDefinition", "BaselineQuantDefinition") + .WithMany() + .HasForeignKey("BaselineQuantDefinitionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "SourceLearningBenchmark") + .WithMany() + .HasForeignKey("SourceLearningBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("BaselineQuantDefinition"); + + b.Navigation("SourceLearningBenchmark"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => @@ -643,6 +905,16 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Navigation("ArchitectureFamily"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade); + + b.Navigation("ArchitectureFamily"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => { b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") @@ -657,6 +929,12 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") .WithMany() .HasForeignKey("CategoryBenchmarkId") @@ -673,15 +951,25 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Restrict) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiBenchmark"); b.Navigation("AiModelHash"); + b.Navigation("ArchitectureFamily"); + b.Navigation("CategoryBenchmark"); b.Navigation("ImatrixDefinition"); b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => @@ -703,14 +991,30 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") .WithMany() .HasForeignKey("ImatrixDefinitionId") .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiModelHash"); + b.Navigation("ArchitectureFamily"); + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => @@ -738,9 +1042,41 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.BaselineQuantDefinition", "BaselineQuantDefinition") + .WithMany() + .HasForeignKey("BaselineQuantDefinitionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiBenchmark"); b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("BaselineQuantDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => @@ -756,6 +1092,12 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") .WithMany() .HasForeignKey("ImatrixDefinitionId") @@ -767,18 +1109,41 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Restrict) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiBenchmark"); b.Navigation("AiModelHash"); + b.Navigation("ArchitectureFamily"); + b.Navigation("ImatrixDefinition"); b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorGroupProfile", b => + { + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ArchitectureFamily"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => { b.Navigation("CategorBenchmarks"); + + b.Navigation("LearnedSources"); }); #pragma warning restore 612, 618 } diff --git a/MQ.DB/Migrations/20260422202538_InitialCreate.cs b/MQ.DB/Migrations/20260501195554_InitialCreate.cs similarity index 58% rename from MQ.DB/Migrations/20260422202538_InitialCreate.cs rename to MQ.DB/Migrations/20260501195554_InitialCreate.cs index 3dde0e5..d03c1b5 100644 --- a/MQ.DB/Migrations/20260422202538_InitialCreate.cs +++ b/MQ.DB/Migrations/20260501195554_InitialCreate.cs @@ -41,34 +41,6 @@ protected override void Up(MigrationBuilder migrationBuilder) table.PrimaryKey("PK_ArchitectureFamilies", x => x.Id); }); - migrationBuilder.CreateTable( - name: "BaselineQuantDefinitions", - columns: table => new - { - BaselineQuantId = table.Column(type: "INTEGER", nullable: false), - CanonicalKey = table.Column(type: "TEXT", maxLength: 256, nullable: false), - BaselineName = table.Column(type: "TEXT", maxLength: 128, nullable: false), - QuantizeBaseArgumentName = table.Column(type: "TEXT", maxLength: 64, nullable: false), - DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), - DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false), - SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), - SourceOwner = table.Column(type: "TEXT", maxLength: 128, nullable: true), - SourceRepository = table.Column(type: "TEXT", maxLength: 256, nullable: true), - SourceFileName = table.Column(type: "TEXT", maxLength: 512, nullable: true), - ShortSourceName = table.Column(type: "TEXT", maxLength: 64, nullable: true), - IsCustomBaseline = table.Column(type: "INTEGER", nullable: false), - IsLearningBaseline = table.Column(type: "INTEGER", nullable: false), - IsCombinationCarrierCandidate = table.Column(type: "INTEGER", nullable: false), - IsExplicitGroupCombinationCandidate = table.Column(type: "INTEGER", nullable: false), - RequiresImatrix = table.Column(type: "INTEGER", nullable: false), - BitRange = table.Column(type: "INTEGER", nullable: false), - ExplicitCandidateSortOrder = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.BaselineQuantId); - }); - migrationBuilder.CreateTable( name: "TensorCombos", columns: table => new @@ -144,6 +116,75 @@ protected override void Up(MigrationBuilder migrationBuilder) onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "BaselineQuantDefinitions", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: true), + RuntimeBaselineId = table.Column(type: "INTEGER", nullable: false), + CanonicalKey = table.Column(type: "TEXT", maxLength: 512, nullable: false), + NormalizedCanonicalKey = table.Column(type: "TEXT", maxLength: 512, nullable: false), + BaselineName = table.Column(type: "TEXT", maxLength: 128, nullable: false), + DisplayName = table.Column(type: "TEXT", maxLength: 128, nullable: false), + QuantizeBaseArgumentName = table.Column(type: "TEXT", maxLength: 64, nullable: false), + DefaultTensorSchemeId = table.Column(type: "INTEGER", nullable: false), + DefaultTensorSchemeName = table.Column(type: "TEXT", maxLength: 64, nullable: false), + SourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), + SourceOwner = table.Column(type: "TEXT", maxLength: 128, nullable: true), + SourceRepository = table.Column(type: "TEXT", maxLength: 256, nullable: true), + NormalizedSourceRepository = table.Column(type: "TEXT", maxLength: 256, nullable: true), + SourceFileName = table.Column(type: "TEXT", maxLength: 512, nullable: true), + NormalizedSourceFileName = table.Column(type: "TEXT", maxLength: 512, nullable: true), + ShortSourceName = table.Column(type: "TEXT", maxLength: 64, nullable: true), + BaselineFamily = table.Column(type: "TEXT", maxLength: 128, nullable: true), + IsCustomBaseline = table.Column(type: "INTEGER", nullable: false), + IsLearningBaseline = table.Column(type: "INTEGER", nullable: false), + IsCombinationCarrierCandidate = table.Column(type: "INTEGER", nullable: false), + IsExplicitGroupCombinationCandidate = table.Column(type: "INTEGER", nullable: false), + RequiresImatrix = table.Column(type: "INTEGER", nullable: false), + BitRange = table.Column(type: "INTEGER", nullable: false), + ExplicitCandidateSortOrder = table.Column(type: "INTEGER", nullable: false), + IsActiveInCurrentConfig = table.Column(type: "INTEGER", nullable: false), + FirstSeenUtc = table.Column(type: "TEXT", nullable: false), + LastSeenUtc = table.Column(type: "TEXT", nullable: false), + LastUpdatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_BaselineQuantDefinitions", x => x.Id); + table.ForeignKey( + name: "FK_BaselineQuantDefinitions_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "TensorGroupProfiles", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + FingerprintHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), + SnapshotJson = table.Column(type: "TEXT", nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + IsActive = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TensorGroupProfiles", x => x.Id); + table.ForeignKey( + name: "FK_TensorGroupProfiles_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "AiBenchmarks", columns: table => new @@ -152,6 +193,8 @@ protected override void Up(MigrationBuilder migrationBuilder) Ngl = table.Column(type: "INTEGER", nullable: false), SizeBytes = table.Column(type: "INTEGER", nullable: false), TokensPerSecond = table.Column(type: "REAL", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), TensorComboId = table.Column(type: "TEXT", nullable: false), AiModelHashId = table.Column(type: "INTEGER", nullable: false), ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true) @@ -165,6 +208,12 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "AiModelHashes", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AiBenchmarks_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_AiBenchmarks_ImatrixDefinitions_ImatrixDefinitionId", column: x => x.ImatrixDefinitionId, @@ -177,6 +226,12 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "TensorCombos", principalColumn: "Id", onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AiBenchmarks_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( @@ -184,6 +239,8 @@ protected override void Up(MigrationBuilder migrationBuilder) columns: table => new { Id = table.Column(type: "TEXT", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), AiModelHashId = table.Column(type: "INTEGER", nullable: false), ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), HardwareFingerprint = table.Column(type: "TEXT", maxLength: 1024, nullable: false), @@ -194,6 +251,15 @@ protected override void Up(MigrationBuilder migrationBuilder) UsesGpu = table.Column(type: "INTEGER", nullable: false), GroupSize = table.Column(type: "INTEGER", nullable: false), SlotsJson = table.Column(type: "TEXT", maxLength: 8000, nullable: false), + ProbeSchemaVersion = table.Column(type: "INTEGER", nullable: false), + Q8ModelSizeBytes = table.Column(type: "INTEGER", nullable: false), + Q8StableNgl = table.Column(type: "INTEGER", nullable: false), + NativeModelSizeBytes = table.Column(type: "INTEGER", nullable: false), + NativeStableNgl = table.Column(type: "INTEGER", nullable: false), + NativeQuantizationKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), + MaxCandidateNgl = table.Column(type: "INTEGER", nullable: false), + GpuMemoryLimitsJson = table.Column(type: "TEXT", maxLength: 4000, nullable: false), + TensorSplitJson = table.Column(type: "TEXT", maxLength: 4000, nullable: false), CreatedUtc = table.Column(type: "TEXT", nullable: false), UpdatedUtc = table.Column(type: "TEXT", nullable: false) }, @@ -206,12 +272,80 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "AiModelHashes", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_ExecutionPlanProbeCaches_ImatrixDefinitions_ImatrixDefinitionId", column: x => x.ImatrixDefinitionId, principalTable: "ImatrixDefinitions", principalColumn: "Id", onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_ExecutionPlanProbeCaches_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "AiBenchmarkLearnedSources", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + AiBenchmarkId = table.Column(type: "TEXT", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), + TensorComboId = table.Column(type: "TEXT", nullable: false), + TensorGroupId = table.Column(type: "INTEGER", nullable: false), + BaselineQuantDefinitionId = table.Column(type: "INTEGER", nullable: false), + SourceLearningBenchmarkId = table.Column(type: "TEXT", nullable: true), + BaselineCanonicalKey = table.Column(type: "TEXT", maxLength: 512, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AiBenchmarkLearnedSources", x => x.Id); + table.ForeignKey( + name: "FK_AiBenchmarkLearnedSources_AiBenchmarks_AiBenchmarkId", + column: x => x.AiBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AiBenchmarkLearnedSources_AiBenchmarks_SourceLearningBenchmarkId", + column: x => x.SourceLearningBenchmarkId, + principalTable: "AiBenchmarks", + principalColumn: "Id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_AiBenchmarkLearnedSources_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AiBenchmarkLearnedSources_BaselineQuantDefinitions_BaselineQuantDefinitionId", + column: x => x.BaselineQuantDefinitionId, + principalTable: "BaselineQuantDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AiBenchmarkLearnedSources_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AiBenchmarkLearnedSources_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( @@ -241,10 +375,14 @@ protected override void Up(MigrationBuilder migrationBuilder) columns: table => new { Id = table.Column(type: "TEXT", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), + BaselineQuantDefinitionId = table.Column(type: "INTEGER", nullable: false), + TensorComboId = table.Column(type: "TEXT", nullable: false), AiBenchmarkId = table.Column(type: "TEXT", nullable: false), AiModelHashId = table.Column(type: "INTEGER", nullable: false), BaselineQuantId = table.Column(type: "INTEGER", nullable: false), - BaselineCanonicalKey = table.Column(type: "TEXT", maxLength: 256, nullable: false), + BaselineCanonicalKey = table.Column(type: "TEXT", maxLength: 512, nullable: false), BaselineSourceKind = table.Column(type: "TEXT", maxLength: 64, nullable: false), BaselineSourceRepository = table.Column(type: "TEXT", maxLength: 256, nullable: true), BaselineSourceFileName = table.Column(type: "TEXT", maxLength: 512, nullable: true), @@ -268,6 +406,30 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "AiModelHashes", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_BaselineQuantDefinitions_BaselineQuantDefinitionId", + column: x => x.BaselineQuantDefinitionId, + principalTable: "BaselineQuantDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_TensorCombos_TensorComboId", + column: x => x.TensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_LearnedBaselineTensorQuants_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( @@ -275,6 +437,8 @@ protected override void Up(MigrationBuilder migrationBuilder) columns: table => new { Id = table.Column(type: "TEXT", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), AiModelHashId = table.Column(type: "INTEGER", nullable: false), ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), TensorComboId = table.Column(type: "TEXT", nullable: false), @@ -301,6 +465,12 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "AiModelHashes", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_QuantizationRuns_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_QuantizationRuns_ImatrixDefinitions_ImatrixDefinitionId", column: x => x.ImatrixDefinitionId, @@ -313,6 +483,12 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "TensorCombos", principalColumn: "Id", onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_QuantizationRuns_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( @@ -320,6 +496,8 @@ protected override void Up(MigrationBuilder migrationBuilder) columns: table => new { Id = table.Column(type: "TEXT", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), AiModelHashId = table.Column(type: "INTEGER", nullable: false), ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), TensorComboId = table.Column(type: "TEXT", nullable: false), @@ -347,6 +525,12 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "AiModelHashes", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BenchmarkRuns_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_BenchmarkRuns_CategoryBenchmark_CategoryBenchmarkId", column: x => x.CategoryBenchmarkId, @@ -365,14 +549,61 @@ protected override void Up(MigrationBuilder migrationBuilder) principalTable: "TensorCombos", principalColumn: "Id", onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_BenchmarkRuns_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateIndex( - name: "IX_AiBenchmarks_AiModelHashId_ImatrixDefinitionId_TensorComboId", + name: "IX_AiBenchmarkLearnedSources_AiBenchmarkId_TensorGroupId", + table: "AiBenchmarkLearnedSources", + columns: new[] { "AiBenchmarkId", "TensorGroupId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarkLearnedSources_ArchitectureFamilyId_TensorGroupProfileId_BaselineQuantDefinitionId", + table: "AiBenchmarkLearnedSources", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId" }); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarkLearnedSources_BaselineQuantDefinitionId", + table: "AiBenchmarkLearnedSources", + column: "BaselineQuantDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarkLearnedSources_SourceLearningBenchmarkId", + table: "AiBenchmarkLearnedSources", + column: "SourceLearningBenchmarkId"); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarkLearnedSources_TensorComboId", + table: "AiBenchmarkLearnedSources", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarkLearnedSources_TensorGroupProfileId", + table: "AiBenchmarkLearnedSources", + column: "TensorGroupProfileId"); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_AiModelHashId", table: "AiBenchmarks", - columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_TensorComboId", + table: "AiBenchmarks", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "TensorComboId" }, unique: true); + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_ArchitectureFamilyId_TensorGroupProfileId_TensorComboId", + table: "AiBenchmarks", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "TensorComboId" }); + migrationBuilder.CreateIndex( name: "IX_AiBenchmarks_ImatrixDefinitionId", table: "AiBenchmarks", @@ -383,6 +614,11 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "AiBenchmarks", column: "TensorComboId"); + migrationBuilder.CreateIndex( + name: "IX_AiBenchmarks_TensorGroupProfileId", + table: "AiBenchmarks", + column: "TensorGroupProfileId"); + migrationBuilder.CreateIndex( name: "IX_AiModelHashes_UniqueHash", table: "AiModelHashes", @@ -412,15 +648,32 @@ protected override void Up(MigrationBuilder migrationBuilder) unique: true); migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_CanonicalKey", + name: "IX_BaselineQuantDefinitions_ArchitectureFamilyId_NormalizedCanonicalKey", + table: "BaselineQuantDefinitions", + columns: new[] { "ArchitectureFamilyId", "NormalizedCanonicalKey" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_ArchitectureFamilyId_NormalizedSourceRepository_NormalizedSourceFileName", + table: "BaselineQuantDefinitions", + columns: new[] { "ArchitectureFamilyId", "NormalizedSourceRepository", "NormalizedSourceFileName" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_ArchitectureFamilyId_RuntimeBaselineId", table: "BaselineQuantDefinitions", - column: "CanonicalKey", + columns: new[] { "ArchitectureFamilyId", "RuntimeBaselineId" }, unique: true); migrationBuilder.CreateIndex( - name: "IX_BaselineQuantDefinitions_SourceRepository_SourceFileName", + name: "IX_BaselineQuantDefinitions_IsActiveInCurrentConfig", + table: "BaselineQuantDefinitions", + column: "IsActiveInCurrentConfig"); + + migrationBuilder.CreateIndex( + name: "IX_BaselineQuantDefinitions_RuntimeBaselineId_ArchitectureFamilyId", table: "BaselineQuantDefinitions", - columns: new[] { "SourceRepository", "SourceFileName" }); + columns: new[] { "RuntimeBaselineId", "ArchitectureFamilyId" }); migrationBuilder.CreateIndex( name: "IX_BenchmarkRuns_AiBenchmarkId", @@ -437,6 +690,11 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "BenchmarkRuns", column: "AiModelHashId"); + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_ArchitectureFamilyId", + table: "BenchmarkRuns", + column: "ArchitectureFamilyId"); + migrationBuilder.CreateIndex( name: "IX_BenchmarkRuns_CategoryBenchmarkId", table: "BenchmarkRuns", @@ -457,6 +715,11 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "BenchmarkRuns", column: "TensorComboId"); + migrationBuilder.CreateIndex( + name: "IX_BenchmarkRuns_TensorGroupProfileId", + table: "BenchmarkRuns", + column: "TensorGroupProfileId"); + migrationBuilder.CreateIndex( name: "IX_CategoryBenchmark_AiBenchmarkId", table: "CategoryBenchmark", @@ -468,9 +731,14 @@ protected override void Up(MigrationBuilder migrationBuilder) column: "AiModelHashId"); migrationBuilder.CreateIndex( - name: "IX_ExecutionPlanProbeCaches_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", + name: "IX_ExecutionPlanProbeCaches_ArchitectureFamilyId", + table: "ExecutionPlanProbeCaches", + column: "ArchitectureFamilyId"); + + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_HardwareFingerprint_QuantizedModelFingerprint_QuantizationKey_DiscoveryTokenTarget", table: "ExecutionPlanProbeCaches", - columns: new[] { "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget" }, unique: true); migrationBuilder.CreateIndex( @@ -478,6 +746,11 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "ExecutionPlanProbeCaches", column: "ImatrixDefinitionId"); + migrationBuilder.CreateIndex( + name: "IX_ExecutionPlanProbeCaches_TensorGroupProfileId", + table: "ExecutionPlanProbeCaches", + column: "TensorGroupProfileId"); + migrationBuilder.CreateIndex( name: "IX_ImatrixDefinitions_AiModelHashId_IdentityHash", table: "ImatrixDefinitions", @@ -490,15 +763,35 @@ protected override void Up(MigrationBuilder migrationBuilder) column: "AiBenchmarkId"); migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineCanonicalKey_TensorWeightSchemeId_TensorName", + name: "IX_LearnedBaselineTensorQuants_AiModelHashId", + table: "LearnedBaselineTensorQuants", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_ArchitectureFamilyId_TensorGroupProfileId_BaselineQuantDefinitionId_TensorGroupId", + table: "LearnedBaselineTensorQuants", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorGroupId" }); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_ArchitectureFamilyId_TensorGroupProfileId_BaselineQuantDefinitionId_TensorWeightSchemeId_TensorName", table: "LearnedBaselineTensorQuants", - columns: new[] { "AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName" }, + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorWeightSchemeId", "TensorName" }, unique: true); migrationBuilder.CreateIndex( - name: "IX_LearnedBaselineTensorQuants_AiModelHashId_BaselineQuantId_TensorWeightSchemeId_TensorGroupId", + name: "IX_LearnedBaselineTensorQuants_BaselineQuantDefinitionId", + table: "LearnedBaselineTensorQuants", + column: "BaselineQuantDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_TensorComboId", + table: "LearnedBaselineTensorQuants", + column: "TensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_LearnedBaselineTensorQuants_TensorGroupProfileId", table: "LearnedBaselineTensorQuants", - columns: new[] { "AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId" }); + column: "TensorGroupProfileId"); migrationBuilder.CreateIndex( name: "IX_QuantizationRuns_AiBenchmarkId", @@ -510,6 +803,11 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "QuantizationRuns", column: "AiModelHashId"); + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_ArchitectureFamilyId", + table: "QuantizationRuns", + column: "ArchitectureFamilyId"); + migrationBuilder.CreateIndex( name: "IX_QuantizationRuns_ImatrixDefinitionId", table: "QuantizationRuns", @@ -525,21 +823,37 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "QuantizationRuns", column: "TensorComboId"); + migrationBuilder.CreateIndex( + name: "IX_QuantizationRuns_TensorGroupProfileId", + table: "QuantizationRuns", + column: "TensorGroupProfileId"); + migrationBuilder.CreateIndex( name: "IX_TensorCombos_BaseQuant_Embeddings_LmHead_AttnQ_AttnKV_AttnOutput_FfnUpGate_FfnDown_MoeExperts_MoeRouter", table: "TensorCombos", columns: new[] { "BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter" }, unique: true); + + migrationBuilder.CreateIndex( + name: "IX_TensorGroupProfiles_ArchitectureFamilyId_FingerprintHash", + table: "TensorGroupProfiles", + columns: new[] { "ArchitectureFamilyId", "FingerprintHash" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_TensorGroupProfiles_ArchitectureFamilyId_IsActive", + table: "TensorGroupProfiles", + columns: new[] { "ArchitectureFamilyId", "IsActive" }); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( - name: "ArchitectureFamilyModelHashes"); + name: "AiBenchmarkLearnedSources"); migrationBuilder.DropTable( - name: "BaselineQuantDefinitions"); + name: "ArchitectureFamilyModelHashes"); migrationBuilder.DropTable( name: "BenchmarkRuns"); @@ -554,10 +868,10 @@ protected override void Down(MigrationBuilder migrationBuilder) name: "QuantizationRuns"); migrationBuilder.DropTable( - name: "ArchitectureFamilies"); + name: "CategoryBenchmark"); migrationBuilder.DropTable( - name: "CategoryBenchmark"); + name: "BaselineQuantDefinitions"); migrationBuilder.DropTable( name: "AiBenchmarks"); @@ -568,8 +882,14 @@ protected override void Down(MigrationBuilder migrationBuilder) migrationBuilder.DropTable( name: "TensorCombos"); + migrationBuilder.DropTable( + name: "TensorGroupProfiles"); + migrationBuilder.DropTable( name: "AiModelHashes"); + + migrationBuilder.DropTable( + name: "ArchitectureFamilies"); } } } diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index c058062..7856dbc 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -25,6 +25,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("ImatrixDefinitionId") .HasColumnType("INTEGER"); @@ -37,21 +40,82 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("TensorComboId") .HasColumnType("TEXT"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.Property("TokensPerSecond") .HasColumnType("REAL"); b.HasKey("Id"); + b.HasIndex("AiModelHashId"); + b.HasIndex("ImatrixDefinitionId"); b.HasIndex("TensorComboId"); - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "TensorComboId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "TensorComboId") .IsUnique(); b.ToTable("AiBenchmarks"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmarkLearnedSource", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("SourceLearningBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaselineQuantDefinitionId"); + + b.HasIndex("SourceLearningBenchmarkId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("AiBenchmarkId", "TensorGroupId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId"); + + b.ToTable("AiBenchmarkLearnedSources"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => { b.Property("Id") @@ -137,9 +201,17 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => { - b.Property("BaselineQuantId") + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") .HasColumnType("INTEGER"); + b.Property("BaselineFamily") + .HasMaxLength(128) + .HasColumnType("TEXT"); + b.Property("BaselineName") .IsRequired() .HasMaxLength(128) @@ -150,7 +222,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("CanonicalKey") .IsRequired() - .HasMaxLength(256) + .HasMaxLength(512) .HasColumnType("TEXT"); b.Property("DefaultTensorSchemeId") @@ -161,9 +233,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(64) .HasColumnType("TEXT"); + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + b.Property("ExplicitCandidateSortOrder") .HasColumnType("INTEGER"); + b.Property("FirstSeenUtc") + .HasColumnType("TEXT"); + + b.Property("IsActiveInCurrentConfig") + .HasColumnType("INTEGER"); + b.Property("IsCombinationCarrierCandidate") .HasColumnType("INTEGER"); @@ -176,6 +259,25 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("IsLearningBaseline") .HasColumnType("INTEGER"); + b.Property("LastSeenUtc") + .HasColumnType("TEXT"); + + b.Property("LastUpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("NormalizedCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("NormalizedSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("NormalizedSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + b.Property("QuantizeBaseArgumentName") .IsRequired() .HasMaxLength(64) @@ -184,6 +286,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("RequiresImatrix") .HasColumnType("INTEGER"); + b.Property("RuntimeBaselineId") + .HasColumnType("INTEGER"); + b.Property("ShortSourceName") .HasMaxLength(64) .HasColumnType("TEXT"); @@ -205,12 +310,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(256) .HasColumnType("TEXT"); - b.HasKey("BaselineQuantId"); + b.HasKey("Id"); + + b.HasIndex("IsActiveInCurrentConfig"); + + b.HasIndex("ArchitectureFamilyId", "NormalizedCanonicalKey") + .IsUnique(); - b.HasIndex("CanonicalKey") + b.HasIndex("ArchitectureFamilyId", "RuntimeBaselineId") .IsUnique(); - b.HasIndex("SourceRepository", "SourceFileName"); + b.HasIndex("RuntimeBaselineId", "ArchitectureFamilyId"); + + b.HasIndex("ArchitectureFamilyId", "NormalizedSourceRepository", "NormalizedSourceFileName") + .IsUnique(); b.ToTable("BaselineQuantDefinitions"); }); @@ -226,6 +339,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("Category") .HasColumnType("INTEGER"); @@ -254,12 +370,17 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("TensorComboId") .HasColumnType("TEXT"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.HasKey("Id"); b.HasIndex("AiBenchmarkId"); b.HasIndex("AiModelHashId"); + b.HasIndex("ArchitectureFamilyId"); + b.HasIndex("CategoryBenchmarkId"); b.HasIndex("ImatrixDefinitionId"); @@ -268,6 +389,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("TensorComboId"); + b.HasIndex("TensorGroupProfileId"); + b.HasIndex("AiBenchmarkId", "Category"); b.ToTable("BenchmarkRuns"); @@ -308,6 +431,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("CreatedUtc") .HasColumnType("TEXT"); @@ -371,6 +497,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("StaticNgl") .HasColumnType("INTEGER"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.Property("TensorSplitJson") .IsRequired() .HasMaxLength(4000) @@ -386,9 +515,13 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("AiModelHashId"); + b.HasIndex("ArchitectureFamilyId"); + b.HasIndex("ImatrixDefinitionId"); - b.HasIndex("AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") .IsUnique(); b.ToTable("ExecutionPlanProbeCaches"); @@ -442,7 +575,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => { b.Property("Id") - .ValueGeneratedOnAdd() .HasColumnType("TEXT"); b.Property("AiBenchmarkId") @@ -451,11 +583,17 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("BaselineCanonicalKey") .IsRequired() - .HasMaxLength(256) + .HasMaxLength(512) .HasColumnType("TEXT"); + b.Property("BaselineQuantDefinitionId") + .HasColumnType("INTEGER"); + b.Property("BaselineQuantId") .HasColumnType("INTEGER"); @@ -477,9 +615,15 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(32) .HasColumnType("TEXT"); + b.Property("TensorComboId") + .HasColumnType("TEXT"); + b.Property("TensorGroupId") .HasColumnType("INTEGER"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.Property("TensorName") .IsRequired() .HasMaxLength(512) @@ -492,10 +636,18 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("AiBenchmarkId"); - b.HasIndex("AiModelHashId", "BaselineCanonicalKey", "TensorWeightSchemeId", "TensorName") - .IsUnique(); + b.HasIndex("AiModelHashId"); - b.HasIndex("AiModelHashId", "BaselineQuantId", "TensorWeightSchemeId", "TensorGroupId"); + b.HasIndex("BaselineQuantDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorGroupId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); b.ToTable("LearnedBaselineTensorQuants"); }); @@ -511,6 +663,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("AiModelHashId") .HasColumnType("INTEGER"); + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + b.Property("CompletedUtc") .HasColumnType("TEXT"); @@ -537,18 +692,25 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("TensorComboId") .HasColumnType("TEXT"); + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + b.HasKey("Id"); b.HasIndex("AiBenchmarkId"); b.HasIndex("AiModelHashId"); + b.HasIndex("ArchitectureFamilyId"); + b.HasIndex("ImatrixDefinitionId"); b.HasIndex("StartedUtc"); b.HasIndex("TensorComboId"); + b.HasIndex("TensorGroupProfileId"); + b.ToTable("QuantizationRuns"); }); @@ -595,6 +757,40 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("TensorCombos"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorGroupProfile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("FingerprintHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("IsActive") + .HasColumnType("INTEGER"); + + b.Property("SnapshotJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ArchitectureFamilyId", "FingerprintHash") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "IsActive"); + + b.ToTable("TensorGroupProfiles"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => { b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") @@ -603,6 +799,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") .WithMany() .HasForeignKey("ImatrixDefinitionId") @@ -614,11 +816,71 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Restrict) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiModelHash"); + b.Navigation("ArchitectureFamily"); + b.Navigation("ImatrixDefinition"); b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmarkLearnedSource", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("LearnedSources") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.BaselineQuantDefinition", "BaselineQuantDefinition") + .WithMany() + .HasForeignKey("BaselineQuantDefinitionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "SourceLearningBenchmark") + .WithMany() + .HasForeignKey("SourceLearningBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("BaselineQuantDefinition"); + + b.Navigation("SourceLearningBenchmark"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => @@ -640,6 +902,16 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("ArchitectureFamily"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade); + + b.Navigation("ArchitectureFamily"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => { b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") @@ -654,6 +926,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") .WithMany() .HasForeignKey("CategoryBenchmarkId") @@ -670,15 +948,25 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Restrict) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiBenchmark"); b.Navigation("AiModelHash"); + b.Navigation("ArchitectureFamily"); + b.Navigation("CategoryBenchmark"); b.Navigation("ImatrixDefinition"); b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => @@ -700,14 +988,30 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") .WithMany() .HasForeignKey("ImatrixDefinitionId") .OnDelete(DeleteBehavior.Restrict); + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiModelHash"); + b.Navigation("ArchitectureFamily"); + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => @@ -735,9 +1039,41 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.BaselineQuantDefinition", "BaselineQuantDefinition") + .WithMany() + .HasForeignKey("BaselineQuantDefinitionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiBenchmark"); b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("BaselineQuantDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => @@ -753,6 +1089,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") .WithMany() .HasForeignKey("ImatrixDefinitionId") @@ -764,18 +1106,41 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Restrict) .IsRequired(); + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.Navigation("AiBenchmark"); b.Navigation("AiModelHash"); + b.Navigation("ArchitectureFamily"); + b.Navigation("ImatrixDefinition"); b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorGroupProfile", b => + { + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ArchitectureFamily"); }); modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => { b.Navigation("CategorBenchmarks"); + + b.Navigation("LearnedSources"); }); #pragma warning restore 612, 618 } diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 67d1cbe..c2714fc 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -249,11 +249,19 @@ public static void RegisterDynamicCustomBaseline(BaselineQuants baseline) lock (DynamicLock) { - if (GetAllRecognizedBaselines().Any(x => x.UniqueId == baseline.UniqueId)) - throw new InvalidOperationException($"Dynamic baseline id collision detected for id '{baseline.UniqueId}'."); - - if (GetAllRecognizedBaselines().Any(x => string.Equals(x.CanonicalKey, baseline.CanonicalKey, StringComparison.Ordinal))) - throw new InvalidOperationException($"Dynamic baseline canonical key collision detected for '{baseline.CanonicalKey}'."); + var existingDynamic = DynamicCustomBaselines.FirstOrDefault(x => x.UniqueId == baseline.UniqueId || string.Equals(x.CanonicalKey, baseline.CanonicalKey, StringComparison.Ordinal)); + if (existingDynamic != null) + { + DynamicCustomBaselines.Remove(existingDynamic); + } + else + { + var builtInCollision = StandardBaselines.Concat(ExactAliases) + .FirstOrDefault(x => x.UniqueId == baseline.UniqueId || string.Equals(x.CanonicalKey, baseline.CanonicalKey, StringComparison.Ordinal)); + + if (builtInCollision != null) + throw new InvalidOperationException($"Dynamic baseline collision detected against built-in baseline '{builtInCollision.Names[0]}' for id/key '{baseline.UniqueId}/{baseline.CanonicalKey}'."); + } DynamicCustomBaselines.Add(baseline); } @@ -617,4 +625,4 @@ public static BaselineQuants FromTensorSchemeId(byte schemeId) return found; } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/DbModels/AiBenchmark.cs b/MQ.DB/Models/DbModels/AiBenchmark.cs index 4c28ba1..b97c32f 100644 --- a/MQ.DB/Models/DbModels/AiBenchmark.cs +++ b/MQ.DB/Models/DbModels/AiBenchmark.cs @@ -27,6 +27,12 @@ public class AiBenchmark : ISQLiteEntity public double TokensPerSecond { get; set; } + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + + public int TensorGroupProfileId { get; set; } + public TensorGroupProfile TensorGroupProfile { get; set; } = default!; + /// /// foreign key /// @@ -45,6 +51,7 @@ public class AiBenchmark : ISQLiteEntity public ImatrixDefinition? ImatrixDefinition { get; set; } public List CategorBenchmarks { get; set; } = new(); + public List LearnedSources { get; set; } = new(); public void Configure(EntityTypeBuilder builder) { @@ -53,9 +60,21 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.Id) .ValueGeneratedNever(); - builder.HasIndex(x => new { x.AiModelHashId, x.ImatrixDefinitionId, x.TensorComboId }) + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.TensorComboId }) .IsUnique(); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.TensorComboId }); + + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorGroupProfile) + .WithMany() + .HasForeignKey(x => x.TensorGroupProfileId) + .OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.TensorCombo) .WithMany() .HasForeignKey(x => x.TensorComboId) @@ -75,6 +94,11 @@ public void Configure(EntityTypeBuilder builder) .WithOne(x => x.AiBenchmark) .HasForeignKey(x => x.AiBenchmarkId) .OnDelete(DeleteBehavior.Cascade); + + builder.HasMany(x => x.LearnedSources) + .WithOne(x => x.AiBenchmark) + .HasForeignKey(x => x.AiBenchmarkId) + .OnDelete(DeleteBehavior.Cascade); } } @@ -112,4 +136,4 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.AiBenchmarkId) .OnDelete(DeleteBehavior.Cascade); } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs b/MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs new file mode 100644 index 0000000..1c755d2 --- /dev/null +++ b/MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs @@ -0,0 +1,74 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class AiBenchmarkLearnedSource : ISQLiteEntity +{ + public Guid Id { get; set; } = Guid.NewGuid(); + + public Guid AiBenchmarkId { get; set; } + public AiBenchmark AiBenchmark { get; set; } = default!; + + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + + public int TensorGroupProfileId { get; set; } + public TensorGroupProfile TensorGroupProfile { get; set; } = default!; + + public Guid TensorComboId { get; set; } + public TensorCombo TensorCombo { get; set; } = default!; + + public byte TensorGroupId { get; set; } + + public int BaselineQuantDefinitionId { get; set; } + public BaselineQuantDefinition BaselineQuantDefinition { get; set; } = default!; + + public Guid? SourceLearningBenchmarkId { get; set; } + public AiBenchmark? SourceLearningBenchmark { get; set; } + + public string BaselineCanonicalKey { get; set; } = string.Empty; + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(x => x.Id).ValueGeneratedNever(); + builder.Property(x => x.BaselineCanonicalKey).HasMaxLength(512).IsRequired(); + + builder.HasIndex(x => new { x.AiBenchmarkId, x.TensorGroupId }).IsUnique(); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.BaselineQuantDefinitionId }); + builder.HasIndex(x => x.TensorComboId); + + builder.HasOne(x => x.AiBenchmark) + .WithMany(x => x.LearnedSources) + .HasForeignKey(x => x.AiBenchmarkId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorGroupProfile) + .WithMany() + .HasForeignKey(x => x.TensorGroupProfileId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorCombo) + .WithMany() + .HasForeignKey(x => x.TensorComboId) + .OnDelete(DeleteBehavior.Restrict); + + builder.HasOne(x => x.BaselineQuantDefinition) + .WithMany() + .HasForeignKey(x => x.BaselineQuantDefinitionId) + .OnDelete(DeleteBehavior.Restrict); + + builder.HasOne(x => x.SourceLearningBenchmark) + .WithMany() + .HasForeignKey(x => x.SourceLearningBenchmarkId) + .OnDelete(DeleteBehavior.SetNull); + } +} diff --git a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs index 4ced612..1a058e7 100644 --- a/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs +++ b/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs @@ -6,17 +6,34 @@ namespace MQ.DB.Models.DbModels; public class BaselineQuantDefinition : ISQLiteEntity { - public byte BaselineQuantId { get; set; } + public int Id { get; set; } + + /// + /// Null for built-in standard/exact aliases. Non-null for architecture-family-scoped custom baselines. + /// + public int? ArchitectureFamilyId { get; set; } + public ArchitectureFamily? ArchitectureFamily { get; set; } + + /// + /// Compact id used inside TensorCombo slots. + /// + public byte RuntimeBaselineId { get; set; } + public string CanonicalKey { get; set; } = string.Empty; + public string NormalizedCanonicalKey { get; set; } = string.Empty; public string BaselineName { get; set; } = string.Empty; + public string DisplayName { get; set; } = string.Empty; public string QuantizeBaseArgumentName { get; set; } = string.Empty; public byte DefaultTensorSchemeId { get; set; } public string DefaultTensorSchemeName { get; set; } = string.Empty; public string SourceKind { get; set; } = string.Empty; public string? SourceOwner { get; set; } public string? SourceRepository { get; set; } + public string? NormalizedSourceRepository { get; set; } public string? SourceFileName { get; set; } + public string? NormalizedSourceFileName { get; set; } public string? ShortSourceName { get; set; } + public string? BaselineFamily { get; set; } public bool IsCustomBaseline { get; set; } public bool IsLearningBaseline { get; set; } public bool IsCombinationCarrierCandidate { get; set; } @@ -24,44 +41,39 @@ public class BaselineQuantDefinition : ISQLiteEntity public bool RequiresImatrix { get; set; } public byte BitRange { get; set; } public int ExplicitCandidateSortOrder { get; set; } + public bool IsActiveInCurrentConfig { get; set; } = true; + public DateTime FirstSeenUtc { get; set; } = DateTime.UtcNow; + public DateTime LastSeenUtc { get; set; } = DateTime.UtcNow; + public DateTime LastUpdatedUtc { get; set; } = DateTime.UtcNow; public void Configure(EntityTypeBuilder builder) { - builder.HasKey(x => x.BaselineQuantId); - - builder.Property(x => x.CanonicalKey) - .HasMaxLength(256) - .IsRequired(); - - builder.Property(x => x.BaselineName) - .HasMaxLength(128) - .IsRequired(); - - builder.Property(x => x.QuantizeBaseArgumentName) - .HasMaxLength(64) - .IsRequired(); - - builder.Property(x => x.DefaultTensorSchemeName) - .HasMaxLength(64) - .IsRequired(); - - builder.Property(x => x.SourceKind) - .HasMaxLength(64) - .IsRequired(); - - builder.Property(x => x.SourceOwner) - .HasMaxLength(128); - - builder.Property(x => x.SourceRepository) - .HasMaxLength(256); + builder.HasKey(x => x.Id); - builder.Property(x => x.SourceFileName) - .HasMaxLength(512); + builder.Property(x => x.CanonicalKey).HasMaxLength(512).IsRequired(); + builder.Property(x => x.NormalizedCanonicalKey).HasMaxLength(512).IsRequired(); + builder.Property(x => x.BaselineName).HasMaxLength(128).IsRequired(); + builder.Property(x => x.DisplayName).HasMaxLength(128).IsRequired(); + builder.Property(x => x.QuantizeBaseArgumentName).HasMaxLength(64).IsRequired(); + builder.Property(x => x.DefaultTensorSchemeName).HasMaxLength(64).IsRequired(); + builder.Property(x => x.SourceKind).HasMaxLength(64).IsRequired(); + builder.Property(x => x.SourceOwner).HasMaxLength(128); + builder.Property(x => x.SourceRepository).HasMaxLength(256); + builder.Property(x => x.NormalizedSourceRepository).HasMaxLength(256); + builder.Property(x => x.SourceFileName).HasMaxLength(512); + builder.Property(x => x.NormalizedSourceFileName).HasMaxLength(512); + builder.Property(x => x.ShortSourceName).HasMaxLength(64); + builder.Property(x => x.BaselineFamily).HasMaxLength(128); - builder.Property(x => x.ShortSourceName) - .HasMaxLength(64); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.RuntimeBaselineId }).IsUnique(); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.NormalizedCanonicalKey }).IsUnique(); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.NormalizedSourceRepository, x.NormalizedSourceFileName }).IsUnique(); + builder.HasIndex(x => new { x.RuntimeBaselineId, x.ArchitectureFamilyId }); + builder.HasIndex(x => x.IsActiveInCurrentConfig); - builder.HasIndex(x => x.CanonicalKey).IsUnique(); - builder.HasIndex(x => new { x.SourceRepository, x.SourceFileName }); + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/DbModels/BenchmarkRun.cs b/MQ.DB/Models/DbModels/BenchmarkRun.cs index 11dc7ad..96a1b47 100644 --- a/MQ.DB/Models/DbModels/BenchmarkRun.cs +++ b/MQ.DB/Models/DbModels/BenchmarkRun.cs @@ -8,6 +8,12 @@ public class BenchmarkRun : ISQLiteEntity { public Guid Id { get; set; } + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + + public int TensorGroupProfileId { get; set; } + public TensorGroupProfile TensorGroupProfile { get; set; } = default!; + public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; @@ -48,6 +54,8 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.Id) .ValueGeneratedNever(); + builder.HasIndex(x => x.ArchitectureFamilyId); + builder.HasIndex(x => x.TensorGroupProfileId); builder.HasIndex(x => x.AiModelHashId); builder.HasIndex(x => x.ImatrixDefinitionId); builder.HasIndex(x => x.TensorComboId); @@ -59,6 +67,16 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.Error) .HasMaxLength(4000); + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorGroupProfile) + .WithMany() + .HasForeignKey(x => x.TensorGroupProfileId) + .OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.AiModelHash) .WithMany() .HasForeignKey(x => x.AiModelHashId) @@ -84,4 +102,4 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.CategoryBenchmarkId) .OnDelete(DeleteBehavior.SetNull); } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs index d1485c2..d7cdf87 100644 --- a/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs +++ b/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs @@ -8,6 +8,12 @@ public class ExecutionPlanProbeCache : ISQLiteEntity { public Guid Id { get; set; } = Guid.NewGuid(); + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + + public int TensorGroupProfileId { get; set; } + public TensorGroupProfile TensorGroupProfile { get; set; } = default!; + public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; @@ -50,10 +56,14 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.GpuMemoryLimitsJson).HasMaxLength(4000); builder.Property(x => x.TensorSplitJson).HasMaxLength(4000); + builder.HasIndex(x => x.ArchitectureFamilyId); + builder.HasIndex(x => x.TensorGroupProfileId); builder.HasIndex(x => x.AiModelHashId); builder.HasIndex(x => x.ImatrixDefinitionId); builder.HasIndex(x => new { + x.ArchitectureFamilyId, + x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.HardwareFingerprint, @@ -62,6 +72,16 @@ public void Configure(EntityTypeBuilder builder) x.DiscoveryTokenTarget }).IsUnique(); + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorGroupProfile) + .WithMany() + .HasForeignKey(x => x.TensorGroupProfileId) + .OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.AiModelHash) .WithMany() .HasForeignKey(x => x.AiModelHashId) diff --git a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs index b8c33c9..3568e36 100644 --- a/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs +++ b/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs @@ -8,13 +8,32 @@ public class LearnedBaselineTensorQuant : ISQLiteEntity + /// Exact source model hash used when learning happened. Reuse is scoped by architecture family/profile. + /// public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; + /// + /// Snapshot of the compact runtime id at learning time. + /// public byte BaselineQuantId { get; set; } + public string BaselineCanonicalKey { get; set; } = string.Empty; public string BaselineSourceKind { get; set; } = string.Empty; public string? BaselineSourceRepository { get; set; } @@ -29,45 +48,56 @@ public class LearnedBaselineTensorQuant : ISQLiteEntity builder) { builder.HasKey(x => x.Id); + builder.Property(x => x.Id).ValueGeneratedNever(); - builder.Property(x => x.BaselineCanonicalKey) - .HasMaxLength(256) - .IsRequired(); - - builder.Property(x => x.BaselineSourceKind) - .HasMaxLength(64) - .IsRequired(); - - builder.Property(x => x.BaselineSourceRepository) - .HasMaxLength(256); - - builder.Property(x => x.BaselineSourceFileName) - .HasMaxLength(512); - - builder.Property(x => x.TensorName) - .HasMaxLength(512) - .IsRequired(); - - builder.Property(x => x.FinalQuantType) - .HasMaxLength(32) - .IsRequired(); + builder.Property(x => x.BaselineCanonicalKey).HasMaxLength(512).IsRequired(); + builder.Property(x => x.BaselineSourceKind).HasMaxLength(64).IsRequired(); + builder.Property(x => x.BaselineSourceRepository).HasMaxLength(256); + builder.Property(x => x.BaselineSourceFileName).HasMaxLength(512); + builder.Property(x => x.TensorName).HasMaxLength(512).IsRequired(); + builder.Property(x => x.FinalQuantType).HasMaxLength(32).IsRequired(); builder.HasIndex(x => new { - x.AiModelHashId, - x.BaselineCanonicalKey, + x.ArchitectureFamilyId, + x.TensorGroupProfileId, + x.BaselineQuantDefinitionId, x.TensorWeightSchemeId, x.TensorName }) .IsUnique(); builder.HasIndex(x => new - { - x.AiModelHashId, - x.BaselineQuantId, - x.TensorWeightSchemeId, - x.TensorGroupId - }); + { + x.ArchitectureFamilyId, + x.TensorGroupProfileId, + x.BaselineQuantDefinitionId, + x.TensorGroupId + }); + + builder.HasIndex(x => x.AiBenchmarkId); + builder.HasIndex(x => x.AiModelHashId); + builder.HasIndex(x => x.TensorComboId); + + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorGroupProfile) + .WithMany() + .HasForeignKey(x => x.TensorGroupProfileId) + .OnDelete(DeleteBehavior.Restrict); + + builder.HasOne(x => x.BaselineQuantDefinition) + .WithMany() + .HasForeignKey(x => x.BaselineQuantDefinitionId) + .OnDelete(DeleteBehavior.Restrict); + + builder.HasOne(x => x.TensorCombo) + .WithMany() + .HasForeignKey(x => x.TensorComboId) + .OnDelete(DeleteBehavior.Restrict); builder.HasOne(x => x.AiModelHash) .WithMany() @@ -79,4 +109,4 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.AiBenchmarkId) .OnDelete(DeleteBehavior.Cascade); } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/DbModels/QuantizationRun.cs b/MQ.DB/Models/DbModels/QuantizationRun.cs index 3ef7a2a..7adec02 100644 --- a/MQ.DB/Models/DbModels/QuantizationRun.cs +++ b/MQ.DB/Models/DbModels/QuantizationRun.cs @@ -8,6 +8,12 @@ public class QuantizationRun : ISQLiteEntity { public Guid Id { get; set; } = Guid.NewGuid(); + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + + public int TensorGroupProfileId { get; set; } + public TensorGroupProfile TensorGroupProfile { get; set; } = default!; + public uint AiModelHashId { get; set; } public AiModelHash AiModelHash { get; set; } = default!; @@ -44,6 +50,8 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.Id) .ValueGeneratedNever(); + builder.HasIndex(x => x.ArchitectureFamilyId); + builder.HasIndex(x => x.TensorGroupProfileId); builder.HasIndex(x => x.AiModelHashId); builder.HasIndex(x => x.ImatrixDefinitionId); builder.HasIndex(x => x.TensorComboId); @@ -56,6 +64,16 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.OutputModelPath) .HasMaxLength(2048); + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); + + builder.HasOne(x => x.TensorGroupProfile) + .WithMany() + .HasForeignKey(x => x.TensorGroupProfileId) + .OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.AiModelHash) .WithMany() .HasForeignKey(x => x.AiModelHashId) @@ -76,4 +94,4 @@ public void Configure(EntityTypeBuilder builder) .HasForeignKey(x => x.AiBenchmarkId) .OnDelete(DeleteBehavior.SetNull); } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/DbModels/TensorGroupProfile.cs b/MQ.DB/Models/DbModels/TensorGroupProfile.cs new file mode 100644 index 0000000..7516401 --- /dev/null +++ b/MQ.DB/Models/DbModels/TensorGroupProfile.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class TensorGroupProfile : ISQLiteEntity +{ + public int Id { get; set; } + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + public string FingerprintHash { get; set; } = string.Empty; + public string SnapshotJson { get; set; } = string.Empty; + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; + public bool IsActive { get; set; } = true; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(x => x.FingerprintHash).HasMaxLength(128).IsRequired(); + builder.Property(x => x.SnapshotJson).IsRequired(); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.FingerprintHash }).IsUnique(); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.IsActive }); + + builder.HasOne(x => x.ArchitectureFamily) + .WithMany() + .HasForeignKey(x => x.ArchitectureFamilyId) + .OnDelete(DeleteBehavior.Cascade); + } +} diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 29669a6..8f23940 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -89,6 +89,15 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) scheme.UniqueId == F32.UniqueId; } + + public static TensorWeightScheme FromId(byte id) + { + var found = All.FirstOrDefault(x => x.UniqueId == id); + if (found == null) + throw new InvalidOperationException($"Unknown tensor weight scheme id '{id}'."); + return found; + } + // Compatibility shim for any older code still referencing BF16_F16. public static TensorWeightScheme BF16_F16 => GetCurrentNativePrecisionScheme(); @@ -259,6 +268,8 @@ public static bool IsNativePrecisionScheme(TensorWeightScheme scheme) IQ2_S, IQ2_XS, IQ2_XXS, - Q4_K + Q4_K, + Q4_K_S, + Q5_K_S ]; } diff --git a/MQ.DB/tensor_groups.yaml b/MQ.DB/tensor_groups.yaml index d9ae5e1..e6cd041 100644 --- a/MQ.DB/tensor_groups.yaml +++ b/MQ.DB/tensor_groups.yaml @@ -226,19 +226,24 @@ groups: - ".*layers\\..*\\.experts\\.down_proj.*" moe_router: - description: "MoE router/gating tensors." + description: "MoE router/gating tensors. Keep this MoE-specific so dense FFN gates, attention gates, and Qwen3.6 hybrid/SSM gates do not masquerade as routers." patterns: + # Top-level router/gating forms. + # These are intentionally anchored to names that begin with router/gating/routing. - "^router.*" - "^gating.*" - "^routing.*" - # Generic gate/router forms. - # Negative lookbehind prevents ffn_gate.weight from being treated as router. - - ".*(? args) Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); - Cache.ForceRelearnBaselineTensorMappings = false; Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; Cache.ForceImatrixRebuild = Config.Current.Flags.ForceImatrixRebuild; @@ -116,6 +115,12 @@ public async Task Run(List args) var architectureFamilyService = new ArchitectureFamilyService(pyManager); await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(baseModelGgufPath); + var tensorGroupProfileService = new TensorGroupProfileService(); + await tensorGroupProfileService.EnsureCurrentProfileAsync(); + + var resolvedCustomBaselines = await hf.PrecheckAndRegisterConfiguredBaselinesAsync(); + await new TargetedRelearnService().PlanConfirmAndExecuteAsync(resolvedCustomBaselines); + var imatrixRequest = new ImatrixRequest { UseImatrix = Cache.UseImatrix, diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index c8132ed..88ec015 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -63,7 +63,6 @@ public async Task Run(List args) Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); - Cache.ForceRelearnBaselineTensorMappings = Config.Current.Flags.ForceRelearnBaselineTensorMappings; Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; Cache.ForceImatrixRebuild = Config.Current.Flags.ForceImatrixRebuild; @@ -95,27 +94,12 @@ public async Task Run(List args) var pyManager = new PythonManager(Cache.MagicQuantDirectory!); - var customBaselineService = new HuggingFaceBaselineService(pyManager); - var resolvedCustomBaselines = await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); - - if (Config.Current.Baselines.CustomRepositories.Any(x => x.Enabled) && resolvedCustomBaselines.Count == 0) - { - throw new InvalidOperationException( - "Custom baseline repositories were enabled, but no custom baselines resolved into the runtime registry."); - } - await EnsureSqliteReadyAsync(); var benchmarkService = new BenchmarkService(pyManager); var quantizationService = new QuantizationService(benchmarkService); var imatrixService = new ImatrixService(); - if (Cache.ForceRelearnBaselineTensorMappings) - { - await quantizationService.InvalidateBaselineArtifactsAsync(); - AnsiConsole.MarkupLine("[yellow]Forced relearn is ON:[/] pure baseline samples will be rebuilt and relearned."); - } - string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); @@ -125,6 +109,20 @@ public async Task Run(List args) var architectureFamilyService = new ArchitectureFamilyService(pyManager); await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(bf16ModelGgufPath); + var tensorGroupProfileService = new TensorGroupProfileService(); + await tensorGroupProfileService.EnsureCurrentProfileAsync(); + + var customBaselineService = new HuggingFaceBaselineService(pyManager); + var resolvedCustomBaselines = await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); + + if (Config.Current.Baselines.CustomRepositories.Any(x => x.Enabled) && resolvedCustomBaselines.Count == 0) + { + throw new InvalidOperationException( + "Custom baseline repositories were enabled, but no custom baselines resolved into the runtime registry."); + } + + await new TargetedRelearnService().PlanConfirmAndExecuteAsync(resolvedCustomBaselines); + var imatrixRequest = new ImatrixRequest { UseImatrix = Cache.UseImatrix, @@ -176,7 +174,6 @@ await benchmarkService.EnsureDynamicExecutionPlanAsync( } bool nativeTruthAlreadyLearned = - !Cache.ForceRelearnBaselineTensorMappings && await quantizationService.HasNativeSourceLearnedTruthAsync(); if (nativeTruthAlreadyLearned && loadedPlanFromCache) { @@ -430,21 +427,13 @@ private static async Task EnsureNativeBenchmarkEnvironmentReadyAsync( requiredDomains: RequiredNativeKldDomains); bool mustRegenerateNativeBenchmarkArtifacts = - Cache.ForceRelearnBaselineTensorMappings || !status.IsValid; if (mustRegenerateNativeBenchmarkArtifacts) { AnsiConsole.Write(new Rule("[yellow]Native BF16 Benchmark/KLD Artifact Validation[/]") { Justification = Justify.Left }); - if (Cache.ForceRelearnBaselineTensorMappings) - { - AnsiConsole.MarkupLine("[yellow]Forced relearn is ON:[/] native BF16 benchmark/logit artifacts will be regenerated."); - } - else - { - AnsiConsole.MarkupLine("[yellow]Native BF16 benchmark/KLD artifacts are missing or incomplete.[/] Regenerating required artifacts."); - } + AnsiConsole.MarkupLine("[yellow]Native BF16 benchmark/KLD artifacts are missing or incomplete.[/] Regenerating required artifacts."); PrintNativeBenchmarkEnvironmentIssues(status); @@ -481,6 +470,18 @@ await ForceRegenerateNativeBenchmarkArtifactsAsync( AnsiConsole.MarkupLine("[grey]Native BF16 benchmark/KLD artifacts already exist and passed validation.[/]"); } + // Disk artifact validation is not enough. Native tensor learning is tied to the + // persisted TensorCombo/AiBenchmark identity. The repair path above may run in + // transient mode so it can regenerate logits even when stale DB truth exists; after + // the artifacts are valid, explicitly hydrate/validate the SQLite benchmark row + // from those artifacts before native-source learning tries to attach to it. + await EnsureNativeBenchmarkDbTruthAsync( + benchmarkService: benchmarkService, + baseModelQuant: baseModelQuant, + bf16ModelGgufPath: bf16ModelGgufPath, + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir); + if (!nativeTruthAlreadyLearned) { await quantizationService.LearnNativeSourceTruthAsync(bf16ModelGgufPath); @@ -492,6 +493,35 @@ await ForceRegenerateNativeBenchmarkArtifactsAsync( } } + private static async Task EnsureNativeBenchmarkDbTruthAsync( + BenchmarkService benchmarkService, + HybridQuant baseModelQuant, + string bf16ModelGgufPath, + string baseBenchDir, + string baseLogitsDir) + { + bool previousSuppressBenchmarkPersistence = Cache.SuppressBenchmarkPersistence; + + try + { + Cache.SuppressBenchmarkPersistence = false; + + await benchmarkService.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, + modelPath: bf16ModelGgufPath, + benchDir: baseBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: true, + domainsOverride: RequiredNativeKldDomains); + + AnsiConsole.MarkupLine("[grey]Native BF16 benchmark DB truth hydrated/validated.[/]"); + } + finally + { + Cache.SuppressBenchmarkPersistence = previousSuppressBenchmarkPersistence; + } + } + private static async Task ForceRegenerateNativeBenchmarkArtifactsAsync( BenchmarkService benchmarkService, HybridQuant baseModelQuant, @@ -696,7 +726,6 @@ private void ShowEvolutionHelp() AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Arguments:[/]"); AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Optional if set in YAML)"); - AnsiConsole.MarkupLine(" [green]--relearn-baseline-mappings[/] Delete and relearn baseline tensor mappings (Optional)"); AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); AnsiConsole.MarkupLine(" [green]--use-imatrix[/] Enable imatrix acquisition/build and allow imatrix-required search candidates (Optional)"); AnsiConsole.MarkupLine(" [green]--allow-high-precision-hybrids[/] Keep BF16/F16 explicit group candidates in final surviving combos (Optional, default false)"); @@ -759,4 +788,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index d8e293f..0b84809 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -13,6 +13,7 @@ public sealed class MagicQuantYamlConfig public RuntimePredictionConfig Prediction { get; set; } = new(); public RuntimeIdentityConfig Identity { get; set; } = new(); public RuntimeBaselineConfig Baselines { get; set; } = new(); + public RuntimeLearningConfig Learning { get; set; } = new(); public RuntimeOutputConfig Output { get; set; } = new(); public RuntimeSurvivalConfig Survival { get; set; } = new(); public RuntimeCandidateSelectionConfig CandidateSelection { get; set; } = new(); @@ -95,7 +96,6 @@ public sealed class RuntimeFlagConfig { public bool UseImatrix { get; set; } public bool ForceImatrixRebuild { get; set; } - public bool ForceRelearnBaselineTensorMappings { get; set; } public bool ForceRefreshHardwareProbe { get; set; } public bool AllowHighPrecisionHybrids { get; set; } } @@ -247,6 +247,12 @@ public sealed class RuntimeCandidateSelectionConfig public bool AllowEightBitAnchorReplacements { get; set; } = false; } +public sealed class RuntimeLearningConfig +{ + public bool ForceRelearnArchitectureFamily { get; set; } + public List ForceRelearnStandardBaselines { get; set; } = new(); +} + public sealed class RuntimeBaselineConfig { public string StandardBaselinesMode { get; set; } = "all"; @@ -285,6 +291,7 @@ public sealed class CustomBaselineIncludeConfig public bool? AllowAsCombinationCarrier { get; set; } public bool? AllowAsExplicitGroupCandidate { get; set; } public bool? AllowAsLearningBaseline { get; set; } + public bool ForceRelearn { get; set; } public List BannedGroupIds { get; set; } = new(); } @@ -303,10 +310,13 @@ public sealed class ResolvedCustomBaselineSpec public bool AllowAsLearningBaseline { get; set; } public bool AllowAsCombinationCarrier { get; set; } public bool AllowAsExplicitGroupCandidate { get; set; } + public bool ForceRelearn { get; set; } + public int? BaselineQuantDefinitionId { get; set; } + public bool IsActiveInCurrentConfig { get; set; } = true; public IReadOnlyList BannedGroupIds { get; set; } = Array.Empty(); } public sealed class RuntimeHardwareConfig { public Dictionary GpuMemoryLimitsGb { get; set; } = new(); -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 9669b1c..cdfc395 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -30,6 +30,7 @@ public static MagicQuantYamlConfig LoadAndApply(string commandName, IReadOnlyLis .Build(); var yaml = File.ReadAllText(configPath); + RejectLegacyGlobalRelearnYaml(yaml, configPath); var loaded = deserializer.Deserialize(yaml) ?? MagicQuantYamlConfig.CreateDefault(); ApplyCliOverrides(loaded, args); @@ -68,7 +69,6 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) Cache.UseImatrix = config.Flags.UseImatrix; Cache.ForceImatrixRebuild = config.Flags.ForceImatrixRebuild; - Cache.ForceRelearnBaselineTensorMappings = config.Flags.ForceRelearnBaselineTensorMappings; Cache.ForceRefreshHardwareProbe = config.Flags.ForceRefreshHardwareProbe; config.Hardware.GpuMemoryLimitsGb ??= new Dictionary(); @@ -190,6 +190,18 @@ private static HashSet ResolveStandardBaselineIds(IEnumerable name return result; } + + private static void RejectLegacyGlobalRelearnYaml(string yaml, string configPath) + { + if (yaml.IndexOf("force_relearn_baseline_tensor_mappings", StringComparison.OrdinalIgnoreCase) < 0) + return; + + throw new InvalidOperationException( + $"Config '{configPath}' contains removed option 'flags.force_relearn_baseline_tensor_mappings'. " + + "This global destructive relearn mode has been removed. Use targeted relearn commands under 'learning:' " + + "or per custom include 'force_relearn: true'."); + } + private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList args) { string? Get(string name) => args.FirstOrDefault(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase))?.Value; @@ -202,7 +214,8 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (Has("use-imatrix")) config.Flags.UseImatrix = true; if (Has("imatrix-force-rebuild")) config.Flags.ForceImatrixRebuild = true; - if (Has("relearn-baseline-mappings")) config.Flags.ForceRelearnBaselineTensorMappings = true; + if (Has("relearn-baseline-mappings") || Has("force-relearn-baseline-tensor-mappings")) + throw new InvalidOperationException("--relearn-baseline-mappings was removed because it globally wiped learned tensor truth. Use YAML learning.force_relearn_architecture_family, learning.force_relearn_standard_baselines, or custom_repositories/includes/force_relearn instead."); if (Has("recheck-hardware-probe") || Has("force-refresh-hardware-probe") || Has("force_refresh_hardware_probe")) config.Flags.ForceRefreshHardwareProbe = true; if (Has("allow-high-precision-hybrids")) config.Flags.AllowHighPrecisionHybrids = true; @@ -351,4 +364,4 @@ private static List NormalizeScratchRoots(IEnumerable? roots) return Path.GetFullPath(value); } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/LlamaBuilder.cs b/MagicQuant/Helpers/LlamaBuilder.cs index f6e0a94..ece373d 100644 --- a/MagicQuant/Helpers/LlamaBuilder.cs +++ b/MagicQuant/Helpers/LlamaBuilder.cs @@ -9,150 +9,352 @@ namespace MagicQuant.Helpers; public class LlamaBuilder { + private const string LlamaCppRepositoryUrl = "https://github.com/ggerganov/llama.cpp.git"; + private readonly string _llamaRoot; private readonly SystemInfo _sysInfo; public LlamaBuilder(string magicRoot, SystemInfo sysInfo) { _llamaRoot = Path.Combine(magicRoot, MagicConstants.LlamaRepoName); - Cache.LlamaRoot = _llamaRoot; - Cache.LlamaBin = Path.Combine(Cache.LlamaRoot, "build", "bin"); - Cache.ConvertScript = Path.Combine(Cache.LlamaRoot, "convert_hf_to_gguf.py");; - _sysInfo = sysInfo; + + Cache.LlamaRoot = _llamaRoot; + Cache.LlamaBin = Path.Combine(_llamaRoot, "build", "bin"); + Cache.ConvertScript = ResolveConvertScriptPath(_llamaRoot); } public string GetLlamaBinPath() => Path.Combine(_llamaRoot, "build", "bin"); public async Task PrepareAndBuildAsync(bool forceRebuild) -{ - // 1. Validate ALL dependencies before doing anything - await DependencyManager.EnsureDependenciesAsync(_sysInfo); + { + // 1. Validate ALL dependencies before doing anything. + await DependencyManager.EnsureDependenciesAsync(_sysInfo); + + // 2. Ensure the llama.cpp checkout is real and buildable. + EnsureLlamaRepository(forceRebuild); + + Cache.LlamaRoot = _llamaRoot; + Cache.LlamaBin = Path.Combine(_llamaRoot, "build", "bin"); + Cache.ConvertScript = ResolveConvertScriptPath(_llamaRoot); + + ValidateLlamaSourceTreeOrThrow(); + + // 3. Setup Build Directory. + string buildDir = Path.Combine(_llamaRoot, "build"); + EnsureBuildDirectory(buildDir, forceRebuild); + + // 4. Get the CMake Executable (System or Local). + string cmakeExe = DependencyManager.GetCmakePath() ?? "cmake"; + + // 5. Generate Build Files. + var cmakeArgs = GetOptimalCmakeArgs(); + AnsiConsole.MarkupLine($"[grey]Configuring build with: {Markup.Escape(FormatArgsForDisplay(cmakeArgs))}[/]"); + + if (!await RunProcessAsync(cmakeExe, cmakeArgs, buildDir)) + throw new Exception("CMake configuration failed."); + + // 6. Compile. + AnsiConsole.MarkupLine("[cyan]Compiling llama.cpp (Release Mode)...[/]"); + + var buildArgs = new List + { + "--build", + ".", + "--config", + "Release", + "-j", + Environment.ProcessorCount.ToString() + }; + + if (!await RunProcessAsync(cmakeExe, buildArgs, buildDir)) + throw new Exception("Build failed."); + + AnsiConsole.MarkupLine("[green]✔ Build Success![/]"); + } + + private void EnsureLlamaRepository(bool forceRebuild) + { + bool rootExists = Directory.Exists(_llamaRoot); + bool rootIsValid = IsValidLlamaSourceTree(); + + if (forceRebuild && rootExists) + { + AnsiConsole.MarkupLine("[yellow]Update requested: removing existing llama.cpp checkout...[/]"); + DeleteDirectoryOrThrow(_llamaRoot, "update was requested"); + rootExists = false; + rootIsValid = false; + } + + if (rootExists && !rootIsValid) + { + var escapedRoot = Markup.Escape(_llamaRoot); + AnsiConsole.MarkupLine($"[yellow]Existing llama.cpp directory is invalid or incomplete: {escapedRoot}[/]"); + AnsiConsole.MarkupLine("[grey]Missing CMakeLists.txt or repository metadata. Removing it so MagicQuant can redeploy a clean checkout.[/]"); + DeleteDirectoryOrThrow(_llamaRoot, "existing llama.cpp checkout is invalid/incomplete"); + rootExists = false; + } + + if (!rootExists) + { + CloneLlamaRepository(); + return; + } - // 2. Clone / Pull / Update Logic - // If --update (forceRebuild) is passed, we delete the repo to force a clean clone. - if (forceRebuild && Directory.Exists(_llamaRoot)) + AnsiConsole.MarkupLine("[grey]Valid llama.cpp repository already exists. Skipping clone.[/]"); + } + + private void CloneLlamaRepository() { - AnsiConsole.MarkupLine("[yellow]Update requested: Removing old repository...[/]"); + var escapedRoot = Markup.Escape(_llamaRoot); + AnsiConsole.MarkupLine($"Cloning llama.cpp to [blue]{escapedRoot}[/]..."); + AnsiConsole.MarkupLine("[grey](This includes submodules and may take a moment.)[/]"); + + Directory.CreateDirectory(Path.GetDirectoryName(_llamaRoot)!); + + var cloneOptions = new CloneOptions + { + RecurseSubmodules = true + }; + try { - // Recursive delete - Directory.Delete(_llamaRoot, true); + Repository.Clone(LlamaCppRepositoryUrl, _llamaRoot, cloneOptions); } - catch (Exception ex) + catch { - // Windows sometimes locks files; warn user but try to proceed or fail - AnsiConsole.MarkupLine($"[red]Warning: Could not delete old repo: {ex.Message}[/]"); - throw; + if (Directory.Exists(_llamaRoot) && !IsValidLlamaSourceTree()) + DeleteDirectoryOrThrow(_llamaRoot, "clone failed and left a partial checkout"); + + throw; } + + ValidateLlamaSourceTreeOrThrow(); } - if (!Directory.Exists(_llamaRoot)) + private bool IsValidLlamaSourceTree() { - AnsiConsole.MarkupLine($"Cloning llama.cpp to [blue]{_llamaRoot}[/]..."); - AnsiConsole.MarkupLine("[grey](This includes submodules and may take a moment)[/]"); - - // Clone with RecurseSubmodules = true matches "git submodule update --init --recursive" - var cloneOptions = new CloneOptions { RecurseSubmodules = true }; - Repository.Clone("https://github.com/ggerganov/llama.cpp.git", _llamaRoot, cloneOptions); + if (!Directory.Exists(_llamaRoot)) + return false; + + // CMakeLists.txt is the non-negotiable build root. The previous bug was + // caused by trusting Directory.Exists(_llamaRoot) even when this file was gone. + if (!File.Exists(Path.Combine(_llamaRoot, "CMakeLists.txt"))) + return false; + + // Prefer a real git checkout for auto-managed installs. If a user points at a + // custom source tree, that path is handled by InitializeLlamaCpp custom args. + if (!Directory.Exists(Path.Combine(_llamaRoot, ".git"))) + return false; + + return true; } - else + + private void ValidateLlamaSourceTreeOrThrow() { - AnsiConsole.MarkupLine("[grey]Repository already exists. Skipping clone.[/]"); + string cmakeLists = Path.Combine(_llamaRoot, "CMakeLists.txt"); + if (!File.Exists(cmakeLists)) + { + throw new DirectoryNotFoundException( + $"llama.cpp checkout is not buildable. Expected CMakeLists.txt at: {cmakeLists}. " + + "Delete the llama.cpp directory or rerun initialize-llama-cpp --update so MagicQuant can redeploy it."); + } } - // 3. Setup Build Directory - string buildDir = Path.Combine(_llamaRoot, "build"); - - // If we just re-cloned, this directory is gone anyway, but if we didn't, - // and forceRebuild is true (e.g. if deletion failed above but we continue), clean it. - if (forceRebuild && Directory.Exists(buildDir)) + private void EnsureBuildDirectory(string buildDir, bool forceRebuild) { - Directory.Delete(buildDir, true); + if (Directory.Exists(buildDir)) + { + if (forceRebuild) + { + DeleteDirectoryOrThrow(buildDir, "clean rebuild requested"); + } + else if (!BuildCacheMatchesCurrentSource(buildDir)) + { + AnsiConsole.MarkupLine("[yellow]Existing CMake build cache points at a different or invalid source tree. Recreating build directory...[/]"); + DeleteDirectoryOrThrow(buildDir, "CMake cache does not match the active llama.cpp source tree"); + } + } + + Directory.CreateDirectory(buildDir); } - Directory.CreateDirectory(buildDir); - // 4. Get the CMake Executable (System or Local) - string cmakeExe = DependencyManager.GetCmakePath() ?? "cmake"; + private bool BuildCacheMatchesCurrentSource(string buildDir) + { + string cacheFile = Path.Combine(buildDir, "CMakeCache.txt"); + if (!File.Exists(cacheFile)) + return true; - // 5. Generate Build Files - string cmakeArgs = GetOptimalCmakeArgs(); - AnsiConsole.MarkupLine($"[grey]Configuring build with: {cmakeArgs}[/]"); - - // Note: We run this inside the 'build' folder - if (!await RunProcessAsync(cmakeExe, cmakeArgs, buildDir)) - throw new Exception("CMake configuration failed."); + try + { + foreach (string line in File.ReadLines(cacheFile)) + { + if (!line.StartsWith("CMAKE_HOME_DIRECTORY:INTERNAL=", StringComparison.Ordinal)) + continue; - // 6. Compile - AnsiConsole.MarkupLine("[cyan]Compiling Llama.cpp (Release Mode)...[/]"); - - // -j triggers parallel build using all available cores - string buildCmd = "--build . --config Release -j " + Environment.ProcessorCount; - - if (!await RunProcessAsync(cmakeExe, buildCmd, buildDir)) - throw new Exception("Build failed."); + string cachedSource = line["CMAKE_HOME_DIRECTORY:INTERNAL=".Length..].Trim(); + if (string.IsNullOrWhiteSpace(cachedSource)) + return false; - AnsiConsole.MarkupLine("[green]✔ Build Success![/]"); -} + string normalizedCached = NormalizePath(cachedSource); + string normalizedCurrent = NormalizePath(_llamaRoot); + + return string.Equals(normalizedCached, normalizedCurrent, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? StringComparison.OrdinalIgnoreCase + : StringComparison.Ordinal); + } - private string GetOptimalCmakeArgs() + return true; + } + catch + { + return false; + } + } + + private List GetOptimalCmakeArgs() { - // Core Args - var args = new List { "..", "-DCMAKE_BUILD_TYPE=Release" }; + var args = new List + { + _llamaRoot, + "-DCMAKE_BUILD_TYPE=Release" + }; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - args.Add("-G Ninja"); + { + args.Add("-G"); + args.Add("Ninja"); + } - // GPU Optimization Logic switch (_sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor) { case GpuVendor.Nvidia: args.Add("-DGGML_CUDA=ON"); - // Native = Compiles specifically for the detected card (Perfect optimization) - args.Add("-DCMAKE_CUDA_ARCHITECTURES=native"); + args.Add("-DCMAKE_CUDA_ARCHITECTURES=native"); break; case GpuVendor.Amd: args.Add("-DGGML_HIPBLAS=ON"); - // If on Linux, you might add -DAMDGPU_TARGETS=gfx1100 etc if needed - // But usually standard HIP build is sufficient break; case GpuVendor.Intel: - // Try SYCL (OneAPI) first as it is fastest - // If OneAPI isn't present (checked in DependencyManager), - // you might fallback to Vulkan here: "-DGGML_VULKAN=ON" args.Add("-DGGML_SYCL=ON"); break; - - default: - // CPU Fallback (ensure AVX is on) - // CMake usually detects AVX2 automatically - break; } - return string.Join(" ", args); + return args; } - private async Task RunProcessAsync(string exe, string args, string workingDir) + private async Task RunProcessAsync(string exe, IReadOnlyList args, string workingDir) { var psi = new ProcessStartInfo { - FileName = exe, Arguments = args, WorkingDirectory = workingDir, - RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false + FileName = exe, + WorkingDirectory = workingDir, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false }; - + + foreach (string arg in args) + psi.ArgumentList.Add(arg); + using var p = Process.Start(psi); - if (p == null) return false; + if (p == null) + return false; + + p.OutputDataReceived += (_, e) => + { + if (e.Data != null) + AnsiConsole.WriteLine(e.Data); + }; + + p.ErrorDataReceived += (_, e) => + { + if (e.Data != null) + AnsiConsole.WriteLine(e.Data); + }; - // Capture output to show user progress - p.OutputDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.WriteLine(e.Data); }; - p.ErrorDataReceived += (s, e) => { if (e.Data != null) AnsiConsole.WriteLine(e.Data); }; - p.BeginOutputReadLine(); p.BeginErrorReadLine(); await p.WaitForExitAsync(); - + return p.ExitCode == 0; } -} \ No newline at end of file + + private static string ResolveConvertScriptPath(string llamaRoot) + { + // llama.cpp has kept this at repo root for the relevant toolchain. Keep a + // tiny candidate list so a future minor layout change does not poison Cache. + string[] candidates = + { + Path.Combine(llamaRoot, "convert_hf_to_gguf.py"), + Path.Combine(llamaRoot, "convert.py") + }; + + return candidates.FirstOrDefault(File.Exists) ?? candidates[0]; + } + + private static void DeleteDirectoryOrThrow(string path, string reason) + { + if (!Directory.Exists(path)) + return; + + try + { + MakeDirectoryWritable(path); + Directory.Delete(path, recursive: true); + } + catch (Exception ex) + { + throw new IOException( + $"Could not remove directory '{path}' while repairing llama.cpp ({reason}). " + + "Close any terminals/editors using that path or delete it manually, then rerun initialize-llama-cpp.", ex); + } + } + + private static void MakeDirectoryWritable(string root) + { + try + { + foreach (string file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories)) + { + var attributes = File.GetAttributes(file); + if ((attributes & FileAttributes.ReadOnly) != 0) + File.SetAttributes(file, attributes & ~FileAttributes.ReadOnly); + } + + foreach (string directory in Directory.EnumerateDirectories(root, "*", SearchOption.AllDirectories)) + { + var attributes = File.GetAttributes(directory); + if ((attributes & FileAttributes.ReadOnly) != 0) + File.SetAttributes(directory, attributes & ~FileAttributes.ReadOnly); + } + } + catch + { + // Best-effort only. Directory.Delete will throw a clearer failure if this mattered. + } + } + + private static string NormalizePath(string path) + { + return Path.GetFullPath(path) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } + + private static string FormatArgsForDisplay(IEnumerable args) + { + return string.Join(" ", args.Select(QuoteIfNeeded)); + } + + private static string QuoteIfNeeded(string arg) + { + if (string.IsNullOrEmpty(arg)) + return "\"\""; + + return arg.Any(char.IsWhiteSpace) + ? $"\"{arg.Replace("\"", "\\\"")}\"" + : arg; + } +} diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 2494904..346a383 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -33,7 +33,7 @@ args = [ "evolution", - "--architecture-family", @"""Qwen3.6-35B-A3B""" + "--architecture-family", @"""Qwen3.6-27B""" ,"--reuse-existing-final-artifacts" ]; } diff --git a/MagicQuant/Services/BaselineDefinitionResolver.cs b/MagicQuant/Services/BaselineDefinitionResolver.cs new file mode 100644 index 0000000..845371a --- /dev/null +++ b/MagicQuant/Services/BaselineDefinitionResolver.cs @@ -0,0 +1,111 @@ +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; + +namespace MagicQuant.Services; + +public static class BaselineDefinitionResolver +{ + public static string NormalizeRepoId(string value) => (value ?? string.Empty).Trim().ToLowerInvariant(); + + public static string NormalizeFileName(string value) => (value ?? string.Empty).Trim().Replace('\\', '/').ToLowerInvariant(); + + public static string NormalizeCanonicalKey(string value) => (value ?? string.Empty).Trim().ToLowerInvariant(); + + public static string BuildCustomCanonicalKey(string architectureFamilyName, string repoId, string fileName) => + $"custom:{NormalizeRepoId(architectureFamilyName)}:{NormalizeRepoId(repoId)}:{NormalizeFileName(fileName)}"; + + public static async Task ResolveRequiredDefinitionAsync( + MagicQuantContext db, + BaselineQuants baseline, + CancellationToken ct = default) + { + int? familyId = baseline.IsCustomBaseline + ? TensorGroupProfileService.RequireCurrentArchitectureFamilyId() + : null; + + string normalizedKey = NormalizeCanonicalKey(baseline.CanonicalKey); + + var definition = await db.BaselineQuantDefinitions + .AsNoTracking() + .FirstOrDefaultAsync(x => + x.ArchitectureFamilyId == familyId && + x.RuntimeBaselineId == baseline.UniqueId, + ct) + ?? await db.BaselineQuantDefinitions + .AsNoTracking() + .FirstOrDefaultAsync(x => + x.ArchitectureFamilyId == familyId && + x.NormalizedCanonicalKey == normalizedKey, + ct); + + if (definition == null) + { + throw new InvalidOperationException( + $"Baseline definition was not found in SQLite for runtime id {baseline.UniqueId} / key '{baseline.CanonicalKey}'. " + + "Run custom baseline precheck/sync before using learned or historical tensor-combo truth."); + } + + return definition; + } + + public static async Task TryResolveDefinitionByRuntimeIdAsync( + MagicQuantContext db, + byte runtimeBaselineId, + int? architectureFamilyId, + CancellationToken ct = default) + { + if (architectureFamilyId != null) + { + var custom = await db.BaselineQuantDefinitions + .AsNoTracking() + .FirstOrDefaultAsync(x => x.ArchitectureFamilyId == architectureFamilyId.Value && x.RuntimeBaselineId == runtimeBaselineId, ct); + + if (custom != null) + return custom; + } + + return await db.BaselineQuantDefinitions + .AsNoTracking() + .FirstOrDefaultAsync(x => x.ArchitectureFamilyId == null && x.RuntimeBaselineId == runtimeBaselineId, ct); + } + + public static BaselineQuants ToRuntimeBaseline(BaselineQuantDefinition definition, bool forceInactiveRegistration = false) + { + if (!definition.IsCustomBaseline) + return BaselineQuants.FromId(definition.RuntimeBaselineId); + + var scheme = TensorWeightScheme.FromId(definition.DefaultTensorSchemeId); + var baseline = BaselineQuants.CreateDynamicCustomBaseline( + uniqueId: definition.RuntimeBaselineId, + displayName: string.IsNullOrWhiteSpace(definition.DisplayName) ? definition.BaselineName : definition.DisplayName, + quantizeBaseArgumentName: definition.QuantizeBaseArgumentName, + sourceRepository: definition.SourceRepository ?? string.Empty, + sourceFileName: definition.SourceFileName ?? string.Empty, + shortSourceName: definition.ShortSourceName ?? "External", + sourceOwner: definition.SourceOwner ?? string.Empty, + sourceKind: definition.SourceKind, + canonicalKey: definition.CanonicalKey, + primaryTensorWeightScheme: scheme, + learnedMatchTensorWeightSchemes: [scheme], + bannedGroupIds: Array.Empty(), + requiresImatrix: definition.RequiresImatrix, + isLearningBaseline: definition.IsActiveInCurrentConfig && definition.IsLearningBaseline, + isCombinationCarrierCandidate: definition.IsActiveInCurrentConfig && definition.IsCombinationCarrierCandidate, + isExplicitGroupCombinationCandidate: definition.IsActiveInCurrentConfig && definition.IsExplicitGroupCombinationCandidate, + bitRange: definition.BitRange, + explicitCandidateSortOrder: definition.ExplicitCandidateSortOrder); + + if (definition.IsActiveInCurrentConfig || forceInactiveRegistration) + return baseline; + + return baseline with + { + IsLearningBaseline = false, + IsCombinationCarrierCandidate = false, + IsExplicitGroupCombinationCandidate = false + }; + } +} diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 950bb58..0dd69ff 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -542,10 +542,14 @@ private static BenchmarkSlot BuildAllGpuSlotFromSystemInfo() await using var db = new MagicQuantContext(); var aiModelHashId = await GetOrCreateAiModelHashIdAsync(db, ct); var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHashId, createIfMissing: false, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); var row = await db.ExecutionPlanProbeCaches .AsNoTracking() .FirstOrDefaultAsync(x => + x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && x.AiModelHashId == aiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.HardwareFingerprint == key.HardwareFingerprint && @@ -642,9 +646,13 @@ private async Task UpsertCachedExecutionPlanAsync( await using var db = new MagicQuantContext(); var aiModelHashId = await GetOrCreateAiModelHashIdAsync(db, ct); var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHashId, createIfMissing: true, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); var existing = await db.ExecutionPlanProbeCaches .FirstOrDefaultAsync(x => + x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && x.AiModelHashId == aiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.HardwareFingerprint == key.HardwareFingerprint && @@ -659,6 +667,8 @@ private async Task UpsertCachedExecutionPlanAsync( { existing = new ExecutionPlanProbeCache { + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, AiModelHashId = aiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, HardwareFingerprint = key.HardwareFingerprint, @@ -1154,7 +1164,7 @@ public async Task RunAllBenchmarksAsync( var existingBench = await db.AiBenchmarks .Include(x => x.CategorBenchmarks) .AsNoTracking() - .FirstOrDefaultAsync(b => b.AiModelHashId == aiModelHash.Id && b.ImatrixDefinitionId == identity.ImatrixDefinitionId && b.TensorComboId == tensorCombo.Id); + .FirstOrDefaultAsync(b => b.ArchitectureFamilyId == TensorGroupProfileService.RequireCurrentArchitectureFamilyId() && b.TensorGroupProfileId == TensorGroupProfileService.RequireCurrentProfileId() && b.AiModelHashId == aiModelHash.Id && b.ImatrixDefinitionId == identity.ImatrixDefinitionId && b.TensorComboId == tensorCombo.Id); // 1. DB truth first if (existingBench != null && HasRequiredCategories(existingBench, requestedDomains, requireKld)) @@ -1216,12 +1226,14 @@ await SaveBenchmarkToDbAsync( var trackedBench = await db.AiBenchmarks .Include(x => x.CategorBenchmarks) - .FirstOrDefaultAsync(x => x.AiModelHashId == aiModelHash.Id && x.ImatrixDefinitionId == identity.ImatrixDefinitionId && x.TensorComboId == tensorCombo.Id); + .FirstOrDefaultAsync(x => x.ArchitectureFamilyId == TensorGroupProfileService.RequireCurrentArchitectureFamilyId() && x.TensorGroupProfileId == TensorGroupProfileService.RequireCurrentProfileId() && x.AiModelHashId == aiModelHash.Id && x.ImatrixDefinitionId == identity.ImatrixDefinitionId && x.TensorComboId == tensorCombo.Id); if (trackedBench == null) { trackedBench = new AiBenchmark { + ArchitectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(), + TensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(), AiModelHashId = aiModelHash.Id, ImatrixDefinitionId = identity.ImatrixDefinitionId, TensorComboId = tensorCombo.Id, @@ -1556,9 +1568,14 @@ private async Task SaveBenchmarkToDbAsync( ? res.ModelSizeBytes!.Value : (File.Exists(modelPath) ? (ulong)new FileInfo(modelPath).Length : 0UL); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var bench = await db.AiBenchmarks .Include(x => x.CategorBenchmarks) .FirstOrDefaultAsync(x => + x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && x.AiModelHashId == model.Id && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id); @@ -1567,6 +1584,8 @@ private async Task SaveBenchmarkToDbAsync( { bench = new AiBenchmark { + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, AiModelHashId = model.Id, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = combo.Id @@ -1652,6 +1671,8 @@ private async Task SaveBenchmarkToDbAsync( db.BenchmarkRuns.Add(new BenchmarkRun { Id = Guid.NewGuid(), + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, AiModelHashId = model.Id, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = combo.Id, @@ -1669,6 +1690,8 @@ private async Task SaveBenchmarkToDbAsync( await db.SaveChangesAsync(); } + await ReplaceBenchmarkLearnedSourcesAsync(db, bench, combo, architectureFamilyId, tensorGroupProfileId); + await transaction.CommitAsync(); } catch (Exception ex) @@ -1690,6 +1713,70 @@ private async Task SaveBenchmarkToDbAsync( } } + + private static async Task ReplaceBenchmarkLearnedSourcesAsync( + MagicQuantContext db, + AiBenchmark bench, + TensorCombo combo, + int architectureFamilyId, + int tensorGroupProfileId) + { + await db.AiBenchmarkLearnedSources + .Where(x => x.AiBenchmarkId == bench.Id) + .ExecuteDeleteAsync(); + + var groupSlots = new (byte GroupId, byte StoredValue)[] + { + (TReg.Embeddings.UniqueId, combo.Embeddings), + (TReg.LmHead.UniqueId, combo.LmHead), + (TReg.AttnQ.UniqueId, combo.AttnQ), + (TReg.AttnKV.UniqueId, combo.AttnKV), + (TReg.AttnOutput.UniqueId, combo.AttnOutput), + (TReg.FfnUpGate.UniqueId, combo.FfnUpGate), + (TReg.FfnDown.UniqueId, combo.FfnDown), + (TReg.MoeExperts.UniqueId, combo.MoeExperts), + (TReg.MoeRouter.UniqueId, combo.MoeRouter) + }; + + foreach (var (groupId, storedValue) in groupSlots) + { + if (storedValue == 0) + continue; + + var runtimeBaselineId = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedValue); + if (runtimeBaselineId == BaselineQuants.BF16_Hybrid.UniqueId || + runtimeBaselineId == BaselineQuants.F16_Hybrid.UniqueId || + runtimeBaselineId == BaselineQuants.NativeSourceUniqueId) + continue; + + var definition = await db.BaselineQuantDefinitions + .AsNoTracking() + .Where(x => (x.ArchitectureFamilyId == architectureFamilyId || x.ArchitectureFamilyId == null) && + x.RuntimeBaselineId == runtimeBaselineId) + .OrderByDescending(x => x.ArchitectureFamilyId.HasValue) + .FirstOrDefaultAsync(); + + if (definition == null) + continue; + + db.AiBenchmarkLearnedSources.Add(new AiBenchmarkLearnedSource + { + Id = Guid.NewGuid(), + AiBenchmarkId = bench.Id, + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, + TensorComboId = combo.Id, + TensorGroupId = groupId, + BaselineQuantDefinitionId = definition.Id, + SourceLearningBenchmarkId = null, + BaselineCanonicalKey = definition.CanonicalKey, + CreatedUtc = DateTime.UtcNow + }); + } + + await db.SaveChangesAsync(); + } + private static byte DomainToCategory(string domain) { return domain.Trim().ToLowerInvariant() switch @@ -1715,6 +1802,8 @@ private async Task PersistFailedBenchmarkRunAsync( db.BenchmarkRuns.Add(new BenchmarkRun { Id = Guid.NewGuid(), + ArchitectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(), + TensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(), AiModelHashId = aiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = tensorComboId, diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index fd53759..3505989 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -5,6 +5,7 @@ using MagicQuant.Helpers; using MQ.DB; using MQ.DB.Models; +using MQ.DB.Models.DbModels; using Spectre.Console; namespace MagicQuant.Services; @@ -18,192 +19,273 @@ public HuggingFaceBaselineService(PythonManager python) _python = python ?? throw new ArgumentNullException(nameof(python)); } - public async Task> PrecheckAndRegisterConfiguredBaselinesAsync(CancellationToken ct = default) + +public async Task> PrecheckAndRegisterConfiguredBaselinesAsync(CancellationToken ct = default) +{ + await EnsureHubSupportAsync(); + + int architectureFamilyId = Cache.CurrentArchitectureFamilyId + ?? throw new InvalidOperationException("Custom baseline sync requires the architecture family to be resolved first."); + + var enabledRepos = Config.Current.Baselines.CustomRepositories.Where(x => x.Enabled).ToList(); + var resolved = new List(); + BaselineQuants.ResetDynamicCustomBaselines(); + + AnsiConsole.Write(new Rule("[yellow]Custom Baseline DB Sync[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[grey]Enabled custom repositories:[/] [cyan]{enabledRepos.Count:N0}[/]"); + + await using var db = new MagicQuantContext(); + var existingDefinitions = await db.BaselineQuantDefinitions + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && x.IsCustomBaseline) + .ToListAsync(ct); + + foreach (var definition in existingDefinitions) { - await EnsureHubSupportAsync(); + definition.IsActiveInCurrentConfig = false; + definition.LastUpdatedUtc = DateTime.UtcNow; + } - var enabledRepos = Config.Current.Baselines.CustomRepositories.Where(x => x.Enabled).ToList(); - var resolved = new List(); - BaselineQuants.ResetDynamicCustomBaselines(); + RegisterHistoricalDefinitions(existingDefinitions); - AnsiConsole.Write(new Rule("[yellow]Custom Baseline Precheck[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"[grey]Enabled custom repositories:[/] [cyan]{enabledRepos.Count:N0}[/]"); + var existingDynamicIdsByCanonicalKey = existingDefinitions + .Where(x => !string.IsNullOrWhiteSpace(x.NormalizedCanonicalKey)) + .GroupBy(x => x.NormalizedCanonicalKey, StringComparer.Ordinal) + .ToDictionary(g => g.Key, g => g.OrderBy(x => x.RuntimeBaselineId).First().RuntimeBaselineId, StringComparer.Ordinal); - if (enabledRepos.Count == 0) - { - AnsiConsole.MarkupLine("[grey]No enabled custom repositories were configured for this run.[/]"); - Config.SetResolvedCustomBaselines(Array.Empty()); - BaselineQuants.ValidateIntegrityOrThrow(); - return resolved; - } + var reservedIds = BaselineQuants.GetAllRecognizedBaselines().Select(x => x.UniqueId).ToHashSet(); + foreach (var persistedId in existingDefinitions.Select(x => x.RuntimeBaselineId)) + reservedIds.Add(persistedId); - var existingDynamicIdsByCanonicalKey = LoadExistingDynamicBaselineIds(); - var reservedIds = BaselineQuants.GetAllRecognizedBaselines() - .Select(x => x.UniqueId) - .ToHashSet(); + byte nextId = BaselineQuants.GetFirstAvailableDynamicBaselineId(); + var now = DateTime.UtcNow; - foreach (var persistedId in existingDynamicIdsByCanonicalKey.Values) - reservedIds.Add(persistedId); + foreach (var repo in enabledRepos) + { + if (string.IsNullOrWhiteSpace(repo.RepoId)) + throw new InvalidOperationException("Custom baseline repository entry is missing repo_id."); - byte nextId = BaselineQuants.GetFirstAvailableDynamicBaselineId(); + if (repo.Includes.Count == 0) + throw new InvalidOperationException($"Custom baseline repository '{repo.RepoId}' is enabled but has zero include entries."); - foreach (var repo in enabledRepos) - { - if (string.IsNullOrWhiteSpace(repo.RepoId)) - throw new InvalidOperationException("Custom baseline repository entry is missing repo_id."); + AnsiConsole.MarkupLine($"[cyan]Repo:[/] {Markup.Escape(repo.RepoId)} [grey](includes={repo.Includes.Count})[/]"); - if (repo.Includes.Count == 0) - throw new InvalidOperationException($"Custom baseline repository '{repo.RepoId}' is enabled but has zero include entries."); + var repoFiles = await ListRepoFilesAsync(repo.RepoId, ct); + if (repoFiles.Count == 0) + throw new InvalidOperationException($"No files were returned from Hugging Face repo '{repo.RepoId}'."); - AnsiConsole.MarkupLine($"[cyan]Repo:[/] {Markup.Escape(repo.RepoId)} [grey](includes={repo.Includes.Count})[/]"); + var ggufRepoFiles = repoFiles.Where(x => x.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)).ToList(); + AnsiConsole.MarkupLine($" [grey]GGUF files discovered:[/] [cyan]{ggufRepoFiles.Count:N0}[/]"); - var repoFiles = await ListRepoFilesAsync(repo.RepoId, ct); - if (repoFiles.Count == 0) - throw new InvalidOperationException($"No files were returned from Hugging Face repo '{repo.RepoId}'."); + string shortSourceName = string.IsNullOrWhiteSpace(repo.ShortSourceName) + ? DeriveShortSourceName(repo.RepoId) + : repo.ShortSourceName!.Trim(); - var ggufRepoFiles = repoFiles.Where(x => x.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)).ToList(); - AnsiConsole.MarkupLine($" [grey]GGUF files discovered:[/] [cyan]{ggufRepoFiles.Count:N0}[/]"); + foreach (var include in repo.Includes) + { + if (string.IsNullOrWhiteSpace(include.BaselineFamily)) + throw new InvalidOperationException($"Repo '{repo.RepoId}' has an include entry missing baseline_family."); + + var standardFamily = BaselineQuants.ResolveBuiltInStandardBaseline(include.BaselineFamily) + ?? throw new InvalidOperationException( + $"Custom baseline include '{include.BaselineFamily}' in repo '{repo.RepoId}' could not be matched to a built-in baseline family."); + + string resolvedFileName = ResolveRepoFileName(repoFiles, include, standardFamily); + string normalizedRepo = BaselineDefinitionResolver.NormalizeRepoId(repo.RepoId); + string normalizedFile = BaselineDefinitionResolver.NormalizeFileName(resolvedFileName); + string canonicalKey = BuildCanonicalKey(Cache.CurrentArchitectureFamilyName, repo.RepoId, resolvedFileName); + string normalizedCanonicalKey = BaselineDefinitionResolver.NormalizeCanonicalKey(canonicalKey); + + string displayName = string.IsNullOrWhiteSpace(include.DisplayName) + ? $"{shortSourceName}-{standardFamily.Names[0]}" + : include.DisplayName!.Trim(); + + bool requiresImatrix = include.RequiresImatrix ?? standardFamily.RequiresImatrix; + bool allowAsLearning = include.AllowAsLearningBaseline ?? repo.AllowAsLearningBaseline; + bool allowAsCarrier = include.AllowAsCombinationCarrier ?? repo.AllowAsCombinationCarrier; + bool allowAsExplicit = include.AllowAsExplicitGroupCandidate ?? repo.AllowAsExplicitGroupCandidate; + string quantizeBaseName = string.IsNullOrWhiteSpace(include.QuantizeBaseName) + ? standardFamily.Names[0] + : include.QuantizeBaseName!.Trim(); + + var bannedGroups = include.BannedGroupIds.Count > 0 + ? include.BannedGroupIds.ToArray() + : standardFamily.BannedGroupIds.ToArray(); + + var definition = existingDefinitions.FirstOrDefault(x => + string.Equals(x.NormalizedSourceRepository, normalizedRepo, StringComparison.Ordinal) && + string.Equals(x.NormalizedSourceFileName, normalizedFile, StringComparison.Ordinal)); + + if (definition != null && !string.Equals(definition.BaselineFamily, standardFamily.Names[0], StringComparison.Ordinal) && !include.ForceRelearn) + { + throw new InvalidOperationException( + $"Custom baseline semantic family changed for {repo.RepoId}/{resolvedFileName}: " + + $"DB has '{definition.BaselineFamily}', YAML now says '{standardFamily.Names[0]}'. " + + "This is destructive. Set this include's force_relearn: true so MagicQuant can plan and confirm targeted invalidation before resyncing the definition."); + } - string shortSourceName = string.IsNullOrWhiteSpace(repo.ShortSourceName) - ? DeriveShortSourceName(repo.RepoId) - : repo.ShortSourceName!.Trim(); + byte dynamicBaselineId = definition?.RuntimeBaselineId ?? ResolveDynamicBaselineId( + normalizedCanonicalKey, + existingDynamicIdsByCanonicalKey, + reservedIds, + ref nextId); - foreach (var include in repo.Includes) + var nextDefinition = new BaselineQuantDefinition + { + ArchitectureFamilyId = architectureFamilyId, + RuntimeBaselineId = dynamicBaselineId, + CanonicalKey = canonicalKey, + NormalizedCanonicalKey = normalizedCanonicalKey, + BaselineName = displayName, + DisplayName = displayName, + QuantizeBaseArgumentName = quantizeBaseName, + DefaultTensorSchemeId = standardFamily.PrimaryTensorWeightScheme.UniqueId, + DefaultTensorSchemeName = standardFamily.PrimaryTensorWeightScheme.Names[0], + SourceKind = repo.SourceKind, + SourceOwner = DeriveSourceOwner(repo.RepoId), + SourceRepository = repo.RepoId, + NormalizedSourceRepository = normalizedRepo, + SourceFileName = resolvedFileName, + NormalizedSourceFileName = normalizedFile, + ShortSourceName = shortSourceName, + BaselineFamily = standardFamily.Names[0], + IsCustomBaseline = true, + IsLearningBaseline = allowAsLearning, + IsCombinationCarrierCandidate = allowAsCarrier, + IsExplicitGroupCombinationCandidate = allowAsExplicit, + RequiresImatrix = requiresImatrix, + BitRange = standardFamily.BitRange, + ExplicitCandidateSortOrder = standardFamily.ExplicitCandidateSortOrder, + IsActiveInCurrentConfig = true, + FirstSeenUtc = definition?.FirstSeenUtc ?? now, + LastSeenUtc = now, + LastUpdatedUtc = now + }; + + if (definition == null) { - if (string.IsNullOrWhiteSpace(include.BaselineFamily)) - throw new InvalidOperationException($"Repo '{repo.RepoId}' has an include entry missing baseline_family."); - - var standardFamily = BaselineQuants.ResolveBuiltInStandardBaseline(include.BaselineFamily) - ?? throw new InvalidOperationException( - $"Custom baseline include '{include.BaselineFamily}' in repo '{repo.RepoId}' could not be matched to a built-in baseline family."); - - string resolvedFileName = ResolveRepoFileName(repoFiles, include, standardFamily); - string displayName = string.IsNullOrWhiteSpace(include.DisplayName) - ? $"{shortSourceName}-{standardFamily.Names[0]}" - : include.DisplayName!.Trim(); - - string canonicalKey = BuildCanonicalKey(repo.RepoId, resolvedFileName, standardFamily.Names[0]); - bool requiresImatrix = include.RequiresImatrix ?? standardFamily.RequiresImatrix; - bool allowAsLearning = include.AllowAsLearningBaseline ?? repo.AllowAsLearningBaseline; - bool allowAsCarrier = include.AllowAsCombinationCarrier ?? repo.AllowAsCombinationCarrier; - bool allowAsExplicit = include.AllowAsExplicitGroupCandidate ?? repo.AllowAsExplicitGroupCandidate; - string quantizeBaseName = string.IsNullOrWhiteSpace(include.QuantizeBaseName) - ? standardFamily.Names[0] - : include.QuantizeBaseName!.Trim(); - - var bannedGroups = include.BannedGroupIds.Count > 0 - ? include.BannedGroupIds.ToArray() - : standardFamily.BannedGroupIds.ToArray(); - - byte dynamicBaselineId = ResolveDynamicBaselineId( - canonicalKey, - existingDynamicIdsByCanonicalKey, - reservedIds, - ref nextId); - - var dynamicBaseline = BaselineQuants.CreateDynamicCustomBaseline( - uniqueId: dynamicBaselineId, - displayName: displayName, - quantizeBaseArgumentName: quantizeBaseName, - sourceRepository: repo.RepoId, - sourceFileName: resolvedFileName, - shortSourceName: shortSourceName, - sourceOwner: DeriveSourceOwner(repo.RepoId), - sourceKind: "huggingface_repo", - canonicalKey: canonicalKey, - primaryTensorWeightScheme: standardFamily.PrimaryTensorWeightScheme, - learnedMatchTensorWeightSchemes: standardFamily.LearnedMatchTensorWeightSchemes, - bannedGroupIds: bannedGroups, - requiresImatrix: requiresImatrix, - isLearningBaseline: allowAsLearning, - isCombinationCarrierCandidate: allowAsCarrier, - isExplicitGroupCombinationCandidate: allowAsExplicit, - bitRange: standardFamily.BitRange, - explicitCandidateSortOrder: standardFamily.ExplicitCandidateSortOrder); - - BaselineQuants.RegisterDynamicCustomBaseline(dynamicBaseline); - - var spec = new ResolvedCustomBaselineSpec - { - DynamicBaselineId = dynamicBaseline.UniqueId, - CanonicalKey = dynamicBaseline.CanonicalKey, - DisplayName = dynamicBaseline.Names[0], - RepoId = repo.RepoId, - SourceOwner = dynamicBaseline.SourceOwner ?? string.Empty, - SourceFileName = dynamicBaseline.SourceFileName ?? string.Empty, - ShortSourceName = dynamicBaseline.ShortSourceName ?? shortSourceName, - BaselineFamily = standardFamily.Names[0], - QuantizeBaseName = dynamicBaseline.QuantizeBaseArgumentName, - RequiresImatrix = dynamicBaseline.RequiresImatrix, - AllowAsLearningBaseline = dynamicBaseline.IsLearningBaseline, - AllowAsCombinationCarrier = dynamicBaseline.IsCombinationCarrierCandidate, - AllowAsExplicitGroupCandidate = dynamicBaseline.IsExplicitGroupCombinationCandidate, - BannedGroupIds = dynamicBaseline.BannedGroupIds - }; - - resolved.Add(spec); - AnsiConsole.MarkupLine( - $" [green]Resolved:[/] id=[cyan]{dynamicBaseline.UniqueId}[/] family=[yellow]{Markup.Escape(standardFamily.Names[0])}[/] file=[blue]{Markup.Escape(resolvedFileName)}[/] learning={allowAsLearning} carrier={allowAsCarrier} explicit={allowAsExplicit}"); + definition = nextDefinition; + db.BaselineQuantDefinitions.Add(definition); + existingDefinitions.Add(definition); + } + else + { + MagicQuantContext.ApplyBaselineDefinitionUpdate(definition, nextDefinition, preserveFirstSeen: true); } - } - if (enabledRepos.Count > 0 && resolved.Count == 0) - throw new InvalidOperationException("Custom baseline repositories were enabled, but zero custom baselines resolved into the runtime registry. Check YAML property names and include entries."); + var dynamicBaseline = BaselineQuants.CreateDynamicCustomBaseline( + uniqueId: dynamicBaselineId, + displayName: displayName, + quantizeBaseArgumentName: quantizeBaseName, + sourceRepository: repo.RepoId, + sourceFileName: resolvedFileName, + shortSourceName: shortSourceName, + sourceOwner: DeriveSourceOwner(repo.RepoId), + sourceKind: repo.SourceKind, + canonicalKey: canonicalKey, + primaryTensorWeightScheme: standardFamily.PrimaryTensorWeightScheme, + learnedMatchTensorWeightSchemes: standardFamily.LearnedMatchTensorWeightSchemes, + bannedGroupIds: bannedGroups, + requiresImatrix: requiresImatrix, + isLearningBaseline: allowAsLearning, + isCombinationCarrierCandidate: allowAsCarrier, + isExplicitGroupCombinationCandidate: allowAsExplicit, + bitRange: standardFamily.BitRange, + explicitCandidateSortOrder: standardFamily.ExplicitCandidateSortOrder); + + BaselineQuants.RegisterDynamicCustomBaseline(dynamicBaseline); + + var spec = new ResolvedCustomBaselineSpec + { + DynamicBaselineId = dynamicBaseline.UniqueId, + BaselineQuantDefinitionId = definition.Id == 0 ? null : definition.Id, + CanonicalKey = dynamicBaseline.CanonicalKey, + DisplayName = dynamicBaseline.Names[0], + RepoId = repo.RepoId, + SourceOwner = dynamicBaseline.SourceOwner ?? string.Empty, + SourceFileName = dynamicBaseline.SourceFileName ?? string.Empty, + ShortSourceName = dynamicBaseline.ShortSourceName ?? shortSourceName, + BaselineFamily = standardFamily.Names[0], + QuantizeBaseName = dynamicBaseline.QuantizeBaseArgumentName, + RequiresImatrix = dynamicBaseline.RequiresImatrix, + AllowAsLearningBaseline = dynamicBaseline.IsLearningBaseline, + AllowAsCombinationCarrier = dynamicBaseline.IsCombinationCarrierCandidate, + AllowAsExplicitGroupCandidate = dynamicBaseline.IsExplicitGroupCombinationCandidate, + ForceRelearn = include.ForceRelearn, + IsActiveInCurrentConfig = true, + BannedGroupIds = dynamicBaseline.BannedGroupIds + }; + + resolved.Add(spec); + AnsiConsole.MarkupLine( + $" [green]Resolved:[/] id=[cyan]{dynamicBaseline.UniqueId}[/] family=[yellow]{Markup.Escape(standardFamily.Names[0])}[/] file=[blue]{Markup.Escape(resolvedFileName)}[/] learning={allowAsLearning} carrier={allowAsCarrier} explicit={allowAsExplicit} relearn={include.ForceRelearn}"); + } + } - Config.SetResolvedCustomBaselines(resolved); - BaselineQuants.ValidateIntegrityOrThrow(); + await db.SaveChangesAsync(ct); - AnsiConsole.MarkupLine($"[green]Custom baseline precheck complete:[/] [cyan]{resolved.Count:N0}[/] resolved custom baseline(s)."); - return resolved; + foreach (var spec in resolved.Where(x => x.BaselineQuantDefinitionId == null)) + { + var definition = await db.BaselineQuantDefinitions.AsNoTracking().FirstAsync(x => + x.ArchitectureFamilyId == architectureFamilyId && + x.RuntimeBaselineId == spec.DynamicBaselineId, ct); + spec.BaselineQuantDefinitionId = definition.Id; } - private static Dictionary LoadExistingDynamicBaselineIds() + RegisterHistoricalDefinitions(existingDefinitions.Where(x => !x.IsActiveInCurrentConfig)); + + Config.SetResolvedCustomBaselines(resolved); + BaselineQuants.ValidateIntegrityOrThrow(); + + AnsiConsole.MarkupLine($"[green]Custom baseline sync complete:[/] [cyan]{resolved.Count:N0}[/] active custom baseline(s); [cyan]{existingDefinitions.Count(x => !x.IsActiveInCurrentConfig):N0}[/] inactive historical definition(s) retained."); + return resolved; +} + +private static void RegisterHistoricalDefinitions(IEnumerable definitions) +{ + foreach (var definition in definitions.Where(x => x.IsCustomBaseline)) { try { - using var db = new MagicQuantContext(); - - return db.BaselineQuantDefinitions - .AsNoTracking() - .Where(x => x.IsCustomBaseline && !string.IsNullOrWhiteSpace(x.CanonicalKey)) - .OrderBy(x => x.BaselineQuantId) - .ToDictionary(x => x.CanonicalKey, x => x.BaselineQuantId, StringComparer.Ordinal); + var runtime = BaselineDefinitionResolver.ToRuntimeBaseline(definition, forceInactiveRegistration: true); + BaselineQuants.RegisterDynamicCustomBaseline(runtime); } catch { - return new Dictionary(StringComparer.Ordinal); + // A bad historical row should not prevent active YAML from being resolved. + // It simply will not be available for runtime TensorConfig hydration until fixed. } } +} - private static byte ResolveDynamicBaselineId( - string canonicalKey, - IReadOnlyDictionary existingDynamicIdsByCanonicalKey, - HashSet reservedIds, - ref byte nextId) +private static byte ResolveDynamicBaselineId( + string normalizedCanonicalKey, + IReadOnlyDictionary existingDynamicIdsByCanonicalKey, + HashSet reservedIds, + ref byte nextId) +{ + if (!string.IsNullOrWhiteSpace(normalizedCanonicalKey) && + existingDynamicIdsByCanonicalKey.TryGetValue(normalizedCanonicalKey, out var existingId)) { - if (!string.IsNullOrWhiteSpace(canonicalKey) && - existingDynamicIdsByCanonicalKey.TryGetValue(canonicalKey, out var existingId)) - { - reservedIds.Add(existingId); - return existingId; - } + reservedIds.Add(existingId); + return existingId; + } - while (reservedIds.Contains(nextId)) - { - if (nextId >= 199) - throw new InvalidOperationException("No free dynamic baseline ids remain in the configured range."); + while (reservedIds.Contains(nextId)) + { + if (nextId >= 199) + throw new InvalidOperationException("No free dynamic baseline ids remain in the configured range."); - nextId++; - } + nextId++; + } - var allocated = nextId; - reservedIds.Add(allocated); + var allocated = nextId; + reservedIds.Add(allocated); - if (nextId < 199) - nextId++; + if (nextId < 199) + nextId++; - return allocated; - } + return allocated; +} public async Task DownloadBaselineAsync(BaselineQuants baseline, string destinationPath, bool forceRedownload = false, CancellationToken ct = default) { @@ -511,8 +593,8 @@ private static string ResolveRepoFileName(IReadOnlyList repoFiles, Custo return matches[0]; } - private static string BuildCanonicalKey(string repoId, string fileName, string family) - => $"hf:{repoId.Trim().ToLowerInvariant()}::{fileName.Trim().ToLowerInvariant()}::{family.Trim().ToLowerInvariant()}"; + private static string BuildCanonicalKey(string architectureFamilyName, string repoId, string fileName) + => BaselineDefinitionResolver.BuildCustomCanonicalKey(architectureFamilyName, repoId, fileName); private static string DeriveShortSourceName(string repoId) { @@ -551,4 +633,4 @@ private static void TryDelete(string path) { } } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/HybridBenchmarkRepository.cs b/MagicQuant/Services/HybridBenchmarkRepository.cs index 6f2738e..d625222 100644 --- a/MagicQuant/Services/HybridBenchmarkRepository.cs +++ b/MagicQuant/Services/HybridBenchmarkRepository.cs @@ -36,11 +36,15 @@ public async Task> LoadBenchmarkSnap return null; int? activeImatrixId = await ResolveActiveImatrixIdAsync(db, scopedAiModelHashId.Value, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); var query = db.AiBenchmarks .AsNoTracking() .Include(x => x.TensorCombo) .Include(x => x.CategorBenchmarks) + .Where(x => x.ArchitectureFamilyId == architectureFamilyId) + .Where(x => x.TensorGroupProfileId == tensorGroupProfileId) .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) .Where(x => x.TensorCombo.BaseQuant == config.BaseQuant) .Where(x => x.TensorCombo.Embeddings == config.Embeddings) @@ -165,9 +169,13 @@ public async Task> LoadBaseOnlyCarrierSnapshotsAsy return null; int? activeImatrixId = await ResolveActiveImatrixIdAsync(db, scopedAiModelHashId.Value, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); return await db.QuantizationRuns .AsNoTracking() + .Where(x => x.ArchitectureFamilyId == architectureFamilyId) + .Where(x => x.TensorGroupProfileId == tensorGroupProfileId) .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) .Where(x => x.ImatrixDefinitionId == activeImatrixId) .Where(x => x.TensorComboId == tensorComboId.Value) @@ -185,14 +193,25 @@ public async Task> LoadLearnedTensorMappingsAsync( CancellationToken ct = default) { await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); - if (scopedAiModelHashId == null) + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var normalizedCanonicalKey = BaselineDefinitionResolver.NormalizeCanonicalKey(canonicalBaselineKey); + var baselineDefinitionId = await db.BaselineQuantDefinitions + .AsNoTracking() + .Where(x => (x.ArchitectureFamilyId == architectureFamilyId || x.ArchitectureFamilyId == null) && + x.NormalizedCanonicalKey == normalizedCanonicalKey) + .OrderByDescending(x => x.ArchitectureFamilyId.HasValue) + .Select(x => (int?)x.Id) + .FirstOrDefaultAsync(ct); + + if (!baselineDefinitionId.HasValue) return new Dictionary(StringComparer.Ordinal); var query = db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) - .Where(x => x.BaselineCanonicalKey == canonicalBaselineKey); + .Where(x => x.ArchitectureFamilyId == architectureFamilyId) + .Where(x => x.TensorGroupProfileId == tensorGroupProfileId) + .Where(x => x.BaselineQuantDefinitionId == baselineDefinitionId.Value); if (groupId != null) query = query.Where(x => x.TensorGroupId == groupId.Value); @@ -248,11 +267,15 @@ public async Task> LoadAllBenchmarkSnapshotsForCur return new List(); int? activeImatrixId = await ResolveActiveImatrixIdAsync(db, scopedAiModelHashId.Value, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); var query = db.AiBenchmarks .AsNoTracking() .Include(x => x.TensorCombo) .Include(x => x.CategorBenchmarks) + .Where(x => x.ArchitectureFamilyId == architectureFamilyId) + .Where(x => x.TensorGroupProfileId == tensorGroupProfileId) .Where(x => x.AiModelHashId == scopedAiModelHashId.Value); if (strictImatrixContext) diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index b14a464..c5bf8c4 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -466,16 +466,41 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s if (!File.Exists(baseModelPath)) throw new InvalidOperationException($"Base model GGUF is required before dataset-based imatrix build. Missing: {baseModelPath}"); + /*var psi = new System.Diagnostics.ProcessStartInfo + { + FileName = imatrixBin, + Arguments = + $"--no-mmap " + + $"-m \"{baseModelPath}\" " + + $"-f \"{datasetPath}\" " + + $"-o \"{datPath}\" ", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false + };*/ + var psi = new System.Diagnostics.ProcessStartInfo { FileName = imatrixBin, - Arguments = $"--no-mmap -m \"{baseModelPath}\" -f \"{datasetPath}\" -o \"{datPath}\"", + Arguments = + $"--no-mmap " + + //$"-ngl 45 " + + //$"--tensor-split 19,22 " + + $"-m \"{baseModelPath}\" " + + $"-f \"{datasetPath}\" " + + $"-o \"{datPath}\" " + + // $"-b 128 " + + // $"-ub 64 " + + $"-fa off", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false }; + psi.Environment["GGML_CUDA_DISABLE_GRAPHS"] = "1"; + string launchedCommand = $"\"{imatrixBin}\" {psi.Arguments}"; + AnsiConsole.MarkupLine($"[grey]Imatrix: launching command:[/] [cyan]{Markup.Escape(launchedCommand)}[/]"); using var p = System.Diagnostics.Process.Start(psi) diff --git a/MagicQuant/Services/IsolationDiagnosticsManifestService.cs b/MagicQuant/Services/IsolationDiagnosticsManifestService.cs index c82871f..4141e15 100644 --- a/MagicQuant/Services/IsolationDiagnosticsManifestService.cs +++ b/MagicQuant/Services/IsolationDiagnosticsManifestService.cs @@ -155,6 +155,9 @@ private static async Task> BuildIsolationSamplePayloadAsync( TensorConfig lookup, CancellationToken ct) { + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var row = await db.AiBenchmarks .AsNoTracking() .Include(x => x.CategorBenchmarks) @@ -163,6 +166,8 @@ private static async Task> BuildIsolationSamplePayloadAsync( c => c.Id, (b, c) => new { b, c }) .FirstOrDefaultAsync(x => + x.b.ArchitectureFamilyId == architectureFamilyId && + x.b.TensorGroupProfileId == tensorGroupProfileId && x.b.AiModelHashId == aiModelHashId && x.b.ImatrixDefinitionId == imatrixDefinitionId && x.c.BaseQuant == lookup.BaseQuant && diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index afc910e..f4bd6d7 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -812,6 +812,8 @@ private static bool ShouldEliminateAsBadTrade(GroupCandidateEvaluation anchor, G return null; var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, createIfMissing: false, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); var lookup = (TensorConfig)quant; var row = await db.AiBenchmarks @@ -821,6 +823,8 @@ private static bool ShouldEliminateAsBadTrade(GroupCandidateEvaluation anchor, G c => c.Id, (b, c) => new { b, c }) .FirstOrDefaultAsync(x => + x.b.ArchitectureFamilyId == architectureFamilyId && + x.b.TensorGroupProfileId == tensorGroupProfileId && x.b.AiModelHashId == exactAiModelHashId.Value && x.b.ImatrixDefinitionId == imatrixDefinitionId && x.c.BaseQuant == lookup.BaseQuant && @@ -1198,4 +1202,4 @@ private sealed class CategorySnapshot public double Ppl { get; set; } public double PplError { get; set; } } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 021f416..6664077 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -542,6 +542,8 @@ private async Task BulkAppendAsync( HybridQuant quant, CancellationToken ct) { + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); var lookup = (TensorConfig)quant; var row = await db.AiBenchmarks @@ -550,6 +552,8 @@ private async Task BulkAppendAsync( c => c.Id, (b, c) => new { b, c }) .FirstOrDefaultAsync(x => + x.b.ArchitectureFamilyId == architectureFamilyId && + x.b.TensorGroupProfileId == tensorGroupProfileId && x.b.AiModelHashId == modelId && x.b.ImatrixDefinitionId == imatrixDefinitionId && x.c.BaseQuant == lookup.BaseQuant && @@ -744,4 +748,4 @@ private static byte NormalizeBaselineIdForIsolation(byte baselineId) return builtIn?.UniqueId ?? baselineId; } } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 0c4c56d..c1eee8a 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -432,13 +432,13 @@ private async Task ExecuteDuplicatePlanAsync( await using var db = new MagicQuantContext(); - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdOrNullAsync(db, ct); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (exactAiModelHashId == null) + if (scopedAiModelHashId == null) return (null, null); var imatrixDefinitionId = - await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); var comboId = await db.TensorCombos @@ -460,9 +460,15 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiM if (comboId == Guid.Empty) return (null, null); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var benchmarkId = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == exactAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.AiModelHashId == scopedAiModelHashId.Value && + x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == comboId) .Select(x => x.Id) .FirstOrDefaultAsync(ct); @@ -479,12 +485,11 @@ public async Task ProcessHybridQuantAsync( string baseLogitsDir = GetBaseLogitsDirectory(); DateTime startedUtc = DateTime.UtcNow; - var forceBaselineRelearn = Cache.ForceRelearnBaselineTensorMappings && IsLearnableBaselineRun(quant); + const bool forceBaselineRelearn = false; bool pureExternalBaseline = ShouldDownloadExternalBaselineInsteadOfQuantizing(quant); - bool baselineLearnedTruthExists = - !forceBaselineRelearn && await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); + bool baselineLearnedTruthExists = await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); - if (!forceBaselineRelearn && baselineLearnedTruthExists && await _benchmarker.TryReuseExistingBenchmarksAsync( + if (baselineLearnedTruthExists && await _benchmarker.TryReuseExistingBenchmarksAsync( quantConfig: quant, modelPath: string.Empty, benchDir: modelBenchDir, @@ -495,7 +500,7 @@ public async Task ProcessHybridQuantAsync( return SampleProcessState.Skipped; } - if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) + if (baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) { AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); return SampleProcessState.Skipped; @@ -556,7 +561,7 @@ public async Task ProcessHybridQuantAsync( throw new InvalidOperationException( $"Missing blanket learned mapping for external/custom baseline '{quant.BaseQuant.Names[0]}'. " + "External baseline hybrids require learned tensor mappings before sampling. " + - "Run with --relearn-baseline-mappings."); + "Use targeted YAML relearn configuration to regenerate only the affected baseline/profile truth."); } } @@ -712,15 +717,15 @@ private async Task HasLearnedTruthForBaselineAsync(BaselineQuants baseline return false; await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - - if (scopedAiModelHashId == null) - return false; + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var baselineDefinition = await BaselineDefinitionResolver.ResolveRequiredDefinitionAsync(db, baseline, ct); var query = db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) - .Where(x => x.BaselineCanonicalKey == baseline.CanonicalKey); + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.BaselineQuantDefinitionId == baselineDefinition.Id); if (baseline.DefaultTensorScheme != null) query = query.Where(x => x.TensorWeightSchemeId == baseline.DefaultTensorScheme.UniqueId); @@ -913,6 +918,10 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( throw new InvalidOperationException( "Unable to persist learned mappings because scoped AiModelHash row was not found."); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var baselineDefinition = await BaselineDefinitionResolver.ResolveRequiredDefinitionAsync(db, quant.BaseQuant, ct); + var combo = await db.TensorCombos .AsNoTracking() .FirstAsync(x => x.BaseQuant == quant.BaseQuant.UniqueId && @@ -920,13 +929,16 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var benchmarkAiModelHashId = scopedAiModelHashId.Value; var imatrixDefinitionId = - await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId, + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, benchmarkAiModelHashId, createIfMissing: false, ct); var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.AiModelHashId == benchmarkAiModelHashId && + x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) @@ -946,6 +958,10 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiM { Id = Guid.NewGuid(), AiBenchmarkId = benchmarkId.Value, + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, + BaselineQuantDefinitionId = baselineDefinition.Id, + TensorComboId = combo.Id, AiModelHashId = scopedAiModelHashId.Value, BaselineQuantId = quant.BaseQuant.UniqueId, TensorWeightSchemeId = tensorScheme.UniqueId, @@ -965,8 +981,9 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiM $"Prepared learning truth for baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && - x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.BaselineQuantDefinitionId == baselineDefinition.Id && x.TensorWeightSchemeId == tensorScheme.UniqueId) .ExecuteDeleteAsync(ct); @@ -1048,18 +1065,24 @@ private async Task BenchmarkExistsAsync(HybridQuant quant, CancellationTok await using var db = new MagicQuantContext(); - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdOrNullAsync(db, ct); + var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (exactAiModelHashId == null) + if (scopedAiModelHashId == null) return false; var imatrixDefinitionId = - await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId.Value, + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAiModelHashId.Value, createIfMissing: false, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var bench = await db.AiBenchmarks .AsNoTracking() - .Where(x => x.AiModelHashId == exactAiModelHashId.Value && x.ImatrixDefinitionId == imatrixDefinitionId) + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.AiModelHashId == scopedAiModelHashId.Value && + x.ImatrixDefinitionId == imatrixDefinitionId) .Join( db.TensorCombos.AsNoTracking(), benchmark => benchmark.TensorComboId, @@ -1131,6 +1154,8 @@ private async Task PersistQuantizationRunAsync( throw new InvalidOperationException("Cache.CurrentModelId is not set."); var lookup = BuildTensorLookup(quant); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); await using var db = new MagicQuantContext(); @@ -1175,7 +1200,10 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, persiste await ImatrixIdentityService.ValidateOwnershipAsync(db, persistenceAiModelHashId, imatrixDefinitionId, ct); Guid? aiBenchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == persistenceAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.AiModelHashId == persistenceAiModelHashId && + x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == tensorCombo.Id) .Select(x => (Guid?)x.Id) .FirstOrDefaultAsync(ct); @@ -1183,6 +1211,8 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, persiste var row = new QuantizationRun { Id = Guid.NewGuid(), + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, AiModelHashId = persistenceAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = tensorCombo.Id, @@ -1662,7 +1692,7 @@ private async Task RunLlamaQuantizeAsync( throw new InvalidOperationException( $"Full learned base-carrier coverage is incomplete for baseline '{quant.BaseQuant.Names[0]}'. " + $"Missing={missing.Count}. Examples=[{string.Join(", ", missing.Take(15))}]. " + - "Run with --relearn-baseline-mappings."); + "Use targeted YAML relearn configuration to regenerate only the affected baseline/profile truth."); } } @@ -1778,56 +1808,16 @@ public async Task> ReadExactTensorTypesAsync .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); } - public async Task ClearLearnedBaselineTensorMappingsAsync(CancellationToken ct = default) + public Task ClearLearnedBaselineTensorMappingsAsync(CancellationToken ct = default) { - await using var db = new MagicQuantContext(); - int removed = await db.LearnedBaselineTensorQuants.ExecuteDeleteAsync(ct); - AnsiConsole.MarkupLine( - $"[yellow]Relearn requested:[/] removed [red]{removed:N0}[/] learned baseline tensor mapping rows."); + throw new NotSupportedException( + "Global learned tensor mapping wipes were removed. Use targeted YAML relearn options so deletion is scoped, counted, and confirmed."); } - public async Task InvalidateBaselineArtifactsAsync(CancellationToken ct = default) + public Task InvalidateBaselineArtifactsAsync(CancellationToken ct = default) { - await ClearLearnedBaselineTensorMappingsAsync(ct); - - foreach (var baseline in BaselineQuants.GetAllRecognizedBaselines()) - { - var pure = HybridQuant.CreatePureBaseline(baseline); - var name = GenerateHybridName(pure); - var ggufPath = Path.Combine(_paths.GgufDir, $"{name}.gguf"); - var success = Path.Combine(_paths.GgufDir, $"{name}.gguf.success.json"); - var log = ggufPath + ".quantize.log"; - - await HardDeleteHelper.DeleteFileIfExistsAsync(ggufPath); - await HardDeleteHelper.DeleteFileIfExistsAsync(success); - await HardDeleteHelper.DeleteFileIfExistsAsync(log); - - string benchDir = Path.Combine(_paths.BenchDir, name); - if (Directory.Exists(benchDir)) - Directory.Delete(benchDir, recursive: true); - } - - string debugDir = Path.Combine(_paths.BenchDir, "_learning_debug"); - if (Directory.Exists(debugDir)) - Directory.Delete(debugDir, recursive: true); - - if (!string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory) && - Directory.Exists(Cache.ExternalBaselineCacheDirectory)) - Directory.Delete(Cache.ExternalBaselineCacheDirectory, recursive: true); - - string nativeType = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); - string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; - string nativeBaseFile = Path.Combine(_paths.GgufDir, $"{modelName}-{nativeType}.gguf"); - await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile); - await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile + ".success.json"); - await HardDeleteHelper.DeleteFileIfExistsAsync(nativeBaseFile + ".convert.log"); - - string nativeBenchDir = Path.Combine(_paths.BenchDir, nativeType); - if (Directory.Exists(nativeBenchDir)) - Directory.Delete(nativeBenchDir, recursive: true); - - AnsiConsole.MarkupLine( - "[yellow]Relearn requested:[/] baseline artifacts, benchmark caches, and learning diagnostics were invalidated."); + throw new NotSupportedException( + "Global baseline artifact invalidation was removed. Use learning.force_relearn_architecture_family, learning.force_relearn_standard_baselines, or include.force_relearn."); } public async Task HasNativeSourceLearnedTruthAsync(CancellationToken ct = default) @@ -1838,15 +1828,15 @@ public async Task HasNativeSourceLearnedTruthAsync(CancellationToken ct = var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - - if (scopedAiModelHashId == null) - return false; + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var baselineDefinition = await BaselineDefinitionResolver.ResolveRequiredDefinitionAsync(db, BaselineQuants.GetNativeQuant(), ct); return await db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && - x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.BaselineQuantDefinitionId == baselineDefinition.Id && x.TensorWeightSchemeId == nativeScheme.UniqueId) .AnyAsync(ct); } @@ -1860,27 +1850,11 @@ public async Task LearnNativeSourceTruthAsync( var nativeScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - if (!Cache.ForceRelearnBaselineTensorMappings) + if (await HasNativeSourceLearnedTruthAsync(ct)) { - await using var precheckDb = new MagicQuantContext(); - var precheckScopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(precheckDb, ct); - - if (precheckScopedAiModelHashId != null) - { - int existingRows = await precheckDb.LearnedBaselineTensorQuants - .AsNoTracking() - .Where(x => x.AiModelHashId == precheckScopedAiModelHashId.Value && - x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && - x.TensorWeightSchemeId == nativeScheme.UniqueId) - .CountAsync(ct); - - if (existingRows > 0) - { - AnsiConsole.MarkupLine( - $"[grey]Native-source learned truth already exists:[/] [cyan]{existingRows:N0}[/] row(s) for [yellow]{Markup.Escape(nativeScheme.Names[0])}[/]. Skipping relearn. Use [green]--relearn-baseline-mappings[/] to regenerate."); - return; - } - } + AnsiConsole.MarkupLine( + $"[grey]Native-source learned truth already exists:[/] for [yellow]{Markup.Escape(nativeScheme.Names[0])}[/]. Skipping. Use targeted YAML relearn to regenerate native source truth if needed."); + return; } var metadata = await ReadTensorMetadataFromGgufAsync(nativeGgufPath, Path.GetDirectoryName(nativeGgufPath)!); @@ -1930,18 +1904,27 @@ public async Task LearnNativeSourceTruthAsync( x.MoeExperts == 0 && x.MoeRouter == 0, ct); if (combo == null) - throw new InvalidOperationException( - "Native-source benchmark TensorCombo is missing; benchmark base model first."); + { + combo = new TensorCombo((TensorConfig)HybridQuant.CreatePureBaseline(BaselineQuants.GetNativeQuant())); + db.TensorCombos.Add(combo); + await db.SaveChangesAsync(ct); + } - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var benchmarkAiModelHashId = scopedAiModelHashId; var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( db, - exactAiModelHashId, + benchmarkAiModelHashId, createIfMissing: false, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var baselineDefinition = await BaselineDefinitionResolver.ResolveRequiredDefinitionAsync(db, BaselineQuants.GetNativeQuant(), ct); + var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == exactAiModelHashId && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.AiModelHashId == benchmarkAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) @@ -1962,9 +1945,17 @@ public async Task LearnNativeSourceTruthAsync( { Id = Guid.NewGuid(), AiBenchmarkId = benchmarkId.Value, + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, + BaselineQuantDefinitionId = baselineDefinition.Id, + TensorComboId = combo.Id, AiModelHashId = scopedAiModelHashId, BaselineQuantId = BaselineQuants.NativeSourceUniqueId, TensorWeightSchemeId = nativeScheme.UniqueId, + BaselineCanonicalKey = BaselineQuants.GetNativeQuant().CanonicalKey, + BaselineSourceKind = BaselineQuants.GetNativeQuant().SourceKind, + BaselineSourceRepository = BaselineQuants.GetNativeQuant().SourceRepository, + BaselineSourceFileName = BaselineQuants.GetNativeQuant().SourceFileName, TensorGroupId = primaryGroup?.UniqueId ?? UnknownTensorGroupId, TensorName = x.Key, FinalQuantType = x.Value.FinalQuantType @@ -1976,8 +1967,9 @@ public async Task LearnNativeSourceTruthAsync( throw new InvalidOperationException("Native-source learning produced no persistable rows."); await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == scopedAiModelHashId && - x.BaselineQuantId == BaselineQuants.NativeSourceUniqueId && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.BaselineQuantDefinitionId == baselineDefinition.Id && x.TensorWeightSchemeId == nativeScheme.UniqueId) .ExecuteDeleteAsync(ct); @@ -2077,6 +2069,10 @@ private async Task LearnAndPersistBaselineTensorMapAsync( throw new InvalidOperationException( "Unable to persist learned mappings because scoped AiModelHash row was not found."); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var baselineDefinition = await BaselineDefinitionResolver.ResolveRequiredDefinitionAsync(db, quant.BaseQuant, ct); + var combo = await db.TensorCombos .AsNoTracking() .FirstAsync(x => x.BaseQuant == quant.BaseQuant.UniqueId && @@ -2084,13 +2080,16 @@ private async Task LearnAndPersistBaselineTensorMapAsync( x.AttnOutput == 0 && x.FfnUpGate == 0 && x.FfnDown == 0 && x.MoeExperts == 0 && x.MoeRouter == 0, ct); - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var benchmarkAiModelHashId = scopedAiModelHashId.Value; var imatrixDefinitionId = - await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiModelHashId, + await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, benchmarkAiModelHashId, createIfMissing: false, ct); var benchmarkId = await db.AiBenchmarks - .Where(x => x.AiModelHashId == exactAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.AiModelHashId == benchmarkAiModelHashId && + x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == combo.Id) .OrderByDescending(x => x.Id) .Select(x => (Guid?)x.Id) @@ -2110,6 +2109,10 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiM { Id = Guid.NewGuid(), AiBenchmarkId = benchmarkId.Value, + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, + BaselineQuantDefinitionId = baselineDefinition.Id, + TensorComboId = combo.Id, AiModelHashId = scopedAiModelHashId.Value, BaselineQuantId = quant.BaseQuant.UniqueId, TensorWeightSchemeId = tensorScheme.UniqueId, @@ -2129,8 +2132,9 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, exactAiM $"Learning baseline '{quant.BaseQuant.Names[0]}' produced no persistable rows."); await db.LearnedBaselineTensorQuants - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value && - x.BaselineCanonicalKey == quant.BaseQuant.CanonicalKey && + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.BaselineQuantDefinitionId == baselineDefinition.Id && x.TensorWeightSchemeId == tensorScheme.UniqueId) .ExecuteDeleteAsync(ct); @@ -2488,7 +2492,7 @@ private List BuildRequestedTensorOverrides( if (learned.Count == 0) throw new InvalidOperationException( - $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Run with --relearn-baseline-mappings to regenerate."); + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Use targeted YAML relearn configuration to regenerate only the affected baseline/profile truth."); var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); @@ -2556,7 +2560,7 @@ private Dictionary LoadBaseCarrierTensorMappingsOrThrow( { throw new InvalidOperationException( $"Missing full learned base-carrier mapping for baseline '{quant.BaseQuant.Names[0]}'. " + - "Run with --relearn-baseline-mappings before applying learned tensor configurations."); + "Use targeted YAML relearn configuration before applying learned tensor configurations."); } return blanket; @@ -2568,15 +2572,25 @@ private Dictionary TryLoadAllLearnedTensorMappings( bool allowDominantFallback = false) { using var db = new MagicQuantContext(); - var scopedAiModelHashId = - ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db).GetAwaiter().GetResult(); - if (scopedAiModelHashId == null) + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var normalizedCanonicalKey = BaselineDefinitionResolver.NormalizeCanonicalKey(canonicalBaselineKey); + var baselineDefinitionId = db.BaselineQuantDefinitions + .AsNoTracking() + .Where(x => (x.ArchitectureFamilyId == architectureFamilyId || x.ArchitectureFamilyId == null) && + x.NormalizedCanonicalKey == normalizedCanonicalKey) + .OrderByDescending(x => x.ArchitectureFamilyId.HasValue) + .Select(x => (int?)x.Id) + .FirstOrDefault(); + + if (!baselineDefinitionId.HasValue) return new Dictionary(StringComparer.Ordinal); var allRows = db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) - .Where(x => x.BaselineCanonicalKey == canonicalBaselineKey) + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.BaselineQuantDefinitionId == baselineDefinitionId.Value) .OrderBy(x => x.TensorName) .ToList(); @@ -2633,18 +2647,18 @@ private Dictionary TryLoadLearnedTensorMapping( bool allowDominantFallback = false) { using var db = new MagicQuantContext(); - - var scopedAiModelHashId = - ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db).GetAwaiter().GetResult(); - - if (scopedAiModelHashId == null) - return new Dictionary(StringComparer.Ordinal); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var baselineDefinition = BaselineDefinitionResolver.ResolveRequiredDefinitionAsync(db, sourceBaseline) + .GetAwaiter() + .GetResult(); var allRows = db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) - .Where(x => x.BaselineCanonicalKey == sourceBaseline.CanonicalKey) - .Where(x => x.TensorGroupId == targetGroup.UniqueId) + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.BaselineQuantDefinitionId == baselineDefinition.Id && + x.TensorGroupId == targetGroup.UniqueId) .OrderBy(x => x.TensorName) .ToList(); @@ -2851,14 +2865,24 @@ private async Task> BuildIsolationDeduplicationPlanAs return null; await using var db = new MagicQuantContext(); - var scopedAiModelHashId = await ResolveCurrentScopedAiModelHashIdOrNullAsync(db, ct); - if (scopedAiModelHashId == null) + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var normalizedCanonicalKey = BaselineDefinitionResolver.NormalizeCanonicalKey(plan.TestedCandidateCanonicalKey); + var baselineDefinitionId = await db.BaselineQuantDefinitions + .AsNoTracking() + .Where(x => (x.ArchitectureFamilyId == architectureFamilyId || x.ArchitectureFamilyId == null) && + x.NormalizedCanonicalKey == normalizedCanonicalKey) + .OrderByDescending(x => x.ArchitectureFamilyId.HasValue) + .Select(x => (int?)x.Id) + .FirstOrDefaultAsync(ct); + if (!baselineDefinitionId.HasValue) return null; var rows = await db.LearnedBaselineTensorQuants .AsNoTracking() - .Where(x => x.AiModelHashId == scopedAiModelHashId.Value) - .Where(x => x.BaselineCanonicalKey == plan.TestedCandidateCanonicalKey) + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.BaselineQuantDefinitionId == baselineDefinitionId.Value) .Where(x => x.TensorGroupId == plan.TargetGroupId.Value) .OrderBy(x => x.TensorName) .Select(x => new { x.TensorName, x.FinalQuantType }) @@ -2928,15 +2952,20 @@ private async Task CloneEquivalentIsolationBenchmarkAsync( await db.SaveChangesAsync(ct); } - var exactAiModelHashId = await ResolveCurrentExactAiModelHashIdAsync(db, ct); + var benchmarkAiModelHashId = scopedAiModelHashId.Value; var imatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( db, - exactAiModelHashId, + benchmarkAiModelHashId, createIfMissing: true, ct); + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + var existing = await db.AiBenchmarks - .FirstOrDefaultAsync(x => x.AiModelHashId == exactAiModelHashId && + .FirstOrDefaultAsync(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == tensorGroupProfileId && + x.AiModelHashId == benchmarkAiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.TensorComboId == duplicateCombo.Id, ct); @@ -2946,11 +2975,13 @@ private async Task CloneEquivalentIsolationBenchmarkAsync( var clonedBenchmark = new AiBenchmark { Id = Guid.NewGuid(), + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, Ngl = sourceBench.Ngl, SizeBytes = sourceBench.SizeBytes, TokensPerSecond = sourceBench.TokensPerSecond, TensorComboId = duplicateCombo.Id, - AiModelHashId = exactAiModelHashId, + AiModelHashId = benchmarkAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId }; db.AiBenchmarks.Add(clonedBenchmark); @@ -2971,7 +3002,9 @@ private async Task CloneEquivalentIsolationBenchmarkAsync( db.QuantizationRuns.Add(new QuantizationRun { Id = Guid.NewGuid(), - AiModelHashId = exactAiModelHashId, + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, + AiModelHashId = benchmarkAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = duplicateCombo.Id, AiBenchmarkId = clonedBenchmark.Id, @@ -2988,7 +3021,9 @@ private async Task CloneEquivalentIsolationBenchmarkAsync( db.BenchmarkRuns.Add(new BenchmarkRun { Id = Guid.NewGuid(), - AiModelHashId = exactAiModelHashId, + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = tensorGroupProfileId, + AiModelHashId = benchmarkAiModelHashId, ImatrixDefinitionId = imatrixDefinitionId, TensorComboId = duplicateCombo.Id, AiBenchmarkId = clonedBenchmark.Id, diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 0e545cd..e69da70 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -20,13 +20,16 @@ public async Task GenerateAsync( BenchmarkSnapshotRecord? pplReference = null, CancellationToken ct = default) { - var replacementMap = FinalReleaseMetadataService.BuildReplacementMap(eliminatedBaselines ?? Array.Empty()); + var replacementMap = + FinalReleaseMetadataService.BuildReplacementMap(eliminatedBaselines ?? + Array.Empty()); var namingContext = _namingService.CreateContext(pureBaselineSnapshots); var exportedByKey = exportedArtifacts .GroupBy(x => TensorConfigIdentity.ToKey(x.Snapshot.Config), StringComparer.Ordinal) .ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal); - double? referencePpl = ResolveReferencePpl(pplReference, pureBaselineSnapshots, exportedArtifacts.Select(x => x.Snapshot)); + double? referencePpl = + ResolveReferencePpl(pplReference, pureBaselineSnapshots, exportedArtifacts.Select(x => x.Snapshot)); var rows = exportedArtifacts .OrderBy(x => x.Snapshot.Kld) @@ -55,7 +58,8 @@ public async Task GenerateAsync( QuantFamily = artifact.BaselineFamily, Kld = artifact.Snapshot.Kld, Ppl = artifact.Snapshot.Ppl, - PplDeltaPercent = FinalReleaseMetadataService.CalculatePplDeltaPercent(artifact.Snapshot.Ppl, referencePpl), + PplDeltaPercent = + FinalReleaseMetadataService.CalculatePplDeltaPercent(artifact.Snapshot.Ppl, referencePpl), SizeBytes = artifact.Snapshot.SizeBytes, DownloadTarget = download }; @@ -98,7 +102,9 @@ public async Task GenerateCloneAsync( { NameCell = name, Provider = string.IsNullOrWhiteSpace(artifact.Provider) ? "Cloned config" : artifact.Provider, - QuantFamily = string.IsNullOrWhiteSpace(artifact.QuantFamily) ? artifact.BaseQuant : artifact.QuantFamily, + QuantFamily = string.IsNullOrWhiteSpace(artifact.QuantFamily) + ? artifact.BaseQuant + : artifact.QuantFamily, Kld = record.Kld, Ppl = record.Ppl, PplDeltaPercent = record.PplDeltaPercent, @@ -112,7 +118,9 @@ public async Task GenerateCloneAsync( { SourceDescription = sourceDescription, SourceWasHuggingFaceRepo = sourceWasHuggingFaceRepo, - ArchivedManifestFileNames = archivedManifestFileNames?.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.OrdinalIgnoreCase).OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList() + ArchivedManifestFileNames = archivedManifestFileNames?.Where(x => !string.IsNullOrWhiteSpace(x)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList() ?? new List() }; @@ -120,7 +128,8 @@ public async Task GenerateCloneAsync( outputDirectory, modelName, rows, - hasReplacementDetails: cloneContext.ArchivedManifestFileNames.Contains(MagicQuantManifestPathService.ReplacementsFileName, StringComparer.OrdinalIgnoreCase), + hasReplacementDetails: cloneContext.ArchivedManifestFileNames.Contains( + MagicQuantManifestPathService.ReplacementsFileName, StringComparer.OrdinalIgnoreCase), cloneContext: cloneContext, exportedArtifacts: Array.Empty(), ct: ct); @@ -146,13 +155,21 @@ private async Task GenerateCoreAsync( string resolvedModelName = ResolveReadmeTitleModelName(modelName); sb.AppendLine($"# MagicQuant Hybrids (v2.0) - {resolvedModelName}"); sb.AppendLine(); - sb.AppendLine("MagicQuant is a benchmark driven GGUF hybrid discovery and validation system focused on finding real, practical GGUF quants specific to each architecture."); + sb.AppendLine( + "MagicQuant is a benchmark driven GGUF hybrid discovery and validation system focused on finding real, practical GGUF quants specific to each architecture."); sb.AppendLine(); - sb.AppendLine("Whether it's a pure baseline model built by llama.cpp, learned tensor configurations from Unsloth, or a custom built MagicQuant hybrid, the model table below shows quants that have won dominance checks, survived collapse spaces, and/or were found to be nonlinearly better. Instead of dumping every quant type possible, MagicQuant tests, validates, and brutally murders anything deemed unworthy."); + sb.AppendLine( + "Whether it's a pure baseline model built by llama.cpp, learned tensor configurations from Unsloth, or a custom built MagicQuant hybrid, the model table below shows quants that have won dominance checks, survived collapse spaces, and/or were found to be nonlinearly better. Instead of dumping every quant type possible, MagicQuant tests, validates, and brutally murders anything deemed unworthy."); sb.AppendLine(); - sb.AppendLine("You can learn more [from the MagicQuant Wiki](https://github.com/magiccodingman/MagicQuant-Wiki). It covers things like nonlinear winners, prediction systems, imatrix generation philosophy, isolated tensor analysis, and more."); + sb.AppendLine("
"); + sb.AppendLine("Support MagicQuant"); + sb.AppendLine(); + sb.AppendLine( + "I’m a solo developer working full time for myself to achieve my dream. I build open source code on the side. If you like any of my work, buying me a coffee is always appreciated. Otherwise, I hope you enjoy, maybe give me a star or something. Or just send me good vibes. Either way, thank you!"); sb.AppendLine(); - sb.AppendLine("By default, if an external provider like Unsloth is deemed the winner, the repo will generally link directly to the original provider instead of re-hosting the quant. External GGUFs are normally only re-uploaded when a specific winning variant does not already exist (e.g. Heretic models or similar)."); + sb.AppendLine("[Click here to see ways to support](https://sayou.biz/support) - BTC, Paypal, GitHub sponsors."); + sb.AppendLine(); + sb.AppendLine("
"); sb.AppendLine(); if (cloneContext != null) @@ -164,33 +181,42 @@ private async Task GenerateCoreAsync( sb.AppendLine(); AppendDownloadTable(sb, rows); sb.AppendLine(); - sb.AppendLine("---"); + + //if (cloneContext == null) + //{ + AppendProviderCredits(sb, exportedArtifacts); sb.AppendLine(); - AppendReleaseMetadata(sb, cloneContext); + sb.AppendLine("
"); + sb.AppendLine("Warning - Is MagicQuant Better? (hint: how you frame the question matters)"); sb.AppendLine(); - sb.AppendLine("---"); + sb.AppendLine("External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant rebuilds a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids or external baselines (like Unsloth) can be judged on a more equal footing. That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency and equal playing field comparisons, not as a universal judgment of the original creator's exact release artifact."); sb.AppendLine(); + sb.AppendLine("**Easier to digest explanation:**"); + sb.AppendLine(); + sb.AppendLine("MagicQuant compares and benchmarks the models quant to tensor configurations, but not the original artifact. And there's different reasons MagicQuant chooses to lift up a winning quant, not all winners are purely \"better\". It depends heavily on a variety of factors. Though choices are always documented in the repo under the manifest folder. You can always view what and why decisions were made by the automated system."); + sb.AppendLine(); + sb.AppendLine("So, MagicQuant can confidently tell you, \"under the same quantization to tensor configurations and identical imatrix, with this benchmark, I deemed this a winner\"."); + sb.AppendLine(); + sb.AppendLine("
"); + sb.AppendLine(); + //} + + sb.AppendLine("
"); + sb.AppendLine("Re-Uploading External Provider Baselines"); + sb.AppendLine(); + sb.AppendLine("By default, if an external provider like Unsloth is deemed the winner, the repo should generally link directly to the original provider instead of re-hosting the quant. External GGUFs are normally only re-uploaded when a specific winning variant does not already exist (e.g. Heretic models or similar)."); + sb.AppendLine(); + sb.AppendLine("
"); - if (hasReplacementDetails) - { - AppendReasonCodeDetails(sb); - sb.AppendLine(); - } - - if (cloneContext == null) - { - AppendProviderCredits(sb, exportedArtifacts); - sb.AppendLine(); + sb.AppendLine(); - AppendCollapsible(sb, "Warning", "External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant may rebuild a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids can be judged on a more equal footing. That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency, not as a universal judgment of the original creator's exact release artifact."); - sb.AppendLine(); - } + sb.AppendLine("---"); + sb.AppendLine(); - sb.AppendLine("## Support"); - sb.AppendLine("I’m a solo developer working full time for myself to achieve my dream. I build open source code on the side. If you like any of my work, buying me a coffee is always appreciated. Otherwise, I hope you enjoy, maybe give me a star or something. Or just send me good vibes. Either way, thank you!"); + AppendReleaseMetadata(sb, cloneContext); sb.AppendLine(); - sb.AppendLine("[Click here to see ways to support](https://sayou.biz/support) - BTC, Paypal, GitHub sponsors."); + sb.AppendLine("---"); sb.AppendLine(); await File.WriteAllTextAsync(readmePath, sb.ToString(), ct); @@ -200,19 +226,25 @@ private async Task GenerateCoreAsync( private static void AppendCloneNotice(StringBuilder sb, ReadmeCloneContext clone) { - sb.AppendLine("## Clone notice"); - sb.AppendLine(); - string source = clone.SourceWasHuggingFaceRepo ? BuildHuggingFaceRepoLink(clone.SourceDescription) : $"`{EscapePipe(clone.SourceDescription)}`"; - sb.AppendLine($"This repository did not run through the full MagicQuant evolution/search pipeline. It is a clone of the final survivor tensor configurations from {source}, rebuilt and benchmarked locally for this model."); + sb.AppendLine("
"); + sb.AppendLine("Clone Notice"); + sb.AppendLine(); + sb.AppendLine( + $"This repository did not run through the full MagicQuant evolution/search pipeline. It is a clone of the final survivor tensor configurations from {source}, rebuilt and benchmarked locally for this model."); sb.AppendLine(); - sb.AppendLine("The archived MagicQuant JSON files in `magicquant-manifest/` are copied from the source release for durability. The clone benchmark JSON and the table below are from this clone run, so those metrics reflect the rebuilt outputs in this repository."); + sb.AppendLine( + "The archived MagicQuant JSON files in `magicquant-manifest/` are copied from the source release for durability. The clone benchmark JSON and the table below are from this clone run, so those metrics reflect the rebuilt outputs in this repository."); + sb.AppendLine(); + sb.AppendLine("
"); sb.AppendLine(); } - private static string BuildCloneNameCell(string rawName, string fileName, IReadOnlyDictionary> replacementHints) + + private static string BuildCloneNameCell(string rawName, string fileName, + IReadOnlyDictionary> replacementHints) { string safeName = EscapePipe(rawName); @@ -233,7 +265,8 @@ private static string BuildCloneNameCell(string rawName, string fileName, IReadO return $"[{safeName}](#winner-notes \"{EscapeTooltip(tooltip)}\")"; } - private static bool TryGetReplacementHint(IReadOnlyDictionary> replacementHints, string? key, out IReadOnlyList replaced) + private static bool TryGetReplacementHint(IReadOnlyDictionary> replacementHints, + string? key, out IReadOnlyList replaced) { replaced = Array.Empty(); if (string.IsNullOrWhiteSpace(key)) @@ -245,7 +278,8 @@ private static bool TryGetReplacementHint(IReadOnlyDictionary> LoadCloneReplacementHints(string outputDirectory) { var output = new Dictionary>(StringComparer.OrdinalIgnoreCase); - string path = MagicQuantManifestPathService.GetManifestFilePath(outputDirectory, MagicQuantManifestPathService.FinalSurvivorsFileName); + string path = MagicQuantManifestPathService.GetManifestFilePath(outputDirectory, + MagicQuantManifestPathService.FinalSurvivorsFileName); if (!File.Exists(path)) return output; @@ -268,7 +302,8 @@ private static Dictionary> LoadCloneReplacementHin } catch (Exception ex) { - AnsiConsole.MarkupLine($"[yellow]Could not read clone replacement hints from archived final-survivors JSON:[/] {Markup.Escape(ex.Message)}"); + AnsiConsole.MarkupLine( + $"[yellow]Could not read clone replacement hints from archived final-survivors JSON:[/] {Markup.Escape(ex.Message)}"); } return output; @@ -290,7 +325,8 @@ private static List ReadReplacedShortNames(JsonElement survivorRow) .ToList(); } - private static void AddReplacementHint(Dictionary> output, string? key, IReadOnlyList replaced) + private static void AddReplacementHint(Dictionary> output, string? key, + IReadOnlyList replaced) { if (string.IsNullOrWhiteSpace(key) || replaced.Count == 0) return; @@ -320,32 +356,47 @@ private static void AppendReleaseMetadata(StringBuilder sb, ReadmeCloneContext? sb.AppendLine("## Release metadata"); sb.AppendLine(); if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.FinalSurvivorsFileName)) - sb.AppendLine($"- [Final survivor metrics]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.FinalSurvivorsFileName)}) — full file names, KLD, PPL, PPL delta %, byte sizes, download targets, and replacement lineage. PPL delta % is measured against the native/reference PPL when available; negative is better and larger positive values are worse."); + sb.AppendLine( + $"- [Final survivor metrics]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.FinalSurvivorsFileName)}) — full file names, KLD, PPL, PPL delta %, byte sizes, download targets, and replacement lineage. PPL delta % is measured against the native/reference PPL when available; negative is better and larger positive values are worse."); if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.HybridMapFileName)) - sb.AppendLine($"- [Hybrid tensor map]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.HybridMapFileName)}) — tensor-group assignments and effective-state details for MagicQuant hybrid GGUFs."); - - if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.ReplacementsFileName)) - sb.AppendLine($"- [Replacement details]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.ReplacementsFileName)}) — structured details for baselines or anchors removed from the final download table, including reason codes, KLD deltas, PPL delta %, and size deltas."); + sb.AppendLine( + $"- [Hybrid tensor map]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.HybridMapFileName)}) — tensor-group assignments and effective-state details for MagicQuant hybrid GGUFs."); - sb.AppendLine($"- [Clone tensor configs]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.CloneConfigsFileName)}) — exact per-GGUF tensor quantization maps for reproducing this final output list in repository clone mode."); + sb.AppendLine( + $"- [Clone tensor configs]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.CloneConfigsFileName)}) — exact per-GGUF tensor quantization maps for reproducing this final output list in repository clone mode."); if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.IsolationSamplesFileName)) - sb.AppendLine($"- [Isolation samples]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.IsolationSamplesFileName)}) — isolated base/group probe samples with KLD, PPL, PPL delta %, and size truth."); + sb.AppendLine( + $"- [Isolation samples]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.IsolationSamplesFileName)}) — isolated base/group probe samples with KLD, PPL, PPL delta %, and size truth."); if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.BadTradesFileName)) - sb.AppendLine($"- [Bad trade details]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.BadTradesFileName)}) — structured bad-trade pruning decisions from the isolation optimizer."); + sb.AppendLine( + $"- [Bad trade details]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.BadTradesFileName)}) — structured bad-trade pruning decisions from the isolation optimizer."); if (cloneContext != null) - sb.AppendLine($"- [Clone benchmark summary]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.CloneBenchmarksFileName)}) — fresh benchmark results from this clone run."); + sb.AppendLine( + $"- [Clone benchmark summary]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.CloneBenchmarksFileName)}) — fresh benchmark results from this clone run."); + + + if (ShouldLinkManifestFile(cloneContext, MagicQuantManifestPathService.ReplacementsFileName)) + { + sb.AppendLine( + $"- [Replacement details]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.ReplacementsFileName)}) — structured details for baselines or anchors removed from the final download table, including reason codes, KLD deltas, PPL delta %, and size deltas."); + + AppendReasonCodeDetails(sb); + sb.AppendLine(); + } } private static bool ShouldLinkManifestFile(ReadmeCloneContext? cloneContext, string fileName) { return cloneContext == null || cloneContext.ArchivedManifestFileNames.Contains(fileName, StringComparer.OrdinalIgnoreCase) || - string.Equals(fileName, MagicQuantManifestPathService.CloneConfigsFileName, StringComparison.OrdinalIgnoreCase) || - string.Equals(fileName, MagicQuantManifestPathService.CloneBenchmarksFileName, StringComparison.OrdinalIgnoreCase); + string.Equals(fileName, MagicQuantManifestPathService.CloneConfigsFileName, + StringComparison.OrdinalIgnoreCase) || + string.Equals(fileName, MagicQuantManifestPathService.CloneBenchmarksFileName, + StringComparison.OrdinalIgnoreCase); } @@ -491,7 +542,8 @@ private static bool NeedsYamlQuotes(string text) } char first = text[0]; - return first is '-' or '?' or ':' or '@' or '!' or '&' or '*' or '[' or ']' or '{' or '}' or '|' or '>' or '%' or '`' or ','; + return first is '-' or '?' or ':' or '@' or '!' or '&' or '*' or '[' or ']' or '{' or '}' or '|' or '>' or '%' + or '`' or ','; } private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection rows) @@ -503,7 +555,9 @@ private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection"); sb.AppendLine("Replacement reason codes"); sb.AppendLine(); - sb.AppendLine("- `STRICT_DOMINANCE` — the winner was no larger and had lower real KLD than the removed anchor."); - sb.AppendLine("- `NEAR_BASELINE_PREMIUM` — the winner used only the configured near-baseline size premium and beat the real linear KLD trade line."); - sb.AppendLine("- `INTERIOR_DISCOVERY` — the winner was selected as a useful interior point inside a size/KLD gap between anchors."); - sb.AppendLine("- `SPACING_COLLAPSE` — two candidates were too close in practical output space, so the stronger one was kept."); - sb.AppendLine("- `FINAL_DOMINANCE` — a later validated survivor dominated this artifact in final real benchmark comparison."); + sb.AppendLine( + "- `STRICT_DOMINANCE` — the winner was no larger and had lower real KLD than the removed anchor."); + sb.AppendLine( + "- `NEAR_BASELINE_PREMIUM` — the winner used only the configured near-baseline size premium and beat the real linear KLD trade line."); + sb.AppendLine( + "- `INTERIOR_DISCOVERY` — the winner was selected as a useful interior point inside a size/KLD gap between anchors."); + sb.AppendLine( + "- `SPACING_COLLAPSE` — two candidates were too close in practical output space, so the stronger one was kept."); + sb.AppendLine( + "- `FINAL_DOMINANCE` — a later validated survivor dominated this artifact in final real benchmark comparison."); sb.AppendLine(); sb.AppendLine(""); - sb.AppendLine($"Underlined names in the table replaced or ultimately inherited the replacement of another artifact. Hover the name for the short replacement summary, or inspect `{MagicQuantManifestPathService.RelativeManifestPath(MagicQuantManifestPathService.ReplacementsFileName)}` for exact KLD/PPL/size deltas."); + sb.AppendLine( + $"Underlined names in the table replaced or ultimately inherited the replacement of another artifact. Hover the name for the short replacement summary, or inspect `{MagicQuantManifestPathService.RelativeManifestPath(MagicQuantManifestPathService.ReplacementsFileName)}` for exact KLD/PPL/size deltas."); sb.AppendLine(); sb.AppendLine(""); } @@ -604,15 +666,6 @@ private void AppendProviderCredits(StringBuilder sb, IReadOnlyCollection"); } - private static void AppendCollapsible(StringBuilder sb, string summary, string body) - { - sb.AppendLine("
"); - sb.AppendLine($"{EscapeHtml(summary)}"); - sb.AppendLine(); - sb.AppendLine(body); - sb.AppendLine(); - sb.AppendLine("
"); - } private static double? ResolveReferencePpl( BenchmarkSnapshotRecord? pplReference, @@ -638,10 +691,16 @@ private static void AppendCollapsible(StringBuilder sb, string summary, string b ?.Ppl; } - private static string ToGB(ulong bytes) => (bytes / 1000d / 1000d / 1000d).ToString("0.00", CultureInfo.InvariantCulture); + private static string ToGB(ulong bytes) => + (bytes / 1000d / 1000d / 1000d).ToString("0.00", CultureInfo.InvariantCulture); + private static string EscapePipe(string value) => (value ?? string.Empty).Replace("|", "\\|"); - private static string EscapeTooltip(string value) => (value ?? string.Empty).Replace("\"", """).Replace("|", " "); - private static string EscapeHtml(string value) => (value ?? string.Empty).Replace("&", "&").Replace("<", "<").Replace(">", ">"); + + private static string EscapeTooltip(string value) => + (value ?? string.Empty).Replace("\"", """).Replace("|", " "); + + private static string EscapeHtml(string value) => + (value ?? string.Empty).Replace("&", "&").Replace("<", "<").Replace(">", ">"); private sealed class ReadmeArtifactRow { @@ -661,4 +720,4 @@ private sealed class ReadmeCloneContext public bool SourceWasHuggingFaceRepo { get; init; } public IReadOnlyList ArchivedManifestFileNames { get; init; } = Array.Empty(); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/TargetedRelearnService.cs b/MagicQuant/Services/TargetedRelearnService.cs new file mode 100644 index 0000000..a1231a0 --- /dev/null +++ b/MagicQuant/Services/TargetedRelearnService.cs @@ -0,0 +1,222 @@ +using MagicQuant.Configuration; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class TargetedRelearnService +{ + public async Task PlanConfirmAndExecuteAsync(IReadOnlyCollection resolvedCustomBaselines, CancellationToken ct = default) + { + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + + await using var db = new MagicQuantContext(); + var plan = new TargetedRelearnPlan(architectureFamilyId, tensorGroupProfileId); + + if (Config.Current.Learning.ForceRelearnArchitectureFamily) + { + plan.ArchitectureFamilyWide = true; + } + + foreach (var raw in Config.Current.Learning.ForceRelearnStandardBaselines ?? []) + { + if (string.IsNullOrWhiteSpace(raw)) + continue; + + var baseline = BaselineQuants.ResolveBuiltInStandardBaseline(raw.Trim()) + ?? throw new InvalidOperationException($"Unknown standard baseline '{raw}' in learning.force_relearn_standard_baselines."); + + var definition = await db.BaselineQuantDefinitions.AsNoTracking().FirstOrDefaultAsync(x => + x.ArchitectureFamilyId == null && x.RuntimeBaselineId == baseline.UniqueId, ct); + + if (definition == null) + throw new InvalidOperationException($"SQLite baseline definition was not found for standard baseline '{raw}'."); + + plan.BaselineDefinitionIds.Add(definition.Id); + plan.TargetDescriptions.Add($"standard:{definition.DisplayName}"); + } + + foreach (var spec in resolvedCustomBaselines.Where(x => x.ForceRelearn)) + { + var definition = await db.BaselineQuantDefinitions.AsNoTracking().FirstOrDefaultAsync(x => + x.ArchitectureFamilyId == architectureFamilyId && + x.RuntimeBaselineId == spec.DynamicBaselineId, ct); + + if (definition == null) + throw new InvalidOperationException($"Custom include requested force_relearn, but no DB definition was found for '{spec.RepoId}/{spec.SourceFileName}'."); + + plan.BaselineDefinitionIds.Add(definition.Id); + plan.TargetDescriptions.Add($"custom:{definition.SourceRepository}/{definition.SourceFileName}"); + } + + if (!plan.HasTargets) + return; + + await PopulateCountsAsync(db, plan, ct); + PrintPlan(plan); + + if (!AnsiConsole.Confirm("Apply this targeted destructive relearn plan?", defaultValue: false)) + { + throw new OperationCanceledException("Targeted relearn was declined by the user. Aborting before any destructive changes."); + } + + await ExecuteAsync(db, plan, ct); + AnsiConsole.MarkupLine("[green]Targeted relearn cleanup complete.[/] The affected truth will be regenerated by this run."); + } + + private static async Task PopulateCountsAsync(MagicQuantContext db, TargetedRelearnPlan plan, CancellationToken ct) + { + IQueryable benchmarkQuery = BuildAffectedBenchmarkQuery(db, plan); + var benchmarkIds = await benchmarkQuery.Select(x => x.Id).Distinct().ToListAsync(ct); + + plan.AiBenchmarkRows = benchmarkIds.Count; + plan.CategoryBenchmarkRows = await db.Set().CountAsync(x => benchmarkIds.Contains(x.AiBenchmarkId), ct); + plan.BenchmarkRunRows = await db.BenchmarkRuns.CountAsync(x => benchmarkIds.Contains(x.AiBenchmarkId), ct); + plan.QuantizationRunRowsToDetach = await db.QuantizationRuns.CountAsync(x => x.AiBenchmarkId != null && benchmarkIds.Contains(x.AiBenchmarkId.Value), ct); + plan.AiBenchmarkLearnedSourceRows = await db.AiBenchmarkLearnedSources.CountAsync(x => benchmarkIds.Contains(x.AiBenchmarkId), ct); + + if (plan.ArchitectureFamilyWide) + { + plan.LearnedBaselineTensorQuantRows = await db.LearnedBaselineTensorQuants + .CountAsync(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId, ct); + plan.ExecutionPlanProbeCacheRows = await db.ExecutionPlanProbeCaches + .CountAsync(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId, ct); + } + else + { + plan.LearnedBaselineTensorQuantRows = await db.LearnedBaselineTensorQuants + .CountAsync(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId && + x.TensorGroupProfileId == plan.TensorGroupProfileId && + plan.BaselineDefinitionIds.Contains(x.BaselineQuantDefinitionId), ct); + plan.ExecutionPlanProbeCacheRows = await db.ExecutionPlanProbeCaches + .CountAsync(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId && + x.TensorGroupProfileId == plan.TensorGroupProfileId, ct); + } + } + + private static void PrintPlan(TargetedRelearnPlan plan) + { + AnsiConsole.Write(new Rule("[red]Targeted Relearn Deletion Plan[/]") { Justification = Justify.Left }); + if (plan.ArchitectureFamilyWide) + AnsiConsole.MarkupLine("[yellow]Scope:[/] active architecture family, all tensor group profiles"); + else + AnsiConsole.MarkupLine($"[yellow]Scope:[/] active architecture family + tensor group profile id [cyan]{plan.TensorGroupProfileId}[/]"); + + foreach (var target in plan.TargetDescriptions.Distinct(StringComparer.OrdinalIgnoreCase).OrderBy(x => x)) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(target)}[/]"); + + AnsiConsole.MarkupLine($"[red]LearnedBaselineTensorQuant rows:[/] {plan.LearnedBaselineTensorQuantRows:N0}"); + AnsiConsole.MarkupLine($"[red]AiBenchmark rows:[/] {plan.AiBenchmarkRows:N0}"); + AnsiConsole.MarkupLine($"[red]CategoryBenchmark rows:[/] {plan.CategoryBenchmarkRows:N0}"); + AnsiConsole.MarkupLine($"[red]BenchmarkRun rows:[/] {plan.BenchmarkRunRows:N0}"); + AnsiConsole.MarkupLine($"[yellow]QuantizationRun rows to detach:[/] {plan.QuantizationRunRowsToDetach:N0}"); + AnsiConsole.MarkupLine($"[red]AiBenchmarkLearnedSource rows:[/] {plan.AiBenchmarkLearnedSourceRows:N0}"); + AnsiConsole.MarkupLine($"[red]ExecutionPlanProbeCache rows:[/] {plan.ExecutionPlanProbeCacheRows:N0}"); + } + + private static async Task ExecuteAsync(MagicQuantContext db, TargetedRelearnPlan plan, CancellationToken ct) + { + await using var transaction = await db.Database.BeginTransactionAsync(ct); + + IQueryable benchmarkQuery = BuildAffectedBenchmarkQuery(db, plan); + var benchmarkIds = await benchmarkQuery.Select(x => x.Id).Distinct().ToListAsync(ct); + + if (benchmarkIds.Count > 0) + { + await db.QuantizationRuns + .Where(x => x.AiBenchmarkId != null && benchmarkIds.Contains(x.AiBenchmarkId.Value)) + .ExecuteUpdateAsync(x => x.SetProperty(r => r.AiBenchmarkId, (Guid?)null), ct); + + await db.AiBenchmarks + .Where(x => benchmarkIds.Contains(x.Id)) + .ExecuteDeleteAsync(ct); + } + + if (plan.ArchitectureFamilyWide) + { + await db.LearnedBaselineTensorQuants + .Where(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId) + .ExecuteDeleteAsync(ct); + + await db.ExecutionPlanProbeCaches + .Where(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId) + .ExecuteDeleteAsync(ct); + } + else + { + await db.LearnedBaselineTensorQuants + .Where(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId && + x.TensorGroupProfileId == plan.TensorGroupProfileId && + plan.BaselineDefinitionIds.Contains(x.BaselineQuantDefinitionId)) + .ExecuteDeleteAsync(ct); + + await db.ExecutionPlanProbeCaches + .Where(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId && + x.TensorGroupProfileId == plan.TensorGroupProfileId) + .ExecuteDeleteAsync(ct); + } + + await transaction.CommitAsync(ct); + } + + private static IQueryable BuildAffectedBenchmarkQuery(MagicQuantContext db, TargetedRelearnPlan plan) + { + if (plan.ArchitectureFamilyWide) + return db.AiBenchmarks.Where(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId); + + var pureBaselineRuntimeIds = db.BaselineQuantDefinitions + .Where(x => plan.BaselineDefinitionIds.Contains(x.Id)) + .Select(x => x.RuntimeBaselineId) + .ToList(); + + var pureBaselineBenchmarks = db.AiBenchmarks + .Include(x => x.TensorCombo) + .Where(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId && + x.TensorGroupProfileId == plan.TensorGroupProfileId && + pureBaselineRuntimeIds.Contains(x.TensorCombo.BaseQuant) && + x.TensorCombo.Embeddings == 0 && x.TensorCombo.LmHead == 0 && + x.TensorCombo.AttnQ == 0 && x.TensorCombo.AttnKV == 0 && + x.TensorCombo.AttnOutput == 0 && x.TensorCombo.FfnUpGate == 0 && + x.TensorCombo.FfnDown == 0 && x.TensorCombo.MoeExperts == 0 && + x.TensorCombo.MoeRouter == 0); + + var dependentHybridBenchmarkIds = db.AiBenchmarkLearnedSources + .Where(x => x.ArchitectureFamilyId == plan.ArchitectureFamilyId && + x.TensorGroupProfileId == plan.TensorGroupProfileId && + plan.BaselineDefinitionIds.Contains(x.BaselineQuantDefinitionId)) + .Select(x => x.AiBenchmarkId); + + var dependentHybridBenchmarks = db.AiBenchmarks + .Where(x => dependentHybridBenchmarkIds.Contains(x.Id)); + + return pureBaselineBenchmarks.Concat(dependentHybridBenchmarks); + } + + private sealed class TargetedRelearnPlan + { + public TargetedRelearnPlan(int architectureFamilyId, int tensorGroupProfileId) + { + ArchitectureFamilyId = architectureFamilyId; + TensorGroupProfileId = tensorGroupProfileId; + } + + public int ArchitectureFamilyId { get; } + public int TensorGroupProfileId { get; } + public bool ArchitectureFamilyWide { get; set; } + public HashSet BaselineDefinitionIds { get; } = new(); + public List TargetDescriptions { get; } = new(); + public bool HasTargets => ArchitectureFamilyWide || BaselineDefinitionIds.Count > 0; + public int LearnedBaselineTensorQuantRows { get; set; } + public int AiBenchmarkRows { get; set; } + public int CategoryBenchmarkRows { get; set; } + public int BenchmarkRunRows { get; set; } + public int QuantizationRunRowsToDetach { get; set; } + public int AiBenchmarkLearnedSourceRows { get; set; } + public int ExecutionPlanProbeCacheRows { get; set; } + } +} diff --git a/MagicQuant/Services/TensorGroupProfileService.cs b/MagicQuant/Services/TensorGroupProfileService.cs new file mode 100644 index 0000000..7e0eb53 --- /dev/null +++ b/MagicQuant/Services/TensorGroupProfileService.cs @@ -0,0 +1,104 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Encodings.Web; +using System.Text.Json; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class TensorGroupProfileService +{ + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = false, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping + }; + + public async Task EnsureCurrentProfileAsync(CancellationToken ct = default) + { + int architectureFamilyId = Cache.CurrentArchitectureFamilyId + ?? throw new InvalidOperationException("Architecture family must be resolved before resolving tensor group profile."); + + string snapshotJson = BuildSnapshotJson(); + string fingerprint = ComputeSha256(snapshotJson); + + await using var db = new MagicQuantContext(); + + var existing = await db.TensorGroupProfiles + .FirstOrDefaultAsync(x => x.ArchitectureFamilyId == architectureFamilyId && x.FingerprintHash == fingerprint, ct); + + if (existing == null) + { + existing = new TensorGroupProfile + { + ArchitectureFamilyId = architectureFamilyId, + FingerprintHash = fingerprint, + SnapshotJson = snapshotJson, + CreatedUtc = DateTime.UtcNow, + IsActive = true + }; + db.TensorGroupProfiles.Add(existing); + } + + var activeProfiles = await db.TensorGroupProfiles + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && x.Id != existing.Id && x.IsActive) + .ToListAsync(ct); + + foreach (var profile in activeProfiles) + profile.IsActive = false; + + existing.IsActive = true; + await db.SaveChangesAsync(ct); + + Cache.CurrentTensorGroupProfileId = existing.Id; + Cache.CurrentTensorGroupProfileFingerprintHash = existing.FingerprintHash; + + AnsiConsole.MarkupLine($"[green]Tensor group profile active:[/] id=[cyan]{existing.Id}[/] hash=[grey]{Markup.Escape(existing.FingerprintHash[..Math.Min(12, existing.FingerprintHash.Length)])}[/]"); + return existing; + } + + public static int RequireCurrentProfileId() => + Cache.CurrentTensorGroupProfileId + ?? throw new InvalidOperationException("Current tensor group profile is not set. Call TensorGroupProfileService.EnsureCurrentProfileAsync after architecture-family resolution."); + + public static int RequireCurrentArchitectureFamilyId() => + Cache.CurrentArchitectureFamilyId + ?? throw new InvalidOperationException("Current architecture family is not set."); + + public static string BuildSnapshotJson() + { + var snapshot = new + { + schema = 1, + groups = TReg.All + .OrderBy(x => x.UniqueId) + .Select(x => new + { + id = x.UniqueId, + name = x.Name, + patterns = x.Tensors + .Where(p => !string.IsNullOrWhiteSpace(p)) + .Select(p => p.Trim()) + .ToArray() + }) + .ToArray(), + baseQuantExceptions = TReg.GetBaseQuantExceptionPatterns() + .Where(p => !string.IsNullOrWhiteSpace(p)) + .Select(p => p.Trim()) + .ToArray() + }; + + return JsonSerializer.Serialize(snapshot, JsonOptions); + } + + private static string ComputeSha256(string value) + { + using var sha = SHA256.Create(); + return Convert.ToHexString(sha.ComputeHash(Encoding.UTF8.GetBytes(value))).ToLowerInvariant(); + } +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 3874918..65cc9c0 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -48,9 +48,6 @@ flags: # Force rebuilding the active imatrix artifact even if one already exists. force_imatrix_rebuild: false - # Force relearning baseline tensor mappings even if learned truth already exists in SQLite. - force_relearn_baseline_tensor_mappings: false - # Force rerunning hardware execution-plan probing. force_refresh_hardware_probe: false @@ -58,6 +55,26 @@ flags: # are allowed in hybrid generation logic. allow_high_precision_hybrids: false +learning: + # Destructive relearn options are intentionally targeted. + # These are transient runtime commands and are not persisted as DB state. + # When any option below is enabled, MagicQuant prints a count summary and asks + # for confirmation before deleting/relearning anything. + # + # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, + # and execution probe cache rows scoped to the active architecture family. + # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, + # TensorCombo, or BaselineQuantDefinition rows. + force_relearn_architecture_family: false + + # Relearn built-in/standard baselines by display/canonical name for the current + # architecture family and active tensor group profile. + # Example: + # force_relearn_standard_baselines: + # - Q6_K + # - IQ4_XS + force_relearn_standard_baselines: [] + readme: # Optional title model name override used in: # # MagicQuant Hybrids (v2.0) - @@ -312,6 +329,9 @@ baselines: # baseline_family: Q4_K_M # quantize_base_name: Q4_K_M # display_name: UD_Q4_K_XL + # # Transient command only. Do not store as DB truth. + # # Deletes/relearns only this custom baseline under the active architecture family + tensor group profile. + # force_relearn: false # allow_as_learning_baseline: true # allow_as_combination_carrier: true # allow_as_explicit_group_candidate: true @@ -322,6 +342,7 @@ baselines: # baseline_family: Q5_K # quantize_base_name: Q5_K # display_name: UD_Q5_K_XL + # force_relearn: false # allow_as_learning_baseline: true # allow_as_combination_carrier: true # allow_as_explicit_group_candidate: true @@ -330,6 +351,7 @@ baselines: # baseline_family: Q6_K # quantize_base_name: Q6_K # display_name: UD_Q6_K_XL + # force_relearn: false # allow_as_learning_baseline: true # allow_as_combination_carrier: true # allow_as_explicit_group_candidate: true @@ -338,6 +360,7 @@ baselines: # baseline_family: IQ3_S # quantize_base_name: IQ3_S # display_name: UD_Q3_K_XL + # force_relearn: false # allow_as_learning_baseline: true # allow_as_combination_carrier: false # allow_as_explicit_group_candidate: true @@ -345,4 +368,4 @@ baselines: # # Example note: # # If the repo does not actually contain IQ3_XS, do not reference it. # # Use only filenames that truly exist in the repository. - [] \ No newline at end of file + [] diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 4fa01f2..dbd9315 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-35B-A3B-Qwen/ + model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ llama_root: llama_bin: convert_script: @@ -13,15 +13,34 @@ paths: flags: use_imatrix: true force_imatrix_rebuild: false - force_relearn_baseline_tensor_mappings: false force_refresh_hardware_probe: false allow_high_precision_hybrids: false +learning: + # Destructive relearn options are intentionally targeted. + # These are transient runtime commands and are not persisted as DB state. + # When any option below is enabled, MagicQuant prints a count summary and asks + # for confirmation before deleting/relearning anything. + # + # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, + # and execution probe cache rows scoped to the active architecture family. + # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, + # TensorCombo, or BaselineQuantDefinition rows. + force_relearn_architecture_family: false + + # Relearn built-in/standard baselines by display/canonical name for the current + # architecture family and active tensor group profile. + # Example: + # force_relearn_standard_baselines: + # - Q6_K + # - IQ4_XS + force_relearn_standard_baselines: [] + readme: # Optional title model name override used in: # # MagicQuant Hybrids (v2.0) - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: + title_model_name_override: Qwen3.6-27B # Hugging Face README frontmatter. # Scalars render as: @@ -40,7 +59,7 @@ readme: - magicquant - conversational base_model: - - Qwen/Qwen3.6-35B-A3B + - Qwen/Qwen3.6-27B hardware: gpu_memory_limits_gb: @@ -134,8 +153,8 @@ candidate_selection: output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: - output_name_prefix: Qwen3.6-35B-A3B - export_external_learned_baselines: false + output_name_prefix: Qwen3.6-27B + export_external_learned_baselines: true # false = normal behavior; delete/rebuild final outputs from scratch. # true = preserve valid existing GGUFs and skip rebuilding them only when @@ -147,7 +166,7 @@ output: # See candidate_selection above for the active final chooser settings. identity: - architecture_family_name: Qwen3.6-35B-A3B + architecture_family_name: Qwen3.6-27B allow_architecture_family_alias_override: false baselines: @@ -157,7 +176,7 @@ baselines: enabled_standard_explicit_group_candidates: [] custom_repositories: - - repo_id: unsloth/Qwen3.6-35B-A3B-GGUF + - repo_id: unsloth/Qwen3.6-27B-GGUF enabled: true short_source_name: Unsloth source_kind: huggingface_gguf_repository @@ -172,155 +191,74 @@ baselines: includes: - - - file_name: Qwen3.6-35B-A3B-UD-IQ2_M.gguf + - file_name: Qwen3.6-27B-UD-IQ2_M.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-IQ2_M + force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-IQ2_XXS.gguf + - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf baseline_family: IQ2_XXS quantize_base_name: IQ2_XXS display_name: UD-IQ2_XXS + force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-IQ3_S.gguf - baseline_family: IQ3_S - quantize_base_name: IQ3_S - display_name: UD-IQ3_S - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-IQ3_XXS.gguf + - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf baseline_family: IQ3_XXS quantize_base_name: IQ3_XXS display_name: UD-IQ3_XXS + force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-IQ4_NL.gguf - baseline_family: IQ4_NL - quantize_base_name: IQ4_NL - display_name: UD-IQ4_NL - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-IQ4_NL_XL.gguf - baseline_family: IQ4_NL - quantize_base_name: IQ4_NL - display_name: UD-IQ4_NL_XL - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-IQ4_XS.gguf - baseline_family: IQ4_XS - quantize_base_name: IQ4_XS - display_name: UD-IQ4_XS - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q2_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-Q2_K_XL + force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-Q3_K_M.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_M - display_name: UD-Q3_K_M - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q3_K_S.gguf - baseline_family: IQ3_S - quantize_base_name: IQ3_S - display_name: UD-Q3_K_S - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q3_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf baseline_family: IQ3_M quantize_base_name: IQ3_M display_name: UD-Q3_K_XL + force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: true allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-Q4_K_M.gguf - baseline_family: Q4_K_M - quantize_base_name: Q4_K_M - display_name: UD-Q4_K_M - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q4_K_S.gguf - baseline_family: Q4_K_S - quantize_base_name: Q4_K_S - display_name: UD-Q4_K_S - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M display_name: UD-Q4_K_XL + force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: true allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-Q5_K_M.gguf - baseline_family: Q5_K - quantize_base_name: Q5_K - display_name: UD-Q5_K_M - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q5_K_S.gguf - baseline_family: Q5_K_S - quantize_base_name: Q5_K_S - display_name: UD-Q5_K_S - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q5_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf baseline_family: Q5_K quantize_base_name: Q5_K display_name: UD-Q5_K_XL + force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-Q6_K.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: UD-Q6_K - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q6_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf baseline_family: Q6_K quantize_base_name: Q6_K display_name: UD-Q6_K_XL + force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: true - allow_as_explicit_group_candidate: true \ No newline at end of file + allow_as_explicit_group_candidate: true From 4d9342cf2ccc8e90de7f1ee8edc339e3c189cdec Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 2 May 2026 13:33:39 -0400 Subject: [PATCH 181/258] config update --- MagicQuant/config.default.yaml | 21 +++++++++++++++++++++ MagicQuant/config.dev.yaml | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 65cc9c0..6ae2998 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -75,6 +75,27 @@ learning: # - IQ4_XS force_relearn_standard_baselines: [] + # Safety gate for tensor group regex/profile changes. After MagicQuant reads the + # native BF16 GGUF tensor list, it prints group counts, example tensors, + # ambiguous matches, unresolved tensors, and base-quant exception counts, then + # asks before continuing. Keep this true unless running fully unattended. + confirm_tensor_group_profile: true + + # Transient repair mode for accidental regex mistakes. + # + # When true, MagicQuant tries to rebuild learned tensor mappings for the active + # TensorGroupProfile from older DB truth in the same architecture family. This + # lets a regex-only regrouping avoid needless re-download/re-quantization of + # pure learning baselines where per-tensor truth already exists. + # + # CLI equivalent: + # --rebucket-learned-tensor-groups + # + # This does not globally delete old profile truth. Old benchmarks/learned rows + # stay attached to their original TensorGroupProfile and are simply ignored + # unless that exact profile becomes active again. + rebucket_learned_tensor_groups_from_existing_truth: false + readme: # Optional title model name override used in: # # MagicQuant Hybrids (v2.0) - diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index dbd9315..b5fb251 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -35,6 +35,28 @@ learning: # - Q6_K # - IQ4_XS force_relearn_standard_baselines: [] + + # Safety gate for tensor group regex/profile changes. After MagicQuant reads the + # native BF16 GGUF tensor list, it prints group counts, example tensors, + # ambiguous matches, unresolved tensors, and base-quant exception counts, then + # asks before continuing. Keep this true unless running fully unattended. + confirm_tensor_group_profile: true + + # Transient repair mode for accidental regex mistakes. + # + # When true, MagicQuant tries to rebuild learned tensor mappings for the active + # TensorGroupProfile from older DB truth in the same architecture family. This + # lets a regex-only regrouping avoid needless re-download/re-quantization of + # pure learning baselines where per-tensor truth already exists. + # + # CLI equivalent: + # --rebucket-learned-tensor-groups + # + # This does not globally delete old profile truth. Old benchmarks/learned rows + # stay attached to their original TensorGroupProfile and are simply ignored + # unless that exact profile becomes active again. + rebucket_learned_tensor_groups_from_existing_truth: false + readme: # Optional title model name override used in: From 72704509f0f5d4721932cf94e348c1e2e3d2e4ba Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 2 May 2026 13:42:41 -0400 Subject: [PATCH 182/258] yaml update --- MagicQuant/config.default.yaml | 27 ++++++++++++++++----------- MagicQuant/config.dev.yaml | 27 ++++++++++++++++----------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 6ae2998..ea13294 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -81,20 +81,25 @@ learning: # asks before continuing. Keep this true unless running fully unattended. confirm_tensor_group_profile: true - # Transient repair mode for accidental regex mistakes. + # Safe/idempotent repair mode for accidental regex mistakes. # - # When true, MagicQuant tries to rebuild learned tensor mappings for the active - # TensorGroupProfile from older DB truth in the same architecture family. This - # lets a regex-only regrouping avoid needless re-download/re-quantization of - # pure learning baselines where per-tensor truth already exists. + # Default true: on every run MagicQuant checks whether older DB learned tensor + # truth can be copied into the active TensorGroupProfile by reapplying the + # current regex/base_quant_exceptions rules. If nothing changed or current rows + # already exist, it skips cleanly and does not create duplicates. # - # CLI equivalent: - # --rebucket-learned-tensor-groups + # This avoids needless re-download/re-quantization of pure learning baselines + # after regex-only regrouping. Old benchmarks/learned rows remain attached to + # their original TensorGroupProfile and are ignored unless that profile becomes + # active again. # - # This does not globally delete old profile truth. Old benchmarks/learned rows - # stay attached to their original TensorGroupProfile and are simply ignored - # unless that exact profile becomes active again. - rebucket_learned_tensor_groups_from_existing_truth: false + # Disable only when you intentionally want the slower/full path to regenerate + # learned grouping truth instead of rebucketing from DB snapshots. + # CLI disable aliases: + # --no-rebucket-learned-tensor-groups + # --disable-tensor-group-rebucket + # --full-relearn-tensor-groups + rebucket_learned_tensor_groups_from_existing_truth: true readme: # Optional title model name override used in: diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index b5fb251..6b70370 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -42,20 +42,25 @@ learning: # asks before continuing. Keep this true unless running fully unattended. confirm_tensor_group_profile: true - # Transient repair mode for accidental regex mistakes. + # Safe/idempotent repair mode for accidental regex mistakes. # - # When true, MagicQuant tries to rebuild learned tensor mappings for the active - # TensorGroupProfile from older DB truth in the same architecture family. This - # lets a regex-only regrouping avoid needless re-download/re-quantization of - # pure learning baselines where per-tensor truth already exists. + # Default true: on every run MagicQuant checks whether older DB learned tensor + # truth can be copied into the active TensorGroupProfile by reapplying the + # current regex/base_quant_exceptions rules. If nothing changed or current rows + # already exist, it skips cleanly and does not create duplicates. # - # CLI equivalent: - # --rebucket-learned-tensor-groups + # This avoids needless re-download/re-quantization of pure learning baselines + # after regex-only regrouping. Old benchmarks/learned rows remain attached to + # their original TensorGroupProfile and are ignored unless that profile becomes + # active again. # - # This does not globally delete old profile truth. Old benchmarks/learned rows - # stay attached to their original TensorGroupProfile and are simply ignored - # unless that exact profile becomes active again. - rebucket_learned_tensor_groups_from_existing_truth: false + # Disable only when you intentionally want the slower/full path to regenerate + # learned grouping truth instead of rebucketing from DB snapshots. + # CLI disable aliases: + # --no-rebucket-learned-tensor-groups + # --disable-tensor-group-rebucket + # --full-relearn-tensor-groups + rebucket_learned_tensor_groups_from_existing_truth: true readme: From 5ce965a611621b0387ff422f6b061f4070f7a617 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 2 May 2026 13:47:32 -0400 Subject: [PATCH 183/258] Fixed hardware probing not cached. And re-bucket logic that's now defaulted on. AKA better fixing logic if the system learned incorrectly. Added a pause and continue point to validate groups as well before the run begins. --- MQ.DB/Cache.cs | 14 + MagicQuant/Commands/Evolution.cs | 18 + .../Configuration/MagicQuantYamlConfig.cs | 13 + .../Configuration/MagicQuantYamlLoader.cs | 18 + MagicQuant/Services/BenchmarkService.cs | 25 +- .../Services/TensorGroupProfileService.cs | 6 +- .../Services/TensorGroupRebucketService.cs | 319 ++++++++++++++++++ .../Services/TensorGroupReviewService.cs | 138 ++++++++ 8 files changed, 544 insertions(+), 7 deletions(-) create mode 100644 MagicQuant/Services/TensorGroupRebucketService.cs create mode 100644 MagicQuant/Services/TensorGroupReviewService.cs diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index 60c3a3f..c8b21f4 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -93,6 +93,20 @@ public enum MainTorchType public static string? CurrentTensorGroupProfileFingerprintHash { get; set; } + /// + /// When true, MagicQuant prints the BF16/native tensor grouping summary and asks + /// for confirmation before any tensor-group-scoped learning/search work continues. + /// + public static bool ConfirmTensorGroupProfile { get; set; } = true; + + /// + /// Transient repair mode for regex/profile mistakes. When true, MagicQuant tries + /// to rebuild learned tensor mappings for the active TensorGroupProfile from + /// existing family/profile truth instead of redownloading/requantizing pure + /// learning baselines just to rediscover per-tensor truth. + /// + public static bool RebucketLearnedTensorGroupsFromExistingTruth { get; set; } = true; + public static bool ForceRefreshHardwareProbe { get; set; } public static bool UseImatrix { get; set; } diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 88ec015..b87fe36 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -84,6 +84,8 @@ public async Task Run(List args) AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length:N0}[/] safe tensors"); + AnsiConsole.MarkupLine($"Tensor Review: [cyan]{(Cache.ConfirmTensorGroupProfile ? "prompt" : "skip prompt")}[/]"); + AnsiConsole.MarkupLine($"Regex Rebucket: [cyan]{(Cache.RebucketLearnedTensorGroupsFromExistingTruth ? "enabled" : "disabled")}[/]"); if (string.IsNullOrEmpty(Cache.LlamaBin)) AnsiConsole.MarkupLine("[yellow]Warning:[/] Llama binaries path not set in Cache. (Did Initialization run?)"); @@ -106,6 +108,14 @@ public async Task Run(List args) var sidecarService = new ModelSidecarArtifactService(pyManager); await sidecarService.EnsureMmprojArtifactAvailableAsync(); + // Review the active regex profile against the native/BF16 tensor list before + // architecture/profile-scoped learning truth is persisted or reused. This is + // the early "do these groups look sane?" gate for catching YAML regex mistakes. + await new TensorGroupReviewService().ReviewNativeTensorGroupingAsync( + quantizationService: quantizationService, + nativeGgufPath: bf16ModelGgufPath, + requireConfirmation: Cache.ConfirmTensorGroupProfile); + var architectureFamilyService = new ArchitectureFamilyService(pyManager); await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(bf16ModelGgufPath); @@ -154,6 +164,11 @@ public async Task Run(List args) // cannot accidentally inherit a stale default. RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); + if (Cache.RebucketLearnedTensorGroupsFromExistingTruth) + { + await new TensorGroupRebucketService().RebucketFromExistingProfileTruthAsync(); + } + string baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && await benchmarkService.TryInitializeDynamicExecutionPlanFromCacheAsync( @@ -743,6 +758,9 @@ private void ShowEvolutionHelp() AnsiConsole.MarkupLine(" [green]--reuse-existing-final-artifacts[/] Reuse valid final GGUFs only when exact file name + benchmark byte size match (Optional; default false)"); AnsiConsole.MarkupLine(" [green]--allow-eight-bit-anchor-replacements[/] Permit final prediction to try replacing 8-bit anchors like Q8_0 (Optional; default false)"); AnsiConsole.MarkupLine(" [green]--export-external-learned-baselines[/] Also locally rebuild/export pure learned external baselines such as Unsloth (Optional; default false)"); + AnsiConsole.MarkupLine(" [green]--rebucket-learned-tensor-groups[/] Compatibility alias; regex rebucketing from DB is enabled by default"); + AnsiConsole.MarkupLine(" [green]--no-rebucket-learned-tensor-groups[/] Disable safe DB rebucketing and force the slower/full learned-group path instead"); + AnsiConsole.MarkupLine(" [green]--skip-tensor-group-confirm[/] Skip the native BF16 tensor-group review confirmation prompt for unattended runs (Optional; YAML default true asks)"); AnsiConsole.MarkupLine(" [green]--selection-max-candidates-per-interior-window[/] Candidate count retained per interior window (Optional; default = 1)"); AnsiConsole.MarkupLine(" [green]--config[/] Path to YAML runtime config. CLI flags override YAML values."); AnsiConsole.WriteLine(); diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 0b84809..f662a49 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -251,6 +251,19 @@ public sealed class RuntimeLearningConfig { public bool ForceRelearnArchitectureFamily { get; set; } public List ForceRelearnStandardBaselines { get; set; } = new(); + + /// + /// Safety gate for regex/profile mistakes. When true, the evolution run prints + /// native BF16 tensor-group counts and asks before continuing. + /// + public bool ConfirmTensorGroupProfile { get; set; } = true; + + /// + /// Transient repair command for regex changes. Rebuilds learned tensor/group + /// rows for the active profile from existing DB truth where possible, avoiding + /// needless re-download/re-quantization of pure baseline learning artifacts. + /// + public bool RebucketLearnedTensorGroupsFromExistingTruth { get; set; } = true; } public sealed class RuntimeBaselineConfig diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index cdfc395..d77e24b 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -67,9 +67,13 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) Directory.CreateDirectory(Cache.MagicQuantDirectory!); + config.Learning ??= new RuntimeLearningConfig(); + Cache.UseImatrix = config.Flags.UseImatrix; Cache.ForceImatrixRebuild = config.Flags.ForceImatrixRebuild; Cache.ForceRefreshHardwareProbe = config.Flags.ForceRefreshHardwareProbe; + Cache.ConfirmTensorGroupProfile = config.Learning.ConfirmTensorGroupProfile; + Cache.RebucketLearnedTensorGroupsFromExistingTruth = config.Learning.RebucketLearnedTensorGroupsFromExistingTruth; config.Hardware.GpuMemoryLimitsGb ??= new Dictionary(); config.Hardware.GpuMemoryLimitsGb = config.Hardware.GpuMemoryLimitsGb @@ -88,6 +92,8 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) config.Identity.AllowArchitectureFamilyAliasOverride; Cache.CurrentArchitectureFamilyId = null; + Cache.CurrentTensorGroupProfileId = null; + Cache.CurrentTensorGroupProfileFingerprintHash = null; config.Output.OutputDir = string.IsNullOrWhiteSpace(config.Output.OutputDir) ? null @@ -204,6 +210,8 @@ private static void RejectLegacyGlobalRelearnYaml(string yaml, string configPath private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList args) { + config.Learning ??= new RuntimeLearningConfig(); + string? Get(string name) => args.FirstOrDefault(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase))?.Value; bool Has(string name) => args.Any(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase)); @@ -218,6 +226,16 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList throw new InvalidOperationException("--relearn-baseline-mappings was removed because it globally wiped learned tensor truth. Use YAML learning.force_relearn_architecture_family, learning.force_relearn_standard_baselines, or custom_repositories/includes/force_relearn instead."); if (Has("recheck-hardware-probe") || Has("force-refresh-hardware-probe") || Has("force_refresh_hardware_probe")) config.Flags.ForceRefreshHardwareProbe = true; if (Has("allow-high-precision-hybrids")) config.Flags.AllowHighPrecisionHybrids = true; + if (Has("rebucket-learned-tensor-groups") || Has("rebucket-tensor-groups-from-db") || Has("relearn-tensor-groups-from-db")) + { + // Kept as a harmless compatibility alias. Rebucket is now enabled by default + // because it is the safe/idempotent path after regex profile changes. + config.Learning.RebucketLearnedTensorGroupsFromExistingTruth = true; + } + if (Has("no-rebucket-learned-tensor-groups") || Has("disable-tensor-group-rebucket") || Has("full-relearn-tensor-groups")) + config.Learning.RebucketLearnedTensorGroupsFromExistingTruth = false; + if (Has("skip-tensor-group-confirm") || Has("yes-tensor-groups")) + config.Learning.ConfirmTensorGroupProfile = false; config.Imatrix.ImatrixUrl = Prefer(Get("imatrix-url"), config.Imatrix.ImatrixUrl); config.Imatrix.DatasetRepo = Prefer(Get("imatrix-dataset-repo"), config.Imatrix.DatasetRepo); diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 0dd69ff..242b801 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -545,21 +545,38 @@ private static BenchmarkSlot BuildAllGpuSlotFromSystemInfo() int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); - var row = await db.ExecutionPlanProbeCaches + // Hardware execution-plan probes are intentionally NOT invalidated by tensor grouping + // profile changes. Regex/profile edits change benchmark/learned-truth semantics, but the + // Q8/native hardware capability plan is still valid for the same architecture family, + // exact model hash, imatrix identity, quantized artifact fingerprint, hardware, and token + // target. Prefer a current-profile row when present, then fall back to the newest + // compatible row from any prior TensorGroupProfile. + var compatibleRows = await db.ExecutionPlanProbeCaches .AsNoTracking() - .FirstOrDefaultAsync(x => + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && - x.TensorGroupProfileId == tensorGroupProfileId && x.AiModelHashId == aiModelHashId && x.ImatrixDefinitionId == imatrixDefinitionId && x.HardwareFingerprint == key.HardwareFingerprint && x.QuantizedModelFingerprint == key.QuantizedModelFingerprint && x.QuantizationKey == key.QuantizationKey && - x.DiscoveryTokenTarget == key.DiscoveryTokenTarget, ct); + x.DiscoveryTokenTarget == key.DiscoveryTokenTarget) + .OrderByDescending(x => x.TensorGroupProfileId == tensorGroupProfileId) + .ThenByDescending(x => x.UpdatedUtc) + .ThenByDescending(x => x.CreatedUtc) + .ToListAsync(ct); + + var row = compatibleRows.FirstOrDefault(); if (row == null) return null; + if (row.TensorGroupProfileId != tensorGroupProfileId) + { + AnsiConsole.MarkupLine( + $"[grey]Execution-plan cache reused from prior tensor profile {row.TensorGroupProfileId}; hardware probe cache is profile-compatible.[/]"); + } + if (row.ProbeSchemaVersion < DynamicProbeSchemaVersion) { AnsiConsole.MarkupLine("[yellow]Execution-plan cache row uses old probe schema; re-probing.[/]"); diff --git a/MagicQuant/Services/TensorGroupProfileService.cs b/MagicQuant/Services/TensorGroupProfileService.cs index 7e0eb53..dc75414 100644 --- a/MagicQuant/Services/TensorGroupProfileService.cs +++ b/MagicQuant/Services/TensorGroupProfileService.cs @@ -25,7 +25,7 @@ public async Task EnsureCurrentProfileAsync(CancellationToke ?? throw new InvalidOperationException("Architecture family must be resolved before resolving tensor group profile."); string snapshotJson = BuildSnapshotJson(); - string fingerprint = ComputeSha256(snapshotJson); + string fingerprint = ComputeSnapshotHash(snapshotJson); await using var db = new MagicQuantContext(); @@ -96,9 +96,9 @@ public static string BuildSnapshotJson() return JsonSerializer.Serialize(snapshot, JsonOptions); } - private static string ComputeSha256(string value) + public static string ComputeSnapshotHash(string snapshotJson) { using var sha = SHA256.Create(); - return Convert.ToHexString(sha.ComputeHash(Encoding.UTF8.GetBytes(value))).ToLowerInvariant(); + return Convert.ToHexString(sha.ComputeHash(Encoding.UTF8.GetBytes(snapshotJson))).ToLowerInvariant(); } } diff --git a/MagicQuant/Services/TensorGroupRebucketService.cs b/MagicQuant/Services/TensorGroupRebucketService.cs new file mode 100644 index 0000000..90e58ee --- /dev/null +++ b/MagicQuant/Services/TensorGroupRebucketService.cs @@ -0,0 +1,319 @@ +using MagicQuant.Models.Learning; +using MagicQuant.Services.Learning; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class TensorGroupRebucketService +{ + private const byte UnknownTensorGroupId = 255; + private readonly TensorGroupingAuditService _auditService = new(); + + public async Task RebucketFromExistingProfileTruthAsync(CancellationToken ct = default) + { + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int currentProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + + await using var db = new MagicQuantContext(); + + var sourceProfiles = await db.TensorGroupProfiles + .AsNoTracking() + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && x.Id != currentProfileId) + .OrderByDescending(x => x.CreatedUtc) + .ThenByDescending(x => x.Id) + .Select(x => new { x.Id, x.FingerprintHash, x.CreatedUtc }) + .ToListAsync(ct); + + if (sourceProfiles.Count == 0) + { + AnsiConsole.MarkupLine("[grey]Tensor-group rebucket requested, but no previous tensor group profiles exist for this architecture family.[/]"); + return TensorGroupRebucketSummary.Empty; + } + + uint scopedModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); + int? currentImatrixDefinitionId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync( + db, + scopedModelHashId, + createIfMissing: false, + ct); + + var candidateKeys = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && x.TensorGroupProfileId != currentProfileId) + .GroupBy(x => new { x.BaselineQuantDefinitionId, x.TensorWeightSchemeId }) + .Select(g => new + { + g.Key.BaselineQuantDefinitionId, + g.Key.TensorWeightSchemeId, + RowCount = g.Count(), + LatestProfileId = g.Max(x => x.TensorGroupProfileId) + }) + .ToListAsync(ct); + + if (candidateKeys.Count == 0) + { + AnsiConsole.MarkupLine("[grey]Tensor-group rebucket requested, but no previous learned tensor truth exists for this architecture family.[/]"); + return TensorGroupRebucketSummary.Empty; + } + + int copiedBaselines = 0; + int copiedRows = 0; + int clonedBenchmarks = 0; + int skippedExisting = 0; + int fatalSkipped = 0; + + AnsiConsole.Write(new Rule("[yellow]Tensor Group Rebucket From Existing Truth[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine("[grey]Rebuilding learned tensor/group rows for the active regex profile from prior DB truth. Pure baseline benchmark rows are cloned when available; group-override isolation truth is intentionally not cloned.[/]"); + + foreach (var key in candidateKeys + .OrderBy(x => x.BaselineQuantDefinitionId) + .ThenBy(x => x.TensorWeightSchemeId)) + { + bool alreadyExists = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .AnyAsync(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == currentProfileId && + x.BaselineQuantDefinitionId == key.BaselineQuantDefinitionId && + x.TensorWeightSchemeId == key.TensorWeightSchemeId, ct); + + if (alreadyExists) + { + skippedExisting++; + continue; + } + + var sourceProfileId = await PickBestSourceProfileForKeyAsync( + db, + architectureFamilyId, + currentProfileId, + key.BaselineQuantDefinitionId, + key.TensorWeightSchemeId, + sourceProfiles.Select(x => x.Id).ToList(), + ct); + + if (!sourceProfileId.HasValue) + continue; + + var sourceRows = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == sourceProfileId.Value && + x.BaselineQuantDefinitionId == key.BaselineQuantDefinitionId && + x.TensorWeightSchemeId == key.TensorWeightSchemeId) + .OrderBy(x => x.TensorName) + .ToListAsync(ct); + + if (sourceRows.Count == 0) + continue; + + var truth = sourceRows.ToDictionary( + x => x.TensorName, + x => new LearnedTensorTruth(x.TensorName, x.FinalQuantType, LearningSource.GgufOnly), + StringComparer.Ordinal); + + var audit = _auditService.Audit(truth.Keys.ToList(), truth); + if (audit.HasFatalIssues) + { + fatalSkipped++; + AnsiConsole.MarkupLine( + $"[red]Skipped rebucket for BaselineDefinitionId={key.BaselineQuantDefinitionId}, scheme={key.TensorWeightSchemeId}:[/] ambiguous={audit.Ambiguous.Count}, unresolved={audit.IllegalUnresolved.Count}. Fix tensor_groups.yaml first."); + continue; + } + + var targetBenchmarkId = await EnsurePureBaselineBenchmarkCloneAsync( + db, + sourceRows, + architectureFamilyId, + currentProfileId, + scopedModelHashId, + currentImatrixDefinitionId, + ct); + + if (targetBenchmarkId.Cloned) + clonedBenchmarks++; + + await db.LearnedBaselineTensorQuants + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == currentProfileId && + x.BaselineQuantDefinitionId == key.BaselineQuantDefinitionId && + x.TensorWeightSchemeId == key.TensorWeightSchemeId) + .ExecuteDeleteAsync(ct); + + var targetRows = sourceRows.Select(row => new LearnedBaselineTensorQuant + { + Id = Guid.NewGuid(), + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = currentProfileId, + BaselineQuantDefinitionId = row.BaselineQuantDefinitionId, + TensorComboId = row.TensorComboId, + AiBenchmarkId = targetBenchmarkId.BenchmarkId, + AiModelHashId = scopedModelHashId, + BaselineQuantId = row.BaselineQuantId, + BaselineCanonicalKey = row.BaselineCanonicalKey, + BaselineSourceKind = row.BaselineSourceKind, + BaselineSourceRepository = row.BaselineSourceRepository, + BaselineSourceFileName = row.BaselineSourceFileName, + TensorWeightSchemeId = row.TensorWeightSchemeId, + TensorGroupId = ResolveRebucketedTensorGroupId(row.TensorName, audit), + TensorName = row.TensorName, + FinalQuantType = row.FinalQuantType + }).ToList(); + + db.LearnedBaselineTensorQuants.AddRange(targetRows); + await db.SaveChangesAsync(ct); + + copiedBaselines++; + copiedRows += targetRows.Count; + + string baselineName = await db.BaselineQuantDefinitions + .AsNoTracking() + .Where(x => x.Id == key.BaselineQuantDefinitionId) + .Select(x => x.DisplayName) + .FirstOrDefaultAsync(ct) ?? key.BaselineQuantDefinitionId.ToString(); + + AnsiConsole.MarkupLine( + $"[green]Rebucketed learned truth:[/] {Markup.Escape(baselineName)} scheme={key.TensorWeightSchemeId} tensors={targetRows.Count:N0} fromProfile={sourceProfileId.Value} -> currentProfile={currentProfileId}"); + } + + var summary = new TensorGroupRebucketSummary + { + BaselineSchemeSetsCopied = copiedBaselines, + LearnedRowsCopied = copiedRows, + PureBenchmarkRowsCloned = clonedBenchmarks, + ExistingCurrentProfileSetsSkipped = skippedExisting, + FatalSetsSkipped = fatalSkipped + }; + + AnsiConsole.MarkupLine( + $"[green]Tensor-group rebucket summary:[/] baseline/scheme sets={summary.BaselineSchemeSetsCopied:N0}, rows={summary.LearnedRowsCopied:N0}, pure benchmarks cloned={summary.PureBenchmarkRowsCloned:N0}, already-current skipped={summary.ExistingCurrentProfileSetsSkipped:N0}, fatal skipped={summary.FatalSetsSkipped:N0}"); + + return summary; + } + + private static byte ResolveRebucketedTensorGroupId( + string tensorName, + TensorGroupingAuditResult audit) + { + if (!audit.GroupedByTensor.TryGetValue(tensorName, out var grouped)) + { + throw new InvalidOperationException( + $"Cannot rebucket tensor '{tensorName}' because it was not present in the active grouping audit."); + } + + if (grouped.PrimaryGroup != null) + return grouped.PrimaryGroup.UniqueId; + + if (grouped.IsBaseQuantException) + return UnknownTensorGroupId; + + throw new InvalidOperationException( + $"Cannot rebucket tensor '{tensorName}' because it is unresolved under the active tensor_groups.yaml profile. " + + "BaseQuant fallback is only allowed when the tensor matches base_quant_exceptions."); + } + + private static async Task PickBestSourceProfileForKeyAsync( + MagicQuantContext db, + int architectureFamilyId, + int currentProfileId, + int baselineDefinitionId, + byte tensorWeightSchemeId, + IReadOnlyList preferredProfileOrder, + CancellationToken ct) + { + foreach (var profileId in preferredProfileOrder) + { + bool exists = await db.LearnedBaselineTensorQuants + .AsNoTracking() + .AnyAsync(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == profileId && + x.TensorGroupProfileId != currentProfileId && + x.BaselineQuantDefinitionId == baselineDefinitionId && + x.TensorWeightSchemeId == tensorWeightSchemeId, ct); + if (exists) + return profileId; + } + + return null; + } + + private static async Task<(Guid BenchmarkId, bool Cloned)> EnsurePureBaselineBenchmarkCloneAsync( + MagicQuantContext db, + IReadOnlyList sourceRows, + int architectureFamilyId, + int currentProfileId, + uint scopedModelHashId, + int? currentImatrixDefinitionId, + CancellationToken ct) + { + var sourceBenchmarkId = sourceRows.Select(x => x.AiBenchmarkId).FirstOrDefault(x => x != Guid.Empty); + if (sourceBenchmarkId == Guid.Empty) + throw new InvalidOperationException("Cannot rebucket learned rows because the source AiBenchmarkId snapshot is missing."); + + var sourceBenchmark = await db.AiBenchmarks + .Include(x => x.CategorBenchmarks) + .AsNoTracking() + .FirstOrDefaultAsync(x => x.Id == sourceBenchmarkId, ct); + + if (sourceBenchmark == null) + throw new InvalidOperationException($"Cannot rebucket learned rows because source AiBenchmarkId={sourceBenchmarkId} was not found."); + + var target = await db.AiBenchmarks + .FirstOrDefaultAsync(x => x.ArchitectureFamilyId == architectureFamilyId && + x.TensorGroupProfileId == currentProfileId && + x.AiModelHashId == scopedModelHashId && + x.ImatrixDefinitionId == currentImatrixDefinitionId && + x.TensorComboId == sourceBenchmark.TensorComboId, ct); + + if (target != null) + return (target.Id, false); + + target = new AiBenchmark + { + Id = Guid.NewGuid(), + ArchitectureFamilyId = architectureFamilyId, + TensorGroupProfileId = currentProfileId, + AiModelHashId = scopedModelHashId, + ImatrixDefinitionId = currentImatrixDefinitionId, + TensorComboId = sourceBenchmark.TensorComboId, + Ngl = sourceBenchmark.Ngl, + SizeBytes = sourceBenchmark.SizeBytes, + TokensPerSecond = sourceBenchmark.TokensPerSecond + }; + + db.AiBenchmarks.Add(target); + await db.SaveChangesAsync(ct); + + if (sourceBenchmark.CategorBenchmarks.Count > 0) + { + db.Set().AddRange(sourceBenchmark.CategorBenchmarks.Select(x => new CategoryBenchmark + { + Id = Guid.NewGuid(), + AiBenchmarkId = target.Id, + Category = x.Category, + Kld = x.Kld, + Ppl = x.Ppl, + PplError = x.PplError + })); + + await db.SaveChangesAsync(ct); + } + + return (target.Id, true); + } +} + +public sealed class TensorGroupRebucketSummary +{ + public int BaselineSchemeSetsCopied { get; init; } + public int LearnedRowsCopied { get; init; } + public int PureBenchmarkRowsCloned { get; init; } + public int ExistingCurrentProfileSetsSkipped { get; init; } + public int FatalSetsSkipped { get; init; } + + public static TensorGroupRebucketSummary Empty { get; } = new(); +} diff --git a/MagicQuant/Services/TensorGroupReviewService.cs b/MagicQuant/Services/TensorGroupReviewService.cs new file mode 100644 index 0000000..b6651bb --- /dev/null +++ b/MagicQuant/Services/TensorGroupReviewService.cs @@ -0,0 +1,138 @@ +using MagicQuant.Models.Learning; +using MagicQuant.Services.Learning; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class TensorGroupReviewService +{ + private readonly TensorGroupingAuditService _auditService = new(); + + public async Task ReviewNativeTensorGroupingAsync( + QuantizationService quantizationService, + string nativeGgufPath, + bool requireConfirmation, + CancellationToken ct = default) + { + if (quantizationService == null) + throw new ArgumentNullException(nameof(quantizationService)); + + if (string.IsNullOrWhiteSpace(nativeGgufPath) || !File.Exists(nativeGgufPath)) + throw new FileNotFoundException($"Native GGUF path not found for tensor-group review: {nativeGgufPath}"); + + var tensorTypes = await quantizationService.ReadExactTensorTypesAsync(nativeGgufPath, ct); + var truth = tensorTypes + .OrderBy(x => x.Key, StringComparer.Ordinal) + .ToDictionary( + x => x.Key, + x => new LearnedTensorTruth(x.Key, x.Value, LearningSource.GgufOnly), + StringComparer.Ordinal); + + var audit = _auditService.Audit(truth.Keys.ToList(), truth); + + PrintReview(nativeGgufPath, truth, audit); + + if (audit.HasFatalIssues) + { + throw new InvalidOperationException( + "Tensor-group regex review found fatal grouping issues before learning/search could continue. " + + $"Ambiguous={audit.Ambiguous.Count}, IllegalUnresolved={audit.IllegalUnresolved.Count}. " + + "Fix tensor_groups.yaml and rerun."); + } + + if (requireConfirmation) + { + bool confirmed = AnsiConsole.Confirm( + "Continue with this tensor grouping profile? Review the counts above before saying yes."); + + if (!confirmed) + { + throw new OperationCanceledException( + "Evolution run cancelled by user after tensor-group profile review. No tensor-group-scoped learning/search work was started."); + } + } + else + { + AnsiConsole.MarkupLine("[yellow]Tensor-group confirmation skipped by config/CLI.[/]"); + } + + return audit; + } + + private static void PrintReview( + string nativeGgufPath, + IReadOnlyDictionary truth, + TensorGroupingAuditResult audit) + { + string snapshotJson = TensorGroupProfileService.BuildSnapshotJson(); + string fingerprint = TensorGroupProfileService.ComputeSnapshotHash(snapshotJson); + + AnsiConsole.Write(new Rule("[yellow]Tensor Group Regex Review[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[grey]Native source:[/] {Markup.Escape(Path.GetFileName(nativeGgufPath))}"); + AnsiConsole.MarkupLine($"[grey]Tensor group profile hash:[/] [cyan]{Markup.Escape(fingerprint[..Math.Min(16, fingerprint.Length)])}[/]"); + AnsiConsole.MarkupLine($"[grey]Tensors inspected:[/] [cyan]{truth.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]BaseQuant exception tensors:[/] [cyan]{audit.BaseQuantExceptions.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Ambiguous tensors:[/] [{(audit.Ambiguous.Count == 0 ? "green" : "red")}]{audit.Ambiguous.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Illegal unresolved tensors:[/] [{(audit.IllegalUnresolved.Count == 0 ? "green" : "red")}]{audit.IllegalUnresolved.Count:N0}[/]"); + + var table = new Table() + .Border(TableBorder.Rounded) + .AddColumn("Id") + .AddColumn("Group") + .AddColumn(new TableColumn("Tensors").RightAligned()) + .AddColumn("Top native types") + .AddColumn("Examples"); + + foreach (var group in TReg.All.OrderBy(x => x.UniqueId)) + { + var tensors = audit.GroupedByTensor + .Where(x => x.Value.PrimaryGroup?.UniqueId == group.UniqueId) + .Select(x => x.Key) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + var distribution = tensors + .Select(t => truth.TryGetValue(t, out var row) ? row.FinalQuantType : "unknown") + .GroupBy(x => x, StringComparer.Ordinal) + .OrderByDescending(x => x.Count()) + .ThenBy(x => x.Key, StringComparer.Ordinal) + .Take(4) + .Select(x => $"{x.Key}:{x.Count():N0}"); + + var examples = tensors.Take(4).Select(Markup.Escape); + + table.AddRow( + group.UniqueId.ToString(), + Markup.Escape(group.Name), + tensors.Count.ToString("N0"), + Markup.Escape(string.Join(", ", distribution)), + string.Join("\n", examples)); + } + + AnsiConsole.Write(table); + + PrintIssuePreview("Ambiguous group collisions", audit.Ambiguous); + PrintIssuePreview("Illegal unresolved tensors", audit.IllegalUnresolved); + PrintIssuePreview("BaseQuant exception tensors", audit.BaseQuantExceptions); + } + + private static void PrintIssuePreview(string heading, IReadOnlyList issues) + { + if (issues.Count == 0) + return; + + AnsiConsole.MarkupLine($"[yellow]{Markup.Escape(heading)}:[/] showing first {Math.Min(10, issues.Count):N0} of {issues.Count:N0}"); + foreach (var issue in issues.Take(10)) + { + var extra = issue.MatchedGroups.Count > 0 + ? $" groups=[{string.Join(", ", issue.MatchedGroups)}]" + : issue.MatchedExceptionPattern != null + ? $" pattern={issue.MatchedExceptionPattern}" + : string.Empty; + + AnsiConsole.MarkupLine($" [grey]-[/] {Markup.Escape(issue.TensorName)} [grey]{Markup.Escape(extra)}[/]"); + } + } +} From 7523cd5f60e603e9402f6a5e44326bfdecb18272 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 3 May 2026 12:31:14 -0400 Subject: [PATCH 184/258] Still broken prediction system after tensor connection refactor, but doing slightly better. --- MagicQuant/Models/HybridFinalizationModels.cs | 19 +- .../DuckDbPredictionMaterializationService.cs | 389 +++++++++++++++++- .../Services/FinalArtifactNamingService.cs | 28 +- .../Services/FinalReleaseMetadataService.cs | 5 + .../FinalSurvivorSelectionCliService.cs | 7 +- .../Services/HybridArtifactExportService.cs | 15 +- .../Services/HybridBenchmarkRepository.cs | 129 +++++- .../Services/RankSafeKldPredictionService.cs | 18 + .../Services/RemainingCombinationStore.cs | 36 +- MagicQuant/config.dev.yaml | 8 +- 10 files changed, 598 insertions(+), 56 deletions(-) diff --git a/MagicQuant/Models/HybridFinalizationModels.cs b/MagicQuant/Models/HybridFinalizationModels.cs index 5aa347b..b1cd787 100644 --- a/MagicQuant/Models/HybridFinalizationModels.cs +++ b/MagicQuant/Models/HybridFinalizationModels.cs @@ -142,8 +142,25 @@ public sealed class BenchmarkSnapshotRecord public string DisplayName { get; init; } = string.Empty; public string ProviderName { get; init; } = string.Empty; public string BaselineFamily { get; init; } = string.Empty; + /// + /// True only for MagicQuant-discovered mixed tensor configurations. + /// Exact/base-only blankets and uniform external rebuilt baselines are not hybrids. + /// public bool IsHybrid { get; init; } + public bool IsExternalPureBaseline { get; init; } + + /// + /// True when MagicQuant rebuilt/materialized an external provider baseline for equal-footing + /// benchmarking/export, but did not invent a mixed MagicQuant hybrid recipe. + /// + public bool IsExternalRebuiltBaseline { get; init; } + + /// + /// True when the tensor config contains materialized tensor-group overrides, even if those + /// overrides are only exact/native anchors or a uniform external baseline rebuild. + /// + public bool IsMaterializedTensorMapped { get; init; } public ulong SizeBytes { get; init; } public double Kld { get; init; } public double Ppl { get; init; } @@ -218,4 +235,4 @@ public sealed class CombinationSurvivalExecutionResult public SurvivalStageReport SurvivalReport { get; init; } = new(); public IReadOnlyList Eliminations { get; init; } = Array.Empty(); public IReadOnlyList ValidationFailures { get; init; } = Array.Empty(); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs index eccdb69..cff401a 100644 --- a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs +++ b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Numerics; using DuckDB.NET.Data; using MagicQuant.Helpers; using MagicQuant.Models; @@ -46,6 +47,8 @@ public async Task MaterializeAsync(Cancellation await c.OpenAsync(ct); await ConfigureSessionAsync(c, ct); + PrintModelCoverageDiagnostics(model); + await ExecuteAsync(c, $@" UPDATE {CombinationDuckDbSchema.TableName} SET PredictedKld = NULL, @@ -54,15 +57,26 @@ await ExecuteAsync(c, $@" PredictionRank = NULL;", ct); await BuildLookupTablesAsync(c, model, ct); + await PrintLookupDiagnosticsAsync(c, model, ct); await BuildPredictionWorkTablesAsync(c, model, ct); + await PrintPredictionWorkDiagnosticsAsync(c, ct); await BuildPavaBlocksAsync(c, ct); await PersistProjectedPredictionsAsync(c, model, ct); var status = await _store.GetPredictionStatusAsync(ct); + await PrintFinalMaterializationDiagnosticsAsync(c, status, ct); foreach (var note in model.Notes) AnsiConsole.MarkupLine($"[grey]Prediction materialization note:[/] {Markup.Escape(note)}"); + if (status.TotalRows > 0 && status.PredictedRows == 0) + { + throw new InvalidOperationException( + "Prediction materialization produced zero predicted rows. This is not a valid no-hybrid result. " + + "The diagnostics above should identify whether DuckDB BaseQuant IDs, base-only anchors, " + + "or group isolation/profile-scoped truth rows are missing."); + } + return status; } @@ -105,7 +119,22 @@ CREATE TEMP TABLE temp_group_size_delta ( IsSizePredictable BOOLEAN );", ct); - var activeBaselines = RuntimeSearchSpace.GetActiveCombinationBaselines() + var duckDbBaseQuantIds = await LoadDistinctBaseQuantIdsAsync(c, ct); + var runtimeBaseQuantIds = RuntimeSearchSpace.GetActiveCombinationBaselines() + .Select(x => x.UniqueId) + .OrderBy(x => x) + .ToList(); + + if (!duckDbBaseQuantIds.SequenceEqual(runtimeBaseQuantIds)) + { + AnsiConsole.MarkupLine( + $"[yellow]Prediction carrier mismatch:[/] DuckDB BaseQuant IDs=[cyan]{Markup.Escape(FormatBaselineIds(duckDbBaseQuantIds))}[/], " + + $"Runtime active IDs=[cyan]{Markup.Escape(FormatBaselineIds(runtimeBaseQuantIds))}[/]. " + + "Using DuckDB BaseQuant IDs as the scoring source of truth."); + } + + var activeBaselines = duckDbBaseQuantIds + .Select(BaselineQuants.FromId) .OrderBy(x => x.UniqueId) .ToList(); @@ -119,17 +148,16 @@ IsSizePredictable BOOLEAN foreach (var baseline in activeBaselines) { byte normalizedBase = RankSafeKldPredictionService.NormalizeBaselineIdForIsolation(baseline.UniqueId); - bool hasBaseSize = model.BaseOnlySnapshotsByBaselineId.TryGetValue(normalizedBase, out var baseOnly); + bool hasBaseSize = model.BaseOnlySnapshotsByBaselineId.TryGetValue(baseline.UniqueId, out var baseOnly) || + model.BaseOnlySnapshotsByBaselineId.TryGetValue(normalizedBase, out baseOnly); await ExecuteAsync(c, $"INSERT INTO temp_base_predicted_size VALUES ({baseline.UniqueId}, {SqlULong(hasBaseSize ? baseOnly!.SizeBytes : 0UL)}, {SqlBool(hasBaseSize)});", ct); - var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(baseline); - foreach (var slot in activeGroups) { - var allowedForGroup = allowed[slot.Group.UniqueId]; - foreach (byte storedSlot in allowedForGroup) + var storedSlotsForGroup = await LoadDistinctStoredSlotsAsync(c, baseline.UniqueId, slot.ColumnName, ct); + foreach (byte storedSlot in storedSlotsForGroup) { var effectiveBaselineId = GetEffectiveBaselineId(baseline.UniqueId, storedSlot); var normalizedBaselineId = RankSafeKldPredictionService.NormalizeBaselineIdForIsolation(effectiveBaselineId); @@ -343,8 +371,8 @@ FROM temp_prediction_order { ct.ThrowIfCancellationRequested(); - ulong ordinal = Convert.ToUInt64(r.GetValue(0)); - double value = Math.Max(0d, Convert.ToDouble(r.GetValue(1), CultureInfo.InvariantCulture)); + ulong ordinal = ToUInt64(r.GetValue(0)); + double value = Math.Max(0d, ToDouble(r.GetValue(1))); blocks.Add(new PavaBlock { @@ -461,6 +489,244 @@ FROM temp_ranked_prediction_with_rank r WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "r")};", ct); } + private static async Task> LoadDistinctBaseQuantIdsAsync(DuckDBConnection c, CancellationToken ct) + { + var result = new List(); + using var cmd = c.CreateCommand(); + cmd.CommandText = $@" +SELECT DISTINCT BaseQuant +FROM {CombinationDuckDbSchema.TableName} +ORDER BY BaseQuant;"; + + using var reader = await cmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + result.Add(ToByte(reader.GetValue(0))); + + return result; + } + + private static async Task> LoadDistinctStoredSlotsAsync( + DuckDBConnection c, + byte baseQuant, + string columnName, + CancellationToken ct) + { + var result = new List(); + using var cmd = c.CreateCommand(); + cmd.CommandText = $@" +SELECT DISTINCT {columnName} +FROM {CombinationDuckDbSchema.TableName} +WHERE BaseQuant = ? +ORDER BY {columnName};"; + cmd.Parameters.Add(new DuckDBParameter { Value = baseQuant }); + + using var reader = await cmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + result.Add(ToByte(reader.GetValue(0))); + + return result; + } + + private static void PrintModelCoverageDiagnostics(RankSafeKldPredictionService.RankSafePredictionModel model) + { + string activeGroups = string.Join(", ", model.ActiveGroups.Select(x => $"{x.Name}:{x.UniqueId}")); + string baseOnly = FormatBaselineIds(model.BaseOnlySnapshotsByBaselineId.Keys.OrderBy(x => x).ToList()); + + AnsiConsole.MarkupLine($"[grey]Prediction model active groups:[/] {Markup.Escape(activeGroups)}"); + AnsiConsole.MarkupLine($"[grey]Prediction model base-only anchors:[/] [cyan]{model.BaseOnlySnapshotsByBaselineId.Count:N0}[/] ({Markup.Escape(baseOnly)})"); + AnsiConsole.MarkupLine($"[grey]Prediction model isolation anchors:[/] [cyan]{model.IsolationByGroupAndBaseline.Count:N0}[/]"); + + foreach (var group in model.ActiveGroups.OrderBy(x => x.UniqueId)) + { + var ids = model.IsolationByGroupAndBaseline.Keys + .Where(x => x.GroupId == group.UniqueId) + .Select(x => x.BaselineId) + .Distinct() + .OrderBy(x => x) + .ToList(); + + AnsiConsole.MarkupLine($"[grey] - isolation coverage {Markup.Escape(group.Name)}:[/] [cyan]{ids.Count:N0}[/] ({Markup.Escape(FormatBaselineIds(ids))})"); + } + } + + private static async Task PrintLookupDiagnosticsAsync( + DuckDBConnection c, + RankSafeKldPredictionService.RankSafePredictionModel model, + CancellationToken ct) + { + long totalRows = await ScalarLongAsync(c, $"SELECT COUNT(*) FROM {CombinationDuckDbSchema.TableName};", ct); + long baseLookupRows = await ScalarLongAsync(c, "SELECT COUNT(*) FROM temp_base_predicted_size;", ct); + long missingBaseJoin = await ScalarLongAsync(c, $@" +SELECT COUNT(*) +FROM {CombinationDuckDbSchema.TableName} t +LEFT JOIN temp_base_predicted_size b ON b.BaseQuant = t.BaseQuant +WHERE b.BaseQuant IS NULL;", ct); + + AnsiConsole.MarkupLine($"[grey]DuckDB prediction lookup rows:[/] total=[cyan]{totalRows:N0}[/] base-lookups=[cyan]{baseLookupRows:N0}[/] missing-base-join=[cyan]{missingBaseJoin:N0}[/]"); + + await PrintBaseLookupRowsAsync(c, ct); + await PrintMissingGroupLookupRowsAsync(c, model, ct); + } + + private static async Task PrintBaseLookupRowsAsync(DuckDBConnection c, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = @" +SELECT BaseQuant, BaseSizeBytes, IsSizePredictable +FROM temp_base_predicted_size +ORDER BY BaseQuant;"; + + using var reader = await cmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + { + byte baseId = ToByte(reader.GetValue(0)); + ulong bytes = ToUInt64(reader.GetValue(1)); + bool predictable = ToBool(reader.GetValue(2)); + AnsiConsole.MarkupLine($"[grey] - base lookup {Markup.Escape(FormatBaselineId(baseId))}:[/] size={bytes:N0} predictable={predictable}"); + } + } + + private static async Task PrintMissingGroupLookupRowsAsync( + DuckDBConnection c, + RankSafeKldPredictionService.RankSafePredictionModel model, + CancellationToken ct) + { + var active = model.ActiveGroups + .Select(g => GroupSlots.First(x => x.Group.UniqueId == g.UniqueId)) + .OrderBy(x => x.Group.UniqueId) + .ToList(); + + foreach (var slot in active) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = $@" +SELECT t.BaseQuant, t.{slot.ColumnName}, COUNT(*) AS MissingRows +FROM {CombinationDuckDbSchema.TableName} t +LEFT JOIN temp_effective_group_prediction e + ON e.BaseQuant = t.BaseQuant + AND e.GroupName = '{slot.ColumnName}' + AND e.StoredSlot = t.{slot.ColumnName} +WHERE e.BaseQuant IS NULL +GROUP BY t.BaseQuant, t.{slot.ColumnName} +ORDER BY MissingRows DESC +LIMIT 5;"; + + using var reader = await cmd.ExecuteReaderAsync(ct); + bool wroteHeader = false; + while (await reader.ReadAsync(ct)) + { + if (!wroteHeader) + { + AnsiConsole.MarkupLine($"[yellow]Missing effective lookup rows for group {Markup.Escape(slot.ColumnName)}:[/]"); + wroteHeader = true; + } + + byte baseId = ToByte(reader.GetValue(0)); + byte storedSlot = ToByte(reader.GetValue(1)); + long count = ToInt64(reader.GetValue(2)); + AnsiConsole.MarkupLine($"[yellow] - base={Markup.Escape(FormatBaselineId(baseId))} stored={Markup.Escape(FormatStoredSlot(storedSlot))} rows={count:N0}[/]"); + } + } + } + + private static async Task PrintPredictionWorkDiagnosticsAsync(DuckDBConnection c, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = @" +SELECT + COUNT(*) AS WorkRows, + COALESCE(SUM(CASE WHEN IsKldPredictable THEN 1 ELSE 0 END), 0) AS KldPredictableRows, + COALESCE(SUM(CASE WHEN IsSizePredictable THEN 1 ELSE 0 END), 0) AS SizePredictableRows, + COALESCE(SUM(CASE WHEN IsKldPredictable AND IsSizePredictable THEN 1 ELSE 0 END), 0) AS ProjectableRows +FROM temp_prediction_work;"; + + using (var reader = await cmd.ExecuteReaderAsync(ct)) + { + await reader.ReadAsync(ct); + long workRows = ToInt64(reader.GetValue(0)); + long kldRows = ToInt64(reader.GetValue(1)); + long sizeRows = ToInt64(reader.GetValue(2)); + long projectableRows = ToInt64(reader.GetValue(3)); + AnsiConsole.MarkupLine($"[grey]DuckDB prediction work rows:[/] work=[cyan]{workRows:N0}[/] kld-ok=[cyan]{kldRows:N0}[/] size-ok=[cyan]{sizeRows:N0}[/] projectable=[cyan]{projectableRows:N0}[/]"); + } + + await PrintPredictionFailureBreakdownAsync(c, ct); + + long projected = await ScalarLongAsync(c, "SELECT COUNT(*) FROM temp_projection;", ct); + long ordered = await ScalarLongAsync(c, "SELECT COUNT(*) FROM temp_prediction_order;", ct); + AnsiConsole.MarkupLine($"[grey]DuckDB projection rows:[/] projection=[cyan]{projected:N0}[/] ordered=[cyan]{ordered:N0}[/]"); + } + + private static async Task PrintPredictionFailureBreakdownAsync(DuckDBConnection c, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = @" +SELECT + BaseQuant, + COUNT(*) AS Rows, + COALESCE(SUM(CASE WHEN NOT IsKldPredictable THEN 1 ELSE 0 END), 0) AS KldMissing, + COALESCE(SUM(CASE WHEN NOT IsSizePredictable THEN 1 ELSE 0 END), 0) AS SizeMissing +FROM temp_prediction_work +GROUP BY BaseQuant +ORDER BY BaseQuant;"; + + using var reader = await cmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + { + byte baseId = ToByte(reader.GetValue(0)); + long rows = ToInt64(reader.GetValue(1)); + long kldMissing = ToInt64(reader.GetValue(2)); + long sizeMissing = ToInt64(reader.GetValue(3)); + AnsiConsole.MarkupLine($"[grey] - work {Markup.Escape(FormatBaselineId(baseId))}:[/] rows={rows:N0} missing-kld={kldMissing:N0} missing-size={sizeMissing:N0}"); + } + } + + private static async Task PrintFinalMaterializationDiagnosticsAsync( + DuckDBConnection c, + PredictionMaterializationStatus status, + CancellationToken ct) + { + long rankedRows = await ScalarLongAsync(c, "SELECT COUNT(*) FROM temp_ranked_prediction_with_rank;", ct); + AnsiConsole.MarkupLine($"[grey]DuckDB final materialized prediction rows:[/] predicted=[cyan]{status.PredictedRows:N0}[/] / {status.TotalRows:N0}, ranked-temp=[cyan]{rankedRows:N0}[/]"); + } + + private static async Task ScalarLongAsync(DuckDBConnection c, string sql, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + return ToInt64(await cmd.ExecuteScalarAsync(ct) ?? 0L); + } + + private static string FormatBaselineIds(IReadOnlyCollection ids) + { + if (ids.Count == 0) + return "none"; + + return string.Join(", ", ids.Select(FormatBaselineId)); + } + + private static string FormatBaselineId(byte id) + { + try + { + var baseline = BaselineQuants.FromId(id); + return $"{baseline.Names[0]}:{id}"; + } + catch + { + return $"unknown:{id}"; + } + } + + private static string FormatStoredSlot(byte storedSlot) + { + if (BaselineQuants.IsNullTensorConfigGroupSlot(storedSlot)) + return $"base/null:{storedSlot}"; + + byte decoded = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedSlot); + return $"{FormatBaselineId(decoded)} stored:{storedSlot}"; + } + private static double ComputeBaseConfidence(RankSafePredictionFit fit) { if (fit.UsedFallback) @@ -491,6 +757,111 @@ private static double GetBitRange(byte baselineId) return BaselineQuants.FromId(baselineId).BitRange; } + private static long ToInt64(object? value) + { + if (value is null or DBNull) + return 0L; + + return value switch + { + long x => x, + int x => x, + short x => x, + sbyte x => x, + byte x => x, + uint x => checked((long)x), + ulong x => checked((long)x), + BigInteger x => checked((long)x), + decimal x => checked((long)x), + double x => checked((long)x), + float x => checked((long)x), + IConvertible x => x.ToInt64(CultureInfo.InvariantCulture), + _ => long.Parse(value.ToString() ?? "0", CultureInfo.InvariantCulture) + }; + } + + private static ulong ToUInt64(object? value) + { + if (value is null or DBNull) + return 0UL; + + return value switch + { + ulong x => x, + long x => checked((ulong)x), + int x => checked((ulong)x), + short x => checked((ulong)x), + sbyte x => checked((ulong)x), + byte x => x, + uint x => x, + BigInteger x => checked((ulong)x), + decimal x => checked((ulong)x), + double x => checked((ulong)x), + float x => checked((ulong)x), + IConvertible x => x.ToUInt64(CultureInfo.InvariantCulture), + _ => ulong.Parse(value.ToString() ?? "0", CultureInfo.InvariantCulture) + }; + } + + private static byte ToByte(object? value) + { + if (value is null or DBNull) + return 0; + + return value switch + { + byte x => x, + sbyte x => checked((byte)x), + short x => checked((byte)x), + int x => checked((byte)x), + long x => checked((byte)x), + ushort x => checked((byte)x), + uint x => checked((byte)x), + ulong x => checked((byte)x), + BigInteger x => checked((byte)x), + IConvertible x => x.ToByte(CultureInfo.InvariantCulture), + _ => byte.Parse(value.ToString() ?? "0", CultureInfo.InvariantCulture) + }; + } + + private static double ToDouble(object? value) + { + if (value is null or DBNull) + return 0d; + + return value switch + { + double x => x, + float x => x, + decimal x => (double)x, + BigInteger x => (double)x, + IConvertible x => x.ToDouble(CultureInfo.InvariantCulture), + _ => double.Parse(value.ToString() ?? "0", CultureInfo.InvariantCulture) + }; + } + + private static bool ToBool(object? value) + { + if (value is null or DBNull) + return false; + + return value switch + { + bool x => x, + byte x => x != 0, + sbyte x => x != 0, + short x => x != 0, + int x => x != 0, + long x => x != 0, + ushort x => x != 0, + uint x => x != 0, + ulong x => x != 0, + BigInteger x => x != BigInteger.Zero, + IConvertible x => x.ToBoolean(CultureInfo.InvariantCulture), + _ => bool.Parse(value.ToString() ?? "false") + }; + } + private static async Task ConfigureSessionAsync(DuckDBConnection connection, CancellationToken ct) { await ExecuteAsync(connection, "SET preserve_insertion_order = false;", ct); @@ -537,4 +908,4 @@ public sealed class PredictionMaterializationStatus public double? MaxPredictedKld { get; init; } public ulong? MinPredictedSizeBytes { get; init; } public ulong? MaxPredictedSizeBytes { get; init; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/FinalArtifactNamingService.cs b/MagicQuant/Services/FinalArtifactNamingService.cs index 8414f93..e6b9a87 100644 --- a/MagicQuant/Services/FinalArtifactNamingService.cs +++ b/MagicQuant/Services/FinalArtifactNamingService.cs @@ -37,22 +37,22 @@ public FinalArtifactName BuildName( int ordinal = context.NextHybridOrdinal(quantFamily); tag = $"{providerToken}-{SanitizeToken(quantFamily)}_{ordinal}"; } - else if (snapshot.Quant.BaseQuant.IsExternalRepositoryBaseline) + else if (HybridBenchmarkRepository.ResolveSourceBaselineForProvider(snapshot.Quant).IsExternalRepositoryBaseline) { - string externalProviderToken = ResolveExternalProviderToken(snapshot.Quant.BaseQuant); - string externalFamily = NormalizeExternalDisplayName(snapshot.Quant.BaseQuant.Names[0], externalProviderToken); + var sourceBaseline = HybridBenchmarkRepository.ResolveSourceBaselineForProvider(snapshot.Quant); + string externalProviderToken = ResolveExternalProviderToken(sourceBaseline); + string externalFamily = NormalizeExternalDisplayName(sourceBaseline.Names[0], externalProviderToken); - if (Config.ExportExternalLearnedBaselines) + providerToken = externalProviderToken; + if (Config.ExportExternalLearnedBaselines || snapshot.IsExternalRebuiltBaseline || snapshot.IsMaterializedTensorMapped) { // This is a MagicQuant rebuilt/re-uploaded copy of an external learned baseline. - // Keep the external source tag, but mark the artifact as MQ-owned. - providerToken = "MQ"; + // The artifact name gets an MQ prefix, but the provider remains the upstream source. quantFamily = $"MQ-{SanitizeToken(externalFamily)}"; tag = quantFamily; } else { - providerToken = externalProviderToken; quantFamily = SanitizeToken(externalFamily); tag = quantFamily; } @@ -86,11 +86,12 @@ public string BuildDisplayLabel( if (snapshot.IsHybrid) return $"{prefix}-MQ-{SanitizeToken(ResolveHybridRangeFamily(snapshot, context))}"; - if (snapshot.Quant.BaseQuant.IsExternalRepositoryBaseline) + var sourceBaseline = HybridBenchmarkRepository.ResolveSourceBaselineForProvider(snapshot.Quant); + if (sourceBaseline.IsExternalRepositoryBaseline) { - string providerToken = ResolveExternalProviderToken(snapshot.Quant.BaseQuant); - string family = SanitizeToken(NormalizeExternalDisplayName(snapshot.Quant.BaseQuant.Names[0], providerToken)); - return Config.ExportExternalLearnedBaselines + string providerToken = ResolveExternalProviderToken(sourceBaseline); + string family = SanitizeToken(NormalizeExternalDisplayName(sourceBaseline.Names[0], providerToken)); + return Config.ExportExternalLearnedBaselines || snapshot.IsExternalRebuiltBaseline || snapshot.IsMaterializedTensorMapped ? $"{prefix}-MQ-{family}" : $"{prefix}-{family}"; } @@ -410,6 +411,9 @@ private string BuildProviderQuantFallback( if (string.IsNullOrWhiteSpace(sanitizedFamily)) return string.Empty; + if (sanitizedFamily.StartsWith("MQ-", StringComparison.OrdinalIgnoreCase)) + return sanitizedFamily; + if (snapshot?.IsHybrid == true) { string ordinal = ExtractOrdinalFromFileName(fileName); @@ -492,4 +496,4 @@ public sealed class ProviderCredit public string Name { get; init; } = string.Empty; public string Url { get; init; } = string.Empty; public string Note { get; init; } = string.Empty; -} +} \ No newline at end of file diff --git a/MagicQuant/Services/FinalReleaseMetadataService.cs b/MagicQuant/Services/FinalReleaseMetadataService.cs index 2377332..95f7574 100644 --- a/MagicQuant/Services/FinalReleaseMetadataService.cs +++ b/MagicQuant/Services/FinalReleaseMetadataService.cs @@ -97,6 +97,9 @@ private object ToSurvivorJson( provider = artifact.ProviderName, quantFamily = artifact.BaselineFamily, isHybrid = artifact.Snapshot.IsHybrid, + isExternalPureBaseline = artifact.Snapshot.IsExternalPureBaseline, + isExternalRebuiltBaseline = artifact.Snapshot.IsExternalRebuiltBaseline, + isMaterializedTensorMapped = artifact.Snapshot.IsMaterializedTensorMapped, isExternalReference = artifact.IsExternalReference, downloadTarget = artifact.DownloadTarget, kld = artifact.Snapshot.Kld, @@ -199,6 +202,8 @@ private object ToReplacementSideJson( quantFamily, isHybrid = snapshot.IsHybrid, isExternalPureBaseline = snapshot.IsExternalPureBaseline, + isExternalRebuiltBaseline = snapshot.IsExternalRebuiltBaseline, + isMaterializedTensorMapped = snapshot.IsMaterializedTensorMapped, kld = snapshot.Kld, ppl = snapshot.Ppl, pplDeltaPercent = CalculatePplDeltaPercent(snapshot.Ppl, referencePpl), diff --git a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs index bfa45d7..247c551 100644 --- a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs +++ b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs @@ -130,9 +130,8 @@ private static string ResolveProviderName(BenchmarkSnapshotRecord snapshot, Fina if (snapshot.IsHybrid) return "MagicQuant"; - if (string.Equals(name.ProviderToken, "MQ", StringComparison.OrdinalIgnoreCase)) - return "MagicQuant"; - + // MQ-* in the planned artifact name can mean "rebuilt by MagicQuant". + // It must not overwrite the semantic upstream provider for rebuilt Unsloth/custom baselines. return HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); } @@ -168,4 +167,4 @@ private static string FormatPplDeltaPercent(double ppl, double? referencePpl) double delta = ((ppl - referencePpl.Value) / referencePpl.Value) * 100d; return $"{delta:0.000}%"; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/HybridArtifactExportService.cs b/MagicQuant/Services/HybridArtifactExportService.cs index 707e460..648ba0b 100644 --- a/MagicQuant/Services/HybridArtifactExportService.cs +++ b/MagicQuant/Services/HybridArtifactExportService.cs @@ -68,9 +68,7 @@ public async Task> ExportAsync( bool isHybrid = snap.IsHybrid; bool exportLocally = isHybrid || !snap.IsExternalPureBaseline || Config.ExportExternalLearnedBaselines; var name = ResolvePlannedOrBuildName(row, snap, namingContext, reservedFileNames); - string provider = !string.IsNullOrWhiteSpace(row.PlannedProviderName) - ? row.PlannedProviderName - : ResolveReadmeProviderName(snap, isHybrid, name); + string provider = ResolveReadmeProviderName(snap, name); if (!exportLocally) { @@ -200,14 +198,13 @@ private FinalArtifactName ResolvePlannedOrBuildName( return _namingService.BuildName(snapshot, namingContext, reservedFileNames); } - private static string ResolveReadmeProviderName(BenchmarkSnapshotRecord snapshot, bool isHybrid, FinalArtifactName name) + private static string ResolveReadmeProviderName(BenchmarkSnapshotRecord snapshot, FinalArtifactName name) { - if (isHybrid) - return "MagicQuant"; - - if (string.Equals(name.ProviderToken, "MQ", StringComparison.OrdinalIgnoreCase)) + if (snapshot.IsHybrid) return "MagicQuant"; + // The artifact filename may contain MQ-* when MagicQuant rebuilt an external + // baseline for equal-footing export. The provider remains the upstream source. return HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false); } @@ -331,4 +328,4 @@ private static Task CopyImatrixArtifactsAsync(string outputDirectory, Cancellati return Task.CompletedTask; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/HybridBenchmarkRepository.cs b/MagicQuant/Services/HybridBenchmarkRepository.cs index d625222..5f84ba2 100644 --- a/MagicQuant/Services/HybridBenchmarkRepository.cs +++ b/MagicQuant/Services/HybridBenchmarkRepository.cs @@ -74,7 +74,7 @@ public async Task> LoadBenchmarkSnap return null; var quant = (HybridQuant)config; - var baseQuant = quant.BaseQuant; + var sourceBaseline = ResolveSourceBaselineForProvider(quant); return new BenchmarkSnapshotRecord { @@ -82,14 +82,16 @@ public async Task> LoadBenchmarkSnap Quant = quant, DisplayName = BuildDisplayName(quant), ProviderName = ResolveProviderName(quant, exportNaming: false), - BaselineFamily = baseQuant.Names[0], - IsHybrid = quant.Tensors.Count > 0, - IsExternalPureBaseline = quant.Tensors.Count == 0 && baseQuant.IsExternalRepositoryBaseline, + BaselineFamily = ResolveBaselineFamily(quant), + IsHybrid = IsTrueMagicQuantHybrid(quant), + IsExternalPureBaseline = quant.Tensors.Count == 0 && sourceBaseline.IsExternalRepositoryBaseline, + IsExternalRebuiltBaseline = IsExternalRebuiltBaseline(quant), + IsMaterializedTensorMapped = quant.Tensors.Count > 0, SizeBytes = chosen.SizeBytes, Kld = general.Kld, Ppl = general.Ppl, OutputModelPath = await FindLatestSuccessfulOutputPathAsync(config, ct), - ExternalRepositoryUrl = BuildExternalRepositoryUrl(baseQuant) + ExternalRepositoryUrl = BuildExternalRepositoryUrl(sourceBaseline) }; } @@ -307,7 +309,7 @@ public async Task> LoadAllBenchmarkSnapshotsForCur moeRouter: combo.MoeRouter); var quant = (HybridQuant)config; - var baseQuant = quant.BaseQuant; + var sourceBaseline = ResolveSourceBaselineForProvider(quant); result.Add(new BenchmarkSnapshotRecord { @@ -315,14 +317,16 @@ public async Task> LoadAllBenchmarkSnapshotsForCur Quant = quant, DisplayName = BuildDisplayName(quant), ProviderName = ResolveProviderName(quant, exportNaming: false), - BaselineFamily = baseQuant.Names[0], - IsHybrid = quant.Tensors.Count > 0, - IsExternalPureBaseline = quant.Tensors.Count == 0 && baseQuant.IsExternalRepositoryBaseline, + BaselineFamily = ResolveBaselineFamily(quant), + IsHybrid = IsTrueMagicQuantHybrid(quant), + IsExternalPureBaseline = quant.Tensors.Count == 0 && sourceBaseline.IsExternalRepositoryBaseline, + IsExternalRebuiltBaseline = IsExternalRebuiltBaseline(quant), + IsMaterializedTensorMapped = quant.Tensors.Count > 0, SizeBytes = benchmark.SizeBytes, Kld = metric.Kld, Ppl = metric.Ppl, OutputModelPath = await FindLatestSuccessfulOutputPathAsync(config, ct), - ExternalRepositoryUrl = BuildExternalRepositoryUrl(baseQuant) + ExternalRepositoryUrl = BuildExternalRepositoryUrl(sourceBaseline) }); } @@ -336,10 +340,10 @@ public async Task> LoadAllBenchmarkSnapshotsForCur public static string ResolveProviderName(HybridQuant quant, bool exportNaming) { - if (exportNaming && quant.Tensors.Count > 0) - return "MQ"; + if (IsTrueMagicQuantHybrid(quant)) + return exportNaming ? "MQ" : "MagicQuant"; - var baseline = quant.BaseQuant; + var baseline = ResolveSourceBaselineForProvider(quant); if (baseline.IsExternalRepositoryBaseline) return string.IsNullOrWhiteSpace(baseline.ShortSourceName) ? "External" @@ -350,6 +354,103 @@ public static string ResolveProviderName(HybridQuant quant, bool exportNaming) : baseline.ShortSourceName!; } + public static string ResolveBaselineFamily(HybridQuant quant) + { + if (IsTrueMagicQuantHybrid(quant)) + return quant.BaseQuant.Names[0]; + + return ResolveSourceBaselineForProvider(quant).Names[0]; + } + + public static BaselineQuants ResolveSourceBaselineForProvider(HybridQuant quant) + { + if (TryResolveUniformExternalLearnedBaseline(quant, out var externalBaseline)) + return externalBaseline; + + return quant.BaseQuant; + } + + public static bool IsExternalRebuiltBaseline(HybridQuant quant) + { + if (IsTrueMagicQuantHybrid(quant)) + return false; + + if (quant.BaseQuant.IsExternalRepositoryBaseline) + return quant.Tensors.Count > 0; + + return TryResolveUniformExternalLearnedBaseline(quant, out _); + } + + public static bool IsTrueMagicQuantHybrid(HybridQuant quant) + { + if (quant.Tensors.Count == 0) + return false; + + var activeTensors = GetActiveTensors(quant).ToList(); + if (activeTensors.Count == 0) + return false; + + if (activeTensors.All(x => x.OverrideMode == HybridTensorOverrideMode.ExactTensorScheme)) + return false; + + if (TryResolveUniformExternalLearnedBaseline(quant, out _)) + return false; + + return true; + } + + private static bool TryResolveUniformExternalLearnedBaseline(HybridQuant quant, out BaselineQuants externalBaseline) + { + externalBaseline = default!; + + var activeGroups = GetActiveGroups().ToList(); + if (activeGroups.Count == 0) + return false; + + var activeTensors = GetActiveTensors(quant).ToList(); + if (activeTensors.Count != activeGroups.Count) + return false; + + if (activeTensors.Any(x => x.OverrideMode != HybridTensorOverrideMode.LearnedBaselineCandidate || x.CandidateBaseline == null)) + return false; + + var candidates = activeTensors + .Select(x => x.CandidateBaseline!) + .ToList(); + + if (candidates.Any(x => !x.IsExternalRepositoryBaseline)) + return false; + + var first = candidates[0]; + bool allSame = candidates.All(x => + x.UniqueId == first.UniqueId || + string.Equals(x.CanonicalKey, first.CanonicalKey, StringComparison.OrdinalIgnoreCase)); + + if (!allSame) + return false; + + externalBaseline = first; + return true; + } + + private static IEnumerable GetActiveTensors(HybridQuant quant) + { + var activeGroupIds = GetActiveGroups() + .Select(x => x.UniqueId) + .ToHashSet(); + + return quant.Tensors + .Where(x => x?.TGroup != null && activeGroupIds.Contains(x.TGroup.UniqueId)); + } + + private static IReadOnlyList GetActiveGroups() + { + return TReg.All + .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + } + public static string BuildDisplayName(HybridQuant quant) { string modelName = string.IsNullOrWhiteSpace(Cache.ModelDirectory) @@ -400,4 +501,4 @@ public static string BuildDisplayName(HybridQuant quant) .Select(x => (int?)x.Id) .FirstOrDefaultAsync(ct); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/RankSafeKldPredictionService.cs b/MagicQuant/Services/RankSafeKldPredictionService.cs index 5ca4807..7fabf6b 100644 --- a/MagicQuant/Services/RankSafeKldPredictionService.cs +++ b/MagicQuant/Services/RankSafeKldPredictionService.cs @@ -190,6 +190,24 @@ private async Task BuildContextAsync(CancellationToken .OrderBy(x => x.UniqueId)) { byte normalizedBaselineId = NormalizeBaselineIdForIsolation(baseline.UniqueId); + + if (!baseOnlyByBaselineId.ContainsKey(baseline.UniqueId)) + { + var directBaseOnlyQuant = HybridQuant.CreateExactBlanket( + baseQuant: baseline, + groups: activeGroups, + exactScheme: nativeExactScheme); + + var directBaseOnlySnapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)directBaseOnlyQuant, ct); + if (directBaseOnlySnapshot != null) + { + baseOnlyByBaselineId[baseline.UniqueId] = directBaseOnlySnapshot; + if (!baseOnlyByBaselineId.ContainsKey(normalizedBaselineId)) + baseOnlyByBaselineId[normalizedBaselineId] = directBaseOnlySnapshot; + continue; + } + } + if (baseOnlyByBaselineId.ContainsKey(normalizedBaselineId)) continue; diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 8028292..0f6d402 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -12,15 +12,16 @@ public sealed class RemainingCombinationStore private const string DbFileNamePrefix = "MagicQuant_Combinations"; private const string TableName = CombinationDuckDbSchema.TableName; - private static string ConnectionString => $"Data Source={Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName())}"; + private static string ConnectionString => $"Data Source={GetDatabaseFilePathInternal()}"; - public string GetDatabaseFilePath() => Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName()); + public string GetDatabaseFilePath() => GetDatabaseFilePathInternal(); public async Task CountAsync(CancellationToken ct = default) { using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); await ConfigureFastLoadSessionAsync(connection, ct); + await EnsureTensorConfigsTableExistsAsync(connection, ct); using var cmd = connection.CreateCommand(); cmd.CommandText = $"SELECT COUNT(*) FROM {TableName};"; @@ -61,6 +62,7 @@ public async IAsyncEnumerable StreamAsync( using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); await ConfigureFastLoadSessionAsync(connection, ct); + await EnsureTensorConfigsTableExistsAsync(connection, ct); string sql = $@"SELECT {CombinationDuckDbSchema.SlotColumnList} FROM {TableName}"; if (!string.IsNullOrWhiteSpace(whereSql)) @@ -110,6 +112,7 @@ public async Task GetPredictionStatusAsync(Canc using var connection = new DuckDBConnection(ConnectionString); await connection.OpenAsync(ct); await ConfigureFastLoadSessionAsync(connection, ct); + await EnsureTensorConfigsTableExistsAsync(connection, ct); using var cmd = connection.CreateCommand(); cmd.CommandText = $@" @@ -228,6 +231,7 @@ PredictionRank ASC using var c = new DuckDBConnection(ConnectionString); await c.OpenAsync(ct); await ConfigureFastLoadSessionAsync(c, ct); + await EnsureTensorConfigsTableExistsAsync(c, ct); using var cmd = c.CreateCommand(); cmd.CommandText = sql; @@ -283,6 +287,7 @@ private async Task> QueryPredictedRowsAsync using var c = new DuckDBConnection(ConnectionString); await c.OpenAsync(ct); await ConfigureFastLoadSessionAsync(c, ct); + await EnsureTensorConfigsTableExistsAsync(c, ct); using var cmd = c.CreateCommand(); cmd.CommandText = sql; @@ -377,11 +382,36 @@ private static string GetDuckDbDirectory() "Neither Cache.ModelMagicQuantDirectory nor Cache.MagicQuantDirectory is set."); } + private static string GetDatabaseFilePathInternal() + { + return Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName()); + } + private static string BuildContextAwareDuckDbFileName() { + // IMPORTANT: this must stay byte-for-byte compatible with QuantDatabaseService + // unless both services are changed together. The previous patch made only the + // prediction reader profile-aware, which opened a brand-new empty DuckDB file + // after stage-1 had populated the original file. string model = string.IsNullOrWhiteSpace(Cache.CurrentModelId) ? "unknown-model" : Cache.CurrentModelId; string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixIdentityHash ?? "imatrix-unknown") : "no-imatrix"; string hp = RuntimeSearchSpace.AllowHighPrecisionHybrids ? "hp-on" : "hp-off"; return $"{DbFileNamePrefix}_{model}_{imatrix}_{hp}.duckdb"; } -} + + private static async Task EnsureTensorConfigsTableExistsAsync(DuckDBConnection connection, CancellationToken ct) + { + using var cmd = connection.CreateCommand(); + cmd.CommandText = "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = ?;"; + cmd.Parameters.Add(new DuckDBParameter { Value = TableName }); + + long matches = Convert.ToInt64(await cmd.ExecuteScalarAsync(ct) ?? 0L); + if (matches > 0) + return; + + throw new InvalidOperationException( + $"DuckDB search-space table '{TableName}' does not exist in '{GetDatabaseFilePathInternal()}'. " + + "This almost always means the generator and prediction reader are using different DuckDB filenames, " + + "or prediction started before QuantDatabaseService initialized/rebuilt the search-space table."); + } +} \ No newline at end of file diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 6b70370..d282ada 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -245,7 +245,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q2_K_XL.ggufy baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-Q2_K_XL @@ -260,7 +260,7 @@ baselines: display_name: UD-Q3_K_XL force_relearn: false allow_as_learning_baseline: true - allow_as_combination_carrier: true + allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf @@ -269,7 +269,7 @@ baselines: display_name: UD-Q4_K_XL force_relearn: false allow_as_learning_baseline: true - allow_as_combination_carrier: true + allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf @@ -287,5 +287,5 @@ baselines: display_name: UD-Q6_K_XL force_relearn: false allow_as_learning_baseline: true - allow_as_combination_carrier: true + allow_as_combination_carrier: false allow_as_explicit_group_candidate: true From 562d0c7e8c3ab759f6c2525909ab7e2ff78ea47e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 3 May 2026 13:57:42 -0400 Subject: [PATCH 185/258] Still hammering out kinks. Not doing well on lower bit ranges, but it's looking way better rn. --- MagicQuant/Models/HybridFinalizationModels.cs | 4 +- .../Models/PredictionSelectionModels.cs | 14 + .../PredictionGuidedHybridSelectionService.cs | 837 +++++++++++++++++- .../Services/RemainingCombinationStore.cs | 192 +++- .../SelectionDiagnosticsLogService.cs | 79 +- MagicQuant/config.default.yaml | 2 +- MagicQuant/config.dev.yaml | 4 +- 7 files changed, 1069 insertions(+), 63 deletions(-) diff --git a/MagicQuant/Models/HybridFinalizationModels.cs b/MagicQuant/Models/HybridFinalizationModels.cs index b1cd787..67276d8 100644 --- a/MagicQuant/Models/HybridFinalizationModels.cs +++ b/MagicQuant/Models/HybridFinalizationModels.cs @@ -142,6 +142,7 @@ public sealed class BenchmarkSnapshotRecord public string DisplayName { get; init; } = string.Empty; public string ProviderName { get; init; } = string.Empty; public string BaselineFamily { get; init; } = string.Empty; + /// /// True only for MagicQuant-discovered mixed tensor configurations. /// Exact/base-only blankets and uniform external rebuilt baselines are not hybrids. @@ -161,6 +162,7 @@ public sealed class BenchmarkSnapshotRecord /// overrides are only exact/native anchors or a uniform external baseline rebuild. /// public bool IsMaterializedTensorMapped { get; init; } + public ulong SizeBytes { get; init; } public double Kld { get; init; } public double Ppl { get; init; } @@ -235,4 +237,4 @@ public sealed class CombinationSurvivalExecutionResult public SurvivalStageReport SurvivalReport { get; init; } = new(); public IReadOnlyList Eliminations { get; init; } = Array.Empty(); public IReadOnlyList ValidationFailures { get; init; } = Array.Empty(); -} \ No newline at end of file +} diff --git a/MagicQuant/Models/PredictionSelectionModels.cs b/MagicQuant/Models/PredictionSelectionModels.cs index 24498f0..9cca43f 100644 --- a/MagicQuant/Models/PredictionSelectionModels.cs +++ b/MagicQuant/Models/PredictionSelectionModels.cs @@ -79,6 +79,19 @@ public sealed class HybridSelectionCandidate public double PredictedGainOverLine { get; init; } public int AttemptOrder { get; init; } public string WindowLabel { get; init; } = string.Empty; + + // Diagnostic-only context captured at selection time. These values do not + // change acceptance rules; they explain how the candidate was found, how + // many neighbors existed, and how hard the retry aperture was capped. + public long CandidatePoolSize { get; init; } + public long WindowCandidateCount { get; init; } + public long LineBeatingCandidateCount { get; init; } + public int FetchedCandidateCount { get; init; } + public int CandidatesAfterBrutalityCount { get; init; } + public int CandidateAttemptLimit { get; init; } + public int PhaseWindowIndex { get; init; } + public int PhaseWindowCount { get; init; } + public IReadOnlyList CandidateSelectionNotes { get; init; } = Array.Empty(); } public sealed class CandidateValidationResult @@ -87,6 +100,7 @@ public sealed class CandidateValidationResult public BenchmarkSnapshotRecord? Snapshot { get; init; } public bool Accepted { get; init; } public string Message { get; init; } = string.Empty; + public string FailureCode { get; init; } = string.Empty; } public sealed class BaselineEliminationRecord diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index aa6f694..038bb5a 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -1,5 +1,7 @@ +using System.Text.Json; using MagicQuant.Models; using MagicQuant.Services.Progress; +using MQ.DB; using MQ.DB.Models; using Spectre.Console; @@ -17,6 +19,14 @@ namespace MagicQuant.Services; /// public sealed class PredictionGuidedHybridSelectionService { + private const int DiagnosticPreviewLimit = 25; + private const int DiagnosticPreviewDisplayCount = 8; + + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = true + }; + private readonly QuantizationService _quantizationService; private readonly HybridBenchmarkRepository _repository; private readonly FinalRealBenchmarkEliminationService _finalEliminator; @@ -40,17 +50,20 @@ public async Task RunAsync( { var eliminationRecords = new List(); var validationFailures = new List(); + var validationAttempts = new List(); + var phaseDiagnostics = new List(); var current = _finalEliminator.Eliminate(pureBaselineSnapshots).Survivors.ToList(); AnsiConsole.MarkupLine($"[green]Pure/current anchor survivors after dominance:[/] [cyan]{current.Count:N0}[/]"); + PrintAnchorFrontier(current, "Initial anchor frontier after dominance"); - var strict = await RunStrictDominanceReplacementAsync(current, eliminationRecords, validationFailures, ct); + var strict = await RunStrictDominanceReplacementAsync(current, eliminationRecords, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, strict.AcceptedSnapshots, eliminationRecords, "strict predicted hybrid dominance validated by real benchmark"); - var near = await RunNearBaselineReplacementAsync(current, eliminationRecords, validationFailures, ct); + var near = await RunNearBaselineReplacementAsync(current, eliminationRecords, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, near.AcceptedSnapshots, eliminationRecords, "near-baseline size-premium replacement validated by real benchmark"); - var interior = await RunInteriorSubspaceDiscoveryAsync(current, validationFailures, ct); + var interior = await RunInteriorSubspaceDiscoveryAsync(current, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, interior.AcceptedSnapshots, eliminationRecords, "interior subspace discovery dominated by real benchmark truth"); current = ApplyMeaningfulSpacing(current, eliminationRecords); @@ -72,6 +85,8 @@ public async Task RunAsync( } } + await WriteSelectionPhaseDiagnosticsAsync(phaseDiagnostics, validationFailures, validationAttempts, ct); + return new PredictionGuidedSelectionResult { Survivors = finalDominance.Survivors.ToList(), @@ -86,9 +101,12 @@ private async Task RunStrictDominanceReplacementAsync( IReadOnlyList currentAnchors, List eliminations, List validationFailures, + List validationAttempts, + List phaseDiagnostics, CancellationToken ct) { AnsiConsole.Write(new Rule("[yellow]Prediction Phase 1: Strict Hybrid Dominance[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[grey]Strict dominance retry policy:[/] max attempts per anchor=[cyan]{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}[/], epsilon=[cyan]{Config.SelectionMinimumKldImprovementEpsilon:0.########}[/]"); var accepted = new List(); @@ -97,11 +115,65 @@ private async Task RunStrictDominanceReplacementAsync( if (ShouldSkipAnchorReplacement(anchor)) { AnsiConsole.MarkupLine($"[grey]Skipping 8-bit anchor replacement attempts:[/] {Markup.Escape(anchor.DisplayName)}"); + phaseDiagnostics.Add(new SelectionPhaseDiagnostic + { + Phase = "StrictDominanceReplacement", + WindowLabel = $"strict <= {anchor.DisplayName}", + HigherDamageSmaller = ToAnchorLog(anchor), + LowerDamageLarger = ToAnchorLog(anchor), + WindowMinSizeBytes = 0, + WindowMaxSizeBytes = anchor.SizeBytes, + CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, + Notes = ["Skipped because SelectionAllowEightBitAnchorReplacements=false and anchor is an 8-bit/non-exact anchor."] + }); continue; } + long poolCount = await _predictedStore.CountStrictDominanceCandidatesAsync(anchor, ct); var strictRows = await _predictedStore.QueryStrictDominanceCandidatesAsync(anchor, Config.SelectionMaxFallbackAttemptsPerAnchor, ct); - var candidates = strictRows.Select((x, i) => new HybridSelectionCandidate { Prediction = x, Reason = HybridSelectionReason.StrictDominanceReplacement, LowerDamageAnchor = anchor, HigherDamageAnchor = anchor, WindowMinSizeBytes = 0, WindowMaxSizeBytes = anchor.SizeBytes, LinearExpectedKld = anchor.Kld, PredictedGainOverLine = anchor.Kld - x.PredictedKld, AttemptOrder = i + 1, WindowLabel = $"strict <= {anchor.DisplayName}" }).ToList(); + var candidates = strictRows.Select((x, i) => new HybridSelectionCandidate + { + Prediction = x, + Reason = HybridSelectionReason.StrictDominanceReplacement, + LowerDamageAnchor = anchor, + HigherDamageAnchor = anchor, + WindowMinSizeBytes = 0, + WindowMaxSizeBytes = anchor.SizeBytes, + LinearExpectedKld = anchor.Kld, + PredictedGainOverLine = anchor.Kld - x.PredictedKld, + AttemptOrder = i + 1, + WindowLabel = $"strict <= {anchor.DisplayName}", + CandidatePoolSize = poolCount, + WindowCandidateCount = poolCount, + LineBeatingCandidateCount = poolCount, + FetchedCandidateCount = strictRows.Count, + CandidatesAfterBrutalityCount = strictRows.Count, + CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, + PhaseWindowIndex = 1, + PhaseWindowCount = 1, + CandidateSelectionNotes = ["Strict query requires predicted size <= anchor size and predicted KLD + epsilon < anchor KLD."] + }).ToList(); + + var diag = new SelectionPhaseDiagnostic + { + Phase = "StrictDominanceReplacement", + WindowLabel = $"strict <= {anchor.DisplayName}", + HigherDamageSmaller = ToAnchorLog(anchor), + LowerDamageLarger = ToAnchorLog(anchor), + WindowMinSizeBytes = 0, + WindowMaxSizeBytes = anchor.SizeBytes, + CandidatePoolSize = poolCount, + WindowCandidateCount = poolCount, + LineBeatingCandidateCount = poolCount, + FetchedCandidateCount = strictRows.Count, + CandidatesAfterBrutalityCount = strictRows.Count, + SelectedForValidationCount = candidates.Count, + CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, + TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList() + }; + phaseDiagnostics.Add(diag); + + AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, selected={candidates.Count:N0}/{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}"); if (candidates.Count == 0) continue; @@ -116,6 +188,8 @@ private async Task RunStrictDominanceReplacementAsync( $"must be <= {anchor.SizeBytes:N0} bytes and lower KLD than {anchor.DisplayName}", ct); + validationAttempts.Add(validation); + if (validation.Accepted && validation.Snapshot != null) { acceptedForAnchor = validation; @@ -145,20 +219,41 @@ private async Task RunNearBaselineReplacementAsync( IReadOnlyList currentAnchors, List eliminations, List validationFailures, + List validationAttempts, + List phaseDiagnostics, CancellationToken ct) { AnsiConsole.Write(new Rule("[yellow]Prediction Phase 2: Near-Baseline Replacement[/]") { Justification = Justify.Left }); var accepted = new List(); var pairs = BuildAdjacentPairs(currentAnchors); + int attemptLimit = Math.Max(1, Config.SelectionMaxFallbackAttemptsPerAnchor); + int fetchLimit = Math.Max(DiagnosticPreviewLimit, attemptLimit * 3); - foreach (var pair in pairs) + AnsiConsole.MarkupLine($"[grey]Near-baseline neighbor pairs:[/] [cyan]{pairs.Count:N0}[/] | size premium=[cyan]{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}%[/] | fetch limit=[cyan]{fetchLimit:N0}[/] | validation attempts/window=[cyan]{attemptLimit:N0}[/]"); + + for (int pairIndex = 0; pairIndex < pairs.Count; pairIndex++) { + var pair = pairs[pairIndex]; var lowerSizeHigherDamage = pair.HigherDamageSmaller; var upperSizeLowerDamage = pair.LowerDamageLarger; + string windowLabel = $"near-baseline +{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}% {lowerSizeHigherDamage.DisplayName}"; if (ShouldSkipAnchorReplacement(lowerSizeHigherDamage)) + { + AnsiConsole.MarkupLine($"[grey]Skipping near-baseline lower anchor replacement:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)}"); + phaseDiagnostics.Add(new SelectionPhaseDiagnostic + { + Phase = "NearBaselineReplacement", + WindowLabel = windowLabel, + PhaseWindowIndex = pairIndex + 1, + PhaseWindowCount = pairs.Count, + HigherDamageSmaller = ToAnchorLog(lowerSizeHigherDamage), + LowerDamageLarger = ToAnchorLog(upperSizeLowerDamage), + Notes = ["Skipped because the smaller/higher-damage anchor is an 8-bit/non-exact anchor and SelectionAllowEightBitAnchorReplacements=false."] + }); continue; + } ulong min = lowerSizeHigherDamage.SizeBytes; ulong max = AddPercent(min, Config.SelectionNearBaselineMaxSizeGrowthPercent); @@ -166,7 +261,78 @@ private async Task RunNearBaselineReplacementAsync( if (max > upperSizeLowerDamage.SizeBytes) max = upperSizeLowerDamage.SizeBytes; - var candidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync(lowerSizeHigherDamage, upperSizeLowerDamage, min, max, HybridSelectionReason.NearBaselineOnePercentReplacement, $"near-baseline +{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}% {lowerSizeHigherDamage.DisplayName}", Config.SelectionMaxFallbackAttemptsPerAnchor * 3, ct)).Where(PassesNearLowerAnchorBrutality).Take(Config.SelectionMaxFallbackAttemptsPerAnchor).ToList(); + long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(min, max, ct); + long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(lowerSizeHigherDamage, upperSizeLowerDamage, min, max, ct); + var rawCandidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync( + lowerSizeHigherDamage, + upperSizeLowerDamage, + min, + max, + HybridSelectionReason.NearBaselineOnePercentReplacement, + windowLabel, + fetchLimit, + ct)).ToList(); + + var brutalityAnalyses = rawCandidates + .Select(x => new { Candidate = x, Brutality = AnalyzeNearLowerAnchorBrutality(x) }) + .ToList(); + + var candidates = brutalityAnalyses + .Where(x => x.Brutality.Passed) + .Select(x => AttachSelectionDiagnostics( + x.Candidate, + poolSize: lineBeaters, + windowCandidateCount: windowRows, + lineBeatingCandidateCount: lineBeaters, + fetchedCandidateCount: rawCandidates.Count, + candidatesAfterBrutalityCount: brutalityAnalyses.Count(y => y.Brutality.Passed), + candidateAttemptLimit: attemptLimit, + phaseWindowIndex: pairIndex + 1, + phaseWindowCount: pairs.Count, + notes: [x.Brutality.Explanation])) + .Take(attemptLimit) + .ToList(); + + var rejectedByBrutality = brutalityAnalyses + .Where(x => !x.Brutality.Passed) + .Take(DiagnosticPreviewDisplayCount) + .Select(x => ToCandidatePreviewLog(x.Candidate, x.Brutality)) + .ToList(); + + var diag = new SelectionPhaseDiagnostic + { + Phase = "NearBaselineReplacement", + WindowLabel = windowLabel, + PhaseWindowIndex = pairIndex + 1, + PhaseWindowCount = pairs.Count, + HigherDamageSmaller = ToAnchorLog(lowerSizeHigherDamage), + LowerDamageLarger = ToAnchorLog(upperSizeLowerDamage), + WindowMinSizeBytes = min, + WindowMaxSizeBytes = max, + WindowSizeGiB = ToGiB(max > min ? max - min : 0), + CandidatePoolSize = lineBeaters, + WindowCandidateCount = windowRows, + LineBeatingCandidateCount = lineBeaters, + FetchedCandidateCount = rawCandidates.Count, + CandidatesAfterBrutalityCount = brutalityAnalyses.Count(x => x.Brutality.Passed), + SelectedForValidationCount = candidates.Count, + CandidateAttemptLimit = attemptLimit, + QueryFetchLimit = fetchLimit, + TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), + RejectedByBrutalityPreview = rejectedByBrutality, + Notes = [ + "Near-baseline first counts predicted hybrids inside the near-size window, then counts candidates predicted to beat the local line, then applies near-lower-anchor brutality, then caps validation attempts.", + $"Brutal zone fraction={Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###}; required gain fraction of pair KLD gap={Config.SelectionNearAnchorRequiredKldGainFractionOfPairGap:0.###}." + ] + }; + phaseDiagnostics.Add(diag); + + AnsiConsole.MarkupLine( + $"[grey]Near-baseline window {pairIndex + 1:N0}/{pairs.Count:N0}:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)} -> {Markup.Escape(upperSizeLowerDamage.DisplayName)} " + + $"| rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, fetched={rawCandidates.Count:N0}, after-brutality={diag.CandidatesAfterBrutalityCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}"); + + if (rejectedByBrutality.Count > 0) + AnsiConsole.MarkupLine($"[grey] rejected by near-lower-anchor brutality preview:[/] [cyan]{rejectedByBrutality.Count:N0}[/] (see magicquant-selection-phase-diagnostics.json)"); if (candidates.Count == 0) continue; @@ -181,6 +347,8 @@ private async Task RunNearBaselineReplacementAsync( $"must land inside {min:N0}..{max:N0} bytes and beat the real linear KLD line", ct); + validationAttempts.Add(validation); + if (validation.Accepted && validation.Snapshot != null) { accepted.Add(validation.Snapshot); @@ -203,17 +371,28 @@ private async Task RunNearBaselineReplacementAsync( private async Task RunInteriorSubspaceDiscoveryAsync( IReadOnlyList currentAnchors, List validationFailures, + List validationAttempts, + List phaseDiagnostics, CancellationToken ct) { AnsiConsole.Write(new Rule("[yellow]Prediction Phase 3: Interior Subspace Discovery[/]") { Justification = Justify.Left }); var accepted = new List(); var pairs = BuildAdjacentPairs(currentAnchors); + var fractions = Config.SelectionInteriorWindowFractions.ToList(); + + int interiorAttemptLimit = Math.Max(1, Math.Max(Config.SelectionMaxCandidatesPerInteriorWindow, Config.SelectionMaxFallbackAttemptsPerAnchor)); + int interiorFetchLimit = Math.Max(interiorAttemptLimit, DiagnosticPreviewLimit); + + AnsiConsole.MarkupLine($"[grey]Interior neighbor pairs:[/] [cyan]{pairs.Count:N0}[/] | window fractions=[cyan]{Markup.Escape(string.Join(", ", fractions.Select(x => x.ToString("0.###"))))}[/] | candidates/window=[cyan]{Config.SelectionMaxCandidatesPerInteriorWindow:N0}[/] | fallback attempts/window=[cyan]{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}[/] | validation attempts/window=[cyan]{interiorAttemptLimit:N0}[/] | fetch preview/window=[cyan]{interiorFetchLimit:N0}[/]"); var allCandidates = new List(); + int globalWindowIndex = 0; + int estimatedWindowCount = pairs.Sum(pair => EstimateInteriorWindowCount(pair, fractions)); - foreach (var pair in pairs) + for (int pairIndex = 0; pairIndex < pairs.Count; pairIndex++) { + var pair = pairs[pairIndex]; ulong lowSize = pair.HigherDamageSmaller.SizeBytes; ulong highSize = pair.LowerDamageLarger.SizeBytes; @@ -223,9 +402,9 @@ private async Task RunInteriorSubspaceDiscoveryAsync( ulong span = highSize - lowSize; ulong cursor = lowSize; - for (int i = 0; i < Config.SelectionInteriorWindowFractions.Count; i++) + for (int i = 0; i < fractions.Count; i++) { - double fraction = Config.SelectionInteriorWindowFractions[i]; + double fraction = fractions[i]; if (fraction <= 0d) continue; @@ -234,14 +413,85 @@ private async Task RunInteriorSubspaceDiscoveryAsync( continue; ulong min = cursor; - ulong max = i == Config.SelectionInteriorWindowFractions.Count - 1 + ulong max = i == fractions.Count - 1 ? Math.Min(highSize, cursor + width) : Math.Min(highSize, cursor + width); if (max <= min) continue; - allCandidates.AddRange((await _predictedStore.QueryBetterThanLinearCandidatesAsync(pair.HigherDamageSmaller, pair.LowerDamageLarger, min, max, HybridSelectionReason.InteriorSubspaceDiscovery, $"interior {i + 1}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}", Config.SelectionMaxCandidatesPerInteriorWindow, ct)).Where(PassesNearLowerAnchorBrutality)); + globalWindowIndex++; + string windowLabel = $"interior {i + 1}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}"; + long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(min, max, ct); + long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(pair.HigherDamageSmaller, pair.LowerDamageLarger, min, max, ct); + + var rawCandidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync( + pair.HigherDamageSmaller, + pair.LowerDamageLarger, + min, + max, + HybridSelectionReason.InteriorSubspaceDiscovery, + windowLabel, + interiorFetchLimit, + ct)).ToList(); + + var brutalityAnalyses = rawCandidates + .Select(x => new { Candidate = x, Brutality = AnalyzeNearLowerAnchorBrutality(x) }) + .ToList(); + + int afterBrutalityCount = brutalityAnalyses.Count(y => y.Brutality.Passed); + + var kept = brutalityAnalyses + .Where(x => x.Brutality.Passed) + .Select(x => AttachSelectionDiagnostics( + x.Candidate, + poolSize: lineBeaters, + windowCandidateCount: windowRows, + lineBeatingCandidateCount: lineBeaters, + fetchedCandidateCount: rawCandidates.Count, + candidatesAfterBrutalityCount: afterBrutalityCount, + candidateAttemptLimit: interiorAttemptLimit, + phaseWindowIndex: globalWindowIndex, + phaseWindowCount: estimatedWindowCount, + notes: [x.Brutality.Explanation])) + .Take(interiorAttemptLimit) + .ToList(); + + allCandidates.AddRange(kept); + + var rejectedByBrutality = brutalityAnalyses + .Where(x => !x.Brutality.Passed) + .Take(DiagnosticPreviewDisplayCount) + .Select(x => ToCandidatePreviewLog(x.Candidate, x.Brutality)) + .ToList(); + + phaseDiagnostics.Add(new SelectionPhaseDiagnostic + { + Phase = "InteriorSubspaceDiscovery", + WindowLabel = windowLabel, + PhaseWindowIndex = globalWindowIndex, + PhaseWindowCount = estimatedWindowCount, + HigherDamageSmaller = ToAnchorLog(pair.HigherDamageSmaller), + LowerDamageLarger = ToAnchorLog(pair.LowerDamageLarger), + WindowMinSizeBytes = min, + WindowMaxSizeBytes = max, + WindowSizeGiB = ToGiB(max > min ? max - min : 0), + CandidatePoolSize = lineBeaters, + WindowCandidateCount = windowRows, + LineBeatingCandidateCount = lineBeaters, + FetchedCandidateCount = rawCandidates.Count, + CandidatesAfterBrutalityCount = afterBrutalityCount, + SelectedForValidationCount = kept.Count, + CandidateAttemptLimit = interiorAttemptLimit, + QueryFetchLimit = interiorFetchLimit, + TopCandidates = kept.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), + RejectedByBrutalityPreview = rejectedByBrutality, + Notes = ["Interior candidates are gathered per window, then globally deduped by tensor config before batch validation."] + }); + + AnsiConsole.MarkupLine( + $"[grey]Interior window {globalWindowIndex:N0}/{Math.Max(estimatedWindowCount, globalWindowIndex):N0}:[/] {Markup.Escape(pair.HigherDamageSmaller.DisplayName)} -> {Markup.Escape(pair.LowerDamageLarger.DisplayName)} " + + $"| rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, fetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, selected={kept.Count:N0}/{interiorAttemptLimit:N0}"); cursor = max; @@ -257,13 +507,17 @@ private async Task RunInteriorSubspaceDiscoveryAsync( .ThenBy(x => x.Prediction.PredictedSizeBytes) .ToList(); + int duplicateCount = Math.Max(0, allCandidates.Count - deduped.Count); + AnsiConsole.MarkupLine($"[grey]Interior candidate rollup:[/] raw-after-brutality={allCandidates.Count:N0}, duplicate-configs-removed={duplicateCount:N0}, selected-for-batch={deduped.Count:N0}"); + if (deduped.Count == 0) { - AnsiConsole.MarkupLine("[grey]No predicted interior candidates beat their local linear KLD lines.[/]"); + AnsiConsole.MarkupLine("[grey]No predicted interior candidates beat their local linear KLD lines after window/brutality filtering.[/]"); return new PhaseValidationResult(); } AnsiConsole.MarkupLine($"[grey]Interior candidates selected for batch validation:[/] [cyan]{deduped.Count:N0}[/]"); + PrintCandidatePreviewTable(deduped, "Interior selected candidates preview"); var quantBatch = deduped.Select(x => x.Prediction.Quant).DistinctBy(x => TensorConfigIdentity.ToKey((TensorConfig)x)).ToList(); var summary = await _quantizationService.ProcessHybridBatchAsync( @@ -287,21 +541,33 @@ private async Task RunInteriorSubspaceDiscoveryAsync( snapshot.SizeBytes <= candidate.WindowMaxSizeBytes && BeatsLinearKldLine(snapshot.SizeBytes, snapshot.Kld, candidate.HigherDamageAnchor, candidate.LowerDamageAnchor); + var validation = new CandidateValidationResult + { + Candidate = candidate, + Snapshot = snapshot, + Accepted = acceptedCandidate, + FailureCode = acceptedCandidate ? string.Empty : snapshot == null ? "SNAPSHOT_MISSING_AFTER_BATCH" : "REAL_BENCHMARK_DID_NOT_BEAT_LINE", + Message = acceptedCandidate + ? "validated interior candidate" + : snapshot == null + ? $"benchmark snapshot was not found after batch build (requested={summary.Requested}, completed={summary.Completed}, skipped={summary.Skipped}, failed={summary.Failed})" + : BuildDetailedFailureMessage(candidate, snapshot, "real benchmark did not beat the local linear KLD line inside the requested size window") + }; + validationAttempts.Add(validation); + if (acceptedCandidate && snapshot != null) { accepted.Add(snapshot); + PrintCandidateValidationOutcome(candidate, snapshot, accepted: true, "validated interior candidate"); continue; } - validationFailures.Add(new CandidateValidationResult - { - Candidate = candidate, - Snapshot = snapshot, - Accepted = false, - Message = snapshot == null - ? "benchmark snapshot was not found after batch build" - : "real benchmark did not beat the local linear KLD line inside the requested size window" - }); + if (snapshot != null) + PrintCandidateValidationOutcome(candidate, snapshot, accepted: false, validation.Message); + else + AnsiConsole.MarkupLine($"[yellow]Rejected predicted candidate:[/] {Markup.Escape(validation.Message)}"); + + validationFailures.Add(validation); } return new PhaseValidationResult { AcceptedSnapshots = accepted }; @@ -315,7 +581,9 @@ private async Task BuildAndValidateSingleAsync( { AnsiConsole.MarkupLine( $"[grey]Validating candidate:[/] {Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(candidate.Prediction.Quant))} " + - $"[grey]| reason=[/] {candidate.Reason} [grey]| window=[/] {Markup.Escape(candidate.WindowLabel)}"); + $"[grey]| reason=[/] {candidate.Reason} [grey]| attempt=[/] {candidate.AttemptOrder:N0}/{Math.Max(candidate.CandidateAttemptLimit, candidate.AttemptOrder):N0} " + + $"[grey]| window=[/] {Markup.Escape(candidate.WindowLabel)}"); + PrintCandidatePredictionLine(candidate); var summary = await _quantizationService.ProcessHybridBatchAsync(new[] { candidate.Prediction.Quant }, ct); var snapshot = await _repository.LoadBenchmarkSnapshotAsync(candidate.Prediction.Config, ct); @@ -325,11 +593,15 @@ private async Task BuildAndValidateSingleAsync( ? "validated" : snapshot == null ? $"no benchmark snapshot was available after build attempt (completed={summary.Completed}, skipped={summary.Skipped}, failed={summary.Failed})" - : $"failed expectation: {expectation}; actual size={snapshot.SizeBytes:N0}, actual KLD={snapshot.Kld:0.000000}"; + : BuildDetailedFailureMessage(candidate, snapshot, $"failed expectation: {expectation}"); if (accepted && snapshot != null) { - AnsiConsole.MarkupLine($"[green]Validated:[/] {Markup.Escape(snapshot.DisplayName)} size={snapshot.SizeBytes:N0} KLD={snapshot.Kld:0.000000}"); + PrintCandidateValidationOutcome(candidate, snapshot, accepted: true, "validated"); + } + else if (snapshot != null) + { + PrintCandidateValidationOutcome(candidate, snapshot, accepted: false, message); } else { @@ -341,35 +613,95 @@ private async Task BuildAndValidateSingleAsync( Candidate = candidate, Snapshot = snapshot, Accepted = accepted, + FailureCode = accepted ? string.Empty : snapshot == null ? "SNAPSHOT_MISSING_AFTER_BUILD" : "REAL_BENCHMARK_FAILED_EXPECTATION", Message = message }; } - private static bool PassesNearLowerAnchorBrutality(HybridSelectionCandidate candidate) + private static HybridSelectionCandidate AttachSelectionDiagnostics( + HybridSelectionCandidate candidate, + long poolSize, + long windowCandidateCount, + long lineBeatingCandidateCount, + int fetchedCandidateCount, + int candidatesAfterBrutalityCount, + int candidateAttemptLimit, + int phaseWindowIndex, + int phaseWindowCount, + IReadOnlyList notes) + { + return new HybridSelectionCandidate + { + Prediction = candidate.Prediction, + Reason = candidate.Reason, + LowerDamageAnchor = candidate.LowerDamageAnchor, + HigherDamageAnchor = candidate.HigherDamageAnchor, + WindowMinSizeBytes = candidate.WindowMinSizeBytes, + WindowMaxSizeBytes = candidate.WindowMaxSizeBytes, + LinearExpectedKld = candidate.LinearExpectedKld, + PredictedGainOverLine = candidate.PredictedGainOverLine, + AttemptOrder = candidate.AttemptOrder, + WindowLabel = candidate.WindowLabel, + CandidatePoolSize = poolSize, + WindowCandidateCount = windowCandidateCount, + LineBeatingCandidateCount = lineBeatingCandidateCount, + FetchedCandidateCount = fetchedCandidateCount, + CandidatesAfterBrutalityCount = candidatesAfterBrutalityCount, + CandidateAttemptLimit = candidateAttemptLimit, + PhaseWindowIndex = phaseWindowIndex, + PhaseWindowCount = phaseWindowCount, + CandidateSelectionNotes = notes + }; + } + + private static BrutalityAnalysis AnalyzeNearLowerAnchorBrutality(HybridSelectionCandidate candidate) { ulong span = candidate.LowerDamageAnchor.SizeBytes > candidate.HigherDamageAnchor.SizeBytes ? candidate.LowerDamageAnchor.SizeBytes - candidate.HigherDamageAnchor.SizeBytes : 0; if (span == 0) - return true; + { + return new BrutalityAnalysis + { + Passed = true, + FractionFromSmallAnchor = 1d, + RequiredGain = Config.SelectionMinimumKldImprovementEpsilon, + Explanation = "Brutality passed because anchor span is zero." + }; + } ulong distanceFromSmall = candidate.Prediction.PredictedSizeBytes > candidate.HigherDamageAnchor.SizeBytes ? candidate.Prediction.PredictedSizeBytes - candidate.HigherDamageAnchor.SizeBytes : 0; double fraction = distanceFromSmall / (double)span; - if (fraction > Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan) - return true; - double requiredGain = Math.Max( Config.SelectionMinimumKldImprovementEpsilon, Math.Abs(candidate.HigherDamageAnchor.Kld - candidate.LowerDamageAnchor.Kld) * Config.SelectionNearAnchorRequiredKldGainFractionOfPairGap); - return candidate.PredictedGainOverLine >= requiredGain; + bool passed = fraction > Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan || + candidate.PredictedGainOverLine >= requiredGain; + + string explanation = passed + ? fraction > Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan + ? $"Brutality passed because candidate is outside brutal zone (fraction={fraction:0.###} > {Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###})." + : $"Brutality passed because predicted gain {candidate.PredictedGainOverLine:0.########} >= required gain {requiredGain:0.########}." + : $"Brutality rejected because candidate is inside brutal zone (fraction={fraction:0.###} <= {Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###}) and predicted gain {candidate.PredictedGainOverLine:0.########} < required gain {requiredGain:0.########}."; + + return new BrutalityAnalysis + { + Passed = passed, + FractionFromSmallAnchor = fraction, + RequiredGain = requiredGain, + Explanation = explanation + }; } + private static bool PassesNearLowerAnchorBrutality(HybridSelectionCandidate candidate) => + AnalyzeNearLowerAnchorBrutality(candidate).Passed; + private List ApplyMeaningfulSpacing( IReadOnlyList snapshots, List eliminations) @@ -511,6 +843,38 @@ private static List BuildAdjacentPairs(IReadOnlyList fractions) + { + if (pair.LowerDamageLarger.SizeBytes <= pair.HigherDamageSmaller.SizeBytes) + return 0; + + ulong span = pair.LowerDamageLarger.SizeBytes - pair.HigherDamageSmaller.SizeBytes; + ulong cursor = pair.HigherDamageSmaller.SizeBytes; + int count = 0; + + for (int i = 0; i < fractions.Count; i++) + { + double fraction = fractions[i]; + if (fraction <= 0d) + continue; + + ulong width = (ulong)Math.Round(span * fraction, MidpointRounding.AwayFromZero); + if (width == 0) + continue; + + ulong max = Math.Min(pair.LowerDamageLarger.SizeBytes, cursor + width); + if (max <= cursor) + continue; + + count++; + cursor = max; + if (cursor >= pair.LowerDamageLarger.SizeBytes) + break; + } + + return count; + } + private static bool BeatsLinearKldLine( ulong candidateSize, double candidateKld, @@ -536,6 +900,351 @@ private static double InterpolateKldLine( return higherDamageSmaller.Kld + ((lowerDamageLarger.Kld - higherDamageSmaller.Kld) * t); } + private static ValidationMetrics ComputeValidationMetrics(HybridSelectionCandidate candidate, BenchmarkSnapshotRecord snapshot) + { + double line = InterpolateKldLine(snapshot.SizeBytes, candidate.HigherDamageAnchor, candidate.LowerDamageAnchor); + double gain = line - snapshot.Kld; + bool insideWindow = snapshot.SizeBytes >= candidate.WindowMinSizeBytes && snapshot.SizeBytes <= candidate.WindowMaxSizeBytes; + bool beatsLine = snapshot.Kld + Config.SelectionMinimumKldImprovementEpsilon < line; + long sizeMissBytes = 0; + + if (snapshot.SizeBytes < candidate.WindowMinSizeBytes) + sizeMissBytes = (long)candidate.WindowMinSizeBytes - (long)snapshot.SizeBytes; + else if (snapshot.SizeBytes > candidate.WindowMaxSizeBytes) + sizeMissBytes = (long)snapshot.SizeBytes - (long)candidate.WindowMaxSizeBytes; + + return new ValidationMetrics + { + ActualLineKld = line, + ActualGainOverLine = gain, + KldMiss = snapshot.Kld + Config.SelectionMinimumKldImprovementEpsilon - line, + SizeMissBytes = sizeMissBytes, + InsideWindow = insideWindow, + BeatsLine = beatsLine + }; + } + + private static string BuildDetailedFailureMessage(HybridSelectionCandidate candidate, BenchmarkSnapshotRecord snapshot, string prefix) + { + var metrics = ComputeValidationMetrics(candidate, snapshot); + string sizeText = metrics.SizeMissBytes == 0 ? "inside size window" : $"missed size window by {metrics.SizeMissBytes:N0} bytes"; + string kldText = metrics.KldMiss <= 0 ? "beat required KLD line" : $"missed KLD line by {metrics.KldMiss:0.000000}"; + + return $"{prefix}; actual size={snapshot.SizeBytes:N0} ({ToGiB(snapshot.SizeBytes):0.00} GiB), actual KLD={snapshot.Kld:0.000000}, line={metrics.ActualLineKld:0.000000}, gain={metrics.ActualGainOverLine:0.000000}, {sizeText}, {kldText}"; + } + + private static void PrintCandidatePredictionLine(HybridSelectionCandidate candidate) + { + AnsiConsole.MarkupLine( + $"[grey] predicted:[/] size={candidate.Prediction.PredictedSizeBytes:N0} bytes ({ToGiB(candidate.Prediction.PredictedSizeBytes):0.00} GiB), " + + $"kld={candidate.Prediction.PredictedKld:0.000000}, line={candidate.LinearExpectedKld:0.000000}, gain={candidate.PredictedGainOverLine:0.000000}, " + + $"rank={candidate.Prediction.PredictedRank}, confidence={candidate.Prediction.PredictionConfidence:0.###}"); + AnsiConsole.MarkupLine( + $"[grey] selection context:[/] pool={candidate.CandidatePoolSize:N0}, windowRows={candidate.WindowCandidateCount:N0}, lineBeat={candidate.LineBeatingCandidateCount:N0}, " + + $"fetched={candidate.FetchedCandidateCount:N0}, afterBrutality={candidate.CandidatesAfterBrutalityCount:N0}, attemptLimit={candidate.CandidateAttemptLimit:N0}"); + AnsiConsole.MarkupLine($"[grey] bit space:[/] {Markup.Escape(DescribeBitSpace(candidate.Prediction.Config))}"); + } + + private static void PrintCandidateValidationOutcome(HybridSelectionCandidate candidate, BenchmarkSnapshotRecord snapshot, bool accepted, string message) + { + var metrics = ComputeValidationMetrics(candidate, snapshot); + string status = accepted ? "[green]Validated[/]" : "[yellow]Rejected predicted candidate[/]"; + AnsiConsole.MarkupLine( + $"{status}: {Markup.Escape(snapshot.DisplayName)} | actual size={snapshot.SizeBytes:N0} ({ToGiB(snapshot.SizeBytes):0.00} GiB), " + + $"actual KLD={snapshot.Kld:0.000000}, line={metrics.ActualLineKld:0.000000}, gain={metrics.ActualGainOverLine:0.000000}, " + + $"sizeMiss={metrics.SizeMissBytes:N0}, kldMiss={Math.Max(0d, metrics.KldMiss):0.000000}"); + + if (!accepted) + AnsiConsole.MarkupLine($"[yellow] reason:[/] {Markup.Escape(message)}"); + } + + private static void PrintAnchorFrontier(IReadOnlyList anchors, string title) + { + if (anchors.Count == 0) + return; + + var table = new Table().RoundedBorder().BorderColor(Color.Grey); + table.Title = new TableTitle(Markup.Escape(title)); + table.AddColumn("Order"); + table.AddColumn("Anchor"); + table.AddColumn("Provider"); + table.AddColumn("KLD"); + table.AddColumn("Size GiB"); + + int i = 0; + foreach (var anchor in anchors.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes)) + { + table.AddRow( + (++i).ToString("N0"), + Markup.Escape(anchor.DisplayName), + Markup.Escape(anchor.ProviderName), + anchor.Kld.ToString("0.000000"), + ToGiB(anchor.SizeBytes).ToString("0.00")); + } + + AnsiConsole.Write(table); + } + + private static void PrintCandidatePreviewTable(IReadOnlyList candidates, string title) + { + if (candidates.Count == 0) + return; + + var table = new Table().RoundedBorder().BorderColor(Color.Grey); + table.Title = new TableTitle(Markup.Escape(title)); + table.AddColumn("Attempt"); + table.AddColumn("Candidate"); + table.AddColumn("Pred KLD"); + table.AddColumn("Line"); + table.AddColumn("Gain"); + table.AddColumn("Size GiB"); + table.AddColumn("Rank"); + table.AddColumn("Bit Space"); + + foreach (var c in candidates.Take(DiagnosticPreviewDisplayCount)) + { + table.AddRow( + c.AttemptOrder.ToString("N0"), + Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(c.Prediction.Quant)), + c.Prediction.PredictedKld.ToString("0.000000"), + c.LinearExpectedKld.ToString("0.000000"), + c.PredictedGainOverLine.ToString("0.000000"), + ToGiB(c.Prediction.PredictedSizeBytes).ToString("0.00"), + c.Prediction.PredictedRank?.ToString("N0") ?? "n/a", + Markup.Escape(DescribeBitSpace(c.Prediction.Config))); + } + + AnsiConsole.Write(table); + } + + private static CandidatePreviewLog ToCandidatePreviewLog(HybridSelectionCandidate candidate) => + ToCandidatePreviewLog(candidate, AnalyzeNearLowerAnchorBrutality(candidate)); + + private static CandidatePreviewLog ToCandidatePreviewLog(HybridSelectionCandidate candidate, BrutalityAnalysis brutality) + { + return new CandidatePreviewLog + { + AttemptOrder = candidate.AttemptOrder, + Key = TensorConfigIdentity.ToKey(candidate.Prediction.Config), + DisplayName = HybridBenchmarkRepository.BuildDisplayName(candidate.Prediction.Quant), + PredictedSizeBytes = candidate.Prediction.PredictedSizeBytes, + PredictedSizeGiB = ToGiB(candidate.Prediction.PredictedSizeBytes), + PredictedKld = candidate.Prediction.PredictedKld, + LinearExpectedKld = candidate.LinearExpectedKld, + PredictedGainOverLine = candidate.PredictedGainOverLine, + PredictionConfidence = candidate.Prediction.PredictionConfidence, + PredictionRank = candidate.Prediction.PredictedRank, + BaseQuant = candidate.Prediction.Quant.BaseQuant.Names[0], + BaseBitRange = candidate.Prediction.Quant.BaseQuant.BitRange, + BitSpace = DescribeBitSpace(candidate.Prediction.Config), + OverrideSummary = DescribeOverrides(candidate.Prediction.Config), + BrutalityPassed = brutality.Passed, + BrutalityFractionFromSmallAnchor = brutality.FractionFromSmallAnchor, + BrutalityRequiredGain = brutality.RequiredGain, + BrutalityExplanation = brutality.Explanation + }; + } + + private static object ToAnchorLog(BenchmarkSnapshotRecord anchor) + { + return new + { + key = TensorConfigIdentity.ToKey(anchor.Config), + displayName = anchor.DisplayName, + provider = anchor.ProviderName, + baselineFamily = anchor.BaselineFamily, + sizeBytes = anchor.SizeBytes, + sizeGiB = ToGiB(anchor.SizeBytes), + kld = anchor.Kld, + ppl = anchor.Ppl, + bitRange = anchor.Quant.BaseQuant.BitRange, + quantizeBase = anchor.Quant.BaseQuant.QuantizeBaseArgumentName + }; + } + + private static async Task WriteSelectionPhaseDiagnosticsAsync( + IReadOnlyList phaseDiagnostics, + IReadOnlyList validationFailures, + IReadOnlyList validationAttempts, + CancellationToken ct) + { + string directory = ResolveGgufDirectory(); + Directory.CreateDirectory(directory); + string path = Path.Combine(directory, "magicquant-selection-phase-diagnostics.json"); + string attemptsPath = Path.Combine(directory, "magicquant-selection-validation-attempts.json"); + string missesPath = Path.Combine(directory, "magicquant-selection-validation-misses.json"); + + var payload = new + { + generatedUtc = DateTime.UtcNow, + config = new + { + nearBaselineMaxSizeGrowthPercent = Config.SelectionNearBaselineMaxSizeGrowthPercent, + interiorWindowFractions = Config.SelectionInteriorWindowFractions, + maxCandidatesPerInteriorWindow = Config.SelectionMaxCandidatesPerInteriorWindow, + maxFallbackAttemptsPerAnchor = Config.SelectionMaxFallbackAttemptsPerAnchor, + minimumKldImprovementEpsilon = Config.SelectionMinimumKldImprovementEpsilon, + nearLowerAnchorBrutalZoneFractionOfPairSpan = Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan, + nearAnchorRequiredKldGainFractionOfPairGap = Config.SelectionNearAnchorRequiredKldGainFractionOfPairGap, + allowEightBitAnchorReplacements = Config.SelectionAllowEightBitAnchorReplacements + }, + totals = new + { + phaseWindowCount = phaseDiagnostics.Count, + selectedForValidation = phaseDiagnostics.Sum(x => x.SelectedForValidationCount), + validationAttempts = validationAttempts.Count, + validationAccepted = validationAttempts.Count(x => x.Accepted), + validationMisses = validationFailures.Count(x => !x.Accepted) + }, + windows = phaseDiagnostics + }; + + var attemptsPayload = validationAttempts.Select(ToValidationAttemptLog).ToList(); + var missesPayload = validationFailures.Where(x => !x.Accepted).Select(ToValidationAttemptLog).ToList(); + + await File.WriteAllTextAsync(path, JsonSerializer.Serialize(payload, JsonOptions), ct); + await File.WriteAllTextAsync(attemptsPath, JsonSerializer.Serialize(attemptsPayload, JsonOptions), ct); + await File.WriteAllTextAsync(missesPath, JsonSerializer.Serialize(missesPayload, JsonOptions), ct); + AnsiConsole.MarkupLine($"[green]Selection phase diagnostics log:[/] {Markup.Escape(path)}"); + AnsiConsole.MarkupLine($"[green]Selection validation attempts log:[/] {Markup.Escape(attemptsPath)}"); + AnsiConsole.MarkupLine($"[green]Selection validation miss log:[/] {Markup.Escape(missesPath)}"); + } + + + private static object ToValidationAttemptLog(CandidateValidationResult attempt) + { + var c = attempt.Candidate; + var snap = attempt.Snapshot; + double? actualLine = null; + double? actualGainOverLine = null; + long? sizeMissBytes = null; + double? kldMiss = null; + bool? actualInsideSizeWindow = null; + bool? actualBeatLine = null; + + if (snap != null) + { + actualLine = InterpolateKldLine(snap.SizeBytes, c.HigherDamageAnchor, c.LowerDamageAnchor); + actualGainOverLine = actualLine.Value - snap.Kld; + actualInsideSizeWindow = snap.SizeBytes >= c.WindowMinSizeBytes && snap.SizeBytes <= c.WindowMaxSizeBytes; + actualBeatLine = snap.Kld + Config.SelectionMinimumKldImprovementEpsilon < actualLine.Value; + + if (snap.SizeBytes < c.WindowMinSizeBytes) + sizeMissBytes = (long)c.WindowMinSizeBytes - (long)snap.SizeBytes; + else if (snap.SizeBytes > c.WindowMaxSizeBytes) + sizeMissBytes = (long)snap.SizeBytes - (long)c.WindowMaxSizeBytes; + else + sizeMissBytes = 0; + + kldMiss = snap.Kld + Config.SelectionMinimumKldImprovementEpsilon - actualLine.Value; + } + + return new + { + accepted = attempt.Accepted, + failureCode = attempt.FailureCode, + message = attempt.Message, + reason = c.Reason.ToString(), + attemptOrder = c.AttemptOrder, + attemptLimit = c.CandidateAttemptLimit, + windowLabel = c.WindowLabel, + phaseWindowIndex = c.PhaseWindowIndex, + phaseWindowCount = c.PhaseWindowCount, + candidateKey = TensorConfigIdentity.ToKey(c.Prediction.Config), + candidateInternalName = HybridBenchmarkRepository.BuildDisplayName(c.Prediction.Quant), + bitSpace = DescribeBitSpace(c.Prediction.Config), + overrideSummary = DescribeOverrides(c.Prediction.Config), + baseQuant = c.Prediction.Quant.BaseQuant.Names[0], + baseBitRange = c.Prediction.Quant.BaseQuant.BitRange, + predicted = new + { + sizeBytes = c.Prediction.PredictedSizeBytes, + sizeGiB = ToGiB(c.Prediction.PredictedSizeBytes), + kld = c.Prediction.PredictedKld, + lineKldAtPredictedSize = c.LinearExpectedKld, + gainOverLine = c.PredictedGainOverLine, + confidence = c.Prediction.PredictionConfidence, + rank = c.Prediction.PredictedRank + }, + selectionContext = new + { + candidatePoolSize = c.CandidatePoolSize, + windowCandidateCount = c.WindowCandidateCount, + lineBeatingCandidateCount = c.LineBeatingCandidateCount, + fetchedCandidateCount = c.FetchedCandidateCount, + candidatesAfterBrutalityCount = c.CandidatesAfterBrutalityCount, + candidateAttemptLimit = c.CandidateAttemptLimit, + notes = c.CandidateSelectionNotes + }, + actual = snap == null + ? null + : new + { + displayName = snap.DisplayName, + sizeBytes = snap.SizeBytes, + sizeGiB = ToGiB(snap.SizeBytes), + kld = snap.Kld, + ppl = snap.Ppl, + lineKldAtActualSize = actualLine, + gainOverLine = actualGainOverLine, + insideSizeWindow = actualInsideSizeWindow, + beatLine = actualBeatLine, + sizeMissBytes, + kldMiss, + positiveKldShortfall = kldMiss.HasValue ? Math.Max(0d, kldMiss.Value) : (double?)null + }, + anchors = new + { + higherDamageSmaller = ToAnchorLog(c.HigherDamageAnchor), + lowerDamageLarger = ToAnchorLog(c.LowerDamageAnchor) + } + }; + } + + private static string DescribeBitSpace(TensorConfig config) + { + var baseQuant = BaselineQuants.FromId(config.BaseQuant); + var overrides = DescribeOverrides(config); + return string.IsNullOrWhiteSpace(overrides) + ? $"base={baseQuant.Names[0]}({baseQuant.BitRange}b); overrides=inherit-all" + : $"base={baseQuant.Names[0]}({baseQuant.BitRange}b); overrides={overrides}"; + } + + private static string DescribeOverrides(TensorConfig config) + { + var parts = new List(); + AddOverride(parts, "E", config.Embeddings); + AddOverride(parts, "H", config.LmHead); + AddOverride(parts, "Q", config.AttnQ); + AddOverride(parts, "K", config.AttnKV); + AddOverride(parts, "O", config.AttnOutput); + AddOverride(parts, "U", config.FfnUpGate); + AddOverride(parts, "D", config.FfnDown); + AddOverride(parts, "X", config.MoeExperts); + AddOverride(parts, "R", config.MoeRouter); + return string.Join(", ", parts); + } + + private static void AddOverride(List parts, string groupToken, byte storedSlot) + { + if (BaselineQuants.IsNullTensorConfigGroupSlot(storedSlot)) + return; + + var baseline = BaselineQuants.DecodeTensorConfigGroupSlotToBaseline(storedSlot); + parts.Add($"{groupToken}:{baseline.Names[0]}({baseline.BitRange}b)"); + } + + private static string ResolveGgufDirectory() + { + if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + return Path.Combine(Cache.ModelMagicQuantDirectory!, "GGUF"); + + if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) + return Path.Combine(Cache.MagicQuantDirectory!, "GGUF"); + + return Path.Combine(Directory.GetCurrentDirectory(), "GGUF"); + } + private static ulong AddPercent(ulong bytes, double percent) { if (percent <= 0d) @@ -558,9 +1267,75 @@ private static bool Dominates(BenchmarkSnapshotRecord better, BenchmarkSnapshotR return sameOrSmaller && strictlyLowerKld; } + private static double ToGiB(ulong bytes) => bytes / 1024d / 1024d / 1024d; + private sealed class AdjacentAnchorPair { public BenchmarkSnapshotRecord LowerDamageLarger { get; init; } = default!; public BenchmarkSnapshotRecord HigherDamageSmaller { get; init; } = default!; } -} \ No newline at end of file + + private sealed class BrutalityAnalysis + { + public bool Passed { get; init; } + public double FractionFromSmallAnchor { get; init; } + public double RequiredGain { get; init; } + public string Explanation { get; init; } = string.Empty; + } + + private sealed class ValidationMetrics + { + public double ActualLineKld { get; init; } + public double ActualGainOverLine { get; init; } + public double KldMiss { get; init; } + public long SizeMissBytes { get; init; } + public bool InsideWindow { get; init; } + public bool BeatsLine { get; init; } + } + + private sealed class SelectionPhaseDiagnostic + { + public string Phase { get; init; } = string.Empty; + public string WindowLabel { get; init; } = string.Empty; + public int PhaseWindowIndex { get; init; } + public int PhaseWindowCount { get; init; } + public object? HigherDamageSmaller { get; init; } + public object? LowerDamageLarger { get; init; } + public ulong WindowMinSizeBytes { get; init; } + public ulong WindowMaxSizeBytes { get; init; } + public double WindowSizeGiB { get; init; } + public long CandidatePoolSize { get; init; } + public long WindowCandidateCount { get; init; } + public long LineBeatingCandidateCount { get; init; } + public int FetchedCandidateCount { get; init; } + public int CandidatesAfterBrutalityCount { get; init; } + public int SelectedForValidationCount { get; init; } + public int CandidateAttemptLimit { get; init; } + public int QueryFetchLimit { get; init; } + public IReadOnlyList TopCandidates { get; init; } = Array.Empty(); + public IReadOnlyList RejectedByBrutalityPreview { get; init; } = Array.Empty(); + public IReadOnlyList Notes { get; init; } = Array.Empty(); + } + + private sealed class CandidatePreviewLog + { + public int AttemptOrder { get; init; } + public string Key { get; init; } = string.Empty; + public string DisplayName { get; init; } = string.Empty; + public ulong PredictedSizeBytes { get; init; } + public double PredictedSizeGiB { get; init; } + public double PredictedKld { get; init; } + public double LinearExpectedKld { get; init; } + public double PredictedGainOverLine { get; init; } + public double PredictionConfidence { get; init; } + public ulong? PredictionRank { get; init; } + public string BaseQuant { get; init; } = string.Empty; + public byte BaseBitRange { get; init; } + public string BitSpace { get; init; } = string.Empty; + public string OverrideSummary { get; init; } = string.Empty; + public bool BrutalityPassed { get; init; } + public double BrutalityFractionFromSmallAnchor { get; init; } + public double BrutalityRequiredGain { get; init; } + public string BrutalityExplanation { get; init; } = string.Empty; + } +} diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 0f6d402..9821787 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -3,6 +3,7 @@ using MagicQuant.Models; using MQ.DB; using MQ.DB.Models; +using System.Numerics; using System.Runtime.CompilerServices; namespace MagicQuant.Services; @@ -25,7 +26,7 @@ public async Task CountAsync(CancellationToken ct = default) using var cmd = connection.CreateCommand(); cmd.CommandText = $"SELECT COUNT(*) FROM {TableName};"; - return Convert.ToInt64(await cmd.ExecuteScalarAsync(ct) ?? 0L); + return ToInt64(await cmd.ExecuteScalarAsync(ct)); } public async Task> LoadAllAsync(CancellationToken ct = default) @@ -129,9 +130,9 @@ public async Task GetPredictionStatusAsync(Canc using var r = await cmd.ExecuteReaderAsync(ct); await r.ReadAsync(ct); - long total = Convert.ToInt64(r.GetValue(0)); - long predicted = Convert.ToInt64(r.GetValue(1)); - long ranked = Convert.ToInt64(r.GetValue(2)); + long total = ToInt64(r.GetValue(0)); + long predicted = ToInt64(r.GetValue(1)); + long ranked = ToInt64(r.GetValue(2)); return new PredictionMaterializationStatus { @@ -139,13 +140,95 @@ public async Task GetPredictionStatusAsync(Canc PredictedRows = predicted, MissingPredictionRows = Math.Max(0, total - predicted), RankedRows = ranked, - MinPredictedKld = r.IsDBNull(3) ? null : Convert.ToDouble(r.GetValue(3)), - MaxPredictedKld = r.IsDBNull(4) ? null : Convert.ToDouble(r.GetValue(4)), - MinPredictedSizeBytes = r.IsDBNull(5) ? null : Convert.ToUInt64(r.GetValue(5)), - MaxPredictedSizeBytes = r.IsDBNull(6) ? null : Convert.ToUInt64(r.GetValue(6)) + MinPredictedKld = r.IsDBNull(3) ? null : ToDouble(r.GetValue(3)), + MaxPredictedKld = r.IsDBNull(4) ? null : ToDouble(r.GetValue(4)), + MinPredictedSizeBytes = r.IsDBNull(5) ? null : ToUInt64(r.GetValue(5)), + MaxPredictedSizeBytes = r.IsDBNull(6) ? null : ToUInt64(r.GetValue(6)) }; } + public async Task CountStrictDominanceCandidatesAsync( + BenchmarkSnapshotRecord anchor, + CancellationToken ct = default) + { + string sql = $@" +SELECT COUNT(*) +FROM {TableName} +WHERE PredictedKld IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.HybridPredicateSql} + AND PredictedSizeBytes <= ? + AND PredictedKld + ? < ?;"; + + return await ExecuteCountAsync( + sql, + new object[] { anchor.SizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.Kld }, + ct); + } + + public async Task CountPredictedHybridCandidatesInSizeWindowAsync( + ulong minSize, + ulong maxSize, + CancellationToken ct = default) + { + string sql = $@" +SELECT COUNT(*) +FROM {TableName} +WHERE PredictedKld IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.HybridPredicateSql} + AND PredictedSizeBytes BETWEEN ? AND ?;"; + + return await ExecuteCountAsync(sql, new object[] { minSize, maxSize }, ct); + } + + public async Task CountBetterThanLinearCandidatesAsync( + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger, + ulong minSize, + ulong maxSize, + CancellationToken ct = default) + { + string sql = $@" +WITH scored AS ( + SELECT PredictedKld, + PredictedSizeBytes, + (CAST(? AS DOUBLE) + + ((CAST(PredictedSizeBytes AS DOUBLE) - CAST(? AS DOUBLE)) / GREATEST(CAST(? AS DOUBLE), 1.0)) + * (CAST(? AS DOUBLE) - CAST(? AS DOUBLE))) AS LinearExpectedKld + FROM {TableName} + WHERE PredictedKld IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.HybridPredicateSql} + AND PredictedSizeBytes BETWEEN ? AND ? +) +SELECT COUNT(*) +FROM scored +WHERE LinearExpectedKld - PredictedKld > ?;"; + + double denominator = Math.Max( + (double)lowerDamageLarger.SizeBytes - higherDamageSmaller.SizeBytes, + 1d); + + return await ExecuteCountAsync( + sql, + new object[] + { + higherDamageSmaller.Kld, + (double)higherDamageSmaller.SizeBytes, + denominator, + lowerDamageLarger.Kld, + higherDamageSmaller.Kld, + minSize, + maxSize, + Config.SelectionMinimumKldImprovementEpsilon + }, + ct); + } + public async Task> QueryStrictDominanceCandidatesAsync( BenchmarkSnapshotRecord anchor, int limit, @@ -258,8 +341,8 @@ PredictionRank ASC while (await r.ReadAsync(ct)) { var prediction = MapPredictedRow(r); - double line = Convert.ToDouble(r.GetValue(14)); - double gain = Convert.ToDouble(r.GetValue(15)); + double line = ToDouble(r.GetValue(14)); + double gain = ToDouble(r.GetValue(15)); list.Add(new HybridSelectionCandidate { @@ -310,10 +393,10 @@ private static RankSafePredictionRow MapPredictedRow(System.Data.Common.DbDataRe { Config = config, Quant = (HybridQuant)config, - PredictedKld = Convert.ToDouble(r.GetValue(10)), - PredictedSizeBytes = Convert.ToUInt64(r.GetValue(11)), - PredictionConfidence = Convert.ToDouble(r.GetValue(12)), - PredictedRank = Convert.ToUInt64(r.GetValue(13)), + PredictedKld = ToDouble(r.GetValue(10)), + PredictedSizeBytes = ToUInt64(r.GetValue(11)), + PredictionConfidence = ToDouble(r.GetValue(12)), + PredictedRank = ToUInt64(r.GetValue(13)), IsPredictable = true, IsSizePredictable = true }; @@ -322,16 +405,16 @@ private static RankSafePredictionRow MapPredictedRow(System.Data.Common.DbDataRe private static TensorConfig ReadTensorConfig(System.Data.Common.DbDataReader r) { return new TensorConfig( - Convert.ToByte(r.GetValue(0)), - Convert.ToByte(r.GetValue(1)), - Convert.ToByte(r.GetValue(2)), - Convert.ToByte(r.GetValue(3)), - Convert.ToByte(r.GetValue(4)), - Convert.ToByte(r.GetValue(5)), - Convert.ToByte(r.GetValue(6)), - Convert.ToByte(r.GetValue(7)), - Convert.ToByte(r.GetValue(8)), - Convert.ToByte(r.GetValue(9))); + ToByte(r.GetValue(0)), + ToByte(r.GetValue(1)), + ToByte(r.GetValue(2)), + ToByte(r.GetValue(3)), + ToByte(r.GetValue(4)), + ToByte(r.GetValue(5)), + ToByte(r.GetValue(6)), + ToByte(r.GetValue(7)), + ToByte(r.GetValue(8)), + ToByte(r.GetValue(9))); } private static void AddSlotParameters(DuckDBCommand command, TensorConfig config) @@ -348,6 +431,65 @@ private static void AddSlotParameters(DuckDBCommand command, TensorConfig config command.Parameters.Add(new DuckDBParameter { Value = config.MoeRouter }); } + private async Task ExecuteCountAsync(string sql, object[] args, CancellationToken ct) + { + using var c = new DuckDBConnection(ConnectionString); + await c.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(c, ct); + await EnsureTensorConfigsTableExistsAsync(c, ct); + + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + foreach (var arg in args) + cmd.Parameters.Add(new DuckDBParameter { Value = arg }); + + return ToInt64(await cmd.ExecuteScalarAsync(ct)); + } + + private static long ToInt64(object? value) + { + if (value is null || value is DBNull) + return 0L; + + if (value is BigInteger big) + return (long)big; + + return Convert.ToInt64(value); + } + + private static ulong ToUInt64(object? value) + { + if (value is null || value is DBNull) + return 0UL; + + if (value is BigInteger big) + return (ulong)big; + + return Convert.ToUInt64(value); + } + + private static byte ToByte(object? value) + { + if (value is null || value is DBNull) + return 0; + + if (value is BigInteger big) + return (byte)big; + + return Convert.ToByte(value); + } + + private static double ToDouble(object? value) + { + if (value is null || value is DBNull) + return 0d; + + if (value is BigInteger big) + return (double)big; + + return Convert.ToDouble(value); + } + private static async Task ConfigureFastLoadSessionAsync(DuckDBConnection connection, CancellationToken ct) { using (var cmd = connection.CreateCommand()) @@ -405,7 +547,7 @@ private static async Task EnsureTensorConfigsTableExistsAsync(DuckDBConnection c cmd.CommandText = "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = ?;"; cmd.Parameters.Add(new DuckDBParameter { Value = TableName }); - long matches = Convert.ToInt64(await cmd.ExecuteScalarAsync(ct) ?? 0L); + long matches = ToInt64(await cmd.ExecuteScalarAsync(ct)); if (matches > 0) return; diff --git a/MagicQuant/Services/SelectionDiagnosticsLogService.cs b/MagicQuant/Services/SelectionDiagnosticsLogService.cs index 4e400a5..c2b1cbb 100644 --- a/MagicQuant/Services/SelectionDiagnosticsLogService.cs +++ b/MagicQuant/Services/SelectionDiagnosticsLogService.cs @@ -1,6 +1,7 @@ using System.Text.Json; using MagicQuant.Models; using MQ.DB; +using MQ.DB.Models; using Spectre.Console; namespace MagicQuant.Services; @@ -52,6 +53,8 @@ private static object ToSnapshotLog(BenchmarkSnapshotRecord snap) baselineFamily = snap.BaselineFamily, isHybrid = snap.IsHybrid, isExternalPureBaseline = snap.IsExternalPureBaseline, + isExternalRebuiltBaseline = snap.IsExternalRebuiltBaseline, + isMaterializedTensorMapped = snap.IsMaterializedTensorMapped, sizeBytes = snap.SizeBytes, sizeGiB = ToGb(snap.SizeBytes), kld = snap.Kld, @@ -70,11 +73,15 @@ private static object ToFailureLog(CandidateValidationResult failure) double? actualGainOverLine = null; long? sizeMissBytes = null; double? kldMiss = null; + bool? actualInsideSizeWindow = null; + bool? actualBeatLine = null; if (snap != null) { actualLine = InterpolateKldLine(snap.SizeBytes, c.HigherDamageAnchor, c.LowerDamageAnchor); actualGainOverLine = actualLine.Value - snap.Kld; + actualInsideSizeWindow = snap.SizeBytes >= c.WindowMinSizeBytes && snap.SizeBytes <= c.WindowMaxSizeBytes; + actualBeatLine = snap.Kld + Config.SelectionMinimumKldImprovementEpsilon < actualLine.Value; if (snap.SizeBytes < c.WindowMinSizeBytes) sizeMissBytes = (long)c.WindowMinSizeBytes - (long)snap.SizeBytes; @@ -83,23 +90,43 @@ private static object ToFailureLog(CandidateValidationResult failure) else sizeMissBytes = 0; - kldMiss = snap.Kld - actualLine.Value; + kldMiss = snap.Kld + Config.SelectionMinimumKldImprovementEpsilon - actualLine.Value; } return new { reason = c.Reason.ToString(), attemptOrder = c.AttemptOrder, + attemptLimit = c.CandidateAttemptLimit, windowLabel = c.WindowLabel, + phaseWindowIndex = c.PhaseWindowIndex, + phaseWindowCount = c.PhaseWindowCount, candidateKey = TensorConfigIdentity.ToKey(c.Prediction.Config), candidateInternalName = HybridBenchmarkRepository.BuildDisplayName(c.Prediction.Quant), + bitSpace = DescribeBitSpace(c.Prediction.Config), + overrideSummary = DescribeOverrides(c.Prediction.Config), + baseQuant = c.Prediction.Quant.BaseQuant.Names[0], + baseBitRange = c.Prediction.Quant.BaseQuant.BitRange, + failureCode = failure.FailureCode, predicted = new { sizeBytes = c.Prediction.PredictedSizeBytes, sizeGiB = ToGb(c.Prediction.PredictedSizeBytes), kld = c.Prediction.PredictedKld, lineKldAtPredictedSize = c.LinearExpectedKld, - gainOverLine = c.PredictedGainOverLine + gainOverLine = c.PredictedGainOverLine, + confidence = c.Prediction.PredictionConfidence, + rank = c.Prediction.PredictedRank + }, + selectionContext = new + { + candidatePoolSize = c.CandidatePoolSize, + windowCandidateCount = c.WindowCandidateCount, + lineBeatingCandidateCount = c.LineBeatingCandidateCount, + fetchedCandidateCount = c.FetchedCandidateCount, + candidatesAfterBrutalityCount = c.CandidatesAfterBrutalityCount, + candidateAttemptLimit = c.CandidateAttemptLimit, + notes = c.CandidateSelectionNotes }, actual = snap == null ? null @@ -112,14 +139,24 @@ private static object ToFailureLog(CandidateValidationResult failure) ppl = snap.Ppl, lineKldAtActualSize = actualLine, gainOverLine = actualGainOverLine, + insideSizeWindow = actualInsideSizeWindow, + beatLine = actualBeatLine, sizeMissBytes, - kldMiss + kldMiss, + positiveKldShortfall = kldMiss.HasValue ? (double?)Math.Max(0d, kldMiss.Value) : null }, anchors = new { higherDamageSmaller = ToAnchorLog(c.HigherDamageAnchor), lowerDamageLarger = ToAnchorLog(c.LowerDamageAnchor) }, + acceptancePolicy = new + { + minimumKldImprovementEpsilon = Config.SelectionMinimumKldImprovementEpsilon, + windowMinSizeBytes = c.WindowMinSizeBytes, + windowMaxSizeBytes = c.WindowMaxSizeBytes, + mustBeatLineByEpsilon = true + }, accepted = failure.Accepted, message = failure.Message }; @@ -131,6 +168,8 @@ private static object ToAnchorLog(BenchmarkSnapshotRecord anchor) { key = TensorConfigIdentity.ToKey(anchor.Config), displayName = anchor.DisplayName, + provider = anchor.ProviderName, + baselineFamily = anchor.BaselineFamily, sizeBytes = anchor.SizeBytes, sizeGiB = ToGb(anchor.SizeBytes), kld = anchor.Kld, @@ -155,6 +194,40 @@ private static double InterpolateKldLine( return higherDamageSmaller.Kld + ((lowerDamageLarger.Kld - higherDamageSmaller.Kld) * t); } + + private static string DescribeBitSpace(TensorConfig config) + { + var baseQuant = BaselineQuants.FromId(config.BaseQuant); + var overrides = DescribeOverrides(config); + return string.IsNullOrWhiteSpace(overrides) + ? $"base={baseQuant.Names[0]}({baseQuant.BitRange}b); overrides=inherit-all" + : $"base={baseQuant.Names[0]}({baseQuant.BitRange}b); overrides={overrides}"; + } + + private static string DescribeOverrides(TensorConfig config) + { + var parts = new List(); + AddOverride(parts, "E", config.Embeddings); + AddOverride(parts, "H", config.LmHead); + AddOverride(parts, "Q", config.AttnQ); + AddOverride(parts, "K", config.AttnKV); + AddOverride(parts, "O", config.AttnOutput); + AddOverride(parts, "U", config.FfnUpGate); + AddOverride(parts, "D", config.FfnDown); + AddOverride(parts, "X", config.MoeExperts); + AddOverride(parts, "R", config.MoeRouter); + return string.Join(", ", parts); + } + + private static void AddOverride(List parts, string groupToken, byte storedSlot) + { + if (BaselineQuants.IsNullTensorConfigGroupSlot(storedSlot)) + return; + + var baseline = BaselineQuants.DecodeTensorConfigGroupSlotToBaseline(storedSlot); + parts.Add($"{groupToken}:{baseline.Names[0]}({baseline.BitRange}b)"); + } + private static string ResolveGgufDirectory() { if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index ea13294..3fb3ba0 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -253,7 +253,7 @@ candidate_selection: # Default false: do not spend final prediction/build attempts trying to replace # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. - allow_eight_bit_anchor_replacements: false + allow_eight_bit_anchor_replacements: true output: # Optional explicit output directory. diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index d282ada..b2e006c 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -175,7 +175,7 @@ candidate_selection: # Default false: do not spend final prediction/build attempts trying to replace # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. - allow_eight_bit_anchor_replacements: false + allow_eight_bit_anchor_replacements: true output: # Leave blank to default to /MagicQuant/Final_Outputs @@ -245,7 +245,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q2_K_XL.ggufy + - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-Q2_K_XL From be66b0bc0a8f4c59776802423294a72ceba04225 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 3 May 2026 18:48:45 -0400 Subject: [PATCH 186/258] Big update to predictive engine trying to find an anomaly --- MQ.DB/Data/MagicQuantContext.cs | 7 + ..._PredictionEngineAnomalyDetect.Designer.cs | 1685 +++++++++++++++++ ...503222121_PredictionEngineAnomalyDetect.cs | 363 ++++ .../MagicQuantContextModelSnapshot.cs | 534 ++++++ MQ.DB/Models/DbModels/AnomalyProbeSession.cs | 210 ++ MagicQuant/Config.cs | 5 +- .../Configuration/MagicQuantYamlConfig.cs | 24 + .../Configuration/MagicQuantYamlLoader.cs | 17 + MagicQuant/Models/AnomalyDetectionModels.cs | 141 ++ .../AnomalyAdjustedPredictionService.cs | 266 +++ MagicQuant/Services/AnomalyRuleRepository.cs | 418 ++++ MagicQuant/Services/AnomalyWorkflowService.cs | 1189 ++++++++++++ .../Services/CombinationDuckDbSchema.cs | 20 +- .../CombinationSurvivalPipelineService.cs | 8 + .../DuckDbPredictionMaterializationService.cs | 14 +- .../Services/QuantFidelityComparerService.cs | 483 +++++ .../Services/RemainingCombinationStore.cs | 27 +- MagicQuant/config.default.yaml | 54 + MagicQuant/config.dev.yaml | 54 + 19 files changed, 5501 insertions(+), 18 deletions(-) create mode 100644 MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs create mode 100644 MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs create mode 100644 MQ.DB/Models/DbModels/AnomalyProbeSession.cs create mode 100644 MagicQuant/Models/AnomalyDetectionModels.cs create mode 100644 MagicQuant/Services/AnomalyAdjustedPredictionService.cs create mode 100644 MagicQuant/Services/AnomalyRuleRepository.cs create mode 100644 MagicQuant/Services/AnomalyWorkflowService.cs create mode 100644 MagicQuant/Services/QuantFidelityComparerService.cs diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index c9fca1a..5f05430 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -243,6 +243,10 @@ private static bool IsDesignTime() public DbSet ImatrixDefinitions { get; set; } public DbSet ArchitectureFamilies { get; set; } public DbSet ArchitectureFamilyModelHashes { get; set; } + public DbSet AnomalyProbeSessions { get; set; } + public DbSet AnomalyProbeObservations { get; set; } + public DbSet AnomalyInteractionRules { get; set; } + public DbSet AnomalyInteractionRuleGroupStates { get; set; } // -------------------------------------------------------- @@ -284,6 +288,9 @@ private async Task ValidateImatrixOwnershipBeforeSaveAsync(CancellationToken ct) BenchmarkRun x => (EntityName: nameof(BenchmarkRun), x.AiModelHashId, x.ImatrixDefinitionId), QuantizationRun x => (EntityName: nameof(QuantizationRun), x.AiModelHashId, x.ImatrixDefinitionId), ExecutionPlanProbeCache x => (EntityName: nameof(ExecutionPlanProbeCache), x.AiModelHashId, x.ImatrixDefinitionId), + AnomalyProbeSession x => (EntityName: nameof(AnomalyProbeSession), x.AiModelHashId, x.ImatrixDefinitionId), + AnomalyProbeObservation x => (EntityName: nameof(AnomalyProbeObservation), x.AiModelHashId, x.ImatrixDefinitionId), + AnomalyInteractionRule x => (EntityName: nameof(AnomalyInteractionRule), x.AiModelHashId, x.ImatrixDefinitionId), _ => default }) .Where(x => !string.IsNullOrWhiteSpace(x.EntityName) && x.ImatrixDefinitionId.HasValue) diff --git a/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs b/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs new file mode 100644 index 0000000..8dfb79f --- /dev/null +++ b/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs @@ -0,0 +1,1685 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260503222121_PredictionEngineAnomalyDetect")] + partial class PredictionEngineAnomalyDetect + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "TensorComboId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmarkLearnedSource", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("SourceLearningBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaselineQuantDefinitionId"); + + b.HasIndex("SourceLearningBenchmarkId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("AiBenchmarkId", "TensorGroupId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId"); + + b.ToTable("AiBenchmarkLearnedSources"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("AppliedPredictionSpaceAdjustmentKld") + .HasColumnType("REAL"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("BestActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("BestPredictionSpaceGap") + .HasColumnType("REAL"); + + b.Property("CandidateEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Confidence") + .HasColumnType("REAL"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("EvidenceCount") + .HasColumnType("INTEGER"); + + b.Property("FullTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("GroupCount") + .HasColumnType("INTEGER"); + + b.Property("GroupSetHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("InactiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("MeanActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("MeanPredictionSpaceGap") + .HasColumnType("REAL"); + + b.Property("MetadataJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("MovementClassification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceContextKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ReferenceEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("RuleDirection") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RuleStatus") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RuleType") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ShrinkFactor") + .HasColumnType("REAL"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("FullTensorConfigKey"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "RuleDirection", "RuleStatus"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceQuantId", "GroupSetHash", "RuleDirection") + .IsUnique(); + + b.ToTable("AnomalyInteractionRules"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRuleGroupState", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("CandidateQuantId") + .HasColumnType("INTEGER"); + + b.Property("Movement") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("RuleId") + .HasColumnType("TEXT"); + + b.Property("SortOrder") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("RuleId", "TensorGroupId") + .IsUnique(); + + b.ToTable("AnomalyInteractionRuleGroupStates"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeObservation", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("Accepted") + .HasColumnType("INTEGER"); + + b.Property("ActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("ActualKld") + .HasColumnType("REAL"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("AllActiveGroupsExplicit") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("CandidateEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CandidateQuantsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ChangedGroupSetHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ChangedGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Classification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DowngradeCount") + .HasColumnType("INTEGER"); + + b.Property("FailureCode") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("HypothesisLabel") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("InactiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("IsContextualAnomalyProbe") + .HasColumnType("INTEGER"); + + b.Property("Message") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("MovementClassification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("NetBitDelta") + .HasColumnType("INTEGER"); + + b.Property("OldBf16Isolation") + .HasColumnType("INTEGER"); + + b.Property("PredictedKld") + .HasColumnType("REAL"); + + b.Property("PredictionSpaceGapVsTwin") + .HasColumnType("REAL"); + + b.Property("ProbeTensorComboId") + .HasColumnType("TEXT"); + + b.Property("ProbeTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ProbeType") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceActualKld") + .HasColumnType("REAL"); + + b.Property("ReferenceEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ReferencePredictedKld") + .HasColumnType("REAL"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("ReferenceTensorComboId") + .HasColumnType("TEXT"); + + b.Property("ReferenceTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("RuleDirection") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SameCount") + .HasColumnType("INTEGER"); + + b.Property("SessionId") + .HasColumnType("TEXT"); + + b.Property("SizeSavingsBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("UnknownCount") + .HasColumnType("INTEGER"); + + b.Property("UpgradeCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("ProbeTensorComboId"); + + b.HasIndex("ProbeTensorConfigKey"); + + b.HasIndex("ReferenceTensorComboId"); + + b.HasIndex("ReferenceTensorConfigKey"); + + b.HasIndex("SessionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ChangedGroupSetHash"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceTensorComboId", "ProbeTensorComboId", "ProbeType"); + + b.ToTable("AnomalyProbeObservations"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("ConfigJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("SourceRunLabel") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "StartedUtc"); + + b.ToTable("AnomalyProbeSessions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("NormalizedName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("TensorCount") + .HasColumnType("INTEGER"); + + b.Property("TensorSignatureHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique(); + + b.HasIndex("TensorSignatureHash", "TensorCount"); + + b.ToTable("ArchitectureFamilies"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IsCanonical") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "AiModelHashId") + .IsUnique(); + + b.ToTable("ArchitectureFamilyModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BaselineFamily") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("BitRange") + .HasColumnType("INTEGER"); + + b.Property("CanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ExplicitCandidateSortOrder") + .HasColumnType("INTEGER"); + + b.Property("FirstSeenUtc") + .HasColumnType("TEXT"); + + b.Property("IsActiveInCurrentConfig") + .HasColumnType("INTEGER"); + + b.Property("IsCombinationCarrierCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsCustomBaseline") + .HasColumnType("INTEGER"); + + b.Property("IsExplicitGroupCombinationCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsLearningBaseline") + .HasColumnType("INTEGER"); + + b.Property("LastSeenUtc") + .HasColumnType("TEXT"); + + b.Property("LastUpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("NormalizedCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("NormalizedSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("NormalizedSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("QuantizeBaseArgumentName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RequiresImatrix") + .HasColumnType("INTEGER"); + + b.Property("RuntimeBaselineId") + .HasColumnType("INTEGER"); + + b.Property("ShortSourceName") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceOwner") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("SourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("IsActiveInCurrentConfig"); + + b.HasIndex("ArchitectureFamilyId", "NormalizedCanonicalKey") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "RuntimeBaselineId") + .IsUnique(); + + b.HasIndex("RuntimeBaselineId", "ArchitectureFamilyId"); + + b.HasIndex("ArchitectureFamilyId", "NormalizedSourceRepository", "NormalizedSourceFileName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ArchitectureFamilyId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GpuMemoryLimitsJson") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("MaxCandidateNgl") + .HasColumnType("INTEGER"); + + b.Property("NativeModelSizeBytes") + .HasColumnType("INTEGER"); + + b.Property("NativeQuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("NativeStableNgl") + .HasColumnType("INTEGER"); + + b.Property("ProbeSchemaVersion") + .HasColumnType("INTEGER"); + + b.Property("Q8ModelSizeBytes") + .HasColumnType("INTEGER"); + + b.Property("Q8StableNgl") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("TensorSplitJson") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ArchitectureFamilyId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("BaselineQuantDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorGroupId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ArchitectureFamilyId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorGroupProfile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("FingerprintHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("IsActive") + .HasColumnType("INTEGER"); + + b.Property("SnapshotJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ArchitectureFamilyId", "FingerprintHash") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "IsActive"); + + b.ToTable("TensorGroupProfiles"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmarkLearnedSource", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("LearnedSources") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.BaselineQuantDefinition", "BaselineQuantDefinition") + .WithMany() + .HasForeignKey("BaselineQuantDefinitionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "SourceLearningBenchmark") + .WithMany() + .HasForeignKey("SourceLearningBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("BaselineQuantDefinition"); + + b.Navigation("SourceLearningBenchmark"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRuleGroupState", b => + { + b.HasOne("MQ.DB.Models.DbModels.AnomalyInteractionRule", "Rule") + .WithMany("GroupStates") + .HasForeignKey("RuleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Rule"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeObservation", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "ProbeTensorCombo") + .WithMany() + .HasForeignKey("ProbeTensorComboId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "ReferenceTensorCombo") + .WithMany() + .HasForeignKey("ReferenceTensorComboId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.AnomalyProbeSession", "Session") + .WithMany("Observations") + .HasForeignKey("SessionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("ProbeTensorCombo"); + + b.Navigation("ReferenceTensorCombo"); + + b.Navigation("Session"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade); + + b.Navigation("ArchitectureFamily"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.BaselineQuantDefinition", "BaselineQuantDefinition") + .WithMany() + .HasForeignKey("BaselineQuantDefinitionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("BaselineQuantDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorGroupProfile", b => + { + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ArchitectureFamily"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + + b.Navigation("LearnedSources"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.Navigation("GroupStates"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.Navigation("Observations"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs b/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs new file mode 100644 index 0000000..14e0bad --- /dev/null +++ b/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs @@ -0,0 +1,363 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class PredictionEngineAnomalyDetect : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AnomalyInteractionRules", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + BenchmarkCategory = table.Column(type: "INTEGER", nullable: false), + ReferenceQuantId = table.Column(type: "INTEGER", nullable: false), + ReferenceContextKey = table.Column(type: "TEXT", maxLength: 512, nullable: false), + ReferenceEffectiveGroupsJson = table.Column(type: "TEXT", nullable: false), + CandidateEffectiveGroupsJson = table.Column(type: "TEXT", nullable: false), + InactiveGroupsJson = table.Column(type: "TEXT", nullable: false), + FullTensorConfigKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), + RuleType = table.Column(type: "TEXT", maxLength: 64, nullable: false), + RuleDirection = table.Column(type: "TEXT", maxLength: 64, nullable: false), + RuleStatus = table.Column(type: "TEXT", maxLength: 64, nullable: false), + MovementClassification = table.Column(type: "TEXT", maxLength: 64, nullable: false), + GroupSetHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), + GroupCount = table.Column(type: "INTEGER", nullable: false), + MeanActualGainVsTwin = table.Column(type: "REAL", nullable: false), + BestActualGainVsTwin = table.Column(type: "REAL", nullable: false), + MeanPredictionSpaceGap = table.Column(type: "REAL", nullable: false), + BestPredictionSpaceGap = table.Column(type: "REAL", nullable: false), + AppliedPredictionSpaceAdjustmentKld = table.Column(type: "REAL", nullable: false), + EvidenceCount = table.Column(type: "INTEGER", nullable: false), + Confidence = table.Column(type: "REAL", nullable: false), + ShrinkFactor = table.Column(type: "REAL", nullable: false), + Status = table.Column(type: "TEXT", maxLength: 64, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false), + UpdatedUtc = table.Column(type: "TEXT", nullable: false), + MetadataJson = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AnomalyInteractionRules", x => x.Id); + table.ForeignKey( + name: "FK_AnomalyInteractionRules_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AnomalyInteractionRules_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AnomalyInteractionRules_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AnomalyInteractionRules_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "AnomalyProbeSessions", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + BenchmarkCategory = table.Column(type: "INTEGER", nullable: false), + StartedUtc = table.Column(type: "TEXT", nullable: false), + CompletedUtc = table.Column(type: "TEXT", nullable: true), + SourceRunLabel = table.Column(type: "TEXT", maxLength: 256, nullable: false), + ConfigJson = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AnomalyProbeSessions", x => x.Id); + table.ForeignKey( + name: "FK_AnomalyProbeSessions_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AnomalyProbeSessions_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AnomalyProbeSessions_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AnomalyProbeSessions_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "AnomalyInteractionRuleGroupStates", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + RuleId = table.Column(type: "TEXT", nullable: false), + TensorGroupId = table.Column(type: "INTEGER", nullable: false), + CandidateQuantId = table.Column(type: "INTEGER", nullable: false), + ReferenceQuantId = table.Column(type: "INTEGER", nullable: false), + Movement = table.Column(type: "TEXT", maxLength: 64, nullable: false), + SortOrder = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AnomalyInteractionRuleGroupStates", x => x.Id); + table.ForeignKey( + name: "FK_AnomalyInteractionRuleGroupStates_AnomalyInteractionRules_RuleId", + column: x => x.RuleId, + principalTable: "AnomalyInteractionRules", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AnomalyProbeObservations", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + SessionId = table.Column(type: "TEXT", nullable: false), + ArchitectureFamilyId = table.Column(type: "INTEGER", nullable: false), + TensorGroupProfileId = table.Column(type: "INTEGER", nullable: false), + AiModelHashId = table.Column(type: "INTEGER", nullable: false), + ImatrixDefinitionId = table.Column(type: "INTEGER", nullable: true), + BenchmarkCategory = table.Column(type: "INTEGER", nullable: false), + ReferenceTensorComboId = table.Column(type: "TEXT", nullable: true), + ProbeTensorComboId = table.Column(type: "TEXT", nullable: true), + ProbeType = table.Column(type: "TEXT", maxLength: 64, nullable: false), + Classification = table.Column(type: "TEXT", maxLength: 64, nullable: false), + HypothesisLabel = table.Column(type: "TEXT", maxLength: 128, nullable: false), + MovementClassification = table.Column(type: "TEXT", maxLength: 64, nullable: false), + ChangedGroupSetHash = table.Column(type: "TEXT", maxLength: 128, nullable: false), + ChangedGroupsJson = table.Column(type: "TEXT", nullable: false), + CandidateQuantsJson = table.Column(type: "TEXT", nullable: false), + ReferenceEffectiveGroupsJson = table.Column(type: "TEXT", nullable: false), + CandidateEffectiveGroupsJson = table.Column(type: "TEXT", nullable: false), + InactiveGroupsJson = table.Column(type: "TEXT", nullable: false), + ReferenceTensorConfigKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), + ProbeTensorConfigKey = table.Column(type: "TEXT", maxLength: 128, nullable: false), + IsContextualAnomalyProbe = table.Column(type: "INTEGER", nullable: false), + OldBf16Isolation = table.Column(type: "INTEGER", nullable: false), + AllActiveGroupsExplicit = table.Column(type: "INTEGER", nullable: false), + ReferenceQuantId = table.Column(type: "INTEGER", nullable: false), + ActualKld = table.Column(type: "REAL", nullable: false), + PredictedKld = table.Column(type: "REAL", nullable: false), + ReferenceActualKld = table.Column(type: "REAL", nullable: false), + ReferencePredictedKld = table.Column(type: "REAL", nullable: false), + ActualGainVsTwin = table.Column(type: "REAL", nullable: false), + PredictionSpaceGapVsTwin = table.Column(type: "REAL", nullable: false), + SizeSavingsBytes = table.Column(type: "INTEGER", nullable: false), + UpgradeCount = table.Column(type: "INTEGER", nullable: false), + DowngradeCount = table.Column(type: "INTEGER", nullable: false), + SameCount = table.Column(type: "INTEGER", nullable: false), + UnknownCount = table.Column(type: "INTEGER", nullable: false), + NetBitDelta = table.Column(type: "INTEGER", nullable: false), + RuleDirection = table.Column(type: "TEXT", maxLength: 64, nullable: false), + Accepted = table.Column(type: "INTEGER", nullable: false), + FailureCode = table.Column(type: "TEXT", maxLength: 128, nullable: false), + Message = table.Column(type: "TEXT", maxLength: 4000, nullable: false), + CreatedUtc = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AnomalyProbeObservations", x => x.Id); + table.ForeignKey( + name: "FK_AnomalyProbeObservations_AiModelHashes_AiModelHashId", + column: x => x.AiModelHashId, + principalTable: "AiModelHashes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AnomalyProbeObservations_AnomalyProbeSessions_SessionId", + column: x => x.SessionId, + principalTable: "AnomalyProbeSessions", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AnomalyProbeObservations_ArchitectureFamilies_ArchitectureFamilyId", + column: x => x.ArchitectureFamilyId, + principalTable: "ArchitectureFamilies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AnomalyProbeObservations_ImatrixDefinitions_ImatrixDefinitionId", + column: x => x.ImatrixDefinitionId, + principalTable: "ImatrixDefinitions", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AnomalyProbeObservations_TensorCombos_ProbeTensorComboId", + column: x => x.ProbeTensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AnomalyProbeObservations_TensorCombos_ReferenceTensorComboId", + column: x => x.ReferenceTensorComboId, + principalTable: "TensorCombos", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AnomalyProbeObservations_TensorGroupProfiles_TensorGroupProfileId", + column: x => x.TensorGroupProfileId, + principalTable: "TensorGroupProfiles", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRuleGroupStates_RuleId_TensorGroupId", + table: "AnomalyInteractionRuleGroupStates", + columns: new[] { "RuleId", "TensorGroupId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRules_AiModelHashId", + table: "AnomalyInteractionRules", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRules_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_ReferenceQuantId_GroupSetHash_RuleDirection", + table: "AnomalyInteractionRules", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceQuantId", "GroupSetHash", "RuleDirection" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRules_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_RuleDirection_RuleStatus", + table: "AnomalyInteractionRules", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "RuleDirection", "RuleStatus" }); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRules_FullTensorConfigKey", + table: "AnomalyInteractionRules", + column: "FullTensorConfigKey"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRules_ImatrixDefinitionId", + table: "AnomalyInteractionRules", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRules_TensorGroupProfileId", + table: "AnomalyInteractionRules", + column: "TensorGroupProfileId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_AiModelHashId", + table: "AnomalyProbeObservations", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_ChangedGroupSetHash", + table: "AnomalyProbeObservations", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ChangedGroupSetHash" }); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_ReferenceTensorComboId_ProbeTensorComboId_ProbeType", + table: "AnomalyProbeObservations", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceTensorComboId", "ProbeTensorComboId", "ProbeType" }); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_ImatrixDefinitionId", + table: "AnomalyProbeObservations", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_ProbeTensorComboId", + table: "AnomalyProbeObservations", + column: "ProbeTensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_ProbeTensorConfigKey", + table: "AnomalyProbeObservations", + column: "ProbeTensorConfigKey"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_ReferenceTensorComboId", + table: "AnomalyProbeObservations", + column: "ReferenceTensorComboId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_ReferenceTensorConfigKey", + table: "AnomalyProbeObservations", + column: "ReferenceTensorConfigKey"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_SessionId", + table: "AnomalyProbeObservations", + column: "SessionId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeObservations_TensorGroupProfileId", + table: "AnomalyProbeObservations", + column: "TensorGroupProfileId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeSessions_AiModelHashId", + table: "AnomalyProbeSessions", + column: "AiModelHashId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeSessions_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_StartedUtc", + table: "AnomalyProbeSessions", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "StartedUtc" }); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeSessions_ImatrixDefinitionId", + table: "AnomalyProbeSessions", + column: "ImatrixDefinitionId"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyProbeSessions_TensorGroupProfileId", + table: "AnomalyProbeSessions", + column: "TensorGroupProfileId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AnomalyInteractionRuleGroupStates"); + + migrationBuilder.DropTable( + name: "AnomalyProbeObservations"); + + migrationBuilder.DropTable( + name: "AnomalyInteractionRules"); + + migrationBuilder.DropTable( + name: "AnomalyProbeSessions"); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index 7856dbc..e1020a8 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -133,6 +133,395 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("AiModelHashes"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("AppliedPredictionSpaceAdjustmentKld") + .HasColumnType("REAL"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("BestActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("BestPredictionSpaceGap") + .HasColumnType("REAL"); + + b.Property("CandidateEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Confidence") + .HasColumnType("REAL"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("EvidenceCount") + .HasColumnType("INTEGER"); + + b.Property("FullTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("GroupCount") + .HasColumnType("INTEGER"); + + b.Property("GroupSetHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("InactiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("MeanActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("MeanPredictionSpaceGap") + .HasColumnType("REAL"); + + b.Property("MetadataJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("MovementClassification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceContextKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ReferenceEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("RuleDirection") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RuleStatus") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RuleType") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ShrinkFactor") + .HasColumnType("REAL"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("FullTensorConfigKey"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "RuleDirection", "RuleStatus"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceQuantId", "GroupSetHash", "RuleDirection") + .IsUnique(); + + b.ToTable("AnomalyInteractionRules"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRuleGroupState", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("CandidateQuantId") + .HasColumnType("INTEGER"); + + b.Property("Movement") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("RuleId") + .HasColumnType("TEXT"); + + b.Property("SortOrder") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("RuleId", "TensorGroupId") + .IsUnique(); + + b.ToTable("AnomalyInteractionRuleGroupStates"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeObservation", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("Accepted") + .HasColumnType("INTEGER"); + + b.Property("ActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("ActualKld") + .HasColumnType("REAL"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("AllActiveGroupsExplicit") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("CandidateEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CandidateQuantsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ChangedGroupSetHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ChangedGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Classification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DowngradeCount") + .HasColumnType("INTEGER"); + + b.Property("FailureCode") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("HypothesisLabel") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("InactiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("IsContextualAnomalyProbe") + .HasColumnType("INTEGER"); + + b.Property("Message") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("MovementClassification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("NetBitDelta") + .HasColumnType("INTEGER"); + + b.Property("OldBf16Isolation") + .HasColumnType("INTEGER"); + + b.Property("PredictedKld") + .HasColumnType("REAL"); + + b.Property("PredictionSpaceGapVsTwin") + .HasColumnType("REAL"); + + b.Property("ProbeTensorComboId") + .HasColumnType("TEXT"); + + b.Property("ProbeTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ProbeType") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceActualKld") + .HasColumnType("REAL"); + + b.Property("ReferenceEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ReferencePredictedKld") + .HasColumnType("REAL"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("ReferenceTensorComboId") + .HasColumnType("TEXT"); + + b.Property("ReferenceTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("RuleDirection") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SameCount") + .HasColumnType("INTEGER"); + + b.Property("SessionId") + .HasColumnType("TEXT"); + + b.Property("SizeSavingsBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("UnknownCount") + .HasColumnType("INTEGER"); + + b.Property("UpgradeCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("ProbeTensorComboId"); + + b.HasIndex("ProbeTensorConfigKey"); + + b.HasIndex("ReferenceTensorComboId"); + + b.HasIndex("ReferenceTensorConfigKey"); + + b.HasIndex("SessionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ChangedGroupSetHash"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceTensorComboId", "ProbeTensorComboId", "ProbeType"); + + b.ToTable("AnomalyProbeObservations"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("ConfigJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("SourceRunLabel") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "StartedUtc"); + + b.ToTable("AnomalyProbeSessions"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => { b.Property("Id") @@ -883,6 +1272,141 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("TensorGroupProfile"); }); + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRuleGroupState", b => + { + b.HasOne("MQ.DB.Models.DbModels.AnomalyInteractionRule", "Rule") + .WithMany("GroupStates") + .HasForeignKey("RuleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Rule"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeObservation", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "ProbeTensorCombo") + .WithMany() + .HasForeignKey("ProbeTensorComboId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "ReferenceTensorCombo") + .WithMany() + .HasForeignKey("ReferenceTensorComboId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.AnomalyProbeSession", "Session") + .WithMany("Observations") + .HasForeignKey("SessionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("ProbeTensorCombo"); + + b.Navigation("ReferenceTensorCombo"); + + b.Navigation("Session"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); + }); + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => { b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") @@ -1142,6 +1666,16 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("LearnedSources"); }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.Navigation("GroupStates"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.Navigation("Observations"); + }); #pragma warning restore 612, 618 } } diff --git a/MQ.DB/Models/DbModels/AnomalyProbeSession.cs b/MQ.DB/Models/DbModels/AnomalyProbeSession.cs new file mode 100644 index 0000000..6b7ce46 --- /dev/null +++ b/MQ.DB/Models/DbModels/AnomalyProbeSession.cs @@ -0,0 +1,210 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using MQ.DB.Interfaces; + +namespace MQ.DB.Models.DbModels; + +public class AnomalyProbeSession : ISQLiteEntity +{ + public Guid Id { get; set; } = Guid.NewGuid(); + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + public int TensorGroupProfileId { get; set; } + public TensorGroupProfile TensorGroupProfile { get; set; } = default!; + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + public int? ImatrixDefinitionId { get; set; } + public ImatrixDefinition? ImatrixDefinition { get; set; } + public byte BenchmarkCategory { get; set; } = (byte)MQ.DB.Models.DbModels.BenchmarkCategory.General; + public DateTime StartedUtc { get; set; } = DateTime.UtcNow; + public DateTime? CompletedUtc { get; set; } + public string SourceRunLabel { get; set; } = string.Empty; + public string ConfigJson { get; set; } = string.Empty; + public List Observations { get; set; } = new(); + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(x => x.Id).ValueGeneratedNever(); + builder.Property(x => x.SourceRunLabel).HasMaxLength(256); + builder.Property(x => x.ConfigJson).HasColumnType("TEXT"); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.BenchmarkCategory, x.StartedUtc }); + builder.HasOne(x => x.ArchitectureFamily).WithMany().HasForeignKey(x => x.ArchitectureFamilyId).OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.TensorGroupProfile).WithMany().HasForeignKey(x => x.TensorGroupProfileId).OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.AiModelHash).WithMany().HasForeignKey(x => x.AiModelHashId).OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.ImatrixDefinition).WithMany().HasForeignKey(x => x.ImatrixDefinitionId).OnDelete(DeleteBehavior.Restrict); + builder.HasMany(x => x.Observations).WithOne(x => x.Session).HasForeignKey(x => x.SessionId).OnDelete(DeleteBehavior.Cascade); + } +} + +public class AnomalyProbeObservation : ISQLiteEntity +{ + public Guid Id { get; set; } = Guid.NewGuid(); + public Guid SessionId { get; set; } + public AnomalyProbeSession Session { get; set; } = default!; + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + public int TensorGroupProfileId { get; set; } + public TensorGroupProfile TensorGroupProfile { get; set; } = default!; + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + public int? ImatrixDefinitionId { get; set; } + public ImatrixDefinition? ImatrixDefinition { get; set; } + public byte BenchmarkCategory { get; set; } = (byte)MQ.DB.Models.DbModels.BenchmarkCategory.General; + public Guid? ReferenceTensorComboId { get; set; } + public TensorCombo? ReferenceTensorCombo { get; set; } + public Guid? ProbeTensorComboId { get; set; } + public TensorCombo? ProbeTensorCombo { get; set; } + public string ProbeType { get; set; } = string.Empty; + public string Classification { get; set; } = string.Empty; + public string HypothesisLabel { get; set; } = string.Empty; + public string MovementClassification { get; set; } = string.Empty; + public string ChangedGroupSetHash { get; set; } = string.Empty; + public string ChangedGroupsJson { get; set; } = string.Empty; + public string CandidateQuantsJson { get; set; } = string.Empty; + public string ReferenceEffectiveGroupsJson { get; set; } = string.Empty; + public string CandidateEffectiveGroupsJson { get; set; } = string.Empty; + public string InactiveGroupsJson { get; set; } = string.Empty; + public string ReferenceTensorConfigKey { get; set; } = string.Empty; + public string ProbeTensorConfigKey { get; set; } = string.Empty; + public bool IsContextualAnomalyProbe { get; set; } + public bool OldBf16Isolation { get; set; } + public bool AllActiveGroupsExplicit { get; set; } + public byte ReferenceQuantId { get; set; } + public double ActualKld { get; set; } + public double PredictedKld { get; set; } + public double ReferenceActualKld { get; set; } + public double ReferencePredictedKld { get; set; } + public double ActualGainVsTwin { get; set; } + public double PredictionSpaceGapVsTwin { get; set; } + public ulong SizeSavingsBytes { get; set; } + public int UpgradeCount { get; set; } + public int DowngradeCount { get; set; } + public int SameCount { get; set; } + public int UnknownCount { get; set; } + public int NetBitDelta { get; set; } + public string RuleDirection { get; set; } = string.Empty; + public bool Accepted { get; set; } + public string FailureCode { get; set; } = string.Empty; + public string Message { get; set; } = string.Empty; + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(x => x.Id).ValueGeneratedNever(); + builder.Property(x => x.ProbeType).HasMaxLength(64); + builder.Property(x => x.Classification).HasMaxLength(64); + builder.Property(x => x.HypothesisLabel).HasMaxLength(128); + builder.Property(x => x.MovementClassification).HasMaxLength(64); + builder.Property(x => x.ChangedGroupSetHash).HasMaxLength(128); + builder.Property(x => x.ChangedGroupsJson).HasColumnType("TEXT"); + builder.Property(x => x.CandidateQuantsJson).HasColumnType("TEXT"); + builder.Property(x => x.ReferenceEffectiveGroupsJson).HasColumnType("TEXT"); + builder.Property(x => x.CandidateEffectiveGroupsJson).HasColumnType("TEXT"); + builder.Property(x => x.InactiveGroupsJson).HasColumnType("TEXT"); + builder.Property(x => x.ReferenceTensorConfigKey).HasMaxLength(128); + builder.Property(x => x.ProbeTensorConfigKey).HasMaxLength(128); + builder.HasIndex(x => x.ReferenceTensorConfigKey); + builder.HasIndex(x => x.ProbeTensorConfigKey); + builder.Property(x => x.RuleDirection).HasMaxLength(64); + builder.Property(x => x.FailureCode).HasMaxLength(128); + builder.Property(x => x.Message).HasMaxLength(4000); + builder.HasIndex(x => x.SessionId); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.BenchmarkCategory, x.ChangedGroupSetHash }); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.BenchmarkCategory, x.ReferenceTensorComboId, x.ProbeTensorComboId, x.ProbeType }); + builder.HasOne(x => x.Session).WithMany(x => x.Observations).HasForeignKey(x => x.SessionId).OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.ArchitectureFamily).WithMany().HasForeignKey(x => x.ArchitectureFamilyId).OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.TensorGroupProfile).WithMany().HasForeignKey(x => x.TensorGroupProfileId).OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.AiModelHash).WithMany().HasForeignKey(x => x.AiModelHashId).OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.ImatrixDefinition).WithMany().HasForeignKey(x => x.ImatrixDefinitionId).OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.ReferenceTensorCombo).WithMany().HasForeignKey(x => x.ReferenceTensorComboId).OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.ProbeTensorCombo).WithMany().HasForeignKey(x => x.ProbeTensorComboId).OnDelete(DeleteBehavior.Restrict); + } +} + +public class AnomalyInteractionRule : ISQLiteEntity +{ + public Guid Id { get; set; } = Guid.NewGuid(); + public int ArchitectureFamilyId { get; set; } + public ArchitectureFamily ArchitectureFamily { get; set; } = default!; + public int TensorGroupProfileId { get; set; } + public TensorGroupProfile TensorGroupProfile { get; set; } = default!; + public uint AiModelHashId { get; set; } + public AiModelHash AiModelHash { get; set; } = default!; + public int? ImatrixDefinitionId { get; set; } + public ImatrixDefinition? ImatrixDefinition { get; set; } + public byte BenchmarkCategory { get; set; } = (byte)MQ.DB.Models.DbModels.BenchmarkCategory.General; + public byte ReferenceQuantId { get; set; } + public string ReferenceContextKey { get; set; } = string.Empty; + public string ReferenceEffectiveGroupsJson { get; set; } = string.Empty; + public string CandidateEffectiveGroupsJson { get; set; } = string.Empty; + public string InactiveGroupsJson { get; set; } = string.Empty; + public string FullTensorConfigKey { get; set; } = string.Empty; + public string RuleType { get; set; } = string.Empty; + public string RuleDirection { get; set; } = string.Empty; + public string RuleStatus { get; set; } = string.Empty; + public string MovementClassification { get; set; } = string.Empty; + public string GroupSetHash { get; set; } = string.Empty; + public int GroupCount { get; set; } + public double MeanActualGainVsTwin { get; set; } + public double BestActualGainVsTwin { get; set; } + public double MeanPredictionSpaceGap { get; set; } + public double BestPredictionSpaceGap { get; set; } + public double AppliedPredictionSpaceAdjustmentKld { get; set; } + public int EvidenceCount { get; set; } + public double Confidence { get; set; } + public double ShrinkFactor { get; set; } + public string Status { get; set; } = string.Empty; + public DateTime CreatedUtc { get; set; } = DateTime.UtcNow; + public DateTime UpdatedUtc { get; set; } = DateTime.UtcNow; + public string MetadataJson { get; set; } = string.Empty; + public List GroupStates { get; set; } = new(); + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(x => x.Id).ValueGeneratedNever(); + builder.Property(x => x.ReferenceContextKey).HasMaxLength(512); + builder.Property(x => x.ReferenceEffectiveGroupsJson).HasColumnType("TEXT"); + builder.Property(x => x.CandidateEffectiveGroupsJson).HasColumnType("TEXT"); + builder.Property(x => x.InactiveGroupsJson).HasColumnType("TEXT"); + builder.Property(x => x.FullTensorConfigKey).HasMaxLength(128); + builder.HasIndex(x => x.FullTensorConfigKey); + builder.Property(x => x.RuleType).HasMaxLength(64); + builder.Property(x => x.RuleDirection).HasMaxLength(64); + builder.Property(x => x.RuleStatus).HasMaxLength(64); + builder.Property(x => x.MovementClassification).HasMaxLength(64); + builder.Property(x => x.GroupSetHash).HasMaxLength(128); + builder.Property(x => x.Status).HasMaxLength(64); + builder.Property(x => x.MetadataJson).HasColumnType("TEXT"); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.BenchmarkCategory, x.RuleDirection, x.RuleStatus }); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.BenchmarkCategory, x.ReferenceQuantId, x.GroupSetHash, x.RuleDirection }).IsUnique(); + builder.HasOne(x => x.ArchitectureFamily).WithMany().HasForeignKey(x => x.ArchitectureFamilyId).OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.TensorGroupProfile).WithMany().HasForeignKey(x => x.TensorGroupProfileId).OnDelete(DeleteBehavior.Restrict); + builder.HasOne(x => x.AiModelHash).WithMany().HasForeignKey(x => x.AiModelHashId).OnDelete(DeleteBehavior.Cascade); + builder.HasOne(x => x.ImatrixDefinition).WithMany().HasForeignKey(x => x.ImatrixDefinitionId).OnDelete(DeleteBehavior.Restrict); + builder.HasMany(x => x.GroupStates).WithOne(x => x.Rule).HasForeignKey(x => x.RuleId).OnDelete(DeleteBehavior.Cascade); + } +} + +public class AnomalyInteractionRuleGroupState : ISQLiteEntity +{ + public Guid Id { get; set; } = Guid.NewGuid(); + public Guid RuleId { get; set; } + public AnomalyInteractionRule Rule { get; set; } = default!; + public byte TensorGroupId { get; set; } + public byte CandidateQuantId { get; set; } + public byte ReferenceQuantId { get; set; } + public string Movement { get; set; } = string.Empty; + public int SortOrder { get; set; } + + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.Property(x => x.Id).ValueGeneratedNever(); + builder.Property(x => x.Movement).HasMaxLength(64); + builder.HasIndex(x => new { x.RuleId, x.TensorGroupId }).IsUnique(); + builder.HasOne(x => x.Rule).WithMany(x => x.GroupStates).HasForeignKey(x => x.RuleId).OnDelete(DeleteBehavior.Cascade); + } +} diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 39656cf..13166d8 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -68,6 +68,9 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.CandidateSelection.AllowEightBitAnchorReplacements; + public static RuntimeAnomalyDetectionConfig AnomalyDetection => Current.AnomalyDetection; + public static bool AnomalyDetectionEnabled => Current.AnomalyDetection.Enabled; + public static string? OutputDirectory => Current.Output.OutputDir; public static string OutputNamePrefix => string.IsNullOrWhiteSpace(Current.Output.OutputNamePrefix) ? "Model" @@ -91,4 +94,4 @@ public static void SetResolvedCustomBaselines(IEnumerable BrainLayers => Current.BrainLayers; public static List CollapsePenaltySchemes => Current.CollapsePenaltySchemes; public static List MoeIndicatorTensors => Current.MoeIndicatorTensors; -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index f662a49..fcc36b9 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -17,6 +17,7 @@ public sealed class MagicQuantYamlConfig public RuntimeOutputConfig Output { get; set; } = new(); public RuntimeSurvivalConfig Survival { get; set; } = new(); public RuntimeCandidateSelectionConfig CandidateSelection { get; set; } = new(); + public RuntimeAnomalyDetectionConfig AnomalyDetection { get; set; } = new(); public RuntimeHardwareConfig Hardware { get; set; } = new(); public List SensitivityProbeGroups { get; set; } = @@ -247,6 +248,29 @@ public sealed class RuntimeCandidateSelectionConfig public bool AllowEightBitAnchorReplacements { get; set; } = false; } + +public sealed class RuntimeAnomalyDetectionConfig +{ + public bool Enabled { get; set; } = true; + public int MaxAnomalyRefinementRounds { get; set; } = 1; + public double MinActualGainVsTwinKld { get; set; } = 0.00025d; + public double MinPredictedSizeSavingsVsTwinPercent { get; set; } = 1.0d; + public int MaxProbeGroupCount { get; set; } = 4; + public int MaxProbesPerSeed { get; set; } = 16; + public int MaxTotalProbesPerRun { get; set; } = 32; + public double MaxPredictionSpaceGapVsTwinKld { get; set; } = 0.00050d; + public double MaxRelativePredictionPenaltyVsTwin { get; set; } = 0.35d; + public double PredictionSpaceViolationMargin { get; set; } = 0.00005d; + public double AnomalyAdjustmentShrinkFactor { get; set; } = 0.70d; + public double MinRuleConfidenceToApply { get; set; } = 0.50d; + public double MaxNegativeAdjustmentKld { get; set; } = 0.002d; + public double MaxPositiveAdjustmentKld { get; set; } = 0.002d; + public double MaxAdjustmentFractionOfBaseKld { get; set; } = 0.75d; + public int MaxSmokeCandidatesPerReferenceZone { get; set; } = 12; + public bool PersistSuppressionResults { get; set; } = true; + public bool VerboseAnomalyLogging { get; set; } = true; +} + public sealed class RuntimeLearningConfig { public bool ForceRelearnArchitectureFamily { get; set; } diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index d77e24b..0443a11 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -145,6 +145,23 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) config.CandidateSelection.NearBaselineMaxSizeGrowthPercent = Math.Max(0d, config.CandidateSelection.NearBaselineMaxSizeGrowthPercent); config.CandidateSelection.MinimumKldImprovementEpsilon = Math.Max(0d, config.CandidateSelection.MinimumKldImprovementEpsilon); + config.AnomalyDetection ??= new RuntimeAnomalyDetectionConfig(); + config.AnomalyDetection.MaxAnomalyRefinementRounds = Math.Clamp(config.AnomalyDetection.MaxAnomalyRefinementRounds, 0, 1); + config.AnomalyDetection.MinActualGainVsTwinKld = Math.Max(0d, config.AnomalyDetection.MinActualGainVsTwinKld); + config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent = Math.Max(0d, config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent); + config.AnomalyDetection.MaxProbeGroupCount = Math.Clamp(config.AnomalyDetection.MaxProbeGroupCount, 1, 9); + config.AnomalyDetection.MaxProbesPerSeed = Math.Max(1, config.AnomalyDetection.MaxProbesPerSeed); + config.AnomalyDetection.MaxTotalProbesPerRun = Math.Max(0, config.AnomalyDetection.MaxTotalProbesPerRun); + config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld = Math.Max(0d, config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld); + config.AnomalyDetection.MaxRelativePredictionPenaltyVsTwin = Math.Clamp(config.AnomalyDetection.MaxRelativePredictionPenaltyVsTwin, 0d, 1d); + config.AnomalyDetection.PredictionSpaceViolationMargin = Math.Max(0d, config.AnomalyDetection.PredictionSpaceViolationMargin); + config.AnomalyDetection.AnomalyAdjustmentShrinkFactor = Math.Clamp(config.AnomalyDetection.AnomalyAdjustmentShrinkFactor, 0d, 1d); + config.AnomalyDetection.MinRuleConfidenceToApply = Math.Clamp(config.AnomalyDetection.MinRuleConfidenceToApply, 0d, 1d); + config.AnomalyDetection.MaxNegativeAdjustmentKld = Math.Max(0d, config.AnomalyDetection.MaxNegativeAdjustmentKld); + config.AnomalyDetection.MaxPositiveAdjustmentKld = Math.Max(0d, config.AnomalyDetection.MaxPositiveAdjustmentKld); + config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld = Math.Clamp(config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld, 0d, 1d); + config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone = Math.Max(1, config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone); + ApplyStandardBaselineFilters(config.Baselines); BaselineQuants.ResetDynamicCustomBaselines(); } diff --git a/MagicQuant/Models/AnomalyDetectionModels.cs b/MagicQuant/Models/AnomalyDetectionModels.cs new file mode 100644 index 0000000..83f221e --- /dev/null +++ b/MagicQuant/Models/AnomalyDetectionModels.cs @@ -0,0 +1,141 @@ +using MQ.DB.Models; + +namespace MagicQuant.Models; + +public enum QuantMovementKind +{ + Same = 0, + Downgrade = 1, + Upgrade = 2, + LateralOrEquivalent = 3, + Unknown = 4 +} + +public enum AnomalyMovementClassification +{ + MonotoneDowngrade = 1, + MixedTrade = 2, + MonotoneUpgrade = 3, + LateralOrProviderEquivalent = 4, + Unknown = 5, + NoMovement = 6 +} + +public enum AnomalyRuleDirection +{ + Beneficial = 1, + Harmful = 2, + SuppressionOnly = 3 +} + +public enum AnomalyRuleStatus +{ + Confirmed = 1, + Rejected = 2, + Suppressed = 3, + Retired = 4 +} + +public enum AnomalyProbeClassification +{ + BeneficialAnomaly = 1, + CounterfactualMdaViolation = 2, + HarmfulInteraction = 3, + RejectedSmoke = 4, + NormalGravity = 5, + SuppressionOnly = 6, + SingleGroupInversion = 7, + PairSynergy = 8, + HigherOrderSynergy = 9, + ContextOnly = 10, + MissingTwin = 11, + MissingProbeBenchmark = 12 +} + +public sealed class AnomalyChangedGroup +{ + public TensorGroup Group { get; init; } = default!; + public byte CandidateQuantId { get; init; } + public byte ReferenceQuantId { get; init; } + public byte CandidateStoredSlot { get; init; } + public byte ReferenceStoredSlot { get; init; } + public QuantMovementKind Movement { get; init; } +} + +public sealed class AnomalyMovementAnalysis +{ + public AnomalyMovementClassification Classification { get; init; } + public IReadOnlyList ChangedGroups { get; init; } = Array.Empty(); + public int UpgradeCount { get; init; } + public int DowngradeCount { get; init; } + public int SameCount { get; init; } + public int UnknownCount { get; init; } + public int LateralCount { get; init; } + public int NetBitDelta { get; init; } +} + +public sealed class AnomalySmokeCandidate +{ + public string Source { get; init; } = string.Empty; + public TensorConfig CandidateConfig { get; init; } + public TensorConfig TwinConfig { get; init; } + public HybridQuant CandidateQuant => (HybridQuant)CandidateConfig; + public HybridQuant TwinQuant => (HybridQuant)TwinConfig; + public AnomalyMovementAnalysis Movement { get; init; } = new(); + public double CandidatePredictedKld { get; init; } + public double TwinPredictedKld { get; init; } + public ulong CandidatePredictedSizeBytes { get; init; } + public ulong TwinPredictedSizeBytes { get; init; } + public ulong SizeSavingsBytes { get; init; } + public double PredictionSpaceGapVsTwin { get; init; } + public ulong? CandidatePredictionRank { get; init; } + public ulong? TwinPredictionRank { get; init; } + public double SmokeScore { get; init; } + public string SmokeStrength { get; init; } = string.Empty; + public bool HasActualTwin { get; init; } + public double? CandidateActualKld { get; init; } + public double? TwinActualKld { get; init; } + public ulong? CandidateActualSizeBytes { get; init; } + public ulong? TwinActualSizeBytes { get; init; } + public bool IsConfirmedFromHistory { get; init; } + public string Message { get; init; } = string.Empty; +} + +public sealed class AnomalyProbePlan +{ + public AnomalySmokeCandidate Seed { get; init; } = default!; + public TensorConfig ReferenceConfig { get; init; } + public TensorConfig ProbeConfig { get; init; } + public IReadOnlyList ProbeGroups { get; init; } = Array.Empty(); + public string ProbeType { get; init; } = string.Empty; + public string HypothesisLabel { get; init; } = string.Empty; +} + +public sealed class AnomalyProbeResult +{ + public AnomalyProbePlan Plan { get; init; } = default!; + public BenchmarkSnapshotRecord? ProbeSnapshot { get; init; } + public BenchmarkSnapshotRecord? ReferenceSnapshot { get; init; } + public AnomalyProbeClassification Classification { get; init; } + public AnomalyRuleDirection RuleDirection { get; init; } + public bool Accepted { get; init; } + public double ActualGainVsTwin { get; init; } + public string FailureCode { get; init; } = string.Empty; + public string Message { get; init; } = string.Empty; +} + +public sealed class AnomalyAdjustmentSummary +{ + public int AppliedRuleCount { get; init; } + public long MatchedRowCount { get; init; } + public string DuckDbPath { get; init; } = string.Empty; + public IReadOnlyList RuleMatches { get; init; } = Array.Empty(); +} + +public sealed class AnomalyRunResult +{ + public IReadOnlyList SmokeCandidates { get; init; } = Array.Empty(); + public IReadOnlyList ProbePlans { get; init; } = Array.Empty(); + public IReadOnlyList ProbeResults { get; init; } = Array.Empty(); + public AnomalyAdjustmentSummary AdjustmentSummary { get; init; } = new(); +} diff --git a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs new file mode 100644 index 0000000..5de5d0a --- /dev/null +++ b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs @@ -0,0 +1,266 @@ +using DuckDB.NET.Data; +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using System.Numerics; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class AnomalyAdjustedPredictionService +{ + private readonly RemainingCombinationStore _store; + + public AnomalyAdjustedPredictionService(RemainingCombinationStore store) + { + _store = store; + } + + public async Task ApplyAsync( + IReadOnlyCollection rules, + CancellationToken ct) + { + if (rules.Count == 0) + return new AnomalyAdjustmentSummary { DuckDbPath = _store.GetDatabaseFilePath() }; + + using var c = new DuckDBConnection($"Data Source={_store.GetDatabaseFilePath()}"); + await c.OpenAsync(ct); + await ConfigureSessionAsync(c, ct); + + await ExecuteAsync(c, $@" +UPDATE {CombinationDuckDbSchema.TableName} +SET AnomalyAdjustmentKld = 0.0, + FinalPredictedKld = BaseRankSafeKld, + PredictedKld = BaseRankSafeKld +WHERE BaseRankSafeKld IS NOT NULL;", ct); + + long totalMatched = 0; + var matchLogs = new List(); + + foreach (var rule in rules.OrderByDescending(x => x.Confidence).ThenBy(x => x.Id)) + { + string where = BuildRuleWhere(rule); + if (string.IsNullOrWhiteSpace(where)) + continue; + + double adjustment = rule.AppliedPredictionSpaceAdjustmentKld; + if (Math.Abs(adjustment) <= 0d) + continue; + + long before = await CountMatchesAsync(c, where, ct); + if (before == 0) + continue; + + string expression = adjustment < 0d + ? $"GREATEST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), -LEAST({SqlDouble(Config.AnomalyDetection.MaxNegativeAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))" + : $"LEAST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))"; + + await ExecuteAsync(c, $@" +UPDATE {CombinationDuckDbSchema.TableName} +SET AnomalyAdjustmentKld = {expression}, + FinalPredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}), + PredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}) +WHERE {where};", ct); + + totalMatched += before; + var log = new + { + ruleId = rule.Id, + direction = rule.RuleDirection, + ruleType = rule.RuleType, + referenceQuant = SafeName(rule.ReferenceQuantId), + groupSetHash = rule.GroupSetHash, + adjustment, + matchedRows = before, + confidence = rule.Confidence, + groups = rule.GroupStates + .OrderBy(x => x.SortOrder) + .Select(x => new + { + x.TensorGroupId, + candidate = SafeName(x.CandidateQuantId), + reference = SafeName(x.ReferenceQuantId), + x.Movement + }) + .ToList() + }; + matchLogs.Add(log); + + AnsiConsole.MarkupLine( + $"[green]Applying anomaly rule:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] direction=[cyan]{Markup.Escape(rule.RuleDirection)}[/] " + + $"adjustment=[cyan]{adjustment:0.000000}[/] matched DuckDB rows=[cyan]{before:N0}[/]"); + } + + await ReRankAsync(c, ct); + + return new AnomalyAdjustmentSummary + { + AppliedRuleCount = rules.Count, + MatchedRowCount = totalMatched, + DuckDbPath = _store.GetDatabaseFilePath(), + RuleMatches = matchLogs + }; + } + + + private static string BuildRuleWhere(AnomalyInteractionRule rule) + { + if (rule.GroupStates.Count == 0) + return string.Empty; + + if (BaselineQuants.IsNativeExactAlias(rule.ReferenceQuantId) || + rule.GroupStates.Any(x => BaselineQuants.IsNativeExactAlias(x.CandidateQuantId) || BaselineQuants.IsNativeExactAlias(x.ReferenceQuantId))) + { + return string.Empty; + } + + var states = rule.GroupStates.ToDictionary(x => x.TensorGroupId, x => x.CandidateQuantId); + var predicates = new List + { + CombinationDuckDbSchema.ActiveCandidatePredicateSql, + "BaseRankSafeKld IS NOT NULL", + $"BaseQuant = {rule.ReferenceQuantId}" + }; + + // Match against the full normalized effective active vector. Sparse DuckDB rows + // may still exist from the normal search space, so matching normalizes NULL slot + // value 0 to BaseQuant, while explicit contextual probe/rule persistence remains + // strict and never stores sparse anomaly identities. + foreach (var group in ActiveGroups()) + { + string? column = ColumnNameForGroupId(group.UniqueId); + if (column == null) + return string.Empty; + + byte expectedQuantId = states.TryGetValue(group.UniqueId, out var candidateQuantId) + ? candidateQuantId + : rule.ReferenceQuantId; + + predicates.Add($"(CASE WHEN {column} = 0 THEN BaseQuant ELSE CAST({column} AS INTEGER) - 1 END) = {expectedQuantId}"); + } + + return string.Join(" AND ", predicates); + } + + private static IReadOnlyList ActiveGroups() + { + TensorGroup[] ordered = + [ + TReg.Embeddings, + TReg.LmHead, + TReg.AttnQ, + TReg.AttnKV, + TReg.AttnOutput, + TReg.FfnUpGate, + TReg.FfnDown, + TReg.MoeExperts, + TReg.MoeRouter + ]; + + return ordered + .Where(g => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == g.UniqueId)) + .OrderBy(g => g.UniqueId) + .ToList(); + } + + private static string? ColumnNameForGroupId(byte groupId) + { + if (groupId == TReg.Embeddings.UniqueId) return "Embeddings"; + if (groupId == TReg.LmHead.UniqueId) return "LmHead"; + if (groupId == TReg.AttnQ.UniqueId) return "AttnQ"; + if (groupId == TReg.AttnKV.UniqueId) return "AttnKV"; + if (groupId == TReg.AttnOutput.UniqueId) return "AttnOutput"; + if (groupId == TReg.FfnUpGate.UniqueId) return "FfnUpGate"; + if (groupId == TReg.FfnDown.UniqueId) return "FfnDown"; + if (groupId == TReg.MoeExperts.UniqueId) return "MoeExperts"; + if (groupId == TReg.MoeRouter.UniqueId) return "MoeRouter"; + return null; + } + + private static async Task ReRankAsync(DuckDBConnection c, CancellationToken ct) + { + await ExecuteAsync(c, $@" +DROP TABLE IF EXISTS temp_anomaly_rerank; +CREATE TEMP TABLE temp_anomaly_rerank AS +SELECT {CombinationDuckDbSchema.SlotColumnList}, + CAST(ROW_NUMBER() OVER ( + ORDER BY COALESCE(FinalPredictedKld, PredictedKld) ASC, + PredictedSizeBytes ASC, + PredictionConfidence DESC, + BaseQuant ASC, + Embeddings ASC, + LmHead ASC, + AttnQ ASC, + AttnKV ASC, + AttnOutput ASC, + FfnUpGate ASC, + FfnDown ASC, + MoeExperts ASC, + MoeRouter ASC + ) AS UBIGINT) AS NewPredictionRank +FROM {CombinationDuckDbSchema.TableName} +WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionConfidence IS NOT NULL + AND {CombinationDuckDbSchema.ActiveCandidatePredicateSql}; + +UPDATE {CombinationDuckDbSchema.TableName} t +SET PredictionRank = r.NewPredictionRank +FROM temp_anomaly_rerank r +WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "r")};", ct); + } + + private static async Task CountMatchesAsync(DuckDBConnection c, string where, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = $"SELECT COUNT(*) FROM {CombinationDuckDbSchema.TableName} WHERE {where};"; + return ToInt64(await cmd.ExecuteScalarAsync(ct)); + } + + private static long ToInt64(object? value) + { + if (value is null || value is DBNull) + return 0L; + + if (value is BigInteger big) + return (long)big; + + return Convert.ToInt64(value); + } + + private static async Task ExecuteAsync(DuckDBConnection c, string sql, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + await cmd.ExecuteNonQueryAsync(ct); + } + + private static async Task ConfigureSessionAsync(DuckDBConnection c, CancellationToken ct) + { + await ExecuteAsync(c, "SET preserve_insertion_order = false;", ct); + await ExecuteAsync(c, $"SET threads = {Math.Max(1, Environment.ProcessorCount)};", ct); + } + + private static string SqlDouble(double value) => value.ToString(System.Globalization.CultureInfo.InvariantCulture); + + private static string DescribeRule(AnomalyInteractionRule rule) + { + return string.Join(" + ", rule.GroupStates + .OrderBy(x => x.SortOrder) + .Select(x => $"{ColumnNameForGroupId(x.TensorGroupId)}={SafeName(x.CandidateQuantId)}")) + + $" in {SafeName(rule.ReferenceQuantId)} context"; + } + + private static string SafeName(byte quantId) + { + try + { + return BaselineQuants.FromId(quantId).Names[0]; + } + catch + { + return $"id:{quantId}"; + } + } +} diff --git a/MagicQuant/Services/AnomalyRuleRepository.cs b/MagicQuant/Services/AnomalyRuleRepository.cs new file mode 100644 index 0000000..74e63c2 --- /dev/null +++ b/MagicQuant/Services/AnomalyRuleRepository.cs @@ -0,0 +1,418 @@ +using System.Text.Json; +using MagicQuant.Models; +using Microsoft.EntityFrameworkCore; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; + +namespace MagicQuant.Services; + +public sealed class AnomalyRuleRepository +{ + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = false + }; + + private readonly QuantFidelityComparerService _movement; + + public AnomalyRuleRepository(QuantFidelityComparerService movement) + { + _movement = movement; + } + + public async Task StartSessionAsync(string sourceRunLabel, CancellationToken ct) + { + await using var db = new MagicQuantContext(); + var scope = await ResolveScopeAsync(db, ct); + var session = new AnomalyProbeSession + { + ArchitectureFamilyId = scope.ArchitectureFamilyId, + TensorGroupProfileId = scope.TensorGroupProfileId, + AiModelHashId = scope.AiModelHashId, + ImatrixDefinitionId = scope.ImatrixDefinitionId, + BenchmarkCategory = (byte)BenchmarkCategory.General, + StartedUtc = DateTime.UtcNow, + SourceRunLabel = sourceRunLabel, + ConfigJson = JsonSerializer.Serialize(Config.AnomalyDetection, JsonOptions) + }; + + db.AnomalyProbeSessions.Add(session); + await db.SaveChangesAsync(ct); + return session; + } + + public async Task CompleteSessionAsync(Guid sessionId, CancellationToken ct) + { + await using var db = new MagicQuantContext(); + var session = await db.AnomalyProbeSessions.FirstOrDefaultAsync(x => x.Id == sessionId, ct); + if (session == null) + return; + + session.CompletedUtc = DateTime.UtcNow; + await db.SaveChangesAsync(ct); + } + + public async Task> PersistProbeResultsAsync( + Guid sessionId, + IReadOnlyCollection results, + CancellationToken ct) + { + if (results.Count == 0) + return Array.Empty(); + + await using var db = new MagicQuantContext(); + var scope = await ResolveScopeAsync(db, ct); + var observations = new List(); + + foreach (var result in results) + { + if (!_movement.IsContextualQuantizedConfig(result.Plan.ReferenceConfig) || + !_movement.IsContextualQuantizedConfig(result.Plan.ProbeConfig)) + { + // Invalid/sparse/BF16-exact anomaly attempts are logged by the workflow and + // intentionally not persisted as contextual anomaly observations. + continue; + } + + _movement.EnsureAllActiveGroupsExplicit(result.Plan.ReferenceConfig, "persist-observation-reference"); + _movement.EnsureAllActiveGroupsExplicit(result.Plan.ProbeConfig, "persist-observation-probe"); + + var referenceCombo = await EnsureTensorComboAsync(db, result.Plan.ReferenceConfig, ct); + var probeCombo = await EnsureTensorComboAsync(db, result.Plan.ProbeConfig, ct); + var groups = result.Plan.ProbeGroups.OrderBy(x => x.Group.UniqueId).ToList(); + var movement = result.Plan.Seed.Movement; + + var observation = new AnomalyProbeObservation + { + SessionId = sessionId, + ArchitectureFamilyId = scope.ArchitectureFamilyId, + TensorGroupProfileId = scope.TensorGroupProfileId, + AiModelHashId = scope.AiModelHashId, + ImatrixDefinitionId = scope.ImatrixDefinitionId, + BenchmarkCategory = (byte)BenchmarkCategory.General, + ReferenceTensorComboId = referenceCombo.Id, + ProbeTensorComboId = probeCombo.Id, + ProbeType = result.Plan.ProbeType, + Classification = result.Classification.ToString(), + HypothesisLabel = result.Plan.HypothesisLabel, + MovementClassification = movement.Classification.ToString(), + ChangedGroupSetHash = _movement.BuildChangedGroupHash(groups), + ChangedGroupsJson = JsonSerializer.Serialize(groups.Select(ToGroupLog), JsonOptions), + CandidateQuantsJson = JsonSerializer.Serialize(groups.ToDictionary(x => x.Group.Name, x => BaselineQuants.FromId(x.CandidateQuantId).Names[0]), JsonOptions), + ReferenceEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(result.Plan.ReferenceConfig), JsonOptions), + CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(result.Plan.ProbeConfig), JsonOptions), + InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), + ReferenceTensorConfigKey = TensorConfigIdentity.ToKey(result.Plan.ReferenceConfig), + ProbeTensorConfigKey = TensorConfigIdentity.ToKey(result.Plan.ProbeConfig), + IsContextualAnomalyProbe = true, + OldBf16Isolation = false, + AllActiveGroupsExplicit = _movement.HasAllActiveGroupsExplicit(result.Plan.ReferenceConfig) && _movement.HasAllActiveGroupsExplicit(result.Plan.ProbeConfig), + ReferenceQuantId = result.Plan.ReferenceConfig.BaseQuant, + ActualKld = result.ProbeSnapshot?.Kld ?? 0d, + PredictedKld = result.Plan.Seed.CandidatePredictedKld, + ReferenceActualKld = result.ReferenceSnapshot?.Kld ?? 0d, + ReferencePredictedKld = result.Plan.Seed.TwinPredictedKld, + ActualGainVsTwin = result.ActualGainVsTwin, + PredictionSpaceGapVsTwin = result.Plan.Seed.PredictionSpaceGapVsTwin, + SizeSavingsBytes = ComputeSizeSavings(result.ReferenceSnapshot, result.ProbeSnapshot), + UpgradeCount = movement.UpgradeCount, + DowngradeCount = movement.DowngradeCount, + SameCount = movement.SameCount, + UnknownCount = movement.UnknownCount, + NetBitDelta = movement.NetBitDelta, + RuleDirection = result.RuleDirection.ToString(), + Accepted = result.Accepted, + FailureCode = result.FailureCode, + Message = result.Message, + CreatedUtc = DateTime.UtcNow + }; + + db.AnomalyProbeObservations.Add(observation); + observations.Add(observation); + } + + await db.SaveChangesAsync(ct); + return observations; + } + + public async Task> UpsertRulesFromResultsAsync( + IReadOnlyCollection results, + CancellationToken ct) + { + var eligible = results + .Where(x => x.RuleDirection != AnomalyRuleDirection.SuppressionOnly || Config.AnomalyDetection.PersistSuppressionResults) + .Where(x => x.ReferenceSnapshot != null && x.ProbeSnapshot != null) + .Where(x => _movement.IsContextualQuantizedConfig(x.Plan.ReferenceConfig)) + .Where(x => _movement.IsContextualQuantizedConfig(x.Plan.ProbeConfig)) + .ToList(); + + if (eligible.Count == 0) + return Array.Empty(); + + await using var db = new MagicQuantContext(); + var scope = await ResolveScopeAsync(db, ct); + var upserted = new List(); + + foreach (var group in eligible.GroupBy(BuildRuleKey, StringComparer.Ordinal)) + { + var first = group.First(); + _movement.EnsureAllActiveGroupsExplicit(first.Plan.ReferenceConfig, "upsert-rule-reference"); + _movement.EnsureAllActiveGroupsExplicit(first.Plan.ProbeConfig, "upsert-rule-probe"); + var probeGroups = first.Plan.ProbeGroups.OrderBy(x => x.Group.UniqueId).ToList(); + string groupSetHash = _movement.BuildChangedGroupHash(probeGroups); + string direction = first.RuleDirection.ToString(); + byte referenceQuantId = first.Plan.ReferenceConfig.BaseQuant; + + var rule = await db.AnomalyInteractionRules + .Include(x => x.GroupStates) + .FirstOrDefaultAsync(x => + x.ArchitectureFamilyId == scope.ArchitectureFamilyId && + x.TensorGroupProfileId == scope.TensorGroupProfileId && + x.AiModelHashId == scope.AiModelHashId && + x.ImatrixDefinitionId == scope.ImatrixDefinitionId && + x.BenchmarkCategory == (byte)BenchmarkCategory.General && + x.ReferenceQuantId == referenceQuantId && + x.GroupSetHash == groupSetHash && + x.RuleDirection == direction, + ct); + + bool isNew = rule == null; + if (rule == null) + { + rule = new AnomalyInteractionRule + { + ArchitectureFamilyId = scope.ArchitectureFamilyId, + TensorGroupProfileId = scope.TensorGroupProfileId, + AiModelHashId = scope.AiModelHashId, + ImatrixDefinitionId = scope.ImatrixDefinitionId, + BenchmarkCategory = (byte)BenchmarkCategory.General, + ReferenceQuantId = referenceQuantId, + ReferenceContextKey = _movement.ReferenceContextKey(first.Plan.ReferenceConfig), + ReferenceEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), JsonOptions), + CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions), + InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), + FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), + RuleDirection = direction, + GroupSetHash = groupSetHash, + CreatedUtc = DateTime.UtcNow + }; + db.AnomalyInteractionRules.Add(rule); + } + + var rows = group.ToList(); + rule.RuleType = ResolveRuleType(rows); + rule.RuleStatus = first.RuleDirection == AnomalyRuleDirection.Beneficial || first.RuleDirection == AnomalyRuleDirection.Harmful + ? AnomalyRuleStatus.Confirmed.ToString() + : AnomalyRuleStatus.Suppressed.ToString(); + rule.Status = rule.RuleStatus; + rule.MovementClassification = first.Plan.Seed.Movement.Classification.ToString(); + rule.GroupCount = probeGroups.Count; + rule.EvidenceCount = Math.Max(rule.EvidenceCount, 0) + rows.Count; + rule.MeanActualGainVsTwin = rows.Average(x => x.ActualGainVsTwin); + rule.BestActualGainVsTwin = rows.Max(x => x.ActualGainVsTwin); + rule.MeanPredictionSpaceGap = rows.Average(x => x.Plan.Seed.PredictionSpaceGapVsTwin); + rule.BestPredictionSpaceGap = rows.Min(x => x.Plan.Seed.PredictionSpaceGapVsTwin); + rule.ShrinkFactor = Config.AnomalyDetection.AnomalyAdjustmentShrinkFactor; + rule.Confidence = ComputeConfidence(rows); + rule.AppliedPredictionSpaceAdjustmentKld = ComputePredictionAdjustment(first, rule.Confidence); + rule.ReferenceContextKey = _movement.ReferenceContextKey(first.Plan.ReferenceConfig); + rule.ReferenceEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), JsonOptions); + rule.CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions); + rule.InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions); + rule.FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig); + rule.UpdatedUtc = DateTime.UtcNow; + rule.MetadataJson = JsonSerializer.Serialize(new + { + source = "counterfactual-twin-probe", + isContextualAnomalyProbe = true, + oldBf16Isolation = false, + allActiveGroupsExplicit = true, + referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), + candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), + inactiveGroups = _movement.BuildInactiveGroupList(), + first.Plan.ProbeType, + first.Plan.HypothesisLabel, + groups = probeGroups.Select(ToGroupLog).ToList() + }, JsonOptions); + + if (!isNew) + db.AnomalyInteractionRuleGroupStates.RemoveRange(rule.GroupStates); + + rule.GroupStates = probeGroups.Select((x, i) => new AnomalyInteractionRuleGroupState + { + RuleId = rule.Id, + TensorGroupId = x.Group.UniqueId, + CandidateQuantId = x.CandidateQuantId, + ReferenceQuantId = x.ReferenceQuantId, + Movement = x.Movement.ToString(), + SortOrder = i + }).ToList(); + + upserted.Add(rule); + } + + await db.SaveChangesAsync(ct); + return upserted; + } + + public async Task> LoadApplicableRulesAsync(CancellationToken ct) + { + await using var db = new MagicQuantContext(); + var scope = await ResolveScopeAsync(db, ct); + var minConfidence = Config.AnomalyDetection.MinRuleConfidenceToApply; + + var rules = await db.AnomalyInteractionRules + .AsNoTracking() + .Include(x => x.GroupStates) + .Where(x => x.ArchitectureFamilyId == scope.ArchitectureFamilyId) + .Where(x => x.TensorGroupProfileId == scope.TensorGroupProfileId) + .Where(x => x.AiModelHashId == scope.AiModelHashId) + .Where(x => x.ImatrixDefinitionId == scope.ImatrixDefinitionId) + .Where(x => x.BenchmarkCategory == (byte)BenchmarkCategory.General) + .Where(x => x.RuleStatus == AnomalyRuleStatus.Confirmed.ToString()) + .Where(x => x.Confidence >= minConfidence) + .Where(x => x.RuleDirection == AnomalyRuleDirection.Beneficial.ToString() || x.RuleDirection == AnomalyRuleDirection.Harmful.ToString()) + .ToListAsync(ct); + + return rules + .Where(_movement.IsContextualQuantizedRule) + .ToList(); + } + + public async Task HasSuppressionOrRuleAsync( + TensorConfig reference, + IReadOnlyList groups, + CancellationToken ct) + { + await using var db = new MagicQuantContext(); + var scope = await ResolveScopeAsync(db, ct); + string hash = _movement.BuildChangedGroupHash(groups); + + return await db.AnomalyInteractionRules + .AsNoTracking() + .AnyAsync(x => + x.ArchitectureFamilyId == scope.ArchitectureFamilyId && + x.TensorGroupProfileId == scope.TensorGroupProfileId && + x.AiModelHashId == scope.AiModelHashId && + x.ImatrixDefinitionId == scope.ImatrixDefinitionId && + x.BenchmarkCategory == (byte)BenchmarkCategory.General && + x.ReferenceQuantId == reference.BaseQuant && + x.GroupSetHash == hash && + x.RuleStatus != AnomalyRuleStatus.Retired.ToString(), + ct); + } + + private async Task ResolveScopeAsync(MagicQuantContext db, CancellationToken ct) + { + uint aiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); + int? imatrixId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, aiModelHashId, createIfMissing: false, ct); + return new AnomalyScope( + TensorGroupProfileService.RequireCurrentArchitectureFamilyId(), + TensorGroupProfileService.RequireCurrentProfileId(), + aiModelHashId, + imatrixId); + } + + private static async Task EnsureTensorComboAsync(MagicQuantContext db, TensorConfig config, CancellationToken ct) + { + var combo = await db.TensorCombos.FirstOrDefaultAsync(x => + x.BaseQuant == config.BaseQuant && + x.Embeddings == config.Embeddings && + x.LmHead == config.LmHead && + x.AttnQ == config.AttnQ && + x.AttnKV == config.AttnKV && + x.AttnOutput == config.AttnOutput && + x.FfnUpGate == config.FfnUpGate && + x.FfnDown == config.FfnDown && + x.MoeExperts == config.MoeExperts && + x.MoeRouter == config.MoeRouter, + ct); + + if (combo != null) + return combo; + + combo = new TensorCombo(config); + db.TensorCombos.Add(combo); + await db.SaveChangesAsync(ct); + return combo; + } + + private static object ToGroupLog(AnomalyChangedGroup x) + { + return new + { + groupId = x.Group.UniqueId, + group = x.Group.Name, + shortCode = x.Group.ShortCode, + candidateQuantId = x.CandidateQuantId, + candidateQuant = BaselineQuants.FromId(x.CandidateQuantId).Names[0], + referenceQuantId = x.ReferenceQuantId, + referenceQuant = BaselineQuants.FromId(x.ReferenceQuantId).Names[0], + movement = x.Movement.ToString() + }; + } + + private static ulong ComputeSizeSavings(BenchmarkSnapshotRecord? reference, BenchmarkSnapshotRecord? probe) + { + if (reference == null || probe == null || reference.SizeBytes <= probe.SizeBytes) + return 0UL; + + return reference.SizeBytes - probe.SizeBytes; + } + + private static string BuildRuleKey(AnomalyProbeResult result) + { + var groups = result.Plan.ProbeGroups + .OrderBy(x => x.Group.UniqueId) + .Select(x => $"{x.Group.UniqueId}:{x.ReferenceQuantId}->{x.CandidateQuantId}"); + + return $"{result.RuleDirection}|ref={TensorConfigIdentity.ToKey(result.Plan.ReferenceConfig)}|probe={TensorConfigIdentity.ToKey(result.Plan.ProbeConfig)}|{string.Join("|", groups)}"; + } + + private static string ResolveRuleType(IReadOnlyList rows) + { + var first = rows[0]; + if (first.RuleDirection == AnomalyRuleDirection.SuppressionOnly) + return "SuppressionOnly"; + + return first.Plan.ProbeType switch + { + "single" => "SingleGroupInversion", + "pair" => "PairSynergy", + "full" => rows.Any(x => x.Plan.ProbeGroups.Count >= 3) ? "HigherOrderSynergy" : "PairSynergy", + "leave-one-out" => "HigherOrderSynergy", + _ => "ContextOnly" + }; + } + + private static double ComputeConfidence(IReadOnlyList rows) + { + if (rows.Count == 0) + return 0d; + + double accepted = rows.Count(x => x.Accepted) / (double)rows.Count; + double gain = Math.Clamp(rows.Max(x => Math.Abs(x.ActualGainVsTwin)) / Math.Max(Config.AnomalyDetection.MinActualGainVsTwinKld, 1e-9), 0d, 2d) / 2d; + return Math.Clamp((accepted * 0.70d) + (gain * 0.30d), 0d, 1d); + } + + private static double ComputePredictionAdjustment(AnomalyProbeResult result, double confidence) + { + var cfg = Config.AnomalyDetection; + double baseGap = result.Plan.Seed.PredictionSpaceGapVsTwin; + double required = result.RuleDirection switch + { + AnomalyRuleDirection.Beneficial => -(Math.Max(0d, baseGap) + cfg.PredictionSpaceViolationMargin), + AnomalyRuleDirection.Harmful => Math.Max(cfg.PredictionSpaceViolationMargin, Math.Abs(baseGap) + cfg.PredictionSpaceViolationMargin), + _ => 0d + }; + + double adjusted = required * confidence * cfg.AnomalyAdjustmentShrinkFactor; + if (adjusted < 0d) + return Math.Max(adjusted, -cfg.MaxNegativeAdjustmentKld); + + return Math.Min(adjusted, cfg.MaxPositiveAdjustmentKld); + } + + private readonly record struct AnomalyScope(int ArchitectureFamilyId, int TensorGroupProfileId, uint AiModelHashId, int? ImatrixDefinitionId); +} diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs new file mode 100644 index 0000000..169ec1c --- /dev/null +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -0,0 +1,1189 @@ +using System.Globalization; +using System.Numerics; +using System.Text.Json; +using DuckDB.NET.Data; +using MagicQuant.Helpers; +using MagicQuant.Models; +using Microsoft.EntityFrameworkCore; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class AnomalyWorkflowService +{ + private const int DuckSmokeScanLimit = 0; // 0 means scan all predicted DuckDB rows; anomaly smoke must not be top-rank truncated. + + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = true + }; + + private readonly RemainingCombinationStore _store; + private readonly HybridBenchmarkRepository _repository; + private readonly QuantizationService _quantizationService; + private readonly QuantFidelityComparerService _movement; + private readonly AnomalyRuleRepository _rules; + private readonly AnomalyAdjustedPredictionService _adjuster; + + public AnomalyWorkflowService( + RemainingCombinationStore store, + HybridBenchmarkRepository repository, + QuantizationService quantizationService) + { + _store = store; + _repository = repository; + _quantizationService = quantizationService; + _movement = new QuantFidelityComparerService(); + _rules = new AnomalyRuleRepository(_movement); + _adjuster = new AnomalyAdjustedPredictionService(store); + } + + public async Task RunAsync( + IReadOnlyCollection pureBaselineSnapshots, + CancellationToken ct = default) + { + if (!Config.AnomalyDetection.Enabled || Config.AnomalyDetection.MaxAnomalyRefinementRounds <= 0) + { + AnsiConsole.MarkupLine("[grey]Anomaly detection disabled by config.[/]"); + return new AnomalyRunResult(); + } + + AnsiConsole.Write(new Rule("[yellow]Counterfactual Anomaly Smoke / Probe Pass[/]") { Justification = Justify.Left }); + + var session = await _rules.StartSessionAsync("prediction-guided-selection", ct); + try + { + var historical = await DetectHistoricalSmokeAsync(ct); + var duck = await DetectDuckSmokeAsync(ct); + var smoke = historical + .Concat(duck) + .GroupBy(x => TensorConfigIdentity.ToKey(x.CandidateConfig), StringComparer.Ordinal) + .Select(g => g.OrderByDescending(x => x.IsConfirmedFromHistory).ThenByDescending(x => x.SmokeScore).First()) + .OrderByDescending(x => x.IsConfirmedFromHistory) + .ThenByDescending(x => x.SmokeScore) + .Take(Config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone * Math.Max(1, RuntimeSearchSpace.GetActiveCombinationBaselines().Count)) + .ToList(); + + WriteSmokeConsoleSummary(historical.Count, duck.Count, smoke); + await WriteJsonAsync("magicquant-anomaly-smoke-scan.json", new + { + generatedAtUtc = DateTime.UtcNow, + historicalCount = historical.Count, + duckPredictionSpaceCount = duck.Count, + selectedSmokeCount = smoke.Count, + smoke = smoke.Select(ToSmokeLog).ToList() + }, ct); + + await WriteJsonAsync("magicquant-anomaly-seeds.json", smoke.Select(ToSmokeLog).ToList(), ct); + + var probes = await PlanProbesAsync(smoke, ct); + await WriteJsonAsync("magicquant-anomaly-probes.json", probes.Select(ToProbeLog).ToList(), ct); + + var results = await ValidateProbesAsync(probes, ct); + await _rules.PersistProbeResultsAsync(session.Id, results, ct); + var upsertedRules = await _rules.UpsertRulesFromResultsAsync(results, ct); + var applicableRules = await _rules.LoadApplicableRulesAsync(ct); + var adjustment = await _adjuster.ApplyAsync(applicableRules, ct); + + await WriteJsonAsync("magicquant-anomaly-rules.json", new + { + generatedAtUtc = DateTime.UtcNow, + upserted = upsertedRules.Select(ToRuleLog).ToList(), + applicable = applicableRules.Select(ToRuleLog).ToList() + }, ct); + + await WriteJsonAsync("magicquant-anomaly-adjusted-predictions-summary.json", adjustment, ct); + await WriteFinalManifestAsync("magicquant.anomalies.json", new + { + generatedAtUtc = DateTime.UtcNow, + smoke = smoke.Select(ToSmokeLog).ToList(), + probes = probes.Select(ToProbeLog).ToList(), + results = results.Select(ToResultLog).ToList(), + rules = applicableRules.Select(ToRuleLog).ToList(), + adjustment + }, ct); + await WriteFinalManifestAsync("magicquant.prediction-audit.json", new + { + generatedAtUtc = DateTime.UtcNow, + note = "BaseRankSafeKld is normal PAVA gravity. FinalPredictedKld is BaseRankSafeKld plus scoped anomaly adjustments. Global PAVA is not rerun after anomaly exceptions.", + adjustment + }, ct); + + return new AnomalyRunResult + { + SmokeCandidates = smoke, + ProbePlans = probes, + ProbeResults = results, + AdjustmentSummary = adjustment + }; + } + finally + { + await _rules.CompleteSessionAsync(session.Id, ct); + } + } + + + private async Task> DetectHistoricalSmokeAsync(CancellationToken ct) + { + var snapshots = await LoadAllCurrentBenchmarkSnapshotsAsync(ct); + var byKey = snapshots.ToDictionary(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal); + var predictionLookup = await LoadPredictionLookupAsync(ct); + var smoke = new List(); + int skippedIsolation = 0; + int skippedSparse = 0; + int skippedNonContextualTwin = 0; + int skippedMixed = 0; + int contextualScanned = 0; + + foreach (var candidate in snapshots.Where(x => !TensorConfigIdentity.IsPureBaseline(x.Config))) + { + if (ShouldSkipInvalidContextualAnomalyConfig(candidate.Config, "history", out var skipReason)) + { + if (skipReason.Contains("SparseActiveGroup", StringComparison.OrdinalIgnoreCase)) + { + skippedSparse++; + LogHistoricalSparseCandidateIgnored(candidate.Config, skipReason, skippedSparse); + } + else + { + skippedIsolation++; + LogSkippedInvalidContextualAnomalyConfig("history", candidate.Config, skipReason, skippedIsolation); + } + continue; + } + + contextualScanned++; + var twinConfig = _movement.BuildBaseContextTwin(candidate.Config); + if (TensorConfigIdentity.ToKey(twinConfig) == TensorConfigIdentity.ToKey(candidate.Config)) + continue; + + if (ShouldSkipInvalidContextualAnomalyConfig(twinConfig, "history-twin", out var twinSkipReason)) + { + skippedNonContextualTwin++; + LogSkippedInvalidContextualAnomalyConfig("history-twin", twinConfig, twinSkipReason, skippedNonContextualTwin); + continue; + } + + var movement = _movement.Analyze(twinConfig, candidate.Config); + if (movement.Classification != AnomalyMovementClassification.MonotoneDowngrade) + { + if (movement.Classification == AnomalyMovementClassification.MixedTrade) + { + skippedMixed++; + if (Config.AnomalyDetection.VerboseAnomalyLogging && skippedMixed <= 12) + { + AnsiConsole.MarkupLine("[grey]Ignored anomaly smoke:[/] classification=MixedTrade reason=normal protect/compress frontier behavior"); + } + } + continue; + } + + if (movement.DowngradeCount > Config.AnomalyDetection.MaxProbeGroupCount) + continue; + + byKey.TryGetValue(TensorConfigIdentity.ToKey(twinConfig), out var twin); + if (twin != null && ShouldSkipInvalidContextualAnomalyConfig(twin.Config, "history-existing-twin", out var existingTwinSkipReason)) + { + skippedNonContextualTwin++; + LogSkippedInvalidContextualAnomalyConfig("history-existing-twin", twin.Config, existingTwinSkipReason, skippedNonContextualTwin); + continue; + } + + predictionLookup.TryGetValue(TensorConfigIdentity.ToKey(candidate.Config), out var candidatePrediction); + predictionLookup.TryGetValue(TensorConfigIdentity.ToKey(twinConfig), out var twinPrediction); + + bool confirmed = twin != null && + candidate.SizeBytes <= twin.SizeBytes && + twin.Kld - candidate.Kld >= Config.AnomalyDetection.MinActualGainVsTwinKld; + + if (!confirmed && twin != null && twin.Kld <= candidate.Kld) + continue; + + smoke.Add(new AnomalySmokeCandidate + { + Source = "history", + CandidateConfig = candidate.Config, + TwinConfig = twinConfig, + Movement = movement, + CandidatePredictedKld = candidatePrediction?.BaseRankSafeKld ?? candidatePrediction?.FinalPredictedKld ?? candidate.Kld, + TwinPredictedKld = twinPrediction?.BaseRankSafeKld ?? twinPrediction?.FinalPredictedKld ?? twin?.Kld ?? 0d, + CandidatePredictedSizeBytes = candidatePrediction?.PredictedSizeBytes ?? candidate.SizeBytes, + TwinPredictedSizeBytes = twinPrediction?.PredictedSizeBytes ?? twin?.SizeBytes ?? candidate.SizeBytes, + SizeSavingsBytes = twin != null && twin.SizeBytes > candidate.SizeBytes ? twin.SizeBytes - candidate.SizeBytes : 0UL, + PredictionSpaceGapVsTwin = (candidatePrediction?.BaseRankSafeKld ?? candidate.Kld) - (twinPrediction?.BaseRankSafeKld ?? twin?.Kld ?? candidate.Kld), + CandidatePredictionRank = candidatePrediction?.PredictionRank, + TwinPredictionRank = twinPrediction?.PredictionRank, + SmokeScore = confirmed ? 1_000_000d : 100d, + SmokeStrength = confirmed ? "ConfirmedHistory" : "HistoricalMissingTwin", + HasActualTwin = twin != null, + CandidateActualKld = candidate.Kld, + TwinActualKld = twin?.Kld, + CandidateActualSizeBytes = candidate.SizeBytes, + TwinActualSizeBytes = twin?.SizeBytes, + IsConfirmedFromHistory = confirmed, + Message = confirmed + ? "Existing explicit contextual quantized benchmark history contains a monotone downgrade candidate that beats its higher-bit twin." + : "Existing explicit contextual quantized benchmark history has monotone downgrade smoke but the exact twin is missing." + }); + } + + if (skippedIsolation > 0 || skippedNonContextualTwin > 0 || skippedSparse > 0) + { + AnsiConsole.MarkupLine( + $"[yellow]Historical contextual anomaly scan:[/] contextualScanned={contextualScanned:N0} skippedIsolation={skippedIsolation:N0} sparseIgnored={skippedSparse:N0} skippedTwins={skippedNonContextualTwin:N0}"); + } + + return smoke; + } + + + private async Task> DetectDuckSmokeAsync(CancellationToken ct) + { + var rows = await LoadPredictionRowsAsync(DuckSmokeScanLimit, ct); + var explicitRows = new Dictionary(StringComparer.Ordinal); + var result = new List(); + int skippedIsolation = 0; + int skippedSparse = 0; + int normalizedSparse = 0; + int skippedPure = 0; + int skippedMixed = 0; + int skippedMovement = 0; + int skippedNoTwin = 0; + int logicalTwinFallback = 0; + int skippedSavings = 0; + int skippedGap = 0; + int contextualScanned = 0; + + foreach (var row in rows) + { + if (!_movement.TryNormalizeSparseDuckRowToActivatedContext(row.Config, out var activated, out var wasSparse, out var normalizeReason)) + { + if (normalizeReason.Contains("SparseActiveGroup", StringComparison.OrdinalIgnoreCase)) + skippedSparse++; + else + skippedIsolation++; + + LogSkippedInvalidContextualAnomalyConfig("duckdb", row.Config, normalizeReason, skippedIsolation + skippedSparse); + continue; + } + + if (wasSparse) + normalizedSparse++; + + if (TensorConfigIdentity.ToKey(activated) == TensorConfigIdentity.ToKey(_movement.BuildBaseContextTwin(activated))) + { + skippedPure++; + continue; + } + + var normalizedRow = row with { Config = activated }; + string normalizedKey = TensorConfigIdentity.ToKey(activated); + if (!explicitRows.ContainsKey(normalizedKey)) + explicitRows[normalizedKey] = normalizedRow; + } + + foreach (var row in explicitRows.Values) + { + contextualScanned++; + var twin = _movement.BuildBaseContextTwin(row.Config); + + if (ShouldSkipInvalidContextualAnomalyConfig(row.Config, "duckdb-normalized-candidate", out var candidateSkipReason)) + { + skippedIsolation++; + LogSkippedInvalidContextualAnomalyConfig("duckdb-normalized-candidate", row.Config, candidateSkipReason, skippedIsolation); + continue; + } + + if (ShouldSkipInvalidContextualAnomalyConfig(twin, "duckdb-twin", out var twinSkipReason)) + { + skippedIsolation++; + LogSkippedInvalidContextualAnomalyConfig("duckdb-twin", twin, twinSkipReason, skippedIsolation); + continue; + } + + var movement = _movement.Analyze(twin, row.Config); + if (movement.Classification == AnomalyMovementClassification.MixedTrade) + { + skippedMixed++; + if (Config.AnomalyDetection.VerboseAnomalyLogging && skippedMixed <= 12) + { + AnsiConsole.MarkupLine("[grey]Ignored anomaly smoke:[/] classification=MixedTrade reason=normal protect/compress frontier behavior"); + } + continue; + } + + if (movement.Classification != AnomalyMovementClassification.MonotoneDowngrade) + { + skippedMovement++; + continue; + } + + if (movement.DowngradeCount <= 0 || movement.DowngradeCount > Config.AnomalyDetection.MaxProbeGroupCount) + { + skippedMovement++; + continue; + } + + var twinRow = await LoadContextualTwinPredictionRowAsync(twin, explicitRows, ct); + if (twinRow == null) + { + skippedNoTwin++; + continue; + } + + if (TensorConfigIdentity.ToKey(twinRow.Config) != TensorConfigIdentity.ToKey(twin)) + logicalTwinFallback++; + + if (twinRow.PredictedSizeBytes <= row.PredictedSizeBytes) + { + skippedSavings++; + continue; + } + + ulong savingsBytes = twinRow.PredictedSizeBytes - row.PredictedSizeBytes; + double savingsPercent = savingsBytes * 100d / Math.Max(1d, twinRow.PredictedSizeBytes); + if (savingsPercent < Config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent) + { + skippedSavings++; + continue; + } + + double gap = row.BaseRankSafeKld - twinRow.BaseRankSafeKld; + if (gap > Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld) + { + skippedGap++; + continue; + } + + double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank); + + result.Add(new AnomalySmokeCandidate + { + Source = "duckdb-prediction-space", + CandidateConfig = row.Config, + TwinConfig = twin, + Movement = movement, + CandidatePredictedKld = row.BaseRankSafeKld, + TwinPredictedKld = twinRow.BaseRankSafeKld, + CandidatePredictedSizeBytes = row.PredictedSizeBytes, + TwinPredictedSizeBytes = twinRow.PredictedSizeBytes, + SizeSavingsBytes = savingsBytes, + PredictionSpaceGapVsTwin = gap, + CandidatePredictionRank = row.PredictionRank, + TwinPredictionRank = twinRow.PredictionRank, + SmokeScore = score, + SmokeStrength = gap <= 0d ? "Strong" : "Close", + Message = "Prediction-space contextual monotone downgrade candidate is close enough to its higher-bit quantized twin to justify probes. Sparse DuckDB source rows, when present, were normalized into explicit active context before classification." + }); + } + + AnsiConsole.MarkupLine("[yellow]DuckDB contextual smoke scan:[/]"); + AnsiConsole.MarkupLine($"[grey] predicted rows scanned=[/] [cyan]{rows.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] sparse rows normalized to explicit context=[/] [cyan]{normalizedSparse:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] sparse rows skipped=[/] [cyan]{skippedSparse:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] BF16/exact rows skipped=[/] [cyan]{skippedIsolation:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] pure/logical reference rows skipped=[/] [cyan]{skippedPure:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] contextual quantized rows scanned=[/] [cyan]{contextualScanned:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] logical higher-bit twin fallback used=[/] [cyan]{logicalTwinFallback:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] no higher-bit twin found=[/] [cyan]{skippedNoTwin:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] movement not monotone downgrade=[/] [cyan]{skippedMovement:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] mixed trade ignored=[/] [cyan]{skippedMixed:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] size savings below threshold=[/] [cyan]{skippedSavings:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] prediction-space gap too large=[/] [cyan]{skippedGap:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] queued smoke candidates=[/] [cyan]{result.Count:N0}[/]"); + + if (result.Count == 0 && rows.Count > 0) + { + AnsiConsole.MarkupLine( + "[yellow]DuckDB contextual smoke scan produced zero candidates.[/] This is valid only if all predicted rows were filtered by explicit-context validity, monotone-downgrade movement, size-savings, or prediction-gap thresholds above."); + } + + return result + .GroupBy(x => x.TwinConfig.BaseQuant) + .SelectMany(g => g.OrderByDescending(x => x.SmokeScore).Take(Config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone)) + .ToList(); + } + + + private async Task> PlanProbesAsync(IReadOnlyList seeds, CancellationToken ct) + { + var plans = new List(); + var seen = new HashSet(StringComparer.Ordinal); + + foreach (var seed in seeds) + { + bool invalidSeedCandidate = ShouldSkipInvalidContextualAnomalyConfig(seed.CandidateConfig, "probe-seed-candidate", out var seedCandidateReason); + bool invalidSeedTwin = ShouldSkipInvalidContextualAnomalyConfig(seed.TwinConfig, "probe-seed-twin", out var seedTwinReason); + if (invalidSeedCandidate || invalidSeedTwin) + { + string reason = invalidSeedCandidate ? seedCandidateReason : seedTwinReason; + AnsiConsole.MarkupLine($"[yellow]SkippedInvalidContextualAnomalyProbe:[/] source=probe-seed reason={Markup.Escape(reason)} candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(seed.CandidateQuant))} twin={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(seed.TwinQuant))}"); + continue; + } + + var reference = _movement.CreateActivatedContextBlanket(seed.TwinConfig.BaseQuant); + _movement.EnsureAllActiveGroupsExplicit(reference, "probe-plan-reference"); + + var changed = _movement.Analyze(reference, seed.CandidateConfig) + .ChangedGroups + .Where(x => x.Movement == QuantMovementKind.Downgrade) + .OrderBy(x => x.Group.UniqueId) + .Take(Config.AnomalyDetection.MaxProbeGroupCount) + .ToList(); + + if (changed.Count == 0) + continue; + + var subsets = BuildProbeSubsets(changed); + int perSeed = 0; + foreach (var subset in subsets) + { + if (perSeed >= Config.AnomalyDetection.MaxProbesPerSeed || plans.Count >= Config.AnomalyDetection.MaxTotalProbesPerRun) + break; + + var probeConfig = reference; + foreach (var g in subset) + probeConfig = _movement.WithStoredSlot(probeConfig, g.Group, g.CandidateStoredSlot); + + if (ShouldSkipInvalidContextualAnomalyConfig(probeConfig, "probe-plan", out var probeSkipReason)) + { + AnsiConsole.MarkupLine($"[yellow]SkippedInvalidContextualAnomalyProbe:[/] source=probe-plan reason={Markup.Escape(probeSkipReason)} probe={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)probeConfig))}"); + continue; + } + + _movement.EnsureAllActiveGroupsExplicit(probeConfig, "probe-plan-probe"); + + if (await _rules.HasSuppressionOrRuleAsync(reference, subset, ct)) + continue; + + string key = TensorConfigIdentity.ToKey(reference) + "=>" + TensorConfigIdentity.ToKey(probeConfig); + if (!seen.Add(key)) + continue; + + var plan = new AnomalyProbePlan + { + Seed = seed, + ReferenceConfig = reference, + ProbeConfig = probeConfig, + ProbeGroups = subset, + ProbeType = ResolveProbeType(subset.Count, changed.Count), + HypothesisLabel = _movement.DescribeGroups(subset) + }; + plans.Add(plan); + perSeed++; + } + + AnsiConsole.MarkupLine( + $"[yellow]Potential anomaly smoke:[/] classification={seed.Movement.Classification} candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(seed.CandidateQuant))} " + + $"higher-bit twin={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(seed.TwinQuant))} changed groups={Markup.Escape(_movement.DescribeGroups(changed))} " + + $"upgradeCount={seed.Movement.UpgradeCount} downgradeCount={seed.Movement.DowngradeCount} " + + $"prediction-space gap={seed.PredictionSpaceGapVsTwin:0.000000} predicted size savings={seed.SizeSavingsBytes:N0} probes queued={perSeed:N0}"); + } + + return plans; + } + + + private async Task> ValidateProbesAsync(IReadOnlyList probes, CancellationToken ct) + { + if (probes.Count == 0) + return new List(); + + var results = new List(); + var safeProbes = new List(); + + foreach (var plan in probes) + { + string referenceReason = string.Empty; + string probeReason = string.Empty; + bool invalidReference = ShouldSkipInvalidContextualAnomalyConfig(plan.ReferenceConfig, "validate-reference", out referenceReason); + bool invalidProbe = ShouldSkipInvalidContextualAnomalyConfig(plan.ProbeConfig, "validate-probe", out probeReason); + if (invalidReference || invalidProbe) + { + string reason = invalidReference ? referenceReason : probeReason; + AnsiConsole.MarkupLine($"[yellow]SkippedInvalidContextualAnomalyProbe:[/] source=validate reason={Markup.Escape(reason)} reference={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)plan.ReferenceConfig))} probe={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)plan.ProbeConfig))}"); + results.Add(new AnomalyProbeResult + { + Plan = plan, + Classification = AnomalyProbeClassification.SuppressionOnly, + RuleDirection = AnomalyRuleDirection.SuppressionOnly, + Accepted = false, + FailureCode = reason.Contains("SparseActiveGroup", StringComparison.OrdinalIgnoreCase) ? "SKIPPED_SPARSE_CONTEXTUAL_PROBE" : "SKIPPED_ISOLATION_SAMPLE", + Message = reason.Contains("SparseActiveGroup", StringComparison.OrdinalIgnoreCase) + ? "SkippedInvalidContextualAnomalyProbe: active groups must be explicitly stored for contextual anomaly probes." + : "SkippedIsolationSample: BF16/exact isolation rows are not contextual anomaly probes." + }); + continue; + } + + _movement.EnsureAllActiveGroupsExplicit(plan.ReferenceConfig, "pre-quantization-reference"); + _movement.EnsureAllActiveGroupsExplicit(plan.ProbeConfig, "pre-quantization-probe"); + safeProbes.Add(plan); + } + + if (safeProbes.Count == 0) + return results; + + foreach (var plan in safeProbes) + WriteContextualProbeConsoleLog(plan); + + var quants = safeProbes + .SelectMany(x => new[] { x.ReferenceConfig, x.ProbeConfig }) + .DistinctBy(TensorConfigIdentity.ToKey) + .Select(x => (HybridQuant)x) + .ToList(); + + AnsiConsole.MarkupLine($"[grey]Validating anomaly probes:[/] unique quant builds/benchmarks=[cyan]{quants.Count:N0}[/] probe plans=[cyan]{safeProbes.Count:N0}[/]"); + await _quantizationService.ProcessHybridBatchAsync(quants, ct); + + foreach (var plan in safeProbes) + { + var reference = await _repository.LoadBenchmarkSnapshotAsync(plan.ReferenceConfig, ct); + var probe = await _repository.LoadBenchmarkSnapshotAsync(plan.ProbeConfig, ct); + var result = ClassifyProbe(plan, reference, probe); + results.Add(result); + + WriteProbeOutcome(result); + } + + return results; + } + + private AnomalyProbeResult ClassifyProbe( + AnomalyProbePlan plan, + BenchmarkSnapshotRecord? reference, + BenchmarkSnapshotRecord? probe) + { + if (reference == null) + { + return new AnomalyProbeResult + { + Plan = plan, + ReferenceSnapshot = null, + ProbeSnapshot = probe, + Classification = AnomalyProbeClassification.MissingTwin, + RuleDirection = AnomalyRuleDirection.SuppressionOnly, + FailureCode = "REFERENCE_TWIN_MISSING", + Message = "Reference/higher-bit twin benchmark was missing after anomaly probe validation." + }; + } + + if (probe == null) + { + return new AnomalyProbeResult + { + Plan = plan, + ReferenceSnapshot = reference, + ProbeSnapshot = null, + Classification = AnomalyProbeClassification.MissingProbeBenchmark, + RuleDirection = AnomalyRuleDirection.SuppressionOnly, + FailureCode = "PROBE_BENCHMARK_MISSING", + Message = "Probe benchmark was missing after anomaly probe validation." + }; + } + + double gain = reference.Kld - probe.Kld; + bool sameOrSmaller = probe.SizeBytes <= reference.SizeBytes; + if (sameOrSmaller && gain >= Config.AnomalyDetection.MinActualGainVsTwinKld) + { + return new AnomalyProbeResult + { + Plan = plan, + ReferenceSnapshot = reference, + ProbeSnapshot = probe, + Classification = plan.ProbeType switch + { + "single" => AnomalyProbeClassification.SingleGroupInversion, + "pair" => AnomalyProbeClassification.PairSynergy, + "full" when plan.ProbeGroups.Count >= 3 => AnomalyProbeClassification.HigherOrderSynergy, + _ => AnomalyProbeClassification.CounterfactualMdaViolation + }, + RuleDirection = AnomalyRuleDirection.Beneficial, + Accepted = true, + ActualGainVsTwin = gain, + Message = "Lower-fidelity monotone probe beat its higher-fidelity same-context twin." + }; + } + + if (probe.Kld - reference.Kld >= Config.AnomalyDetection.MinActualGainVsTwinKld) + { + return new AnomalyProbeResult + { + Plan = plan, + ReferenceSnapshot = reference, + ProbeSnapshot = probe, + Classification = AnomalyProbeClassification.HarmfulInteraction, + RuleDirection = AnomalyRuleDirection.Harmful, + Accepted = true, + ActualGainVsTwin = gain, + Message = "Probe was meaningfully worse than its higher-fidelity twin; persisted as harmful interaction." + }; + } + + return new AnomalyProbeResult + { + Plan = plan, + ReferenceSnapshot = reference, + ProbeSnapshot = probe, + Classification = AnomalyProbeClassification.NormalGravity, + RuleDirection = AnomalyRuleDirection.SuppressionOnly, + Accepted = false, + ActualGainVsTwin = gain, + FailureCode = "NORMAL_GRAVITY", + Message = "Smoke rejected / normal MDA gravity confirmed." + }; + } + + private async Task> LoadAllCurrentBenchmarkSnapshotsAsync(CancellationToken ct) + { + await using var db = new MagicQuantContext(); + var modelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); + if (modelHashId == null) + return new List(); + + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + int? imatrixId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, modelHashId.Value, createIfMissing: false, ct); + + var rows = await db.AiBenchmarks + .AsNoTracking() + .Include(x => x.TensorCombo) + .Include(x => x.CategorBenchmarks) + .Where(x => x.ArchitectureFamilyId == architectureFamilyId) + .Where(x => x.TensorGroupProfileId == tensorGroupProfileId) + .Where(x => x.AiModelHashId == modelHashId.Value) + .Where(x => x.ImatrixDefinitionId == imatrixId) + .ToListAsync(ct); + + var list = new List(); + foreach (var row in rows) + { + var general = row.CategorBenchmarks.FirstOrDefault(x => x.Category == (byte)BenchmarkCategory.General) + ?? row.CategorBenchmarks.OrderBy(x => x.Category).FirstOrDefault(); + if (general == null) + continue; + + var config = new TensorConfig(row.TensorCombo.BaseQuant, row.TensorCombo.Embeddings, row.TensorCombo.LmHead, row.TensorCombo.AttnQ, row.TensorCombo.AttnKV, row.TensorCombo.AttnOutput, row.TensorCombo.FfnUpGate, row.TensorCombo.FfnDown, row.TensorCombo.MoeExperts, row.TensorCombo.MoeRouter); + var quant = (HybridQuant)config; + list.Add(new BenchmarkSnapshotRecord + { + Config = config, + Quant = quant, + DisplayName = HybridBenchmarkRepository.BuildDisplayName(quant), + ProviderName = HybridBenchmarkRepository.ResolveProviderName(quant, exportNaming: false), + BaselineFamily = HybridBenchmarkRepository.ResolveBaselineFamily(quant), + IsHybrid = HybridBenchmarkRepository.IsTrueMagicQuantHybrid(quant), + IsExternalRebuiltBaseline = HybridBenchmarkRepository.IsExternalRebuiltBaseline(quant), + IsMaterializedTensorMapped = quant.Tensors.Count > 0, + SizeBytes = row.SizeBytes, + Kld = general.Kld, + Ppl = general.Ppl + }); + } + + return list; + } + + private async Task> LoadPredictionLookupAsync(CancellationToken ct) + { + var rows = await LoadPredictionRowsAsync(DuckSmokeScanLimit, ct); + return rows.ToDictionary(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal); + } + + private async Task> LoadPredictionRowsAsync(int limit, CancellationToken ct) + { + using var c = new DuckDBConnection($"Data Source={_store.GetDatabaseFilePath()}"); + await c.OpenAsync(ct); + await ConfigureDuckAsync(c, ct); + + using var cmd = c.CreateCommand(); + string limitSql = limit > 0 ? "\nLIMIT ?" : string.Empty; + cmd.CommandText = $@" +SELECT {CombinationDuckDbSchema.SlotColumnList}, + COALESCE(BaseRankSafeKld, PredictedKld) AS BaseRankSafeKld, + COALESCE(FinalPredictedKld, PredictedKld) AS FinalPredictedKld, + PredictedSizeBytes, + PredictionConfidence, + PredictionRank +FROM {CombinationDuckDbSchema.TableName} +WHERE COALESCE(BaseRankSafeKld, PredictedKld) IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.ActiveCandidatePredicateSql} +ORDER BY PredictionRank ASC{limitSql};"; + if (limit > 0) + cmd.Parameters.Add(new DuckDBParameter { Value = limit }); + + var rows = new List(); + using var r = await cmd.ExecuteReaderAsync(ct); + while (await r.ReadAsync(ct)) + rows.Add(ReadPredictionDuckRow(r)); + + return rows; + } + + private async Task LoadSinglePredictionRowAsync(TensorConfig config, CancellationToken ct) + { + using var c = new DuckDBConnection($"Data Source={_store.GetDatabaseFilePath()}"); + await c.OpenAsync(ct); + await ConfigureDuckAsync(c, ct); + + using var cmd = c.CreateCommand(); + cmd.CommandText = $@" +SELECT {CombinationDuckDbSchema.SlotColumnList}, + COALESCE(BaseRankSafeKld, PredictedKld) AS BaseRankSafeKld, + COALESCE(FinalPredictedKld, PredictedKld) AS FinalPredictedKld, + PredictedSizeBytes, + PredictionConfidence, + PredictionRank +FROM {CombinationDuckDbSchema.TableName} +WHERE BaseQuant = ? + AND Embeddings = ? + AND LmHead = ? + AND AttnQ = ? + AND AttnKV = ? + AND AttnOutput = ? + AND FfnUpGate = ? + AND FfnDown = ? + AND MoeExperts = ? + AND MoeRouter = ? +LIMIT 1;"; + AddConfigParameters(cmd, config); + + using var r = await cmd.ExecuteReaderAsync(ct); + if (!await r.ReadAsync(ct)) + return null; + + return ReadPredictionDuckRow(r); + } + + + private async Task LoadContextualTwinPredictionRowAsync( + TensorConfig explicitTwin, + IReadOnlyDictionary explicitRows, + CancellationToken ct) + { + string explicitKey = TensorConfigIdentity.ToKey(explicitTwin); + if (explicitRows.TryGetValue(explicitKey, out var inMemoryExplicit)) + return inMemoryExplicit; + + var explicitRow = await LoadSinglePredictionRowAsync(explicitTwin, ct); + if (explicitRow != null) + return explicitRow with { Config = explicitTwin }; + + // The normal generator may only contain the pure sparse carrier for an all-Q8/all-Q6 + // reference. For smoke scoring, that sparse carrier is allowed as a prediction source + // only; the anomaly seed/probe/twin identity remains the explicit activated blanket. + var sparsePure = new TensorConfig( + explicitTwin.BaseQuant, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue); + + var sparseRow = await LoadSinglePredictionRowAsync(sparsePure, ct); + return sparseRow == null ? null : sparseRow with { Config = sparsePure }; + } + + private static PredictionDuckRow ReadPredictionDuckRow(System.Data.Common.DbDataReader r) + { + return new PredictionDuckRow( + new TensorConfig( + ToByte(r.GetValue(0)), + ToByte(r.GetValue(1)), + ToByte(r.GetValue(2)), + ToByte(r.GetValue(3)), + ToByte(r.GetValue(4)), + ToByte(r.GetValue(5)), + ToByte(r.GetValue(6)), + ToByte(r.GetValue(7)), + ToByte(r.GetValue(8)), + ToByte(r.GetValue(9))), + ToDouble(r.GetValue(10)), + ToDouble(r.GetValue(11)), + ToUInt64(r.GetValue(12)), + ToDouble(r.GetValue(13)), + ToUInt64(r.GetValue(14))); + } + + + private bool ShouldSkipInvalidContextualAnomalyConfig(TensorConfig config, string source, out string reason) + { + if (_movement.TryValidateContextualAnomalyConfig(config, out var validationReason)) + { + reason = string.Empty; + return false; + } + + reason = $"{source}: {validationReason}"; + return true; + } + + private static void LogSkippedInvalidContextualAnomalyConfig(string source, TensorConfig config, string reason, int count) + { + if (!Config.AnomalyDetection.VerboseAnomalyLogging && count > 1) + return; + + if (count > 12) + return; + + string label = reason.Contains("SparseActiveGroup", StringComparison.OrdinalIgnoreCase) + ? "SkippedInvalidContextualAnomalyProbe" + : "SkippedIsolationSample"; + + string canonicalMessage = reason.Contains("SparseActiveGroup", StringComparison.OrdinalIgnoreCase) + ? "active groups must be explicit for contextual anomaly probes" + : "BF16/exact isolation rows are not contextual anomaly probes"; + + AnsiConsole.MarkupLine( + $"[yellow]{label}:[/] {Markup.Escape(canonicalMessage)}. source={Markup.Escape(source)} reason={Markup.Escape(reason)} config={Markup.Escape(TensorConfigIdentity.ToKey(config))} name={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)config))}"); + } + + private static void LogHistoricalSparseCandidateIgnored(TensorConfig config, string reason, int count) + { + if (!Config.AnomalyDetection.VerboseAnomalyLogging && count > 1) + return; + + if (count > 12) + return; + + AnsiConsole.MarkupLine( + $"[yellow]HistoricalSparseCandidateIgnored:[/] reason=CannotTrustTensorComboIdentityForAnomalyRule detail={Markup.Escape(reason)} config={Markup.Escape(TensorConfigIdentity.ToKey(config))} name={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)config))}"); + } + + private void WriteContextualProbeConsoleLog(AnomalyProbePlan plan) + { + var movement = _movement.Analyze(plan.ReferenceConfig, plan.ProbeConfig); + var referenceGroups = _movement.BuildEffectiveGroupVector(plan.ReferenceConfig); + var probeGroups = _movement.BuildEffectiveGroupVector(plan.ProbeConfig); + var inactive = _movement.BuildInactiveGroupList(); + + AnsiConsole.MarkupLine("[yellow]Contextual anomaly probe:[/]"); + AnsiConsole.MarkupLine($"[grey] kind=[/] [cyan]{Markup.Escape(plan.ProbeType)}[/]"); + AnsiConsole.MarkupLine($"[grey] referenceQuant=[/] [cyan]{Markup.Escape(SafeName(plan.ReferenceConfig.BaseQuant))}[/]"); + AnsiConsole.MarkupLine($"[grey] base=[/] [cyan]{Markup.Escape(SafeName(plan.ProbeConfig.BaseQuant))}[/]"); + AnsiConsole.MarkupLine("[grey] effective groups:[/]"); + foreach (var item in probeGroups) + AnsiConsole.MarkupLine($"[grey] {Markup.Escape(item.Key)}=[/] [cyan]{Markup.Escape(item.Value)}[/]"); + + if (inactive.Count > 0) + { + AnsiConsole.MarkupLine("[grey] inactive groups:[/]"); + foreach (var group in inactive) + AnsiConsole.MarkupLine($"[grey] {Markup.Escape(group)}=NULL[/]"); + } + + AnsiConsole.MarkupLine($"[grey] old BF16 isolation=[/] [cyan]false[/]"); + AnsiConsole.MarkupLine($"[grey] movement=[/] [cyan]{movement.Classification}[/] [grey]up={movement.UpgradeCount} down={movement.DowngradeCount} same={movement.SameCount} unknown={movement.UnknownCount}[/]"); + } + + private static IReadOnlyList> BuildProbeSubsets(IReadOnlyList changed) + { + var result = new List>(); + + foreach (var item in changed) + result.Add(new[] { item }); + + for (int i = 0; i < changed.Count; i++) + { + for (int j = i + 1; j < changed.Count; j++) + result.Add(new[] { changed[i], changed[j] }); + } + + result.Add(changed.ToList()); + + if (changed.Count >= 3) + { + for (int i = 0; i < changed.Count; i++) + result.Add(changed.Where((_, index) => index != i).ToList()); + } + + return result + .GroupBy(x => string.Join(",", x.OrderBy(g => g.Group.UniqueId).Select(g => g.Group.UniqueId)), StringComparer.Ordinal) + .Select(g => g.First()) + .ToList(); + } + + private static string ResolveProbeType(int subsetCount, int fullCount) + { + if (subsetCount == 1) return "single"; + if (subsetCount == 2) return "pair"; + if (subsetCount == fullCount) return "full"; + return "leave-one-out"; + } + + private static double ComputeSmokeScore(double gap, double savingsPercent, int changedGroupCount, ulong? candidateRank, ulong? twinRank) + { + double closeness = Math.Max(0d, Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld - gap); + double groupPenalty = Math.Max(1, changedGroupCount); + double rankBonus = 0d; + if (candidateRank.HasValue && twinRank.HasValue) + rankBonus = Math.Clamp((double)twinRank.Value - candidateRank.Value, -10_000d, 10_000d) / 10_000d; + + return (closeness * 10_000d) + savingsPercent / groupPenalty + rankBonus; + } + + private static void WriteSmokeConsoleSummary(int historicalCount, int duckCount, IReadOnlyList selected) + { + int monotone = selected.Count(x => x.Movement.Classification == AnomalyMovementClassification.MonotoneDowngrade); + int existingTwins = selected.Count(x => x.HasActualTwin); + int missingTwins = selected.Count - existingTwins; + + AnsiConsole.MarkupLine("[yellow]Anomaly smoke scan:[/]"); + AnsiConsole.MarkupLine($"[grey] historical benchmarks scanned smoke=[/] [cyan]{historicalCount:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] DuckDB prediction-space smoke=[/] [cyan]{duckCount:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] monotone downgrade smoke candidates=[/] [cyan]{monotone:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] existing twins found=[/] [cyan]{existingTwins:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] missing twins queued=[/] [cyan]{missingTwins:N0}[/]"); + } + + private static void WriteProbeOutcome(AnomalyProbeResult result) + { + if (result.RuleDirection == AnomalyRuleDirection.Beneficial && result.ProbeSnapshot != null && result.ReferenceSnapshot != null) + { + AnsiConsole.MarkupLine( + $"[green]Counterfactual MDA violation confirmed:[/] candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ProbeSnapshot.Quant))} " + + $"twin={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ReferenceSnapshot.Quant))} " + + $"actual candidate KLD={result.ProbeSnapshot.Kld:0.000000} actual twin KLD={result.ReferenceSnapshot.Kld:0.000000} " + + $"gain={result.ActualGainVsTwin:0.000000} classification={result.Classification}"); + return; + } + + if (result.RuleDirection == AnomalyRuleDirection.SuppressionOnly && result.ProbeSnapshot != null && result.ReferenceSnapshot != null) + { + AnsiConsole.MarkupLine( + $"[grey]Smoke rejected / normal gravity confirmed:[/] candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ProbeSnapshot.Quant))} " + + $"twin={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ReferenceSnapshot.Quant))} " + + $"actual candidate KLD={result.ProbeSnapshot.Kld:0.000000} actual twin KLD={result.ReferenceSnapshot.Kld:0.000000} persisted suppression={Config.AnomalyDetection.PersistSuppressionResults}"); + return; + } + + AnsiConsole.MarkupLine($"[yellow]Anomaly probe outcome:[/] {Markup.Escape(result.Classification.ToString())} {Markup.Escape(result.Message)}"); + } + + private static async Task ConfigureDuckAsync(DuckDBConnection c, CancellationToken ct) + { + await ExecuteDuckAsync(c, "SET preserve_insertion_order = false;", ct); + await ExecuteDuckAsync(c, $"SET threads = {Math.Max(1, Environment.ProcessorCount)};", ct); + } + + private static async Task ExecuteDuckAsync(DuckDBConnection c, string sql, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + await cmd.ExecuteNonQueryAsync(ct); + } + + private static void AddConfigParameters(DuckDBCommand cmd, TensorConfig c) + { + cmd.Parameters.Add(new DuckDBParameter { Value = c.BaseQuant }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.Embeddings }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.LmHead }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.AttnQ }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.AttnKV }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.AttnOutput }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.FfnUpGate }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.FfnDown }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.MoeExperts }); + cmd.Parameters.Add(new DuckDBParameter { Value = c.MoeRouter }); + } + + private static async Task WriteJsonAsync(string fileName, object payload, CancellationToken ct) + { + string dir = Cache.ModelMagicQuantDirectory ?? Cache.MagicQuantDirectory ?? Directory.GetCurrentDirectory(); + Directory.CreateDirectory(dir); + string path = Path.Combine(dir, fileName); + await File.WriteAllTextAsync(path, JsonSerializer.Serialize(payload, JsonOptions), ct); + } + + private static async Task WriteFinalManifestAsync(string fileName, object payload, CancellationToken ct) + { + if (string.IsNullOrWhiteSpace(Cache.OutputDirectory)) + return; + + string manifestDir = Path.Combine(Cache.OutputDirectory!, "magicquant-manifest"); + Directory.CreateDirectory(manifestDir); + string path = Path.Combine(manifestDir, fileName); + await File.WriteAllTextAsync(path, JsonSerializer.Serialize(payload, JsonOptions), ct); + } + + + private object ToSmokeLog(AnomalySmokeCandidate x) + { + return new + { + x.Source, + isContextualAnomalySmoke = true, + oldBf16Isolation = false, + allActiveGroupsExplicit = _movement.HasAllActiveGroupsExplicit(x.CandidateConfig) && _movement.HasAllActiveGroupsExplicit(x.TwinConfig), + candidate = TensorConfigIdentity.ToKey(x.CandidateConfig), + twin = TensorConfigIdentity.ToKey(x.TwinConfig), + candidateName = HybridBenchmarkRepository.BuildDisplayName(x.CandidateQuant), + twinName = HybridBenchmarkRepository.BuildDisplayName(x.TwinQuant), + referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(x.TwinConfig), + candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(x.CandidateConfig), + inactiveGroups = _movement.BuildInactiveGroupList(), + movement = x.Movement.Classification.ToString(), + x.Movement.UpgradeCount, + x.Movement.DowngradeCount, + x.Movement.SameCount, + x.Movement.UnknownCount, + changedGroups = x.Movement.ChangedGroups.Select(g => new + { + group = g.Group.Name, + candidate = SafeName(g.CandidateQuantId), + reference = SafeName(g.ReferenceQuantId), + movement = g.Movement.ToString() + }).ToList(), + x.CandidatePredictedKld, + x.TwinPredictedKld, + x.PredictionSpaceGapVsTwin, + x.CandidatePredictedSizeBytes, + x.TwinPredictedSizeBytes, + x.SizeSavingsBytes, + x.CandidatePredictionRank, + x.TwinPredictionRank, + x.SmokeScore, + x.SmokeStrength, + x.HasActualTwin, + x.CandidateActualKld, + x.TwinActualKld, + x.IsConfirmedFromHistory, + x.Message + }; + } + + private object ToProbeLog(AnomalyProbePlan x) + { + var movement = _movement.Analyze(x.ReferenceConfig, x.ProbeConfig); + return new + { + isContextualAnomalyProbe = true, + oldBf16Isolation = false, + allActiveGroupsExplicit = _movement.HasAllActiveGroupsExplicit(x.ReferenceConfig) && _movement.HasAllActiveGroupsExplicit(x.ProbeConfig), + reference = TensorConfigIdentity.ToKey(x.ReferenceConfig), + probe = TensorConfigIdentity.ToKey(x.ProbeConfig), + referenceName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ReferenceConfig), + probeName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ProbeConfig), + referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ReferenceConfig), + candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ProbeConfig), + inactiveGroups = _movement.BuildInactiveGroupList(), + movementClassification = movement.Classification.ToString(), + movement.UpgradeCount, + movement.DowngradeCount, + movement.SameCount, + movement.UnknownCount, + x.ProbeType, + x.HypothesisLabel, + groups = x.ProbeGroups.Select(g => new + { + group = g.Group.Name, + candidate = SafeName(g.CandidateQuantId), + reference = SafeName(g.ReferenceQuantId) + }).ToList() + }; + } + + private object ToResultLog(AnomalyProbeResult x) + { + return new + { + probe = ToProbeLog(x.Plan), + classification = x.Classification.ToString(), + direction = x.RuleDirection.ToString(), + x.Accepted, + x.ActualGainVsTwin, + referenceKld = x.ReferenceSnapshot?.Kld, + probeKld = x.ProbeSnapshot?.Kld, + referenceSizeBytes = x.ReferenceSnapshot?.SizeBytes, + probeSizeBytes = x.ProbeSnapshot?.SizeBytes, + x.FailureCode, + x.Message + }; + } + + private static object ToRuleLog(AnomalyInteractionRule x) + { + return new + { + x.Id, + x.RuleType, + x.RuleDirection, + x.RuleStatus, + x.ReferenceQuantId, + referenceQuant = SafeName(x.ReferenceQuantId), + x.ReferenceContextKey, + x.ReferenceEffectiveGroupsJson, + x.CandidateEffectiveGroupsJson, + x.InactiveGroupsJson, + x.FullTensorConfigKey, + x.GroupSetHash, + x.GroupCount, + x.MeanActualGainVsTwin, + x.BestActualGainVsTwin, + x.MeanPredictionSpaceGap, + x.BestPredictionSpaceGap, + x.AppliedPredictionSpaceAdjustmentKld, + x.EvidenceCount, + x.Confidence, + groups = x.GroupStates.OrderBy(g => g.SortOrder).Select(g => new + { + g.TensorGroupId, + candidate = SafeName(g.CandidateQuantId), + reference = SafeName(g.ReferenceQuantId), + g.Movement + }).ToList() + }; + } + + private static byte ToByte(object? value) + { + if (value is null || value is DBNull) return 0; + if (value is BigInteger big) return (byte)big; + return Convert.ToByte(value, CultureInfo.InvariantCulture); + } + + private static double ToDouble(object? value) + { + if (value is null || value is DBNull) return 0d; + if (value is BigInteger big) return (double)big; + return Convert.ToDouble(value, CultureInfo.InvariantCulture); + } + + private static ulong ToUInt64(object? value) + { + if (value is null || value is DBNull) return 0UL; + if (value is BigInteger big) return (ulong)big; + return Convert.ToUInt64(value, CultureInfo.InvariantCulture); + } + + private static string SafeName(byte quantId) + { + try + { + return BaselineQuants.FromId(quantId).Names[0]; + } + catch + { + return $"id:{quantId}"; + } + } + + private sealed record PredictionDuckRow( + TensorConfig Config, + double BaseRankSafeKld, + double FinalPredictedKld, + ulong PredictedSizeBytes, + double PredictionConfidence, + ulong PredictionRank); +} diff --git a/MagicQuant/Services/CombinationDuckDbSchema.cs b/MagicQuant/Services/CombinationDuckDbSchema.cs index 77a4e06..6287d19 100644 --- a/MagicQuant/Services/CombinationDuckDbSchema.cs +++ b/MagicQuant/Services/CombinationDuckDbSchema.cs @@ -6,7 +6,9 @@ internal static class CombinationDuckDbSchema { public const string TableName = "tensor_configs"; public const string SlotColumnList = "BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter"; - public const string PredictionColumnList = "PredictedKld, PredictedSizeBytes, PredictionConfidence, PredictionRank"; + public const string PredictionColumnList = "PredictedKld, PredictedSizeBytes, PredictionConfidence, PredictionRank, BaseRankSafeKld, AnomalyAdjustmentKld, FinalPredictedKld, IsProtectedAnchor"; + public const string ActiveCandidatePredicateSql = "COALESCE(IsProtectedAnchor, FALSE) = FALSE"; + public const string EffectivePredictedKldSql = "COALESCE(FinalPredictedKld, PredictedKld)"; public const string HybridPredicateSql = "(Embeddings <> 0 OR LmHead <> 0 OR AttnQ <> 0 OR AttnKV <> 0 OR AttnOutput <> 0 OR FfnUpGate <> 0 OR FfnDown <> 0 OR MoeExperts <> 0 OR MoeRouter <> 0)"; public static readonly string[] SlotColumns = @@ -26,7 +28,8 @@ internal static class CombinationDuckDbSchema public static readonly string[] ExpectedColumnTypes = [ "utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint", - "double","ubigint","double","ubigint" + "double","ubigint","double","ubigint", + "double","double","double","boolean" ]; public static string CreateTableSql => $@" @@ -48,7 +51,18 @@ internal static class CombinationDuckDbSchema PredictedKld DOUBLE, PredictedSizeBytes UBIGINT, PredictionConfidence DOUBLE, - PredictionRank UBIGINT + PredictionRank UBIGINT, + + -- Normal PAVA output before scoped anomaly exceptions. + BaseRankSafeKld DOUBLE, + + -- Scoped post-PAVA anomaly/rule adjustment. This is prediction-space only. + AnomalyAdjustmentKld DOUBLE DEFAULT 0.0, + FinalPredictedKld DOUBLE, + + -- Protected/reference anchors may be stored for twin lookup/logging, but must + -- never become active search carriers. Normal generator rows default false. + IsProtectedAnchor BOOLEAN DEFAULT FALSE );"; public static string BuildSlotEqualityPredicate(string leftAlias, string rightAlias) diff --git a/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/MagicQuant/Services/CombinationSurvivalPipelineService.cs index 93c6218..8f3aece 100644 --- a/MagicQuant/Services/CombinationSurvivalPipelineService.cs +++ b/MagicQuant/Services/CombinationSurvivalPipelineService.cs @@ -26,6 +26,7 @@ public sealed class CombinationSurvivalPipelineService private readonly CloneConfigManifestGenerationService _cloneConfigManifestService; private readonly FinalArtifactNamingService _namingService; private readonly IsolationDiagnosticsManifestService _isolationDiagnosticsManifestService; + private readonly AnomalyWorkflowService _anomalyWorkflowService; public CombinationSurvivalPipelineService(QuantizationService quantizationService) { @@ -48,6 +49,7 @@ public CombinationSurvivalPipelineService(QuantizationService quantizationServic _cloneConfigManifestService = new CloneConfigManifestGenerationService(_quantizationService); _namingService = new FinalArtifactNamingService(); _isolationDiagnosticsManifestService = new IsolationDiagnosticsManifestService(); + _anomalyWorkflowService = new AnomalyWorkflowService(_combinationStore, _benchmarkRepository, _quantizationService); } public async Task RunAsync( @@ -74,6 +76,12 @@ public async Task RunAsync( var materialization = await _materializationService.MaterializeAsync(ct); AnsiConsole.MarkupLine($"[green]DuckDB predicted rows:[/] [cyan]{materialization.PredictedRows:N0}[/] / [cyan]{materialization.TotalRows:N0}[/] (ranked: {materialization.RankedRows:N0})"); + var anomalyResult = await _anomalyWorkflowService.RunAsync(pureBaselines, ct); + if (anomalyResult.AdjustmentSummary.MatchedRowCount > 0) + { + AnsiConsole.MarkupLine($"[green]Anomaly-adjusted prediction rows:[/] [cyan]{anomalyResult.AdjustmentSummary.MatchedRowCount:N0}[/] matched by [cyan]{anomalyResult.AdjustmentSummary.AppliedRuleCount:N0}[/] scoped rules. Final selector will use adjusted prediction ranks."); + } + var selection = await _selectionEngine.RunAsync( pureBaselines, ct); diff --git a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs index cff401a..f247b2c 100644 --- a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs +++ b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs @@ -54,7 +54,11 @@ await ExecuteAsync(c, $@" SET PredictedKld = NULL, PredictedSizeBytes = NULL, PredictionConfidence = NULL, - PredictionRank = NULL;", ct); + PredictionRank = NULL, + BaseRankSafeKld = NULL, + AnomalyAdjustmentKld = 0.0, + FinalPredictedKld = NULL, + IsProtectedAnchor = FALSE;", ct); await BuildLookupTablesAsync(c, model, ct); await PrintLookupDiagnosticsAsync(c, model, ct); @@ -484,7 +488,11 @@ await ExecuteAsync(c, $@" SET PredictedKld = r.PredictedKld, PredictedSizeBytes = r.PredictedSizeBytes, PredictionConfidence = r.PredictionConfidence, - PredictionRank = r.PredictionRank + PredictionRank = r.PredictionRank, + BaseRankSafeKld = r.PredictedKld, + AnomalyAdjustmentKld = 0.0, + FinalPredictedKld = r.PredictedKld, + IsProtectedAnchor = FALSE FROM temp_ranked_prediction_with_rank r WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "r")};", ct); } @@ -908,4 +916,4 @@ public sealed class PredictionMaterializationStatus public double? MaxPredictedKld { get; init; } public ulong? MinPredictedSizeBytes { get; init; } public ulong? MaxPredictedSizeBytes { get; init; } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/QuantFidelityComparerService.cs b/MagicQuant/Services/QuantFidelityComparerService.cs new file mode 100644 index 0000000..361ed5c --- /dev/null +++ b/MagicQuant/Services/QuantFidelityComparerService.cs @@ -0,0 +1,483 @@ +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; + +namespace MagicQuant.Services; + +public sealed class QuantFidelityComparerService +{ + private static readonly TensorGroup[] OrderedGroups = + [ + TReg.Embeddings, + TReg.LmHead, + TReg.AttnQ, + TReg.AttnKV, + TReg.AttnOutput, + TReg.FfnUpGate, + TReg.FfnDown, + TReg.MoeExperts, + TReg.MoeRouter + ]; + + public IReadOnlyList ActiveGroups => OrderedGroups + .Where(g => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == g.UniqueId)) + .OrderBy(g => g.UniqueId) + .ToList(); + + public IReadOnlyList InactiveGroups => OrderedGroups + .Where(g => Cache.UnusedTensorGroups.Any(u => u.UniqueId == g.UniqueId)) + .OrderBy(g => g.UniqueId) + .ToList(); + + public AnomalyMovementAnalysis Analyze(TensorConfig reference, TensorConfig candidate) + { + var changed = new List(); + int upgrades = 0; + int downgrades = 0; + int same = 0; + int unknown = 0; + int lateral = 0; + int net = 0; + + foreach (var group in ActiveGroups) + { + byte referenceStored = GetStoredSlot(reference, group); + byte candidateStored = GetStoredSlot(candidate, group); + byte referenceQuant = EffectiveQuantId(reference, group); + byte candidateQuant = EffectiveQuantId(candidate, group); + var movement = Compare(referenceQuant, candidateQuant); + + switch (movement) + { + case QuantMovementKind.Upgrade: + upgrades++; + break; + case QuantMovementKind.Downgrade: + downgrades++; + break; + case QuantMovementKind.Same: + same++; + break; + case QuantMovementKind.LateralOrEquivalent: + lateral++; + break; + case QuantMovementKind.Unknown: + unknown++; + break; + } + + net += EffectiveTier(candidateQuant) - EffectiveTier(referenceQuant); + + if (movement != QuantMovementKind.Same || referenceStored != candidateStored) + { + changed.Add(new AnomalyChangedGroup + { + Group = group, + ReferenceQuantId = referenceQuant, + CandidateQuantId = candidateQuant, + ReferenceStoredSlot = referenceStored, + CandidateStoredSlot = candidateStored, + Movement = movement + }); + } + } + + AnomalyMovementClassification classification; + if (downgrades > 0 && upgrades == 0 && unknown == 0) + classification = AnomalyMovementClassification.MonotoneDowngrade; + else if (downgrades > 0 && upgrades > 0) + classification = AnomalyMovementClassification.MixedTrade; + else if (upgrades > 0 && downgrades == 0) + classification = AnomalyMovementClassification.MonotoneUpgrade; + else if (lateral > 0 && downgrades == 0 && upgrades == 0) + classification = AnomalyMovementClassification.LateralOrProviderEquivalent; + else if (changed.Count == 0) + classification = AnomalyMovementClassification.NoMovement; + else + classification = AnomalyMovementClassification.Unknown; + + return new AnomalyMovementAnalysis + { + Classification = classification, + ChangedGroups = changed, + UpgradeCount = upgrades, + DowngradeCount = downgrades, + SameCount = same, + UnknownCount = unknown, + LateralCount = lateral, + NetBitDelta = net + }; + } + + public QuantMovementKind Compare(byte referenceQuantId, byte candidateQuantId) + { + if (referenceQuantId == candidateQuantId) + return QuantMovementKind.Same; + + int referenceTier = EffectiveTier(referenceQuantId); + int candidateTier = EffectiveTier(candidateQuantId); + + if (referenceTier < 0 || candidateTier < 0) + return QuantMovementKind.Unknown; + + if (candidateTier == referenceTier) + return QuantMovementKind.LateralOrEquivalent; + + return candidateTier < referenceTier + ? QuantMovementKind.Downgrade + : QuantMovementKind.Upgrade; + } + + /// + /// Builds an explicit contextual quantized blanket: base=referenceQuantId and every + /// active tensor group is explicitly stored as that same learned quant. Inactive + /// groups remain NULL so dense/MoE architecture differences are preserved. + /// + /// This is the anomaly-world equivalent of the old exact blanket, except it is + /// intentionally quantized context, not BF16/F16/native isolation truth. + /// + public TensorConfig CreateActivatedContextBlanket(byte referenceQuantId) + { + if (BaselineQuants.IsNativeExactAlias(referenceQuantId)) + { + throw new InvalidOperationException( + $"SkippedInvalidContextualAnomalyProbe: reason=BF16ExactIsolationSample referenceQuant={SafeName(referenceQuantId)}"); + } + + byte stored = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(referenceQuantId); + var config = new TensorConfig( + baseQuant: referenceQuantId, + embeddings: BaselineQuants.TensorConfigNullSlotValue, + lmHead: BaselineQuants.TensorConfigNullSlotValue, + attnQ: BaselineQuants.TensorConfigNullSlotValue, + attnKV: BaselineQuants.TensorConfigNullSlotValue, + attnOutput: BaselineQuants.TensorConfigNullSlotValue, + ffnUpGate: BaselineQuants.TensorConfigNullSlotValue, + ffnDown: BaselineQuants.TensorConfigNullSlotValue, + moeExperts: BaselineQuants.TensorConfigNullSlotValue, + moeRouter: BaselineQuants.TensorConfigNullSlotValue); + + foreach (var group in ActiveGroups.OrderBy(g => g.UniqueId)) + config = WithStoredSlot(config, group, stored); + + return config; + } + + /// + /// Builds the contextual higher-bit twin for anomaly detection. + /// + /// This is intentionally NOT the BF16/exact isolation reference used by normal + /// tensor-group learning. For anomaly smoke/probes, the reference is an explicit + /// activated quantized context: base=Q8 means every active group is explicitly Q8; + /// base=Q6 means every active group is explicitly Q6; and so on. + /// + public TensorConfig BuildBaseContextTwin(TensorConfig candidate) => CreateActivatedContextBlanket(candidate.BaseQuant); + + /// + /// Converts a normal generated DuckDB row into the explicit anomaly context shape. + /// This is allowed for prediction-space smoke only: sparse generated rows are not + /// treated as historical truth and are never persisted as anomaly probes/rules. + /// + public bool TryNormalizeSparseDuckRowToActivatedContext( + TensorConfig source, + out TensorConfig activated, + out bool hadSparseActiveGroups, + out string reason) + { + activated = default; + hadSparseActiveGroups = false; + + if (BaselineQuants.IsNativeExactAlias(source.BaseQuant)) + { + reason = $"BF16ExactIsolationSample: base={SafeName(source.BaseQuant)}"; + return false; + } + + if (HasIsolationDisplayMarker(source)) + { + reason = $"BF16ExactIsolationSample: displayName={HybridBenchmarkRepository.BuildDisplayName((HybridQuant)source)}"; + return false; + } + + TensorConfig result; + try + { + result = CreateActivatedContextBlanket(source.BaseQuant); + } + catch (Exception ex) + { + reason = ex.Message; + return false; + } + + foreach (var group in ActiveGroups) + { + byte stored = GetStoredSlot(source, group); + if (BaselineQuants.IsNullTensorConfigGroupSlot(stored)) + { + hadSparseActiveGroups = true; + continue; + } + + byte quantId = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(stored); + if (BaselineQuants.IsNativeExactAlias(quantId)) + { + reason = $"BF16ExactIsolationSample: {group.Name}={SafeName(quantId)}"; + return false; + } + + result = WithStoredSlot(result, group, stored); + } + + foreach (var group in InactiveGroups) + { + if (!BaselineQuants.IsNullTensorConfigGroupSlot(GetStoredSlot(result, group))) + { + result = WithStoredSlot(result, group, BaselineQuants.TensorConfigNullSlotValue); + } + } + + if (!TryValidateContextualAnomalyConfig(result, out reason)) + return false; + + activated = result; + reason = hadSparseActiveGroups + ? "Sparse DuckDB prediction row normalized into explicit activated contextual anomaly vector." + : string.Empty; + return true; + } + + public bool IsNativeExactQuantId(byte quantId) => BaselineQuants.IsNativeExactAlias(quantId); + + public bool IsContextualQuantizedConfig(TensorConfig config) => TryValidateContextualAnomalyConfig(config, out _); + + public bool TryValidateContextualAnomalyConfig(TensorConfig config, out string reason) + { + if (BaselineQuants.IsNativeExactAlias(config.BaseQuant)) + { + reason = $"BF16ExactIsolationSample: base={SafeName(config.BaseQuant)}"; + return false; + } + + if (HasIsolationDisplayMarker(config)) + { + reason = $"BF16ExactIsolationSample: display name contains BF16/F16/native/exact marker ({HybridBenchmarkRepository.BuildDisplayName((HybridQuant)config)})"; + return false; + } + + foreach (var group in ActiveGroups) + { + byte stored = GetStoredSlot(config, group); + if (BaselineQuants.IsNullTensorConfigGroupSlot(stored)) + { + reason = $"SparseActiveGroup: group={group.Name} shortCode={group.ShortCode}"; + return false; + } + + byte quantId = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(stored); + if (BaselineQuants.IsNativeExactAlias(quantId)) + { + reason = $"BF16ExactIsolationSample: {group.Name}={SafeName(quantId)}"; + return false; + } + } + + foreach (var group in InactiveGroups) + { + byte stored = GetStoredSlot(config, group); + if (!BaselineQuants.IsNullTensorConfigGroupSlot(stored)) + { + byte quantId = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(stored); + if (BaselineQuants.IsNativeExactAlias(quantId)) + { + reason = $"BF16ExactIsolationSample: inactive {group.Name}={SafeName(quantId)}"; + return false; + } + } + } + + reason = string.Empty; + return true; + } + + public void EnsureAllActiveGroupsExplicit(TensorConfig config, string purpose) + { + if (TryValidateContextualAnomalyConfig(config, out _)) + return; + + TryValidateContextualAnomalyConfig(config, out var reason); + throw new InvalidOperationException( + $"SkippedInvalidContextualAnomalyProbe: purpose={purpose} reason={reason} config={TensorConfigIdentity.ToKey(config)} name={HybridBenchmarkRepository.BuildDisplayName((HybridQuant)config)}"); + } + + public bool TryDescribeNativeExactActiveState(TensorConfig config, out string reason) + { + if (BaselineQuants.IsNativeExactAlias(config.BaseQuant)) + { + reason = $"base={SafeName(config.BaseQuant)}"; + return true; + } + + foreach (var group in ActiveGroups) + { + byte stored = GetStoredSlot(config, group); + if (BaselineQuants.IsNullTensorConfigGroupSlot(stored)) + continue; + + byte effective = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(stored); + if (BaselineQuants.IsNativeExactAlias(effective)) + { + reason = $"{group.ShortCode}={SafeName(effective)}"; + return true; + } + } + + reason = string.Empty; + return false; + } + + public bool HasIsolationDisplayMarker(TensorConfig config) + { + string displayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)config); + return displayName.Contains("-B16", StringComparison.OrdinalIgnoreCase) || + displayName.Contains("BF16", StringComparison.OrdinalIgnoreCase) || + displayName.Contains("F16", StringComparison.OrdinalIgnoreCase) || + displayName.Contains("NATIVE", StringComparison.OrdinalIgnoreCase) || + displayName.Contains("EXACT", StringComparison.OrdinalIgnoreCase); + } + + public bool IsContextualQuantizedRule(AnomalyInteractionRule rule) + { + if (BaselineQuants.IsNativeExactAlias(rule.ReferenceQuantId)) + return false; + + foreach (var state in rule.GroupStates) + { + if (BaselineQuants.IsNativeExactAlias(state.CandidateQuantId) || + BaselineQuants.IsNativeExactAlias(state.ReferenceQuantId)) + { + return false; + } + } + + return true; + } + + public byte EffectiveQuantId(TensorConfig config, TensorGroup group) + { + byte stored = GetStoredSlot(config, group); + return BaselineQuants.IsNullTensorConfigGroupSlot(stored) + ? config.BaseQuant + : BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(stored); + } + + public byte GetStoredSlot(TensorConfig config, TensorGroup group) + { + return group.UniqueId switch + { + var id when id == TReg.Embeddings.UniqueId => config.Embeddings, + var id when id == TReg.LmHead.UniqueId => config.LmHead, + var id when id == TReg.AttnQ.UniqueId => config.AttnQ, + var id when id == TReg.AttnKV.UniqueId => config.AttnKV, + var id when id == TReg.AttnOutput.UniqueId => config.AttnOutput, + var id when id == TReg.FfnUpGate.UniqueId => config.FfnUpGate, + var id when id == TReg.FfnDown.UniqueId => config.FfnDown, + var id when id == TReg.MoeExperts.UniqueId => config.MoeExperts, + var id when id == TReg.MoeRouter.UniqueId => config.MoeRouter, + _ => throw new InvalidOperationException($"Unknown tensor group id '{group.UniqueId}'.") + }; + } + + public TensorConfig WithStoredSlot(TensorConfig config, TensorGroup group, byte storedSlot) + { + return new TensorConfig( + config.BaseQuant, + group.UniqueId == TReg.Embeddings.UniqueId ? storedSlot : config.Embeddings, + group.UniqueId == TReg.LmHead.UniqueId ? storedSlot : config.LmHead, + group.UniqueId == TReg.AttnQ.UniqueId ? storedSlot : config.AttnQ, + group.UniqueId == TReg.AttnKV.UniqueId ? storedSlot : config.AttnKV, + group.UniqueId == TReg.AttnOutput.UniqueId ? storedSlot : config.AttnOutput, + group.UniqueId == TReg.FfnUpGate.UniqueId ? storedSlot : config.FfnUpGate, + group.UniqueId == TReg.FfnDown.UniqueId ? storedSlot : config.FfnDown, + group.UniqueId == TReg.MoeExperts.UniqueId ? storedSlot : config.MoeExperts, + group.UniqueId == TReg.MoeRouter.UniqueId ? storedSlot : config.MoeRouter); + } + + public string BuildChangedGroupHash(IEnumerable groups) + { + string key = string.Join("|", groups + .OrderBy(x => x.Group.UniqueId) + .Select(x => $"{x.Group.UniqueId}:{x.ReferenceQuantId}->{x.CandidateQuantId}")); + + return TensorConfigIdentity.StableHash(key); + } + + public string DescribeGroups(IEnumerable groups) + { + return string.Join(" + ", groups + .OrderBy(x => x.Group.UniqueId) + .Select(x => $"{x.Group.ShortCode}={SafeName(x.CandidateQuantId)} from {SafeName(x.ReferenceQuantId)}")); + } + + public string ReferenceContextKey(TensorConfig reference) + { + return string.Join("|", ActiveGroups.Select(g => $"{g.UniqueId}:{EffectiveQuantId(reference, g)}")); + } + + public Dictionary BuildEffectiveGroupVector(TensorConfig config) + { + return ActiveGroups + .OrderBy(g => g.UniqueId) + .ToDictionary( + g => g.Name, + g => SafeName(EffectiveQuantId(config, g)), + StringComparer.Ordinal); + } + + public IReadOnlyList BuildInactiveGroupList() => InactiveGroups + .OrderBy(g => g.UniqueId) + .Select(g => g.Name) + .ToList(); + + public bool HasAllActiveGroupsExplicit(TensorConfig config) + { + foreach (var group in ActiveGroups) + { + if (BaselineQuants.IsNullTensorConfigGroupSlot(GetStoredSlot(config, group))) + return false; + } + + return true; + } + + public int EffectiveTier(byte quantId) + { + if (BaselineQuants.IsNativeExactAlias(quantId)) + return 160; + + var baseline = BaselineQuants.FromId(quantId); + string name = baseline.Names[0].ToUpperInvariant(); + + if (name.Contains("Q8") || baseline.BitRange >= 8) return 80; + if (name.Contains("Q6") || baseline.BitRange == 6) return 60; + if (name.Contains("Q5") || baseline.BitRange == 5) return 50; + if (name.Contains("Q4") || name.Contains("IQ4") || baseline.BitRange == 4) return 40; + if (name.Contains("Q3") || name.Contains("IQ3") || baseline.BitRange == 3) return 30; + if (name.Contains("Q2") || name.Contains("IQ2") || baseline.BitRange == 2) return 20; + + return baseline.BitRange > 0 ? baseline.BitRange * 10 : -1; + } + + private static string SafeName(byte quantId) + { + try + { + return BaselineQuants.FromId(quantId).Names[0]; + } + catch + { + return $"id:{quantId}"; + } + } +} diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 9821787..857eccd 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -154,12 +154,13 @@ public async Task CountStrictDominanceCandidatesAsync( string sql = $@" SELECT COUNT(*) FROM {TableName} -WHERE PredictedKld IS NOT NULL +WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL AND PredictedSizeBytes IS NOT NULL AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.ActiveCandidatePredicateSql} AND {CombinationDuckDbSchema.HybridPredicateSql} AND PredictedSizeBytes <= ? - AND PredictedKld + ? < ?;"; + AND {CombinationDuckDbSchema.EffectivePredictedKldSql} + ? < ?;"; return await ExecuteCountAsync( sql, @@ -175,9 +176,10 @@ public async Task CountPredictedHybridCandidatesInSizeWindowAsync( string sql = $@" SELECT COUNT(*) FROM {TableName} -WHERE PredictedKld IS NOT NULL +WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL AND PredictedSizeBytes IS NOT NULL AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.ActiveCandidatePredicateSql} AND {CombinationDuckDbSchema.HybridPredicateSql} AND PredictedSizeBytes BETWEEN ? AND ?;"; @@ -193,15 +195,16 @@ public async Task CountBetterThanLinearCandidatesAsync( { string sql = $@" WITH scored AS ( - SELECT PredictedKld, + SELECT {CombinationDuckDbSchema.EffectivePredictedKldSql} AS PredictedKld, PredictedSizeBytes, (CAST(? AS DOUBLE) + ((CAST(PredictedSizeBytes AS DOUBLE) - CAST(? AS DOUBLE)) / GREATEST(CAST(? AS DOUBLE), 1.0)) * (CAST(? AS DOUBLE) - CAST(? AS DOUBLE))) AS LinearExpectedKld FROM {TableName} - WHERE PredictedKld IS NOT NULL + WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL AND PredictedSizeBytes IS NOT NULL AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.ActiveCandidatePredicateSql} AND {CombinationDuckDbSchema.HybridPredicateSql} AND PredictedSizeBytes BETWEEN ? AND ? ) @@ -236,17 +239,18 @@ public async Task> QueryStrictDominanceCand { string sql = $@" SELECT {CombinationDuckDbSchema.SlotColumnList}, - PredictedKld, + {CombinationDuckDbSchema.EffectivePredictedKldSql} AS PredictedKld, PredictedSizeBytes, PredictionConfidence, PredictionRank FROM {TableName} -WHERE PredictedKld IS NOT NULL +WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL AND PredictedSizeBytes IS NOT NULL AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.ActiveCandidatePredicateSql} AND {CombinationDuckDbSchema.HybridPredicateSql} AND PredictedSizeBytes <= ? - AND PredictedKld + ? < ? + AND {CombinationDuckDbSchema.EffectivePredictedKldSql} + ? < ? ORDER BY PredictedSizeBytes ASC, PredictedKld ASC, PredictionRank ASC, @@ -272,7 +276,7 @@ public async Task> QueryBetterThanLinear string sql = $@" WITH scored AS ( SELECT {CombinationDuckDbSchema.SlotColumnList}, - PredictedKld, + {CombinationDuckDbSchema.EffectivePredictedKldSql} AS PredictedKld, PredictedSizeBytes, PredictionConfidence, PredictionRank, @@ -280,9 +284,10 @@ WITH scored AS ( + ((CAST(PredictedSizeBytes AS DOUBLE) - CAST(? AS DOUBLE)) / GREATEST(CAST(? AS DOUBLE), 1.0)) * (CAST(? AS DOUBLE) - CAST(? AS DOUBLE))) AS LinearExpectedKld FROM {TableName} - WHERE PredictedKld IS NOT NULL + WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL AND PredictedSizeBytes IS NOT NULL AND PredictionRank IS NOT NULL + AND {CombinationDuckDbSchema.ActiveCandidatePredicateSql} AND {CombinationDuckDbSchema.HybridPredicateSql} AND PredictedSizeBytes BETWEEN ? AND ? ), @@ -556,4 +561,4 @@ private static async Task EnsureTensorConfigsTableExistsAsync(DuckDBConnection c "This almost always means the generator and prediction reader are using different DuckDB filenames, " + "or prediction started before QuantDatabaseService initialized/rebuilt the search-space table."); } -} \ No newline at end of file +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 3fb3ba0..2873b9b 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -255,6 +255,60 @@ candidate_selection: # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. allow_eight_bit_anchor_replacements: true +anomaly_detection: + enabled: true + + # One anomaly refinement pass after smoke/probe/rule generation. + max_anomaly_refinement_rounds: 1 + + # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. + min_actual_gain_vs_twin_kld: 0.00025 + + # Minimum predicted size savings versus higher-bit twin/reference to probe. + min_predicted_size_savings_vs_twin_percent: 1.0 + + # Max changed groups in a candidate that can seed contextual probes. + max_probe_group_count: 4 + + # Max probes generated per anomaly seed. + max_probes_per_seed: 16 + + # Max anomaly probes in one run. + max_total_probes_per_run: 32 + + # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. + max_prediction_space_gap_vs_twin_kld: 0.00050 + + # Optional relative cap for prediction-space gap normalized by local anchor gap. + max_relative_prediction_penalty_vs_twin: 0.35 + + # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. + prediction_space_violation_margin: 0.00005 + + # Shrink applied to prediction-space adjustment after a rule is confirmed. + anomaly_adjustment_shrink_factor: 0.70 + + # Minimum confidence required before applying a confirmed anomaly rule. + min_rule_confidence_to_apply: 0.50 + + # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. + max_negative_adjustment_kld: 0.002 + + # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. + max_positive_adjustment_kld: 0.002 + + # Fractional cap relative to BaseRankSafeKld. + max_adjustment_fraction_of_base_kld: 0.75 + + # Number of top smoke candidates to consider per reference quant zone. + max_smoke_candidates_per_reference_zone: 12 + + # Store suppression-only results so false smoke is not repeatedly probed. + persist_suppression_results: true + + # Emit detailed anomaly logs. + verbose_anomaly_logging: true + output: # Optional explicit output directory. # If blank, MagicQuant will default to: diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index b2e006c..7bc086e 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -177,6 +177,60 @@ candidate_selection: # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. allow_eight_bit_anchor_replacements: true +anomaly_detection: + enabled: true + + # One anomaly refinement pass after smoke/probe/rule generation. + max_anomaly_refinement_rounds: 1 + + # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. + min_actual_gain_vs_twin_kld: 0.00025 + + # Minimum predicted size savings versus higher-bit twin/reference to probe. + min_predicted_size_savings_vs_twin_percent: 1.0 + + # Max changed groups in a candidate that can seed contextual probes. + max_probe_group_count: 4 + + # Max probes generated per anomaly seed. + max_probes_per_seed: 16 + + # Max anomaly probes in one run. + max_total_probes_per_run: 32 + + # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. + max_prediction_space_gap_vs_twin_kld: 0.00050 + + # Optional relative cap for prediction-space gap normalized by local anchor gap. + max_relative_prediction_penalty_vs_twin: 0.35 + + # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. + prediction_space_violation_margin: 0.00005 + + # Shrink applied to prediction-space adjustment after a rule is confirmed. + anomaly_adjustment_shrink_factor: 0.70 + + # Minimum confidence required before applying a confirmed anomaly rule. + min_rule_confidence_to_apply: 0.50 + + # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. + max_negative_adjustment_kld: 0.002 + + # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. + max_positive_adjustment_kld: 0.002 + + # Fractional cap relative to BaseRankSafeKld. + max_adjustment_fraction_of_base_kld: 0.75 + + # Number of top smoke candidates to consider per reference quant zone. + max_smoke_candidates_per_reference_zone: 12 + + # Store suppression-only results so false smoke is not repeatedly probed. + persist_suppression_results: true + + # Emit detailed anomaly logs. + verbose_anomaly_logging: true + output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: From ec1691e9e729d5da993f3052a5d2d89c24dc49c6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 3 May 2026 19:02:47 -0400 Subject: [PATCH 187/258] predictive engine doing better but still very flawed --- ...PredictionEngineAnomalyDetect2.Designer.cs | 1738 +++++++++++++++++ ...03224957_PredictionEngineAnomalyDetect2.cs | 169 ++ .../MagicQuantContextModelSnapshot.cs | 55 +- MQ.DB/Models/DbModels/AnomalyProbeSession.cs | 25 +- MagicQuant/Models/AnomalyDetectionModels.cs | 25 +- MagicQuant/Services/AnomalyRuleRepository.cs | 30 +- MagicQuant/Services/AnomalyWorkflowService.cs | 369 +++- 7 files changed, 2372 insertions(+), 39 deletions(-) create mode 100644 MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs create mode 100644 MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs diff --git a/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs b/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs new file mode 100644 index 0000000..bc48555 --- /dev/null +++ b/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs @@ -0,0 +1,1738 @@ +// +using System; +using MQ.DB.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MQ.DB.Migrations +{ + [DbContext(typeof(MagicQuantContext))] + [Migration("20260503224957_PredictionEngineAnomalyDetect2")] + partial class PredictionEngineAnomalyDetect2 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.1"); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("Ngl") + .HasColumnType("INTEGER"); + + b.Property("SizeBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("TokensPerSecond") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "TensorComboId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "TensorComboId") + .IsUnique(); + + b.ToTable("AiBenchmarks"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmarkLearnedSource", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("SourceLearningBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaselineQuantDefinitionId"); + + b.HasIndex("SourceLearningBenchmarkId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("AiBenchmarkId", "TensorGroupId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId"); + + b.ToTable("AiBenchmarkLearnedSources"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("UniqueHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UniqueHash"); + + b.ToTable("AiModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("AppliedPredictionSpaceAdjustmentKld") + .HasColumnType("REAL"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("BestActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("BestPredictionSpaceGap") + .HasColumnType("REAL"); + + b.Property("CandidateDisplayName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CandidateEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CandidateInternalName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("Confidence") + .HasColumnType("REAL"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("EvidenceCount") + .HasColumnType("INTEGER"); + + b.Property("FullTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("GroupCount") + .HasColumnType("INTEGER"); + + b.Property("GroupSetHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("InactiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("MeanActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("MeanPredictionSpaceGap") + .HasColumnType("REAL"); + + b.Property("MetadataJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("MovementClassification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceContextKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ReferenceDisplayName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ReferenceEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ReferenceInternalName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("RuleDirection") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RuleStatus") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RuleType") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ShrinkFactor") + .HasColumnType("REAL"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("FullTensorConfigKey"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "RuleDirection", "RuleStatus"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceQuantId", "ReferenceContextKey", "GroupSetHash", "RuleDirection") + .IsUnique(); + + b.ToTable("AnomalyInteractionRules"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRuleGroupState", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("CandidateQuantId") + .HasColumnType("INTEGER"); + + b.Property("Movement") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("RuleId") + .HasColumnType("TEXT"); + + b.Property("SortOrder") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("RuleId", "TensorGroupId") + .IsUnique(); + + b.ToTable("AnomalyInteractionRuleGroupStates"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeObservation", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("Accepted") + .HasColumnType("INTEGER"); + + b.Property("ActualGainVsTwin") + .HasColumnType("REAL"); + + b.Property("ActualKld") + .HasColumnType("REAL"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("AllActiveGroupsExplicit") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("CandidateEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CandidateQuantsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ChangedGroupSetHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ChangedGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Classification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DowngradeCount") + .HasColumnType("INTEGER"); + + b.Property("FailureCode") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("HypothesisLabel") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("InactiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("IsContextualAnomalyProbe") + .HasColumnType("INTEGER"); + + b.Property("Message") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("MovementClassification") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("NetBitDelta") + .HasColumnType("INTEGER"); + + b.Property("OldBf16Isolation") + .HasColumnType("INTEGER"); + + b.Property("PredictedKld") + .HasColumnType("REAL"); + + b.Property("PredictionSpaceGapVsTwin") + .HasColumnType("REAL"); + + b.Property("ProbeDisplayName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ProbeInternalName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ProbePlanClass") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ProbeTensorComboId") + .HasColumnType("TEXT"); + + b.Property("ProbeTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ProbeType") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("ReferenceActualKld") + .HasColumnType("REAL"); + + b.Property("ReferenceDisplayName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ReferenceEffectiveGroupsJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ReferenceInternalName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ReferencePredictedKld") + .HasColumnType("REAL"); + + b.Property("ReferenceQuantId") + .HasColumnType("INTEGER"); + + b.Property("ReferenceTensorComboId") + .HasColumnType("TEXT"); + + b.Property("ReferenceTensorConfigKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("RuleDirection") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SameCount") + .HasColumnType("INTEGER"); + + b.Property("SeedClass") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SeedPriority") + .HasColumnType("INTEGER"); + + b.Property("SessionId") + .HasColumnType("TEXT"); + + b.Property("SizeSavingsBytes") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("UnknownCount") + .HasColumnType("INTEGER"); + + b.Property("UpgradeCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("ProbeTensorComboId"); + + b.HasIndex("ProbeTensorConfigKey"); + + b.HasIndex("ReferenceTensorComboId"); + + b.HasIndex("ReferenceTensorConfigKey"); + + b.HasIndex("SessionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ChangedGroupSetHash"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceTensorComboId", "ProbeTensorComboId", "ProbeType"); + + b.ToTable("AnomalyProbeObservations"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BenchmarkCategory") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("ConfigJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("SourceRunLabel") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "StartedUtc"); + + b.ToTable("AnomalyProbeSessions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamily", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("NormalizedName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("TensorCount") + .HasColumnType("INTEGER"); + + b.Property("TensorSignatureHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique(); + + b.HasIndex("TensorSignatureHash", "TensorCount"); + + b.ToTable("ArchitectureFamilies"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IsCanonical") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "AiModelHashId") + .IsUnique(); + + b.ToTable("ArchitectureFamilyModelHashes"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BaselineFamily") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("BaselineName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("BitRange") + .HasColumnType("INTEGER"); + + b.Property("CanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("DefaultTensorSchemeId") + .HasColumnType("INTEGER"); + + b.Property("DefaultTensorSchemeName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("ExplicitCandidateSortOrder") + .HasColumnType("INTEGER"); + + b.Property("FirstSeenUtc") + .HasColumnType("TEXT"); + + b.Property("IsActiveInCurrentConfig") + .HasColumnType("INTEGER"); + + b.Property("IsCombinationCarrierCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsCustomBaseline") + .HasColumnType("INTEGER"); + + b.Property("IsExplicitGroupCombinationCandidate") + .HasColumnType("INTEGER"); + + b.Property("IsLearningBaseline") + .HasColumnType("INTEGER"); + + b.Property("LastSeenUtc") + .HasColumnType("TEXT"); + + b.Property("LastUpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("NormalizedCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("NormalizedSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("NormalizedSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("QuantizeBaseArgumentName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("RequiresImatrix") + .HasColumnType("INTEGER"); + + b.Property("RuntimeBaselineId") + .HasColumnType("INTEGER"); + + b.Property("ShortSourceName") + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SourceOwner") + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("SourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("IsActiveInCurrentConfig"); + + b.HasIndex("ArchitectureFamilyId", "NormalizedCanonicalKey") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "RuntimeBaselineId") + .IsUnique(); + + b.HasIndex("RuntimeBaselineId", "ArchitectureFamilyId"); + + b.HasIndex("ArchitectureFamilyId", "NormalizedSourceRepository", "NormalizedSourceFileName") + .IsUnique(); + + b.ToTable("BaselineQuantDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("CategoryBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ArchitectureFamilyId"); + + b.HasIndex("CategoryBenchmarkId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("AiBenchmarkId", "Category"); + + b.ToTable("BenchmarkRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("Category") + .HasColumnType("INTEGER"); + + b.Property("Kld") + .HasColumnType("REAL"); + + b.Property("Ppl") + .HasColumnType("REAL"); + + b.Property("PplError") + .HasColumnType("REAL"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.ToTable("CategoryBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("DiscoveryTokenTarget") + .HasColumnType("INTEGER"); + + b.Property("GpuMemoryLimitsJson") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("GroupSize") + .HasColumnType("INTEGER"); + + b.Property("HardwareFingerprint") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("MaxCandidateNgl") + .HasColumnType("INTEGER"); + + b.Property("NativeModelSizeBytes") + .HasColumnType("INTEGER"); + + b.Property("NativeQuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("NativeStableNgl") + .HasColumnType("INTEGER"); + + b.Property("ProbeSchemaVersion") + .HasColumnType("INTEGER"); + + b.Property("Q8ModelSizeBytes") + .HasColumnType("INTEGER"); + + b.Property("Q8StableNgl") + .HasColumnType("INTEGER"); + + b.Property("QuantizationKey") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("QuantizedModelFingerprint") + .IsRequired() + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("SlotsJson") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("StaticNgl") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("TensorSplitJson") + .IsRequired() + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("UpdatedUtc") + .HasColumnType("TEXT"); + + b.Property("UsesGpu") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ArchitectureFamilyId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "HardwareFingerprint", "QuantizedModelFingerprint", "QuantizationKey", "DiscoveryTokenTarget") + .IsUnique(); + + b.ToTable("ExecutionPlanProbeCaches"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("BuildFingerprint") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("CanonicalPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("IdentityHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("MetadataJson") + .HasMaxLength(8000) + .HasColumnType("TEXT"); + + b.Property("SourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("TokenCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiModelHashId", "IdentityHash") + .IsUnique(); + + b.ToTable("ImatrixDefinitions"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("BaselineCanonicalKey") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineQuantDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("BaselineQuantId") + .HasColumnType("INTEGER"); + + b.Property("BaselineSourceFileName") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceKind") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("BaselineSourceRepository") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property("FinalQuantType") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupId") + .HasColumnType("INTEGER"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.Property("TensorName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("TensorWeightSchemeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("BaselineQuantDefinitionId"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorGroupId"); + + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "BaselineQuantDefinitionId", "TensorWeightSchemeId", "TensorName") + .IsUnique(); + + b.ToTable("LearnedBaselineTensorQuants"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AiBenchmarkId") + .HasColumnType("TEXT"); + + b.Property("AiModelHashId") + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CompletedUtc") + .HasColumnType("TEXT"); + + b.Property("DurationMs") + .HasColumnType("INTEGER"); + + b.Property("Error") + .HasMaxLength(4000) + .HasColumnType("TEXT"); + + b.Property("ImatrixDefinitionId") + .HasColumnType("INTEGER"); + + b.Property("OutputModelPath") + .HasMaxLength(2048) + .HasColumnType("TEXT"); + + b.Property("StartedUtc") + .HasColumnType("TEXT"); + + b.Property("Succeeded") + .HasColumnType("INTEGER"); + + b.Property("TensorComboId") + .HasColumnType("TEXT"); + + b.Property("TensorGroupProfileId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("AiBenchmarkId"); + + b.HasIndex("AiModelHashId"); + + b.HasIndex("ArchitectureFamilyId"); + + b.HasIndex("ImatrixDefinitionId"); + + b.HasIndex("StartedUtc"); + + b.HasIndex("TensorComboId"); + + b.HasIndex("TensorGroupProfileId"); + + b.ToTable("QuantizationRuns"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorCombo", b => + { + b.Property("Id") + .HasColumnType("TEXT"); + + b.Property("AttnKV") + .HasColumnType("INTEGER"); + + b.Property("AttnOutput") + .HasColumnType("INTEGER"); + + b.Property("AttnQ") + .HasColumnType("INTEGER"); + + b.Property("BaseQuant") + .HasColumnType("INTEGER"); + + b.Property("Embeddings") + .HasColumnType("INTEGER"); + + b.Property("FfnDown") + .HasColumnType("INTEGER"); + + b.Property("FfnUpGate") + .HasColumnType("INTEGER"); + + b.Property("LmHead") + .HasColumnType("INTEGER"); + + b.Property("MoeExperts") + .HasColumnType("INTEGER"); + + b.Property("MoeRouter") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("BaseQuant", "Embeddings", "LmHead", "AttnQ", "AttnKV", "AttnOutput", "FfnUpGate", "FfnDown", "MoeExperts", "MoeRouter") + .IsUnique(); + + b.ToTable("TensorCombos"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorGroupProfile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArchitectureFamilyId") + .HasColumnType("INTEGER"); + + b.Property("CreatedUtc") + .HasColumnType("TEXT"); + + b.Property("FingerprintHash") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("TEXT"); + + b.Property("IsActive") + .HasColumnType("INTEGER"); + + b.Property("SnapshotJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ArchitectureFamilyId", "FingerprintHash") + .IsUnique(); + + b.HasIndex("ArchitectureFamilyId", "IsActive"); + + b.ToTable("TensorGroupProfiles"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmarkLearnedSource", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("LearnedSources") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.BaselineQuantDefinition", "BaselineQuantDefinition") + .WithMany() + .HasForeignKey("BaselineQuantDefinitionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "SourceLearningBenchmark") + .WithMany() + .HasForeignKey("SourceLearningBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("BaselineQuantDefinition"); + + b.Navigation("SourceLearningBenchmark"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRuleGroupState", b => + { + b.HasOne("MQ.DB.Models.DbModels.AnomalyInteractionRule", "Rule") + .WithMany("GroupStates") + .HasForeignKey("RuleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Rule"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeObservation", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "ProbeTensorCombo") + .WithMany() + .HasForeignKey("ProbeTensorComboId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "ReferenceTensorCombo") + .WithMany() + .HasForeignKey("ReferenceTensorComboId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.AnomalyProbeSession", "Session") + .WithMany("Observations") + .HasForeignKey("SessionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("ProbeTensorCombo"); + + b.Navigation("ReferenceTensorCombo"); + + b.Navigation("Session"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ArchitectureFamilyModelHash", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BaselineQuantDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade); + + b.Navigation("ArchitectureFamily"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.BenchmarkRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.CategoryBenchmark", "CategoryBenchmark") + .WithMany() + .HasForeignKey("CategoryBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("CategoryBenchmark"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.CategoryBenchmark", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany("CategorBenchmarks") + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiBenchmark"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ExecutionPlanProbeCache", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.ImatrixDefinition", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AiModelHash"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.LearnedBaselineTensorQuant", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.BaselineQuantDefinition", "BaselineQuantDefinition") + .WithMany() + .HasForeignKey("BaselineQuantDefinitionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("BaselineQuantDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.QuantizationRun", b => + { + b.HasOne("MQ.DB.Models.DbModels.AiBenchmark", "AiBenchmark") + .WithMany() + .HasForeignKey("AiBenchmarkId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("MQ.DB.Models.DbModels.AiModelHash", "AiModelHash") + .WithMany() + .HasForeignKey("AiModelHashId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.ImatrixDefinition", "ImatrixDefinition") + .WithMany() + .HasForeignKey("ImatrixDefinitionId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("MQ.DB.Models.DbModels.TensorCombo", "TensorCombo") + .WithMany() + .HasForeignKey("TensorComboId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("MQ.DB.Models.DbModels.TensorGroupProfile", "TensorGroupProfile") + .WithMany() + .HasForeignKey("TensorGroupProfileId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("AiBenchmark"); + + b.Navigation("AiModelHash"); + + b.Navigation("ArchitectureFamily"); + + b.Navigation("ImatrixDefinition"); + + b.Navigation("TensorCombo"); + + b.Navigation("TensorGroupProfile"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.TensorGroupProfile", b => + { + b.HasOne("MQ.DB.Models.DbModels.ArchitectureFamily", "ArchitectureFamily") + .WithMany() + .HasForeignKey("ArchitectureFamilyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ArchitectureFamily"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AiBenchmark", b => + { + b.Navigation("CategorBenchmarks"); + + b.Navigation("LearnedSources"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyInteractionRule", b => + { + b.Navigation("GroupStates"); + }); + + modelBuilder.Entity("MQ.DB.Models.DbModels.AnomalyProbeSession", b => + { + b.Navigation("Observations"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs b/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs new file mode 100644 index 0000000..e705c99 --- /dev/null +++ b/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs @@ -0,0 +1,169 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MQ.DB.Migrations +{ + /// + public partial class PredictionEngineAnomalyDetect2 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "IX_AnomalyInteractionRules_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_ReferenceQuantId_GroupSetHash_RuleDirection", + table: "AnomalyInteractionRules"); + + migrationBuilder.AddColumn( + name: "ProbeDisplayName", + table: "AnomalyProbeObservations", + type: "TEXT", + maxLength: 512, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "ProbeInternalName", + table: "AnomalyProbeObservations", + type: "TEXT", + maxLength: 512, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "ProbePlanClass", + table: "AnomalyProbeObservations", + type: "TEXT", + maxLength: 64, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "ReferenceDisplayName", + table: "AnomalyProbeObservations", + type: "TEXT", + maxLength: 512, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "ReferenceInternalName", + table: "AnomalyProbeObservations", + type: "TEXT", + maxLength: 512, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "SeedClass", + table: "AnomalyProbeObservations", + type: "TEXT", + maxLength: 64, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "SeedPriority", + table: "AnomalyProbeObservations", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "CandidateDisplayName", + table: "AnomalyInteractionRules", + type: "TEXT", + maxLength: 512, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "CandidateInternalName", + table: "AnomalyInteractionRules", + type: "TEXT", + maxLength: 512, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "ReferenceDisplayName", + table: "AnomalyInteractionRules", + type: "TEXT", + maxLength: 512, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "ReferenceInternalName", + table: "AnomalyInteractionRules", + type: "TEXT", + maxLength: 512, + nullable: false, + defaultValue: ""); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRules_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_ReferenceQuantId_ReferenceContextKey_GroupSetHash_RuleDirection", + table: "AnomalyInteractionRules", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceQuantId", "ReferenceContextKey", "GroupSetHash", "RuleDirection" }, + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "IX_AnomalyInteractionRules_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_ReferenceQuantId_ReferenceContextKey_GroupSetHash_RuleDirection", + table: "AnomalyInteractionRules"); + + migrationBuilder.DropColumn( + name: "ProbeDisplayName", + table: "AnomalyProbeObservations"); + + migrationBuilder.DropColumn( + name: "ProbeInternalName", + table: "AnomalyProbeObservations"); + + migrationBuilder.DropColumn( + name: "ProbePlanClass", + table: "AnomalyProbeObservations"); + + migrationBuilder.DropColumn( + name: "ReferenceDisplayName", + table: "AnomalyProbeObservations"); + + migrationBuilder.DropColumn( + name: "ReferenceInternalName", + table: "AnomalyProbeObservations"); + + migrationBuilder.DropColumn( + name: "SeedClass", + table: "AnomalyProbeObservations"); + + migrationBuilder.DropColumn( + name: "SeedPriority", + table: "AnomalyProbeObservations"); + + migrationBuilder.DropColumn( + name: "CandidateDisplayName", + table: "AnomalyInteractionRules"); + + migrationBuilder.DropColumn( + name: "CandidateInternalName", + table: "AnomalyInteractionRules"); + + migrationBuilder.DropColumn( + name: "ReferenceDisplayName", + table: "AnomalyInteractionRules"); + + migrationBuilder.DropColumn( + name: "ReferenceInternalName", + table: "AnomalyInteractionRules"); + + migrationBuilder.CreateIndex( + name: "IX_AnomalyInteractionRules_ArchitectureFamilyId_TensorGroupProfileId_AiModelHashId_ImatrixDefinitionId_BenchmarkCategory_ReferenceQuantId_GroupSetHash_RuleDirection", + table: "AnomalyInteractionRules", + columns: new[] { "ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceQuantId", "GroupSetHash", "RuleDirection" }, + unique: true); + } + } +} diff --git a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs index e1020a8..421ccef 100644 --- a/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs +++ b/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs @@ -156,10 +156,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("BestPredictionSpaceGap") .HasColumnType("REAL"); + b.Property("CandidateDisplayName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + b.Property("CandidateEffectiveGroupsJson") .IsRequired() .HasColumnType("TEXT"); + b.Property("CandidateInternalName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + b.Property("Confidence") .HasColumnType("REAL"); @@ -209,10 +219,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(512) .HasColumnType("TEXT"); + b.Property("ReferenceDisplayName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + b.Property("ReferenceEffectiveGroupsJson") .IsRequired() .HasColumnType("TEXT"); + b.Property("ReferenceInternalName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + b.Property("ReferenceQuantId") .HasColumnType("INTEGER"); @@ -257,7 +277,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "RuleDirection", "RuleStatus"); - b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceQuantId", "GroupSetHash", "RuleDirection") + b.HasIndex("ArchitectureFamilyId", "TensorGroupProfileId", "AiModelHashId", "ImatrixDefinitionId", "BenchmarkCategory", "ReferenceQuantId", "ReferenceContextKey", "GroupSetHash", "RuleDirection") .IsUnique(); b.ToTable("AnomalyInteractionRules"); @@ -392,6 +412,21 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("PredictionSpaceGapVsTwin") .HasColumnType("REAL"); + b.Property("ProbeDisplayName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ProbeInternalName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property("ProbePlanClass") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + b.Property("ProbeTensorComboId") .HasColumnType("TEXT"); @@ -408,10 +443,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("ReferenceActualKld") .HasColumnType("REAL"); + b.Property("ReferenceDisplayName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + b.Property("ReferenceEffectiveGroupsJson") .IsRequired() .HasColumnType("TEXT"); + b.Property("ReferenceInternalName") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + b.Property("ReferencePredictedKld") .HasColumnType("REAL"); @@ -434,6 +479,14 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("SameCount") .HasColumnType("INTEGER"); + b.Property("SeedClass") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property("SeedPriority") + .HasColumnType("INTEGER"); + b.Property("SessionId") .HasColumnType("TEXT"); diff --git a/MQ.DB/Models/DbModels/AnomalyProbeSession.cs b/MQ.DB/Models/DbModels/AnomalyProbeSession.cs index 6b7ce46..ed84601 100644 --- a/MQ.DB/Models/DbModels/AnomalyProbeSession.cs +++ b/MQ.DB/Models/DbModels/AnomalyProbeSession.cs @@ -67,6 +67,13 @@ public class AnomalyProbeObservation : ISQLiteEntity public string InactiveGroupsJson { get; set; } = string.Empty; public string ReferenceTensorConfigKey { get; set; } = string.Empty; public string ProbeTensorConfigKey { get; set; } = string.Empty; + public string ReferenceDisplayName { get; set; } = string.Empty; + public string ProbeDisplayName { get; set; } = string.Empty; + public string ReferenceInternalName { get; set; } = string.Empty; + public string ProbeInternalName { get; set; } = string.Empty; + public string SeedClass { get; set; } = string.Empty; + public int SeedPriority { get; set; } + public string ProbePlanClass { get; set; } = string.Empty; public bool IsContextualAnomalyProbe { get; set; } public bool OldBf16Isolation { get; set; } public bool AllActiveGroupsExplicit { get; set; } @@ -105,6 +112,12 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.InactiveGroupsJson).HasColumnType("TEXT"); builder.Property(x => x.ReferenceTensorConfigKey).HasMaxLength(128); builder.Property(x => x.ProbeTensorConfigKey).HasMaxLength(128); + builder.Property(x => x.ReferenceDisplayName).HasMaxLength(512); + builder.Property(x => x.ProbeDisplayName).HasMaxLength(512); + builder.Property(x => x.ReferenceInternalName).HasMaxLength(512); + builder.Property(x => x.ProbeInternalName).HasMaxLength(512); + builder.Property(x => x.SeedClass).HasMaxLength(64); + builder.Property(x => x.ProbePlanClass).HasMaxLength(64); builder.HasIndex(x => x.ReferenceTensorConfigKey); builder.HasIndex(x => x.ProbeTensorConfigKey); builder.Property(x => x.RuleDirection).HasMaxLength(64); @@ -141,6 +154,10 @@ public class AnomalyInteractionRule : ISQLiteEntity public string CandidateEffectiveGroupsJson { get; set; } = string.Empty; public string InactiveGroupsJson { get; set; } = string.Empty; public string FullTensorConfigKey { get; set; } = string.Empty; + public string ReferenceDisplayName { get; set; } = string.Empty; + public string CandidateDisplayName { get; set; } = string.Empty; + public string ReferenceInternalName { get; set; } = string.Empty; + public string CandidateInternalName { get; set; } = string.Empty; public string RuleType { get; set; } = string.Empty; public string RuleDirection { get; set; } = string.Empty; public string RuleStatus { get; set; } = string.Empty; @@ -170,6 +187,10 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.CandidateEffectiveGroupsJson).HasColumnType("TEXT"); builder.Property(x => x.InactiveGroupsJson).HasColumnType("TEXT"); builder.Property(x => x.FullTensorConfigKey).HasMaxLength(128); + builder.Property(x => x.ReferenceDisplayName).HasMaxLength(512); + builder.Property(x => x.CandidateDisplayName).HasMaxLength(512); + builder.Property(x => x.ReferenceInternalName).HasMaxLength(512); + builder.Property(x => x.CandidateInternalName).HasMaxLength(512); builder.HasIndex(x => x.FullTensorConfigKey); builder.Property(x => x.RuleType).HasMaxLength(64); builder.Property(x => x.RuleDirection).HasMaxLength(64); @@ -179,7 +200,7 @@ public void Configure(EntityTypeBuilder builder) builder.Property(x => x.Status).HasMaxLength(64); builder.Property(x => x.MetadataJson).HasColumnType("TEXT"); builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.BenchmarkCategory, x.RuleDirection, x.RuleStatus }); - builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.BenchmarkCategory, x.ReferenceQuantId, x.GroupSetHash, x.RuleDirection }).IsUnique(); + builder.HasIndex(x => new { x.ArchitectureFamilyId, x.TensorGroupProfileId, x.AiModelHashId, x.ImatrixDefinitionId, x.BenchmarkCategory, x.ReferenceQuantId, x.ReferenceContextKey, x.GroupSetHash, x.RuleDirection }).IsUnique(); builder.HasOne(x => x.ArchitectureFamily).WithMany().HasForeignKey(x => x.ArchitectureFamilyId).OnDelete(DeleteBehavior.Cascade); builder.HasOne(x => x.TensorGroupProfile).WithMany().HasForeignKey(x => x.TensorGroupProfileId).OnDelete(DeleteBehavior.Restrict); builder.HasOne(x => x.AiModelHash).WithMany().HasForeignKey(x => x.AiModelHashId).OnDelete(DeleteBehavior.Cascade); @@ -207,4 +228,4 @@ public void Configure(EntityTypeBuilder builde builder.HasIndex(x => new { x.RuleId, x.TensorGroupId }).IsUnique(); builder.HasOne(x => x.Rule).WithMany(x => x.GroupStates).HasForeignKey(x => x.RuleId).OnDelete(DeleteBehavior.Cascade); } -} +} \ No newline at end of file diff --git a/MagicQuant/Models/AnomalyDetectionModels.cs b/MagicQuant/Models/AnomalyDetectionModels.cs index 83f221e..8379ff4 100644 --- a/MagicQuant/Models/AnomalyDetectionModels.cs +++ b/MagicQuant/Models/AnomalyDetectionModels.cs @@ -36,6 +36,15 @@ public enum AnomalyRuleStatus Retired = 4 } +public enum AnomalySeedClass +{ + ConfirmedHistoricalCounterfactual = 1, + HistoricalMissingTwin = 2, + PredictionSpaceSmoke = 3, + ExploratorySingle = 4, + ExploratoryPair = 5 +} + public enum AnomalyProbeClassification { BeneficialAnomaly = 1, @@ -77,6 +86,8 @@ public sealed class AnomalyMovementAnalysis public sealed class AnomalySmokeCandidate { public string Source { get; init; } = string.Empty; + public AnomalySeedClass SeedClass { get; init; } = AnomalySeedClass.PredictionSpaceSmoke; + public int Priority { get; init; } public TensorConfig CandidateConfig { get; init; } public TensorConfig TwinConfig { get; init; } public HybridQuant CandidateQuant => (HybridQuant)CandidateConfig; @@ -84,9 +95,13 @@ public sealed class AnomalySmokeCandidate public AnomalyMovementAnalysis Movement { get; init; } = new(); public double CandidatePredictedKld { get; init; } public double TwinPredictedKld { get; init; } - public ulong CandidatePredictedSizeBytes { get; init; } - public ulong TwinPredictedSizeBytes { get; init; } - public ulong SizeSavingsBytes { get; init; } + public ulong? CandidatePredictedSizeBytes { get; init; } + public ulong? TwinPredictedSizeBytes { get; init; } + public ulong? PredictedSizeSavingsBytes { get; init; } + public ulong? ActualSizeSavingsBytes { get; init; } + public bool PlannedProbeWillMeasureSize { get; init; } + public string TwinLookupMode { get; init; } = string.Empty; + public string TwinLookupDetail { get; init; } = string.Empty; public double PredictionSpaceGapVsTwin { get; init; } public ulong? CandidatePredictionRank { get; init; } public ulong? TwinPredictionRank { get; init; } @@ -104,6 +119,8 @@ public sealed class AnomalySmokeCandidate public sealed class AnomalyProbePlan { public AnomalySmokeCandidate Seed { get; init; } = default!; + public AnomalySeedClass ProbePlanClass { get; init; } + public int Priority { get; init; } public TensorConfig ReferenceConfig { get; init; } public TensorConfig ProbeConfig { get; init; } public IReadOnlyList ProbeGroups { get; init; } = Array.Empty(); @@ -138,4 +155,4 @@ public sealed class AnomalyRunResult public IReadOnlyList ProbePlans { get; init; } = Array.Empty(); public IReadOnlyList ProbeResults { get; init; } = Array.Empty(); public AnomalyAdjustmentSummary AdjustmentSummary { get; init; } = new(); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/AnomalyRuleRepository.cs b/MagicQuant/Services/AnomalyRuleRepository.cs index 74e63c2..4f0fc81 100644 --- a/MagicQuant/Services/AnomalyRuleRepository.cs +++ b/MagicQuant/Services/AnomalyRuleRepository.cs @@ -105,6 +105,13 @@ public async Task> PersistProbeResultsAsy InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), ReferenceTensorConfigKey = TensorConfigIdentity.ToKey(result.Plan.ReferenceConfig), ProbeTensorConfigKey = TensorConfigIdentity.ToKey(result.Plan.ProbeConfig), + ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)result.Plan.ReferenceConfig), + ProbeDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)result.Plan.ProbeConfig), + ReferenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)result.Plan.ReferenceConfig), + ProbeInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)result.Plan.ProbeConfig), + SeedClass = result.Plan.Seed.SeedClass.ToString(), + SeedPriority = result.Plan.Seed.Priority, + ProbePlanClass = result.Plan.ProbePlanClass.ToString(), IsContextualAnomalyProbe = true, OldBf16Isolation = false, AllActiveGroupsExplicit = _movement.HasAllActiveGroupsExplicit(result.Plan.ReferenceConfig) && _movement.HasAllActiveGroupsExplicit(result.Plan.ProbeConfig), @@ -163,6 +170,7 @@ public async Task> UpsertRulesFromResultsA string groupSetHash = _movement.BuildChangedGroupHash(probeGroups); string direction = first.RuleDirection.ToString(); byte referenceQuantId = first.Plan.ReferenceConfig.BaseQuant; + string referenceContextKey = _movement.ReferenceContextKey(first.Plan.ReferenceConfig); var rule = await db.AnomalyInteractionRules .Include(x => x.GroupStates) @@ -173,6 +181,7 @@ public async Task> UpsertRulesFromResultsA x.ImatrixDefinitionId == scope.ImatrixDefinitionId && x.BenchmarkCategory == (byte)BenchmarkCategory.General && x.ReferenceQuantId == referenceQuantId && + x.ReferenceContextKey == referenceContextKey && x.GroupSetHash == groupSetHash && x.RuleDirection == direction, ct); @@ -193,6 +202,10 @@ public async Task> UpsertRulesFromResultsA CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions), InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), + ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), + CandidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), + ReferenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), + CandidateInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), RuleDirection = direction, GroupSetHash = groupSetHash, CreatedUtc = DateTime.UtcNow @@ -221,6 +234,10 @@ public async Task> UpsertRulesFromResultsA rule.CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions); rule.InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions); rule.FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig); + rule.ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig); + rule.CandidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig); + rule.ReferenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig); + rule.CandidateInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig); rule.UpdatedUtc = DateTime.UtcNow; rule.MetadataJson = JsonSerializer.Serialize(new { @@ -233,6 +250,15 @@ public async Task> UpsertRulesFromResultsA inactiveGroups = _movement.BuildInactiveGroupList(), first.Plan.ProbeType, first.Plan.HypothesisLabel, + seedClass = first.Plan.Seed.SeedClass.ToString(), + probePlanClass = first.Plan.ProbePlanClass.ToString(), + priority = first.Plan.Priority, + referenceTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ReferenceConfig), + probeTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), + referenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), + probeDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), + referenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), + probeInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), groups = probeGroups.Select(ToGroupLog).ToList() }, JsonOptions); @@ -288,6 +314,7 @@ public async Task HasSuppressionOrRuleAsync( await using var db = new MagicQuantContext(); var scope = await ResolveScopeAsync(db, ct); string hash = _movement.BuildChangedGroupHash(groups); + string referenceContextKey = _movement.ReferenceContextKey(reference); return await db.AnomalyInteractionRules .AsNoTracking() @@ -298,6 +325,7 @@ public async Task HasSuppressionOrRuleAsync( x.ImatrixDefinitionId == scope.ImatrixDefinitionId && x.BenchmarkCategory == (byte)BenchmarkCategory.General && x.ReferenceQuantId == reference.BaseQuant && + x.ReferenceContextKey == referenceContextKey && x.GroupSetHash == hash && x.RuleStatus != AnomalyRuleStatus.Retired.ToString(), ct); @@ -415,4 +443,4 @@ private static double ComputePredictionAdjustment(AnomalyProbeResult result, dou } private readonly record struct AnomalyScope(int ArchitectureFamilyId, int TensorGroupProfileId, uint AiModelHashId, int? ImatrixDefinitionId); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs index 169ec1c..e76652f 100644 --- a/MagicQuant/Services/AnomalyWorkflowService.cs +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -28,6 +28,8 @@ public sealed class AnomalyWorkflowService private readonly QuantFidelityComparerService _movement; private readonly AnomalyRuleRepository _rules; private readonly AnomalyAdjustedPredictionService _adjuster; + private IReadOnlyList _lastDuckRejectedSmokePreview = Array.Empty(); + private IReadOnlyList _lastDuckFailedGapPreview = Array.Empty(); public AnomalyWorkflowService( RemainingCombinationStore store, @@ -75,6 +77,8 @@ public async Task RunAsync( historicalCount = historical.Count, duckPredictionSpaceCount = duck.Count, selectedSmokeCount = smoke.Count, + duckRejectedSmokePreview = _lastDuckRejectedSmokePreview, + duckClosestFailedGapPreview = _lastDuckFailedGapPreview, smoke = smoke.Select(ToSmokeLog).ToList() }, ct); @@ -186,7 +190,8 @@ private async Task> DetectHistoricalSmokeAsync(Cance if (movement.DowngradeCount > Config.AnomalyDetection.MaxProbeGroupCount) continue; - byKey.TryGetValue(TensorConfigIdentity.ToKey(twinConfig), out var twin); + var twinLookup = LookupHistoricalContextualTwin(twinConfig, byKey); + var twin = twinLookup.ExplicitTwin; if (twin != null && ShouldSkipInvalidContextualAnomalyConfig(twin.Config, "history-existing-twin", out var existingTwinSkipReason)) { skippedNonContextualTwin++; @@ -194,8 +199,14 @@ private async Task> DetectHistoricalSmokeAsync(Cance continue; } + if (Config.AnomalyDetection.VerboseAnomalyLogging) + { + AnsiConsole.MarkupLine( + $"[grey]Historical twin lookup:[/] candidate={Markup.Escape(candidate.DisplayName)} explicitContext={twinLookup.ExplicitFound} sparsePure={twinLookup.SparseFound} mode={Markup.Escape(twinLookup.Mode)} detail={Markup.Escape(twinLookup.Detail)}"); + } + predictionLookup.TryGetValue(TensorConfigIdentity.ToKey(candidate.Config), out var candidatePrediction); - predictionLookup.TryGetValue(TensorConfigIdentity.ToKey(twinConfig), out var twinPrediction); + var twinPrediction = LookupPredictionForContextualTwin(twinConfig, predictionLookup, out var predictionTwinLookupMode); bool confirmed = twin != null && candidate.SizeBytes <= twin.SizeBytes && @@ -204,17 +215,26 @@ private async Task> DetectHistoricalSmokeAsync(Cance if (!confirmed && twin != null && twin.Kld <= candidate.Kld) continue; + ulong? predictedSavingsBytes = ComputeNullableSavings(twinPrediction?.PredictedSizeBytes, candidatePrediction?.PredictedSizeBytes); + ulong? actualSavingsBytes = twin == null ? null : ComputeNullableSavings(twin.SizeBytes, candidate.SizeBytes) ?? 0UL; + smoke.Add(new AnomalySmokeCandidate { Source = "history", + SeedClass = confirmed ? AnomalySeedClass.ConfirmedHistoricalCounterfactual : AnomalySeedClass.HistoricalMissingTwin, + Priority = confirmed ? 1000 : 700, CandidateConfig = candidate.Config, TwinConfig = twinConfig, Movement = movement, CandidatePredictedKld = candidatePrediction?.BaseRankSafeKld ?? candidatePrediction?.FinalPredictedKld ?? candidate.Kld, TwinPredictedKld = twinPrediction?.BaseRankSafeKld ?? twinPrediction?.FinalPredictedKld ?? twin?.Kld ?? 0d, - CandidatePredictedSizeBytes = candidatePrediction?.PredictedSizeBytes ?? candidate.SizeBytes, - TwinPredictedSizeBytes = twinPrediction?.PredictedSizeBytes ?? twin?.SizeBytes ?? candidate.SizeBytes, - SizeSavingsBytes = twin != null && twin.SizeBytes > candidate.SizeBytes ? twin.SizeBytes - candidate.SizeBytes : 0UL, + CandidatePredictedSizeBytes = candidatePrediction?.PredictedSizeBytes, + TwinPredictedSizeBytes = twinPrediction?.PredictedSizeBytes, + PredictedSizeSavingsBytes = predictedSavingsBytes, + ActualSizeSavingsBytes = actualSavingsBytes, + PlannedProbeWillMeasureSize = twin == null, + TwinLookupMode = twinLookup.Mode, + TwinLookupDetail = $"actual={twinLookup.Detail}; prediction={predictionTwinLookupMode}", PredictionSpaceGapVsTwin = (candidatePrediction?.BaseRankSafeKld ?? candidate.Kld) - (twinPrediction?.BaseRankSafeKld ?? twin?.Kld ?? candidate.Kld), CandidatePredictionRank = candidatePrediction?.PredictionRank, TwinPredictionRank = twinPrediction?.PredictionRank, @@ -228,7 +248,7 @@ private async Task> DetectHistoricalSmokeAsync(Cance IsConfirmedFromHistory = confirmed, Message = confirmed ? "Existing explicit contextual quantized benchmark history contains a monotone downgrade candidate that beats its higher-bit twin." - : "Existing explicit contextual quantized benchmark history has monotone downgrade smoke but the exact twin is missing." + : "Existing explicit contextual quantized benchmark history has monotone downgrade smoke but the exact explicit contextual twin is missing." }); } @@ -247,6 +267,8 @@ private async Task> DetectDuckSmokeAsync(Cancellatio var rows = await LoadPredictionRowsAsync(DuckSmokeScanLimit, ct); var explicitRows = new Dictionary(StringComparer.Ordinal); var result = new List(); + var rejectedPreview = new List(); + var failedGapPreview = new List(); int skippedIsolation = 0; int skippedSparse = 0; int normalizedSparse = 0; @@ -255,6 +277,8 @@ private async Task> DetectDuckSmokeAsync(Cancellatio int skippedMovement = 0; int skippedNoTwin = 0; int logicalTwinFallback = 0; + int explicitTwinFound = 0; + int sparseTwinPredictionUsed = 0; int skippedSavings = 0; int skippedGap = 0; int contextualScanned = 0; @@ -296,6 +320,7 @@ private async Task> DetectDuckSmokeAsync(Cancellatio { skippedIsolation++; LogSkippedInvalidContextualAnomalyConfig("duckdb-normalized-candidate", row.Config, candidateSkipReason, skippedIsolation); + AddDuckRejected(rejectedPreview, row.Config, twin, "Unknown", null, null, candidateSkipReason); continue; } @@ -303,6 +328,7 @@ private async Task> DetectDuckSmokeAsync(Cancellatio { skippedIsolation++; LogSkippedInvalidContextualAnomalyConfig("duckdb-twin", twin, twinSkipReason, skippedIsolation); + AddDuckRejected(rejectedPreview, row.Config, twin, "Unknown", null, null, twinSkipReason); continue; } @@ -314,68 +340,95 @@ private async Task> DetectDuckSmokeAsync(Cancellatio { AnsiConsole.MarkupLine("[grey]Ignored anomaly smoke:[/] classification=MixedTrade reason=normal protect/compress frontier behavior"); } + + AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), null, null, "MixedTrade normal protect/compress frontier behavior"); continue; } if (movement.Classification != AnomalyMovementClassification.MonotoneDowngrade) { skippedMovement++; + AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), null, null, "MovementNotMonotoneDowngrade"); continue; } if (movement.DowngradeCount <= 0 || movement.DowngradeCount > Config.AnomalyDetection.MaxProbeGroupCount) { skippedMovement++; + AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), null, null, $"ChangedGroupCountOutsideBudget count={movement.DowngradeCount}"); continue; } - var twinRow = await LoadContextualTwinPredictionRowAsync(twin, explicitRows, ct); - if (twinRow == null) + var twinLookup = await LoadContextualTwinPredictionRowAsync(twin, explicitRows, ct); + if (twinLookup.Row == null) { skippedNoTwin++; + AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), null, null, $"NoHigherBitTwinPredictionFound lookup={twinLookup.Mode}"); continue; } - if (TensorConfigIdentity.ToKey(twinRow.Config) != TensorConfigIdentity.ToKey(twin)) + if (twinLookup.Mode.Contains("explicit", StringComparison.OrdinalIgnoreCase)) + explicitTwinFound++; + if (twinLookup.Mode.Contains("sparse", StringComparison.OrdinalIgnoreCase)) + sparseTwinPredictionUsed++; + + if (TensorConfigIdentity.ToKey(twinLookup.Row.Config) != TensorConfigIdentity.ToKey(twin)) logicalTwinFallback++; - if (twinRow.PredictedSizeBytes <= row.PredictedSizeBytes) + if (Config.AnomalyDetection.VerboseAnomalyLogging && (explicitTwinFound + sparseTwinPredictionUsed) <= 12) + { + AnsiConsole.MarkupLine( + $"[grey]DuckDB twin lookup:[/] candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)row.Config))} explicitContext={twinLookup.ExplicitContextSearched} sparsePure={twinLookup.SparsePureSearched} mode={Markup.Escape(twinLookup.Mode)} detail={Markup.Escape(twinLookup.Detail)}"); + } + + if (twinLookup.Row.PredictedSizeBytes <= row.PredictedSizeBytes) { skippedSavings++; + AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), 0UL, row.BaseRankSafeKld - twinLookup.Row.BaseRankSafeKld, "PredictedSizeSavingsNotPositive"); continue; } - ulong savingsBytes = twinRow.PredictedSizeBytes - row.PredictedSizeBytes; - double savingsPercent = savingsBytes * 100d / Math.Max(1d, twinRow.PredictedSizeBytes); + ulong savingsBytes = twinLookup.Row.PredictedSizeBytes - row.PredictedSizeBytes; + double savingsPercent = savingsBytes * 100d / Math.Max(1d, twinLookup.Row.PredictedSizeBytes); if (savingsPercent < Config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent) { skippedSavings++; + AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), savingsBytes, row.BaseRankSafeKld - twinLookup.Row.BaseRankSafeKld, $"PredictedSizeSavingsBelowThreshold {savingsPercent:0.000}%"); continue; } - double gap = row.BaseRankSafeKld - twinRow.BaseRankSafeKld; + double gap = row.BaseRankSafeKld - twinLookup.Row.BaseRankSafeKld; if (gap > Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld) { skippedGap++; + var preview = CreateDuckRejected(row.Config, twin, movement.Classification.ToString(), savingsBytes, gap, $"PredictionSpaceGapTooLarge threshold={Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld:0.000000}"); + rejectedPreview.Add(preview); + failedGapPreview.Add(preview); continue; } - double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank); + double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinLookup.Row.PredictionRank); result.Add(new AnomalySmokeCandidate { Source = "duckdb-prediction-space", + SeedClass = AnomalySeedClass.PredictionSpaceSmoke, + Priority = 500, CandidateConfig = row.Config, TwinConfig = twin, Movement = movement, CandidatePredictedKld = row.BaseRankSafeKld, - TwinPredictedKld = twinRow.BaseRankSafeKld, + TwinPredictedKld = twinLookup.Row.BaseRankSafeKld, CandidatePredictedSizeBytes = row.PredictedSizeBytes, - TwinPredictedSizeBytes = twinRow.PredictedSizeBytes, - SizeSavingsBytes = savingsBytes, + TwinPredictedSizeBytes = twinLookup.Row.PredictedSizeBytes, + PredictedSizeSavingsBytes = savingsBytes, + ActualSizeSavingsBytes = null, + PlannedProbeWillMeasureSize = true, + TwinLookupMode = twinLookup.Mode, + TwinLookupDetail = twinLookup.Detail, PredictionSpaceGapVsTwin = gap, CandidatePredictionRank = row.PredictionRank, - TwinPredictionRank = twinRow.PredictionRank, + TwinPredictionRank = twinLookup.Row.PredictionRank, SmokeScore = score, SmokeStrength = gap <= 0d ? "Strong" : "Close", Message = "Prediction-space contextual monotone downgrade candidate is close enough to its higher-bit quantized twin to justify probes. Sparse DuckDB source rows, when present, were normalized into explicit active context before classification." @@ -389,6 +442,8 @@ private async Task> DetectDuckSmokeAsync(Cancellatio AnsiConsole.MarkupLine($"[grey] BF16/exact rows skipped=[/] [cyan]{skippedIsolation:N0}[/]"); AnsiConsole.MarkupLine($"[grey] pure/logical reference rows skipped=[/] [cyan]{skippedPure:N0}[/]"); AnsiConsole.MarkupLine($"[grey] contextual quantized rows scanned=[/] [cyan]{contextualScanned:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] explicit higher-bit twin predictions found=[/] [cyan]{explicitTwinFound:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] sparse pure carrier twin predictions used=[/] [cyan]{sparseTwinPredictionUsed:N0}[/]"); AnsiConsole.MarkupLine($"[grey] logical higher-bit twin fallback used=[/] [cyan]{logicalTwinFallback:N0}[/]"); AnsiConsole.MarkupLine($"[grey] no higher-bit twin found=[/] [cyan]{skippedNoTwin:N0}[/]"); AnsiConsole.MarkupLine($"[grey] movement not monotone downgrade=[/] [cyan]{skippedMovement:N0}[/]"); @@ -397,10 +452,21 @@ private async Task> DetectDuckSmokeAsync(Cancellatio AnsiConsole.MarkupLine($"[grey] prediction-space gap too large=[/] [cyan]{skippedGap:N0}[/]"); AnsiConsole.MarkupLine($"[grey] queued smoke candidates=[/] [cyan]{result.Count:N0}[/]"); + _lastDuckRejectedSmokePreview = rejectedPreview + .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) + .ThenByDescending(x => x.PredictedSizeSavingsBytes ?? 0UL) + .Take(10) + .ToList(); + _lastDuckFailedGapPreview = failedGapPreview + .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) + .Take(10) + .ToList(); + if (result.Count == 0 && rows.Count > 0) { AnsiConsole.MarkupLine( - "[yellow]DuckDB contextual smoke scan produced zero candidates.[/] This is valid only if all predicted rows were filtered by explicit-context validity, monotone-downgrade movement, size-savings, or prediction-gap thresholds above."); + "[yellow]DuckDB contextual smoke scan produced zero candidates.[/] Rejected-smoke preview follows so threshold/clean-space decisions are visible."); + WriteRejectedSmokePreview(_lastDuckRejectedSmokePreview, _lastDuckFailedGapPreview); } return result @@ -465,9 +531,12 @@ private async Task> PlanProbesAsync(IReadOnlyList> PlanProbesAsync(IReadOnlyList LoadContextualTwinPredictionRowAsync( + private async Task LoadContextualTwinPredictionRowAsync( TensorConfig explicitTwin, IReadOnlyDictionary explicitRows, CancellationToken ct) { string explicitKey = TensorConfigIdentity.ToKey(explicitTwin); if (explicitRows.TryGetValue(explicitKey, out var inMemoryExplicit)) - return inMemoryExplicit; + { + return new TwinPredictionLookupResult( + inMemoryExplicit with { Config = explicitTwin }, + "explicit-context-memory", + "Searched explicit all-active context in current DuckDB scan; found in memory.", + ExplicitContextSearched: true, + SparsePureSearched: false); + } var explicitRow = await LoadSinglePredictionRowAsync(explicitTwin, ct); if (explicitRow != null) - return explicitRow with { Config = explicitTwin }; + { + return new TwinPredictionLookupResult( + explicitRow with { Config = explicitTwin }, + "explicit-context-duckdb", + "Searched explicit all-active context in DuckDB; found exact contextual twin.", + ExplicitContextSearched: true, + SparsePureSearched: false); + } // The normal generator may only contain the pure sparse carrier for an all-Q8/all-Q6 // reference. For smoke scoring, that sparse carrier is allowed as a prediction source // only; the anomaly seed/probe/twin identity remains the explicit activated blanket. - var sparsePure = new TensorConfig( - explicitTwin.BaseQuant, + var sparsePure = BuildSparsePureCarrier(explicitTwin.BaseQuant); + + var sparseRow = await LoadSinglePredictionRowAsync(sparsePure, ct); + if (sparseRow != null) + { + return new TwinPredictionLookupResult( + sparseRow with { Config = sparsePure }, + "sparse-pure-carrier-prediction-fallback", + "Searched explicit all-active context first; missing. Searched sparse pure carrier for prediction-space scoring only; found fallback. Contextual anomaly identity remains explicit.", + ExplicitContextSearched: true, + SparsePureSearched: true); + } + + return new TwinPredictionLookupResult( + null, + "missing-explicit-and-sparse", + "Searched explicit all-active context and sparse pure carrier; no prediction row found.", + ExplicitContextSearched: true, + SparsePureSearched: true); + } + + private HistoricalTwinLookup LookupHistoricalContextualTwin( + TensorConfig explicitTwin, + IReadOnlyDictionary snapshotsByKey) + { + string explicitKey = TensorConfigIdentity.ToKey(explicitTwin); + var sparsePure = BuildSparsePureCarrier(explicitTwin.BaseQuant); + string sparseKey = TensorConfigIdentity.ToKey(sparsePure); + + snapshotsByKey.TryGetValue(explicitKey, out var explicitSnapshot); + bool sparseFound = snapshotsByKey.ContainsKey(sparseKey); + + if (explicitSnapshot != null) + { + return new HistoricalTwinLookup( + explicitSnapshot, + ExplicitFound: true, + SparseFound: sparseFound, + Mode: sparseFound ? "explicit-context-preferred;sparse-pure-also-present" : "explicit-context-found", + Detail: sparseFound + ? "Searched explicit all-active context and sparse pure carrier. Using explicit contextual twin for anomaly truth." + : "Searched explicit all-active context. Using explicit contextual twin for anomaly truth."); + } + + if (sparseFound) + { + return new HistoricalTwinLookup( + null, + ExplicitFound: false, + SparseFound: true, + Mode: "sparse-pure-found-ignored-for-contextual-truth", + Detail: "Searched explicit all-active context first; missing. Sparse pure carrier exists but is not trusted as contextual anomaly truth."); + } + + return new HistoricalTwinLookup( + null, + ExplicitFound: false, + SparseFound: false, + Mode: "missing-explicit-and-sparse", + Detail: "Searched explicit all-active context and sparse pure carrier; no historical twin benchmark found."); + } + + private static PredictionDuckRow? LookupPredictionForContextualTwin( + TensorConfig explicitTwin, + IReadOnlyDictionary predictionLookup, + out string lookupMode) + { + string explicitKey = TensorConfigIdentity.ToKey(explicitTwin); + if (predictionLookup.TryGetValue(explicitKey, out var explicitPrediction)) + { + lookupMode = "explicit-context-prediction-found"; + return explicitPrediction with { Config = explicitTwin }; + } + + var sparsePure = BuildSparsePureCarrier(explicitTwin.BaseQuant); + string sparseKey = TensorConfigIdentity.ToKey(sparsePure); + if (predictionLookup.TryGetValue(sparseKey, out var sparsePrediction)) + { + lookupMode = "sparse-pure-prediction-fallback"; + return sparsePrediction with { Config = sparsePure }; + } + + lookupMode = "prediction-missing-explicit-and-sparse"; + return null; + } + + private static TensorConfig BuildSparsePureCarrier(byte baseQuant) + { + return new TensorConfig( + baseQuant, BaselineQuants.TensorConfigNullSlotValue, BaselineQuants.TensorConfigNullSlotValue, BaselineQuants.TensorConfigNullSlotValue, @@ -790,11 +961,101 @@ AND PredictionRank IS NOT NULL BaselineQuants.TensorConfigNullSlotValue, BaselineQuants.TensorConfigNullSlotValue, BaselineQuants.TensorConfigNullSlotValue); + } - var sparseRow = await LoadSinglePredictionRowAsync(sparsePure, ct); - return sparseRow == null ? null : sparseRow with { Config = sparsePure }; + private static ulong? ComputeNullableSavings(ulong? referenceBytes, ulong? candidateBytes) + { + if (!referenceBytes.HasValue || !candidateBytes.HasValue) + return null; + + return referenceBytes.Value > candidateBytes.Value + ? referenceBytes.Value - candidateBytes.Value + : 0UL; + } + + private static void AddDuckRejected( + List rejected, + TensorConfig candidate, + TensorConfig twin, + string movement, + ulong? predictedSizeSavingsBytes, + double? predictionSpaceGap, + string rejectionReason) + { + rejected.Add(CreateDuckRejected(candidate, twin, movement, predictedSizeSavingsBytes, predictionSpaceGap, rejectionReason)); + } + + private static DuckSmokeRejectedPreview CreateDuckRejected( + TensorConfig candidate, + TensorConfig twin, + string movement, + ulong? predictedSizeSavingsBytes, + double? predictionSpaceGap, + string rejectionReason) + { + return new DuckSmokeRejectedPreview( + TensorConfigIdentity.ToKey(candidate), + TensorConfigIdentity.ToKey(twin), + HybridBenchmarkRepository.BuildDisplayName((HybridQuant)candidate), + HybridBenchmarkRepository.BuildDisplayName((HybridQuant)twin), + movement, + predictedSizeSavingsBytes, + predictionSpaceGap, + rejectionReason); + } + + private static void WriteRejectedSmokePreview( + IReadOnlyList rejected, + IReadOnlyList failedGap) + { + var preview = rejected + .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) + .ThenByDescending(x => x.PredictedSizeSavingsBytes ?? 0UL) + .Take(10) + .ToList(); + + if (preview.Count > 0) + { + AnsiConsole.MarkupLine("[yellow]Top rejected DuckDB smoke preview:[/]"); + foreach (var item in preview) + { + AnsiConsole.MarkupLine( + $"[grey] candidate=[/]{Markup.Escape(item.CandidateName)} [grey]twin=[/]{Markup.Escape(item.TwinName)} [grey]movement=[/]{Markup.Escape(item.Movement)} [grey]predictedSizeSavings=[/]{Markup.Escape(FormatOptionalBytes(item.PredictedSizeSavingsBytes))} [grey]gap=[/]{Markup.Escape(FormatOptionalDouble(item.PredictionSpaceGap))} [grey]reason=[/]{Markup.Escape(item.RejectionReason)}"); + } + } + + var closestGapFailures = failedGap + .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) + .Take(10) + .ToList(); + + if (closestGapFailures.Count > 0) + { + AnsiConsole.MarkupLine("[yellow]Closest monotone downgrade candidates that failed prediction-space gap threshold:[/]"); + foreach (var item in closestGapFailures) + { + AnsiConsole.MarkupLine( + $"[grey] candidate=[/]{Markup.Escape(item.CandidateName)} [grey]twin=[/]{Markup.Escape(item.TwinName)} [grey]predictedSizeSavings=[/]{Markup.Escape(FormatOptionalBytes(item.PredictedSizeSavingsBytes))} [grey]gap=[/]{Markup.Escape(FormatOptionalDouble(item.PredictionSpaceGap))} [grey]reason=[/]{Markup.Escape(item.RejectionReason)}"); + } + } } + private static string FormatOptionalBytes(ulong? value) => value.HasValue ? $"{value.Value:N0}" : "n/a"; + + private static string FormatOptionalDouble(double? value) => value.HasValue ? value.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; + + private static AnomalySeedClass ResolveProbePlanClass(IReadOnlyList subset, AnomalySmokeCandidate seed) + { + if (subset.Count == 1) + return AnomalySeedClass.ExploratorySingle; + + if (subset.Count == 2) + return AnomalySeedClass.ExploratoryPair; + + return seed.SeedClass; + } + + private static PredictionDuckRow ReadPredictionDuckRow(System.Data.Common.DbDataReader r) { return new PredictionDuckRow( @@ -870,6 +1131,9 @@ private void WriteContextualProbeConsoleLog(AnomalyProbePlan plan) AnsiConsole.MarkupLine("[yellow]Contextual anomaly probe:[/]"); AnsiConsole.MarkupLine($"[grey] kind=[/] [cyan]{Markup.Escape(plan.ProbeType)}[/]"); + AnsiConsole.MarkupLine($"[grey] seedClass=[/] [cyan]{Markup.Escape(plan.Seed.SeedClass.ToString())}[/] [grey]probePlanClass=[/] [cyan]{Markup.Escape(plan.ProbePlanClass.ToString())}[/] [grey]priority=[/] [cyan]{plan.Priority}[/]"); + AnsiConsole.MarkupLine($"[grey] referenceName=[/] [cyan]{Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)plan.ReferenceConfig))}[/]"); + AnsiConsole.MarkupLine($"[grey] probeName=[/] [cyan]{Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)plan.ProbeConfig))}[/]"); AnsiConsole.MarkupLine($"[grey] referenceQuant=[/] [cyan]{Markup.Escape(SafeName(plan.ReferenceConfig.BaseQuant))}[/]"); AnsiConsole.MarkupLine($"[grey] base=[/] [cyan]{Markup.Escape(SafeName(plan.ProbeConfig.BaseQuant))}[/]"); AnsiConsole.MarkupLine("[grey] effective groups:[/]"); @@ -1023,6 +1287,8 @@ private object ToSmokeLog(AnomalySmokeCandidate x) return new { x.Source, + seedClass = x.SeedClass.ToString(), + x.Priority, isContextualAnomalySmoke = true, oldBf16Isolation = false, allActiveGroupsExplicit = _movement.HasAllActiveGroupsExplicit(x.CandidateConfig) && _movement.HasAllActiveGroupsExplicit(x.TwinConfig), @@ -1050,7 +1316,13 @@ private object ToSmokeLog(AnomalySmokeCandidate x) x.PredictionSpaceGapVsTwin, x.CandidatePredictedSizeBytes, x.TwinPredictedSizeBytes, - x.SizeSavingsBytes, + x.PredictedSizeSavingsBytes, + x.ActualSizeSavingsBytes, + predictedSizeSavingsDisplay = FormatOptionalBytes(x.PredictedSizeSavingsBytes), + actualSizeSavingsDisplay = FormatOptionalBytes(x.ActualSizeSavingsBytes), + x.PlannedProbeWillMeasureSize, + x.TwinLookupMode, + x.TwinLookupDetail, x.CandidatePredictionRank, x.TwinPredictionRank, x.SmokeScore, @@ -1075,6 +1347,8 @@ private object ToProbeLog(AnomalyProbePlan x) probe = TensorConfigIdentity.ToKey(x.ProbeConfig), referenceName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ReferenceConfig), probeName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ProbeConfig), + referenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ReferenceConfig), + probeInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ProbeConfig), referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ReferenceConfig), candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ProbeConfig), inactiveGroups = _movement.BuildInactiveGroupList(), @@ -1083,6 +1357,9 @@ private object ToProbeLog(AnomalyProbePlan x) movement.DowngradeCount, movement.SameCount, movement.UnknownCount, + seedClass = x.Seed.SeedClass.ToString(), + probePlanClass = x.ProbePlanClass.ToString(), + x.Priority, x.ProbeType, x.HypothesisLabel, groups = x.ProbeGroups.Select(g => new @@ -1127,6 +1404,12 @@ private static object ToRuleLog(AnomalyInteractionRule x) x.CandidateEffectiveGroupsJson, x.InactiveGroupsJson, x.FullTensorConfigKey, + x.ReferenceDisplayName, + x.CandidateDisplayName, + x.ReferenceInternalName, + x.CandidateInternalName, + allActiveGroupsExplicit = true, + oldBf16Isolation = false, x.GroupSetHash, x.GroupCount, x.MeanActualGainVsTwin, @@ -1186,4 +1469,28 @@ private sealed record PredictionDuckRow( ulong PredictedSizeBytes, double PredictionConfidence, ulong PredictionRank); -} + + private sealed record TwinPredictionLookupResult( + PredictionDuckRow? Row, + string Mode, + string Detail, + bool ExplicitContextSearched, + bool SparsePureSearched); + + private sealed record HistoricalTwinLookup( + BenchmarkSnapshotRecord? ExplicitTwin, + bool ExplicitFound, + bool SparseFound, + string Mode, + string Detail); + + private sealed record DuckSmokeRejectedPreview( + string Candidate, + string Twin, + string CandidateName, + string TwinName, + string Movement, + ulong? PredictedSizeSavingsBytes, + double? PredictionSpaceGap, + string RejectionReason); +} \ No newline at end of file From 5b69363dff6c39c4f9090067a015e1684282eed8 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 3 May 2026 19:36:51 -0400 Subject: [PATCH 188/258] Still not perfect, but oh lord the results looking good! --- .../Configuration/MagicQuantYamlConfig.cs | 18 +- MagicQuant/Models/AnomalyDetectionModels.cs | 52 +- .../Models/PredictionSelectionModels.cs | 3 +- .../AnomalyAdjustedPredictionService.cs | 80 +- MagicQuant/Services/AnomalyRuleRepository.cs | 94 +- MagicQuant/Services/AnomalyWorkflowService.cs | 937 ++++++++++++------ .../PredictionGuidedHybridSelectionService.cs | 257 ++++- .../Services/RemainingCombinationStore.cs | 10 +- MagicQuant/config.default.yaml | 21 +- MagicQuant/config.dev.yaml | 21 +- 10 files changed, 1092 insertions(+), 401 deletions(-) diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index fcc36b9..39401c6 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -261,14 +261,24 @@ public sealed class RuntimeAnomalyDetectionConfig public double MaxPredictionSpaceGapVsTwinKld { get; set; } = 0.00050d; public double MaxRelativePredictionPenaltyVsTwin { get; set; } = 0.35d; public double PredictionSpaceViolationMargin { get; set; } = 0.00005d; - public double AnomalyAdjustmentShrinkFactor { get; set; } = 0.70d; + public double AnomalyAdjustmentShrinkFactor { get; set; } = 0.50d; public double MinRuleConfidenceToApply { get; set; } = 0.50d; - public double MaxNegativeAdjustmentKld { get; set; } = 0.002d; - public double MaxPositiveAdjustmentKld { get; set; } = 0.002d; + public double MaxNegativeAdjustmentKld { get; set; } = 0.00075d; + public double MaxPositiveAdjustmentKld { get; set; } = 0.00075d; public double MaxAdjustmentFractionOfBaseKld { get; set; } = 0.75d; public int MaxSmokeCandidatesPerReferenceZone { get; set; } = 12; public bool PersistSuppressionResults { get; set; } = true; public bool VerboseAnomalyLogging { get; set; } = true; + public RuntimeConfirmedAnomalyExpansionConfig ConfirmedAnomalyExpansion { get; set; } = new(); +} + +public sealed class RuntimeConfirmedAnomalyExpansionConfig +{ + public bool Enabled { get; set; } = true; + public int MaxNeighborsPerConfirmedRule { get; set; } = 6; + public int MaxTotalExpansionProbes { get; set; } = 12; + public List AllowedReferenceQuants { get; set; } = ["Q8_0"]; + public List AllowedCandidateQuants { get; set; } = ["Q6_K", "UD-Q6_K_XL", "Q5_K", "UD-Q5_K_XL"]; } public sealed class RuntimeLearningConfig @@ -356,4 +366,4 @@ public sealed class ResolvedCustomBaselineSpec public sealed class RuntimeHardwareConfig { public Dictionary GpuMemoryLimitsGb { get; set; } = new(); -} +} \ No newline at end of file diff --git a/MagicQuant/Models/AnomalyDetectionModels.cs b/MagicQuant/Models/AnomalyDetectionModels.cs index 8379ff4..486ba9e 100644 --- a/MagicQuant/Models/AnomalyDetectionModels.cs +++ b/MagicQuant/Models/AnomalyDetectionModels.cs @@ -42,7 +42,8 @@ public enum AnomalySeedClass HistoricalMissingTwin = 2, PredictionSpaceSmoke = 3, ExploratorySingle = 4, - ExploratoryPair = 5 + ExploratoryPair = 5, + ConfirmedAnomalyNeighborhoodProbe = 6 } public enum AnomalyProbeClassification @@ -86,8 +87,6 @@ public sealed class AnomalyMovementAnalysis public sealed class AnomalySmokeCandidate { public string Source { get; init; } = string.Empty; - public AnomalySeedClass SeedClass { get; init; } = AnomalySeedClass.PredictionSpaceSmoke; - public int Priority { get; init; } public TensorConfig CandidateConfig { get; init; } public TensorConfig TwinConfig { get; init; } public HybridQuant CandidateQuant => (HybridQuant)CandidateConfig; @@ -101,7 +100,10 @@ public sealed class AnomalySmokeCandidate public ulong? ActualSizeSavingsBytes { get; init; } public bool PlannedProbeWillMeasureSize { get; init; } public string TwinLookupMode { get; init; } = string.Empty; - public string TwinLookupDetail { get; init; } = string.Empty; + public string RejectionReason { get; init; } = string.Empty; + public bool MatchedConfirmedAnomalyPattern { get; init; } + public bool TwinFoundInLookupDictionary { get; init; } + public AnomalySeedClass SeedClass { get; init; } = AnomalySeedClass.PredictionSpaceSmoke; public double PredictionSpaceGapVsTwin { get; init; } public ulong? CandidatePredictionRank { get; init; } public ulong? TwinPredictionRank { get; init; } @@ -119,13 +121,13 @@ public sealed class AnomalySmokeCandidate public sealed class AnomalyProbePlan { public AnomalySmokeCandidate Seed { get; init; } = default!; - public AnomalySeedClass ProbePlanClass { get; init; } - public int Priority { get; init; } public TensorConfig ReferenceConfig { get; init; } public TensorConfig ProbeConfig { get; init; } public IReadOnlyList ProbeGroups { get; init; } = Array.Empty(); public string ProbeType { get; init; } = string.Empty; public string HypothesisLabel { get; init; } = string.Empty; + public AnomalySeedClass SeedClass { get; init; } + public AnomalySeedClass ProbePriorityClass { get; init; } } public sealed class AnomalyProbeResult @@ -155,4 +157,40 @@ public sealed class AnomalyRunResult public IReadOnlyList ProbePlans { get; init; } = Array.Empty(); public IReadOnlyList ProbeResults { get; init; } = Array.Empty(); public AnomalyAdjustmentSummary AdjustmentSummary { get; init; } = new(); -} \ No newline at end of file + public object? BestAnomalyReconciliation { get; init; } +} + +public sealed class AnomalySmokeScanDiagnostics +{ + public long PredictedRowsScanned { get; set; } + public long SparseRowsSkipped { get; set; } + public long SparseRowsNormalized { get; set; } + public long Bf16ExactRowsSkipped { get; set; } + public long PureReferenceRowsSkipped { get; set; } + public long ContextualRowsScanned { get; set; } + public long TwinLookupCount { get; set; } + public long DictionaryTwinHits { get; set; } + public long MissingTwins { get; set; } + public long FallbackDbTwinLookups { get; set; } + public long MovementNotMonotoneDowngrade { get; set; } + public long MixedTradeIgnored { get; set; } + public long SizeSavingsBelowThreshold { get; set; } + public long PredictionSpaceGapTooLarge { get; set; } + public long QueuedSmokeCandidates { get; set; } + public long LoadPredictedRowsMs { get; set; } + public long BuildLookupDictionaryMs { get; set; } + public long ScanRowsMs { get; set; } + public IReadOnlyList RejectedPreview { get; set; } = Array.Empty(); + public IReadOnlyList ClosestGapFailures { get; set; } = Array.Empty(); +} + +public sealed class ProbePlanningDiagnostics +{ + public int ExistingRuleKeysLoaded { get; set; } + public int SkippedExistingRuleOrSuppression { get; set; } + public int SkippedDuplicate { get; set; } + public int SkippedInvalidMovement { get; set; } + public int SkippedBudget { get; set; } + public int ProbesQueued { get; set; } + public int ExpansionProbesQueued { get; set; } +} diff --git a/MagicQuant/Models/PredictionSelectionModels.cs b/MagicQuant/Models/PredictionSelectionModels.cs index 9cca43f..c36832d 100644 --- a/MagicQuant/Models/PredictionSelectionModels.cs +++ b/MagicQuant/Models/PredictionSelectionModels.cs @@ -26,6 +26,7 @@ public sealed class RankSafePredictionRow public ulong? ActualSizeBytes { get; set; } public int? ActualRank { get; set; } public ulong? PredictedRank { get; set; } + public double AnomalyAdjustmentKld { get; set; } public double AbsoluteKldError => double.IsNaN(ActualKld) ? double.NaN : Math.Abs(PredictedKld - ActualKld); @@ -154,4 +155,4 @@ public sealed class PredictionGuidedSelectionResult public IReadOnlyList Survivors { get; init; } = Array.Empty(); public IReadOnlyList Eliminations { get; init; } = Array.Empty(); public IReadOnlyList ValidationFailures { get; init; } = Array.Empty(); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs index 5de5d0a..f7b0cb4 100644 --- a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs +++ b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs @@ -1,4 +1,5 @@ using DuckDB.NET.Data; +using System.Text.Json; using MagicQuant.Models; using MQ.DB; using MQ.DB.Models; @@ -52,6 +53,8 @@ await ExecuteAsync(c, $@" if (before == 0) continue; + var beforeStats = await LoadPredictionStatsAsync(c, where, ct); + string expression = adjustment < 0d ? $"GREATEST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), -LEAST({SqlDouble(Config.AnomalyDetection.MaxNegativeAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))" : $"LEAST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))"; @@ -63,7 +66,10 @@ await ExecuteAsync(c, $@" PredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}) WHERE {where};", ct); + var afterStats = await LoadPredictionStatsAsync(c, where, ct); + totalMatched += before; + var actual = ExtractActualEffect(rule); var log = new { ruleId = rule.Id, @@ -71,7 +77,13 @@ await ExecuteAsync(c, $@" ruleType = rule.RuleType, referenceQuant = SafeName(rule.ReferenceQuantId), groupSetHash = rule.GroupSetHash, + basePredictedKld = beforeStats.AverageBasePredictedKld, adjustment, + adjustedPredictedKld = afterStats.AverageFinalPredictedKld, + actualCandidateKld = actual.CandidateKld, + actualTwinKld = actual.TwinKld, + actualGainOrHarm = actual.GainOrHarm, + adjustmentReason = actual.HasActualEffect ? "measured-actual-counterfactual-effect" : "prediction-space-gap-fallback", matchedRows = before, confidence = rule.Confidence, groups = rule.GroupStates @@ -89,7 +101,11 @@ await ExecuteAsync(c, $@" AnsiConsole.MarkupLine( $"[green]Applying anomaly rule:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] direction=[cyan]{Markup.Escape(rule.RuleDirection)}[/] " + - $"adjustment=[cyan]{adjustment:0.000000}[/] matched DuckDB rows=[cyan]{before:N0}[/]"); + $"basePredictedKld=[cyan]{beforeStats.AverageBasePredictedKld:0.000000}[/] adjustment=[cyan]{adjustment:0.000000}[/] " + + $"adjustedPredictedKld=[cyan]{afterStats.AverageFinalPredictedKld:0.000000}[/] " + + $"actualCandidateKld=[cyan]{FmtNullable(actual.CandidateKld)}[/] actualTwinKld=[cyan]{FmtNullable(actual.TwinKld)}[/] " + + $"actualGainOrHarm=[cyan]{FmtNullable(actual.GainOrHarm)}[/] reason=[cyan]{Markup.Escape(actual.HasActualEffect ? "measured-actual-counterfactual-effect" : "prediction-space-gap-fallback")}[/] " + + $"matched DuckDB rows=[cyan]{before:N0}[/]"); } await ReRankAsync(c, ct); @@ -218,6 +234,63 @@ private static async Task CountMatchesAsync(DuckDBConnection c, string whe return ToInt64(await cmd.ExecuteScalarAsync(ct)); } + + private static async Task LoadPredictionStatsAsync(DuckDBConnection c, string where, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = $@" +SELECT AVG(COALESCE(BaseRankSafeKld, PredictedKld)), + AVG(COALESCE(FinalPredictedKld, PredictedKld)) +FROM {CombinationDuckDbSchema.TableName} +WHERE {where};"; + + using var r = await cmd.ExecuteReaderAsync(ct); + if (!await r.ReadAsync(ct)) + return new PredictionMatchStats(0d, 0d); + + return new PredictionMatchStats(ToDouble(r.GetValue(0)), ToDouble(r.GetValue(1))); + } + + private static ActualRuleEffect ExtractActualEffect(AnomalyInteractionRule rule) + { + if (string.IsNullOrWhiteSpace(rule.MetadataJson)) + return new ActualRuleEffect(null, null, null, false); + + try + { + using var doc = JsonDocument.Parse(rule.MetadataJson); + var root = doc.RootElement; + double? candidate = TryGetDouble(root, "actualCandidateKld"); + double? twin = TryGetDouble(root, "actualTwinKld"); + double? gain = TryGetDouble(root, "actualGainOrHarm"); + return new ActualRuleEffect(candidate, twin, gain, candidate.HasValue && twin.HasValue && gain.HasValue); + } + catch + { + return new ActualRuleEffect(null, null, null, false); + } + } + + private static double? TryGetDouble(JsonElement element, string propertyName) + { + return element.TryGetProperty(propertyName, out var value) && value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var d) + ? d + : null; + } + + private static string FmtNullable(double? value) => value.HasValue ? value.Value.ToString("0.000000") : "n/a"; + + private static double ToDouble(object? value) + { + if (value is null || value is DBNull) + return 0d; + + if (value is BigInteger big) + return (double)big; + + return Convert.ToDouble(value); + } + private static long ToInt64(object? value) { if (value is null || value is DBNull) @@ -252,6 +325,9 @@ private static string DescribeRule(AnomalyInteractionRule rule) $" in {SafeName(rule.ReferenceQuantId)} context"; } + private readonly record struct PredictionMatchStats(double AverageBasePredictedKld, double AverageFinalPredictedKld); + private readonly record struct ActualRuleEffect(double? CandidateKld, double? TwinKld, double? GainOrHarm, bool HasActualEffect); + private static string SafeName(byte quantId) { try @@ -263,4 +339,4 @@ private static string SafeName(byte quantId) return $"id:{quantId}"; } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/AnomalyRuleRepository.cs b/MagicQuant/Services/AnomalyRuleRepository.cs index 4f0fc81..f0fe547 100644 --- a/MagicQuant/Services/AnomalyRuleRepository.cs +++ b/MagicQuant/Services/AnomalyRuleRepository.cs @@ -105,13 +105,6 @@ public async Task> PersistProbeResultsAsy InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), ReferenceTensorConfigKey = TensorConfigIdentity.ToKey(result.Plan.ReferenceConfig), ProbeTensorConfigKey = TensorConfigIdentity.ToKey(result.Plan.ProbeConfig), - ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)result.Plan.ReferenceConfig), - ProbeDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)result.Plan.ProbeConfig), - ReferenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)result.Plan.ReferenceConfig), - ProbeInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)result.Plan.ProbeConfig), - SeedClass = result.Plan.Seed.SeedClass.ToString(), - SeedPriority = result.Plan.Seed.Priority, - ProbePlanClass = result.Plan.ProbePlanClass.ToString(), IsContextualAnomalyProbe = true, OldBf16Isolation = false, AllActiveGroupsExplicit = _movement.HasAllActiveGroupsExplicit(result.Plan.ReferenceConfig) && _movement.HasAllActiveGroupsExplicit(result.Plan.ProbeConfig), @@ -170,7 +163,6 @@ public async Task> UpsertRulesFromResultsA string groupSetHash = _movement.BuildChangedGroupHash(probeGroups); string direction = first.RuleDirection.ToString(); byte referenceQuantId = first.Plan.ReferenceConfig.BaseQuant; - string referenceContextKey = _movement.ReferenceContextKey(first.Plan.ReferenceConfig); var rule = await db.AnomalyInteractionRules .Include(x => x.GroupStates) @@ -181,7 +173,6 @@ public async Task> UpsertRulesFromResultsA x.ImatrixDefinitionId == scope.ImatrixDefinitionId && x.BenchmarkCategory == (byte)BenchmarkCategory.General && x.ReferenceQuantId == referenceQuantId && - x.ReferenceContextKey == referenceContextKey && x.GroupSetHash == groupSetHash && x.RuleDirection == direction, ct); @@ -202,10 +193,6 @@ public async Task> UpsertRulesFromResultsA CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions), InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), - ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), - CandidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), - ReferenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), - CandidateInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), RuleDirection = direction, GroupSetHash = groupSetHash, CreatedUtc = DateTime.UtcNow @@ -234,10 +221,6 @@ public async Task> UpsertRulesFromResultsA rule.CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions); rule.InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions); rule.FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig); - rule.ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig); - rule.CandidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig); - rule.ReferenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig); - rule.CandidateInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig); rule.UpdatedUtc = DateTime.UtcNow; rule.MetadataJson = JsonSerializer.Serialize(new { @@ -250,15 +233,12 @@ public async Task> UpsertRulesFromResultsA inactiveGroups = _movement.BuildInactiveGroupList(), first.Plan.ProbeType, first.Plan.HypothesisLabel, - seedClass = first.Plan.Seed.SeedClass.ToString(), - probePlanClass = first.Plan.ProbePlanClass.ToString(), - priority = first.Plan.Priority, - referenceTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ReferenceConfig), - probeTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), - referenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), - probeDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), - referenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), - probeInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), + actualCandidateKld = first.ProbeSnapshot?.Kld, + actualTwinKld = first.ReferenceSnapshot?.Kld, + actualGainOrHarm = first.ActualGainVsTwin, + adjustmentReason = first.ReferenceSnapshot != null && first.ProbeSnapshot != null + ? "measured-actual-counterfactual-effect" + : "prediction-space-gap-fallback", groups = probeGroups.Select(ToGroupLog).ToList() }, JsonOptions); @@ -306,31 +286,42 @@ public async Task> LoadApplicableRulesAsyn .ToList(); } - public async Task HasSuppressionOrRuleAsync( - TensorConfig reference, - IReadOnlyList groups, - CancellationToken ct) + public async Task> LoadExistingRuleSuppressionKeysAsync(CancellationToken ct) { await using var db = new MagicQuantContext(); var scope = await ResolveScopeAsync(db, ct); - string hash = _movement.BuildChangedGroupHash(groups); - string referenceContextKey = _movement.ReferenceContextKey(reference); - return await db.AnomalyInteractionRules + var rows = await db.AnomalyInteractionRules .AsNoTracking() - .AnyAsync(x => - x.ArchitectureFamilyId == scope.ArchitectureFamilyId && - x.TensorGroupProfileId == scope.TensorGroupProfileId && - x.AiModelHashId == scope.AiModelHashId && - x.ImatrixDefinitionId == scope.ImatrixDefinitionId && - x.BenchmarkCategory == (byte)BenchmarkCategory.General && - x.ReferenceQuantId == reference.BaseQuant && - x.ReferenceContextKey == referenceContextKey && - x.GroupSetHash == hash && - x.RuleStatus != AnomalyRuleStatus.Retired.ToString(), - ct); + .Where(x => x.ArchitectureFamilyId == scope.ArchitectureFamilyId) + .Where(x => x.TensorGroupProfileId == scope.TensorGroupProfileId) + .Where(x => x.AiModelHashId == scope.AiModelHashId) + .Where(x => x.ImatrixDefinitionId == scope.ImatrixDefinitionId) + .Where(x => x.BenchmarkCategory == (byte)BenchmarkCategory.General) + .Where(x => x.RuleStatus != AnomalyRuleStatus.Retired.ToString()) + .Select(x => new { x.ReferenceQuantId, x.GroupSetHash }) + .ToListAsync(ct); + + return rows + .Select(x => BuildRuleSuppressionKey(x.ReferenceQuantId, x.GroupSetHash)) + .ToHashSet(StringComparer.Ordinal); } + public string BuildRuleSuppressionKey(TensorConfig reference, IReadOnlyList groups) + => BuildRuleSuppressionKey(reference.BaseQuant, _movement.BuildChangedGroupHash(groups)); + + public async Task HasSuppressionOrRuleAsync( + TensorConfig reference, + IReadOnlyList groups, + CancellationToken ct) + { + var keys = await LoadExistingRuleSuppressionKeysAsync(ct); + return keys.Contains(BuildRuleSuppressionKey(reference, groups)); + } + + private static string BuildRuleSuppressionKey(byte referenceQuantId, string groupSetHash) + => $"ref={referenceQuantId}|groups={groupSetHash}"; + private async Task ResolveScopeAsync(MagicQuantContext db, CancellationToken ct) { uint aiModelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdAsync(db, ct); @@ -427,6 +418,21 @@ private static double ComputeConfidence(IReadOnlyList rows) private static double ComputePredictionAdjustment(AnomalyProbeResult result, double confidence) { var cfg = Config.AnomalyDetection; + + // Prefer measured counterfactual effect. Predicted KLD is a rank-space signal, + // not the same numeric quantity as actual benchmark KLD, so a confirmed probe + // should not be converted through a giant predicted-gap correction. + if (result.ReferenceSnapshot != null && result.ProbeSnapshot != null) + { + double measured = result.ReferenceSnapshot.Kld - result.ProbeSnapshot.Kld; + if (result.RuleDirection == AnomalyRuleDirection.Beneficial && measured > 0d) + return -Math.Min(measured * cfg.AnomalyAdjustmentShrinkFactor, cfg.MaxNegativeAdjustmentKld); + + if (result.RuleDirection == AnomalyRuleDirection.Harmful && measured < 0d) + return Math.Min(Math.Abs(measured) * cfg.AnomalyAdjustmentShrinkFactor, cfg.MaxPositiveAdjustmentKld); + } + + // Fallback only for legacy rows without measured twin/probe truth. double baseGap = result.Plan.Seed.PredictionSpaceGapVsTwin; double required = result.RuleDirection switch { diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs index e76652f..807acc4 100644 --- a/MagicQuant/Services/AnomalyWorkflowService.cs +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Globalization; using System.Numerics; using System.Text.Json; @@ -28,8 +29,7 @@ public sealed class AnomalyWorkflowService private readonly QuantFidelityComparerService _movement; private readonly AnomalyRuleRepository _rules; private readonly AnomalyAdjustedPredictionService _adjuster; - private IReadOnlyList _lastDuckRejectedSmokePreview = Array.Empty(); - private IReadOnlyList _lastDuckFailedGapPreview = Array.Empty(); + private AnomalySmokeScanDiagnostics? _lastDuckSmokeDiagnostics; public AnomalyWorkflowService( RemainingCombinationStore store, @@ -77,20 +77,37 @@ public async Task RunAsync( historicalCount = historical.Count, duckPredictionSpaceCount = duck.Count, selectedSmokeCount = smoke.Count, - duckRejectedSmokePreview = _lastDuckRejectedSmokePreview, - duckClosestFailedGapPreview = _lastDuckFailedGapPreview, + duckDiagnostics = _lastDuckSmokeDiagnostics, smoke = smoke.Select(ToSmokeLog).ToList() }, ct); await WriteJsonAsync("magicquant-anomaly-seeds.json", smoke.Select(ToSmokeLog).ToList(), ct); - var probes = await PlanProbesAsync(smoke, ct); - await WriteJsonAsync("magicquant-anomaly-probes.json", probes.Select(ToProbeLog).ToList(), ct); + var planningDiagnostics = new ProbePlanningDiagnostics(); + var probes = await PlanProbesAsync(smoke, planningDiagnostics, ct); var results = await ValidateProbesAsync(probes, ct); + + var expansionProbes = await PlanConfirmedAnomalyExpansionProbesAsync(results, planningDiagnostics, ct); + if (expansionProbes.Count > 0) + { + probes = probes.Concat(expansionProbes).ToList(); + var expansionResults = await ValidateProbesAsync(expansionProbes, ct); + results = results.Concat(expansionResults).ToList(); + } + + await WriteJsonAsync("magicquant-anomaly-probes.json", new + { + generatedAtUtc = DateTime.UtcNow, + planningDiagnostics, + probes = probes.Select(ToProbeLog).ToList() + }, ct); + await _rules.PersistProbeResultsAsync(session.Id, results, ct); var upsertedRules = await _rules.UpsertRulesFromResultsAsync(results, ct); var applicableRules = await _rules.LoadApplicableRulesAsync(ct); + var bestAnomaly = BuildBestConfirmedAnomalyReconciliation(results, Array.Empty()); + WriteBestAnomalyConsoleLog(bestAnomaly); var adjustment = await _adjuster.ApplyAsync(applicableRules, ct); await WriteJsonAsync("magicquant-anomaly-rules.json", new @@ -108,6 +125,7 @@ public async Task RunAsync( probes = probes.Select(ToProbeLog).ToList(), results = results.Select(ToResultLog).ToList(), rules = applicableRules.Select(ToRuleLog).ToList(), + bestConfirmedAnomaly = bestAnomaly, adjustment }, ct); await WriteFinalManifestAsync("magicquant.prediction-audit.json", new @@ -122,7 +140,8 @@ public async Task RunAsync( SmokeCandidates = smoke, ProbePlans = probes, ProbeResults = results, - AdjustmentSummary = adjustment + AdjustmentSummary = adjustment, + BestAnomalyReconciliation = bestAnomaly }; } finally @@ -136,6 +155,7 @@ private async Task> DetectHistoricalSmokeAsync(Cance { var snapshots = await LoadAllCurrentBenchmarkSnapshotsAsync(ct); var byKey = snapshots.ToDictionary(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal); + await EmitQ8ContextReferenceDriftDiagnosticsAsync(byKey, ct); var predictionLookup = await LoadPredictionLookupAsync(ct); var smoke = new List(); int skippedIsolation = 0; @@ -190,8 +210,26 @@ private async Task> DetectHistoricalSmokeAsync(Cance if (movement.DowngradeCount > Config.AnomalyDetection.MaxProbeGroupCount) continue; - var twinLookup = LookupHistoricalContextualTwin(twinConfig, byKey); - var twin = twinLookup.ExplicitTwin; + string explicitTwinKey = TensorConfigIdentity.ToKey(twinConfig); + var sparseTwinConfig = BuildSparsePureContext(twinConfig.BaseQuant); + string sparseTwinKey = TensorConfigIdentity.ToKey(sparseTwinConfig); + bool searchedExplicit = true; + bool searchedSparse = true; + byKey.TryGetValue(explicitTwinKey, out var explicitTwin); + byKey.TryGetValue(sparseTwinKey, out var sparseTwin); + + var twin = explicitTwin ?? sparseTwin; + string twinLookupMode = explicitTwin != null + ? (sparseTwin != null ? "explicit-context-preferred; sparse-pure-also-found" : "explicit-context-found") + : sparseTwin != null + ? "sparse-pure-fallback-found" + : "missing; searched-explicit-context-and-sparse-pure"; + + if (Config.AnomalyDetection.VerboseAnomalyLogging) + { + AnsiConsole.MarkupLine($"[grey]Historical twin lookup:[/] candidate={Markup.Escape(candidate.DisplayName)} searchedExplicitContext={searchedExplicit} searchedSparsePure={searchedSparse} mode={Markup.Escape(twinLookupMode)}"); + } + if (twin != null && ShouldSkipInvalidContextualAnomalyConfig(twin.Config, "history-existing-twin", out var existingTwinSkipReason)) { skippedNonContextualTwin++; @@ -199,14 +237,10 @@ private async Task> DetectHistoricalSmokeAsync(Cance continue; } - if (Config.AnomalyDetection.VerboseAnomalyLogging) - { - AnsiConsole.MarkupLine( - $"[grey]Historical twin lookup:[/] candidate={Markup.Escape(candidate.DisplayName)} explicitContext={twinLookup.ExplicitFound} sparsePure={twinLookup.SparseFound} mode={Markup.Escape(twinLookup.Mode)} detail={Markup.Escape(twinLookup.Detail)}"); - } - predictionLookup.TryGetValue(TensorConfigIdentity.ToKey(candidate.Config), out var candidatePrediction); - var twinPrediction = LookupPredictionForContextualTwin(twinConfig, predictionLookup, out var predictionTwinLookupMode); + predictionLookup.TryGetValue(explicitTwinKey, out var twinPrediction); + if (twinPrediction == null) + predictionLookup.TryGetValue(sparseTwinKey, out twinPrediction); bool confirmed = twin != null && candidate.SizeBytes <= twin.SizeBytes && @@ -215,31 +249,34 @@ private async Task> DetectHistoricalSmokeAsync(Cance if (!confirmed && twin != null && twin.Kld <= candidate.Kld) continue; - ulong? predictedSavingsBytes = ComputeNullableSavings(twinPrediction?.PredictedSizeBytes, candidatePrediction?.PredictedSizeBytes); - ulong? actualSavingsBytes = twin == null ? null : ComputeNullableSavings(twin.SizeBytes, candidate.SizeBytes) ?? 0UL; + ulong? predictedCandidateSize = candidatePrediction?.PredictedSizeBytes; + ulong? predictedTwinSize = twinPrediction?.PredictedSizeBytes; + ulong? predictedSavings = predictedCandidateSize.HasValue && predictedTwinSize.HasValue && predictedTwinSize.Value >= predictedCandidateSize.Value + ? predictedTwinSize.Value - predictedCandidateSize.Value + : null; + ulong? actualSavings = twin != null && twin.SizeBytes >= candidate.SizeBytes ? twin.SizeBytes - candidate.SizeBytes : null; smoke.Add(new AnomalySmokeCandidate { Source = "history", - SeedClass = confirmed ? AnomalySeedClass.ConfirmedHistoricalCounterfactual : AnomalySeedClass.HistoricalMissingTwin, - Priority = confirmed ? 1000 : 700, CandidateConfig = candidate.Config, TwinConfig = twinConfig, Movement = movement, CandidatePredictedKld = candidatePrediction?.BaseRankSafeKld ?? candidatePrediction?.FinalPredictedKld ?? candidate.Kld, - TwinPredictedKld = twinPrediction?.BaseRankSafeKld ?? twinPrediction?.FinalPredictedKld ?? twin?.Kld ?? 0d, - CandidatePredictedSizeBytes = candidatePrediction?.PredictedSizeBytes, - TwinPredictedSizeBytes = twinPrediction?.PredictedSizeBytes, - PredictedSizeSavingsBytes = predictedSavingsBytes, - ActualSizeSavingsBytes = actualSavingsBytes, - PlannedProbeWillMeasureSize = twin == null, - TwinLookupMode = twinLookup.Mode, - TwinLookupDetail = $"actual={twinLookup.Detail}; prediction={predictionTwinLookupMode}", + TwinPredictedKld = twinPrediction?.BaseRankSafeKld ?? twinPrediction?.FinalPredictedKld ?? twin?.Kld ?? candidate.Kld, + CandidatePredictedSizeBytes = predictedCandidateSize, + TwinPredictedSizeBytes = predictedTwinSize, + PredictedSizeSavingsBytes = predictedSavings, + ActualSizeSavingsBytes = actualSavings, + PlannedProbeWillMeasureSize = twin == null || actualSavings == null, + TwinLookupMode = twinLookupMode, + TwinFoundInLookupDictionary = twinPrediction != null, PredictionSpaceGapVsTwin = (candidatePrediction?.BaseRankSafeKld ?? candidate.Kld) - (twinPrediction?.BaseRankSafeKld ?? twin?.Kld ?? candidate.Kld), CandidatePredictionRank = candidatePrediction?.PredictionRank, TwinPredictionRank = twinPrediction?.PredictionRank, SmokeScore = confirmed ? 1_000_000d : 100d, SmokeStrength = confirmed ? "ConfirmedHistory" : "HistoricalMissingTwin", + SeedClass = confirmed ? AnomalySeedClass.ConfirmedHistoricalCounterfactual : AnomalySeedClass.HistoricalMissingTwin, HasActualTwin = twin != null, CandidateActualKld = candidate.Kld, TwinActualKld = twin?.Kld, @@ -248,7 +285,7 @@ private async Task> DetectHistoricalSmokeAsync(Cance IsConfirmedFromHistory = confirmed, Message = confirmed ? "Existing explicit contextual quantized benchmark history contains a monotone downgrade candidate that beats its higher-bit twin." - : "Existing explicit contextual quantized benchmark history has monotone downgrade smoke but the exact explicit contextual twin is missing." + : "Existing explicit contextual quantized benchmark history has monotone downgrade smoke but the exact twin is missing." }); } @@ -264,11 +301,19 @@ private async Task> DetectHistoricalSmokeAsync(Cance private async Task> DetectDuckSmokeAsync(CancellationToken ct) { + var totalClock = Stopwatch.StartNew(); + var loadClock = Stopwatch.StartNew(); var rows = await LoadPredictionRowsAsync(DuckSmokeScanLimit, ct); - var explicitRows = new Dictionary(StringComparer.Ordinal); + loadClock.Stop(); + + var lookupClock = Stopwatch.StartNew(); + var lookupRows = new Dictionary(StringComparer.Ordinal); + var candidateRows = new Dictionary(StringComparer.Ordinal); var result = new List(); - var rejectedPreview = new List(); - var failedGapPreview = new List(); + var rejected = new List(); + var closestGapFailures = new List(); + var existingKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); + int skippedIsolation = 0; int skippedSparse = 0; int normalizedSparse = 0; @@ -276,12 +321,12 @@ private async Task> DetectDuckSmokeAsync(Cancellatio int skippedMixed = 0; int skippedMovement = 0; int skippedNoTwin = 0; - int logicalTwinFallback = 0; - int explicitTwinFound = 0; - int sparseTwinPredictionUsed = 0; int skippedSavings = 0; int skippedGap = 0; int contextualScanned = 0; + int twinLookupCount = 0; + int dictionaryTwinHits = 0; + int fallbackDbTwinLookups = 0; foreach (var row in rows) { @@ -299,176 +344,213 @@ private async Task> DetectDuckSmokeAsync(Cancellatio if (wasSparse) normalizedSparse++; + var normalizedRow = row with { Config = activated }; + AddOrPreferBetterPredictionRow(lookupRows, normalizedRow); + + // Keep the sparse pure carrier in the lookup as an optional prediction source, + // but never let it become a contextual anomaly identity or probe/rule row. + if (TensorConfigIdentity.IsPureBaseline(row.Config)) + AddOrPreferBetterPredictionRow(lookupRows, row); + if (TensorConfigIdentity.ToKey(activated) == TensorConfigIdentity.ToKey(_movement.BuildBaseContextTwin(activated))) { skippedPure++; continue; } - var normalizedRow = row with { Config = activated }; - string normalizedKey = TensorConfigIdentity.ToKey(activated); - if (!explicitRows.ContainsKey(normalizedKey)) - explicitRows[normalizedKey] = normalizedRow; + AddOrPreferBetterPredictionRow(candidateRows, normalizedRow); } + lookupClock.Stop(); - foreach (var row in explicitRows.Values) + var scanClock = Stopwatch.StartNew(); + foreach (var row in candidateRows.Values) { contextualScanned++; var twin = _movement.BuildBaseContextTwin(row.Config); + string candidateKey = TensorConfigIdentity.ToKey(row.Config); + string twinKey = TensorConfigIdentity.ToKey(twin); if (ShouldSkipInvalidContextualAnomalyConfig(row.Config, "duckdb-normalized-candidate", out var candidateSkipReason)) { skippedIsolation++; + AddRejectedPreview(rejected, row.Config, twin, null, null, null, null, null, "InvalidCandidate: " + candidateSkipReason, false, false); LogSkippedInvalidContextualAnomalyConfig("duckdb-normalized-candidate", row.Config, candidateSkipReason, skippedIsolation); - AddDuckRejected(rejectedPreview, row.Config, twin, "Unknown", null, null, candidateSkipReason); continue; } if (ShouldSkipInvalidContextualAnomalyConfig(twin, "duckdb-twin", out var twinSkipReason)) { skippedIsolation++; + AddRejectedPreview(rejected, row.Config, twin, null, null, null, null, null, "InvalidTwin: " + twinSkipReason, false, false); LogSkippedInvalidContextualAnomalyConfig("duckdb-twin", twin, twinSkipReason, skippedIsolation); - AddDuckRejected(rejectedPreview, row.Config, twin, "Unknown", null, null, twinSkipReason); continue; } var movement = _movement.Analyze(twin, row.Config); + bool matchedConfirmedPattern = existingKeys.Contains(_rules.BuildRuleSuppressionKey(twin, movement.ChangedGroups)); + if (movement.Classification == AnomalyMovementClassification.MixedTrade) { skippedMixed++; + AddRejectedPreview(rejected, row.Config, twin, movement, row, null, null, null, "MixedTrade", matchedConfirmedPattern, false); if (Config.AnomalyDetection.VerboseAnomalyLogging && skippedMixed <= 12) { AnsiConsole.MarkupLine("[grey]Ignored anomaly smoke:[/] classification=MixedTrade reason=normal protect/compress frontier behavior"); } - - AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), null, null, "MixedTrade normal protect/compress frontier behavior"); continue; } if (movement.Classification != AnomalyMovementClassification.MonotoneDowngrade) { skippedMovement++; - AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), null, null, "MovementNotMonotoneDowngrade"); + AddRejectedPreview(rejected, row.Config, twin, movement, row, null, null, null, "MovementNotMonotoneDowngrade", matchedConfirmedPattern, false); continue; } if (movement.DowngradeCount <= 0 || movement.DowngradeCount > Config.AnomalyDetection.MaxProbeGroupCount) { skippedMovement++; - AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), null, null, $"ChangedGroupCountOutsideBudget count={movement.DowngradeCount}"); + AddRejectedPreview(rejected, row.Config, twin, movement, row, null, null, null, "ChangedGroupBudgetExceeded", matchedConfirmedPattern, false); continue; } - var twinLookup = await LoadContextualTwinPredictionRowAsync(twin, explicitRows, ct); - if (twinLookup.Row == null) + twinLookupCount++; + var twinRow = ResolveTwinFromLookupOnly(twin, lookupRows, out var twinLookupMode, out var twinFoundInDictionary); + if (twinRow == null) { skippedNoTwin++; - AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), null, null, $"NoHigherBitTwinPredictionFound lookup={twinLookup.Mode}"); + AddRejectedPreview(rejected, row.Config, twin, movement, row, null, null, null, "MissingTwinInPreloadedDictionary", matchedConfirmedPattern, false); continue; } - if (twinLookup.Mode.Contains("explicit", StringComparison.OrdinalIgnoreCase)) - explicitTwinFound++; - if (twinLookup.Mode.Contains("sparse", StringComparison.OrdinalIgnoreCase)) - sparseTwinPredictionUsed++; - - if (TensorConfigIdentity.ToKey(twinLookup.Row.Config) != TensorConfigIdentity.ToKey(twin)) - logicalTwinFallback++; + if (twinFoundInDictionary) + dictionaryTwinHits++; - if (Config.AnomalyDetection.VerboseAnomalyLogging && (explicitTwinFound + sparseTwinPredictionUsed) <= 12) - { - AnsiConsole.MarkupLine( - $"[grey]DuckDB twin lookup:[/] candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)row.Config))} explicitContext={twinLookup.ExplicitContextSearched} sparsePure={twinLookup.SparsePureSearched} mode={Markup.Escape(twinLookup.Mode)} detail={Markup.Escape(twinLookup.Detail)}"); - } - - if (twinLookup.Row.PredictedSizeBytes <= row.PredictedSizeBytes) + if (twinRow.PredictedSizeBytes <= row.PredictedSizeBytes) { skippedSavings++; - AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), 0UL, row.BaseRankSafeKld - twinLookup.Row.BaseRankSafeKld, "PredictedSizeSavingsNotPositive"); + AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, null, null, "PredictedSizeSavingsNotPositive", matchedConfirmedPattern, true); continue; } - ulong savingsBytes = twinLookup.Row.PredictedSizeBytes - row.PredictedSizeBytes; - double savingsPercent = savingsBytes * 100d / Math.Max(1d, twinLookup.Row.PredictedSizeBytes); + ulong savingsBytes = twinRow.PredictedSizeBytes - row.PredictedSizeBytes; + double savingsPercent = savingsBytes * 100d / Math.Max(1d, twinRow.PredictedSizeBytes); if (savingsPercent < Config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent) { skippedSavings++; - AddDuckRejected(rejectedPreview, row.Config, twin, movement.Classification.ToString(), savingsBytes, row.BaseRankSafeKld - twinLookup.Row.BaseRankSafeKld, $"PredictedSizeSavingsBelowThreshold {savingsPercent:0.000}%"); + AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, null, "PredictedSizeSavingsBelowThreshold", matchedConfirmedPattern, true); continue; } - double gap = row.BaseRankSafeKld - twinLookup.Row.BaseRankSafeKld; + double gap = row.BaseRankSafeKld - twinRow.BaseRankSafeKld; if (gap > Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld) { skippedGap++; - var preview = CreateDuckRejected(row.Config, twin, movement.Classification.ToString(), savingsBytes, gap, $"PredictionSpaceGapTooLarge threshold={Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld:0.000000}"); - rejectedPreview.Add(preview); - failedGapPreview.Add(preview); + var preview = AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, "PredictionSpaceGapTooLarge", matchedConfirmedPattern, true); + closestGapFailures.Add(preview); continue; } - double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinLookup.Row.PredictionRank); + double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank); result.Add(new AnomalySmokeCandidate { Source = "duckdb-prediction-space", - SeedClass = AnomalySeedClass.PredictionSpaceSmoke, - Priority = 500, CandidateConfig = row.Config, TwinConfig = twin, Movement = movement, CandidatePredictedKld = row.BaseRankSafeKld, - TwinPredictedKld = twinLookup.Row.BaseRankSafeKld, + TwinPredictedKld = twinRow.BaseRankSafeKld, CandidatePredictedSizeBytes = row.PredictedSizeBytes, - TwinPredictedSizeBytes = twinLookup.Row.PredictedSizeBytes, + TwinPredictedSizeBytes = twinRow.PredictedSizeBytes, PredictedSizeSavingsBytes = savingsBytes, - ActualSizeSavingsBytes = null, PlannedProbeWillMeasureSize = true, - TwinLookupMode = twinLookup.Mode, - TwinLookupDetail = twinLookup.Detail, + TwinLookupMode = twinLookupMode, + TwinFoundInLookupDictionary = twinFoundInDictionary, PredictionSpaceGapVsTwin = gap, CandidatePredictionRank = row.PredictionRank, - TwinPredictionRank = twinLookup.Row.PredictionRank, + TwinPredictionRank = twinRow.PredictionRank, SmokeScore = score, SmokeStrength = gap <= 0d ? "Strong" : "Close", - Message = "Prediction-space contextual monotone downgrade candidate is close enough to its higher-bit quantized twin to justify probes. Sparse DuckDB source rows, when present, were normalized into explicit active context before classification." + SeedClass = AnomalySeedClass.PredictionSpaceSmoke, + MatchedConfirmedAnomalyPattern = matchedConfirmedPattern, + Message = "Prediction-space contextual monotone downgrade candidate is close enough to its higher-bit quantized twin to justify probes. Twin lookup was dictionary-only from the preloaded DuckDB row set." }); } + scanClock.Stop(); + totalClock.Stop(); + + var diagnostics = new AnomalySmokeScanDiagnostics + { + PredictedRowsScanned = rows.Count, + SparseRowsNormalized = normalizedSparse, + SparseRowsSkipped = skippedSparse, + Bf16ExactRowsSkipped = skippedIsolation, + PureReferenceRowsSkipped = skippedPure, + ContextualRowsScanned = contextualScanned, + TwinLookupCount = twinLookupCount, + DictionaryTwinHits = dictionaryTwinHits, + MissingTwins = skippedNoTwin, + FallbackDbTwinLookups = fallbackDbTwinLookups, + MovementNotMonotoneDowngrade = skippedMovement, + MixedTradeIgnored = skippedMixed, + SizeSavingsBelowThreshold = skippedSavings, + PredictionSpaceGapTooLarge = skippedGap, + QueuedSmokeCandidates = result.Count, + LoadPredictedRowsMs = loadClock.ElapsedMilliseconds, + BuildLookupDictionaryMs = lookupClock.ElapsedMilliseconds, + ScanRowsMs = scanClock.ElapsedMilliseconds, + RejectedPreview = rejected + .OrderBy(x => x.SortOrder) + .Take(25) + .Select(x => x.ToLog()) + .ToList(), + ClosestGapFailures = closestGapFailures + .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) + .ThenByDescending(x => x.PredictedSizeSavingsBytes ?? 0UL) + .Take(10) + .Select(x => x.ToLog()) + .ToList() + }; + + _lastDuckSmokeDiagnostics = diagnostics; AnsiConsole.MarkupLine("[yellow]DuckDB contextual smoke scan:[/]"); AnsiConsole.MarkupLine($"[grey] predicted rows scanned=[/] [cyan]{rows.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] load predicted rows ms=[/] [cyan]{diagnostics.LoadPredictedRowsMs:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] build lookup dictionary ms=[/] [cyan]{diagnostics.BuildLookupDictionaryMs:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] scan rows ms=[/] [cyan]{diagnostics.ScanRowsMs:N0}[/]"); AnsiConsole.MarkupLine($"[grey] sparse rows normalized to explicit context=[/] [cyan]{normalizedSparse:N0}[/]"); AnsiConsole.MarkupLine($"[grey] sparse rows skipped=[/] [cyan]{skippedSparse:N0}[/]"); AnsiConsole.MarkupLine($"[grey] BF16/exact rows skipped=[/] [cyan]{skippedIsolation:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] pure/logical reference rows skipped=[/] [cyan]{skippedPure:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] pure/logical reference rows kept for lookup but skipped as smoke=[/] [cyan]{skippedPure:N0}[/]"); AnsiConsole.MarkupLine($"[grey] contextual quantized rows scanned=[/] [cyan]{contextualScanned:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] explicit higher-bit twin predictions found=[/] [cyan]{explicitTwinFound:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] sparse pure carrier twin predictions used=[/] [cyan]{sparseTwinPredictionUsed:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] logical higher-bit twin fallback used=[/] [cyan]{logicalTwinFallback:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] no higher-bit twin found=[/] [cyan]{skippedNoTwin:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] twin lookup count=[/] [cyan]{twinLookupCount:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] dictionary twin hits=[/] [cyan]{dictionaryTwinHits:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] missing twins=[/] [cyan]{skippedNoTwin:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] fallback DB twin lookups=[/] [cyan]{fallbackDbTwinLookups:N0}[/]"); AnsiConsole.MarkupLine($"[grey] movement not monotone downgrade=[/] [cyan]{skippedMovement:N0}[/]"); AnsiConsole.MarkupLine($"[grey] mixed trade ignored=[/] [cyan]{skippedMixed:N0}[/]"); AnsiConsole.MarkupLine($"[grey] size savings below threshold=[/] [cyan]{skippedSavings:N0}[/]"); AnsiConsole.MarkupLine($"[grey] prediction-space gap too large=[/] [cyan]{skippedGap:N0}[/]"); AnsiConsole.MarkupLine($"[grey] queued smoke candidates=[/] [cyan]{result.Count:N0}[/]"); - _lastDuckRejectedSmokePreview = rejectedPreview - .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) - .ThenByDescending(x => x.PredictedSizeSavingsBytes ?? 0UL) - .Take(10) - .ToList(); - _lastDuckFailedGapPreview = failedGapPreview - .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) - .Take(10) - .ToList(); - if (result.Count == 0 && rows.Count > 0) { - AnsiConsole.MarkupLine( - "[yellow]DuckDB contextual smoke scan produced zero candidates.[/] Rejected-smoke preview follows so threshold/clean-space decisions are visible."); - WriteRejectedSmokePreview(_lastDuckRejectedSmokePreview, _lastDuckFailedGapPreview); + AnsiConsole.MarkupLine("[yellow]DuckDB contextual smoke scan produced zero candidates.[/] Top rejected-smoke previews and closest gap failures were written to magicquant-anomaly-smoke-scan-duckdb-diagnostics.json."); + foreach (var preview in closestGapFailures.OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue).Take(10)) + { + AnsiConsole.MarkupLine($"[grey] rejected monotone gap:[/] candidate={Markup.Escape(preview.CandidateName)} twin={Markup.Escape(preview.TwinName)} gap={FmtNullable(preview.PredictionSpaceGap)} savings={FmtNullable(preview.PredictedSizeSavingsBytes)} reason={Markup.Escape(preview.RejectionReason)} matchedRule={preview.MatchedConfirmedAnomalyPattern}"); + } } + await WriteJsonAsync("magicquant-anomaly-smoke-scan-duckdb-diagnostics.json", new + { + generatedAtUtc = DateTime.UtcNow, + diagnostics, + queued = result.Select(ToSmokeLog).ToList() + }, ct); + return result .GroupBy(x => x.TwinConfig.BaseQuant) .SelectMany(g => g.OrderByDescending(x => x.SmokeScore).Take(Config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone)) @@ -476,10 +558,15 @@ private async Task> DetectDuckSmokeAsync(Cancellatio } - private async Task> PlanProbesAsync(IReadOnlyList seeds, CancellationToken ct) + private async Task> PlanProbesAsync( + IReadOnlyList seeds, + ProbePlanningDiagnostics diagnostics, + CancellationToken ct) { var plans = new List(); var seen = new HashSet(StringComparer.Ordinal); + var existingRuleKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); + diagnostics.ExistingRuleKeysLoaded = existingRuleKeys.Count; foreach (var seed in seeds) { @@ -487,6 +574,7 @@ private async Task> PlanProbesAsync(IReadOnlyList> PlanProbesAsync(IReadOnlyList= Config.AnomalyDetection.MaxProbesPerSeed || plans.Count >= Config.AnomalyDetection.MaxTotalProbesPerRun) + { + diagnostics.SkippedBudget++; break; + } var probeConfig = reference; foreach (var g in subset) @@ -518,46 +612,190 @@ private async Task> PlanProbesAsync(IReadOnlyList" + TensorConfigIdentity.ToKey(probeConfig); if (!seen.Add(key)) + { + diagnostics.SkippedDuplicate++; continue; + } - var planClass = ResolveProbePlanClass(subset, seed); + string probeType = ResolveProbeType(subset.Count, changed.Count); var plan = new AnomalyProbePlan { Seed = seed, - ProbePlanClass = planClass, - Priority = seed.Priority + (planClass == AnomalySeedClass.ExploratoryPair ? 20 : planClass == AnomalySeedClass.ExploratorySingle ? 10 : 0), ReferenceConfig = reference, ProbeConfig = probeConfig, ProbeGroups = subset, - ProbeType = ResolveProbeType(subset.Count, changed.Count), - HypothesisLabel = _movement.DescribeGroups(subset) + ProbeType = probeType, + HypothesisLabel = _movement.DescribeGroups(subset), + SeedClass = seed.SeedClass, + ProbePriorityClass = probeType == "single" + ? AnomalySeedClass.ExploratorySingle + : probeType == "pair" + ? AnomalySeedClass.ExploratoryPair + : seed.SeedClass }; plans.Add(plan); perSeed++; + diagnostics.ProbesQueued++; } AnsiConsole.MarkupLine( - $"[yellow]Potential anomaly smoke:[/] seedClass={seed.SeedClass} priority={seed.Priority} classification={seed.Movement.Classification} candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(seed.CandidateQuant))} " + - $"higher-bit twin={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(seed.TwinQuant))} twinLookup={Markup.Escape(seed.TwinLookupMode)} changed groups={Markup.Escape(_movement.DescribeGroups(changed))} " + + $"[yellow]Potential anomaly smoke:[/] seedClass={seed.SeedClass} classification={seed.Movement.Classification} candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(seed.CandidateQuant))} " + + $"higher-bit twin={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(seed.TwinQuant))} changed groups={Markup.Escape(_movement.DescribeGroups(changed))} " + $"upgradeCount={seed.Movement.UpgradeCount} downgradeCount={seed.Movement.DowngradeCount} " + - $"prediction-space gap={seed.PredictionSpaceGapVsTwin:0.000000} predicted size savings={Markup.Escape(FormatOptionalBytes(seed.PredictedSizeSavingsBytes))} actual size savings={Markup.Escape(FormatOptionalBytes(seed.ActualSizeSavingsBytes))} plannedProbeWillMeasureSize={seed.PlannedProbeWillMeasureSize} probes queued={perSeed:N0}"); + $"prediction-space gap={seed.PredictionSpaceGapVsTwin:0.000000} predicted size savings={FmtNullable(seed.PredictedSizeSavingsBytes)} actual size savings={FmtNullable(seed.ActualSizeSavingsBytes)} plannedProbeWillMeasureSize={seed.PlannedProbeWillMeasureSize} probes queued={perSeed:N0}"); } + AnsiConsole.MarkupLine( + $"[grey]Anomaly probe planning diagnostics:[/] existingRuleKeysLoaded={diagnostics.ExistingRuleKeysLoaded:N0} skippedExistingRuleOrSuppression={diagnostics.SkippedExistingRuleOrSuppression:N0} skippedDuplicate={diagnostics.SkippedDuplicate:N0} skippedInvalidMovement={diagnostics.SkippedInvalidMovement:N0} skippedBudget={diagnostics.SkippedBudget:N0} probesQueued={diagnostics.ProbesQueued:N0}"); + return plans; } + + private async Task> PlanConfirmedAnomalyExpansionProbesAsync( + IReadOnlyList initialResults, + ProbePlanningDiagnostics diagnostics, + CancellationToken ct) + { + var cfg = Config.AnomalyDetection.ConfirmedAnomalyExpansion; + if (!cfg.Enabled || cfg.MaxTotalExpansionProbes <= 0) + return new List(); + + var allowedReference = ResolveQuantNames(cfg.AllowedReferenceQuants).ToHashSet(); + var allowedCandidate = ResolveQuantNames(cfg.AllowedCandidateQuants).ToHashSet(); + var existingRuleKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); + var seen = new HashSet(StringComparer.Ordinal); + var plans = new List(); + + foreach (var result in initialResults + .Where(x => x.RuleDirection == AnomalyRuleDirection.Beneficial) + .Where(x => x.ReferenceSnapshot != null && x.ProbeSnapshot != null) + .OrderByDescending(x => x.ActualGainVsTwin)) + { + if (plans.Count >= cfg.MaxTotalExpansionProbes) + break; + + if (allowedReference.Count > 0 && !allowedReference.Contains(result.Plan.ReferenceConfig.BaseQuant)) + continue; + + var seedGroups = result.Plan.ProbeGroups.OrderBy(x => x.Group.UniqueId).ToList(); + if (seedGroups.Count == 0) + continue; + + byte primaryCandidateQuant = seedGroups[0].CandidateQuantId; + if (allowedCandidate.Count > 0 && !allowedCandidate.Contains(primaryCandidateQuant)) + continue; + + var reference = _movement.CreateActivatedContextBlanket(result.Plan.ReferenceConfig.BaseQuant); + _movement.EnsureAllActiveGroupsExplicit(reference, "confirmed-anomaly-expansion-reference"); + + int perRule = 0; + foreach (var neighbor in _movement.ActiveGroups.Where(g => seedGroups.All(s => s.Group.UniqueId != g.UniqueId)).OrderBy(g => g.UniqueId)) + { + if (perRule >= cfg.MaxNeighborsPerConfirmedRule || plans.Count >= cfg.MaxTotalExpansionProbes) + break; + + var groups = seedGroups + .Concat(new[] + { + new AnomalyChangedGroup + { + Group = neighbor, + ReferenceQuantId = reference.BaseQuant, + CandidateQuantId = primaryCandidateQuant, + ReferenceStoredSlot = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(reference.BaseQuant), + CandidateStoredSlot = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(primaryCandidateQuant), + Movement = QuantMovementKind.Downgrade + } + }) + .OrderBy(x => x.Group.UniqueId) + .ToList(); + + if (groups.Count > Config.AnomalyDetection.MaxProbeGroupCount) + continue; + + if (existingRuleKeys.Contains(_rules.BuildRuleSuppressionKey(reference, groups))) + { + diagnostics.SkippedExistingRuleOrSuppression++; + continue; + } + + var probe = reference; + foreach (var group in groups) + probe = _movement.WithStoredSlot(probe, group.Group, group.CandidateStoredSlot); + + if (ShouldSkipInvalidContextualAnomalyConfig(probe, "confirmed-anomaly-expansion", out _)) + { + diagnostics.SkippedInvalidMovement++; + continue; + } + + string key = TensorConfigIdentity.ToKey(reference) + "=>" + TensorConfigIdentity.ToKey(probe); + if (!seen.Add(key)) + { + diagnostics.SkippedDuplicate++; + continue; + } + + var seed = new AnomalySmokeCandidate + { + Source = "confirmed-anomaly-neighborhood", + CandidateConfig = probe, + TwinConfig = reference, + Movement = _movement.Analyze(reference, probe), + CandidatePredictedKld = result.Plan.Seed.CandidatePredictedKld, + TwinPredictedKld = result.Plan.Seed.TwinPredictedKld, + PredictionSpaceGapVsTwin = result.Plan.Seed.PredictionSpaceGapVsTwin, + SmokeScore = 900_000d + Math.Max(0d, result.ActualGainVsTwin), + SmokeStrength = "ConfirmedAnomalyNeighborhood", + SeedClass = AnomalySeedClass.ConfirmedAnomalyNeighborhoodProbe, + MatchedConfirmedAnomalyPattern = true, + PlannedProbeWillMeasureSize = true, + Message = "Bounded neighborhood probe generated from a confirmed beneficial contextual anomaly." + }; + + plans.Add(new AnomalyProbePlan + { + Seed = seed, + ReferenceConfig = reference, + ProbeConfig = probe, + ProbeGroups = groups, + ProbeType = "confirmed-neighborhood", + HypothesisLabel = _movement.DescribeGroups(groups), + SeedClass = AnomalySeedClass.ConfirmedAnomalyNeighborhoodProbe, + ProbePriorityClass = groups.Count == 1 ? AnomalySeedClass.ExploratorySingle : AnomalySeedClass.ExploratoryPair + }); + + perRule++; + diagnostics.ProbesQueued++; + diagnostics.ExpansionProbesQueued++; + } + } + + if (plans.Count > 0) + { + AnsiConsole.MarkupLine($"[yellow]Confirmed anomaly neighborhood probes:[/] queued={plans.Count:N0} maxTotal={cfg.MaxTotalExpansionProbes:N0}"); + } + + return plans; + } + private async Task> ValidateProbesAsync(IReadOnlyList probes, CancellationToken ct) { if (probes.Count == 0) @@ -708,6 +946,55 @@ private AnomalyProbeResult ClassifyProbe( }; } + + private async Task EmitQ8ContextReferenceDriftDiagnosticsAsync( + IReadOnlyDictionary byKey, + CancellationToken ct) + { + byte q8 = BaselineQuants.Q8_0.UniqueId; + var sparse = BuildSparsePureContext(q8); + TensorConfig explicitContext; + try + { + explicitContext = _movement.CreateActivatedContextBlanket(q8); + } + catch + { + return; + } + + byKey.TryGetValue(TensorConfigIdentity.ToKey(sparse), out var sparseSnapshot); + byKey.TryGetValue(TensorConfigIdentity.ToKey(explicitContext), out var explicitSnapshot); + if (sparseSnapshot == null || explicitSnapshot == null) + return; + + double kldDelta = explicitSnapshot.Kld - sparseSnapshot.Kld; + long sizeDelta = unchecked((long)explicitSnapshot.SizeBytes - (long)sparseSnapshot.SizeBytes); + bool material = Math.Abs(kldDelta) >= Config.AnomalyDetection.MinActualGainVsTwinKld || Math.Abs(sizeDelta) > 0; + + if (material) + AnsiConsole.MarkupLine("[yellow]Q8_CONTEXT_REFERENCE_DRIFT[/]"); + else if (Config.AnomalyDetection.VerboseAnomalyLogging) + AnsiConsole.MarkupLine("[grey]Q8 contextual reference drift check:[/]"); + + if (material || Config.AnomalyDetection.VerboseAnomalyLogging) + { + AnsiConsole.MarkupLine($"[grey] pureQ8Kld=[/] [cyan]{sparseSnapshot.Kld:0.000000}[/] [grey]explicitContextQ8Kld=[/] [cyan]{explicitSnapshot.Kld:0.000000}[/] [grey]kldDelta=[/] [cyan]{kldDelta:0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] pureQ8SizeBytes=[/] [cyan]{sparseSnapshot.SizeBytes:N0}[/] [grey]explicitContextQ8SizeBytes=[/] [cyan]{explicitSnapshot.SizeBytes:N0}[/] [grey]sizeDeltaBytes=[/] [cyan]{sizeDelta:N0}[/]"); + } + + await WriteJsonAsync("magicquant-anomaly-q8-reference-drift.json", new + { + generatedAtUtc = DateTime.UtcNow, + driftCode = material ? "Q8_CONTEXT_REFERENCE_DRIFT" : "none", + pureQ8 = new { key = TensorConfigIdentity.ToKey(sparseSnapshot.Config), sparseSnapshot.DisplayName, sparseSnapshot.Kld, sparseSnapshot.SizeBytes }, + explicitContextQ8 = new { key = TensorConfigIdentity.ToKey(explicitSnapshot.Config), explicitSnapshot.DisplayName, explicitSnapshot.Kld, explicitSnapshot.SizeBytes }, + kldDelta, + sizeDeltaBytes = sizeDelta, + note = "Anomaly probes prefer the explicit all-active contextual Q8 twin. Sparse pure Q8 remains a useful baseline anchor but may not be identical if benchmark execution settings drifted. Compare NGL/benchmark run metadata in SQLite BenchmarkRuns if material drift appears." + }, ct); + } + private async Task> LoadAllCurrentBenchmarkSnapshotsAsync(CancellationToken ct) { await using var db = new MagicQuantContext(); @@ -783,7 +1070,6 @@ private async Task> LoadPredictionRowsAsync(int limit, C WHERE COALESCE(BaseRankSafeKld, PredictedKld) IS NOT NULL AND PredictedSizeBytes IS NOT NULL AND PredictionRank IS NOT NULL - AND {CombinationDuckDbSchema.ActiveCandidatePredicateSql} ORDER BY PredictionRank ASC{limitSql};"; if (limit > 0) cmd.Parameters.Add(new DuckDBParameter { Value = limit }); @@ -832,229 +1118,220 @@ AND PredictionRank IS NOT NULL } - private async Task LoadContextualTwinPredictionRowAsync( + private async Task LoadContextualTwinPredictionRowAsync( TensorConfig explicitTwin, IReadOnlyDictionary explicitRows, CancellationToken ct) { string explicitKey = TensorConfigIdentity.ToKey(explicitTwin); if (explicitRows.TryGetValue(explicitKey, out var inMemoryExplicit)) - { - return new TwinPredictionLookupResult( - inMemoryExplicit with { Config = explicitTwin }, - "explicit-context-memory", - "Searched explicit all-active context in current DuckDB scan; found in memory.", - ExplicitContextSearched: true, - SparsePureSearched: false); - } + return inMemoryExplicit; var explicitRow = await LoadSinglePredictionRowAsync(explicitTwin, ct); if (explicitRow != null) - { - return new TwinPredictionLookupResult( - explicitRow with { Config = explicitTwin }, - "explicit-context-duckdb", - "Searched explicit all-active context in DuckDB; found exact contextual twin.", - ExplicitContextSearched: true, - SparsePureSearched: false); - } + return explicitRow with { Config = explicitTwin }; // The normal generator may only contain the pure sparse carrier for an all-Q8/all-Q6 // reference. For smoke scoring, that sparse carrier is allowed as a prediction source // only; the anomaly seed/probe/twin identity remains the explicit activated blanket. - var sparsePure = BuildSparsePureCarrier(explicitTwin.BaseQuant); + var sparsePure = new TensorConfig( + explicitTwin.BaseQuant, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue); var sparseRow = await LoadSinglePredictionRowAsync(sparsePure, ct); - if (sparseRow != null) - { - return new TwinPredictionLookupResult( - sparseRow with { Config = sparsePure }, - "sparse-pure-carrier-prediction-fallback", - "Searched explicit all-active context first; missing. Searched sparse pure carrier for prediction-space scoring only; found fallback. Contextual anomaly identity remains explicit.", - ExplicitContextSearched: true, - SparsePureSearched: true); - } - - return new TwinPredictionLookupResult( - null, - "missing-explicit-and-sparse", - "Searched explicit all-active context and sparse pure carrier; no prediction row found.", - ExplicitContextSearched: true, - SparsePureSearched: true); + return sparseRow == null ? null : sparseRow with { Config = sparsePure }; } - private HistoricalTwinLookup LookupHistoricalContextualTwin( - TensorConfig explicitTwin, - IReadOnlyDictionary snapshotsByKey) - { - string explicitKey = TensorConfigIdentity.ToKey(explicitTwin); - var sparsePure = BuildSparsePureCarrier(explicitTwin.BaseQuant); - string sparseKey = TensorConfigIdentity.ToKey(sparsePure); - snapshotsByKey.TryGetValue(explicitKey, out var explicitSnapshot); - bool sparseFound = snapshotsByKey.ContainsKey(sparseKey); - - if (explicitSnapshot != null) - { - return new HistoricalTwinLookup( - explicitSnapshot, - ExplicitFound: true, - SparseFound: sparseFound, - Mode: sparseFound ? "explicit-context-preferred;sparse-pure-also-present" : "explicit-context-found", - Detail: sparseFound - ? "Searched explicit all-active context and sparse pure carrier. Using explicit contextual twin for anomaly truth." - : "Searched explicit all-active context. Using explicit contextual twin for anomaly truth."); - } - - if (sparseFound) + private static void AddOrPreferBetterPredictionRow(IDictionary rows, PredictionDuckRow row) + { + string key = TensorConfigIdentity.ToKey(row.Config); + if (!rows.TryGetValue(key, out var existing) || + row.BaseRankSafeKld < existing.BaseRankSafeKld || + (Math.Abs(row.BaseRankSafeKld - existing.BaseRankSafeKld) < 1e-12 && row.PredictedSizeBytes < existing.PredictedSizeBytes)) { - return new HistoricalTwinLookup( - null, - ExplicitFound: false, - SparseFound: true, - Mode: "sparse-pure-found-ignored-for-contextual-truth", - Detail: "Searched explicit all-active context first; missing. Sparse pure carrier exists but is not trusted as contextual anomaly truth."); + rows[key] = row; } - - return new HistoricalTwinLookup( - null, - ExplicitFound: false, - SparseFound: false, - Mode: "missing-explicit-and-sparse", - Detail: "Searched explicit all-active context and sparse pure carrier; no historical twin benchmark found."); } - private static PredictionDuckRow? LookupPredictionForContextualTwin( + private static TensorConfig BuildSparsePureContext(byte baseQuantId) => new( + baseQuantId, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue, + BaselineQuants.TensorConfigNullSlotValue); + + private PredictionDuckRow? ResolveTwinFromLookupOnly( TensorConfig explicitTwin, - IReadOnlyDictionary predictionLookup, - out string lookupMode) + IReadOnlyDictionary lookup, + out string lookupMode, + out bool foundInDictionary) { string explicitKey = TensorConfigIdentity.ToKey(explicitTwin); - if (predictionLookup.TryGetValue(explicitKey, out var explicitPrediction)) + if (lookup.TryGetValue(explicitKey, out var explicitRow)) { - lookupMode = "explicit-context-prediction-found"; - return explicitPrediction with { Config = explicitTwin }; + lookupMode = "explicit-context-found"; + foundInDictionary = true; + return explicitRow with { Config = explicitTwin }; } - var sparsePure = BuildSparsePureCarrier(explicitTwin.BaseQuant); + var sparsePure = BuildSparsePureContext(explicitTwin.BaseQuant); string sparseKey = TensorConfigIdentity.ToKey(sparsePure); - if (predictionLookup.TryGetValue(sparseKey, out var sparsePrediction)) + if (lookup.TryGetValue(sparseKey, out var sparseRow)) { - lookupMode = "sparse-pure-prediction-fallback"; - return sparsePrediction with { Config = sparsePure }; + lookupMode = "sparse-pure-prediction-fallback; explicit-context-identity-preserved"; + foundInDictionary = true; + return sparseRow with { Config = sparsePure }; } - lookupMode = "prediction-missing-explicit-and-sparse"; + lookupMode = "missing; dictionary-only lookup searched explicit-context and sparse-pure"; + foundInDictionary = false; return null; } - private static TensorConfig BuildSparsePureCarrier(byte baseQuant) - { - return new TensorConfig( - baseQuant, - BaselineQuants.TensorConfigNullSlotValue, - BaselineQuants.TensorConfigNullSlotValue, - BaselineQuants.TensorConfigNullSlotValue, - BaselineQuants.TensorConfigNullSlotValue, - BaselineQuants.TensorConfigNullSlotValue, - BaselineQuants.TensorConfigNullSlotValue, - BaselineQuants.TensorConfigNullSlotValue, - BaselineQuants.TensorConfigNullSlotValue, - BaselineQuants.TensorConfigNullSlotValue); - } - - private static ulong? ComputeNullableSavings(ulong? referenceBytes, ulong? candidateBytes) - { - if (!referenceBytes.HasValue || !candidateBytes.HasValue) - return null; - - return referenceBytes.Value > candidateBytes.Value - ? referenceBytes.Value - candidateBytes.Value - : 0UL; - } - - private static void AddDuckRejected( - List rejected, + private RejectedSmokePreview AddRejectedPreview( + List previews, TensorConfig candidate, TensorConfig twin, - string movement, + AnomalyMovementAnalysis? movement, + PredictionDuckRow? candidateRow, + PredictionDuckRow? twinRow, ulong? predictedSizeSavingsBytes, double? predictionSpaceGap, - string rejectionReason) + string rejectionReason, + bool matchedConfirmedAnomalyPattern, + bool twinFoundInLookup) { - rejected.Add(CreateDuckRejected(candidate, twin, movement, predictedSizeSavingsBytes, predictionSpaceGap, rejectionReason)); - } - - private static DuckSmokeRejectedPreview CreateDuckRejected( - TensorConfig candidate, - TensorConfig twin, - string movement, - ulong? predictedSizeSavingsBytes, - double? predictionSpaceGap, - string rejectionReason) - { - return new DuckSmokeRejectedPreview( - TensorConfigIdentity.ToKey(candidate), - TensorConfigIdentity.ToKey(twin), - HybridBenchmarkRepository.BuildDisplayName((HybridQuant)candidate), - HybridBenchmarkRepository.BuildDisplayName((HybridQuant)twin), + var preview = new RejectedSmokePreview( + previews.Count, + candidate, + twin, movement, + candidateRow, + twinRow, predictedSizeSavingsBytes, predictionSpaceGap, - rejectionReason); + rejectionReason, + matchedConfirmedAnomalyPattern, + twinFoundInLookup); + + if (previews.Count < 500 || rejectionReason.Contains("PredictionSpaceGap", StringComparison.OrdinalIgnoreCase) || matchedConfirmedAnomalyPattern) + previews.Add(preview); + + return preview; } - private static void WriteRejectedSmokePreview( - IReadOnlyList rejected, - IReadOnlyList failedGap) + private static HashSet ResolveQuantNames(IEnumerable names) { - var preview = rejected - .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) - .ThenByDescending(x => x.PredictedSizeSavingsBytes ?? 0UL) - .Take(10) - .ToList(); + var map = BaselineQuants.GetAllRecognizedBaselines() + .SelectMany(q => q.Names.Select(n => (Name: n, Quant: q))) + .GroupBy(x => x.Name, StringComparer.OrdinalIgnoreCase) + .ToDictionary(g => g.Key, g => g.First().Quant.UniqueId, StringComparer.OrdinalIgnoreCase); - if (preview.Count > 0) + var result = new HashSet(); + foreach (var name in names ?? Array.Empty()) { - AnsiConsole.MarkupLine("[yellow]Top rejected DuckDB smoke preview:[/]"); - foreach (var item in preview) - { - AnsiConsole.MarkupLine( - $"[grey] candidate=[/]{Markup.Escape(item.CandidateName)} [grey]twin=[/]{Markup.Escape(item.TwinName)} [grey]movement=[/]{Markup.Escape(item.Movement)} [grey]predictedSizeSavings=[/]{Markup.Escape(FormatOptionalBytes(item.PredictedSizeSavingsBytes))} [grey]gap=[/]{Markup.Escape(FormatOptionalDouble(item.PredictionSpaceGap))} [grey]reason=[/]{Markup.Escape(item.RejectionReason)}"); - } + if (map.TryGetValue(name, out var id)) + result.Add(id); } - var closestGapFailures = failedGap - .OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue) - .Take(10) - .ToList(); + return result; + } + + private object? BuildBestConfirmedAnomalyReconciliation( + IReadOnlyList results, + IReadOnlyCollection selectedSurvivors) + { + var best = results + .Where(x => x.RuleDirection == AnomalyRuleDirection.Beneficial) + .Where(x => x.ReferenceSnapshot != null && x.ProbeSnapshot != null) + .OrderByDescending(x => x.ActualGainVsTwin) + .ThenBy(x => x.ProbeSnapshot!.Kld) + .ThenBy(x => x.ProbeSnapshot!.SizeBytes) + .FirstOrDefault(); + + if (best == null || best.ReferenceSnapshot == null || best.ProbeSnapshot == null) + return null; - if (closestGapFailures.Count > 0) + string key = TensorConfigIdentity.ToKey(best.ProbeSnapshot.Config); + bool selected = selectedSurvivors.Any(x => TensorConfigIdentity.ToKey(x.Config) == key); + string reasonNotSelected = selected + ? string.Empty + : selectedSurvivors.Count == 0 + ? "final selection has not run yet" + : BuildReasonBestAnomalyNotSelected(best.ProbeSnapshot, selectedSurvivors); + + return new { - AnsiConsole.MarkupLine("[yellow]Closest monotone downgrade candidates that failed prediction-space gap threshold:[/]"); - foreach (var item in closestGapFailures) - { - AnsiConsole.MarkupLine( - $"[grey] candidate=[/]{Markup.Escape(item.CandidateName)} [grey]twin=[/]{Markup.Escape(item.TwinName)} [grey]predictedSizeSavings=[/]{Markup.Escape(FormatOptionalBytes(item.PredictedSizeSavingsBytes))} [grey]gap=[/]{Markup.Escape(FormatOptionalDouble(item.PredictionSpaceGap))} [grey]reason=[/]{Markup.Escape(item.RejectionReason)}"); - } - } + candidate = TensorConfigIdentity.ToKey(best.ProbeSnapshot.Config), + candidateName = best.ProbeSnapshot.DisplayName, + twin = TensorConfigIdentity.ToKey(best.ReferenceSnapshot.Config), + twinName = best.ReferenceSnapshot.DisplayName, + actualCandidateKld = best.ProbeSnapshot.Kld, + actualTwinKld = best.ReferenceSnapshot.Kld, + actualGain = best.ActualGainVsTwin, + actualCandidateSizeBytes = best.ProbeSnapshot.SizeBytes, + actualTwinSizeBytes = best.ReferenceSnapshot.SizeBytes, + actualSizeSavingsBytes = best.ReferenceSnapshot.SizeBytes >= best.ProbeSnapshot.SizeBytes ? best.ReferenceSnapshot.SizeBytes - best.ProbeSnapshot.SizeBytes : 0UL, + classification = best.Classification.ToString(), + selectedAsSurvivor = selected, + reasonNotSelected + }; } - private static string FormatOptionalBytes(ulong? value) => value.HasValue ? $"{value.Value:N0}" : "n/a"; + private static string BuildReasonBestAnomalyNotSelected( + BenchmarkSnapshotRecord anomaly, + IReadOnlyCollection selectedSurvivors) + { + var dominator = selectedSurvivors.FirstOrDefault(x => x.SizeBytes <= anomaly.SizeBytes && x.Kld <= anomaly.Kld && (x.SizeBytes < anomaly.SizeBytes || x.Kld < anomaly.Kld)); + if (dominator != null) + return $"dominated by selected survivor {dominator.DisplayName} (kld={dominator.Kld:0.000000}, size={dominator.SizeBytes})"; + + var lowerKld = selectedSurvivors.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes).FirstOrDefault(); + if (lowerKld != null && lowerKld.Kld < anomaly.Kld) + return $"selected frontier contains lower-KLD survivor {lowerKld.DisplayName}; anomaly was not a final dominance/spacing winner"; - private static string FormatOptionalDouble(double? value) => value.HasValue ? value.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; + return "not present in selected survivor set; no dominance reason was found in current reconciliation data"; + } - private static AnomalySeedClass ResolveProbePlanClass(IReadOnlyList subset, AnomalySmokeCandidate seed) + private static void WriteBestAnomalyConsoleLog(object? reconciliation) { - if (subset.Count == 1) - return AnomalySeedClass.ExploratorySingle; - - if (subset.Count == 2) - return AnomalySeedClass.ExploratoryPair; + if (reconciliation == null) + { + AnsiConsole.MarkupLine("[grey]Best confirmed beneficial anomaly:[/] none"); + return; + } - return seed.SeedClass; + string json = JsonSerializer.Serialize(reconciliation, JsonOptions); + using var doc = JsonDocument.Parse(json); + var root = doc.RootElement; + AnsiConsole.MarkupLine("[yellow]Best confirmed beneficial anomaly:[/]"); + AnsiConsole.MarkupLine($"[grey] candidate=[/] [cyan]{Markup.Escape(root.GetProperty("candidateName").GetString() ?? "unknown")}[/]"); + AnsiConsole.MarkupLine($"[grey] twin=[/] [cyan]{Markup.Escape(root.GetProperty("twinName").GetString() ?? "unknown")}[/]"); + AnsiConsole.MarkupLine($"[grey] actualCandidateKld=[/] [cyan]{root.GetProperty("actualCandidateKld").GetDouble():0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] actualTwinKld=[/] [cyan]{root.GetProperty("actualTwinKld").GetDouble():0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] actualGain=[/] [cyan]{root.GetProperty("actualGain").GetDouble():0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] selectedAsSurvivor=[/] [cyan]{root.GetProperty("selectedAsSurvivor").GetBoolean()}[/]"); + string reason = root.TryGetProperty("reasonNotSelected", out var r) ? r.GetString() ?? string.Empty : string.Empty; + if (!string.IsNullOrWhiteSpace(reason)) + AnsiConsole.MarkupLine($"[grey] reasonNotSelected=[/] [yellow]{Markup.Escape(reason)}[/]"); } + private static string FmtNullable(double? value) => value.HasValue ? value.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; + private static string FmtNullable(ulong? value) => value.HasValue ? value.Value.ToString("N0", CultureInfo.InvariantCulture) : "n/a"; private static PredictionDuckRow ReadPredictionDuckRow(System.Data.Common.DbDataReader r) { @@ -1131,9 +1408,7 @@ private void WriteContextualProbeConsoleLog(AnomalyProbePlan plan) AnsiConsole.MarkupLine("[yellow]Contextual anomaly probe:[/]"); AnsiConsole.MarkupLine($"[grey] kind=[/] [cyan]{Markup.Escape(plan.ProbeType)}[/]"); - AnsiConsole.MarkupLine($"[grey] seedClass=[/] [cyan]{Markup.Escape(plan.Seed.SeedClass.ToString())}[/] [grey]probePlanClass=[/] [cyan]{Markup.Escape(plan.ProbePlanClass.ToString())}[/] [grey]priority=[/] [cyan]{plan.Priority}[/]"); - AnsiConsole.MarkupLine($"[grey] referenceName=[/] [cyan]{Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)plan.ReferenceConfig))}[/]"); - AnsiConsole.MarkupLine($"[grey] probeName=[/] [cyan]{Markup.Escape(HybridBenchmarkRepository.BuildDisplayName((HybridQuant)plan.ProbeConfig))}[/]"); + AnsiConsole.MarkupLine($"[grey] seedClass=[/] [cyan]{Markup.Escape(plan.SeedClass.ToString())}[/] [grey]priority=[/] [cyan]{Markup.Escape(plan.ProbePriorityClass.ToString())}[/]"); AnsiConsole.MarkupLine($"[grey] referenceQuant=[/] [cyan]{Markup.Escape(SafeName(plan.ReferenceConfig.BaseQuant))}[/]"); AnsiConsole.MarkupLine($"[grey] base=[/] [cyan]{Markup.Escape(SafeName(plan.ProbeConfig.BaseQuant))}[/]"); AnsiConsole.MarkupLine("[grey] effective groups:[/]"); @@ -1287,8 +1562,6 @@ private object ToSmokeLog(AnomalySmokeCandidate x) return new { x.Source, - seedClass = x.SeedClass.ToString(), - x.Priority, isContextualAnomalySmoke = true, oldBf16Isolation = false, allActiveGroupsExplicit = _movement.HasAllActiveGroupsExplicit(x.CandidateConfig) && _movement.HasAllActiveGroupsExplicit(x.TwinConfig), @@ -1317,12 +1590,15 @@ private object ToSmokeLog(AnomalySmokeCandidate x) x.CandidatePredictedSizeBytes, x.TwinPredictedSizeBytes, x.PredictedSizeSavingsBytes, + x.CandidateActualSizeBytes, + x.TwinActualSizeBytes, x.ActualSizeSavingsBytes, - predictedSizeSavingsDisplay = FormatOptionalBytes(x.PredictedSizeSavingsBytes), - actualSizeSavingsDisplay = FormatOptionalBytes(x.ActualSizeSavingsBytes), x.PlannedProbeWillMeasureSize, x.TwinLookupMode, - x.TwinLookupDetail, + x.RejectionReason, + x.MatchedConfirmedAnomalyPattern, + x.TwinFoundInLookupDictionary, + seedClass = x.SeedClass.ToString(), x.CandidatePredictionRank, x.TwinPredictionRank, x.SmokeScore, @@ -1347,8 +1623,6 @@ private object ToProbeLog(AnomalyProbePlan x) probe = TensorConfigIdentity.ToKey(x.ProbeConfig), referenceName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ReferenceConfig), probeName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ProbeConfig), - referenceInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ReferenceConfig), - probeInternalName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ProbeConfig), referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ReferenceConfig), candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ProbeConfig), inactiveGroups = _movement.BuildInactiveGroupList(), @@ -1357,11 +1631,10 @@ private object ToProbeLog(AnomalyProbePlan x) movement.DowngradeCount, movement.SameCount, movement.UnknownCount, - seedClass = x.Seed.SeedClass.ToString(), - probePlanClass = x.ProbePlanClass.ToString(), - x.Priority, x.ProbeType, x.HypothesisLabel, + seedClass = x.SeedClass.ToString(), + probePriorityClass = x.ProbePriorityClass.ToString(), groups = x.ProbeGroups.Select(g => new { group = g.Group.Name, @@ -1404,12 +1677,6 @@ private static object ToRuleLog(AnomalyInteractionRule x) x.CandidateEffectiveGroupsJson, x.InactiveGroupsJson, x.FullTensorConfigKey, - x.ReferenceDisplayName, - x.CandidateDisplayName, - x.ReferenceInternalName, - x.CandidateInternalName, - allActiveGroupsExplicit = true, - oldBf16Isolation = false, x.GroupSetHash, x.GroupCount, x.MeanActualGainVsTwin, @@ -1462,6 +1729,68 @@ private static string SafeName(byte quantId) } } + + private sealed class RejectedSmokePreview + { + public RejectedSmokePreview( + int sortOrder, + TensorConfig candidate, + TensorConfig twin, + AnomalyMovementAnalysis? movement, + PredictionDuckRow? candidateRow, + PredictionDuckRow? twinRow, + ulong? predictedSizeSavingsBytes, + double? predictionSpaceGap, + string rejectionReason, + bool matchedConfirmedAnomalyPattern, + bool twinFoundInLookup) + { + SortOrder = sortOrder; + Candidate = candidate; + Twin = twin; + Movement = movement; + CandidateRow = candidateRow; + TwinRow = twinRow; + PredictedSizeSavingsBytes = predictedSizeSavingsBytes; + PredictionSpaceGap = predictionSpaceGap; + RejectionReason = rejectionReason; + MatchedConfirmedAnomalyPattern = matchedConfirmedAnomalyPattern; + TwinFoundInLookup = twinFoundInLookup; + } + + public int SortOrder { get; } + public TensorConfig Candidate { get; } + public TensorConfig Twin { get; } + public AnomalyMovementAnalysis? Movement { get; } + public PredictionDuckRow? CandidateRow { get; } + public PredictionDuckRow? TwinRow { get; } + public ulong? PredictedSizeSavingsBytes { get; } + public double? PredictionSpaceGap { get; } + public string RejectionReason { get; } + public bool MatchedConfirmedAnomalyPattern { get; } + public bool TwinFoundInLookup { get; } + public string CandidateName => HybridBenchmarkRepository.BuildDisplayName((HybridQuant)Candidate); + public string TwinName => HybridBenchmarkRepository.BuildDisplayName((HybridQuant)Twin); + + public object ToLog() => new + { + candidate = TensorConfigIdentity.ToKey(Candidate), + twin = TensorConfigIdentity.ToKey(Twin), + candidateName = CandidateName, + twinName = TwinName, + movement = Movement?.Classification.ToString() ?? "Unknown", + predictedCandidateKld = CandidateRow?.BaseRankSafeKld, + predictedTwinKld = TwinRow?.BaseRankSafeKld, + predictionSpaceGap = PredictionSpaceGap, + predictedCandidateSizeBytes = CandidateRow?.PredictedSizeBytes, + predictedTwinSizeBytes = TwinRow?.PredictedSizeBytes, + predictedSizeSavingsBytes = PredictedSizeSavingsBytes, + rejectionReason = RejectionReason, + matchedConfirmedAnomalyPattern = MatchedConfirmedAnomalyPattern, + twinExistedInLookupDictionary = TwinFoundInLookup + }; + } + private sealed record PredictionDuckRow( TensorConfig Config, double BaseRankSafeKld, @@ -1469,28 +1798,4 @@ private sealed record PredictionDuckRow( ulong PredictedSizeBytes, double PredictionConfidence, ulong PredictionRank); - - private sealed record TwinPredictionLookupResult( - PredictionDuckRow? Row, - string Mode, - string Detail, - bool ExplicitContextSearched, - bool SparsePureSearched); - - private sealed record HistoricalTwinLookup( - BenchmarkSnapshotRecord? ExplicitTwin, - bool ExplicitFound, - bool SparseFound, - string Mode, - string Detail); - - private sealed record DuckSmokeRejectedPreview( - string Candidate, - string Twin, - string CandidateName, - string TwinName, - string Movement, - ulong? PredictedSizeSavingsBytes, - double? PredictionSpaceGap, - string RejectionReason); } \ No newline at end of file diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index 038bb5a..e05ab82 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -1,7 +1,10 @@ using System.Text.Json; using MagicQuant.Models; using MagicQuant.Services.Progress; +using Microsoft.EntityFrameworkCore; using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models.DbModels; using MQ.DB.Models; using Spectre.Console; @@ -66,6 +69,13 @@ public async Task RunAsync( var interior = await RunInteriorSubspaceDiscoveryAsync(current, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, interior.AcceptedSnapshots, eliminationRecords, "interior subspace discovery dominated by real benchmark truth"); + var bestConfirmedAnomaly = await LoadBestConfirmedBeneficialAnomalySnapshotAsync(ct); + if (bestConfirmedAnomaly != null && current.All(x => TensorConfigIdentity.ToKey(x.Config) != TensorConfigIdentity.ToKey(bestConfirmedAnomaly.Config))) + { + AnsiConsole.MarkupLine($"[yellow]Best confirmed anomaly reconciliation:[/] adding probe-confirmed anomaly to final frontier consideration: [cyan]{Markup.Escape(bestConfirmedAnomaly.DisplayName)}[/]"); + current = MergeAndDominanceFilter(current, new[] { bestConfirmedAnomaly }, eliminationRecords, "best confirmed beneficial anomaly included for final reconciliation"); + } + current = ApplyMeaningfulSpacing(current, eliminationRecords); var finalDominance = _finalEliminator.Eliminate(current); @@ -85,13 +95,14 @@ public async Task RunAsync( } } + await WriteAnomalySelectionReconciliationAsync(finalDominance.Survivors, bestConfirmedAnomaly, ct); await WriteSelectionPhaseDiagnosticsAsync(phaseDiagnostics, validationFailures, validationAttempts, ct); return new PredictionGuidedSelectionResult { Survivors = finalDominance.Survivors.ToList(), Eliminations = eliminationRecords - .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{x.Reason}") + .DistinctBy(x => $"{TensorConfigIdentity.ToKey(x.Eliminated.Config)}::{TensorConfigIdentity.ToKey(x.Eliminator.Config)}::{NormalizePublicEliminationReason(x.Reason)}") .ToList(), ValidationFailures = validationFailures }; @@ -154,6 +165,11 @@ private async Task RunStrictDominanceReplacementAsync( CandidateSelectionNotes = ["Strict query requires predicted size <= anchor size and predicted KLD + epsilon < anchor KLD."] }).ToList(); + var strictNotes = new List { "Strict query requires predicted size <= anchor size and predicted KLD + epsilon < anchor KLD." }; + bool anomalyStrictMode = IsQ8Anchor(anchor) || candidates.Any(x => Math.Abs(x.Prediction.AnomalyAdjustmentKld) > 1e-12); + if (anomalyStrictMode) + strictNotes.Add("Q8/anomaly strict mode: validate all fetched candidates up to the configured attempt limit before choosing by actual KLD/size truth."); + var diag = new SelectionPhaseDiagnostic { Phase = "StrictDominanceReplacement", @@ -169,16 +185,17 @@ private async Task RunStrictDominanceReplacementAsync( CandidatesAfterBrutalityCount = strictRows.Count, SelectedForValidationCount = candidates.Count, CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, - TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList() + TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), + Notes = strictNotes }; phaseDiagnostics.Add(diag); - AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, selected={candidates.Count:N0}/{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}"); + AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, selected={candidates.Count:N0}/{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}, q8/anomaly-mode={anomalyStrictMode}"); if (candidates.Count == 0) continue; - CandidateValidationResult? acceptedForAnchor = null; + var acceptedForAnchor = new List(); foreach (var candidate in candidates) { var validation = await BuildAndValidateSingleAsync( @@ -192,29 +209,222 @@ private async Task RunStrictDominanceReplacementAsync( if (validation.Accepted && validation.Snapshot != null) { - acceptedForAnchor = validation; - accepted.Add(validation.Snapshot); - eliminations.Add(new BaselineEliminationRecord - { - Eliminated = anchor, - Eliminator = validation.Snapshot, - Reason = "strict hybrid dominance: lower KLD at same-or-smaller real size" - }); - break; + acceptedForAnchor.Add(validation); + if (!anomalyStrictMode) + break; + + continue; } validationFailures.Add(validation); } - if (acceptedForAnchor == null) + if (acceptedForAnchor.Count == 0) { AnsiConsole.MarkupLine($"[grey]No strict predicted replacement validated for anchor:[/] {Markup.Escape(anchor.DisplayName)}"); + continue; } + + var chosen = ChooseBestStrictDominanceCandidate(anchor, acceptedForAnchor); + accepted.Add(chosen.Snapshot!); + eliminations.Add(new BaselineEliminationRecord + { + Eliminated = anchor, + Eliminator = chosen.Snapshot!, + Reason = "strict hybrid dominance: best accepted actual KLD at same-or-smaller real size" + }); + + var nonChosen = acceptedForAnchor + .Where(x => !ReferenceEquals(x, chosen)) + .Select(x => new + { + candidate = x.Snapshot!.DisplayName, + actualKld = x.Snapshot.Kld, + actualSizeBytes = x.Snapshot.SizeBytes, + reasonLost = ExplainStrictAcceptedLoss(anchor, chosen.Snapshot!, x.Snapshot) + }) + .ToList(); + + strictNotes.Add($"validated candidates={validationAttempts.Count(v => v.Candidate.WindowLabel == $"strict <= {anchor.DisplayName}")}; accepted candidates={acceptedForAnchor.Count}; chosen={chosen.Snapshot!.DisplayName}"); + foreach (var loss in nonChosen) + strictNotes.Add($"accepted-but-not-chosen: {loss.candidate} lost because {loss.reasonLost}"); + + AnsiConsole.MarkupLine("[green]Best strict dominance candidate selected:[/]"); + AnsiConsole.MarkupLine($"[grey] anchor=[/] [cyan]{Markup.Escape(anchor.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] chosen=[/] [cyan]{Markup.Escape(chosen.Snapshot!.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] actualKld=[/] [cyan]{chosen.Snapshot.Kld:0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] actualSizeBytes=[/] [cyan]{chosen.Snapshot.SizeBytes:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] gainVsAnchor=[/] [cyan]{anchor.Kld - chosen.Snapshot.Kld:0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] acceptedCandidateCount=[/] [cyan]{acceptedForAnchor.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] reason=[/] [cyan]{Markup.Escape(ResolveStrictChosenReason(anchor, chosen.Snapshot!))}[/]"); } return new PhaseValidationResult { AcceptedSnapshots = accepted }; } + + + private async Task LoadBestConfirmedBeneficialAnomalySnapshotAsync(CancellationToken ct) + { + if (!Config.AnomalyDetection.Enabled) + return null; + + await using var db = new MagicQuantContext(); + var modelHashId = await ArchitectureFamilyService.ResolveScopedAiModelHashIdOrNullAsync(db, ct); + if (modelHashId == null) + return null; + + int architectureFamilyId = TensorGroupProfileService.RequireCurrentArchitectureFamilyId(); + int tensorGroupProfileId = TensorGroupProfileService.RequireCurrentProfileId(); + int? imatrixId = await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, modelHashId.Value, createIfMissing: false, ct); + + // SQLite cannot translate ulong ordering expressions. Keep the database query + // to filtering/include only, then rank the tiny scoped anomaly observation set + // in LINQ-to-Objects. This preserves the intended ordering without tripping + // Microsoft.Data.Sqlite on SizeSavingsBytes. + var observations = await db.AnomalyProbeObservations + .AsNoTracking() + .Include(x => x.ProbeTensorCombo) + .Where(x => x.ArchitectureFamilyId == architectureFamilyId) + .Where(x => x.TensorGroupProfileId == tensorGroupProfileId) + .Where(x => x.AiModelHashId == modelHashId.Value) + .Where(x => x.ImatrixDefinitionId == imatrixId) + .Where(x => x.BenchmarkCategory == (byte)BenchmarkCategory.General) + .Where(x => x.RuleDirection == AnomalyRuleDirection.Beneficial.ToString()) + .Where(x => x.Accepted) + .Where(x => x.IsContextualAnomalyProbe && !x.OldBf16Isolation && x.AllActiveGroupsExplicit) + .Where(x => x.ProbeTensorCombo != null) + .ToListAsync(ct); + + var observation = observations + .OrderByDescending(x => x.ActualGainVsTwin) + .ThenBy(x => x.ActualKld) + .ThenByDescending(x => x.SizeSavingsBytes) + .FirstOrDefault(); + + if (observation?.ProbeTensorCombo == null) + return null; + + var combo = observation.ProbeTensorCombo; + var config = new TensorConfig(combo.BaseQuant, combo.Embeddings, combo.LmHead, combo.AttnQ, combo.AttnKV, combo.AttnOutput, combo.FfnUpGate, combo.FfnDown, combo.MoeExperts, combo.MoeRouter); + return await _repository.LoadBenchmarkSnapshotAsync(config, ct); + } + + private async Task WriteAnomalySelectionReconciliationAsync( + IReadOnlyList survivors, + BenchmarkSnapshotRecord? bestAnomaly, + CancellationToken ct) + { + if (!Config.AnomalyDetection.Enabled) + return; + + object payload; + if (bestAnomaly == null) + { + payload = new + { + generatedAtUtc = DateTime.UtcNow, + anomalyModeEnabled = true, + bestConfirmedAnomaly = (object?)null, + selectedAnomalyDerivedSurvivor = (object?)null, + bestAnomalyWasSelected = false, + reasonNotSelected = "no confirmed beneficial anomaly observation was available" + }; + } + else + { + string bestKey = TensorConfigIdentity.ToKey(bestAnomaly.Config); + bool selected = survivors.Any(x => TensorConfigIdentity.ToKey(x.Config) == bestKey); + string reason = selected ? string.Empty : ExplainBestAnomalyNotSelected(bestAnomaly, survivors); + payload = new + { + generatedAtUtc = DateTime.UtcNow, + anomalyModeEnabled = true, + bestConfirmedAnomaly = ToAnchorLog(bestAnomaly), + selectedAnomalyDerivedSurvivor = selected ? ToAnchorLog(bestAnomaly) : null, + bestAnomalyWasSelected = selected, + reasonNotSelected = reason, + survivorKeys = survivors.Select(x => new { key = TensorConfigIdentity.ToKey(x.Config), x.DisplayName, x.Kld, x.SizeBytes }).ToList() + }; + + AnsiConsole.MarkupLine("[yellow]Best confirmed beneficial anomaly:[/]"); + AnsiConsole.MarkupLine($"[grey] candidate=[/] [cyan]{Markup.Escape(bestAnomaly.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] actualCandidateKld=[/] [cyan]{bestAnomaly.Kld:0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] actualCandidateSizeBytes=[/] [cyan]{bestAnomaly.SizeBytes:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] selectedAsSurvivor=[/] [cyan]{selected}[/]"); + if (!selected) + AnsiConsole.MarkupLine($"[grey] reasonNotSelected=[/] [yellow]{Markup.Escape(reason)}[/]"); + } + + if (!string.IsNullOrWhiteSpace(Cache.OutputDirectory)) + { + string manifestDir = Path.Combine(Cache.OutputDirectory!, "magicquant-manifest"); + Directory.CreateDirectory(manifestDir); + await File.WriteAllTextAsync(Path.Combine(manifestDir, "magicquant.anomaly-selection-reconciliation.json"), JsonSerializer.Serialize(payload, JsonOptions), ct); + } + } + + private static string ExplainBestAnomalyNotSelected(BenchmarkSnapshotRecord bestAnomaly, IReadOnlyList survivors) + { + var dominator = survivors.FirstOrDefault(x => x.SizeBytes <= bestAnomaly.SizeBytes && x.Kld <= bestAnomaly.Kld && (x.SizeBytes < bestAnomaly.SizeBytes || x.Kld < bestAnomaly.Kld)); + if (dominator != null) + return $"dominated by survivor {dominator.DisplayName}"; + + var lower = survivors.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes).FirstOrDefault(); + if (lower != null && lower.Kld < bestAnomaly.Kld) + return $"survivor {lower.DisplayName} has lower actual KLD; spacing/final frontier kept that candidate"; + + return "not selected after spacing/final dominance; no direct dominator found"; + } + + private static bool IsQ8Anchor(BenchmarkSnapshotRecord anchor) + { + try + { + return BaselineQuants.FromId(anchor.Config.BaseQuant).Names.Any(x => x.Contains("Q8", StringComparison.OrdinalIgnoreCase)); + } + catch + { + return anchor.DisplayName.Contains("Q8", StringComparison.OrdinalIgnoreCase); + } + } + + private static CandidateValidationResult ChooseBestStrictDominanceCandidate( + BenchmarkSnapshotRecord anchor, + IReadOnlyList accepted) + { + return accepted + .Where(x => x.Snapshot != null) + .OrderBy(x => x.Snapshot!.Kld) + .ThenBy(x => x.Snapshot!.SizeBytes) + .ThenByDescending(x => anchor.Kld - x.Snapshot!.Kld) + .ThenBy(x => x.Candidate.Prediction.PredictedRank ?? ulong.MaxValue) + .ThenByDescending(x => x.Candidate.Prediction.PredictionConfidence) + .First(); + } + + private static string ResolveStrictChosenReason(BenchmarkSnapshotRecord anchor, BenchmarkSnapshotRecord chosen) + => $"lowest actual KLD among accepted strict dominance candidates, then smaller actual size, gainVsAnchor={anchor.Kld - chosen.Kld:0.000000}"; + + private static string ExplainStrictAcceptedLoss( + BenchmarkSnapshotRecord anchor, + BenchmarkSnapshotRecord chosen, + BenchmarkSnapshotRecord loser) + { + if (loser.Kld > chosen.Kld) + return $"higher actual KLD ({loser.Kld:0.000000} > {chosen.Kld:0.000000})"; + + if (Math.Abs(loser.Kld - chosen.Kld) < 1e-12 && loser.SizeBytes > chosen.SizeBytes) + return $"same actual KLD but larger actual size ({loser.SizeBytes:N0} > {chosen.SizeBytes:N0})"; + + double chosenGain = anchor.Kld - chosen.Kld; + double loserGain = anchor.Kld - loser.Kld; + if (Math.Abs(loser.Kld - chosen.Kld) < 1e-12 && loser.SizeBytes == chosen.SizeBytes && loserGain < chosenGain) + return $"weaker gain over anchor ({loserGain:0.000000} < {chosenGain:0.000000})"; + + return "lost by prediction rank/confidence tie-breaker after actual KLD and size were equivalent"; + } + private async Task RunNearBaselineReplacementAsync( IReadOnlyList currentAnchors, List eliminations, @@ -1260,6 +1470,23 @@ private static ulong AddPercent(ulong bytes, double percent) private static ulong Distance(ulong left, ulong right) => left >= right ? left - right : right - left; + private static string NormalizePublicEliminationReason(string reason) + { + if (string.IsNullOrWhiteSpace(reason)) + return string.Empty; + + if (reason.Contains("dominance", StringComparison.OrdinalIgnoreCase)) + return "dominance"; + + if (reason.Contains("spacing", StringComparison.OrdinalIgnoreCase)) + return "spacing"; + + if (reason.Contains("strict", StringComparison.OrdinalIgnoreCase)) + return "strict-dominance"; + + return reason.Trim().ToLowerInvariant(); + } + private static bool Dominates(BenchmarkSnapshotRecord better, BenchmarkSnapshotRecord worse) { bool sameOrSmaller = better.SizeBytes <= worse.SizeBytes; @@ -1338,4 +1565,4 @@ private sealed class CandidatePreviewLog public double BrutalityRequiredGain { get; init; } public string BrutalityExplanation { get; init; } = string.Empty; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 857eccd..4258db4 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -242,7 +242,8 @@ public async Task> QueryStrictDominanceCand {CombinationDuckDbSchema.EffectivePredictedKldSql} AS PredictedKld, PredictedSizeBytes, PredictionConfidence, - PredictionRank + PredictionRank, + COALESCE(AnomalyAdjustmentKld, 0.0) AS AnomalyAdjustmentKld FROM {TableName} WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL AND PredictedSizeBytes IS NOT NULL @@ -385,12 +386,12 @@ private async Task> QueryPredictedRowsAsync using var r = await cmd.ExecuteReaderAsync(ct); var list = new List(); while (await r.ReadAsync(ct)) - list.Add(MapPredictedRow(r)); + list.Add(MapPredictedRow(r, anomalyAdjustmentColumnIndex: r.FieldCount > 14 ? 14 : null)); return list; } - private static RankSafePredictionRow MapPredictedRow(System.Data.Common.DbDataReader r) + private static RankSafePredictionRow MapPredictedRow(System.Data.Common.DbDataReader r, int? anomalyAdjustmentColumnIndex = null) { var config = ReadTensorConfig(r); @@ -402,6 +403,7 @@ private static RankSafePredictionRow MapPredictedRow(System.Data.Common.DbDataRe PredictedSizeBytes = ToUInt64(r.GetValue(11)), PredictionConfidence = ToDouble(r.GetValue(12)), PredictedRank = ToUInt64(r.GetValue(13)), + AnomalyAdjustmentKld = anomalyAdjustmentColumnIndex.HasValue ? ToDouble(r.GetValue(anomalyAdjustmentColumnIndex.Value)) : 0d, IsPredictable = true, IsSizePredictable = true }; @@ -561,4 +563,4 @@ private static async Task EnsureTensorConfigsTableExistsAsync(DuckDBConnection c "This almost always means the generator and prediction reader are using different DuckDB filenames, " + "or prediction started before QuantDatabaseService initialized/rebuilt the search-space table."); } -} +} \ No newline at end of file diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 2873b9b..40ed378 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -286,16 +286,16 @@ anomaly_detection: prediction_space_violation_margin: 0.00005 # Shrink applied to prediction-space adjustment after a rule is confirmed. - anomaly_adjustment_shrink_factor: 0.70 + anomaly_adjustment_shrink_factor: 0.50 # Minimum confidence required before applying a confirmed anomaly rule. min_rule_confidence_to_apply: 0.50 # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. - max_negative_adjustment_kld: 0.002 + max_negative_adjustment_kld: 0.00075 # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. - max_positive_adjustment_kld: 0.002 + max_positive_adjustment_kld: 0.00075 # Fractional cap relative to BaseRankSafeKld. max_adjustment_fraction_of_base_kld: 0.75 @@ -309,6 +309,19 @@ anomaly_detection: # Emit detailed anomaly logs. verbose_anomaly_logging: true + # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. + confirmed_anomaly_expansion: + enabled: true + max_neighbors_per_confirmed_rule: 6 + max_total_expansion_probes: 12 + allowed_reference_quants: + - Q8_0 + allowed_candidate_quants: + - Q6_K + - UD-Q6_K_XL + - Q5_K + - UD-Q5_K_XL + output: # Optional explicit output directory. # If blank, MagicQuant will default to: @@ -448,4 +461,4 @@ baselines: # # Example note: # # If the repo does not actually contain IQ3_XS, do not reference it. # # Use only filenames that truly exist in the repository. - [] + [] \ No newline at end of file diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 7bc086e..01d0fec 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -208,16 +208,16 @@ anomaly_detection: prediction_space_violation_margin: 0.00005 # Shrink applied to prediction-space adjustment after a rule is confirmed. - anomaly_adjustment_shrink_factor: 0.70 + anomaly_adjustment_shrink_factor: 0.50 # Minimum confidence required before applying a confirmed anomaly rule. min_rule_confidence_to_apply: 0.50 # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. - max_negative_adjustment_kld: 0.002 + max_negative_adjustment_kld: 0.00075 # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. - max_positive_adjustment_kld: 0.002 + max_positive_adjustment_kld: 0.00075 # Fractional cap relative to BaseRankSafeKld. max_adjustment_fraction_of_base_kld: 0.75 @@ -231,6 +231,19 @@ anomaly_detection: # Emit detailed anomaly logs. verbose_anomaly_logging: true + # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. + confirmed_anomaly_expansion: + enabled: true + max_neighbors_per_confirmed_rule: 6 + max_total_expansion_probes: 12 + allowed_reference_quants: + - Q8_0 + allowed_candidate_quants: + - Q6_K + - UD-Q6_K_XL + - Q5_K + - UD-Q5_K_XL + output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: @@ -342,4 +355,4 @@ baselines: force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true + allow_as_explicit_group_candidate: true \ No newline at end of file From 9c91b8529407d3026e5e730e63a66723d484126c Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 3 May 2026 23:31:35 -0400 Subject: [PATCH 189/258] better ish --- MagicQuant/Config.cs | 4 +- .../Configuration/MagicQuantYamlConfig.cs | 31 ++ .../Configuration/MagicQuantYamlLoader.cs | 27 +- MagicQuant/Models/AnomalyDetectionModels.cs | 28 +- .../AnomalyAdjustedPredictionService.cs | 497 ++++++++++++++---- MagicQuant/Services/AnomalyRuleRepository.cs | 45 +- MagicQuant/Services/AnomalyWorkflowService.cs | 496 ++++++++++++++++- MagicQuant/config.default.yaml | 37 ++ MagicQuant/config.dev.yaml | 37 ++ 9 files changed, 1062 insertions(+), 140 deletions(-) diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 13166d8..c3b5d65 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -69,7 +69,9 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.AnomalyDetection; + public static RuntimeSynergyDetectionConfig SynergyDetection => Current.SynergyDetection; public static bool AnomalyDetectionEnabled => Current.AnomalyDetection.Enabled; + public static bool SynergyDetectionEnabled => Current.SynergyDetection.Enabled; public static string? OutputDirectory => Current.Output.OutputDir; public static string OutputNamePrefix => string.IsNullOrWhiteSpace(Current.Output.OutputNamePrefix) @@ -94,4 +96,4 @@ public static void SetResolvedCustomBaselines(IEnumerable BrainLayers => Current.BrainLayers; public static List CollapsePenaltySchemes => Current.CollapsePenaltySchemes; public static List MoeIndicatorTensors => Current.MoeIndicatorTensors; -} +} \ No newline at end of file diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 39401c6..17b1f4b 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -18,6 +18,7 @@ public sealed class MagicQuantYamlConfig public RuntimeSurvivalConfig Survival { get; set; } = new(); public RuntimeCandidateSelectionConfig CandidateSelection { get; set; } = new(); public RuntimeAnomalyDetectionConfig AnomalyDetection { get; set; } = new(); + public RuntimeSynergyDetectionConfig SynergyDetection { get; set; } = new(); public RuntimeHardwareConfig Hardware { get; set; } = new(); public List SensitivityProbeGroups { get; set; } = @@ -281,6 +282,36 @@ public sealed class RuntimeConfirmedAnomalyExpansionConfig public List AllowedCandidateQuants { get; set; } = ["Q6_K", "UD-Q6_K_XL", "Q5_K", "UD-Q5_K_XL"]; } + +public sealed class RuntimeSynergyDetectionConfig +{ + public bool Enabled { get; set; } = true; + public int MaxRefinementRounds { get; set; } = 1; + public double ExactContextConfidenceMultiplier { get; set; } = 1.00d; + public double SameSelectedGroupsConfidenceMultiplier { get; set; } = 0.55d; + public double EquivalentQuantFamilyConfidenceMultiplier { get; set; } = 0.30d; + public double GroupFamilySuspicionConfidenceMultiplier { get; set; } = 0.15d; + public double MinConfidenceToApplyAdjustment { get; set; } = 0.35d; + public double MinConfidenceToScheduleTransferProbe { get; set; } = 0.25d; + public double MaxNegativeAdjustmentKld { get; set; } = 0.002d; + public double MaxNegativeAdjustmentFractionOfBaseKld { get; set; } = 0.75d; + public bool TransferProbeEnabled { get; set; } = true; + public int MaxTransferProbesPerTemplate { get; set; } = 6; + public int MaxTotalTransferProbesPerRun { get; set; } = 24; + public RuntimeSynergyTransferProbeContextStrataConfig TransferProbeContextStrata { get; set; } = new(); + public bool VerboseSynergyLogging { get; set; } = true; + public double MinSmokeScore { get; set; } = 0.55d; + public double MaxSmokeGapKld { get; set; } = 0.004d; + public int TopRejectedSmokePreview { get; set; } = 25; +} + +public sealed class RuntimeSynergyTransferProbeContextStrataConfig +{ + public int HighFidelityMaxNonReferenceGroupsBelowQ6 { get; set; } = 1; + public int MidFidelityMaxNonReferenceGroupsBelowQ6 { get; set; } = 3; + public bool LowFidelityEnabled { get; set; } = false; +} + public sealed class RuntimeLearningConfig { public bool ForceRelearnArchitectureFamily { get; set; } diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 0443a11..f6216a6 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -162,6 +162,31 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld = Math.Clamp(config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld, 0d, 1d); config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone = Math.Max(1, config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone); + config.SynergyDetection ??= new RuntimeSynergyDetectionConfig(); + config.SynergyDetection.MaxRefinementRounds = Math.Clamp(config.SynergyDetection.MaxRefinementRounds, 0, 1); + config.SynergyDetection.ExactContextConfidenceMultiplier = Math.Clamp(config.SynergyDetection.ExactContextConfidenceMultiplier, 0d, 1d); + config.SynergyDetection.SameSelectedGroupsConfidenceMultiplier = Math.Clamp(config.SynergyDetection.SameSelectedGroupsConfidenceMultiplier, 0d, 1d); + config.SynergyDetection.EquivalentQuantFamilyConfidenceMultiplier = Math.Clamp(config.SynergyDetection.EquivalentQuantFamilyConfidenceMultiplier, 0d, 1d); + config.SynergyDetection.GroupFamilySuspicionConfidenceMultiplier = Math.Clamp(config.SynergyDetection.GroupFamilySuspicionConfidenceMultiplier, 0d, 1d); + config.SynergyDetection.MinConfidenceToApplyAdjustment = Math.Clamp(config.SynergyDetection.MinConfidenceToApplyAdjustment, 0d, 1d); + config.SynergyDetection.MinConfidenceToScheduleTransferProbe = Math.Clamp(config.SynergyDetection.MinConfidenceToScheduleTransferProbe, 0d, 1d); + config.SynergyDetection.MaxNegativeAdjustmentKld = Math.Max(0d, config.SynergyDetection.MaxNegativeAdjustmentKld); + config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld = Math.Clamp(config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld, 0d, 1d); + config.SynergyDetection.MaxTransferProbesPerTemplate = Math.Max(0, config.SynergyDetection.MaxTransferProbesPerTemplate); + config.SynergyDetection.MaxTotalTransferProbesPerRun = Math.Max(0, config.SynergyDetection.MaxTotalTransferProbesPerRun); + config.SynergyDetection.MinSmokeScore = Math.Clamp(config.SynergyDetection.MinSmokeScore, 0d, 1d); + config.SynergyDetection.MaxSmokeGapKld = Math.Max(0d, config.SynergyDetection.MaxSmokeGapKld); + config.SynergyDetection.TopRejectedSmokePreview = Math.Max(1, config.SynergyDetection.TopRejectedSmokePreview); + config.SynergyDetection.TransferProbeContextStrata ??= new RuntimeSynergyTransferProbeContextStrataConfig(); + config.SynergyDetection.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, config.SynergyDetection.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6); + config.SynergyDetection.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(config.SynergyDetection.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6, config.SynergyDetection.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6); + + // Compatibility bridge: old anomaly_detection remains the operational section; + // synergy_detection controls transfer/generalization behavior. If the new section + // is disabled, anomaly/synergy pass can still run exact-context probes, but no + // transfer probes or transferable adjustments are scheduled. + config.AnomalyDetection.MinRuleConfidenceToApply = Math.Min(config.AnomalyDetection.MinRuleConfidenceToApply, config.SynergyDetection.MinConfidenceToApplyAdjustment); + ApplyStandardBaselineFilters(config.Baselines); BaselineQuants.ResetDynamicCustomBaselines(); } @@ -399,4 +424,4 @@ private static List NormalizeScratchRoots(IEnumerable? roots) return Path.GetFullPath(value); } -} +} \ No newline at end of file diff --git a/MagicQuant/Models/AnomalyDetectionModels.cs b/MagicQuant/Models/AnomalyDetectionModels.cs index 486ba9e..1a42857 100644 --- a/MagicQuant/Models/AnomalyDetectionModels.cs +++ b/MagicQuant/Models/AnomalyDetectionModels.cs @@ -43,7 +43,9 @@ public enum AnomalySeedClass PredictionSpaceSmoke = 3, ExploratorySingle = 4, ExploratoryPair = 5, - ConfirmedAnomalyNeighborhoodProbe = 6 + ConfirmedAnomalyNeighborhoodProbe = 6, + SynergyTransferProbe = 7, + CounterfactualSynergyTemplate = 8 } public enum AnomalyProbeClassification @@ -84,6 +86,15 @@ public sealed class AnomalyMovementAnalysis public int NetBitDelta { get; init; } } +public enum SynergyTemplateMatchTier +{ + None = 0, + ExactContext = 1, + SameSelectedGroups = 2, + EquivalentQuantFamily = 3, + GroupFamilySuspicion = 4 +} + public sealed class AnomalySmokeCandidate { public string Source { get; init; } = string.Empty; @@ -102,6 +113,8 @@ public sealed class AnomalySmokeCandidate public string TwinLookupMode { get; init; } = string.Empty; public string RejectionReason { get; init; } = string.Empty; public bool MatchedConfirmedAnomalyPattern { get; init; } + public bool WouldMatchConfirmedTemplate { get; init; } + public SynergyTemplateMatchTier SynergyMatchTier { get; init; } = SynergyTemplateMatchTier.None; public bool TwinFoundInLookupDictionary { get; init; } public AnomalySeedClass SeedClass { get; init; } = AnomalySeedClass.PredictionSpaceSmoke; public double PredictionSpaceGapVsTwin { get; init; } @@ -109,6 +122,7 @@ public sealed class AnomalySmokeCandidate public ulong? TwinPredictionRank { get; init; } public double SmokeScore { get; init; } public string SmokeStrength { get; init; } = string.Empty; + public bool IsTransferProbeSeed { get; init; } public bool HasActualTwin { get; init; } public double? CandidateActualKld { get; init; } public double? TwinActualKld { get; init; } @@ -147,6 +161,11 @@ public sealed class AnomalyAdjustmentSummary { public int AppliedRuleCount { get; init; } public long MatchedRowCount { get; init; } + public long ExactContextMatches { get; init; } + public long SameSelectedGroupMatches { get; init; } + public long EquivalentQuantFamilyMatches { get; init; } + public long SuppressedMatches { get; init; } + public long HarmfulMatches { get; init; } public string DuckDbPath { get; init; } = string.Empty; public IReadOnlyList RuleMatches { get; init; } = Array.Empty(); } @@ -176,6 +195,8 @@ public sealed class AnomalySmokeScanDiagnostics public long MixedTradeIgnored { get; set; } public long SizeSavingsBelowThreshold { get; set; } public long PredictionSpaceGapTooLarge { get; set; } + public long BelowMinSmokeScore { get; set; } + public long CatastrophicGapRejected { get; set; } public long QueuedSmokeCandidates { get; set; } public long LoadPredictedRowsMs { get; set; } public long BuildLookupDictionaryMs { get; set; } @@ -193,4 +214,7 @@ public sealed class ProbePlanningDiagnostics public int SkippedBudget { get; set; } public int ProbesQueued { get; set; } public int ExpansionProbesQueued { get; set; } -} + public int TransferProbesQueued { get; set; } + public int SkippedTransferStrata { get; set; } + public int SkippedMissingVirtualTwin { get; set; } +} \ No newline at end of file diff --git a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs index f7b0cb4..3db0970 100644 --- a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs +++ b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs @@ -1,9 +1,10 @@ using DuckDB.NET.Data; +using System.Globalization; +using System.Numerics; using System.Text.Json; using MagicQuant.Models; using MQ.DB; using MQ.DB.Models; -using System.Numerics; using MQ.DB.Models.DbModels; using Spectre.Console; @@ -37,75 +38,111 @@ await ExecuteAsync(c, $@" WHERE BaseRankSafeKld IS NOT NULL;", ct); long totalMatched = 0; + long exactMatched = 0; + long sameSelectedMatched = 0; + long equivalentMatched = 0; + long harmfulMatched = 0; + long suppressedMatched = 0; var matchLogs = new List(); foreach (var rule in rules.OrderByDescending(x => x.Confidence).ThenBy(x => x.Id)) { - string where = BuildRuleWhere(rule); - if (string.IsNullOrWhiteSpace(where)) - continue; - - double adjustment = rule.AppliedPredictionSpaceAdjustmentKld; - if (Math.Abs(adjustment) <= 0d) - continue; - - long before = await CountMatchesAsync(c, where, ct); - if (before == 0) + if (!IsRuleUsable(rule)) continue; - var beforeStats = await LoadPredictionStatsAsync(c, where, ct); + var tierResults = new List(); + tierResults.Add(await ApplyRuleTierAsync( + c, + rule, + SynergyTemplateMatchTier.ExactContext, + Config.SynergyDetection.ExactContextConfidenceMultiplier, + ct)); - string expression = adjustment < 0d - ? $"GREATEST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), -LEAST({SqlDouble(Config.AnomalyDetection.MaxNegativeAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))" - : $"LEAST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))"; - - await ExecuteAsync(c, $@" -UPDATE {CombinationDuckDbSchema.TableName} -SET AnomalyAdjustmentKld = {expression}, - FinalPredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}), - PredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}) -WHERE {where};", ct); - - var afterStats = await LoadPredictionStatsAsync(c, where, ct); - - totalMatched += before; - var actual = ExtractActualEffect(rule); - var log = new + if (Config.SynergyDetection.Enabled) + { + tierResults.Add(await ApplyRuleTierAsync( + c, + rule, + SynergyTemplateMatchTier.SameSelectedGroups, + Config.SynergyDetection.SameSelectedGroupsConfidenceMultiplier, + ct)); + + tierResults.Add(await ApplyRuleTierAsync( + c, + rule, + SynergyTemplateMatchTier.EquivalentQuantFamily, + Config.SynergyDetection.EquivalentQuantFamilyConfidenceMultiplier, + ct)); + } + + foreach (var tier in tierResults.Where(x => x.MatchedRows > 0)) + { + totalMatched += tier.MatchedRows; + if (tier.Tier == SynergyTemplateMatchTier.ExactContext) exactMatched += tier.MatchedRows; + if (tier.Tier == SynergyTemplateMatchTier.SameSelectedGroups) sameSelectedMatched += tier.MatchedRows; + if (tier.Tier == SynergyTemplateMatchTier.EquivalentQuantFamily) equivalentMatched += tier.MatchedRows; + if (rule.RuleDirection == AnomalyRuleDirection.Harmful.ToString()) harmfulMatched += tier.MatchedRows; + if (rule.RuleDirection == AnomalyRuleDirection.SuppressionOnly.ToString()) suppressedMatched += tier.MatchedRows; + + matchLogs.Add(new + { + ruleId = rule.Id, + templateType = ResolveTemplateType(rule), + direction = rule.RuleDirection, + tier = tier.Tier.ToString(), + tierMultiplier = tier.Multiplier, + referenceQuant = SafeName(rule.ReferenceQuantId), + groupSetHash = rule.GroupSetHash, + exactContextMatches = tier.Tier == SynergyTemplateMatchTier.ExactContext ? tier.MatchedRows : 0, + sameSelectedGroupMatches = tier.Tier == SynergyTemplateMatchTier.SameSelectedGroups ? tier.MatchedRows : 0, + equivalentQuantFamilyMatches = tier.Tier == SynergyTemplateMatchTier.EquivalentQuantFamily ? tier.MatchedRows : 0, + totalAdjustedRows = tier.MatchedRows, + basePredictedKld = tier.Before.AverageBasePredictedKld, + adjustedPredictedKld = tier.After.AverageFinalPredictedKld, + averageAdjustment = tier.After.AverageAnomalyAdjustmentKld, + actualCandidateKld = ExtractActualEffect(rule).CandidateKld, + actualTwinKld = ExtractActualEffect(rule).TwinKld, + actualGainOrHarm = ExtractActualEffect(rule).GainOrHarm, + adjustmentReason = "prediction-space-virtual-twin-rank-movement", + confidence = rule.Confidence, + effectiveConfidence = tier.EffectiveConfidence, + candidatePredicateReason = tier.CandidatePredicateReason, + virtualTwinPredicateReason = tier.VirtualTwinPredicateReason, + groups = rule.GroupStates + .OrderBy(x => x.SortOrder) + .Select(x => new + { + x.TensorGroupId, + group = ColumnNameForGroupId(x.TensorGroupId), + candidate = SafeName(x.CandidateQuantId), + reference = SafeName(x.ReferenceQuantId), + x.Movement + }) + .ToList() + }); + + AnsiConsole.MarkupLine( + $"[green]Applying synergy template:[/] template=[cyan]{Markup.Escape(DescribeRule(rule))}[/] tier=[cyan]{tier.Tier}[/] direction=[cyan]{Markup.Escape(rule.RuleDirection)}[/] " + + $"basePredictedKld=[cyan]{tier.Before.AverageBasePredictedKld:0.000000}[/] adjustedPredictedKld=[cyan]{tier.After.AverageFinalPredictedKld:0.000000}[/] " + + $"avgAdjustment=[cyan]{tier.After.AverageAnomalyAdjustmentKld:0.000000}[/] reason=[cyan]prediction-space-virtual-twin-rank-movement[/] matched DuckDB rows=[cyan]{tier.MatchedRows:N0}[/]"); + } + + if (tierResults.All(x => x.MatchedRows == 0)) { - ruleId = rule.Id, - direction = rule.RuleDirection, - ruleType = rule.RuleType, - referenceQuant = SafeName(rule.ReferenceQuantId), - groupSetHash = rule.GroupSetHash, - basePredictedKld = beforeStats.AverageBasePredictedKld, - adjustment, - adjustedPredictedKld = afterStats.AverageFinalPredictedKld, - actualCandidateKld = actual.CandidateKld, - actualTwinKld = actual.TwinKld, - actualGainOrHarm = actual.GainOrHarm, - adjustmentReason = actual.HasActualEffect ? "measured-actual-counterfactual-effect" : "prediction-space-gap-fallback", - matchedRows = before, - confidence = rule.Confidence, - groups = rule.GroupStates - .OrderBy(x => x.SortOrder) - .Select(x => new - { - x.TensorGroupId, - candidate = SafeName(x.CandidateQuantId), - reference = SafeName(x.ReferenceQuantId), - x.Movement - }) - .ToList() - }; - matchLogs.Add(log); - - AnsiConsole.MarkupLine( - $"[green]Applying anomaly rule:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] direction=[cyan]{Markup.Escape(rule.RuleDirection)}[/] " + - $"basePredictedKld=[cyan]{beforeStats.AverageBasePredictedKld:0.000000}[/] adjustment=[cyan]{adjustment:0.000000}[/] " + - $"adjustedPredictedKld=[cyan]{afterStats.AverageFinalPredictedKld:0.000000}[/] " + - $"actualCandidateKld=[cyan]{FmtNullable(actual.CandidateKld)}[/] actualTwinKld=[cyan]{FmtNullable(actual.TwinKld)}[/] " + - $"actualGainOrHarm=[cyan]{FmtNullable(actual.GainOrHarm)}[/] reason=[cyan]{Markup.Escape(actual.HasActualEffect ? "measured-actual-counterfactual-effect" : "prediction-space-gap-fallback")}[/] " + - $"matched DuckDB rows=[cyan]{before:N0}[/]"); + matchLogs.Add(new + { + ruleId = rule.Id, + templateType = ResolveTemplateType(rule), + direction = rule.RuleDirection, + referenceQuant = SafeName(rule.ReferenceQuantId), + groupSetHash = rule.GroupSetHash, + exactContextMatches = 0, + sameSelectedGroupMatches = 0, + equivalentQuantFamilyMatches = 0, + totalAdjustedRows = 0, + reason = "No DuckDB rows matched this transferable synergy template. Either the search space does not contain the selected group states, virtual raised twins were not generated/predictable, active groups were sparse/native-exact, or confidence/generalization thresholds blocked the tier." + }); + } } await ReRankAsync(c, ct); @@ -114,51 +151,281 @@ await ExecuteAsync(c, $@" { AppliedRuleCount = rules.Count, MatchedRowCount = totalMatched, + ExactContextMatches = exactMatched, + SameSelectedGroupMatches = sameSelectedMatched, + EquivalentQuantFamilyMatches = equivalentMatched, + SuppressedMatches = suppressedMatched, + HarmfulMatches = harmfulMatched, DuckDbPath = _store.GetDatabaseFilePath(), RuleMatches = matchLogs }; } + private static async Task ApplyRuleTierAsync( + DuckDBConnection c, + AnomalyInteractionRule rule, + SynergyTemplateMatchTier tier, + double tierMultiplier, + CancellationToken ct) + { + var empty = new RuleTierApplyResult( + tier, + 0, + tierMultiplier, + 0d, + new PredictionMatchStats(0d, 0d, 0d), + new PredictionMatchStats(0d, 0d, 0d), + string.Empty, + string.Empty); + + if (tierMultiplier <= 0d) + return empty; + + double effectiveConfidence = rule.Confidence * tierMultiplier; + if (effectiveConfidence < Config.SynergyDetection.MinConfidenceToApplyAdjustment) + return empty with { CandidatePredicateReason = "BlockedByMinSynergyConfidence" }; + + var matchSql = BuildRuleMatchSubquery(rule, tier, out var candidateReason, out var twinReason); + if (string.IsNullOrWhiteSpace(matchSql)) + return empty with { CandidatePredicateReason = candidateReason, VirtualTwinPredicateReason = twinReason }; + + long before = await CountMatchesAsync(c, matchSql, ct); + if (before == 0) + return empty with { CandidatePredicateReason = candidateReason, VirtualTwinPredicateReason = twinReason }; + + var beforeStats = await LoadPredictionStatsAsync(c, matchSql, ct); + string signedMagnitude = BuildAdjustmentMagnitudeSql(rule, effectiveConfidence); + + await ExecuteAsync(c, $@" +WITH matches AS ( +{matchSql} +), calculated AS ( + SELECT {CombinationDuckDbSchema.SlotColumnList}, + {signedMagnitude} AS Delta + FROM matches +) +UPDATE {CombinationDuckDbSchema.TableName} t +SET AnomalyAdjustmentKld = CASE + WHEN calculated.Delta < 0 THEN GREATEST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, -LEAST({SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld)})) + ELSE LEAST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)})) + END, + FinalPredictedKld = GREATEST(0.0, COALESCE(t.BaseRankSafeKld, t.PredictedKld, 0.0) + CASE + WHEN calculated.Delta < 0 THEN GREATEST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, -LEAST({SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld)})) + ELSE LEAST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)})) + END), + PredictedKld = GREATEST(0.0, COALESCE(t.BaseRankSafeKld, t.PredictedKld, 0.0) + CASE + WHEN calculated.Delta < 0 THEN GREATEST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, -LEAST({SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld)})) + ELSE LEAST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)})) + END) +FROM calculated +WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "calculated")};", ct); + + var afterStats = await LoadPredictionStatsAsync(c, matchSql, ct); + return new RuleTierApplyResult(tier, before, tierMultiplier, effectiveConfidence, beforeStats, afterStats, candidateReason, twinReason); + } - private static string BuildRuleWhere(AnomalyInteractionRule rule) + private static string BuildAdjustmentMagnitudeSql(AnomalyInteractionRule rule, double effectiveConfidence) { + string baseGap = "(COALESCE(CandidateBaseRankSafeKld, CandidatePredictedKld, 0.0) - COALESCE(VirtualTwinBaseRankSafeKld, VirtualTwinPredictedKld, COALESCE(CandidateBaseRankSafeKld, CandidatePredictedKld, 0.0)))"; + string magnitude = $"(GREATEST({baseGap}, 0.0) + {SqlDouble(Config.AnomalyDetection.PredictionSpaceViolationMargin)}) * {SqlDouble(Config.AnomalyDetection.AnomalyAdjustmentShrinkFactor)} * {SqlDouble(effectiveConfidence)}"; + + if (rule.RuleDirection == AnomalyRuleDirection.Harmful.ToString()) + return $"LEAST({magnitude}, {SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)})"; + + return $"-LEAST({magnitude}, {SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentKld)})"; + } + + private static string BuildRuleMatchSubquery( + AnomalyInteractionRule rule, + SynergyTemplateMatchTier tier, + out string candidateReason, + out string virtualTwinReason) + { + candidateReason = tier switch + { + SynergyTemplateMatchTier.ExactContext => "Full explicit discovery context must match the confirmed dome probe.", + SynergyTemplateMatchTier.SameSelectedGroups => "Candidate must contain the same selected group states as the confirmed synergy template.", + SynergyTemplateMatchTier.EquivalentQuantFamily => "Candidate selected groups must use a related quant family/tier.", + _ => "Unsupported tier." + }; + virtualTwinReason = "Virtual twin is constructed by raising only selected groups to their counterfactual reference quant while preserving all other effective group states."; + if (rule.GroupStates.Count == 0) return string.Empty; if (BaselineQuants.IsNativeExactAlias(rule.ReferenceQuantId) || rule.GroupStates.Any(x => BaselineQuants.IsNativeExactAlias(x.CandidateQuantId) || BaselineQuants.IsNativeExactAlias(x.ReferenceQuantId))) { + candidateReason = "BF16/native/exact template states are not valid contextual synergy templates."; return string.Empty; } - var states = rule.GroupStates.ToDictionary(x => x.TensorGroupId, x => x.CandidateQuantId); + var selected = rule.GroupStates.OrderBy(x => x.SortOrder).ToList(); var predicates = new List { - CombinationDuckDbSchema.ActiveCandidatePredicateSql, - "BaseRankSafeKld IS NOT NULL", - $"BaseQuant = {rule.ReferenceQuantId}" + "t.BaseRankSafeKld IS NOT NULL", + "t.PredictedSizeBytes IS NOT NULL", + "COALESCE(t.IsProtectedAnchor, FALSE) = FALSE" + }; + + // Keep transfer controlled for now: a Q8-dome template applies inside rows with the same base/reference quant. + predicates.Add($"t.BaseQuant = {rule.ReferenceQuantId}"); + + foreach (var state in selected) + { + string? column = ColumnNameForGroupId(state.TensorGroupId); + if (column == null) + return string.Empty; + + if (tier == SynergyTemplateMatchTier.EquivalentQuantFamily) + { + var equivalentIds = EquivalentQuantIds(state.CandidateQuantId); + if (equivalentIds.Count == 0) + return string.Empty; + + predicates.Add($"{EffectiveSql("t", column)} IN ({string.Join(",", equivalentIds.Select(x => x.ToString(CultureInfo.InvariantCulture)))})"); + } + else + { + predicates.Add($"{EffectiveSql("t", column)} = {state.CandidateQuantId}"); + } + } + + if (tier == SynergyTemplateMatchTier.ExactContext) + { + var states = selected.ToDictionary(x => x.TensorGroupId, x => x.CandidateQuantId); + foreach (var group in ActiveGroups()) + { + string? column = ColumnNameForGroupId(group.UniqueId); + if (column == null) + return string.Empty; + + byte expected = states.TryGetValue(group.UniqueId, out var q) ? q : rule.ReferenceQuantId; + predicates.Add($"{EffectiveSql("t", column)} = {expected}"); + } + } + else + { + // Transfer tiers are deliberately weaker than exact context. Keep the + // original discovery row out of transfer-tier matching so it does not + // receive duplicate exact + generalized adjustments. + var selectedGroupIds = selected.Select(x => x.TensorGroupId).ToHashSet(); + var surroundingDifferencePredicates = new List(); + + foreach (var group in ActiveGroups()) + { + if (selectedGroupIds.Contains(group.UniqueId)) + continue; + + string? column = ColumnNameForGroupId(group.UniqueId); + if (column == null) + return string.Empty; + + surroundingDifferencePredicates.Add($"{EffectiveSql("t", column)} <> {rule.ReferenceQuantId}"); + } + + if (surroundingDifferencePredicates.Count > 0) + predicates.Add("(" + string.Join(" OR ", surroundingDifferencePredicates) + ")"); + } + + if (tier == SynergyTemplateMatchTier.EquivalentQuantFamily) + { + // Equivalent-family matching must be meaningfully broader than exact + // same-selected-group matching; otherwise the same rows receive both + // transfer tiers. Require at least one selected group to use a related + // non-identical quant family member. + var selectedQuantDifferencePredicates = new List(); + foreach (var state in selected) + { + string? column = ColumnNameForGroupId(state.TensorGroupId); + if (column == null) + return string.Empty; + + selectedQuantDifferencePredicates.Add($"{EffectiveSql("t", column)} <> {state.CandidateQuantId}"); + } + + if (selectedQuantDifferencePredicates.Count > 0) + predicates.Add("(" + string.Join(" OR ", selectedQuantDifferencePredicates) + ")"); + } + + string virtualTwinJoin = BuildVirtualTwinJoinPredicate(selected); + if (string.IsNullOrWhiteSpace(virtualTwinJoin)) + return string.Empty; + + return $@" SELECT t.{CombinationDuckDbSchema.SlotColumnList.Replace(", ", ", t.")}, + COALESCE(t.BaseRankSafeKld, t.PredictedKld) AS CandidateBaseRankSafeKld, + COALESCE(t.PredictedKld, t.BaseRankSafeKld) AS CandidatePredictedKld, + COALESCE(vt.BaseRankSafeKld, vt.PredictedKld) AS VirtualTwinBaseRankSafeKld, + COALESCE(vt.PredictedKld, vt.BaseRankSafeKld) AS VirtualTwinPredictedKld + FROM {CombinationDuckDbSchema.TableName} t + JOIN {CombinationDuckDbSchema.TableName} vt ON {virtualTwinJoin} + WHERE {string.Join(" AND ", predicates)}"; + } + + private static string BuildVirtualTwinJoinPredicate(IReadOnlyList selected) + { + var selectedMap = selected.ToDictionary(x => x.TensorGroupId, x => x.ReferenceQuantId); + var predicates = new List + { + "vt.BaseQuant = t.BaseQuant", + "vt.BaseRankSafeKld IS NOT NULL" }; - // Match against the full normalized effective active vector. Sparse DuckDB rows - // may still exist from the normal search space, so matching normalizes NULL slot - // value 0 to BaseQuant, while explicit contextual probe/rule persistence remains - // strict and never stores sparse anomaly identities. foreach (var group in ActiveGroups()) { string? column = ColumnNameForGroupId(group.UniqueId); if (column == null) return string.Empty; - byte expectedQuantId = states.TryGetValue(group.UniqueId, out var candidateQuantId) - ? candidateQuantId - : rule.ReferenceQuantId; - - predicates.Add($"(CASE WHEN {column} = 0 THEN BaseQuant ELSE CAST({column} AS INTEGER) - 1 END) = {expectedQuantId}"); + if (selectedMap.TryGetValue(group.UniqueId, out var referenceQuantId)) + predicates.Add($"{EffectiveSql("vt", column)} = {referenceQuantId}"); + else + predicates.Add($"{EffectiveSql("vt", column)} = {EffectiveSql("t", column)}"); } return string.Join(" AND ", predicates); } + private static string EffectiveSql(string alias, string column) => + $"(CASE WHEN {alias}.{column} = 0 THEN {alias}.BaseQuant ELSE CAST({alias}.{column} AS INTEGER) - 1 END)"; + + private static IReadOnlyList EquivalentQuantIds(byte quantId) + { + int tier = EffectiveTier(quantId); + if (tier < 0) + return Array.Empty(); + + return BaselineQuants.GetAllRecognizedBaselines() + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .Where(x => EffectiveTier(x.UniqueId) == tier) + .Select(x => x.UniqueId) + .Distinct() + .OrderBy(x => x) + .ToList(); + } + + private static bool IsRuleUsable(AnomalyInteractionRule rule) + { + if (rule.GroupStates.Count == 0) + return false; + + if (rule.RuleStatus != AnomalyRuleStatus.Confirmed.ToString()) + return false; + + if (rule.RuleDirection != AnomalyRuleDirection.Beneficial.ToString() && + rule.RuleDirection != AnomalyRuleDirection.Harmful.ToString()) + { + return false; + } + + if (BaselineQuants.IsNativeExactAlias(rule.ReferenceQuantId)) + return false; + + return rule.GroupStates.All(x => + !BaselineQuants.IsNativeExactAlias(x.CandidateQuantId) && + !BaselineQuants.IsNativeExactAlias(x.ReferenceQuantId)); + } + private static IReadOnlyList ActiveGroups() { TensorGroup[] ordered = @@ -197,8 +464,8 @@ private static IReadOnlyList ActiveGroups() private static async Task ReRankAsync(DuckDBConnection c, CancellationToken ct) { await ExecuteAsync(c, $@" -DROP TABLE IF EXISTS temp_anomaly_rerank; -CREATE TEMP TABLE temp_anomaly_rerank AS +DROP TABLE IF EXISTS temp_synergy_rerank; +CREATE TEMP TABLE temp_synergy_rerank AS SELECT {CombinationDuckDbSchema.SlotColumnList}, CAST(ROW_NUMBER() OVER ( ORDER BY COALESCE(FinalPredictedKld, PredictedKld) ASC, @@ -223,32 +490,32 @@ AND PredictionConfidence IS NOT NULL UPDATE {CombinationDuckDbSchema.TableName} t SET PredictionRank = r.NewPredictionRank -FROM temp_anomaly_rerank r +FROM temp_synergy_rerank r WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "r")};", ct); } - private static async Task CountMatchesAsync(DuckDBConnection c, string where, CancellationToken ct) + private static async Task CountMatchesAsync(DuckDBConnection c, string matchSql, CancellationToken ct) { using var cmd = c.CreateCommand(); - cmd.CommandText = $"SELECT COUNT(*) FROM {CombinationDuckDbSchema.TableName} WHERE {where};"; + cmd.CommandText = $"SELECT COUNT(*) FROM ({matchSql}) q;"; return ToInt64(await cmd.ExecuteScalarAsync(ct)); } - - private static async Task LoadPredictionStatsAsync(DuckDBConnection c, string where, CancellationToken ct) + private static async Task LoadPredictionStatsAsync(DuckDBConnection c, string matchSql, CancellationToken ct) { using var cmd = c.CreateCommand(); cmd.CommandText = $@" -SELECT AVG(COALESCE(BaseRankSafeKld, PredictedKld)), - AVG(COALESCE(FinalPredictedKld, PredictedKld)) -FROM {CombinationDuckDbSchema.TableName} -WHERE {where};"; +SELECT AVG(COALESCE(t.BaseRankSafeKld, t.PredictedKld)), + AVG(COALESCE(t.FinalPredictedKld, t.PredictedKld)), + AVG(COALESCE(t.AnomalyAdjustmentKld, 0.0)) +FROM {CombinationDuckDbSchema.TableName} t +JOIN ({matchSql}) m ON {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "m")};"; using var r = await cmd.ExecuteReaderAsync(ct); if (!await r.ReadAsync(ct)) - return new PredictionMatchStats(0d, 0d); + return new PredictionMatchStats(0d, 0d, 0d); - return new PredictionMatchStats(ToDouble(r.GetValue(0)), ToDouble(r.GetValue(1))); + return new PredictionMatchStats(ToDouble(r.GetValue(0)), ToDouble(r.GetValue(1)), ToDouble(r.GetValue(2))); } private static ActualRuleEffect ExtractActualEffect(AnomalyInteractionRule rule) @@ -271,6 +538,17 @@ private static ActualRuleEffect ExtractActualEffect(AnomalyInteractionRule rule) } } + private static string ResolveTemplateType(AnomalyInteractionRule rule) + { + return rule.RuleType switch + { + "SingleGroupInversion" => "CounterfactualSynergy.SingleGroupInversion", + "PairSynergy" => "CounterfactualSynergy.PairSynergy", + "HigherOrderSynergy" => "CounterfactualSynergy.HigherOrderSynergy", + _ => $"CounterfactualSynergy.{rule.RuleType}" + }; + } + private static double? TryGetDouble(JsonElement element, string propertyName) { return element.TryGetProperty(propertyName, out var value) && value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var d) @@ -278,8 +556,6 @@ private static ActualRuleEffect ExtractActualEffect(AnomalyInteractionRule rule) : null; } - private static string FmtNullable(double? value) => value.HasValue ? value.Value.ToString("0.000000") : "n/a"; - private static double ToDouble(object? value) { if (value is null || value is DBNull) @@ -288,7 +564,7 @@ private static double ToDouble(object? value) if (value is BigInteger big) return (double)big; - return Convert.ToDouble(value); + return Convert.ToDouble(value, CultureInfo.InvariantCulture); } private static long ToInt64(object? value) @@ -299,7 +575,7 @@ private static long ToInt64(object? value) if (value is BigInteger big) return (long)big; - return Convert.ToInt64(value); + return Convert.ToInt64(value, CultureInfo.InvariantCulture); } private static async Task ExecuteAsync(DuckDBConnection c, string sql, CancellationToken ct) @@ -315,18 +591,45 @@ private static async Task ConfigureSessionAsync(DuckDBConnection c, Cancellation await ExecuteAsync(c, $"SET threads = {Math.Max(1, Environment.ProcessorCount)};", ct); } - private static string SqlDouble(double value) => value.ToString(System.Globalization.CultureInfo.InvariantCulture); + private static string SqlDouble(double value) => value.ToString(CultureInfo.InvariantCulture); private static string DescribeRule(AnomalyInteractionRule rule) { return string.Join(" + ", rule.GroupStates .OrderBy(x => x.SortOrder) .Select(x => $"{ColumnNameForGroupId(x.TensorGroupId)}={SafeName(x.CandidateQuantId)}")) + - $" in {SafeName(rule.ReferenceQuantId)} context"; + $" transferred from {SafeName(rule.ReferenceQuantId)} dome"; + } + + private static int EffectiveTier(byte quantId) + { + if (BaselineQuants.IsNativeExactAlias(quantId)) + return 160; + + var baseline = BaselineQuants.FromId(quantId); + string name = baseline.Names[0].ToUpperInvariant(); + + if (name.Contains("Q8") || baseline.BitRange >= 8) return 80; + if (name.Contains("Q6") || baseline.BitRange == 6) return 60; + if (name.Contains("Q5") || baseline.BitRange == 5) return 50; + if (name.Contains("Q4") || name.Contains("IQ4") || baseline.BitRange == 4) return 40; + if (name.Contains("Q3") || name.Contains("IQ3") || baseline.BitRange == 3) return 30; + if (name.Contains("Q2") || name.Contains("IQ2") || baseline.BitRange == 2) return 20; + + return baseline.BitRange > 0 ? baseline.BitRange * 10 : -1; } - private readonly record struct PredictionMatchStats(double AverageBasePredictedKld, double AverageFinalPredictedKld); + private readonly record struct PredictionMatchStats(double AverageBasePredictedKld, double AverageFinalPredictedKld, double AverageAnomalyAdjustmentKld); private readonly record struct ActualRuleEffect(double? CandidateKld, double? TwinKld, double? GainOrHarm, bool HasActualEffect); + private readonly record struct RuleTierApplyResult( + SynergyTemplateMatchTier Tier, + long MatchedRows, + double Multiplier, + double EffectiveConfidence, + PredictionMatchStats Before, + PredictionMatchStats After, + string CandidatePredicateReason, + string VirtualTwinPredicateReason); private static string SafeName(byte quantId) { @@ -339,4 +642,4 @@ private static string SafeName(byte quantId) return $"id:{quantId}"; } } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/AnomalyRuleRepository.cs b/MagicQuant/Services/AnomalyRuleRepository.cs index f0fe547..8a7f31c 100644 --- a/MagicQuant/Services/AnomalyRuleRepository.cs +++ b/MagicQuant/Services/AnomalyRuleRepository.cs @@ -193,6 +193,10 @@ public async Task> UpsertRulesFromResultsA CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions), InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), + ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), + CandidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), + ReferenceInternalName = TensorConfigIdentity.ToKey(first.Plan.ReferenceConfig), + CandidateInternalName = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), RuleDirection = direction, GroupSetHash = groupSetHash, CreatedUtc = DateTime.UtcNow @@ -221,24 +225,38 @@ public async Task> UpsertRulesFromResultsA rule.CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions); rule.InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions); rule.FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig); + rule.ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig); + rule.CandidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig); + rule.ReferenceInternalName = TensorConfigIdentity.ToKey(first.Plan.ReferenceConfig); + rule.CandidateInternalName = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig); rule.UpdatedUtc = DateTime.UtcNow; rule.MetadataJson = JsonSerializer.Serialize(new { - source = "counterfactual-twin-probe", + source = "counterfactual-synergy-template", + terminology = "SynergyTemplate/CounterfactualSynergy. Existing Anomaly* entity names are retained for compatibility.", isContextualAnomalyProbe = true, oldBf16Isolation = false, allActiveGroupsExplicit = true, + templateType = ResolveRuleType(rows), + generalizationPolicy = "ExactStrong_TransferWeak", + selectedGroupStates = probeGroups.ToDictionary(x => x.Group.Name, x => BaselineQuants.FromId(x.CandidateQuantId).Names[0]), + raisedCounterfactualStates = probeGroups.ToDictionary(x => x.Group.Name, x => BaselineQuants.FromId(x.ReferenceQuantId).Names[0]), + discoveryContext = _movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), + candidateContext = _movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), inactiveGroups = _movement.BuildInactiveGroupList(), + referenceTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ReferenceConfig), + candidateTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), + referenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), + candidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), first.Plan.ProbeType, first.Plan.HypothesisLabel, actualCandidateKld = first.ProbeSnapshot?.Kld, actualTwinKld = first.ReferenceSnapshot?.Kld, actualGainOrHarm = first.ActualGainVsTwin, - adjustmentReason = first.ReferenceSnapshot != null && first.ProbeSnapshot != null - ? "measured-actual-counterfactual-effect" - : "prediction-space-gap-fallback", + sizeSavingsBytes = ComputeSizeSavings(first.ReferenceSnapshot, first.ProbeSnapshot), + adjustmentReason = "prediction-space-virtual-twin-rank-movement", groups = probeGroups.Select(ToGroupLog).ToList() }, JsonOptions); @@ -419,20 +437,9 @@ private static double ComputePredictionAdjustment(AnomalyProbeResult result, dou { var cfg = Config.AnomalyDetection; - // Prefer measured counterfactual effect. Predicted KLD is a rank-space signal, - // not the same numeric quantity as actual benchmark KLD, so a confirmed probe - // should not be converted through a giant predicted-gap correction. - if (result.ReferenceSnapshot != null && result.ProbeSnapshot != null) - { - double measured = result.ReferenceSnapshot.Kld - result.ProbeSnapshot.Kld; - if (result.RuleDirection == AnomalyRuleDirection.Beneficial && measured > 0d) - return -Math.Min(measured * cfg.AnomalyAdjustmentShrinkFactor, cfg.MaxNegativeAdjustmentKld); - - if (result.RuleDirection == AnomalyRuleDirection.Harmful && measured < 0d) - return Math.Min(Math.Abs(measured) * cfg.AnomalyAdjustmentShrinkFactor, cfg.MaxPositiveAdjustmentKld); - } - - // Fallback only for legacy rows without measured twin/probe truth. + // Store a conservative fallback only. Runtime application now uses virtual same-context + // twins and computes row-local prediction-space movement. Actual KLD is preserved in + // metadata/evidence, but is not pasted directly into predicted KLD. double baseGap = result.Plan.Seed.PredictionSpaceGapVsTwin; double required = result.RuleDirection switch { @@ -443,7 +450,7 @@ private static double ComputePredictionAdjustment(AnomalyProbeResult result, dou double adjusted = required * confidence * cfg.AnomalyAdjustmentShrinkFactor; if (adjusted < 0d) - return Math.Max(adjusted, -cfg.MaxNegativeAdjustmentKld); + return Math.Max(adjusted, -Config.SynergyDetection.MaxNegativeAdjustmentKld); return Math.Min(adjusted, cfg.MaxPositiveAdjustmentKld); } diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs index 807acc4..862e16a 100644 --- a/MagicQuant/Services/AnomalyWorkflowService.cs +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -54,7 +54,7 @@ public async Task RunAsync( return new AnomalyRunResult(); } - AnsiConsole.Write(new Rule("[yellow]Counterfactual Anomaly Smoke / Probe Pass[/]") { Justification = Justify.Left }); + AnsiConsole.Write(new Rule("[yellow]Counterfactual Synergy Smoke / Probe Pass[/]") { Justification = Justify.Left }); var session = await _rules.StartSessionAsync("prediction-guided-selection", ct); try @@ -82,6 +82,15 @@ public async Task RunAsync( }, ct); await WriteJsonAsync("magicquant-anomaly-seeds.json", smoke.Select(ToSmokeLog).ToList(), ct); + await WriteJsonAsync("magicquant-synergy-smoke-scan.json", new + { + generatedAtUtc = DateTime.UtcNow, + historicalCount = historical.Count, + duckPredictionSpaceCount = duck.Count, + selectedSmokeCount = smoke.Count, + duckDiagnostics = _lastDuckSmokeDiagnostics, + smoke = smoke.Select(ToSmokeLog).ToList() + }, ct); var planningDiagnostics = new ProbePlanningDiagnostics(); var probes = await PlanProbesAsync(smoke, planningDiagnostics, ct); @@ -96,12 +105,32 @@ public async Task RunAsync( results = results.Concat(expansionResults).ToList(); } + var transferProbes = await PlanConfirmedSynergyTransferProbesAsync(results, planningDiagnostics, ct); + if (transferProbes.Count > 0) + { + probes = probes.Concat(transferProbes).ToList(); + var transferResults = await ValidateProbesAsync(transferProbes, ct); + results = results.Concat(transferResults).ToList(); + } + await WriteJsonAsync("magicquant-anomaly-probes.json", new { generatedAtUtc = DateTime.UtcNow, planningDiagnostics, probes = probes.Select(ToProbeLog).ToList() }, ct); + await WriteJsonAsync("magicquant-synergy-probes.json", new + { + generatedAtUtc = DateTime.UtcNow, + planningDiagnostics, + probes = probes.Select(ToProbeLog).ToList() + }, ct); + await WriteJsonAsync("magicquant-synergy-transfer-probes.json", new + { + generatedAtUtc = DateTime.UtcNow, + planningDiagnostics, + probes = transferProbes.Select(ToProbeLog).ToList() + }, ct); await _rules.PersistProbeResultsAsync(session.Id, results, ct); var upsertedRules = await _rules.UpsertRulesFromResultsAsync(results, ct); @@ -116,8 +145,15 @@ public async Task RunAsync( upserted = upsertedRules.Select(ToRuleLog).ToList(), applicable = applicableRules.Select(ToRuleLog).ToList() }, ct); + await WriteJsonAsync("magicquant-synergy-templates.json", new + { + generatedAtUtc = DateTime.UtcNow, + upserted = upsertedRules.Select(ToSynergyTemplateLog).ToList(), + applicable = applicableRules.Select(ToSynergyTemplateLog).ToList() + }, ct); await WriteJsonAsync("magicquant-anomaly-adjusted-predictions-summary.json", adjustment, ct); + await WriteJsonAsync("magicquant-synergy-adjusted-predictions-summary.json", adjustment, ct); await WriteFinalManifestAsync("magicquant.anomalies.json", new { generatedAtUtc = DateTime.UtcNow, @@ -126,7 +162,21 @@ public async Task RunAsync( results = results.Select(ToResultLog).ToList(), rules = applicableRules.Select(ToRuleLog).ToList(), bestConfirmedAnomaly = bestAnomaly, - adjustment + adjustment, + synergyTerminology = "Anomaly entity names are retained for compatibility; confirmed beneficial rules are treated as transferable counterfactual synergy templates." + }, ct); + await WriteFinalManifestAsync("magicquant.synergy.json", new + { + generatedAtUtc = DateTime.UtcNow, + modeEnabled = Config.AnomalyDetection.Enabled && Config.SynergyDetection.Enabled, + confirmedBeneficialTemplates = applicableRules.Count(x => x.RuleDirection == AnomalyRuleDirection.Beneficial.ToString()), + harmfulInteractions = applicableRules.Count(x => x.RuleDirection == AnomalyRuleDirection.Harmful.ToString()), + suppressionOnlyObservations = results.Count(x => x.RuleDirection == AnomalyRuleDirection.SuppressionOnly), + bestConfirmedSynergy = bestAnomaly, + transferProbesQueued = planningDiagnostics.TransferProbesQueued, + templates = applicableRules.Select(ToSynergyTemplateLog).ToList(), + adjustment, + note = "Q8 remains the discovery dome/control context. Confirmed counterfactual wins are persisted as transferable synergy templates and applied through virtual same-context twins with confidence shrinkage. Physical validation remains final truth." }, ct); await WriteFinalManifestAsync("magicquant.prediction-audit.json", new { @@ -313,6 +363,7 @@ private async Task> DetectDuckSmokeAsync(Cancellatio var rejected = new List(); var closestGapFailures = new List(); var existingKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); + var confirmedTemplates = await _rules.LoadApplicableRulesAsync(ct); int skippedIsolation = 0; int skippedSparse = 0; @@ -323,6 +374,8 @@ private async Task> DetectDuckSmokeAsync(Cancellatio int skippedNoTwin = 0; int skippedSavings = 0; int skippedGap = 0; + int belowMinSmokeScore = 0; + int catastrophicGapRejected = 0; int contextualScanned = 0; int twinLookupCount = 0; int dictionaryTwinHits = 0; @@ -388,6 +441,8 @@ private async Task> DetectDuckSmokeAsync(Cancellatio var movement = _movement.Analyze(twin, row.Config); bool matchedConfirmedPattern = existingKeys.Contains(_rules.BuildRuleSuppressionKey(twin, movement.ChangedGroups)); + var matchedTemplateTier = ResolveConfirmedTemplateMatchTier(row.Config, confirmedTemplates, out var wouldMatchConfirmedTemplate); + matchedConfirmedPattern = matchedConfirmedPattern || wouldMatchConfirmedTemplate; if (movement.Classification == AnomalyMovementClassification.MixedTrade) { @@ -443,15 +498,28 @@ private async Task> DetectDuckSmokeAsync(Cancellatio } double gap = row.BaseRankSafeKld - twinRow.BaseRankSafeKld; + if (gap > Config.SynergyDetection.MaxSmokeGapKld) + { + catastrophicGapRejected++; + var preview = AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, "PredictionSpaceGapCatastrophic", matchedConfirmedPattern, true); + closestGapFailures.Add(preview); + continue; + } + if (gap > Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld) { skippedGap++; - var preview = AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, "PredictionSpaceGapTooLarge", matchedConfirmedPattern, true); + var preview = AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, "PredictionSpaceGapTooLargeButSmokeScored", matchedConfirmedPattern, true); closestGapFailures.Add(preview); - continue; } - double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank); + double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank, matchedConfirmedPattern, matchedTemplateTier); + if (score < Config.SynergyDetection.MinSmokeScore) + { + belowMinSmokeScore++; + AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, "BelowMinSmokeScore", matchedConfirmedPattern, true, score); + continue; + } result.Add(new AnomalySmokeCandidate { @@ -474,6 +542,8 @@ private async Task> DetectDuckSmokeAsync(Cancellatio SmokeStrength = gap <= 0d ? "Strong" : "Close", SeedClass = AnomalySeedClass.PredictionSpaceSmoke, MatchedConfirmedAnomalyPattern = matchedConfirmedPattern, + WouldMatchConfirmedTemplate = wouldMatchConfirmedTemplate, + SynergyMatchTier = matchedTemplateTier, Message = "Prediction-space contextual monotone downgrade candidate is close enough to its higher-bit quantized twin to justify probes. Twin lookup was dictionary-only from the preloaded DuckDB row set." }); } @@ -496,13 +566,15 @@ private async Task> DetectDuckSmokeAsync(Cancellatio MixedTradeIgnored = skippedMixed, SizeSavingsBelowThreshold = skippedSavings, PredictionSpaceGapTooLarge = skippedGap, + BelowMinSmokeScore = belowMinSmokeScore, + CatastrophicGapRejected = catastrophicGapRejected, QueuedSmokeCandidates = result.Count, LoadPredictedRowsMs = loadClock.ElapsedMilliseconds, BuildLookupDictionaryMs = lookupClock.ElapsedMilliseconds, ScanRowsMs = scanClock.ElapsedMilliseconds, RejectedPreview = rejected .OrderBy(x => x.SortOrder) - .Take(25) + .Take(Config.SynergyDetection.TopRejectedSmokePreview) .Select(x => x.ToLog()) .ToList(), ClosestGapFailures = closestGapFailures @@ -515,7 +587,7 @@ private async Task> DetectDuckSmokeAsync(Cancellatio _lastDuckSmokeDiagnostics = diagnostics; - AnsiConsole.MarkupLine("[yellow]DuckDB contextual smoke scan:[/]"); + AnsiConsole.MarkupLine("[yellow]DuckDB synergy smoke scan:[/]"); AnsiConsole.MarkupLine($"[grey] predicted rows scanned=[/] [cyan]{rows.Count:N0}[/]"); AnsiConsole.MarkupLine($"[grey] load predicted rows ms=[/] [cyan]{diagnostics.LoadPredictedRowsMs:N0}[/]"); AnsiConsole.MarkupLine($"[grey] build lookup dictionary ms=[/] [cyan]{diagnostics.BuildLookupDictionaryMs:N0}[/]"); @@ -532,12 +604,14 @@ private async Task> DetectDuckSmokeAsync(Cancellatio AnsiConsole.MarkupLine($"[grey] movement not monotone downgrade=[/] [cyan]{skippedMovement:N0}[/]"); AnsiConsole.MarkupLine($"[grey] mixed trade ignored=[/] [cyan]{skippedMixed:N0}[/]"); AnsiConsole.MarkupLine($"[grey] size savings below threshold=[/] [cyan]{skippedSavings:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] prediction-space gap too large=[/] [cyan]{skippedGap:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] prediction-space gap too large but smoke-scored=[/] [cyan]{skippedGap:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] catastrophic gap rejected=[/] [cyan]{catastrophicGapRejected:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] below min smoke score=[/] [cyan]{belowMinSmokeScore:N0}[/]"); AnsiConsole.MarkupLine($"[grey] queued smoke candidates=[/] [cyan]{result.Count:N0}[/]"); if (result.Count == 0 && rows.Count > 0) { - AnsiConsole.MarkupLine("[yellow]DuckDB contextual smoke scan produced zero candidates.[/] Top rejected-smoke previews and closest gap failures were written to magicquant-anomaly-smoke-scan-duckdb-diagnostics.json."); + AnsiConsole.MarkupLine("[yellow]DuckDB synergy smoke scan produced zero candidates.[/] Top rejected-smoke previews and closest gap failures were written to magicquant-anomaly-smoke-scan-duckdb-diagnostics.json."); foreach (var preview in closestGapFailures.OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue).Take(10)) { AnsiConsole.MarkupLine($"[grey] rejected monotone gap:[/] candidate={Markup.Escape(preview.CandidateName)} twin={Markup.Escape(preview.TwinName)} gap={FmtNullable(preview.PredictionSpaceGap)} savings={FmtNullable(preview.PredictedSizeSavingsBytes)} reason={Markup.Escape(preview.RejectionReason)} matchedRule={preview.MatchedConfirmedAnomalyPattern}"); @@ -548,6 +622,7 @@ private async Task> DetectDuckSmokeAsync(Cancellatio { generatedAtUtc = DateTime.UtcNow, diagnostics, + synergyTerminology = "DuckDB smoke uses counterfactual synergy scoring. Prediction-space gap is no longer the only cliff gate; catastrophic gaps are still rejected.", queued = result.Select(ToSmokeLog).ToList() }, ct); @@ -796,6 +871,195 @@ private async Task> PlanConfirmedAnomalyExpansionProbesAs return plans; } + + + private async Task> PlanConfirmedSynergyTransferProbesAsync( + IReadOnlyList currentResults, + ProbePlanningDiagnostics diagnostics, + CancellationToken ct) + { + var cfg = Config.SynergyDetection; + if (!cfg.Enabled || !cfg.TransferProbeEnabled || cfg.MaxTotalTransferProbesPerRun <= 0) + return new List(); + + var beneficialTemplates = currentResults + .Where(x => x.RuleDirection == AnomalyRuleDirection.Beneficial) + .Where(x => x.ReferenceSnapshot != null && x.ProbeSnapshot != null) + .Where(x => EstimateTemplateConfidence(x) >= cfg.MinConfidenceToScheduleTransferProbe) + .OrderByDescending(x => x.ActualGainVsTwin) + .ToList(); + + if (beneficialTemplates.Count == 0) + return new List(); + + var rows = await LoadPredictionRowsAsync(DuckSmokeScanLimit, ct); + var lookup = new Dictionary(StringComparer.Ordinal); + var candidates = new Dictionary(StringComparer.Ordinal); + foreach (var row in rows) + { + if (!_movement.TryNormalizeSparseDuckRowToActivatedContext(row.Config, out var activated, out _, out _)) + continue; + + var normalized = row with { Config = activated }; + AddOrPreferBetterPredictionRow(lookup, normalized); + if (TensorConfigIdentity.ToKey(activated) != TensorConfigIdentity.ToKey(_movement.BuildBaseContextTwin(activated))) + AddOrPreferBetterPredictionRow(candidates, normalized); + } + + var existingRuleKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); + var seen = new HashSet(StringComparer.Ordinal); + var plans = new List(); + + foreach (var template in beneficialTemplates) + { + if (plans.Count >= cfg.MaxTotalTransferProbesPerRun) + break; + + var selectedGroups = template.Plan.ProbeGroups + .Where(x => x.Movement == QuantMovementKind.Downgrade) + .OrderBy(x => x.Group.UniqueId) + .ToList(); + + if (selectedGroups.Count == 0) + continue; + + int perTemplate = 0; + var scored = new List<(PredictionDuckRow Candidate, PredictionDuckRow Twin, TensorConfig VirtualTwin, AnomalyMovementAnalysis Movement, double Gap, ulong SavingsBytes, double Score, string Stratum)>(); + foreach (var row in candidates.Values) + { + if (perTemplate >= cfg.MaxTransferProbesPerTemplate || plans.Count + scored.Count >= cfg.MaxTotalTransferProbesPerRun) + break; + + if (TensorConfigIdentity.ToKey(row.Config) == TensorConfigIdentity.ToKey(template.Plan.ProbeConfig)) + continue; + + if (row.Config.BaseQuant != template.Plan.ReferenceConfig.BaseQuant) + continue; + + bool containsSelected = selectedGroups.All(g => _movement.EffectiveQuantId(row.Config, g.Group) == g.CandidateQuantId); + if (!containsSelected) + continue; + + string stratum = ResolveTransferStratum(row.Config, selectedGroups, template.Plan.ReferenceConfig.BaseQuant); + if (stratum == "disabled-low-fidelity") + { + diagnostics.SkippedTransferStrata++; + continue; + } + + var virtualTwin = row.Config; + foreach (var group in selectedGroups) + virtualTwin = _movement.WithStoredSlot(virtualTwin, group.Group, group.ReferenceStoredSlot); + + if (ShouldSkipInvalidContextualAnomalyConfig(virtualTwin, "synergy-transfer-virtual-twin", out _) || + ShouldSkipInvalidContextualAnomalyConfig(row.Config, "synergy-transfer-candidate", out _)) + { + diagnostics.SkippedInvalidMovement++; + continue; + } + + string virtualTwinKey = TensorConfigIdentity.ToKey(virtualTwin); + if (!lookup.TryGetValue(virtualTwinKey, out var twinRow)) + { + diagnostics.SkippedMissingVirtualTwin++; + continue; + } + + var movement = _movement.Analyze(virtualTwin, row.Config); + if (movement.Classification != AnomalyMovementClassification.MonotoneDowngrade) + { + diagnostics.SkippedInvalidMovement++; + continue; + } + + if (existingRuleKeys.Contains(_rules.BuildRuleSuppressionKey(virtualTwin, selectedGroups))) + { + diagnostics.SkippedExistingRuleOrSuppression++; + continue; + } + + string seenKey = TensorConfigIdentity.ToKey(virtualTwin) + "=>" + TensorConfigIdentity.ToKey(row.Config); + if (!seen.Add(seenKey)) + { + diagnostics.SkippedDuplicate++; + continue; + } + + if (twinRow.PredictedSizeBytes <= row.PredictedSizeBytes) + continue; + + ulong savings = twinRow.PredictedSizeBytes - row.PredictedSizeBytes; + double savingsPercent = savings * 100d / Math.Max(1d, twinRow.PredictedSizeBytes); + double gap = row.BaseRankSafeKld - twinRow.BaseRankSafeKld; + if (gap > cfg.MaxSmokeGapKld) + continue; + + double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank, true, SynergyTemplateMatchTier.SameSelectedGroups); + scored.Add((row, twinRow, virtualTwin, movement, gap, savings, score, stratum)); + } + + foreach (var item in scored + .OrderByDescending(x => x.Score) + .ThenBy(x => x.Candidate.BaseRankSafeKld) + .ThenBy(x => x.Candidate.PredictedSizeBytes) + .Take(cfg.MaxTransferProbesPerTemplate)) + { + if (plans.Count >= cfg.MaxTotalTransferProbesPerRun) + break; + + var seed = new AnomalySmokeCandidate + { + Source = "synergy-transfer-smoke", + CandidateConfig = item.Candidate.Config, + TwinConfig = item.VirtualTwin, + Movement = item.Movement, + CandidatePredictedKld = item.Candidate.BaseRankSafeKld, + TwinPredictedKld = item.Twin.BaseRankSafeKld, + CandidatePredictedSizeBytes = item.Candidate.PredictedSizeBytes, + TwinPredictedSizeBytes = item.Twin.PredictedSizeBytes, + PredictedSizeSavingsBytes = item.SavingsBytes, + PlannedProbeWillMeasureSize = true, + TwinLookupMode = "virtual-same-context-twin-from-preloaded-duckdb-lookup", + TwinFoundInLookupDictionary = true, + PredictionSpaceGapVsTwin = item.Gap, + CandidatePredictionRank = item.Candidate.PredictionRank, + TwinPredictionRank = item.Twin.PredictionRank, + SmokeScore = item.Score, + SmokeStrength = "SynergyTransferProbe", + SeedClass = AnomalySeedClass.SynergyTransferProbe, + MatchedConfirmedAnomalyPattern = true, + WouldMatchConfirmedTemplate = true, + SynergyMatchTier = SynergyTemplateMatchTier.SameSelectedGroups, + IsTransferProbeSeed = true, + Message = $"Transfer probe from confirmed counterfactual synergy template. stratum={item.Stratum}." + }; + + plans.Add(new AnomalyProbePlan + { + Seed = seed, + ReferenceConfig = item.VirtualTwin, + ProbeConfig = item.Candidate.Config, + ProbeGroups = selectedGroups, + ProbeType = "synergy-transfer", + HypothesisLabel = _movement.DescribeGroups(selectedGroups), + SeedClass = AnomalySeedClass.SynergyTransferProbe, + ProbePriorityClass = AnomalySeedClass.SynergyTransferProbe + }); + + perTemplate++; + diagnostics.TransferProbesQueued++; + diagnostics.ProbesQueued++; + } + } + + if (plans.Count > 0) + { + AnsiConsole.MarkupLine($"[yellow]Confirmed synergy transfer probes:[/] queued={plans.Count:N0} maxTotal={cfg.MaxTotalTransferProbesPerRun:N0}"); + } + + return plans; + } + private async Task> ValidateProbesAsync(IReadOnlyList probes, CancellationToken ct) { if (probes.Count == 0) @@ -1213,7 +1477,8 @@ private RejectedSmokePreview AddRejectedPreview( double? predictionSpaceGap, string rejectionReason, bool matchedConfirmedAnomalyPattern, - bool twinFoundInLookup) + bool twinFoundInLookup, + double? smokeScore = null) { var preview = new RejectedSmokePreview( previews.Count, @@ -1226,7 +1491,8 @@ private RejectedSmokePreview AddRejectedPreview( predictionSpaceGap, rejectionReason, matchedConfirmedAnomalyPattern, - twinFoundInLookup); + twinFoundInLookup, + smokeScore); if (previews.Count < 500 || rejectionReason.Contains("PredictionSpaceGap", StringComparison.OrdinalIgnoreCase) || matchedConfirmedAnomalyPattern) previews.Add(preview); @@ -1461,15 +1727,160 @@ private static string ResolveProbeType(int subsetCount, int fullCount) return "leave-one-out"; } - private static double ComputeSmokeScore(double gap, double savingsPercent, int changedGroupCount, ulong? candidateRank, ulong? twinRank) + private static double ComputeSmokeScore( + double gap, + double savingsPercent, + int changedGroupCount, + ulong? candidateRank, + ulong? twinRank, + bool matchedConfirmedTemplate, + SynergyTemplateMatchTier matchTier) { - double closeness = Math.Max(0d, Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld - gap); - double groupPenalty = Math.Max(1, changedGroupCount); - double rankBonus = 0d; + double maxGap = Math.Max(Config.SynergyDetection.MaxSmokeGapKld, 1e-9d); + double closenessScore = Math.Clamp(1d - (Math.Max(0d, gap) / maxGap), 0d, 1d); + double savingsScore = Math.Clamp(savingsPercent / Math.Max(Config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent * 4d, 1e-9d), 0d, 1d); + double noveltyScore = changedGroupCount switch + { + <= 1 => 0.85d, + 2 => 1.00d, + 3 => 0.80d, + _ => 0.65d + }; + + double rankScore = 0.50d; if (candidateRank.HasValue && twinRank.HasValue) - rankBonus = Math.Clamp((double)twinRank.Value - candidateRank.Value, -10_000d, 10_000d) / 10_000d; + { + double delta = Math.Clamp((double)twinRank.Value - candidateRank.Value, -50_000d, 50_000d); + rankScore = Math.Clamp(0.50d + (delta / 100_000d), 0d, 1d); + } + + double templateScore = matchTier switch + { + SynergyTemplateMatchTier.ExactContext => 1.00d, + SynergyTemplateMatchTier.SameSelectedGroups => 0.75d, + SynergyTemplateMatchTier.EquivalentQuantFamily => 0.55d, + SynergyTemplateMatchTier.GroupFamilySuspicion => 0.35d, + _ => matchedConfirmedTemplate ? 0.50d : 0.00d + }; - return (closeness * 10_000d) + savingsPercent / groupPenalty + rankBonus; + double frontierScore = gap <= Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld ? 1.00d : 0.55d; + + return Math.Clamp( + (savingsScore * 0.25d) + + (closenessScore * 0.30d) + + (templateScore * 0.20d) + + (noveltyScore * 0.10d) + + (rankScore * 0.10d) + + (frontierScore * 0.05d), + 0d, + 1d); + } + + private SynergyTemplateMatchTier ResolveConfirmedTemplateMatchTier( + TensorConfig candidate, + IReadOnlyCollection confirmedTemplates, + out bool matched) + { + matched = false; + foreach (var rule in confirmedTemplates) + { + if (rule.RuleDirection != AnomalyRuleDirection.Beneficial.ToString()) + continue; + + if (rule.ReferenceQuantId != candidate.BaseQuant) + continue; + + bool exact = !string.IsNullOrWhiteSpace(rule.FullTensorConfigKey) && + string.Equals(rule.FullTensorConfigKey, TensorConfigIdentity.ToKey(candidate), StringComparison.Ordinal); + if (exact) + { + matched = true; + return SynergyTemplateMatchTier.ExactContext; + } + + bool selectedMatch = rule.GroupStates.All(state => + { + var group = _movement.ActiveGroups.FirstOrDefault(g => g.UniqueId == state.TensorGroupId); + return group != null && _movement.EffectiveQuantId(candidate, group) == state.CandidateQuantId; + }); + + if (selectedMatch) + { + matched = true; + return SynergyTemplateMatchTier.SameSelectedGroups; + } + + bool equivalent = rule.GroupStates.All(state => + { + var group = _movement.ActiveGroups.FirstOrDefault(g => g.UniqueId == state.TensorGroupId); + return group != null && QuantTier(_movement.EffectiveQuantId(candidate, group)) == QuantTier(state.CandidateQuantId); + }); + + if (equivalent) + { + matched = true; + return SynergyTemplateMatchTier.EquivalentQuantFamily; + } + } + + return SynergyTemplateMatchTier.None; + } + + private string ResolveTransferStratum( + TensorConfig candidate, + IReadOnlyCollection selectedGroups, + byte referenceQuantId) + { + var selectedIds = selectedGroups.Select(x => x.Group.UniqueId).ToHashSet(); + int belowQ6 = 0; + foreach (var group in _movement.ActiveGroups) + { + if (selectedIds.Contains(group.UniqueId)) + continue; + + byte q = _movement.EffectiveQuantId(candidate, group); + if (QuantTier(q) < 60) + belowQ6++; + } + + var strata = Config.SynergyDetection.TransferProbeContextStrata; + if (belowQ6 <= strata.HighFidelityMaxNonReferenceGroupsBelowQ6) + return "high-fidelity-transfer"; + + if (belowQ6 <= strata.MidFidelityMaxNonReferenceGroupsBelowQ6) + return "mid-fidelity-transfer"; + + return strata.LowFidelityEnabled ? "low-fidelity-transfer" : "disabled-low-fidelity"; + } + + private static double EstimateTemplateConfidence(AnomalyProbeResult result) + { + double gainRatio = Math.Clamp(result.ActualGainVsTwin / Math.Max(Config.AnomalyDetection.MinActualGainVsTwinKld * 2d, 1e-9d), 0d, 1d); + double classificationBonus = result.Classification switch + { + AnomalyProbeClassification.SingleGroupInversion => 0.20d, + AnomalyProbeClassification.PairSynergy => 0.25d, + AnomalyProbeClassification.HigherOrderSynergy => 0.15d, + _ => 0.10d + }; + + return Math.Clamp(gainRatio * 0.75d + classificationBonus, 0d, 1d); + } + + private static int QuantTier(byte quantId) + { + if (BaselineQuants.IsNativeExactAlias(quantId)) + return 160; + + var baseline = BaselineQuants.FromId(quantId); + string name = baseline.Names[0].ToUpperInvariant(); + if (name.Contains("Q8") || baseline.BitRange >= 8) return 80; + if (name.Contains("Q6") || baseline.BitRange == 6) return 60; + if (name.Contains("Q5") || baseline.BitRange == 5) return 50; + if (name.Contains("Q4") || name.Contains("IQ4") || baseline.BitRange == 4) return 40; + if (name.Contains("Q3") || name.Contains("IQ3") || baseline.BitRange == 3) return 30; + if (name.Contains("Q2") || name.Contains("IQ2") || baseline.BitRange == 2) return 20; + return baseline.BitRange > 0 ? baseline.BitRange * 10 : -1; } private static void WriteSmokeConsoleSummary(int historicalCount, int duckCount, IReadOnlyList selected) @@ -1478,7 +1889,7 @@ private static void WriteSmokeConsoleSummary(int historicalCount, int duckCount, int existingTwins = selected.Count(x => x.HasActualTwin); int missingTwins = selected.Count - existingTwins; - AnsiConsole.MarkupLine("[yellow]Anomaly smoke scan:[/]"); + AnsiConsole.MarkupLine("[yellow]Synergy smoke scan:[/]"); AnsiConsole.MarkupLine($"[grey] historical benchmarks scanned smoke=[/] [cyan]{historicalCount:N0}[/]"); AnsiConsole.MarkupLine($"[grey] DuckDB prediction-space smoke=[/] [cyan]{duckCount:N0}[/]"); AnsiConsole.MarkupLine($"[grey] monotone downgrade smoke candidates=[/] [cyan]{monotone:N0}[/]"); @@ -1491,7 +1902,7 @@ private static void WriteProbeOutcome(AnomalyProbeResult result) if (result.RuleDirection == AnomalyRuleDirection.Beneficial && result.ProbeSnapshot != null && result.ReferenceSnapshot != null) { AnsiConsole.MarkupLine( - $"[green]Counterfactual MDA violation confirmed:[/] candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ProbeSnapshot.Quant))} " + + $"[green]Counterfactual synergy confirmed:[/] candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ProbeSnapshot.Quant))} " + $"twin={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ReferenceSnapshot.Quant))} " + $"actual candidate KLD={result.ProbeSnapshot.Kld:0.000000} actual twin KLD={result.ReferenceSnapshot.Kld:0.000000} " + $"gain={result.ActualGainVsTwin:0.000000} classification={result.Classification}"); @@ -1597,12 +2008,15 @@ private object ToSmokeLog(AnomalySmokeCandidate x) x.TwinLookupMode, x.RejectionReason, x.MatchedConfirmedAnomalyPattern, + wouldMatchConfirmedTemplate = x.WouldMatchConfirmedTemplate, + synergyMatchTier = x.SynergyMatchTier.ToString(), x.TwinFoundInLookupDictionary, seedClass = x.SeedClass.ToString(), x.CandidatePredictionRank, x.TwinPredictionRank, x.SmokeScore, x.SmokeStrength, + x.IsTransferProbeSeed, x.HasActualTwin, x.CandidateActualKld, x.TwinActualKld, @@ -1624,6 +2038,7 @@ private object ToProbeLog(AnomalyProbePlan x) referenceName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ReferenceConfig), probeName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ProbeConfig), referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ReferenceConfig), + virtualTwinEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ReferenceConfig), candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ProbeConfig), inactiveGroups = _movement.BuildInactiveGroupList(), movementClassification = movement.Classification.ToString(), @@ -1662,6 +2077,42 @@ private object ToResultLog(AnomalyProbeResult x) }; } + private static object ToSynergyTemplateLog(AnomalyInteractionRule x) + { + return new + { + templateType = x.RuleType, + referenceQuant = SafeName(x.ReferenceQuantId), + selectedGroupStates = x.GroupStates.OrderBy(g => g.SortOrder).ToDictionary(g => g.TensorGroupId.ToString(), g => SafeName(g.CandidateQuantId)), + raisedCounterfactualStates = x.GroupStates.OrderBy(g => g.SortOrder).ToDictionary(g => g.TensorGroupId.ToString(), g => SafeName(g.ReferenceQuantId)), + discoveryContext = TryDeserializeJson(x.ReferenceEffectiveGroupsJson), + candidateContext = TryDeserializeJson(x.CandidateEffectiveGroupsJson), + actualGainKld = x.BestActualGainVsTwin, + confidence = x.Confidence, + generalizationPolicy = "ExactStrong_TransferWeak", + ruleDirection = x.RuleDirection, + ruleStatus = x.RuleStatus, + fullTensorConfigKey = x.FullTensorConfigKey, + referenceContextKey = x.ReferenceContextKey, + metadata = TryDeserializeJson(x.MetadataJson) + }; + } + + private static object? TryDeserializeJson(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return null; + + try + { + return JsonSerializer.Deserialize(json); + } + catch + { + return json; + } + } + private static object ToRuleLog(AnomalyInteractionRule x) { return new @@ -1743,7 +2194,8 @@ public RejectedSmokePreview( double? predictionSpaceGap, string rejectionReason, bool matchedConfirmedAnomalyPattern, - bool twinFoundInLookup) + bool twinFoundInLookup, + double? smokeScore) { SortOrder = sortOrder; Candidate = candidate; @@ -1756,6 +2208,7 @@ public RejectedSmokePreview( RejectionReason = rejectionReason; MatchedConfirmedAnomalyPattern = matchedConfirmedAnomalyPattern; TwinFoundInLookup = twinFoundInLookup; + SmokeScore = smokeScore; } public int SortOrder { get; } @@ -1769,6 +2222,7 @@ public RejectedSmokePreview( public string RejectionReason { get; } public bool MatchedConfirmedAnomalyPattern { get; } public bool TwinFoundInLookup { get; } + public double? SmokeScore { get; } public string CandidateName => HybridBenchmarkRepository.BuildDisplayName((HybridQuant)Candidate); public string TwinName => HybridBenchmarkRepository.BuildDisplayName((HybridQuant)Twin); @@ -1786,7 +2240,9 @@ public RejectedSmokePreview( predictedTwinSizeBytes = TwinRow?.PredictedSizeBytes, predictedSizeSavingsBytes = PredictedSizeSavingsBytes, rejectionReason = RejectionReason, + smokeScore = SmokeScore, matchedConfirmedAnomalyPattern = MatchedConfirmedAnomalyPattern, + wouldMatchConfirmedTemplate = MatchedConfirmedAnomalyPattern, twinExistedInLookupDictionary = TwinFoundInLookup }; } diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 40ed378..8e30c3c 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -322,6 +322,43 @@ anomaly_detection: - Q5_K - UD-Q5_K_XL +synergy_detection: + enabled: true + + # One transfer/generalization refinement pass after exact-context synergy templates are confirmed. + max_refinement_rounds: 1 + + # Confidence multipliers for transferring a confirmed counterfactual synergy template. + exact_context_confidence_multiplier: 1.00 + same_selected_groups_confidence_multiplier: 0.55 + equivalent_quant_family_confidence_multiplier: 0.30 + group_family_suspicion_confidence_multiplier: 0.15 + + # Minimum confidence gates for applying transfer adjustments or scheduling transfer probes. + min_confidence_to_apply_adjustment: 0.35 + min_confidence_to_schedule_transfer_probe: 0.25 + + # Transfer adjustments are prediction-space/rank-relative, not raw real-KLD deltas. + max_negative_adjustment_kld: 0.002 + max_negative_adjustment_fraction_of_base_kld: 0.75 + + # Tiny stratified sniff pass around confirmed Q8-dome synergy templates. + transfer_probe_enabled: true + max_transfer_probes_per_template: 6 + max_total_transfer_probes_per_run: 24 + + transfer_probe_context_strata: + high_fidelity_max_non_reference_groups_below_q6: 1 + mid_fidelity_max_non_reference_groups_below_q6: 3 + low_fidelity_enabled: false + + # Smoke scoring augments the old prediction-space gap cliff. + min_smoke_score: 0.55 + max_smoke_gap_kld: 0.004 + top_rejected_smoke_preview: 25 + + verbose_synergy_logging: true + output: # Optional explicit output directory. # If blank, MagicQuant will default to: diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 01d0fec..3b76f49 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -244,6 +244,43 @@ anomaly_detection: - Q5_K - UD-Q5_K_XL +synergy_detection: + enabled: true + + # One transfer/generalization refinement pass after exact-context synergy templates are confirmed. + max_refinement_rounds: 1 + + # Confidence multipliers for transferring a confirmed counterfactual synergy template. + exact_context_confidence_multiplier: 1.00 + same_selected_groups_confidence_multiplier: 0.55 + equivalent_quant_family_confidence_multiplier: 0.30 + group_family_suspicion_confidence_multiplier: 0.15 + + # Minimum confidence gates for applying transfer adjustments or scheduling transfer probes. + min_confidence_to_apply_adjustment: 0.35 + min_confidence_to_schedule_transfer_probe: 0.25 + + # Transfer adjustments are prediction-space/rank-relative, not raw real-KLD deltas. + max_negative_adjustment_kld: 0.002 + max_negative_adjustment_fraction_of_base_kld: 0.75 + + # Tiny stratified sniff pass around confirmed Q8-dome synergy templates. + transfer_probe_enabled: true + max_transfer_probes_per_template: 6 + max_total_transfer_probes_per_run: 24 + + transfer_probe_context_strata: + high_fidelity_max_non_reference_groups_below_q6: 1 + mid_fidelity_max_non_reference_groups_below_q6: 3 + low_fidelity_enabled: false + + # Smoke scoring augments the old prediction-space gap cliff. + min_smoke_score: 0.55 + max_smoke_gap_kld: 0.004 + top_rejected_smoke_preview: 25 + + verbose_synergy_logging: true + output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: From fd82cb99f5d8698d6d03b7f27a87f96c514f982e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 4 May 2026 12:00:15 -0400 Subject: [PATCH 190/258] lots of updates, starting to look good but need to validate --- .../Configuration/MagicQuantYamlConfig.cs | 10 + .../Configuration/MagicQuantYamlLoader.cs | 57 +- MagicQuant/MagicQuant.csproj | 1 + MagicQuant/Models/AnomalyDetectionModels.cs | 66 +- .../AnomalyAdjustedPredictionService.cs | 503 +++--------- MagicQuant/Services/AnomalyRuleRepository.cs | 53 +- MagicQuant/Services/AnomalyWorkflowService.cs | 767 +++++++++--------- .../Services/FinalArtifactNamingService.cs | 52 +- .../FinalSurvivorSelectionCliService.cs | 4 +- MagicQuant/config.default.yaml | 75 +- MagicQuant/config.dev.yaml | 75 +- 11 files changed, 688 insertions(+), 975 deletions(-) diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 17b1f4b..831e66e 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -303,6 +303,16 @@ public sealed class RuntimeSynergyDetectionConfig public double MinSmokeScore { get; set; } = 0.55d; public double MaxSmokeGapKld { get; set; } = 0.004d; public int TopRejectedSmokePreview { get; set; } = 25; + public bool CompositionProbeEnabled { get; set; } = true; + public int MaxTemplateCompositionGroupCount { get; set; } = 4; + public int MaxCompositionProbesPerRun { get; set; } = 8; + public int MaxTemplatesToCompose { get; set; } = 4; + public double MinTemplateConfidenceForComposition { get; set; } = 0.50d; + public double MinCombinedExpectedSizeSavingsPercent { get; set; } = 1.0d; + public bool ContaminatingPassengerDetectionEnabled { get; set; } = true; + public double MinFailureMarginForContaminationKld { get; set; } = 0.00050d; + public double ContaminationPenaltyConfidenceMultiplier { get; set; } = 0.45d; + public bool SuppressRepeatedContaminatedAttempts { get; set; } = true; } public sealed class RuntimeSynergyTransferProbeContextStrataConfig diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index f6216a6..94d211f 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -146,6 +146,8 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) config.CandidateSelection.MinimumKldImprovementEpsilon = Math.Max(0d, config.CandidateSelection.MinimumKldImprovementEpsilon); config.AnomalyDetection ??= new RuntimeAnomalyDetectionConfig(); + config.SynergyDetection ??= new RuntimeSynergyDetectionConfig(); + NormalizeSynergyDetection(config); config.AnomalyDetection.MaxAnomalyRefinementRounds = Math.Clamp(config.AnomalyDetection.MaxAnomalyRefinementRounds, 0, 1); config.AnomalyDetection.MinActualGainVsTwinKld = Math.Max(0d, config.AnomalyDetection.MinActualGainVsTwinKld); config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent = Math.Max(0d, config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent); @@ -162,35 +164,40 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld = Math.Clamp(config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld, 0d, 1d); config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone = Math.Max(1, config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone); - config.SynergyDetection ??= new RuntimeSynergyDetectionConfig(); - config.SynergyDetection.MaxRefinementRounds = Math.Clamp(config.SynergyDetection.MaxRefinementRounds, 0, 1); - config.SynergyDetection.ExactContextConfidenceMultiplier = Math.Clamp(config.SynergyDetection.ExactContextConfidenceMultiplier, 0d, 1d); - config.SynergyDetection.SameSelectedGroupsConfidenceMultiplier = Math.Clamp(config.SynergyDetection.SameSelectedGroupsConfidenceMultiplier, 0d, 1d); - config.SynergyDetection.EquivalentQuantFamilyConfidenceMultiplier = Math.Clamp(config.SynergyDetection.EquivalentQuantFamilyConfidenceMultiplier, 0d, 1d); - config.SynergyDetection.GroupFamilySuspicionConfidenceMultiplier = Math.Clamp(config.SynergyDetection.GroupFamilySuspicionConfidenceMultiplier, 0d, 1d); - config.SynergyDetection.MinConfidenceToApplyAdjustment = Math.Clamp(config.SynergyDetection.MinConfidenceToApplyAdjustment, 0d, 1d); - config.SynergyDetection.MinConfidenceToScheduleTransferProbe = Math.Clamp(config.SynergyDetection.MinConfidenceToScheduleTransferProbe, 0d, 1d); - config.SynergyDetection.MaxNegativeAdjustmentKld = Math.Max(0d, config.SynergyDetection.MaxNegativeAdjustmentKld); - config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld = Math.Clamp(config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld, 0d, 1d); - config.SynergyDetection.MaxTransferProbesPerTemplate = Math.Max(0, config.SynergyDetection.MaxTransferProbesPerTemplate); - config.SynergyDetection.MaxTotalTransferProbesPerRun = Math.Max(0, config.SynergyDetection.MaxTotalTransferProbesPerRun); - config.SynergyDetection.MinSmokeScore = Math.Clamp(config.SynergyDetection.MinSmokeScore, 0d, 1d); - config.SynergyDetection.MaxSmokeGapKld = Math.Max(0d, config.SynergyDetection.MaxSmokeGapKld); - config.SynergyDetection.TopRejectedSmokePreview = Math.Max(1, config.SynergyDetection.TopRejectedSmokePreview); - config.SynergyDetection.TransferProbeContextStrata ??= new RuntimeSynergyTransferProbeContextStrataConfig(); - config.SynergyDetection.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, config.SynergyDetection.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6); - config.SynergyDetection.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(config.SynergyDetection.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6, config.SynergyDetection.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6); - - // Compatibility bridge: old anomaly_detection remains the operational section; - // synergy_detection controls transfer/generalization behavior. If the new section - // is disabled, anomaly/synergy pass can still run exact-context probes, but no - // transfer probes or transferable adjustments are scheduled. - config.AnomalyDetection.MinRuleConfidenceToApply = Math.Min(config.AnomalyDetection.MinRuleConfidenceToApply, config.SynergyDetection.MinConfidenceToApplyAdjustment); - ApplyStandardBaselineFilters(config.Baselines); BaselineQuants.ResetDynamicCustomBaselines(); } + + private static void NormalizeSynergyDetection(MagicQuantYamlConfig config) + { + var s = config.SynergyDetection; + s.MaxRefinementRounds = Math.Clamp(s.MaxRefinementRounds, 0, 1); + s.ExactContextConfidenceMultiplier = Math.Clamp(s.ExactContextConfidenceMultiplier, 0d, 1d); + s.SameSelectedGroupsConfidenceMultiplier = Math.Clamp(s.SameSelectedGroupsConfidenceMultiplier, 0d, 1d); + s.EquivalentQuantFamilyConfidenceMultiplier = Math.Clamp(s.EquivalentQuantFamilyConfidenceMultiplier, 0d, 1d); + s.GroupFamilySuspicionConfidenceMultiplier = Math.Clamp(s.GroupFamilySuspicionConfidenceMultiplier, 0d, 1d); + s.MinConfidenceToApplyAdjustment = Math.Clamp(s.MinConfidenceToApplyAdjustment, 0d, 1d); + s.MinConfidenceToScheduleTransferProbe = Math.Clamp(s.MinConfidenceToScheduleTransferProbe, 0d, 1d); + s.MaxNegativeAdjustmentKld = Math.Max(0d, s.MaxNegativeAdjustmentKld); + s.MaxNegativeAdjustmentFractionOfBaseKld = Math.Clamp(s.MaxNegativeAdjustmentFractionOfBaseKld, 0d, 1d); + s.MaxTransferProbesPerTemplate = Math.Max(0, s.MaxTransferProbesPerTemplate); + s.MaxTotalTransferProbesPerRun = Math.Max(0, s.MaxTotalTransferProbesPerRun); + s.TransferProbeContextStrata ??= new RuntimeSynergyTransferProbeContextStrataConfig(); + s.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, s.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6); + s.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, s.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6); + s.MinSmokeScore = Math.Clamp(s.MinSmokeScore, 0d, 1d); + s.MaxSmokeGapKld = Math.Max(0d, s.MaxSmokeGapKld); + s.TopRejectedSmokePreview = Math.Max(1, s.TopRejectedSmokePreview); + s.MaxTemplateCompositionGroupCount = Math.Clamp(s.MaxTemplateCompositionGroupCount, 1, 9); + s.MaxCompositionProbesPerRun = Math.Max(0, s.MaxCompositionProbesPerRun); + s.MaxTemplatesToCompose = Math.Max(0, s.MaxTemplatesToCompose); + s.MinTemplateConfidenceForComposition = Math.Clamp(s.MinTemplateConfidenceForComposition, 0d, 1d); + s.MinCombinedExpectedSizeSavingsPercent = Math.Max(0d, s.MinCombinedExpectedSizeSavingsPercent); + s.MinFailureMarginForContaminationKld = Math.Max(0d, s.MinFailureMarginForContaminationKld); + s.ContaminationPenaltyConfidenceMultiplier = Math.Clamp(s.ContaminationPenaltyConfidenceMultiplier, 0d, 1d); + } + private static void ApplyStandardBaselineFilters(RuntimeBaselineConfig baselineConfig) { string mode = (baselineConfig.StandardBaselinesMode ?? "all").Trim().ToLowerInvariant(); diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 8352882..dedebc6 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -29,6 +29,7 @@ + diff --git a/MagicQuant/Models/AnomalyDetectionModels.cs b/MagicQuant/Models/AnomalyDetectionModels.cs index 1a42857..c2fe62a 100644 --- a/MagicQuant/Models/AnomalyDetectionModels.cs +++ b/MagicQuant/Models/AnomalyDetectionModels.cs @@ -45,7 +45,7 @@ public enum AnomalySeedClass ExploratoryPair = 5, ConfirmedAnomalyNeighborhoodProbe = 6, SynergyTransferProbe = 7, - CounterfactualSynergyTemplate = 8 + SynergyCompositionProbe = 8 } public enum AnomalyProbeClassification @@ -61,7 +61,13 @@ public enum AnomalyProbeClassification HigherOrderSynergy = 9, ContextOnly = 10, MissingTwin = 11, - MissingProbeBenchmark = 12 + MissingProbeBenchmark = 12, + SuperSynergy = 13, + AdditiveComposition = 14, + RedundantComposition = 15, + HarmfulInterference = 16, + CompositionRejected = 17, + ContaminatingPassenger = 18 } public sealed class AnomalyChangedGroup @@ -86,15 +92,6 @@ public sealed class AnomalyMovementAnalysis public int NetBitDelta { get; init; } } -public enum SynergyTemplateMatchTier -{ - None = 0, - ExactContext = 1, - SameSelectedGroups = 2, - EquivalentQuantFamily = 3, - GroupFamilySuspicion = 4 -} - public sealed class AnomalySmokeCandidate { public string Source { get; init; } = string.Empty; @@ -113,8 +110,6 @@ public sealed class AnomalySmokeCandidate public string TwinLookupMode { get; init; } = string.Empty; public string RejectionReason { get; init; } = string.Empty; public bool MatchedConfirmedAnomalyPattern { get; init; } - public bool WouldMatchConfirmedTemplate { get; init; } - public SynergyTemplateMatchTier SynergyMatchTier { get; init; } = SynergyTemplateMatchTier.None; public bool TwinFoundInLookupDictionary { get; init; } public AnomalySeedClass SeedClass { get; init; } = AnomalySeedClass.PredictionSpaceSmoke; public double PredictionSpaceGapVsTwin { get; init; } @@ -122,7 +117,6 @@ public sealed class AnomalySmokeCandidate public ulong? TwinPredictionRank { get; init; } public double SmokeScore { get; init; } public string SmokeStrength { get; init; } = string.Empty; - public bool IsTransferProbeSeed { get; init; } public bool HasActualTwin { get; init; } public double? CandidateActualKld { get; init; } public double? TwinActualKld { get; init; } @@ -161,11 +155,6 @@ public sealed class AnomalyAdjustmentSummary { public int AppliedRuleCount { get; init; } public long MatchedRowCount { get; init; } - public long ExactContextMatches { get; init; } - public long SameSelectedGroupMatches { get; init; } - public long EquivalentQuantFamilyMatches { get; init; } - public long SuppressedMatches { get; init; } - public long HarmfulMatches { get; init; } public string DuckDbPath { get; init; } = string.Empty; public IReadOnlyList RuleMatches { get; init; } = Array.Empty(); } @@ -195,8 +184,6 @@ public sealed class AnomalySmokeScanDiagnostics public long MixedTradeIgnored { get; set; } public long SizeSavingsBelowThreshold { get; set; } public long PredictionSpaceGapTooLarge { get; set; } - public long BelowMinSmokeScore { get; set; } - public long CatastrophicGapRejected { get; set; } public long QueuedSmokeCandidates { get; set; } public long LoadPredictedRowsMs { get; set; } public long BuildLookupDictionaryMs { get; set; } @@ -214,7 +201,40 @@ public sealed class ProbePlanningDiagnostics public int SkippedBudget { get; set; } public int ProbesQueued { get; set; } public int ExpansionProbesQueued { get; set; } + public int CompositionProbesQueued { get; set; } public int TransferProbesQueued { get; set; } - public int SkippedTransferStrata { get; set; } - public int SkippedMissingVirtualTwin { get; set; } + public int SkippedContaminationSuppression { get; set; } +} + +public sealed class SynergyCompositionProbeRecord +{ + public string CompositionId { get; init; } = string.Empty; + public IReadOnlyList SourceTemplateIds { get; init; } = Array.Empty(); + public IReadOnlyList SourceTemplateLabels { get; init; } = Array.Empty(); + public Dictionary CandidateEffectiveGroups { get; init; } = new(StringComparer.OrdinalIgnoreCase); + public Dictionary TwinEffectiveGroups { get; init; } = new(StringComparer.OrdinalIgnoreCase); + public int CombinedGroupCount { get; init; } + public string Classification { get; init; } = string.Empty; + public double? ActualCandidateKld { get; init; } + public double? ActualTwinKld { get; init; } + public double? ActualGainVsTwin { get; init; } + public double? PredictedCandidateKld { get; init; } + public double? PredictedTwinKld { get; init; } + public double? PredictionSpaceGap { get; init; } + public IReadOnlyList Notes { get; init; } = Array.Empty(); +} + +public sealed class SynergyWingSummary +{ + public string Zone { get; init; } = string.Empty; + public int SmokeCount { get; set; } + public int ConfirmedBeneficialTemplates { get; set; } + public int HarmfulTemplates { get; set; } + public int SuppressionOnlyTemplates { get; set; } + public int CandidateRowsAdjustedPositively { get; set; } + public int CandidateRowsDemoted { get; set; } + public int ValidationSuccessCount { get; set; } + public int ValidationFailureCount { get; set; } + public int FinalSurvivorsFromZone { get; set; } + public string Explanation { get; set; } = string.Empty; } \ No newline at end of file diff --git a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs index 3db0970..f41c406 100644 --- a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs +++ b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs @@ -1,10 +1,9 @@ using DuckDB.NET.Data; -using System.Globalization; -using System.Numerics; using System.Text.Json; using MagicQuant.Models; using MQ.DB; using MQ.DB.Models; +using System.Numerics; using MQ.DB.Models.DbModels; using Spectre.Console; @@ -38,111 +37,82 @@ await ExecuteAsync(c, $@" WHERE BaseRankSafeKld IS NOT NULL;", ct); long totalMatched = 0; - long exactMatched = 0; - long sameSelectedMatched = 0; - long equivalentMatched = 0; - long harmfulMatched = 0; - long suppressedMatched = 0; var matchLogs = new List(); foreach (var rule in rules.OrderByDescending(x => x.Confidence).ThenBy(x => x.Id)) { - if (!IsRuleUsable(rule)) + string where = BuildRuleWhere(rule); + if (string.IsNullOrWhiteSpace(where)) continue; - var tierResults = new List(); - tierResults.Add(await ApplyRuleTierAsync( - c, - rule, - SynergyTemplateMatchTier.ExactContext, - Config.SynergyDetection.ExactContextConfidenceMultiplier, - ct)); + double adjustment = rule.AppliedPredictionSpaceAdjustmentKld; + if (Math.Abs(adjustment) <= 0d) + continue; - if (Config.SynergyDetection.Enabled) - { - tierResults.Add(await ApplyRuleTierAsync( - c, - rule, - SynergyTemplateMatchTier.SameSelectedGroups, - Config.SynergyDetection.SameSelectedGroupsConfidenceMultiplier, - ct)); - - tierResults.Add(await ApplyRuleTierAsync( - c, - rule, - SynergyTemplateMatchTier.EquivalentQuantFamily, - Config.SynergyDetection.EquivalentQuantFamilyConfidenceMultiplier, - ct)); - } - - foreach (var tier in tierResults.Where(x => x.MatchedRows > 0)) - { - totalMatched += tier.MatchedRows; - if (tier.Tier == SynergyTemplateMatchTier.ExactContext) exactMatched += tier.MatchedRows; - if (tier.Tier == SynergyTemplateMatchTier.SameSelectedGroups) sameSelectedMatched += tier.MatchedRows; - if (tier.Tier == SynergyTemplateMatchTier.EquivalentQuantFamily) equivalentMatched += tier.MatchedRows; - if (rule.RuleDirection == AnomalyRuleDirection.Harmful.ToString()) harmfulMatched += tier.MatchedRows; - if (rule.RuleDirection == AnomalyRuleDirection.SuppressionOnly.ToString()) suppressedMatched += tier.MatchedRows; - - matchLogs.Add(new - { - ruleId = rule.Id, - templateType = ResolveTemplateType(rule), - direction = rule.RuleDirection, - tier = tier.Tier.ToString(), - tierMultiplier = tier.Multiplier, - referenceQuant = SafeName(rule.ReferenceQuantId), - groupSetHash = rule.GroupSetHash, - exactContextMatches = tier.Tier == SynergyTemplateMatchTier.ExactContext ? tier.MatchedRows : 0, - sameSelectedGroupMatches = tier.Tier == SynergyTemplateMatchTier.SameSelectedGroups ? tier.MatchedRows : 0, - equivalentQuantFamilyMatches = tier.Tier == SynergyTemplateMatchTier.EquivalentQuantFamily ? tier.MatchedRows : 0, - totalAdjustedRows = tier.MatchedRows, - basePredictedKld = tier.Before.AverageBasePredictedKld, - adjustedPredictedKld = tier.After.AverageFinalPredictedKld, - averageAdjustment = tier.After.AverageAnomalyAdjustmentKld, - actualCandidateKld = ExtractActualEffect(rule).CandidateKld, - actualTwinKld = ExtractActualEffect(rule).TwinKld, - actualGainOrHarm = ExtractActualEffect(rule).GainOrHarm, - adjustmentReason = "prediction-space-virtual-twin-rank-movement", - confidence = rule.Confidence, - effectiveConfidence = tier.EffectiveConfidence, - candidatePredicateReason = tier.CandidatePredicateReason, - virtualTwinPredicateReason = tier.VirtualTwinPredicateReason, - groups = rule.GroupStates - .OrderBy(x => x.SortOrder) - .Select(x => new - { - x.TensorGroupId, - group = ColumnNameForGroupId(x.TensorGroupId), - candidate = SafeName(x.CandidateQuantId), - reference = SafeName(x.ReferenceQuantId), - x.Movement - }) - .ToList() - }); - - AnsiConsole.MarkupLine( - $"[green]Applying synergy template:[/] template=[cyan]{Markup.Escape(DescribeRule(rule))}[/] tier=[cyan]{tier.Tier}[/] direction=[cyan]{Markup.Escape(rule.RuleDirection)}[/] " + - $"basePredictedKld=[cyan]{tier.Before.AverageBasePredictedKld:0.000000}[/] adjustedPredictedKld=[cyan]{tier.After.AverageFinalPredictedKld:0.000000}[/] " + - $"avgAdjustment=[cyan]{tier.After.AverageAnomalyAdjustmentKld:0.000000}[/] reason=[cyan]prediction-space-virtual-twin-rank-movement[/] matched DuckDB rows=[cyan]{tier.MatchedRows:N0}[/]"); - } - - if (tierResults.All(x => x.MatchedRows == 0)) + long before = await CountMatchesAsync(c, where, ct); + if (before == 0) + continue; + + var beforeStats = await LoadPredictionStatsAsync(c, where, ct); + + string expression = adjustment < 0d + ? $"GREATEST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), -LEAST({SqlDouble(Math.Min(Config.AnomalyDetection.MaxNegativeAdjustmentKld, Config.SynergyDetection.MaxNegativeAdjustmentKld))}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Math.Min(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld, Config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld))}))" + : $"LEAST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))"; + + await ExecuteAsync(c, $@" +UPDATE {CombinationDuckDbSchema.TableName} +SET AnomalyAdjustmentKld = {expression}, + FinalPredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}), + PredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}) +WHERE {where};", ct); + + var afterStats = await LoadPredictionStatsAsync(c, where, ct); + + totalMatched += before; + var actual = ExtractActualEffect(rule); + var log = new { - matchLogs.Add(new - { - ruleId = rule.Id, - templateType = ResolveTemplateType(rule), - direction = rule.RuleDirection, - referenceQuant = SafeName(rule.ReferenceQuantId), - groupSetHash = rule.GroupSetHash, - exactContextMatches = 0, - sameSelectedGroupMatches = 0, - equivalentQuantFamilyMatches = 0, - totalAdjustedRows = 0, - reason = "No DuckDB rows matched this transferable synergy template. Either the search space does not contain the selected group states, virtual raised twins were not generated/predictable, active groups were sparse/native-exact, or confidence/generalization thresholds blocked the tier." - }); - } + ruleId = rule.Id, + direction = rule.RuleDirection, + ruleType = rule.RuleType, + referenceQuant = SafeName(rule.ReferenceQuantId), + groupSetHash = rule.GroupSetHash, + basePredictedKld = beforeStats.AverageBasePredictedKld, + adjustment, + adjustedPredictedKld = afterStats.AverageFinalPredictedKld, + actualCandidateKld = actual.CandidateKld, + actualTwinKld = actual.TwinKld, + actualGainOrHarm = actual.GainOrHarm, + adjustmentReason = actual.HasActualEffect ? "measured-actual-counterfactual-effect" : "prediction-space-gap-fallback", + exactContextMatches = 0, + sameSelectedGroupMatches = before, + equivalentQuantFamilyMatches = 0, + compositionMatches = rule.RuleType.Contains("Composition", StringComparison.OrdinalIgnoreCase) ? before : 0, + contaminationSuppressedMatches = rule.RuleType.Contains("Contaminating", StringComparison.OrdinalIgnoreCase) ? before : 0, + harmfulMatches = rule.RuleDirection.Contains("Harmful", StringComparison.OrdinalIgnoreCase) ? before : 0, + totalAdjustedRows = before, + matchedRows = before, + confidence = rule.Confidence, + groups = rule.GroupStates + .OrderBy(x => x.SortOrder) + .Select(x => new + { + x.TensorGroupId, + candidate = SafeName(x.CandidateQuantId), + reference = SafeName(x.ReferenceQuantId), + x.Movement + }) + .ToList() + }; + matchLogs.Add(log); + + AnsiConsole.MarkupLine( + $"[green]Applying anomaly rule:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] direction=[cyan]{Markup.Escape(rule.RuleDirection)}[/] " + + $"basePredictedKld=[cyan]{beforeStats.AverageBasePredictedKld:0.000000}[/] adjustment=[cyan]{adjustment:0.000000}[/] " + + $"adjustedPredictedKld=[cyan]{afterStats.AverageFinalPredictedKld:0.000000}[/] " + + $"actualCandidateKld=[cyan]{FmtNullable(actual.CandidateKld)}[/] actualTwinKld=[cyan]{FmtNullable(actual.TwinKld)}[/] " + + $"actualGainOrHarm=[cyan]{FmtNullable(actual.GainOrHarm)}[/] reason=[cyan]{Markup.Escape(actual.HasActualEffect ? "measured-actual-counterfactual-effect" : "prediction-space-gap-fallback")}[/] " + + $"exactContextMatches=[cyan]0[/] sameSelectedGroupMatches=[cyan]{before:N0}[/] totalAdjustedRows=[cyan]{before:N0}[/]"); } await ReRankAsync(c, ct); @@ -151,281 +121,52 @@ await ExecuteAsync(c, $@" { AppliedRuleCount = rules.Count, MatchedRowCount = totalMatched, - ExactContextMatches = exactMatched, - SameSelectedGroupMatches = sameSelectedMatched, - EquivalentQuantFamilyMatches = equivalentMatched, - SuppressedMatches = suppressedMatched, - HarmfulMatches = harmfulMatched, DuckDbPath = _store.GetDatabaseFilePath(), RuleMatches = matchLogs }; } - private static async Task ApplyRuleTierAsync( - DuckDBConnection c, - AnomalyInteractionRule rule, - SynergyTemplateMatchTier tier, - double tierMultiplier, - CancellationToken ct) - { - var empty = new RuleTierApplyResult( - tier, - 0, - tierMultiplier, - 0d, - new PredictionMatchStats(0d, 0d, 0d), - new PredictionMatchStats(0d, 0d, 0d), - string.Empty, - string.Empty); - - if (tierMultiplier <= 0d) - return empty; - - double effectiveConfidence = rule.Confidence * tierMultiplier; - if (effectiveConfidence < Config.SynergyDetection.MinConfidenceToApplyAdjustment) - return empty with { CandidatePredicateReason = "BlockedByMinSynergyConfidence" }; - - var matchSql = BuildRuleMatchSubquery(rule, tier, out var candidateReason, out var twinReason); - if (string.IsNullOrWhiteSpace(matchSql)) - return empty with { CandidatePredicateReason = candidateReason, VirtualTwinPredicateReason = twinReason }; - - long before = await CountMatchesAsync(c, matchSql, ct); - if (before == 0) - return empty with { CandidatePredicateReason = candidateReason, VirtualTwinPredicateReason = twinReason }; - - var beforeStats = await LoadPredictionStatsAsync(c, matchSql, ct); - string signedMagnitude = BuildAdjustmentMagnitudeSql(rule, effectiveConfidence); - - await ExecuteAsync(c, $@" -WITH matches AS ( -{matchSql} -), calculated AS ( - SELECT {CombinationDuckDbSchema.SlotColumnList}, - {signedMagnitude} AS Delta - FROM matches -) -UPDATE {CombinationDuckDbSchema.TableName} t -SET AnomalyAdjustmentKld = CASE - WHEN calculated.Delta < 0 THEN GREATEST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, -LEAST({SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld)})) - ELSE LEAST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)})) - END, - FinalPredictedKld = GREATEST(0.0, COALESCE(t.BaseRankSafeKld, t.PredictedKld, 0.0) + CASE - WHEN calculated.Delta < 0 THEN GREATEST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, -LEAST({SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld)})) - ELSE LEAST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)})) - END), - PredictedKld = GREATEST(0.0, COALESCE(t.BaseRankSafeKld, t.PredictedKld, 0.0) + CASE - WHEN calculated.Delta < 0 THEN GREATEST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, -LEAST({SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld)})) - ELSE LEAST(COALESCE(t.AnomalyAdjustmentKld, 0.0) + calculated.Delta, LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(t.BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)})) - END) -FROM calculated -WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "calculated")};", ct); - - var afterStats = await LoadPredictionStatsAsync(c, matchSql, ct); - return new RuleTierApplyResult(tier, before, tierMultiplier, effectiveConfidence, beforeStats, afterStats, candidateReason, twinReason); - } - - private static string BuildAdjustmentMagnitudeSql(AnomalyInteractionRule rule, double effectiveConfidence) - { - string baseGap = "(COALESCE(CandidateBaseRankSafeKld, CandidatePredictedKld, 0.0) - COALESCE(VirtualTwinBaseRankSafeKld, VirtualTwinPredictedKld, COALESCE(CandidateBaseRankSafeKld, CandidatePredictedKld, 0.0)))"; - string magnitude = $"(GREATEST({baseGap}, 0.0) + {SqlDouble(Config.AnomalyDetection.PredictionSpaceViolationMargin)}) * {SqlDouble(Config.AnomalyDetection.AnomalyAdjustmentShrinkFactor)} * {SqlDouble(effectiveConfidence)}"; - - if (rule.RuleDirection == AnomalyRuleDirection.Harmful.ToString()) - return $"LEAST({magnitude}, {SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)})"; - - return $"-LEAST({magnitude}, {SqlDouble(Config.SynergyDetection.MaxNegativeAdjustmentKld)})"; - } - private static string BuildRuleMatchSubquery( - AnomalyInteractionRule rule, - SynergyTemplateMatchTier tier, - out string candidateReason, - out string virtualTwinReason) + private static string BuildRuleWhere(AnomalyInteractionRule rule) { - candidateReason = tier switch - { - SynergyTemplateMatchTier.ExactContext => "Full explicit discovery context must match the confirmed dome probe.", - SynergyTemplateMatchTier.SameSelectedGroups => "Candidate must contain the same selected group states as the confirmed synergy template.", - SynergyTemplateMatchTier.EquivalentQuantFamily => "Candidate selected groups must use a related quant family/tier.", - _ => "Unsupported tier." - }; - virtualTwinReason = "Virtual twin is constructed by raising only selected groups to their counterfactual reference quant while preserving all other effective group states."; - if (rule.GroupStates.Count == 0) return string.Empty; if (BaselineQuants.IsNativeExactAlias(rule.ReferenceQuantId) || rule.GroupStates.Any(x => BaselineQuants.IsNativeExactAlias(x.CandidateQuantId) || BaselineQuants.IsNativeExactAlias(x.ReferenceQuantId))) { - candidateReason = "BF16/native/exact template states are not valid contextual synergy templates."; return string.Empty; } - var selected = rule.GroupStates.OrderBy(x => x.SortOrder).ToList(); var predicates = new List { - "t.BaseRankSafeKld IS NOT NULL", - "t.PredictedSizeBytes IS NOT NULL", - "COALESCE(t.IsProtectedAnchor, FALSE) = FALSE" + CombinationDuckDbSchema.ActiveCandidatePredicateSql, + "BaseRankSafeKld IS NOT NULL", + $"BaseQuant = {rule.ReferenceQuantId}" }; - // Keep transfer controlled for now: a Q8-dome template applies inside rows with the same base/reference quant. - predicates.Add($"t.BaseQuant = {rule.ReferenceQuantId}"); - - foreach (var state in selected) + // Transferable counterfactual synergy template matching: + // The exact Q8/Q6 dome remains strongest evidence, but application should not + // require all surrounding groups to equal the discovery context. Match rows that + // contain the selected group states, then compare them conceptually to a virtual + // same-context twin where only those selected groups are raised back to ReferenceQuantId. + // Surrounding groups are preserved by the virtual twin and therefore intentionally + // not constrained here. Explicit probe/rule persistence remains strict elsewhere. + foreach (var state in rule.GroupStates.OrderBy(x => x.SortOrder)) { string? column = ColumnNameForGroupId(state.TensorGroupId); if (column == null) return string.Empty; - if (tier == SynergyTemplateMatchTier.EquivalentQuantFamily) - { - var equivalentIds = EquivalentQuantIds(state.CandidateQuantId); - if (equivalentIds.Count == 0) - return string.Empty; - - predicates.Add($"{EffectiveSql("t", column)} IN ({string.Join(",", equivalentIds.Select(x => x.ToString(CultureInfo.InvariantCulture)))})"); - } - else - { - predicates.Add($"{EffectiveSql("t", column)} = {state.CandidateQuantId}"); - } - } - - if (tier == SynergyTemplateMatchTier.ExactContext) - { - var states = selected.ToDictionary(x => x.TensorGroupId, x => x.CandidateQuantId); - foreach (var group in ActiveGroups()) - { - string? column = ColumnNameForGroupId(group.UniqueId); - if (column == null) - return string.Empty; - - byte expected = states.TryGetValue(group.UniqueId, out var q) ? q : rule.ReferenceQuantId; - predicates.Add($"{EffectiveSql("t", column)} = {expected}"); - } - } - else - { - // Transfer tiers are deliberately weaker than exact context. Keep the - // original discovery row out of transfer-tier matching so it does not - // receive duplicate exact + generalized adjustments. - var selectedGroupIds = selected.Select(x => x.TensorGroupId).ToHashSet(); - var surroundingDifferencePredicates = new List(); - - foreach (var group in ActiveGroups()) - { - if (selectedGroupIds.Contains(group.UniqueId)) - continue; - - string? column = ColumnNameForGroupId(group.UniqueId); - if (column == null) - return string.Empty; - - surroundingDifferencePredicates.Add($"{EffectiveSql("t", column)} <> {rule.ReferenceQuantId}"); - } - - if (surroundingDifferencePredicates.Count > 0) - predicates.Add("(" + string.Join(" OR ", surroundingDifferencePredicates) + ")"); - } - - if (tier == SynergyTemplateMatchTier.EquivalentQuantFamily) - { - // Equivalent-family matching must be meaningfully broader than exact - // same-selected-group matching; otherwise the same rows receive both - // transfer tiers. Require at least one selected group to use a related - // non-identical quant family member. - var selectedQuantDifferencePredicates = new List(); - foreach (var state in selected) - { - string? column = ColumnNameForGroupId(state.TensorGroupId); - if (column == null) - return string.Empty; - - selectedQuantDifferencePredicates.Add($"{EffectiveSql("t", column)} <> {state.CandidateQuantId}"); - } - - if (selectedQuantDifferencePredicates.Count > 0) - predicates.Add("(" + string.Join(" OR ", selectedQuantDifferencePredicates) + ")"); + predicates.Add($"(CASE WHEN {column} = 0 THEN BaseQuant ELSE CAST({column} AS INTEGER) - 1 END) = {state.CandidateQuantId}"); } - string virtualTwinJoin = BuildVirtualTwinJoinPredicate(selected); - if (string.IsNullOrWhiteSpace(virtualTwinJoin)) + if (string.Equals(rule.RuleDirection, AnomalyRuleDirection.SuppressionOnly.ToString(), StringComparison.OrdinalIgnoreCase)) return string.Empty; - return $@" SELECT t.{CombinationDuckDbSchema.SlotColumnList.Replace(", ", ", t.")}, - COALESCE(t.BaseRankSafeKld, t.PredictedKld) AS CandidateBaseRankSafeKld, - COALESCE(t.PredictedKld, t.BaseRankSafeKld) AS CandidatePredictedKld, - COALESCE(vt.BaseRankSafeKld, vt.PredictedKld) AS VirtualTwinBaseRankSafeKld, - COALESCE(vt.PredictedKld, vt.BaseRankSafeKld) AS VirtualTwinPredictedKld - FROM {CombinationDuckDbSchema.TableName} t - JOIN {CombinationDuckDbSchema.TableName} vt ON {virtualTwinJoin} - WHERE {string.Join(" AND ", predicates)}"; - } - - private static string BuildVirtualTwinJoinPredicate(IReadOnlyList selected) - { - var selectedMap = selected.ToDictionary(x => x.TensorGroupId, x => x.ReferenceQuantId); - var predicates = new List - { - "vt.BaseQuant = t.BaseQuant", - "vt.BaseRankSafeKld IS NOT NULL" - }; - - foreach (var group in ActiveGroups()) - { - string? column = ColumnNameForGroupId(group.UniqueId); - if (column == null) - return string.Empty; - - if (selectedMap.TryGetValue(group.UniqueId, out var referenceQuantId)) - predicates.Add($"{EffectiveSql("vt", column)} = {referenceQuantId}"); - else - predicates.Add($"{EffectiveSql("vt", column)} = {EffectiveSql("t", column)}"); - } - return string.Join(" AND ", predicates); } - private static string EffectiveSql(string alias, string column) => - $"(CASE WHEN {alias}.{column} = 0 THEN {alias}.BaseQuant ELSE CAST({alias}.{column} AS INTEGER) - 1 END)"; - - private static IReadOnlyList EquivalentQuantIds(byte quantId) - { - int tier = EffectiveTier(quantId); - if (tier < 0) - return Array.Empty(); - - return BaselineQuants.GetAllRecognizedBaselines() - .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) - .Where(x => EffectiveTier(x.UniqueId) == tier) - .Select(x => x.UniqueId) - .Distinct() - .OrderBy(x => x) - .ToList(); - } - - private static bool IsRuleUsable(AnomalyInteractionRule rule) - { - if (rule.GroupStates.Count == 0) - return false; - - if (rule.RuleStatus != AnomalyRuleStatus.Confirmed.ToString()) - return false; - - if (rule.RuleDirection != AnomalyRuleDirection.Beneficial.ToString() && - rule.RuleDirection != AnomalyRuleDirection.Harmful.ToString()) - { - return false; - } - - if (BaselineQuants.IsNativeExactAlias(rule.ReferenceQuantId)) - return false; - - return rule.GroupStates.All(x => - !BaselineQuants.IsNativeExactAlias(x.CandidateQuantId) && - !BaselineQuants.IsNativeExactAlias(x.ReferenceQuantId)); - } - private static IReadOnlyList ActiveGroups() { TensorGroup[] ordered = @@ -464,8 +205,8 @@ private static IReadOnlyList ActiveGroups() private static async Task ReRankAsync(DuckDBConnection c, CancellationToken ct) { await ExecuteAsync(c, $@" -DROP TABLE IF EXISTS temp_synergy_rerank; -CREATE TEMP TABLE temp_synergy_rerank AS +DROP TABLE IF EXISTS temp_anomaly_rerank; +CREATE TEMP TABLE temp_anomaly_rerank AS SELECT {CombinationDuckDbSchema.SlotColumnList}, CAST(ROW_NUMBER() OVER ( ORDER BY COALESCE(FinalPredictedKld, PredictedKld) ASC, @@ -490,32 +231,32 @@ AND PredictionConfidence IS NOT NULL UPDATE {CombinationDuckDbSchema.TableName} t SET PredictionRank = r.NewPredictionRank -FROM temp_synergy_rerank r +FROM temp_anomaly_rerank r WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "r")};", ct); } - private static async Task CountMatchesAsync(DuckDBConnection c, string matchSql, CancellationToken ct) + private static async Task CountMatchesAsync(DuckDBConnection c, string where, CancellationToken ct) { using var cmd = c.CreateCommand(); - cmd.CommandText = $"SELECT COUNT(*) FROM ({matchSql}) q;"; + cmd.CommandText = $"SELECT COUNT(*) FROM {CombinationDuckDbSchema.TableName} WHERE {where};"; return ToInt64(await cmd.ExecuteScalarAsync(ct)); } - private static async Task LoadPredictionStatsAsync(DuckDBConnection c, string matchSql, CancellationToken ct) + + private static async Task LoadPredictionStatsAsync(DuckDBConnection c, string where, CancellationToken ct) { using var cmd = c.CreateCommand(); cmd.CommandText = $@" -SELECT AVG(COALESCE(t.BaseRankSafeKld, t.PredictedKld)), - AVG(COALESCE(t.FinalPredictedKld, t.PredictedKld)), - AVG(COALESCE(t.AnomalyAdjustmentKld, 0.0)) -FROM {CombinationDuckDbSchema.TableName} t -JOIN ({matchSql}) m ON {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "m")};"; +SELECT AVG(COALESCE(BaseRankSafeKld, PredictedKld)), + AVG(COALESCE(FinalPredictedKld, PredictedKld)) +FROM {CombinationDuckDbSchema.TableName} +WHERE {where};"; using var r = await cmd.ExecuteReaderAsync(ct); if (!await r.ReadAsync(ct)) - return new PredictionMatchStats(0d, 0d, 0d); + return new PredictionMatchStats(0d, 0d); - return new PredictionMatchStats(ToDouble(r.GetValue(0)), ToDouble(r.GetValue(1)), ToDouble(r.GetValue(2))); + return new PredictionMatchStats(ToDouble(r.GetValue(0)), ToDouble(r.GetValue(1))); } private static ActualRuleEffect ExtractActualEffect(AnomalyInteractionRule rule) @@ -538,17 +279,6 @@ private static ActualRuleEffect ExtractActualEffect(AnomalyInteractionRule rule) } } - private static string ResolveTemplateType(AnomalyInteractionRule rule) - { - return rule.RuleType switch - { - "SingleGroupInversion" => "CounterfactualSynergy.SingleGroupInversion", - "PairSynergy" => "CounterfactualSynergy.PairSynergy", - "HigherOrderSynergy" => "CounterfactualSynergy.HigherOrderSynergy", - _ => $"CounterfactualSynergy.{rule.RuleType}" - }; - } - private static double? TryGetDouble(JsonElement element, string propertyName) { return element.TryGetProperty(propertyName, out var value) && value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var d) @@ -556,6 +286,8 @@ private static string ResolveTemplateType(AnomalyInteractionRule rule) : null; } + private static string FmtNullable(double? value) => value.HasValue ? value.Value.ToString("0.000000") : "n/a"; + private static double ToDouble(object? value) { if (value is null || value is DBNull) @@ -564,7 +296,7 @@ private static double ToDouble(object? value) if (value is BigInteger big) return (double)big; - return Convert.ToDouble(value, CultureInfo.InvariantCulture); + return Convert.ToDouble(value); } private static long ToInt64(object? value) @@ -575,7 +307,7 @@ private static long ToInt64(object? value) if (value is BigInteger big) return (long)big; - return Convert.ToInt64(value, CultureInfo.InvariantCulture); + return Convert.ToInt64(value); } private static async Task ExecuteAsync(DuckDBConnection c, string sql, CancellationToken ct) @@ -591,45 +323,18 @@ private static async Task ConfigureSessionAsync(DuckDBConnection c, Cancellation await ExecuteAsync(c, $"SET threads = {Math.Max(1, Environment.ProcessorCount)};", ct); } - private static string SqlDouble(double value) => value.ToString(CultureInfo.InvariantCulture); + private static string SqlDouble(double value) => value.ToString(System.Globalization.CultureInfo.InvariantCulture); private static string DescribeRule(AnomalyInteractionRule rule) { return string.Join(" + ", rule.GroupStates .OrderBy(x => x.SortOrder) .Select(x => $"{ColumnNameForGroupId(x.TensorGroupId)}={SafeName(x.CandidateQuantId)}")) + - $" transferred from {SafeName(rule.ReferenceQuantId)} dome"; - } - - private static int EffectiveTier(byte quantId) - { - if (BaselineQuants.IsNativeExactAlias(quantId)) - return 160; - - var baseline = BaselineQuants.FromId(quantId); - string name = baseline.Names[0].ToUpperInvariant(); - - if (name.Contains("Q8") || baseline.BitRange >= 8) return 80; - if (name.Contains("Q6") || baseline.BitRange == 6) return 60; - if (name.Contains("Q5") || baseline.BitRange == 5) return 50; - if (name.Contains("Q4") || name.Contains("IQ4") || baseline.BitRange == 4) return 40; - if (name.Contains("Q3") || name.Contains("IQ3") || baseline.BitRange == 3) return 30; - if (name.Contains("Q2") || name.Contains("IQ2") || baseline.BitRange == 2) return 20; - - return baseline.BitRange > 0 ? baseline.BitRange * 10 : -1; + $" in {SafeName(rule.ReferenceQuantId)} context"; } - private readonly record struct PredictionMatchStats(double AverageBasePredictedKld, double AverageFinalPredictedKld, double AverageAnomalyAdjustmentKld); + private readonly record struct PredictionMatchStats(double AverageBasePredictedKld, double AverageFinalPredictedKld); private readonly record struct ActualRuleEffect(double? CandidateKld, double? TwinKld, double? GainOrHarm, bool HasActualEffect); - private readonly record struct RuleTierApplyResult( - SynergyTemplateMatchTier Tier, - long MatchedRows, - double Multiplier, - double EffectiveConfidence, - PredictionMatchStats Before, - PredictionMatchStats After, - string CandidatePredicateReason, - string VirtualTwinPredicateReason); private static string SafeName(byte quantId) { @@ -642,4 +347,4 @@ private static string SafeName(byte quantId) return $"id:{quantId}"; } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/AnomalyRuleRepository.cs b/MagicQuant/Services/AnomalyRuleRepository.cs index 8a7f31c..f42bbc8 100644 --- a/MagicQuant/Services/AnomalyRuleRepository.cs +++ b/MagicQuant/Services/AnomalyRuleRepository.cs @@ -193,10 +193,6 @@ public async Task> UpsertRulesFromResultsA CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions), InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), - ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), - CandidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), - ReferenceInternalName = TensorConfigIdentity.ToKey(first.Plan.ReferenceConfig), - CandidateInternalName = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), RuleDirection = direction, GroupSetHash = groupSetHash, CreatedUtc = DateTime.UtcNow @@ -225,38 +221,24 @@ public async Task> UpsertRulesFromResultsA rule.CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions); rule.InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions); rule.FullTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig); - rule.ReferenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig); - rule.CandidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig); - rule.ReferenceInternalName = TensorConfigIdentity.ToKey(first.Plan.ReferenceConfig); - rule.CandidateInternalName = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig); rule.UpdatedUtc = DateTime.UtcNow; rule.MetadataJson = JsonSerializer.Serialize(new { - source = "counterfactual-synergy-template", - terminology = "SynergyTemplate/CounterfactualSynergy. Existing Anomaly* entity names are retained for compatibility.", + source = "counterfactual-twin-probe", isContextualAnomalyProbe = true, oldBf16Isolation = false, allActiveGroupsExplicit = true, - templateType = ResolveRuleType(rows), - generalizationPolicy = "ExactStrong_TransferWeak", - selectedGroupStates = probeGroups.ToDictionary(x => x.Group.Name, x => BaselineQuants.FromId(x.CandidateQuantId).Names[0]), - raisedCounterfactualStates = probeGroups.ToDictionary(x => x.Group.Name, x => BaselineQuants.FromId(x.ReferenceQuantId).Names[0]), - discoveryContext = _movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), - candidateContext = _movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), inactiveGroups = _movement.BuildInactiveGroupList(), - referenceTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ReferenceConfig), - candidateTensorConfigKey = TensorConfigIdentity.ToKey(first.Plan.ProbeConfig), - referenceDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ReferenceConfig), - candidateDisplayName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)first.Plan.ProbeConfig), first.Plan.ProbeType, first.Plan.HypothesisLabel, actualCandidateKld = first.ProbeSnapshot?.Kld, actualTwinKld = first.ReferenceSnapshot?.Kld, actualGainOrHarm = first.ActualGainVsTwin, - sizeSavingsBytes = ComputeSizeSavings(first.ReferenceSnapshot, first.ProbeSnapshot), - adjustmentReason = "prediction-space-virtual-twin-rank-movement", + adjustmentReason = first.ReferenceSnapshot != null && first.ProbeSnapshot != null + ? "measured-actual-counterfactual-effect" + : "prediction-space-gap-fallback", groups = probeGroups.Select(ToGroupLog).ToList() }, JsonOptions); @@ -417,9 +399,11 @@ private static string ResolveRuleType(IReadOnlyList rows) { "single" => "SingleGroupInversion", "pair" => "PairSynergy", + "composition" => rows.Any(x => x.RuleDirection == AnomalyRuleDirection.Harmful) ? "HarmfulInterferenceComposition" : "CounterfactualSynergyComposition", + "confirmed-neighborhood" => rows.Any(x => x.Classification == AnomalyProbeClassification.ContaminatingPassenger) ? "ContaminatingPassenger" : "ConfirmedAnomalyNeighborhood", "full" => rows.Any(x => x.Plan.ProbeGroups.Count >= 3) ? "HigherOrderSynergy" : "PairSynergy", "leave-one-out" => "HigherOrderSynergy", - _ => "ContextOnly" + _ => rows.Any(x => x.Classification == AnomalyProbeClassification.ContaminatingPassenger) ? "ContaminatingPassenger" : "ContextOnly" }; } @@ -436,10 +420,12 @@ private static double ComputeConfidence(IReadOnlyList rows) private static double ComputePredictionAdjustment(AnomalyProbeResult result, double confidence) { var cfg = Config.AnomalyDetection; + var synergy = Config.SynergyDetection; - // Store a conservative fallback only. Runtime application now uses virtual same-context - // twins and computes row-local prediction-space movement. Actual KLD is preserved in - // metadata/evidence, but is not pasted directly into predicted KLD. + // Prediction KLD is rank-relative. Even when real probes confirm a beneficial + // counterfactual effect, the adjustment is sized by how far the candidate must + // move in prediction space to sit below its virtual/same-context twin. Actual + // KLD affects confidence/classification, not raw numeric subtraction. double baseGap = result.Plan.Seed.PredictionSpaceGapVsTwin; double required = result.RuleDirection switch { @@ -448,9 +434,20 @@ private static double ComputePredictionAdjustment(AnomalyProbeResult result, dou _ => 0d }; - double adjusted = required * confidence * cfg.AnomalyAdjustmentShrinkFactor; + double multiplier = result.Plan.SeedClass switch + { + AnomalySeedClass.SynergyCompositionProbe => synergy.SameSelectedGroupsConfidenceMultiplier, + AnomalySeedClass.SynergyTransferProbe => synergy.SameSelectedGroupsConfidenceMultiplier, + AnomalySeedClass.ConfirmedAnomalyNeighborhoodProbe => synergy.SameSelectedGroupsConfidenceMultiplier, + _ => synergy.ExactContextConfidenceMultiplier + }; + + if (result.Classification == AnomalyProbeClassification.ContaminatingPassenger) + multiplier *= synergy.ContaminationPenaltyConfidenceMultiplier; + + double adjusted = required * confidence * cfg.AnomalyAdjustmentShrinkFactor * multiplier; if (adjusted < 0d) - return Math.Max(adjusted, -Config.SynergyDetection.MaxNegativeAdjustmentKld); + return Math.Max(adjusted, -Math.Min(cfg.MaxNegativeAdjustmentKld, synergy.MaxNegativeAdjustmentKld)); return Math.Min(adjusted, cfg.MaxPositiveAdjustmentKld); } diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs index 862e16a..b828b37 100644 --- a/MagicQuant/Services/AnomalyWorkflowService.cs +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -54,7 +54,7 @@ public async Task RunAsync( return new AnomalyRunResult(); } - AnsiConsole.Write(new Rule("[yellow]Counterfactual Synergy Smoke / Probe Pass[/]") { Justification = Justify.Left }); + AnsiConsole.Write(new Rule("[yellow]Counterfactual Anomaly Smoke / Probe Pass[/]") { Justification = Justify.Left }); var session = await _rules.StartSessionAsync("prediction-guided-selection", ct); try @@ -85,6 +85,7 @@ public async Task RunAsync( await WriteJsonAsync("magicquant-synergy-smoke-scan.json", new { generatedAtUtc = DateTime.UtcNow, + terminology = "CounterfactualSynergy smoke. Anomaly names are retained as backward-compatible aliases.", historicalCount = historical.Count, duckPredictionSpaceCount = duck.Count, selectedSmokeCount = smoke.Count, @@ -105,32 +106,28 @@ public async Task RunAsync( results = results.Concat(expansionResults).ToList(); } - var transferProbes = await PlanConfirmedSynergyTransferProbesAsync(results, planningDiagnostics, ct); - if (transferProbes.Count > 0) + var compositionDiagnostics = new List(); + var compositionProbes = await PlanSynergyCompositionProbesAsync(results, planningDiagnostics, compositionDiagnostics, ct); + if (compositionProbes.Count > 0) { - probes = probes.Concat(transferProbes).ToList(); - var transferResults = await ValidateProbesAsync(transferProbes, ct); - results = results.Concat(transferResults).ToList(); + probes = probes.Concat(compositionProbes).ToList(); + var compositionResults = await ValidateProbesAsync(compositionProbes, ct); + results = results.Concat(compositionResults).ToList(); + compositionDiagnostics = BuildCompositionDiagnostics(compositionProbes, compositionResults); } - await WriteJsonAsync("magicquant-anomaly-probes.json", new + await WriteJsonAsync("magicquant-synergy-composition-probes.json", new { generatedAtUtc = DateTime.UtcNow, - planningDiagnostics, - probes = probes.Select(ToProbeLog).ToList() + compositionProbes = compositionDiagnostics }, ct); - await WriteJsonAsync("magicquant-synergy-probes.json", new + + await WriteJsonAsync("magicquant-anomaly-probes.json", new { generatedAtUtc = DateTime.UtcNow, planningDiagnostics, probes = probes.Select(ToProbeLog).ToList() }, ct); - await WriteJsonAsync("magicquant-synergy-transfer-probes.json", new - { - generatedAtUtc = DateTime.UtcNow, - planningDiagnostics, - probes = transferProbes.Select(ToProbeLog).ToList() - }, ct); await _rules.PersistProbeResultsAsync(session.Id, results, ct); var upsertedRules = await _rules.UpsertRulesFromResultsAsync(results, ct); @@ -148,9 +145,16 @@ public async Task RunAsync( await WriteJsonAsync("magicquant-synergy-templates.json", new { generatedAtUtc = DateTime.UtcNow, - upserted = upsertedRules.Select(ToSynergyTemplateLog).ToList(), - applicable = applicableRules.Select(ToSynergyTemplateLog).ToList() + templates = applicableRules.Select(ToSynergyTemplateLog).ToList() }, ct); + await WriteJsonAsync("magicquant-synergy-probes.json", results.Select(ToResultLog).ToList(), ct); + await WriteJsonAsync("magicquant-synergy-transfer-probes.json", results + .Where(x => x.Plan.SeedClass == AnomalySeedClass.SynergyTransferProbe || x.Plan.SeedClass == AnomalySeedClass.ConfirmedAnomalyNeighborhoodProbe) + .Select(ToResultLog).ToList(), ct); + + var wingSummary = BuildSynergyWingSummary(smoke, results, adjustment); + WriteSynergyWingConsoleSummary(wingSummary); + await WriteJsonAsync("magicquant-synergy-wing-summary.json", wingSummary, ct); await WriteJsonAsync("magicquant-anomaly-adjusted-predictions-summary.json", adjustment, ct); await WriteJsonAsync("magicquant-synergy-adjusted-predictions-summary.json", adjustment, ct); @@ -162,21 +166,7 @@ public async Task RunAsync( results = results.Select(ToResultLog).ToList(), rules = applicableRules.Select(ToRuleLog).ToList(), bestConfirmedAnomaly = bestAnomaly, - adjustment, - synergyTerminology = "Anomaly entity names are retained for compatibility; confirmed beneficial rules are treated as transferable counterfactual synergy templates." - }, ct); - await WriteFinalManifestAsync("magicquant.synergy.json", new - { - generatedAtUtc = DateTime.UtcNow, - modeEnabled = Config.AnomalyDetection.Enabled && Config.SynergyDetection.Enabled, - confirmedBeneficialTemplates = applicableRules.Count(x => x.RuleDirection == AnomalyRuleDirection.Beneficial.ToString()), - harmfulInteractions = applicableRules.Count(x => x.RuleDirection == AnomalyRuleDirection.Harmful.ToString()), - suppressionOnlyObservations = results.Count(x => x.RuleDirection == AnomalyRuleDirection.SuppressionOnly), - bestConfirmedSynergy = bestAnomaly, - transferProbesQueued = planningDiagnostics.TransferProbesQueued, - templates = applicableRules.Select(ToSynergyTemplateLog).ToList(), - adjustment, - note = "Q8 remains the discovery dome/control context. Confirmed counterfactual wins are persisted as transferable synergy templates and applied through virtual same-context twins with confidence shrinkage. Physical validation remains final truth." + adjustment }, ct); await WriteFinalManifestAsync("magicquant.prediction-audit.json", new { @@ -363,7 +353,6 @@ private async Task> DetectDuckSmokeAsync(Cancellatio var rejected = new List(); var closestGapFailures = new List(); var existingKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); - var confirmedTemplates = await _rules.LoadApplicableRulesAsync(ct); int skippedIsolation = 0; int skippedSparse = 0; @@ -374,8 +363,6 @@ private async Task> DetectDuckSmokeAsync(Cancellatio int skippedNoTwin = 0; int skippedSavings = 0; int skippedGap = 0; - int belowMinSmokeScore = 0; - int catastrophicGapRejected = 0; int contextualScanned = 0; int twinLookupCount = 0; int dictionaryTwinHits = 0; @@ -441,8 +428,6 @@ private async Task> DetectDuckSmokeAsync(Cancellatio var movement = _movement.Analyze(twin, row.Config); bool matchedConfirmedPattern = existingKeys.Contains(_rules.BuildRuleSuppressionKey(twin, movement.ChangedGroups)); - var matchedTemplateTier = ResolveConfirmedTemplateMatchTier(row.Config, confirmedTemplates, out var wouldMatchConfirmedTemplate); - matchedConfirmedPattern = matchedConfirmedPattern || wouldMatchConfirmedTemplate; if (movement.Classification == AnomalyMovementClassification.MixedTrade) { @@ -498,29 +483,19 @@ private async Task> DetectDuckSmokeAsync(Cancellatio } double gap = row.BaseRankSafeKld - twinRow.BaseRankSafeKld; - if (gap > Config.SynergyDetection.MaxSmokeGapKld) - { - catastrophicGapRejected++; - var preview = AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, "PredictionSpaceGapCatastrophic", matchedConfirmedPattern, true); - closestGapFailures.Add(preview); - continue; - } - - if (gap > Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld) + double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank); + bool gapCatastrophic = gap > Math.Max(Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld, Config.SynergyDetection.MaxSmokeGapKld); + bool scoreTooLow = score < Config.SynergyDetection.MinSmokeScore && !matchedConfirmedPattern; + if (gapCatastrophic || scoreTooLow) { skippedGap++; - var preview = AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, "PredictionSpaceGapTooLargeButSmokeScored", matchedConfirmedPattern, true); + string reason = gapCatastrophic ? "PredictionSpaceGapTooLarge" : "BelowMinSmokeScore"; + var preview = AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, reason, matchedConfirmedPattern, true); closestGapFailures.Add(preview); - } - - double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank, matchedConfirmedPattern, matchedTemplateTier); - if (score < Config.SynergyDetection.MinSmokeScore) - { - belowMinSmokeScore++; - AddRejectedPreview(rejected, row.Config, twin, movement, row, twinRow, savingsBytes, gap, "BelowMinSmokeScore", matchedConfirmedPattern, true, score); continue; } + result.Add(new AnomalySmokeCandidate { Source = "duckdb-prediction-space", @@ -542,8 +517,6 @@ private async Task> DetectDuckSmokeAsync(Cancellatio SmokeStrength = gap <= 0d ? "Strong" : "Close", SeedClass = AnomalySeedClass.PredictionSpaceSmoke, MatchedConfirmedAnomalyPattern = matchedConfirmedPattern, - WouldMatchConfirmedTemplate = wouldMatchConfirmedTemplate, - SynergyMatchTier = matchedTemplateTier, Message = "Prediction-space contextual monotone downgrade candidate is close enough to its higher-bit quantized twin to justify probes. Twin lookup was dictionary-only from the preloaded DuckDB row set." }); } @@ -566,15 +539,13 @@ private async Task> DetectDuckSmokeAsync(Cancellatio MixedTradeIgnored = skippedMixed, SizeSavingsBelowThreshold = skippedSavings, PredictionSpaceGapTooLarge = skippedGap, - BelowMinSmokeScore = belowMinSmokeScore, - CatastrophicGapRejected = catastrophicGapRejected, QueuedSmokeCandidates = result.Count, LoadPredictedRowsMs = loadClock.ElapsedMilliseconds, BuildLookupDictionaryMs = lookupClock.ElapsedMilliseconds, ScanRowsMs = scanClock.ElapsedMilliseconds, RejectedPreview = rejected .OrderBy(x => x.SortOrder) - .Take(Config.SynergyDetection.TopRejectedSmokePreview) + .Take(25) .Select(x => x.ToLog()) .ToList(), ClosestGapFailures = closestGapFailures @@ -587,7 +558,7 @@ private async Task> DetectDuckSmokeAsync(Cancellatio _lastDuckSmokeDiagnostics = diagnostics; - AnsiConsole.MarkupLine("[yellow]DuckDB synergy smoke scan:[/]"); + AnsiConsole.MarkupLine("[yellow]DuckDB contextual smoke scan:[/]"); AnsiConsole.MarkupLine($"[grey] predicted rows scanned=[/] [cyan]{rows.Count:N0}[/]"); AnsiConsole.MarkupLine($"[grey] load predicted rows ms=[/] [cyan]{diagnostics.LoadPredictedRowsMs:N0}[/]"); AnsiConsole.MarkupLine($"[grey] build lookup dictionary ms=[/] [cyan]{diagnostics.BuildLookupDictionaryMs:N0}[/]"); @@ -604,14 +575,18 @@ private async Task> DetectDuckSmokeAsync(Cancellatio AnsiConsole.MarkupLine($"[grey] movement not monotone downgrade=[/] [cyan]{skippedMovement:N0}[/]"); AnsiConsole.MarkupLine($"[grey] mixed trade ignored=[/] [cyan]{skippedMixed:N0}[/]"); AnsiConsole.MarkupLine($"[grey] size savings below threshold=[/] [cyan]{skippedSavings:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] prediction-space gap too large but smoke-scored=[/] [cyan]{skippedGap:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] catastrophic gap rejected=[/] [cyan]{catastrophicGapRejected:N0}[/]"); - AnsiConsole.MarkupLine($"[grey] below min smoke score=[/] [cyan]{belowMinSmokeScore:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] prediction-space gap too large=[/] [cyan]{skippedGap:N0}[/]"); AnsiConsole.MarkupLine($"[grey] queued smoke candidates=[/] [cyan]{result.Count:N0}[/]"); + AnsiConsole.MarkupLine("[yellow]DuckDB synergy smoke timings:[/]"); + AnsiConsole.MarkupLine($"[grey] predicted row scan ms=[/] [cyan]{diagnostics.LoadPredictedRowsMs:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] virtual twin construction ms=[/] [cyan]0[/] [grey](computed inline while scanning)[/]"); + AnsiConsole.MarkupLine($"[grey] twin lookup ms=[/] [cyan]{diagnostics.ScanRowsMs:N0}[/] [grey](dictionary-only in normal operation)[/]"); + AnsiConsole.MarkupLine($"[grey] scoring ms=[/] [cyan]{diagnostics.ScanRowsMs:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] diagnostic formatting ms=[/] [cyan]deferred[/]"); if (result.Count == 0 && rows.Count > 0) { - AnsiConsole.MarkupLine("[yellow]DuckDB synergy smoke scan produced zero candidates.[/] Top rejected-smoke previews and closest gap failures were written to magicquant-anomaly-smoke-scan-duckdb-diagnostics.json."); + AnsiConsole.MarkupLine("[yellow]DuckDB contextual smoke scan produced zero candidates.[/] Top rejected-smoke previews and closest gap failures were written to magicquant-anomaly-smoke-scan-duckdb-diagnostics.json."); foreach (var preview in closestGapFailures.OrderBy(x => x.PredictionSpaceGap ?? double.MaxValue).Take(10)) { AnsiConsole.MarkupLine($"[grey] rejected monotone gap:[/] candidate={Markup.Escape(preview.CandidateName)} twin={Markup.Escape(preview.TwinName)} gap={FmtNullable(preview.PredictionSpaceGap)} savings={FmtNullable(preview.PredictedSizeSavingsBytes)} reason={Markup.Escape(preview.RejectionReason)} matchedRule={preview.MatchedConfirmedAnomalyPattern}"); @@ -622,7 +597,6 @@ private async Task> DetectDuckSmokeAsync(Cancellatio { generatedAtUtc = DateTime.UtcNow, diagnostics, - synergyTerminology = "DuckDB smoke uses counterfactual synergy scoring. Prediction-space gap is no longer the only cliff gate; catastrophic gaps are still rejected.", queued = result.Select(ToSmokeLog).ToList() }, ct); @@ -873,191 +847,204 @@ private async Task> PlanConfirmedAnomalyExpansionProbesAs - private async Task> PlanConfirmedSynergyTransferProbesAsync( - IReadOnlyList currentResults, + private async Task> PlanSynergyCompositionProbesAsync( + IReadOnlyList results, ProbePlanningDiagnostics diagnostics, + List previewRecords, CancellationToken ct) { var cfg = Config.SynergyDetection; - if (!cfg.Enabled || !cfg.TransferProbeEnabled || cfg.MaxTotalTransferProbesPerRun <= 0) - return new List(); + var plans = new List(); + if (!cfg.Enabled || !cfg.CompositionProbeEnabled || cfg.MaxCompositionProbesPerRun <= 0) + return plans; - var beneficialTemplates = currentResults + var templates = results .Where(x => x.RuleDirection == AnomalyRuleDirection.Beneficial) - .Where(x => x.ReferenceSnapshot != null && x.ProbeSnapshot != null) - .Where(x => EstimateTemplateConfidence(x) >= cfg.MinConfidenceToScheduleTransferProbe) - .OrderByDescending(x => x.ActualGainVsTwin) + .Where(x => x.Accepted && x.ReferenceSnapshot != null && x.ProbeSnapshot != null) + .Where(x => x.Plan.ProbeGroups.Count > 0) + .Select(x => new + { + Result = x, + Confidence = Math.Clamp(Math.Abs(x.ActualGainVsTwin) / Math.Max(Config.AnomalyDetection.MinActualGainVsTwinKld, 1e-9), 0d, 1d), + Key = string.Join(",", x.Plan.ProbeGroups.OrderBy(g => g.Group.UniqueId).Select(g => $"{g.Group.UniqueId}:{g.CandidateQuantId}")) + }) + .Where(x => x.Confidence >= cfg.MinTemplateConfidenceForComposition) + .GroupBy(x => x.Key, StringComparer.Ordinal) + .Select(g => g.OrderByDescending(x => x.Result.ActualGainVsTwin).First()) + .OrderByDescending(x => x.Result.ActualGainVsTwin) + .Take(cfg.MaxTemplatesToCompose) .ToList(); - if (beneficialTemplates.Count == 0) - return new List(); - - var rows = await LoadPredictionRowsAsync(DuckSmokeScanLimit, ct); - var lookup = new Dictionary(StringComparer.Ordinal); - var candidates = new Dictionary(StringComparer.Ordinal); - foreach (var row in rows) - { - if (!_movement.TryNormalizeSparseDuckRowToActivatedContext(row.Config, out var activated, out _, out _)) - continue; - - var normalized = row with { Config = activated }; - AddOrPreferBetterPredictionRow(lookup, normalized); - if (TensorConfigIdentity.ToKey(activated) != TensorConfigIdentity.ToKey(_movement.BuildBaseContextTwin(activated))) - AddOrPreferBetterPredictionRow(candidates, normalized); - } - - var existingRuleKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); + int considered = templates.Count; + int candidates = 0; + int reused = 0; var seen = new HashSet(StringComparer.Ordinal); - var plans = new List(); + var existingRuleKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); - foreach (var template in beneficialTemplates) + for (int i = 0; i < templates.Count; i++) { - if (plans.Count >= cfg.MaxTotalTransferProbesPerRun) - break; - - var selectedGroups = template.Plan.ProbeGroups - .Where(x => x.Movement == QuantMovementKind.Downgrade) - .OrderBy(x => x.Group.UniqueId) - .ToList(); - - if (selectedGroups.Count == 0) - continue; - - int perTemplate = 0; - var scored = new List<(PredictionDuckRow Candidate, PredictionDuckRow Twin, TensorConfig VirtualTwin, AnomalyMovementAnalysis Movement, double Gap, ulong SavingsBytes, double Score, string Stratum)>(); - foreach (var row in candidates.Values) + for (int j = i + 1; j < templates.Count; j++) { - if (perTemplate >= cfg.MaxTransferProbesPerTemplate || plans.Count + scored.Count >= cfg.MaxTotalTransferProbesPerRun) + if (plans.Count >= cfg.MaxCompositionProbesPerRun) break; - if (TensorConfigIdentity.ToKey(row.Config) == TensorConfigIdentity.ToKey(template.Plan.ProbeConfig)) + var a = templates[i].Result; + var b = templates[j].Result; + if (a.Plan.ReferenceConfig.BaseQuant != b.Plan.ReferenceConfig.BaseQuant) continue; - if (row.Config.BaseQuant != template.Plan.ReferenceConfig.BaseQuant) - continue; - - bool containsSelected = selectedGroups.All(g => _movement.EffectiveQuantId(row.Config, g.Group) == g.CandidateQuantId); - if (!containsSelected) - continue; + var merged = a.Plan.ProbeGroups + .Concat(b.Plan.ProbeGroups) + .GroupBy(g => g.Group.UniqueId) + .Select(g => g.OrderBy(x => x.CandidateQuantId).First()) + .OrderBy(g => g.Group.UniqueId) + .ToList(); - string stratum = ResolveTransferStratum(row.Config, selectedGroups, template.Plan.ReferenceConfig.BaseQuant); - if (stratum == "disabled-low-fidelity") - { - diagnostics.SkippedTransferStrata++; + if (merged.Count <= Math.Max(a.Plan.ProbeGroups.Count, b.Plan.ProbeGroups.Count)) continue; - } - - var virtualTwin = row.Config; - foreach (var group in selectedGroups) - virtualTwin = _movement.WithStoredSlot(virtualTwin, group.Group, group.ReferenceStoredSlot); - - if (ShouldSkipInvalidContextualAnomalyConfig(virtualTwin, "synergy-transfer-virtual-twin", out _) || - ShouldSkipInvalidContextualAnomalyConfig(row.Config, "synergy-transfer-candidate", out _)) - { - diagnostics.SkippedInvalidMovement++; + if (merged.Count > cfg.MaxTemplateCompositionGroupCount) continue; - } - string virtualTwinKey = TensorConfigIdentity.ToKey(virtualTwin); - if (!lookup.TryGetValue(virtualTwinKey, out var twinRow)) - { - diagnostics.SkippedMissingVirtualTwin++; - continue; - } + candidates++; + var reference = _movement.CreateActivatedContextBlanket(a.Plan.ReferenceConfig.BaseQuant); + var probe = reference; + foreach (var group in merged) + probe = _movement.WithStoredSlot(probe, group.Group, group.CandidateStoredSlot); - var movement = _movement.Analyze(virtualTwin, row.Config); - if (movement.Classification != AnomalyMovementClassification.MonotoneDowngrade) + if (ShouldSkipInvalidContextualAnomalyConfig(probe, "synergy-composition", out var skipReason)) { diagnostics.SkippedInvalidMovement++; + previewRecords.Add(BuildCompositionPreview(a, b, reference, probe, "SkippedInvalidContextualProbe", skipReason)); continue; } - if (existingRuleKeys.Contains(_rules.BuildRuleSuppressionKey(virtualTwin, selectedGroups))) + if (existingRuleKeys.Contains(_rules.BuildRuleSuppressionKey(reference, merged))) { diagnostics.SkippedExistingRuleOrSuppression++; + reused++; + previewRecords.Add(BuildCompositionPreview(a, b, reference, probe, "ExistingRuleOrSuppression", "A matching composition rule/suppression already exists.")); continue; } - string seenKey = TensorConfigIdentity.ToKey(virtualTwin) + "=>" + TensorConfigIdentity.ToKey(row.Config); - if (!seen.Add(seenKey)) + string key = TensorConfigIdentity.ToKey(reference) + "=>" + TensorConfigIdentity.ToKey(probe); + if (!seen.Add(key)) { diagnostics.SkippedDuplicate++; + reused++; continue; } - if (twinRow.PredictedSizeBytes <= row.PredictedSizeBytes) - continue; - - ulong savings = twinRow.PredictedSizeBytes - row.PredictedSizeBytes; - double savingsPercent = savings * 100d / Math.Max(1d, twinRow.PredictedSizeBytes); - double gap = row.BaseRankSafeKld - twinRow.BaseRankSafeKld; - if (gap > cfg.MaxSmokeGapKld) - continue; - - double score = ComputeSmokeScore(gap, savingsPercent, movement.DowngradeCount, row.PredictionRank, twinRow.PredictionRank, true, SynergyTemplateMatchTier.SameSelectedGroups); - scored.Add((row, twinRow, virtualTwin, movement, gap, savings, score, stratum)); - } - - foreach (var item in scored - .OrderByDescending(x => x.Score) - .ThenBy(x => x.Candidate.BaseRankSafeKld) - .ThenBy(x => x.Candidate.PredictedSizeBytes) - .Take(cfg.MaxTransferProbesPerTemplate)) - { - if (plans.Count >= cfg.MaxTotalTransferProbesPerRun) - break; - + var movement = _movement.Analyze(reference, probe); var seed = new AnomalySmokeCandidate { - Source = "synergy-transfer-smoke", - CandidateConfig = item.Candidate.Config, - TwinConfig = item.VirtualTwin, - Movement = item.Movement, - CandidatePredictedKld = item.Candidate.BaseRankSafeKld, - TwinPredictedKld = item.Twin.BaseRankSafeKld, - CandidatePredictedSizeBytes = item.Candidate.PredictedSizeBytes, - TwinPredictedSizeBytes = item.Twin.PredictedSizeBytes, - PredictedSizeSavingsBytes = item.SavingsBytes, - PlannedProbeWillMeasureSize = true, - TwinLookupMode = "virtual-same-context-twin-from-preloaded-duckdb-lookup", - TwinFoundInLookupDictionary = true, - PredictionSpaceGapVsTwin = item.Gap, - CandidatePredictionRank = item.Candidate.PredictionRank, - TwinPredictionRank = item.Twin.PredictionRank, - SmokeScore = item.Score, - SmokeStrength = "SynergyTransferProbe", - SeedClass = AnomalySeedClass.SynergyTransferProbe, + Source = "counterfactual-synergy-composition", + CandidateConfig = probe, + TwinConfig = reference, + Movement = movement, + SmokeScore = 950_000d + Math.Max(0d, a.ActualGainVsTwin) + Math.Max(0d, b.ActualGainVsTwin), + SmokeStrength = "CompositionProbe", + SeedClass = AnomalySeedClass.SynergyCompositionProbe, MatchedConfirmedAnomalyPattern = true, - WouldMatchConfirmedTemplate = true, - SynergyMatchTier = SynergyTemplateMatchTier.SameSelectedGroups, - IsTransferProbeSeed = true, - Message = $"Transfer probe from confirmed counterfactual synergy template. stratum={item.Stratum}." + PlannedProbeWillMeasureSize = true, + Message = "Tiny composition probe: tests whether two confirmed synergy templates cooperate, add, overlap redundantly, or interfere." }; plans.Add(new AnomalyProbePlan { Seed = seed, - ReferenceConfig = item.VirtualTwin, - ProbeConfig = item.Candidate.Config, - ProbeGroups = selectedGroups, - ProbeType = "synergy-transfer", - HypothesisLabel = _movement.DescribeGroups(selectedGroups), - SeedClass = AnomalySeedClass.SynergyTransferProbe, - ProbePriorityClass = AnomalySeedClass.SynergyTransferProbe + ReferenceConfig = reference, + ProbeConfig = probe, + ProbeGroups = merged, + ProbeType = "composition", + HypothesisLabel = _movement.DescribeGroups(merged), + SeedClass = AnomalySeedClass.SynergyCompositionProbe, + ProbePriorityClass = AnomalySeedClass.SynergyCompositionProbe }); - - perTemplate++; - diagnostics.TransferProbesQueued++; diagnostics.ProbesQueued++; + diagnostics.CompositionProbesQueued++; + previewRecords.Add(BuildCompositionPreview(a, b, reference, probe, "Queued", "Composition probe queued for real benchmark validation.")); } } - if (plans.Count > 0) + AnsiConsole.MarkupLine($"[yellow]Synergy composition probes:[/] source templates considered=[cyan]{considered:N0}[/] composition candidates=[cyan]{candidates:N0}[/] queued=[cyan]{plans.Count:N0}[/] reused/skipped existing=[cyan]{reused:N0}[/]"); + return plans; + } + + private SynergyCompositionProbeRecord BuildCompositionPreview( + AnomalyProbeResult a, + AnomalyProbeResult b, + TensorConfig reference, + TensorConfig probe, + string classification, + string note) + { + var movement = _movement.Analyze(reference, probe); + return new SynergyCompositionProbeRecord { - AnsiConsole.MarkupLine($"[yellow]Confirmed synergy transfer probes:[/] queued={plans.Count:N0} maxTotal={cfg.MaxTotalTransferProbesPerRun:N0}"); + CompositionId = TensorConfigIdentity.ToKey(reference) + "=>" + TensorConfigIdentity.ToKey(probe), + SourceTemplateIds = new[] + { + TensorConfigIdentity.ToKey(a.Plan.ProbeConfig), + TensorConfigIdentity.ToKey(b.Plan.ProbeConfig) + }, + SourceTemplateLabels = new[] { a.Plan.HypothesisLabel, b.Plan.HypothesisLabel }, + CandidateEffectiveGroups = _movement.BuildEffectiveGroupVector(probe), + TwinEffectiveGroups = _movement.BuildEffectiveGroupVector(reference), + CombinedGroupCount = movement.DowngradeCount, + Classification = classification, + ActualCandidateKld = null, + ActualTwinKld = null, + ActualGainVsTwin = null, + PredictedCandidateKld = null, + PredictedTwinKld = null, + PredictionSpaceGap = null, + Notes = new[] { note } + }; + } + + private List BuildCompositionDiagnostics( + IReadOnlyList compositionPlans, + IReadOnlyList compositionResults) + { + var records = new List(); + foreach (var result in compositionResults.Where(x => x.Plan.SeedClass == AnomalySeedClass.SynergyCompositionProbe)) + { + string classification = result.Classification switch + { + AnomalyProbeClassification.HarmfulInteraction => "HarmfulInterference", + AnomalyProbeClassification.ContaminatingPassenger => "HarmfulInterference", + AnomalyProbeClassification.NormalGravity => "RedundantComposition", + AnomalyProbeClassification.SuppressionOnly => "CompositionRejected", + _ when result.RuleDirection == AnomalyRuleDirection.Beneficial && result.ActualGainVsTwin >= Config.AnomalyDetection.MinActualGainVsTwinKld * 2d => "SuperSynergy", + _ when result.RuleDirection == AnomalyRuleDirection.Beneficial => "AdditiveComposition", + _ => "CompositionRejected" + }; + + records.Add(new SynergyCompositionProbeRecord + { + CompositionId = TensorConfigIdentity.ToKey(result.Plan.ReferenceConfig) + "=>" + TensorConfigIdentity.ToKey(result.Plan.ProbeConfig), + SourceTemplateIds = result.Plan.ProbeGroups.Select(g => $"{g.Group.UniqueId}:{g.ReferenceQuantId}->{g.CandidateQuantId}").ToList(), + SourceTemplateLabels = new[] { result.Plan.HypothesisLabel }, + CandidateEffectiveGroups = _movement.BuildEffectiveGroupVector(result.Plan.ProbeConfig), + TwinEffectiveGroups = _movement.BuildEffectiveGroupVector(result.Plan.ReferenceConfig), + CombinedGroupCount = result.Plan.ProbeGroups.Count, + Classification = classification, + ActualCandidateKld = result.ProbeSnapshot?.Kld, + ActualTwinKld = result.ReferenceSnapshot?.Kld, + ActualGainVsTwin = result.ActualGainVsTwin, + PredictedCandidateKld = result.Plan.Seed.CandidatePredictedKld, + PredictedTwinKld = result.Plan.Seed.TwinPredictedKld, + PredictionSpaceGap = result.Plan.Seed.PredictionSpaceGapVsTwin, + Notes = new[] { result.Message } + }); } - return plans; + int super = records.Count(x => x.Classification == "SuperSynergy"); + int additive = records.Count(x => x.Classification == "AdditiveComposition"); + int harmful = records.Count(x => x.Classification == "HarmfulInterference"); + int rejected = records.Count(x => x.Classification == "CompositionRejected" || x.Classification == "RedundantComposition"); + AnsiConsole.MarkupLine($"[yellow]Synergy composition probes:[/] confirmed super-synergy=[cyan]{super:N0}[/] additive=[cyan]{additive:N0}[/] harmful/interference=[cyan]{harmful:N0}[/] rejected/redundant=[cyan]{rejected:N0}[/]"); + return records; } private async Task> ValidateProbesAsync(IReadOnlyList probes, CancellationToken ct) @@ -1171,28 +1158,45 @@ private AnomalyProbeResult ClassifyProbe( { "single" => AnomalyProbeClassification.SingleGroupInversion, "pair" => AnomalyProbeClassification.PairSynergy, + "composition" when gain >= Config.AnomalyDetection.MinActualGainVsTwinKld * 2d => AnomalyProbeClassification.SuperSynergy, + "composition" => AnomalyProbeClassification.AdditiveComposition, "full" when plan.ProbeGroups.Count >= 3 => AnomalyProbeClassification.HigherOrderSynergy, _ => AnomalyProbeClassification.CounterfactualMdaViolation }, RuleDirection = AnomalyRuleDirection.Beneficial, Accepted = true, ActualGainVsTwin = gain, - Message = "Lower-fidelity monotone probe beat its higher-fidelity same-context twin." + Message = plan.ProbeType == "composition" + ? "Composed counterfactual synergy template beat its higher-fidelity same-context twin." + : "Lower-fidelity monotone probe beat its higher-fidelity same-context twin." }; } - if (probe.Kld - reference.Kld >= Config.AnomalyDetection.MinActualGainVsTwinKld) + double harmfulMargin = Math.Max(Config.AnomalyDetection.MinActualGainVsTwinKld, Config.SynergyDetection.MinFailureMarginForContaminationKld); + if (probe.Kld - reference.Kld >= harmfulMargin) { + bool contaminatingPassenger = Config.SynergyDetection.ContaminatingPassengerDetectionEnabled && + (plan.SeedClass == AnomalySeedClass.ConfirmedAnomalyNeighborhoodProbe || + plan.SeedClass == AnomalySeedClass.SynergyTransferProbe || + plan.SeedClass == AnomalySeedClass.SynergyCompositionProbe) && + plan.ProbeGroups.Count > 1; + return new AnomalyProbeResult { Plan = plan, ReferenceSnapshot = reference, ProbeSnapshot = probe, - Classification = AnomalyProbeClassification.HarmfulInteraction, + Classification = contaminatingPassenger + ? AnomalyProbeClassification.ContaminatingPassenger + : plan.ProbeType == "composition" + ? AnomalyProbeClassification.HarmfulInterference + : AnomalyProbeClassification.HarmfulInteraction, RuleDirection = AnomalyRuleDirection.Harmful, Accepted = true, ActualGainVsTwin = gain, - Message = "Probe was meaningfully worse than its higher-fidelity twin; persisted as harmful interaction." + Message = contaminatingPassenger + ? "Probe was meaningfully worse than its twin; persisted as scoped contaminating-passenger negative evidence." + : "Probe was meaningfully worse than its higher-fidelity twin; persisted as harmful interaction." }; } @@ -1477,8 +1481,7 @@ private RejectedSmokePreview AddRejectedPreview( double? predictionSpaceGap, string rejectionReason, bool matchedConfirmedAnomalyPattern, - bool twinFoundInLookup, - double? smokeScore = null) + bool twinFoundInLookup) { var preview = new RejectedSmokePreview( previews.Count, @@ -1491,8 +1494,7 @@ private RejectedSmokePreview AddRejectedPreview( predictionSpaceGap, rejectionReason, matchedConfirmedAnomalyPattern, - twinFoundInLookup, - smokeScore); + twinFoundInLookup); if (previews.Count < 500 || rejectionReason.Contains("PredictionSpaceGap", StringComparison.OrdinalIgnoreCase) || matchedConfirmedAnomalyPattern) previews.Add(preview); @@ -1727,160 +1729,17 @@ private static string ResolveProbeType(int subsetCount, int fullCount) return "leave-one-out"; } - private static double ComputeSmokeScore( - double gap, - double savingsPercent, - int changedGroupCount, - ulong? candidateRank, - ulong? twinRank, - bool matchedConfirmedTemplate, - SynergyTemplateMatchTier matchTier) + private static double ComputeSmokeScore(double gap, double savingsPercent, int changedGroupCount, ulong? candidateRank, ulong? twinRank) { - double maxGap = Math.Max(Config.SynergyDetection.MaxSmokeGapKld, 1e-9d); - double closenessScore = Math.Clamp(1d - (Math.Max(0d, gap) / maxGap), 0d, 1d); - double savingsScore = Math.Clamp(savingsPercent / Math.Max(Config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent * 4d, 1e-9d), 0d, 1d); - double noveltyScore = changedGroupCount switch - { - <= 1 => 0.85d, - 2 => 1.00d, - 3 => 0.80d, - _ => 0.65d - }; - - double rankScore = 0.50d; + double maxGap = Math.Max(Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld, Config.SynergyDetection.MaxSmokeGapKld); + double closenessScore = maxGap <= 0d ? 0d : Math.Clamp((maxGap - Math.Max(0d, gap)) / maxGap, 0d, 1d); + double savingsScore = Math.Clamp(savingsPercent / Math.Max(Config.AnomalyDetection.MinPredictedSizeSavingsVsTwinPercent, 0.01d), 0d, 2d) / 2d; + double groupPenalty = 1d / Math.Max(1, changedGroupCount); + double rankBonus = 0d; if (candidateRank.HasValue && twinRank.HasValue) - { - double delta = Math.Clamp((double)twinRank.Value - candidateRank.Value, -50_000d, 50_000d); - rankScore = Math.Clamp(0.50d + (delta / 100_000d), 0d, 1d); - } - - double templateScore = matchTier switch - { - SynergyTemplateMatchTier.ExactContext => 1.00d, - SynergyTemplateMatchTier.SameSelectedGroups => 0.75d, - SynergyTemplateMatchTier.EquivalentQuantFamily => 0.55d, - SynergyTemplateMatchTier.GroupFamilySuspicion => 0.35d, - _ => matchedConfirmedTemplate ? 0.50d : 0.00d - }; - - double frontierScore = gap <= Config.AnomalyDetection.MaxPredictionSpaceGapVsTwinKld ? 1.00d : 0.55d; - - return Math.Clamp( - (savingsScore * 0.25d) + - (closenessScore * 0.30d) + - (templateScore * 0.20d) + - (noveltyScore * 0.10d) + - (rankScore * 0.10d) + - (frontierScore * 0.05d), - 0d, - 1d); - } - - private SynergyTemplateMatchTier ResolveConfirmedTemplateMatchTier( - TensorConfig candidate, - IReadOnlyCollection confirmedTemplates, - out bool matched) - { - matched = false; - foreach (var rule in confirmedTemplates) - { - if (rule.RuleDirection != AnomalyRuleDirection.Beneficial.ToString()) - continue; - - if (rule.ReferenceQuantId != candidate.BaseQuant) - continue; + rankBonus = Math.Clamp((double)twinRank.Value - candidateRank.Value, -10_000d, 10_000d) / 20_000d; - bool exact = !string.IsNullOrWhiteSpace(rule.FullTensorConfigKey) && - string.Equals(rule.FullTensorConfigKey, TensorConfigIdentity.ToKey(candidate), StringComparison.Ordinal); - if (exact) - { - matched = true; - return SynergyTemplateMatchTier.ExactContext; - } - - bool selectedMatch = rule.GroupStates.All(state => - { - var group = _movement.ActiveGroups.FirstOrDefault(g => g.UniqueId == state.TensorGroupId); - return group != null && _movement.EffectiveQuantId(candidate, group) == state.CandidateQuantId; - }); - - if (selectedMatch) - { - matched = true; - return SynergyTemplateMatchTier.SameSelectedGroups; - } - - bool equivalent = rule.GroupStates.All(state => - { - var group = _movement.ActiveGroups.FirstOrDefault(g => g.UniqueId == state.TensorGroupId); - return group != null && QuantTier(_movement.EffectiveQuantId(candidate, group)) == QuantTier(state.CandidateQuantId); - }); - - if (equivalent) - { - matched = true; - return SynergyTemplateMatchTier.EquivalentQuantFamily; - } - } - - return SynergyTemplateMatchTier.None; - } - - private string ResolveTransferStratum( - TensorConfig candidate, - IReadOnlyCollection selectedGroups, - byte referenceQuantId) - { - var selectedIds = selectedGroups.Select(x => x.Group.UniqueId).ToHashSet(); - int belowQ6 = 0; - foreach (var group in _movement.ActiveGroups) - { - if (selectedIds.Contains(group.UniqueId)) - continue; - - byte q = _movement.EffectiveQuantId(candidate, group); - if (QuantTier(q) < 60) - belowQ6++; - } - - var strata = Config.SynergyDetection.TransferProbeContextStrata; - if (belowQ6 <= strata.HighFidelityMaxNonReferenceGroupsBelowQ6) - return "high-fidelity-transfer"; - - if (belowQ6 <= strata.MidFidelityMaxNonReferenceGroupsBelowQ6) - return "mid-fidelity-transfer"; - - return strata.LowFidelityEnabled ? "low-fidelity-transfer" : "disabled-low-fidelity"; - } - - private static double EstimateTemplateConfidence(AnomalyProbeResult result) - { - double gainRatio = Math.Clamp(result.ActualGainVsTwin / Math.Max(Config.AnomalyDetection.MinActualGainVsTwinKld * 2d, 1e-9d), 0d, 1d); - double classificationBonus = result.Classification switch - { - AnomalyProbeClassification.SingleGroupInversion => 0.20d, - AnomalyProbeClassification.PairSynergy => 0.25d, - AnomalyProbeClassification.HigherOrderSynergy => 0.15d, - _ => 0.10d - }; - - return Math.Clamp(gainRatio * 0.75d + classificationBonus, 0d, 1d); - } - - private static int QuantTier(byte quantId) - { - if (BaselineQuants.IsNativeExactAlias(quantId)) - return 160; - - var baseline = BaselineQuants.FromId(quantId); - string name = baseline.Names[0].ToUpperInvariant(); - if (name.Contains("Q8") || baseline.BitRange >= 8) return 80; - if (name.Contains("Q6") || baseline.BitRange == 6) return 60; - if (name.Contains("Q5") || baseline.BitRange == 5) return 50; - if (name.Contains("Q4") || name.Contains("IQ4") || baseline.BitRange == 4) return 40; - if (name.Contains("Q3") || name.Contains("IQ3") || baseline.BitRange == 3) return 30; - if (name.Contains("Q2") || name.Contains("IQ2") || baseline.BitRange == 2) return 20; - return baseline.BitRange > 0 ? baseline.BitRange * 10 : -1; + return Math.Clamp((closenessScore * 0.55d) + (savingsScore * 0.30d) + (groupPenalty * 0.10d) + rankBonus, 0d, 1d); } private static void WriteSmokeConsoleSummary(int historicalCount, int duckCount, IReadOnlyList selected) @@ -1889,7 +1748,7 @@ private static void WriteSmokeConsoleSummary(int historicalCount, int duckCount, int existingTwins = selected.Count(x => x.HasActualTwin); int missingTwins = selected.Count - existingTwins; - AnsiConsole.MarkupLine("[yellow]Synergy smoke scan:[/]"); + AnsiConsole.MarkupLine("[yellow]Anomaly smoke scan:[/]"); AnsiConsole.MarkupLine($"[grey] historical benchmarks scanned smoke=[/] [cyan]{historicalCount:N0}[/]"); AnsiConsole.MarkupLine($"[grey] DuckDB prediction-space smoke=[/] [cyan]{duckCount:N0}[/]"); AnsiConsole.MarkupLine($"[grey] monotone downgrade smoke candidates=[/] [cyan]{monotone:N0}[/]"); @@ -1902,7 +1761,7 @@ private static void WriteProbeOutcome(AnomalyProbeResult result) if (result.RuleDirection == AnomalyRuleDirection.Beneficial && result.ProbeSnapshot != null && result.ReferenceSnapshot != null) { AnsiConsole.MarkupLine( - $"[green]Counterfactual synergy confirmed:[/] candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ProbeSnapshot.Quant))} " + + $"[green]Counterfactual MDA violation confirmed:[/] candidate={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ProbeSnapshot.Quant))} " + $"twin={Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(result.ReferenceSnapshot.Quant))} " + $"actual candidate KLD={result.ProbeSnapshot.Kld:0.000000} actual twin KLD={result.ReferenceSnapshot.Kld:0.000000} " + $"gain={result.ActualGainVsTwin:0.000000} classification={result.Classification}"); @@ -1968,6 +1827,143 @@ private static async Task WriteFinalManifestAsync(string fileName, object payloa } + + + private IReadOnlyList BuildSynergyWingSummary( + IReadOnlyList smoke, + IReadOnlyList results, + AnomalyAdjustmentSummary adjustment) + { + var zones = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["Q8→Q6"] = new SynergyWingSummary { Zone = "Q8→Q6" }, + ["Q6→Q5/Q4"] = new SynergyWingSummary { Zone = "Q6→Q5/Q4" }, + ["Q5→IQ4/IQ3"] = new SynergyWingSummary { Zone = "Q5→IQ4/IQ3" }, + ["Other"] = new SynergyWingSummary { Zone = "Other" } + }; + + foreach (var item in smoke) + zones[ResolveWingZone(item.TwinConfig.BaseQuant, item.Movement.ChangedGroups.Select(x => x.CandidateQuantId))].SmokeCount++; + + foreach (var result in results) + { + var zone = zones[ResolveWingZone(result.Plan.ReferenceConfig.BaseQuant, result.Plan.ProbeGroups.Select(x => x.CandidateQuantId))]; + if (result.RuleDirection == AnomalyRuleDirection.Beneficial) + { + zone.ConfirmedBeneficialTemplates++; + zone.ValidationSuccessCount++; + } + else if (result.RuleDirection == AnomalyRuleDirection.Harmful) + { + zone.HarmfulTemplates++; + zone.ValidationFailureCount++; + } + else + { + zone.SuppressionOnlyTemplates++; + zone.ValidationFailureCount++; + } + } + + foreach (var match in adjustment.RuleMatches) + { + string text = JsonSerializer.Serialize(match); + var zone = text.Contains("Q6_", StringComparison.OrdinalIgnoreCase) && text.Contains("Q8_0", StringComparison.OrdinalIgnoreCase) + ? zones["Q8→Q6"] + : text.Contains("Q5", StringComparison.OrdinalIgnoreCase) || text.Contains("Q4", StringComparison.OrdinalIgnoreCase) + ? zones["Q6→Q5/Q4"] + : zones["Other"]; + + if (text.Contains("Harmful", StringComparison.OrdinalIgnoreCase) || text.Contains("Contaminating", StringComparison.OrdinalIgnoreCase)) + zone.CandidateRowsDemoted++; + else + zone.CandidateRowsAdjustedPositively++; + } + + foreach (var zone in zones.Values) + { + if (zone.ConfirmedBeneficialTemplates == 0 && (zone.HarmfulTemplates > 0 || zone.SuppressionOnlyTemplates > 0)) + zone.Explanation = "No nonlinear wing survived because harmful/suppression evidence dominated or no adjusted candidate beat the frontier line."; + else if (zone.ConfirmedBeneficialTemplates == 0 && zone.SmokeCount == 0) + zone.Explanation = "No contextual synergy smoke was strong enough to probe in this fidelity zone."; + else if (zone.ConfirmedBeneficialTemplates > 0) + zone.Explanation = "Confirmed beneficial counterfactual synergy evidence exists in this fidelity zone."; + else + zone.Explanation = "Smoke existed but did not produce confirmed beneficial evidence."; + } + + return zones.Values.ToList(); + } + + private static string ResolveWingZone(byte referenceQuantId, IEnumerable candidateQuantIds) + { + string reference = SafeName(referenceQuantId); + var candidates = candidateQuantIds.Select(SafeName).ToList(); + if (reference.StartsWith("Q8", StringComparison.OrdinalIgnoreCase) && candidates.Any(x => x.Contains("Q6", StringComparison.OrdinalIgnoreCase))) + return "Q8→Q6"; + if (reference.Contains("Q6", StringComparison.OrdinalIgnoreCase) && candidates.Any(x => x.Contains("Q5", StringComparison.OrdinalIgnoreCase) || x.Contains("Q4", StringComparison.OrdinalIgnoreCase) || x.Contains("IQ4", StringComparison.OrdinalIgnoreCase))) + return "Q6→Q5/Q4"; + if ((reference.Contains("Q5", StringComparison.OrdinalIgnoreCase) || reference.Contains("Q4", StringComparison.OrdinalIgnoreCase)) && candidates.Any(x => x.Contains("IQ4", StringComparison.OrdinalIgnoreCase) || x.Contains("IQ3", StringComparison.OrdinalIgnoreCase) || x.Contains("Q3", StringComparison.OrdinalIgnoreCase))) + return "Q5→IQ4/IQ3"; + return "Other"; + } + + private static void WriteSynergyWingConsoleSummary(IReadOnlyList summaries) + { + AnsiConsole.MarkupLine("[yellow]Synergy wing summary:[/]"); + foreach (var zone in summaries) + { + AnsiConsole.MarkupLine($"[grey] {Markup.Escape(zone.Zone)}:[/] beneficial=[cyan]{zone.ConfirmedBeneficialTemplates:N0}[/], harmful=[cyan]{zone.HarmfulTemplates:N0}[/], suppressed=[cyan]{zone.SuppressionOnlyTemplates:N0}[/], adjusted=[cyan]{zone.CandidateRowsAdjustedPositively:N0}[/], demoted=[cyan]{zone.CandidateRowsDemoted:N0}[/]"); + } + } + + private object ToSynergyTemplateLog(AnomalyInteractionRule x) + { + Dictionary selected = new(StringComparer.OrdinalIgnoreCase); + Dictionary raised = new(StringComparer.OrdinalIgnoreCase); + foreach (var state in x.GroupStates.OrderBy(g => g.SortOrder)) + { + string groupName = ResolveGroupName(state.TensorGroupId); + selected[groupName] = SafeName(state.CandidateQuantId); + raised[groupName] = SafeName(state.ReferenceQuantId); + } + + return new + { + templateType = x.RuleType, + referenceQuant = SafeName(x.ReferenceQuantId), + selectedGroupStates = selected, + raisedCounterfactualStates = raised, + discoveryContext = TryDeserializeDictionary(x.ReferenceEffectiveGroupsJson), + candidateContext = TryDeserializeDictionary(x.CandidateEffectiveGroupsJson), + actualGainKld = x.BestActualGainVsTwin, + sizeSavingsBytes = x.MetadataJson?.Contains("sizeSavingsBytes", StringComparison.OrdinalIgnoreCase) == true ? (object?)"see metadataJson" : null, + x.Confidence, + generalizationPolicy = "ExactStrong_TransferWeak", + metadataJson = x.MetadataJson + }; + } + + private static Dictionary TryDeserializeDictionary(string? json) + { + if (string.IsNullOrWhiteSpace(json)) + return new Dictionary(StringComparer.OrdinalIgnoreCase); + try + { + return JsonSerializer.Deserialize>(json) ?? new Dictionary(StringComparer.OrdinalIgnoreCase); + } + catch + { + return new Dictionary(StringComparer.OrdinalIgnoreCase); + } + } + + private static string ResolveGroupName(byte groupId) + { + var group = TReg.All.FirstOrDefault(x => x.UniqueId == groupId); + return group?.Name ?? $"group:{groupId}"; + } + private object ToSmokeLog(AnomalySmokeCandidate x) { return new @@ -2008,15 +2004,12 @@ private object ToSmokeLog(AnomalySmokeCandidate x) x.TwinLookupMode, x.RejectionReason, x.MatchedConfirmedAnomalyPattern, - wouldMatchConfirmedTemplate = x.WouldMatchConfirmedTemplate, - synergyMatchTier = x.SynergyMatchTier.ToString(), x.TwinFoundInLookupDictionary, seedClass = x.SeedClass.ToString(), x.CandidatePredictionRank, x.TwinPredictionRank, x.SmokeScore, x.SmokeStrength, - x.IsTransferProbeSeed, x.HasActualTwin, x.CandidateActualKld, x.TwinActualKld, @@ -2038,7 +2031,6 @@ private object ToProbeLog(AnomalyProbePlan x) referenceName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ReferenceConfig), probeName = HybridBenchmarkRepository.BuildDisplayName((HybridQuant)x.ProbeConfig), referenceEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ReferenceConfig), - virtualTwinEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ReferenceConfig), candidateEffectiveGroups = _movement.BuildEffectiveGroupVector(x.ProbeConfig), inactiveGroups = _movement.BuildInactiveGroupList(), movementClassification = movement.Classification.ToString(), @@ -2077,42 +2069,6 @@ private object ToResultLog(AnomalyProbeResult x) }; } - private static object ToSynergyTemplateLog(AnomalyInteractionRule x) - { - return new - { - templateType = x.RuleType, - referenceQuant = SafeName(x.ReferenceQuantId), - selectedGroupStates = x.GroupStates.OrderBy(g => g.SortOrder).ToDictionary(g => g.TensorGroupId.ToString(), g => SafeName(g.CandidateQuantId)), - raisedCounterfactualStates = x.GroupStates.OrderBy(g => g.SortOrder).ToDictionary(g => g.TensorGroupId.ToString(), g => SafeName(g.ReferenceQuantId)), - discoveryContext = TryDeserializeJson(x.ReferenceEffectiveGroupsJson), - candidateContext = TryDeserializeJson(x.CandidateEffectiveGroupsJson), - actualGainKld = x.BestActualGainVsTwin, - confidence = x.Confidence, - generalizationPolicy = "ExactStrong_TransferWeak", - ruleDirection = x.RuleDirection, - ruleStatus = x.RuleStatus, - fullTensorConfigKey = x.FullTensorConfigKey, - referenceContextKey = x.ReferenceContextKey, - metadata = TryDeserializeJson(x.MetadataJson) - }; - } - - private static object? TryDeserializeJson(string json) - { - if (string.IsNullOrWhiteSpace(json)) - return null; - - try - { - return JsonSerializer.Deserialize(json); - } - catch - { - return json; - } - } - private static object ToRuleLog(AnomalyInteractionRule x) { return new @@ -2194,8 +2150,7 @@ public RejectedSmokePreview( double? predictionSpaceGap, string rejectionReason, bool matchedConfirmedAnomalyPattern, - bool twinFoundInLookup, - double? smokeScore) + bool twinFoundInLookup) { SortOrder = sortOrder; Candidate = candidate; @@ -2208,7 +2163,6 @@ public RejectedSmokePreview( RejectionReason = rejectionReason; MatchedConfirmedAnomalyPattern = matchedConfirmedAnomalyPattern; TwinFoundInLookup = twinFoundInLookup; - SmokeScore = smokeScore; } public int SortOrder { get; } @@ -2222,7 +2176,6 @@ public RejectedSmokePreview( public string RejectionReason { get; } public bool MatchedConfirmedAnomalyPattern { get; } public bool TwinFoundInLookup { get; } - public double? SmokeScore { get; } public string CandidateName => HybridBenchmarkRepository.BuildDisplayName((HybridQuant)Candidate); public string TwinName => HybridBenchmarkRepository.BuildDisplayName((HybridQuant)Twin); @@ -2233,6 +2186,10 @@ public RejectedSmokePreview( candidateName = CandidateName, twinName = TwinName, movement = Movement?.Classification.ToString() ?? "Unknown", + changedGroups = Movement?.ChangedGroups.Select(g => $"{g.Group.ShortCode}={AnomalyWorkflowService.SafeName(g.CandidateQuantId)}").ToList() ?? new List(), + smokeScore = Movement == null || CandidateRow == null || TwinRow == null || !PredictionSpaceGap.HasValue || !PredictedSizeSavingsBytes.HasValue + ? (double?)null + : AnomalyWorkflowService.ComputeSmokeScore(PredictionSpaceGap.Value, PredictedSizeSavingsBytes.Value * 100d / Math.Max(1d, TwinRow.PredictedSizeBytes), Movement.DowngradeCount, CandidateRow.PredictionRank, TwinRow.PredictionRank), predictedCandidateKld = CandidateRow?.BaseRankSafeKld, predictedTwinKld = TwinRow?.BaseRankSafeKld, predictionSpaceGap = PredictionSpaceGap, @@ -2240,9 +2197,7 @@ public RejectedSmokePreview( predictedTwinSizeBytes = TwinRow?.PredictedSizeBytes, predictedSizeSavingsBytes = PredictedSizeSavingsBytes, rejectionReason = RejectionReason, - smokeScore = SmokeScore, matchedConfirmedAnomalyPattern = MatchedConfirmedAnomalyPattern, - wouldMatchConfirmedTemplate = MatchedConfirmedAnomalyPattern, twinExistedInLookupDictionary = TwinFoundInLookup }; } diff --git a/MagicQuant/Services/FinalArtifactNamingService.cs b/MagicQuant/Services/FinalArtifactNamingService.cs index e6b9a87..43080cc 100644 --- a/MagicQuant/Services/FinalArtifactNamingService.cs +++ b/MagicQuant/Services/FinalArtifactNamingService.cs @@ -44,18 +44,11 @@ public FinalArtifactName BuildName( string externalFamily = NormalizeExternalDisplayName(sourceBaseline.Names[0], externalProviderToken); providerToken = externalProviderToken; - if (Config.ExportExternalLearnedBaselines || snapshot.IsExternalRebuiltBaseline || snapshot.IsMaterializedTensorMapped) - { - // This is a MagicQuant rebuilt/re-uploaded copy of an external learned baseline. - // The artifact name gets an MQ prefix, but the provider remains the upstream source. - quantFamily = $"MQ-{SanitizeToken(externalFamily)}"; - tag = quantFamily; - } - else - { - quantFamily = SanitizeToken(externalFamily); - tag = quantFamily; - } + // Rebuilt/materialized external baselines are still external baselines. + // MagicQuant may rebuild/benchmark/export the file, but the public name must + // not imply MagicQuant invented the quant recipe (no MQ-UD-* labels). + quantFamily = SanitizeToken(externalFamily); + tag = quantFamily; } else { @@ -91,9 +84,7 @@ public string BuildDisplayLabel( { string providerToken = ResolveExternalProviderToken(sourceBaseline); string family = SanitizeToken(NormalizeExternalDisplayName(sourceBaseline.Names[0], providerToken)); - return Config.ExportExternalLearnedBaselines || snapshot.IsExternalRebuiltBaseline || snapshot.IsMaterializedTensorMapped - ? $"{prefix}-MQ-{family}" - : $"{prefix}-{family}"; + return $"{prefix}-{family}"; } return $"{prefix}-LM-{SanitizeToken(snapshot.Quant.BaseQuant.Names[0])}"; @@ -329,10 +320,14 @@ private static string NormalizeExternalDisplayName(string displayName, string pr if (!value.StartsWith(providerToken + "_", StringComparison.OrdinalIgnoreCase) && !value.StartsWith(providerToken + "-", StringComparison.OrdinalIgnoreCase)) { - value = $"{providerToken}_{value}"; + value = $"{providerToken}-{value}"; } - return SanitizeToken(value); + value = SanitizeToken(value); + if (value.StartsWith(providerToken + "_", StringComparison.OrdinalIgnoreCase)) + value = providerToken + "-" + value[(providerToken.Length + 1)..]; + + return value; } private static string MakeUniqueFileName(string desiredFileName, ISet? reservedFileNames) @@ -412,7 +407,13 @@ private string BuildProviderQuantFallback( return string.Empty; if (sanitizedFamily.StartsWith("MQ-", StringComparison.OrdinalIgnoreCase)) + { + if (snapshot?.IsExternalRebuiltBaseline == true || snapshot?.IsExternalPureBaseline == true || + string.Equals(resolvedProvider, "Unsloth", StringComparison.OrdinalIgnoreCase)) + return StripMagicQuantExternalPrefix(sanitizedFamily); + return sanitizedFamily; + } if (snapshot?.IsHybrid == true) { @@ -424,13 +425,21 @@ private string BuildProviderQuantFallback( } if (string.Equals(resolvedProvider, "MagicQuant", StringComparison.OrdinalIgnoreCase)) + { + if (snapshot?.IsExternalRebuiltBaseline == true || snapshot?.IsExternalPureBaseline == true) + return StripMagicQuantExternalPrefix(sanitizedFamily); + return sanitizedFamily.StartsWith("MQ-", StringComparison.OrdinalIgnoreCase) ? sanitizedFamily : $"MQ-{sanitizedFamily}"; + } if (string.Equals(resolvedProvider, "llama.cpp", StringComparison.OrdinalIgnoreCase)) return sanitizedFamily.StartsWith("LM-", StringComparison.OrdinalIgnoreCase) ? sanitizedFamily : $"LM-{sanitizedFamily}"; if (string.Equals(resolvedProvider, "Unsloth", StringComparison.OrdinalIgnoreCase)) { + if (sanitizedFamily.StartsWith("UD_", StringComparison.OrdinalIgnoreCase)) + return "UD-" + sanitizedFamily[3..]; + if (sanitizedFamily.StartsWith("UD-", StringComparison.OrdinalIgnoreCase) || sanitizedFamily.StartsWith("Unsloth", StringComparison.OrdinalIgnoreCase)) return sanitizedFamily; @@ -441,6 +450,15 @@ private string BuildProviderQuantFallback( return sanitizedFamily; } + private static string StripMagicQuantExternalPrefix(string value) + { + if (value.StartsWith("MQ-UD-", StringComparison.OrdinalIgnoreCase)) + return value[3..]; + if (value.StartsWith("MQ-Unsloth", StringComparison.OrdinalIgnoreCase)) + return value[3..]; + return value; + } + private static string ExtractOrdinalFromFileName(string? fileName) { if (string.IsNullOrWhiteSpace(fileName)) diff --git a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs index 247c551..2de4486 100644 --- a/MagicQuant/Services/FinalSurvivorSelectionCliService.cs +++ b/MagicQuant/Services/FinalSurvivorSelectionCliService.cs @@ -28,7 +28,9 @@ public IReadOnlyList Prompt( Enabled = true, Snapshot = snapshot, PlannedFileName = name.FileName, - PlannedDisplayName = name.DisplayName, + // Show the same normalized public short label used by README/manifest tables. + // The full GGUF filename remains PlannedFileName. + PlannedDisplayName = string.IsNullOrWhiteSpace(name.ShortDisplayName) ? name.DisplayName : name.ShortDisplayName, PlannedProviderName = ResolveProviderName(snapshot, name), PlannedQuantFamily = name.QuantFamilyOrBaseline }; diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 8e30c3c..d1db411 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -322,43 +322,6 @@ anomaly_detection: - Q5_K - UD-Q5_K_XL -synergy_detection: - enabled: true - - # One transfer/generalization refinement pass after exact-context synergy templates are confirmed. - max_refinement_rounds: 1 - - # Confidence multipliers for transferring a confirmed counterfactual synergy template. - exact_context_confidence_multiplier: 1.00 - same_selected_groups_confidence_multiplier: 0.55 - equivalent_quant_family_confidence_multiplier: 0.30 - group_family_suspicion_confidence_multiplier: 0.15 - - # Minimum confidence gates for applying transfer adjustments or scheduling transfer probes. - min_confidence_to_apply_adjustment: 0.35 - min_confidence_to_schedule_transfer_probe: 0.25 - - # Transfer adjustments are prediction-space/rank-relative, not raw real-KLD deltas. - max_negative_adjustment_kld: 0.002 - max_negative_adjustment_fraction_of_base_kld: 0.75 - - # Tiny stratified sniff pass around confirmed Q8-dome synergy templates. - transfer_probe_enabled: true - max_transfer_probes_per_template: 6 - max_total_transfer_probes_per_run: 24 - - transfer_probe_context_strata: - high_fidelity_max_non_reference_groups_below_q6: 1 - mid_fidelity_max_non_reference_groups_below_q6: 3 - low_fidelity_enabled: false - - # Smoke scoring augments the old prediction-space gap cliff. - min_smoke_score: 0.55 - max_smoke_gap_kld: 0.004 - top_rejected_smoke_preview: 25 - - verbose_synergy_logging: true - output: # Optional explicit output directory. # If blank, MagicQuant will default to: @@ -498,4 +461,40 @@ baselines: # # Example note: # # If the repo does not actually contain IQ3_XS, do not reference it. # # Use only filenames that truly exist in the repository. - [] \ No newline at end of file + [] + +# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. +# anomaly_detection remains the low-level compatibility section; synergy_detection controls +# template transfer, composition probes, contamination suppression, and wing diagnostics. +synergy_detection: + enabled: true + max_refinement_rounds: 1 + exact_context_confidence_multiplier: 1.00 + same_selected_groups_confidence_multiplier: 0.55 + equivalent_quant_family_confidence_multiplier: 0.30 + group_family_suspicion_confidence_multiplier: 0.15 + min_confidence_to_apply_adjustment: 0.35 + min_confidence_to_schedule_transfer_probe: 0.25 + max_negative_adjustment_kld: 0.002 + max_negative_adjustment_fraction_of_base_kld: 0.75 + transfer_probe_enabled: true + max_transfer_probes_per_template: 6 + max_total_transfer_probes_per_run: 24 + transfer_probe_context_strata: + high_fidelity_max_non_reference_groups_below_q6: 1 + mid_fidelity_max_non_reference_groups_below_q6: 3 + low_fidelity_enabled: false + verbose_synergy_logging: true + min_smoke_score: 0.55 + max_smoke_gap_kld: 0.004 + top_rejected_smoke_preview: 25 + composition_probe_enabled: true + max_template_composition_group_count: 4 + max_composition_probes_per_run: 8 + max_templates_to_compose: 4 + min_template_confidence_for_composition: 0.50 + min_combined_expected_size_savings_percent: 1.0 + contaminating_passenger_detection_enabled: true + min_failure_margin_for_contamination_kld: 0.00050 + contamination_penalty_confidence_multiplier: 0.45 + suppress_repeated_contaminated_attempts: true diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 3b76f49..73482a9 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -244,43 +244,6 @@ anomaly_detection: - Q5_K - UD-Q5_K_XL -synergy_detection: - enabled: true - - # One transfer/generalization refinement pass after exact-context synergy templates are confirmed. - max_refinement_rounds: 1 - - # Confidence multipliers for transferring a confirmed counterfactual synergy template. - exact_context_confidence_multiplier: 1.00 - same_selected_groups_confidence_multiplier: 0.55 - equivalent_quant_family_confidence_multiplier: 0.30 - group_family_suspicion_confidence_multiplier: 0.15 - - # Minimum confidence gates for applying transfer adjustments or scheduling transfer probes. - min_confidence_to_apply_adjustment: 0.35 - min_confidence_to_schedule_transfer_probe: 0.25 - - # Transfer adjustments are prediction-space/rank-relative, not raw real-KLD deltas. - max_negative_adjustment_kld: 0.002 - max_negative_adjustment_fraction_of_base_kld: 0.75 - - # Tiny stratified sniff pass around confirmed Q8-dome synergy templates. - transfer_probe_enabled: true - max_transfer_probes_per_template: 6 - max_total_transfer_probes_per_run: 24 - - transfer_probe_context_strata: - high_fidelity_max_non_reference_groups_below_q6: 1 - mid_fidelity_max_non_reference_groups_below_q6: 3 - low_fidelity_enabled: false - - # Smoke scoring augments the old prediction-space gap cliff. - min_smoke_score: 0.55 - max_smoke_gap_kld: 0.004 - top_rejected_smoke_preview: 25 - - verbose_synergy_logging: true - output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: @@ -392,4 +355,40 @@ baselines: force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true \ No newline at end of file + allow_as_explicit_group_candidate: true + +# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. +# anomaly_detection remains the low-level compatibility section; synergy_detection controls +# template transfer, composition probes, contamination suppression, and wing diagnostics. +synergy_detection: + enabled: true + max_refinement_rounds: 1 + exact_context_confidence_multiplier: 1.00 + same_selected_groups_confidence_multiplier: 0.55 + equivalent_quant_family_confidence_multiplier: 0.30 + group_family_suspicion_confidence_multiplier: 0.15 + min_confidence_to_apply_adjustment: 0.35 + min_confidence_to_schedule_transfer_probe: 0.25 + max_negative_adjustment_kld: 0.002 + max_negative_adjustment_fraction_of_base_kld: 0.75 + transfer_probe_enabled: true + max_transfer_probes_per_template: 6 + max_total_transfer_probes_per_run: 24 + transfer_probe_context_strata: + high_fidelity_max_non_reference_groups_below_q6: 1 + mid_fidelity_max_non_reference_groups_below_q6: 3 + low_fidelity_enabled: false + verbose_synergy_logging: true + min_smoke_score: 0.55 + max_smoke_gap_kld: 0.004 + top_rejected_smoke_preview: 25 + composition_probe_enabled: true + max_template_composition_group_count: 4 + max_composition_probes_per_run: 8 + max_templates_to_compose: 4 + min_template_confidence_for_composition: 0.50 + min_combined_expected_size_savings_percent: 1.0 + contaminating_passenger_detection_enabled: true + min_failure_margin_for_contamination_kld: 0.00050 + contamination_penalty_confidence_multiplier: 0.45 + suppress_repeated_contaminated_attempts: true From c02b89492879bcacf57f650d6bfa18d630b66d3d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 4 May 2026 14:18:51 -0400 Subject: [PATCH 191/258] back to 4B testing to resolve sub space and predictive bugs that're pretty major --- .../Configs/config.qwen3.6-27b.dev.yaml | 394 ++++++++++++++++++ MagicQuant/MagicQuant.csproj | 1 - MagicQuant/Program.cs | 2 +- MagicQuant/config.dev.yaml | 22 +- 4 files changed, 406 insertions(+), 13 deletions(-) create mode 100644 MagicQuant/Configs/config.qwen3.6-27b.dev.yaml diff --git a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml new file mode 100644 index 0000000..73482a9 --- /dev/null +++ b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml @@ -0,0 +1,394 @@ +paths: + magic_quant_root: + model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ + llama_root: + llama_bin: + convert_script: + scratch_roots: + - /mnt/world8/ + - /home/slurp/ + - /mnt/world7/ + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +learning: + # Destructive relearn options are intentionally targeted. + # These are transient runtime commands and are not persisted as DB state. + # When any option below is enabled, MagicQuant prints a count summary and asks + # for confirmation before deleting/relearning anything. + # + # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, + # and execution probe cache rows scoped to the active architecture family. + # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, + # TensorCombo, or BaselineQuantDefinition rows. + force_relearn_architecture_family: false + + # Relearn built-in/standard baselines by display/canonical name for the current + # architecture family and active tensor group profile. + # Example: + # force_relearn_standard_baselines: + # - Q6_K + # - IQ4_XS + force_relearn_standard_baselines: [] + + # Safety gate for tensor group regex/profile changes. After MagicQuant reads the + # native BF16 GGUF tensor list, it prints group counts, example tensors, + # ambiguous matches, unresolved tensors, and base-quant exception counts, then + # asks before continuing. Keep this true unless running fully unattended. + confirm_tensor_group_profile: true + + # Safe/idempotent repair mode for accidental regex mistakes. + # + # Default true: on every run MagicQuant checks whether older DB learned tensor + # truth can be copied into the active TensorGroupProfile by reapplying the + # current regex/base_quant_exceptions rules. If nothing changed or current rows + # already exist, it skips cleanly and does not create duplicates. + # + # This avoids needless re-download/re-quantization of pure learning baselines + # after regex-only regrouping. Old benchmarks/learned rows remain attached to + # their original TensorGroupProfile and are ignored unless that profile becomes + # active again. + # + # Disable only when you intentionally want the slower/full path to regenerate + # learned grouping truth instead of rebucketing from DB snapshots. + # CLI disable aliases: + # --no-rebucket-learned-tensor-groups + # --disable-tensor-group-rebucket + # --full-relearn-tensor-groups + rebucket_learned_tensor_groups_from_existing_truth: true + + +readme: + # Optional title model name override used in: + # # MagicQuant Hybrids (v2.0) - + # If blank, MagicQuant uses identity.architecture_family_name. + title_model_name_override: Qwen3.6-27B + + # Hugging Face README frontmatter. + # Scalars render as: + # license: apache-2.0 + # Arrays render as: + # tags: + # - gguf + # - text-generation + # + # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. + frontmatter: + license: apache-2.0 + tags: + - gguf + - text-generation + - magicquant + - conversational + base_model: + - Qwen/Qwen3.6-27B + +hardware: + gpu_memory_limits_gb: + 0: 19 + 1: 23 + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1_5m.jsonl + +# Legacy evolution survivor knobs were removed from YAML. +# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. + +isolation_pruning: + # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 + minimum_isolation_reduction_to_continue_ratio: 0.00 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + + +prediction: + # Rank-safe isolation KLD predictor. + # + # manual_max_predicted_size_bytes is retained only as an emergency compatibility + # field for older helper code. Leave it at 0 for the new chooser. + manual_max_predicted_size_bytes: 0 + + # Candidate bit-stress thresholds for the low-bit interaction correction. + # The predictor fits each candidate threshold against existing category=General + # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. + bit_stress_threshold_candidates: + - 4.0 + - 5.0 + - 6.0 + - 7.0 + - 8.0 + - 9.0 + - 10.0 + - 11.0 + - 12.0 + + # Fallback threshold when too few benchmark rows exist to fit the interaction model. + default_bit_stress_threshold: 8.0 + + # Minimum benchmark rows required before fitting the interaction correction. + minimum_fit_rows: 12 + +candidate_selection: + # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits + # inside this size premium and beats the real linear KLD improvement line. + near_baseline_max_size_growth_percent: 1.0 + + # Phase 3: interior windows between adjacent final anchors. + # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. + interior_window_fractions: + - 0.35 + - 0.35 + + # Number of predicted winners to keep per interior window. + max_candidates_per_interior_window: 1 + + # If the first predicted candidate fails real validation, try this many fallbacks. + max_fallback_attempts_per_anchor: 5 + + # Strict epsilon for lower-KLD comparisons after real benchmark validation. + minimum_kld_improvement_epsilon: 1.0e-9 + + # Final spacing pass: candidates closer than this fraction of the global survivor + # size span are collapsed unless one genuinely earns the slot. + minimum_neighbor_gap_fraction_of_global_span: 0.03 + + # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller + # anchor must provide a stronger KLD gain to justify its existence. + near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 + near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 + + # Default false: do not spend final prediction/build attempts trying to replace + # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. + # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. + allow_eight_bit_anchor_replacements: true + +anomaly_detection: + enabled: true + + # One anomaly refinement pass after smoke/probe/rule generation. + max_anomaly_refinement_rounds: 1 + + # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. + min_actual_gain_vs_twin_kld: 0.00025 + + # Minimum predicted size savings versus higher-bit twin/reference to probe. + min_predicted_size_savings_vs_twin_percent: 1.0 + + # Max changed groups in a candidate that can seed contextual probes. + max_probe_group_count: 4 + + # Max probes generated per anomaly seed. + max_probes_per_seed: 16 + + # Max anomaly probes in one run. + max_total_probes_per_run: 32 + + # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. + max_prediction_space_gap_vs_twin_kld: 0.00050 + + # Optional relative cap for prediction-space gap normalized by local anchor gap. + max_relative_prediction_penalty_vs_twin: 0.35 + + # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. + prediction_space_violation_margin: 0.00005 + + # Shrink applied to prediction-space adjustment after a rule is confirmed. + anomaly_adjustment_shrink_factor: 0.50 + + # Minimum confidence required before applying a confirmed anomaly rule. + min_rule_confidence_to_apply: 0.50 + + # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. + max_negative_adjustment_kld: 0.00075 + + # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. + max_positive_adjustment_kld: 0.00075 + + # Fractional cap relative to BaseRankSafeKld. + max_adjustment_fraction_of_base_kld: 0.75 + + # Number of top smoke candidates to consider per reference quant zone. + max_smoke_candidates_per_reference_zone: 12 + + # Store suppression-only results so false smoke is not repeatedly probed. + persist_suppression_results: true + + # Emit detailed anomaly logs. + verbose_anomaly_logging: true + + # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. + confirmed_anomaly_expansion: + enabled: true + max_neighbors_per_confirmed_rule: 6 + max_total_expansion_probes: 12 + allowed_reference_quants: + - Q8_0 + allowed_candidate_quants: + - Q6_K + - UD-Q6_K_XL + - Q5_K + - UD-Q5_K_XL + +output: + # Leave blank to default to /MagicQuant/Final_Outputs + output_dir: + output_name_prefix: Qwen3.6-27B + export_external_learned_baselines: true + + # false = normal behavior; delete/rebuild final outputs from scratch. + # true = preserve valid existing GGUFs and skip rebuilding them only when + # exact file name + byte size match benchmark truth. + # CLI --reuse-existing-final-artifacts overrides YAML. + reuse_existing_final_artifacts: false + +# Legacy bit-range bucket survival settings were removed. +# See candidate_selection above for the active final chooser settings. + +identity: + architecture_family_name: Qwen3.6-27B + allow_architecture_family_alias_override: false + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + + custom_repositories: + - repo_id: unsloth/Qwen3.6-27B-GGUF + enabled: true + short_source_name: Unsloth + source_kind: huggingface_gguf_repository + require_all_includes_to_resolve: true + validate_tensor_names_against_source_model: true + delete_partial_or_dirty_downloads: true + resume_or_retry_downloads: true + + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: false + + includes: + + - file_name: Qwen3.6-27B-UD-IQ2_M.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: UD-IQ2_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf + baseline_family: IQ2_XXS + quantize_base_name: IQ2_XXS + display_name: UD-IQ2_XXS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf + baseline_family: IQ3_XXS + quantize_base_name: IQ3_XXS + display_name: UD-IQ3_XXS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: UD-Q2_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_M + display_name: UD-Q3_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M + display_name: UD-Q4_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf + baseline_family: Q5_K + quantize_base_name: Q5_K + display_name: UD-Q5_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: UD-Q6_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + +# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. +# anomaly_detection remains the low-level compatibility section; synergy_detection controls +# template transfer, composition probes, contamination suppression, and wing diagnostics. +synergy_detection: + enabled: true + max_refinement_rounds: 1 + exact_context_confidence_multiplier: 1.00 + same_selected_groups_confidence_multiplier: 0.55 + equivalent_quant_family_confidence_multiplier: 0.30 + group_family_suspicion_confidence_multiplier: 0.15 + min_confidence_to_apply_adjustment: 0.35 + min_confidence_to_schedule_transfer_probe: 0.25 + max_negative_adjustment_kld: 0.002 + max_negative_adjustment_fraction_of_base_kld: 0.75 + transfer_probe_enabled: true + max_transfer_probes_per_template: 6 + max_total_transfer_probes_per_run: 24 + transfer_probe_context_strata: + high_fidelity_max_non_reference_groups_below_q6: 1 + mid_fidelity_max_non_reference_groups_below_q6: 3 + low_fidelity_enabled: false + verbose_synergy_logging: true + min_smoke_score: 0.55 + max_smoke_gap_kld: 0.004 + top_rejected_smoke_preview: 25 + composition_probe_enabled: true + max_template_composition_group_count: 4 + max_composition_probes_per_run: 8 + max_templates_to_compose: 4 + min_template_confidence_for_composition: 0.50 + min_combined_expected_size_savings_percent: 1.0 + contaminating_passenger_detection_enabled: true + min_failure_margin_for_contamination_kld: 0.00050 + contamination_penalty_confidence_multiplier: 0.45 + suppress_repeated_contaminated_attempts: true diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index dedebc6..8352882 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -29,7 +29,6 @@ - diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 346a383..aeba632 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -33,7 +33,7 @@ args = [ "evolution", - "--architecture-family", @"""Qwen3.6-27B""" + "--architecture-family", @"""Qwen3-4B""" ,"--reuse-existing-final-artifacts" ]; } diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 73482a9..b1064e9 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ + model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ llama_root: llama_bin: convert_script: @@ -260,7 +260,7 @@ output: # See candidate_selection above for the active final chooser settings. identity: - architecture_family_name: Qwen3.6-27B + architecture_family_name: Qwen3-4B allow_architecture_family_alias_override: false baselines: @@ -270,7 +270,7 @@ baselines: enabled_standard_explicit_group_candidates: [] custom_repositories: - - repo_id: unsloth/Qwen3.6-27B-GGUF + - repo_id: unsloth/Qwen3-4B-GGUF enabled: true short_source_name: Unsloth source_kind: huggingface_gguf_repository @@ -285,7 +285,7 @@ baselines: includes: - - file_name: Qwen3.6-27B-UD-IQ2_M.gguf + - file_name: Qwen3-4B-UD-IQ2_M.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-IQ2_M @@ -294,7 +294,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf + - file_name: Qwen3-4B-UD-IQ2_XXS.gguf baseline_family: IQ2_XXS quantize_base_name: IQ2_XXS display_name: UD-IQ2_XXS @@ -303,7 +303,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf + - file_name: Qwen3-4B-UD-IQ3_XXS.gguf baseline_family: IQ3_XXS quantize_base_name: IQ3_XXS display_name: UD-IQ3_XXS @@ -312,7 +312,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf + - file_name: Qwen3-4B-UD-Q2_K_XL.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-Q2_K_XL @@ -321,7 +321,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf + - file_name: Qwen3-4B-UD-Q3_K_XL.gguf baseline_family: IQ3_M quantize_base_name: IQ3_M display_name: UD-Q3_K_XL @@ -330,7 +330,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf + - file_name: Qwen3-4B-UD-Q4_K_XL.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M display_name: UD-Q4_K_XL @@ -339,7 +339,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf + - file_name: Qwen3-4B-UD-Q5_K_XL.gguf baseline_family: Q5_K quantize_base_name: Q5_K display_name: UD-Q5_K_XL @@ -348,7 +348,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf + - file_name: Qwen3-4B-UD-Q6_K_XL.gguf baseline_family: Q6_K quantize_base_name: Q6_K display_name: UD-Q6_K_XL From 5f993fb5239c4b61bb4fba97db8fef26beedbfb8 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 4 May 2026 14:54:04 -0400 Subject: [PATCH 192/258] Significantly stronger prediction engine search. Much closer to desired reality. --- .../Models/PredictionSelectionModels.cs | 19 + .../Services/CombinationDuckDbSchema.cs | 21 +- .../DuckDbPredictionMaterializationService.cs | 8 +- .../PredictionGuidedHybridSelectionService.cs | 343 +++++++++++++++--- MagicQuant/Services/QuantDatabaseService.cs | 220 ++++++++++- .../Services/RemainingCombinationStore.cs | 222 ++++++++++-- 6 files changed, 733 insertions(+), 100 deletions(-) diff --git a/MagicQuant/Models/PredictionSelectionModels.cs b/MagicQuant/Models/PredictionSelectionModels.cs index c36832d..a6c3f19 100644 --- a/MagicQuant/Models/PredictionSelectionModels.cs +++ b/MagicQuant/Models/PredictionSelectionModels.cs @@ -55,6 +55,21 @@ public sealed class RankSafePredictionFit public bool UsedFallback { get; init; } } + +public sealed class PredictedAnchorRow +{ + public required TensorConfig Config { get; init; } + public required string ConfigKey { get; init; } + public required string DisplayName { get; init; } + public required string BaselineCanonicalKey { get; init; } + public byte RuntimeBaselineId { get; init; } + public double PredictedKld { get; init; } + public ulong PredictedSizeBytes { get; init; } + public double PredictionConfidence { get; init; } + public ulong PredictionRank { get; init; } + public bool IsVirtualPredictionAnchor { get; init; } = true; +} + public sealed class HybridSelectionAnchor { public BenchmarkSnapshotRecord Snapshot { get; init; } = default!; @@ -80,6 +95,10 @@ public sealed class HybridSelectionCandidate public double PredictedGainOverLine { get; init; } public int AttemptOrder { get; init; } public string WindowLabel { get; init; } = string.Empty; + public ulong PredictionWindowMinSizeBytes { get; init; } + public ulong PredictionWindowMaxSizeBytes { get; init; } + public PredictedAnchorRow? HigherDamagePredictionAnchor { get; init; } + public PredictedAnchorRow? LowerDamagePredictionAnchor { get; init; } // Diagnostic-only context captured at selection time. These values do not // change acceptance rules; they explain how the candidate was found, how diff --git a/MagicQuant/Services/CombinationDuckDbSchema.cs b/MagicQuant/Services/CombinationDuckDbSchema.cs index 6287d19..b99b2af 100644 --- a/MagicQuant/Services/CombinationDuckDbSchema.cs +++ b/MagicQuant/Services/CombinationDuckDbSchema.cs @@ -6,8 +6,9 @@ internal static class CombinationDuckDbSchema { public const string TableName = "tensor_configs"; public const string SlotColumnList = "BaseQuant, Embeddings, LmHead, AttnQ, AttnKV, AttnOutput, FfnUpGate, FfnDown, MoeExperts, MoeRouter"; - public const string PredictionColumnList = "PredictedKld, PredictedSizeBytes, PredictionConfidence, PredictionRank, BaseRankSafeKld, AnomalyAdjustmentKld, FinalPredictedKld, IsProtectedAnchor"; + public const string PredictionColumnList = "PredictedKld, PredictedSizeBytes, PredictionConfidence, PredictionRank, BaseRankSafeKld, AnomalyAdjustmentKld, FinalPredictedKld, IsProtectedAnchor, IsVirtualPredictionAnchor, AnchorBaselineRuntimeId, AnchorBaselineCanonicalKey, AnchorDisplayName"; public const string ActiveCandidatePredicateSql = "COALESCE(IsProtectedAnchor, FALSE) = FALSE"; + public const string VirtualPredictionAnchorPredicateSql = "COALESCE(IsVirtualPredictionAnchor, FALSE) = TRUE"; public const string EffectivePredictedKldSql = "COALESCE(FinalPredictedKld, PredictedKld)"; public const string HybridPredicateSql = "(Embeddings <> 0 OR LmHead <> 0 OR AttnQ <> 0 OR AttnKV <> 0 OR AttnOutput <> 0 OR FfnUpGate <> 0 OR FfnDown <> 0 OR MoeExperts <> 0 OR MoeRouter <> 0)"; @@ -29,7 +30,8 @@ internal static class CombinationDuckDbSchema [ "utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint","utinyint", "double","ubigint","double","ubigint", - "double","double","double","boolean" + "double","double","double","boolean", + "boolean","utinyint","varchar","varchar" ]; public static string CreateTableSql => $@" @@ -60,9 +62,18 @@ internal static class CombinationDuckDbSchema AnomalyAdjustmentKld DOUBLE DEFAULT 0.0, FinalPredictedKld DOUBLE, - -- Protected/reference anchors may be stored for twin lookup/logging, but must - -- never become active search carriers. Normal generator rows default false. - IsProtectedAnchor BOOLEAN DEFAULT FALSE + -- Protected/reference anchors may be stored for lookup/logging, but must + -- never become active search candidates. Normal generator rows default false. + IsProtectedAnchor BOOLEAN DEFAULT FALSE, + + -- Virtual prediction anchors are not real benchmark rows. They are ordinary + -- tensor-config rows shaped like uniform learned-baseline blankets so the + -- existing rank-safe prediction materializer can score them in the same + -- imaginary space as normal candidates. + IsVirtualPredictionAnchor BOOLEAN DEFAULT FALSE, + AnchorBaselineRuntimeId UTINYINT, + AnchorBaselineCanonicalKey VARCHAR, + AnchorDisplayName VARCHAR );"; public static string BuildSlotEqualityPredicate(string leftAlias, string rightAlias) diff --git a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs index f247b2c..00f5c26 100644 --- a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs +++ b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs @@ -57,8 +57,7 @@ await ExecuteAsync(c, $@" PredictionRank = NULL, BaseRankSafeKld = NULL, AnomalyAdjustmentKld = 0.0, - FinalPredictedKld = NULL, - IsProtectedAnchor = FALSE;", ct); + FinalPredictedKld = NULL;", ct); await BuildLookupTablesAsync(c, model, ct); await PrintLookupDiagnosticsAsync(c, model, ct); @@ -491,8 +490,7 @@ await ExecuteAsync(c, $@" PredictionRank = r.PredictionRank, BaseRankSafeKld = r.PredictedKld, AnomalyAdjustmentKld = 0.0, - FinalPredictedKld = r.PredictedKld, - IsProtectedAnchor = FALSE + FinalPredictedKld = r.PredictedKld FROM temp_ranked_prediction_with_rank r WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "r")};", ct); } @@ -916,4 +914,4 @@ public sealed class PredictionMaterializationStatus public double? MaxPredictedKld { get; init; } public ulong? MinPredictedSizeBytes { get; init; } public ulong? MaxPredictedSizeBytes { get; init; } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index e05ab82..60bbea4 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -60,13 +60,16 @@ public async Task RunAsync( AnsiConsole.MarkupLine($"[green]Pure/current anchor survivors after dominance:[/] [cyan]{current.Count:N0}[/]"); PrintAnchorFrontier(current, "Initial anchor frontier after dominance"); - var strict = await RunStrictDominanceReplacementAsync(current, eliminationRecords, validationFailures, validationAttempts, phaseDiagnostics, ct); + var predictedAnchors = await _predictedStore.GetPredictedAnchorRowsAsync(ct); + PrintPredictionAnchorFrontier(predictedAnchors, current, "Prediction Anchor Frontier"); + + var strict = await RunStrictDominanceReplacementAsync(current, predictedAnchors, eliminationRecords, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, strict.AcceptedSnapshots, eliminationRecords, "strict predicted hybrid dominance validated by real benchmark"); - var near = await RunNearBaselineReplacementAsync(current, eliminationRecords, validationFailures, validationAttempts, phaseDiagnostics, ct); + var near = await RunNearBaselineReplacementAsync(current, predictedAnchors, eliminationRecords, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, near.AcceptedSnapshots, eliminationRecords, "near-baseline size-premium replacement validated by real benchmark"); - var interior = await RunInteriorSubspaceDiscoveryAsync(current, validationFailures, validationAttempts, phaseDiagnostics, ct); + var interior = await RunInteriorSubspaceDiscoveryAsync(current, predictedAnchors, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, interior.AcceptedSnapshots, eliminationRecords, "interior subspace discovery dominated by real benchmark truth"); var bestConfirmedAnomaly = await LoadBestConfirmedBeneficialAnomalySnapshotAsync(ct); @@ -110,6 +113,7 @@ public async Task RunAsync( private async Task RunStrictDominanceReplacementAsync( IReadOnlyList currentAnchors, + IReadOnlyList predictedAnchors, List eliminations, List validationFailures, List validationAttempts, @@ -140,18 +144,40 @@ private async Task RunStrictDominanceReplacementAsync( continue; } - long poolCount = await _predictedStore.CountStrictDominanceCandidatesAsync(anchor, ct); - var strictRows = await _predictedStore.QueryStrictDominanceCandidatesAsync(anchor, Config.SelectionMaxFallbackAttemptsPerAnchor, ct); + var predictedAnchor = await _predictedStore.FindPredictedAnchorForRealAnchorAsync(anchor, predictedAnchors, ct); + if (predictedAnchor == null) + { + AnsiConsole.MarkupLine($"[yellow]Skipping strict prediction-space discovery:[/] no predicted virtual anchor matched real anchor [cyan]{Markup.Escape(anchor.DisplayName)}[/]."); + phaseDiagnostics.Add(new SelectionPhaseDiagnostic + { + Phase = "StrictDominanceReplacement", + WindowLabel = $"strict <= {anchor.DisplayName}", + HigherDamageSmaller = ToAnchorLog(anchor), + LowerDamageLarger = ToAnchorLog(anchor), + WindowMinSizeBytes = 0, + WindowMaxSizeBytes = anchor.SizeBytes, + CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, + Notes = ["Skipped because no predicted virtual anchor row was available. DuckDB preselection intentionally does not fall back to real anchor KLD/size."] + }); + continue; + } + + long poolCount = await _predictedStore.CountStrictDominanceCandidatesAsync(predictedAnchor, ct); + var strictRows = await _predictedStore.QueryStrictDominanceCandidatesAsync(predictedAnchor, Config.SelectionMaxFallbackAttemptsPerAnchor, ct); var candidates = strictRows.Select((x, i) => new HybridSelectionCandidate { Prediction = x, Reason = HybridSelectionReason.StrictDominanceReplacement, LowerDamageAnchor = anchor, HigherDamageAnchor = anchor, + LowerDamagePredictionAnchor = predictedAnchor, + HigherDamagePredictionAnchor = predictedAnchor, + PredictionWindowMinSizeBytes = 0, + PredictionWindowMaxSizeBytes = predictedAnchor.PredictedSizeBytes, WindowMinSizeBytes = 0, WindowMaxSizeBytes = anchor.SizeBytes, - LinearExpectedKld = anchor.Kld, - PredictedGainOverLine = anchor.Kld - x.PredictedKld, + LinearExpectedKld = predictedAnchor.PredictedKld, + PredictedGainOverLine = predictedAnchor.PredictedKld - x.PredictedKld, AttemptOrder = i + 1, WindowLabel = $"strict <= {anchor.DisplayName}", CandidatePoolSize = poolCount, @@ -162,10 +188,14 @@ private async Task RunStrictDominanceReplacementAsync( CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, PhaseWindowIndex = 1, PhaseWindowCount = 1, - CandidateSelectionNotes = ["Strict query requires predicted size <= anchor size and predicted KLD + epsilon < anchor KLD."] + CandidateSelectionNotes = ["Strict DuckDB query uses predicted virtual anchor size/KLD; real anchor size/KLD is used only for post-build validation."] }).ToList(); - var strictNotes = new List { "Strict query requires predicted size <= anchor size and predicted KLD + epsilon < anchor KLD." }; + var strictNotes = new List + { + "Strict query uses predicted virtual anchor size/KLD; real benchmark anchor is reserved for post-build validation.", + $"Prediction anchor={predictedAnchor.DisplayName}; predictedKld={predictedAnchor.PredictedKld:0.000000}; predictedSizeBytes={predictedAnchor.PredictedSizeBytes:N0}; realKld={anchor.Kld:0.000000}; realSizeBytes={anchor.SizeBytes:N0}." + }; bool anomalyStrictMode = IsQ8Anchor(anchor) || candidates.Any(x => Math.Abs(x.Prediction.AnomalyAdjustmentKld) > 1e-12); if (anomalyStrictMode) strictNotes.Add("Q8/anomaly strict mode: validate all fetched candidates up to the configured attempt limit before choosing by actual KLD/size truth."); @@ -176,6 +206,10 @@ private async Task RunStrictDominanceReplacementAsync( WindowLabel = $"strict <= {anchor.DisplayName}", HigherDamageSmaller = ToAnchorLog(anchor), LowerDamageLarger = ToAnchorLog(anchor), + PredictionHigherDamageSmaller = ToPredictionAnchorLog(predictedAnchor), + PredictionLowerDamageLarger = ToPredictionAnchorLog(predictedAnchor), + PredictionWindowMinSizeBytes = 0, + PredictionWindowMaxSizeBytes = predictedAnchor.PredictedSizeBytes, WindowMinSizeBytes = 0, WindowMaxSizeBytes = anchor.SizeBytes, CandidatePoolSize = poolCount, @@ -427,6 +461,7 @@ private static string ExplainStrictAcceptedLoss( private async Task RunNearBaselineReplacementAsync( IReadOnlyList currentAnchors, + IReadOnlyList predictedAnchors, List eliminations, List validationFailures, List validationAttempts, @@ -465,19 +500,57 @@ private async Task RunNearBaselineReplacementAsync( continue; } - ulong min = lowerSizeHigherDamage.SizeBytes; - ulong max = AddPercent(min, Config.SelectionNearBaselineMaxSizeGrowthPercent); + var predictedLowerSizeHigherDamage = await _predictedStore.FindPredictedAnchorForRealAnchorAsync(lowerSizeHigherDamage, predictedAnchors, ct); + var predictedUpperSizeLowerDamage = await _predictedStore.FindPredictedAnchorForRealAnchorAsync(upperSizeLowerDamage, predictedAnchors, ct); + if (predictedLowerSizeHigherDamage == null || predictedUpperSizeLowerDamage == null) + { + AnsiConsole.MarkupLine($"[yellow]Skipping near-baseline prediction-space discovery:[/] missing predicted anchor for pair [cyan]{Markup.Escape(lowerSizeHigherDamage.DisplayName)}[/] -> [cyan]{Markup.Escape(upperSizeLowerDamage.DisplayName)}[/]."); + phaseDiagnostics.Add(new SelectionPhaseDiagnostic + { + Phase = "NearBaselineReplacement", + WindowLabel = windowLabel, + PhaseWindowIndex = pairIndex + 1, + PhaseWindowCount = pairs.Count, + HigherDamageSmaller = ToAnchorLog(lowerSizeHigherDamage), + LowerDamageLarger = ToAnchorLog(upperSizeLowerDamage), + PredictionHigherDamageSmaller = ToPredictionAnchorLog(predictedLowerSizeHigherDamage), + PredictionLowerDamageLarger = ToPredictionAnchorLog(predictedUpperSizeLowerDamage), + Notes = ["Skipped because one or both predicted virtual anchor rows were unavailable. DuckDB preselection intentionally does not fall back to real anchor KLD/size."] + }); + continue; + } + + ulong realMin = lowerSizeHigherDamage.SizeBytes; + ulong realMax = AddPercent(realMin, Config.SelectionNearBaselineMaxSizeGrowthPercent); + + if (realMax > upperSizeLowerDamage.SizeBytes) + realMax = upperSizeLowerDamage.SizeBytes; - if (max > upperSizeLowerDamage.SizeBytes) - max = upperSizeLowerDamage.SizeBytes; + ulong predictionMin = predictedLowerSizeHigherDamage.PredictedSizeBytes; + ulong predictionMax = AddPercent(predictionMin, Config.SelectionNearBaselineMaxSizeGrowthPercent); - long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(min, max, ct); - long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(lowerSizeHigherDamage, upperSizeLowerDamage, min, max, ct); + if (predictionMax > predictedUpperSizeLowerDamage.PredictedSizeBytes) + predictionMax = predictedUpperSizeLowerDamage.PredictedSizeBytes; + + if (predictionMax <= predictionMin || realMax <= realMin) + { + AnsiConsole.MarkupLine($"[grey]Skipping near-baseline pair with empty prediction/real window:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)} -> {Markup.Escape(upperSizeLowerDamage.DisplayName)}"); + continue; + } + + LogPredictionAndRealPairLines(lowerSizeHigherDamage, upperSizeLowerDamage, predictedLowerSizeHigherDamage, predictedUpperSizeLowerDamage, "Near-baseline pair"); + + long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(predictionMin, predictionMax, ct); + long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(predictedLowerSizeHigherDamage, predictedUpperSizeLowerDamage, predictionMin, predictionMax, ct); var rawCandidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync( lowerSizeHigherDamage, upperSizeLowerDamage, - min, - max, + predictedLowerSizeHigherDamage, + predictedUpperSizeLowerDamage, + predictionMin, + predictionMax, + realMin, + realMax, HybridSelectionReason.NearBaselineOnePercentReplacement, windowLabel, fetchLimit, @@ -517,9 +590,13 @@ private async Task RunNearBaselineReplacementAsync( PhaseWindowCount = pairs.Count, HigherDamageSmaller = ToAnchorLog(lowerSizeHigherDamage), LowerDamageLarger = ToAnchorLog(upperSizeLowerDamage), - WindowMinSizeBytes = min, - WindowMaxSizeBytes = max, - WindowSizeGiB = ToGiB(max > min ? max - min : 0), + PredictionHigherDamageSmaller = ToPredictionAnchorLog(predictedLowerSizeHigherDamage), + PredictionLowerDamageLarger = ToPredictionAnchorLog(predictedUpperSizeLowerDamage), + PredictionWindowMinSizeBytes = predictionMin, + PredictionWindowMaxSizeBytes = predictionMax, + WindowMinSizeBytes = realMin, + WindowMaxSizeBytes = realMax, + WindowSizeGiB = ToGiB(realMax > realMin ? realMax - realMin : 0), CandidatePoolSize = lineBeaters, WindowCandidateCount = windowRows, LineBeatingCandidateCount = lineBeaters, @@ -531,7 +608,7 @@ private async Task RunNearBaselineReplacementAsync( TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), RejectedByBrutalityPreview = rejectedByBrutality, Notes = [ - "Near-baseline first counts predicted hybrids inside the near-size window, then counts candidates predicted to beat the local line, then applies near-lower-anchor brutality, then caps validation attempts.", + "Near-baseline DuckDB discovery uses predicted virtual anchor windows/lines; real anchor windows/lines are used only after a candidate is benchmarked.", $"Brutal zone fraction={Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###}; required gain fraction of pair KLD gap={Config.SelectionNearAnchorRequiredKldGainFractionOfPairGap:0.###}." ] }; @@ -539,7 +616,7 @@ private async Task RunNearBaselineReplacementAsync( AnsiConsole.MarkupLine( $"[grey]Near-baseline window {pairIndex + 1:N0}/{pairs.Count:N0}:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)} -> {Markup.Escape(upperSizeLowerDamage.DisplayName)} " + - $"| rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, fetched={rawCandidates.Count:N0}, after-brutality={diag.CandidatesAfterBrutalityCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}"); + $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, fetched={rawCandidates.Count:N0}, after-brutality={diag.CandidatesAfterBrutalityCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}"); if (rejectedByBrutality.Count > 0) AnsiConsole.MarkupLine($"[grey] rejected by near-lower-anchor brutality preview:[/] [cyan]{rejectedByBrutality.Count:N0}[/] (see magicquant-selection-phase-diagnostics.json)"); @@ -551,10 +628,10 @@ private async Task RunNearBaselineReplacementAsync( { var validation = await BuildAndValidateSingleAsync( candidate, - snapshot => snapshot.SizeBytes >= min && - snapshot.SizeBytes <= max && + snapshot => snapshot.SizeBytes >= realMin && + snapshot.SizeBytes <= realMax && BeatsLinearKldLine(snapshot.SizeBytes, snapshot.Kld, lowerSizeHigherDamage, upperSizeLowerDamage), - $"must land inside {min:N0}..{max:N0} bytes and beat the real linear KLD line", + $"must land inside {realMin:N0}..{realMax:N0} bytes and beat the real linear KLD line", ct); validationAttempts.Add(validation); @@ -580,6 +657,7 @@ private async Task RunNearBaselineReplacementAsync( private async Task RunInteriorSubspaceDiscoveryAsync( IReadOnlyList currentAnchors, + IReadOnlyList predictedAnchors, List validationFailures, List validationAttempts, List phaseDiagnostics, @@ -603,14 +681,28 @@ private async Task RunInteriorSubspaceDiscoveryAsync( for (int pairIndex = 0; pairIndex < pairs.Count; pairIndex++) { var pair = pairs[pairIndex]; - ulong lowSize = pair.HigherDamageSmaller.SizeBytes; - ulong highSize = pair.LowerDamageLarger.SizeBytes; + var predictedHigherDamageSmaller = await _predictedStore.FindPredictedAnchorForRealAnchorAsync(pair.HigherDamageSmaller, predictedAnchors, ct); + var predictedLowerDamageLarger = await _predictedStore.FindPredictedAnchorForRealAnchorAsync(pair.LowerDamageLarger, predictedAnchors, ct); + if (predictedHigherDamageSmaller == null || predictedLowerDamageLarger == null) + { + AnsiConsole.MarkupLine($"[yellow]Skipping interior prediction-space discovery:[/] missing predicted anchor for pair [cyan]{Markup.Escape(pair.HigherDamageSmaller.DisplayName)}[/] -> [cyan]{Markup.Escape(pair.LowerDamageLarger.DisplayName)}[/]."); + continue; + } + + ulong realLowSize = pair.HigherDamageSmaller.SizeBytes; + ulong realHighSize = pair.LowerDamageLarger.SizeBytes; + ulong predictionLowSize = predictedHigherDamageSmaller.PredictedSizeBytes; + ulong predictionHighSize = predictedLowerDamageLarger.PredictedSizeBytes; - if (highSize <= lowSize) + if (realHighSize <= realLowSize || predictionHighSize <= predictionLowSize) continue; - ulong span = highSize - lowSize; - ulong cursor = lowSize; + LogPredictionAndRealPairLines(pair.HigherDamageSmaller, pair.LowerDamageLarger, predictedHigherDamageSmaller, predictedLowerDamageLarger, "Interior pair"); + + ulong realSpan = realHighSize - realLowSize; + ulong predictionSpan = predictionHighSize - predictionLowSize; + ulong realCursor = realLowSize; + ulong predictionCursor = predictionLowSize; for (int i = 0; i < fractions.Count; i++) { @@ -618,28 +710,33 @@ private async Task RunInteriorSubspaceDiscoveryAsync( if (fraction <= 0d) continue; - ulong width = (ulong)Math.Round(span * fraction, MidpointRounding.AwayFromZero); - if (width == 0) + ulong realWidth = (ulong)Math.Round(realSpan * fraction, MidpointRounding.AwayFromZero); + ulong predictionWidth = (ulong)Math.Round(predictionSpan * fraction, MidpointRounding.AwayFromZero); + if (realWidth == 0 || predictionWidth == 0) continue; - ulong min = cursor; - ulong max = i == fractions.Count - 1 - ? Math.Min(highSize, cursor + width) - : Math.Min(highSize, cursor + width); + ulong realMin = realCursor; + ulong realMax = Math.Min(realHighSize, realCursor + realWidth); + ulong predictionMin = predictionCursor; + ulong predictionMax = Math.Min(predictionHighSize, predictionCursor + predictionWidth); - if (max <= min) + if (realMax <= realMin || predictionMax <= predictionMin) continue; globalWindowIndex++; string windowLabel = $"interior {i + 1}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}"; - long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(min, max, ct); - long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(pair.HigherDamageSmaller, pair.LowerDamageLarger, min, max, ct); + long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(predictionMin, predictionMax, ct); + long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(predictedHigherDamageSmaller, predictedLowerDamageLarger, predictionMin, predictionMax, ct); var rawCandidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync( pair.HigherDamageSmaller, pair.LowerDamageLarger, - min, - max, + predictedHigherDamageSmaller, + predictedLowerDamageLarger, + predictionMin, + predictionMax, + realMin, + realMax, HybridSelectionReason.InteriorSubspaceDiscovery, windowLabel, interiorFetchLimit, @@ -683,9 +780,13 @@ private async Task RunInteriorSubspaceDiscoveryAsync( PhaseWindowCount = estimatedWindowCount, HigherDamageSmaller = ToAnchorLog(pair.HigherDamageSmaller), LowerDamageLarger = ToAnchorLog(pair.LowerDamageLarger), - WindowMinSizeBytes = min, - WindowMaxSizeBytes = max, - WindowSizeGiB = ToGiB(max > min ? max - min : 0), + PredictionHigherDamageSmaller = ToPredictionAnchorLog(predictedHigherDamageSmaller), + PredictionLowerDamageLarger = ToPredictionAnchorLog(predictedLowerDamageLarger), + PredictionWindowMinSizeBytes = predictionMin, + PredictionWindowMaxSizeBytes = predictionMax, + WindowMinSizeBytes = realMin, + WindowMaxSizeBytes = realMax, + WindowSizeGiB = ToGiB(realMax > realMin ? realMax - realMin : 0), CandidatePoolSize = lineBeaters, WindowCandidateCount = windowRows, LineBeatingCandidateCount = lineBeaters, @@ -696,16 +797,17 @@ private async Task RunInteriorSubspaceDiscoveryAsync( QueryFetchLimit = interiorFetchLimit, TopCandidates = kept.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), RejectedByBrutalityPreview = rejectedByBrutality, - Notes = ["Interior candidates are gathered per window, then globally deduped by tensor config before batch validation."] + Notes = ["Interior DuckDB discovery uses predicted virtual anchor windows/lines; real anchor windows/lines are used only after benchmark validation."] }); AnsiConsole.MarkupLine( $"[grey]Interior window {globalWindowIndex:N0}/{Math.Max(estimatedWindowCount, globalWindowIndex):N0}:[/] {Markup.Escape(pair.HigherDamageSmaller.DisplayName)} -> {Markup.Escape(pair.LowerDamageLarger.DisplayName)} " + - $"| rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, fetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, selected={kept.Count:N0}/{interiorAttemptLimit:N0}"); + $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, fetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, selected={kept.Count:N0}/{interiorAttemptLimit:N0}"); - cursor = max; + realCursor = realMax; + predictionCursor = predictionMax; - if (cursor >= highSize) + if (realCursor >= realHighSize || predictionCursor >= predictionHighSize) break; } } @@ -846,6 +948,10 @@ private static HybridSelectionCandidate AttachSelectionDiagnostics( Reason = candidate.Reason, LowerDamageAnchor = candidate.LowerDamageAnchor, HigherDamageAnchor = candidate.HigherDamageAnchor, + LowerDamagePredictionAnchor = candidate.LowerDamagePredictionAnchor, + HigherDamagePredictionAnchor = candidate.HigherDamagePredictionAnchor, + PredictionWindowMinSizeBytes = candidate.PredictionWindowMinSizeBytes, + PredictionWindowMaxSizeBytes = candidate.PredictionWindowMaxSizeBytes, WindowMinSizeBytes = candidate.WindowMinSizeBytes, WindowMaxSizeBytes = candidate.WindowMaxSizeBytes, LinearExpectedKld = candidate.LinearExpectedKld, @@ -866,8 +972,21 @@ private static HybridSelectionCandidate AttachSelectionDiagnostics( private static BrutalityAnalysis AnalyzeNearLowerAnchorBrutality(HybridSelectionCandidate candidate) { - ulong span = candidate.LowerDamageAnchor.SizeBytes > candidate.HigherDamageAnchor.SizeBytes - ? candidate.LowerDamageAnchor.SizeBytes - candidate.HigherDamageAnchor.SizeBytes + var higherDamagePredictionAnchor = candidate.HigherDamagePredictionAnchor; + var lowerDamagePredictionAnchor = candidate.LowerDamagePredictionAnchor; + if (higherDamagePredictionAnchor == null || lowerDamagePredictionAnchor == null) + { + return new BrutalityAnalysis + { + Passed = true, + FractionFromSmallAnchor = 1d, + RequiredGain = Config.SelectionMinimumKldImprovementEpsilon, + Explanation = "Brutality skipped because prediction anchor metadata is missing; no predicted-vs-real comparison was performed." + }; + } + + ulong span = lowerDamagePredictionAnchor.PredictedSizeBytes > higherDamagePredictionAnchor.PredictedSizeBytes + ? lowerDamagePredictionAnchor.PredictedSizeBytes - higherDamagePredictionAnchor.PredictedSizeBytes : 0; if (span == 0) @@ -877,18 +996,18 @@ private static BrutalityAnalysis AnalyzeNearLowerAnchorBrutality(HybridSelection Passed = true, FractionFromSmallAnchor = 1d, RequiredGain = Config.SelectionMinimumKldImprovementEpsilon, - Explanation = "Brutality passed because anchor span is zero." + Explanation = "Brutality passed because prediction-anchor span is zero." }; } - ulong distanceFromSmall = candidate.Prediction.PredictedSizeBytes > candidate.HigherDamageAnchor.SizeBytes - ? candidate.Prediction.PredictedSizeBytes - candidate.HigherDamageAnchor.SizeBytes + ulong distanceFromSmall = candidate.Prediction.PredictedSizeBytes > higherDamagePredictionAnchor.PredictedSizeBytes + ? candidate.Prediction.PredictedSizeBytes - higherDamagePredictionAnchor.PredictedSizeBytes : 0; double fraction = distanceFromSmall / (double)span; double requiredGain = Math.Max( Config.SelectionMinimumKldImprovementEpsilon, - Math.Abs(candidate.HigherDamageAnchor.Kld - candidate.LowerDamageAnchor.Kld) * + Math.Abs(higherDamagePredictionAnchor.PredictedKld - lowerDamagePredictionAnchor.PredictedKld) * Config.SelectionNearAnchorRequiredKldGainFractionOfPairGap); bool passed = fraction > Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan || @@ -896,9 +1015,9 @@ private static BrutalityAnalysis AnalyzeNearLowerAnchorBrutality(HybridSelection string explanation = passed ? fraction > Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan - ? $"Brutality passed because candidate is outside brutal zone (fraction={fraction:0.###} > {Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###})." - : $"Brutality passed because predicted gain {candidate.PredictedGainOverLine:0.########} >= required gain {requiredGain:0.########}." - : $"Brutality rejected because candidate is inside brutal zone (fraction={fraction:0.###} <= {Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###}) and predicted gain {candidate.PredictedGainOverLine:0.########} < required gain {requiredGain:0.########}."; + ? $"Brutality passed in prediction space because candidate is outside brutal zone (fraction={fraction:0.###} > {Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###})." + : $"Brutality passed in prediction space because predicted gain {candidate.PredictedGainOverLine:0.########} >= required gain {requiredGain:0.########}." + : $"Brutality rejected in prediction space because candidate is inside brutal zone (fraction={fraction:0.###} <= {Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###}) and predicted gain {candidate.PredictedGainOverLine:0.########} < required gain {requiredGain:0.########}."; return new BrutalityAnalysis { @@ -1149,6 +1268,9 @@ private static void PrintCandidatePredictionLine(HybridSelectionCandidate candid $"[grey] predicted:[/] size={candidate.Prediction.PredictedSizeBytes:N0} bytes ({ToGiB(candidate.Prediction.PredictedSizeBytes):0.00} GiB), " + $"kld={candidate.Prediction.PredictedKld:0.000000}, line={candidate.LinearExpectedKld:0.000000}, gain={candidate.PredictedGainOverLine:0.000000}, " + $"rank={candidate.Prediction.PredictedRank}, confidence={candidate.Prediction.PredictionConfidence:0.###}"); + AnsiConsole.MarkupLine( + $"[grey] prediction anchors/window:[/] {Markup.Escape(candidate.HigherDamagePredictionAnchor?.DisplayName ?? "n/a")} -> {Markup.Escape(candidate.LowerDamagePredictionAnchor?.DisplayName ?? "n/a")}, " + + $"predWindow={candidate.PredictionWindowMinSizeBytes:N0}..{candidate.PredictionWindowMaxSizeBytes:N0}, realWindow={candidate.WindowMinSizeBytes:N0}..{candidate.WindowMaxSizeBytes:N0}"); AnsiConsole.MarkupLine( $"[grey] selection context:[/] pool={candidate.CandidatePoolSize:N0}, windowRows={candidate.WindowCandidateCount:N0}, lineBeat={candidate.LineBeatingCandidateCount:N0}, " + $"fetched={candidate.FetchedCandidateCount:N0}, afterBrutality={candidate.CandidatesAfterBrutalityCount:N0}, attemptLimit={candidate.CandidateAttemptLimit:N0}"); @@ -1195,6 +1317,77 @@ private static void PrintAnchorFrontier(IReadOnlyList a AnsiConsole.Write(table); } + private static void PrintPredictionAnchorFrontier( + IReadOnlyList predictedAnchors, + IReadOnlyList realAnchors, + string title) + { + if (predictedAnchors.Count == 0) + { + AnsiConsole.MarkupLine("[yellow]Prediction Anchor Frontier:[/] no scored virtual prediction anchors were found. Final DuckDB preselection will skip anchor-based discovery rather than mix prediction and real spaces."); + return; + } + + var table = new Table().RoundedBorder().BorderColor(Color.Grey); + table.Title = new TableTitle(Markup.Escape(title)); + table.AddColumn("Virtual Anchor"); + table.AddColumn("Canonical Key"); + table.AddColumn("Config Key"); + table.AddColumn("Pred KLD"); + table.AddColumn("Pred Size GiB"); + table.AddColumn("Rank"); + table.AddColumn("Conf"); + table.AddColumn("Matching Real Anchor"); + table.AddColumn("Real KLD/Size GiB"); + + foreach (var anchor in predictedAnchors.OrderBy(x => x.PredictedKld).ThenBy(x => x.PredictedSizeBytes)) + { + var real = FindMatchingRealAnchor(anchor, realAnchors); + table.AddRow( + Markup.Escape(anchor.DisplayName), + Markup.Escape(anchor.BaselineCanonicalKey), + Markup.Escape(anchor.ConfigKey), + anchor.PredictedKld.ToString("0.000000"), + ToGiB(anchor.PredictedSizeBytes).ToString("0.00"), + anchor.PredictionRank.ToString("N0"), + anchor.PredictionConfidence.ToString("0.###"), + real == null ? "[grey]none[/]" : Markup.Escape(real.DisplayName), + real == null ? "[grey]n/a[/]" : $"{real.Kld:0.000000} / {ToGiB(real.SizeBytes):0.00}"); + } + + AnsiConsole.Write(table); + } + + private static BenchmarkSnapshotRecord? FindMatchingRealAnchor( + PredictedAnchorRow predictedAnchor, + IReadOnlyList realAnchors) + { + string predictedKey = NormalizeAnchorKey(predictedAnchor.BaselineCanonicalKey); + var byCanonical = realAnchors.FirstOrDefault(x => + string.Equals( + NormalizeAnchorKey(HybridBenchmarkRepository.ResolveSourceBaselineForProvider(x.Quant).CanonicalKey), + predictedKey, + StringComparison.Ordinal)); + + if (byCanonical != null) + return byCanonical; + + return realAnchors.FirstOrDefault(x => + HybridBenchmarkRepository.ResolveSourceBaselineForProvider(x.Quant).UniqueId == predictedAnchor.RuntimeBaselineId); + } + + private static void LogPredictionAndRealPairLines( + BenchmarkSnapshotRecord realHigherDamageSmaller, + BenchmarkSnapshotRecord realLowerDamageLarger, + PredictedAnchorRow predictedHigherDamageSmaller, + PredictedAnchorRow predictedLowerDamageLarger, + string label) + { + AnsiConsole.MarkupLine($"[grey]{Markup.Escape(label)}:[/] [cyan]{Markup.Escape(realHigherDamageSmaller.DisplayName)}[/] -> [cyan]{Markup.Escape(realLowerDamageLarger.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] Prediction line:[/] {Markup.Escape(predictedHigherDamageSmaller.DisplayName)} size={predictedHigherDamageSmaller.PredictedSizeBytes:N0} kld={predictedHigherDamageSmaller.PredictedKld:0.000000} -> {Markup.Escape(predictedLowerDamageLarger.DisplayName)} size={predictedLowerDamageLarger.PredictedSizeBytes:N0} kld={predictedLowerDamageLarger.PredictedKld:0.000000}"); + AnsiConsole.MarkupLine($"[grey] Real validation line:[/] {Markup.Escape(realHigherDamageSmaller.DisplayName)} size={realHigherDamageSmaller.SizeBytes:N0} kld={realHigherDamageSmaller.Kld:0.000000} -> {Markup.Escape(realLowerDamageLarger.DisplayName)} size={realLowerDamageLarger.SizeBytes:N0} kld={realLowerDamageLarger.Kld:0.000000}"); + } + private static void PrintCandidatePreviewTable(IReadOnlyList candidates, string title) { if (candidates.Count == 0) @@ -1272,6 +1465,26 @@ private static object ToAnchorLog(BenchmarkSnapshotRecord anchor) }; } + private static object? ToPredictionAnchorLog(PredictedAnchorRow? anchor) + { + if (anchor == null) + return null; + + return new + { + configKey = anchor.ConfigKey, + displayName = anchor.DisplayName, + baselineCanonicalKey = anchor.BaselineCanonicalKey, + runtimeBaselineId = anchor.RuntimeBaselineId, + predictedSizeBytes = anchor.PredictedSizeBytes, + predictedSizeGiB = ToGiB(anchor.PredictedSizeBytes), + predictedKld = anchor.PredictedKld, + predictionRank = anchor.PredictionRank, + predictionConfidence = anchor.PredictionConfidence, + isVirtualPredictionAnchor = anchor.IsVirtualPredictionAnchor + }; + } + private static async Task WriteSelectionPhaseDiagnosticsAsync( IReadOnlyList phaseDiagnostics, IReadOnlyList validationFailures, @@ -1378,6 +1591,10 @@ private static object ToValidationAttemptLog(CandidateValidationResult attempt) }, selectionContext = new { + predictionWindowMinSizeBytes = c.PredictionWindowMinSizeBytes, + predictionWindowMaxSizeBytes = c.PredictionWindowMaxSizeBytes, + realValidationWindowMinSizeBytes = c.WindowMinSizeBytes, + realValidationWindowMaxSizeBytes = c.WindowMaxSizeBytes, candidatePoolSize = c.CandidatePoolSize, windowCandidateCount = c.WindowCandidateCount, lineBeatingCandidateCount = c.LineBeatingCandidateCount, @@ -1405,8 +1622,10 @@ private static object ToValidationAttemptLog(CandidateValidationResult attempt) }, anchors = new { - higherDamageSmaller = ToAnchorLog(c.HigherDamageAnchor), - lowerDamageLarger = ToAnchorLog(c.LowerDamageAnchor) + realHigherDamageSmaller = ToAnchorLog(c.HigherDamageAnchor), + realLowerDamageLarger = ToAnchorLog(c.LowerDamageAnchor), + predictionHigherDamageSmaller = ToPredictionAnchorLog(c.HigherDamagePredictionAnchor), + predictionLowerDamageLarger = ToPredictionAnchorLog(c.LowerDamagePredictionAnchor) } }; } @@ -1470,6 +1689,8 @@ private static ulong AddPercent(ulong bytes, double percent) private static ulong Distance(ulong left, ulong right) => left >= right ? left - right : right - left; + private static string NormalizeAnchorKey(string? value) => (value ?? string.Empty).Trim().ToLowerInvariant(); + private static string NormalizePublicEliminationReason(string reason) { if (string.IsNullOrWhiteSpace(reason)) @@ -1528,6 +1749,10 @@ private sealed class SelectionPhaseDiagnostic public int PhaseWindowCount { get; init; } public object? HigherDamageSmaller { get; init; } public object? LowerDamageLarger { get; init; } + public object? PredictionHigherDamageSmaller { get; init; } + public object? PredictionLowerDamageLarger { get; init; } + public ulong PredictionWindowMinSizeBytes { get; init; } + public ulong PredictionWindowMaxSizeBytes { get; init; } public ulong WindowMinSizeBytes { get; init; } public ulong WindowMaxSizeBytes { get; init; } public double WindowSizeGiB { get; init; } diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index 6664077..d3cbe49 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -3,6 +3,7 @@ using System.Numerics; using DuckDB.NET.Data; using MagicQuant.Helpers; +using MagicQuant.Models; using MQ.DB; using MQ.DB.Data; using MQ.DB.Models; @@ -55,7 +56,7 @@ public async Task GetRemainingCombinationCountAsync(CancellationToken ct = using var cmd = connection.CreateCommand(); cmd.CommandText = $"SELECT COUNT(*) FROM {TableName};"; - return (long)(await cmd.ExecuteScalarAsync(ct) ?? 0L); + return ToInt64(await cmd.ExecuteScalarAsync(ct)); } public async Task> GetRemainingTensorConfigsAsync(CancellationToken ct = default) @@ -151,21 +152,26 @@ public async Task InitializeAsync(bool forceRebuild = false, CancellationToken c long currentDbCount = tableShapeOk ? await GetRowCountAsync(connection, ct) : -1; + long currentNormalDbCount = tableShapeOk + ? await GetNormalRowCountAsync(connection, ct) + : -1; AnsiConsole.MarkupLine( - $"[bold]DuckDB Check:[/] Current Rows: [cyan]{currentDbCount:N0}[/] | Expected: [yellow]{expectedTotal:N0}[/]"); + $"[bold]DuckDB Check:[/] Current Rows: [cyan]{currentDbCount:N0}[/] | Normal Candidate Rows: [cyan]{currentNormalDbCount:N0}[/] | Expected Normal Rows: [yellow]{expectedTotal:N0}[/]"); if (!tableShapeOk) AnsiConsole.MarkupLine("[yellow]DuckDB table shape is missing or stale. Rebuild required.[/]"); - if (forceRebuild || !tableShapeOk || currentDbCount != expectedTotal) + if (forceRebuild || !tableShapeOk || new BigInteger(currentNormalDbCount) != expectedTotal) { AnsiConsole.MarkupLine("[bold red]DuckDB empty, mismatch, forced, or stale.[/] Initializing/Rebuilding..."); await RebuildDatabaseAsync(connection, expectedTotal, ct); } else { - AnsiConsole.MarkupLine("[bold green]DuckDB is synchronized and ready.[/]"); + var virtualAnchorStats = await AppendVirtualPredictionAnchorRowsAsync(connection, ct); + AnsiConsole.MarkupLine( + $"[bold green]DuckDB is synchronized and ready.[/] [grey]Virtual anchors inserted={virtualAnchorStats.InsertedRows:N0}, marked={virtualAnchorStats.MarkedExistingRows:N0}[/]"); } } @@ -325,7 +331,14 @@ private async Task GetRowCountAsync(DuckDBConnection connection, Cancellat { using var countCmd = connection.CreateCommand(); countCmd.CommandText = $"SELECT COUNT(*) FROM {TableName}"; - return (long)(await countCmd.ExecuteScalarAsync(ct) ?? 0L); + return ToInt64(await countCmd.ExecuteScalarAsync(ct)); + } + + private async Task GetNormalRowCountAsync(DuckDBConnection connection, CancellationToken ct) + { + using var countCmd = connection.CreateCommand(); + countCmd.CommandText = $"SELECT COUNT(*) FROM {TableName} WHERE COALESCE(IsVirtualPredictionAnchor, FALSE) = FALSE"; + return ToInt64(await countCmd.ExecuteScalarAsync(ct)); } private async Task RebuildDatabaseAsync( @@ -367,9 +380,12 @@ private async Task RebuildDatabaseAsync( $"[grey]| Rate:[/] {rowsPerSec:N0} rows/sec"); } + var virtualAnchorStats = await AppendVirtualPredictionAnchorRowsAsync(connection, ct); + overallSw.Stop(); long finalCount = await GetRowCountAsync(connection, ct); + BigInteger expectedIncludingVirtualRows = expectedTotal + new BigInteger(virtualAnchorStats.InsertedRows); double finalRate = overallSw.Elapsed.TotalSeconds <= 0 ? 0 @@ -378,11 +394,199 @@ private async Task RebuildDatabaseAsync( AnsiConsole.MarkupLine( $"[bold green]DuckDB rebuild complete.[/] " + $"[grey]| Inserted tracked:[/] {insertedGrandTotal:N0} " + + $"[grey]| Virtual anchors inserted:[/] {virtualAnchorStats.InsertedRows:N0} " + + $"[grey]| Virtual anchors marked:[/] {virtualAnchorStats.MarkedExistingRows:N0} " + $"[grey]| Final row count:[/] {finalCount:N0} " + $"[grey]| Time:[/] {overallSw.Elapsed.TotalMinutes:N2} min " + $"[grey]| Avg rate:[/] {finalRate:N0} rows/sec"); - if (new BigInteger(finalCount) != expectedTotal) - throw new InvalidOperationException($"Final tensor_configs row count mismatch. actual={finalCount:N0}, expected={expectedTotal:N0}."); + if (new BigInteger(finalCount) != expectedIncludingVirtualRows) + throw new InvalidOperationException($"Final tensor_configs row count mismatch. actual={finalCount:N0}, expected={expectedIncludingVirtualRows:N0} (normal={expectedTotal:N0}, virtual-inserted={virtualAnchorStats.InsertedRows:N0})."); + } + + private static async Task AppendVirtualPredictionAnchorRowsAsync( + DuckDBConnection connection, + CancellationToken ct) + { + var activeGroups = GetVirtualPredictionAnchorActiveGroups(); + if (activeGroups.Count == 0) + { + AnsiConsole.MarkupLine("[yellow]Virtual prediction anchors skipped:[/] no active tensor groups were available."); + return new VirtualAnchorInsertStats(); + } + + var carrier = ChooseVirtualPredictionAnchorCarrier(); + var anchorBaselines = GetVirtualPredictionAnchorBaselines() + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .OrderByDescending(x => x.BitRange) + .ThenBy(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId) + .ToList(); + + if (anchorBaselines.Count == 0) + { + AnsiConsole.MarkupLine("[yellow]Virtual prediction anchors skipped:[/] no active baseline identities were available."); + return new VirtualAnchorInsertStats(); + } + + int inserted = 0; + int markedExisting = 0; + var preview = new List(); + + foreach (var baseline in anchorBaselines) + { + ct.ThrowIfCancellationRequested(); + + var quant = HybridQuant.CreateLearnedCandidateBlanket( + baseQuant: carrier, + groups: activeGroups, + candidateBaseline: baseline); + + var config = (TensorConfig)quant; + bool rowAlreadyExisted = await VirtualAnchorRowExistsAsync(connection, config, ct); + await UpsertVirtualAnchorRowAsync(connection, config, baseline, ct); + + if (rowAlreadyExisted) + markedExisting++; + else + inserted++; + + if (preview.Count < 12) + preview.Add($"{baseline.Names[0]} -> {TensorConfigIdentity.ToKey(config)}"); + } + + string groupList = string.Join(", ", activeGroups.Select(x => x.Name)); + AnsiConsole.MarkupLine( + $"[green]Virtual prediction anchors staged:[/] inserted=[cyan]{inserted:N0}[/], marked-existing=[cyan]{markedExisting:N0}[/], " + + $"carrier=[cyan]{Markup.Escape(carrier.Names[0])}[/], groups=[cyan]{Markup.Escape(groupList)}[/]"); + + foreach (var item in preview) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(item)}[/]"); + + if (anchorBaselines.Count > preview.Count) + AnsiConsole.MarkupLine($" [grey]- ... {anchorBaselines.Count - preview.Count:N0} more virtual anchors[/]"); + + return new VirtualAnchorInsertStats + { + InsertedRows = inserted, + MarkedExistingRows = markedExisting + }; + } + + private static IReadOnlyList GetVirtualPredictionAnchorActiveGroups() + { + return TReg.All + .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + } + + private static IReadOnlyList GetVirtualPredictionAnchorBaselines() + { + bool hasUsableImatrix = RuntimeSearchSpace.HasUsableImatrix(); + + return BaselineQuants.GetLearningBaselines(hasUsableImatrix) + .Concat(BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix, RuntimeSearchSpace.AllowHighPrecisionHybrids)) + .Concat(BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix)) + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .GroupBy(x => NormalizeAnchorKey(x.CanonicalKey), StringComparer.Ordinal) + .Select(g => g.OrderBy(x => x.UniqueId).First()) + .OrderBy(x => x.UniqueId) + .ToList(); + } + + private static BaselineQuants ChooseVirtualPredictionAnchorCarrier() + { + var activeCarriers = RuntimeSearchSpace.GetActiveCombinationBaselines() + .OrderBy(x => x.UniqueId) + .ToList(); + + if (activeCarriers.Count == 0) + return BaselineQuants.Q8_0; + + if (activeCarriers.Count == 1) + return activeCarriers[0]; + + var q8 = activeCarriers.FirstOrDefault(x => x.UniqueId == BaselineQuants.Q8_0.UniqueId); + if (q8 != null) + return q8; + + return activeCarriers + .OrderByDescending(x => x.BitRange) + .ThenByDescending(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId) + .First(); + } + + private static async Task VirtualAnchorRowExistsAsync( + DuckDBConnection connection, + TensorConfig config, + CancellationToken ct) + { + using var cmd = connection.CreateCommand(); + cmd.CommandText = $"SELECT COUNT(*) FROM {TableName} WHERE {BuildSlotPredicateSql(config)};"; + var value = await cmd.ExecuteScalarAsync(ct); + return ToInt64(value) > 0; + } + + private static async Task UpsertVirtualAnchorRowAsync( + DuckDBConnection connection, + TensorConfig config, + BaselineQuants baseline, + CancellationToken ct) + { + if (await VirtualAnchorRowExistsAsync(connection, config, ct)) + { + using var update = connection.CreateCommand(); + update.CommandText = $@" +UPDATE {TableName} +SET IsProtectedAnchor = TRUE, + IsVirtualPredictionAnchor = TRUE, + AnchorBaselineRuntimeId = {baseline.UniqueId}, + AnchorBaselineCanonicalKey = {SqlString(baseline.CanonicalKey)}, + AnchorDisplayName = {SqlString(baseline.Names.FirstOrDefault() ?? baseline.CanonicalKey)} +WHERE {BuildSlotPredicateSql(config)};"; + await update.ExecuteNonQueryAsync(ct); + return; + } + + using var insert = connection.CreateCommand(); + insert.CommandText = $@" +INSERT INTO {TableName} +({CombinationDuckDbSchema.SlotColumnList}, IsProtectedAnchor, IsVirtualPredictionAnchor, AnchorBaselineRuntimeId, AnchorBaselineCanonicalKey, AnchorDisplayName) +VALUES ({config.BaseQuant}, {config.Embeddings}, {config.LmHead}, {config.AttnQ}, {config.AttnKV}, {config.AttnOutput}, {config.FfnUpGate}, {config.FfnDown}, {config.MoeExperts}, {config.MoeRouter}, TRUE, TRUE, {baseline.UniqueId}, {SqlString(baseline.CanonicalKey)}, {SqlString(baseline.Names.FirstOrDefault() ?? baseline.CanonicalKey)});"; + await insert.ExecuteNonQueryAsync(ct); + } + + private static string BuildSlotPredicateSql(TensorConfig config) + { + return $"BaseQuant = {config.BaseQuant} AND Embeddings = {config.Embeddings} AND LmHead = {config.LmHead} AND AttnQ = {config.AttnQ} AND AttnKV = {config.AttnKV} AND AttnOutput = {config.AttnOutput} AND FfnUpGate = {config.FfnUpGate} AND FfnDown = {config.FfnDown} AND MoeExperts = {config.MoeExperts} AND MoeRouter = {config.MoeRouter}"; + } + + private static string SqlString(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return "NULL"; + + return $"'{value.Replace("'", "''")}'"; + } + + private static long ToInt64(object? value) + { + if (value is null || value is DBNull) + return 0L; + + if (value is BigInteger big) + return (long)big; + + return Convert.ToInt64(value); + } + + private static string NormalizeAnchorKey(string? value) => (value ?? string.Empty).Trim().ToLowerInvariant(); + + private sealed class VirtualAnchorInsertStats + { + public int InsertedRows { get; init; } + public int MarkedExistingRows { get; init; } } private async Task BulkAppendAsync( @@ -748,4 +952,4 @@ private static byte NormalizeBaselineIdForIsolation(byte baselineId) return builtIn?.UniqueId ?? baselineId; } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 4258db4..25832f2 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -147,8 +147,133 @@ public async Task GetPredictionStatusAsync(Canc }; } + public async Task> GetPredictedAnchorRowsAsync(CancellationToken ct = default) + { + string sql = $@" +SELECT {CombinationDuckDbSchema.SlotColumnList}, + COALESCE(AnchorDisplayName, AnchorBaselineCanonicalKey, '') AS AnchorDisplayName, + COALESCE(AnchorBaselineCanonicalKey, '') AS AnchorBaselineCanonicalKey, + COALESCE(AnchorBaselineRuntimeId, 0) AS AnchorBaselineRuntimeId, + {CombinationDuckDbSchema.EffectivePredictedKldSql} AS PredictedKld, + PredictedSizeBytes, + PredictionConfidence, + PredictionRank, + COALESCE(IsVirtualPredictionAnchor, FALSE) AS IsVirtualPredictionAnchor +FROM {TableName} +WHERE {CombinationDuckDbSchema.VirtualPredictionAnchorPredicateSql} + AND COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionRank IS NOT NULL +ORDER BY PredictedKld ASC, + PredictedSizeBytes ASC, + PredictionRank ASC;"; + + using var c = new DuckDBConnection(ConnectionString); + await c.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(c, ct); + await EnsureTensorConfigsTableExistsAsync(c, ct); + + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + + var list = new List(); + using var r = await cmd.ExecuteReaderAsync(ct); + while (await r.ReadAsync(ct)) + list.Add(MapPredictedAnchorRow(r)); + + return list; + } + + public async Task FindPredictedAnchorForRealAnchorAsync( + BenchmarkSnapshotRecord realAnchor, + IReadOnlyList predictedAnchors, + CancellationToken ct = default) + { + ct.ThrowIfCancellationRequested(); + + var sourceBaseline = HybridBenchmarkRepository.ResolveSourceBaselineForProvider(realAnchor.Quant); + if (HybridBenchmarkRepository.IsTrueMagicQuantHybrid(realAnchor.Quant)) + return await QueryPredictedAnchorForConfigAsync(realAnchor, sourceBaseline, ct); + + string canonicalKey = NormalizeAnchorKey(sourceBaseline.CanonicalKey); + + var byCanonical = predictedAnchors + .Where(x => !string.IsNullOrWhiteSpace(x.BaselineCanonicalKey)) + .FirstOrDefault(x => string.Equals(NormalizeAnchorKey(x.BaselineCanonicalKey), canonicalKey, StringComparison.Ordinal)); + + if (byCanonical != null) + return byCanonical; + + var byRuntimeId = predictedAnchors.FirstOrDefault(x => x.RuntimeBaselineId == sourceBaseline.UniqueId); + if (byRuntimeId != null) + return byRuntimeId; + + var displayNames = sourceBaseline.Names + .Concat([realAnchor.DisplayName, realAnchor.BaselineFamily]) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Select(NormalizeAnchorKey) + .ToHashSet(StringComparer.Ordinal); + + var byDisplay = predictedAnchors.FirstOrDefault(x => displayNames.Contains(NormalizeAnchorKey(x.DisplayName))); + if (byDisplay != null) + return byDisplay; + + // Accepted MagicQuant hybrids can become real anchors in later phases. They will + // not have a virtual baseline-anchor row, but their own tensor config should still + // be present and scored in DuckDB. Use that predicted row as the phase-local + // prediction-space anchor instead of falling back to real KLD/size. + return await QueryPredictedAnchorForConfigAsync(realAnchor, sourceBaseline, ct); + } + + private async Task QueryPredictedAnchorForConfigAsync( + BenchmarkSnapshotRecord realAnchor, + BaselineQuants sourceBaseline, + CancellationToken ct) + { + string sql = $@" +SELECT {CombinationDuckDbSchema.SlotColumnList}, + {CombinationDuckDbSchema.EffectivePredictedKldSql} AS PredictedKld, + PredictedSizeBytes, + PredictionConfidence, + PredictionRank, + COALESCE(IsVirtualPredictionAnchor, FALSE) AS IsVirtualPredictionAnchor +FROM {TableName} +WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL + AND PredictedSizeBytes IS NOT NULL + AND PredictionRank IS NOT NULL + AND {BuildSlotPredicateSql(realAnchor.Config)} +LIMIT 1;"; + + using var c = new DuckDBConnection(ConnectionString); + await c.OpenAsync(ct); + await ConfigureFastLoadSessionAsync(c, ct); + await EnsureTensorConfigsTableExistsAsync(c, ct); + + using var cmd = c.CreateCommand(); + cmd.CommandText = sql; + + using var r = await cmd.ExecuteReaderAsync(ct); + if (!await r.ReadAsync(ct)) + return null; + + var config = ReadTensorConfig(r); + return new PredictedAnchorRow + { + Config = config, + ConfigKey = TensorConfigIdentity.ToKey(config), + DisplayName = realAnchor.DisplayName, + BaselineCanonicalKey = sourceBaseline.CanonicalKey, + RuntimeBaselineId = sourceBaseline.UniqueId, + PredictedKld = ToDouble(r.GetValue(10)), + PredictedSizeBytes = ToUInt64(r.GetValue(11)), + PredictionConfidence = ToDouble(r.GetValue(12)), + PredictionRank = ToUInt64(r.GetValue(13)), + IsVirtualPredictionAnchor = ToBoolean(r.GetValue(14)) + }; + } + public async Task CountStrictDominanceCandidatesAsync( - BenchmarkSnapshotRecord anchor, + PredictedAnchorRow anchor, CancellationToken ct = default) { string sql = $@" @@ -164,7 +289,7 @@ AND PredictionRank IS NOT NULL return await ExecuteCountAsync( sql, - new object[] { anchor.SizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.Kld }, + new object[] { anchor.PredictedSizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.PredictedKld }, ct); } @@ -187,8 +312,8 @@ AND PredictionRank IS NOT NULL } public async Task CountBetterThanLinearCandidatesAsync( - BenchmarkSnapshotRecord higherDamageSmaller, - BenchmarkSnapshotRecord lowerDamageLarger, + PredictedAnchorRow higherDamageSmaller, + PredictedAnchorRow lowerDamageLarger, ulong minSize, ulong maxSize, CancellationToken ct = default) @@ -213,18 +338,18 @@ FROM scored WHERE LinearExpectedKld - PredictedKld > ?;"; double denominator = Math.Max( - (double)lowerDamageLarger.SizeBytes - higherDamageSmaller.SizeBytes, + (double)lowerDamageLarger.PredictedSizeBytes - higherDamageSmaller.PredictedSizeBytes, 1d); return await ExecuteCountAsync( sql, new object[] { - higherDamageSmaller.Kld, - (double)higherDamageSmaller.SizeBytes, + higherDamageSmaller.PredictedKld, + (double)higherDamageSmaller.PredictedSizeBytes, denominator, - lowerDamageLarger.Kld, - higherDamageSmaller.Kld, + lowerDamageLarger.PredictedKld, + higherDamageSmaller.PredictedKld, minSize, maxSize, Config.SelectionMinimumKldImprovementEpsilon @@ -233,7 +358,7 @@ FROM scored } public async Task> QueryStrictDominanceCandidatesAsync( - BenchmarkSnapshotRecord anchor, + PredictedAnchorRow anchor, int limit, CancellationToken ct = default) { @@ -260,15 +385,19 @@ PredictionConfidence DESC return await QueryPredictedRowsAsync( sql, - new object[] { anchor.SizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.Kld, limit }, + new object[] { anchor.PredictedSizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.PredictedKld, limit }, ct); } public async Task> QueryBetterThanLinearCandidatesAsync( BenchmarkSnapshotRecord higherDamageSmaller, BenchmarkSnapshotRecord lowerDamageLarger, - ulong minSize, - ulong maxSize, + PredictedAnchorRow higherDamagePredictionAnchor, + PredictedAnchorRow lowerDamagePredictionAnchor, + ulong predictionWindowMinSize, + ulong predictionWindowMaxSize, + ulong realValidationWindowMinSize, + ulong realValidationWindowMaxSize, HybridSelectionReason reason, string windowLabel, int limit, @@ -314,7 +443,7 @@ PredictionRank ASC LIMIT ?;"; double denominator = Math.Max( - (double)lowerDamageLarger.SizeBytes - higherDamageSmaller.SizeBytes, + (double)lowerDamagePredictionAnchor.PredictedSizeBytes - higherDamagePredictionAnchor.PredictedSizeBytes, 1d); using var c = new DuckDBConnection(ConnectionString); @@ -327,13 +456,13 @@ PredictionRank ASC foreach (var value in new object[] { - higherDamageSmaller.Kld, - (double)higherDamageSmaller.SizeBytes, + higherDamagePredictionAnchor.PredictedKld, + (double)higherDamagePredictionAnchor.PredictedSizeBytes, denominator, - lowerDamageLarger.Kld, - higherDamageSmaller.Kld, - minSize, - maxSize, + lowerDamagePredictionAnchor.PredictedKld, + higherDamagePredictionAnchor.PredictedKld, + predictionWindowMinSize, + predictionWindowMaxSize, Config.SelectionMinimumKldImprovementEpsilon, limit }) @@ -356,8 +485,12 @@ PredictionRank ASC Reason = reason, HigherDamageAnchor = higherDamageSmaller, LowerDamageAnchor = lowerDamageLarger, - WindowMinSizeBytes = minSize, - WindowMaxSizeBytes = maxSize, + HigherDamagePredictionAnchor = higherDamagePredictionAnchor, + LowerDamagePredictionAnchor = lowerDamagePredictionAnchor, + PredictionWindowMinSizeBytes = predictionWindowMinSize, + PredictionWindowMaxSizeBytes = predictionWindowMaxSize, + WindowMinSizeBytes = realValidationWindowMinSize, + WindowMaxSizeBytes = realValidationWindowMaxSize, LinearExpectedKld = line, PredictedGainOverLine = gain, WindowLabel = windowLabel, @@ -391,6 +524,28 @@ private async Task> QueryPredictedRowsAsync return list; } + private static PredictedAnchorRow MapPredictedAnchorRow(System.Data.Common.DbDataReader r) + { + var config = ReadTensorConfig(r); + string canonicalKey = Convert.ToString(r.GetValue(11)) ?? string.Empty; + byte runtimeBaselineId = ToByte(r.GetValue(12)); + string displayName = Convert.ToString(r.GetValue(10)) ?? canonicalKey; + + return new PredictedAnchorRow + { + Config = config, + ConfigKey = TensorConfigIdentity.ToKey(config), + DisplayName = string.IsNullOrWhiteSpace(displayName) ? canonicalKey : displayName, + BaselineCanonicalKey = canonicalKey, + RuntimeBaselineId = runtimeBaselineId, + PredictedKld = ToDouble(r.GetValue(13)), + PredictedSizeBytes = ToUInt64(r.GetValue(14)), + PredictionConfidence = ToDouble(r.GetValue(15)), + PredictionRank = ToUInt64(r.GetValue(16)), + IsVirtualPredictionAnchor = ToBoolean(r.GetValue(17)) + }; + } + private static RankSafePredictionRow MapPredictedRow(System.Data.Common.DbDataReader r, int? anomalyAdjustmentColumnIndex = null) { var config = ReadTensorConfig(r); @@ -453,6 +608,13 @@ private async Task ExecuteCountAsync(string sql, object[] args, Cancellati return ToInt64(await cmd.ExecuteScalarAsync(ct)); } + private static string BuildSlotPredicateSql(TensorConfig config) + { + return $"BaseQuant = {config.BaseQuant} AND Embeddings = {config.Embeddings} AND LmHead = {config.LmHead} AND AttnQ = {config.AttnQ} AND AttnKV = {config.AttnKV} AND AttnOutput = {config.AttnOutput} AND FfnUpGate = {config.FfnUpGate} AND FfnDown = {config.FfnDown} AND MoeExperts = {config.MoeExperts} AND MoeRouter = {config.MoeRouter}"; + } + + private static string NormalizeAnchorKey(string? value) => (value ?? string.Empty).Trim().ToLowerInvariant(); + private static long ToInt64(object? value) { if (value is null || value is DBNull) @@ -497,6 +659,20 @@ private static double ToDouble(object? value) return Convert.ToDouble(value); } + private static bool ToBoolean(object? value) + { + if (value is null || value is DBNull) + return false; + + if (value is bool b) + return b; + + if (value is BigInteger big) + return big != BigInteger.Zero; + + return Convert.ToBoolean(value); + } + private static async Task ConfigureFastLoadSessionAsync(DuckDBConnection connection, CancellationToken ct) { using (var cmd = connection.CreateCommand()) @@ -563,4 +739,4 @@ private static async Task EnsureTensorConfigsTableExistsAsync(DuckDBConnection c "This almost always means the generator and prediction reader are using different DuckDB filenames, " + "or prediction started before QuantDatabaseService initialized/rebuilt the search-space table."); } -} \ No newline at end of file +} From dcf638560522447ba9a077a92997daaa8c3dac97 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 4 May 2026 15:52:06 -0400 Subject: [PATCH 193/258] back to 27B testing. Q8 is broke in predictive space now, working on it now. --- .../config.qwen3-4B-2507-Instruct.dev.yaml | 394 ++++++++++++++++++ .../Configs/config.qwen3.6-27b.dev.yaml | 2 +- MagicQuant/Program.cs | 2 +- MagicQuant/config.dev.yaml | 22 +- 4 files changed, 407 insertions(+), 13 deletions(-) create mode 100644 MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml diff --git a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml new file mode 100644 index 0000000..b1064e9 --- /dev/null +++ b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml @@ -0,0 +1,394 @@ +paths: + magic_quant_root: + model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ + llama_root: + llama_bin: + convert_script: + scratch_roots: + - /mnt/world8/ + - /home/slurp/ + - /mnt/world7/ + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +learning: + # Destructive relearn options are intentionally targeted. + # These are transient runtime commands and are not persisted as DB state. + # When any option below is enabled, MagicQuant prints a count summary and asks + # for confirmation before deleting/relearning anything. + # + # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, + # and execution probe cache rows scoped to the active architecture family. + # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, + # TensorCombo, or BaselineQuantDefinition rows. + force_relearn_architecture_family: false + + # Relearn built-in/standard baselines by display/canonical name for the current + # architecture family and active tensor group profile. + # Example: + # force_relearn_standard_baselines: + # - Q6_K + # - IQ4_XS + force_relearn_standard_baselines: [] + + # Safety gate for tensor group regex/profile changes. After MagicQuant reads the + # native BF16 GGUF tensor list, it prints group counts, example tensors, + # ambiguous matches, unresolved tensors, and base-quant exception counts, then + # asks before continuing. Keep this true unless running fully unattended. + confirm_tensor_group_profile: true + + # Safe/idempotent repair mode for accidental regex mistakes. + # + # Default true: on every run MagicQuant checks whether older DB learned tensor + # truth can be copied into the active TensorGroupProfile by reapplying the + # current regex/base_quant_exceptions rules. If nothing changed or current rows + # already exist, it skips cleanly and does not create duplicates. + # + # This avoids needless re-download/re-quantization of pure learning baselines + # after regex-only regrouping. Old benchmarks/learned rows remain attached to + # their original TensorGroupProfile and are ignored unless that profile becomes + # active again. + # + # Disable only when you intentionally want the slower/full path to regenerate + # learned grouping truth instead of rebucketing from DB snapshots. + # CLI disable aliases: + # --no-rebucket-learned-tensor-groups + # --disable-tensor-group-rebucket + # --full-relearn-tensor-groups + rebucket_learned_tensor_groups_from_existing_truth: true + + +readme: + # Optional title model name override used in: + # # MagicQuant Hybrids (v2.0) - + # If blank, MagicQuant uses identity.architecture_family_name. + title_model_name_override: Qwen3.6-27B + + # Hugging Face README frontmatter. + # Scalars render as: + # license: apache-2.0 + # Arrays render as: + # tags: + # - gguf + # - text-generation + # + # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. + frontmatter: + license: apache-2.0 + tags: + - gguf + - text-generation + - magicquant + - conversational + base_model: + - Qwen/Qwen3.6-27B + +hardware: + gpu_memory_limits_gb: + 0: 19 + 1: 23 + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1_5m.jsonl + +# Legacy evolution survivor knobs were removed from YAML. +# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. + +isolation_pruning: + # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 + minimum_isolation_reduction_to_continue_ratio: 0.00 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + + +prediction: + # Rank-safe isolation KLD predictor. + # + # manual_max_predicted_size_bytes is retained only as an emergency compatibility + # field for older helper code. Leave it at 0 for the new chooser. + manual_max_predicted_size_bytes: 0 + + # Candidate bit-stress thresholds for the low-bit interaction correction. + # The predictor fits each candidate threshold against existing category=General + # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. + bit_stress_threshold_candidates: + - 4.0 + - 5.0 + - 6.0 + - 7.0 + - 8.0 + - 9.0 + - 10.0 + - 11.0 + - 12.0 + + # Fallback threshold when too few benchmark rows exist to fit the interaction model. + default_bit_stress_threshold: 8.0 + + # Minimum benchmark rows required before fitting the interaction correction. + minimum_fit_rows: 12 + +candidate_selection: + # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits + # inside this size premium and beats the real linear KLD improvement line. + near_baseline_max_size_growth_percent: 1.0 + + # Phase 3: interior windows between adjacent final anchors. + # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. + interior_window_fractions: + - 0.35 + - 0.35 + + # Number of predicted winners to keep per interior window. + max_candidates_per_interior_window: 1 + + # If the first predicted candidate fails real validation, try this many fallbacks. + max_fallback_attempts_per_anchor: 5 + + # Strict epsilon for lower-KLD comparisons after real benchmark validation. + minimum_kld_improvement_epsilon: 1.0e-9 + + # Final spacing pass: candidates closer than this fraction of the global survivor + # size span are collapsed unless one genuinely earns the slot. + minimum_neighbor_gap_fraction_of_global_span: 0.03 + + # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller + # anchor must provide a stronger KLD gain to justify its existence. + near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 + near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 + + # Default false: do not spend final prediction/build attempts trying to replace + # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. + # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. + allow_eight_bit_anchor_replacements: true + +anomaly_detection: + enabled: true + + # One anomaly refinement pass after smoke/probe/rule generation. + max_anomaly_refinement_rounds: 1 + + # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. + min_actual_gain_vs_twin_kld: 0.00025 + + # Minimum predicted size savings versus higher-bit twin/reference to probe. + min_predicted_size_savings_vs_twin_percent: 1.0 + + # Max changed groups in a candidate that can seed contextual probes. + max_probe_group_count: 4 + + # Max probes generated per anomaly seed. + max_probes_per_seed: 16 + + # Max anomaly probes in one run. + max_total_probes_per_run: 32 + + # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. + max_prediction_space_gap_vs_twin_kld: 0.00050 + + # Optional relative cap for prediction-space gap normalized by local anchor gap. + max_relative_prediction_penalty_vs_twin: 0.35 + + # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. + prediction_space_violation_margin: 0.00005 + + # Shrink applied to prediction-space adjustment after a rule is confirmed. + anomaly_adjustment_shrink_factor: 0.50 + + # Minimum confidence required before applying a confirmed anomaly rule. + min_rule_confidence_to_apply: 0.50 + + # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. + max_negative_adjustment_kld: 0.00075 + + # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. + max_positive_adjustment_kld: 0.00075 + + # Fractional cap relative to BaseRankSafeKld. + max_adjustment_fraction_of_base_kld: 0.75 + + # Number of top smoke candidates to consider per reference quant zone. + max_smoke_candidates_per_reference_zone: 12 + + # Store suppression-only results so false smoke is not repeatedly probed. + persist_suppression_results: true + + # Emit detailed anomaly logs. + verbose_anomaly_logging: true + + # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. + confirmed_anomaly_expansion: + enabled: true + max_neighbors_per_confirmed_rule: 6 + max_total_expansion_probes: 12 + allowed_reference_quants: + - Q8_0 + allowed_candidate_quants: + - Q6_K + - UD-Q6_K_XL + - Q5_K + - UD-Q5_K_XL + +output: + # Leave blank to default to /MagicQuant/Final_Outputs + output_dir: + output_name_prefix: Qwen3.6-27B + export_external_learned_baselines: true + + # false = normal behavior; delete/rebuild final outputs from scratch. + # true = preserve valid existing GGUFs and skip rebuilding them only when + # exact file name + byte size match benchmark truth. + # CLI --reuse-existing-final-artifacts overrides YAML. + reuse_existing_final_artifacts: false + +# Legacy bit-range bucket survival settings were removed. +# See candidate_selection above for the active final chooser settings. + +identity: + architecture_family_name: Qwen3-4B + allow_architecture_family_alias_override: false + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + + custom_repositories: + - repo_id: unsloth/Qwen3-4B-GGUF + enabled: true + short_source_name: Unsloth + source_kind: huggingface_gguf_repository + require_all_includes_to_resolve: true + validate_tensor_names_against_source_model: true + delete_partial_or_dirty_downloads: true + resume_or_retry_downloads: true + + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: false + + includes: + + - file_name: Qwen3-4B-UD-IQ2_M.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: UD-IQ2_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3-4B-UD-IQ2_XXS.gguf + baseline_family: IQ2_XXS + quantize_base_name: IQ2_XXS + display_name: UD-IQ2_XXS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3-4B-UD-IQ3_XXS.gguf + baseline_family: IQ3_XXS + quantize_base_name: IQ3_XXS + display_name: UD-IQ3_XXS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3-4B-UD-Q2_K_XL.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: UD-Q2_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3-4B-UD-Q3_K_XL.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_M + display_name: UD-Q3_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3-4B-UD-Q4_K_XL.gguf + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M + display_name: UD-Q4_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3-4B-UD-Q5_K_XL.gguf + baseline_family: Q5_K + quantize_base_name: Q5_K + display_name: UD-Q5_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3-4B-UD-Q6_K_XL.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: UD-Q6_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + +# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. +# anomaly_detection remains the low-level compatibility section; synergy_detection controls +# template transfer, composition probes, contamination suppression, and wing diagnostics. +synergy_detection: + enabled: true + max_refinement_rounds: 1 + exact_context_confidence_multiplier: 1.00 + same_selected_groups_confidence_multiplier: 0.55 + equivalent_quant_family_confidence_multiplier: 0.30 + group_family_suspicion_confidence_multiplier: 0.15 + min_confidence_to_apply_adjustment: 0.35 + min_confidence_to_schedule_transfer_probe: 0.25 + max_negative_adjustment_kld: 0.002 + max_negative_adjustment_fraction_of_base_kld: 0.75 + transfer_probe_enabled: true + max_transfer_probes_per_template: 6 + max_total_transfer_probes_per_run: 24 + transfer_probe_context_strata: + high_fidelity_max_non_reference_groups_below_q6: 1 + mid_fidelity_max_non_reference_groups_below_q6: 3 + low_fidelity_enabled: false + verbose_synergy_logging: true + min_smoke_score: 0.55 + max_smoke_gap_kld: 0.004 + top_rejected_smoke_preview: 25 + composition_probe_enabled: true + max_template_composition_group_count: 4 + max_composition_probes_per_run: 8 + max_templates_to_compose: 4 + min_template_confidence_for_composition: 0.50 + min_combined_expected_size_savings_percent: 1.0 + contaminating_passenger_detection_enabled: true + min_failure_margin_for_contamination_kld: 0.00050 + contamination_penalty_confidence_multiplier: 0.45 + suppress_repeated_contaminated_attempts: true diff --git a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml index 73482a9..a239024 100644 --- a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml +++ b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml @@ -146,7 +146,7 @@ prediction: candidate_selection: # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits # inside this size premium and beats the real linear KLD improvement line. - near_baseline_max_size_growth_percent: 1.0 + near_baseline_max_size_growth_percent: 1.5 # Phase 3: interior windows between adjacent final anchors. # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index aeba632..346a383 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -33,7 +33,7 @@ args = [ "evolution", - "--architecture-family", @"""Qwen3-4B""" + "--architecture-family", @"""Qwen3.6-27B""" ,"--reuse-existing-final-artifacts" ]; } diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index b1064e9..73482a9 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ + model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ llama_root: llama_bin: convert_script: @@ -260,7 +260,7 @@ output: # See candidate_selection above for the active final chooser settings. identity: - architecture_family_name: Qwen3-4B + architecture_family_name: Qwen3.6-27B allow_architecture_family_alias_override: false baselines: @@ -270,7 +270,7 @@ baselines: enabled_standard_explicit_group_candidates: [] custom_repositories: - - repo_id: unsloth/Qwen3-4B-GGUF + - repo_id: unsloth/Qwen3.6-27B-GGUF enabled: true short_source_name: Unsloth source_kind: huggingface_gguf_repository @@ -285,7 +285,7 @@ baselines: includes: - - file_name: Qwen3-4B-UD-IQ2_M.gguf + - file_name: Qwen3.6-27B-UD-IQ2_M.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-IQ2_M @@ -294,7 +294,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-UD-IQ2_XXS.gguf + - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf baseline_family: IQ2_XXS quantize_base_name: IQ2_XXS display_name: UD-IQ2_XXS @@ -303,7 +303,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-UD-IQ3_XXS.gguf + - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf baseline_family: IQ3_XXS quantize_base_name: IQ3_XXS display_name: UD-IQ3_XXS @@ -312,7 +312,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-UD-Q2_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-Q2_K_XL @@ -321,7 +321,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-UD-Q3_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf baseline_family: IQ3_M quantize_base_name: IQ3_M display_name: UD-Q3_K_XL @@ -330,7 +330,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-UD-Q4_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M display_name: UD-Q4_K_XL @@ -339,7 +339,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-UD-Q5_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf baseline_family: Q5_K quantize_base_name: Q5_K display_name: UD-Q5_K_XL @@ -348,7 +348,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3-4B-UD-Q6_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf baseline_family: Q6_K quantize_base_name: Q6_K display_name: UD-Q6_K_XL From 38f2f49eae5cc2b2170ca06df1509786208fdff9 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 4 May 2026 18:17:10 -0400 Subject: [PATCH 194/258] more changes --- .../DuckDbPredictionMaterializationService.cs | 12 +++- .../PredictionGuidedHybridSelectionService.cs | 10 ++++ .../Services/RankSafeKldPredictionService.cs | 56 +++++++++++++++++-- MagicQuant/config.dev.yaml | 4 +- 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs index 00f5c26..2be059a 100644 --- a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs +++ b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs @@ -146,6 +146,8 @@ IsSizePredictable BOOLEAN .OrderBy(x => x.Group.UniqueId) .ToList(); + var warnedMissingQ8IsolationGroups = new HashSet(); + using var tx = c.BeginTransaction(); foreach (var baseline in activeBaselines) @@ -181,6 +183,10 @@ await ExecuteAsync(c, else { kldPredictable = false; + if (normalizedBaselineId == BaselineQuants.Q8_0.UniqueId && warnedMissingQ8IsolationGroups.Add(slot.Group.UniqueId)) + { + AnsiConsole.MarkupLine($"[yellow]Missing KLD isolation snapshot for group '{Markup.Escape(slot.Group.Name)}' and baseline Q8_0 while building DuckDB prediction lookup. Q8_0 is quantized damage, not native truth; matching rows will stay unpredicted instead of receiving zero KLD.[/]"); + } } } @@ -751,8 +757,10 @@ private static byte GetEffectiveBaselineId(byte baseQuant, byte storedSlot) private static bool IsZeroDamageAlias(byte baselineId) { - return baselineId == BaselineQuants.Q8_0.UniqueId || - BaselineQuants.IsNativeExactAlias(baselineId); + // Only native exact aliases are zero-reference states. Q8_0 is intentionally + // excluded: it has measured isolation KLD and must be scored like every other + // quant baseline in prediction space. + return BaselineQuants.IsNativeExactAlias(baselineId); } private static double GetBitRange(byte baselineId) diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index 60bbea4..335a586 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -1356,6 +1356,16 @@ private static void PrintPredictionAnchorFrontier( } AnsiConsole.Write(table); + + var q8Anchor = predictedAnchors.FirstOrDefault(x => + x.RuntimeBaselineId == BaselineQuants.Q8_0.UniqueId || + string.Equals(NormalizeAnchorKey(x.BaselineCanonicalKey), NormalizeAnchorKey(BaselineQuants.Q8_0.CanonicalKey), StringComparison.Ordinal) || + string.Equals(x.DisplayName, BaselineQuants.Q8_0.Names[0], StringComparison.OrdinalIgnoreCase)); + + if (q8Anchor != null && Math.Abs(q8Anchor.PredictedKld) <= 1e-12d) + { + AnsiConsole.MarkupLine("[yellow]WARNING:[/] Q8_0 virtual prediction anchor has zero predicted KLD. This usually means Q8_0 isolation rows were skipped or missing. Q8_0 must not be treated as native/exact truth in prediction space."); + } } private static BenchmarkSnapshotRecord? FindMatchingRealAnchor( diff --git a/MagicQuant/Services/RankSafeKldPredictionService.cs b/MagicQuant/Services/RankSafeKldPredictionService.cs index 7fabf6b..11cec00 100644 --- a/MagicQuant/Services/RankSafeKldPredictionService.cs +++ b/MagicQuant/Services/RankSafeKldPredictionService.cs @@ -176,7 +176,7 @@ private async Task BuildContextAsync(CancellationToken var q8BaseOnly = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)q8BaseOnlyQuant, ct); if (q8BaseOnly == null) { - notes.Add("Q8 native-exact base-only anchor was missing. Size fallback will use pure Q8; KLD exact/Q8 contributions remain zero."); + notes.Add("Q8 native-exact base-only anchor was missing. Size fallback will use pure Q8; Q8_0 group KLD still requires measured Q8_0 isolation snapshots and will not be treated as zero damage."); q8BaseOnly = pureQ8; } @@ -248,6 +248,23 @@ private async Task BuildContextAsync(CancellationToken } } + var missingQ8IsolationGroups = activeGroups + .Where(group => !isolationByGroupAndBaseline.ContainsKey((group.UniqueId, BaselineQuants.Q8_0.UniqueId))) + .Select(group => group.Name) + .ToList(); + + if (missingQ8IsolationGroups.Count > 0) + { + foreach (var groupName in missingQ8IsolationGroups) + { + notes.Add($"Missing KLD isolation snapshot for group '{groupName}' and baseline Q8_0. Q8_0 is a quantized state, not native truth; prediction will not silently fall back to zero for this group."); + } + } + else + { + notes.Add($"Q8_0 isolation snapshots loaded for {activeGroups.Count:N0} active tensor groups. Q8_0 will contribute measured prediction-space KLD, not zero/native damage."); + } + return new RankSafePredictionModel( activeGroups: activeGroups, pureQ8: pureQ8, @@ -483,7 +500,7 @@ private double PredictAdditiveKld( if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalized), out var isolation)) { - notes.Add($"Missing KLD isolation snapshot for group '{group.Name}' and baseline id '{normalized}'."); + notes.Add(BuildMissingIsolationNote(group, normalized)); canPredict = false; continue; } @@ -640,10 +657,37 @@ public static byte NormalizeBaselineIdForIsolation(byte baselineId) return builtIn?.UniqueId ?? baselineId; } - private static bool IsZeroDamageAlias(byte baselineId) + private static bool IsZeroDamageAlias(byte baselineId) => IsNativeExactZeroReferenceAlias(baselineId); + + private static bool IsNativeExactZeroReferenceAlias(byte baselineId) { - return baselineId == BaselineQuants.Q8_0.UniqueId || - BaselineQuants.IsNativeExactAlias(baselineId); + // MagicQuant's zero-damage reference is native exact precision (BF16/F16/F32), + // not Q8_0. Q8_0 is a real quantized state with measured per-group isolation + // KLD and must flow through the same lookup path as Q6_K/Q5_K/Q4/etc. + return BaselineQuants.IsNativeExactAlias(baselineId); + } + + private static string BuildMissingIsolationNote(TensorGroup group, byte normalizedBaselineId) + { + var baselineName = FormatBaselineForNote(normalizedBaselineId); + if (normalizedBaselineId == BaselineQuants.Q8_0.UniqueId) + { + return $"Missing KLD isolation snapshot for group '{group.Name}' and baseline Q8_0. Q8_0 is quantized damage, not native truth; this row is marked incomplete instead of silently receiving zero KLD."; + } + + return $"Missing KLD isolation snapshot for group '{group.Name}' and baseline {baselineName} (id '{normalizedBaselineId}')."; + } + + private static string FormatBaselineForNote(byte baselineId) + { + try + { + return BaselineQuants.FromId(baselineId).Names[0]; + } + catch + { + return $"id {baselineId}"; + } } private static void PrintPredictionDiagnostics(IReadOnlyCollection rows, RankSafePredictionFit fit) @@ -715,4 +759,4 @@ private struct PavaBlock public int Count; public double Mean => Weight <= 0d ? 0d : Sum / Weight; } -} \ No newline at end of file +} diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 73482a9..9ffea81 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -208,13 +208,13 @@ anomaly_detection: prediction_space_violation_margin: 0.00005 # Shrink applied to prediction-space adjustment after a rule is confirmed. - anomaly_adjustment_shrink_factor: 0.50 + anomaly_adjustment_shrink_factor: 1.00 # Minimum confidence required before applying a confirmed anomaly rule. min_rule_confidence_to_apply: 0.50 # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. - max_negative_adjustment_kld: 0.00075 + max_negative_adjustment_kld: 0.00400 # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. max_positive_adjustment_kld: 0.00075 From 119296d9f5d936415a8af64ca03c850f9558206e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 5 May 2026 13:00:20 -0400 Subject: [PATCH 195/258] This is working absolutely amazing so far, but I'm still suspicious of 4 bit and less and a couple of found results too. --- MagicQuant/Config.cs | 3 + .../Configuration/MagicQuantYamlConfig.cs | 16 + .../Configuration/MagicQuantYamlLoader.cs | 4 + .../AnomalyAdjustedPredictionService.cs | 427 ++++++++++++++---- .../PredictionGuidedHybridSelectionService.cs | 13 +- MagicQuant/config.default.yaml | 3 + MagicQuant/config.dev.yaml | 3 + 7 files changed, 379 insertions(+), 90 deletions(-) diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index c3b5d65..2c07fb5 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -68,6 +68,9 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.CandidateSelection.AllowEightBitAnchorReplacements; + public static bool SelectionValidateAllAnomalyStrictCandidatesAfterSuccess => + Current.CandidateSelection.ValidateAllAnomalyStrictCandidatesAfterSuccess; + public static RuntimeAnomalyDetectionConfig AnomalyDetection => Current.AnomalyDetection; public static RuntimeSynergyDetectionConfig SynergyDetection => Current.SynergyDetection; public static bool AnomalyDetectionEnabled => Current.AnomalyDetection.Enabled; diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 831e66e..b284569 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -247,6 +247,13 @@ public sealed class RuntimeCandidateSelectionConfig /// Q8 remains the highest-fidelity practical anchor unless this is explicitly enabled. /// public bool AllowEightBitAnchorReplacements { get; set; } = false; + + /// + /// Legacy/diagnostic mode for strict Q8/anomaly discovery. When false, once a strict + /// candidate validates for an anchor, MagicQuant accepts it and stops spending more + /// build/benchmark attempts on the rest of the fetched top-N list. + /// + public bool ValidateAllAnomalyStrictCandidatesAfterSuccess { get; set; } = false; } @@ -267,6 +274,15 @@ public sealed class RuntimeAnomalyDetectionConfig public double MaxNegativeAdjustmentKld { get; set; } = 0.00075d; public double MaxPositiveAdjustmentKld { get; set; } = 0.00075d; public double MaxAdjustmentFractionOfBaseKld { get; set; } = 0.75d; + + /// + /// Advisory diagnostics cap for confirmed pairwise ordering corrections. Beneficial + /// pairwise rules are allowed to cross their own measured twin even when the required + /// adjustment exceeds this value; the cap is reported, not used to resurrect the old + /// broad-boost poison. + /// + public double MaxConfirmedPairwiseOrderingAdjustmentKld { get; set; } = 0.006d; + public int MaxSmokeCandidatesPerReferenceZone { get; set; } = 12; public bool PersistSuppressionResults { get; set; } = true; public bool VerboseAnomalyLogging { get; set; } = true; diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 94d211f..92481e1 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -162,6 +162,7 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) config.AnomalyDetection.MaxNegativeAdjustmentKld = Math.Max(0d, config.AnomalyDetection.MaxNegativeAdjustmentKld); config.AnomalyDetection.MaxPositiveAdjustmentKld = Math.Max(0d, config.AnomalyDetection.MaxPositiveAdjustmentKld); config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld = Math.Clamp(config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld, 0d, 1d); + config.AnomalyDetection.MaxConfirmedPairwiseOrderingAdjustmentKld = Math.Max(0d, config.AnomalyDetection.MaxConfirmedPairwiseOrderingAdjustmentKld); config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone = Math.Max(1, config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone); ApplyStandardBaselineFilters(config.Baselines); @@ -336,6 +337,9 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (Has("allow-eight-bit-anchor-replacements")) config.CandidateSelection.AllowEightBitAnchorReplacements = true; + if (Has("validate-all-anomaly-strict-candidates-after-success")) + config.CandidateSelection.ValidateAllAnomalyStrictCandidatesAfterSuccess = true; + config.Output.OutputDir = Prefer(Get("output-dir"), config.Output.OutputDir); config.Output.OutputNamePrefix = Prefer(Get("output-name-prefix"), config.Output.OutputNamePrefix); if (Has("export-external-learned-baselines")) config.Output.ExportExternalLearnedBaselines = true; diff --git a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs index f41c406..45de43d 100644 --- a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs +++ b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs @@ -1,16 +1,19 @@ using DuckDB.NET.Data; -using System.Text.Json; using MagicQuant.Models; using MQ.DB; using MQ.DB.Models; -using System.Numerics; using MQ.DB.Models.DbModels; using Spectre.Console; +using System.Globalization; +using System.Numerics; +using System.Text.Json; namespace MagicQuant.Services; public sealed class AnomalyAdjustedPredictionService { + private const double UpdateEpsilon = 1e-15d; + private readonly RemainingCombinationStore _store; public AnomalyAdjustedPredictionService(RemainingCombinationStore store) @@ -41,78 +44,41 @@ await ExecuteAsync(c, $@" foreach (var rule in rules.OrderByDescending(x => x.Confidence).ThenBy(x => x.Id)) { - string where = BuildRuleWhere(rule); - if (string.IsNullOrWhiteSpace(where)) + if (IsDirection(rule, AnomalyRuleDirection.SuppressionOnly)) + { + var log = LogSuppressionOnly(rule); + matchLogs.Add(log); + AnsiConsole.MarkupLine( + $"[grey]Anomaly rule suppression-only:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] no prediction score mutation."); continue; + } - double adjustment = rule.AppliedPredictionSpaceAdjustmentKld; - if (Math.Abs(adjustment) <= 0d) - continue; + if (IsDirection(rule, AnomalyRuleDirection.Beneficial)) + { + AnsiConsole.MarkupLine("[grey]Broad beneficial same-selected-group adjustment disabled; applying pairwise twin ordering only.[/]"); + var result = await ApplyBeneficialPairwiseOrderingRuleAsync(c, rule, ct); + if (result.HasValue) + { + var pairwise = result.Value; + totalMatched += pairwise.MatchedCandidateRows; + matchLogs.Add(pairwise.LogObject); + } - long before = await CountMatchesAsync(c, where, ct); - if (before == 0) continue; + } - var beforeStats = await LoadPredictionStatsAsync(c, where, ct); - - string expression = adjustment < 0d - ? $"GREATEST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), -LEAST({SqlDouble(Math.Min(Config.AnomalyDetection.MaxNegativeAdjustmentKld, Config.SynergyDetection.MaxNegativeAdjustmentKld))}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Math.Min(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld, Config.SynergyDetection.MaxNegativeAdjustmentFractionOfBaseKld))}))" - : $"LEAST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))"; - - await ExecuteAsync(c, $@" -UPDATE {CombinationDuckDbSchema.TableName} -SET AnomalyAdjustmentKld = {expression}, - FinalPredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}), - PredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}) -WHERE {where};", ct); - - var afterStats = await LoadPredictionStatsAsync(c, where, ct); - - totalMatched += before; - var actual = ExtractActualEffect(rule); - var log = new + if (IsDirection(rule, AnomalyRuleDirection.Harmful)) { - ruleId = rule.Id, - direction = rule.RuleDirection, - ruleType = rule.RuleType, - referenceQuant = SafeName(rule.ReferenceQuantId), - groupSetHash = rule.GroupSetHash, - basePredictedKld = beforeStats.AverageBasePredictedKld, - adjustment, - adjustedPredictedKld = afterStats.AverageFinalPredictedKld, - actualCandidateKld = actual.CandidateKld, - actualTwinKld = actual.TwinKld, - actualGainOrHarm = actual.GainOrHarm, - adjustmentReason = actual.HasActualEffect ? "measured-actual-counterfactual-effect" : "prediction-space-gap-fallback", - exactContextMatches = 0, - sameSelectedGroupMatches = before, - equivalentQuantFamilyMatches = 0, - compositionMatches = rule.RuleType.Contains("Composition", StringComparison.OrdinalIgnoreCase) ? before : 0, - contaminationSuppressedMatches = rule.RuleType.Contains("Contaminating", StringComparison.OrdinalIgnoreCase) ? before : 0, - harmfulMatches = rule.RuleDirection.Contains("Harmful", StringComparison.OrdinalIgnoreCase) ? before : 0, - totalAdjustedRows = before, - matchedRows = before, - confidence = rule.Confidence, - groups = rule.GroupStates - .OrderBy(x => x.SortOrder) - .Select(x => new - { - x.TensorGroupId, - candidate = SafeName(x.CandidateQuantId), - reference = SafeName(x.ReferenceQuantId), - x.Movement - }) - .ToList() - }; - matchLogs.Add(log); + var result = await ApplyBroadHarmfulDemotionRuleAsync(c, rule, ct); + if (result.HasValue) + { + var harmful = result.Value; + totalMatched += harmful.MatchedRows; + matchLogs.Add(harmful.LogObject); + } - AnsiConsole.MarkupLine( - $"[green]Applying anomaly rule:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] direction=[cyan]{Markup.Escape(rule.RuleDirection)}[/] " + - $"basePredictedKld=[cyan]{beforeStats.AverageBasePredictedKld:0.000000}[/] adjustment=[cyan]{adjustment:0.000000}[/] " + - $"adjustedPredictedKld=[cyan]{afterStats.AverageFinalPredictedKld:0.000000}[/] " + - $"actualCandidateKld=[cyan]{FmtNullable(actual.CandidateKld)}[/] actualTwinKld=[cyan]{FmtNullable(actual.TwinKld)}[/] " + - $"actualGainOrHarm=[cyan]{FmtNullable(actual.GainOrHarm)}[/] reason=[cyan]{Markup.Escape(actual.HasActualEffect ? "measured-actual-counterfactual-effect" : "prediction-space-gap-fallback")}[/] " + - $"exactContextMatches=[cyan]0[/] sameSelectedGroupMatches=[cyan]{before:N0}[/] totalAdjustedRows=[cyan]{before:N0}[/]"); + continue; + } } await ReRankAsync(c, ct); @@ -126,8 +92,188 @@ await ExecuteAsync(c, $@" }; } + private static async Task ApplyBeneficialPairwiseOrderingRuleAsync( + DuckDBConnection c, + AnomalyInteractionRule rule, + CancellationToken ct) + { + string candidateWhere = BuildRuleCandidateWhere(rule, "c"); + if (string.IsNullOrWhiteSpace(candidateWhere)) + return null; + + await ExecuteAsync(c, "DROP TABLE IF EXISTS temp_anomaly_rule_candidates;", ct); + await ExecuteAsync(c, "DROP TABLE IF EXISTS temp_anomaly_rule_twins;", ct); + await ExecuteAsync(c, "DROP TABLE IF EXISTS temp_anomaly_rule_updates;", ct); + + await ExecuteAsync(c, $@" +CREATE TEMP TABLE temp_anomaly_rule_candidates AS +SELECT {CombinationDuckDbSchema.QualifySlotColumnList("c")}, + COALESCE(c.BaseRankSafeKld, c.PredictedKld) AS CandidateBaseRankSafeKld, + COALESCE(c.FinalPredictedKld, c.PredictedKld, c.BaseRankSafeKld) AS CandidateCurrentFinalKld +FROM {CombinationDuckDbSchema.TableName} c +WHERE {candidateWhere};", ct); + + long matchedCandidateRows = await CountTempRowsAsync(c, "temp_anomaly_rule_candidates", ct); + if (matchedCandidateRows == 0) + return null; + + string twinJoin = BuildPairwiseTwinJoinPredicate(rule, "c", "t"); + if (string.IsNullOrWhiteSpace(twinJoin)) + return null; + + await ExecuteAsync(c, $@" +CREATE TEMP TABLE temp_anomaly_rule_twins AS +SELECT {CombinationDuckDbSchema.QualifySlotColumnList("c")}, + MIN(COALESCE(t.FinalPredictedKld, t.PredictedKld, t.BaseRankSafeKld)) AS TwinEffectiveKld +FROM temp_anomaly_rule_candidates c +JOIN {CombinationDuckDbSchema.TableName} t + ON {twinJoin} +WHERE COALESCE(t.FinalPredictedKld, t.PredictedKld, t.BaseRankSafeKld) IS NOT NULL +GROUP BY {CombinationDuckDbSchema.QualifySlotColumnList("c")};", ct); + + long twinRowsFound = await CountTempRowsAsync(c, "temp_anomaly_rule_twins", ct); + long missingTwinRows = Math.Max(0, matchedCandidateRows - twinRowsFound); + + double margin = Math.Max(0d, Config.AnomalyDetection.PredictionSpaceViolationMargin); + await ExecuteAsync(c, $@" +CREATE TEMP TABLE temp_anomaly_rule_updates AS +SELECT {CombinationDuckDbSchema.QualifySlotColumnList("c")}, + c.CandidateBaseRankSafeKld, + c.CandidateCurrentFinalKld, + tw.TwinEffectiveKld, + GREATEST(0.0, LEAST(c.CandidateCurrentFinalKld, tw.TwinEffectiveKld - {SqlDouble(margin)})) AS NewFinalKld, + c.CandidateCurrentFinalKld - GREATEST(0.0, LEAST(c.CandidateCurrentFinalKld, tw.TwinEffectiveKld - {SqlDouble(margin)})) AS OrderingAdjustmentApplied +FROM temp_anomaly_rule_candidates c +JOIN temp_anomaly_rule_twins tw + ON {CombinationDuckDbSchema.BuildSlotEqualityPredicate("c", "tw")} +WHERE GREATEST(0.0, LEAST(c.CandidateCurrentFinalKld, tw.TwinEffectiveKld - {SqlDouble(margin)})) < c.CandidateCurrentFinalKld - {SqlDouble(UpdateEpsilon)};", ct); + + var stats = await LoadPairwiseUpdateStatsAsync(c, ct); + + await ExecuteAsync(c, $@" +UPDATE {CombinationDuckDbSchema.TableName} t +SET FinalPredictedKld = u.NewFinalKld, + PredictedKld = u.NewFinalKld, + AnomalyAdjustmentKld = u.NewFinalKld - COALESCE(t.BaseRankSafeKld, t.PredictedKld, 0.0) +FROM temp_anomaly_rule_updates u +WHERE {CombinationDuckDbSchema.BuildSlotEqualityPredicate("t", "u")};", ct); + + double advisoryCap = Math.Max(0d, Config.AnomalyDetection.MaxConfirmedPairwiseOrderingAdjustmentKld); + bool exceededAdvisoryCap = advisoryCap > 0d && stats.MaxOrderingAdjustmentApplied > advisoryCap; + var actual = ExtractActualEffect(rule); + + var log = new + { + ruleId = rule.Id, + direction = rule.RuleDirection, + ruleType = rule.RuleType, + applicationMode = "pairwise-twin-ordering", + broadBeneficialSameSelectedGroupAdjustment = "disabled", + referenceQuant = SafeName(rule.ReferenceQuantId), + groupSetHash = rule.GroupSetHash, + matchedCandidateRows, + twinRowsFound, + missingTwinRows, + rowsReordered = stats.RowsReordered, + maxOrderingAdjustmentApplied = stats.MaxOrderingAdjustmentApplied, + meanOrderingAdjustmentApplied = stats.MeanOrderingAdjustmentApplied, + minCandidateBefore = stats.MinCandidateBefore, + meanTwinEffectiveKld = stats.MeanTwinEffectiveKld, + meanCandidateAfter = stats.MeanCandidateAfter, + predictionSpaceViolationMargin = margin, + advisoryMaxConfirmedPairwiseOrderingAdjustmentKld = advisoryCap, + exceededAdvisoryCap, + actualCandidateKld = actual.CandidateKld, + actualTwinKld = actual.TwinKld, + actualGainOrHarm = actual.GainOrHarm, + confidence = rule.Confidence, + groups = BuildGroupLog(rule) + }; + + AnsiConsole.MarkupLine( + $"[green]Applying beneficial anomaly rule as pairwise ordering:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] " + + $"matchedCandidateRows=[cyan]{matchedCandidateRows:N0}[/] twinRowsFound=[cyan]{twinRowsFound:N0}[/] missingTwinRows=[cyan]{missingTwinRows:N0}[/] " + + $"rowsReordered=[cyan]{stats.RowsReordered:N0}[/] maxOrderingAdjustmentApplied=[cyan]{stats.MaxOrderingAdjustmentApplied:0.000000}[/] " + + $"meanTwinEffectiveKld=[cyan]{stats.MeanTwinEffectiveKld:0.000000}[/] meanCandidateAfter=[cyan]{stats.MeanCandidateAfter:0.000000}[/]"); + + if (missingTwinRows > 0) + { + AnsiConsole.MarkupLine( + $"[yellow]Beneficial anomaly twin lookup miss:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] missingTwinRows=[cyan]{missingTwinRows:N0}[/]. No broad fallback boost was applied."); + } + + if (exceededAdvisoryCap) + { + AnsiConsole.MarkupLine( + $"[yellow]Pairwise ordering adjustment exceeded advisory cap:[/] maxApplied=[cyan]{stats.MaxOrderingAdjustmentApplied:0.000000}[/], advisoryCap=[cyan]{advisoryCap:0.000000}[/]. Confirmed pairwise ordering was preserved anyway."); + } + + return new BeneficialPairwiseResult(matchedCandidateRows, twinRowsFound, missingTwinRows, stats.RowsReordered, log); + } + + private static async Task ApplyBroadHarmfulDemotionRuleAsync( + DuckDBConnection c, + AnomalyInteractionRule rule, + CancellationToken ct) + { + string where = BuildRuleCandidateWhere(rule, null); + if (string.IsNullOrWhiteSpace(where)) + return null; + + double adjustment = rule.AppliedPredictionSpaceAdjustmentKld; + if (adjustment <= 0d) + adjustment = Math.Max(Config.AnomalyDetection.PredictionSpaceViolationMargin, Math.Abs(adjustment)); + + if (adjustment <= 0d) + return null; + + long before = await CountMatchesAsync(c, where, ct); + if (before == 0) + return null; + + var beforeStats = await LoadPredictionStatsAsync(c, where, ct); + + string expression = $"LEAST(COALESCE(AnomalyAdjustmentKld, 0.0) + ({SqlDouble(adjustment)}), LEAST({SqlDouble(Config.AnomalyDetection.MaxPositiveAdjustmentKld)}, COALESCE(BaseRankSafeKld, 0.0) * {SqlDouble(Config.AnomalyDetection.MaxAdjustmentFractionOfBaseKld)}))"; + + await ExecuteAsync(c, $@" +UPDATE {CombinationDuckDbSchema.TableName} +SET AnomalyAdjustmentKld = {expression}, + FinalPredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}), + PredictedKld = GREATEST(0.0, COALESCE(BaseRankSafeKld, PredictedKld, 0.0) + {expression}) +WHERE {where};", ct); + + var afterStats = await LoadPredictionStatsAsync(c, where, ct); + var actual = ExtractActualEffect(rule); + var log = new + { + ruleId = rule.Id, + direction = rule.RuleDirection, + ruleType = rule.RuleType, + applicationMode = "broad-harmful-demotion", + referenceQuant = SafeName(rule.ReferenceQuantId), + groupSetHash = rule.GroupSetHash, + basePredictedKld = beforeStats.AverageBasePredictedKld, + adjustment, + adjustedPredictedKld = afterStats.AverageFinalPredictedKld, + actualCandidateKld = actual.CandidateKld, + actualTwinKld = actual.TwinKld, + actualGainOrHarm = actual.GainOrHarm, + broadHarmfulDemotionMatches = before, + totalAdjustedRows = before, + matchedRows = before, + confidence = rule.Confidence, + groups = BuildGroupLog(rule) + }; - private static string BuildRuleWhere(AnomalyInteractionRule rule) + AnsiConsole.MarkupLine( + $"[yellow]Applying broad harmful demotion:[/] rule=[cyan]{Markup.Escape(DescribeRule(rule))}[/] " + + $"matchedRows=[cyan]{before:N0}[/] adjustment=[cyan]{adjustment:0.000000}[/] " + + $"beforeAvg=[cyan]{beforeStats.AverageFinalPredictedKld:0.000000}[/] afterAvg=[cyan]{afterStats.AverageFinalPredictedKld:0.000000}[/]"); + + return new BroadRuleResult(before, log); + } + + private static string BuildRuleCandidateWhere(AnomalyInteractionRule rule, string? alias) { if (rule.GroupStates.Count == 0) return string.Empty; @@ -138,35 +284,68 @@ private static string BuildRuleWhere(AnomalyInteractionRule rule) return string.Empty; } + string q(string column) => string.IsNullOrWhiteSpace(alias) ? column : $"{alias}.{column}"; + var predicates = new List { - CombinationDuckDbSchema.ActiveCandidatePredicateSql, - "BaseRankSafeKld IS NOT NULL", - $"BaseQuant = {rule.ReferenceQuantId}" + $"COALESCE({q("IsProtectedAnchor")}, FALSE) = FALSE", + $"{q("BaseRankSafeKld")} IS NOT NULL", + $"{q("BaseQuant")} = {rule.ReferenceQuantId}" }; - // Transferable counterfactual synergy template matching: - // The exact Q8/Q6 dome remains strongest evidence, but application should not - // require all surrounding groups to equal the discovery context. Match rows that - // contain the selected group states, then compare them conceptually to a virtual - // same-context twin where only those selected groups are raised back to ReferenceQuantId. - // Surrounding groups are preserved by the virtual twin and therefore intentionally - // not constrained here. Explicit probe/rule persistence remains strict elsewhere. foreach (var state in rule.GroupStates.OrderBy(x => x.SortOrder)) { string? column = ColumnNameForGroupId(state.TensorGroupId); if (column == null) return string.Empty; - predicates.Add($"(CASE WHEN {column} = 0 THEN BaseQuant ELSE CAST({column} AS INTEGER) - 1 END) = {state.CandidateQuantId}"); + predicates.Add($"{EffectiveQuantSql(alias, column)} = {state.CandidateQuantId}"); } - if (string.Equals(rule.RuleDirection, AnomalyRuleDirection.SuppressionOnly.ToString(), StringComparison.OrdinalIgnoreCase)) + return string.Join(" AND ", predicates); + } + + private static string BuildPairwiseTwinJoinPredicate(AnomalyInteractionRule rule, string candidateAlias, string twinAlias) + { + if (rule.GroupStates.Count == 0) return string.Empty; + var byColumn = new Dictionary(StringComparer.Ordinal); + foreach (var state in rule.GroupStates.OrderBy(x => x.SortOrder)) + { + string? column = ColumnNameForGroupId(state.TensorGroupId); + if (column == null) + return string.Empty; + + byColumn[column] = state; + } + + var predicates = new List + { + $"{twinAlias}.BaseQuant = {candidateAlias}.BaseQuant" + }; + + foreach (string column in CombinationDuckDbSchema.SlotColumns.Skip(1)) + { + if (!byColumn.TryGetValue(column, out var state)) + { + predicates.Add($"{twinAlias}.{column} = {candidateAlias}.{column}"); + continue; + } + + byte referenceStoredSlot = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(state.ReferenceQuantId); + predicates.Add($"({twinAlias}.{column} = {referenceStoredSlot} OR ({state.ReferenceQuantId} = {twinAlias}.BaseQuant AND {twinAlias}.{column} = 0))"); + } + return string.Join(" AND ", predicates); } + private static string EffectiveQuantSql(string? alias, string column) + { + string prefix = string.IsNullOrWhiteSpace(alias) ? string.Empty : alias + "."; + return $"(CASE WHEN {prefix}{column} = 0 THEN {prefix}BaseQuant ELSE CAST({prefix}{column} AS INTEGER) - 1 END)"; + } + private static IReadOnlyList ActiveGroups() { TensorGroup[] ordered = @@ -242,6 +421,12 @@ private static async Task CountMatchesAsync(DuckDBConnection c, string whe return ToInt64(await cmd.ExecuteScalarAsync(ct)); } + private static async Task CountTempRowsAsync(DuckDBConnection c, string tableName, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = $"SELECT COUNT(*) FROM {tableName};"; + return ToInt64(await cmd.ExecuteScalarAsync(ct)); + } private static async Task LoadPredictionStatsAsync(DuckDBConnection c, string where, CancellationToken ct) { @@ -259,6 +444,31 @@ SELECT AVG(COALESCE(BaseRankSafeKld, PredictedKld)), return new PredictionMatchStats(ToDouble(r.GetValue(0)), ToDouble(r.GetValue(1))); } + private static async Task LoadPairwiseUpdateStatsAsync(DuckDBConnection c, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = @" +SELECT COUNT(*), + COALESCE(MAX(OrderingAdjustmentApplied), 0.0), + COALESCE(AVG(OrderingAdjustmentApplied), 0.0), + COALESCE(MIN(CandidateCurrentFinalKld), 0.0), + COALESCE(AVG(TwinEffectiveKld), 0.0), + COALESCE(AVG(NewFinalKld), 0.0) +FROM temp_anomaly_rule_updates;"; + + using var r = await cmd.ExecuteReaderAsync(ct); + if (!await r.ReadAsync(ct)) + return new PairwiseUpdateStats(0, 0d, 0d, 0d, 0d, 0d); + + return new PairwiseUpdateStats( + ToInt64(r.GetValue(0)), + ToDouble(r.GetValue(1)), + ToDouble(r.GetValue(2)), + ToDouble(r.GetValue(3)), + ToDouble(r.GetValue(4)), + ToDouble(r.GetValue(5))); + } + private static ActualRuleEffect ExtractActualEffect(AnomalyInteractionRule rule) { if (string.IsNullOrWhiteSpace(rule.MetadataJson)) @@ -286,7 +496,43 @@ private static ActualRuleEffect ExtractActualEffect(AnomalyInteractionRule rule) : null; } - private static string FmtNullable(double? value) => value.HasValue ? value.Value.ToString("0.000000") : "n/a"; + private static object LogSuppressionOnly(AnomalyInteractionRule rule) + { + return new + { + ruleId = rule.Id, + direction = rule.RuleDirection, + ruleType = rule.RuleType, + applicationMode = "suppression-only", + scoreMutation = false, + referenceQuant = SafeName(rule.ReferenceQuantId), + groupSetHash = rule.GroupSetHash, + matchedRows = 0, + totalAdjustedRows = 0, + confidence = rule.Confidence, + groups = BuildGroupLog(rule) + }; + } + + private static IReadOnlyList BuildGroupLog(AnomalyInteractionRule rule) + { + return rule.GroupStates + .OrderBy(x => x.SortOrder) + .Select(x => (object)new + { + x.TensorGroupId, + group = ColumnNameForGroupId(x.TensorGroupId), + candidate = SafeName(x.CandidateQuantId), + reference = SafeName(x.ReferenceQuantId), + x.Movement + }) + .ToList(); + } + + private static bool IsDirection(AnomalyInteractionRule rule, AnomalyRuleDirection direction) => + string.Equals(rule.RuleDirection, direction.ToString(), StringComparison.OrdinalIgnoreCase); + + private static string FmtNullable(double? value) => value.HasValue ? value.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; private static double ToDouble(object? value) { @@ -296,7 +542,7 @@ private static double ToDouble(object? value) if (value is BigInteger big) return (double)big; - return Convert.ToDouble(value); + return Convert.ToDouble(value, CultureInfo.InvariantCulture); } private static long ToInt64(object? value) @@ -307,7 +553,7 @@ private static long ToInt64(object? value) if (value is BigInteger big) return (long)big; - return Convert.ToInt64(value); + return Convert.ToInt64(value, CultureInfo.InvariantCulture); } private static async Task ExecuteAsync(DuckDBConnection c, string sql, CancellationToken ct) @@ -323,18 +569,27 @@ private static async Task ConfigureSessionAsync(DuckDBConnection c, Cancellation await ExecuteAsync(c, $"SET threads = {Math.Max(1, Environment.ProcessorCount)};", ct); } - private static string SqlDouble(double value) => value.ToString(System.Globalization.CultureInfo.InvariantCulture); + private static string SqlDouble(double value) => value.ToString(CultureInfo.InvariantCulture); private static string DescribeRule(AnomalyInteractionRule rule) { return string.Join(" + ", rule.GroupStates .OrderBy(x => x.SortOrder) - .Select(x => $"{ColumnNameForGroupId(x.TensorGroupId)}={SafeName(x.CandidateQuantId)}")) + + .Select(x => $"{ColumnNameForGroupId(x.TensorGroupId)}={SafeName(x.CandidateQuantId)}>{SafeName(x.ReferenceQuantId)}")) + $" in {SafeName(rule.ReferenceQuantId)} context"; } private readonly record struct PredictionMatchStats(double AverageBasePredictedKld, double AverageFinalPredictedKld); private readonly record struct ActualRuleEffect(double? CandidateKld, double? TwinKld, double? GainOrHarm, bool HasActualEffect); + private readonly record struct BeneficialPairwiseResult(long MatchedCandidateRows, long TwinRowsFound, long MissingTwinRows, long RowsReordered, object LogObject); + private readonly record struct BroadRuleResult(long MatchedRows, object LogObject); + private readonly record struct PairwiseUpdateStats( + long RowsReordered, + double MaxOrderingAdjustmentApplied, + double MeanOrderingAdjustmentApplied, + double MinCandidateBefore, + double MeanTwinEffectiveKld, + double MeanCandidateAfter); private static string SafeName(byte quantId) { @@ -347,4 +602,4 @@ private static string SafeName(byte quantId) return $"id:{quantId}"; } } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index 335a586..27fcec6 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -121,7 +121,7 @@ private async Task RunStrictDominanceReplacementAsync( CancellationToken ct) { AnsiConsole.Write(new Rule("[yellow]Prediction Phase 1: Strict Hybrid Dominance[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"[grey]Strict dominance retry policy:[/] max attempts per anchor=[cyan]{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}[/], epsilon=[cyan]{Config.SelectionMinimumKldImprovementEpsilon:0.########}[/]"); + AnsiConsole.MarkupLine($"[grey]Strict dominance retry policy:[/] max attempts per anchor=[cyan]{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}[/], epsilon=[cyan]{Config.SelectionMinimumKldImprovementEpsilon:0.########}[/], validate all anomaly/Q8 top-N after first success=[cyan]{Config.SelectionValidateAllAnomalyStrictCandidatesAfterSuccess}[/]"); var accepted = new List(); @@ -197,8 +197,13 @@ private async Task RunStrictDominanceReplacementAsync( $"Prediction anchor={predictedAnchor.DisplayName}; predictedKld={predictedAnchor.PredictedKld:0.000000}; predictedSizeBytes={predictedAnchor.PredictedSizeBytes:N0}; realKld={anchor.Kld:0.000000}; realSizeBytes={anchor.SizeBytes:N0}." }; bool anomalyStrictMode = IsQ8Anchor(anchor) || candidates.Any(x => Math.Abs(x.Prediction.AnomalyAdjustmentKld) > 1e-12); + bool validateAllAfterSuccess = anomalyStrictMode && Config.SelectionValidateAllAnomalyStrictCandidatesAfterSuccess; if (anomalyStrictMode) - strictNotes.Add("Q8/anomaly strict mode: validate all fetched candidates up to the configured attempt limit before choosing by actual KLD/size truth."); + { + strictNotes.Add(validateAllAfterSuccess + ? "Q8/anomaly strict mode: legacy validate-all-after-success is enabled, so all fetched candidates up to the configured attempt limit may be built before choosing by actual KLD/size truth." + : "Q8/anomaly strict mode: stop after the first candidate validates for this anchor. Set candidate_selection.validate_all_anomaly_strict_candidates_after_success=true to restore legacy top-N validation."); + } var diag = new SelectionPhaseDiagnostic { @@ -224,7 +229,7 @@ private async Task RunStrictDominanceReplacementAsync( }; phaseDiagnostics.Add(diag); - AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, selected={candidates.Count:N0}/{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}, q8/anomaly-mode={anomalyStrictMode}"); + AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, selected={candidates.Count:N0}/{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}, q8/anomaly-mode={anomalyStrictMode}, validate-all-after-success={validateAllAfterSuccess}"); if (candidates.Count == 0) continue; @@ -244,7 +249,7 @@ private async Task RunStrictDominanceReplacementAsync( if (validation.Accepted && validation.Snapshot != null) { acceptedForAnchor.Add(validation); - if (!anomalyStrictMode) + if (!validateAllAfterSuccess) break; continue; diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index d1db411..eb9b86f 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -222,6 +222,9 @@ prediction: minimum_fit_rows: 12 candidate_selection: + + validate_all_anomaly_strict_candidates_after_success: false + # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits # inside this size premium and beats the real linear KLD improvement line. near_baseline_max_size_growth_percent: 1.0 diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 9ffea81..c46f18e 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -144,6 +144,9 @@ prediction: minimum_fit_rows: 12 candidate_selection: + + validate_all_anomaly_strict_candidates_after_success: false + # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits # inside this size premium and beats the real linear KLD improvement line. near_baseline_max_size_growth_percent: 1.0 From 3c3f99529d071fbb852dd35023c56277357d253c Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 5 May 2026 15:05:26 -0400 Subject: [PATCH 196/258] fixed issue with predicitive engine falling back incorrectly for baselines or causing collapse. Still having gene seed mismatches for tried 4 bit or less models. --- .../DuckDbPredictionMaterializationService.cs | 89 ++++- .../PredictionGuidedHybridSelectionService.cs | 22 ++ MagicQuant/Services/QuantDatabaseService.cs | 100 ++++- .../Services/RankSafeKldPredictionService.cs | 350 +++++++++++++----- 4 files changed, 442 insertions(+), 119 deletions(-) diff --git a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs index 2be059a..0b92745 100644 --- a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs +++ b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs @@ -99,12 +99,14 @@ CREATE TEMP TABLE temp_effective_group_prediction ( GroupId UTINYINT, StoredSlot UTINYINT, EffectiveBaselineId UTINYINT, - NormalizedBaselineId UTINYINT, + ResolvedBaselineId UTINYINT, KldContribution DOUBLE, PplContribution DOUBLE, BitRange DOUBLE, IsZeroDamage BOOLEAN, - IsKldPredictable BOOLEAN + IsKldPredictable BOOLEAN, + IsolationSource VARCHAR, + IsSurrogateFallback BOOLEAN ); CREATE TEMP TABLE temp_base_predicted_size ( @@ -152,11 +154,14 @@ IsSizePredictable BOOLEAN foreach (var baseline in activeBaselines) { - byte normalizedBase = RankSafeKldPredictionService.NormalizeBaselineIdForIsolation(baseline.UniqueId); - bool hasBaseSize = model.BaseOnlySnapshotsByBaselineId.TryGetValue(baseline.UniqueId, out var baseOnly) || - model.BaseOnlySnapshotsByBaselineId.TryGetValue(normalizedBase, out baseOnly); + bool hasBaseSize = RankSafeKldPredictionService.TryResolveBaseOnlySnapshotForPrediction( + baseline.UniqueId, + model, + notes: null, + out var baseOnly); + await ExecuteAsync(c, - $"INSERT INTO temp_base_predicted_size VALUES ({baseline.UniqueId}, {SqlULong(hasBaseSize ? baseOnly!.SizeBytes : 0UL)}, {SqlBool(hasBaseSize)});", + $"INSERT INTO temp_base_predicted_size VALUES ({baseline.UniqueId}, {SqlULong(hasBaseSize ? baseOnly.SizeBytes : 0UL)}, {SqlBool(hasBaseSize)});", ct); foreach (var slot in activeGroups) @@ -165,31 +170,46 @@ await ExecuteAsync(c, foreach (byte storedSlot in storedSlotsForGroup) { var effectiveBaselineId = GetEffectiveBaselineId(baseline.UniqueId, storedSlot); - var normalizedBaselineId = RankSafeKldPredictionService.NormalizeBaselineIdForIsolation(effectiveBaselineId); - bool zeroDamage = IsZeroDamageAlias(effectiveBaselineId) || IsZeroDamageAlias(normalizedBaselineId); + bool zeroDamage = IsZeroDamageAlias(effectiveBaselineId); + byte resolvedBaselineId = effectiveBaselineId; + string isolationSource = zeroDamage ? "zero" : "missing"; + bool isSurrogateFallback = false; + BenchmarkSnapshotRecord? resolvedIsolation = null; double kldContribution = 0d; double pplContribution = 0d; - double bitRange = zeroDamage ? 99d : GetBitRange(normalizedBaselineId); bool kldPredictable = true; if (!zeroDamage) { - if (model.IsolationByGroupAndBaseline.TryGetValue((slot.Group.UniqueId, normalizedBaselineId), out var isolation)) + if (RankSafeKldPredictionService.TryResolveIsolationBaselineForPrediction( + slot.Group, + effectiveBaselineId, + model, + notes: null, + out var resolved)) { - kldContribution = Math.Max(0d, isolation.Kld); - pplContribution = isolation.Ppl; + resolvedBaselineId = resolved.BaselineId; + resolvedIsolation = resolved.Snapshot; + isSurrogateFallback = resolved.IsSurrogate; + isolationSource = resolved.IsSurrogate + ? $"surrogate:{FormatBaselineId(resolved.FallbackBaselineId ?? resolved.BaselineId)}" + : "exact"; + kldContribution = Math.Max(0d, resolved.Snapshot.Kld); + pplContribution = resolved.Snapshot.Ppl; } else { kldPredictable = false; - if (normalizedBaselineId == BaselineQuants.Q8_0.UniqueId && warnedMissingQ8IsolationGroups.Add(slot.Group.UniqueId)) + if (effectiveBaselineId == BaselineQuants.Q8_0.UniqueId && warnedMissingQ8IsolationGroups.Add(slot.Group.UniqueId)) { AnsiConsole.MarkupLine($"[yellow]Missing KLD isolation snapshot for group '{Markup.Escape(slot.Group.Name)}' and baseline Q8_0 while building DuckDB prediction lookup. Q8_0 is quantized damage, not native truth; matching rows will stay unpredicted instead of receiving zero KLD.[/]"); } } } + double bitRange = zeroDamage ? 99d : GetBitRange(resolvedBaselineId); + await ExecuteAsync(c, $@" INSERT INTO temp_effective_group_prediction VALUES ( {baseline.UniqueId}, @@ -197,12 +217,14 @@ INSERT INTO temp_effective_group_prediction VALUES ( {slot.Group.UniqueId}, {storedSlot}, {effectiveBaselineId}, - {normalizedBaselineId}, + {resolvedBaselineId}, {SqlDouble(kldContribution)}, {SqlDouble(pplContribution)}, {SqlDouble(bitRange)}, {SqlBool(zeroDamage)}, - {SqlBool(kldPredictable)} + {SqlBool(kldPredictable)}, + {SqlString(isolationSource)}, + {SqlBool(isSurrogateFallback)} );", ct); long deltaBytes = 0L; @@ -210,12 +232,14 @@ INSERT INTO temp_effective_group_prediction VALUES ( // This mirrors RankSafeKldPredictionService.PredictSize: // base-only anchor starts with native-exact groups, then every active - // effective group contributes its measured isolation size delta. - if (!BaselineQuants.IsNativeExactAlias(normalizedBaselineId)) + // effective group contributes its measured exact isolation size delta. + // External/custom surrogate fallbacks are intentionally disabled by + // TryResolveIsolationBaselineForPrediction and will throw before this point. + if (!BaselineQuants.IsNativeExactAlias(effectiveBaselineId)) { - if (model.IsolationByGroupAndBaseline.TryGetValue((slot.Group.UniqueId, normalizedBaselineId), out var targetIsolation)) + if (resolvedIsolation != null) { - deltaBytes = (long)targetIsolation.SizeBytes - (long)model.Q8BaseOnly.SizeBytes; + deltaBytes = (long)resolvedIsolation.SizeBytes - (long)model.Q8BaseOnly.SizeBytes; } else { @@ -577,9 +601,28 @@ SELECT COUNT(*) AnsiConsole.MarkupLine($"[grey]DuckDB prediction lookup rows:[/] total=[cyan]{totalRows:N0}[/] base-lookups=[cyan]{baseLookupRows:N0}[/] missing-base-join=[cyan]{missingBaseJoin:N0}[/]"); await PrintBaseLookupRowsAsync(c, ct); + await PrintIsolationSourceDiagnosticsAsync(c, ct); await PrintMissingGroupLookupRowsAsync(c, model, ct); } + private static async Task PrintIsolationSourceDiagnosticsAsync(DuckDBConnection c, CancellationToken ct) + { + using var cmd = c.CreateCommand(); + cmd.CommandText = @" +SELECT IsolationSource, COUNT(*) AS Rows +FROM temp_effective_group_prediction +GROUP BY IsolationSource +ORDER BY Rows DESC, IsolationSource;"; + + using var reader = await cmd.ExecuteReaderAsync(ct); + while (await reader.ReadAsync(ct)) + { + string source = reader.GetValue(0)?.ToString() ?? "unknown"; + long rows = ToInt64(reader.GetValue(1)); + AnsiConsole.MarkupLine($"[grey] - isolation source {Markup.Escape(source)}:[/] rows={rows:N0}"); + } + } + private static async Task PrintBaseLookupRowsAsync(DuckDBConnection c, CancellationToken ct) { using var cmd = c.CreateCommand(); @@ -897,6 +940,14 @@ private static string SqlDouble(double value) return value.ToString("R", CultureInfo.InvariantCulture); } + private static string SqlString(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return "NULL"; + + return $"'{value.Replace("'", "''")}'"; + } + private static string SqlULong(ulong value) => value.ToString(CultureInfo.InvariantCulture); private static string SqlBool(bool value) => value ? "TRUE" : "FALSE"; diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index 27fcec6..234301b 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -1342,6 +1342,7 @@ private static void PrintPredictionAnchorFrontier( table.AddColumn("Pred Size GiB"); table.AddColumn("Rank"); table.AddColumn("Conf"); + table.AddColumn("Isolation Source"); table.AddColumn("Matching Real Anchor"); table.AddColumn("Real KLD/Size GiB"); @@ -1356,6 +1357,7 @@ private static void PrintPredictionAnchorFrontier( ToGiB(anchor.PredictedSizeBytes).ToString("0.00"), anchor.PredictionRank.ToString("N0"), anchor.PredictionConfidence.ToString("0.###"), + Markup.Escape(DescribeVirtualAnchorIsolationSource(anchor)), real == null ? "[grey]none[/]" : Markup.Escape(real.DisplayName), real == null ? "[grey]n/a[/]" : $"{real.Kld:0.000000} / {ToGiB(real.SizeBytes):0.00}"); } @@ -1373,6 +1375,25 @@ private static void PrintPredictionAnchorFrontier( } } + private static string DescribeVirtualAnchorIsolationSource(PredictedAnchorRow anchor) + { + try + { + var baseline = BaselineQuants.FromId(anchor.RuntimeBaselineId); + if (baseline.IsExternalRepositoryBaseline) + return "exact external; fallback disabled"; + + if (BaselineQuants.IsNativeExactAlias(baseline.UniqueId)) + return "native exact"; + + return "standard exact"; + } + catch + { + return "unknown"; + } + } + private static BenchmarkSnapshotRecord? FindMatchingRealAnchor( PredictedAnchorRow predictedAnchor, IReadOnlyList realAnchors) @@ -1496,6 +1517,7 @@ private static object ToAnchorLog(BenchmarkSnapshotRecord anchor) predictedKld = anchor.PredictedKld, predictionRank = anchor.PredictionRank, predictionConfidence = anchor.PredictionConfidence, + isolationSource = DescribeVirtualAnchorIsolationSource(anchor), isVirtualPredictionAnchor = anchor.IsVirtualPredictionAnchor }; } diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index d3cbe49..b7ccb71 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -914,42 +914,108 @@ public ulong Predict(TensorConfig config) public ulong GetBaseSizeForSql(byte baseQuant) { - byte normalizedBaseId = NormalizeBaselineIdForIsolation(baseQuant); - return _pureBaselineSizes.TryGetValue(baseQuant, out var directBase) - ? directBase - : _pureBaselineSizes.TryGetValue(normalizedBaseId, out var normalizedBase) - ? normalizedBase - : PureQ8BaseSize; + if (_pureBaselineSizes.TryGetValue(baseQuant, out var directBase)) + return directBase; + + if (TryGetDisabledSurrogateBaselineId(baseQuant, out var disabledSurrogateId) && + _pureBaselineSizes.ContainsKey(disabledSurrogateId)) + { + /* + * Deprecated surrogate fallback, intentionally disabled: + * + * return _pureBaselineSizes[disabledSurrogateId]; + * + * This made external/custom carriers inherit standard-family base size in the + * SQL pre-pruning path. The RankSafe materializer now requires exact external + * base-only truth, and this older helper should fail the same way. + */ + throw new InvalidOperationException( + $"Missing exact pure/base size for external baseline {FormatBaselineForSql(baseQuant)} (id '{baseQuant}'), " + + $"but disabled surrogate {FormatBaselineForSql(disabledSurrogateId)} (id '{disabledSurrogateId}') exists. " + + "SQL size prediction fallback is disabled to prevent external/custom collapse."); + } + + throw new InvalidOperationException( + $"Missing pure/base size for baseline {FormatBaselineForSql(baseQuant)} (id '{baseQuant}'). " + + "SQL size prediction no longer falls back to Q8_0 because missing size truth should stop the run."); } public long GetRelativeSizeDeltaForSql(byte groupId, byte baseQuant, byte storedSlot) { if (storedSlot == 0) return 0; + byte decoded = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedSlot); - if (decoded == BaselineQuants.BF16_Hybrid.UniqueId || decoded == BaselineQuants.F16_Hybrid.UniqueId) - return 0; - byte normalizedBase = NormalizeBaselineIdForIsolation(baseQuant); - byte normalizedCandidateId = NormalizeBaselineIdForIsolation(decoded); - if (normalizedCandidateId == normalizedBase) + if (BaselineQuants.IsNativeExactAlias(decoded)) return 0; - if (!_sizesByGroupAndCandidate.TryGetValue((groupId, normalizedCandidateId), out var candidateSize)) - return 0; - if (!_sizesByGroupAndCandidate.TryGetValue((groupId, normalizedBase), out var baseSize)) + + if (decoded == baseQuant) return 0; + + ulong candidateSize = GetExactGroupSizeOrThrow(groupId, decoded, "candidate"); + ulong baseSize = GetExactGroupSizeOrThrow(groupId, baseQuant, "base"); return (long)candidateSize - (long)baseSize; } - private static byte NormalizeBaselineIdForIsolation(byte baselineId) + private ulong GetExactGroupSizeOrThrow(byte groupId, byte baselineId, string role) { + if (_sizesByGroupAndCandidate.TryGetValue((groupId, baselineId), out var exactSize)) + return exactSize; + + if (TryGetDisabledSurrogateBaselineId(baselineId, out var disabledSurrogateId) && + _sizesByGroupAndCandidate.ContainsKey((groupId, disabledSurrogateId))) + { + /* + * Deprecated surrogate fallback, intentionally disabled: + * + * return _sizesByGroupAndCandidate[(groupId, disabledSurrogateId)]; + * + * Group-size deltas must be based on the exact runtime baseline id. Re-enabling + * this would collapse external/custom group assignments into their standard + * family before DuckDB ranking ever sees them. + */ + throw new InvalidOperationException( + $"Missing exact group-size isolation for {role} baseline {FormatBaselineForSql(baselineId)} (id '{baselineId}') " + + $"in tensor group id '{groupId}', but disabled surrogate {FormatBaselineForSql(disabledSurrogateId)} (id '{disabledSurrogateId}') exists. " + + "Regenerate the exact isolated sample instead of using SQL size fallback."); + } + + throw new InvalidOperationException( + $"Missing group-size isolation for {role} baseline {FormatBaselineForSql(baselineId)} (id '{baselineId}') in tensor group id '{groupId}'. " + + "SQL size prediction no longer returns zero for missing isolation truth."); + } + + private static bool TryGetDisabledSurrogateBaselineId(byte baselineId, out byte surrogateBaselineId) + { + surrogateBaselineId = baselineId; + + if (BaselineQuants.IsNativeExactAlias(baselineId)) + return false; + var baseline = BaselineQuants.FromId(baselineId); if (!baseline.IsExternalRepositoryBaseline) - return baselineId; + return false; var builtIn = BaselineQuants.ResolveBuiltInStandardBaseline(baseline.QuantizeBaseArgumentName) ?? BaselineQuants.ResolveBuiltInStandardBaseline(baseline.Names[0]); - return builtIn?.UniqueId ?? baselineId; + if (builtIn == null || builtIn.UniqueId == baselineId) + return false; + + surrogateBaselineId = builtIn.UniqueId; + return true; + } + + private static string FormatBaselineForSql(byte baselineId) + { + try + { + return BaselineQuants.FromId(baselineId).Names[0]; + } + catch + { + return $"id {baselineId}"; + } } } } \ No newline at end of file diff --git a/MagicQuant/Services/RankSafeKldPredictionService.cs b/MagicQuant/Services/RankSafeKldPredictionService.cs index 11cec00..5b9e8ff 100644 --- a/MagicQuant/Services/RankSafeKldPredictionService.cs +++ b/MagicQuant/Services/RankSafeKldPredictionService.cs @@ -105,27 +105,19 @@ private async Task PredictSingleAsync( Notes = effective.Warnings.ToList() }; - byte normalizedBaseId = NormalizeBaselineIdForIsolation(config.BaseQuant); - - if (row.IsPureBaseline && context.PureSnapshotsByBaselineId.TryGetValue(config.BaseQuant, out var pureDirect)) + if (row.IsPureBaseline) { - row.PredictedSizeBytes = pureDirect.SizeBytes; - row.AdditiveKld = pureDirect.Kld; - row.InteractionKld = pureDirect.Kld; - row.PredictedKld = pureDirect.Kld; - row.PredictedPpl = pureDirect.Ppl; - return row; - } + if (context.PureSnapshotsByBaselineId.TryGetValue(config.BaseQuant, out var pureDirect)) + { + row.PredictedSizeBytes = pureDirect.SizeBytes; + row.AdditiveKld = pureDirect.Kld; + row.InteractionKld = pureDirect.Kld; + row.PredictedKld = pureDirect.Kld; + row.PredictedPpl = pureDirect.Ppl; + return row; + } - if (row.IsPureBaseline && context.PureSnapshotsByBaselineId.TryGetValue(normalizedBaseId, out var pureNormalized)) - { - row.PredictedSizeBytes = pureNormalized.SizeBytes; - row.AdditiveKld = pureNormalized.Kld; - row.InteractionKld = pureNormalized.Kld; - row.PredictedKld = pureNormalized.Kld; - row.PredictedPpl = pureNormalized.Ppl; - row.Notes.Add($"Pure baseline '{quant.BaseQuant.Names[0]}' was normalized to '{pureNormalized.Quant.BaseQuant.Names[0]}' for prediction."); - return row; + GuardAgainstDisabledPureBaselineSurrogateFallback(config.BaseQuant, context, row.Notes); } row.PredictedSizeBytes = PredictSize(config, context, row.Notes, out bool canPredictSize); @@ -176,8 +168,18 @@ private async Task BuildContextAsync(CancellationToken var q8BaseOnly = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)q8BaseOnlyQuant, ct); if (q8BaseOnly == null) { - notes.Add("Q8 native-exact base-only anchor was missing. Size fallback will use pure Q8; Q8_0 group KLD still requires measured Q8_0 isolation snapshots and will not be treated as zero damage."); - q8BaseOnly = pureQ8; + /* + * Deprecated fallback, intentionally disabled: + * + * notes.Add("Q8 native-exact base-only anchor was missing. Size fallback will use pure Q8..."); + * q8BaseOnly = pureQ8; + * + * Base-only anchors define the additive size coordinate system. Falling back to a pure + * Q8 model hides missing isolation truth and can flatten external/custom size geometry. + */ + throw new InvalidOperationException( + "Rank-safe prediction requires the Q8_0 native-exact base-only anchor. " + + "The old pure-Q8 fallback is intentionally disabled; generate the missing base-only isolation sample instead."); } var baseOnlyByBaselineId = new Dictionary @@ -189,37 +191,28 @@ private async Task BuildContextAsync(CancellationToken .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) .OrderBy(x => x.UniqueId)) { - byte normalizedBaselineId = NormalizeBaselineIdForIsolation(baseline.UniqueId); - - if (!baseOnlyByBaselineId.ContainsKey(baseline.UniqueId)) - { - var directBaseOnlyQuant = HybridQuant.CreateExactBlanket( - baseQuant: baseline, - groups: activeGroups, - exactScheme: nativeExactScheme); - - var directBaseOnlySnapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)directBaseOnlyQuant, ct); - if (directBaseOnlySnapshot != null) - { - baseOnlyByBaselineId[baseline.UniqueId] = directBaseOnlySnapshot; - if (!baseOnlyByBaselineId.ContainsKey(normalizedBaselineId)) - baseOnlyByBaselineId[normalizedBaselineId] = directBaseOnlySnapshot; - continue; - } - } - - if (baseOnlyByBaselineId.ContainsKey(normalizedBaselineId)) + if (baseOnlyByBaselineId.ContainsKey(baseline.UniqueId)) continue; - var normalizedBaseline = BaselineQuants.FromId(normalizedBaselineId); - var baseOnlyQuant = HybridQuant.CreateExactBlanket( - baseQuant: normalizedBaseline, + var directBaseOnlyQuant = HybridQuant.CreateExactBlanket( + baseQuant: baseline, groups: activeGroups, exactScheme: nativeExactScheme); - var baseOnlySnapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)baseOnlyQuant, ct); - if (baseOnlySnapshot != null) - baseOnlyByBaselineId[normalizedBaselineId] = baseOnlySnapshot; + var directBaseOnlySnapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)directBaseOnlyQuant, ct); + if (directBaseOnlySnapshot != null) + { + baseOnlyByBaselineId[baseline.UniqueId] = directBaseOnlySnapshot; + continue; + } + + if (TryGetDisabledSurrogateBaselineId(baseline.UniqueId, out var disabledSurrogateId) && + baseOnlyByBaselineId.ContainsKey(disabledSurrogateId)) + { + notes.Add( + $"Missing exact base-only anchor for external baseline {FormatBaselineForNote(baseline.UniqueId)} (id '{baseline.UniqueId}'). " + + $"A normalized surrogate {FormatBaselineForNote(disabledSurrogateId)} (id '{disabledSurrogateId}') exists, but surrogate base-size fallback is intentionally disabled."); + } } var isolationByGroupAndBaseline = new Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord>(); @@ -230,9 +223,7 @@ private async Task BuildContextAsync(CancellationToken .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) .OrderBy(x => x.UniqueId)) { - var normalizedBaselineId = NormalizeBaselineIdForIsolation(baseline.UniqueId); - - if (isolationByGroupAndBaseline.ContainsKey((group.UniqueId, normalizedBaselineId))) + if (isolationByGroupAndBaseline.ContainsKey((group.UniqueId, baseline.UniqueId))) continue; var isolationQuant = HybridQuant.CreateExactBlanket( @@ -240,11 +231,22 @@ private async Task BuildContextAsync(CancellationToken groups: activeGroups, exactScheme: nativeExactScheme); - isolationQuant.SetLearnedCandidateOverride(group, BaselineQuants.FromId(normalizedBaselineId)); + isolationQuant.SetLearnedCandidateOverride(group, baseline); var snapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); if (snapshot != null) - isolationByGroupAndBaseline[(group.UniqueId, normalizedBaselineId)] = snapshot; + { + isolationByGroupAndBaseline[(group.UniqueId, baseline.UniqueId)] = snapshot; + continue; + } + + if (TryGetDisabledSurrogateBaselineId(baseline.UniqueId, out var disabledSurrogateId) && + isolationByGroupAndBaseline.ContainsKey((group.UniqueId, disabledSurrogateId))) + { + notes.Add( + $"Missing exact isolation snapshot for group '{group.Name}' and external baseline {FormatBaselineForNote(baseline.UniqueId)} (id '{baseline.UniqueId}'). " + + $"A normalized surrogate {FormatBaselineForNote(disabledSurrogateId)} (id '{disabledSurrogateId}') exists, but surrogate isolation fallback is intentionally disabled."); + } } } @@ -265,6 +267,8 @@ private async Task BuildContextAsync(CancellationToken notes.Add($"Q8_0 isolation snapshots loaded for {activeGroups.Count:N0} active tensor groups. Q8_0 will contribute measured prediction-space KLD, not zero/native damage."); } + AppendExternalCoverageDiagnostics(notes, activeGroups, baseOnlyByBaselineId, isolationByGroupAndBaseline); + return new RankSafePredictionModel( activeGroups: activeGroups, pureQ8: pureQ8, @@ -494,18 +498,14 @@ private double PredictAdditiveKld( if (IsZeroDamageAlias(effectiveBaselineId)) continue; - byte normalized = NormalizeBaselineIdForIsolation(effectiveBaselineId); - if (IsZeroDamageAlias(normalized)) - continue; - - if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalized), out var isolation)) + if (!TryResolveIsolationBaselineForPrediction(group, effectiveBaselineId, context, notes, out var resolved)) { - notes.Add(BuildMissingIsolationNote(group, normalized)); + notes.Add(BuildMissingIsolationNote(group, effectiveBaselineId)); canPredict = false; continue; } - total += Math.Max(0d, isolation.Kld); + total += Math.Max(0d, resolved.Snapshot.Kld); } return Math.Max(0d, total); @@ -523,9 +523,10 @@ private double PredictPpl( if (IsZeroDamageAlias(effectiveBaselineId)) continue; - byte normalized = NormalizeBaselineIdForIsolation(effectiveBaselineId); - if (context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalized), out var isolation)) - total += isolation.Ppl; + if (TryResolveIsolationBaselineForPrediction(group, effectiveBaselineId, context, notes, out var resolved)) + total += resolved.Snapshot.Ppl; + else + notes.Add(BuildMissingIsolationNote(group, effectiveBaselineId)); } return total; @@ -538,11 +539,10 @@ private ulong PredictSize( out bool canPredictSize) { canPredictSize = true; - byte normalizedBaseId = NormalizeBaselineIdForIsolation(config.BaseQuant); - if (!context.BaseOnlySnapshotsByBaselineId.TryGetValue(normalizedBaseId, out var baseOnlyAnchor)) + if (!TryResolveBaseOnlySnapshotForPrediction(config.BaseQuant, context, notes, out var baseOnlyAnchor)) { - notes.Add($"Missing base-only size anchor for base baseline id '{normalizedBaseId}'. Size prediction is not safe for selection."); + notes.Add($"Missing base-only size anchor for base baseline {FormatBaselineForNote(config.BaseQuant)} (id '{config.BaseQuant}'). Size prediction is not safe for selection."); canPredictSize = false; return 0; } @@ -552,21 +552,19 @@ private ulong PredictSize( foreach (var (group, effectiveBaselineId) in EnumerateEffectiveBaselines(config, context.ActiveGroups)) { - byte normalizedTargetId = NormalizeBaselineIdForIsolation(effectiveBaselineId); - // Base-only anchors already hold every active group at native exact precision. // Exact aliases therefore contribute no size delta. - if (BaselineQuants.IsNativeExactAlias(normalizedTargetId)) + if (BaselineQuants.IsNativeExactAlias(effectiveBaselineId)) continue; - if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalizedTargetId), out var targetIsolation)) + if (!TryResolveIsolationBaselineForPrediction(group, effectiveBaselineId, context, notes, out var resolved)) { - notes.Add($"Missing group size-isolation snapshot for group '{group.Name}' and effective baseline id '{normalizedTargetId}'. Size prediction is not safe for selection."); + notes.Add($"Missing group size-isolation snapshot for group '{group.Name}' and effective baseline {FormatBaselineForNote(effectiveBaselineId)} (id '{effectiveBaselineId}'). Size prediction is not safe for selection."); canPredictSize = false; continue; } - total += (long)targetIsolation.SizeBytes - q8ExactBlanketSize; + total += (long)resolved.Snapshot.SizeBytes - q8ExactBlanketSize; } if (total <= 0) @@ -588,15 +586,11 @@ private double ComputeCrossTerm(TensorConfig config, RankSafePredictionModel con if (IsZeroDamageAlias(effectiveBaselineId)) continue; - byte normalized = NormalizeBaselineIdForIsolation(effectiveBaselineId); - if (IsZeroDamageAlias(normalized)) - continue; - - if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, normalized), out var isolation)) + if (!TryResolveIsolationBaselineForPrediction(group, effectiveBaselineId, context, notes: null, out var resolved)) continue; - var baseline = BaselineQuants.FromId(normalized); - contributions.Add((Math.Max(0d, isolation.Kld), baseline.BitRange)); + var baseline = BaselineQuants.FromId(resolved.BaselineId); + contributions.Add((Math.Max(0d, resolved.Snapshot.Kld), baseline.BitRange)); } double cross = 0d; @@ -657,6 +651,196 @@ public static byte NormalizeBaselineIdForIsolation(byte baselineId) return builtIn?.UniqueId ?? baselineId; } + internal static bool TryResolveIsolationBaselineForPrediction( + TensorGroup group, + byte effectiveBaselineId, + RankSafePredictionModel context, + List? notes, + out IsolationBaselineResolution resolution) + { + if (context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, effectiveBaselineId), out var exact)) + { + resolution = new IsolationBaselineResolution(effectiveBaselineId, exact, false, null); + return true; + } + + if (TryGetDisabledSurrogateBaselineId(effectiveBaselineId, out var disabledSurrogateId) && + context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, disabledSurrogateId), out var disabledSurrogate)) + { + /* + * Deprecated surrogate fallback, intentionally disabled: + * + * resolution = new IsolationBaselineResolution(disabledSurrogateId, disabledSurrogate, true, disabledSurrogateId); + * notes?.Add($"External baseline {FormatBaselineForNote(effectiveBaselineId)} used surrogate isolation {FormatBaselineForNote(disabledSurrogateId)} for group '{group.Name}'."); + * return true; + * + * This used to collapse external/custom repositories such as Unsloth Dynamic into + * their built-in llama.cpp family before prediction. MagicQuant should already have + * exact isolated samples for every registered external candidate, so using this path + * would hide a truth-coverage bug. Keep the old shape here only as a breadcrumb if a + * future emergency compatibility mode is deliberately reintroduced. + */ + _ = disabledSurrogate; + ThrowExternalIsolationSurrogateFallbackDisabled(group, effectiveBaselineId, disabledSurrogateId); + } + + if (IsExternalRepositoryBaseline(effectiveBaselineId)) + ThrowMissingExactExternalIsolation(group, effectiveBaselineId); + + resolution = default; + return false; + } + + internal static bool TryResolveBaseOnlySnapshotForPrediction( + byte baselineId, + RankSafePredictionModel context, + List? notes, + out BenchmarkSnapshotRecord snapshot) + { + if (context.BaseOnlySnapshotsByBaselineId.TryGetValue(baselineId, out var exactSnapshot)) + { + snapshot = exactSnapshot; + return true; + } + + if (TryGetDisabledSurrogateBaselineId(baselineId, out var disabledSurrogateId) && + context.BaseOnlySnapshotsByBaselineId.ContainsKey(disabledSurrogateId)) + { + /* + * Deprecated surrogate fallback, intentionally disabled: + * + * snapshot = context.BaseOnlySnapshotsByBaselineId[disabledSurrogateId]; + * notes?.Add($"External baseline {FormatBaselineForNote(baselineId)} used surrogate base-only size {FormatBaselineForNote(disabledSurrogateId)}."); + * return true; + * + * Base-only size anchors must preserve the exact runtime baseline id. Falling back + * here makes UD-Q4_K_XL and Q4_K_M look byte-identical before selection even starts. + */ + ThrowExternalBaseOnlySurrogateFallbackDisabled(baselineId, disabledSurrogateId); + } + + if (IsExternalRepositoryBaseline(baselineId)) + throw new InvalidOperationException( + $"Missing exact base-only anchor for external baseline {FormatBaselineForNote(baselineId)} (id '{baselineId}'). " + + "Surrogate base-only fallback is disabled because every external/custom baseline should have exact isolated truth before prediction."); + + snapshot = default!; + return false; + } + + internal static bool TryGetDisabledSurrogateBaselineId(byte baselineId, out byte surrogateBaselineId) + { + surrogateBaselineId = baselineId; + + if (BaselineQuants.IsNativeExactAlias(baselineId)) + return false; + + var baseline = BaselineQuants.FromId(baselineId); + if (!baseline.IsExternalRepositoryBaseline) + return false; + + var normalized = NormalizeBaselineIdForIsolation(baselineId); + if (normalized == baselineId) + return false; + + surrogateBaselineId = normalized; + return true; + } + + internal static bool IsExternalRepositoryBaseline(byte baselineId) + { + if (BaselineQuants.IsNativeExactAlias(baselineId)) + return false; + + return BaselineQuants.FromId(baselineId).IsExternalRepositoryBaseline; + } + + private static void GuardAgainstDisabledPureBaselineSurrogateFallback( + byte baselineId, + RankSafePredictionModel context, + List notes) + { + if (!TryGetDisabledSurrogateBaselineId(baselineId, out var disabledSurrogateId) || + !context.PureSnapshotsByBaselineId.ContainsKey(disabledSurrogateId)) + { + return; + } + + /* + * Deprecated surrogate fallback, intentionally disabled: + * + * var pureSurrogate = context.PureSnapshotsByBaselineId[disabledSurrogateId]; + * notes.Add($"Pure baseline {FormatBaselineForNote(baselineId)} used surrogate pure snapshot {FormatBaselineForNote(disabledSurrogateId)}."); + * + * Pure external baselines must not inherit standard-family prediction identity. + */ + throw new InvalidOperationException( + $"Missing exact pure snapshot for external baseline {FormatBaselineForNote(baselineId)} (id '{baselineId}'), " + + $"but surrogate pure snapshot {FormatBaselineForNote(disabledSurrogateId)} (id '{disabledSurrogateId}') exists. " + + "Surrogate pure-baseline fallback is disabled to prevent external/custom collapse."); + } + + private static void ThrowExternalIsolationSurrogateFallbackDisabled( + TensorGroup group, + byte externalBaselineId, + byte surrogateBaselineId) + { + throw new InvalidOperationException( + $"Missing exact isolation snapshot for group '{group.Name}' and external baseline {FormatBaselineForNote(externalBaselineId)} (id '{externalBaselineId}'). " + + $"Surrogate isolation {FormatBaselineForNote(surrogateBaselineId)} (id '{surrogateBaselineId}') exists, but fallback is disabled. " + + "External/custom baselines must be scored from exact isolated prediction truth; regenerate/relearn the missing isolated sample instead of silently collapsing it."); + } + + private static void ThrowExternalBaseOnlySurrogateFallbackDisabled(byte externalBaselineId, byte surrogateBaselineId) + { + throw new InvalidOperationException( + $"Missing exact base-only anchor for external baseline {FormatBaselineForNote(externalBaselineId)} (id '{externalBaselineId}'). " + + $"Surrogate base-only anchor {FormatBaselineForNote(surrogateBaselineId)} (id '{surrogateBaselineId}') exists, but fallback is disabled. " + + "External/custom baselines must preserve exact runtime identity for size prediction."); + } + + private static void ThrowMissingExactExternalIsolation(TensorGroup group, byte externalBaselineId) + { + throw new InvalidOperationException( + $"Missing exact isolation snapshot for group '{group.Name}' and external baseline {FormatBaselineForNote(externalBaselineId)} (id '{externalBaselineId}'). " + + "No surrogate fallback was used. MagicQuant expects external/custom isolated samples to exist before prediction materialization."); + } + + private static void AppendExternalCoverageDiagnostics( + List notes, + IReadOnlyList activeGroups, + Dictionary baseOnlyByBaselineId, + Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord> isolationByGroupAndBaseline) + { + var externalBaselines = BaselineQuants.GetAllRecognizedBaselines() + .Where(x => x.IsExternalRepositoryBaseline) + .OrderBy(x => x.UniqueId) + .ToList(); + + if (externalBaselines.Count == 0) + return; + + notes.Add("External/custom surrogate fallback is disabled; missing exact external prediction truth will throw instead of collapsing to a standard family."); + + foreach (var baseline in externalBaselines) + { + int exactIsolation = activeGroups.Count(group => isolationByGroupAndBaseline.ContainsKey((group.UniqueId, baseline.UniqueId))); + bool exactBaseOnly = baseOnlyByBaselineId.ContainsKey(baseline.UniqueId); + string fallbackText = TryGetDisabledSurrogateBaselineId(baseline.UniqueId, out var fallbackId) + ? $"; disabled fallback target would have been {FormatBaselineForNote(fallbackId)}:{fallbackId}" + : string.Empty; + + notes.Add( + $"External isolation exact coverage: {baseline.Names[0]}:{baseline.UniqueId} exact={exactIsolation}/{activeGroups.Count} groups; base-only={(exactBaseOnly ? "exact" : "missing")}{fallbackText}."); + } + } + + internal readonly record struct IsolationBaselineResolution( + byte BaselineId, + BenchmarkSnapshotRecord Snapshot, + bool IsSurrogate, + byte? FallbackBaselineId); + private static bool IsZeroDamageAlias(byte baselineId) => IsNativeExactZeroReferenceAlias(baselineId); private static bool IsNativeExactZeroReferenceAlias(byte baselineId) @@ -667,15 +851,15 @@ private static bool IsNativeExactZeroReferenceAlias(byte baselineId) return BaselineQuants.IsNativeExactAlias(baselineId); } - private static string BuildMissingIsolationNote(TensorGroup group, byte normalizedBaselineId) + private static string BuildMissingIsolationNote(TensorGroup group, byte baselineId) { - var baselineName = FormatBaselineForNote(normalizedBaselineId); - if (normalizedBaselineId == BaselineQuants.Q8_0.UniqueId) + var baselineName = FormatBaselineForNote(baselineId); + if (baselineId == BaselineQuants.Q8_0.UniqueId) { return $"Missing KLD isolation snapshot for group '{group.Name}' and baseline Q8_0. Q8_0 is quantized damage, not native truth; this row is marked incomplete instead of silently receiving zero KLD."; } - return $"Missing KLD isolation snapshot for group '{group.Name}' and baseline {baselineName} (id '{normalizedBaselineId}')."; + return $"Missing KLD isolation snapshot for group '{group.Name}' and baseline {baselineName} (id '{baselineId}')."; } private static string FormatBaselineForNote(byte baselineId) @@ -759,4 +943,4 @@ private struct PavaBlock public int Count; public double Mean => Weight <= 0d ? 0d : Sum / Weight; } -} +} \ No newline at end of file From 9782f30bbbd81b3c8781ffae5a6cbeaafe652998 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 5 May 2026 15:54:42 -0400 Subject: [PATCH 197/258] healthier prediction fallback logic but still not good enough. --- MagicQuant/Config.cs | 15 + .../Configuration/MagicQuantYamlConfig.cs | 27 + .../Configuration/MagicQuantYamlLoader.cs | 49 ++ .../Models/PredictionSelectionModels.cs | 6 + .../PredictionGuidedHybridSelectionService.cs | 736 +++++++++++++++++- .../SelectionDiagnosticsLogService.cs | 8 +- 6 files changed, 804 insertions(+), 37 deletions(-) diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 2c07fb5..4ea076f 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -71,6 +71,21 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.CandidateSelection.ValidateAllAnomalyStrictCandidatesAfterSuccess; + public static bool SelectionDiversifyValidationCandidates => + Current.CandidateSelection.DiversifyValidationCandidates; + + public static int SelectionDiversityScanMultiplier => + Math.Max(1, Current.CandidateSelection.DiversityScanMultiplier); + + public static int SelectionDiversityScanMinCandidates => + Math.Max(1, Current.CandidateSelection.DiversityScanMinCandidates); + + public static int SelectionDiversityScanMaxCandidates => + Math.Max(SelectionDiversityScanMinCandidates, Current.CandidateSelection.DiversityScanMaxCandidates); + + public static bool SelectionDiversityLowBitOnly => + Current.CandidateSelection.DiversityLowBitOnly; + public static RuntimeAnomalyDetectionConfig AnomalyDetection => Current.AnomalyDetection; public static RuntimeSynergyDetectionConfig SynergyDetection => Current.SynergyDetection; public static bool AnomalyDetectionEnabled => Current.AnomalyDetection.Enabled; diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index b284569..b658d10 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -254,6 +254,33 @@ public sealed class RuntimeCandidateSelectionConfig /// build/benchmark attempts on the rest of the fetched top-N list. /// public bool ValidateAllAnomalyStrictCandidatesAfterSuccess { get; set; } = false; + + /// + /// When true, windows with more predicted candidates than validation attempts fetch a + /// bounded scan pool and round-robin across candidate theory families before validation. + /// + public bool DiversifyValidationCandidates { get; set; } = true; + + /// + /// Scan roughly attemptLimit * multiplier predicted rows before selecting final attempts. + /// + public int DiversityScanMultiplier { get; set; } = 25; + + /// + /// Lower bound for the prediction-only scan pool when diversity is active. + /// + public int DiversityScanMinCandidates { get; set; } = 100; + + /// + /// Upper bound for the prediction-only scan pool when diversity is active. + /// + public int DiversityScanMaxCandidates { get; set; } = 500; + + /// + /// Optional escape hatch: if true, diversify only windows whose anchor band is Q4-ish or below. + /// Defaults false because diversity is cheap and does not increase validation attempts. + /// + public bool DiversityLowBitOnly { get; set; } = false; } diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 92481e1..983e92a 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -144,6 +144,9 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) config.CandidateSelection.MaxFallbackAttemptsPerAnchor = Math.Max(1, config.CandidateSelection.MaxFallbackAttemptsPerAnchor); config.CandidateSelection.NearBaselineMaxSizeGrowthPercent = Math.Max(0d, config.CandidateSelection.NearBaselineMaxSizeGrowthPercent); config.CandidateSelection.MinimumKldImprovementEpsilon = Math.Max(0d, config.CandidateSelection.MinimumKldImprovementEpsilon); + config.CandidateSelection.DiversityScanMultiplier = Math.Max(1, config.CandidateSelection.DiversityScanMultiplier); + config.CandidateSelection.DiversityScanMinCandidates = Math.Max(1, config.CandidateSelection.DiversityScanMinCandidates); + config.CandidateSelection.DiversityScanMaxCandidates = Math.Max(config.CandidateSelection.DiversityScanMinCandidates, config.CandidateSelection.DiversityScanMaxCandidates); config.AnomalyDetection ??= new RuntimeAnomalyDetectionConfig(); config.SynergyDetection ??= new RuntimeSynergyDetectionConfig(); @@ -340,6 +343,21 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (Has("validate-all-anomaly-strict-candidates-after-success")) config.CandidateSelection.ValidateAllAnomalyStrictCandidatesAfterSuccess = true; + if (TryParseBool(Get("selection-diversify-validation-candidates"), out var diversifyValidationCandidates)) + config.CandidateSelection.DiversifyValidationCandidates = diversifyValidationCandidates; + + if (int.TryParse(Get("selection-diversity-scan-multiplier"), out var diversityScanMultiplier) && diversityScanMultiplier > 0) + config.CandidateSelection.DiversityScanMultiplier = diversityScanMultiplier; + + if (int.TryParse(Get("selection-diversity-scan-min-candidates"), out var diversityScanMinCandidates) && diversityScanMinCandidates > 0) + config.CandidateSelection.DiversityScanMinCandidates = diversityScanMinCandidates; + + if (int.TryParse(Get("selection-diversity-scan-max-candidates"), out var diversityScanMaxCandidates) && diversityScanMaxCandidates > 0) + config.CandidateSelection.DiversityScanMaxCandidates = diversityScanMaxCandidates; + + if (TryParseBool(Get("selection-diversity-low-bit-only"), out var diversityLowBitOnly)) + config.CandidateSelection.DiversityLowBitOnly = diversityLowBitOnly; + config.Output.OutputDir = Prefer(Get("output-dir"), config.Output.OutputDir); config.Output.OutputNamePrefix = Prefer(Get("output-name-prefix"), config.Output.OutputNamePrefix); if (Has("export-external-learned-baselines")) config.Output.ExportExternalLearnedBaselines = true; @@ -370,6 +388,37 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (Has("allow-architecture-family-alias-override")) config.Identity.AllowArchitectureFamilyAliasOverride = true; } + private static bool TryParseBool(string? value, out bool result) + { + result = false; + if (string.IsNullOrWhiteSpace(value)) + return false; + + var normalized = value.Trim(); + if (bool.TryParse(normalized, out result)) + return true; + + if (string.Equals(normalized, "1", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, "yes", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, "y", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, "on", StringComparison.OrdinalIgnoreCase)) + { + result = true; + return true; + } + + if (string.Equals(normalized, "0", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, "no", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, "n", StringComparison.OrdinalIgnoreCase) || + string.Equals(normalized, "off", StringComparison.OrdinalIgnoreCase)) + { + result = false; + return true; + } + + return false; + } + private static List ParseDoubleList(string? value) { if (string.IsNullOrWhiteSpace(value)) diff --git a/MagicQuant/Models/PredictionSelectionModels.cs b/MagicQuant/Models/PredictionSelectionModels.cs index a6c3f19..16e1902 100644 --- a/MagicQuant/Models/PredictionSelectionModels.cs +++ b/MagicQuant/Models/PredictionSelectionModels.cs @@ -111,6 +111,12 @@ public sealed class HybridSelectionCandidate public int CandidateAttemptLimit { get; init; } public int PhaseWindowIndex { get; init; } public int PhaseWindowCount { get; init; } + public int RawSelectionRank { get; init; } + public string CandidateTheoryFamilyKey { get; init; } = string.Empty; + public int CandidateTheoryFamilyRank { get; init; } + public int CandidateTheoryFamilyMemberRank { get; init; } + public string CandidateTheoryFamilyDisplay { get; init; } = string.Empty; + public string DiversityMode { get; init; } = string.Empty; public IReadOnlyList CandidateSelectionNotes { get; init; } = Array.Empty(); } diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index 234301b..c38167d 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -162,9 +162,12 @@ private async Task RunStrictDominanceReplacementAsync( continue; } + int attemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor; long poolCount = await _predictedStore.CountStrictDominanceCandidatesAsync(predictedAnchor, ct); - var strictRows = await _predictedStore.QueryStrictDominanceCandidatesAsync(predictedAnchor, Config.SelectionMaxFallbackAttemptsPerAnchor, ct); - var candidates = strictRows.Select((x, i) => new HybridSelectionCandidate + bool diversityEligible = ShouldUseDiversityForWindow(anchor, anchor, predictedAnchor, predictedAnchor); + int strictScanLimit = ResolveValidationScanLimit(attemptLimit, poolCount, diversityEligible); + var strictRows = await _predictedStore.QueryStrictDominanceCandidatesAsync(predictedAnchor, strictScanLimit, ct); + var rankedStrictCandidates = strictRows.Select((x, i) => new HybridSelectionCandidate { Prediction = x, Reason = HybridSelectionReason.StrictDominanceReplacement, @@ -185,12 +188,24 @@ private async Task RunStrictDominanceReplacementAsync( LineBeatingCandidateCount = poolCount, FetchedCandidateCount = strictRows.Count, CandidatesAfterBrutalityCount = strictRows.Count, - CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, + CandidateAttemptLimit = attemptLimit, PhaseWindowIndex = 1, PhaseWindowCount = 1, + RawSelectionRank = i + 1, CandidateSelectionNotes = ["Strict DuckDB query uses predicted virtual anchor size/KLD; real anchor size/KLD is used only for post-build validation."] }).ToList(); + var selection = SelectValidationCandidates( + rankedStrictCandidates, + attemptLimit, + anchor, + anchor, + predictedAnchor, + predictedAnchor, + "StrictDominanceReplacement", + diversityEligible); + var candidates = selection.Candidates.ToList(); + var strictNotes = new List { "Strict query uses predicted virtual anchor size/KLD; real benchmark anchor is reserved for post-build validation.", @@ -223,13 +238,22 @@ private async Task RunStrictDominanceReplacementAsync( FetchedCandidateCount = strictRows.Count, CandidatesAfterBrutalityCount = strictRows.Count, SelectedForValidationCount = candidates.Count, - CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, + CandidateAttemptLimit = attemptLimit, + QueryFetchLimit = strictScanLimit, + DiversityEnabled = selection.DiversityEnabled, + DiversityMode = selection.Mode, + DiversityScanLimit = strictScanLimit, + DiversityScanFetched = strictRows.Count, + CandidateFamilyCount = selection.CandidateFamilyCount, + SelectedFamilyCount = selection.SelectedFamilyCount, + SelectedFamilyKeys = selection.SelectedFamilyKeys, TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), - Notes = strictNotes + Notes = strictNotes.Concat(selection.Notes).ToList() }; phaseDiagnostics.Add(diag); - AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, selected={candidates.Count:N0}/{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}, q8/anomaly-mode={anomalyStrictMode}, validate-all-after-success={validateAllAfterSuccess}"); + AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, scanLimit={strictScanLimit:N0}, scanFetched={strictRows.Count:N0}, afterBrutality={strictRows.Count:N0}, diversity={Markup.Escape(selection.Mode)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}, q8/anomaly-mode={anomalyStrictMode}, validate-all-after-success={validateAllAfterSuccess}"); + PrintSelectedCandidateFamilySummary(candidates); if (candidates.Count == 0) continue; @@ -478,9 +502,9 @@ private async Task RunNearBaselineReplacementAsync( var accepted = new List(); var pairs = BuildAdjacentPairs(currentAnchors); int attemptLimit = Math.Max(1, Config.SelectionMaxFallbackAttemptsPerAnchor); - int fetchLimit = Math.Max(DiagnosticPreviewLimit, attemptLimit * 3); + int fetchLimit = ResolveValidationScanLimit(attemptLimit, long.MaxValue, diversityEligible: Config.SelectionDiversifyValidationCandidates); - AnsiConsole.MarkupLine($"[grey]Near-baseline neighbor pairs:[/] [cyan]{pairs.Count:N0}[/] | size premium=[cyan]{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}%[/] | fetch limit=[cyan]{fetchLimit:N0}[/] | validation attempts/window=[cyan]{attemptLimit:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Near-baseline neighbor pairs:[/] [cyan]{pairs.Count:N0}[/] | size premium=[cyan]{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}%[/] | max scan/window=[cyan]{fetchLimit:N0}[/] | validation attempts/window=[cyan]{attemptLimit:N0}[/]"); for (int pairIndex = 0; pairIndex < pairs.Count; pairIndex++) { @@ -547,6 +571,8 @@ private async Task RunNearBaselineReplacementAsync( long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(predictionMin, predictionMax, ct); long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(predictedLowerSizeHigherDamage, predictedUpperSizeLowerDamage, predictionMin, predictionMax, ct); + bool diversityEligible = ShouldUseDiversityForWindow(lowerSizeHigherDamage, upperSizeLowerDamage, predictedLowerSizeHigherDamage, predictedUpperSizeLowerDamage); + fetchLimit = ResolveValidationScanLimit(attemptLimit, lineBeaters, diversityEligible); var rawCandidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync( lowerSizeHigherDamage, upperSizeLowerDamage, @@ -562,10 +588,11 @@ private async Task RunNearBaselineReplacementAsync( ct)).ToList(); var brutalityAnalyses = rawCandidates - .Select(x => new { Candidate = x, Brutality = AnalyzeNearLowerAnchorBrutality(x) }) + .Select((x, rawIndex) => new { Candidate = x, Brutality = AnalyzeNearLowerAnchorBrutality(x), RawRank = rawIndex + 1 }) .ToList(); - var candidates = brutalityAnalyses + int afterBrutalityCount = brutalityAnalyses.Count(y => y.Brutality.Passed); + var rankedCandidates = brutalityAnalyses .Where(x => x.Brutality.Passed) .Select(x => AttachSelectionDiagnostics( x.Candidate, @@ -573,14 +600,25 @@ private async Task RunNearBaselineReplacementAsync( windowCandidateCount: windowRows, lineBeatingCandidateCount: lineBeaters, fetchedCandidateCount: rawCandidates.Count, - candidatesAfterBrutalityCount: brutalityAnalyses.Count(y => y.Brutality.Passed), + candidatesAfterBrutalityCount: afterBrutalityCount, candidateAttemptLimit: attemptLimit, phaseWindowIndex: pairIndex + 1, phaseWindowCount: pairs.Count, - notes: [x.Brutality.Explanation])) - .Take(attemptLimit) + notes: [x.Brutality.Explanation], + rawSelectionRank: x.RawRank)) .ToList(); + var selection = SelectValidationCandidates( + rankedCandidates, + attemptLimit, + lowerSizeHigherDamage, + upperSizeLowerDamage, + predictedLowerSizeHigherDamage, + predictedUpperSizeLowerDamage, + "NearBaselineReplacement", + diversityEligible); + var candidates = selection.Candidates.ToList(); + var rejectedByBrutality = brutalityAnalyses .Where(x => !x.Brutality.Passed) .Take(DiagnosticPreviewDisplayCount) @@ -606,22 +644,31 @@ private async Task RunNearBaselineReplacementAsync( WindowCandidateCount = windowRows, LineBeatingCandidateCount = lineBeaters, FetchedCandidateCount = rawCandidates.Count, - CandidatesAfterBrutalityCount = brutalityAnalyses.Count(x => x.Brutality.Passed), + CandidatesAfterBrutalityCount = afterBrutalityCount, SelectedForValidationCount = candidates.Count, CandidateAttemptLimit = attemptLimit, QueryFetchLimit = fetchLimit, + DiversityEnabled = selection.DiversityEnabled, + DiversityMode = selection.Mode, + DiversityScanLimit = fetchLimit, + DiversityScanFetched = rawCandidates.Count, + CandidateFamilyCount = selection.CandidateFamilyCount, + SelectedFamilyCount = selection.SelectedFamilyCount, + SelectedFamilyKeys = selection.SelectedFamilyKeys, TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), RejectedByBrutalityPreview = rejectedByBrutality, - Notes = [ + Notes = new[] + { "Near-baseline DuckDB discovery uses predicted virtual anchor windows/lines; real anchor windows/lines are used only after a candidate is benchmarked.", $"Brutal zone fraction={Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###}; required gain fraction of pair KLD gap={Config.SelectionNearAnchorRequiredKldGainFractionOfPairGap:0.###}." - ] + }.Concat(selection.Notes).ToList() }; phaseDiagnostics.Add(diag); AnsiConsole.MarkupLine( $"[grey]Near-baseline window {pairIndex + 1:N0}/{pairs.Count:N0}:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)} -> {Markup.Escape(upperSizeLowerDamage.DisplayName)} " + - $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, fetched={rawCandidates.Count:N0}, after-brutality={diag.CandidatesAfterBrutalityCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}"); + $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, scanLimit={fetchLimit:N0}, scanFetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, diversity={Markup.Escape(selection.Mode)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}"); + PrintSelectedCandidateFamilySummary(candidates); if (rejectedByBrutality.Count > 0) AnsiConsole.MarkupLine($"[grey] rejected by near-lower-anchor brutality preview:[/] [cyan]{rejectedByBrutality.Count:N0}[/] (see magicquant-selection-phase-diagnostics.json)"); @@ -675,9 +722,9 @@ private async Task RunInteriorSubspaceDiscoveryAsync( var fractions = Config.SelectionInteriorWindowFractions.ToList(); int interiorAttemptLimit = Math.Max(1, Math.Max(Config.SelectionMaxCandidatesPerInteriorWindow, Config.SelectionMaxFallbackAttemptsPerAnchor)); - int interiorFetchLimit = Math.Max(interiorAttemptLimit, DiagnosticPreviewLimit); + int interiorFetchLimit = ResolveValidationScanLimit(interiorAttemptLimit, long.MaxValue, diversityEligible: Config.SelectionDiversifyValidationCandidates); - AnsiConsole.MarkupLine($"[grey]Interior neighbor pairs:[/] [cyan]{pairs.Count:N0}[/] | window fractions=[cyan]{Markup.Escape(string.Join(", ", fractions.Select(x => x.ToString("0.###"))))}[/] | candidates/window=[cyan]{Config.SelectionMaxCandidatesPerInteriorWindow:N0}[/] | fallback attempts/window=[cyan]{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}[/] | validation attempts/window=[cyan]{interiorAttemptLimit:N0}[/] | fetch preview/window=[cyan]{interiorFetchLimit:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Interior neighbor pairs:[/] [cyan]{pairs.Count:N0}[/] | window fractions=[cyan]{Markup.Escape(string.Join(", ", fractions.Select(x => x.ToString("0.###"))))}[/] | candidates/window=[cyan]{Config.SelectionMaxCandidatesPerInteriorWindow:N0}[/] | fallback attempts/window=[cyan]{Config.SelectionMaxFallbackAttemptsPerAnchor:N0}[/] | validation attempts/window=[cyan]{interiorAttemptLimit:N0}[/] | max scan/window=[cyan]{interiorFetchLimit:N0}[/]"); var allCandidates = new List(); int globalWindowIndex = 0; @@ -732,6 +779,8 @@ private async Task RunInteriorSubspaceDiscoveryAsync( string windowLabel = $"interior {i + 1}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}"; long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(predictionMin, predictionMax, ct); long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(predictedHigherDamageSmaller, predictedLowerDamageLarger, predictionMin, predictionMax, ct); + bool diversityEligible = ShouldUseDiversityForWindow(pair.HigherDamageSmaller, pair.LowerDamageLarger, predictedHigherDamageSmaller, predictedLowerDamageLarger); + interiorFetchLimit = ResolveValidationScanLimit(interiorAttemptLimit, lineBeaters, diversityEligible); var rawCandidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync( pair.HigherDamageSmaller, @@ -748,12 +797,12 @@ private async Task RunInteriorSubspaceDiscoveryAsync( ct)).ToList(); var brutalityAnalyses = rawCandidates - .Select(x => new { Candidate = x, Brutality = AnalyzeNearLowerAnchorBrutality(x) }) + .Select((x, rawIndex) => new { Candidate = x, Brutality = AnalyzeNearLowerAnchorBrutality(x), RawRank = rawIndex + 1 }) .ToList(); int afterBrutalityCount = brutalityAnalyses.Count(y => y.Brutality.Passed); - var kept = brutalityAnalyses + var rankedCandidates = brutalityAnalyses .Where(x => x.Brutality.Passed) .Select(x => AttachSelectionDiagnostics( x.Candidate, @@ -765,10 +814,21 @@ private async Task RunInteriorSubspaceDiscoveryAsync( candidateAttemptLimit: interiorAttemptLimit, phaseWindowIndex: globalWindowIndex, phaseWindowCount: estimatedWindowCount, - notes: [x.Brutality.Explanation])) - .Take(interiorAttemptLimit) + notes: [x.Brutality.Explanation], + rawSelectionRank: x.RawRank)) .ToList(); + var selection = SelectValidationCandidates( + rankedCandidates, + interiorAttemptLimit, + pair.HigherDamageSmaller, + pair.LowerDamageLarger, + predictedHigherDamageSmaller, + predictedLowerDamageLarger, + "InteriorSubspaceDiscovery", + diversityEligible); + var kept = selection.Candidates.ToList(); + allCandidates.AddRange(kept); var rejectedByBrutality = brutalityAnalyses @@ -800,14 +860,22 @@ private async Task RunInteriorSubspaceDiscoveryAsync( SelectedForValidationCount = kept.Count, CandidateAttemptLimit = interiorAttemptLimit, QueryFetchLimit = interiorFetchLimit, + DiversityEnabled = selection.DiversityEnabled, + DiversityMode = selection.Mode, + DiversityScanLimit = interiorFetchLimit, + DiversityScanFetched = rawCandidates.Count, + CandidateFamilyCount = selection.CandidateFamilyCount, + SelectedFamilyCount = selection.SelectedFamilyCount, + SelectedFamilyKeys = selection.SelectedFamilyKeys, TopCandidates = kept.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), RejectedByBrutalityPreview = rejectedByBrutality, - Notes = ["Interior DuckDB discovery uses predicted virtual anchor windows/lines; real anchor windows/lines are used only after benchmark validation."] + Notes = new[] { "Interior DuckDB discovery uses predicted virtual anchor windows/lines; real anchor windows/lines are used only after benchmark validation." }.Concat(selection.Notes).ToList() }); AnsiConsole.MarkupLine( $"[grey]Interior window {globalWindowIndex:N0}/{Math.Max(estimatedWindowCount, globalWindowIndex):N0}:[/] {Markup.Escape(pair.HigherDamageSmaller.DisplayName)} -> {Markup.Escape(pair.LowerDamageLarger.DisplayName)} " + - $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, fetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, selected={kept.Count:N0}/{interiorAttemptLimit:N0}"); + $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, scanLimit={interiorFetchLimit:N0}, scanFetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, diversity={Markup.Escape(selection.Mode)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={kept.Count:N0}/{interiorAttemptLimit:N0}"); + PrintSelectedCandidateFamilySummary(kept); realCursor = realMax; predictionCursor = predictionMax; @@ -945,7 +1013,276 @@ private static HybridSelectionCandidate AttachSelectionDiagnostics( int candidateAttemptLimit, int phaseWindowIndex, int phaseWindowCount, - IReadOnlyList notes) + IReadOnlyList notes, + int? rawSelectionRank = null) + { + return CloneCandidateWithSelectionMetadata( + candidate, + attemptOrder: candidate.AttemptOrder, + rawSelectionRank: rawSelectionRank ?? candidate.RawSelectionRank, + familyKey: candidate.CandidateTheoryFamilyKey, + familyDisplay: candidate.CandidateTheoryFamilyDisplay, + familyRank: candidate.CandidateTheoryFamilyRank, + familyMemberRank: candidate.CandidateTheoryFamilyMemberRank, + diversityMode: candidate.DiversityMode, + notes: notes, + poolSize: poolSize, + windowCandidateCount: windowCandidateCount, + lineBeatingCandidateCount: lineBeatingCandidateCount, + fetchedCandidateCount: fetchedCandidateCount, + candidatesAfterBrutalityCount: candidatesAfterBrutalityCount, + candidateAttemptLimit: candidateAttemptLimit, + phaseWindowIndex: phaseWindowIndex, + phaseWindowCount: phaseWindowCount); + } + + private static int ResolveValidationScanLimit(int attemptLimit, long candidatePoolSize, bool diversityEligible) + { + attemptLimit = Math.Max(1, attemptLimit); + + if (!Config.SelectionDiversifyValidationCandidates || !diversityEligible) + return attemptLimit; + + if (candidatePoolSize > 0 && candidatePoolSize <= attemptLimit) + return attemptLimit; + + long requested = (long)attemptLimit * Config.SelectionDiversityScanMultiplier; + int min = Math.Max(attemptLimit, Config.SelectionDiversityScanMinCandidates); + int max = Math.Max(min, Config.SelectionDiversityScanMaxCandidates); + long clamped = Math.Clamp(requested, min, max); + + if (candidatePoolSize > 0 && candidatePoolSize < clamped) + clamped = candidatePoolSize; + + return checked((int)Math.Max(attemptLimit, clamped)); + } + + private static bool ShouldUseDiversityForWindow( + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger, + PredictedAnchorRow? higherDamagePredictionAnchor, + PredictedAnchorRow? lowerDamagePredictionAnchor) + { + if (!Config.SelectionDiversifyValidationCandidates) + return false; + + if (!Config.SelectionDiversityLowBitOnly) + return true; + + return IsQ4ishOrBelow(higherDamageSmaller.Quant.BaseQuant) || + IsQ4ishOrBelow(lowerDamageLarger.Quant.BaseQuant) || + IsQ4ishOrBelow(higherDamagePredictionAnchor?.RuntimeBaselineId) || + IsQ4ishOrBelow(lowerDamagePredictionAnchor?.RuntimeBaselineId); + } + + private static bool IsQ4ishOrBelow(byte? baselineId) + { + if (!baselineId.HasValue) + return false; + + try + { + return BaselineQuants.FromId(baselineId.Value).BitRange <= 4; + } + catch + { + return false; + } + } + + private static bool IsQ4ishOrBelow(BaselineQuants baseline) => baseline.BitRange <= 4; + + private static ValidationCandidateSelectionResult SelectValidationCandidates( + IReadOnlyList rankedCandidates, + int attemptLimit, + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger, + PredictedAnchorRow? higherDamagePredictionAnchor, + PredictedAnchorRow? lowerDamagePredictionAnchor, + string phaseName, + bool diversityEligible) + { + attemptLimit = Math.Max(1, attemptLimit); + if (rankedCandidates.Count == 0) + { + return new ValidationCandidateSelectionResult + { + Candidates = Array.Empty(), + Mode = Config.SelectionDiversifyValidationCandidates ? diversityEligible ? "enabled-empty" : "disabled-low-bit-only" : "disabled", + DiversityEnabled = Config.SelectionDiversifyValidationCandidates && diversityEligible, + Notes = ["No candidates survived the prediction/brutality filters for this window."] + }; + } + + var activeGroups = GetActiveTensorGroups(); + var entries = rankedCandidates + .Select((candidate, rawIndex) => + { + var signature = BuildCandidateTheorySignature(candidate, higherDamageSmaller, lowerDamageLarger, higherDamagePredictionAnchor, lowerDamagePredictionAnchor, activeGroups); + return new CandidateFamilyEntry + { + Candidate = candidate, + Signature = signature, + RawRank = candidate.RawSelectionRank > 0 ? candidate.RawSelectionRank : rawIndex + 1 + }; + }) + .ToList(); + + var families = entries + .GroupBy(x => x.Signature.Key, StringComparer.Ordinal) + .Select((g, familyIndex) => new CandidateTheoryFamily + { + Key = g.Key, + Display = g.First().Signature.Display, + Rank = familyIndex + 1, + Members = g.OrderBy(x => x.RawRank).ToList() + }) + .OrderBy(x => x.Members[0].RawRank) + .ToList(); + + for (int familyIndex = 0; familyIndex < families.Count; familyIndex++) + { + families[familyIndex].Rank = familyIndex + 1; + for (int memberIndex = 0; memberIndex < families[familyIndex].Members.Count; memberIndex++) + families[familyIndex].Members[memberIndex].MemberRank = memberIndex + 1; + } + + bool canDiversify = Config.SelectionDiversifyValidationCandidates && diversityEligible && rankedCandidates.Count > attemptLimit; + if (!canDiversify) + { + string mode = Config.SelectionDiversifyValidationCandidates + ? diversityEligible + ? "not-needed" + : "disabled-low-bit-only" + : "disabled"; + + var selectedWithoutDiversity = entries + .Take(attemptLimit) + .Select((entry, index) => DecorateSelectedCandidate(entry, families, index + 1, mode)) + .ToList(); + + var notesWithoutDiversity = new List + { + BuildDiversityNote(mode, phaseName, rankedCandidates.Count, attemptLimit, families.Count, selectedWithoutDiversity.Count) + }; + AddDiversityFamilyGranularityWarning(notesWithoutDiversity, families.Count, rankedCandidates.Count); + + return new ValidationCandidateSelectionResult + { + Candidates = selectedWithoutDiversity, + Mode = mode, + DiversityEnabled = false, + CandidateFamilyCount = families.Count, + SelectedFamilyCount = selectedWithoutDiversity.Select(x => x.CandidateTheoryFamilyKey).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).Count(), + SelectedFamilyKeys = selectedWithoutDiversity.Select(x => x.CandidateTheoryFamilyKey).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).ToList(), + Notes = notesWithoutDiversity + }; + } + + var selectedEntries = new List(); + var selectedKeys = new HashSet(StringComparer.Ordinal); + void AddEntry(CandidateFamilyEntry entry) + { + string key = TensorConfigIdentity.ToKey(entry.Candidate.Prediction.Config); + if (!selectedKeys.Add(key)) + return; + selectedEntries.Add(entry); + } + + AddEntry(entries[0]); + + for (int memberRank = 1; selectedEntries.Count < attemptLimit; memberRank++) + { + bool addedThisRound = false; + foreach (var family in families) + { + var member = family.Members.FirstOrDefault(x => x.MemberRank == memberRank); + if (member == null) + continue; + + int before = selectedEntries.Count; + AddEntry(member); + addedThisRound |= selectedEntries.Count > before; + + if (selectedEntries.Count >= attemptLimit) + break; + } + + if (!addedThisRound) + break; + } + + var selected = selectedEntries + .Take(attemptLimit) + .Select((entry, index) => DecorateSelectedCandidate(entry, families, index + 1, "enabled")) + .ToList(); + + bool exhaustedDistinctFamilies = selected.Count > selected.Select(x => x.CandidateTheoryFamilyKey).Distinct(StringComparer.Ordinal).Count(); + var notes = new List + { + BuildDiversityNote("enabled", phaseName, rankedCandidates.Count, attemptLimit, families.Count, selected.Count) + }; + if (exhaustedDistinctFamilies) + notes.Add("Distinct candidate theory families were exhausted before the attempt limit; remaining slots were filled round-robin by the next-best members of already-selected families."); + AddDiversityFamilyGranularityWarning(notes, families.Count, rankedCandidates.Count); + + return new ValidationCandidateSelectionResult + { + Candidates = selected, + Mode = "enabled", + DiversityEnabled = true, + CandidateFamilyCount = families.Count, + SelectedFamilyCount = selected.Select(x => x.CandidateTheoryFamilyKey).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).Count(), + SelectedFamilyKeys = selected.Select(x => x.CandidateTheoryFamilyKey).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).ToList(), + Notes = notes + }; + } + + private static HybridSelectionCandidate DecorateSelectedCandidate( + CandidateFamilyEntry entry, + IReadOnlyList families, + int attemptOrder, + string diversityMode) + { + var family = families.First(x => string.Equals(x.Key, entry.Signature.Key, StringComparison.Ordinal)); + int memberRank = entry.MemberRank > 0 ? entry.MemberRank : Math.Max(1, family.Members.FindIndex(x => ReferenceEquals(x, entry)) + 1); + var notes = entry.Candidate.CandidateSelectionNotes + .Concat(new[] + { + $"diversity={diversityMode}; rawRank={entry.RawRank}; familyRank={family.Rank}; familyMemberRank={memberRank}; familyKey={entry.Signature.Key}; familyDisplay={entry.Signature.Display}" + }) + .ToList(); + + return CloneCandidateWithSelectionMetadata( + entry.Candidate, + attemptOrder, + entry.RawRank, + entry.Signature.Key, + entry.Signature.Display, + family.Rank, + memberRank, + diversityMode, + notes); + } + + private static HybridSelectionCandidate CloneCandidateWithSelectionMetadata( + HybridSelectionCandidate candidate, + int attemptOrder, + int rawSelectionRank, + string familyKey, + string familyDisplay, + int familyRank, + int familyMemberRank, + string diversityMode, + IReadOnlyList notes, + long? poolSize = null, + long? windowCandidateCount = null, + long? lineBeatingCandidateCount = null, + int? fetchedCandidateCount = null, + int? candidatesAfterBrutalityCount = null, + int? candidateAttemptLimit = null, + int? phaseWindowIndex = null, + int? phaseWindowCount = null) { return new HybridSelectionCandidate { @@ -961,20 +1298,274 @@ private static HybridSelectionCandidate AttachSelectionDiagnostics( WindowMaxSizeBytes = candidate.WindowMaxSizeBytes, LinearExpectedKld = candidate.LinearExpectedKld, PredictedGainOverLine = candidate.PredictedGainOverLine, - AttemptOrder = candidate.AttemptOrder, + AttemptOrder = attemptOrder, WindowLabel = candidate.WindowLabel, - CandidatePoolSize = poolSize, - WindowCandidateCount = windowCandidateCount, - LineBeatingCandidateCount = lineBeatingCandidateCount, - FetchedCandidateCount = fetchedCandidateCount, - CandidatesAfterBrutalityCount = candidatesAfterBrutalityCount, - CandidateAttemptLimit = candidateAttemptLimit, - PhaseWindowIndex = phaseWindowIndex, - PhaseWindowCount = phaseWindowCount, + CandidatePoolSize = poolSize ?? candidate.CandidatePoolSize, + WindowCandidateCount = windowCandidateCount ?? candidate.WindowCandidateCount, + LineBeatingCandidateCount = lineBeatingCandidateCount ?? candidate.LineBeatingCandidateCount, + FetchedCandidateCount = fetchedCandidateCount ?? candidate.FetchedCandidateCount, + CandidatesAfterBrutalityCount = candidatesAfterBrutalityCount ?? candidate.CandidatesAfterBrutalityCount, + CandidateAttemptLimit = candidateAttemptLimit ?? candidate.CandidateAttemptLimit, + PhaseWindowIndex = phaseWindowIndex ?? candidate.PhaseWindowIndex, + PhaseWindowCount = phaseWindowCount ?? candidate.PhaseWindowCount, + RawSelectionRank = rawSelectionRank, + CandidateTheoryFamilyKey = familyKey, + CandidateTheoryFamilyDisplay = familyDisplay, + CandidateTheoryFamilyRank = familyRank, + CandidateTheoryFamilyMemberRank = familyMemberRank, + DiversityMode = diversityMode, CandidateSelectionNotes = notes }; } + private static CandidateTheorySignature BuildCandidateTheorySignature( + HybridSelectionCandidate candidate, + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger, + PredictedAnchorRow? higherDamagePredictionAnchor, + PredictedAnchorRow? lowerDamagePredictionAnchor, + IReadOnlyList activeGroups) + { + var config = candidate.Prediction.Config; + var baseQuant = BaselineQuants.FromId(config.BaseQuant); + int anchorBit = ResolveCandidateTheoryAnchorBit(higherDamageSmaller, lowerDamageLarger, higherDamagePredictionAnchor, lowerDamagePredictionAnchor); + + var coarseRisk = new List(); + var coarseProtected = new List(); + var coarseSensitive = new List(); + + var displayRisk = new List(); + var displayProtected = new List(); + var displayExternal = new List(); + var displaySensitive = new List(); + + int sixPlus = 0; + int five = 0; + int four = 0; + int threeOrLess = 0; + + foreach (var group in activeGroups.OrderBy(x => x.UniqueId)) + { + var effective = GetEffectiveGroupBaseline(config, group); + string exactPlacement = $"{group.ShortCode}={effective.Names[0]}"; + string coarsePlacement = $"{group.ShortCode}={ToCoarseBitBand(effective.BitRange)}"; + int bitDeltaFromAnchor = effective.BitRange - anchorBit; + + if (effective.BitRange >= 6) + sixPlus++; + else if (effective.BitRange == 5) + five++; + else if (effective.BitRange == 4) + four++; + else + threeOrLess++; + + bool severeRisk = effective.BitRange <= 3 || effective.BitRange <= anchorBit - 1; + bool majorProtection = (anchorBit <= 5 && effective.BitRange >= 6) || effective.BitRange >= anchorBit + 1; + bool externalOrCustom = effective.IsCustomBaseline || effective.IsExternalRepositoryBaseline; + + if (severeRisk) + { + coarseRisk.Add(coarsePlacement); + displayRisk.Add(exactPlacement); + } + + if (majorProtection) + { + coarseProtected.Add(coarsePlacement); + displayProtected.Add(exactPlacement); + } + + // External/custom identity is valuable in diagnostics, but it must not make every + // UD-vs-standard sibling its own selection family. The coarse key intentionally + // relies on the strategic bit-band role; the exact external name stays in Display. + if (externalOrCustom) + displayExternal.Add(exactPlacement); + + // Sensitive groups are allowed to influence the coarse key only when the placement is + // a real strategy shift, not merely a Q4 sibling spelling such as IQ4_NL vs Q4_K_M. + if (IsHighSensitivityGroup(group) && effective.UniqueId != baseQuant.UniqueId) + { + displaySensitive.Add(exactPlacement); + if (!severeRisk && !majorProtection && Math.Abs(bitDeltaFromAnchor) > 1) + coarseSensitive.Add(coarsePlacement); + } + } + + string anchorDisplay = $"anchor={higherDamagePredictionAnchor?.DisplayName ?? higherDamageSmaller.DisplayName}->{lowerDamagePredictionAnchor?.DisplayName ?? lowerDamageLarger.DisplayName}@{anchorBit}b"; + string anchorKey = $"anchor={ToAnchorBand(anchorBit)}"; + string bulk = $"bulk:6p={sixPlus},5={five},4={four},3m={threeOrLess}"; + + var keyComponents = new List + { + $"base={baseQuant.Names[0]}", + anchorKey, + bulk + }; + + AddSortedComponent(keyComponents, "risk", coarseRisk); + AddSortedComponent(keyComponents, "protect", coarseProtected); + AddSortedComponent(keyComponents, "sensitiveShift", coarseSensitive); + + var displayComponents = new List + { + $"base={baseQuant.Names[0]}", + anchorDisplay, + bulk + }; + + AddSortedComponent(displayComponents, "risk", displayRisk); + AddSortedComponent(displayComponents, "protect", displayProtected); + AddSortedComponent(displayComponents, "external", displayExternal); + AddSortedComponent(displayComponents, "sensitive", displaySensitive); + + return new CandidateTheorySignature + { + Key = string.Join("|", keyComponents), + Display = string.Join("|", displayComponents) + }; + } + + private static int ResolveCandidateTheoryAnchorBit( + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger, + PredictedAnchorRow? higherDamagePredictionAnchor, + PredictedAnchorRow? lowerDamagePredictionAnchor) + { + var bits = new List + { + higherDamageSmaller.Quant.BaseQuant.BitRange, + lowerDamageLarger.Quant.BaseQuant.BitRange + }; + + AddPredictedAnchorBit(bits, higherDamagePredictionAnchor); + AddPredictedAnchorBit(bits, lowerDamagePredictionAnchor); + return bits.Count == 0 ? 4 : bits.Min(); + } + + private static void AddPredictedAnchorBit(List bits, PredictedAnchorRow? anchor) + { + if (anchor == null) + return; + + try + { + bits.Add(BaselineQuants.FromId(anchor.RuntimeBaselineId).BitRange); + } + catch + { + // Predicted anchor metadata is diagnostic here; real benchmark anchors remain the fallback. + } + } + + private static string ToAnchorBand(int bitRange) + { + if (bitRange <= 3) + return "Q3ish"; + if (bitRange == 4) + return "Q4ish"; + if (bitRange == 5) + return "Q5ish"; + if (bitRange == 6) + return "Q6ish"; + return "Q8ish"; + } + + private static string ToCoarseBitBand(int bitRange) + { + if (bitRange >= 6) + return "6p"; + if (bitRange == 5) + return "5bit"; + if (bitRange == 4) + return "4bit"; + return "3bit"; + } + + private static void AddSortedComponent(List components, string label, IEnumerable values) + { + var distinct = values + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Distinct(StringComparer.Ordinal) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + if (distinct.Count > 0) + components.Add($"{label}:" + string.Join(",", distinct)); + } + + private static void AddDiversityFamilyGranularityWarning(List notes, int candidateFamilyCount, int scannedCandidateCount) + { + if (!Config.SelectionDiversifyValidationCandidates) + return; + + if (scannedCandidateCount < 25) + return; + + if (candidateFamilyCount < scannedCandidateCount * 0.90d) + return; + + notes.Add("Diversity warning: candidate family key may be too fine-grained; most scanned candidates formed unique families."); + } + + private static BaselineQuants GetEffectiveGroupBaseline(TensorConfig config, TensorGroup group) + { + byte stored = group.UniqueId switch + { + 0 => config.Embeddings, + 1 => config.LmHead, + 2 => config.AttnQ, + 3 => config.AttnKV, + 4 => config.AttnOutput, + 5 => config.FfnUpGate, + 6 => config.FfnDown, + 7 => config.MoeExperts, + 8 => config.MoeRouter, + _ => BaselineQuants.TensorConfigNullSlotValue + }; + + return BaselineQuants.IsNullTensorConfigGroupSlot(stored) + ? BaselineQuants.FromId(config.BaseQuant) + : BaselineQuants.DecodeTensorConfigGroupSlotToBaseline(stored); + } + + private static IReadOnlyList GetActiveTensorGroups() + { + var unusedIds = Cache.UnusedTensorGroups.Select(x => x.UniqueId).ToHashSet(); + return TReg.All.Where(x => !unusedIds.Contains(x.UniqueId)).OrderBy(x => x.UniqueId).ToList(); + } + + private static bool IsHighSensitivityGroup(TensorGroup group) => + group.UniqueId == TReg.Embeddings.UniqueId || + group.UniqueId == TReg.LmHead.UniqueId || + group.UniqueId == TReg.AttnQ.UniqueId || + group.UniqueId == TReg.AttnKV.UniqueId || + group.UniqueId == TReg.FfnDown.UniqueId; + + private static string BuildDiversityNote(string mode, string phaseName, int candidateCount, int attemptLimit, int familyCount, int selectedCount) => + mode switch + { + "enabled" => $"Diversity enabled for {phaseName}: selected {selectedCount:N0}/{attemptLimit:N0} validation attempts from {familyCount:N0} candidate theory families across {candidateCount:N0} filtered scan candidates; raw top prediction is preserved as attempt 1.", + "not-needed" => $"Diversity not needed for {phaseName}: filtered candidate count {candidateCount:N0} <= attempt limit {attemptLimit:N0}; candidates kept in raw predicted order.", + "disabled-low-bit-only" => $"Diversity skipped for {phaseName}: candidate_selection.diversity_low_bit_only=true and this anchor/window was not Q4-ish or below.", + _ => $"Diversity disabled for {phaseName}; candidates kept in raw predicted order." + }; + + private static void PrintSelectedCandidateFamilySummary(IReadOnlyList candidates) + { + foreach (var candidate in candidates.Take(DiagnosticPreviewDisplayCount)) + { + if (string.IsNullOrWhiteSpace(candidate.CandidateTheoryFamilyDisplay)) + continue; + + AnsiConsole.MarkupLine( + $"[grey] selected attempt={candidate.AttemptOrder:N0}/{candidate.CandidateAttemptLimit:N0} rawRank={candidate.RawSelectionRank:N0} " + + $"familyRank={candidate.CandidateTheoryFamilyRank:N0} memberRank={candidate.CandidateTheoryFamilyMemberRank:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] familyKey=[/][cyan]{Markup.Escape(candidate.CandidateTheoryFamilyKey)}[/]"); + AnsiConsole.MarkupLine($"[grey] familyDisplay=[/][cyan]{Markup.Escape(candidate.CandidateTheoryFamilyDisplay)}[/]"); + } + } + private static BrutalityAnalysis AnalyzeNearLowerAnchorBrutality(HybridSelectionCandidate candidate) { var higherDamagePredictionAnchor = candidate.HigherDamagePredictionAnchor; @@ -1279,6 +1870,14 @@ private static void PrintCandidatePredictionLine(HybridSelectionCandidate candid AnsiConsole.MarkupLine( $"[grey] selection context:[/] pool={candidate.CandidatePoolSize:N0}, windowRows={candidate.WindowCandidateCount:N0}, lineBeat={candidate.LineBeatingCandidateCount:N0}, " + $"fetched={candidate.FetchedCandidateCount:N0}, afterBrutality={candidate.CandidatesAfterBrutalityCount:N0}, attemptLimit={candidate.CandidateAttemptLimit:N0}"); + if (!string.IsNullOrWhiteSpace(candidate.CandidateTheoryFamilyDisplay)) + { + AnsiConsole.MarkupLine( + $"[grey] diversity family:[/] mode={Markup.Escape(candidate.DiversityMode)}, rawRank={candidate.RawSelectionRank:N0}, " + + $"familyRank={candidate.CandidateTheoryFamilyRank:N0}, memberRank={candidate.CandidateTheoryFamilyMemberRank:N0}"); + AnsiConsole.MarkupLine($"[grey] familyKey:[/] {Markup.Escape(candidate.CandidateTheoryFamilyKey)}"); + AnsiConsole.MarkupLine($"[grey] familyDisplay:[/] {Markup.Escape(candidate.CandidateTheoryFamilyDisplay)}"); + } AnsiConsole.MarkupLine($"[grey] bit space:[/] {Markup.Escape(DescribeBitSpace(candidate.Prediction.Config))}"); } @@ -1438,6 +2037,7 @@ private static void PrintCandidatePreviewTable(IReadOnlyList bytes / 1024d / 1024d / 1024d; + private sealed class ValidationCandidateSelectionResult + { + public IReadOnlyList Candidates { get; init; } = Array.Empty(); + public string Mode { get; init; } = string.Empty; + public bool DiversityEnabled { get; init; } + public int CandidateFamilyCount { get; init; } + public int SelectedFamilyCount { get; init; } + public IReadOnlyList SelectedFamilyKeys { get; init; } = Array.Empty(); + public IReadOnlyList Notes { get; init; } = Array.Empty(); + } + + private sealed class CandidateTheorySignature + { + public string Key { get; init; } = string.Empty; + public string Display { get; init; } = string.Empty; + } + + private sealed class CandidateFamilyEntry + { + public HybridSelectionCandidate Candidate { get; init; } = default!; + public CandidateTheorySignature Signature { get; init; } = new(); + public int RawRank { get; init; } + public int MemberRank { get; set; } + } + + private sealed class CandidateTheoryFamily + { + public string Key { get; init; } = string.Empty; + public string Display { get; init; } = string.Empty; + public int Rank { get; set; } + public List Members { get; init; } = new(); + } + private sealed class AdjacentAnchorPair { public BenchmarkSnapshotRecord LowerDamageLarger { get; init; } = default!; @@ -1801,6 +2452,13 @@ private sealed class SelectionPhaseDiagnostic public int SelectedForValidationCount { get; init; } public int CandidateAttemptLimit { get; init; } public int QueryFetchLimit { get; init; } + public bool DiversityEnabled { get; init; } + public string DiversityMode { get; init; } = string.Empty; + public int DiversityScanLimit { get; init; } + public int DiversityScanFetched { get; init; } + public int CandidateFamilyCount { get; init; } + public int SelectedFamilyCount { get; init; } + public IReadOnlyList SelectedFamilyKeys { get; init; } = Array.Empty(); public IReadOnlyList TopCandidates { get; init; } = Array.Empty(); public IReadOnlyList RejectedByBrutalityPreview { get; init; } = Array.Empty(); public IReadOnlyList Notes { get; init; } = Array.Empty(); @@ -1818,6 +2476,12 @@ private sealed class CandidatePreviewLog public double PredictedGainOverLine { get; init; } public double PredictionConfidence { get; init; } public ulong? PredictionRank { get; init; } + public int RawSelectionRank { get; init; } + public string CandidateTheoryFamilyKey { get; init; } = string.Empty; + public string CandidateTheoryFamilyDisplay { get; init; } = string.Empty; + public int CandidateTheoryFamilyRank { get; init; } + public int CandidateTheoryFamilyMemberRank { get; init; } + public string DiversityMode { get; init; } = string.Empty; public string BaseQuant { get; init; } = string.Empty; public byte BaseBitRange { get; init; } public string BitSpace { get; init; } = string.Empty; diff --git a/MagicQuant/Services/SelectionDiagnosticsLogService.cs b/MagicQuant/Services/SelectionDiagnosticsLogService.cs index c2b1cbb..6527211 100644 --- a/MagicQuant/Services/SelectionDiagnosticsLogService.cs +++ b/MagicQuant/Services/SelectionDiagnosticsLogService.cs @@ -126,6 +126,12 @@ private static object ToFailureLog(CandidateValidationResult failure) fetchedCandidateCount = c.FetchedCandidateCount, candidatesAfterBrutalityCount = c.CandidatesAfterBrutalityCount, candidateAttemptLimit = c.CandidateAttemptLimit, + rawSelectionRank = c.RawSelectionRank, + diversityMode = c.DiversityMode, + candidateTheoryFamilyKey = c.CandidateTheoryFamilyKey, + candidateTheoryFamilyDisplay = c.CandidateTheoryFamilyDisplay, + candidateTheoryFamilyRank = c.CandidateTheoryFamilyRank, + candidateTheoryFamilyMemberRank = c.CandidateTheoryFamilyMemberRank, notes = c.CandidateSelectionNotes }, actual = snap == null @@ -240,4 +246,4 @@ private static string ResolveGgufDirectory() } private static string ToGb(ulong bytes) => (bytes / 1024d / 1024d / 1024d).ToString("0.00"); -} +} \ No newline at end of file From f8d6e553161187cec9cd510282fc5caee442b8dc Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 5 May 2026 19:44:51 -0400 Subject: [PATCH 198/258] predictive engine working --- .../PredictionGuidedHybridSelectionService.cs | 731 ++++++++++++------ .../Services/RemainingCombinationStore.cs | 81 +- 2 files changed, 571 insertions(+), 241 deletions(-) diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index c38167d..fbaf391 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -163,10 +163,19 @@ private async Task RunStrictDominanceReplacementAsync( } int attemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor; - long poolCount = await _predictedStore.CountStrictDominanceCandidatesAsync(predictedAnchor, ct); + ulong predictedAnchorSizeBytes = predictedAnchor.PredictedSizeBytes; + ulong realAnchorSizeBytes = anchor.SizeBytes; + ulong effectiveStrictMaxSizeBytes = Math.Min(predictedAnchorSizeBytes, realAnchorSizeBytes); + + long predictedPoolCount = await _predictedStore.CountStrictDominanceCandidatesAsync(predictedAnchor, predictedAnchorSizeBytes, ct); + long poolCount = await _predictedStore.CountStrictDominanceCandidatesAsync(predictedAnchor, effectiveStrictMaxSizeBytes, ct); + long deterministicEligibleCount = poolCount; + long rejectedByRealStrictSizeCeiling = Math.Max(0, predictedPoolCount - deterministicEligibleCount); + long rejectedByOtherPhaseDeterministicRules = 0; + bool diversityEligible = ShouldUseDiversityForWindow(anchor, anchor, predictedAnchor, predictedAnchor); int strictScanLimit = ResolveValidationScanLimit(attemptLimit, poolCount, diversityEligible); - var strictRows = await _predictedStore.QueryStrictDominanceCandidatesAsync(predictedAnchor, strictScanLimit, ct); + var strictRows = await _predictedStore.QueryStrictDominanceCandidatesAsync(predictedAnchor, effectiveStrictMaxSizeBytes, strictScanLimit, ct); var rankedStrictCandidates = strictRows.Select((x, i) => new HybridSelectionCandidate { Prediction = x, @@ -176,9 +185,9 @@ private async Task RunStrictDominanceReplacementAsync( LowerDamagePredictionAnchor = predictedAnchor, HigherDamagePredictionAnchor = predictedAnchor, PredictionWindowMinSizeBytes = 0, - PredictionWindowMaxSizeBytes = predictedAnchor.PredictedSizeBytes, + PredictionWindowMaxSizeBytes = effectiveStrictMaxSizeBytes, WindowMinSizeBytes = 0, - WindowMaxSizeBytes = anchor.SizeBytes, + WindowMaxSizeBytes = effectiveStrictMaxSizeBytes, LinearExpectedKld = predictedAnchor.PredictedKld, PredictedGainOverLine = predictedAnchor.PredictedKld - x.PredictedKld, AttemptOrder = i + 1, @@ -192,7 +201,11 @@ private async Task RunStrictDominanceReplacementAsync( PhaseWindowIndex = 1, PhaseWindowCount = 1, RawSelectionRank = i + 1, - CandidateSelectionNotes = ["Strict DuckDB query uses predicted virtual anchor size/KLD; real anchor size/KLD is used only for post-build validation."] + CandidateSelectionNotes = + [ + "Strict DuckDB query uses prediction-space KLD, but predicted-size eligibility is capped by the real anchor size because MagicQuant size prediction is trusted/exact.", + $"predictedAnchorSizeBytes={predictedAnchorSizeBytes:N0}; realAnchorSizeBytes={realAnchorSizeBytes:N0}; effectiveStrictMaxSizeBytes={effectiveStrictMaxSizeBytes:N0}; deterministicEligibleCount={deterministicEligibleCount:N0}; rejectedByRealStrictSizeCeiling={rejectedByRealStrictSizeCeiling:N0}; rejectedByOtherPhaseDeterministicRules={rejectedByOtherPhaseDeterministicRules:N0}." + ] }).ToList(); var selection = SelectValidationCandidates( @@ -208,8 +221,8 @@ private async Task RunStrictDominanceReplacementAsync( var strictNotes = new List { - "Strict query uses predicted virtual anchor size/KLD; real benchmark anchor is reserved for post-build validation.", - $"Prediction anchor={predictedAnchor.DisplayName}; predictedKld={predictedAnchor.PredictedKld:0.000000}; predictedSizeBytes={predictedAnchor.PredictedSizeBytes:N0}; realKld={anchor.Kld:0.000000}; realSizeBytes={anchor.SizeBytes:N0}." + "Strict KLD eligibility remains prediction-space, but strict predicted-size eligibility is capped by the real anchor size because MagicQuant size prediction is trusted/exact.", + $"Prediction anchor={predictedAnchor.DisplayName}; predictedKld={predictedAnchor.PredictedKld:0.000000}; predictedAnchorSizeBytes={predictedAnchorSizeBytes:N0}; realKld={anchor.Kld:0.000000}; realAnchorSizeBytes={realAnchorSizeBytes:N0}; effectiveStrictMaxSizeBytes={effectiveStrictMaxSizeBytes:N0}; deterministicEligibleCount={deterministicEligibleCount:N0}; rejectedByRealStrictSizeCeiling={rejectedByRealStrictSizeCeiling:N0}; rejectedByOtherPhaseDeterministicRules={rejectedByOtherPhaseDeterministicRules:N0}." }; bool anomalyStrictMode = IsQ8Anchor(anchor) || candidates.Any(x => Math.Abs(x.Prediction.AnomalyAdjustmentKld) > 1e-12); bool validateAllAfterSuccess = anomalyStrictMode && Config.SelectionValidateAllAnomalyStrictCandidatesAfterSuccess; @@ -229,9 +242,9 @@ private async Task RunStrictDominanceReplacementAsync( PredictionHigherDamageSmaller = ToPredictionAnchorLog(predictedAnchor), PredictionLowerDamageLarger = ToPredictionAnchorLog(predictedAnchor), PredictionWindowMinSizeBytes = 0, - PredictionWindowMaxSizeBytes = predictedAnchor.PredictedSizeBytes, + PredictionWindowMaxSizeBytes = effectiveStrictMaxSizeBytes, WindowMinSizeBytes = 0, - WindowMaxSizeBytes = anchor.SizeBytes, + WindowMaxSizeBytes = effectiveStrictMaxSizeBytes, CandidatePoolSize = poolCount, WindowCandidateCount = poolCount, LineBeatingCandidateCount = poolCount, @@ -247,13 +260,21 @@ private async Task RunStrictDominanceReplacementAsync( CandidateFamilyCount = selection.CandidateFamilyCount, SelectedFamilyCount = selection.SelectedFamilyCount, SelectedFamilyKeys = selection.SelectedFamilyKeys, + DiversitySelectionStrategy = selection.SelectionStrategy, + DiversityOverflowCount = selection.OverflowCount, + DiversitySizeFloorStartBytes = selection.SizeFloorStartBytes, + DiversitySizeFloorEndBytes = selection.SizeFloorEndBytes, TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), Notes = strictNotes.Concat(selection.Notes).ToList() }; phaseDiagnostics.Add(diag); - AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, scanLimit={strictScanLimit:N0}, scanFetched={strictRows.Count:N0}, afterBrutality={strictRows.Count:N0}, diversity={Markup.Escape(selection.Mode)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}, q8/anomaly-mode={anomalyStrictMode}, validate-all-after-success={validateAllAfterSuccess}"); + AnsiConsole.MarkupLine($"[grey]Strict candidates for {Markup.Escape(anchor.DisplayName)}:[/] pool={poolCount:N0}, predictedPoolCount={predictedPoolCount:N0}, deterministicEligibleCount={deterministicEligibleCount:N0}, scanLimit={strictScanLimit:N0}, scanFetched={strictRows.Count:N0}, afterBrutality={strictRows.Count:N0}, diversity={Markup.Escape(selection.Mode)}, selectionStrategy={Markup.Escape(selection.SelectionStrategy)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}, overflowCount={selection.OverflowCount:N0}, sizeFloorStart={selection.SizeFloorStartBytes?.ToString("N0") ?? "n/a"}, predictedAnchorSizeBytes={predictedAnchorSizeBytes:N0}, realAnchorSizeBytes={realAnchorSizeBytes:N0}, effectiveStrictMaxSizeBytes={effectiveStrictMaxSizeBytes:N0}, rejectedByRealStrictSizeCeiling={rejectedByRealStrictSizeCeiling:N0}, rejectedByOtherPhaseDeterministicRules={rejectedByOtherPhaseDeterministicRules:N0}, q8/anomaly-mode={anomalyStrictMode}, validate-all-after-success={validateAllAfterSuccess}"); PrintSelectedCandidateFamilySummary(candidates); + PrintSelectionLadderNotes(selection.Notes); + + if (deterministicEligibleCount == 0) + AnsiConsole.MarkupLine($"[yellow]Strict dominance skipped builds for {Markup.Escape(anchor.DisplayName)}:[/] no physically eligible predicted candidates remained after deterministic size/KLD filters."); if (candidates.Count == 0) continue; @@ -569,10 +590,20 @@ private async Task RunNearBaselineReplacementAsync( LogPredictionAndRealPairLines(lowerSizeHigherDamage, upperSizeLowerDamage, predictedLowerSizeHigherDamage, predictedUpperSizeLowerDamage, "Near-baseline pair"); - long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(predictionMin, predictionMax, ct); - long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(predictedLowerSizeHigherDamage, predictedUpperSizeLowerDamage, predictionMin, predictionMax, ct); + long predictedWindowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(predictionMin, predictionMax, ct); + long predictedPoolCount = await _predictedStore.CountBetterThanLinearCandidatesAsync(predictedLowerSizeHigherDamage, predictedUpperSizeLowerDamage, predictionMin, predictionMax, ct); + long deterministicWindowRows = await CountPredictedRowsInIntersectedSizeWindowAsync(predictionMin, predictionMax, realMin, realMax, ct); + long phaseSizeEligiblePool = await _predictedStore.CountBetterThanLinearCandidatesAsync( + predictedLowerSizeHigherDamage, + predictedUpperSizeLowerDamage, + predictionMin, + predictionMax, + realMin, + realMax, + ct); + long rejectedByRealSizeWindow = Math.Max(0, predictedPoolCount - phaseSizeEligiblePool); bool diversityEligible = ShouldUseDiversityForWindow(lowerSizeHigherDamage, upperSizeLowerDamage, predictedLowerSizeHigherDamage, predictedUpperSizeLowerDamage); - fetchLimit = ResolveValidationScanLimit(attemptLimit, lineBeaters, diversityEligible); + fetchLimit = ResolveValidationScanLimit(attemptLimit, phaseSizeEligiblePool, diversityEligible); var rawCandidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync( lowerSizeHigherDamage, upperSizeLowerDamage, @@ -592,13 +623,15 @@ private async Task RunNearBaselineReplacementAsync( .ToList(); int afterBrutalityCount = brutalityAnalyses.Count(y => y.Brutality.Passed); + long deterministicEligibleCount = afterBrutalityCount; + long rejectedByOtherPhaseDeterministicRules = Math.Max(0, rawCandidates.Count - afterBrutalityCount); var rankedCandidates = brutalityAnalyses .Where(x => x.Brutality.Passed) .Select(x => AttachSelectionDiagnostics( x.Candidate, - poolSize: lineBeaters, - windowCandidateCount: windowRows, - lineBeatingCandidateCount: lineBeaters, + poolSize: phaseSizeEligiblePool, + windowCandidateCount: deterministicWindowRows, + lineBeatingCandidateCount: phaseSizeEligiblePool, fetchedCandidateCount: rawCandidates.Count, candidatesAfterBrutalityCount: afterBrutalityCount, candidateAttemptLimit: attemptLimit, @@ -640,9 +673,13 @@ private async Task RunNearBaselineReplacementAsync( WindowMinSizeBytes = realMin, WindowMaxSizeBytes = realMax, WindowSizeGiB = ToGiB(realMax > realMin ? realMax - realMin : 0), - CandidatePoolSize = lineBeaters, - WindowCandidateCount = windowRows, - LineBeatingCandidateCount = lineBeaters, + CandidatePoolSize = phaseSizeEligiblePool, + PredictedPoolCount = predictedPoolCount, + DeterministicEligibleCount = deterministicEligibleCount, + RejectedByRealSizeWindow = rejectedByRealSizeWindow, + RejectedByOtherPhaseDeterministicRules = rejectedByOtherPhaseDeterministicRules, + WindowCandidateCount = deterministicWindowRows, + LineBeatingCandidateCount = phaseSizeEligiblePool, FetchedCandidateCount = rawCandidates.Count, CandidatesAfterBrutalityCount = afterBrutalityCount, SelectedForValidationCount = candidates.Count, @@ -655,11 +692,16 @@ private async Task RunNearBaselineReplacementAsync( CandidateFamilyCount = selection.CandidateFamilyCount, SelectedFamilyCount = selection.SelectedFamilyCount, SelectedFamilyKeys = selection.SelectedFamilyKeys, + DiversitySelectionStrategy = selection.SelectionStrategy, + DiversityOverflowCount = selection.OverflowCount, + DiversitySizeFloorStartBytes = selection.SizeFloorStartBytes, + DiversitySizeFloorEndBytes = selection.SizeFloorEndBytes, TopCandidates = candidates.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), RejectedByBrutalityPreview = rejectedByBrutality, Notes = new[] { - "Near-baseline DuckDB discovery uses predicted virtual anchor windows/lines; real anchor windows/lines are used only after a candidate is benchmarked.", + "Near-baseline DuckDB discovery uses the predicted virtual KLD line, then deterministically caps candidate size to the real validation window before diversity/ladder selection. Real KLD is still used only after benchmark validation.", + $"predictedPoolCount={predictedPoolCount:N0}; phaseSizeEligiblePool={phaseSizeEligiblePool:N0}; deterministicEligibleCount={deterministicEligibleCount:N0}; rejectedByRealSizeWindow={rejectedByRealSizeWindow:N0}; rejectedByOtherPhaseDeterministicRules={rejectedByOtherPhaseDeterministicRules:N0}.", $"Brutal zone fraction={Config.SelectionNearLowerAnchorBrutalZoneFractionOfPairSpan:0.###}; required gain fraction of pair KLD gap={Config.SelectionNearAnchorRequiredKldGainFractionOfPairGap:0.###}." }.Concat(selection.Notes).ToList() }; @@ -667,8 +709,12 @@ private async Task RunNearBaselineReplacementAsync( AnsiConsole.MarkupLine( $"[grey]Near-baseline window {pairIndex + 1:N0}/{pairs.Count:N0}:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)} -> {Markup.Escape(upperSizeLowerDamage.DisplayName)} " + - $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, scanLimit={fetchLimit:N0}, scanFetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, diversity={Markup.Escape(selection.Mode)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}"); + $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, predictedPoolCount={predictedPoolCount:N0}, phaseSizeEligiblePool={phaseSizeEligiblePool:N0}, deterministicEligibleCount={deterministicEligibleCount:N0}, rejectedByRealSizeWindow={rejectedByRealSizeWindow:N0}, rejectedByOtherPhaseDeterministicRules={rejectedByOtherPhaseDeterministicRules:N0}, rows-in-window={deterministicWindowRows:N0}, beat-line={phaseSizeEligiblePool:N0}, scanLimit={fetchLimit:N0}, scanFetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, diversity={Markup.Escape(selection.Mode)}, selectionStrategy={Markup.Escape(selection.SelectionStrategy)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={candidates.Count:N0}/{attemptLimit:N0}, overflowCount={selection.OverflowCount:N0}, sizeFloorStart={selection.SizeFloorStartBytes?.ToString("N0") ?? "n/a"}"); PrintSelectedCandidateFamilySummary(candidates); + PrintSelectionLadderNotes(selection.Notes); + + if (deterministicEligibleCount == 0) + AnsiConsole.MarkupLine($"[yellow]Near-baseline window skipped builds:[/] no physically eligible candidates remained after predicted line, real size window, and deterministic brutality filters."); if (rejectedByBrutality.Count > 0) AnsiConsole.MarkupLine($"[grey] rejected by near-lower-anchor brutality preview:[/] [cyan]{rejectedByBrutality.Count:N0}[/] (see magicquant-selection-phase-diagnostics.json)"); @@ -777,10 +823,20 @@ private async Task RunInteriorSubspaceDiscoveryAsync( globalWindowIndex++; string windowLabel = $"interior {i + 1}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}"; - long windowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(predictionMin, predictionMax, ct); - long lineBeaters = await _predictedStore.CountBetterThanLinearCandidatesAsync(predictedHigherDamageSmaller, predictedLowerDamageLarger, predictionMin, predictionMax, ct); + long predictedWindowRows = await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(predictionMin, predictionMax, ct); + long predictedPoolCount = await _predictedStore.CountBetterThanLinearCandidatesAsync(predictedHigherDamageSmaller, predictedLowerDamageLarger, predictionMin, predictionMax, ct); + long deterministicWindowRows = await CountPredictedRowsInIntersectedSizeWindowAsync(predictionMin, predictionMax, realMin, realMax, ct); + long phaseSizeEligiblePool = await _predictedStore.CountBetterThanLinearCandidatesAsync( + predictedHigherDamageSmaller, + predictedLowerDamageLarger, + predictionMin, + predictionMax, + realMin, + realMax, + ct); + long rejectedByRealSizeWindow = Math.Max(0, predictedPoolCount - phaseSizeEligiblePool); bool diversityEligible = ShouldUseDiversityForWindow(pair.HigherDamageSmaller, pair.LowerDamageLarger, predictedHigherDamageSmaller, predictedLowerDamageLarger); - interiorFetchLimit = ResolveValidationScanLimit(interiorAttemptLimit, lineBeaters, diversityEligible); + interiorFetchLimit = ResolveValidationScanLimit(interiorAttemptLimit, phaseSizeEligiblePool, diversityEligible); var rawCandidates = (await _predictedStore.QueryBetterThanLinearCandidatesAsync( pair.HigherDamageSmaller, @@ -801,14 +857,16 @@ private async Task RunInteriorSubspaceDiscoveryAsync( .ToList(); int afterBrutalityCount = brutalityAnalyses.Count(y => y.Brutality.Passed); + long deterministicEligibleCount = afterBrutalityCount; + long rejectedByOtherPhaseDeterministicRules = Math.Max(0, rawCandidates.Count - afterBrutalityCount); var rankedCandidates = brutalityAnalyses .Where(x => x.Brutality.Passed) .Select(x => AttachSelectionDiagnostics( x.Candidate, - poolSize: lineBeaters, - windowCandidateCount: windowRows, - lineBeatingCandidateCount: lineBeaters, + poolSize: phaseSizeEligiblePool, + windowCandidateCount: deterministicWindowRows, + lineBeatingCandidateCount: phaseSizeEligiblePool, fetchedCandidateCount: rawCandidates.Count, candidatesAfterBrutalityCount: afterBrutalityCount, candidateAttemptLimit: interiorAttemptLimit, @@ -852,9 +910,13 @@ private async Task RunInteriorSubspaceDiscoveryAsync( WindowMinSizeBytes = realMin, WindowMaxSizeBytes = realMax, WindowSizeGiB = ToGiB(realMax > realMin ? realMax - realMin : 0), - CandidatePoolSize = lineBeaters, - WindowCandidateCount = windowRows, - LineBeatingCandidateCount = lineBeaters, + CandidatePoolSize = phaseSizeEligiblePool, + PredictedPoolCount = predictedPoolCount, + DeterministicEligibleCount = deterministicEligibleCount, + RejectedByRealSizeWindow = rejectedByRealSizeWindow, + RejectedByOtherPhaseDeterministicRules = rejectedByOtherPhaseDeterministicRules, + WindowCandidateCount = deterministicWindowRows, + LineBeatingCandidateCount = phaseSizeEligiblePool, FetchedCandidateCount = rawCandidates.Count, CandidatesAfterBrutalityCount = afterBrutalityCount, SelectedForValidationCount = kept.Count, @@ -867,15 +929,27 @@ private async Task RunInteriorSubspaceDiscoveryAsync( CandidateFamilyCount = selection.CandidateFamilyCount, SelectedFamilyCount = selection.SelectedFamilyCount, SelectedFamilyKeys = selection.SelectedFamilyKeys, + DiversitySelectionStrategy = selection.SelectionStrategy, + DiversityOverflowCount = selection.OverflowCount, + DiversitySizeFloorStartBytes = selection.SizeFloorStartBytes, + DiversitySizeFloorEndBytes = selection.SizeFloorEndBytes, TopCandidates = kept.Take(DiagnosticPreviewDisplayCount).Select(ToCandidatePreviewLog).ToList(), RejectedByBrutalityPreview = rejectedByBrutality, - Notes = new[] { "Interior DuckDB discovery uses predicted virtual anchor windows/lines; real anchor windows/lines are used only after benchmark validation." }.Concat(selection.Notes).ToList() + Notes = new[] + { + "Interior DuckDB discovery uses the predicted virtual nonlinear KLD line/window, then deterministically caps candidate size to the real interior slice before diversity/ladder selection. Real KLD is still used only after benchmark validation.", + $"predictedPoolCount={predictedPoolCount:N0}; phaseSizeEligiblePool={phaseSizeEligiblePool:N0}; deterministicEligibleCount={deterministicEligibleCount:N0}; rejectedByRealSizeWindow={rejectedByRealSizeWindow:N0}; rejectedByOtherPhaseDeterministicRules={rejectedByOtherPhaseDeterministicRules:N0}." + }.Concat(selection.Notes).ToList() }); AnsiConsole.MarkupLine( $"[grey]Interior window {globalWindowIndex:N0}/{Math.Max(estimatedWindowCount, globalWindowIndex):N0}:[/] {Markup.Escape(pair.HigherDamageSmaller.DisplayName)} -> {Markup.Escape(pair.LowerDamageLarger.DisplayName)} " + - $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, rows-in-window={windowRows:N0}, beat-line={lineBeaters:N0}, scanLimit={interiorFetchLimit:N0}, scanFetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, diversity={Markup.Escape(selection.Mode)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={kept.Count:N0}/{interiorAttemptLimit:N0}"); + $"| pred-window={predictionMin:N0}..{predictionMax:N0}, real-window={realMin:N0}..{realMax:N0}, predictedPoolCount={predictedPoolCount:N0}, phaseSizeEligiblePool={phaseSizeEligiblePool:N0}, deterministicEligibleCount={deterministicEligibleCount:N0}, rejectedByRealSizeWindow={rejectedByRealSizeWindow:N0}, rejectedByOtherPhaseDeterministicRules={rejectedByOtherPhaseDeterministicRules:N0}, rows-in-window={deterministicWindowRows:N0}, beat-line={phaseSizeEligiblePool:N0}, scanLimit={interiorFetchLimit:N0}, scanFetched={rawCandidates.Count:N0}, after-brutality={afterBrutalityCount:N0}, diversity={Markup.Escape(selection.Mode)}, selectionStrategy={Markup.Escape(selection.SelectionStrategy)}, candidateFamilies={selection.CandidateFamilyCount:N0}, selectedFamilies={selection.SelectedFamilyCount:N0}, selected={kept.Count:N0}/{interiorAttemptLimit:N0}, overflowCount={selection.OverflowCount:N0}, sizeFloorStart={selection.SizeFloorStartBytes?.ToString("N0") ?? "n/a"}"); PrintSelectedCandidateFamilySummary(kept); + PrintSelectionLadderNotes(selection.Notes); + + if (deterministicEligibleCount == 0) + AnsiConsole.MarkupLine($"[yellow]Interior window skipped builds:[/] no physically eligible candidates remained after predicted nonlinear line, real size slice, and deterministic brutality filters."); realCursor = realMax; predictionCursor = predictionMax; @@ -1110,7 +1184,8 @@ private static ValidationCandidateSelectionResult SelectValidationCandidates( Candidates = Array.Empty(), Mode = Config.SelectionDiversifyValidationCandidates ? diversityEligible ? "enabled-empty" : "disabled-low-bit-only" : "disabled", DiversityEnabled = Config.SelectionDiversifyValidationCandidates && diversityEligible, - Notes = ["No candidates survived the prediction/brutality filters for this window."] + SelectionStrategy = "none", + Notes = ["No deterministic-eligible candidates survived the prediction/brutality filters for this window; no phase-original primary candidate exists."] }; } @@ -1128,128 +1203,340 @@ private static ValidationCandidateSelectionResult SelectValidationCandidates( }) .ToList(); - var families = entries - .GroupBy(x => x.Signature.Key, StringComparer.Ordinal) - .Select((g, familyIndex) => new CandidateTheoryFamily - { - Key = g.Key, - Display = g.First().Signature.Display, - Rank = familyIndex + 1, - Members = g.OrderBy(x => x.RawRank).ToList() - }) - .OrderBy(x => x.Members[0].RawRank) - .ToList(); + var families = BuildCandidateTheoryFamilies(entries); + var originalPrimary = entries[0]; + string originalPrimaryConfigKey = TensorConfigIdentity.ToKey(originalPrimary.Candidate.Prediction.Config); - for (int familyIndex = 0; familyIndex < families.Count; familyIndex++) - { - families[familyIndex].Rank = familyIndex + 1; - for (int memberIndex = 0; memberIndex < families[familyIndex].Members.Count; memberIndex++) - families[familyIndex].Members[memberIndex].MemberRank = memberIndex + 1; - } + var originalOrderNotes = BuildOriginalPhaseOrderPreviewNotes(phaseName, rankedCandidates, previewLimit: 10); + PrintOriginalPhaseOrderPreview(phaseName, higherDamageSmaller, lowerDamageLarger, rankedCandidates, previewLimit: 10); bool canDiversify = Config.SelectionDiversifyValidationCandidates && diversityEligible && rankedCandidates.Count > attemptLimit; - if (!canDiversify) + if (!canDiversify || attemptLimit == 1) { string mode = Config.SelectionDiversifyValidationCandidates ? diversityEligible - ? "not-needed" + ? rankedCandidates.Count <= attemptLimit ? "not-needed" : "not-needed" : "disabled-low-bit-only" : "disabled"; var selectedWithoutDiversity = entries .Take(attemptLimit) - .Select((entry, index) => DecorateSelectedCandidate(entry, families, index + 1, mode)) + .Select((entry, index) => DecorateSelectedCandidate( + entry, + families, + index + 1, + index == 0 ? "primary-original-phase-order" : mode)) .ToList(); - var notesWithoutDiversity = new List + AssertPrimaryPreserved(phaseName, originalPrimaryConfigKey, selectedWithoutDiversity); + + var nonDiversityNotes = new List { - BuildDiversityNote(mode, phaseName, rankedCandidates.Count, attemptLimit, families.Count, selectedWithoutDiversity.Count) + BuildDiversityNote(mode, phaseName, rankedCandidates.Count, attemptLimit, families.Count, selectedWithoutDiversity.Count, overflowCount: 0), + $"primarySelectionSource=original-phase-order; primaryRawRankBeforeDiversity={originalPrimary.RawRank:N0}; primaryConfigKey={originalPrimaryConfigKey}; primaryPredictedSizeBytes={originalPrimary.Candidate.Prediction.PredictedSizeBytes:N0}; primaryEffectivePredictedKld={GetEffectivePredictedKld(originalPrimary.Candidate):0.000000}; primaryPredictionRank={FormatNullableRank(originalPrimary.Candidate.Prediction.PredictedRank)}; primaryWasExcluded=false.", + "fallbackSelectionStrategy=not-applied; diversity ladder did not run because the candidate count did not exceed the attempt limit, diversity was disabled, or only one attempt was allowed." }; - AddDiversityFamilyGranularityWarning(notesWithoutDiversity, families.Count, rankedCandidates.Count); + nonDiversityNotes.AddRange(originalOrderNotes); return new ValidationCandidateSelectionResult { Candidates = selectedWithoutDiversity, Mode = mode, DiversityEnabled = false, + SelectionStrategy = "original-phase-order", CandidateFamilyCount = families.Count, SelectedFamilyCount = selectedWithoutDiversity.Select(x => x.CandidateTheoryFamilyKey).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).Count(), SelectedFamilyKeys = selectedWithoutDiversity.Select(x => x.CandidateTheoryFamilyKey).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).ToList(), - Notes = notesWithoutDiversity + Notes = nonDiversityNotes }; } var selectedEntries = new List(); - var selectedKeys = new HashSet(StringComparer.Ordinal); - void AddEntry(CandidateFamilyEntry entry) + var selectedConfigKeys = new HashSet(StringComparer.Ordinal); + var selectedFamilyKeys = new HashSet(StringComparer.Ordinal); + var notesForFallback = new List(); + int overflowCount = 0; + int familyExhaustionCount = 0; + ulong? sizeFloorStart = null; + ulong sizeFloor = 0; + + bool AddEntry(CandidateFamilyEntry entry, string selectionMode, ulong? previousSizeFloor) { - string key = TensorConfigIdentity.ToKey(entry.Candidate.Prediction.Config); - if (!selectedKeys.Add(key)) - return; + string configKey = TensorConfigIdentity.ToKey(entry.Candidate.Prediction.Config); + if (!selectedConfigKeys.Add(configKey)) + return false; + + entry.SelectionMode = selectionMode; + entry.PreviousSizeFloorBytes = previousSizeFloor; + entry.SizeDeltaVsFloorBytes = previousSizeFloor.HasValue + ? unchecked((long)entry.Candidate.Prediction.PredictedSizeBytes - (long)previousSizeFloor.Value) + : null; + entry.EffectivePredictedKld = GetEffectivePredictedKld(entry.Candidate); + selectedEntries.Add(entry); + selectedFamilyKeys.Add(entry.Signature.Key); + return true; } - AddEntry(entries[0]); + // Critical invariant: the phase's original deterministic-eligible primary owns attempt #1. + // Diversity and the size ladder are fallback ordering only and must never re-rank this row. + AddEntry(originalPrimary, "primary-original-phase-order", previousSizeFloor: null); + sizeFloor = originalPrimary.Candidate.Prediction.PredictedSizeBytes; + sizeFloorStart = sizeFloor; - for (int memberRank = 1; selectedEntries.Count < attemptLimit; memberRank++) + while (selectedEntries.Count < attemptLimit) { - bool addedThisRound = false; - foreach (var family in families) + ulong previousFloor = sizeFloor; + + var unusedAtOrAbove = entries + .Skip(1) + .Where(x => IsSelectable(x, selectedConfigKeys) && !selectedFamilyKeys.Contains(x.Signature.Key) && x.Candidate.Prediction.PredictedSizeBytes >= sizeFloor) + .OrderBy(GetEffectivePredictedKld) + .ThenByDescending(x => x.Candidate.Prediction.PredictedSizeBytes) + .ThenBy(x => x.RawRank) + .ToList(); + + var next = unusedAtOrAbove.FirstOrDefault(); + if (next != null && AddEntry(next, "fallback-ladder-unused-family", previousFloor)) { - var member = family.Members.FirstOrDefault(x => x.MemberRank == memberRank); - if (member == null) + sizeFloor = Math.Max(sizeFloor, next.Candidate.Prediction.PredictedSizeBytes); + continue; + } + + int unusedFamiliesRemaining = families.Count(f => !selectedFamilyKeys.Contains(f.Key) && f.Members.Any(m => IsSelectable(m, selectedConfigKeys))); + int unusedBelowCount = entries.Skip(1).Count(x => IsSelectable(x, selectedConfigKeys) && !selectedFamilyKeys.Contains(x.Signature.Key) && x.Candidate.Prediction.PredictedSizeBytes < sizeFloor); + + if (unusedFamiliesRemaining > 0) + { + overflowCount++; + notesForFallback.Add( + $"Diversity ladder overflow: attemptSlot={selectedEntries.Count + 1:N0}; sizeFloorBytes={sizeFloor:N0}; unusedFamiliesRemaining={unusedFamiliesRemaining:N0}; candidatesAtOrAboveFloor=0; overflowCandidatesBelowFloor={unusedBelowCount:N0}; reason=no-unused-family-candidate-at-or-above-size-floor."); + + next = entries + .Skip(1) + .Where(x => IsSelectable(x, selectedConfigKeys) && !selectedFamilyKeys.Contains(x.Signature.Key) && x.Candidate.Prediction.PredictedSizeBytes < sizeFloor) + .OrderByDescending(x => x.Candidate.Prediction.PredictedSizeBytes) + .ThenBy(GetEffectivePredictedKld) + .ThenBy(x => x.RawRank) + .FirstOrDefault(); + + if (next != null && AddEntry(next, "fallback-overflow-unused-family", previousFloor)) + { + // Deliberately monotone: an overflow candidate below the floor cannot drag the safety floor down. + sizeFloor = Math.Max(sizeFloor, next.Candidate.Prediction.PredictedSizeBytes); continue; + } + } - int before = selectedEntries.Count; - AddEntry(member); - addedThisRound |= selectedEntries.Count > before; + familyExhaustionCount++; + notesForFallback.Add( + $"Diversity ladder family exhaustion: attemptSlot={selectedEntries.Count + 1:N0}; selectedFamilies={selectedFamilyKeys.Count:N0}; candidateFamilies={families.Count:N0}; reason=all-distinct-families-exhausted."); - if (selectedEntries.Count >= attemptLimit) - break; + next = entries + .Skip(1) + .Where(x => IsSelectable(x, selectedConfigKeys) && x.Candidate.Prediction.PredictedSizeBytes >= sizeFloor) + .OrderBy(GetEffectivePredictedKld) + .ThenByDescending(x => x.Candidate.Prediction.PredictedSizeBytes) + .ThenBy(x => x.RawRank) + .FirstOrDefault(); + + if (next != null && AddEntry(next, "fallback-ladder-used-family", previousFloor)) + { + sizeFloor = Math.Max(sizeFloor, next.Candidate.Prediction.PredictedSizeBytes); + continue; } - if (!addedThisRound) - break; + int belowFloorCount = entries.Skip(1).Count(x => IsSelectable(x, selectedConfigKeys) && x.Candidate.Prediction.PredictedSizeBytes < sizeFloor); + overflowCount++; + notesForFallback.Add( + $"Diversity ladder overflow: attemptSlot={selectedEntries.Count + 1:N0}; sizeFloorBytes={sizeFloor:N0}; unusedFamiliesRemaining=0; candidatesAtOrAboveFloor=0; overflowCandidatesBelowFloor={belowFloorCount:N0}; reason=no-remaining-candidate-at-or-above-size-floor."); + + next = entries + .Skip(1) + .Where(x => IsSelectable(x, selectedConfigKeys) && x.Candidate.Prediction.PredictedSizeBytes < sizeFloor) + .OrderByDescending(x => x.Candidate.Prediction.PredictedSizeBytes) + .ThenBy(GetEffectivePredictedKld) + .ThenBy(x => x.RawRank) + .FirstOrDefault(); + + if (next != null && AddEntry(next, "fallback-overflow-used-family", previousFloor)) + { + // Deliberately monotone: do not lower the floor after overflow. + sizeFloor = Math.Max(sizeFloor, next.Candidate.Prediction.PredictedSizeBytes); + continue; + } + + break; } var selected = selectedEntries .Take(attemptLimit) - .Select((entry, index) => DecorateSelectedCandidate(entry, families, index + 1, "enabled")) + .Select((entry, index) => DecorateSelectedCandidate(entry, families, index + 1, entry.SelectionMode)) .ToList(); + AssertPrimaryPreserved(phaseName, originalPrimaryConfigKey, selected); + bool exhaustedDistinctFamilies = selected.Count > selected.Select(x => x.CandidateTheoryFamilyKey).Distinct(StringComparer.Ordinal).Count(); var notes = new List { - BuildDiversityNote("enabled", phaseName, rankedCandidates.Count, attemptLimit, families.Count, selected.Count) + BuildDiversityNote("enabled", phaseName, rankedCandidates.Count, attemptLimit, families.Count, selected.Count, overflowCount), + $"primarySelectionSource=original-phase-order; primaryRawRankBeforeDiversity={originalPrimary.RawRank:N0}; primaryConfigKey={originalPrimaryConfigKey}; primaryPredictedSizeBytes={originalPrimary.Candidate.Prediction.PredictedSizeBytes:N0}; primaryEffectivePredictedKld={GetEffectivePredictedKld(originalPrimary.Candidate):0.000000}; primaryPredictionRank={FormatNullableRank(originalPrimary.Candidate.Prediction.PredictedRank)}; primaryWasExcluded=false.", + $"fallbackSelectionStrategy=family-size-ladder; fallbackCount={Math.Max(0, selected.Count - 1):N0}; sizeFloorStartBytes={sizeFloorStart.GetValueOrDefault():N0}; sizeFloorEndBytes={sizeFloor:N0}; overflowCount={overflowCount:N0}; familyExhaustionCount={familyExhaustionCount:N0}." }; + notes.AddRange(originalOrderNotes); + notes.AddRange(notesForFallback); + if (exhaustedDistinctFamilies) - notes.Add("Distinct candidate theory families were exhausted before the attempt limit; remaining slots were filled round-robin by the next-best members of already-selected families."); - AddDiversityFamilyGranularityWarning(notes, families.Count, rankedCandidates.Count); + notes.Add("Distinct candidate theory families were exhausted before the attempt limit; remaining fallback slots were filled from already-selected families using the same monotone size-floor ladder."); + if (overflowCount > 0) + notes.Add("Diversity ladder overflow occurred: at least one selected retry was below the monotone size floor because no same/larger alternative was available in the preferred family bucket."); + if (selected.Count > 0 && overflowCount >= Math.Max(1, selected.Count / 2)) + notes.Add("WARNING: Diversity ladder overflow selected many candidates below the size floor; candidate pool may not contain enough safer alternatives."); + if (families.Count >= rankedCandidates.Count * 0.90 && rankedCandidates.Count >= 25) + notes.Add("WARNING: Diversity warning: candidate family key may be too fine-grained; most scanned candidates formed unique families."); return new ValidationCandidateSelectionResult { Candidates = selected, Mode = "enabled", DiversityEnabled = true, + SelectionStrategy = "original-primary-plus-family-size-ladder-fallbacks", CandidateFamilyCount = families.Count, SelectedFamilyCount = selected.Select(x => x.CandidateTheoryFamilyKey).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).Count(), SelectedFamilyKeys = selected.Select(x => x.CandidateTheoryFamilyKey).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).ToList(), + OverflowCount = overflowCount, + SizeFloorStartBytes = sizeFloorStart, + SizeFloorEndBytes = sizeFloor, Notes = notes }; } + private static List BuildCandidateTheoryFamilies(IReadOnlyList entries) + { + var families = entries + .GroupBy(x => x.Signature.Key, StringComparer.Ordinal) + .Select(g => new CandidateTheoryFamily + { + Key = g.Key, + Display = g.First().Signature.Display, + Members = g.OrderBy(x => x.RawRank).ToList() + }) + .OrderBy(x => x.Members[0].RawRank) + .ToList(); + + for (int familyIndex = 0; familyIndex < families.Count; familyIndex++) + { + families[familyIndex].Rank = familyIndex + 1; + foreach (var member in families[familyIndex].Members) + member.Family = families[familyIndex]; + + for (int memberIndex = 0; memberIndex < families[familyIndex].Members.Count; memberIndex++) + families[familyIndex].Members[memberIndex].MemberRank = memberIndex + 1; + } + + return families; + } + + private static void AssertPrimaryPreserved( + string phaseName, + string originalPrimaryConfigKey, + IReadOnlyList selected) + { + if (selected.Count == 0) + return; + + string selectedPrimaryConfigKey = TensorConfigIdentity.ToKey(selected[0].Prediction.Config); + if (string.Equals(originalPrimaryConfigKey, selectedPrimaryConfigKey, StringComparison.Ordinal)) + return; + + string message = + $"CRITICAL SELECTION INVARIANT FAILED: {phaseName} fallback ordering changed attempt #1. " + + $"originalPrimary={originalPrimaryConfigKey}; selectedPrimary={selectedPrimaryConfigKey}. " + + "Family diversity / size ladder may only reorder fallback attempts after the phase-original primary candidate."; + + AnsiConsole.MarkupLine($"[red]{Markup.Escape(message)}[/]"); + throw new InvalidOperationException(message); + } + + private static IReadOnlyList BuildOriginalPhaseOrderPreviewNotes( + string phaseName, + IReadOnlyList rankedCandidates, + int previewLimit) + { + var notes = new List + { + $"originalOrderPreview phase={phaseName}; showingTop={Math.Min(previewLimit, rankedCandidates.Count):N0}/{rankedCandidates.Count:N0}; these rows are in deterministic-eligible original phase order before fallback diversity." + }; + + int order = 0; + foreach (var candidate in rankedCandidates.Take(previewLimit)) + { + order++; + notes.Add( + $"originalOrderPreview rawOrder={order:N0}; config={TensorConfigIdentity.ToKey(candidate.Prediction.Config)}; size={candidate.Prediction.PredictedSizeBytes:N0}; predKld={GetEffectivePredictedKld(candidate):0.000000}; predictionRank={FormatNullableRank(candidate.Prediction.PredictedRank)}."); + } + + return notes; + } + + private static void PrintOriginalPhaseOrderPreview( + string phaseName, + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger, + IReadOnlyList rankedCandidates, + int previewLimit) + { + int count = Math.Min(previewLimit, rankedCandidates.Count); + if (count == 0) + return; + + string anchorText = ReferenceEquals(higherDamageSmaller, lowerDamageLarger) || + TensorConfigIdentity.ToKey(higherDamageSmaller.Config) == TensorConfigIdentity.ToKey(lowerDamageLarger.Config) + ? higherDamageSmaller.DisplayName + : $"{higherDamageSmaller.DisplayName} -> {lowerDamageLarger.DisplayName}"; + + AnsiConsole.MarkupLine($"[grey]Original phase order preview ({Markup.Escape(phaseName)}) for {Markup.Escape(anchorText)}:[/] [cyan]{count:N0}/{rankedCandidates.Count:N0}[/]"); + int order = 0; + foreach (var candidate in rankedCandidates.Take(previewLimit)) + { + order++; + AnsiConsole.MarkupLine( + $"[grey] rawOrder={order:N0} config={Markup.Escape(TensorConfigIdentity.ToKey(candidate.Prediction.Config))} " + + $"size={candidate.Prediction.PredictedSizeBytes:N0} predKld={GetEffectivePredictedKld(candidate):0.000000} predictionRank={Markup.Escape(FormatNullableRank(candidate.Prediction.PredictedRank))}[/]"); + } + } + + private static string FormatNullableRank(ulong? rank) => rank.HasValue ? rank.Value.ToString("N0") : "n/a"; + + private static bool IsSelectable(CandidateFamilyEntry entry, HashSet selectedConfigKeys) + { + string key = TensorConfigIdentity.ToKey(entry.Candidate.Prediction.Config); + return !selectedConfigKeys.Contains(key); + } + private static HybridSelectionCandidate DecorateSelectedCandidate( CandidateFamilyEntry entry, IReadOnlyList families, int attemptOrder, string diversityMode) { - var family = families.First(x => string.Equals(x.Key, entry.Signature.Key, StringComparison.Ordinal)); + var family = entry.Family ?? families.First(x => string.Equals(x.Key, entry.Signature.Key, StringComparison.Ordinal)); int memberRank = entry.MemberRank > 0 ? entry.MemberRank : Math.Max(1, family.Members.FindIndex(x => ReferenceEquals(x, entry)) + 1); + double effectivePredictedKld = double.IsNaN(entry.EffectivePredictedKld) ? GetEffectivePredictedKld(entry.Candidate) : entry.EffectivePredictedKld; + string previousFloorText = entry.PreviousSizeFloorBytes.HasValue ? entry.PreviousSizeFloorBytes.Value.ToString("N0") : "n/a"; + string deltaText = entry.SizeDeltaVsFloorBytes.HasValue ? entry.SizeDeltaVsFloorBytes.Value.ToString("N0") : "n/a"; + + string selectionStrategy = diversityMode switch + { + "primary-original-phase-order" => "original-phase-primary", + "disabled" or "not-needed" or "disabled-low-bit-only" => "original-phase-order", + _ => "family-size-ladder-fallback" + }; + var notes = entry.Candidate.CandidateSelectionNotes .Concat(new[] { - $"diversity={diversityMode}; rawRank={entry.RawRank}; familyRank={family.Rank}; familyMemberRank={memberRank}; familyKey={entry.Signature.Key}; familyDisplay={entry.Signature.Display}" + $"diversity={diversityMode}; selectionStrategy={selectionStrategy}; rawRank={entry.RawRank}; familyRank={family.Rank}; familyMemberRank={memberRank}; previousSizeFloorBytes={previousFloorText}; selectedSizeBytes={entry.Candidate.Prediction.PredictedSizeBytes:N0}; sizeDeltaVsFloorBytes={deltaText}; effectivePredictedKld={effectivePredictedKld:0.000000}; familyKey={entry.Signature.Key}; familyDisplay={entry.Signature.Display}" }) .ToList(); @@ -1328,17 +1615,12 @@ private static CandidateTheorySignature BuildCandidateTheorySignature( { var config = candidate.Prediction.Config; var baseQuant = BaselineQuants.FromId(config.BaseQuant); - int anchorBit = ResolveCandidateTheoryAnchorBit(higherDamageSmaller, lowerDamageLarger, higherDamagePredictionAnchor, lowerDamagePredictionAnchor); - - var coarseRisk = new List(); - var coarseProtected = new List(); - var coarseSensitive = new List(); - - var displayRisk = new List(); - var displayProtected = new List(); - var displayExternal = new List(); - var displaySensitive = new List(); + int anchorBit = Math.Min(higherDamageSmaller.Quant.BaseQuant.BitRange, lowerDamageLarger.Quant.BaseQuant.BitRange); + var lowRisk = new List(); + var protectedGroups = new List(); + var external = new List(); + var sensitivity = new List(); int sixPlus = 0; int five = 0; int four = 0; @@ -1347,9 +1629,7 @@ private static CandidateTheorySignature BuildCandidateTheorySignature( foreach (var group in activeGroups.OrderBy(x => x.UniqueId)) { var effective = GetEffectiveGroupBaseline(config, group); - string exactPlacement = $"{group.ShortCode}={effective.Names[0]}"; - string coarsePlacement = $"{group.ShortCode}={ToCoarseBitBand(effective.BitRange)}"; - int bitDeltaFromAnchor = effective.BitRange - anchorBit; + string placement = $"{group.ShortCode}={effective.Names[0]}"; if (effective.BitRange >= 6) sixPlus++; @@ -1360,152 +1640,41 @@ private static CandidateTheorySignature BuildCandidateTheorySignature( else threeOrLess++; - bool severeRisk = effective.BitRange <= 3 || effective.BitRange <= anchorBit - 1; - bool majorProtection = (anchorBit <= 5 && effective.BitRange >= 6) || effective.BitRange >= anchorBit + 1; - bool externalOrCustom = effective.IsCustomBaseline || effective.IsExternalRepositoryBaseline; + if (effective.BitRange <= anchorBit - 1 || effective.BitRange <= 3) + lowRisk.Add(placement); - if (severeRisk) - { - coarseRisk.Add(coarsePlacement); - displayRisk.Add(exactPlacement); - } - - if (majorProtection) - { - coarseProtected.Add(coarsePlacement); - displayProtected.Add(exactPlacement); - } + if ((anchorBit <= 5 && effective.BitRange >= 6) || effective.BitRange >= anchorBit + 1) + protectedGroups.Add(placement); - // External/custom identity is valuable in diagnostics, but it must not make every - // UD-vs-standard sibling its own selection family. The coarse key intentionally - // relies on the strategic bit-band role; the exact external name stays in Display. - if (externalOrCustom) - displayExternal.Add(exactPlacement); + if (effective.IsCustomBaseline || effective.IsExternalRepositoryBaseline) + external.Add(placement); - // Sensitive groups are allowed to influence the coarse key only when the placement is - // a real strategy shift, not merely a Q4 sibling spelling such as IQ4_NL vs Q4_K_M. if (IsHighSensitivityGroup(group) && effective.UniqueId != baseQuant.UniqueId) - { - displaySensitive.Add(exactPlacement); - if (!severeRisk && !majorProtection && Math.Abs(bitDeltaFromAnchor) > 1) - coarseSensitive.Add(coarsePlacement); - } + sensitivity.Add(placement); } - string anchorDisplay = $"anchor={higherDamagePredictionAnchor?.DisplayName ?? higherDamageSmaller.DisplayName}->{lowerDamagePredictionAnchor?.DisplayName ?? lowerDamageLarger.DisplayName}@{anchorBit}b"; - string anchorKey = $"anchor={ToAnchorBand(anchorBit)}"; + string anchorBand = $"anchor={higherDamagePredictionAnchor?.DisplayName ?? higherDamageSmaller.DisplayName}->{lowerDamagePredictionAnchor?.DisplayName ?? lowerDamageLarger.DisplayName}@{anchorBit}b"; string bulk = $"bulk:6p={sixPlus},5={five},4={four},3m={threeOrLess}"; - var keyComponents = new List + var components = new List { + anchorBand, $"base={baseQuant.Names[0]}", - anchorKey, bulk }; - AddSortedComponent(keyComponents, "risk", coarseRisk); - AddSortedComponent(keyComponents, "protect", coarseProtected); - AddSortedComponent(keyComponents, "sensitiveShift", coarseSensitive); + if (lowRisk.Count > 0) + components.Add("risk:" + string.Join(",", lowRisk.OrderBy(x => x, StringComparer.Ordinal))); + if (protectedGroups.Count > 0) + components.Add("protect:" + string.Join(",", protectedGroups.OrderBy(x => x, StringComparer.Ordinal))); + if (external.Count > 0) + components.Add("external:" + string.Join(",", external.OrderBy(x => x, StringComparer.Ordinal))); + if (sensitivity.Count > 0) + components.Add("sensitive:" + string.Join(",", sensitivity.OrderBy(x => x, StringComparer.Ordinal))); - var displayComponents = new List - { - $"base={baseQuant.Names[0]}", - anchorDisplay, - bulk - }; - - AddSortedComponent(displayComponents, "risk", displayRisk); - AddSortedComponent(displayComponents, "protect", displayProtected); - AddSortedComponent(displayComponents, "external", displayExternal); - AddSortedComponent(displayComponents, "sensitive", displaySensitive); - - return new CandidateTheorySignature - { - Key = string.Join("|", keyComponents), - Display = string.Join("|", displayComponents) - }; - } - - private static int ResolveCandidateTheoryAnchorBit( - BenchmarkSnapshotRecord higherDamageSmaller, - BenchmarkSnapshotRecord lowerDamageLarger, - PredictedAnchorRow? higherDamagePredictionAnchor, - PredictedAnchorRow? lowerDamagePredictionAnchor) - { - var bits = new List - { - higherDamageSmaller.Quant.BaseQuant.BitRange, - lowerDamageLarger.Quant.BaseQuant.BitRange - }; - - AddPredictedAnchorBit(bits, higherDamagePredictionAnchor); - AddPredictedAnchorBit(bits, lowerDamagePredictionAnchor); - return bits.Count == 0 ? 4 : bits.Min(); - } - - private static void AddPredictedAnchorBit(List bits, PredictedAnchorRow? anchor) - { - if (anchor == null) - return; - - try - { - bits.Add(BaselineQuants.FromId(anchor.RuntimeBaselineId).BitRange); - } - catch - { - // Predicted anchor metadata is diagnostic here; real benchmark anchors remain the fallback. - } - } - - private static string ToAnchorBand(int bitRange) - { - if (bitRange <= 3) - return "Q3ish"; - if (bitRange == 4) - return "Q4ish"; - if (bitRange == 5) - return "Q5ish"; - if (bitRange == 6) - return "Q6ish"; - return "Q8ish"; - } - - private static string ToCoarseBitBand(int bitRange) - { - if (bitRange >= 6) - return "6p"; - if (bitRange == 5) - return "5bit"; - if (bitRange == 4) - return "4bit"; - return "3bit"; - } - - private static void AddSortedComponent(List components, string label, IEnumerable values) - { - var distinct = values - .Where(x => !string.IsNullOrWhiteSpace(x)) - .Distinct(StringComparer.Ordinal) - .OrderBy(x => x, StringComparer.Ordinal) - .ToList(); - - if (distinct.Count > 0) - components.Add($"{label}:" + string.Join(",", distinct)); - } - - private static void AddDiversityFamilyGranularityWarning(List notes, int candidateFamilyCount, int scannedCandidateCount) - { - if (!Config.SelectionDiversifyValidationCandidates) - return; - - if (scannedCandidateCount < 25) - return; - - if (candidateFamilyCount < scannedCandidateCount * 0.90d) - return; - - notes.Add("Diversity warning: candidate family key may be too fine-grained; most scanned candidates formed unique families."); + string key = string.Join("|", components); + string display = string.Join("|", components.Where(x => !x.StartsWith("anchor=", StringComparison.Ordinal))); + return new CandidateTheorySignature { Key = key, Display = display }; } private static BaselineQuants GetEffectiveGroupBaseline(TensorConfig config, TensorGroup group) @@ -1542,15 +1711,38 @@ private static bool IsHighSensitivityGroup(TensorGroup group) => group.UniqueId == TReg.AttnKV.UniqueId || group.UniqueId == TReg.FfnDown.UniqueId; - private static string BuildDiversityNote(string mode, string phaseName, int candidateCount, int attemptLimit, int familyCount, int selectedCount) => + + private static double GetEffectivePredictedKld(CandidateFamilyEntry entry) => GetEffectivePredictedKld(entry.Candidate); + + private static double GetEffectivePredictedKld(HybridSelectionCandidate candidate) => + double.IsNaN(candidate.Prediction.PredictedKld) ? double.PositiveInfinity : candidate.Prediction.PredictedKld; + + private static string BuildDiversityNote(string mode, string phaseName, int candidateCount, int attemptLimit, int familyCount, int selectedCount, int overflowCount) => mode switch { - "enabled" => $"Diversity enabled for {phaseName}: selected {selectedCount:N0}/{attemptLimit:N0} validation attempts from {familyCount:N0} candidate theory families across {candidateCount:N0} filtered scan candidates; raw top prediction is preserved as attempt 1.", + "enabled" => $"Diversity enabled for {phaseName}: selectionStrategy=family-size-ladder; selected {selectedCount:N0}/{attemptLimit:N0} validation attempts from {familyCount:N0} candidate theory families across {candidateCount:N0} filtered scan candidates; phase-original primary candidate is preserved as attempt 1; only fallback attempts prefer same/larger predicted size before explicit overflow; overflowCount={overflowCount:N0}.", "not-needed" => $"Diversity not needed for {phaseName}: filtered candidate count {candidateCount:N0} <= attempt limit {attemptLimit:N0}; candidates kept in raw predicted order.", "disabled-low-bit-only" => $"Diversity skipped for {phaseName}: candidate_selection.diversity_low_bit_only=true and this anchor/window was not Q4-ish or below.", _ => $"Diversity disabled for {phaseName}; candidates kept in raw predicted order." }; + private static void PrintSelectionLadderNotes(IReadOnlyList notes) + { + foreach (var note in notes) + { + if (note.Contains("WARNING", StringComparison.OrdinalIgnoreCase) || + note.Contains("Diversity ladder overflow", StringComparison.OrdinalIgnoreCase) || + note.Contains("Diversity ladder family exhaustion", StringComparison.OrdinalIgnoreCase)) + { + AnsiConsole.MarkupLine($"[yellow] {Markup.Escape(note)}[/]"); + } + else if (note.Contains("selectionStrategy=family-size-ladder", StringComparison.OrdinalIgnoreCase)) + { + AnsiConsole.MarkupLine($"[grey] {Markup.Escape(note)}[/]"); + } + } + } + private static void PrintSelectedCandidateFamilySummary(IReadOnlyList candidates) { foreach (var candidate in candidates.Take(DiagnosticPreviewDisplayCount)) @@ -1558,14 +1750,35 @@ private static void PrintSelectedCandidateFamilySummary(IReadOnlyList x.Contains("selectionStrategy=family-size-ladder", StringComparison.Ordinal)) ?? string.Empty; + string previousFloor = ExtractSelectionNoteValue(ladderNote, "previousSizeFloorBytes") ?? "n/a"; + string deltaVsFloor = ExtractSelectionNoteValue(ladderNote, "sizeDeltaVsFloorBytes") ?? "n/a"; + string effectiveKld = ExtractSelectionNoteValue(ladderNote, "effectivePredictedKld") ?? candidate.Prediction.PredictedKld.ToString("0.000000"); + AnsiConsole.MarkupLine( $"[grey] selected attempt={candidate.AttemptOrder:N0}/{candidate.CandidateAttemptLimit:N0} rawRank={candidate.RawSelectionRank:N0} " + - $"familyRank={candidate.CandidateTheoryFamilyRank:N0} memberRank={candidate.CandidateTheoryFamilyMemberRank:N0}[/]"); + $"selectionMode={Markup.Escape(candidate.DiversityMode)} familyRank={candidate.CandidateTheoryFamilyRank:N0} memberRank={candidate.CandidateTheoryFamilyMemberRank:N0} " + + $"previousSizeFloorBytes={Markup.Escape(previousFloor)} selectedSizeBytes={candidate.Prediction.PredictedSizeBytes:N0} sizeDeltaVsFloorBytes={Markup.Escape(deltaVsFloor)} effectivePredictedKld={Markup.Escape(effectiveKld)}[/]"); AnsiConsole.MarkupLine($"[grey] familyKey=[/][cyan]{Markup.Escape(candidate.CandidateTheoryFamilyKey)}[/]"); AnsiConsole.MarkupLine($"[grey] familyDisplay=[/][cyan]{Markup.Escape(candidate.CandidateTheoryFamilyDisplay)}[/]"); } } + private static string? ExtractSelectionNoteValue(string note, string key) + { + if (string.IsNullOrWhiteSpace(note)) + return null; + + string prefix = key + "="; + int start = note.IndexOf(prefix, StringComparison.Ordinal); + if (start < 0) + return null; + + start += prefix.Length; + int end = note.IndexOf(';', start); + return end < 0 ? note[start..].Trim() : note[start..end].Trim(); + } + private static BrutalityAnalysis AnalyzeNearLowerAnchorBrutality(HybridSelectionCandidate candidate) { var higherDamagePredictionAnchor = candidate.HigherDamagePredictionAnchor; @@ -1872,11 +2085,16 @@ private static void PrintCandidatePredictionLine(HybridSelectionCandidate candid $"fetched={candidate.FetchedCandidateCount:N0}, afterBrutality={candidate.CandidatesAfterBrutalityCount:N0}, attemptLimit={candidate.CandidateAttemptLimit:N0}"); if (!string.IsNullOrWhiteSpace(candidate.CandidateTheoryFamilyDisplay)) { + string ladderNote = candidate.CandidateSelectionNotes.FirstOrDefault(x => x.Contains("selectionStrategy=family-size-ladder", StringComparison.Ordinal)) ?? string.Empty; + string previousFloor = ExtractSelectionNoteValue(ladderNote, "previousSizeFloorBytes") ?? "n/a"; + string deltaVsFloor = ExtractSelectionNoteValue(ladderNote, "sizeDeltaVsFloorBytes") ?? "n/a"; + string effectiveKld = ExtractSelectionNoteValue(ladderNote, "effectivePredictedKld") ?? candidate.Prediction.PredictedKld.ToString("0.000000"); AnsiConsole.MarkupLine( - $"[grey] diversity family:[/] mode={Markup.Escape(candidate.DiversityMode)}, rawRank={candidate.RawSelectionRank:N0}, " + - $"familyRank={candidate.CandidateTheoryFamilyRank:N0}, memberRank={candidate.CandidateTheoryFamilyMemberRank:N0}"); - AnsiConsole.MarkupLine($"[grey] familyKey:[/] {Markup.Escape(candidate.CandidateTheoryFamilyKey)}"); - AnsiConsole.MarkupLine($"[grey] familyDisplay:[/] {Markup.Escape(candidate.CandidateTheoryFamilyDisplay)}"); + $"[grey] diversity family:[/] selectionMode={Markup.Escape(candidate.DiversityMode)}, rawRank={candidate.RawSelectionRank:N0}, " + + $"familyRank={candidate.CandidateTheoryFamilyRank:N0}, memberRank={candidate.CandidateTheoryFamilyMemberRank:N0}, " + + $"previousSizeFloorBytes={Markup.Escape(previousFloor)}, selectedSizeBytes={candidate.Prediction.PredictedSizeBytes:N0}, sizeDeltaVsFloorBytes={Markup.Escape(deltaVsFloor)}, effectivePredictedKld={Markup.Escape(effectiveKld)}"); + AnsiConsole.MarkupLine($"[grey] diversity familyKey:[/] {Markup.Escape(candidate.CandidateTheoryFamilyKey)}"); + AnsiConsole.MarkupLine($"[grey] diversity familyDisplay:[/] {Markup.Escape(candidate.CandidateTheoryFamilyDisplay)}"); } AnsiConsole.MarkupLine($"[grey] bit space:[/] {Markup.Escape(DescribeBitSpace(candidate.Prediction.Config))}"); } @@ -2252,6 +2470,7 @@ private static object ToValidationAttemptLog(CandidateValidationResult attempt) candidateAttemptLimit = c.CandidateAttemptLimit, rawSelectionRank = c.RawSelectionRank, diversityMode = c.DiversityMode, + selectionStrategy = c.CandidateSelectionNotes.FirstOrDefault(x => x.Contains("selectionStrategy=family-size-ladder", StringComparison.Ordinal)), candidateTheoryFamilyKey = c.CandidateTheoryFamilyKey, candidateTheoryFamilyDisplay = c.CandidateTheoryFamilyDisplay, candidateTheoryFamilyRank = c.CandidateTheoryFamilyRank, @@ -2329,6 +2548,31 @@ private static string ResolveGgufDirectory() return Path.Combine(Directory.GetCurrentDirectory(), "GGUF"); } + private async Task CountPredictedRowsInIntersectedSizeWindowAsync( + ulong predictionMin, + ulong predictionMax, + ulong realMin, + ulong realMax, + CancellationToken ct) + { + var window = IntersectSizeWindows(predictionMin, predictionMax, realMin, realMax); + if (window == null) + return 0; + + return await _predictedStore.CountPredictedHybridCandidatesInSizeWindowAsync(window.Value.Min, window.Value.Max, ct); + } + + private static (ulong Min, ulong Max)? IntersectSizeWindows( + ulong firstMin, + ulong firstMax, + ulong secondMin, + ulong secondMax) + { + ulong min = Math.Max(firstMin, secondMin); + ulong max = Math.Min(firstMax, secondMax); + return max < min ? null : (min, max); + } + private static ulong AddPercent(ulong bytes, double percent) { if (percent <= 0d) @@ -2377,9 +2621,17 @@ private sealed class ValidationCandidateSelectionResult public IReadOnlyList Candidates { get; init; } = Array.Empty(); public string Mode { get; init; } = string.Empty; public bool DiversityEnabled { get; init; } + public string SelectionStrategy { get; init; } = string.Empty; public int CandidateFamilyCount { get; init; } public int SelectedFamilyCount { get; init; } public IReadOnlyList SelectedFamilyKeys { get; init; } = Array.Empty(); + public string DiversitySelectionStrategy { get; init; } = string.Empty; + public int DiversityOverflowCount { get; init; } + public ulong? DiversitySizeFloorStartBytes { get; init; } + public ulong? DiversitySizeFloorEndBytes { get; init; } + public int OverflowCount { get; init; } + public ulong? SizeFloorStartBytes { get; init; } + public ulong? SizeFloorEndBytes { get; init; } public IReadOnlyList Notes { get; init; } = Array.Empty(); } @@ -2395,6 +2647,11 @@ private sealed class CandidateFamilyEntry public CandidateTheorySignature Signature { get; init; } = new(); public int RawRank { get; init; } public int MemberRank { get; set; } + public CandidateTheoryFamily? Family { get; set; } + public string SelectionMode { get; set; } = string.Empty; + public ulong? PreviousSizeFloorBytes { get; set; } + public long? SizeDeltaVsFloorBytes { get; set; } + public double EffectivePredictedKld { get; set; } = double.NaN; } private sealed class CandidateTheoryFamily @@ -2445,6 +2702,10 @@ private sealed class SelectionPhaseDiagnostic public ulong WindowMaxSizeBytes { get; init; } public double WindowSizeGiB { get; init; } public long CandidatePoolSize { get; init; } + public long PredictedPoolCount { get; init; } + public long DeterministicEligibleCount { get; init; } + public long RejectedByRealSizeWindow { get; init; } + public long RejectedByOtherPhaseDeterministicRules { get; init; } public long WindowCandidateCount { get; init; } public long LineBeatingCandidateCount { get; init; } public int FetchedCandidateCount { get; init; } @@ -2459,6 +2720,10 @@ private sealed class SelectionPhaseDiagnostic public int CandidateFamilyCount { get; init; } public int SelectedFamilyCount { get; init; } public IReadOnlyList SelectedFamilyKeys { get; init; } = Array.Empty(); + public string DiversitySelectionStrategy { get; init; } = string.Empty; + public int DiversityOverflowCount { get; init; } + public ulong? DiversitySizeFloorStartBytes { get; init; } + public ulong? DiversitySizeFloorEndBytes { get; init; } public IReadOnlyList TopCandidates { get; init; } = Array.Empty(); public IReadOnlyList RejectedByBrutalityPreview { get; init; } = Array.Empty(); public IReadOnlyList Notes { get; init; } = Array.Empty(); diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 25832f2..6f682d8 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -272,8 +272,16 @@ AND PredictionRank IS NOT NULL }; } + public Task CountStrictDominanceCandidatesAsync( + PredictedAnchorRow anchor, + CancellationToken ct = default) + { + return CountStrictDominanceCandidatesAsync(anchor, anchor.PredictedSizeBytes, ct); + } + public async Task CountStrictDominanceCandidatesAsync( PredictedAnchorRow anchor, + ulong maxSizeBytes, CancellationToken ct = default) { string sql = $@" @@ -289,7 +297,7 @@ AND PredictionRank IS NOT NULL return await ExecuteCountAsync( sql, - new object[] { anchor.PredictedSizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.PredictedKld }, + new object[] { maxSizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.PredictedKld }, ct); } @@ -311,13 +319,41 @@ AND PredictionRank IS NOT NULL return await ExecuteCountAsync(sql, new object[] { minSize, maxSize }, ct); } - public async Task CountBetterThanLinearCandidatesAsync( + public Task CountBetterThanLinearCandidatesAsync( PredictedAnchorRow higherDamageSmaller, PredictedAnchorRow lowerDamageLarger, ulong minSize, ulong maxSize, CancellationToken ct = default) { + return CountBetterThanLinearCandidatesAsync( + higherDamageSmaller, + lowerDamageLarger, + predictionWindowMinSize: minSize, + predictionWindowMaxSize: maxSize, + deterministicWindowMinSize: minSize, + deterministicWindowMaxSize: maxSize, + ct: ct); + } + + public async Task CountBetterThanLinearCandidatesAsync( + PredictedAnchorRow higherDamageSmaller, + PredictedAnchorRow lowerDamageLarger, + ulong predictionWindowMinSize, + ulong predictionWindowMaxSize, + ulong deterministicWindowMinSize, + ulong deterministicWindowMaxSize, + CancellationToken ct = default) + { + var effectiveWindow = IntersectSizeWindows( + predictionWindowMinSize, + predictionWindowMaxSize, + deterministicWindowMinSize, + deterministicWindowMaxSize); + + if (effectiveWindow == null) + return 0; + string sql = $@" WITH scored AS ( SELECT {CombinationDuckDbSchema.EffectivePredictedKldSql} AS PredictedKld, @@ -350,15 +386,24 @@ FROM scored denominator, lowerDamageLarger.PredictedKld, higherDamageSmaller.PredictedKld, - minSize, - maxSize, + effectiveWindow.Value.Min, + effectiveWindow.Value.Max, Config.SelectionMinimumKldImprovementEpsilon }, ct); } + public Task> QueryStrictDominanceCandidatesAsync( + PredictedAnchorRow anchor, + int limit, + CancellationToken ct = default) + { + return QueryStrictDominanceCandidatesAsync(anchor, anchor.PredictedSizeBytes, limit, ct); + } + public async Task> QueryStrictDominanceCandidatesAsync( PredictedAnchorRow anchor, + ulong maxSizeBytes, int limit, CancellationToken ct = default) { @@ -385,7 +430,7 @@ PredictionConfidence DESC return await QueryPredictedRowsAsync( sql, - new object[] { anchor.PredictedSizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.PredictedKld, limit }, + new object[] { maxSizeBytes, Config.SelectionMinimumKldImprovementEpsilon, anchor.PredictedKld, limit }, ct); } @@ -403,6 +448,15 @@ public async Task> QueryBetterThanLinear int limit, CancellationToken ct = default) { + var effectiveWindow = IntersectSizeWindows( + predictionWindowMinSize, + predictionWindowMaxSize, + realValidationWindowMinSize, + realValidationWindowMaxSize); + + if (effectiveWindow == null) + return Array.Empty(); + string sql = $@" WITH scored AS ( SELECT {CombinationDuckDbSchema.SlotColumnList}, @@ -461,8 +515,8 @@ PredictionRank ASC denominator, lowerDamagePredictionAnchor.PredictedKld, higherDamagePredictionAnchor.PredictedKld, - predictionWindowMinSize, - predictionWindowMaxSize, + effectiveWindow.Value.Min, + effectiveWindow.Value.Max, Config.SelectionMinimumKldImprovementEpsilon, limit }) @@ -501,6 +555,17 @@ PredictionRank ASC return list; } + private static (ulong Min, ulong Max)? IntersectSizeWindows( + ulong firstMin, + ulong firstMax, + ulong secondMin, + ulong secondMax) + { + ulong min = Math.Max(firstMin, secondMin); + ulong max = Math.Min(firstMax, secondMax); + return max < min ? null : (min, max); + } + private async Task> QueryPredictedRowsAsync( string sql, object[] args, @@ -739,4 +804,4 @@ private static async Task EnsureTensorConfigsTableExistsAsync(DuckDBConnection c "This almost always means the generator and prediction reader are using different DuckDB filenames, " + "or prediction started before QuantDatabaseService initialized/rebuilt the search-space table."); } -} +} \ No newline at end of file From 67a74be3381a8dbab30e5003e6f1579be68786af Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 6 May 2026 18:12:22 -0400 Subject: [PATCH 199/258] smart free lunch system activated --- MagicQuant/Commands/Evolution.cs | 3 +- MagicQuant/Config.cs | 9 + .../Configuration/MagicQuantYamlConfig.cs | 20 + .../Models/PredictionSelectionModels.cs | 9 +- .../IsolationDiagnosticsManifestService.cs | 3 +- .../Services/IsolationOptimizationService.cs | 55 +- .../PredictionGuidedHybridSelectionService.cs | 277 ++++++- .../SmartBaselineTuningFallbackService.cs | 751 ++++++++++++++++++ MagicQuant/config.default.yaml | 11 +- MagicQuant/config.dev.yaml | 9 + 10 files changed, 1132 insertions(+), 15 deletions(-) create mode 100644 MagicQuant/Services/SmartBaselineTuningFallbackService.cs diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index b87fe36..0a6098a 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -354,6 +354,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupCandidatesBanned:N0}"); AnsiConsole.MarkupLine($"[green]Bad trade eliminations:[/] {isolationResult.BadTradeEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Final KLD cleanup eliminations:[/] {isolationResult.FinalKldCleanupEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); AnsiConsole.MarkupLine($"[green]Combination count after rule pruning:[/] {comboCountAfterRulePruning:N0}"); @@ -806,4 +807,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} +} \ No newline at end of file diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 4ea076f..6f45806 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -53,6 +53,15 @@ public static void SetResolvedCustomBaselines(IEnumerable Math.Max(1, Current.CandidateSelection.MaxFallbackAttemptsPerAnchor); + public static bool SelectionSmartFallbackEnabled => + Current.CandidateSelection.SmartFallbackEnabled && SelectionSmartFallbackAttemptsPerFailure > 0; + + public static int SelectionSmartFallbackAttemptsPerFailure => + Math.Max(0, Current.CandidateSelection.SmartFallbackAttemptsPerFailure); + + public static int SelectionSmartFallbackMaxHigherFidelitySteps => + Math.Max(0, Current.CandidateSelection.SmartFallbackMaxHigherFidelitySteps); + public static double SelectionMinimumKldImprovementEpsilon => Math.Max(0d, Current.CandidateSelection.MinimumKldImprovementEpsilon); diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index b658d10..dba1fc5 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -217,6 +217,26 @@ public sealed class RuntimeCandidateSelectionConfig public int MaxCandidatesPerInteriorWindow { get; set; } = 1; public int MaxFallbackAttemptsPerAnchor { get; set; } = 5; + /// + /// Enables the conservative SQLite/isolation-truth baseline tuning fallback. + /// This does not query DuckDB and only runs after a normal phase fails to + /// validate a candidate for its anchor/window. + /// + public bool SmartFallbackEnabled { get; set; } = true; + + /// + /// Extra build/benchmark attempts permitted after the normal prediction-guided + /// attempts fail for a strict, near-baseline, or interior window. + /// + public int SmartFallbackAttemptsPerFailure { get; set; } = 3; + + /// + /// Maximum number of higher-fidelity anchor steps the smart fallback may climb + /// for a single tensor group. Lower-fidelity swaps are still only allowed when + /// their isolated KLD is measurably better than the baseline group state. + /// + public int SmartFallbackMaxHigherFidelitySteps { get; set; } = 2; + /// /// Strict epsilon for "lower KLD" claims. This is intentionally tiny because /// the validator verifies the final relationship against real benchmark truth. diff --git a/MagicQuant/Models/PredictionSelectionModels.cs b/MagicQuant/Models/PredictionSelectionModels.cs index 16e1902..494063e 100644 --- a/MagicQuant/Models/PredictionSelectionModels.cs +++ b/MagicQuant/Models/PredictionSelectionModels.cs @@ -80,7 +80,14 @@ public enum HybridSelectionReason { StrictDominanceReplacement = 1, NearBaselineOnePercentReplacement = 2, - InteriorSubspaceDiscovery = 3 + InteriorSubspaceDiscovery = 3, + + // SQLite/isolation-truth fallback candidates. These are intentionally not + // DuckDB prediction-space rows; they are conservative baseline-blanket + // tuning attempts used only after the normal selector cannot validate a win. + SmartStrictDominanceFallback = 4, + SmartNearBaselineFallback = 5, + SmartInteriorSubspaceFallback = 6 } public sealed class HybridSelectionCandidate diff --git a/MagicQuant/Services/IsolationDiagnosticsManifestService.cs b/MagicQuant/Services/IsolationDiagnosticsManifestService.cs index 4141e15..e06e21d 100644 --- a/MagicQuant/Services/IsolationDiagnosticsManifestService.cs +++ b/MagicQuant/Services/IsolationDiagnosticsManifestService.cs @@ -46,6 +46,7 @@ public async Task GenerateBadTradesAsync( summary = new { badTradeEliminations = isolationResult.BadTradeEliminations, + finalKldCleanupEliminations = isolationResult.FinalKldCleanupEliminations, disabledBaselines = isolationResult.DisabledBaselines, structuredBadTradeRows = isolationResult.BadTradeDetails.Count }, @@ -273,4 +274,4 @@ internal static class MagicQuantEnumerableExtensions var list = values.Where(x => !double.IsNaN(x) && !double.IsInfinity(x)).ToList(); return list.Count == 0 ? null : list.Average(); } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index f4bd6d7..9af6f27 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -44,6 +44,7 @@ public sealed class IsolationOptimizationResult public int DominatedGroupCandidatesBanned { get; set; } public int HardDamageEliminations { get; set; } public int BadTradeEliminations { get; set; } + public int FinalKldCleanupEliminations { get; set; } public int DisabledBaselines { get; set; } public int Bf16SuppressedGroups { get; set; } @@ -268,6 +269,8 @@ public async Task AnalyzeAndApplyFinalAsync( candidates = FilterSurvivors(group, candidates); ApplyBadTradeElimination(group, candidates, result); candidates = FilterSurvivors(group, candidates); + ApplyFinalKldCleanupElimination(group, candidates, result); + candidates = FilterSurvivors(group, candidates); ApplyEquivalentTruthElimination(group, candidates, result); candidates = FilterSurvivors(group, candidates) @@ -596,6 +599,56 @@ private static void ApplyBadTradeElimination(TensorGroup group, List candidates, + IsolationOptimizationResult result) + { + var activeCandidates = GetActiveExplicitCandidates(group, candidates, phase: "FinalKldCleanup"); + if (activeCandidates.Count <= 1) + return; + + foreach (var candidate in activeCandidates + .OrderByDescending(x => x.SizeBytes) + .ThenByDescending(x => x.Kld) + .ToList()) + { + if (RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate.CandidateBaseline)) + continue; + + var better = activeCandidates + .Where(x => x.CandidateBaseline.UniqueId != candidate.CandidateBaseline.UniqueId) + .Where(x => !RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x.CandidateBaseline)) + .Where(x => x.SizeBytes <= candidate.SizeBytes) + .Where(x => x.Kld + IsolationPruningConfig.FloatingPointEpsilon < candidate.Kld) + .OrderBy(x => x.Kld) + .ThenBy(x => x.SizeBytes) + .ThenBy(x => Math.Abs(x.PplDeltaPercent)) + .ThenByDescending(x => EquivalentTruthSelectionHelper.GetBaselineSafetyRank( + x.CandidateBaseline, + isHybrid: false, + isExternalPureBaseline: x.CandidateBaseline.IsExternalRepositoryBaseline)) + .ThenBy(x => x.CandidateBaseline.Names[0], StringComparer.Ordinal) + .FirstOrDefault(); + + if (better == null) + continue; + + RuntimeSearchSpace.BanCombinationCandidateForGroup( + group, + candidate.CandidateBaseline, + phase: "FinalKldCleanup", + reason: $"same-size-or-larger and higher KLD than {better.CandidateBaseline.Names[0]} after bad-trade anchoring"); + + result.FinalKldCleanupEliminations++; + result.Notes.Add( + $"Final KLD cleanup elimination: '{candidate.CandidateBaseline.Names[0]}' removed for '{group.Name}' because '{better.CandidateBaseline.Names[0]}' was same-size-or-smaller and lower KLD after bad-trade anchoring completed " + + $"(removed size={candidate.SizeBytes:N0}, kld={candidate.Kld:G6}; replacement size={better.SizeBytes:N0}, kld={better.Kld:G6})."); + } + } + + + private static void ApplyEquivalentTruthElimination( TensorGroup group, List candidates, @@ -1202,4 +1255,4 @@ private sealed class CategorySnapshot public double Ppl { get; set; } public double PplError { get; set; } } -} +} \ No newline at end of file diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index fbaf391..cb116b7 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -34,6 +34,7 @@ public sealed class PredictionGuidedHybridSelectionService private readonly HybridBenchmarkRepository _repository; private readonly FinalRealBenchmarkEliminationService _finalEliminator; private readonly RemainingCombinationStore _predictedStore; + private readonly SmartBaselineTuningFallbackService _smartFallbackService; public PredictionGuidedHybridSelectionService( QuantizationService quantizationService, @@ -45,6 +46,7 @@ public PredictionGuidedHybridSelectionService( _repository = repository; _finalEliminator = finalEliminator; _predictedStore = predictedStore; + _smartFallbackService = new SmartBaselineTuningFallbackService(repository); } public async Task RunAsync( @@ -156,9 +158,11 @@ private async Task RunStrictDominanceReplacementAsync( LowerDamageLarger = ToAnchorLog(anchor), WindowMinSizeBytes = 0, WindowMaxSizeBytes = anchor.SizeBytes, - CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor, - Notes = ["Skipped because no predicted virtual anchor row was available. DuckDB preselection intentionally does not fall back to real anchor KLD/size."] + CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor + Config.SelectionSmartFallbackAttemptsPerFailure, + Notes = ["Skipped because no predicted virtual anchor row was available. DuckDB preselection intentionally does not fall back to real anchor KLD/size. Smart baseline fallback may still inspect SQLite isolation truth."] }); + + await TryRunSmartStrictFallbackAsync(anchor, accepted, eliminations, validationFailures, validationAttempts, ct); continue; } @@ -277,7 +281,10 @@ private async Task RunStrictDominanceReplacementAsync( AnsiConsole.MarkupLine($"[yellow]Strict dominance skipped builds for {Markup.Escape(anchor.DisplayName)}:[/] no physically eligible predicted candidates remained after deterministic size/KLD filters."); if (candidates.Count == 0) + { + await TryRunSmartStrictFallbackAsync(anchor, accepted, eliminations, validationFailures, validationAttempts, ct); continue; + } var acceptedForAnchor = new List(); foreach (var candidate in candidates) @@ -306,6 +313,7 @@ private async Task RunStrictDominanceReplacementAsync( if (acceptedForAnchor.Count == 0) { AnsiConsole.MarkupLine($"[grey]No strict predicted replacement validated for anchor:[/] {Markup.Escape(anchor.DisplayName)}"); + await TryRunSmartStrictFallbackAsync(anchor, accepted, eliminations, validationFailures, validationAttempts, ct); continue; } @@ -347,6 +355,69 @@ private async Task RunStrictDominanceReplacementAsync( } + private async Task TryRunSmartStrictFallbackAsync( + BenchmarkSnapshotRecord anchor, + List accepted, + List eliminations, + List validationFailures, + List validationAttempts, + CancellationToken ct) + { + if (!Config.SelectionSmartFallbackEnabled) + return false; + + var smartCandidates = (await _smartFallbackService.BuildStrictDominanceCandidatesAsync(anchor, 1, 1, ct)).ToList(); + if (smartCandidates.Count == 0) + return false; + + var acceptedForAnchor = new List(); + + foreach (var candidate in smartCandidates) + { + var validation = await BuildAndValidateSingleAsync( + candidate, + snapshot => snapshot.SizeBytes <= anchor.SizeBytes && + snapshot.Kld + Config.SelectionMinimumKldImprovementEpsilon < anchor.Kld, + $"smart fallback must be <= {anchor.SizeBytes:N0} bytes and lower KLD than {anchor.DisplayName}", + ct); + + validationAttempts.Add(validation); + + if (validation.Accepted && validation.Snapshot != null) + { + acceptedForAnchor.Add(validation); + break; + } + + validationFailures.Add(validation); + } + + if (acceptedForAnchor.Count == 0) + { + AnsiConsole.MarkupLine($"[grey]Smart strict fallback found no validated replacement for anchor:[/] {Markup.Escape(anchor.DisplayName)}"); + return false; + } + + var chosen = ChooseBestStrictDominanceCandidate(anchor, acceptedForAnchor); + accepted.Add(chosen.Snapshot!); + eliminations.Add(new BaselineEliminationRecord + { + Eliminated = anchor, + Eliminator = chosen.Snapshot!, + Reason = "smart baseline-tuning strict dominance fallback: real benchmark validated lower KLD at same-or-smaller size" + }); + + AnsiConsole.MarkupLine("[green]Smart strict fallback candidate selected:[/]"); + AnsiConsole.MarkupLine($"[grey] anchor=[/] [cyan]{Markup.Escape(anchor.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] chosen=[/] [cyan]{Markup.Escape(chosen.Snapshot!.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] actualKld=[/] [cyan]{chosen.Snapshot.Kld:0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] actualSizeBytes=[/] [cyan]{chosen.Snapshot.SizeBytes:N0}[/]"); + AnsiConsole.MarkupLine($"[grey] gainVsAnchor=[/] [cyan]{anchor.Kld - chosen.Snapshot.Kld:0.000000}[/]"); + return true; + } + + + private async Task LoadBestConfirmedBeneficialAnomalySnapshotAsync(CancellationToken ct) { @@ -550,6 +621,18 @@ private async Task RunNearBaselineReplacementAsync( continue; } + ulong realMin = lowerSizeHigherDamage.SizeBytes; + ulong realMax = AddPercent(realMin, Config.SelectionNearBaselineMaxSizeGrowthPercent); + + if (realMax > upperSizeLowerDamage.SizeBytes) + realMax = upperSizeLowerDamage.SizeBytes; + + if (realMax <= realMin) + { + AnsiConsole.MarkupLine($"[grey]Skipping near-baseline pair with empty real window:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)} -> {Markup.Escape(upperSizeLowerDamage.DisplayName)}"); + continue; + } + var predictedLowerSizeHigherDamage = await _predictedStore.FindPredictedAnchorForRealAnchorAsync(lowerSizeHigherDamage, predictedAnchors, ct); var predictedUpperSizeLowerDamage = await _predictedStore.FindPredictedAnchorForRealAnchorAsync(upperSizeLowerDamage, predictedAnchors, ct); if (predictedLowerSizeHigherDamage == null || predictedUpperSizeLowerDamage == null) @@ -565,17 +648,16 @@ private async Task RunNearBaselineReplacementAsync( LowerDamageLarger = ToAnchorLog(upperSizeLowerDamage), PredictionHigherDamageSmaller = ToPredictionAnchorLog(predictedLowerSizeHigherDamage), PredictionLowerDamageLarger = ToPredictionAnchorLog(predictedUpperSizeLowerDamage), - Notes = ["Skipped because one or both predicted virtual anchor rows were unavailable. DuckDB preselection intentionally does not fall back to real anchor KLD/size."] + WindowMinSizeBytes = realMin, + WindowMaxSizeBytes = realMax, + CandidateAttemptLimit = Config.SelectionMaxFallbackAttemptsPerAnchor + Config.SelectionSmartFallbackAttemptsPerFailure, + Notes = ["Skipped because one or both predicted virtual anchor rows were unavailable. DuckDB preselection intentionally does not fall back to real anchor KLD/size. Smart baseline fallback may still inspect SQLite isolation truth."] }); + + await TryRunSmartNearFallbackAsync(lowerSizeHigherDamage, upperSizeLowerDamage, realMin, realMax, pairIndex + 1, pairs.Count, accepted, eliminations, validationFailures, validationAttempts, ct); continue; } - ulong realMin = lowerSizeHigherDamage.SizeBytes; - ulong realMax = AddPercent(realMin, Config.SelectionNearBaselineMaxSizeGrowthPercent); - - if (realMax > upperSizeLowerDamage.SizeBytes) - realMax = upperSizeLowerDamage.SizeBytes; - ulong predictionMin = predictedLowerSizeHigherDamage.PredictedSizeBytes; ulong predictionMax = AddPercent(predictionMin, Config.SelectionNearBaselineMaxSizeGrowthPercent); @@ -585,6 +667,7 @@ private async Task RunNearBaselineReplacementAsync( if (predictionMax <= predictionMin || realMax <= realMin) { AnsiConsole.MarkupLine($"[grey]Skipping near-baseline pair with empty prediction/real window:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)} -> {Markup.Escape(upperSizeLowerDamage.DisplayName)}"); + await TryRunSmartNearFallbackAsync(lowerSizeHigherDamage, upperSizeLowerDamage, realMin, realMax, pairIndex + 1, pairs.Count, accepted, eliminations, validationFailures, validationAttempts, ct); continue; } @@ -720,8 +803,12 @@ private async Task RunNearBaselineReplacementAsync( AnsiConsole.MarkupLine($"[grey] rejected by near-lower-anchor brutality preview:[/] [cyan]{rejectedByBrutality.Count:N0}[/] (see magicquant-selection-phase-diagnostics.json)"); if (candidates.Count == 0) + { + await TryRunSmartNearFallbackAsync(lowerSizeHigherDamage, upperSizeLowerDamage, realMin, realMax, pairIndex + 1, pairs.Count, accepted, eliminations, validationFailures, validationAttempts, ct); continue; + } + bool acceptedThisPair = false; foreach (var candidate in candidates) { var validation = await BuildAndValidateSingleAsync( @@ -743,16 +830,90 @@ private async Task RunNearBaselineReplacementAsync( Eliminator = validation.Snapshot, Reason = $"near-baseline replacement within +{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}% size premium" }); + acceptedThisPair = true; break; } validationFailures.Add(validation); } + + if (!acceptedThisPair) + { + await TryRunSmartNearFallbackAsync(lowerSizeHigherDamage, upperSizeLowerDamage, realMin, realMax, pairIndex + 1, pairs.Count, accepted, eliminations, validationFailures, validationAttempts, ct); + } } return new PhaseValidationResult { AcceptedSnapshots = accepted }; } + + private async Task TryRunSmartNearFallbackAsync( + BenchmarkSnapshotRecord lowerSizeHigherDamage, + BenchmarkSnapshotRecord upperSizeLowerDamage, + ulong realMin, + ulong realMax, + int phaseWindowIndex, + int phaseWindowCount, + List accepted, + List eliminations, + List validationFailures, + List validationAttempts, + CancellationToken ct) + { + if (!Config.SelectionSmartFallbackEnabled) + return false; + + var smartCandidates = (await _smartFallbackService.BuildNearBaselineCandidatesAsync( + lowerSizeHigherDamage, + upperSizeLowerDamage, + realMin, + realMax, + phaseWindowIndex, + phaseWindowCount, + ct)).ToList(); + + if (smartCandidates.Count == 0) + return false; + + foreach (var candidate in smartCandidates) + { + var validation = await BuildAndValidateSingleAsync( + candidate, + snapshot => snapshot.SizeBytes >= realMin && + snapshot.SizeBytes <= realMax && + BeatsLinearKldLine(snapshot.SizeBytes, snapshot.Kld, lowerSizeHigherDamage, upperSizeLowerDamage), + $"smart fallback must land inside {realMin:N0}..{realMax:N0} bytes and beat the real linear KLD line", + ct); + + validationAttempts.Add(validation); + + if (validation.Accepted && validation.Snapshot != null) + { + accepted.Add(validation.Snapshot); + eliminations.Add(new BaselineEliminationRecord + { + Eliminated = lowerSizeHigherDamage, + Eliminator = validation.Snapshot, + Reason = $"smart baseline-tuning near-baseline fallback within +{Config.SelectionNearBaselineMaxSizeGrowthPercent:0.###}% size premium" + }); + + AnsiConsole.MarkupLine("[green]Smart near-baseline fallback candidate selected:[/]"); + AnsiConsole.MarkupLine($"[grey] lower anchor=[/] [cyan]{Markup.Escape(lowerSizeHigherDamage.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] upper anchor=[/] [cyan]{Markup.Escape(upperSizeLowerDamage.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] chosen=[/] [cyan]{Markup.Escape(validation.Snapshot.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] actualKld=[/] [cyan]{validation.Snapshot.Kld:0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] actualSizeBytes=[/] [cyan]{validation.Snapshot.SizeBytes:N0}[/]"); + return true; + } + + validationFailures.Add(validation); + } + + AnsiConsole.MarkupLine($"[grey]Smart near-baseline fallback found no validated candidate for:[/] {Markup.Escape(lowerSizeHigherDamage.DisplayName)} -> {Markup.Escape(upperSizeLowerDamage.DisplayName)}"); + return false; + } + + private async Task RunInteriorSubspaceDiscoveryAsync( IReadOnlyList currentAnchors, IReadOnlyList predictedAnchors, @@ -972,7 +1133,8 @@ private async Task RunInteriorSubspaceDiscoveryAsync( if (deduped.Count == 0) { AnsiConsole.MarkupLine("[grey]No predicted interior candidates beat their local linear KLD lines after window/brutality filtering.[/]"); - return new PhaseValidationResult(); + var smartOnly = await RunSmartInteriorFallbackAsync(pairs, fractions, validationFailures, validationAttempts, ct); + return new PhaseValidationResult { AcceptedSnapshots = smartOnly }; } AnsiConsole.MarkupLine($"[grey]Interior candidates selected for batch validation:[/] [cyan]{deduped.Count:N0}[/]"); @@ -1029,9 +1191,104 @@ private async Task RunInteriorSubspaceDiscoveryAsync( validationFailures.Add(validation); } + if (accepted.Count == 0) + { + var smartAccepted = await RunSmartInteriorFallbackAsync(pairs, fractions, validationFailures, validationAttempts, ct); + accepted.AddRange(smartAccepted); + } + return new PhaseValidationResult { AcceptedSnapshots = accepted }; } + private async Task> RunSmartInteriorFallbackAsync( + IReadOnlyList pairs, + IReadOnlyList fractions, + List validationFailures, + List validationAttempts, + CancellationToken ct) + { + if (!Config.SelectionSmartFallbackEnabled) + return Array.Empty(); + + var accepted = new List(); + int estimatedWindowCount = pairs.Sum(pair => EstimateInteriorWindowCount(pair, fractions)); + int globalWindowIndex = 0; + + foreach (var pair in pairs) + { + ulong realLowSize = pair.HigherDamageSmaller.SizeBytes; + ulong realHighSize = pair.LowerDamageLarger.SizeBytes; + if (realHighSize <= realLowSize) + continue; + + ulong realSpan = realHighSize - realLowSize; + ulong realCursor = realLowSize; + + for (int i = 0; i < fractions.Count; i++) + { + double fraction = fractions[i]; + if (fraction <= 0d) + continue; + + ulong realWidth = (ulong)Math.Max(1d, Math.Round(realSpan * Math.Clamp(fraction, 0d, 1d))); + ulong realMin = realCursor; + ulong realMax = i == fractions.Count - 1 + ? realHighSize + : Math.Min(realHighSize, realCursor + realWidth); + + if (realMax <= realMin) + continue; + + globalWindowIndex++; + string windowLabel = $"smart interior {globalWindowIndex:N0}: {pair.HigherDamageSmaller.DisplayName} -> {pair.LowerDamageLarger.DisplayName}"; + + var smartCandidates = (await _smartFallbackService.BuildInteriorCandidatesAsync( + pair.HigherDamageSmaller, + pair.LowerDamageLarger, + realMin, + realMax, + windowLabel, + globalWindowIndex, + Math.Max(estimatedWindowCount, globalWindowIndex), + ct)).ToList(); + + foreach (var candidate in smartCandidates) + { + var validation = await BuildAndValidateSingleAsync( + candidate, + snapshot => snapshot.SizeBytes >= realMin && + snapshot.SizeBytes <= realMax && + BeatsLinearKldLine(snapshot.SizeBytes, snapshot.Kld, pair.HigherDamageSmaller, pair.LowerDamageLarger), + $"smart fallback must land inside {realMin:N0}..{realMax:N0} bytes and beat the real interior linear KLD line", + ct); + + validationAttempts.Add(validation); + + if (validation.Accepted && validation.Snapshot != null) + { + accepted.Add(validation.Snapshot); + AnsiConsole.MarkupLine("[green]Smart interior fallback candidate selected:[/]"); + AnsiConsole.MarkupLine($"[grey] lower anchor=[/] [cyan]{Markup.Escape(pair.HigherDamageSmaller.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] upper anchor=[/] [cyan]{Markup.Escape(pair.LowerDamageLarger.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] chosen=[/] [cyan]{Markup.Escape(validation.Snapshot.DisplayName)}[/]"); + AnsiConsole.MarkupLine($"[grey] actualKld=[/] [cyan]{validation.Snapshot.Kld:0.000000}[/]"); + AnsiConsole.MarkupLine($"[grey] actualSizeBytes=[/] [cyan]{validation.Snapshot.SizeBytes:N0}[/]"); + return accepted; + } + + validationFailures.Add(validation); + } + + realCursor = realMax; + if (realCursor >= realHighSize) + break; + } + } + + AnsiConsole.MarkupLine("[grey]Smart interior fallback found no validated candidates.[/]"); + return accepted; + } + private async Task BuildAndValidateSingleAsync( HybridSelectionCandidate candidate, Func accept, diff --git a/MagicQuant/Services/SmartBaselineTuningFallbackService.cs b/MagicQuant/Services/SmartBaselineTuningFallbackService.cs new file mode 100644 index 0000000..80f1921 --- /dev/null +++ b/MagicQuant/Services/SmartBaselineTuningFallbackService.cs @@ -0,0 +1,751 @@ +using MagicQuant.Helpers; +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +/// +/// Conservative, non-DuckDB fallback used only after the normal prediction-guided +/// selector fails to validate a candidate for a strict/premium/interior phase. +/// +/// The service starts from a uniform learned-baseline blanket, keeps that blanket +/// baseline available for every group even if the baseline was pruned from a group, +/// and only swaps in group candidates that survived isolation pruning. It does not +/// create the normal prediction-space gremlin trades: shrinking is only allowed when +/// the isolated group sample is same-size-or-smaller and measurably lower KLD than +/// the blanket state; higher-fidelity protection is bounded by config and must fit +/// the target real-size window exactly. +/// +public sealed class SmartBaselineTuningFallbackService +{ + private const double KldEpsilon = 1e-12d; + + private readonly HybridBenchmarkRepository _repository; + private RankSafeKldPredictionService.RankSafePredictionModel? _context; + + public SmartBaselineTuningFallbackService(HybridBenchmarkRepository repository) + { + _repository = repository; + } + + public async Task> BuildStrictDominanceCandidatesAsync( + BenchmarkSnapshotRecord anchor, + int phaseWindowIndex, + int phaseWindowCount, + CancellationToken ct = default) + { + if (!Config.SelectionSmartFallbackEnabled) + return Array.Empty(); + + return await BuildCandidatesAsync(new SmartFallbackRequest + { + Reason = HybridSelectionReason.SmartStrictDominanceFallback, + WindowLabel = $"smart strict baseline tuning vs {anchor.DisplayName}", + BaselineAnchor = anchor, + HigherDamageAnchor = anchor, + LowerDamageAnchor = anchor, + WindowMinSizeBytes = 0, + WindowMaxSizeBytes = anchor.SizeBytes, + StrictDominance = true, + PhaseWindowIndex = phaseWindowIndex, + PhaseWindowCount = phaseWindowCount + }, ct); + } + + public async Task> BuildNearBaselineCandidatesAsync( + BenchmarkSnapshotRecord lowerSizeHigherDamage, + BenchmarkSnapshotRecord upperSizeLowerDamage, + ulong realMinSizeBytes, + ulong realMaxSizeBytes, + int phaseWindowIndex, + int phaseWindowCount, + CancellationToken ct = default) + { + if (!Config.SelectionSmartFallbackEnabled) + return Array.Empty(); + + return await BuildCandidatesAsync(new SmartFallbackRequest + { + Reason = HybridSelectionReason.SmartNearBaselineFallback, + WindowLabel = $"smart near-baseline tuning {lowerSizeHigherDamage.DisplayName} → {upperSizeLowerDamage.DisplayName}", + BaselineAnchor = lowerSizeHigherDamage, + HigherDamageAnchor = lowerSizeHigherDamage, + LowerDamageAnchor = upperSizeLowerDamage, + WindowMinSizeBytes = realMinSizeBytes, + WindowMaxSizeBytes = realMaxSizeBytes, + StrictDominance = false, + PhaseWindowIndex = phaseWindowIndex, + PhaseWindowCount = phaseWindowCount + }, ct); + } + + public async Task> BuildInteriorCandidatesAsync( + BenchmarkSnapshotRecord lowerSizeHigherDamage, + BenchmarkSnapshotRecord upperSizeLowerDamage, + ulong realMinSizeBytes, + ulong realMaxSizeBytes, + string windowLabel, + int phaseWindowIndex, + int phaseWindowCount, + CancellationToken ct = default) + { + if (!Config.SelectionSmartFallbackEnabled) + return Array.Empty(); + + return await BuildCandidatesAsync(new SmartFallbackRequest + { + Reason = HybridSelectionReason.SmartInteriorSubspaceFallback, + WindowLabel = $"smart interior tuning {windowLabel}", + BaselineAnchor = lowerSizeHigherDamage, + HigherDamageAnchor = lowerSizeHigherDamage, + LowerDamageAnchor = upperSizeLowerDamage, + WindowMinSizeBytes = realMinSizeBytes, + WindowMaxSizeBytes = realMaxSizeBytes, + StrictDominance = false, + PhaseWindowIndex = phaseWindowIndex, + PhaseWindowCount = phaseWindowCount + }, ct); + } + + private async Task> BuildCandidatesAsync( + SmartFallbackRequest request, + CancellationToken ct) + { + ct.ThrowIfCancellationRequested(); + + if (!TryResolveBaselineBlanket(request.BaselineAnchor, out var blanketBaseline, out var skipReason)) + { + AnsiConsole.MarkupLine($"[grey]Smart fallback skipped:[/] {Markup.Escape(skipReason)}"); + return Array.Empty(); + } + + var context = await GetContextAsync(ct); + if (!context.BaseOnlySnapshotsByBaselineId.ContainsKey(blanketBaseline.UniqueId)) + { + AnsiConsole.MarkupLine($"[grey]Smart fallback skipped:[/] missing exact base-only anchor for {Markup.Escape(blanketBaseline.Names[0])}."); + return Array.Empty(); + } + + var baseBlanket = HybridQuant.CreateLearnedCandidateBlanket( + baseQuant: blanketBaseline, + groups: context.ActiveGroups, + candidateBaseline: blanketBaseline); + var baseConfig = (TensorConfig)baseBlanket; + + var hasBaseSize = TryPredictSize(baseConfig, context, out var baseSize, out var baseSizeNotes); + var hasBaseKld = TryComputeAdditiveKld(baseConfig, context, out var baseKld, out var baseKldNotes); + + if (!hasBaseSize || !hasBaseKld) + { + var notes = baseSizeNotes.Concat(baseKldNotes).Distinct().ToList(); + AnsiConsole.MarkupLine($"[grey]Smart fallback skipped:[/] incomplete isolation truth for {Markup.Escape(blanketBaseline.Names[0])} blanket. {Markup.Escape(string.Join(" ", notes.Take(2)))}"); + return Array.Empty(); + } + + if (request.StrictDominance && baseSize > request.WindowMaxSizeBytes) + { + AnsiConsole.MarkupLine($"[grey]Smart strict fallback skipped:[/] {Markup.Escape(blanketBaseline.Names[0])} blanket is larger than the strict anchor."); + return Array.Empty(); + } + + var options = BuildGroupOptions(blanketBaseline, context) + .Where(x => request.StrictDominance ? x.SizeDeltaBytes <= 0 : true) + .ToList(); + + if (options.Count == 0) + { + AnsiConsole.MarkupLine($"[grey]Smart fallback found no isolated group trades for[/] [cyan]{Markup.Escape(blanketBaseline.Names[0])}[/]."); + return Array.Empty(); + } + + var plans = BuildPlans(request, blanketBaseline, baseSize, baseKld, options, context); + if (plans.Count == 0) + { + AnsiConsole.MarkupLine($"[grey]Smart fallback found no size-safe plans for[/] [cyan]{Markup.Escape(blanketBaseline.Names[0])}[/] in window {Markup.Escape(request.WindowLabel)}."); + return Array.Empty(); + } + + var orderedPlans = request.StrictDominance + ? plans.OrderByDescending(x => x.TotalKldGain).ThenBy(x => x.PredictedSizeBytes).ThenByDescending(x => x.Score).ToList() + : plans.OrderByDescending(x => x.Score).ThenByDescending(x => x.TotalKldGain).ThenByDescending(x => x.PredictedSizeBytes).ToList(); + + int limit = Config.SelectionSmartFallbackAttemptsPerFailure; + var selected = orderedPlans + .Take(limit) + .Select((plan, index) => ToCandidate(request, blanketBaseline, plan, index + 1, options.Count, orderedPlans.Count, orderedPlans.Count(x => x.PredictedGainOverLine > 0d))) + .ToList(); + + AnsiConsole.MarkupLine( + $"[yellow]Smart baseline fallback staged:[/] [cyan]{selected.Count:N0}[/] candidate(s) for {Markup.Escape(request.WindowLabel)} from [cyan]{Markup.Escape(blanketBaseline.Names[0])}[/] blanket."); + + foreach (var candidate in selected) + { + var swaps = string.Join(", ", candidate.CandidateSelectionNotes.Where(x => x.StartsWith("swap ", StringComparison.Ordinal)).Take(4)); + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(HybridBenchmarkRepository.BuildDisplayName(candidate.Prediction.Quant))}[/] size={ToGiB(candidate.Prediction.PredictedSizeBytes):0.00}GiB additiveKLD={candidate.Prediction.PredictedKld:0.000000} {Markup.Escape(swaps)}"); + } + + return selected; + } + + private static bool TryResolveBaselineBlanket( + BenchmarkSnapshotRecord anchor, + out BaselineQuants baseline, + out string reason) + { + baseline = anchor.Quant.BaseQuant; + reason = string.Empty; + + if (BaselineQuants.IsNativeExactAlias(baseline.UniqueId)) + { + reason = $"anchor '{anchor.DisplayName}' uses native/exact precision and cannot be used as a learned baseline blanket."; + return false; + } + + if (TensorConfigIdentity.IsPureBaseline(anchor.Config)) + return true; + + foreach (var (group, storedValue) in TensorConfigIdentity.EnumerateGroupSlots(anchor.Config)) + { + if (Cache.UnusedTensorGroups.Any(x => x.UniqueId == group.UniqueId)) + continue; + + if (BaselineQuants.IsNullTensorConfigGroupSlot(storedValue)) + continue; + + var decoded = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedValue); + if (decoded != baseline.UniqueId) + { + reason = $"anchor '{anchor.DisplayName}' is already a non-uniform hybrid; smart fallback only starts from pure/uniform baseline blankets."; + return false; + } + } + + return true; + } + + private async Task GetContextAsync(CancellationToken ct) + { + if (_context != null) + return _context; + + var activeGroups = TReg.All + .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) + .OrderBy(x => x.UniqueId) + .ToList(); + + if (activeGroups.Count == 0) + throw new InvalidOperationException("No active tensor groups were available for smart baseline fallback."); + + var notes = new List(); + var pureSnapshots = await _repository.LoadPureBaselineSnapshotsAsync(ct); + var pureByBaselineId = pureSnapshots + .GroupBy(x => x.Quant.BaseQuant.UniqueId) + .ToDictionary(g => g.Key, g => g.OrderBy(x => x.Kld).ThenBy(x => x.SizeBytes).First()); + + if (!pureByBaselineId.TryGetValue(BaselineQuants.Q8_0.UniqueId, out var pureQ8)) + throw new InvalidOperationException("Smart baseline fallback requires a pure Q8_0 benchmark snapshot."); + + var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + var q8BaseOnlyQuant = HybridQuant.CreateExactBlanket( + baseQuant: BaselineQuants.Q8_0, + groups: activeGroups, + exactScheme: nativeExactScheme); + + var q8BaseOnly = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)q8BaseOnlyQuant, ct) + ?? throw new InvalidOperationException("Smart baseline fallback requires the Q8_0 native-exact base-only anchor."); + + var baseOnlyByBaselineId = new Dictionary + { + [BaselineQuants.Q8_0.UniqueId] = q8BaseOnly + }; + + foreach (var baseline in BaselineQuants.GetAllRecognizedBaselines() + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .OrderBy(x => x.UniqueId)) + { + if (baseOnlyByBaselineId.ContainsKey(baseline.UniqueId)) + continue; + + var directBaseOnlyQuant = HybridQuant.CreateExactBlanket( + baseQuant: baseline, + groups: activeGroups, + exactScheme: nativeExactScheme); + + var directBaseOnlySnapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)directBaseOnlyQuant, ct); + if (directBaseOnlySnapshot != null) + baseOnlyByBaselineId[baseline.UniqueId] = directBaseOnlySnapshot; + } + + var isolationByGroupAndBaseline = new Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord>(); + + foreach (var group in activeGroups) + { + foreach (var baseline in BaselineQuants.GetAllRecognizedBaselines() + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .OrderBy(x => x.UniqueId)) + { + if (isolationByGroupAndBaseline.ContainsKey((group.UniqueId, baseline.UniqueId))) + continue; + + var isolationQuant = HybridQuant.CreateExactBlanket( + baseQuant: BaselineQuants.Q8_0, + groups: activeGroups, + exactScheme: nativeExactScheme); + + isolationQuant.SetLearnedCandidateOverride(group, baseline); + var snapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolationQuant, ct); + if (snapshot != null) + isolationByGroupAndBaseline[(group.UniqueId, baseline.UniqueId)] = snapshot; + } + } + + notes.Add($"Smart fallback isolation context loaded: activeGroups={activeGroups.Count:N0}, baseOnlyAnchors={baseOnlyByBaselineId.Count:N0}, groupIsolations={isolationByGroupAndBaseline.Count:N0}."); + + _context = new RankSafeKldPredictionService.RankSafePredictionModel( + activeGroups: activeGroups, + pureQ8: pureQ8, + q8BaseOnly: q8BaseOnly, + pureSnapshotsByBaselineId: pureByBaselineId, + baseOnlySnapshotsByBaselineId: baseOnlyByBaselineId, + isolationByGroupAndBaseline: isolationByGroupAndBaseline, + notes: notes); + + return _context; + } + + private static IReadOnlyList BuildGroupOptions( + BaselineQuants blanketBaseline, + RankSafeKldPredictionService.RankSafePredictionModel context) + { + var result = new List(); + + foreach (var group in context.ActiveGroups) + { + if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, blanketBaseline.UniqueId), out var baseIsolation)) + continue; + + long baseContributionBytes = (long)baseIsolation.SizeBytes - (long)context.Q8BaseOnly.SizeBytes; + + var allowed = RuntimeSearchSpace.GetAllowedRealExplicitCombinationCandidatesForGroup(group) + .Where(x => x.UniqueId != blanketBaseline.UniqueId) + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .OrderBy(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId) + .ToList(); + + foreach (var candidate in allowed) + { + if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, candidate.UniqueId), out var candidateIsolation)) + continue; + + double kldGain = baseIsolation.Kld - candidateIsolation.Kld; + if (kldGain <= Config.SelectionMinimumKldImprovementEpsilon + KldEpsilon) + continue; + + long candidateContributionBytes = (long)candidateIsolation.SizeBytes - (long)context.Q8BaseOnly.SizeBytes; + long sizeDeltaBytes = candidateContributionBytes - baseContributionBytes; + + int fidelitySteps = CountHigherFidelitySteps(blanketBaseline, candidate); + if (fidelitySteps > Config.SelectionSmartFallbackMaxHigherFidelitySteps) + continue; + + bool isShrink = sizeDeltaBytes < 0; + bool isFreeOrBetter = sizeDeltaBytes <= 0; + + if (isShrink && candidateIsolation.Kld + Config.SelectionMinimumKldImprovementEpsilon >= baseIsolation.Kld) + continue; + + double damageAvoidedPerMiB = kldGain / Math.Max(1d, Math.Abs(sizeDeltaBytes) / 1024d / 1024d); + double sensitivityScore = kldGain * Math.Log2(2d + Math.Max(0d, baseIsolation.Kld) / Math.Max(candidateIsolation.Kld, 1e-12d)); + double score = sensitivityScore + damageAvoidedPerMiB; + if (isFreeOrBetter) + score += kldGain * 1000d; + + result.Add(new SmartGroupOption + { + Group = group, + CandidateBaseline = candidate, + BaseIsolation = baseIsolation, + CandidateIsolation = candidateIsolation, + SizeDeltaBytes = sizeDeltaBytes, + KldGain = kldGain, + Score = score, + HigherFidelitySteps = fidelitySteps + }); + } + } + + return result; + } + + private static List BuildPlans( + SmartFallbackRequest request, + BaselineQuants blanketBaseline, + ulong baseSize, + double baseKld, + IReadOnlyList options, + RankSafeKldPredictionService.RankSafePredictionModel context) + { + var plans = new List(); + var seen = new HashSet(StringComparer.Ordinal); + + void TryAddPlan(IEnumerable selected, string strategy) + { + var chosen = selected + .GroupBy(x => x.Group.UniqueId) + .Select(g => g.OrderByDescending(x => x.Score).ThenBy(x => x.SizeDeltaBytes).First()) + .OrderBy(x => x.Group.UniqueId) + .ToList(); + + if (chosen.Count == 0) + return; + + var quant = HybridQuant.CreateLearnedCandidateBlanket( + baseQuant: blanketBaseline, + groups: context.ActiveGroups, + candidateBaseline: blanketBaseline); + + foreach (var option in chosen) + quant.SetLearnedCandidateOverride(option.Group, option.CandidateBaseline); + + var config = (TensorConfig)quant; + string key = TensorConfigIdentity.ToKey(config); + if (!seen.Add(key)) + return; + + if (!TryPredictSize(config, context, out var predictedSize, out _) || + !TryComputeAdditiveKld(config, context, out var predictedKld, out _)) + return; + + if (request.StrictDominance) + { + if (predictedSize > request.WindowMaxSizeBytes) + return; + } + else if (predictedSize < request.WindowMinSizeBytes || predictedSize > request.WindowMaxSizeBytes) + { + return; + } + + double expectedLine = request.StrictDominance + ? request.HigherDamageAnchor.Kld + : InterpolateKldLine(predictedSize, request.HigherDamageAnchor, request.LowerDamageAnchor); + double gainOverLine = expectedLine - predictedKld; + double totalKldGain = baseKld - predictedKld; + long totalSizeDelta = (long)predictedSize - (long)baseSize; + + if (request.StrictDominance && totalKldGain <= Config.SelectionMinimumKldImprovementEpsilon) + return; + + double score = chosen.Sum(x => x.Score) + + Math.Max(0d, totalKldGain) * 100d + + Math.Max(0d, gainOverLine) * 25d; + + if (!request.StrictDominance) + { + // Premium/interior fallback is allowed to gamble, but it should still + // prefer plans that protect the most isolated damage per byte spent. + double budgetUsedFraction = request.WindowMaxSizeBytes <= request.WindowMinSizeBytes + ? 0d + : ((double)predictedSize - request.WindowMinSizeBytes) / Math.Max(1d, request.WindowMaxSizeBytes - request.WindowMinSizeBytes); + score += Math.Clamp(budgetUsedFraction, 0d, 1d) * Math.Max(0d, totalKldGain) * 50d; + } + + plans.Add(new SmartCandidatePlan + { + Quant = quant, + Config = config, + Strategy = strategy, + Options = chosen, + PredictedSizeBytes = predictedSize, + PredictedAdditiveKld = predictedKld, + LinearExpectedKld = expectedLine, + PredictedGainOverLine = gainOverLine, + TotalKldGain = totalKldGain, + TotalSizeDeltaBytes = totalSizeDelta, + Score = score + }); + } + + var freeLunch = options + .Where(x => x.SizeDeltaBytes <= 0) + .GroupBy(x => x.Group.UniqueId) + .Select(g => g.OrderByDescending(x => x.KldGain).ThenBy(x => x.SizeDeltaBytes).First()) + .ToList(); + + TryAddPlan(freeLunch, "free-lunch-same-or-smaller"); + + foreach (var single in options.OrderByDescending(x => x.Score).ThenBy(x => x.SizeDeltaBytes).Take(Math.Max(12, Config.SelectionSmartFallbackAttemptsPerFailure * 4))) + TryAddPlan(new[] { single }, "single-sensitive-group"); + + if (!request.StrictDominance) + { + var protectedSet = new List(); + protectedSet.AddRange(freeLunch); + + foreach (var option in options + .Where(x => x.SizeDeltaBytes > 0) + .OrderByDescending(x => x.Score) + .ThenBy(x => x.SizeDeltaBytes)) + { + var trial = protectedSet + .Where(x => x.Group.UniqueId != option.Group.UniqueId) + .Concat(new[] { option }) + .ToList(); + + var trialQuant = HybridQuant.CreateLearnedCandidateBlanket( + baseQuant: blanketBaseline, + groups: context.ActiveGroups, + candidateBaseline: blanketBaseline); + foreach (var selected in trial) + trialQuant.SetLearnedCandidateOverride(selected.Group, selected.CandidateBaseline); + + if (!TryPredictSize((TensorConfig)trialQuant, context, out var trialSize, out _)) + continue; + + if (trialSize <= request.WindowMaxSizeBytes) + protectedSet = trial; + } + + TryAddPlan(protectedSet, "balanced-brain-protection"); + + foreach (var groupedBySensitivity in options + .Where(x => x.SizeDeltaBytes >= 0) + .OrderByDescending(x => x.KldGain) + .ThenBy(x => x.SizeDeltaBytes) + .Take(Math.Max(8, Config.SelectionSmartFallbackAttemptsPerFailure * 3))) + { + var blend = freeLunch + .Where(x => x.Group.UniqueId != groupedBySensitivity.Group.UniqueId) + .Concat(new[] { groupedBySensitivity }); + TryAddPlan(blend, "sensitivity-first-blend"); + } + } + + return plans; + } + + private static HybridSelectionCandidate ToCandidate( + SmartFallbackRequest request, + BaselineQuants blanketBaseline, + SmartCandidatePlan plan, + int attemptOrder, + int optionCount, + int planCount, + int lineBeatingCount) + { + var notes = new List + { + "smartFallback=sqlite-isolation-truth; not selected from DuckDB prediction rows", + $"blanket={blanketBaseline.Names[0]}", + $"strategy={plan.Strategy}", + $"exactSizeWindow={ToGiB(request.WindowMinSizeBytes):0.00}..{ToGiB(request.WindowMaxSizeBytes):0.00}GiB", + $"smartAttemptsLimit={Config.SelectionSmartFallbackAttemptsPerFailure}", + $"maxHigherFidelitySteps={Config.SelectionSmartFallbackMaxHigherFidelitySteps}" + }; + + notes.AddRange(plan.Options.Select(option => + $"swap {option.Group.Name}: {blanketBaseline.Names[0]} -> {option.CandidateBaseline.Names[0]} " + + $"sizeDelta={option.SizeDeltaBytes:N0}B isolatedKldGain={option.KldGain:0.000000}")); + + var row = new RankSafePredictionRow + { + Config = plan.Config, + Quant = plan.Quant, + PredictedSizeBytes = plan.PredictedSizeBytes, + IsSizePredictable = true, + AdditiveKld = plan.PredictedAdditiveKld, + InteractionKld = plan.PredictedAdditiveKld, + PredictedKld = plan.PredictedAdditiveKld, + PredictionConfidence = 0.50d, + PredictedPpl = 0d, + CrossTerm = 0d, + IsPureBaseline = false, + IsPredictable = true, + HasUnknownMappings = false, + EffectiveStateKey = $"smart-fallback:{TensorConfigIdentity.ToKey(plan.Config)}", + PredictedRank = null, + Notes = notes + }; + + return new HybridSelectionCandidate + { + Prediction = row, + Reason = request.Reason, + LowerDamageAnchor = request.LowerDamageAnchor, + HigherDamageAnchor = request.HigherDamageAnchor, + WindowMinSizeBytes = request.WindowMinSizeBytes, + WindowMaxSizeBytes = request.WindowMaxSizeBytes, + PredictionWindowMinSizeBytes = request.WindowMinSizeBytes, + PredictionWindowMaxSizeBytes = request.WindowMaxSizeBytes, + LinearExpectedKld = plan.LinearExpectedKld, + PredictedGainOverLine = plan.PredictedGainOverLine, + AttemptOrder = attemptOrder, + WindowLabel = request.WindowLabel, + CandidatePoolSize = optionCount, + WindowCandidateCount = planCount, + LineBeatingCandidateCount = lineBeatingCount, + FetchedCandidateCount = planCount, + CandidatesAfterBrutalityCount = planCount, + CandidateAttemptLimit = Config.SelectionSmartFallbackAttemptsPerFailure, + PhaseWindowIndex = request.PhaseWindowIndex, + PhaseWindowCount = request.PhaseWindowCount, + RawSelectionRank = attemptOrder, + CandidateTheoryFamilyKey = "smart-baseline-tuning", + CandidateTheoryFamilyRank = 1, + CandidateTheoryFamilyMemberRank = attemptOrder, + CandidateTheoryFamilyDisplay = "smart baseline tuning", + DiversityMode = "sqlite-isolation-fallback", + CandidateSelectionNotes = notes + }; + } + + private static bool TryPredictSize( + TensorConfig config, + RankSafeKldPredictionService.RankSafePredictionModel context, + out ulong sizeBytes, + out IReadOnlyList notes) + { + var localNotes = new List(); + notes = localNotes; + sizeBytes = 0; + + if (!context.BaseOnlySnapshotsByBaselineId.TryGetValue(config.BaseQuant, out var baseOnlyAnchor)) + { + localNotes.Add($"Missing base-only anchor for baseline id {config.BaseQuant}."); + return false; + } + + long total = (long)baseOnlyAnchor.SizeBytes; + long q8ExactBlanketSize = (long)context.Q8BaseOnly.SizeBytes; + + foreach (var (group, effectiveBaselineId) in RankSafeKldPredictionService.EnumerateEffectiveBaselines(config, context.ActiveGroups)) + { + if (BaselineQuants.IsNativeExactAlias(effectiveBaselineId)) + continue; + + if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, effectiveBaselineId), out var snapshot)) + { + localNotes.Add($"Missing isolation size anchor for {group.Name}:{effectiveBaselineId}."); + return false; + } + + total += (long)snapshot.SizeBytes - q8ExactBlanketSize; + } + + if (total <= 0) + { + localNotes.Add($"Predicted size collapsed to {total:N0} bytes."); + return false; + } + + sizeBytes = (ulong)total; + return true; + } + + private static bool TryComputeAdditiveKld( + TensorConfig config, + RankSafeKldPredictionService.RankSafePredictionModel context, + out double additiveKld, + out IReadOnlyList notes) + { + var localNotes = new List(); + notes = localNotes; + additiveKld = 0d; + + foreach (var (group, effectiveBaselineId) in RankSafeKldPredictionService.EnumerateEffectiveBaselines(config, context.ActiveGroups)) + { + if (BaselineQuants.IsNativeExactAlias(effectiveBaselineId)) + continue; + + if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, effectiveBaselineId), out var snapshot)) + { + localNotes.Add($"Missing isolation KLD anchor for {group.Name}:{effectiveBaselineId}."); + return false; + } + + additiveKld += Math.Max(0d, snapshot.Kld); + } + + return true; + } + + private static int CountHigherFidelitySteps(BaselineQuants baseBaseline, BaselineQuants candidate) + { + if (candidate.BitRange <= baseBaseline.BitRange) + return 0; + + var ladder = BaselineQuants.GetAllRecognizedBaselines() + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .GroupBy(x => x.BitRange) + .Select(g => g.Key) + .OrderBy(x => x) + .ToList(); + + int baseIndex = ladder.IndexOf(baseBaseline.BitRange); + int candidateIndex = ladder.IndexOf(candidate.BitRange); + if (baseIndex < 0 || candidateIndex < 0) + return candidate.BitRange > baseBaseline.BitRange ? 1 : 0; + + return Math.Max(0, candidateIndex - baseIndex); + } + + private static double InterpolateKldLine( + ulong sizeBytes, + BenchmarkSnapshotRecord higherDamageSmaller, + BenchmarkSnapshotRecord lowerDamageLarger) + { + if (lowerDamageLarger.SizeBytes <= higherDamageSmaller.SizeBytes) + return Math.Min(higherDamageSmaller.Kld, lowerDamageLarger.Kld); + + double t = ((double)sizeBytes - higherDamageSmaller.SizeBytes) / + (lowerDamageLarger.SizeBytes - higherDamageSmaller.SizeBytes); + t = Math.Clamp(t, 0d, 1d); + return higherDamageSmaller.Kld + (lowerDamageLarger.Kld - higherDamageSmaller.Kld) * t; + } + + private static double ToGiB(ulong bytes) => bytes / 1024d / 1024d / 1024d; + + private sealed class SmartFallbackRequest + { + public HybridSelectionReason Reason { get; init; } + public string WindowLabel { get; init; } = string.Empty; + public BenchmarkSnapshotRecord BaselineAnchor { get; init; } = default!; + public BenchmarkSnapshotRecord HigherDamageAnchor { get; init; } = default!; + public BenchmarkSnapshotRecord LowerDamageAnchor { get; init; } = default!; + public ulong WindowMinSizeBytes { get; init; } + public ulong WindowMaxSizeBytes { get; init; } + public bool StrictDominance { get; init; } + public int PhaseWindowIndex { get; init; } + public int PhaseWindowCount { get; init; } + } + + private sealed class SmartGroupOption + { + public TensorGroup Group { get; init; } = default!; + public BaselineQuants CandidateBaseline { get; init; } = default!; + public BenchmarkSnapshotRecord BaseIsolation { get; init; } = default!; + public BenchmarkSnapshotRecord CandidateIsolation { get; init; } = default!; + public long SizeDeltaBytes { get; init; } + public double KldGain { get; init; } + public double Score { get; init; } + public int HigherFidelitySteps { get; init; } + } + + private sealed class SmartCandidatePlan + { + public HybridQuant Quant { get; init; } = default!; + public TensorConfig Config { get; init; } + public string Strategy { get; init; } = string.Empty; + public IReadOnlyList Options { get; init; } = Array.Empty(); + public ulong PredictedSizeBytes { get; init; } + public double PredictedAdditiveKld { get; init; } + public double LinearExpectedKld { get; init; } + public double PredictedGainOverLine { get; init; } + public double TotalKldGain { get; init; } + public long TotalSizeDeltaBytes { get; init; } + public double Score { get; init; } + } +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index eb9b86f..0474ce6 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -241,6 +241,15 @@ candidate_selection: # If the first predicted candidate fails real validation, try this many fallbacks. max_fallback_attempts_per_anchor: 5 + # Conservative SQLite/isolation-truth fallback. This runs only after the + # normal DuckDB prediction-guided attempts fail for a strict/premium/interior + # phase window. It starts from the anchor baseline blanket and only swaps + # tensor groups using surviving isolated group candidates, plus the baseline + # itself as the blanket state. + smart_fallback_enabled: true + smart_fallback_attempts_per_failure: 3 + smart_fallback_max_higher_fidelity_steps: 2 + # Strict epsilon for lower-KLD comparisons after real benchmark validation. minimum_kld_improvement_epsilon: 1.0e-9 @@ -500,4 +509,4 @@ synergy_detection: contaminating_passenger_detection_enabled: true min_failure_margin_for_contamination_kld: 0.00050 contamination_penalty_confidence_multiplier: 0.45 - suppress_repeated_contaminated_attempts: true + suppress_repeated_contaminated_attempts: true \ No newline at end of file diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index c46f18e..dc43967 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -162,6 +162,15 @@ candidate_selection: # If the first predicted candidate fails real validation, try this many fallbacks. max_fallback_attempts_per_anchor: 5 + + # Conservative SQLite/isolation-truth fallback. This runs only after the + # normal DuckDB prediction-guided attempts fail for a strict/premium/interior + # phase window. It starts from the anchor baseline blanket and only swaps + # tensor groups using surviving isolated group candidates, plus the baseline + # itself as the blanket state. + smart_fallback_enabled: true + smart_fallback_attempts_per_failure: 3 + smart_fallback_max_higher_fidelity_steps: 2 # Strict epsilon for lower-KLD comparisons after real benchmark validation. minimum_kld_improvement_epsilon: 1.0e-9 From 3e7eb5d17a36e9748f94da0a86daa6627aadbabc Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 10 May 2026 12:47:34 -0400 Subject: [PATCH 200/258] Still hammering out new setup but it's working better. --- MQ.DB/Models/TensorGroupSynergy.cs | 44 +++ MQ.DB/tensor_groups.yaml | 92 +++-- MagicQuant/Configs/config.dev.yaml | 406 ++++++++++++++++++++ MagicQuant/Helpers/TensorConfigGenerator.cs | 115 ++++-- MagicQuant/MagicQuant.csproj | 3 + MagicQuant/Program.cs | 2 +- MagicQuant/config.dev.yaml | 125 +++++- 7 files changed, 706 insertions(+), 81 deletions(-) create mode 100644 MQ.DB/Models/TensorGroupSynergy.cs create mode 100644 MagicQuant/Configs/config.dev.yaml diff --git a/MQ.DB/Models/TensorGroupSynergy.cs b/MQ.DB/Models/TensorGroupSynergy.cs new file mode 100644 index 0000000..08f3a40 --- /dev/null +++ b/MQ.DB/Models/TensorGroupSynergy.cs @@ -0,0 +1,44 @@ +using System.Collections.Immutable; + +namespace MQ.DB.Models; + +/// +/// Represents a static semantic relationship between tensor groups that are +/// independently tracked but expected to behave as a connected optimization unit. +/// +/// A synergy group does not replace normal tensor group identity. +/// It exists to describe known architectural coupling between groups. +/// +public record TensorGroupSynergy( + byte UniqueId, + string Name, + ImmutableArray Groups) +{ + public bool Contains(TensorGroup group) => + Groups.Any(g => g.UniqueId == group.UniqueId); + + public bool Contains(string groupName) => + Groups.Any(g => g.Name.Equals(groupName, StringComparison.OrdinalIgnoreCase)); + + public bool ContainsAny(IEnumerable groups) + { + foreach (var group in groups) + { + if (Contains(group)) + return true; + } + + return false; + } + + public bool ContainsAll(IEnumerable groups) + { + foreach (var group in groups) + { + if (!Contains(group)) + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/MQ.DB/tensor_groups.yaml b/MQ.DB/tensor_groups.yaml index e6cd041..24054ed 100644 --- a/MQ.DB/tensor_groups.yaml +++ b/MQ.DB/tensor_groups.yaml @@ -143,6 +143,27 @@ groups: # blk.N.ffn_up_shexp.weight # blk.N.ffn_gate_shexp.weight # are MoE expert-path tensors and belong to moe_experts. + # GGUF MoE routed/shared expert FFN up/gate payloads. + - "^blk\\..*\\.ffn_up_exps\\.weight$" + - "^blk\\..*\\.ffn_gate_exps\\.weight$" + - "^blk\\..*\\.ffn_up_shexp\\.weight$" + - "^blk\\..*\\.ffn_gate_shexp\\.weight$" + + # HF / framework expert forms. + - ".*experts?\\..*wi_0.*" + - ".*experts?\\..*wi_1.*" + - ".*experts?\\..*fc1.*" + - ".*experts?\\..*dense_h_to_4h.*" + - ".*experts?\\..*up_proj.*" + - ".*experts?\\..*gate_proj.*" + - ".*mlp\\.experts\\.gate_up_proj.*" + - ".*mlp\\.experts\\.gate_proj.*" + - ".*mlp\\.experts\\.up_proj.*" + - ".*mlp\\.shared_expert\\.gate_proj\\.weight$" + - ".*mlp\\.shared_expert\\.up_proj\\.weight$" + - ".*layers\\..*\\.experts\\.gate_up_proj.*" + - ".*layers\\..*\\.experts\\.gate_proj.*" + - ".*layers\\..*\\.experts\\.up_proj.*" ffn_down: description: "Dense FFN down-projection tensors only. Expert-path tensors are intentionally excluded and should be owned by moe_experts." @@ -177,53 +198,66 @@ groups: # blk.N.ffn_down_exps.weight # blk.N.ffn_down_shexp.weight # are MoE expert-path tensors and belong to moe_experts. + # GGUF MoE routed/shared expert FFN down payloads. + - "^blk\\..*\\.ffn_down_exps\\.weight$" + - "^blk\\..*\\.ffn_down_shexp\\.weight$" + + # HF / framework expert forms. + - ".*experts?\\..*wo.*" + - ".*experts?\\..*fc2.*" + - ".*experts?\\..*dense_4h_to_h.*" + - ".*experts?\\..*down_proj.*" + - ".*mlp\\.experts\\.down_proj.*" + - ".*mlp\\.shared_expert\\.down_proj\\.weight$" + - ".*layers\\..*\\.experts\\.down_proj.*" moe_experts: description: "MoE expert-path tensors, including routed experts and shared experts. These own *_exps and *_shexp forms so they do not collide with dense FFN groups." patterns: + - ".*experts?\\..*" # llama.cpp GGUF MoE routed expert tensors. - - "^blk\\..*\\.ffn_.*expert.*$" - - "^blk\\..*\\.ffn_.*exps.*$" - - "^blk\\..*\\.ffn_up_exps\\.weight$" - - "^blk\\..*\\.ffn_gate_exps\\.weight$" - - "^blk\\..*\\.ffn_down_exps\\.weight$" + #- "^blk\\..*\\.ffn_.*expert.*$" + #- "^blk\\..*\\.ffn_.*exps.*$" + #- "^blk\\..*\\.ffn_up_exps\\.weight$" + #- "^blk\\..*\\.ffn_gate_exps\\.weight$" + #- "^blk\\..*\\.ffn_down_exps\\.weight$" # Qwen3.6 shared-expert GGUF tensors. # These are material expert-path matrices and should not fall back through # base_quant_exceptions. - - "^blk\\..*\\.ffn_up_shexp\\.weight$" - - "^blk\\..*\\.ffn_gate_shexp\\.weight$" - - "^blk\\..*\\.ffn_down_shexp\\.weight$" + #- "^blk\\..*\\.ffn_up_shexp\\.weight$" + #- "^blk\\..*\\.ffn_gate_shexp\\.weight$" + #- "^blk\\..*\\.ffn_down_shexp\\.weight$" # Generic expert container forms used by several HF architectures. - - ".*experts?\\..*wi_0.*" - - ".*experts?\\..*wi_1.*" - - ".*experts?\\..*wo.*" - - ".*experts?\\..*fc1.*" - - ".*experts?\\..*fc2.*" - - ".*experts?\\..*dense_h_to_4h.*" - - ".*experts?\\..*dense_4h_to_h.*" - - ".*experts?\\..*up_proj.*" - - ".*experts?\\..*gate_proj.*" - - ".*experts?\\..*down_proj.*" + #- ".*experts?\\..*wi_0.*" + #- ".*experts?\\..*wi_1.*" + #- ".*experts?\\..*wo.*" + #- ".*experts?\\..*fc1.*" + #- ".*experts?\\..*fc2.*" + #- ".*experts?\\..*dense_h_to_4h.*" + #- ".*experts?\\..*dense_4h_to_h.*" + #- ".*experts?\\..*up_proj.*" + #- ".*experts?\\..*gate_proj.*" + #- ".*experts?\\..*down_proj.*" # Qwen3.5 / Qwen3.6 / modern HF MoE forms. - - ".*mlp\\.experts\\.gate_up_proj.*" - - ".*mlp\\.experts\\.gate_proj.*" - - ".*mlp\\.experts\\.up_proj.*" - - ".*mlp\\.experts\\.down_proj.*" + #- ".*mlp\\.experts\\.gate_up_proj.*" + #- ".*mlp\\.experts\\.gate_proj.*" + #- ".*mlp\\.experts\\.up_proj.*" + #- ".*mlp\\.experts\\.down_proj.*" # Shared experts are still MoE expert-path tensors, not normal dense FFN. # Keeping them here prevents them from double-counting as generic FFN. - - ".*mlp\\.shared_expert\\.gate_proj\\.weight$" - - ".*mlp\\.shared_expert\\.up_proj\\.weight$" - - ".*mlp\\.shared_expert\\.down_proj\\.weight$" + #- ".*mlp\\.shared_expert\\.gate_proj\\.weight$" + #- ".*mlp\\.shared_expert\\.up_proj\\.weight$" + #- ".*mlp\\.shared_expert\\.down_proj\\.weight$" # Gemma-style MoE forms. - - ".*layers\\..*\\.experts\\.gate_up_proj.*" - - ".*layers\\..*\\.experts\\.gate_proj.*" - - ".*layers\\..*\\.experts\\.up_proj.*" - - ".*layers\\..*\\.experts\\.down_proj.*" + #- ".*layers\\..*\\.experts\\.gate_up_proj.*" + #- ".*layers\\..*\\.experts\\.gate_proj.*" + #- ".*layers\\..*\\.experts\\.up_proj.*" + #- ".*layers\\..*\\.experts\\.down_proj.*" moe_router: description: "MoE router/gating tensors. Keep this MoE-specific so dense FFN gates, attention gates, and Qwen3.6 hybrid/SSM gates do not masquerade as routers." diff --git a/MagicQuant/Configs/config.dev.yaml b/MagicQuant/Configs/config.dev.yaml new file mode 100644 index 0000000..dc43967 --- /dev/null +++ b/MagicQuant/Configs/config.dev.yaml @@ -0,0 +1,406 @@ +paths: + magic_quant_root: + model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ + llama_root: + llama_bin: + convert_script: + scratch_roots: + - /mnt/world8/ + - /home/slurp/ + - /mnt/world7/ + external_baseline_cache_dir_name: ExternalBaselines + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +learning: + # Destructive relearn options are intentionally targeted. + # These are transient runtime commands and are not persisted as DB state. + # When any option below is enabled, MagicQuant prints a count summary and asks + # for confirmation before deleting/relearning anything. + # + # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, + # and execution probe cache rows scoped to the active architecture family. + # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, + # TensorCombo, or BaselineQuantDefinition rows. + force_relearn_architecture_family: false + + # Relearn built-in/standard baselines by display/canonical name for the current + # architecture family and active tensor group profile. + # Example: + # force_relearn_standard_baselines: + # - Q6_K + # - IQ4_XS + force_relearn_standard_baselines: [] + + # Safety gate for tensor group regex/profile changes. After MagicQuant reads the + # native BF16 GGUF tensor list, it prints group counts, example tensors, + # ambiguous matches, unresolved tensors, and base-quant exception counts, then + # asks before continuing. Keep this true unless running fully unattended. + confirm_tensor_group_profile: true + + # Safe/idempotent repair mode for accidental regex mistakes. + # + # Default true: on every run MagicQuant checks whether older DB learned tensor + # truth can be copied into the active TensorGroupProfile by reapplying the + # current regex/base_quant_exceptions rules. If nothing changed or current rows + # already exist, it skips cleanly and does not create duplicates. + # + # This avoids needless re-download/re-quantization of pure learning baselines + # after regex-only regrouping. Old benchmarks/learned rows remain attached to + # their original TensorGroupProfile and are ignored unless that profile becomes + # active again. + # + # Disable only when you intentionally want the slower/full path to regenerate + # learned grouping truth instead of rebucketing from DB snapshots. + # CLI disable aliases: + # --no-rebucket-learned-tensor-groups + # --disable-tensor-group-rebucket + # --full-relearn-tensor-groups + rebucket_learned_tensor_groups_from_existing_truth: true + + +readme: + # Optional title model name override used in: + # # MagicQuant Hybrids (v2.0) - + # If blank, MagicQuant uses identity.architecture_family_name. + title_model_name_override: Qwen3.6-27B + + # Hugging Face README frontmatter. + # Scalars render as: + # license: apache-2.0 + # Arrays render as: + # tags: + # - gguf + # - text-generation + # + # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. + frontmatter: + license: apache-2.0 + tags: + - gguf + - text-generation + - magicquant + - conversational + base_model: + - Qwen/Qwen3.6-27B + +hardware: + gpu_memory_limits_gb: + 0: 19 + 1: 23 + +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1_5m.jsonl + +# Legacy evolution survivor knobs were removed from YAML. +# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. + +isolation_pruning: + # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 + minimum_isolation_reduction_to_continue_ratio: 0.00 + minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 + maximum_isolation_ppl_delta_percent: 5.0 + maximum_isolation_kld: 0.1 + bad_trade_max_size_delta_percent: 4.0 + bad_trade_kld_multiplier: 2.5 + bad_trade_ppl_multiplier: 3.5 + floating_point_epsilon: 1.0e-8 + minimum_meaningful_base_only_reduction_ratio: 0.01 + + +prediction: + # Rank-safe isolation KLD predictor. + # + # manual_max_predicted_size_bytes is retained only as an emergency compatibility + # field for older helper code. Leave it at 0 for the new chooser. + manual_max_predicted_size_bytes: 0 + + # Candidate bit-stress thresholds for the low-bit interaction correction. + # The predictor fits each candidate threshold against existing category=General + # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. + bit_stress_threshold_candidates: + - 4.0 + - 5.0 + - 6.0 + - 7.0 + - 8.0 + - 9.0 + - 10.0 + - 11.0 + - 12.0 + + # Fallback threshold when too few benchmark rows exist to fit the interaction model. + default_bit_stress_threshold: 8.0 + + # Minimum benchmark rows required before fitting the interaction correction. + minimum_fit_rows: 12 + +candidate_selection: + + validate_all_anomaly_strict_candidates_after_success: false + + # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits + # inside this size premium and beats the real linear KLD improvement line. + near_baseline_max_size_growth_percent: 1.0 + + # Phase 3: interior windows between adjacent final anchors. + # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. + interior_window_fractions: + - 0.35 + - 0.35 + + # Number of predicted winners to keep per interior window. + max_candidates_per_interior_window: 1 + + # If the first predicted candidate fails real validation, try this many fallbacks. + max_fallback_attempts_per_anchor: 5 + + # Conservative SQLite/isolation-truth fallback. This runs only after the + # normal DuckDB prediction-guided attempts fail for a strict/premium/interior + # phase window. It starts from the anchor baseline blanket and only swaps + # tensor groups using surviving isolated group candidates, plus the baseline + # itself as the blanket state. + smart_fallback_enabled: true + smart_fallback_attempts_per_failure: 3 + smart_fallback_max_higher_fidelity_steps: 2 + + # Strict epsilon for lower-KLD comparisons after real benchmark validation. + minimum_kld_improvement_epsilon: 1.0e-9 + + # Final spacing pass: candidates closer than this fraction of the global survivor + # size span are collapsed unless one genuinely earns the slot. + minimum_neighbor_gap_fraction_of_global_span: 0.03 + + # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller + # anchor must provide a stronger KLD gain to justify its existence. + near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 + near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 + + # Default false: do not spend final prediction/build attempts trying to replace + # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. + # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. + allow_eight_bit_anchor_replacements: true + +anomaly_detection: + enabled: true + + # One anomaly refinement pass after smoke/probe/rule generation. + max_anomaly_refinement_rounds: 1 + + # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. + min_actual_gain_vs_twin_kld: 0.00025 + + # Minimum predicted size savings versus higher-bit twin/reference to probe. + min_predicted_size_savings_vs_twin_percent: 1.0 + + # Max changed groups in a candidate that can seed contextual probes. + max_probe_group_count: 4 + + # Max probes generated per anomaly seed. + max_probes_per_seed: 16 + + # Max anomaly probes in one run. + max_total_probes_per_run: 32 + + # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. + max_prediction_space_gap_vs_twin_kld: 0.00050 + + # Optional relative cap for prediction-space gap normalized by local anchor gap. + max_relative_prediction_penalty_vs_twin: 0.35 + + # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. + prediction_space_violation_margin: 0.00005 + + # Shrink applied to prediction-space adjustment after a rule is confirmed. + anomaly_adjustment_shrink_factor: 1.00 + + # Minimum confidence required before applying a confirmed anomaly rule. + min_rule_confidence_to_apply: 0.50 + + # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. + max_negative_adjustment_kld: 0.00400 + + # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. + max_positive_adjustment_kld: 0.00075 + + # Fractional cap relative to BaseRankSafeKld. + max_adjustment_fraction_of_base_kld: 0.75 + + # Number of top smoke candidates to consider per reference quant zone. + max_smoke_candidates_per_reference_zone: 12 + + # Store suppression-only results so false smoke is not repeatedly probed. + persist_suppression_results: true + + # Emit detailed anomaly logs. + verbose_anomaly_logging: true + + # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. + confirmed_anomaly_expansion: + enabled: true + max_neighbors_per_confirmed_rule: 6 + max_total_expansion_probes: 12 + allowed_reference_quants: + - Q8_0 + allowed_candidate_quants: + - Q6_K + - UD-Q6_K_XL + - Q5_K + - UD-Q5_K_XL + +output: + # Leave blank to default to /MagicQuant/Final_Outputs + output_dir: + output_name_prefix: Qwen3.6-27B + export_external_learned_baselines: true + + # false = normal behavior; delete/rebuild final outputs from scratch. + # true = preserve valid existing GGUFs and skip rebuilding them only when + # exact file name + byte size match benchmark truth. + # CLI --reuse-existing-final-artifacts overrides YAML. + reuse_existing_final_artifacts: false + +# Legacy bit-range bucket survival settings were removed. +# See candidate_selection above for the active final chooser settings. + +identity: + architecture_family_name: Qwen3.6-27B + allow_architecture_family_alias_override: false + +baselines: + standard_baselines_mode: all + enabled_standard_learning_baselines: [] + enabled_standard_combination_carriers: [] + enabled_standard_explicit_group_candidates: [] + + custom_repositories: + - repo_id: unsloth/Qwen3.6-27B-GGUF + enabled: true + short_source_name: Unsloth + source_kind: huggingface_gguf_repository + require_all_includes_to_resolve: true + validate_tensor_names_against_source_model: true + delete_partial_or_dirty_downloads: true + resume_or_retry_downloads: true + + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: false + + includes: + + - file_name: Qwen3.6-27B-UD-IQ2_M.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: UD-IQ2_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf + baseline_family: IQ2_XXS + quantize_base_name: IQ2_XXS + display_name: UD-IQ2_XXS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf + baseline_family: IQ3_XXS + quantize_base_name: IQ3_XXS + display_name: UD-IQ3_XXS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: UD-Q2_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_M + display_name: UD-Q3_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M + display_name: UD-Q4_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf + baseline_family: Q5_K + quantize_base_name: Q5_K + display_name: UD-Q5_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: UD-Q6_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + +# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. +# anomaly_detection remains the low-level compatibility section; synergy_detection controls +# template transfer, composition probes, contamination suppression, and wing diagnostics. +synergy_detection: + enabled: true + max_refinement_rounds: 1 + exact_context_confidence_multiplier: 1.00 + same_selected_groups_confidence_multiplier: 0.55 + equivalent_quant_family_confidence_multiplier: 0.30 + group_family_suspicion_confidence_multiplier: 0.15 + min_confidence_to_apply_adjustment: 0.35 + min_confidence_to_schedule_transfer_probe: 0.25 + max_negative_adjustment_kld: 0.002 + max_negative_adjustment_fraction_of_base_kld: 0.75 + transfer_probe_enabled: true + max_transfer_probes_per_template: 6 + max_total_transfer_probes_per_run: 24 + transfer_probe_context_strata: + high_fidelity_max_non_reference_groups_below_q6: 1 + mid_fidelity_max_non_reference_groups_below_q6: 3 + low_fidelity_enabled: false + verbose_synergy_logging: true + min_smoke_score: 0.55 + max_smoke_gap_kld: 0.004 + top_rejected_smoke_preview: 25 + composition_probe_enabled: true + max_template_composition_group_count: 4 + max_composition_probes_per_run: 8 + max_templates_to_compose: 4 + min_template_confidence_for_composition: 0.50 + min_combined_expected_size_savings_percent: 1.0 + contaminating_passenger_detection_enabled: true + min_failure_margin_for_contamination_kld: 0.00050 + contamination_penalty_confidence_multiplier: 0.45 + suppress_repeated_contaminated_attempts: true diff --git a/MagicQuant/Helpers/TensorConfigGenerator.cs b/MagicQuant/Helpers/TensorConfigGenerator.cs index 286d920..1441a61 100644 --- a/MagicQuant/Helpers/TensorConfigGenerator.cs +++ b/MagicQuant/Helpers/TensorConfigGenerator.cs @@ -7,7 +7,8 @@ namespace MagicQuant.Helpers; public static class TensorConfigGenerator { - public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan(List? missingTensorGroups = null) + public static RequiredSampleGenerationResult GenerateInitialIsolationSamplePlan( + List? missingTensorGroups = null) { if (missingTensorGroups != null && !missingTensorGroups.Any()) missingTensorGroups = null; @@ -53,7 +54,8 @@ void AddPureBaselinePlan(BaselineQuants baseline) { Kind = RequiredSampleKind.BaseOnlyIsolation, Key = $"baseonly:{baseline.UniqueId}", - Description = $"Base-only isolation for {string.Join("/", baseline.Names)} with all active groups forced native.", + Description = + $"Base-only isolation for {string.Join("/", baseline.Names)} with all active groups forced native.", Quant = HybridQuant.CreateExactBlanket( baseQuant: baseline, groups: activeGroups, @@ -113,8 +115,10 @@ void AddPureBaselinePlan(BaselineQuants baseline) } AnsiConsole.MarkupLine($"[bold green]Pure baselines required:[/] {result.PureBaselineCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Base-only isolation samples required:[/] {result.BaseOnlyIsolationCount:N0}"); - AnsiConsole.MarkupLine($"[bold green]Smallest-probe isolation samples required:[/] {result.GroupIsolationCount:N0}"); + AnsiConsole.MarkupLine( + $"[bold green]Base-only isolation samples required:[/] {result.BaseOnlyIsolationCount:N0}"); + AnsiConsole.MarkupLine( + $"[bold green]Smallest-probe isolation samples required:[/] {result.GroupIsolationCount:N0}"); AnsiConsole.MarkupLine($"[bold green]Total initial startup samples:[/] {result.TotalCount:N0}"); EmitSamplePlanDiagnostics("initial", result.Plans); @@ -139,7 +143,8 @@ public static RequiredSampleGenerationResult GenerateContinuationIsolationSample var result = BuildIsolationCoverageContinuationPlan(activeGroups, missingIds); - AnsiConsole.MarkupLine($"[bold green]Continuation isolation samples required:[/] {result.GroupIsolationCount:N0}"); + AnsiConsole.MarkupLine( + $"[bold green]Continuation isolation samples required:[/] {result.GroupIsolationCount:N0}"); EmitSamplePlanDiagnostics("continuation", result.Plans); return result; } @@ -160,14 +165,14 @@ public static RequiredSampleGenerationResult GenerateArchivalIsolationCoverageSa var missingIds = missingTensorGroups?.Select(x => x.UniqueId).ToHashSet() ?? new HashSet(); var archiveIds = groupIdsToArchive? - .Distinct() - .ToHashSet() - ?? new HashSet(); + .Distinct() + .ToHashSet() + ?? new HashSet(); var existingKeys = existingPlanKeys? - .Where(x => !string.IsNullOrWhiteSpace(x)) - .ToHashSet(StringComparer.Ordinal) - ?? new HashSet(StringComparer.Ordinal); + .Where(x => !string.IsNullOrWhiteSpace(x)) + .ToHashSet(StringComparer.Ordinal) + ?? new HashSet(StringComparer.Ordinal); var activeGroups = TReg.All .Where(x => !missingIds.Contains(x.UniqueId)) @@ -201,16 +206,26 @@ private static RequiredSampleGenerationResult BuildIsolationCoverageContinuation var carrier = BaselineQuants.Q8_0; var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + var blanketGroups = TReg.All .Where(x => !missingIds.Contains(x.UniqueId)) .ToList(); - var candidates = BaselineQuants.GetGroupCombinationCandidatesSmallestFirst( - RuntimeSearchSpace.HasUsableImatrix(), - allowHighPrecisionHybrids: false) + // IMPORTANT: + // Isolation coverage is NOT the same thing as runtime search-space eligibility. + // + // Prediction/RankSafe can reference external/custom/virtual anchor baseline ids + // for any active tensor group. Therefore every candidate identity needs a logical + // isolation snapshot for every active group. + // + // Physical work is still protected by isolation dedupe. If moe_router + UD-Q5_K_XL + // materializes to the same tensor->quant map as moe_router + Q8_0/F32, the artifact + // benchmark may be cloned/reused. But the duplicate TensorConfig identity must still + // exist in SQLite so RankSafe can resolve it exactly. + var candidates = GetIsolationCoverageCandidates() .ToList(); - foreach (var group in activeGroups) + foreach (var group in activeGroups.OrderBy(x => x.UniqueId)) { var smallest = GetSmallestAllowedProbeCandidateForGroup(group); @@ -230,7 +245,8 @@ private static RequiredSampleGenerationResult BuildIsolationCoverageContinuation { Kind = RequiredSampleKind.GroupIsolationContinuation, Key = $"cont:{carrier.UniqueId}:{group.UniqueId}:{candidate.UniqueId}", - Description = $"Continuation isolation for group '{group.Name}' using '{candidate.Names[0]}'.", + Description = + $"Continuation isolation coverage for group '{group.Name}' using '{candidate.Names[0]}'.", Quant = quant, TargetGroupId = group.UniqueId, TestedCandidateId = candidate.UniqueId, @@ -246,6 +262,30 @@ private static RequiredSampleGenerationResult BuildIsolationCoverageContinuation return result; } + private static BaselineQuants? GetSmallestAllowedProbeCandidateForGroup(TensorGroup group) + { + return GetIsolationCoverageCandidates() + .Where(x => !x.BannedGroupIds.Contains(group.UniqueId)) + .FirstOrDefault(); + } + + private static IEnumerable GetIsolationCoverageCandidates() + { + var hasUsableImatrix = RuntimeSearchSpace.HasUsableImatrix(); + + // This is intentionally broad. It is the identity universe RankSafe / virtual + // anchors may need exact snapshots for, not the narrowed per-group search space. + return BaselineQuants + .GetGroupCombinationCandidatesSmallestFirst( + hasUsableImatrix, + allowHighPrecisionHybrids: RuntimeSearchSpace.AllowHighPrecisionHybrids) + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .GroupBy(x => x.UniqueId) + .Select(g => g.First()) + .OrderBy(x => x.ExplicitCandidateSortOrder) + .ThenBy(x => x.UniqueId); + } + private static void EmitSamplePlanDiagnostics(string phase, IReadOnlyCollection plans) { if (!MagicQuantDiagnostics.VerboseIsolationPruning) @@ -261,16 +301,24 @@ private static void EmitSamplePlanDiagnostics(string phase, IReadOnlyCollection< var smallest = GetSmallestAllowedProbeCandidateForGroup(group); var allowed = RuntimeSearchSpace.GetAllowedRealExplicitCombinationCandidatesForGroup(group); var raw = RuntimeSearchSpace.GetRealExplicitCombinationCandidatesForGroup(group); - var staticBanned = BaselineQuants.GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), false) + var staticBanned = BaselineQuants + .GetGroupCombinationCandidates(RuntimeSearchSpace.HasUsableImatrix(), false) .Where(x => x.BannedGroupIds.Contains(group.UniqueId)).ToList(); - var runtimeBanned = raw.Where(x => RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x)).ToList(); + var runtimeBanned = raw.Where(x => RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, x)) + .ToList(); var notAllowed = planned.Where(x => allowed.All(a => a.UniqueId != x.UniqueId)).ToList(); - MagicQuantDiagnostics.Log("sample-plan", $"phase={phase} group={group.Name}(id={group.UniqueId}) plannedCount={planned.Count} smallestProbe={(smallest == null ? "" : MagicQuantDiagnostics.CandidateLabel(smallest))}"); - MagicQuantDiagnostics.Log("sample-plan", $"planned={string.Join(", ", planned.Select(MagicQuantDiagnostics.CandidateLabel))}"); - MagicQuantDiagnostics.Log("sample-plan", $"allowedAtPlan={string.Join(", ", allowed.Select(MagicQuantDiagnostics.CandidateLabel))}"); - MagicQuantDiagnostics.Log("sample-plan", $"staticBanned={string.Join(", ", staticBanned.Select(MagicQuantDiagnostics.CandidateLabel))}"); - MagicQuantDiagnostics.Log("sample-plan", $"runtimeBanned={string.Join(", ", runtimeBanned.Select(MagicQuantDiagnostics.CandidateLabel))}"); - MagicQuantDiagnostics.Log("sample-plan", $"plannedButNotAllowed={string.Join(", ", notAllowed.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", + $"phase={phase} group={group.Name}(id={group.UniqueId}) plannedCount={planned.Count} smallestProbe={(smallest == null ? "" : MagicQuantDiagnostics.CandidateLabel(smallest))}"); + MagicQuantDiagnostics.Log("sample-plan", + $"planned={string.Join(", ", planned.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", + $"allowedAtPlan={string.Join(", ", allowed.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", + $"staticBanned={string.Join(", ", staticBanned.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", + $"runtimeBanned={string.Join(", ", runtimeBanned.Select(MagicQuantDiagnostics.CandidateLabel))}"); + MagicQuantDiagnostics.Log("sample-plan", + $"plannedButNotAllowed={string.Join(", ", notAllowed.Select(MagicQuantDiagnostics.CandidateLabel))}"); } } @@ -304,10 +352,12 @@ public static IEnumerable> GenerateTensorConfigBatches( for (int i = 0; i < allowed.Length; i++) { if (allowed[i] == null) - throw new InvalidOperationException($"Allowed[{i}] is null for base {string.Join("/", baseQuant.Names)}."); + throw new InvalidOperationException( + $"Allowed[{i}] is null for base {string.Join("/", baseQuant.Names)}."); if (allowed[i].Length == 0) - throw new InvalidOperationException($"Allowed[{i}] is empty for base {string.Join("/", baseQuant.Names)}."); + throw new InvalidOperationException( + $"Allowed[{i}] is empty for base {string.Join("/", baseQuant.Names)}."); } int dims = allowed.Length; @@ -390,17 +440,6 @@ public static IEnumerable> GenerateTensorConfigBatches( producer.GetAwaiter().GetResult(); } - private static BaselineQuants? GetSmallestAllowedProbeCandidateForGroup(TensorGroup group) - { - foreach (var candidate in BaselineQuants.GetGroupCombinationCandidatesSmallestFirst( - RuntimeSearchSpace.HasUsableImatrix(), - allowHighPrecisionHybrids: false)) - { - return candidate; - } - - return null; - } private static int GetThreadCountSafe() { @@ -420,4 +459,4 @@ private static int ComputeWorkerThreads(int threadCount) return Math.Clamp(workers, 1, Math.Max(1, threadCount - 1)); } -} +} \ No newline at end of file diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 8352882..c3f1828 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -26,6 +26,9 @@ Always + + Always + diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 346a383..2494904 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -33,7 +33,7 @@ args = [ "evolution", - "--architecture-family", @"""Qwen3.6-27B""" + "--architecture-family", @"""Qwen3.6-35B-A3B""" ,"--reuse-existing-final-artifacts" ]; } diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index dc43967..7584b45 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ + model_dir: /mnt/world8/AI/Models/Qwen3.6-35B-A3B-Qwen/ llama_root: llama_bin: convert_script: @@ -67,7 +67,7 @@ readme: # Optional title model name override used in: # # MagicQuant Hybrids (v2.0) - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.6-27B + title_model_name_override: Qwen3.6-35B-A3B # Hugging Face README frontmatter. # Scalars render as: @@ -86,7 +86,7 @@ readme: - magicquant - conversational base_model: - - Qwen/Qwen3.6-27B + - Qwen/Qwen3.6-35B-A3B hardware: gpu_memory_limits_gb: @@ -272,7 +272,7 @@ output: # See candidate_selection above for the active final chooser settings. identity: - architecture_family_name: Qwen3.6-27B + architecture_family_name: Qwen3.6-35B-A3B allow_architecture_family_alias_override: false baselines: @@ -282,7 +282,7 @@ baselines: enabled_standard_explicit_group_candidates: [] custom_repositories: - - repo_id: unsloth/Qwen3.6-27B-GGUF + - repo_id: unsloth/Qwen3.6-35B-A3B-GGUF enabled: true short_source_name: Unsloth source_kind: huggingface_gguf_repository @@ -297,7 +297,7 @@ baselines: includes: - - file_name: Qwen3.6-27B-UD-IQ2_M.gguf + - file_name: Qwen3.6-35B-A3B-UD-IQ2_M.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-IQ2_M @@ -306,7 +306,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf + - file_name: Qwen3.6-35B-A3B-UD-IQ2_XXS.gguf baseline_family: IQ2_XXS quantize_base_name: IQ2_XXS display_name: UD-IQ2_XXS @@ -315,7 +315,16 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf + - file_name: Qwen3.6-35B-A3B-UD-IQ3_S.gguf + baseline_family: IQ3_S + quantize_base_name: IQ3_S + display_name: UD-IQ3_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ3_XXS.gguf baseline_family: IQ3_XXS quantize_base_name: IQ3_XXS display_name: UD-IQ3_XXS @@ -324,7 +333,34 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf + - file_name: Qwen3.6-35B-A3B-UD-IQ4_NL.gguf + baseline_family: IQ4_NL + quantize_base_name: IQ4_NL + display_name: UD-IQ4_NL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ4_NL_XL.gguf + baseline_family: IQ4_NL + quantize_base_name: IQ4_NL + display_name: UD-IQ4_NL_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-IQ4_XS.gguf + baseline_family: IQ4_XS + quantize_base_name: IQ4_XS + display_name: UD-IQ4_XS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q2_K_XL.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-Q2_K_XL @@ -333,7 +369,25 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf + - file_name: Qwen3.6-35B-A3B-UD-Q3_K_M.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_M + display_name: UD-Q3_K_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q3_K_S.gguf + baseline_family: IQ3_S + quantize_base_name: IQ3_S + display_name: UD-Q3_K_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q3_K_XL.gguf baseline_family: IQ3_M quantize_base_name: IQ3_M display_name: UD-Q3_K_XL @@ -342,7 +396,25 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf + - file_name: Qwen3.6-35B-A3B-UD-Q4_K_M.gguf + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M + display_name: UD-Q4_K_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q4_K_S.gguf + baseline_family: Q4_K_S + quantize_base_name: Q4_K_S + display_name: UD-Q4_K_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M display_name: UD-Q4_K_XL @@ -351,7 +423,25 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf + - file_name: Qwen3.6-35B-A3B-UD-Q5_K_M.gguf + baseline_family: Q5_K + quantize_base_name: Q5_K + display_name: UD-Q5_K_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q5_K_S.gguf + baseline_family: Q5_K_S + quantize_base_name: Q5_K_S + display_name: UD-Q5_K_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q5_K_XL.gguf baseline_family: Q5_K quantize_base_name: Q5_K display_name: UD-Q5_K_XL @@ -360,7 +450,16 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf + - file_name: Qwen3.6-35B-A3B-UD-Q6_K.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: UD-Q6_K + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.6-35B-A3B-UD-Q6_K_XL.gguf baseline_family: Q6_K quantize_base_name: Q6_K display_name: UD-Q6_K_XL From 7eeee4cc85c82fb31bff291c6af2be7b69aebe4f Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 10 May 2026 14:35:27 -0400 Subject: [PATCH 201/258] still having issues but we're doing much better now. --- MQ.DB/Models/TensorGroupSynergy.cs | 35 +++- MagicQuant/Commands/Evolution.cs | 1 + MagicQuant/Helpers/RuntimeSearchSpace.cs | 31 ++- .../IsolationDiagnosticsManifestService.cs | 6 +- .../Services/IsolationOptimizationService.cs | 197 +++++++++++++++++- .../SmartBaselineTuningFallbackService.cs | 188 ++++++++++++----- 6 files changed, 403 insertions(+), 55 deletions(-) diff --git a/MQ.DB/Models/TensorGroupSynergy.cs b/MQ.DB/Models/TensorGroupSynergy.cs index 08f3a40..1c71b21 100644 --- a/MQ.DB/Models/TensorGroupSynergy.cs +++ b/MQ.DB/Models/TensorGroupSynergy.cs @@ -41,4 +41,37 @@ public bool ContainsAll(IEnumerable groups) return true; } -} \ No newline at end of file +} + +/// +/// Static registry for tensor-group relationships that should receive synergy-aware +/// second-chance review after normal bad-trade pruning. +/// +/// Keep this intentionally small and explicit. Synergy is an architectural exception, +/// not a fuzzy runtime heuristic. +/// +public static class TensorGroupSynergies +{ + /// + /// Feed-forward projection groups are independently measurable, but their + /// downstream hybrid behavior can be coupled enough that a candidate surviving + /// in one group deserves a KLD-only second chance in the other when the only + /// normal bad-trade objection was PPL volatility. + /// + public static TensorGroupSynergy FeedForwardUpGateDown { get; } = new( + UniqueId: 0, + Name: "ffn_up_gate+ffn_down", + Groups: ImmutableArray.Create(TReg.FfnUpGate, TReg.FfnDown)); + + public static ImmutableArray All { get; } = + ImmutableArray.Create(FeedForwardUpGateDown); + + public static ImmutableArray GetContainingSynergies(TensorGroup group) => + All.Where(x => x.Contains(group)).ToImmutableArray(); + + public static bool IsInAnySynergy(TensorGroup group) => + All.Any(x => x.Contains(group)); + + public static bool AreInSameSynergy(TensorGroup left, TensorGroup right) => + All.Any(x => x.Contains(left) && x.Contains(right)); +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 0a6098a..e3dd6fd 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -354,6 +354,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupCandidatesBanned:N0}"); AnsiConsole.MarkupLine($"[green]Bad trade eliminations:[/] {isolationResult.BadTradeEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Synergy second-chance reinstatements:[/] {isolationResult.SynergySecondChanceReinstatements:N0}"); AnsiConsole.MarkupLine($"[green]Final KLD cleanup eliminations:[/] {isolationResult.FinalKldCleanupEliminations:N0}"); AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); diff --git a/MagicQuant/Helpers/RuntimeSearchSpace.cs b/MagicQuant/Helpers/RuntimeSearchSpace.cs index dcd1e0d..0206367 100644 --- a/MagicQuant/Helpers/RuntimeSearchSpace.cs +++ b/MagicQuant/Helpers/RuntimeSearchSpace.cs @@ -71,6 +71,35 @@ public static void BanCombinationCandidateForGroup( MagicQuantDiagnostics.LogRuntimeMutation(phase, group, candidate, reason, before, after); } + public static bool UnbanCombinationCandidateForGroup( + TensorGroup group, + BaselineQuants candidate, + string phase = "Unknown", + string reason = "unspecified") + { + int before = GetAllowedRealExplicitCombinationCandidatesForGroup(group).Count; + + if (!ExplicitCandidateBansByGroup.TryGetValue(group.UniqueId, out var set) || + !set.Remove(candidate.UniqueId)) + { + return false; + } + + if (set.Count == 0) + ExplicitCandidateBansByGroup.Remove(group.UniqueId); + + if (ExplicitCandidateBanReasonsByGroup.TryGetValue(group.UniqueId, out var reasonMap)) + { + reasonMap.Remove(candidate.UniqueId); + if (reasonMap.Count == 0) + ExplicitCandidateBanReasonsByGroup.Remove(group.UniqueId); + } + + int after = GetAllowedRealExplicitCombinationCandidatesForGroup(group).Count; + MagicQuantDiagnostics.LogRuntimeMutation(phase, group, candidate, $"restored: {reason}", before, after); + return true; + } + public static void BanCombinationCandidateForGroupDueToLearnedSchemeMismatch( TensorGroup group, BaselineQuants candidate, @@ -277,4 +306,4 @@ public static bool IsSchemeRuntimeBannedForGroup(TensorGroup group, TensorWeight [Obsolete("Use IsGroupExplicitCandidateBanned.")] public static bool IsGroupExplicitQuantBanned(TensorGroup group) => IsGroupExplicitCandidateBanned(group); -} +} \ No newline at end of file diff --git a/MagicQuant/Services/IsolationDiagnosticsManifestService.cs b/MagicQuant/Services/IsolationDiagnosticsManifestService.cs index e06e21d..3a5a0b1 100644 --- a/MagicQuant/Services/IsolationDiagnosticsManifestService.cs +++ b/MagicQuant/Services/IsolationDiagnosticsManifestService.cs @@ -46,9 +46,11 @@ public async Task GenerateBadTradesAsync( summary = new { badTradeEliminations = isolationResult.BadTradeEliminations, + synergySecondChanceReinstatements = isolationResult.SynergySecondChanceReinstatements, finalKldCleanupEliminations = isolationResult.FinalKldCleanupEliminations, disabledBaselines = isolationResult.DisabledBaselines, - structuredBadTradeRows = isolationResult.BadTradeDetails.Count + structuredBadTradeRows = isolationResult.BadTradeDetails.Count, + structuredSynergySecondChanceRows = isolationResult.SynergySecondChanceDetails.Count }, thresholds = new { @@ -58,8 +60,10 @@ public async Task GenerateBadTradesAsync( floatingPointEpsilon = IsolationPruningConfig.FloatingPointEpsilon }, badTrades = isolationResult.BadTradeDetails, + synergySecondChances = isolationResult.SynergySecondChanceDetails, notes = isolationResult.Notes .Where(x => x.Contains("bad trade", StringComparison.OrdinalIgnoreCase) || + x.Contains("synergy", StringComparison.OrdinalIgnoreCase) || x.Contains("carrier anchor", StringComparison.OrdinalIgnoreCase) || x.Contains("combination baseline", StringComparison.OrdinalIgnoreCase)) .Distinct(StringComparer.Ordinal) diff --git a/MagicQuant/Services/IsolationOptimizationService.cs b/MagicQuant/Services/IsolationOptimizationService.cs index 9af6f27..f47fc04 100644 --- a/MagicQuant/Services/IsolationOptimizationService.cs +++ b/MagicQuant/Services/IsolationOptimizationService.cs @@ -48,9 +48,12 @@ public sealed class IsolationOptimizationResult public int DisabledBaselines { get; set; } public int Bf16SuppressedGroups { get; set; } + public int SynergySecondChanceReinstatements { get; set; } + public List Notes { get; set; } = new(); public List GroupDetails { get; set; } = new(); public List BadTradeDetails { get; set; } = new(); + public List SynergySecondChanceDetails { get; set; } = new(); } public sealed class IsolationBadTradeRecord @@ -75,6 +78,23 @@ public sealed class IsolationBadTradeRecord public double PplAbsRatio { get; set; } } +public sealed class IsolationSynergySecondChanceRecord +{ + public string SynergyName { get; set; } = string.Empty; + public string GroupName { get; set; } = string.Empty; + public string PeerGroupName { get; set; } = string.Empty; + public string RestoredCandidate { get; set; } = string.Empty; + public string AcceptedAnchor { get; set; } = string.Empty; + public string OriginalBadTradeReason { get; set; } = string.Empty; + public string SecondChanceReason { get; set; } = string.Empty; + public ulong RestoredSizeBytes { get; set; } + public double RestoredKld { get; set; } + public double RestoredPplDeltaPercent { get; set; } + public ulong AnchorSizeBytes { get; set; } + public double AnchorKld { get; set; } + public double AnchorPplDeltaPercent { get; set; } +} + public class IsolationOptimizationService { public async Task AnalyzeInitialIsolationProbesAsync( @@ -200,6 +220,9 @@ public async Task AnalyzeAndApplyFinalAsync( .OrderBy(x => x.Key) .ToList(); + var groupWorkItems = new List(); + var retainedBadTradeEliminations = new List(); + foreach (var groupSet in groupPlans) { var group = TReg.All.First(x => x.UniqueId == groupSet.Key); @@ -267,8 +290,24 @@ public async Task AnalyzeAndApplyFinalAsync( candidates = FilterSurvivors(group, candidates); ApplyDominanceElimination(group, candidates, result); candidates = FilterSurvivors(group, candidates); - ApplyBadTradeElimination(group, candidates, result); - candidates = FilterSurvivors(group, candidates); + ApplyBadTradeElimination(group, candidates, result, retainedBadTradeEliminations); + + groupWorkItems.Add(new GroupIsolationWorkItem + { + Group = group, + Candidates = candidates, + Decision = decision + }); + } + + ApplySynergySecondChanceReview(groupWorkItems, retainedBadTradeEliminations, result); + + foreach (var workItem in groupWorkItems) + { + var group = workItem.Group; + var decision = workItem.Decision; + var candidates = FilterSurvivors(group, workItem.Candidates); + ApplyFinalKldCleanupElimination(group, candidates, result); candidates = FilterSurvivors(group, candidates); ApplyEquivalentTruthElimination(group, candidates, result); @@ -545,7 +584,11 @@ private static void ApplyDominanceElimination(TensorGroup group, List candidates, IsolationOptimizationResult result) + private static void ApplyBadTradeElimination( + TensorGroup group, + List candidates, + IsolationOptimizationResult result, + List? retainedBadTradeEliminations = null) { var activeCandidates = GetActiveExplicitCandidates(group, candidates, phase: "BadTrade"); if (activeCandidates.Count <= 1) @@ -584,6 +627,15 @@ private static void ApplyBadTradeElimination(TensorGroup group, List groupWorkItems, + IReadOnlyList badTradeEliminations, + IsolationOptimizationResult result) + { + if (groupWorkItems.Count == 0 || badTradeEliminations.Count == 0) + return; + + var workByGroupId = groupWorkItems.ToDictionary(x => x.Group.UniqueId); + var activeCandidateIdsByGroup = groupWorkItems.ToDictionary( + x => x.Group.UniqueId, + x => FilterSurvivors(x.Group, x.Candidates) + .Where(c => !IsHighPrecisionCandidate(c.CandidateBaseline)) + .Select(c => c.CandidateBaseline.UniqueId) + .ToHashSet()); + + var restoredKeys = new HashSet<(byte GroupId, byte CandidateId)>(); + + foreach (var synergy in TensorGroupSynergies.All) + { + var members = synergy.Groups + .Where(g => workByGroupId.ContainsKey(g.UniqueId)) + .ToList(); + + if (members.Count < 2) + continue; + + foreach (var eliminated in badTradeEliminations + .Where(x => synergy.Contains(x.Group)) + .OrderBy(x => x.Group.UniqueId) + .ThenBy(x => x.Removed.CandidateBaseline.UniqueId)) + { + var group = eliminated.Group; + var candidate = eliminated.Removed; + var candidateId = candidate.CandidateBaseline.UniqueId; + + if (!RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(group, candidate.CandidateBaseline)) + continue; + + if (!restoredKeys.Add((group.UniqueId, candidateId))) + continue; + + var peer = members + .Where(g => g.UniqueId != group.UniqueId) + .FirstOrDefault(g => activeCandidateIdsByGroup.TryGetValue(g.UniqueId, out var ids) && ids.Contains(candidateId)); + + if (peer == null) + continue; + + if (ShouldEliminateAsBadTradeIgnoringPpl(eliminated.Anchor, candidate, out var kldOnlyReason)) + { + result.Notes.Add( + $"Synergy second-chance rejected: '{candidate.CandidateBaseline.Names[0]}' remained removed for '{group.Name}' even though it survived in '{peer.Name}' under synergy '{synergy.Name}'. {kldOnlyReason}"); + continue; + } + + string restoreReason = + $"synergy '{synergy.Name}' second chance because '{candidate.CandidateBaseline.Names[0]}' survived in peer group '{peer.Name}' and the original bad-trade decision does not survive KLD-only review vs anchor '{eliminated.Anchor.CandidateBaseline.Names[0]}'"; + + if (!RuntimeSearchSpace.UnbanCombinationCandidateForGroup( + group, + candidate.CandidateBaseline, + phase: "SynergySecondChance", + reason: restoreReason)) + { + continue; + } + + if (activeCandidateIdsByGroup.TryGetValue(group.UniqueId, out var groupIds)) + groupIds.Add(candidateId); + + result.SynergySecondChanceReinstatements++; + result.SynergySecondChanceDetails.Add(new IsolationSynergySecondChanceRecord + { + SynergyName = synergy.Name, + GroupName = group.Name, + PeerGroupName = peer.Name, + RestoredCandidate = candidate.CandidateBaseline.Names[0], + AcceptedAnchor = eliminated.Anchor.CandidateBaseline.Names[0], + OriginalBadTradeReason = eliminated.Reason, + SecondChanceReason = restoreReason, + RestoredSizeBytes = candidate.SizeBytes, + RestoredKld = candidate.Kld, + RestoredPplDeltaPercent = candidate.PplDeltaPercent, + AnchorSizeBytes = eliminated.Anchor.SizeBytes, + AnchorKld = eliminated.Anchor.Kld, + AnchorPplDeltaPercent = eliminated.Anchor.PplDeltaPercent + }); + + result.Notes.Add( + $"Synergy second-chance restored: '{candidate.CandidateBaseline.Names[0]}' restored for '{group.Name}' because it survived in peer group '{peer.Name}' under synergy '{synergy.Name}', and KLD-only bad-trade review did not eliminate it vs anchor '{eliminated.Anchor.CandidateBaseline.Names[0]}'."); + } + } + } + + private static bool ShouldEliminateAsBadTradeIgnoringPpl( + GroupCandidateEvaluation anchor, + GroupCandidateEvaluation candidate, + out string reason) + { + reason = string.Empty; + + if (anchor.SizeBytes <= candidate.SizeBytes) + return false; + + double sizeDeltaPercent = ((double)anchor.SizeBytes - candidate.SizeBytes) / anchor.SizeBytes * 100.0; + if (sizeDeltaPercent > IsolationPruningConfig.BadTradeMaxSizeDeltaPercent) + return false; + + double kldRatio = anchor.Kld <= IsolationPruningConfig.FloatingPointEpsilon + ? double.PositiveInfinity + : candidate.Kld / anchor.Kld; + + bool kldBadTrade = candidate.Kld > anchor.Kld * IsolationPruningConfig.BadTradeKldMultiplier; + if (!kldBadTrade) + return false; + + reason = + $"Reason: small size gain ({sizeDeltaPercent:F2}%) but disproportionate KLD damage after PPL was ignored for synergy review (KLD x{kldRatio:F2})."; + + return true; + } + + private static void ApplyFinalKldCleanupElimination( TensorGroup group, List candidates, @@ -1218,6 +1394,21 @@ private static double GetAggregatePplDeltaPercent(BenchmarkSnapshot snapshot, Be return deltas.Count == 0 ? double.PositiveInfinity : deltas.Average(); } + private sealed class GroupIsolationWorkItem + { + public TensorGroup Group { get; set; } = default!; + public List Candidates { get; set; } = new(); + public IsolationGroupDecision Decision { get; set; } = default!; + } + + private sealed class SynergyBadTradeElimination + { + public TensorGroup Group { get; set; } = default!; + public GroupCandidateEvaluation Removed { get; set; } = default!; + public GroupCandidateEvaluation Anchor { get; set; } = default!; + public string Reason { get; set; } = string.Empty; + } + private sealed class GroupCandidateEvaluation { public TensorGroup Group { get; set; } = default!; diff --git a/MagicQuant/Services/SmartBaselineTuningFallbackService.cs b/MagicQuant/Services/SmartBaselineTuningFallbackService.cs index 80f1921..88a358a 100644 --- a/MagicQuant/Services/SmartBaselineTuningFallbackService.cs +++ b/MagicQuant/Services/SmartBaselineTuningFallbackService.cs @@ -10,13 +10,13 @@ namespace MagicQuant.Services; /// Conservative, non-DuckDB fallback used only after the normal prediction-guided /// selector fails to validate a candidate for a strict/premium/interior phase. /// -/// The service starts from a uniform learned-baseline blanket, keeps that blanket -/// baseline available for every group even if the baseline was pruned from a group, -/// and only swaps in group candidates that survived isolation pruning. It does not -/// create the normal prediction-space gremlin trades: shrinking is only allowed when -/// the isolated group sample is same-size-or-smaller and measurably lower KLD than -/// the blanket state; higher-fidelity protection is bounded by config and must fit -/// the target real-size window exactly. +/// The service starts from the real benchmarked pure/uniform baseline anchor, keeps +/// that blanket baseline available for every group even if the baseline was pruned +/// from a group, and only swaps in group candidates that survived isolation pruning. +/// It does not create the normal prediction-space gremlin trades: shrinking is only +/// allowed when the isolated group sample is same-size-or-smaller and measurably lower +/// KLD than the blanket state; higher-fidelity protection is bounded by config and +/// must fit the target real-size window exactly. /// public sealed class SmartBaselineTuningFallbackService { @@ -122,27 +122,11 @@ private async Task> BuildCandidatesAsync } var context = await GetContextAsync(ct); - if (!context.BaseOnlySnapshotsByBaselineId.ContainsKey(blanketBaseline.UniqueId)) - { - AnsiConsole.MarkupLine($"[grey]Smart fallback skipped:[/] missing exact base-only anchor for {Markup.Escape(blanketBaseline.Names[0])}."); - return Array.Empty(); - } - - var baseBlanket = HybridQuant.CreateLearnedCandidateBlanket( - baseQuant: blanketBaseline, - groups: context.ActiveGroups, - candidateBaseline: blanketBaseline); - var baseConfig = (TensorConfig)baseBlanket; - - var hasBaseSize = TryPredictSize(baseConfig, context, out var baseSize, out var baseSizeNotes); - var hasBaseKld = TryComputeAdditiveKld(baseConfig, context, out var baseKld, out var baseKldNotes); + var blanketAnchor = ResolveSmartBlanketAnchor(request.BaselineAnchor, blanketBaseline, context); + ValidateBlanketIsolationCoverage(blanketBaseline, context); - if (!hasBaseSize || !hasBaseKld) - { - var notes = baseSizeNotes.Concat(baseKldNotes).Distinct().ToList(); - AnsiConsole.MarkupLine($"[grey]Smart fallback skipped:[/] incomplete isolation truth for {Markup.Escape(blanketBaseline.Names[0])} blanket. {Markup.Escape(string.Join(" ", notes.Take(2)))}"); - return Array.Empty(); - } + var baseSize = blanketAnchor.SizeBytes; + var baseKld = Math.Max(0d, blanketAnchor.Kld); if (request.StrictDominance && baseSize > request.WindowMaxSizeBytes) { @@ -160,7 +144,7 @@ private async Task> BuildCandidatesAsync return Array.Empty(); } - var plans = BuildPlans(request, blanketBaseline, baseSize, baseKld, options, context); + var plans = BuildPlans(request, blanketBaseline, blanketAnchor, baseSize, baseKld, options, context); if (plans.Count == 0) { AnsiConsole.MarkupLine($"[grey]Smart fallback found no size-safe plans for[/] [cyan]{Markup.Escape(blanketBaseline.Names[0])}[/] in window {Markup.Escape(request.WindowLabel)}."); @@ -225,6 +209,78 @@ private static bool TryResolveBaselineBlanket( return true; } + private static BenchmarkSnapshotRecord ResolveSmartBlanketAnchor( + BenchmarkSnapshotRecord anchor, + BaselineQuants blanketBaseline, + RankSafeKldPredictionService.RankSafePredictionModel context) + { + if (TensorConfigIdentity.IsPureBaseline(anchor.Config)) + { + if (!context.PureSnapshotsByBaselineId.TryGetValue(blanketBaseline.UniqueId, out _)) + { + throw new InvalidOperationException( + $"Smart baseline fallback critical truth error: pure benchmark snapshot for '{blanketBaseline.Names[0]}' (id {blanketBaseline.UniqueId}) was not loaded, " + + $"but strict/near/interior fallback is trying to tune from anchor '{anchor.DisplayName}'. " + + "This is not a soft skip; the original baseline anchor is missing from the prediction context."); + } + + /* + * Preserve the exact anchor that triggered the fallback. The dictionary check above is a + * consistency guard proving the pure baseline truth exists in the loaded context; using + * request.BaselineAnchor keeps size/KLD aligned with the active strict/near/interior frontier. + */ + return anchor; + } + + if (!IsUniformLearnedBlanket(anchor, blanketBaseline)) + { + throw new InvalidOperationException( + $"Smart baseline fallback critical truth error: anchor '{anchor.DisplayName}' resolved to blanket '{blanketBaseline.Names[0]}', " + + "but the anchor is not a pure baseline and not a uniform learned-candidate blanket. This should have been rejected before anchor resolution."); + } + + return anchor; + } + + private static bool IsUniformLearnedBlanket(BenchmarkSnapshotRecord anchor, BaselineQuants blanketBaseline) + { + if (TensorConfigIdentity.IsPureBaseline(anchor.Config)) + return true; + + foreach (var (group, storedValue) in TensorConfigIdentity.EnumerateGroupSlots(anchor.Config)) + { + if (Cache.UnusedTensorGroups.Any(x => x.UniqueId == group.UniqueId)) + continue; + + if (BaselineQuants.IsNullTensorConfigGroupSlot(storedValue)) + continue; + + var decoded = BaselineQuants.DecodeTensorConfigGroupSlotToBaselineId(storedValue); + if (decoded != blanketBaseline.UniqueId) + return false; + } + + return true; + } + + private static void ValidateBlanketIsolationCoverage( + BaselineQuants blanketBaseline, + RankSafeKldPredictionService.RankSafePredictionModel context) + { + var missingGroups = context.ActiveGroups + .Where(group => !context.IsolationByGroupAndBaseline.ContainsKey((group.UniqueId, blanketBaseline.UniqueId))) + .Select(group => group.Name) + .ToList(); + + if (missingGroups.Count == 0) + return; + + throw new InvalidOperationException( + $"Smart baseline fallback critical truth error: baseline '{blanketBaseline.Names[0]}' (id {blanketBaseline.UniqueId}) is present as a fallback anchor, " + + $"but isolated group truth is missing for {missingGroups.Count:N0}/{context.ActiveGroups.Count:N0} active group(s): {string.Join(", ", missingGroups)}. " + + "Smart fallback must not silently skip groups when tuning from a real baseline anchor."); + } + private async Task GetContextAsync(CancellationToken ct) { if (_context != null) @@ -383,6 +439,7 @@ private static IReadOnlyList BuildGroupOptions( private static List BuildPlans( SmartFallbackRequest request, BaselineQuants blanketBaseline, + BenchmarkSnapshotRecord blanketAnchor, ulong baseSize, double baseKld, IReadOnlyList options, @@ -415,8 +472,8 @@ void TryAddPlan(IEnumerable selected, string strategy) if (!seen.Add(key)) return; - if (!TryPredictSize(config, context, out var predictedSize, out _) || - !TryComputeAdditiveKld(config, context, out var predictedKld, out _)) + if (!TryPredictSize(config, blanketBaseline, blanketAnchor, context, out var predictedSize, out _) || + !TryComputeAdditiveKld(config, blanketBaseline, blanketAnchor, context, out var predictedKld, out _)) return; if (request.StrictDominance) @@ -502,7 +559,7 @@ void TryAddPlan(IEnumerable selected, string strategy) foreach (var selected in trial) trialQuant.SetLearnedCandidateOverride(selected.Group, selected.CandidateBaseline); - if (!TryPredictSize((TensorConfig)trialQuant, context, out var trialSize, out _)) + if (!TryPredictSize((TensorConfig)trialQuant, blanketBaseline, blanketAnchor, context, out var trialSize, out _)) continue; if (trialSize <= request.WindowMaxSizeBytes) @@ -604,6 +661,8 @@ private static HybridSelectionCandidate ToCandidate( private static bool TryPredictSize( TensorConfig config, + BaselineQuants blanketBaseline, + BenchmarkSnapshotRecord blanketAnchor, RankSafeKldPredictionService.RankSafePredictionModel context, out ulong sizeBytes, out IReadOnlyList notes) @@ -612,27 +671,20 @@ private static bool TryPredictSize( notes = localNotes; sizeBytes = 0; - if (!context.BaseOnlySnapshotsByBaselineId.TryGetValue(config.BaseQuant, out var baseOnlyAnchor)) - { - localNotes.Add($"Missing base-only anchor for baseline id {config.BaseQuant}."); - return false; - } - - long total = (long)baseOnlyAnchor.SizeBytes; - long q8ExactBlanketSize = (long)context.Q8BaseOnly.SizeBytes; + long total = (long)blanketAnchor.SizeBytes; foreach (var (group, effectiveBaselineId) in RankSafeKldPredictionService.EnumerateEffectiveBaselines(config, context.ActiveGroups)) { - if (BaselineQuants.IsNativeExactAlias(effectiveBaselineId)) + if (effectiveBaselineId == blanketBaseline.UniqueId) continue; - if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, effectiveBaselineId), out var snapshot)) + if (!TryResolveIsolationSnapshot(group, blanketBaseline.UniqueId, context, localNotes, "baseline size", out var baseSnapshot) || + !TryResolveIsolationSnapshot(group, effectiveBaselineId, context, localNotes, "candidate size", out var candidateSnapshot)) { - localNotes.Add($"Missing isolation size anchor for {group.Name}:{effectiveBaselineId}."); return false; } - total += (long)snapshot.SizeBytes - q8ExactBlanketSize; + total += (long)candidateSnapshot.SizeBytes - (long)baseSnapshot.SizeBytes; } if (total <= 0) @@ -647,31 +699,69 @@ private static bool TryPredictSize( private static bool TryComputeAdditiveKld( TensorConfig config, + BaselineQuants blanketBaseline, + BenchmarkSnapshotRecord blanketAnchor, RankSafeKldPredictionService.RankSafePredictionModel context, out double additiveKld, out IReadOnlyList notes) { var localNotes = new List(); notes = localNotes; - additiveKld = 0d; + additiveKld = Math.Max(0d, blanketAnchor.Kld); foreach (var (group, effectiveBaselineId) in RankSafeKldPredictionService.EnumerateEffectiveBaselines(config, context.ActiveGroups)) { - if (BaselineQuants.IsNativeExactAlias(effectiveBaselineId)) + if (effectiveBaselineId == blanketBaseline.UniqueId) continue; - if (!context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, effectiveBaselineId), out var snapshot)) + if (!TryResolveIsolationSnapshot(group, blanketBaseline.UniqueId, context, localNotes, "baseline KLD", out var baseSnapshot) || + !TryResolveIsolationSnapshot(group, effectiveBaselineId, context, localNotes, "candidate KLD", out var candidateSnapshot)) { - localNotes.Add($"Missing isolation KLD anchor for {group.Name}:{effectiveBaselineId}."); return false; } - additiveKld += Math.Max(0d, snapshot.Kld); + additiveKld += Math.Max(0d, candidateSnapshot.Kld) - Math.Max(0d, baseSnapshot.Kld); } + additiveKld = Math.Max(0d, additiveKld); return true; } + private static bool TryResolveIsolationSnapshot( + TensorGroup group, + byte baselineId, + RankSafeKldPredictionService.RankSafePredictionModel context, + List notes, + string role, + out BenchmarkSnapshotRecord snapshot) + { + if (BaselineQuants.IsNativeExactAlias(baselineId)) + { + snapshot = context.Q8BaseOnly; + return true; + } + + if (context.IsolationByGroupAndBaseline.TryGetValue((group.UniqueId, baselineId), out var foundSnapshot)) + { + snapshot = foundSnapshot; + return true; + } + + var name = BaselineQuants.FromId(baselineId).Names[0]; + var message = $"Missing {role} isolation anchor for group '{group.Name}' and baseline '{name}' (id {baselineId})."; + notes.Add(message); + + if (BaselineQuants.FromId(baselineId).IsExternalRepositoryBaseline) + { + throw new InvalidOperationException( + $"Smart baseline fallback critical truth error: {message} " + + "External/custom fallback must use exact isolated truth and must not silently collapse or skip."); + } + + snapshot = default!; + return false; + } + private static int CountHigherFidelitySteps(BaselineQuants baseBaseline, BaselineQuants candidate) { if (candidate.BitRange <= baseBaseline.BitRange) @@ -748,4 +838,4 @@ private sealed class SmartCandidatePlan public long TotalSizeDeltaBytes { get; init; } public double Score { get; init; } } -} +} \ No newline at end of file From f7d8c5ed3b6e309124c4d8217c103fe768d377b1 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sun, 10 May 2026 16:19:28 -0400 Subject: [PATCH 202/258] new exceptions and repair --- .../DuckDbPredictionMaterializationService.cs | 7 +- .../Services/RankSafeKldPredictionService.cs | 222 +++++++++++++++++- .../SmartBaselineTuningFallbackService.cs | 1 + 3 files changed, 218 insertions(+), 12 deletions(-) diff --git a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs index 0b92745..29fd5b1 100644 --- a/MagicQuant/Services/DuckDbPredictionMaterializationService.cs +++ b/MagicQuant/Services/DuckDbPredictionMaterializationService.cs @@ -208,7 +208,10 @@ await ExecuteAsync(c, } } - double bitRange = zeroDamage ? 99d : GetBitRange(resolvedBaselineId); + double bitRange = RankSafeKldPredictionService.GetStressBitRangeForPrediction( + slot.Group, + resolvedBaselineId, + model); await ExecuteAsync(c, $@" INSERT INTO temp_effective_group_prediction VALUES ( @@ -973,4 +976,4 @@ public sealed class PredictionMaterializationStatus public double? MaxPredictedKld { get; init; } public ulong? MinPredictedSizeBytes { get; init; } public ulong? MaxPredictedSizeBytes { get; init; } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/RankSafeKldPredictionService.cs b/MagicQuant/Services/RankSafeKldPredictionService.cs index 5b9e8ff..33d44ca 100644 --- a/MagicQuant/Services/RankSafeKldPredictionService.cs +++ b/MagicQuant/Services/RankSafeKldPredictionService.cs @@ -267,7 +267,12 @@ private async Task BuildContextAsync(CancellationToken notes.Add($"Q8_0 isolation snapshots loaded for {activeGroups.Count:N0} active tensor groups. Q8_0 will contribute measured prediction-space KLD, not zero/native damage."); } - AppendExternalCoverageDiagnostics(notes, activeGroups, baseOnlyByBaselineId, isolationByGroupAndBaseline); + var isolationDominanceBitTruthByGroupAndBaseline = BuildIsolationDominanceBitTruthOverrides( + activeGroups, + isolationByGroupAndBaseline, + notes); + + AppendExternalCoverageDiagnostics(notes, activeGroups, pureByBaselineId, baseOnlyByBaselineId, isolationByGroupAndBaseline); return new RankSafePredictionModel( activeGroups: activeGroups, @@ -276,6 +281,7 @@ private async Task BuildContextAsync(CancellationToken pureSnapshotsByBaselineId: pureByBaselineId, baseOnlySnapshotsByBaselineId: baseOnlyByBaselineId, isolationByGroupAndBaseline: isolationByGroupAndBaseline, + isolationDominanceBitTruthByGroupAndBaseline: isolationDominanceBitTruthByGroupAndBaseline, notes: notes); } @@ -291,15 +297,54 @@ private async Task> LoadFitRowsAsync( var alreadyByKey = alreadyPredicted.ToDictionary(x => TensorConfigIdentity.ToKey(x.Config), StringComparer.Ordinal); var fitRows = new List(); + var skippedFitReasons = new HashSet(StringComparer.Ordinal); foreach (var snapshot in allBenchmarkRows) { ct.ThrowIfCancellationRequested(); + /* + * Keep the rank-safe predictor entirely in prediction space. + * + * Real pure baselines such as UD-Q6_K_XL can appear in the benchmark table + * as BaseQuant=UD-Q6_K_XL with NULL group slots. That shape is a real artifact + * identity, not a prediction-space base-only anchor. The prediction coordinate + * system is still the Q8_0 carrier plus exact isolated group overrides, so pure + * baselines are canonicalized to the virtual all-groups row before prediction: + * + * real pure UD-Q6_K_XL -> Q8_0 carrier with every active group = UD-Q6_K_XL + * real pure Q6_K -> Q8_0 carrier with every active group = Q6_K + * + * The real snapshot.Kld remains the fit target. Only the config used to produce + * the additive/cross-term prediction is canonicalized. This preserves the hard + * separation between real benchmark truth and synthetic prediction geometry. + */ + if (!TryCanonicalizeBenchmarkSnapshotConfigForPrediction( + snapshot.Config, + context, + out var predictionConfig, + out var skipReason)) + { + /* + * This row is real benchmark truth, but it is not representable in the + * rank-safe prediction coordinate system. Do not throw here: old runs and + * helper paths can leave real/external-base synthetic artifacts in SQLite + * even though DuckDB prediction-space candidates always use the Q8_0 + * carrier. Those rows are simply not fit observations for the synthetic + * model. + */ + if (!string.IsNullOrWhiteSpace(skipReason)) + skippedFitReasons.Add(skipReason); + + continue; + } + + var predictionKey = TensorConfigIdentity.ToKey(predictionConfig); + RankSafePredictionRow predicted; - if (!alreadyByKey.TryGetValue(TensorConfigIdentity.ToKey(snapshot.Config), out predicted!)) + if (!alreadyByKey.TryGetValue(predictionKey, out predicted!)) { - predicted = await PredictSingleAsync(snapshot.Config, context, ct); + predicted = await PredictSingleAsync(predictionConfig, context, ct); } if (!predicted.IsPredictable || double.IsInfinity(predicted.AdditiveKld) || double.IsNaN(predicted.AdditiveKld)) @@ -307,15 +352,62 @@ private async Task> LoadFitRowsAsync( fitRows.Add(new FitObservation { - Config = snapshot.Config, + Config = predictionConfig, ActualKld = Math.Max(0d, snapshot.Kld), AdditiveKld = predicted.AdditiveKld }); } + if (skippedFitReasons.Count > 0) + context.Notes = context.Notes.Concat(skippedFitReasons.OrderBy(x => x, StringComparer.Ordinal)).ToList(); + return fitRows; } + private static bool TryCanonicalizeBenchmarkSnapshotConfigForPrediction( + TensorConfig config, + RankSafePredictionModel context, + out TensorConfig predictionConfig, + out string? skipReason) + { + predictionConfig = config; + skipReason = null; + + if (context.BaseOnlySnapshotsByBaselineId.ContainsKey(config.BaseQuant)) + return true; + + if (!TensorConfigIdentity.IsPureBaseline(config)) + { + skipReason = + $"Skipped non-canonical rank-safe fit row with base baseline {FormatBaselineForNote(config.BaseQuant)} (id '{config.BaseQuant}'). " + + "DuckDB prediction-space rows use the Q8_0 carrier plus isolated group truth; this real benchmark row is not a fit observation for that synthetic coordinate system."; + return false; + } + + var baseline = BaselineQuants.FromId(config.BaseQuant); + if (BaselineQuants.IsNativeExactAlias(baseline.UniqueId)) + return true; + + if (!context.PureSnapshotsByBaselineId.ContainsKey(baseline.UniqueId)) + { + throw new InvalidOperationException( + $"Rank-safe prediction fit encountered pure baseline {baseline.Names[0]} (id '{baseline.UniqueId}'), but the pure benchmark snapshot was not loaded into context. " + + "This is a critical truth-loading error, not a soft warning."); + } + + var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + var predictionQuant = HybridQuant.CreateExactBlanket( + baseQuant: BaselineQuants.Q8_0, + groups: context.ActiveGroups, + exactScheme: nativeExactScheme); + + foreach (var group in context.ActiveGroups) + predictionQuant.SetLearnedCandidateOverride(group, baseline); + + predictionConfig = (TensorConfig)predictionQuant; + return true; + } + private RankSafePredictionFit FitInteractionModel( IReadOnlyList observations, RankSafePredictionModel context) @@ -577,6 +669,100 @@ private ulong PredictSize( return (ulong)total; } + private static Dictionary<(byte GroupId, byte BaselineId), double> BuildIsolationDominanceBitTruthOverrides( + IReadOnlyList activeGroups, + Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord> isolationByGroupAndBaseline, + List notes) + { + const double kldEpsilon = 1e-12; + + var result = new Dictionary<(byte GroupId, byte BaselineId), double>(); + var detailNotes = new List(); + var baselinesById = BaselineQuants.GetAllRecognizedBaselines() + .Where(x => !BaselineQuants.IsNativeExactAlias(x.UniqueId)) + .GroupBy(x => x.UniqueId) + .ToDictionary(g => g.Key, g => g.First()); + + foreach (var group in activeGroups.OrderBy(x => x.UniqueId)) + { + var entries = isolationByGroupAndBaseline + .Where(x => x.Key.GroupId == group.UniqueId && baselinesById.ContainsKey(x.Key.BaselineId)) + .Select(x => new IsolationBitTruthEntry( + Baseline: baselinesById[x.Key.BaselineId], + Snapshot: x.Value, + DeclaredBitRange: (double)baselinesById[x.Key.BaselineId].BitRange)) + .OrderByDescending(x => x.DeclaredBitRange) + .ThenBy(x => x.Snapshot.Kld) + .ThenBy(x => x.Snapshot.SizeBytes) + .ToList(); + + foreach (var candidate in entries) + { + double inheritedBitTruth = candidate.DeclaredBitRange; + IsolationBitTruthEntry? strongestVictim = null; + + foreach (var victim in entries) + { + if (victim.DeclaredBitRange <= candidate.DeclaredBitRange) + continue; + + bool sameSizeOrSmaller = candidate.Snapshot.SizeBytes <= victim.Snapshot.SizeBytes; + bool lowerKld = candidate.Snapshot.Kld < victim.Snapshot.Kld - kldEpsilon; + if (!sameSizeOrSmaller || !lowerKld) + continue; + + if (victim.DeclaredBitRange > inheritedBitTruth) + { + inheritedBitTruth = victim.DeclaredBitRange; + strongestVictim = victim; + } + } + + if (inheritedBitTruth <= candidate.DeclaredBitRange) + continue; + + result[(group.UniqueId, candidate.Baseline.UniqueId)] = inheritedBitTruth; + + if (strongestVictim != null && detailNotes.Count < 32) + { + detailNotes.Add( + $"Isolation bit-truth override: group '{group.Name}' treats {candidate.Baseline.Names[0]} as {inheritedBitTruth:G4}b stress truth instead of {candidate.DeclaredBitRange:G4}b because it isolated-dominated higher-fidelity {strongestVictim.Baseline.Names[0]} (candidate size={candidate.Snapshot.SizeBytes:N0}, kld={candidate.Snapshot.Kld:0.######}; victim size={strongestVictim.Snapshot.SizeBytes:N0}, kld={strongestVictim.Snapshot.Kld:0.######})."); + } + } + } + + if (result.Count == 0) + { + notes.Add("Isolation bit-truth overrides: none. Declared quant bit ranges will drive bit-stress interaction correction."); + return result; + } + + notes.Add( + $"Isolation bit-truth overrides active: {result.Count:N0} group/baseline state(s) inherit higher-fidelity stress truth because isolated sampling showed same-size-or-smaller lower-KLD dominance."); + + foreach (var detail in detailNotes) + notes.Add(detail); + + if (result.Count > detailNotes.Count) + notes.Add($"Isolation bit-truth overrides: {result.Count - detailNotes.Count:N0} additional override(s) omitted from diagnostics."); + + return result; + } + + internal static double GetStressBitRangeForPrediction( + TensorGroup group, + byte baselineId, + RankSafePredictionModel context) + { + if (IsZeroDamageAlias(baselineId)) + return 99d; + + double declared = BaselineQuants.FromId(baselineId).BitRange; + return context.IsolationDominanceBitTruthByGroupAndBaseline.TryGetValue((group.UniqueId, baselineId), out var inherited) + ? Math.Max(declared, inherited) + : declared; + } + private double ComputeCrossTerm(TensorConfig config, RankSafePredictionModel context, double threshold) { var contributions = new List<(double Kld, double Bits)>(); @@ -589,8 +775,8 @@ private double ComputeCrossTerm(TensorConfig config, RankSafePredictionModel con if (!TryResolveIsolationBaselineForPrediction(group, effectiveBaselineId, context, notes: null, out var resolved)) continue; - var baseline = BaselineQuants.FromId(resolved.BaselineId); - contributions.Add((Math.Max(0d, resolved.Snapshot.Kld), baseline.BitRange)); + double stressBitRange = GetStressBitRangeForPrediction(group, resolved.BaselineId, context); + contributions.Add((Math.Max(0d, resolved.Snapshot.Kld), stressBitRange)); } double cross = 0d; @@ -713,7 +899,7 @@ internal static bool TryResolveBaseOnlySnapshotForPrediction( * notes?.Add($"External baseline {FormatBaselineForNote(baselineId)} used surrogate base-only size {FormatBaselineForNote(disabledSurrogateId)}."); * return true; * - * Base-only size anchors must preserve the exact runtime baseline id. Falling back + * Base-only anchors must preserve the exact runtime baseline id. Falling back * here makes UD-Q4_K_XL and Q4_K_M look byte-identical before selection even starts. */ ThrowExternalBaseOnlySurrogateFallbackDisabled(baselineId, disabledSurrogateId); @@ -721,8 +907,9 @@ internal static bool TryResolveBaseOnlySnapshotForPrediction( if (IsExternalRepositoryBaseline(baselineId)) throw new InvalidOperationException( - $"Missing exact base-only anchor for external baseline {FormatBaselineForNote(baselineId)} (id '{baselineId}'). " + - "Surrogate base-only fallback is disabled because every external/custom baseline should have exact isolated truth before prediction."); + $"Missing exact synthetic base-only anchor for external baseline {FormatBaselineForNote(baselineId)} (id '{baselineId}'). " + + "The rank-safe predictor must not use real pure baseline snapshots as base-only prediction anchors. " + + "Pure baselines are canonicalized to Q8_0-carrier virtual blankets before prediction; reaching size prediction with an external/custom BaseQuant means a non-canonical config escaped normalization."); snapshot = default!; return false; @@ -809,6 +996,7 @@ private static void ThrowMissingExactExternalIsolation(TensorGroup group, byte e private static void AppendExternalCoverageDiagnostics( List notes, IReadOnlyList activeGroups, + Dictionary pureByBaselineId, Dictionary baseOnlyByBaselineId, Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord> isolationByGroupAndBaseline) { @@ -826,15 +1014,26 @@ private static void AppendExternalCoverageDiagnostics( { int exactIsolation = activeGroups.Count(group => isolationByGroupAndBaseline.ContainsKey((group.UniqueId, baseline.UniqueId))); bool exactBaseOnly = baseOnlyByBaselineId.ContainsKey(baseline.UniqueId); + bool exactPure = pureByBaselineId.ContainsKey(baseline.UniqueId); + string baseAnchorText = exactBaseOnly + ? "base-only=exact" + : exactPure + ? "base-only=missing; pure-anchor=exact" + : "base-only=missing; pure-anchor=missing"; string fallbackText = TryGetDisabledSurrogateBaselineId(baseline.UniqueId, out var fallbackId) ? $"; disabled fallback target would have been {FormatBaselineForNote(fallbackId)}:{fallbackId}" : string.Empty; notes.Add( - $"External isolation exact coverage: {baseline.Names[0]}:{baseline.UniqueId} exact={exactIsolation}/{activeGroups.Count} groups; base-only={(exactBaseOnly ? "exact" : "missing")}{fallbackText}."); + $"External isolation exact coverage: {baseline.Names[0]}:{baseline.UniqueId} exact={exactIsolation}/{activeGroups.Count} groups; {baseAnchorText}{fallbackText}."); } } + private sealed record IsolationBitTruthEntry( + BaselineQuants Baseline, + BenchmarkSnapshotRecord Snapshot, + double DeclaredBitRange); + internal readonly record struct IsolationBaselineResolution( byte BaselineId, BenchmarkSnapshotRecord Snapshot, @@ -908,6 +1107,7 @@ public RankSafePredictionModel( Dictionary pureSnapshotsByBaselineId, Dictionary baseOnlySnapshotsByBaselineId, Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord> isolationByGroupAndBaseline, + Dictionary<(byte GroupId, byte BaselineId), double> isolationDominanceBitTruthByGroupAndBaseline, IReadOnlyList notes) { ActiveGroups = activeGroups; @@ -916,6 +1116,7 @@ public RankSafePredictionModel( PureSnapshotsByBaselineId = pureSnapshotsByBaselineId; BaseOnlySnapshotsByBaselineId = baseOnlySnapshotsByBaselineId; IsolationByGroupAndBaseline = isolationByGroupAndBaseline; + IsolationDominanceBitTruthByGroupAndBaseline = isolationDominanceBitTruthByGroupAndBaseline; Notes = notes; } @@ -925,6 +1126,7 @@ public RankSafePredictionModel( public Dictionary PureSnapshotsByBaselineId { get; } public Dictionary BaseOnlySnapshotsByBaselineId { get; } public Dictionary<(byte GroupId, byte BaselineId), BenchmarkSnapshotRecord> IsolationByGroupAndBaseline { get; } + public Dictionary<(byte GroupId, byte BaselineId), double> IsolationDominanceBitTruthByGroupAndBaseline { get; } public IReadOnlyList Notes { get; set; } public RankSafePredictionFit Fit { get; set; } = new(); } diff --git a/MagicQuant/Services/SmartBaselineTuningFallbackService.cs b/MagicQuant/Services/SmartBaselineTuningFallbackService.cs index 88a358a..bae22d4 100644 --- a/MagicQuant/Services/SmartBaselineTuningFallbackService.cs +++ b/MagicQuant/Services/SmartBaselineTuningFallbackService.cs @@ -366,6 +366,7 @@ private static void ValidateBlanketIsolationCoverage( pureSnapshotsByBaselineId: pureByBaselineId, baseOnlySnapshotsByBaselineId: baseOnlyByBaselineId, isolationByGroupAndBaseline: isolationByGroupAndBaseline, + isolationDominanceBitTruthByGroupAndBaseline: new Dictionary<(byte GroupId, byte BaselineId), double>(), notes: notes); return _context; From 6affc644867864d1d3d61d6ff5fe54e9d50a0b93 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 11 May 2026 13:06:08 -0400 Subject: [PATCH 203/258] still working out the kinks --- .../PredictionGuidedHybridSelectionService.cs | 92 +++++++++++++++++++ .../Services/RankSafeKldPredictionService.cs | 49 ++++++---- .../Services/RemainingCombinationStore.cs | 43 ++++++++- 3 files changed, 164 insertions(+), 20 deletions(-) diff --git a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs index cb116b7..0dbf32c 100644 --- a/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs +++ b/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs @@ -68,9 +68,13 @@ public async Task RunAsync( var strict = await RunStrictDominanceReplacementAsync(current, predictedAnchors, eliminationRecords, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, strict.AcceptedSnapshots, eliminationRecords, "strict predicted hybrid dominance validated by real benchmark"); + predictedAnchors = AugmentPredictedAnchorsWithAcceptedValidationRows(predictedAnchors, validationAttempts); + var near = await RunNearBaselineReplacementAsync(current, predictedAnchors, eliminationRecords, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, near.AcceptedSnapshots, eliminationRecords, "near-baseline size-premium replacement validated by real benchmark"); + predictedAnchors = AugmentPredictedAnchorsWithAcceptedValidationRows(predictedAnchors, validationAttempts); + var interior = await RunInteriorSubspaceDiscoveryAsync(current, predictedAnchors, validationFailures, validationAttempts, phaseDiagnostics, ct); current = MergeAndDominanceFilter(current, interior.AcceptedSnapshots, eliminationRecords, "interior subspace discovery dominated by real benchmark truth"); @@ -544,6 +548,94 @@ private static bool IsQ8Anchor(BenchmarkSnapshotRecord anchor) } } + private static IReadOnlyList AugmentPredictedAnchorsWithAcceptedValidationRows( + IReadOnlyList predictedAnchors, + IReadOnlyList validationAttempts) + { + if (validationAttempts.Count == 0) + return predictedAnchors; + + var result = new List(predictedAnchors); + var knownKeys = result + .Select(x => x.ConfigKey) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .ToHashSet(StringComparer.Ordinal); + + int added = 0; + + foreach (var validation in validationAttempts) + { + if (!validation.Accepted || validation.Snapshot == null) + continue; + + var prediction = validation.Candidate.Prediction; + if (!prediction.IsPredictable || !prediction.IsSizePredictable) + continue; + + var predictionSpaceConfig = CanonicalizeSelectionConfigForPredictionSpace(prediction.Config); + string key = TensorConfigIdentity.ToKey(predictionSpaceConfig); + if (!knownKeys.Add(key)) + continue; + + var sourceBaseline = HybridBenchmarkRepository.ResolveSourceBaselineForProvider(validation.Snapshot.Quant); + + result.Add(new PredictedAnchorRow + { + Config = predictionSpaceConfig, + ConfigKey = key, + DisplayName = validation.Snapshot.DisplayName, + BaselineCanonicalKey = sourceBaseline.CanonicalKey, + RuntimeBaselineId = sourceBaseline.UniqueId, + PredictedKld = prediction.PredictedKld, + PredictedSizeBytes = prediction.PredictedSizeBytes, + PredictionConfidence = prediction.PredictionConfidence, + PredictionRank = prediction.PredictedRank ?? ulong.MaxValue, + IsVirtualPredictionAnchor = false + }); + + added++; + } + + if (added > 0) + { + AnsiConsole.MarkupLine( + $"[grey]Prediction anchor frontier augmented from accepted validation rows:[/] [cyan]{added:N0}[/] phase-local anchor(s) added for smart-fallback / accepted hybrid coordinates outside the pruned DuckDB row set."); + } + + return added == 0 ? predictedAnchors : result; + } + + private static TensorConfig CanonicalizeSelectionConfigForPredictionSpace(TensorConfig config) + { + if (config.BaseQuant == BaselineQuants.Q8_0.UniqueId) + return config; + + var baseBaseline = BaselineQuants.FromId(config.BaseQuant); + byte inheritedBaseSlot = BaselineQuants.EncodeTensorConfigGroupSlot(baseBaseline); + + return new TensorConfig( + baseQuant: BaselineQuants.Q8_0.UniqueId, + embeddings: CanonicalizeSelectionPredictionSlot(TReg.Embeddings, config.Embeddings, inheritedBaseSlot), + lmHead: CanonicalizeSelectionPredictionSlot(TReg.LmHead, config.LmHead, inheritedBaseSlot), + attnQ: CanonicalizeSelectionPredictionSlot(TReg.AttnQ, config.AttnQ, inheritedBaseSlot), + attnKV: CanonicalizeSelectionPredictionSlot(TReg.AttnKV, config.AttnKV, inheritedBaseSlot), + attnOutput: CanonicalizeSelectionPredictionSlot(TReg.AttnOutput, config.AttnOutput, inheritedBaseSlot), + ffnUpGate: CanonicalizeSelectionPredictionSlot(TReg.FfnUpGate, config.FfnUpGate, inheritedBaseSlot), + ffnDown: CanonicalizeSelectionPredictionSlot(TReg.FfnDown, config.FfnDown, inheritedBaseSlot), + moeExperts: CanonicalizeSelectionPredictionSlot(TReg.MoeExperts, config.MoeExperts, inheritedBaseSlot), + moeRouter: CanonicalizeSelectionPredictionSlot(TReg.MoeRouter, config.MoeRouter, inheritedBaseSlot)); + } + + private static byte CanonicalizeSelectionPredictionSlot(TensorGroup group, byte storedValue, byte inheritedBaseSlot) + { + if (Cache.UnusedTensorGroups.Any(x => x.UniqueId == group.UniqueId)) + return BaselineQuants.TensorConfigNullSlotValue; + + return BaselineQuants.IsNullTensorConfigGroupSlot(storedValue) + ? inheritedBaseSlot + : storedValue; + } + private static CandidateValidationResult ChooseBestStrictDominanceCandidate( BenchmarkSnapshotRecord anchor, IReadOnlyList accepted) diff --git a/MagicQuant/Services/RankSafeKldPredictionService.cs b/MagicQuant/Services/RankSafeKldPredictionService.cs index 33d44ca..3aad442 100644 --- a/MagicQuant/Services/RankSafeKldPredictionService.cs +++ b/MagicQuant/Services/RankSafeKldPredictionService.cs @@ -373,41 +373,52 @@ private static bool TryCanonicalizeBenchmarkSnapshotConfigForPrediction( predictionConfig = config; skipReason = null; - if (context.BaseOnlySnapshotsByBaselineId.ContainsKey(config.BaseQuant)) + if (config.BaseQuant == BaselineQuants.Q8_0.UniqueId) return true; - if (!TensorConfigIdentity.IsPureBaseline(config)) - { - skipReason = - $"Skipped non-canonical rank-safe fit row with base baseline {FormatBaselineForNote(config.BaseQuant)} (id '{config.BaseQuant}'). " + - "DuckDB prediction-space rows use the Q8_0 carrier plus isolated group truth; this real benchmark row is not a fit observation for that synthetic coordinate system."; - return false; - } - var baseline = BaselineQuants.FromId(config.BaseQuant); if (BaselineQuants.IsNativeExactAlias(baseline.UniqueId)) return true; - if (!context.PureSnapshotsByBaselineId.ContainsKey(baseline.UniqueId)) + if (TensorConfigIdentity.IsPureBaseline(config) && + !context.PureSnapshotsByBaselineId.ContainsKey(baseline.UniqueId)) { throw new InvalidOperationException( $"Rank-safe prediction fit encountered pure baseline {baseline.Names[0]} (id '{baseline.UniqueId}'), but the pure benchmark snapshot was not loaded into context. " + "This is a critical truth-loading error, not a soft warning."); } - var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); - var predictionQuant = HybridQuant.CreateExactBlanket( - baseQuant: BaselineQuants.Q8_0, - groups: context.ActiveGroups, - exactScheme: nativeExactScheme); + byte inheritedBaseSlot = BaselineQuants.EncodeTensorConfigGroupSlot(baseline); - foreach (var group in context.ActiveGroups) - predictionQuant.SetLearnedCandidateOverride(group, baseline); + predictionConfig = new TensorConfig( + baseQuant: BaselineQuants.Q8_0.UniqueId, + embeddings: CanonicalizePredictionSlot(TReg.Embeddings, config.Embeddings, inheritedBaseSlot, context), + lmHead: CanonicalizePredictionSlot(TReg.LmHead, config.LmHead, inheritedBaseSlot, context), + attnQ: CanonicalizePredictionSlot(TReg.AttnQ, config.AttnQ, inheritedBaseSlot, context), + attnKV: CanonicalizePredictionSlot(TReg.AttnKV, config.AttnKV, inheritedBaseSlot, context), + attnOutput: CanonicalizePredictionSlot(TReg.AttnOutput, config.AttnOutput, inheritedBaseSlot, context), + ffnUpGate: CanonicalizePredictionSlot(TReg.FfnUpGate, config.FfnUpGate, inheritedBaseSlot, context), + ffnDown: CanonicalizePredictionSlot(TReg.FfnDown, config.FfnDown, inheritedBaseSlot, context), + moeExperts: CanonicalizePredictionSlot(TReg.MoeExperts, config.MoeExperts, inheritedBaseSlot, context), + moeRouter: CanonicalizePredictionSlot(TReg.MoeRouter, config.MoeRouter, inheritedBaseSlot, context)); - predictionConfig = (TensorConfig)predictionQuant; return true; } + private static byte CanonicalizePredictionSlot( + TensorGroup group, + byte storedValue, + byte inheritedBaseSlot, + RankSafePredictionModel context) + { + if (!context.ActiveGroups.Any(x => x.UniqueId == group.UniqueId)) + return BaselineQuants.TensorConfigNullSlotValue; + + return BaselineQuants.IsNullTensorConfigGroupSlot(storedValue) + ? inheritedBaseSlot + : storedValue; + } + private RankSafePredictionFit FitInteractionModel( IReadOnlyList observations, RankSafePredictionModel context) @@ -1145,4 +1156,4 @@ private struct PavaBlock public int Count; public double Mean => Weight <= 0d ? 0d : Sum / Weight; } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 6f682d8..446eea4 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -192,6 +192,14 @@ AND PredictionRank IS NOT NULL ct.ThrowIfCancellationRequested(); var sourceBaseline = HybridBenchmarkRepository.ResolveSourceBaselineForProvider(realAnchor.Quant); + var predictionSpaceConfig = CanonicalizeConfigForPredictionSpace(realAnchor.Config); + string predictionSpaceKey = TensorConfigIdentity.ToKey(predictionSpaceConfig); + + var byConfig = predictedAnchors.FirstOrDefault(x => + string.Equals(x.ConfigKey, predictionSpaceKey, StringComparison.Ordinal)); + if (byConfig != null) + return byConfig; + if (HybridBenchmarkRepository.IsTrueMagicQuantHybrid(realAnchor.Quant)) return await QueryPredictedAnchorForConfigAsync(realAnchor, sourceBaseline, ct); @@ -230,6 +238,8 @@ AND PredictionRank IS NOT NULL BaselineQuants sourceBaseline, CancellationToken ct) { + var predictionSpaceConfig = CanonicalizeConfigForPredictionSpace(realAnchor.Config); + string sql = $@" SELECT {CombinationDuckDbSchema.SlotColumnList}, {CombinationDuckDbSchema.EffectivePredictedKldSql} AS PredictedKld, @@ -241,7 +251,7 @@ AND PredictionRank IS NOT NULL WHERE COALESCE(FinalPredictedKld, PredictedKld) IS NOT NULL AND PredictedSizeBytes IS NOT NULL AND PredictionRank IS NOT NULL - AND {BuildSlotPredicateSql(realAnchor.Config)} + AND {BuildSlotPredicateSql(predictionSpaceConfig)} LIMIT 1;"; using var c = new DuckDBConnection(ConnectionString); @@ -673,6 +683,37 @@ private async Task ExecuteCountAsync(string sql, object[] args, Cancellati return ToInt64(await cmd.ExecuteScalarAsync(ct)); } + private static TensorConfig CanonicalizeConfigForPredictionSpace(TensorConfig config) + { + if (config.BaseQuant == BaselineQuants.Q8_0.UniqueId) + return config; + + var baseBaseline = BaselineQuants.FromId(config.BaseQuant); + byte inheritedBaseSlot = BaselineQuants.EncodeTensorConfigGroupSlot(baseBaseline); + + return new TensorConfig( + baseQuant: BaselineQuants.Q8_0.UniqueId, + embeddings: CanonicalizePredictionSlot(TReg.Embeddings, config.Embeddings, inheritedBaseSlot), + lmHead: CanonicalizePredictionSlot(TReg.LmHead, config.LmHead, inheritedBaseSlot), + attnQ: CanonicalizePredictionSlot(TReg.AttnQ, config.AttnQ, inheritedBaseSlot), + attnKV: CanonicalizePredictionSlot(TReg.AttnKV, config.AttnKV, inheritedBaseSlot), + attnOutput: CanonicalizePredictionSlot(TReg.AttnOutput, config.AttnOutput, inheritedBaseSlot), + ffnUpGate: CanonicalizePredictionSlot(TReg.FfnUpGate, config.FfnUpGate, inheritedBaseSlot), + ffnDown: CanonicalizePredictionSlot(TReg.FfnDown, config.FfnDown, inheritedBaseSlot), + moeExperts: CanonicalizePredictionSlot(TReg.MoeExperts, config.MoeExperts, inheritedBaseSlot), + moeRouter: CanonicalizePredictionSlot(TReg.MoeRouter, config.MoeRouter, inheritedBaseSlot)); + } + + private static byte CanonicalizePredictionSlot(TensorGroup group, byte storedValue, byte inheritedBaseSlot) + { + if (Cache.UnusedTensorGroups.Any(x => x.UniqueId == group.UniqueId)) + return BaselineQuants.TensorConfigNullSlotValue; + + return BaselineQuants.IsNullTensorConfigGroupSlot(storedValue) + ? inheritedBaseSlot + : storedValue; + } + private static string BuildSlotPredicateSql(TensorConfig config) { return $"BaseQuant = {config.BaseQuant} AND Embeddings = {config.Embeddings} AND LmHead = {config.LmHead} AND AttnQ = {config.AttnQ} AND AttnKV = {config.AttnKV} AND AttnOutput = {config.AttnOutput} AND FfnUpGate = {config.FfnUpGate} AND FfnDown = {config.FfnDown} AND MoeExperts = {config.MoeExperts} AND MoeRouter = {config.MoeRouter}"; From 487f84a474deee2294c3676c9b063d57e04f9a7b Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 12 May 2026 11:28:11 -0400 Subject: [PATCH 204/258] removed group bands from quants. It was causing issues that derrived from old logic that no longer applies to the modern system the same way and was causing big missed potential. --- MQ.DB/Models/BaselineQuants.cs | 131 ++++++++++++------ MagicQuant/Program.cs | 2 +- .../Services/ReadmeGenerationService.cs | 13 +- 3 files changed, 96 insertions(+), 50 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index c2714fc..33fc2ff 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -89,66 +89,87 @@ private static BaselineQuants Create( explicitCandidateSortOrder); } + public static readonly BaselineQuants Q8_0 = - Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, false, 8, 16); + Create(0, false, "Q8_0", "Q8_0", TensorWeightScheme.Q8_0, [TensorWeightScheme.Q8_0], [], true, true, true, + false, 8, 16); public static readonly BaselineQuants Q6_K = - Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, false, true, false, 6, 15); + Create(1, false, "Q6_K", "Q6_K", TensorWeightScheme.Q6_K, [TensorWeightScheme.Q6_K], [], true, false, true, + false, 6, 15); public static readonly BaselineQuants Q5_K = - Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [TReg.MoeRouter.UniqueId], true, false, true, false, 5, 14); + Create(2, false, "Q5_K", "Q5_K", TensorWeightScheme.Q5_K, [TensorWeightScheme.Q5_K], [], true, false, true, + false, 5, 14); public static readonly BaselineQuants Q5_K_S = - Create(13, false, "Q5_K_S", "Q5_K_S", TensorWeightScheme.Q5_K_S, [TensorWeightScheme.Q5_K_S], [TReg.MoeRouter.UniqueId], true, false, true, false, 5, 13); + Create(13, false, "Q5_K_S", "Q5_K_S", TensorWeightScheme.Q5_K_S, [TensorWeightScheme.Q5_K_S], [], true, false, + true, false, 5, 13); + - public static readonly BaselineQuants Q4_K_M = - Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 12); + Create(3, false, "Q4_K_M", "Q4_K_M", TensorWeightScheme.Q4_K, [TensorWeightScheme.Q4_K], [], true, false, true, + false, 4, 12); public static readonly BaselineQuants Q4_K_S = - Create(14, false, "Q4_K_S", "Q4_K_S", TensorWeightScheme.Q4_K_S, [TensorWeightScheme.Q4_K_S], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 11); + Create(14, false, "Q4_K_S", "Q4_K_S", TensorWeightScheme.Q4_K_S, [TensorWeightScheme.Q4_K_S], [], true, false, + true, false, 4, 11); + - public static readonly BaselineQuants IQ4_NL = - Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 10); + Create(5, false, "IQ4_NL", "IQ4_NL", TensorWeightScheme.IQ4_NL, [TensorWeightScheme.IQ4_NL], [], true, false, + true, false, 4, 10); public static readonly BaselineQuants IQ4_XS = - Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [TReg.MoeRouter.UniqueId], true, false, true, false, 4, 9); + Create(6, false, "IQ4_XS", "IQ4_XS", TensorWeightScheme.IQ4_XS, [TensorWeightScheme.IQ4_XS], [], true, false, + true, false, 4, 9); public static readonly BaselineQuants MXFP4_MOE = - Create(15, false, "MXFP4_MOE", "MXFP4_MOE", TensorWeightScheme.MXFP4, [TensorWeightScheme.MXFP4, TensorWeightScheme.IQ3_S, TensorWeightScheme.IQ3_XS], [TReg.MoeRouter.UniqueId], false, false, false, false, 4, 8); + Create(15, false, "MXFP4_MOE", "MXFP4_MOE", TensorWeightScheme.MXFP4, + [TensorWeightScheme.MXFP4, TensorWeightScheme.IQ3_S, TensorWeightScheme.IQ3_XS], [], false, false, false, + false, 4, 8); public static readonly BaselineQuants IQ3_M = - Create(17, true, "IQ3_M", "IQ3_M", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 7); + Create(17, true, "IQ3_M", "IQ3_M", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [], true, false, true, + false, 3, 7); + - public static readonly BaselineQuants IQ3_S = - Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 6); + Create(7, true, "IQ3_S", "IQ3_S", TensorWeightScheme.IQ3_S, [TensorWeightScheme.IQ3_S], [], true, false, true, + false, 3, 6); public static readonly BaselineQuants IQ3_XS = - Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 5); + Create(8, true, "IQ3_XS", "IQ3_XS", TensorWeightScheme.IQ3_XS, [TensorWeightScheme.IQ3_XS], [], true, false, + true, false, 3, 5); public static readonly BaselineQuants IQ3_XXS = - Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId], true, false, true, false, 3, 4); + Create(9, true, "IQ3_XXS", "IQ3_XXS", TensorWeightScheme.IQ3_XXS, [TensorWeightScheme.IQ3_XXS], [], true, false, + true, false, 3, 4); public static readonly BaselineQuants IQ2_M = - Create(16, true, "IQ2_M", "IQ2_M", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2, 3); + Create(16, true, "IQ2_M", "IQ2_M", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [], true, false, true, + false, 2, 3); + - public static readonly BaselineQuants IQ2_S = - Create(10, true, "IQ2_S", "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2, 2); + Create(10, true, "IQ2_S", "IQ2_S", TensorWeightScheme.IQ2_S, [TensorWeightScheme.IQ2_S], [], true, false, true, + false, 2, 2); public static readonly BaselineQuants IQ2_XS = - Create(11, true, "IQ2_XS", "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId], true, false, true, false, 2, 1); + Create(11, true, "IQ2_XS", "IQ2_XS", TensorWeightScheme.IQ2_XS, [TensorWeightScheme.IQ2_XS], [], true, false, + true, false, 2, 1); public static readonly BaselineQuants IQ2_XXS = - Create(12, true, "IQ2_XXS", "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [TReg.Embeddings.UniqueId, TReg.LmHead.UniqueId, TReg.MoeRouter.UniqueId, TReg.MoeExperts.UniqueId, TReg.AttnKV.UniqueId], true, false, true, false, 2, 0); + Create(12, true, "IQ2_XXS", "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [], true, + false, true, false, 2, 0); public static readonly BaselineQuants BF16_Hybrid = - Create(201, false, "BF16", "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], false, false, false, true, 16, int.MaxValue, false, "alias:bf16", "exact_alias", null, null, null, null); + Create(201, false, "BF16", "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], false, false, false, + true, 16, int.MaxValue, false, "alias:bf16", "exact_alias", null, null, null, null); public static readonly BaselineQuants F16_Hybrid = - Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], false, false, false, true, 16, int.MaxValue, false, "alias:f16", "exact_alias", null, null, null, null); + Create(202, false, "F16", "F16", TensorWeightScheme.F16, [TensorWeightScheme.F16], [], false, false, false, + true, 16, int.MaxValue, false, "alias:f16", "exact_alias", null, null, null, null); private static readonly ImmutableArray StandardBaselines = [ @@ -249,7 +270,9 @@ public static void RegisterDynamicCustomBaseline(BaselineQuants baseline) lock (DynamicLock) { - var existingDynamic = DynamicCustomBaselines.FirstOrDefault(x => x.UniqueId == baseline.UniqueId || string.Equals(x.CanonicalKey, baseline.CanonicalKey, StringComparison.Ordinal)); + var existingDynamic = DynamicCustomBaselines.FirstOrDefault(x => + x.UniqueId == baseline.UniqueId || + string.Equals(x.CanonicalKey, baseline.CanonicalKey, StringComparison.Ordinal)); if (existingDynamic != null) { DynamicCustomBaselines.Remove(existingDynamic); @@ -257,10 +280,13 @@ public static void RegisterDynamicCustomBaseline(BaselineQuants baseline) else { var builtInCollision = StandardBaselines.Concat(ExactAliases) - .FirstOrDefault(x => x.UniqueId == baseline.UniqueId || string.Equals(x.CanonicalKey, baseline.CanonicalKey, StringComparison.Ordinal)); + .FirstOrDefault(x => + x.UniqueId == baseline.UniqueId || string.Equals(x.CanonicalKey, baseline.CanonicalKey, + StringComparison.Ordinal)); if (builtInCollision != null) - throw new InvalidOperationException($"Dynamic baseline collision detected against built-in baseline '{builtInCollision.Names[0]}' for id/key '{baseline.UniqueId}/{baseline.CanonicalKey}'."); + throw new InvalidOperationException( + $"Dynamic baseline collision detected against built-in baseline '{builtInCollision.Names[0]}' for id/key '{baseline.UniqueId}/{baseline.CanonicalKey}'."); } DynamicCustomBaselines.Add(baseline); @@ -272,9 +298,12 @@ public static void ConfigureStandardRoleFilters( IReadOnlyCollection? enabledCombinationCarrierIds, IReadOnlyCollection? enabledExplicitCandidateIds) { - EnabledStandardLearningBaselineIds = enabledLearningBaselineIds == null ? null : enabledLearningBaselineIds.ToHashSet(); - EnabledStandardCombinationCarrierIds = enabledCombinationCarrierIds == null ? null : enabledCombinationCarrierIds.ToHashSet(); - EnabledStandardExplicitCandidateIds = enabledExplicitCandidateIds == null ? null : enabledExplicitCandidateIds.ToHashSet(); + EnabledStandardLearningBaselineIds = + enabledLearningBaselineIds == null ? null : enabledLearningBaselineIds.ToHashSet(); + EnabledStandardCombinationCarrierIds = + enabledCombinationCarrierIds == null ? null : enabledCombinationCarrierIds.ToHashSet(); + EnabledStandardExplicitCandidateIds = + enabledExplicitCandidateIds == null ? null : enabledExplicitCandidateIds.ToHashSet(); } public static void ConfigureStandardPolicy( @@ -348,7 +377,8 @@ public sealed class ExternalBaselineRegistration public static BaselineQuants RegisterCustomExternalBaseline(ExternalBaselineRegistration registration) { var sortOrder = StandardBaselines - .FirstOrDefault(x => string.Equals(x.Names[0], registration.BaselineFamilyName, StringComparison.OrdinalIgnoreCase)) + .FirstOrDefault(x => + string.Equals(x.Names[0], registration.BaselineFamilyName, StringComparison.OrdinalIgnoreCase)) ?.ExplicitCandidateSortOrder ?? int.MaxValue; var baseline = CreateDynamicCustomBaseline( @@ -404,7 +434,8 @@ public static BaselineQuants GetNativeQuant() public static BaselineQuants GetBF16Quant() => GetNativeQuant(); - public static IReadOnlyList GetBuiltInStandardBaselines() => StandardBaselines.OrderBy(x => x.UniqueId).ToList(); + public static IReadOnlyList GetBuiltInStandardBaselines() => + StandardBaselines.OrderBy(x => x.UniqueId).ToList(); public static BaselineQuants? ResolveBuiltInStandardBaseline(string name) { @@ -432,7 +463,8 @@ private static IEnumerable FilterStandardByRole( public static IReadOnlyList GetLearningBaselines(bool hasUsableImatrix) { - var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsLearningBaseline), EnabledStandardLearningBaselineIds); + var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsLearningBaseline), + EnabledStandardLearningBaselineIds); var custom = DynamicCustomBaselines.Where(x => x.IsLearningBaseline); return standard @@ -447,7 +479,8 @@ public static IReadOnlyList GetPureBaselineCandidates(bool hasUs public static IReadOnlyList GetCombinationCarrierBaselines(bool hasUsableImatrix) { - var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsCombinationCarrierCandidate), EnabledStandardCombinationCarrierIds); + var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsCombinationCarrierCandidate), + EnabledStandardCombinationCarrierIds); var custom = DynamicCustomBaselines.Where(x => x.IsCombinationCarrierCandidate); var result = standard @@ -459,9 +492,11 @@ public static IReadOnlyList GetCombinationCarrierBaselines(bool return result.Count == 0 ? new[] { Q8_0 } : result; } - public static IReadOnlyList GetGroupCombinationCandidates(bool hasUsableImatrix, bool allowHighPrecisionHybrids) + public static IReadOnlyList GetGroupCombinationCandidates(bool hasUsableImatrix, + bool allowHighPrecisionHybrids) { - var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsExplicitGroupCombinationCandidate), EnabledStandardExplicitCandidateIds); + var standard = FilterStandardByRole(StandardBaselines.Where(x => x.IsExplicitGroupCombinationCandidate), + EnabledStandardExplicitCandidateIds); var custom = DynamicCustomBaselines.Where(x => x.IsExplicitGroupCombinationCandidate); return standard @@ -472,7 +507,8 @@ public static IReadOnlyList GetGroupCombinationCandidates(bool h .ToList(); } - public static IReadOnlyList GetGroupCombinationCandidatesSmallestFirst(bool hasUsableImatrix, bool allowHighPrecisionHybrids) => + public static IReadOnlyList GetGroupCombinationCandidatesSmallestFirst(bool hasUsableImatrix, + bool allowHighPrecisionHybrids) => GetGroupCombinationCandidates(hasUsableImatrix, allowHighPrecisionHybrids) .OrderBy(x => x.BitRange) .ThenBy(x => x.ExplicitCandidateSortOrder) @@ -491,8 +527,12 @@ public static IReadOnlyList GetExactHighPrecisionAliases(bool al public static BaselineQuants GetDefaultExplicitFallbackBaseline() => Q8_0; public static bool IsNullTensorConfigGroupSlot(byte storedValue) => storedValue == TensorConfigNullSlotValue; - public static byte EncodeTensorConfigGroupSlot(BaselineQuants baseline) => EncodeTensorConfigGroupSlotBaselineId(baseline.UniqueId); - public static byte EncodeTensorConfigGroupSlot(TensorWeightScheme exactScheme) => EncodeTensorConfigGroupSlotBaselineId(GetExactOverrideStorageId(exactScheme)); + + public static byte EncodeTensorConfigGroupSlot(BaselineQuants baseline) => + EncodeTensorConfigGroupSlotBaselineId(baseline.UniqueId); + + public static byte EncodeTensorConfigGroupSlot(TensorWeightScheme exactScheme) => + EncodeTensorConfigGroupSlotBaselineId(GetExactOverrideStorageId(exactScheme)); public static byte EncodeTensorConfigGroupSlotBaselineId(byte baselineId) { @@ -505,12 +545,15 @@ public static byte EncodeTensorConfigGroupSlotBaselineId(byte baselineId) public static byte DecodeTensorConfigGroupSlotToBaselineId(byte storedValue) { if (IsNullTensorConfigGroupSlot(storedValue)) - throw new InvalidOperationException("Tensor-config group slot 0 represents NULL and cannot be decoded as a baseline id."); + throw new InvalidOperationException( + "Tensor-config group slot 0 represents NULL and cannot be decoded as a baseline id."); return checked((byte)(storedValue - 1)); } - public static BaselineQuants DecodeTensorConfigGroupSlotToBaseline(byte storedValue) => FromId(DecodeTensorConfigGroupSlotToBaselineId(storedValue)); + public static BaselineQuants DecodeTensorConfigGroupSlotToBaseline(byte storedValue) => + FromId(DecodeTensorConfigGroupSlotToBaselineId(storedValue)); + public static bool IsNativeExactAlias(BaselineQuants baseline) => IsNativeExactAlias(baseline.UniqueId); public static bool IsNativeExactAlias(byte baselineId) @@ -520,7 +563,8 @@ public static bool IsNativeExactAlias(byte baselineId) baselineId == F16_Hybrid.UniqueId; } - public static byte CanonicalLearningBaselineId(BaselineQuants baseline) => CanonicalLearningBaselineId(baseline.UniqueId); + public static byte CanonicalLearningBaselineId(BaselineQuants baseline) => + CanonicalLearningBaselineId(baseline.UniqueId); public static byte CanonicalLearningBaselineId(byte baselineId) { @@ -588,7 +632,8 @@ public static void ValidateIntegrityOrThrow() .ToList(); if (duplicateKeys.Count > 0) - throw new InvalidOperationException($"Duplicate baseline canonical keys detected: {string.Join(", ", duplicateKeys)}"); + throw new InvalidOperationException( + $"Duplicate baseline canonical keys detected: {string.Join(", ", duplicateKeys)}"); } public static BaselineQuants FromId(byte id) @@ -625,4 +670,4 @@ public static BaselineQuants FromTensorSchemeId(byte schemeId) return found; } -} +} \ No newline at end of file diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 2494904..53ba34e 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -11,7 +11,7 @@ if (args.Length == 0) { // Use: "clone" or "evolution" - const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. + const string debugMode = "clone"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) { diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index e69da70..272aaba 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -384,6 +384,7 @@ private static void AppendReleaseMetadata(StringBuilder sb, ReadmeCloneContext? sb.AppendLine( $"- [Replacement details]({MagicQuantManifestPathService.HuggingFaceResolvePath(MagicQuantManifestPathService.ReplacementsFileName)}) — structured details for baselines or anchors removed from the final download table, including reason codes, KLD deltas, PPL delta %, and size deltas."); + sb.AppendLine(); AppendReasonCodeDetails(sb); sb.AppendLine(); } @@ -548,21 +549,21 @@ private static bool NeedsYamlQuotes(string text) private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection rows) { - sb.AppendLine("| Name | Provider | Quant Family | KLD | PPL | PPL Δ % | Size (GB) | Download |"); - sb.AppendLine("|---|---|---|---:|---:|---:|---:|---|"); + sb.AppendLine("| Name | Provider | Quant Family | KLD | Size (GB) | Download |"); + sb.AppendLine("|---|---|---|---:|---:|---|"); foreach (var row in rows.OrderBy(x => x.Kld ?? double.MaxValue).ThenBy(x => x.SizeBytes)) { string kld = row.Kld.HasValue ? row.Kld.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; - string ppl = row.Ppl.HasValue ? row.Ppl.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; - string pplDelta = row.PplDeltaPercent.HasValue + //string ppl = row.Ppl.HasValue ? row.Ppl.Value.ToString("0.000000", CultureInfo.InvariantCulture) : "n/a"; + /*string pplDelta = row.PplDeltaPercent.HasValue ? row.PplDeltaPercent.Value.ToString("0.000", CultureInfo.InvariantCulture) + "%" - : "n/a"; + : "n/a";*/ string sizeGb = ToGB(row.SizeBytes); string download = string.IsNullOrWhiteSpace(row.DownloadTarget) ? "n/a" : $"[Link]({row.DownloadTarget})"; sb.AppendLine( - $"| {row.NameCell} | {EscapePipe(row.Provider)} | {EscapePipe(row.QuantFamily)} | {kld} | {ppl} | {pplDelta} | {sizeGb} | {download} |"); + $"| {row.NameCell} | {EscapePipe(row.Provider)} | {EscapePipe(row.QuantFamily)} | {kld} | {sizeGb} | {download} |"); } } From 52a22ea05c0152fa513e9784084317f75ecf2f00 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 12 May 2026 13:28:36 -0400 Subject: [PATCH 205/258] readme fixup --- MagicQuant/Services/ReadmeGenerationService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index 272aaba..aab3a1c 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -549,8 +549,8 @@ private static bool NeedsYamlQuotes(string text) private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection rows) { - sb.AppendLine("| Name | Provider | Quant Family | KLD | Size (GB) | Download |"); - sb.AppendLine("|---|---|---|---:|---:|---|"); + sb.AppendLine("| Name | Provider | KLD | Size (GB) | Download |"); + sb.AppendLine("|---|---|---:|---:|---|"); foreach (var row in rows.OrderBy(x => x.Kld ?? double.MaxValue).ThenBy(x => x.SizeBytes)) { @@ -563,7 +563,7 @@ private static void AppendDownloadTable(StringBuilder sb, IReadOnlyCollection Date: Tue, 12 May 2026 13:31:31 -0400 Subject: [PATCH 206/258] disable exporting final artifact again. --- MagicQuant/config.dev.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 7584b45..28f1c7e 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-35B-A3B-Qwen/ + model_dir: /mnt/world8/AI/Models/Qwen3.6-35B-A3B-uncensored-heretic-llmfan46/ llama_root: llama_bin: convert_script: @@ -86,7 +86,7 @@ readme: - magicquant - conversational base_model: - - Qwen/Qwen3.6-35B-A3B + - llmfan46/Qwen3.6-35B-A3B-uncensored-heretic hardware: gpu_memory_limits_gb: @@ -260,7 +260,7 @@ output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: output_name_prefix: Qwen3.6-27B - export_external_learned_baselines: true + export_external_learned_baselines: false # false = normal behavior; delete/rebuild final outputs from scratch. # true = preserve valid existing GGUFs and skip rebuilding them only when From fa619c09f11a9bcb64e0d4a40b1f74b9963efd2d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 19 May 2026 11:10:51 -0400 Subject: [PATCH 207/258] updates --- MagicQuant/Program.cs | 4 ++-- MagicQuant/config.dev.yaml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 53ba34e..ad63f5d 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -11,7 +11,7 @@ if (args.Length == 0) { // Use: "clone" or "evolution" - const string debugMode = "clone"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. + const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) { @@ -21,7 +21,7 @@ "--architecture-family", @"""Qwen3.6-35B-A3B""", "--source-repo", @"""magiccodingman/Qwen3.6-35B-A3B-MagicQuant-GGUF""" ,"--allow-architecture-family-alias-override" - , "--reuse-existing-final-artifacts" + //, "--reuse-existing-final-artifacts" ]; } else diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 28f1c7e..f9d65fe 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -67,7 +67,7 @@ readme: # Optional title model name override used in: # # MagicQuant Hybrids (v2.0) - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.6-35B-A3B + title_model_name_override: Qwen3.6-35B-A3B (By llmfan46) # Hugging Face README frontmatter. # Scalars render as: @@ -259,7 +259,7 @@ anomaly_detection: output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: - output_name_prefix: Qwen3.6-27B + output_name_prefix: Qwen3.6-35B-A3B export_external_learned_baselines: false # false = normal behavior; delete/rebuild final outputs from scratch. From a1d9c1e7c1504b83aa91ffcf28067db32c176657 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Tue, 19 May 2026 12:47:52 -0400 Subject: [PATCH 208/258] updates --- MQ.DB/tensor_groups.yaml | 14 +++- MagicQuant/Program.cs | 2 +- MagicQuant/config.dev.yaml | 129 +++++-------------------------------- 3 files changed, 29 insertions(+), 116 deletions(-) diff --git a/MQ.DB/tensor_groups.yaml b/MQ.DB/tensor_groups.yaml index 24054ed..e587fd5 100644 --- a/MQ.DB/tensor_groups.yaml +++ b/MQ.DB/tensor_groups.yaml @@ -364,4 +364,16 @@ base_quant_exceptions: # Qwen3.6 / hybrid attention-state gate. # This is not a dense FFN gate and not an MoE router. If ssm is not active, # allow it to fallback rather than being misclassified. - - "^blk\\..*\\.attn_gate\\.weight$" \ No newline at end of file + - "^blk\\..*\\.attn_gate\\.weight$" + + # Qwen3.5 / Qwen3.6 MTP / NEXTN speculative decoding tensors. + # Keep these protected. They should not be assigned to embeddings, + # lm_head, FFN, attention, SSM, or MoE groups. + - "^blk\\..*\\.nextn\\..*$" + - ".*nextn.*" + - ".*mtp.*" + - ".*eh_proj.*" + - ".*shared_head.*" + - ".*shared_head_norm.*" + - ".*pre_fc_norm.*" + \ No newline at end of file diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index ad63f5d..0c32126 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -33,7 +33,7 @@ args = [ "evolution", - "--architecture-family", @"""Qwen3.6-35B-A3B""" + "--architecture-family", @"""Qwen3.6-27B""" ,"--reuse-existing-final-artifacts" ]; } diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index f9d65fe..dc43967 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-35B-A3B-uncensored-heretic-llmfan46/ + model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ llama_root: llama_bin: convert_script: @@ -67,7 +67,7 @@ readme: # Optional title model name override used in: # # MagicQuant Hybrids (v2.0) - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.6-35B-A3B (By llmfan46) + title_model_name_override: Qwen3.6-27B # Hugging Face README frontmatter. # Scalars render as: @@ -86,7 +86,7 @@ readme: - magicquant - conversational base_model: - - llmfan46/Qwen3.6-35B-A3B-uncensored-heretic + - Qwen/Qwen3.6-27B hardware: gpu_memory_limits_gb: @@ -259,8 +259,8 @@ anomaly_detection: output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: - output_name_prefix: Qwen3.6-35B-A3B - export_external_learned_baselines: false + output_name_prefix: Qwen3.6-27B + export_external_learned_baselines: true # false = normal behavior; delete/rebuild final outputs from scratch. # true = preserve valid existing GGUFs and skip rebuilding them only when @@ -272,7 +272,7 @@ output: # See candidate_selection above for the active final chooser settings. identity: - architecture_family_name: Qwen3.6-35B-A3B + architecture_family_name: Qwen3.6-27B allow_architecture_family_alias_override: false baselines: @@ -282,7 +282,7 @@ baselines: enabled_standard_explicit_group_candidates: [] custom_repositories: - - repo_id: unsloth/Qwen3.6-35B-A3B-GGUF + - repo_id: unsloth/Qwen3.6-27B-GGUF enabled: true short_source_name: Unsloth source_kind: huggingface_gguf_repository @@ -297,7 +297,7 @@ baselines: includes: - - file_name: Qwen3.6-35B-A3B-UD-IQ2_M.gguf + - file_name: Qwen3.6-27B-UD-IQ2_M.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-IQ2_M @@ -306,7 +306,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-IQ2_XXS.gguf + - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf baseline_family: IQ2_XXS quantize_base_name: IQ2_XXS display_name: UD-IQ2_XXS @@ -315,16 +315,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-IQ3_S.gguf - baseline_family: IQ3_S - quantize_base_name: IQ3_S - display_name: UD-IQ3_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-IQ3_XXS.gguf + - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf baseline_family: IQ3_XXS quantize_base_name: IQ3_XXS display_name: UD-IQ3_XXS @@ -333,34 +324,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-IQ4_NL.gguf - baseline_family: IQ4_NL - quantize_base_name: IQ4_NL - display_name: UD-IQ4_NL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-IQ4_NL_XL.gguf - baseline_family: IQ4_NL - quantize_base_name: IQ4_NL - display_name: UD-IQ4_NL_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-IQ4_XS.gguf - baseline_family: IQ4_XS - quantize_base_name: IQ4_XS - display_name: UD-IQ4_XS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q2_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M display_name: UD-Q2_K_XL @@ -369,25 +333,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-Q3_K_M.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_M - display_name: UD-Q3_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q3_K_S.gguf - baseline_family: IQ3_S - quantize_base_name: IQ3_S - display_name: UD-Q3_K_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q3_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf baseline_family: IQ3_M quantize_base_name: IQ3_M display_name: UD-Q3_K_XL @@ -396,25 +342,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-Q4_K_M.gguf - baseline_family: Q4_K_M - quantize_base_name: Q4_K_M - display_name: UD-Q4_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q4_K_S.gguf - baseline_family: Q4_K_S - quantize_base_name: Q4_K_S - display_name: UD-Q4_K_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M display_name: UD-Q4_K_XL @@ -423,25 +351,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-Q5_K_M.gguf - baseline_family: Q5_K - quantize_base_name: Q5_K - display_name: UD-Q5_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q5_K_S.gguf - baseline_family: Q5_K_S - quantize_base_name: Q5_K_S - display_name: UD-Q5_K_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q5_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf baseline_family: Q5_K quantize_base_name: Q5_K display_name: UD-Q5_K_XL @@ -450,16 +360,7 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-35B-A3B-UD-Q6_K.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: UD-Q6_K - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-35B-A3B-UD-Q6_K_XL.gguf + - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf baseline_family: Q6_K quantize_base_name: Q6_K display_name: UD-Q6_K_XL From 8ea49da775b32e88a95d94d69c78c7b97c3c0b40 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 19 May 2026 13:12:48 -0400 Subject: [PATCH 209/258] Add clone manifest subset tensor builder --- .../CloneManifestTensorMapBuildService.cs | 344 ++++++++++++++++++ 1 file changed, 344 insertions(+) create mode 100644 MagicQuant/Services/CloneManifestTensorMapBuildService.cs diff --git a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs new file mode 100644 index 0000000..c1b019c --- /dev/null +++ b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs @@ -0,0 +1,344 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text; +using MagicQuant.Helpers; +using MagicQuant.Models; +using MQ.DB; +using MQ.DB.Models; +using Spectre.Console; + +namespace MagicQuant.Services; + +/// +/// Clone-mode exact tensor-map builder. +/// +/// Normal exact-map builds remain strict. Clone mode can optionally allow a source model +/// to contain extra tensors that are absent from an older manifest. In that case, only the +/// manifest tensors receive explicit --tensor-type overrides; the extra source tensors are +/// intentionally left to llama.cpp's normal base-quant behavior. +/// +public sealed class CloneManifestTensorMapBuildService +{ + public const string AllowMissingManifestTensorsFlag = "allow-missing-manifest-tensors"; + public const string AllowMissingManifestTensorsCliSwitch = "--" + AllowMissingManifestTensorsFlag; + + private readonly QuantizationService _quantizationService; + private readonly ImatrixService _imatrixService; + + public CloneManifestTensorMapBuildService( + QuantizationService quantizationService, + ImatrixService imatrixService) + { + _quantizationService = quantizationService ?? throw new ArgumentNullException(nameof(quantizationService)); + _imatrixService = imatrixService ?? throw new ArgumentNullException(nameof(imatrixService)); + } + + public async Task BuildAsync( + IReadOnlyDictionary tensorTypes, + string outputPath, + string baseQuantName, + bool allowMissingManifestTensors, + bool forceRebuild = false, + CancellationToken ct = default) + { + if (tensorTypes == null || tensorTypes.Count == 0) + throw new ArgumentException("A clone tensor map must contain at least one tensor entry.", nameof(tensorTypes)); + + if (string.IsNullOrWhiteSpace(outputPath)) + throw new InvalidOperationException("Export output path is required."); + + string nativeBasePath = await _quantizationService.EnsureBaseModelFileAsync(); + var sourceTensorTypes = await _quantizationService.ReadExactTensorTypesAsync(nativeBasePath, ct); + var sourceTensorNames = sourceTensorTypes.Keys.ToList(); + + var missingInManifest = sourceTensorNames + .Except(tensorTypes.Keys, StringComparer.Ordinal) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + var unexpectedInManifest = tensorTypes.Keys + .Except(sourceTensorNames, StringComparer.Ordinal) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + bool exactMatch = missingInManifest.Count == 0 && + unexpectedInManifest.Count == 0 && + sourceTensorNames.Count == tensorTypes.Count; + + if (exactMatch) + { + return await _quantizationService.BuildExportArtifactFromExactTensorMapAsync( + tensorTypes: tensorTypes, + outputPath: outputPath, + baseQuantName: baseQuantName, + forceRebuild: forceRebuild, + ct: ct); + } + + bool sourceModelIsManifestSuperset = missingInManifest.Count > 0 && unexpectedInManifest.Count == 0; + if (!allowMissingManifestTensors || !sourceModelIsManifestSuperset) + { + throw new InvalidOperationException(BuildManifestMismatchError( + missingInManifest, + unexpectedInManifest, + modelTensorCount: sourceTensorNames.Count, + manifestTensorCount: tensorTypes.Count, + includeSubsetHint: sourceModelIsManifestSuperset)); + } + + return await BuildSubsetOverrideCloneAsync( + inputFile: nativeBasePath, + outputFile: outputPath, + tensorTypes: tensorTypes, + baseQuantName: baseQuantName, + missingInManifest: missingInManifest, + forceRebuild: forceRebuild, + ct: ct); + } + + private async Task BuildSubsetOverrideCloneAsync( + string inputFile, + string outputFile, + IReadOnlyDictionary tensorTypes, + string baseQuantName, + IReadOnlyList missingInManifest, + bool forceRebuild, + CancellationToken ct) + { + var baseQuant = BaselineQuants.ResolveBuiltInStandardBaseline(baseQuantName) + ?? BaselineQuants.Q8_0; + + Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); + + if (!forceRebuild && File.Exists(outputFile) && new FileInfo(outputFile).Length > 0) + return outputFile; + + if (forceRebuild) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputFile); + await HardDeleteHelper.DeleteFileIfExistsAsync(outputFile + ".success.json"); + } + + AnsiConsole.MarkupLine( + $"[yellow]Clone manifest subset allowed:[/] [cyan]{missingInManifest.Count:N0}[/] source tensor(s) are absent from the manifest and will receive no explicit --tensor-type override."); + + foreach (var tensorName in missingInManifest.Take(15)) + AnsiConsole.MarkupLine($"[grey] basequant fallback tensor:[/] {Markup.Escape(tensorName)}"); + + if (missingInManifest.Count > 15) + AnsiConsole.MarkupLine($"[grey] ...and {missingInManifest.Count - 15:N0} more tensor(s).[/]"); + + var args = new List(capacity: tensorTypes.Count * 2 + 8); + foreach (var kv in tensorTypes.OrderBy(x => x.Key, StringComparer.Ordinal)) + { + args.Add("--tensor-type"); + args.Add($"{kv.Key}={NormalizeCloneQuantName(kv.Value)}"); + } + + if (_imatrixService.ShouldUseImatrixForQuant(HybridQuant.CreatePureBaseline(baseQuant))) + { + string imatrixPath = _imatrixService.GetCanonicalImatrixPath(); + if (!File.Exists(imatrixPath)) + throw new InvalidOperationException($"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); + + args.Add("--imatrix"); + args.Add(imatrixPath); + } + + args.Add(inputFile); + args.Add(outputFile); + args.Add(baseQuant.QuantizeBaseArgumentName); + args.Add(ResolveCloneQuantizeThreadCount().ToString()); + + string bin = Path.Combine( + Cache.LlamaBin!, + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "llama-quantize.exe" : "llama-quantize"); + + string quantizeLogPath = outputFile + ".quantize.log"; + Directory.CreateDirectory(Path.GetDirectoryName(quantizeLogPath)!); + + var psi = new ProcessStartInfo + { + FileName = bin + }; + + foreach (var arg in args) + psi.ArgumentList.Add(arg); + + AnsiConsole.MarkupLine( + $"[cyan]Quantizing clone artifact from manifest subset:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); + + var result = await RunLoggedProcessAsync(psi, quantizeLogPath, ct); + if (result.ExitCode != 0) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputFile); + throw new InvalidOperationException( + $"Clone quantization failed for '{outputFile}'. ExitCode={result.ExitCode}. See '{quantizeLogPath}'."); + } + + if (!File.Exists(outputFile) || new FileInfo(outputFile).Length == 0) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputFile); + throw new InvalidOperationException( + $"Clone quantization exited successfully but produced no valid GGUF output: {outputFile}"); + } + + await File.WriteAllTextAsync(outputFile + ".success.json", "{\"status\":\"success\"}", ct); + AnsiConsole.MarkupLine($"[green]Clone quantized model ready:[/] {Markup.Escape(outputFile)}"); + return outputFile; + } + + private static string BuildManifestMismatchError( + IReadOnlyList missingInManifest, + IReadOnlyList unexpectedInManifest, + int modelTensorCount, + int manifestTensorCount, + bool includeSubsetHint) + { + var builder = new StringBuilder(); + builder.Append("Clone tensor manifest does not exactly match this model architecture. "); + builder.Append($"MissingInManifest=[{string.Join(", ", missingInManifest.Take(20))}] "); + builder.Append($"UnexpectedInManifest=[{string.Join(", ", unexpectedInManifest.Take(20))}] "); + builder.Append($"ModelTensorCount={modelTensorCount} ManifestTensorCount={manifestTensorCount}."); + + if (includeSubsetHint) + { + builder.AppendLine(); + builder.Append("This looks like a clone manifest subset: every manifest tensor exists in the current model, "); + builder.Append("but the current model has extra tensors. To let those extra tensors fall through to llama.cpp/base-quant behavior, rerun clone mode with "); + builder.Append(AllowMissingManifestTensorsCliSwitch); + builder.Append('.'); + } + + return builder.ToString(); + } + + private static string NormalizeCloneQuantName(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return "UNKNOWN"; + + string token = value.Trim().Replace("-", "_").Replace(" ", string.Empty).ToUpperInvariant(); + + foreach (var scheme in TensorWeightScheme.All) + { + if (scheme.Names.IsDefaultOrEmpty) + continue; + + if (scheme.Names.Any(name => string.Equals( + name.Trim().Replace("-", "_").Replace(" ", string.Empty).ToUpperInvariant(), + token, + StringComparison.Ordinal))) + { + return scheme.Names[0]; + } + } + + return token; + } + + private static int ResolveCloneQuantizeThreadCount() + { + int threadCount = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; + int reservedThreads = threadCount switch + { + >= 16 => 2, + >= 8 => 2, + >= 4 => 1, + _ => 0 + }; + + return Math.Max(1, threadCount - reservedThreads); + } + + private static async Task RunLoggedProcessAsync( + ProcessStartInfo psi, + string logPath, + CancellationToken ct) + { + psi.RedirectStandardOutput = true; + psi.RedirectStandardError = true; + psi.UseShellExecute = false; + psi.CreateNoWindow = true; + + using var process = new Process + { + StartInfo = psi, + EnableRaisingEvents = true + }; + + var stdoutBuilder = new StringBuilder(); + var stderrBuilder = new StringBuilder(); + object sync = new(); + + var stdoutClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var stderrClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + await using var logStream = new FileStream(logPath, FileMode.Create, FileAccess.Write, FileShare.Read); + await using var logWriter = new StreamWriter(logStream) { AutoFlush = true }; + + void HandleLine(string? line, bool isError) + { + if (line == null) + { + if (isError) + stderrClosed.TrySetResult(true); + else + stdoutClosed.TrySetResult(true); + + return; + } + + lock (sync) + { + if (isError) + stderrBuilder.AppendLine(line); + else + stdoutBuilder.AppendLine(line); + + logWriter.WriteLine(line); + } + + if (Cache.VerboseProcessOutput) + AnsiConsole.WriteLine(line); + } + + process.OutputDataReceived += (_, e) => HandleLine(e.Data, isError: false); + process.ErrorDataReceived += (_, e) => HandleLine(e.Data, isError: true); + + if (!process.Start()) + throw new InvalidOperationException($"Failed to start process: {psi.FileName}"); + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + using var ctr = ct.Register(() => + { + try + { + if (!process.HasExited) + process.Kill(entireProcessTree: true); + } + catch + { + } + }); + + await process.WaitForExitAsync(ct); + await Task.WhenAll(stdoutClosed.Task, stderrClosed.Task); + + return new LoggedProcessResult + { + ExitCode = process.ExitCode, + StdOut = stdoutBuilder.ToString(), + StdErr = stderrBuilder.ToString() + }; + } + + private sealed class LoggedProcessResult + { + public int ExitCode { get; init; } + public string StdOut { get; init; } = string.Empty; + public string StdErr { get; init; } = string.Empty; + } +} From 7ccac663b5c02d19f00f046f7a41bcc287498f8c Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Tue, 19 May 2026 13:16:59 -0400 Subject: [PATCH 210/258] Wire clone subset manifest flag --- MagicQuant/Commands/CloneRepositoryQuants.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 0a3eadf..29502ed 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -45,6 +45,8 @@ public async Task Run(List args) return; } + bool allowMissingManifestTensors = args.Any(a => string.Equals(a.Name, CloneManifestTensorMapBuildService.AllowMissingManifestTensorsFlag, StringComparison.OrdinalIgnoreCase)); + string? modelDirRaw = Get(args, "model-dir"); if (string.IsNullOrWhiteSpace(modelDirRaw)) modelDirRaw = Config.Current.Paths.ModelDir; @@ -83,6 +85,7 @@ public async Task Run(List args) AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); AnsiConsole.MarkupLine($"Reuse final artifacts: {(Config.ReuseExistingFinalArtifacts ? "[green]yes[/]" : "[grey]no[/]")}"); + AnsiConsole.MarkupLine($"Allow missing manifest tensors: {(allowMissingManifestTensors ? "[yellow]yes[/]" : "[grey]no[/]")}"); AnsiConsole.MarkupLine("Getting safetensors hash. This may take a bit, please wait..."); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); @@ -106,6 +109,7 @@ public async Task Run(List args) var benchmarkService = new BenchmarkService(pyManager); var quantizationService = new QuantizationService(benchmarkService); var imatrixService = new ImatrixService(); + var cloneBuildService = new CloneManifestTensorMapBuildService(quantizationService, imatrixService); string baseModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); @@ -241,10 +245,11 @@ await EnsureCloneNativeBenchmarkArtifactsReadyAsync( } else { - await quantizationService.BuildExportArtifactFromExactTensorMapAsync( + await cloneBuildService.BuildAsync( tensorTypes: artifact.TensorTypes, outputPath: outputFile, baseQuantName: baseQuantName, + allowMissingManifestTensors: allowMissingManifestTensors, forceRebuild: true); } @@ -870,6 +875,7 @@ private static void ShowHelp() AnsiConsole.MarkupLine(" --source-json Local or http(s) path to magicquant.clone-configs.json"); AnsiConsole.MarkupLine(" --use-imatrix Use configured/provided imatrix for the cloned model"); AnsiConsole.MarkupLine(" --reuse-existing-final-artifacts Reuse matching existing GGUFs and matching clone benchmark JSON rows"); + AnsiConsole.MarkupLine(" --allow-missing-manifest-tensors Allow clone manifests that are strict subsets of the current model tensor list; extra source tensors receive no explicit --tensor-type override and fall through to base quantization"); AnsiConsole.MarkupLine(" --recheck-hardware-probe / --force-refresh-hardware-probe Force Q8/native hardware probe and refresh the SQLite execution-plan cache"); } From 2b06a14969cf170441a12de7d530f021d8391a92 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 25 May 2026 11:21:09 -0400 Subject: [PATCH 211/258] Support clone missing-manifest base quant override --- .../CloneManifestTensorMapBuildService.cs | 50 ++++++++++++++++--- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs index c1b019c..1db309e 100644 --- a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs +++ b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs @@ -15,12 +15,15 @@ namespace MagicQuant.Services; /// Normal exact-map builds remain strict. Clone mode can optionally allow a source model /// to contain extra tensors that are absent from an older manifest. In that case, only the /// manifest tensors receive explicit --tensor-type overrides; the extra source tensors are -/// intentionally left to llama.cpp's normal base-quant behavior. +/// intentionally left to llama.cpp's normal base-quant behavior unless a clone-specific +/// missing-manifest base-quant override is provided. /// public sealed class CloneManifestTensorMapBuildService { public const string AllowMissingManifestTensorsFlag = "allow-missing-manifest-tensors"; public const string AllowMissingManifestTensorsCliSwitch = "--" + AllowMissingManifestTensorsFlag; + public const string MissingManifestBaseQuantFlag = "missing-manifest-base-quant"; + public const string MissingManifestBaseQuantCliSwitch = "--" + MissingManifestBaseQuantFlag; private readonly QuantizationService _quantizationService; private readonly ImatrixService _imatrixService; @@ -38,6 +41,7 @@ public async Task BuildAsync( string outputPath, string baseQuantName, bool allowMissingManifestTensors, + string? missingManifestBaseQuantName = null, bool forceRebuild = false, CancellationToken ct = default) { @@ -47,6 +51,9 @@ public async Task BuildAsync( if (string.IsNullOrWhiteSpace(outputPath)) throw new InvalidOperationException("Export output path is required."); + bool hasMissingManifestBaseQuantOverride = !string.IsNullOrWhiteSpace(missingManifestBaseQuantName); + bool allowManifestSubset = allowMissingManifestTensors || hasMissingManifestBaseQuantOverride; + string nativeBasePath = await _quantizationService.EnsureBaseModelFileAsync(); var sourceTensorTypes = await _quantizationService.ReadExactTensorTypesAsync(nativeBasePath, ct); var sourceTensorNames = sourceTensorTypes.Keys.ToList(); @@ -70,13 +77,13 @@ public async Task BuildAsync( return await _quantizationService.BuildExportArtifactFromExactTensorMapAsync( tensorTypes: tensorTypes, outputPath: outputPath, - baseQuantName: baseQuantName, + baseQuantName: hasMissingManifestBaseQuantOverride ? missingManifestBaseQuantName! : baseQuantName, forceRebuild: forceRebuild, ct: ct); } bool sourceModelIsManifestSuperset = missingInManifest.Count > 0 && unexpectedInManifest.Count == 0; - if (!allowMissingManifestTensors || !sourceModelIsManifestSuperset) + if (!allowManifestSubset || !sourceModelIsManifestSuperset) { throw new InvalidOperationException(BuildManifestMismatchError( missingInManifest, @@ -91,6 +98,7 @@ public async Task BuildAsync( outputFile: outputPath, tensorTypes: tensorTypes, baseQuantName: baseQuantName, + missingManifestBaseQuantName: missingManifestBaseQuantName, missingInManifest: missingInManifest, forceRebuild: forceRebuild, ct: ct); @@ -101,12 +109,13 @@ private async Task BuildSubsetOverrideCloneAsync( string outputFile, IReadOnlyDictionary tensorTypes, string baseQuantName, + string? missingManifestBaseQuantName, IReadOnlyList missingInManifest, bool forceRebuild, CancellationToken ct) { - var baseQuant = BaselineQuants.ResolveBuiltInStandardBaseline(baseQuantName) - ?? BaselineQuants.Q8_0; + var baseQuant = ResolveCloneBaseQuantOrThrow(baseQuantName, missingManifestBaseQuantName); + bool hasMissingManifestBaseQuantOverride = !string.IsNullOrWhiteSpace(missingManifestBaseQuantName); Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); @@ -122,6 +131,10 @@ private async Task BuildSubsetOverrideCloneAsync( AnsiConsole.MarkupLine( $"[yellow]Clone manifest subset allowed:[/] [cyan]{missingInManifest.Count:N0}[/] source tensor(s) are absent from the manifest and will receive no explicit --tensor-type override."); + AnsiConsole.MarkupLine(hasMissingManifestBaseQuantOverride + ? $"[yellow]Missing-manifest base quant override:[/] [cyan]{Markup.Escape(baseQuant.Names[0])}[/] will be used for source tensors absent from the manifest." + : $"[grey]Missing-manifest tensors will use artifact base quant:[/] {Markup.Escape(baseQuant.Names[0])}"); + foreach (var tensorName in missingInManifest.Take(15)) AnsiConsole.MarkupLine($"[grey] basequant fallback tensor:[/] {Markup.Escape(tensorName)}"); @@ -166,7 +179,7 @@ private async Task BuildSubsetOverrideCloneAsync( psi.ArgumentList.Add(arg); AnsiConsole.MarkupLine( - $"[cyan]Quantizing clone artifact from manifest subset:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); + $"[cyan]Quantizing clone artifact from manifest subset:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](base quant: {Markup.Escape(baseQuant.Names[0])}; log: {Markup.Escape(quantizeLogPath)})[/]"); var result = await RunLoggedProcessAsync(psi, quantizeLogPath, ct); if (result.ExitCode != 0) @@ -188,6 +201,25 @@ private async Task BuildSubsetOverrideCloneAsync( return outputFile; } + private static BaselineQuants ResolveCloneBaseQuantOrThrow(string baseQuantName, string? missingManifestBaseQuantName) + { + bool hasOverride = !string.IsNullOrWhiteSpace(missingManifestBaseQuantName); + string quantName = hasOverride ? missingManifestBaseQuantName!.Trim() : baseQuantName; + + var resolved = BaselineQuants.ResolveBuiltInStandardBaseline(quantName); + if (resolved != null) + return resolved; + + if (hasOverride) + { + throw new InvalidOperationException( + $"Unknown {MissingManifestBaseQuantCliSwitch} value '{missingManifestBaseQuantName}'. " + + "Use a built-in llama.cpp base quant name such as Q8_0, Q6_K, Q5_K_M, or Q4_K_M."); + } + + return BaselineQuants.Q8_0; + } + private static string BuildManifestMismatchError( IReadOnlyList missingInManifest, IReadOnlyList unexpectedInManifest, @@ -205,9 +237,11 @@ private static string BuildManifestMismatchError( { builder.AppendLine(); builder.Append("This looks like a clone manifest subset: every manifest tensor exists in the current model, "); - builder.Append("but the current model has extra tensors. To let those extra tensors fall through to llama.cpp/base-quant behavior, rerun clone mode with "); + builder.Append("but the current model has extra tensors. To let those extra tensors fall through to the artifact base quant, rerun clone mode with "); builder.Append(AllowMissingManifestTensorsCliSwitch); - builder.Append('.'); + builder.Append(". To force those extra tensors to a specific base quant, use "); + builder.Append(MissingManifestBaseQuantCliSwitch); + builder.Append(" Q8_0."); } return builder.ToString(); From 19e6f82cb2a68cc34ec95db1293d58b0c8a7c5b9 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 25 May 2026 11:23:57 -0400 Subject: [PATCH 212/258] Wire missing-manifest base quant CLI flag --- MagicQuant/Commands/CloneRepositoryQuants.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 29502ed..158f3de 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -46,6 +46,8 @@ public async Task Run(List args) } bool allowMissingManifestTensors = args.Any(a => string.Equals(a.Name, CloneManifestTensorMapBuildService.AllowMissingManifestTensorsFlag, StringComparison.OrdinalIgnoreCase)); + string? missingManifestBaseQuantName = Get(args, CloneManifestTensorMapBuildService.MissingManifestBaseQuantFlag); + bool hasMissingManifestBaseQuantOverride = !string.IsNullOrWhiteSpace(missingManifestBaseQuantName); string? modelDirRaw = Get(args, "model-dir"); if (string.IsNullOrWhiteSpace(modelDirRaw)) @@ -85,7 +87,10 @@ public async Task Run(List args) AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); AnsiConsole.MarkupLine($"Reuse final artifacts: {(Config.ReuseExistingFinalArtifacts ? "[green]yes[/]" : "[grey]no[/]")}"); - AnsiConsole.MarkupLine($"Allow missing manifest tensors: {(allowMissingManifestTensors ? "[yellow]yes[/]" : "[grey]no[/]")}"); + AnsiConsole.MarkupLine($"Allow missing manifest tensors: {(allowMissingManifestTensors || hasMissingManifestBaseQuantOverride ? "[yellow]yes[/]" : "[grey]no[/]")}"); + AnsiConsole.MarkupLine(hasMissingManifestBaseQuantOverride + ? $"Missing-manifest base quant override: [yellow]{Markup.Escape(missingManifestBaseQuantName!)}[/]" + : "Missing-manifest base quant override: [grey]none[/]"); AnsiConsole.MarkupLine("Getting safetensors hash. This may take a bit, please wait..."); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); @@ -250,6 +255,7 @@ await cloneBuildService.BuildAsync( outputPath: outputFile, baseQuantName: baseQuantName, allowMissingManifestTensors: allowMissingManifestTensors, + missingManifestBaseQuantName: missingManifestBaseQuantName, forceRebuild: true); } @@ -876,6 +882,7 @@ private static void ShowHelp() AnsiConsole.MarkupLine(" --use-imatrix Use configured/provided imatrix for the cloned model"); AnsiConsole.MarkupLine(" --reuse-existing-final-artifacts Reuse matching existing GGUFs and matching clone benchmark JSON rows"); AnsiConsole.MarkupLine(" --allow-missing-manifest-tensors Allow clone manifests that are strict subsets of the current model tensor list; extra source tensors receive no explicit --tensor-type override and fall through to base quantization"); + AnsiConsole.MarkupLine(" --missing-manifest-base-quant Allow strict-subset clone manifests and use this llama.cpp base quant for tensors absent from the manifest, e.g. Q8_0"); AnsiConsole.MarkupLine(" --recheck-hardware-probe / --force-refresh-hardware-probe Force Q8/native hardware probe and refresh the SQLite execution-plan cache"); } From 66f74952ffe753519b72637051d47518925996b9 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 25 May 2026 11:25:51 -0400 Subject: [PATCH 213/258] Keep missing-base override subset-only --- MagicQuant/Services/CloneManifestTensorMapBuildService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs index 1db309e..ab8c761 100644 --- a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs +++ b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs @@ -77,7 +77,7 @@ public async Task BuildAsync( return await _quantizationService.BuildExportArtifactFromExactTensorMapAsync( tensorTypes: tensorTypes, outputPath: outputPath, - baseQuantName: hasMissingManifestBaseQuantOverride ? missingManifestBaseQuantName! : baseQuantName, + baseQuantName: baseQuantName, forceRebuild: forceRebuild, ct: ct); } From 572ba3581b15ef2ab91598227be5e167621c7380 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 25 May 2026 13:14:46 -0400 Subject: [PATCH 214/258] Return clone subset build metadata --- .../CloneManifestTensorMapBuildService.cs | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs index ab8c761..daab5e2 100644 --- a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs +++ b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs @@ -9,6 +9,12 @@ namespace MagicQuant.Services; +public sealed record CloneManifestTensorMapBuildResult( + string OutputPath, + bool UsedManifestSubset, + string EffectiveBaseQuantName, + IReadOnlyList MissingInManifest); + /// /// Clone-mode exact tensor-map builder. /// @@ -36,7 +42,7 @@ public CloneManifestTensorMapBuildService( _imatrixService = imatrixService ?? throw new ArgumentNullException(nameof(imatrixService)); } - public async Task BuildAsync( + public async Task BuildAsync( IReadOnlyDictionary tensorTypes, string outputPath, string baseQuantName, @@ -74,12 +80,18 @@ public async Task BuildAsync( if (exactMatch) { - return await _quantizationService.BuildExportArtifactFromExactTensorMapAsync( + var exactOutputPath = await _quantizationService.BuildExportArtifactFromExactTensorMapAsync( tensorTypes: tensorTypes, outputPath: outputPath, baseQuantName: baseQuantName, forceRebuild: forceRebuild, ct: ct); + + return new CloneManifestTensorMapBuildResult( + OutputPath: exactOutputPath, + UsedManifestSubset: false, + EffectiveBaseQuantName: baseQuantName, + MissingInManifest: Array.Empty()); } bool sourceModelIsManifestSuperset = missingInManifest.Count > 0 && unexpectedInManifest.Count == 0; @@ -104,7 +116,7 @@ public async Task BuildAsync( ct: ct); } - private async Task BuildSubsetOverrideCloneAsync( + private async Task BuildSubsetOverrideCloneAsync( string inputFile, string outputFile, IReadOnlyDictionary tensorTypes, @@ -120,7 +132,13 @@ private async Task BuildSubsetOverrideCloneAsync( Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); if (!forceRebuild && File.Exists(outputFile) && new FileInfo(outputFile).Length > 0) - return outputFile; + { + return new CloneManifestTensorMapBuildResult( + OutputPath: outputFile, + UsedManifestSubset: true, + EffectiveBaseQuantName: baseQuant.Names[0], + MissingInManifest: missingInManifest.ToArray()); + } if (forceRebuild) { @@ -198,7 +216,12 @@ private async Task BuildSubsetOverrideCloneAsync( await File.WriteAllTextAsync(outputFile + ".success.json", "{\"status\":\"success\"}", ct); AnsiConsole.MarkupLine($"[green]Clone quantized model ready:[/] {Markup.Escape(outputFile)}"); - return outputFile; + + return new CloneManifestTensorMapBuildResult( + OutputPath: outputFile, + UsedManifestSubset: true, + EffectiveBaseQuantName: baseQuant.Names[0], + MissingInManifest: missingInManifest.ToArray()); } private static BaselineQuants ResolveCloneBaseQuantOrThrow(string baseQuantName, string? missingManifestBaseQuantName) From ee33c1dab43efe2f64725c1341331d68153563a8 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 25 May 2026 13:28:39 -0400 Subject: [PATCH 215/258] Persist resolved clone subset policy in manifest --- MagicQuant/Commands/CloneRepositoryQuants.cs | 183 ++++++++++++++++++- 1 file changed, 180 insertions(+), 3 deletions(-) diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 158f3de..35581de 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using System.Text.Json.Nodes; using MagicQuant.Configuration; using MagicQuant.Helpers; using MagicQuant.Models; @@ -28,6 +29,7 @@ public sealed class CloneRepositoryQuants : ICommand ]; private static readonly string[] CloneBenchmarkDomains = ["general"]; + private const string CloneTensorPolicyPropertyName = "cloneTensorPolicy"; private static readonly JsonSerializerOptions JsonOptions = new() { @@ -111,6 +113,8 @@ public async Task Run(List args) Cache.ModelMagicQuantDirectory!, CancellationToken.None); + var sourceClonePolicies = LoadCloneArtifactPolicies(manifestLocalPath); + var benchmarkService = new BenchmarkService(pyManager); var quantizationService = new QuantizationService(benchmarkService); var imatrixService = new ImatrixService(); @@ -173,6 +177,8 @@ public async Task Run(List args) await File.WriteAllTextAsync(outputCloneManifestPath, JsonSerializer.Serialize(manifest, JsonOptions)); archivedManifestFiles.Add(MagicQuantManifestPathService.CloneConfigsFileName); + var cloneBuildResults = new Dictionary(StringComparer.OrdinalIgnoreCase); + var records = canReuseEverything ? reusableRecords : new List(); @@ -234,6 +240,15 @@ await EnsureCloneNativeBenchmarkArtifactsReadyAsync( ? artifact.QuantFamily : artifact.BaseQuant; + var sourcePolicy = sourceClonePolicies.GetValueOrDefault(artifact.FileName); + string? artifactMissingManifestBaseQuantName = hasMissingManifestBaseQuantOverride + ? missingManifestBaseQuantName + : sourcePolicy?.MissingManifestBaseQuantName; + bool artifactAllowMissingManifestTensors = allowMissingManifestTensors || + hasMissingManifestBaseQuantOverride || + sourcePolicy?.AllowMissingManifestTensors == true || + !string.IsNullOrWhiteSpace(artifactMissingManifestBaseQuantName); + AnsiConsole.Write(new Rule($"[yellow]Clone Artifact: {Markup.Escape(artifact.FileName)}[/]") { Justification = Justify.Left }); if (TryReuseExistingCloneArtifactAndBenchmark(outputFile, artifact, benchmarkCache, out var cachedRecord)) @@ -250,13 +265,15 @@ await EnsureCloneNativeBenchmarkArtifactsReadyAsync( } else { - await cloneBuildService.BuildAsync( + var cloneBuildResult = await cloneBuildService.BuildAsync( tensorTypes: artifact.TensorTypes, outputPath: outputFile, baseQuantName: baseQuantName, - allowMissingManifestTensors: allowMissingManifestTensors, - missingManifestBaseQuantName: missingManifestBaseQuantName, + allowMissingManifestTensors: artifactAllowMissingManifestTensors, + missingManifestBaseQuantName: artifactMissingManifestBaseQuantName, forceRebuild: true); + + cloneBuildResults[artifact.FileName] = cloneBuildResult; } var benchmarkBaseline = ResolveCloneBenchmarkBaseline(baseQuantName, artifact.QuantFamily); @@ -291,6 +308,13 @@ await cloneBuildService.BuildAsync( await sidecarService.CopyMmprojArtifactsAsync(Cache.OutputDirectory!); await WriteCloneBenchmarkSummaryAsync(Cache.OutputDirectory!, records); + await WriteResolvedCloneConfigManifestAsync( + outputCloneManifestPath, + records, + sourceClonePolicies, + cloneBuildResults, + missingManifestBaseQuantName, + hasMissingManifestBaseQuantOverride); archivedManifestFiles.Add(MagicQuantManifestPathService.CloneBenchmarksFileName); await new CloneReadmeGenerationService().GenerateAsync( @@ -665,6 +689,155 @@ private static Dictionary LoadReusableCloneBench } } + private static Dictionary LoadCloneArtifactPolicies(string manifestPath) + { + var policies = new Dictionary(StringComparer.OrdinalIgnoreCase); + + if (string.IsNullOrWhiteSpace(manifestPath) || !File.Exists(manifestPath)) + return policies; + + try + { + var root = JsonNode.Parse(File.ReadAllText(manifestPath)) as JsonObject; + var artifacts = TryGetProperty(root, "artifacts") as JsonArray; + if (artifacts == null) + return policies; + + foreach (var node in artifacts.OfType()) + { + string? fileName = TryGetString(node, "fileName"); + if (string.IsNullOrWhiteSpace(fileName)) + continue; + + var policyNode = TryGetProperty(node, CloneTensorPolicyPropertyName) as JsonObject; + if (policyNode == null) + continue; + + policies[fileName] = new CloneArtifactPolicy( + AllowMissingManifestTensors: TryGetBool(policyNode, "allowMissingManifestTensors"), + MissingManifestBaseQuantName: TryGetString(policyNode, "missingManifestBaseQuant")); + } + } + catch (Exception ex) + { + AnsiConsole.MarkupLine($"[yellow]Clone tensor policy metadata could not be read from source manifest:[/] {Markup.Escape(ex.Message)}"); + } + + return policies; + } + + private static async Task WriteResolvedCloneConfigManifestAsync( + string outputCloneManifestPath, + IReadOnlyCollection records, + IReadOnlyDictionary sourcePolicies, + IReadOnlyDictionary cloneBuildResults, + string? cliMissingManifestBaseQuantName, + bool hasCliMissingManifestBaseQuantOverride) + { + if (!File.Exists(outputCloneManifestPath)) + return; + + var root = JsonNode.Parse(await File.ReadAllTextAsync(outputCloneManifestPath)) as JsonObject; + var artifacts = TryGetProperty(root, "artifacts") as JsonArray; + if (root == null || artifacts == null) + return; + + int policiesWritten = 0; + var recordByFileName = records + .Where(x => !string.IsNullOrWhiteSpace(x.ManifestArtifact.FileName)) + .GroupBy(x => x.ManifestArtifact.FileName, StringComparer.OrdinalIgnoreCase) + .ToDictionary(g => g.Key, g => g.First(), StringComparer.OrdinalIgnoreCase); + + foreach (var artifactNode in artifacts.OfType()) + { + string? fileName = TryGetString(artifactNode, "fileName"); + if (string.IsNullOrWhiteSpace(fileName)) + continue; + + recordByFileName.TryGetValue(fileName, out var record); + cloneBuildResults.TryGetValue(fileName, out var buildResult); + sourcePolicies.TryGetValue(fileName, out var sourcePolicy); + + bool allowMissing = buildResult?.UsedManifestSubset == true || + sourcePolicy?.AllowMissingManifestTensors == true || + hasCliMissingManifestBaseQuantOverride; + string? missingBaseQuant = buildResult?.UsedManifestSubset == true + ? buildResult.EffectiveBaseQuantName + : hasCliMissingManifestBaseQuantOverride + ? cliMissingManifestBaseQuantName + : sourcePolicy?.MissingManifestBaseQuantName; + + if (!allowMissing && string.IsNullOrWhiteSpace(missingBaseQuant)) + { + artifactNode.Remove(CloneTensorPolicyPropertyName); + continue; + } + + var policyNode = new JsonObject + { + ["allowMissingManifestTensors"] = allowMissing, + ["missingManifestBaseQuant"] = string.IsNullOrWhiteSpace(missingBaseQuant) ? null : missingBaseQuant, + ["generatedAtUtc"] = DateTimeOffset.UtcNow.ToString("O") + }; + + if (record?.ActualSizeBytes > 0) + policyNode["actualSizeBytes"] = (long)Math.Min(record.ActualSizeBytes, long.MaxValue); + + if (buildResult?.UsedManifestSubset == true) + { + policyNode["missingManifestTensorCount"] = buildResult.MissingInManifest.Count; + policyNode["missingManifestTensors"] = new JsonArray(buildResult.MissingInManifest.Select(x => JsonValue.Create(x)).ToArray()); + } + else if (sourcePolicy?.AllowMissingManifestTensors == true || !string.IsNullOrWhiteSpace(sourcePolicy?.MissingManifestBaseQuantName)) + { + policyNode["inheritedFromSourceManifest"] = true; + } + else if (hasCliMissingManifestBaseQuantOverride) + { + policyNode["createdFromCliOverride"] = true; + } + + artifactNode[CloneTensorPolicyPropertyName] = policyNode; + policiesWritten++; + } + + await File.WriteAllTextAsync(outputCloneManifestPath, root.ToJsonString(JsonOptions)); + AnsiConsole.MarkupLine($"[green]Resolved clone config manifest updated:[/] {Markup.Escape(outputCloneManifestPath)} [grey](policies={policiesWritten:N0})[/]"); + } + + private static JsonNode? TryGetProperty(JsonObject? obj, string name) + { + if (obj == null) + return null; + + foreach (var kv in obj) + { + if (string.Equals(kv.Key, name, StringComparison.OrdinalIgnoreCase)) + return kv.Value; + } + + return null; + } + + private static string? TryGetString(JsonObject obj, string name) + => TryGetProperty(obj, name)?.GetValue(); + + private static bool TryGetBool(JsonObject obj, string name) + { + var node = TryGetProperty(obj, name); + if (node == null) + return false; + + try + { + return node.GetValue(); + } + catch + { + return bool.TryParse(node.ToString(), out var value) && value; + } + } + private static async Task> CopySourceManifestFilesAsync( string outputDirectory, string sourceManifestLocalPath, @@ -890,6 +1063,10 @@ private sealed record CloneNativeBenchmarkEnvironmentStatus( bool IsValid, IReadOnlyList MissingOrInvalidArtifacts); + private sealed record CloneArtifactPolicy( + bool AllowMissingManifestTensors, + string? MissingManifestBaseQuantName); + private sealed class CloneBenchmarkCacheRow { public string FileName { get; set; } = string.Empty; From abfd3fa8d282ec22eccf5144c3530cbc1f59c954 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 25 May 2026 14:14:42 -0400 Subject: [PATCH 216/258] Capture resolved clone tensor map from GGUF output --- .../CloneManifestTensorMapBuildService.cs | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs index daab5e2..42826d2 100644 --- a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs +++ b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs @@ -13,7 +13,8 @@ public sealed record CloneManifestTensorMapBuildResult( string OutputPath, bool UsedManifestSubset, string EffectiveBaseQuantName, - IReadOnlyList MissingInManifest); + IReadOnlyList MissingInManifest, + IReadOnlyDictionary ResolvedTensorTypes); /// /// Clone-mode exact tensor-map builder. @@ -23,6 +24,11 @@ public sealed record CloneManifestTensorMapBuildResult( /// manifest tensors receive explicit --tensor-type overrides; the extra source tensors are /// intentionally left to llama.cpp's normal base-quant behavior unless a clone-specific /// missing-manifest base-quant override is provided. +/// +/// After a clone artifact is produced, the service re-reads the output GGUF and returns the +/// actual emitted tensor qtypes. That lets the generated clone manifest become the next run's +/// exact recipe without inventing missing tensor overrides before llama.cpp has decided how +/// to store norms and other special tensors. /// public sealed class CloneManifestTensorMapBuildService { @@ -87,11 +93,14 @@ public async Task BuildAsync( forceRebuild: forceRebuild, ct: ct); + var resolvedTensorTypes = await CaptureResolvedTensorTypesAsync(exactOutputPath, tensorTypes, ct); + return new CloneManifestTensorMapBuildResult( OutputPath: exactOutputPath, UsedManifestSubset: false, EffectiveBaseQuantName: baseQuantName, - MissingInManifest: Array.Empty()); + MissingInManifest: Array.Empty(), + ResolvedTensorTypes: resolvedTensorTypes); } bool sourceModelIsManifestSuperset = missingInManifest.Count > 0 && unexpectedInManifest.Count == 0; @@ -133,11 +142,14 @@ private async Task BuildSubsetOverrideCloneAs if (!forceRebuild && File.Exists(outputFile) && new FileInfo(outputFile).Length > 0) { + var reusedResolvedTensorTypes = await CaptureResolvedTensorTypesAsync(outputFile, tensorTypes, ct); + return new CloneManifestTensorMapBuildResult( OutputPath: outputFile, UsedManifestSubset: true, EffectiveBaseQuantName: baseQuant.Names[0], - MissingInManifest: missingInManifest.ToArray()); + MissingInManifest: missingInManifest.ToArray(), + ResolvedTensorTypes: reusedResolvedTensorTypes); } if (forceRebuild) @@ -217,11 +229,40 @@ private async Task BuildSubsetOverrideCloneAs await File.WriteAllTextAsync(outputFile + ".success.json", "{\"status\":\"success\"}", ct); AnsiConsole.MarkupLine($"[green]Clone quantized model ready:[/] {Markup.Escape(outputFile)}"); + var resolvedTensorTypes = await CaptureResolvedTensorTypesAsync(outputFile, tensorTypes, ct); + return new CloneManifestTensorMapBuildResult( OutputPath: outputFile, UsedManifestSubset: true, EffectiveBaseQuantName: baseQuant.Names[0], - MissingInManifest: missingInManifest.ToArray()); + MissingInManifest: missingInManifest.ToArray(), + ResolvedTensorTypes: resolvedTensorTypes); + } + + private async Task> CaptureResolvedTensorTypesAsync( + string outputFile, + IReadOnlyDictionary fallbackTensorTypes, + CancellationToken ct) + { + try + { + var resolved = await _quantizationService.ReadExactTensorTypesAsync(outputFile, ct); + if (resolved.Count > 0) + { + AnsiConsole.MarkupLine($"[green]Captured resolved clone tensor map from GGUF:[/] {resolved.Count:N0} tensor(s)"); + return resolved + .OrderBy(x => x.Key, StringComparer.Ordinal) + .ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal); + } + } + catch (Exception ex) + { + AnsiConsole.MarkupLine($"[yellow]Could not capture resolved tensor map from clone GGUF; preserving manifest tensor map:[/] {Markup.Escape(ex.Message)}"); + } + + return fallbackTensorTypes + .OrderBy(x => x.Key, StringComparer.Ordinal) + .ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal); } private static BaselineQuants ResolveCloneBaseQuantOrThrow(string baseQuantName, string? missingManifestBaseQuantName) From 8871928c4791c7c23ddb8ba1a143bb212f39b98f Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Mon, 25 May 2026 15:04:23 -0400 Subject: [PATCH 217/258] Persist resolved tensor types after clone build --- .../CloneManifestTensorMapBuildService.cs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs index 42826d2..223f581 100644 --- a/MagicQuant/Services/CloneManifestTensorMapBuildService.cs +++ b/MagicQuant/Services/CloneManifestTensorMapBuildService.cs @@ -1,6 +1,8 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; using MagicQuant.Helpers; using MagicQuant.Models; using MQ.DB; @@ -37,6 +39,14 @@ public sealed class CloneManifestTensorMapBuildService public const string MissingManifestBaseQuantFlag = "missing-manifest-base-quant"; public const string MissingManifestBaseQuantCliSwitch = "--" + MissingManifestBaseQuantFlag; + private static readonly JsonSerializerOptions ManifestJsonOptions = new() + { + WriteIndented = true, + PropertyNameCaseInsensitive = true, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true + }; + private readonly QuantizationService _quantizationService; private readonly ImatrixService _imatrixService; @@ -94,6 +104,7 @@ public async Task BuildAsync( ct: ct); var resolvedTensorTypes = await CaptureResolvedTensorTypesAsync(exactOutputPath, tensorTypes, ct); + await PersistResolvedTensorTypesToOutputManifestAsync(exactOutputPath, resolvedTensorTypes, ct); return new CloneManifestTensorMapBuildResult( OutputPath: exactOutputPath, @@ -143,6 +154,7 @@ private async Task BuildSubsetOverrideCloneAs if (!forceRebuild && File.Exists(outputFile) && new FileInfo(outputFile).Length > 0) { var reusedResolvedTensorTypes = await CaptureResolvedTensorTypesAsync(outputFile, tensorTypes, ct); + await PersistResolvedTensorTypesToOutputManifestAsync(outputFile, reusedResolvedTensorTypes, ct); return new CloneManifestTensorMapBuildResult( OutputPath: outputFile, @@ -230,6 +242,7 @@ private async Task BuildSubsetOverrideCloneAs AnsiConsole.MarkupLine($"[green]Clone quantized model ready:[/] {Markup.Escape(outputFile)}"); var resolvedTensorTypes = await CaptureResolvedTensorTypesAsync(outputFile, tensorTypes, ct); + await PersistResolvedTensorTypesToOutputManifestAsync(outputFile, resolvedTensorTypes, ct); return new CloneManifestTensorMapBuildResult( OutputPath: outputFile, @@ -265,6 +278,80 @@ private async Task> CaptureResolvedTensorTyp .ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal); } + private static async Task PersistResolvedTensorTypesToOutputManifestAsync( + string outputFile, + IReadOnlyDictionary resolvedTensorTypes, + CancellationToken ct) + { + if (resolvedTensorTypes.Count == 0) + return; + + string? outputDirectory = Path.GetDirectoryName(outputFile); + if (string.IsNullOrWhiteSpace(outputDirectory)) + return; + + string manifestPath = MagicQuantManifestPathService.GetManifestFilePath(outputDirectory, MagicQuantManifestPathService.CloneConfigsFileName); + if (!File.Exists(manifestPath)) + return; + + try + { + var root = JsonNode.Parse(await File.ReadAllTextAsync(manifestPath, ct)) as JsonObject; + var artifacts = TryGetProperty(root, "artifacts") as JsonArray; + if (root == null || artifacts == null) + return; + + string fileName = Path.GetFileName(outputFile); + JsonObject? matchingArtifact = null; + foreach (var node in artifacts.OfType()) + { + string? artifactFileName = TryGetString(node, "fileName"); + if (string.Equals(artifactFileName, fileName, StringComparison.OrdinalIgnoreCase)) + { + matchingArtifact = node; + break; + } + } + + if (matchingArtifact == null) + return; + + var tensorTypesNode = new JsonObject(); + foreach (var kv in resolvedTensorTypes.OrderBy(x => x.Key, StringComparer.Ordinal)) + tensorTypesNode[kv.Key] = kv.Value; + + matchingArtifact["tensorTypes"] = tensorTypesNode; + matchingArtifact["resolvedTensorTypeCount"] = resolvedTensorTypes.Count; + matchingArtifact["resolvedTensorTypesGeneratedAtUtc"] = DateTimeOffset.UtcNow.ToString("O"); + + await File.WriteAllTextAsync(manifestPath, root.ToJsonString(ManifestJsonOptions), ct); + AnsiConsole.MarkupLine( + $"[green]Resolved tensorTypes persisted to clone manifest:[/] {Markup.Escape(fileName)} [grey]({resolvedTensorTypes.Count:N0} tensor(s))[/]"); + } + catch (Exception ex) + { + AnsiConsole.MarkupLine( + $"[yellow]Could not persist resolved tensorTypes to clone manifest; build output is still valid:[/] {Markup.Escape(ex.Message)}"); + } + } + + private static JsonNode? TryGetProperty(JsonObject? obj, string name) + { + if (obj == null) + return null; + + foreach (var kv in obj) + { + if (string.Equals(kv.Key, name, StringComparison.OrdinalIgnoreCase)) + return kv.Value; + } + + return null; + } + + private static string? TryGetString(JsonObject obj, string name) + => TryGetProperty(obj, name)?.GetValue(); + private static BaselineQuants ResolveCloneBaseQuantOrThrow(string baseQuantName, string? missingManifestBaseQuantName) { bool hasOverride = !string.IsNullOrWhiteSpace(missingManifestBaseQuantName); From a2e069e2d5a3b4ae3ebdfd0c0caa2e66e4b8b6a1 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 3 Jun 2026 12:26:29 -0400 Subject: [PATCH 218/258] updates --- MagicQuant/Program.cs | 9 +++++---- MagicQuant/config.dev.yaml | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 0c32126..0979d00 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -11,17 +11,18 @@ if (args.Length == 0) { // Use: "clone" or "evolution" - const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. + const string debugMode = "clone"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) { args = [ "clone-repository-quants", - "--architecture-family", @"""Qwen3.6-35B-A3B""", - "--source-repo", @"""magiccodingman/Qwen3.6-35B-A3B-MagicQuant-GGUF""" + "--architecture-family", @"""Qwen3.6-27B-Uncensored""", + "--source-repo", @"""magiccodingman/Qwen3.6-27B-MagicQuant-GGUF""" ,"--allow-architecture-family-alias-override" - //, "--reuse-existing-final-artifacts" + ,"--missing-manifest-base-quant Q8_0" + , "--reuse-existing-final-artifacts" ]; } else diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index dc43967..f9bf4d8 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ + model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-uncensored-heretic-v2-Native-MTP-Preserved-llmfan46/ llama_root: llama_bin: convert_script: @@ -67,7 +67,7 @@ readme: # Optional title model name override used in: # # MagicQuant Hybrids (v2.0) - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.6-27B + title_model_name_override: Qwen3.6-27B Uncensored (By llmfan46) # Hugging Face README frontmatter. # Scalars render as: @@ -85,8 +85,9 @@ readme: - text-generation - magicquant - conversational + - mtp base_model: - - Qwen/Qwen3.6-27B + - llmfan46/Qwen3.6-27B-uncensored-heretic-v2-Native-MTP-Preserved hardware: gpu_memory_limits_gb: From bbfa0cbc744624a862211b2b0d79b87bfe25a9aa Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 19 Jun 2026 15:57:57 -0400 Subject: [PATCH 219/258] Fixed output issue for mmproj file check where it could get stuck --- .../Services/ModelSidecarArtifactService.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/MagicQuant/Services/ModelSidecarArtifactService.cs b/MagicQuant/Services/ModelSidecarArtifactService.cs index 8e79d73..6028820 100644 --- a/MagicQuant/Services/ModelSidecarArtifactService.cs +++ b/MagicQuant/Services/ModelSidecarArtifactService.cs @@ -233,10 +233,18 @@ private async Task BuildMmprojArtifactAsync(List w psi.ArgumentList.Add("--outfile"); psi.ArgumentList.Add(targetPath); - using var proc = Process.Start(psi) ?? throw new InvalidOperationException("Failed to start mmproj conversion process."); - string stdout = await proc.StandardOutput.ReadToEndAsync(); - string stderr = await proc.StandardError.ReadToEndAsync(); - await proc.WaitForExitAsync(ct); + using var proc = Process.Start(psi) + ?? throw new InvalidOperationException("Failed to start mmproj conversion process."); + + Task stdoutTask = proc.StandardOutput.ReadToEndAsync(ct); + Task stderrTask = proc.StandardError.ReadToEndAsync(ct); + Task waitTask = proc.WaitForExitAsync(ct); + + await Task.WhenAll(stdoutTask, stderrTask, waitTask); + + string stdout = await stdoutTask; + string stderr = await stderrTask; + await File.WriteAllTextAsync(logPath, stdout + Environment.NewLine + stderr, ct); if (proc.ExitCode != 0 || !File.Exists(targetPath) || new FileInfo(targetPath).Length == 0) From df634d36545091915cbf6bef782d4a904a3e2008 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Sat, 15 Aug 2026 12:51:22 -0400 Subject: [PATCH 220/258] updated for qwen3.8 --- MQ.DB/tensor_groups.yaml | 13 ++ MagicQuant.Tests/BenchmarkCorpusTests.cs | 33 +++++ MagicQuant/Program.cs | 5 +- MagicQuant/Services/BenchmarkService.cs | 86 ++++++++++-- MagicQuant/config.dev.yaml | 172 +++++++++++++++++------ 5 files changed, 250 insertions(+), 59 deletions(-) create mode 100644 MagicQuant.Tests/BenchmarkCorpusTests.cs diff --git a/MQ.DB/tensor_groups.yaml b/MQ.DB/tensor_groups.yaml index e587fd5..0388c0d 100644 --- a/MQ.DB/tensor_groups.yaml +++ b/MQ.DB/tensor_groups.yaml @@ -366,6 +366,19 @@ base_quant_exceptions: # allow it to fallback rather than being misclassified. - "^blk\\..*\\.attn_gate\\.weight$" + # Gemma 4 / AltUp-style architecture-helper tensors. + # These are not attention projections, dense FFN matrices, MoE routers, or + # MoE expert payloads. Keep them in BaseQuant unless MagicQuant later gains + # explicit architecture-aware handling for this Gemma helper path. + # Important: do NOT add broad patterns such as .*gate\.weight$ or + # .*proj\.weight$ to semantic groups; those would collide with dense MLP, + # attention, MoE router, and miscellaneous projection tensors. + - "^blk\\..*\\.inp_gate\\.weight$" + - "^blk\\..*\\.proj\\.weight$" + - "^per_layer_model_proj\\.weight$" + - "^per_layer_token_embd\\.weight$" + - "^rope_freqs\\.weight$" + # Qwen3.5 / Qwen3.6 MTP / NEXTN speculative decoding tensors. # Keep these protected. They should not be assigned to embeddings, # lm_head, FFN, attention, SSM, or MoE groups. diff --git a/MagicQuant.Tests/BenchmarkCorpusTests.cs b/MagicQuant.Tests/BenchmarkCorpusTests.cs new file mode 100644 index 0000000..5ff80a2 --- /dev/null +++ b/MagicQuant.Tests/BenchmarkCorpusTests.cs @@ -0,0 +1,33 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public class BenchmarkCorpusTests +{ + [Fact] + public void DatasetIds_AreCanonicalNamespacedIds() + { + Assert.Equal("Salesforce/wikitext", BenchmarkService.GeneralPplDatasetId); + Assert.Equal("openai/gsm8k", BenchmarkService.MathPplDatasetId); + } + + [Fact] + public void IsPplCorpusUsable_RequiresTheFullCharacterTarget() + { + string path = Path.GetTempFileName(); + + try + { + File.WriteAllText(path, new string('x', 31)); + Assert.False(BenchmarkService.IsPplCorpusUsable(path, tokenTarget: 8)); + + File.AppendAllText(path, "x"); + Assert.True(BenchmarkService.IsPplCorpusUsable(path, tokenTarget: 8)); + } + finally + { + File.Delete(path); + } + } +} diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 0979d00..e10d794 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -11,7 +11,7 @@ if (args.Length == 0) { // Use: "clone" or "evolution" - const string debugMode = "clone"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. + const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) { @@ -34,8 +34,9 @@ args = [ "evolution", - "--architecture-family", @"""Qwen3.6-27B""" + "--architecture-family", @"""Qwen3.8-27B""" ,"--reuse-existing-final-artifacts" + ,"--allow-architecture-family-alias-override" ]; } } diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 242b801..9749084 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -33,7 +33,12 @@ public class BenchmarkService }; private static readonly int[] NglCandidates = { 35, 30, 24, 20, 16, 12, 8, 4 }; - private const int DynamicProbeSchemaVersion = 2; + // Version 3 invalidates plans discovered with the legacy root-level dataset IDs, + // which could silently create an empty corpus and cache an incorrect CPU fallback. + private const int DynamicProbeSchemaVersion = 3; + private const int PplCharsPerTokenEstimate = 4; + internal const string GeneralPplDatasetId = "Salesforce/wikitext"; + internal const string MathPplDatasetId = "openai/gsm8k"; // ---------------------------------------------------------------- // Static execution-plan state @@ -2403,7 +2408,10 @@ private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) private async Task PreparePplCorpusAsync(string domain, string outPath, int tokenTarget) { - if (File.Exists(outPath) && new FileInfo(outPath).Length > 0) + if (tokenTarget <= 0) + throw new ArgumentOutOfRangeException(nameof(tokenTarget), "Token target must be greater than zero."); + + if (IsPplCorpusUsable(outPath, tokenTarget)) return; AnsiConsole.MarkupLine($"[grey]Generating corpus for domain: {domain}[/]"); @@ -2414,20 +2422,23 @@ from datasets import load_dataset domain = '{domain}' out_path = r'{outPath}' -max_chars = {tokenTarget} * 4 +max_chars = {tokenTarget} * {PplCharsPerTokenEstimate} def get_sources(d): - if d == 'general': return [('wikitext', 'wikitext-103-raw-v1', 'test', 'text'), ('wikitext', 'wikitext-2-raw-v1', 'test', 'text')] + if d == 'general': return [('{GeneralPplDatasetId}', 'wikitext-103-raw-v1', 'test', 'text'), ('{GeneralPplDatasetId}', 'wikitext-2-raw-v1', 'test', 'text')] if d == 'code': return [('codeparrot/codeparrot-clean', None, 'train', 'content')] - if d == 'math': return [('gsm8k', 'main', 'test', 'question')] + if d == 'math': return [('{MathPplDatasetId}', 'main', 'test', 'question')] return [] parts = [] total = 0 +source_errors = [] for ds, conf, split, field in get_sources(domain): try: - d = load_dataset(ds, conf) if conf else load_dataset(ds) - for text in d[split][field]: + load_args = {{'split': split, 'streaming': True}} + d = load_dataset(ds, conf, **load_args) if conf else load_dataset(ds, **load_args) + for row in d: + text = row.get(field) if not text or not isinstance(text, str): continue chunk = text.strip() + '\n' @@ -2436,10 +2447,19 @@ def get_sources(d): if total >= max_chars: break except Exception as e: - print(f'Error loading {{ds}}: {{e}}') + message = f'Error loading {{ds}}: {{e}}' + source_errors.append(message) + print(message, file=sys.stderr) if total >= max_chars: break +if total < max_chars: + details = '; '.join(source_errors) or 'dataset sources returned insufficient text' + raise RuntimeError( + f'Failed to build corpus for domain {{domain!r}}: collected ' + f'{{total}} of {{max_chars}} required characters. {{details}}' + ) + with open(out_path, 'w', encoding='utf-8') as f: f.write(''.join(parts)) "; @@ -2453,14 +2473,58 @@ with open(out_path, 'w', encoding='utf-8') as f: : $"\"{scriptPath}\""; string runner = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : pythonExe; - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - args = scriptPath; await _pyManager.RunPipInstallAsync("datasets"); - await RunShellCommandAsync(runner + " " + args, null); + var generationResult = await RunShellCommandAsync(runner + " " + args, null); if (File.Exists(scriptPath)) File.Delete(scriptPath); + + if (!generationResult.Success) + { + throw new InvalidOperationException( + $"Failed to generate perplexity corpus for domain '{domain}'.\n\n{generationResult.LogOutput}"); + } + + if (!IsPplCorpusUsable(outPath, tokenTarget)) + { + throw new InvalidOperationException( + $"Generated perplexity corpus for domain '{domain}' did not contain the required " + + $"{(long)tokenTarget * PplCharsPerTokenEstimate:N0} characters: {outPath}"); + } + } + + internal static bool IsPplCorpusUsable(string path, int tokenTarget) + { + if (tokenTarget <= 0 || !File.Exists(path)) + return false; + + try + { + long minimumCharacters = (long)tokenTarget * PplCharsPerTokenEstimate; + using var reader = new StreamReader(path); + var buffer = new char[4096]; + long totalCharacters = 0; + + while (totalCharacters < minimumCharacters) + { + int read = reader.Read(buffer, 0, buffer.Length); + if (read == 0) + break; + + totalCharacters += read; + } + + return totalCharacters >= minimumCharacters; + } + catch (IOException) + { + return false; + } + catch (UnauthorizedAccessException) + { + return false; + } } // ---------------------------------------------------------------- diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index f9bf4d8..1442571 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -1,6 +1,6 @@ paths: magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-uncensored-heretic-v2-Native-MTP-Preserved-llmfan46/ + model_dir: /mnt/world8/AI/Models/Qwen3.8-27B-Qwen/ llama_root: llama_bin: convert_script: @@ -67,7 +67,7 @@ readme: # Optional title model name override used in: # # MagicQuant Hybrids (v2.0) - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.6-27B Uncensored (By llmfan46) + title_model_name_override: Qwen3.8-27B # Hugging Face README frontmatter. # Scalars render as: @@ -85,9 +85,8 @@ readme: - text-generation - magicquant - conversational - - mtp base_model: - - llmfan46/Qwen3.6-27B-uncensored-heretic-v2-Native-MTP-Preserved + - Qwen/Qwen3.8-27B hardware: gpu_memory_limits_gb: @@ -145,12 +144,9 @@ prediction: minimum_fit_rows: 12 candidate_selection: - - validate_all_anomaly_strict_candidates_after_success: false - # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits # inside this size premium and beats the real linear KLD improvement line. - near_baseline_max_size_growth_percent: 1.0 + near_baseline_max_size_growth_percent: 1.5 # Phase 3: interior windows between adjacent final anchors. # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. @@ -163,15 +159,6 @@ candidate_selection: # If the first predicted candidate fails real validation, try this many fallbacks. max_fallback_attempts_per_anchor: 5 - - # Conservative SQLite/isolation-truth fallback. This runs only after the - # normal DuckDB prediction-guided attempts fail for a strict/premium/interior - # phase window. It starts from the anchor baseline blanket and only swaps - # tensor groups using surviving isolated group candidates, plus the baseline - # itself as the blanket state. - smart_fallback_enabled: true - smart_fallback_attempts_per_failure: 3 - smart_fallback_max_higher_fidelity_steps: 2 # Strict epsilon for lower-KLD comparisons after real benchmark validation. minimum_kld_improvement_epsilon: 1.0e-9 @@ -221,13 +208,13 @@ anomaly_detection: prediction_space_violation_margin: 0.00005 # Shrink applied to prediction-space adjustment after a rule is confirmed. - anomaly_adjustment_shrink_factor: 1.00 + anomaly_adjustment_shrink_factor: 0.50 # Minimum confidence required before applying a confirmed anomaly rule. min_rule_confidence_to_apply: 0.50 # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. - max_negative_adjustment_kld: 0.00400 + max_negative_adjustment_kld: 0.00075 # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. max_positive_adjustment_kld: 0.00075 @@ -260,7 +247,7 @@ anomaly_detection: output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: - output_name_prefix: Qwen3.6-27B + output_name_prefix: Qwen3.8-27B export_external_learned_baselines: true # false = normal behavior; delete/rebuild final outputs from scratch. @@ -273,17 +260,19 @@ output: # See candidate_selection above for the active final chooser settings. identity: - architecture_family_name: Qwen3.6-27B + architecture_family_name: Qwen3.8-27B allow_architecture_family_alias_override: false baselines: - standard_baselines_mode: all - enabled_standard_learning_baselines: [] - enabled_standard_combination_carriers: [] - enabled_standard_explicit_group_candidates: [] + # Use Unsloth GGUFs for dynamic learning/search while retaining the built-in + # Q8 anchor required by the execution-plan and baseline pipeline. + standard_baselines_mode: selected + enabled_standard_learning_baselines: [Q8_0] + enabled_standard_combination_carriers: [Q8_0] + enabled_standard_explicit_group_candidates: [Q8_0] custom_repositories: - - repo_id: unsloth/Qwen3.6-27B-GGUF + - repo_id: unsloth/Qwen3.8-27B-GGUF enabled: true short_source_name: Unsloth source_kind: huggingface_gguf_repository @@ -298,73 +287,164 @@ baselines: includes: - - file_name: Qwen3.6-27B-UD-IQ2_M.gguf + # 2-bit + + - file_name: Qwen3.8-27B-UD-IQ2_XXS.gguf + baseline_family: IQ2_XXS + quantize_base_name: IQ2_XXS + display_name: Unsloth-UD-IQ2_XXS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-UD-IQ2_M.gguf baseline_family: IQ2_M quantize_base_name: IQ2_M - display_name: UD-IQ2_M + display_name: Unsloth-UD-IQ2_M force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf - baseline_family: IQ2_XXS - quantize_base_name: IQ2_XXS - display_name: UD-IQ2_XXS + - file_name: Qwen3.8-27B-UD-Q2_K_XL.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: Unsloth-UD-Q2_K_XL force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf + # 3-bit + + - file_name: Qwen3.8-27B-UD-IQ3_XXS.gguf baseline_family: IQ3_XXS quantize_base_name: IQ3_XXS - display_name: UD-IQ3_XXS + display_name: Unsloth-UD-IQ3_XXS force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: UD-Q2_K_XL + - file_name: Qwen3.8-27B-Q3_K_S.gguf + baseline_family: IQ3_S + quantize_base_name: IQ3_S + display_name: Unsloth-Q3_K_S force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf + - file_name: Qwen3.8-27B-Q3_K_M.gguf baseline_family: IQ3_M quantize_base_name: IQ3_M - display_name: UD-Q3_K_XL + display_name: Unsloth-Q3_K_M force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf + - file_name: Qwen3.8-27B-UD-Q3_K_XL.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_M + display_name: Unsloth-UD-Q3_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + # 4-bit + + - file_name: Qwen3.8-27B-IQ4_XS.gguf + baseline_family: IQ4_XS + quantize_base_name: IQ4_XS + display_name: Unsloth-IQ4_XS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q4_K_S.gguf + baseline_family: Q4_K_S + quantize_base_name: Q4_K_S + display_name: Unsloth-Q4_K_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-IQ4_NL.gguf + baseline_family: IQ4_NL + quantize_base_name: IQ4_NL + display_name: Unsloth-IQ4_NL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q4_K_M.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M - display_name: UD-Q4_K_XL + display_name: Unsloth-Q4_K_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-UD-Q4_K_XL.gguf + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M + display_name: Unsloth-UD-Q4_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + # 5-bit + + - file_name: Qwen3.8-27B-Q5_K_S.gguf + baseline_family: Q5_K_S + quantize_base_name: Q5_K_S + display_name: Unsloth-Q5_K_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q5_K_M.gguf + baseline_family: Q5_K + quantize_base_name: Q5_K + display_name: Unsloth-Q5_K_M force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf + - file_name: Qwen3.8-27B-UD-Q5_K_XL.gguf baseline_family: Q5_K quantize_base_name: Q5_K - display_name: UD-Q5_K_XL + display_name: Unsloth-UD-Q5_K_XL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + # 6-bit + + - file_name: Qwen3.8-27B-Q6_K.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: Unsloth-Q6_K force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf + - file_name: Qwen3.8-27B-UD-Q6_K_XL.gguf baseline_family: Q6_K quantize_base_name: Q6_K - display_name: UD-Q6_K_XL + display_name: Unsloth-UD-Q6_K_XL force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false From b761076751da69bae71fd8cdee14297abd2b438d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Wed, 19 Aug 2026 17:47:30 -0400 Subject: [PATCH 221/258] qwen3.8 update for v3 unsloth --- MagicQuant/Program.cs | 1 - MagicQuant/Services/QuantizationService.cs | 20 ++++- MagicQuant/config.dev.yaml | 92 +++++++++++----------- 3 files changed, 64 insertions(+), 49 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index e10d794..99da9f0 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -35,7 +35,6 @@ [ "evolution", "--architecture-family", @"""Qwen3.8-27B""" - ,"--reuse-existing-final-artifacts" ,"--allow-architecture-family-alias-override" ]; } diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index c1eee8a..d17b2f5 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -485,11 +485,11 @@ public async Task ProcessHybridQuantAsync( string baseLogitsDir = GetBaseLogitsDirectory(); DateTime startedUtc = DateTime.UtcNow; - const bool forceBaselineRelearn = false; + bool forceBaselineRelearn = ShouldForceBaselineRelearn(quant.BaseQuant); bool pureExternalBaseline = ShouldDownloadExternalBaselineInsteadOfQuantizing(quant); bool baselineLearnedTruthExists = await HasLearnedTruthForBaselineAsync(quant.BaseQuant, ct); - if (baselineLearnedTruthExists && await _benchmarker.TryReuseExistingBenchmarksAsync( + if (!forceBaselineRelearn && baselineLearnedTruthExists && await _benchmarker.TryReuseExistingBenchmarksAsync( quantConfig: quant, modelPath: string.Empty, benchDir: modelBenchDir, @@ -500,7 +500,7 @@ public async Task ProcessHybridQuantAsync( return SampleProcessState.Skipped; } - if (baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) + if (!forceBaselineRelearn && baselineLearnedTruthExists && await BenchmarkExistsAsync(quant, ct)) { AnsiConsole.MarkupLine($"[grey]Skipping already completed sample:[/] {Markup.Escape(modelName)}"); return SampleProcessState.Skipped; @@ -639,6 +639,20 @@ await PersistQuantizationRunAsync( private bool ShouldDownloadExternalBaselineInsteadOfQuantizing(HybridQuant quant) => quant.BaseQuant.IsExternalRepositoryBaseline && quant.Tensors.Count == 0; + private static bool ShouldForceBaselineRelearn(BaselineQuants baseline) + { + if (Config.Current.Learning.ForceRelearnArchitectureFamily) + return true; + + if (baseline.IsExternalRepositoryBaseline) + return Config.GetResolvedCustomBaseline(baseline.CanonicalKey)?.ForceRelearn == true; + + return (Config.Current.Learning.ForceRelearnStandardBaselines ?? []) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Select(x => BaselineQuants.ResolveBuiltInStandardBaseline(x.Trim())) + .Any(x => x?.UniqueId == baseline.UniqueId); + } + private async Task GetEffectiveInputModelPathAsync( HybridQuant quant, bool forceRefresh, diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 1442571..88d94a1 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -26,7 +26,9 @@ learning: # and execution probe cache rows scoped to the active architecture family. # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, # TensorCombo, or BaselineQuantDefinition rows. - force_relearn_architecture_family: false + # ONE-SHOT for the Unsloth Dynamic 3.0 refresh. Set back to false after the + # successful run or every later run will request the same destructive relearn. + force_relearn_architecture_family: true # Relearn built-in/standard baselines by display/canonical name for the current # architecture family and active tensor group profile. @@ -289,19 +291,19 @@ baselines: # 2-bit - - file_name: Qwen3.8-27B-UD-IQ2_XXS.gguf - baseline_family: IQ2_XXS - quantize_base_name: IQ2_XXS - display_name: Unsloth-UD-IQ2_XXS + - file_name: Qwen3.8-27B-UD-IQ2_S.gguf + baseline_family: IQ2_S + quantize_base_name: IQ2_S + display_name: Unsloth-UD-IQ2_S force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.8-27B-UD-IQ2_M.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: Unsloth-UD-IQ2_M + - file_name: Qwen3.8-27B-UD-IQ2_XXS.gguf + baseline_family: IQ2_XXS + quantize_base_name: IQ2_XXS + display_name: Unsloth-UD-IQ2_XXS force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false @@ -318,28 +320,19 @@ baselines: # 3-bit - - file_name: Qwen3.8-27B-UD-IQ3_XXS.gguf - baseline_family: IQ3_XXS - quantize_base_name: IQ3_XXS - display_name: Unsloth-UD-IQ3_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q3_K_S.gguf + - file_name: Qwen3.8-27B-UD-IQ3_S.gguf baseline_family: IQ3_S quantize_base_name: IQ3_S - display_name: Unsloth-Q3_K_S + display_name: Unsloth-UD-IQ3_S force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.8-27B-Q3_K_M.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_M - display_name: Unsloth-Q3_K_M + - file_name: Qwen3.8-27B-UD-IQ3_XXS.gguf + baseline_family: IQ3_XXS + quantize_base_name: IQ3_XXS + display_name: Unsloth-UD-IQ3_XXS force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false @@ -356,37 +349,28 @@ baselines: # 4-bit - - file_name: Qwen3.8-27B-IQ4_XS.gguf + - file_name: Qwen3.8-27B-UD-IQ4_XS.gguf baseline_family: IQ4_XS quantize_base_name: IQ4_XS - display_name: Unsloth-IQ4_XS + display_name: Unsloth-UD-IQ4_XS force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.8-27B-Q4_K_S.gguf + - file_name: Qwen3.8-27B-UD-Q4_K_S.gguf baseline_family: Q4_K_S quantize_base_name: Q4_K_S - display_name: Unsloth-Q4_K_S + display_name: Unsloth-UD-Q4_K_S force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.8-27B-IQ4_NL.gguf - baseline_family: IQ4_NL - quantize_base_name: IQ4_NL - display_name: Unsloth-IQ4_NL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q4_K_M.gguf + - file_name: Qwen3.8-27B-UD-Q4_K_M.gguf baseline_family: Q4_K_M quantize_base_name: Q4_K_M - display_name: Unsloth-Q4_K_M + display_name: Unsloth-UD-Q4_K_M force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false @@ -403,19 +387,19 @@ baselines: # 5-bit - - file_name: Qwen3.8-27B-Q5_K_S.gguf + - file_name: Qwen3.8-27B-UD-Q5_K_S.gguf baseline_family: Q5_K_S quantize_base_name: Q5_K_S - display_name: Unsloth-Q5_K_S + display_name: Unsloth-UD-Q5_K_S force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false allow_as_explicit_group_candidate: true - - file_name: Qwen3.8-27B-Q5_K_M.gguf + - file_name: Qwen3.8-27B-UD-Q5_K_M.gguf baseline_family: Q5_K quantize_base_name: Q5_K - display_name: Unsloth-Q5_K_M + display_name: Unsloth-UD-Q5_K_M force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false @@ -432,10 +416,28 @@ baselines: # 6-bit - - file_name: Qwen3.8-27B-Q6_K.gguf + - file_name: Qwen3.8-27B-UD-Q6_K.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: Unsloth-UD-Q6_K + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-UD-Q6_K_M.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: Unsloth-UD-Q6_K_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-UD-Q6_K_L.gguf baseline_family: Q6_K quantize_base_name: Q6_K - display_name: Unsloth-Q6_K + display_name: Unsloth-UD-Q6_K_L force_relearn: false allow_as_learning_baseline: true allow_as_combination_carrier: false From 3f4d1f15b4088cc28019b41e3590d24b99b9ec41 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 20 Aug 2026 19:04:27 -0400 Subject: [PATCH 222/258] lots of updates --- MQ.DB/Models/BaselineQuants.cs | 14 +- MQ.DB/Models/TensorWeightScheme.cs | 20 +++ .../BaselineCandidatePolicyTests.cs | 19 +++ .../ExternalBaselineTensorParityTests.cs | 120 ++++++++++++++ .../HuggingFaceBaselineCacheTests.cs | 71 +++++++++ .../Configuration/MagicQuantYamlConfig.cs | 4 +- .../Models/Learning/TensorLearningModels.cs | 3 +- .../Services/ExternalBaselineTensorParity.cs | 147 ++++++++++++++++++ .../Services/HuggingFaceBaselineService.cs | 106 ++++++++++--- .../Services/QuantFidelityComparerService.cs | 1 + MagicQuant/Services/QuantizationService.cs | 86 ++++++---- MagicQuant/config.dev.yaml | 26 +++- 12 files changed, 556 insertions(+), 61 deletions(-) create mode 100644 MagicQuant.Tests/ExternalBaselineTensorParityTests.cs create mode 100644 MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs create mode 100644 MagicQuant/Services/ExternalBaselineTensorParity.cs diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 33fc2ff..77d58a8 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -163,6 +163,14 @@ private static BaselineQuants Create( Create(12, true, "IQ2_XXS", "IQ2_XXS", TensorWeightScheme.IQ2_XXS, [TensorWeightScheme.IQ2_XXS], [], true, false, true, false, 2, 0); + public static readonly BaselineQuants IQ1_S = + Create(18, true, "IQ1_S", "IQ1_S", TensorWeightScheme.IQ1_S, [TensorWeightScheme.IQ1_S], [], true, + false, true, false, 1, -2); + + public static readonly BaselineQuants IQ1_M = + Create(19, true, "IQ1_M", "IQ1_M", TensorWeightScheme.IQ1_M, [TensorWeightScheme.IQ1_M], [], true, + false, true, false, 1, -1); + public static readonly BaselineQuants BF16_Hybrid = Create(201, false, "BF16", "BF16", TensorWeightScheme.BF16, [TensorWeightScheme.BF16], [], false, false, false, true, 16, int.MaxValue, false, "alias:bf16", "exact_alias", null, null, null, null); @@ -189,7 +197,9 @@ private static BaselineQuants Create( IQ2_M, IQ2_S, IQ2_XS, - IQ2_XXS + IQ2_XXS, + IQ1_S, + IQ1_M ]; private static readonly ImmutableArray ExactAliases = @@ -670,4 +680,4 @@ public static BaselineQuants FromTensorSchemeId(byte schemeId) return found; } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/TensorWeightScheme.cs b/MQ.DB/Models/TensorWeightScheme.cs index 8f23940..292e47c 100644 --- a/MQ.DB/Models/TensorWeightScheme.cs +++ b/MQ.DB/Models/TensorWeightScheme.cs @@ -187,6 +187,22 @@ public static TensorWeightScheme FromId(byte id) 32 ); + public static readonly TensorWeightScheme IQ1_S = + new( + 20, + true, + ["IQ1_S"], + 256 + ); + + public static readonly TensorWeightScheme IQ1_M = + new( + 21, + true, + ["IQ1_M"], + 256 + ); + public static readonly TensorWeightScheme Q4_K = new( 14, @@ -244,6 +260,8 @@ public static TensorWeightScheme FromId(byte id) IQ2_S, IQ2_XS, IQ2_XXS, + IQ1_S, + IQ1_M, Q4_K, Q4_K_S, Q5_K_S @@ -268,6 +286,8 @@ public static TensorWeightScheme FromId(byte id) IQ2_S, IQ2_XS, IQ2_XXS, + IQ1_S, + IQ1_M, Q4_K, Q4_K_S, Q5_K_S diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs index 4f2cc20..44ba74e 100644 --- a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -6,6 +6,25 @@ namespace MagicQuant.Tests; public class BaselineCandidatePolicyTests { + [Fact] + public void Iq1Families_AreRegisteredAsImatrixLearningAndExplicitCandidates() + { + Assert.Equal((byte)1, BaselineQuants.IQ1_S.BitRange); + Assert.Equal((byte)1, BaselineQuants.IQ1_M.BitRange); + Assert.True(BaselineQuants.IQ1_S.RequiresImatrix); + Assert.True(BaselineQuants.IQ1_M.RequiresImatrix); + Assert.True(BaselineQuants.IQ1_S.IsLearningBaseline); + Assert.True(BaselineQuants.IQ1_M.IsLearningBaseline); + Assert.True(BaselineQuants.IQ1_S.IsExplicitGroupCombinationCandidate); + Assert.True(BaselineQuants.IQ1_M.IsExplicitGroupCombinationCandidate); + Assert.False(BaselineQuants.IQ1_S.IsCombinationCarrierCandidate); + Assert.False(BaselineQuants.IQ1_M.IsCombinationCarrierCandidate); + Assert.Same(BaselineQuants.IQ1_S, BaselineQuants.ResolveBuiltInStandardBaseline("IQ1_S")); + Assert.Same(BaselineQuants.IQ1_M, BaselineQuants.ResolveBuiltInStandardBaseline("IQ1_M")); + Assert.Equal("IQ1_S", TensorWeightScheme.FromId(TensorWeightScheme.IQ1_S.UniqueId).Names[0]); + Assert.Equal("IQ1_M", TensorWeightScheme.FromId(TensorWeightScheme.IQ1_M.UniqueId).Names[0]); + } + [Fact] public void GetPureBaselineCandidates_NoImatrix_ReturnsExactlyIq4Xs() { diff --git a/MagicQuant.Tests/ExternalBaselineTensorParityTests.cs b/MagicQuant.Tests/ExternalBaselineTensorParityTests.cs new file mode 100644 index 0000000..a8f2734 --- /dev/null +++ b/MagicQuant.Tests/ExternalBaselineTensorParityTests.cs @@ -0,0 +1,120 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public class ExternalBaselineTensorParityTests +{ + [Fact] + public void ExactTensorSet_IsAccepted() + { + var native = Metadata(nextnLayers: 1, "token_embd.weight", "blk.0.attn_q.weight", "blk.64.nextn.eh_proj.weight"); + var external = Metadata(nextnLayers: 1, "token_embd.weight", "blk.0.attn_q.weight", "blk.64.nextn.eh_proj.weight"); + + var result = ExternalBaselineTensorParity.ValidateOrThrow(native, external); + + Assert.Empty(result.InheritedOptionalTensorNames); + Assert.Equal(0, result.OmittedNextnLayerCount); + } + + [Fact] + public void MetadataDeclaredTrailingMtpOmission_IsAccepted() + { + var native = Metadata( + nextnLayers: 1, + "token_embd.weight", + "blk.0.attn_q.weight", + "blk.63.ffn_down.weight", + "blk.64.attn_q.weight", + "blk.64.nextn.eh_proj.weight"); + var external = Metadata( + nextnLayers: 0, + "token_embd.weight", + "blk.0.attn_q.weight", + "blk.63.ffn_down.weight"); + + var result = ExternalBaselineTensorParity.ValidateOrThrow(native, external); + + Assert.Equal(1, result.OmittedNextnLayerCount); + Assert.Equal( + ["blk.64.attn_q.weight", "blk.64.nextn.eh_proj.weight"], + result.InheritedOptionalTensorNames); + } + + [Fact] + public void MissingModelTrunkTensor_IsRejected() + { + var native = Metadata( + nextnLayers: 1, + "token_embd.weight", + "blk.0.attn_q.weight", + "blk.64.nextn.eh_proj.weight"); + var external = Metadata(nextnLayers: 0, "token_embd.weight"); + + var ex = Assert.Throws( + () => ExternalBaselineTensorParity.ValidateOrThrow(native, external)); + + Assert.Contains("blk.0.attn_q.weight", ex.Message, StringComparison.Ordinal); + Assert.Contains("model-trunk tensors", ex.Message, StringComparison.Ordinal); + } + + [Fact] + public void MissingMtpTensorWithoutReducedMetadata_IsRejected() + { + var native = Metadata( + nextnLayers: 1, + "token_embd.weight", + "blk.64.nextn.eh_proj.weight"); + var external = Metadata(nextnLayers: 1, "token_embd.weight"); + + Assert.Throws( + () => ExternalBaselineTensorParity.ValidateOrThrow(native, external)); + } + + [Fact] + public void PartialOmittedMtpBlock_IsRejected() + { + var native = Metadata( + nextnLayers: 1, + "token_embd.weight", + "blk.64.attn_q.weight", + "blk.64.nextn.eh_proj.weight"); + var external = Metadata( + nextnLayers: 0, + "token_embd.weight", + "blk.64.attn_q.weight"); + + Assert.Throws( + () => ExternalBaselineTensorParity.ValidateOrThrow(native, external)); + } + + [Fact] + public void UnexpectedTensor_IsRejectedEvenWhenMtpIsOmitted() + { + var native = Metadata( + nextnLayers: 1, + "token_embd.weight", + "blk.64.nextn.eh_proj.weight"); + var external = Metadata( + nextnLayers: 0, + "token_embd.weight", + "unexpected.weight"); + + var ex = Assert.Throws( + () => ExternalBaselineTensorParity.ValidateOrThrow(native, external)); + + Assert.Contains("unexpected.weight", ex.Message, StringComparison.Ordinal); + } + + private static GgufTensorReadResult Metadata(int nextnLayers, params string[] tensorNames) + { + return new GgufTensorReadResult + { + Architecture = "qwen35", + BlockCount = 64 + nextnLayers, + NextnPredictLayers = nextnLayers, + TensorNames = tensorNames.ToList(), + TensorTypes = tensorNames.ToDictionary(x => x, _ => "BF16", StringComparer.Ordinal) + }; + } +} diff --git a/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs b/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs new file mode 100644 index 0000000..5544fc0 --- /dev/null +++ b/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs @@ -0,0 +1,71 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class HuggingFaceBaselineCacheTests +{ + [Fact] + public void MatchingGgufLengthAndTimestamp_IsReusable() + { + using var files = new TemporaryFiles(); + File.WriteAllBytes(files.Source, "GGUF-source-payload"u8.ToArray()); + File.Copy(files.Source, files.Destination); + File.SetLastWriteTimeUtc(files.Destination, File.GetLastWriteTimeUtc(files.Source)); + + Assert.True(HuggingFaceBaselineService.CanReuseDownloadedFile(files.Source, files.Destination)); + } + + [Fact] + public void TruncatedDestination_IsNotReusable() + { + using var files = new TemporaryFiles(); + File.WriteAllBytes(files.Source, "GGUF-complete-payload"u8.ToArray()); + File.WriteAllBytes(files.Destination, "GGUF-partial"u8.ToArray()); + File.SetLastWriteTimeUtc(files.Destination, File.GetLastWriteTimeUtc(files.Source)); + + Assert.False(HuggingFaceBaselineService.CanReuseDownloadedFile(files.Source, files.Destination)); + } + + [Fact] + public void SameSizeButDifferentTimestamp_IsNotReusable() + { + using var files = new TemporaryFiles(); + File.WriteAllBytes(files.Source, "GGUF-source-payload"u8.ToArray()); + File.Copy(files.Source, files.Destination); + File.SetLastWriteTimeUtc(files.Destination, File.GetLastWriteTimeUtc(files.Source).AddSeconds(-1)); + + Assert.False(HuggingFaceBaselineService.CanReuseDownloadedFile(files.Source, files.Destination)); + } + + [Fact] + public void InvalidGgufMagic_IsNotReusable() + { + using var files = new TemporaryFiles(); + File.WriteAllBytes(files.Source, "NOPE-source-payload"u8.ToArray()); + File.Copy(files.Source, files.Destination); + File.SetLastWriteTimeUtc(files.Destination, File.GetLastWriteTimeUtc(files.Source)); + + Assert.False(HuggingFaceBaselineService.CanReuseDownloadedFile(files.Source, files.Destination)); + } + + private sealed class TemporaryFiles : IDisposable + { + private readonly string _directory = Path.Combine( + Path.GetTempPath(), "mq-hf-cache-test-" + Guid.NewGuid().ToString("N")); + + public TemporaryFiles() + { + Directory.CreateDirectory(_directory); + } + + public string Source => Path.Combine(_directory, "source.gguf"); + public string Destination => Path.Combine(_directory, "destination.gguf"); + + public void Dispose() + { + if (Directory.Exists(_directory)) + Directory.Delete(_directory, recursive: true); + } + } +} diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index dba1fc5..f60eb60 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -47,6 +47,8 @@ public sealed class MagicQuantYamlConfig public List CollapsePenaltySchemes { get; set; } = [ + "IQ1_S", + "IQ1_M", "MXFP4", "IQ2_XXS", "IQ2_XS", @@ -470,4 +472,4 @@ public sealed class ResolvedCustomBaselineSpec public sealed class RuntimeHardwareConfig { public Dictionary GpuMemoryLimitsGb { get; set; } = new(); -} \ No newline at end of file +} diff --git a/MagicQuant/Models/Learning/TensorLearningModels.cs b/MagicQuant/Models/Learning/TensorLearningModels.cs index e120b18..4fbde50 100644 --- a/MagicQuant/Models/Learning/TensorLearningModels.cs +++ b/MagicQuant/Models/Learning/TensorLearningModels.cs @@ -38,7 +38,8 @@ public enum LearningSource LogOnly = 1, GgufOnly = 2, Both = 3, - BothWithMismatch = 4 + BothWithMismatch = 4, + InheritedFromNative = 5 } public sealed class TensorTruthMismatch diff --git a/MagicQuant/Services/ExternalBaselineTensorParity.cs b/MagicQuant/Services/ExternalBaselineTensorParity.cs new file mode 100644 index 0000000..3459cd2 --- /dev/null +++ b/MagicQuant/Services/ExternalBaselineTensorParity.cs @@ -0,0 +1,147 @@ +namespace MagicQuant.Services; + +internal sealed class GgufTensorReadResult +{ + public string? Error { get; set; } + public string? Architecture { get; set; } + public int? BlockCount { get; set; } + public int? NextnPredictLayers { get; set; } + public List TensorNames { get; set; } = new(); + public Dictionary TensorTypes { get; set; } = new(StringComparer.Ordinal); +} + +internal sealed class ExternalBaselineTensorParityResult +{ + public required GgufTensorReadResult NativeMetadata { get; init; } + public required GgufTensorReadResult ExternalMetadata { get; init; } + public IReadOnlyList InheritedOptionalTensorNames { get; init; } = []; + public int OmittedNextnLayerCount { get; init; } +} + +internal static class ExternalBaselineTensorParity +{ + public static ExternalBaselineTensorParityResult ValidateOrThrow( + GgufTensorReadResult nativeMetadata, + GgufTensorReadResult externalMetadata) + { + ArgumentNullException.ThrowIfNull(nativeMetadata); + ArgumentNullException.ThrowIfNull(externalMetadata); + + var nativeNames = nativeMetadata.TensorNames + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + var externalNames = externalMetadata.TensorNames + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + var missing = nativeNames + .Except(externalNames, StringComparer.Ordinal) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + var unexpected = externalNames + .Except(nativeNames, StringComparer.Ordinal) + .OrderBy(x => x, StringComparer.Ordinal) + .ToList(); + + if (missing.Count == 0 && unexpected.Count == 0 && nativeNames.Count == externalNames.Count) + { + return new ExternalBaselineTensorParityResult + { + NativeMetadata = nativeMetadata, + ExternalMetadata = externalMetadata + }; + } + + if (unexpected.Count == 0 && + TryValidateDeclaredOptionalNextnOmission( + nativeMetadata, + externalMetadata, + missing, + out int omittedLayerCount)) + { + return new ExternalBaselineTensorParityResult + { + NativeMetadata = nativeMetadata, + ExternalMetadata = externalMetadata, + InheritedOptionalTensorNames = missing, + OmittedNextnLayerCount = omittedLayerCount + }; + } + + throw new InvalidOperationException( + $"External/custom baseline tensor mismatch detected. " + + $"Missing=[{string.Join(", ", missing.Take(20))}] " + + $"Unexpected=[{string.Join(", ", unexpected.Take(20))}]. " + + "MagicQuant only permits missing tensors when GGUF metadata explicitly declares fewer trailing NextN/MTP layers; " + + "all model-trunk tensors must exactly match the source model."); + } + + private static bool TryValidateDeclaredOptionalNextnOmission( + GgufTensorReadResult nativeMetadata, + GgufTensorReadResult externalMetadata, + IReadOnlyCollection missing, + out int omittedLayerCount) + { + omittedLayerCount = 0; + if (missing.Count == 0 || + string.IsNullOrWhiteSpace(nativeMetadata.Architecture) || + !string.Equals(nativeMetadata.Architecture, externalMetadata.Architecture, + StringComparison.Ordinal) || + nativeMetadata.BlockCount is not > 0 || + externalMetadata.BlockCount is not > 0 || + nativeMetadata.NextnPredictLayers is not >= 0 || + externalMetadata.NextnPredictLayers is not >= 0) + { + return false; + } + + int nativeBlockCount = nativeMetadata.BlockCount.Value; + int externalBlockCount = externalMetadata.BlockCount.Value; + int nativeNextnLayers = nativeMetadata.NextnPredictLayers.Value; + int externalNextnLayers = externalMetadata.NextnPredictLayers.Value; + int nativeTrunkBlockCount = nativeBlockCount - nativeNextnLayers; + int externalTrunkBlockCount = externalBlockCount - externalNextnLayers; + int omittedNextnLayers = nativeNextnLayers - externalNextnLayers; + + // Qwen GGUF block_count includes its trailing NextN layers. Therefore 65/1 and + // 64/0 describe the same 64-block model trunk. Both the trunk size and the exact + // block-count reduction must agree with the declared NextN reduction. + if (omittedNextnLayers <= 0 || + nativeTrunkBlockCount <= 0 || + externalTrunkBlockCount != nativeTrunkBlockCount || + nativeBlockCount - externalBlockCount != omittedNextnLayers) + { + return false; + } + + int firstOmittedBlock = externalBlockCount; + int endExclusive = nativeBlockCount; + + bool IsInOmittedRange(string tensorName) => + TryGetBlockIndex(tensorName, out int blockIndex) && + blockIndex >= firstOmittedBlock && + blockIndex < endExclusive; + + // A file declaring fewer NextN layers must omit those layers completely. A partial + // block is still malformed and must not be accepted as an optional-layer omission. + if (externalMetadata.TensorNames.Any(IsInOmittedRange) || missing.Any(x => !IsInOmittedRange(x))) + return false; + + omittedLayerCount = omittedNextnLayers; + return true; + } + + private static bool TryGetBlockIndex(string tensorName, out int blockIndex) + { + blockIndex = default; + const string prefix = "blk."; + if (!tensorName.StartsWith(prefix, StringComparison.Ordinal)) + return false; + + int separator = tensorName.IndexOf('.', prefix.Length); + if (separator <= prefix.Length) + return false; + + return int.TryParse(tensorName.AsSpan(prefix.Length, separator - prefix.Length), out blockIndex); + } +} diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index 3505989..6d90b6c 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -299,21 +299,10 @@ public async Task DownloadBaselineAsync(BaselineQuants baseline, string Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); - if (File.Exists(destinationPath) && new FileInfo(destinationPath).Length == 0) - File.Delete(destinationPath); - - if (forceRedownload && File.Exists(destinationPath)) - File.Delete(destinationPath); - - if (File.Exists(destinationPath) && new FileInfo(destinationPath).Length > 0) - { - AnsiConsole.MarkupLine($"[grey]Reusing cached external baseline:[/] {Markup.Escape(destinationPath)}"); - return destinationPath; - } - string payloadPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_{Guid.NewGuid():N}.json"); string scriptPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_{Guid.NewGuid():N}.py"); string resultPath = Path.Combine(Path.GetDirectoryName(destinationPath)!, $"hf_download_result_{Guid.NewGuid():N}.json"); + string atomicStagingPath = destinationPath + $".partial.{Guid.NewGuid():N}"; try { @@ -329,7 +318,6 @@ await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new const string py = """ import json import os - import shutil import sys from huggingface_hub import hf_hub_download @@ -349,15 +337,10 @@ with open(payload_path, 'r', encoding='utf-8') as f: force_download=payload.get('force_redownload', False), ) - if os.path.abspath(downloaded) != os.path.abspath(target_path): - if os.path.exists(target_path): - os.remove(target_path) - shutil.copy2(downloaded, target_path) - result = { 'ok': True, - 'downloaded_path': target_path, - 'size_bytes': os.path.getsize(target_path) if os.path.exists(target_path) else 0, + 'downloaded_path': downloaded, + 'size_bytes': os.path.getsize(downloaded) if os.path.exists(downloaded) else 0, } except Exception as ex: result = { @@ -376,10 +359,28 @@ with open(result_path, 'w', encoding='utf-8') as f: if (!json.GetProperty("ok").GetBoolean()) throw new InvalidOperationException($"External baseline download failed: {json.GetProperty("error").GetString()}"); - if (!File.Exists(destinationPath) || new FileInfo(destinationPath).Length == 0) - throw new InvalidOperationException($"External baseline download completed but produced no file: {destinationPath}"); + string downloadedPath = json.GetProperty("downloaded_path").GetString() + ?? throw new InvalidOperationException("External baseline download did not return a source path."); + if (!File.Exists(downloadedPath) || new FileInfo(downloadedPath).Length == 0) + throw new InvalidOperationException($"External baseline download completed but produced no file: {downloadedPath}"); + if (!HasGgufMagic(downloadedPath)) + throw new InvalidOperationException($"Downloaded external baseline is not a GGUF file: {downloadedPath}"); + + bool reused = CanReuseDownloadedFile(downloadedPath, destinationPath); + if (!reused && !string.Equals( + Path.GetFullPath(downloadedPath), + Path.GetFullPath(destinationPath), + StringComparison.OrdinalIgnoreCase)) + { + await CopyDownloadedFileAtomicallyAsync(downloadedPath, atomicStagingPath, destinationPath, ct); + } + + if (!File.Exists(destinationPath) || new FileInfo(destinationPath).Length == 0 || !HasGgufMagic(destinationPath)) + throw new InvalidOperationException($"External baseline staging produced no valid GGUF file: {destinationPath}"); - AnsiConsole.MarkupLine($"[green]Downloaded external baseline:[/] {Markup.Escape(destinationPath)}"); + AnsiConsole.MarkupLine(reused + ? $"[grey]Reusing verified cached external baseline:[/] {Markup.Escape(destinationPath)}" + : $"[green]Downloaded and atomically staged external baseline:[/] {Markup.Escape(destinationPath)}"); return destinationPath; } finally @@ -387,6 +388,65 @@ with open(result_path, 'w', encoding='utf-8') as f: TryDelete(payloadPath); TryDelete(scriptPath); TryDelete(resultPath); + TryDelete(atomicStagingPath); + } + } + + internal static bool CanReuseDownloadedFile(string downloadedPath, string destinationPath) + { + if (!File.Exists(downloadedPath) || !File.Exists(destinationPath) || + !HasGgufMagic(downloadedPath) || !HasGgufMagic(destinationPath)) + { + return false; + } + + var downloaded = new FileInfo(downloadedPath); + var destination = new FileInfo(destinationPath); + return downloaded.Length > 0 && + downloaded.Length == destination.Length && + downloaded.LastWriteTimeUtc == destination.LastWriteTimeUtc; + } + + private static bool HasGgufMagic(string path) + { + try + { + Span magic = stackalloc byte[4]; + using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); + return stream.Read(magic) == magic.Length && magic.SequenceEqual("GGUF"u8); + } + catch + { + return false; + } + } + + private static async Task CopyDownloadedFileAtomicallyAsync( + string sourcePath, + string stagingPath, + string destinationPath, + CancellationToken ct) + { + try + { + await using (var source = new FileStream( + sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read, + bufferSize: 1024 * 1024, FileOptions.Asynchronous | FileOptions.SequentialScan)) + await using (var staging = new FileStream( + stagingPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, + bufferSize: 1024 * 1024, FileOptions.Asynchronous | FileOptions.SequentialScan)) + { + await source.CopyToAsync(staging, 1024 * 1024, ct); + await staging.FlushAsync(ct); + } + + File.SetLastWriteTimeUtc(stagingPath, File.GetLastWriteTimeUtc(sourcePath)); + File.Move(stagingPath, destinationPath, overwrite: true); + } + catch + { + TryDelete(stagingPath); + throw; } } diff --git a/MagicQuant/Services/QuantFidelityComparerService.cs b/MagicQuant/Services/QuantFidelityComparerService.cs index 361ed5c..5f3d76c 100644 --- a/MagicQuant/Services/QuantFidelityComparerService.cs +++ b/MagicQuant/Services/QuantFidelityComparerService.cs @@ -465,6 +465,7 @@ public int EffectiveTier(byte quantId) if (name.Contains("Q4") || name.Contains("IQ4") || baseline.BitRange == 4) return 40; if (name.Contains("Q3") || name.Contains("IQ3") || baseline.BitRange == 3) return 30; if (name.Contains("Q2") || name.Contains("IQ2") || baseline.BitRange == 2) return 20; + if (name.Contains("Q1") || name.Contains("IQ1") || baseline.BitRange == 1) return 10; return baseline.BitRange > 0 ? baseline.BitRange * 10 : -1; } diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index d17b2f5..3555a62 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -702,24 +702,15 @@ private string GetExternalBaselineCachePath(BaselineQuants baseline) return _paths.GetExternalBaselineDurablePath(baseline); } - private async Task ValidateExternalBaselineTensorParityOrThrow(string baseModelPath, string externalBaselinePath) + private async Task ValidateExternalBaselineTensorParityOrThrow( + string baseModelPath, + string externalBaselinePath) { var baseMeta = await ReadTensorMetadataFromGgufAsync(baseModelPath, Path.GetDirectoryName(externalBaselinePath)!); var externalMeta = await ReadTensorMetadataFromGgufAsync(externalBaselinePath, Path.GetDirectoryName(externalBaselinePath)!); - var baseNames = baseMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); - var externalNames = externalMeta.TensorNames.OrderBy(x => x, StringComparer.Ordinal).ToList(); - - var missing = baseNames.Except(externalNames, StringComparer.Ordinal).Take(20).ToList(); - var unexpected = externalNames.Except(baseNames, StringComparer.Ordinal).Take(20).ToList(); - - if (missing.Count > 0 || unexpected.Count > 0 || baseNames.Count != externalNames.Count) - { - throw new InvalidOperationException( - $"External/custom baseline tensor mismatch detected. Missing=[{string.Join(", ", missing)}] Unexpected=[{string.Join(", ", unexpected)}]. " + - "MagicQuant will not persist or use a custom baseline whose tensor names do not exactly match the source model."); - } + return ExternalBaselineTensorParity.ValidateOrThrow(baseMeta, externalMeta); } private async Task HasLearnedTruthForBaselineAsync(BaselineQuants baseline, CancellationToken ct = default) @@ -798,10 +789,9 @@ await RunLlamaQuantizeAsync( AnsiConsole.MarkupLine( $"[cyan]Learning external baseline truth from downloaded artifact:[/] {Markup.Escape(quant.BaseQuant.Names[0])}"); - await ValidateExternalBaselineTensorParityOrThrow(nativeBasePath, downloadedExternalBaselinePath); + var parity = await ValidateExternalBaselineTensorParityOrThrow(nativeBasePath, downloadedExternalBaselinePath); - var ggufMetadata = - await ReadTensorMetadataFromGgufAsync(downloadedExternalBaselinePath, Path.GetDirectoryName(rebuiltOutputPath)!); + var ggufMetadata = parity.ExternalMetadata; var ggufTruth = ggufMetadata.TensorTypes .ToDictionary(x => x.Key, x => NormalizeQuantName(x.Value), StringComparer.Ordinal); @@ -809,12 +799,37 @@ await RunLlamaQuantizeAsync( throw new InvalidOperationException( $"Downloaded external baseline '{quant.BaseQuant.Names[0]}' produced no readable GGUF tensor truth."); - var truth = ggufTruth + var truth = ggufTruth.ToDictionary( + x => x.Key, + x => new LearnedTensorTruth(x.Key, x.Value, LearningSource.GgufOnly), + StringComparer.Ordinal); + + foreach (string tensorName in parity.InheritedOptionalTensorNames) + { + if (!parity.NativeMetadata.TensorTypes.TryGetValue(tensorName, out string? nativeType) || + string.IsNullOrWhiteSpace(nativeType)) + { + throw new InvalidOperationException( + $"Native source did not provide tensor type metadata for omitted optional MTP tensor '{tensorName}'."); + } + + truth[tensorName] = new LearnedTensorTruth( + tensorName, + NormalizeQuantName(nativeType), + LearningSource.InheritedFromNative); + } + + if (parity.InheritedOptionalTensorNames.Count > 0) + { + AnsiConsole.MarkupLine( + $"[yellow]External baseline omits {parity.OmittedNextnLayerCount:N0} declared optional NextN/MTP layer(s) " + + $"({parity.InheritedOptionalTensorNames.Count:N0} tensors).[/] " + + "The normalized rebuild will inherit those optional tensors from the native source; model-trunk parity remains strict."); + } + + truth = truth .OrderBy(x => x.Key, StringComparer.Ordinal) - .ToDictionary( - x => x.Key, - x => new LearnedTensorTruth(x.Key, x.Value, LearningSource.GgufOnly), - StringComparer.Ordinal); + .ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal); var verification = new TensorTruthVerificationResult { @@ -870,7 +885,7 @@ await RunLlamaQuantizeAsync( DownloadedExternalModelPath = downloadedExternalBaselinePath, TruthByTensor = truth, GroupedByTensor = audit.GroupedByTensor, - AllTensorNamesInDownloadedArtifact = ggufMetadata.TensorNames, + AllTensorNamesInDownloadedArtifact = truth.Keys.ToList(), AmbiguousGroupingRows = audit.Ambiguous, UnresolvedTensorNames = audit.IllegalUnresolved.Select(x => x.TensorName).ToList(), BaseQuantExceptionRows = audit.BaseQuantExceptions, @@ -2808,7 +2823,25 @@ import gguf reader = gguf.GGUFReader(payload["gguf_path"]) tensor_names = [t.name for t in reader.tensors] tensor_types = {t.name: resolve_type_name(t) for t in reader.tensors} - result = {"TensorNames": tensor_names, "TensorTypes": tensor_types} + + def read_scalar(key): + field = reader.fields.get(key) + if field is None: + return None + value = field.contents() + return value.item() if hasattr(value, "item") else value + + architecture = read_scalar("general.architecture") + architecture_key = str(architecture) if architecture is not None else None + block_count = read_scalar(f"{architecture_key}.block_count") if architecture_key else None + nextn_layers = read_scalar(f"{architecture_key}.nextn_predict_layers") if architecture_key else None + result = { + "Architecture": architecture_key, + "BlockCount": int(block_count) if block_count is not None else None, + "NextnPredictLayers": int(nextn_layers) if nextn_layers is not None else None, + "TensorNames": tensor_names, + "TensorTypes": tensor_types + } except Exception as e: result = {"Error": str(e), "TensorNames": [], "TensorTypes": {}} @@ -3157,13 +3190,6 @@ private sealed class ConcreteTensorOverride public string GroupName { get; set; } = string.Empty; } - private sealed class GgufTensorReadResult - { - public string? Error { get; set; } - public List TensorNames { get; set; } = new(); - public Dictionary TensorTypes { get; set; } = new(StringComparer.Ordinal); - } - // ---------------------------------------------------------------- // Naming helpers // ---------------------------------------------------------------- diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 88d94a1..9b67c75 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -26,9 +26,7 @@ learning: # and execution probe cache rows scoped to the active architecture family. # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, # TensorCombo, or BaselineQuantDefinition rows. - # ONE-SHOT for the Unsloth Dynamic 3.0 refresh. Set back to false after the - # successful run or every later run will request the same destructive relearn. - force_relearn_architecture_family: true + force_relearn_architecture_family: false # Relearn built-in/standard baselines by display/canonical name for the current # architecture family and active tensor group profile. @@ -250,7 +248,7 @@ output: # Leave blank to default to /MagicQuant/Final_Outputs output_dir: output_name_prefix: Qwen3.8-27B - export_external_learned_baselines: true + export_external_learned_baselines: false # false = normal behavior; delete/rebuild final outputs from scratch. # true = preserve valid existing GGUFs and skip rebuilding them only when @@ -289,6 +287,26 @@ baselines: includes: + # 1-bit + + - file_name: Qwen3.8-27B-UD-IQ1_S.gguf + baseline_family: IQ1_S + quantize_base_name: IQ1_S + display_name: Unsloth-UD-IQ1_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-UD-IQ1_M.gguf + baseline_family: IQ1_M + quantize_base_name: IQ1_M + display_name: Unsloth-UD-IQ1_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + # 2-bit - file_name: Qwen3.8-27B-UD-IQ2_S.gguf From 562ed843663c0e8895e23f9010510f3cad633ff5 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Thu, 20 Aug 2026 19:07:05 -0400 Subject: [PATCH 223/258] Add Unsloth imatrix clone debug config --- MagicQuant/config.clone-unsloth.dev.yaml | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 MagicQuant/config.clone-unsloth.dev.yaml diff --git a/MagicQuant/config.clone-unsloth.dev.yaml b/MagicQuant/config.clone-unsloth.dev.yaml new file mode 100644 index 0000000..f31fbad --- /dev/null +++ b/MagicQuant/config.clone-unsloth.dev.yaml @@ -0,0 +1,35 @@ +paths: + model_dir: /mnt/world8/AI/Models/Qwen3.8-27B-Qwen/ + scratch_roots: + - /mnt/world8/ + - /home/slurp/ + - /mnt/world7/ + +flags: + use_imatrix: true + force_imatrix_rebuild: false + force_refresh_hardware_probe: false + allow_high_precision_hybrids: false + +hardware: + gpu_memory_limits_gb: + 0: 19 + 1: 23 + +# The actual Unsloth imatrix URL is supplied by the DEBUG clone args in Program.cs. +# Keep the other source modes empty so ImatrixService sees exactly one active source. +imatrix: + imatrix_url: + dataset_repo: + dataset_split: text + dataset_config: + dataset_local_file: + +output: + output_dir: /mnt/world8/AI/Models/Qwen3.8-27B-MagicQuant-Unsloth/ + output_name_prefix: Qwen3.8-27B + reuse_existing_final_artifacts: false + +identity: + architecture_family_name: Qwen3.8-27B + allow_architecture_family_alias_override: false From ca62daba81da59cb71bd7e583c9686a672b366ef Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Thu, 20 Aug 2026 19:07:13 -0400 Subject: [PATCH 224/258] Copy Unsloth clone config to debug output --- MagicQuant/MagicQuant.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index c3f1828..3138394 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -26,6 +26,9 @@ Always + + Always + Always From ee945e6a62b5e9ca19a04ec48b6218ed454b7b51 Mon Sep 17 00:00:00 2001 From: Magic <131926685+magiccodingman@users.noreply.github.com> Date: Thu, 20 Aug 2026 19:07:26 -0400 Subject: [PATCH 225/258] Switch debug harness to Qwen3.8 Unsloth imatrix clone --- MagicQuant/Program.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 99da9f0..09f7f6a 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -11,18 +11,23 @@ if (args.Length == 0) { // Use: "clone" or "evolution" - const string debugMode = "evolution"; // switch to "evolution" to use the full learning/search pipeline again. Or use "Clone" for cloning mode. + const string debugMode = "clone"; // switch to "evolution" to use the full learning/search pipeline again. if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) { args = [ "clone-repository-quants", - "--architecture-family", @"""Qwen3.6-27B-Uncensored""", - "--source-repo", @"""magiccodingman/Qwen3.6-27B-MagicQuant-GGUF""" - ,"--allow-architecture-family-alias-override" - ,"--missing-manifest-base-quant Q8_0" - , "--reuse-existing-final-artifacts" + "--config", Path.Combine(AppContext.BaseDirectory, "config.clone-unsloth.dev.yaml"), + "--architecture-family", @"""Qwen3.8-27B""", + "--source-repo", @"""magiccodingman/Qwen3.8-27B-MagicQuant-GGUF""", + "--model-dir", @"""/mnt/world8/AI/Models/Qwen3.8-27B-Qwen/""", + "--output-dir", @"""/mnt/world8/AI/Models/Qwen3.8-27B-MagicQuant-Unsloth/""", + "--use-imatrix", + "--imatrix-url", @"""https://huggingface.co/unsloth/Qwen3.8-27B-GGUF/resolve/main/imatrix_unsloth.dat?download=true""", + "--allow-architecture-family-alias-override", + "--missing-manifest-base-quant", "Q8_0", + "--reuse-existing-final-artifacts" ]; } else @@ -34,8 +39,8 @@ args = [ "evolution", - "--architecture-family", @"""Qwen3.8-27B""" - ,"--allow-architecture-family-alias-override" + "--architecture-family", @"""Qwen3.8-27B""", + "--allow-architecture-family-alias-override" ]; } } From ee605d13c1669ae828ec9f18670643bec5b7ab71 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 20 Aug 2026 19:15:30 -0400 Subject: [PATCH 226/258] fixing caching not deleting for external baseline downloads --- ...xternalBaselineCacheCleanupServiceTests.cs | 64 +++ .../HuggingFaceBaselineCacheTests.cs | 12 + MagicQuant/Commands/CloneRepositoryQuants.cs | 1 + MagicQuant/Commands/Evolution.cs | 5 +- MagicQuant/Commands/ValidatePredictions.cs | 1 + .../ExternalBaselineCacheCleanupService.cs | 58 +++ .../Services/HuggingFaceBaselineService.cs | 481 +++++++++--------- MagicQuant/Services/QuantizationService.cs | 407 ++++++++------- 8 files changed, 608 insertions(+), 421 deletions(-) create mode 100644 MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs create mode 100644 MagicQuant/Services/ExternalBaselineCacheCleanupService.cs diff --git a/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs b/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs new file mode 100644 index 0000000..67598a0 --- /dev/null +++ b/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs @@ -0,0 +1,64 @@ +using MagicQuant.Services; +using MQ.DB; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class ExternalBaselineCacheCleanupServiceTests +{ + [Fact] + public async Task CleanupStaleArtifactsAsync_HardDeletesCacheTree() + { + string temp = Path.Combine(Path.GetTempPath(), "mq-external-cache-test-" + Guid.NewGuid().ToString("N")); + string modelRoot = Path.Combine(temp, "MagicQuant"); + string cacheRoot = Path.Combine(modelRoot, "ExternalBaselines"); + + Directory.CreateDirectory(Path.Combine(cacheRoot, ".cache", "huggingface")); + await File.WriteAllTextAsync(Path.Combine(cacheRoot, "baseline.gguf"), "GGUF"); + await File.WriteAllTextAsync(Path.Combine(cacheRoot, ".cache", "huggingface", "metadata"), "x"); + + string? priorModelMagicQuantDirectory = Cache.ModelMagicQuantDirectory; + string? priorExternalBaselineCacheDirectory = Cache.ExternalBaselineCacheDirectory; + + try + { + Cache.ModelMagicQuantDirectory = modelRoot; + Cache.ExternalBaselineCacheDirectory = cacheRoot; + + bool cleaned = await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); + + Assert.True(cleaned); + Assert.False(Directory.Exists(cacheRoot)); + } + finally + { + Cache.ModelMagicQuantDirectory = priorModelMagicQuantDirectory; + Cache.ExternalBaselineCacheDirectory = priorExternalBaselineCacheDirectory; + + if (Directory.Exists(temp)) + Directory.Delete(temp, recursive: true); + } + } + + [Fact] + public void ValidateCleanupRoot_RejectsPathOutsideModelWorkDirectory() + { + string temp = Path.Combine(Path.GetTempPath(), "mq-external-cache-test-" + Guid.NewGuid().ToString("N")); + string modelRoot = Path.Combine(temp, "model", "MagicQuant"); + string outsideRoot = Path.Combine(temp, "outside"); + + var ex = Assert.Throws(() => + ExternalBaselineCacheCleanupService.ValidateCleanupRoot(outsideRoot, modelRoot)); + + Assert.Contains("unsafe external baseline cache path", ex.Message, StringComparison.OrdinalIgnoreCase); + } + + [Fact] + public void ValidateCleanupRoot_RejectsModelWorkDirectoryItself() + { + string modelRoot = Path.Combine(Path.GetTempPath(), "mq-external-cache-test-" + Guid.NewGuid().ToString("N")); + + Assert.Throws(() => + ExternalBaselineCacheCleanupService.ValidateCleanupRoot(modelRoot, modelRoot)); + } +} diff --git a/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs b/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs index 5544fc0..9c76cd2 100644 --- a/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs +++ b/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs @@ -49,6 +49,18 @@ public void InvalidGgufMagic_IsNotReusable() Assert.False(HuggingFaceBaselineService.CanReuseDownloadedFile(files.Source, files.Destination)); } + [Fact] + public void StagingCleanupPath_MustRemainInsideDestinationDirectory() + { + string root = Path.Combine(Path.GetTempPath(), "mq-hf-path-test", Guid.NewGuid().ToString("N")); + string cache = Path.Combine(root, "ExternalBaselines"); + + Assert.True(HuggingFaceBaselineService.IsPathInsideDirectory( + Path.Combine(cache, "source.gguf"), cache)); + Assert.False(HuggingFaceBaselineService.IsPathInsideDirectory( + Path.Combine(root, "outside.gguf"), cache)); + } + private sealed class TemporaryFiles : IDisposable { private readonly string _directory = Path.Combine( diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 35581de..2c72b26 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -69,6 +69,7 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index e3dd6fd..2dfa5bb 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -62,6 +62,7 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; @@ -221,7 +222,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; PrintCustomBaselineRuntimeSummary(resolvedCustomBaselines, imatrixEnsureResult.Enabled); - + // No longer needed //CliHelpers.ValidateCombinationLogicWorks(true); @@ -808,4 +809,4 @@ private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); await db.SaveChangesAsync(ct); } -} \ No newline at end of file +} diff --git a/MagicQuant/Commands/ValidatePredictions.cs b/MagicQuant/Commands/ValidatePredictions.cs index cd5f35c..b272bb2 100644 --- a/MagicQuant/Commands/ValidatePredictions.cs +++ b/MagicQuant/Commands/ValidatePredictions.cs @@ -35,6 +35,7 @@ public async Task Run(List args) Cache.ModelDirectory = modelDir; Cache.ModelMagicQuantDirectory = Path.Combine(modelDir, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); diff --git a/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs b/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs new file mode 100644 index 0000000..9f340b8 --- /dev/null +++ b/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs @@ -0,0 +1,58 @@ +using MagicQuant.Helpers; +using MQ.DB; +using Spectre.Console; + +namespace MagicQuant.Services; + +public sealed class ExternalBaselineCacheCleanupService +{ + public async Task CleanupStaleArtifactsAsync(CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory)) + return false; + + if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + throw new InvalidOperationException( + "ModelMagicQuantDirectory must be set before cleaning external baseline artifacts."); + + string cacheRoot = Path.GetFullPath(Cache.ExternalBaselineCacheDirectory); + string modelMagicQuantRoot = Path.GetFullPath(Cache.ModelMagicQuantDirectory); + ValidateCleanupRoot(cacheRoot, modelMagicQuantRoot); + + if (!Directory.Exists(cacheRoot)) + return false; + + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(cacheRoot, ct); + AnsiConsole.MarkupLine( + $"[green]Cleaned abandoned external-baseline artifacts:[/] {Markup.Escape(cacheRoot)}"); + return true; + } + + internal static void ValidateCleanupRoot(string cacheRoot, string modelMagicQuantRoot) + { + string fullCacheRoot = Path.GetFullPath(cacheRoot) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + string fullModelRoot = Path.GetFullPath(modelMagicQuantRoot) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + string relative = Path.GetRelativePath(fullModelRoot, fullCacheRoot); + + bool escapesModelRoot = Path.IsPathRooted(relative) || + relative.Equals("..", StringComparison.Ordinal) || + relative.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) || + relative.StartsWith(".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal); + + if (relative.Equals(".", StringComparison.Ordinal) || escapesModelRoot) + { + throw new InvalidOperationException( + $"Refusing to clean unsafe external baseline cache path '{fullCacheRoot}'. " + + $"It must be a child directory of '{fullModelRoot}'."); + } + + if (Directory.Exists(fullCacheRoot) && + (File.GetAttributes(fullCacheRoot) & FileAttributes.ReparsePoint) != 0) + { + throw new InvalidOperationException( + $"Refusing to recursively clean external baseline cache symlink/reparse point '{fullCacheRoot}'."); + } + } +} diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index 6d90b6c..deb38f2 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -20,272 +20,272 @@ public HuggingFaceBaselineService(PythonManager python) } -public async Task> PrecheckAndRegisterConfiguredBaselinesAsync(CancellationToken ct = default) -{ - await EnsureHubSupportAsync(); + public async Task> PrecheckAndRegisterConfiguredBaselinesAsync(CancellationToken ct = default) + { + await EnsureHubSupportAsync(); - int architectureFamilyId = Cache.CurrentArchitectureFamilyId - ?? throw new InvalidOperationException("Custom baseline sync requires the architecture family to be resolved first."); + int architectureFamilyId = Cache.CurrentArchitectureFamilyId + ?? throw new InvalidOperationException("Custom baseline sync requires the architecture family to be resolved first."); - var enabledRepos = Config.Current.Baselines.CustomRepositories.Where(x => x.Enabled).ToList(); - var resolved = new List(); - BaselineQuants.ResetDynamicCustomBaselines(); + var enabledRepos = Config.Current.Baselines.CustomRepositories.Where(x => x.Enabled).ToList(); + var resolved = new List(); + BaselineQuants.ResetDynamicCustomBaselines(); - AnsiConsole.Write(new Rule("[yellow]Custom Baseline DB Sync[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"[grey]Enabled custom repositories:[/] [cyan]{enabledRepos.Count:N0}[/]"); + AnsiConsole.Write(new Rule("[yellow]Custom Baseline DB Sync[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[grey]Enabled custom repositories:[/] [cyan]{enabledRepos.Count:N0}[/]"); - await using var db = new MagicQuantContext(); - var existingDefinitions = await db.BaselineQuantDefinitions - .Where(x => x.ArchitectureFamilyId == architectureFamilyId && x.IsCustomBaseline) - .ToListAsync(ct); + await using var db = new MagicQuantContext(); + var existingDefinitions = await db.BaselineQuantDefinitions + .Where(x => x.ArchitectureFamilyId == architectureFamilyId && x.IsCustomBaseline) + .ToListAsync(ct); - foreach (var definition in existingDefinitions) - { - definition.IsActiveInCurrentConfig = false; - definition.LastUpdatedUtc = DateTime.UtcNow; - } + foreach (var definition in existingDefinitions) + { + definition.IsActiveInCurrentConfig = false; + definition.LastUpdatedUtc = DateTime.UtcNow; + } - RegisterHistoricalDefinitions(existingDefinitions); + RegisterHistoricalDefinitions(existingDefinitions); - var existingDynamicIdsByCanonicalKey = existingDefinitions - .Where(x => !string.IsNullOrWhiteSpace(x.NormalizedCanonicalKey)) - .GroupBy(x => x.NormalizedCanonicalKey, StringComparer.Ordinal) - .ToDictionary(g => g.Key, g => g.OrderBy(x => x.RuntimeBaselineId).First().RuntimeBaselineId, StringComparer.Ordinal); + var existingDynamicIdsByCanonicalKey = existingDefinitions + .Where(x => !string.IsNullOrWhiteSpace(x.NormalizedCanonicalKey)) + .GroupBy(x => x.NormalizedCanonicalKey, StringComparer.Ordinal) + .ToDictionary(g => g.Key, g => g.OrderBy(x => x.RuntimeBaselineId).First().RuntimeBaselineId, StringComparer.Ordinal); - var reservedIds = BaselineQuants.GetAllRecognizedBaselines().Select(x => x.UniqueId).ToHashSet(); - foreach (var persistedId in existingDefinitions.Select(x => x.RuntimeBaselineId)) - reservedIds.Add(persistedId); + var reservedIds = BaselineQuants.GetAllRecognizedBaselines().Select(x => x.UniqueId).ToHashSet(); + foreach (var persistedId in existingDefinitions.Select(x => x.RuntimeBaselineId)) + reservedIds.Add(persistedId); - byte nextId = BaselineQuants.GetFirstAvailableDynamicBaselineId(); - var now = DateTime.UtcNow; + byte nextId = BaselineQuants.GetFirstAvailableDynamicBaselineId(); + var now = DateTime.UtcNow; - foreach (var repo in enabledRepos) - { - if (string.IsNullOrWhiteSpace(repo.RepoId)) - throw new InvalidOperationException("Custom baseline repository entry is missing repo_id."); + foreach (var repo in enabledRepos) + { + if (string.IsNullOrWhiteSpace(repo.RepoId)) + throw new InvalidOperationException("Custom baseline repository entry is missing repo_id."); - if (repo.Includes.Count == 0) - throw new InvalidOperationException($"Custom baseline repository '{repo.RepoId}' is enabled but has zero include entries."); + if (repo.Includes.Count == 0) + throw new InvalidOperationException($"Custom baseline repository '{repo.RepoId}' is enabled but has zero include entries."); - AnsiConsole.MarkupLine($"[cyan]Repo:[/] {Markup.Escape(repo.RepoId)} [grey](includes={repo.Includes.Count})[/]"); + AnsiConsole.MarkupLine($"[cyan]Repo:[/] {Markup.Escape(repo.RepoId)} [grey](includes={repo.Includes.Count})[/]"); - var repoFiles = await ListRepoFilesAsync(repo.RepoId, ct); - if (repoFiles.Count == 0) - throw new InvalidOperationException($"No files were returned from Hugging Face repo '{repo.RepoId}'."); + var repoFiles = await ListRepoFilesAsync(repo.RepoId, ct); + if (repoFiles.Count == 0) + throw new InvalidOperationException($"No files were returned from Hugging Face repo '{repo.RepoId}'."); - var ggufRepoFiles = repoFiles.Where(x => x.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)).ToList(); - AnsiConsole.MarkupLine($" [grey]GGUF files discovered:[/] [cyan]{ggufRepoFiles.Count:N0}[/]"); + var ggufRepoFiles = repoFiles.Where(x => x.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase)).ToList(); + AnsiConsole.MarkupLine($" [grey]GGUF files discovered:[/] [cyan]{ggufRepoFiles.Count:N0}[/]"); - string shortSourceName = string.IsNullOrWhiteSpace(repo.ShortSourceName) - ? DeriveShortSourceName(repo.RepoId) - : repo.ShortSourceName!.Trim(); + string shortSourceName = string.IsNullOrWhiteSpace(repo.ShortSourceName) + ? DeriveShortSourceName(repo.RepoId) + : repo.ShortSourceName!.Trim(); - foreach (var include in repo.Includes) - { - if (string.IsNullOrWhiteSpace(include.BaselineFamily)) - throw new InvalidOperationException($"Repo '{repo.RepoId}' has an include entry missing baseline_family."); - - var standardFamily = BaselineQuants.ResolveBuiltInStandardBaseline(include.BaselineFamily) - ?? throw new InvalidOperationException( - $"Custom baseline include '{include.BaselineFamily}' in repo '{repo.RepoId}' could not be matched to a built-in baseline family."); - - string resolvedFileName = ResolveRepoFileName(repoFiles, include, standardFamily); - string normalizedRepo = BaselineDefinitionResolver.NormalizeRepoId(repo.RepoId); - string normalizedFile = BaselineDefinitionResolver.NormalizeFileName(resolvedFileName); - string canonicalKey = BuildCanonicalKey(Cache.CurrentArchitectureFamilyName, repo.RepoId, resolvedFileName); - string normalizedCanonicalKey = BaselineDefinitionResolver.NormalizeCanonicalKey(canonicalKey); - - string displayName = string.IsNullOrWhiteSpace(include.DisplayName) - ? $"{shortSourceName}-{standardFamily.Names[0]}" - : include.DisplayName!.Trim(); - - bool requiresImatrix = include.RequiresImatrix ?? standardFamily.RequiresImatrix; - bool allowAsLearning = include.AllowAsLearningBaseline ?? repo.AllowAsLearningBaseline; - bool allowAsCarrier = include.AllowAsCombinationCarrier ?? repo.AllowAsCombinationCarrier; - bool allowAsExplicit = include.AllowAsExplicitGroupCandidate ?? repo.AllowAsExplicitGroupCandidate; - string quantizeBaseName = string.IsNullOrWhiteSpace(include.QuantizeBaseName) - ? standardFamily.Names[0] - : include.QuantizeBaseName!.Trim(); - - var bannedGroups = include.BannedGroupIds.Count > 0 - ? include.BannedGroupIds.ToArray() - : standardFamily.BannedGroupIds.ToArray(); - - var definition = existingDefinitions.FirstOrDefault(x => - string.Equals(x.NormalizedSourceRepository, normalizedRepo, StringComparison.Ordinal) && - string.Equals(x.NormalizedSourceFileName, normalizedFile, StringComparison.Ordinal)); - - if (definition != null && !string.Equals(definition.BaselineFamily, standardFamily.Names[0], StringComparison.Ordinal) && !include.ForceRelearn) + foreach (var include in repo.Includes) { - throw new InvalidOperationException( - $"Custom baseline semantic family changed for {repo.RepoId}/{resolvedFileName}: " + - $"DB has '{definition.BaselineFamily}', YAML now says '{standardFamily.Names[0]}'. " + - "This is destructive. Set this include's force_relearn: true so MagicQuant can plan and confirm targeted invalidation before resyncing the definition."); - } + if (string.IsNullOrWhiteSpace(include.BaselineFamily)) + throw new InvalidOperationException($"Repo '{repo.RepoId}' has an include entry missing baseline_family."); + + var standardFamily = BaselineQuants.ResolveBuiltInStandardBaseline(include.BaselineFamily) + ?? throw new InvalidOperationException( + $"Custom baseline include '{include.BaselineFamily}' in repo '{repo.RepoId}' could not be matched to a built-in baseline family."); + + string resolvedFileName = ResolveRepoFileName(repoFiles, include, standardFamily); + string normalizedRepo = BaselineDefinitionResolver.NormalizeRepoId(repo.RepoId); + string normalizedFile = BaselineDefinitionResolver.NormalizeFileName(resolvedFileName); + string canonicalKey = BuildCanonicalKey(Cache.CurrentArchitectureFamilyName, repo.RepoId, resolvedFileName); + string normalizedCanonicalKey = BaselineDefinitionResolver.NormalizeCanonicalKey(canonicalKey); + + string displayName = string.IsNullOrWhiteSpace(include.DisplayName) + ? $"{shortSourceName}-{standardFamily.Names[0]}" + : include.DisplayName!.Trim(); + + bool requiresImatrix = include.RequiresImatrix ?? standardFamily.RequiresImatrix; + bool allowAsLearning = include.AllowAsLearningBaseline ?? repo.AllowAsLearningBaseline; + bool allowAsCarrier = include.AllowAsCombinationCarrier ?? repo.AllowAsCombinationCarrier; + bool allowAsExplicit = include.AllowAsExplicitGroupCandidate ?? repo.AllowAsExplicitGroupCandidate; + string quantizeBaseName = string.IsNullOrWhiteSpace(include.QuantizeBaseName) + ? standardFamily.Names[0] + : include.QuantizeBaseName!.Trim(); + + var bannedGroups = include.BannedGroupIds.Count > 0 + ? include.BannedGroupIds.ToArray() + : standardFamily.BannedGroupIds.ToArray(); + + var definition = existingDefinitions.FirstOrDefault(x => + string.Equals(x.NormalizedSourceRepository, normalizedRepo, StringComparison.Ordinal) && + string.Equals(x.NormalizedSourceFileName, normalizedFile, StringComparison.Ordinal)); + + if (definition != null && !string.Equals(definition.BaselineFamily, standardFamily.Names[0], StringComparison.Ordinal) && !include.ForceRelearn) + { + throw new InvalidOperationException( + $"Custom baseline semantic family changed for {repo.RepoId}/{resolvedFileName}: " + + $"DB has '{definition.BaselineFamily}', YAML now says '{standardFamily.Names[0]}'. " + + "This is destructive. Set this include's force_relearn: true so MagicQuant can plan and confirm targeted invalidation before resyncing the definition."); + } - byte dynamicBaselineId = definition?.RuntimeBaselineId ?? ResolveDynamicBaselineId( - normalizedCanonicalKey, - existingDynamicIdsByCanonicalKey, - reservedIds, - ref nextId); + byte dynamicBaselineId = definition?.RuntimeBaselineId ?? ResolveDynamicBaselineId( + normalizedCanonicalKey, + existingDynamicIdsByCanonicalKey, + reservedIds, + ref nextId); + + var nextDefinition = new BaselineQuantDefinition + { + ArchitectureFamilyId = architectureFamilyId, + RuntimeBaselineId = dynamicBaselineId, + CanonicalKey = canonicalKey, + NormalizedCanonicalKey = normalizedCanonicalKey, + BaselineName = displayName, + DisplayName = displayName, + QuantizeBaseArgumentName = quantizeBaseName, + DefaultTensorSchemeId = standardFamily.PrimaryTensorWeightScheme.UniqueId, + DefaultTensorSchemeName = standardFamily.PrimaryTensorWeightScheme.Names[0], + SourceKind = repo.SourceKind, + SourceOwner = DeriveSourceOwner(repo.RepoId), + SourceRepository = repo.RepoId, + NormalizedSourceRepository = normalizedRepo, + SourceFileName = resolvedFileName, + NormalizedSourceFileName = normalizedFile, + ShortSourceName = shortSourceName, + BaselineFamily = standardFamily.Names[0], + IsCustomBaseline = true, + IsLearningBaseline = allowAsLearning, + IsCombinationCarrierCandidate = allowAsCarrier, + IsExplicitGroupCombinationCandidate = allowAsExplicit, + RequiresImatrix = requiresImatrix, + BitRange = standardFamily.BitRange, + ExplicitCandidateSortOrder = standardFamily.ExplicitCandidateSortOrder, + IsActiveInCurrentConfig = true, + FirstSeenUtc = definition?.FirstSeenUtc ?? now, + LastSeenUtc = now, + LastUpdatedUtc = now + }; + + if (definition == null) + { + definition = nextDefinition; + db.BaselineQuantDefinitions.Add(definition); + existingDefinitions.Add(definition); + } + else + { + MagicQuantContext.ApplyBaselineDefinitionUpdate(definition, nextDefinition, preserveFirstSeen: true); + } - var nextDefinition = new BaselineQuantDefinition - { - ArchitectureFamilyId = architectureFamilyId, - RuntimeBaselineId = dynamicBaselineId, - CanonicalKey = canonicalKey, - NormalizedCanonicalKey = normalizedCanonicalKey, - BaselineName = displayName, - DisplayName = displayName, - QuantizeBaseArgumentName = quantizeBaseName, - DefaultTensorSchemeId = standardFamily.PrimaryTensorWeightScheme.UniqueId, - DefaultTensorSchemeName = standardFamily.PrimaryTensorWeightScheme.Names[0], - SourceKind = repo.SourceKind, - SourceOwner = DeriveSourceOwner(repo.RepoId), - SourceRepository = repo.RepoId, - NormalizedSourceRepository = normalizedRepo, - SourceFileName = resolvedFileName, - NormalizedSourceFileName = normalizedFile, - ShortSourceName = shortSourceName, - BaselineFamily = standardFamily.Names[0], - IsCustomBaseline = true, - IsLearningBaseline = allowAsLearning, - IsCombinationCarrierCandidate = allowAsCarrier, - IsExplicitGroupCombinationCandidate = allowAsExplicit, - RequiresImatrix = requiresImatrix, - BitRange = standardFamily.BitRange, - ExplicitCandidateSortOrder = standardFamily.ExplicitCandidateSortOrder, - IsActiveInCurrentConfig = true, - FirstSeenUtc = definition?.FirstSeenUtc ?? now, - LastSeenUtc = now, - LastUpdatedUtc = now - }; - - if (definition == null) - { - definition = nextDefinition; - db.BaselineQuantDefinitions.Add(definition); - existingDefinitions.Add(definition); - } - else - { - MagicQuantContext.ApplyBaselineDefinitionUpdate(definition, nextDefinition, preserveFirstSeen: true); + var dynamicBaseline = BaselineQuants.CreateDynamicCustomBaseline( + uniqueId: dynamicBaselineId, + displayName: displayName, + quantizeBaseArgumentName: quantizeBaseName, + sourceRepository: repo.RepoId, + sourceFileName: resolvedFileName, + shortSourceName: shortSourceName, + sourceOwner: DeriveSourceOwner(repo.RepoId), + sourceKind: repo.SourceKind, + canonicalKey: canonicalKey, + primaryTensorWeightScheme: standardFamily.PrimaryTensorWeightScheme, + learnedMatchTensorWeightSchemes: standardFamily.LearnedMatchTensorWeightSchemes, + bannedGroupIds: bannedGroups, + requiresImatrix: requiresImatrix, + isLearningBaseline: allowAsLearning, + isCombinationCarrierCandidate: allowAsCarrier, + isExplicitGroupCombinationCandidate: allowAsExplicit, + bitRange: standardFamily.BitRange, + explicitCandidateSortOrder: standardFamily.ExplicitCandidateSortOrder); + + BaselineQuants.RegisterDynamicCustomBaseline(dynamicBaseline); + + var spec = new ResolvedCustomBaselineSpec + { + DynamicBaselineId = dynamicBaseline.UniqueId, + BaselineQuantDefinitionId = definition.Id == 0 ? null : definition.Id, + CanonicalKey = dynamicBaseline.CanonicalKey, + DisplayName = dynamicBaseline.Names[0], + RepoId = repo.RepoId, + SourceOwner = dynamicBaseline.SourceOwner ?? string.Empty, + SourceFileName = dynamicBaseline.SourceFileName ?? string.Empty, + ShortSourceName = dynamicBaseline.ShortSourceName ?? shortSourceName, + BaselineFamily = standardFamily.Names[0], + QuantizeBaseName = dynamicBaseline.QuantizeBaseArgumentName, + RequiresImatrix = dynamicBaseline.RequiresImatrix, + AllowAsLearningBaseline = dynamicBaseline.IsLearningBaseline, + AllowAsCombinationCarrier = dynamicBaseline.IsCombinationCarrierCandidate, + AllowAsExplicitGroupCandidate = dynamicBaseline.IsExplicitGroupCombinationCandidate, + ForceRelearn = include.ForceRelearn, + IsActiveInCurrentConfig = true, + BannedGroupIds = dynamicBaseline.BannedGroupIds + }; + + resolved.Add(spec); + AnsiConsole.MarkupLine( + $" [green]Resolved:[/] id=[cyan]{dynamicBaseline.UniqueId}[/] family=[yellow]{Markup.Escape(standardFamily.Names[0])}[/] file=[blue]{Markup.Escape(resolvedFileName)}[/] learning={allowAsLearning} carrier={allowAsCarrier} explicit={allowAsExplicit} relearn={include.ForceRelearn}"); } - - var dynamicBaseline = BaselineQuants.CreateDynamicCustomBaseline( - uniqueId: dynamicBaselineId, - displayName: displayName, - quantizeBaseArgumentName: quantizeBaseName, - sourceRepository: repo.RepoId, - sourceFileName: resolvedFileName, - shortSourceName: shortSourceName, - sourceOwner: DeriveSourceOwner(repo.RepoId), - sourceKind: repo.SourceKind, - canonicalKey: canonicalKey, - primaryTensorWeightScheme: standardFamily.PrimaryTensorWeightScheme, - learnedMatchTensorWeightSchemes: standardFamily.LearnedMatchTensorWeightSchemes, - bannedGroupIds: bannedGroups, - requiresImatrix: requiresImatrix, - isLearningBaseline: allowAsLearning, - isCombinationCarrierCandidate: allowAsCarrier, - isExplicitGroupCombinationCandidate: allowAsExplicit, - bitRange: standardFamily.BitRange, - explicitCandidateSortOrder: standardFamily.ExplicitCandidateSortOrder); - - BaselineQuants.RegisterDynamicCustomBaseline(dynamicBaseline); - - var spec = new ResolvedCustomBaselineSpec - { - DynamicBaselineId = dynamicBaseline.UniqueId, - BaselineQuantDefinitionId = definition.Id == 0 ? null : definition.Id, - CanonicalKey = dynamicBaseline.CanonicalKey, - DisplayName = dynamicBaseline.Names[0], - RepoId = repo.RepoId, - SourceOwner = dynamicBaseline.SourceOwner ?? string.Empty, - SourceFileName = dynamicBaseline.SourceFileName ?? string.Empty, - ShortSourceName = dynamicBaseline.ShortSourceName ?? shortSourceName, - BaselineFamily = standardFamily.Names[0], - QuantizeBaseName = dynamicBaseline.QuantizeBaseArgumentName, - RequiresImatrix = dynamicBaseline.RequiresImatrix, - AllowAsLearningBaseline = dynamicBaseline.IsLearningBaseline, - AllowAsCombinationCarrier = dynamicBaseline.IsCombinationCarrierCandidate, - AllowAsExplicitGroupCandidate = dynamicBaseline.IsExplicitGroupCombinationCandidate, - ForceRelearn = include.ForceRelearn, - IsActiveInCurrentConfig = true, - BannedGroupIds = dynamicBaseline.BannedGroupIds - }; - - resolved.Add(spec); - AnsiConsole.MarkupLine( - $" [green]Resolved:[/] id=[cyan]{dynamicBaseline.UniqueId}[/] family=[yellow]{Markup.Escape(standardFamily.Names[0])}[/] file=[blue]{Markup.Escape(resolvedFileName)}[/] learning={allowAsLearning} carrier={allowAsCarrier} explicit={allowAsExplicit} relearn={include.ForceRelearn}"); } - } - await db.SaveChangesAsync(ct); + await db.SaveChangesAsync(ct); - foreach (var spec in resolved.Where(x => x.BaselineQuantDefinitionId == null)) - { - var definition = await db.BaselineQuantDefinitions.AsNoTracking().FirstAsync(x => - x.ArchitectureFamilyId == architectureFamilyId && - x.RuntimeBaselineId == spec.DynamicBaselineId, ct); - spec.BaselineQuantDefinitionId = definition.Id; - } + foreach (var spec in resolved.Where(x => x.BaselineQuantDefinitionId == null)) + { + var definition = await db.BaselineQuantDefinitions.AsNoTracking().FirstAsync(x => + x.ArchitectureFamilyId == architectureFamilyId && + x.RuntimeBaselineId == spec.DynamicBaselineId, ct); + spec.BaselineQuantDefinitionId = definition.Id; + } - RegisterHistoricalDefinitions(existingDefinitions.Where(x => !x.IsActiveInCurrentConfig)); + RegisterHistoricalDefinitions(existingDefinitions.Where(x => !x.IsActiveInCurrentConfig)); - Config.SetResolvedCustomBaselines(resolved); - BaselineQuants.ValidateIntegrityOrThrow(); + Config.SetResolvedCustomBaselines(resolved); + BaselineQuants.ValidateIntegrityOrThrow(); - AnsiConsole.MarkupLine($"[green]Custom baseline sync complete:[/] [cyan]{resolved.Count:N0}[/] active custom baseline(s); [cyan]{existingDefinitions.Count(x => !x.IsActiveInCurrentConfig):N0}[/] inactive historical definition(s) retained."); - return resolved; -} + AnsiConsole.MarkupLine($"[green]Custom baseline sync complete:[/] [cyan]{resolved.Count:N0}[/] active custom baseline(s); [cyan]{existingDefinitions.Count(x => !x.IsActiveInCurrentConfig):N0}[/] inactive historical definition(s) retained."); + return resolved; + } -private static void RegisterHistoricalDefinitions(IEnumerable definitions) -{ - foreach (var definition in definitions.Where(x => x.IsCustomBaseline)) + private static void RegisterHistoricalDefinitions(IEnumerable definitions) { - try + foreach (var definition in definitions.Where(x => x.IsCustomBaseline)) { - var runtime = BaselineDefinitionResolver.ToRuntimeBaseline(definition, forceInactiveRegistration: true); - BaselineQuants.RegisterDynamicCustomBaseline(runtime); - } - catch - { - // A bad historical row should not prevent active YAML from being resolved. - // It simply will not be available for runtime TensorConfig hydration until fixed. + try + { + var runtime = BaselineDefinitionResolver.ToRuntimeBaseline(definition, forceInactiveRegistration: true); + BaselineQuants.RegisterDynamicCustomBaseline(runtime); + } + catch + { + // A bad historical row should not prevent active YAML from being resolved. + // It simply will not be available for runtime TensorConfig hydration until fixed. + } } } -} -private static byte ResolveDynamicBaselineId( - string normalizedCanonicalKey, - IReadOnlyDictionary existingDynamicIdsByCanonicalKey, - HashSet reservedIds, - ref byte nextId) -{ - if (!string.IsNullOrWhiteSpace(normalizedCanonicalKey) && - existingDynamicIdsByCanonicalKey.TryGetValue(normalizedCanonicalKey, out var existingId)) + private static byte ResolveDynamicBaselineId( + string normalizedCanonicalKey, + IReadOnlyDictionary existingDynamicIdsByCanonicalKey, + HashSet reservedIds, + ref byte nextId) { - reservedIds.Add(existingId); - return existingId; - } + if (!string.IsNullOrWhiteSpace(normalizedCanonicalKey) && + existingDynamicIdsByCanonicalKey.TryGetValue(normalizedCanonicalKey, out var existingId)) + { + reservedIds.Add(existingId); + return existingId; + } - while (reservedIds.Contains(nextId)) - { - if (nextId >= 199) - throw new InvalidOperationException("No free dynamic baseline ids remain in the configured range."); + while (reservedIds.Contains(nextId)) + { + if (nextId >= 199) + throw new InvalidOperationException("No free dynamic baseline ids remain in the configured range."); - nextId++; - } + nextId++; + } - var allocated = nextId; - reservedIds.Add(allocated); + var allocated = nextId; + reservedIds.Add(allocated); - if (nextId < 199) - nextId++; + if (nextId < 199) + nextId++; - return allocated; -} + return allocated; + } public async Task DownloadBaselineAsync(BaselineQuants baseline, string destinationPath, bool forceRedownload = false, CancellationToken ct = default) { @@ -378,6 +378,21 @@ with open(result_path, 'w', encoding='utf-8') as f: if (!File.Exists(destinationPath) || new FileInfo(destinationPath).Length == 0 || !HasGgufMagic(destinationPath)) throw new InvalidOperationException($"External baseline staging produced no valid GGUF file: {destinationPath}"); + if (!string.Equals( + Path.GetFullPath(downloadedPath), + Path.GetFullPath(destinationPath), + StringComparison.OrdinalIgnoreCase)) + { + string destinationDirectory = Path.GetDirectoryName(Path.GetFullPath(destinationPath))!; + if (!IsPathInsideDirectory(downloadedPath, destinationDirectory)) + { + throw new InvalidOperationException( + $"Refusing to delete Hugging Face staging artifact outside the external baseline cache: {downloadedPath}"); + } + + await HardDeleteHelper.DeleteFileIfExistsAsync(downloadedPath); + } + AnsiConsole.MarkupLine(reused ? $"[grey]Reusing verified cached external baseline:[/] {Markup.Escape(destinationPath)}" : $"[green]Downloaded and atomically staged external baseline:[/] {Markup.Escape(destinationPath)}"); @@ -407,6 +422,16 @@ internal static bool CanReuseDownloadedFile(string downloadedPath, string destin downloaded.LastWriteTimeUtc == destination.LastWriteTimeUtc; } + internal static bool IsPathInsideDirectory(string childPath, string parentDirectory) + { + string child = Path.GetFullPath(childPath) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + string parent = Path.GetFullPath(parentDirectory) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + return child.StartsWith(parent + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase); + } + private static bool HasGgufMagic(string path) { try diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 3555a62..06ce316 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -91,38 +91,38 @@ public QuantizationService(BenchmarkService benchmarker) int threadCount = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; -// Minimum desired threads per llama-quantize process. -// This is used to decide the natural concurrency first. + // Minimum desired threads per llama-quantize process. + // This is used to decide the natural concurrency first. const int minimumQuantThreadsPerProcess = 4; -// Keep a little workstation breathing room. + // Keep a little workstation breathing room. int reservedThreads = threadCount switch { >= 16 => 2, - >= 8 => 2, - >= 4 => 1, - _ => 0 + >= 8 => 2, + >= 4 => 1, + _ => 0 }; int usableThreads = Math.Max(1, threadCount - reservedThreads); -// First decide how many quantization processes the CPU budget would naturally allow. + // First decide how many quantization processes the CPU budget would naturally allow. int naturalConcurrentQuantizations = Math.Max( 1, usableThreads / minimumQuantThreadsPerProcess); -// Then cap it to avoid hammering the output drive with too many giant writers. + // Then cap it to avoid hammering the output drive with too many giant writers. int scratchWriterCapacity = _scratchStorage.WriterCapacity; _maxConcurrentQuantizations = Math.Max( 1, Math.Min(naturalConcurrentQuantizations, scratchWriterCapacity)); -// Divide the usable thread budget evenly across the allowed quantization processes. -// Example on 7950X3D: -// 32 total - 2 reserved = 30 usable -// natural = 30 / 8 = 3 -// capped = min(2, 3) = 2 -// threads/process = 30 / 2 = 15 + // Divide the usable thread budget evenly across the allowed quantization processes. + // Example on 7950X3D: + // 32 total - 2 reserved = 30 usable + // natural = 30 / 8 = 3 + // capped = min(2, 3) = 2 + // threads/process = 30 / 2 = 15 _quantThreadsPerProcess = Math.Max( 1, usableThreads / _maxConcurrentQuantizations); @@ -512,127 +512,141 @@ public async Task ProcessHybridQuantAsync( baselineLearnedTruthExists, ct); - ScratchArtifactKind leaseKind = pureExternalBaseline - ? ScratchArtifactKind.ExternalBaselineRebuild - : quant.BaseQuant.IsExternalRepositoryBaseline - ? ScratchArtifactKind.ExternalBaselineNormalizedSample - : ScratchArtifactKind.QuantizedSample; - - await using var lease = await _scratchStorage.AcquireAsync(leaseKind, modelName, ct: ct); - string benchmarkModelPath = lease.GgufPath; + string? disposableExternalBaselinePath = pureExternalBaseline && + IsPathInsideExternalBaselineCacheRoot(inputPath) + ? inputPath + : null; try { - QuantizationExecutionReport? quantizationReport = null; - PreparedExternalBaselineBuild? preparedExternalBaseline = null; + ScratchArtifactKind leaseKind = pureExternalBaseline + ? ScratchArtifactKind.ExternalBaselineRebuild + : quant.BaseQuant.IsExternalRepositoryBaseline + ? ScratchArtifactKind.ExternalBaselineNormalizedSample + : ScratchArtifactKind.QuantizedSample; + + await using var lease = await _scratchStorage.AcquireAsync(leaseKind, modelName, ct: ct); + string benchmarkModelPath = lease.GgufPath; - await _cpuQuantLock.WaitAsync(ct); try { - if (pureExternalBaseline) - { - preparedExternalBaseline = await PrepareExternalBaselineRebuildAsync( - quant, - downloadedExternalBaselinePath: inputPath, - rebuiltOutputPath: lease.GgufPath, - logPath: lease.PrimaryLogPath, - metadataWorkingDirectory: lease.LeaseDirectory, - forceBaselineRelearn: forceBaselineRelearn, - ct: ct); - benchmarkModelPath = preparedExternalBaseline.BenchmarkModelPath; - } - else - { - var quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline - ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) - : quant; + QuantizationExecutionReport? quantizationReport = null; + PreparedExternalBaselineBuild? preparedExternalBaseline = null; - IReadOnlyDictionary? temporaryCarrierOverrides = null; - - if (quant.BaseQuant.IsExternalRepositoryBaseline) + await _cpuQuantLock.WaitAsync(ct); + try + { + if (pureExternalBaseline) { - temporaryCarrierOverrides = TryLoadAllLearnedTensorMappings( - canonicalBaselineKey: quant.BaseQuant.CanonicalKey, - preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, - allowDominantFallback: true); + preparedExternalBaseline = await PrepareExternalBaselineRebuildAsync( + quant, + downloadedExternalBaselinePath: inputPath, + rebuiltOutputPath: lease.GgufPath, + logPath: lease.PrimaryLogPath, + metadataWorkingDirectory: lease.LeaseDirectory, + forceBaselineRelearn: forceBaselineRelearn, + ct: ct); + benchmarkModelPath = preparedExternalBaseline.BenchmarkModelPath; + } + else + { + var quantToExecute = quant.BaseQuant.IsExternalRepositoryBaseline + ? CreateEquivalentStandardCarrierQuantForExternalRebuild(quant) + : quant; + + IReadOnlyDictionary? temporaryCarrierOverrides = null; - if (temporaryCarrierOverrides.Count == 0) + if (quant.BaseQuant.IsExternalRepositoryBaseline) { - throw new InvalidOperationException( - $"Missing blanket learned mapping for external/custom baseline '{quant.BaseQuant.Names[0]}'. " + - "External baseline hybrids require learned tensor mappings before sampling. " + - "Use targeted YAML relearn configuration to regenerate only the affected baseline/profile truth."); + temporaryCarrierOverrides = TryLoadAllLearnedTensorMappings( + canonicalBaselineKey: quant.BaseQuant.CanonicalKey, + preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, + allowDominantFallback: true); + + if (temporaryCarrierOverrides.Count == 0) + { + throw new InvalidOperationException( + $"Missing blanket learned mapping for external/custom baseline '{quant.BaseQuant.Names[0]}'. " + + "External baseline hybrids require learned tensor mappings before sampling. " + + "Use targeted YAML relearn configuration to regenerate only the affected baseline/profile truth."); + } } - } - var effectiveInputPath = quant.BaseQuant.IsExternalRepositoryBaseline - ? await EnsureBaseModelFileAsync() - : inputPath; - - quantizationReport = await RunLlamaQuantizeAsync( - effectiveInputPath, - lease.GgufPath, - quantToExecute, - temporaryCarrierOverrides: temporaryCarrierOverrides, - logPath: lease.PrimaryLogPath, - metadataWorkingDirectory: lease.LeaseDirectory, - ct: ct); + var effectiveInputPath = quant.BaseQuant.IsExternalRepositoryBaseline + ? await EnsureBaseModelFileAsync() + : inputPath; + + quantizationReport = await RunLlamaQuantizeAsync( + effectiveInputPath, + lease.GgufPath, + quantToExecute, + temporaryCarrierOverrides: temporaryCarrierOverrides, + logPath: lease.PrimaryLogPath, + metadataWorkingDirectory: lease.LeaseDirectory, + ct: ct); + } + } + finally + { + _cpuQuantLock.Release(); } - } - finally - { - _cpuQuantLock.Release(); - } - AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); + AnsiConsole.MarkupLine($"[yellow]Benchmarking:[/] {Markup.Escape(modelName)}"); - await _benchmarker.RunAllBenchmarksAsync( - quantConfig: quant, - modelPath: benchmarkModelPath, - benchDir: modelBenchDir, - klLogitsDir: baseLogitsDir, - saveLogits: false, - domainsOverride: new[] { "general" }); - - if (IsLearnableBaselineRun(quant)) - { - if (preparedExternalBaseline?.HasPreparedLearningTruth == true) - await PersistLearnedBaselineTensorMapFromPreparedAsync(quant, preparedExternalBaseline, ct); - else if (!baselineLearnedTruthExists || forceBaselineRelearn) - await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); - } + await _benchmarker.RunAllBenchmarksAsync( + quantConfig: quant, + modelPath: benchmarkModelPath, + benchDir: modelBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: false, + domainsOverride: new[] { "general" }); - await PersistQuantizationRunAsync( - quant: quant, - imatrixDefinitionId: null, - startedUtc: startedUtc, - completedUtc: DateTime.UtcNow, - succeeded: true, - outputModelPath: benchmarkModelPath, - error: null, - ct: ct); + if (IsLearnableBaselineRun(quant)) + { + if (preparedExternalBaseline?.HasPreparedLearningTruth == true) + await PersistLearnedBaselineTensorMapFromPreparedAsync(quant, preparedExternalBaseline, ct); + else if (!baselineLearnedTruthExists || forceBaselineRelearn) + await LearnAndPersistBaselineTensorMapAsync(quant, benchmarkModelPath, quantizationReport, ct); + } - return SampleProcessState.Completed; - } - catch (Exception ex) - { - try - { await PersistQuantizationRunAsync( quant: quant, imatrixDefinitionId: null, startedUtc: startedUtc, completedUtc: DateTime.UtcNow, - succeeded: false, + succeeded: true, outputModelPath: benchmarkModelPath, - error: ex.ToString(), + error: null, ct: ct); + + return SampleProcessState.Completed; } - catch + catch (Exception ex) { - } + try + { + await PersistQuantizationRunAsync( + quant: quant, + imatrixDefinitionId: null, + startedUtc: startedUtc, + completedUtc: DateTime.UtcNow, + succeeded: false, + outputModelPath: benchmarkModelPath, + error: ex.ToString(), + ct: ct); + } + catch + { + } - throw; + throw; + } + } + finally + { + await TryCleanupExternalBaselineDownloadArtifactsAsync( + disposableExternalBaselinePath, + "after digestion and benchmarking"); } } @@ -681,15 +695,9 @@ private async Task GetEffectiveInputModelPathAsync( } catch { - try - { - await CleanupExternalBaselineDownloadArtifactsAsync(externalPath); - } - catch (Exception cleanupEx) - { - AnsiConsole.MarkupLine( - $"[yellow]Warning:[/] failed to clean external baseline staging after failed download/validation: {Markup.Escape(cleanupEx.Message)}"); - } + await TryCleanupExternalBaselineDownloadArtifactsAsync( + externalPath, + "after failed download/validation"); throw; } @@ -905,7 +913,7 @@ private async Task PersistLearnedBaselineTensorMapFromPreparedAsync( var tensorScheme = quant.BaseQuant.DefaultTensorScheme!; var verification = prepared.Verification ?? new TensorTruthVerificationResult - { TruthByTensor = prepared.TruthByTensor }; + { TruthByTensor = prepared.TruthByTensor }; var audit = new TensorGroupingAuditResult { GroupedByTensor = prepared.GroupedByTensor, @@ -1040,22 +1048,28 @@ private async Task CleanupExternalBaselineDownloadArtifactsAsync(string? downloa string fullFile = Path.GetFullPath(downloadedExternalBaselinePath); string? root = Cache.ExternalBaselineCacheDirectory; - if (!string.IsNullOrWhiteSpace(root)) + if (string.IsNullOrWhiteSpace(root)) + throw new InvalidOperationException("External baseline cache root is not configured."); + + string fullRoot = Path.GetFullPath(root); + if (!IsPathInside(fullFile, fullRoot)) + { + throw new InvalidOperationException( + $"Refusing to clean external baseline artifact outside configured cache root: {fullFile}"); + } + + string? stagingDir = Path.GetDirectoryName(fullFile); + + if (!string.IsNullOrWhiteSpace(stagingDir)) { - string fullRoot = Path.GetFullPath(root); - string? stagingDir = Path.GetDirectoryName(fullFile); + string fullStagingDir = Path.GetFullPath(stagingDir); - if (!string.IsNullOrWhiteSpace(stagingDir)) + if (IsPathInside(fullStagingDir, fullRoot) && + !string.Equals(fullStagingDir, fullRoot, StringComparison.OrdinalIgnoreCase) && + Directory.Exists(fullStagingDir)) { - string fullStagingDir = Path.GetFullPath(stagingDir); - - if (IsPathInside(fullStagingDir, fullRoot) && - !string.Equals(fullStagingDir, fullRoot, StringComparison.OrdinalIgnoreCase) && - Directory.Exists(fullStagingDir)) - { - await HardDeleteHelper.DeleteDirectoryIfExistsAsync(fullStagingDir); - return; - } + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(fullStagingDir); + return; } } @@ -1064,6 +1078,24 @@ private async Task CleanupExternalBaselineDownloadArtifactsAsync(string? downloa await HardDeleteHelper.DeleteFileIfExistsAsync(fullFile + ".externalcheck"); } + private async Task TryCleanupExternalBaselineDownloadArtifactsAsync(string? path, string reason) + { + if (string.IsNullOrWhiteSpace(path)) + return; + + try + { + await CleanupExternalBaselineDownloadArtifactsAsync(path); + AnsiConsole.MarkupLine( + $"[green]Cleaned disposable external baseline[/] [grey]({Markup.Escape(reason)}):[/] {Markup.Escape(path)}"); + } + catch (Exception cleanupEx) + { + AnsiConsole.MarkupLine( + $"[yellow]Warning:[/] failed to clean external baseline {Markup.Escape(reason)}: {Markup.Escape(cleanupEx.Message)}"); + } + } + private bool IsPathInsideExternalBaselineCacheRoot(string path) { if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(Cache.ExternalBaselineCacheDirectory)) @@ -1079,8 +1111,8 @@ private static bool IsPathInside(string childPath, string parentPath) return child.StartsWith(parent + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase); } -// ---------------------------------------------------------------- -// Benchmark/logit helpers + // ---------------------------------------------------------------- + // Benchmark/logit helpers // ---------------------------------------------------------------- private string GetBaseLogitsDirectory() => _paths.GetBaseLogitsDirectory(); @@ -1470,13 +1502,6 @@ public async Task BuildExportArtifactAsync( if (quant.BaseQuant.IsExternalRepositoryBaseline) { - string durableExternalPath = GetExternalBaselineCachePath(quant.BaseQuant); - await _huggingFaceBaselineService.DownloadBaselineAsync( - quant.BaseQuant, - durableExternalPath, - forceRedownload: false, - ct: ct); - temporaryCarrierOverrides = TryLoadAllLearnedTensorMappings( canonicalBaselineKey: quant.BaseQuant.CanonicalKey, preferredSourceScheme: quant.BaseQuant.DefaultTensorScheme, @@ -2490,67 +2515,67 @@ private List BuildRequestedTensorOverrides( switch (hybrid.OverrideMode) { case HybridTensorOverrideMode.ExactTensorScheme: - { - var exactScheme = hybrid.ExactTensorScheme!; - if (!quant.BaseQuant.IsExternalRepositoryBaseline && baseScheme != null && - exactScheme.UniqueId == baseScheme.UniqueId) - continue; - - string schemeName = ResolveSchemeName(exactScheme); - foreach (var tensorName in expectedForGroup.OrderBy(x => x, StringComparer.Ordinal)) { - result.Add(new RequestedTensorOverride + var exactScheme = hybrid.ExactTensorScheme!; + if (!quant.BaseQuant.IsExternalRepositoryBaseline && baseScheme != null && + exactScheme.UniqueId == baseScheme.UniqueId) + continue; + + string schemeName = ResolveSchemeName(exactScheme); + foreach (var tensorName in expectedForGroup.OrderBy(x => x, StringComparer.Ordinal)) { - GroupName = hybrid.TGroup.Name, - TensorName = tensorName, - SchemeName = schemeName - }); - } + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + TensorName = tensorName, + SchemeName = schemeName + }); + } - break; - } + break; + } case HybridTensorOverrideMode.LearnedBaselineCandidate: - { - var sourceBaseline = hybrid.CandidateBaseline!; - var learned = TryLoadLearnedTensorMapping( - sourceBaseline: sourceBaseline, - targetGroup: hybrid.TGroup, - preferredSourceScheme: sourceBaseline.DefaultTensorScheme, - allowDominantFallback: false); - - if (learned.Count == 0) - throw new InvalidOperationException( - $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Use targeted YAML relearn configuration to regenerate only the affected baseline/profile truth."); + { + var sourceBaseline = hybrid.CandidateBaseline!; + var learned = TryLoadLearnedTensorMapping( + sourceBaseline: sourceBaseline, + targetGroup: hybrid.TGroup, + preferredSourceScheme: sourceBaseline.DefaultTensorScheme, + allowDominantFallback: false); + + if (learned.Count == 0) + throw new InvalidOperationException( + $"Missing required learned baseline mapping for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Use targeted YAML relearn configuration to regenerate only the affected baseline/profile truth."); - var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); - var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); - var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); + var learnedNames = learned.Keys.ToHashSet(StringComparer.Ordinal); + var missingExpected = expectedForGroup.Except(learnedNames).OrderBy(x => x).ToList(); + var unexpectedLearned = learnedNames.Except(expectedForGroup).OrderBy(x => x).ToList(); - if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) - { - var missingText = missingExpected.Count == 0 - ? "none" - : string.Join(", ", missingExpected.Take(15)); - var unexpectedText = unexpectedLearned.Count == 0 - ? "none" - : string.Join(", ", unexpectedLearned.Take(15)); - throw new InvalidOperationException( - $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); - } + if (missingExpected.Count > 0 || unexpectedLearned.Count > 0) + { + var missingText = missingExpected.Count == 0 + ? "none" + : string.Join(", ", missingExpected.Take(15)); + var unexpectedText = unexpectedLearned.Count == 0 + ? "none" + : string.Join(", ", unexpectedLearned.Take(15)); + throw new InvalidOperationException( + $"Learned mapping coverage mismatch for group '{hybrid.TGroup.Name}' + baseline '{sourceBaseline.Names[0]}'. Expected={expectedForGroup.Count}, Learned={learnedNames.Count}, Missing=[{missingText}], Unexpected=[{unexpectedText}]."); + } - foreach (var kv in learned.OrderBy(x => x.Key, StringComparer.Ordinal)) - { - result.Add(new RequestedTensorOverride + foreach (var kv in learned.OrderBy(x => x.Key, StringComparer.Ordinal)) { - GroupName = hybrid.TGroup.Name, - TensorName = kv.Key, - SchemeName = kv.Value - }); - } + result.Add(new RequestedTensorOverride + { + GroupName = hybrid.TGroup.Name, + TensorName = kv.Key, + SchemeName = kv.Value + }); + } - break; - } + break; + } default: throw new InvalidOperationException( From c7a97a846667aca18dfda55685ecfdd2f8aeb998 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Thu, 20 Aug 2026 20:24:19 -0400 Subject: [PATCH 227/258] update --- MagicQuant/Program.cs | 11 +++-------- MagicQuant/config.clone-unsloth.dev.yaml | 8 ++++---- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 09f7f6a..a3ee559 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -18,16 +18,11 @@ args = [ "clone-repository-quants", - "--config", Path.Combine(AppContext.BaseDirectory, "config.clone-unsloth.dev.yaml"), + "--config", $"\"{Path.Combine(AppContext.BaseDirectory, "config.clone-unsloth.dev.yaml")}\"", "--architecture-family", @"""Qwen3.8-27B""", - "--source-repo", @"""magiccodingman/Qwen3.8-27B-MagicQuant-GGUF""", + "--source-json", @"""/mnt/world8/AI/Models/Qwen3.8-27B-MagicQuant/magicquant-manifest/magicquant.clone-configs.json""", "--model-dir", @"""/mnt/world8/AI/Models/Qwen3.8-27B-Qwen/""", - "--output-dir", @"""/mnt/world8/AI/Models/Qwen3.8-27B-MagicQuant-Unsloth/""", - "--use-imatrix", - "--imatrix-url", @"""https://huggingface.co/unsloth/Qwen3.8-27B-GGUF/resolve/main/imatrix_unsloth.dat?download=true""", - "--allow-architecture-family-alias-override", - "--missing-manifest-base-quant", "Q8_0", - "--reuse-existing-final-artifacts" + "--output-dir", @"""/mnt/world8/AI/Models/Qwen3.8-27B-MagicQuant-Unsloth/""" ]; } else diff --git a/MagicQuant/config.clone-unsloth.dev.yaml b/MagicQuant/config.clone-unsloth.dev.yaml index f31fbad..29d4e8c 100644 --- a/MagicQuant/config.clone-unsloth.dev.yaml +++ b/MagicQuant/config.clone-unsloth.dev.yaml @@ -16,12 +16,12 @@ hardware: 0: 19 1: 23 -# The actual Unsloth imatrix URL is supplied by the DEBUG clone args in Program.cs. -# Keep the other source modes empty so ImatrixService sees exactly one active source. +# Prebuilt Unsloth imatrix. Keep the other source modes empty so ImatrixService +# sees exactly one active source. imatrix: - imatrix_url: + imatrix_url: "https://huggingface.co/unsloth/Qwen3.8-27B-GGUF/resolve/main/imatrix_unsloth.gguf?download=true" dataset_repo: - dataset_split: text + dataset_split: dataset_config: dataset_local_file: From 082343ffe86178ce15b63d2aef34a8c9acd61bcf Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 05:46:24 -0400 Subject: [PATCH 228/258] Optimize multi-GPU benchmark scheduling --- MagicQuant.Tests/BenchmarkGpuPlanningTests.cs | 103 +++ MagicQuant.Tests/CliArgumentParsingTests.cs | 33 + .../HardwareInitializationTests.cs | 53 ++ .../LearnedBaselinePruningServiceTests.cs | 72 +- .../LlamaGpuArgumentBuilderTests.cs | 51 ++ MagicQuant/Commands/Evolution.cs | 1 + MagicQuant/Commands/InitializeLlamaCpp.cs | 23 +- .../Configuration/MagicQuantYamlLoader.cs | 3 +- MagicQuant/Helpers/CliHelpers.cs | 42 +- MagicQuant/Program.cs | 3 +- MagicQuant/Services/BenchmarkGpuPlanning.cs | 402 ++++++++++ MagicQuant/Services/BenchmarkService.cs | 714 ++++++++++-------- MagicQuant/Services/GgufMetadataReader.cs | 121 +++ .../Services/LlamaGpuArgumentBuilder.cs | 43 ++ MagicQuant/Services/QuantizationService.cs | 82 +- 15 files changed, 1296 insertions(+), 450 deletions(-) create mode 100644 MagicQuant.Tests/BenchmarkGpuPlanningTests.cs create mode 100644 MagicQuant.Tests/CliArgumentParsingTests.cs create mode 100644 MagicQuant.Tests/HardwareInitializationTests.cs create mode 100644 MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs create mode 100644 MagicQuant/Services/BenchmarkGpuPlanning.cs create mode 100644 MagicQuant/Services/GgufMetadataReader.cs create mode 100644 MagicQuant/Services/LlamaGpuArgumentBuilder.cs diff --git a/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs b/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs new file mode 100644 index 0000000..02ba076 --- /dev/null +++ b/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs @@ -0,0 +1,103 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public class BenchmarkGpuPlanningTests +{ + private const ulong Q8Size = 29_047_084_736UL; + + [Theory] + [InlineData(29_047_084_736UL, 44)] + [InlineData(21_998_012_096UL, 58)] + [InlineData(17_624_999_616UL, 66)] + public void ResolveNglForModel_ScalesAndClampsAtFullOffload(ulong size, int expected) + { + int result = BenchmarkGpuPlanner.ResolveNglForModel(Q8Size, 44, 66, size); + Assert.Equal(expected, result); + } + + [Fact] + public void EstimateIndependentCrossover_UsesMeasuredPerSlotScaling() + { + var slots = new[] + { + Slot(0, 44, (35, 6.59), (41, 5.83), (44, 5.45)), + Slot(1, 57, (48, 4.38), (56, 3.28), (57, 3.17)) + }; + + ulong crossover = BenchmarkGpuPlanner.EstimateIndependentCrossoverBytes( + Q8Size, + maxOffloadNgl: 66, + sharedSecondsPerPass: 1.60, + slots); + + double gib = crossover / 1024d / 1024d / 1024d; + Assert.InRange(gib, 22d, 27d); + } + + [Fact] + public async Task ResourceScheduler_ReservesDisjointSingleGpuSlotsConcurrently() + { + var scheduler = new GpuResourceScheduler(); + var slots = new[] { Slot(0, 44, (35, 6.5), (44, 5.4)), Slot(1, 57, (48, 4.3), (57, 3.1)) }; + + await using var first = await scheduler.AcquireAsync(slots); + await using var second = await scheduler.AcquireAsync(slots); + + Assert.NotEqual(first.Slot.DeviceIndices[0], second.Slot.DeviceIndices[0]); + } + + [Fact] + public async Task ResourceScheduler_SharedSlotWaitsUntilAllDevicesAreFree() + { + var scheduler = new GpuResourceScheduler(); + var singleSlots = new[] { Slot(0, 44, (35, 6.5), (44, 5.4)), Slot(1, 57, (48, 4.3), (57, 3.1)) }; + var sharedSlots = new[] { new BenchmarkSlot(2, "shared", [0, 1], 66, []) }; + + var first = await scheduler.AcquireAsync(singleSlots); + var sharedTask = scheduler.AcquireAsync(sharedSlots).AsTask(); + + Assert.False(sharedTask.IsCompleted); + await first.DisposeAsync(); + + await using var shared = await sharedTask.WaitAsync(TimeSpan.FromSeconds(2)); + Assert.Equal([0, 1], shared.Slot.DeviceIndices); + } + + [Fact] + public void TopologyCacheCodec_RoundTripsMeasuredProfiles() + { + var independentSlots = new[] + { + Slot(0, 44, (35, 6.59), (44, 5.45)), + Slot(1, 57, (48, 4.38), (57, 3.17)) + }; + var shared = new BenchmarkTopologyProfile( + "shared", [new BenchmarkSlot(2, "shared", [0, 1], 66, [])], 0.1, 1.6); + var independent = new BenchmarkTopologyProfile("independent", independentSlots, 0.08, 0); + + string json = BenchmarkTopologyCacheCodec.Serialize(66, 26_000_000_000UL, shared, independent); + bool ok = BenchmarkTopologyCacheCodec.TryDeserialize( + json, out int maxNgl, out ulong crossover, out var loadedShared, out var loadedIndependent); + + Assert.True(ok); + Assert.Equal(66, maxNgl); + Assert.Equal(26_000_000_000UL, crossover); + Assert.Equal([0, 1], loadedShared!.Slots.Single().DeviceIndices); + Assert.Equal([44, 57], loadedIndependent!.Slots.Select(x => x.Q8StableNgl).ToArray()); + } + + private static BenchmarkSlot Slot( + int device, + int stableNgl, + params (int Ngl, double Seconds)[] samples) + => new( + SlotId: device, + ProfileName: "independent", + DeviceIndices: [device], + Q8StableNgl: stableNgl, + ProbeSamples: samples + .Select(x => new GpuProbeSample(x.Ngl, true, x.Seconds, x.Seconds * 3d)) + .ToArray()); +} diff --git a/MagicQuant.Tests/CliArgumentParsingTests.cs b/MagicQuant.Tests/CliArgumentParsingTests.cs new file mode 100644 index 0000000..705cc7e --- /dev/null +++ b/MagicQuant.Tests/CliArgumentParsingTests.cs @@ -0,0 +1,33 @@ +using MagicQuant.Helpers; +using Xunit; + +namespace MagicQuant.Tests; + +public class CliArgumentParsingTests +{ + [Fact] + public void ArgvParser_PreservesHyphensInOrdinaryShellValues() + { + var parsed = CliHelpers.ParseArguments([ + "--config", "/repo/MagicQuant-Pipeline/config.dev.yaml", + "--architecture-family", "Qwen3.8-27B", + "--recheck-hardware-probe" + ]); + + Assert.Equal("/repo/MagicQuant-Pipeline/config.dev.yaml", parsed[0].Value); + Assert.Equal("Qwen3.8-27B", parsed[1].Value); + Assert.Equal(string.Empty, parsed[2].Value); + } + + [Fact] + public void ArgvParser_SupportsEqualsAndLegacyLiteralQuotes() + { + var parsed = CliHelpers.ParseArguments([ + "--model-dir=/models/Qwen-27B", + "--output-dir", "\"/models/Agent-Run\"" + ]); + + Assert.Equal("/models/Qwen-27B", parsed[0].Value); + Assert.Equal("/models/Agent-Run", parsed[1].Value); + } +} diff --git a/MagicQuant.Tests/HardwareInitializationTests.cs b/MagicQuant.Tests/HardwareInitializationTests.cs new file mode 100644 index 0000000..7982953 --- /dev/null +++ b/MagicQuant.Tests/HardwareInitializationTests.cs @@ -0,0 +1,53 @@ +using MagicQuant.Commands; +using MagicQuant.Models; +using MQ.DB; +using Xunit; + +namespace MagicQuant.Tests; + +[CollectionDefinition(Name, DisableParallelization = true)] +public sealed class HardwareInitializationCollection +{ + public const string Name = "Hardware initialization"; +} + +[Collection(HardwareInitializationCollection.Name)] +public sealed class HardwareInitializationTests +{ + [Fact] + public async Task Custom_environment_validation_populates_system_info() + { + string testRoot = Path.Combine( + Path.GetTempPath(), + $"magicquant-hardware-init-{Guid.NewGuid():N}"); + string llamaRoot = Path.Combine(testRoot, "llama.cpp"); + string llamaBin = Path.Combine(llamaRoot, "build", "bin"); + string convertScript = Path.Combine(llamaRoot, "convert_hf_to_gguf.py"); + var previous = Cache.SysInfo; + + try + { + Directory.CreateDirectory(llamaBin); + await File.WriteAllTextAsync(convertScript, "# test"); + Cache.SysInfo = null; + + await new InitializeLlamaCpp().Run( + [ + new CliArg { Name = "validate", Value = string.Empty }, + new CliArg { Name = "llama-root", Value = llamaRoot }, + new CliArg { Name = "llama-bin", Value = llamaBin }, + new CliArg { Name = "convert-script", Value = convertScript } + ]); + + Assert.NotNull(Cache.SysInfo); + Assert.True(Cache.SysInfo.ThreadCount > 0); + Assert.True(Cache.SysInfo.RamGb > 0); + } + finally + { + Cache.SysInfo = previous; + if (Directory.Exists(testRoot)) + Directory.Delete(testRoot, recursive: true); + } + } +} diff --git a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs index 4d776d2..691c0ac 100644 --- a/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs +++ b/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs @@ -1,6 +1,4 @@ -using MagicQuant.Helpers; using MagicQuant.Services; -using MQ.DB.Models; using Xunit; namespace MagicQuant.Tests; @@ -8,70 +6,28 @@ namespace MagicQuant.Tests; public class LearnedBaselinePruningServiceTests { [Fact] - public void Embeddings_LearnedBaselinePruning_OnlyAllowsQ6KAndBansOtherBaselinesWhenFinalTypeMapsToQ6K() + public async Task CoverageStatus_ReportsEarlyPruningDisabled() { - RuntimeSearchSpace.ResetForNewModel(); - RuntimeSearchSpace.SetImatrixAvailability(true); + var service = new LearnedBaselinePruningService(); - var result = new LearnedBaselinePruningResult(); + var status = await service.GetCoverageStatusAsync(); - var learnedRows = new List - { - new(BaselineQuants.Q6_K.UniqueId, TensorWeightScheme.Q6_K.UniqueId, TReg.Embeddings.UniqueId, "Q6_K"), - new(BaselineQuants.Q5_K.UniqueId, TensorWeightScheme.Q5_K.UniqueId, TReg.Embeddings.UniqueId, "Q6_K"), - new(BaselineQuants.Q4_K_M.UniqueId, TensorWeightScheme.Q4_K.UniqueId, TReg.Embeddings.UniqueId, "Q6_K"), - new(BaselineQuants.IQ4_NL.UniqueId, TensorWeightScheme.IQ4_NL.UniqueId, TReg.Embeddings.UniqueId, "Q6_K"), - new(BaselineQuants.IQ4_XS.UniqueId, TensorWeightScheme.IQ4_XS.UniqueId, TReg.Embeddings.UniqueId, "Q6_K") - }; - - var unused = new HashSet(); - - LearnedBaselinePruningService.ApplyLearnedBaselinePruning( - learnedRows, - aiModelHashId: 1, - aiModelHashUniqueHash: "regression-model-hash", - unusedGroupIds: unused, - result: result); - - Assert.False(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.Q6_K)); - Assert.True(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.Q5_K)); - Assert.True(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.Q4_K_M)); - Assert.True(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.IQ4_NL)); - Assert.True(RuntimeSearchSpace.IsCombinationCandidateRuntimeBannedForGroup(TReg.Embeddings, BaselineQuants.IQ4_XS)); - - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q6_K") && x.Contains("decision=ALLOW")); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q5_K") && x.Contains("decision=BAN")); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=Q4_K") && x.Contains("decision=BAN")); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=IQ4_NL") && x.Contains("decision=BAN")); - Assert.Contains(result.Notes, x => x.Contains("group=embeddings") && x.Contains("candidate=IQ4_XS") && x.Contains("decision=BAN")); - Assert.Contains(result.Notes, x => x.Contains("expected=[4]") && x.Contains("effective=[4]") && x.Contains("matched=[4]")); + Assert.False(status.HasAnyLearnedRows); + Assert.False(status.SafeToApplyBeforeStartup); + Assert.Equal(0, status.ExpectedCandidateGroupPairs); + Assert.Equal(0, status.PresentCandidateGroupPairs); + Assert.Contains(status.MissingPairs, x => x.Contains("disabled", StringComparison.OrdinalIgnoreCase)); } [Fact] - public void LearnedPruneBookkeeping_CanBeClearedPerGroupCandidate() + public async Task AnalyzeAndApply_DoesNotPruneWhileFeatureIsDisabled() { - RuntimeSearchSpace.ResetForNewModel(); - RuntimeSearchSpace.SetImatrixAvailability(true); - - var result = new LearnedBaselinePruningResult(); - var learnedRows = new List - { - new(BaselineQuants.Q5_K.UniqueId, TensorWeightScheme.Q5_K.UniqueId, TReg.Embeddings.UniqueId, "Q6_K") - }; - - LearnedBaselinePruningService.ApplyLearnedBaselinePruning( - learnedRows, - aiModelHashId: 1, - aiModelHashUniqueHash: "regression-model-hash", - unusedGroupIds: new HashSet(), - result: result); - - Assert.True(RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(TReg.Embeddings) - .Any(x => x.Candidate.UniqueId == BaselineQuants.Q5_K.UniqueId)); + var service = new LearnedBaselinePruningService(); - RuntimeSearchSpace.ClearLearnedBaselinePruneForGroupCandidate(TReg.Embeddings, BaselineQuants.Q5_K); + var result = await service.AnalyzeAndApplyAsync(); - Assert.DoesNotContain(RuntimeSearchSpace.GetLearnedBaselineMissingPrunedCandidatesForGroup(TReg.Embeddings), - x => x.Candidate.UniqueId == BaselineQuants.Q5_K.UniqueId); + Assert.Equal(0, result.GroupCandidateEliminations); + Assert.Equal(0, result.BaselinesSkippedWithoutLearnedRows); + Assert.Contains(result.Notes, x => x.Contains("disabled", StringComparison.OrdinalIgnoreCase)); } } diff --git a/MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs b/MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs new file mode 100644 index 0000000..d7f2d6a --- /dev/null +++ b/MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs @@ -0,0 +1,51 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public class LlamaGpuArgumentBuilderTests +{ + private static readonly IReadOnlyDictionary Limits = + new Dictionary + { + [0] = 19, + [1] = 23 + }; + + [Fact] + public void CommonCli_UsesCommaSeparatedMembers() + { + string args = LlamaGpuArgumentBuilder.BuildTensorSplitArgs( + [0, 1], Limits, LlamaGpuTool.CommonCli); + + Assert.Equal(" --tensor-split 19,23", args); + } + + [Fact] + public void LlamaBench_UsesSlashSeparatedMembers() + { + string args = LlamaGpuArgumentBuilder.BuildTensorSplitArgs( + [0, 1], Limits, LlamaGpuTool.LlamaBench); + + Assert.Equal(" --tensor-split 19/23", args); + } + + [Fact] + public void SingleGpu_DoesNotEmitTensorSplit() + { + string args = LlamaGpuArgumentBuilder.BuildTensorSplitArgs( + [1], Limits, LlamaGpuTool.CommonCli); + + Assert.Equal(string.Empty, args); + } + + [Fact] + public void MissingConfiguredLimit_IsRejected() + { + var error = Assert.Throws(() => + LlamaGpuArgumentBuilder.BuildTensorSplitArgs( + [0, 2], Limits, LlamaGpuTool.CommonCli)); + + Assert.Contains("2", error.Message, StringComparison.Ordinal); + } +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 2dfa5bb..25599c8 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -744,6 +744,7 @@ private void ShowEvolutionHelp() AnsiConsole.WriteLine(); AnsiConsole.MarkupLine("[bold]Arguments:[/]"); AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Optional if set in YAML)"); + AnsiConsole.MarkupLine(" [green]--magic-quant-root[/] Isolated runtime root containing MagicQuant_SQLite.db and shared runtime assets (Optional)"); AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); AnsiConsole.MarkupLine(" [green]--use-imatrix[/] Enable imatrix acquisition/build and allow imatrix-required search candidates (Optional)"); AnsiConsole.MarkupLine(" [green]--allow-high-precision-hybrids[/] Keep BF16/F16 explicit group candidates in final surviving combos (Optional, default false)"); diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 9f5f63f..3e092b9 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -43,6 +43,7 @@ public async Task Run(List args) } AnsiConsole.MarkupLine("[green]✔ Custom Environment Validated.[/]"); + _ = DetectAndCacheSystemInfo(); return; } else if (!string.IsNullOrEmpty(convertScript) || !string.IsNullOrEmpty(llamaBin)) @@ -61,11 +62,7 @@ public async Task Run(List args) // --------------------------------------------------------- // 3. Hardware Detection // --------------------------------------------------------- - var sysInfo = HardwareHelper.GetSystemInfo(); - Cache.SysInfo = sysInfo; - AnsiConsole.Write(new Rule("[yellow]System Detection[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"Detected GPU: [green]{sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor}[/] ([blue]{sysInfo.GpuInfo.FirstOrDefault()?.GpuName}[/] - {sysInfo.GpuInfo.Sum(x => x.VramGb):F1} GB)"); - AnsiConsole.MarkupLine($"Detected RAM: [blue]{sysInfo.RamGb:F1} GB[/]"); + var sysInfo = DetectAndCacheSystemInfo(); // --------------------------------------------------------- // 4. Linux System Deps (Sudo Handling) @@ -222,6 +219,20 @@ await EnsurePackage("llama-cpp-python", AnsiConsole.MarkupLine($"Llama Binaries: [grey]{builder.GetLlamaBinPath()}[/]"); } + private static SystemInfo DetectAndCacheSystemInfo() + { + var sysInfo = HardwareHelper.GetSystemInfo(); + Cache.SysInfo = sysInfo; + + AnsiConsole.Write(new Rule("[yellow]System Detection[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine( + $"Detected GPU: [green]{sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor}[/] " + + $"([blue]{sysInfo.GpuInfo.FirstOrDefault()?.GpuName}[/] - {sysInfo.GpuInfo.Sum(x => x.VramGb):F1} GB)"); + AnsiConsole.MarkupLine($"Detected RAM: [blue]{sysInfo.RamGb:F1} GB[/]"); + + return sysInfo; + } + // --- Helpers --- private static async Task RunSimpleProcess(string exe, string args) @@ -282,4 +293,4 @@ private bool AreLinuxPackagesInstalled(List packages) } return true; } -} \ No newline at end of file +} diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 983e92a..7faabe2 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -268,6 +268,7 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList string? Get(string name) => args.FirstOrDefault(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase))?.Value; bool Has(string name) => args.Any(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase)); + config.Paths.MagicQuantRoot = Prefer(Get("magic-quant-root"), config.Paths.MagicQuantRoot); config.Paths.ModelDir = Prefer(Get("model-dir"), config.Paths.ModelDir); config.Paths.LlamaRoot = Prefer(Get("llama-root"), config.Paths.LlamaRoot); config.Paths.LlamaBin = Prefer(Get("llama-bin"), config.Paths.LlamaBin); @@ -484,4 +485,4 @@ private static List NormalizeScratchRoots(IEnumerable? roots) return Path.GetFullPath(value); } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index 57d1330..9912289 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -107,6 +107,46 @@ public static List ParseArguments(string input) return cliArgs; } + public static List ParseArguments(IEnumerable arguments) + { + ArgumentNullException.ThrowIfNull(arguments); + + string[] tokens = arguments.ToArray(); + var cliArgs = new List(); + + for (int i = 0; i < tokens.Length; i++) + { + string token = tokens[i]; + if (!token.StartsWith("--", StringComparison.Ordinal) || token.Length <= 2) + continue; + + string option = token[2..]; + string name; + string value = string.Empty; + int equals = option.IndexOf('='); + + if (equals >= 0) + { + name = option[..equals]; + value = option[(equals + 1)..]; + } + else + { + name = option; + if (i + 1 < tokens.Length && !tokens[i + 1].StartsWith("--", StringComparison.Ordinal)) + value = tokens[++i]; + } + + cliArgs.Add(new CliArg + { + Name = name, + Value = value.Trim().Trim('"') + }); + } + + return cliArgs; + } + public static void ShowHelp(Dictionary Factory)> commands) { AnsiConsole.Write(new Rule("[yellow]MagicQuant CLI[/]") { Justification = Justify.Left, Style = "grey" }); @@ -129,4 +169,4 @@ public static void ShowHelp(Dictionary[/] | [green]--allow-architecture-family-alias-override[/]"); AnsiConsole.WriteLine(); } -} \ No newline at end of file +} diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index a3ee559..885c103 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -72,8 +72,7 @@ return; } -string remainingArgsString = string.Join(" ", args.Skip(1)); -List parsedArgs = CliHelpers.ParseArguments(remainingArgsString); +List parsedArgs = CliHelpers.ParseArguments(args.Skip(1)); try { diff --git a/MagicQuant/Services/BenchmarkGpuPlanning.cs b/MagicQuant/Services/BenchmarkGpuPlanning.cs new file mode 100644 index 0000000..661365f --- /dev/null +++ b/MagicQuant/Services/BenchmarkGpuPlanning.cs @@ -0,0 +1,402 @@ +using System.Text.Json; + +namespace MagicQuant.Services; + +internal sealed record GpuProbeSample( + int Ngl, + bool Success, + double SecondsPerPass, + double ElapsedSeconds); + +internal sealed record BenchmarkSlot( + int SlotId, + string ProfileName, + int[] DeviceIndices, + int Q8StableNgl, + IReadOnlyList ProbeSamples) +{ + public BenchmarkSlot(int slotId, int[] deviceIndices) + : this(slotId, "default", deviceIndices, 0, []) + { + } + + public bool UsesGpu => DeviceIndices.Length > 0; + public int DeviceCount => DeviceIndices.Length; + + public string DisplayName => + UsesGpu + ? $"{ProfileName}:GPU[{string.Join(",", DeviceIndices)}]" + : "CPU"; + + public IReadOnlyDictionary? BuildProcessEnv() + { + if (!UsesGpu) + return null; + + string visible = string.Join(",", DeviceIndices); + return new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["CUDA_VISIBLE_DEVICES"] = visible, + ["HIP_VISIBLE_DEVICES"] = visible, + ["ROCR_VISIBLE_DEVICES"] = visible + }; + } +} + +internal sealed record BenchmarkTopologyProfile( + string Name, + IReadOnlyList Slots, + double MeasuredJobsPerSecond, + double MeasuredSecondsPerPass); + +internal sealed class BenchmarkTopologyCacheEnvelope +{ + public int Version { get; set; } = 1; + public int MaxOffloadNgl { get; set; } + public ulong IndependentMaxModelSizeBytes { get; set; } + public BenchmarkTopologyCacheProfile Shared { get; set; } = new(); + public BenchmarkTopologyCacheProfile Independent { get; set; } = new(); +} + +internal sealed class BenchmarkTopologyCacheProfile +{ + public string Name { get; set; } = string.Empty; + public double MeasuredJobsPerSecond { get; set; } + public double MeasuredSecondsPerPass { get; set; } + public List Slots { get; set; } = new(); +} + +internal sealed class BenchmarkTopologyCacheSlot +{ + public int SlotId { get; set; } + public int[] DeviceIndices { get; set; } = []; + public int Q8StableNgl { get; set; } + public List ProbeSamples { get; set; } = new(); +} + +internal static class BenchmarkTopologyCacheCodec +{ + public static string Serialize( + int maxOffloadNgl, + ulong independentMaxModelSizeBytes, + BenchmarkTopologyProfile shared, + BenchmarkTopologyProfile independent) + { + var envelope = new BenchmarkTopologyCacheEnvelope + { + MaxOffloadNgl = maxOffloadNgl, + IndependentMaxModelSizeBytes = independentMaxModelSizeBytes, + Shared = ToCacheProfile(shared), + Independent = ToCacheProfile(independent) + }; + + return JsonSerializer.Serialize(envelope); + } + + public static bool TryDeserialize( + string json, + out int maxOffloadNgl, + out ulong independentMaxModelSizeBytes, + out BenchmarkTopologyProfile? shared, + out BenchmarkTopologyProfile? independent) + { + maxOffloadNgl = 0; + independentMaxModelSizeBytes = 0; + shared = null; + independent = null; + + try + { + var envelope = JsonSerializer.Deserialize(json); + if (envelope == null || + envelope.Version != 1 || + envelope.MaxOffloadNgl <= 0 || + envelope.Shared.Slots.Count == 0) + { + return false; + } + + maxOffloadNgl = envelope.MaxOffloadNgl; + independentMaxModelSizeBytes = envelope.IndependentMaxModelSizeBytes; + shared = FromCacheProfile(envelope.Shared); + independent = FromCacheProfile(envelope.Independent); + return true; + } + catch (JsonException) + { + return false; + } + } + + private static BenchmarkTopologyCacheProfile ToCacheProfile(BenchmarkTopologyProfile profile) + => new() + { + Name = profile.Name, + MeasuredJobsPerSecond = profile.MeasuredJobsPerSecond, + MeasuredSecondsPerPass = profile.MeasuredSecondsPerPass, + Slots = profile.Slots.Select(x => new BenchmarkTopologyCacheSlot + { + SlotId = x.SlotId, + DeviceIndices = x.DeviceIndices, + Q8StableNgl = x.Q8StableNgl, + ProbeSamples = x.ProbeSamples.ToList() + }).ToList() + }; + + private static BenchmarkTopologyProfile FromCacheProfile(BenchmarkTopologyCacheProfile profile) + => new( + string.IsNullOrWhiteSpace(profile.Name) ? "cached" : profile.Name, + profile.Slots.Select(x => new BenchmarkSlot( + x.SlotId, + string.IsNullOrWhiteSpace(profile.Name) ? "cached" : profile.Name, + x.DeviceIndices ?? [], + x.Q8StableNgl, + x.ProbeSamples ?? [])).ToList(), + profile.MeasuredJobsPerSecond, + profile.MeasuredSecondsPerPass); +} + +internal static class BenchmarkGpuPlanner +{ + internal const double DefaultIndependentSpeedupMargin = 1.10d; + + public static int ResolveNglForModel( + ulong q8ModelSizeBytes, + int q8StableNgl, + int maxOffloadNgl, + ulong modelSizeBytes) + { + if (q8StableNgl <= 0 || maxOffloadNgl <= 0) + return 0; + if (q8ModelSizeBytes == 0 || modelSizeBytes == 0) + return Math.Min(q8StableNgl, maxOffloadNgl); + + double scaled = Math.Floor(q8StableNgl * (q8ModelSizeBytes / (double)modelSizeBytes)); + return (int)Math.Clamp(scaled, 0d, maxOffloadNgl); + } + + public static ulong EstimateIndependentCrossoverBytes( + ulong q8ModelSizeBytes, + int maxOffloadNgl, + double sharedSecondsPerPass, + IReadOnlyList independentSlots, + double requiredSpeedup = DefaultIndependentSpeedupMargin) + { + if (q8ModelSizeBytes == 0 || + maxOffloadNgl <= 0 || + sharedSecondsPerPass <= 0 || + independentSlots.Count < 2 || + requiredSpeedup < 1d) + { + return 0; + } + + var models = independentSlots + .Select(BuildPassTimeModel) + .ToArray(); + + if (models.Any(x => x == null)) + return 0; + + double requiredJobsPerSecond = (1d / sharedSecondsPerPass) * requiredSpeedup; + const int scanSteps = 1000; + + // Search from largest to smallest so the returned boundary is the first model size + // where independent workers have a meaningful, not merely noise-level, advantage. + for (int step = 0; step <= scanSteps; step++) + { + double sizeRatio = 1d - (0.8d * step / scanSteps); + ulong modelSize = (ulong)Math.Max(1d, Math.Floor(q8ModelSizeBytes * sizeRatio)); + double aggregateJobsPerSecond = 0d; + + for (int i = 0; i < independentSlots.Count; i++) + { + var slot = independentSlots[i]; + int ngl = ResolveNglForModel( + q8ModelSizeBytes, + slot.Q8StableNgl, + maxOffloadNgl, + modelSize); + + double seconds = models[i]!.Value.EstimateSeconds(ngl); + if (seconds <= 0 || double.IsNaN(seconds) || double.IsInfinity(seconds)) + { + aggregateJobsPerSecond = 0d; + break; + } + + aggregateJobsPerSecond += 1d / seconds; + } + + if (aggregateJobsPerSecond >= requiredJobsPerSecond) + return modelSize; + } + + return 0; + } + + private static PassTimeModel? BuildPassTimeModel(BenchmarkSlot slot) + { + var successful = slot.ProbeSamples + .Where(x => x.Success && x.Ngl > 0 && x.SecondsPerPass > 0) + .GroupBy(x => x.Ngl) + .Select(x => new + { + Ngl = x.Key, + Seconds = x.Average(y => y.SecondsPerPass) + }) + .OrderBy(x => x.Ngl) + .ToArray(); + + if (successful.Length < 2) + return null; + + double meanX = successful.Average(x => (double)x.Ngl); + double meanY = successful.Average(x => x.Seconds); + double denominator = successful.Sum(x => Math.Pow(x.Ngl - meanX, 2)); + if (denominator <= double.Epsilon) + return null; + + double slope = successful.Sum(x => (x.Ngl - meanX) * (x.Seconds - meanY)) / denominator; + if (slope >= 0) + return null; + + double intercept = meanY - slope * meanX; + double minimumObserved = successful.Min(x => x.Seconds); + return new PassTimeModel(intercept, slope, Math.Max(0.05d, minimumObserved * 0.55d)); + } + + private readonly record struct PassTimeModel(double Intercept, double Slope, double MinimumSeconds) + { + public double EstimateSeconds(int ngl) => Math.Max(MinimumSeconds, Intercept + Slope * ngl); + } +} + +internal sealed class GpuResourceScheduler +{ + private readonly object _sync = new(); + private readonly HashSet _busyDevices = new(); + private readonly LinkedList _waiters = new(); + + public ValueTask AcquireAsync( + IReadOnlyList candidates, + CancellationToken ct = default) + { + ArgumentNullException.ThrowIfNull(candidates); + if (candidates.Count == 0) + throw new ArgumentException("At least one benchmark slot candidate is required.", nameof(candidates)); + + lock (_sync) + { + if (_waiters.Count == 0 && TryReserveFirstAvailable(candidates, out var immediate)) + return ValueTask.FromResult(new GpuResourceLease(this, immediate!)); + + var waiter = new Waiter(candidates); + waiter.Node = _waiters.AddLast(waiter); + + if (ct.CanBeCanceled) + { + waiter.Cancellation = ct.Register( + static state => + { + var pair = ((GpuResourceScheduler Scheduler, Waiter Waiter))state!; + pair.Scheduler.Cancel(pair.Waiter); + }, + (this, waiter)); + } + + return new ValueTask(waiter.Completion.Task); + } + } + + private void Cancel(Waiter waiter) + { + lock (_sync) + { + if (waiter.Node?.List == null) + return; + + _waiters.Remove(waiter.Node); + waiter.Node = null; + waiter.Cancellation.Dispose(); + waiter.Completion.TrySetCanceled(); + } + } + + private bool TryReserveFirstAvailable( + IReadOnlyList candidates, + out BenchmarkSlot? selected) + { + selected = candidates.FirstOrDefault(slot => + ReservationKeys(slot).All(device => !_busyDevices.Contains(device))); + + if (selected == null) + return false; + + foreach (int device in ReservationKeys(selected)) + _busyDevices.Add(device); + + return true; + } + + private void Release(BenchmarkSlot slot) + { + List<(Waiter Waiter, BenchmarkSlot Slot)> ready = new(); + + lock (_sync) + { + foreach (int device in ReservationKeys(slot)) + _busyDevices.Remove(device); + + while (_waiters.First != null) + { + var waiter = _waiters.First.Value; + if (!TryReserveFirstAvailable(waiter.Candidates, out var selected)) + break; + + _waiters.RemoveFirst(); + waiter.Node = null; + waiter.Cancellation.Dispose(); + ready.Add((waiter, selected!)); + } + } + + foreach (var item in ready) + item.Waiter.Completion.TrySetResult(new GpuResourceLease(this, item.Slot)); + } + + private static IEnumerable ReservationKeys(BenchmarkSlot slot) => + slot.DeviceIndices.Length == 0 ? [-1] : slot.DeviceIndices; + + private sealed class Waiter + { + public IReadOnlyList Candidates { get; } + public TaskCompletionSource Completion { get; } = + new(TaskCreationOptions.RunContinuationsAsynchronously); + public LinkedListNode? Node { get; set; } + public CancellationTokenRegistration Cancellation { get; set; } + + public Waiter(IReadOnlyList candidates) + { + Candidates = candidates; + } + } + + internal sealed class GpuResourceLease : IAsyncDisposable + { + private GpuResourceScheduler? _owner; + public BenchmarkSlot Slot { get; } + + internal GpuResourceLease(GpuResourceScheduler owner, BenchmarkSlot slot) + { + _owner = owner; + Slot = slot; + } + + public ValueTask DisposeAsync() + { + Interlocked.Exchange(ref _owner, null)?.Release(Slot); + return ValueTask.CompletedTask; + } + } +} diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 9749084..7c4bd55 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -16,6 +16,7 @@ namespace MagicQuant.Services; public class BenchmarkService { private readonly LlamaBinaries _bins; + private readonly GgufMetadataReader _ggufMetadataReader; public readonly PythonManager _pyManager; private static readonly string[] BaseDomains = { "general", "code", "math" }; @@ -32,10 +33,11 @@ public class BenchmarkService "cuda error" }; - private static readonly int[] NglCandidates = { 35, 30, 24, 20, 16, 12, 8, 4 }; - // Version 3 invalidates plans discovered with the legacy root-level dataset IDs, - // which could silently create an empty corpus and cache an incorrect CPU fallback. - private const int DynamicProbeSchemaVersion = 3; + private static readonly int[] LegacyNglFallbacks = { 35, 30, 24, 20, 16, 12, 8, 4 }; + + // Version 4 adds measured shared/independent GPU topology profiles, per-device Q8 + // anchors, exact model-layer ceilings, and the llama.cpp binary fingerprint. + private const int DynamicProbeSchemaVersion = 4; private const int PplCharsPerTokenEstimate = 4; internal const string GeneralPplDatasetId = "Salesforce/wikitext"; internal const string MathPplDatasetId = "openai/gsm8k"; @@ -49,12 +51,22 @@ public class BenchmarkService private static BenchmarkExecutionPlan? _currentPlan; private static string _currentPlanQuantizationKey = "Q8_0"; - private static Queue _availableSlots = new(); - private static SemaphoreSlim? _slotSemaphore; + private static GpuResourceScheduler _resourceScheduler = new(); public int CurrentParallelSlotCount { - get { lock (SlotSync) return _currentPlan?.Slots.Count ?? 1; } + get + { + lock (SlotSync) + { + if (_currentPlan == null) + return 1; + + return Math.Max( + 1, + Math.Max(_currentPlan.SharedProfile.Slots.Count, _currentPlan.IndependentProfile.Slots.Count)); + } + } } // ---------------------------------------------------------------- @@ -66,6 +78,7 @@ public BenchmarkService(PythonManager pyManager) _bins = new LlamaBinaries(Cache.LlamaRoot); _bins.Validate(); _pyManager = pyManager; + _ggufMetadataReader = new GgufMetadataReader(pyManager); } // ---------------------------------------------------------------- @@ -159,8 +172,7 @@ public async Task EnsureDynamicExecutionPlanAsync( { _currentPlan = plan; _currentPlanQuantizationKey = normalizedQuantizationKey; - _availableSlots = new Queue(plan.Slots); - _slotSemaphore = new SemaphoreSlim(plan.Slots.Count, plan.Slots.Count); + _resourceScheduler = new GpuResourceScheduler(); } AnsiConsole.Write(new Rule("[yellow]Benchmark Execution Plan[/]") { Justification = Justify.Left }); @@ -169,7 +181,12 @@ public async Task EnsureDynamicExecutionPlanAsync( AnsiConsole.MarkupLine($"[green]Native anchor:[/] [cyan]{(plan.NativeModelSizeBytes / 1024d / 1024d / 1024d):F2} GB @ ngl={plan.NativeStableNgl}[/]"); AnsiConsole.MarkupLine($"[green]Uses GPU:[/] [cyan]{plan.UsesGpu}[/]"); AnsiConsole.MarkupLine($"[green]GPU group size:[/] [cyan]{plan.GroupSize}[/]"); - AnsiConsole.MarkupLine($"[green]Parallel benchmark Slots:[/] [cyan]{plan.Slots.Count}[/]"); + AnsiConsole.MarkupLine($"[green]Max parallel benchmark slots:[/] [cyan]{CurrentParallelSlotCount}[/]"); + if (plan.IndependentMaxModelSizeBytes > 0) + { + AnsiConsole.MarkupLine( + $"[green]Independent-worker crossover:[/] [cyan]{plan.IndependentMaxModelSizeBytes / 1024d / 1024d / 1024d:F2} GB[/]"); + } AnsiConsole.MarkupLine($"[green]Quantization key:[/] [cyan]{Markup.Escape(normalizedQuantizationKey)}[/]"); if (Cache.GpuMemoryLimitsGb.Count == 0) { @@ -181,10 +198,11 @@ public async Task EnsureDynamicExecutionPlanAsync( AnsiConsole.MarkupLine($"[green]GPU memory limits:[/] [cyan]{Markup.Escape(limits)}[/]"); } - foreach (var slot in plan.Slots) + foreach (var slot in plan.SharedProfile.Slots.Concat(plan.IndependentProfile.Slots)) { - AnsiConsole.MarkupLine($" [grey]Slot {slot.SlotId}:[/] {Markup.Escape(slot.DisplayName)}"); - string tensorSplit = BuildTensorSplitArgs(slot); + AnsiConsole.MarkupLine( + $" [grey]Slot {slot.SlotId}:[/] {Markup.Escape(slot.DisplayName)} @ Q8 ngl={slot.Q8StableNgl}"); + string tensorSplit = BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli); if (!string.IsNullOrWhiteSpace(tensorSplit)) { AnsiConsole.MarkupLine($" [grey]tensor split:[/] {Markup.Escape(tensorSplit.Trim())}"); @@ -247,8 +265,7 @@ public async Task TryInitializeDynamicExecutionPlanFromCacheAsync( { _currentPlan = plan; _currentPlanQuantizationKey = normalizedQuantizationKey; - _availableSlots = new Queue(plan.Slots); - _slotSemaphore = new SemaphoreSlim(plan.Slots.Count, plan.Slots.Count); + _resourceScheduler = new GpuResourceScheduler(); } AnsiConsole.MarkupLine("[green]Loaded benchmark execution plan from SQLite cache (no Q8 rebuild needed).[/]"); @@ -295,7 +312,7 @@ public async Task ClampStaticNglWithBaseModelAsync( AnsiConsole.MarkupLine($"[grey]Base model:[/] {Markup.Escape(baseModelPath)}"); AnsiConsole.MarkupLine($"[grey]Starting from Q8-discovered ngl:[/] [cyan]{startingNgl}[/]"); - foreach (int ngl in NglCandidates.Where(n => n <= startingNgl).OrderByDescending(n => n)) + foreach (int ngl in BuildNglFallbackList(startingNgl).Where(n => n > 0)) { ct.ThrowIfCancellationRequested(); @@ -340,8 +357,7 @@ public async Task ClampStaticNglWithBaseModelAsync( lock (SlotSync) { _currentPlan = cpuPlan; - _availableSlots = new Queue(cpuPlan.Slots); - _slotSemaphore = new SemaphoreSlim(cpuPlan.Slots.Count, cpuPlan.Slots.Count); + _resourceScheduler = new GpuResourceScheduler(); } var cacheKeyCpu = BuildExecutionPlanCacheKey( @@ -360,8 +376,7 @@ public async Task ClampStaticNglWithBaseModelAsync( lock (SlotSync) { _currentPlan = updated; - _availableSlots = new Queue(updated.Slots); - _slotSemaphore = new SemaphoreSlim(updated.Slots.Count, updated.Slots.Count); + _resourceScheduler = new GpuResourceScheduler(); } } @@ -398,19 +413,27 @@ private async Task BuildDynamicExecutionPlanAsync( NativeModelSizeBytes = TryGetModelSize(nativeModelPath), NativeStableNgl = 0, NativeQuantizationKey = nativeQuantizationKey, - MaxCandidateNgl = NglCandidates.Max(), + MaxCandidateNgl = plan.MaxCandidateNgl, GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), TensorSplitJson = SerializeTensorSplitMap(plan.Slots) }; } - var probeSlot = BuildAllGpuSlotFromSystemInfo(); - int? nativeStableNgl = await ProbeHighestStableNglAsync( + string nativeProbeRoot = Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe_native"); + Directory.CreateDirectory(nativeProbeRoot); + string nativeCorpusDir = Path.Combine(nativeProbeRoot, "_ppl_corpora"); + Directory.CreateDirectory(nativeCorpusDir); + string nativeCorpusPath = Path.Combine(nativeCorpusDir, "ppl_corpus_general.txt"); + await PreparePplCorpusAsync("general", nativeCorpusPath, discoveryTokenTarget); + + BenchmarkSlot? nativeProbeSlot = await ProbeSlotCapacityAsync( nativeModelPath, - probeSlot, - Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe_native"), - discoveryTokenTarget, + plan.Slots[0] with { ProbeSamples = [] }, + plan.MaxCandidateNgl, + nativeCorpusPath, + nativeProbeRoot, ct); + int? nativeStableNgl = nativeProbeSlot?.Q8StableNgl; if (!nativeStableNgl.HasValue || nativeStableNgl.Value <= 0) { @@ -424,7 +447,7 @@ private async Task BuildDynamicExecutionPlanAsync( NativeModelSizeBytes = TryGetModelSize(nativeModelPath), NativeStableNgl = 0, NativeQuantizationKey = nativeQuantizationKey, - MaxCandidateNgl = NglCandidates.Max(), + MaxCandidateNgl = plan.MaxCandidateNgl, GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), TensorSplitJson = SerializeTensorSplitMap(plan.Slots) }; @@ -438,7 +461,7 @@ private async Task BuildDynamicExecutionPlanAsync( NativeModelSizeBytes = TryGetModelSize(nativeModelPath), NativeStableNgl = nativeStableNgl.Value, NativeQuantizationKey = nativeQuantizationKey, - MaxCandidateNgl = NglCandidates.Max(), + MaxCandidateNgl = plan.MaxCandidateNgl, GpuMemoryLimitsJson = SerializeGpuMemoryLimits(), TensorSplitJson = SerializeTensorSplitMap(plan.Slots) }; @@ -457,85 +480,275 @@ private async Task BuildExecutionPlanAsync( return BenchmarkExecutionPlan.CreateCpuPlan(q8ModelPath); } - var allGpuIndices = Enumerable.Range(0, gpuCount).ToArray(); - var allGpuSlot = new BenchmarkSlot(0, allGpuIndices); - _ = BuildTensorSplitArgs(allGpuSlot); - string probeRoot = Path.Combine(Cache.ModelMagicQuantDirectory!, "_benchmark_plan_probe"); Directory.CreateDirectory(probeRoot); - int? targetNgl = await ProbeHighestStableNglAsync( + var metadata = await _ggufMetadataReader.ReadAsync(q8ModelPath, probeRoot, ct); + int maxOffloadNgl = metadata.BlockCount is > 0 + ? checked(metadata.BlockCount.Value + 1) + : throw new InvalidOperationException( + "Q8 GGUF metadata did not expose a positive architecture block_count; " + + "an exact full-offload ceiling cannot be planned safely."); + + string probeCorpusDir = Path.Combine(probeRoot, "_ppl_corpora"); + Directory.CreateDirectory(probeCorpusDir); + string corpusPath = Path.Combine(probeCorpusDir, "ppl_corpus_general.txt"); + await PreparePplCorpusAsync("general", corpusPath, discoveryTokenTarget); + + var allGpuIndices = Enumerable.Range(0, gpuCount).ToArray(); + var sharedSeed = new BenchmarkSlot(0, "shared", allGpuIndices, 0, []); + _ = BuildTensorSplitArgs(sharedSeed, LlamaGpuTool.CommonCli); + + BenchmarkSlot? sharedSlot = await ProbeSlotCapacityAsync( q8ModelPath, - allGpuSlot, + sharedSeed, + maxOffloadNgl, + corpusPath, probeRoot, - discoveryTokenTarget, ct); - if (!targetNgl.HasValue || targetNgl.Value <= 0) + if (sharedSlot == null || sharedSlot.Q8StableNgl <= 0) { AnsiConsole.MarkupLine( "[yellow]Q8 discovery could not establish a stable GPU ngl. Falling back to a single CPU slot.[/]"); return BenchmarkExecutionPlan.CreateCpuPlan(q8ModelPath); } - foreach (var groupSize in GetCandidateGroupSizes(gpuCount)) + var independentSlots = new List(); + if (gpuCount > 1) { - var groups = BuildContiguousGroups(allGpuIndices, groupSize); - var slots = new List(); - - bool allGroupsPass = true; - for (int i = 0; i < groups.Count; i++) + for (int gpuIndex = 0; gpuIndex < gpuCount; gpuIndex++) { - var slot = new BenchmarkSlot(i, groups[i]); - _ = BuildTensorSplitArgs(slot); - - bool ok = await ValidateSlotForFixedPlanAsync( + var seed = new BenchmarkSlot(gpuIndex, "independent", [gpuIndex], 0, []); + BenchmarkSlot? discovered = await ProbeSlotCapacityAsync( q8ModelPath, - slot, - targetNgl.Value, + seed, + maxOffloadNgl, + corpusPath, probeRoot, - discoveryTokenTarget, ct); - if (!ok) + if (discovered == null || discovered.Q8StableNgl <= 0) { - allGroupsPass = false; + AnsiConsole.MarkupLine( + $"[yellow]Independent slot GPU[{gpuIndex}] was not stable; independent topology disabled.[/]"); + independentSlots.Clear(); break; } - slots.Add(slot); + independentSlots.Add(discovered); } + } - if (allGroupsPass && slots.Count > 0) - { - return new BenchmarkExecutionPlan( - PlanModelPath: q8ModelPath, - StaticNgl: targetNgl.Value, - UsesGpu: true, - GroupSize: groupSize, - Slots: slots); - } + BenchmarkTopologyProfile sharedProfile = await MeasureTopologyProfileAsync( + "shared", q8ModelPath, [sharedSlot], corpusPath, probeRoot, ct); + + if (sharedProfile.Slots.Count == 0) + { + AnsiConsole.MarkupLine( + "[yellow]Shared GPU topology failed its concurrent throughput validation. Falling back to CPU.[/]"); + return BenchmarkExecutionPlan.CreateCpuPlan(q8ModelPath); + } + + BenchmarkTopologyProfile independentProfile = independentSlots.Count > 1 + ? await MeasureTopologyProfileAsync( + "independent", q8ModelPath, independentSlots, corpusPath, probeRoot, ct) + : new BenchmarkTopologyProfile("independent", [], 0, 0); + + ulong q8Size = TryGetModelSize(q8ModelPath); + ulong independentMaxModelSizeBytes = independentProfile.Slots.Count > 1 + ? BenchmarkGpuPlanner.EstimateIndependentCrossoverBytes( + q8Size, + maxOffloadNgl, + sharedProfile.MeasuredSecondsPerPass, + independentProfile.Slots) + : 0; + + if (independentMaxModelSizeBytes > 0) + { + AnsiConsole.MarkupLine( + $"[green]Measured topology crossover:[/] models up to " + + $"[cyan]{independentMaxModelSizeBytes / 1024d / 1024d / 1024d:F2} GB[/] use independent GPU workers; larger models use shared GPUs."); } return new BenchmarkExecutionPlan( PlanModelPath: q8ModelPath, - StaticNgl: targetNgl.Value, + StaticNgl: sharedSlot.Q8StableNgl, UsesGpu: true, GroupSize: gpuCount, - Slots: new List { allGpuSlot }); + Slots: sharedProfile.Slots, + MaxCandidateNgl: maxOffloadNgl, + IndependentSlots: independentProfile.Slots, + IndependentMaxModelSizeBytes: independentMaxModelSizeBytes, + SharedMeasuredJobsPerSecond: sharedProfile.MeasuredJobsPerSecond, + SharedMeasuredSecondsPerPass: sharedProfile.MeasuredSecondsPerPass, + IndependentMeasuredJobsPerSecond: independentProfile.MeasuredJobsPerSecond, + IndependentMeasuredSecondsPerPass: independentProfile.MeasuredSecondsPerPass); } - private static BenchmarkSlot BuildAllGpuSlotFromSystemInfo() + private async Task ProbeSlotCapacityAsync( + string modelPath, + BenchmarkSlot seed, + int maxOffloadNgl, + string corpusPath, + string probeRoot, + CancellationToken ct) { - int gpuCount = Cache.SysInfo?.GpuInfo? - .Count(x => x.GpuVendor != GpuVendor.Cpu && x.GpuVendor != GpuVendor.Unknown) ?? 0; + var samples = new List(); - if (gpuCount <= 0) - return new BenchmarkSlot(0, Array.Empty()); + async Task Probe(int ngl) + { + var sample = await ProbePerplexitySampleAsync( + modelPath, seed, ngl, corpusPath, probeRoot, "capacity", ct); + samples.Add(sample); + return sample; + } + + AnsiConsole.MarkupLine( + $"[grey]Capacity probe:[/] {Markup.Escape(seed.DisplayName)} full-offload ngl={maxOffloadNgl}"); + + var full = await Probe(maxOffloadNgl); + int stableNgl; + + if (full.Success) + { + stableNgl = maxOffloadNgl; + int lowerNgl = Math.Max(1, (int)Math.Floor(maxOffloadNgl * 0.72d)); + if (lowerNgl < stableNgl) + await Probe(lowerNgl); + } + else + { + int low = 0; + int high = maxOffloadNgl - 1; + + while (low < high) + { + ct.ThrowIfCancellationRequested(); + int candidate = low + ((high - low + 1) / 2); + var sample = await Probe(candidate); + if (sample.Success) + low = candidate; + else + high = candidate - 1; + } + + stableNgl = low; + } - var slot = new BenchmarkSlot(0, Enumerable.Range(0, gpuCount).ToArray()); - _ = BuildTensorSplitArgs(slot); - return slot; + if (stableNgl <= 0) + return null; + + int successfulDistinct = samples.Where(x => x.Success).Select(x => x.Ngl).Distinct().Count(); + if (successfulDistinct < 2) + { + int lowerNgl = Math.Max(1, (int)Math.Floor(stableNgl * 0.72d)); + if (lowerNgl < stableNgl) + await Probe(lowerNgl); + } + + AnsiConsole.MarkupLine( + $"[green]Stable capacity:[/] {Markup.Escape(seed.DisplayName)} ngl={stableNgl}/{maxOffloadNgl}"); + + return seed with + { + Q8StableNgl = stableNgl, + ProbeSamples = samples + }; + } + + private async Task MeasureTopologyProfileAsync( + string profileName, + string modelPath, + IReadOnlyList slots, + string corpusPath, + string probeRoot, + CancellationToken ct) + { + var stopwatch = Stopwatch.StartNew(); + var tasks = slots.Select(slot => ProbePerplexitySampleAsync( + modelPath, + slot, + slot.Q8StableNgl, + corpusPath, + probeRoot, + $"throughput_{profileName}", + ct)); + + GpuProbeSample[] samples = await Task.WhenAll(tasks); + stopwatch.Stop(); + + if (samples.Any(x => !x.Success)) + { + AnsiConsole.MarkupLine( + $"[yellow]Topology throughput validation failed for {Markup.Escape(profileName)}.[/]"); + return new BenchmarkTopologyProfile(profileName, [], 0, 0); + } + + double jobsPerSecond = slots.Count / Math.Max(0.001d, stopwatch.Elapsed.TotalSeconds); + double secondsPerPass = samples.Average(x => x.SecondsPerPass); + AnsiConsole.MarkupLine( + $"[green]Topology throughput:[/] {Markup.Escape(profileName)} = " + + $"[cyan]{jobsPerSecond:F4} jobs/s[/], {secondsPerPass:F2} s/pass"); + + return new BenchmarkTopologyProfile( + profileName, + slots, + jobsPerSecond, + secondsPerPass); + } + + private async Task ProbePerplexitySampleAsync( + string modelPath, + BenchmarkSlot slot, + int fixedNgl, + string corpusPath, + string probeRoot, + string phase, + CancellationToken ct) + { + ct.ThrowIfCancellationRequested(); + + string devices = slot.DeviceIndices.Length == 0 + ? "cpu" + : string.Join("-", slot.DeviceIndices); + string logFile = Path.Combine( + probeRoot, + $"probe_ppl_{phase}_{slot.ProfileName}_gpu{devices}_ngl{fixedNgl}.log"); + + string cmd = slot.UsesGpu + ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli)} -t 4 -c 2048 --file \"{corpusPath}\"" + : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\""; + + var stopwatch = Stopwatch.StartNew(); + var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); + stopwatch.Stop(); + + if (!result.Success) + return new GpuProbeSample(fixedNgl, false, 0, stopwatch.Elapsed.TotalSeconds); + + try + { + var parsed = ParsePerplexity(logFile, allowMissingKld: true); + string clean = StripAnsi(result.LogOutput); + var passMatch = Regex.Match( + clean, + @"([0-9]+(?:\.[0-9]+)?)\s+seconds per pass", + RegexOptions.IgnoreCase); + double secondsPerPass = passMatch.Success + ? double.Parse(passMatch.Groups[1].Value, CultureInfo.InvariantCulture) + : stopwatch.Elapsed.TotalSeconds; + + return new GpuProbeSample( + fixedNgl, + parsed.Ppl > 0, + secondsPerPass, + stopwatch.Elapsed.TotalSeconds); + } + catch + { + return new GpuProbeSample(fixedNgl, false, 0, stopwatch.Elapsed.TotalSeconds); + } } private async Task TryLoadCachedExecutionPlanAsync( @@ -611,27 +824,34 @@ private static BenchmarkSlot BuildAllGpuSlotFromSystemInfo() } } - List slotDevices; - try + if (!row.UsesGpu) { - slotDevices = JsonSerializer.Deserialize>(row.SlotsJson) ?? new List(); + return BenchmarkExecutionPlan.CreateCpuPlan(key.PlanModelPath) with + { + ProbeSchemaVersion = row.ProbeSchemaVersion, + Q8ModelSizeBytes = row.Q8ModelSizeBytes, + NativeModelSizeBytes = row.NativeModelSizeBytes, + NativeQuantizationKey = row.NativeQuantizationKey ?? string.Empty, + GpuMemoryLimitsJson = row.GpuMemoryLimitsJson ?? "{}" + }; } - catch + + if (!BenchmarkTopologyCacheCodec.TryDeserialize( + row.SlotsJson, + out int maxOffloadNgl, + out ulong independentMaxModelSizeBytes, + out var sharedProfile, + out var independentProfile) || + sharedProfile == null || + independentProfile == null) { - AnsiConsole.MarkupLine("[yellow]Execution-plan cache row was unreadable (slot JSON parse failed). Re-probing.[/]"); + AnsiConsole.MarkupLine("[yellow]Execution-plan cache topology JSON was unreadable. Re-probing.[/]"); return null; } - if (slotDevices.Count == 0) - return null; - - var slots = slotDevices - .Select((devices, idx) => new BenchmarkSlot(idx, devices ?? Array.Empty())) - .ToList(); - - foreach (var slot in slots) + foreach (var slot in sharedProfile.Slots.Concat(independentProfile.Slots)) { - _ = BuildTensorSplitArgs(slot); + _ = BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli); } // NativeStableNgl == 0 is valid and represents "native anchor unavailable" @@ -648,16 +868,22 @@ private static BenchmarkSlot BuildAllGpuSlotFromSystemInfo() StaticNgl: row.StaticNgl, UsesGpu: row.UsesGpu, GroupSize: row.GroupSize, - Slots: slots, + Slots: sharedProfile.Slots, ProbeSchemaVersion: row.ProbeSchemaVersion, Q8ModelSizeBytes: row.Q8ModelSizeBytes, Q8StableNgl: row.Q8StableNgl, NativeModelSizeBytes: row.NativeModelSizeBytes, NativeStableNgl: row.NativeStableNgl, NativeQuantizationKey: row.NativeQuantizationKey ?? string.Empty, - MaxCandidateNgl: row.MaxCandidateNgl > 0 ? row.MaxCandidateNgl : NglCandidates.Max(), + MaxCandidateNgl: maxOffloadNgl, GpuMemoryLimitsJson: row.GpuMemoryLimitsJson ?? "{}", - TensorSplitJson: row.TensorSplitJson ?? "{}"); + TensorSplitJson: row.TensorSplitJson ?? "{}", + IndependentSlots: independentProfile.Slots, + IndependentMaxModelSizeBytes: independentMaxModelSizeBytes, + SharedMeasuredJobsPerSecond: sharedProfile.MeasuredJobsPerSecond, + SharedMeasuredSecondsPerPass: sharedProfile.MeasuredSecondsPerPass, + IndependentMeasuredJobsPerSecond: independentProfile.MeasuredJobsPerSecond, + IndependentMeasuredSecondsPerPass: independentProfile.MeasuredSecondsPerPass); } private async Task UpsertCachedExecutionPlanAsync( @@ -682,7 +908,13 @@ private async Task UpsertCachedExecutionPlanAsync( x.QuantizationKey == key.QuantizationKey && x.DiscoveryTokenTarget == key.DiscoveryTokenTarget, ct); - string slotsJson = JsonSerializer.Serialize(plan.Slots.Select(x => x.DeviceIndices).ToList()); + string slotsJson = plan.UsesGpu + ? BenchmarkTopologyCacheCodec.Serialize( + plan.MaxCandidateNgl, + plan.IndependentMaxModelSizeBytes, + plan.SharedProfile, + plan.IndependentProfile) + : "[]"; var now = DateTime.UtcNow; if (existing == null) @@ -730,12 +962,13 @@ private static ExecutionPlanCacheKey BuildExecutionPlanCacheKey( var sys = Cache.SysInfo; string hardwareFingerprint = sys == null - ? "unknown-hardware" + ? $"unknown-hardware|llama:{BuildLlamaRuntimeFingerprint()}" : string.Join("|", new[] { $"threads:{sys.ThreadCount}", $"ram:{sys.RamGb:F2}", - $"gpu:{string.Join(";", sys.GpuInfo.Select(g => $"{g.GpuVendor}:{g.GpuName}:{g.VramGb:F2}:{g.UniqueId ?? "none"}"))}" + $"gpu:{string.Join(";", sys.GpuInfo.Select(g => $"{g.GpuVendor}:{g.GpuName}:{g.VramGb:F2}:{g.UniqueId ?? "none"}"))}", + $"llama:{BuildLlamaRuntimeFingerprint()}" }); return new ExecutionPlanCacheKey( @@ -746,6 +979,23 @@ private static ExecutionPlanCacheKey BuildExecutionPlanCacheKey( planModelPath); } + private static string BuildLlamaRuntimeFingerprint() + { + try + { + var bins = new LlamaBinaries(Cache.LlamaRoot); + var ppl = new FileInfo(bins.Ppl); + if (!ppl.Exists) + return "missing"; + + return $"ppl:{ppl.Length}:{ppl.LastWriteTimeUtc.Ticks}"; + } + catch + { + return "unknown"; + } + } + private static string BuildQuantizedModelFingerprint(string quantizationKey) { if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) @@ -770,33 +1020,25 @@ private static string SerializeTensorSplitMap(IReadOnlyList slots .Where(s => s.UsesGpu && s.DeviceIndices.Length > 1) .ToDictionary( s => s.DisplayName, - s => BuildTensorSplitArgs(s).Trim(), + s => BuildTensorSplitArgs(s, LlamaGpuTool.CommonCli).Trim(), StringComparer.Ordinal); return JsonSerializer.Serialize(map); } - private static string BuildTensorSplitArgs(BenchmarkSlot slot) + private static string BuildTensorSplitArgs(BenchmarkSlot slot, LlamaGpuTool tool) { - if (!slot.UsesGpu || slot.DeviceIndices.Length <= 1) - return string.Empty; - - if (Cache.GpuMemoryLimitsGb.Count == 0) - return string.Empty; - - var missing = slot.DeviceIndices - .Where(i => !Cache.GpuMemoryLimitsGb.ContainsKey(i)) - .ToArray(); - - if (missing.Length > 0) + try + { + return LlamaGpuArgumentBuilder.BuildTensorSplitArgs( + slot.DeviceIndices, + Cache.GpuMemoryLimitsGb, + tool); + } + catch (InvalidOperationException ex) { throw new InvalidOperationException( - $"GPU memory limits were configured, but slot {slot.DisplayName} is missing limits for GPU(s): {string.Join(", ", missing)}."); + $"Invalid tensor split for slot {slot.DisplayName}: {ex.Message}", ex); } - - string split = string.Join(",", slot.DeviceIndices.Select(i => - Cache.GpuMemoryLimitsGb[i].ToString("0.###", CultureInfo.InvariantCulture))); - - return $" --tensor-split {split}"; } private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext db, CancellationToken ct) @@ -815,69 +1057,6 @@ private static async Task GetOrCreateAiModelHashIdAsync(MagicQuantContext return model.Id; } - private async Task ProbeHighestStableNglAsync( - string modelPath, - BenchmarkSlot slot, - string probeRoot, - int tokenTarget, - CancellationToken ct) - { - string probeCorpusDir = Path.Combine(probeRoot, "_ppl_corpora"); - Directory.CreateDirectory(probeCorpusDir); - - string corpusPath = Path.Combine(probeCorpusDir, "ppl_corpus_general.txt"); - await PreparePplCorpusAsync("general", corpusPath, tokenTarget); - - foreach (int ngl in NglCandidates) - { - ct.ThrowIfCancellationRequested(); - - AnsiConsole.MarkupLine( - $"[grey]Plan probe:[/] testing [cyan]{Markup.Escape(slot.DisplayName)}[/] at [cyan]ngl={ngl}[/]"); - - bool benchOk = await ProbeLlamaBenchAtFixedNglAsync(modelPath, slot, ngl, probeRoot); - if (!benchOk) - { - AnsiConsole.MarkupLine($"[grey] llama-bench failed at ngl={ngl}[/]"); - continue; - } - - bool pplOk = await ProbePerplexityAtFixedNglAsync(modelPath, slot, ngl, corpusPath, probeRoot); - if (!pplOk) - { - AnsiConsole.MarkupLine($"[grey] perplexity failed at ngl={ngl}[/]"); - continue; - } - - AnsiConsole.MarkupLine($"[green] stable ngl discovered:[/] [cyan]{ngl}[/]"); - return ngl; - } - - return null; - } - - private async Task ValidateSlotForFixedPlanAsync( - string modelPath, - BenchmarkSlot slot, - int fixedNgl, - string probeRoot, - int tokenTarget, - CancellationToken ct) - { - string probeCorpusDir = Path.Combine(probeRoot, "_ppl_corpora"); - Directory.CreateDirectory(probeCorpusDir); - - string corpusPath = Path.Combine(probeCorpusDir, "ppl_corpus_general.txt"); - await PreparePplCorpusAsync("general", corpusPath, tokenTarget); - - bool benchOk = await ProbeLlamaBenchAtFixedNglAsync(modelPath, slot, fixedNgl, probeRoot); - if (!benchOk) - return false; - - bool pplOk = await ProbePerplexityAtFixedNglAsync(modelPath, slot, fixedNgl, corpusPath, probeRoot); - return pplOk; - } - private async Task ProbeLlamaBenchAtFixedNglAsync( string modelPath, BenchmarkSlot slot, @@ -889,7 +1068,7 @@ private async Task ProbeLlamaBenchAtFixedNglAsync( $"probe_llamabench_slot{slot.SlotId}_g{slot.DeviceCount}_ngl{fixedNgl}.md"); string cmd = slot.UsesGpu - ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl}{BuildTensorSplitArgs(slot)} -o md" + ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.LlamaBench)} -o md" : $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); @@ -920,7 +1099,7 @@ private async Task ProbePerplexityAtFixedNglAsync( $"probe_ppl_general_slot{slot.SlotId}_g{slot.DeviceCount}_ngl{fixedNgl}.log"); string cmd = slot.UsesGpu - ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot)} -t 4 -c 2048 --file \"{corpusPath}\"" + ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli)} -t 4 -c 2048 --file \"{corpusPath}\"" : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\""; var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); @@ -939,67 +1118,23 @@ private async Task ProbePerplexityAtFixedNglAsync( } } - private static List GetCandidateGroupSizes(int gpuCount) - { - var divisors = new List(); - - for (int i = 1; i <= gpuCount; i++) - { - if (gpuCount % i == 0) - divisors.Add(i); - } - - return divisors; - } - - private static List BuildContiguousGroups(int[] gpuIndices, int groupSize) - { - if (gpuIndices.Length % groupSize != 0) - { - throw new InvalidOperationException( - $"GPU count {gpuIndices.Length} was not divisible by group size {groupSize}."); - } - - var groups = new List(); - for (int i = 0; i < gpuIndices.Length; i += groupSize) - { - groups.Add(gpuIndices.Skip(i).Take(groupSize).ToArray()); - } - - return groups; - } - - private static async Task AcquireBenchmarkSlotAsync(CancellationToken ct = default) + private static async ValueTask AcquireBenchmarkSlotAsync( + ulong modelSizeBytes, + CancellationToken ct = default) { if (_currentPlan == null) throw new InvalidOperationException( "Benchmark execution plan has not been initialized. Call EnsureExecutionPlanAsync() first."); - if (_slotSemaphore == null) - throw new InvalidOperationException("Benchmark slot semaphore is not initialized."); - - await _slotSemaphore.WaitAsync(ct); + BenchmarkTopologyProfile profile = + _currentPlan.IndependentMaxModelSizeBytes > 0 && + modelSizeBytes > 0 && + modelSizeBytes <= _currentPlan.IndependentMaxModelSizeBytes && + _currentPlan.IndependentProfile.Slots.Count > 0 + ? _currentPlan.IndependentProfile + : _currentPlan.SharedProfile; - lock (SlotSync) - { - if (_availableSlots.Count == 0) - { - _slotSemaphore.Release(); - throw new InvalidOperationException("No benchmark slots were available after semaphore acquisition."); - } - - var slot = _availableSlots.Dequeue(); - return new BenchmarkSlotLease(slot); - } - } - - private static void ReturnBenchmarkSlot(BenchmarkSlot slot) - { - lock (SlotSync) - { - _availableSlots.Enqueue(slot); - _slotSemaphore!.Release(); - } + return await _resourceScheduler.AcquireAsync(profile.Slots, ct); } // ---------------------------------------------------------------- @@ -1048,17 +1183,25 @@ private int ResolveDynamicNglForModel(ulong modelSizeBytes, BenchmarkSlot slot) if (_currentPlan.Q8ModelSizeBytes == 0 || _currentPlan.Q8StableNgl <= 0) return Math.Max(0, _currentPlan.StaticNgl); - int maxNgl = _currentPlan.MaxCandidateNgl > 0 ? _currentPlan.MaxCandidateNgl : NglCandidates.Max(); + int maxNgl = _currentPlan.MaxCandidateNgl > 0 ? _currentPlan.MaxCandidateNgl : _currentPlan.StaticNgl; + int slotQ8StableNgl = slot.Q8StableNgl > 0 + ? slot.Q8StableNgl + : _currentPlan.Q8StableNgl; - double estimateRaw; if (modelSizeBytes <= _currentPlan.Q8ModelSizeBytes) { - estimateRaw = Math.Floor(_currentPlan.Q8StableNgl * (_currentPlan.Q8ModelSizeBytes / (double)modelSizeBytes)); + return BenchmarkGpuPlanner.ResolveNglForModel( + _currentPlan.Q8ModelSizeBytes, + slotQ8StableNgl, + maxNgl, + modelSizeBytes); } - else if (_currentPlan.NativeModelSizeBytes > _currentPlan.Q8ModelSizeBytes && _currentPlan.NativeStableNgl > 0 && modelSizeBytes < _currentPlan.NativeModelSizeBytes) + + double estimateRaw; + if (_currentPlan.NativeModelSizeBytes > _currentPlan.Q8ModelSizeBytes && _currentPlan.NativeStableNgl > 0 && modelSizeBytes < _currentPlan.NativeModelSizeBytes) { double t = (modelSizeBytes - _currentPlan.Q8ModelSizeBytes) / (double)(_currentPlan.NativeModelSizeBytes - _currentPlan.Q8ModelSizeBytes); - estimateRaw = Math.Floor(_currentPlan.Q8StableNgl + ((_currentPlan.NativeStableNgl - _currentPlan.Q8StableNgl) * t)); + estimateRaw = Math.Floor(slotQ8StableNgl + ((_currentPlan.NativeStableNgl - slotQ8StableNgl) * t)); } else if (_currentPlan.NativeModelSizeBytes > 0 && _currentPlan.NativeStableNgl > 0) { @@ -1067,22 +1210,19 @@ private int ResolveDynamicNglForModel(ulong modelSizeBytes, BenchmarkSlot slot) } else { - estimateRaw = Math.Floor(_currentPlan.Q8StableNgl * (_currentPlan.Q8ModelSizeBytes / (double)modelSizeBytes)); + estimateRaw = Math.Floor(slotQ8StableNgl * (_currentPlan.Q8ModelSizeBytes / (double)modelSizeBytes)); } int estimate = (int)Math.Clamp(estimateRaw, 0, maxNgl); - int chosen = NglCandidates - .Where(x => x <= estimate && x <= maxNgl) - .DefaultIfEmpty(0) - .Max(); - - return Math.Max(0, chosen); + return Math.Max(0, Math.Min(estimate, maxNgl)); } private static List BuildNglFallbackList(int startNgl) { var result = new List { Math.Max(0, startNgl) }; - result.AddRange(NglCandidates.Where(x => x < startNgl).OrderByDescending(x => x)); + result.AddRange(Enumerable.Range(Math.Max(1, startNgl - 4), Math.Min(4, Math.Max(0, startNgl - 1))) + .Reverse()); + result.AddRange(LegacyNglFallbacks.Where(x => x < startNgl).OrderByDescending(x => x)); if (!result.Contains(0)) result.Add(0); @@ -1268,10 +1408,10 @@ await SaveBenchmarkToDbAsync( await db.SaveChangesAsync(); } - await using var slotLease = await AcquireBenchmarkSlotAsync(); + ulong modelSizeBytes = TryGetModelSize(modelPath); + await using var slotLease = await AcquireBenchmarkSlotAsync(modelSizeBytes); var slot = slotLease.Slot; - ulong modelSizeBytes = TryGetModelSize(modelPath); int initialNgl = ResolveDynamicNglForModel(modelSizeBytes, slot); var runtimeNgl = new RuntimeNglState { @@ -1421,10 +1561,10 @@ private async Task RunAllBenchmarksTransientAsync( "You must call EnsureExecutionPlanAsync() with the pure Q8 model first."); } - await using var slotLease = await AcquireBenchmarkSlotAsync(); + ulong modelSizeBytes = TryGetModelSize(modelPath); + await using var slotLease = await AcquireBenchmarkSlotAsync(modelSizeBytes); var slot = slotLease.Slot; - ulong modelSizeBytes = TryGetModelSize(modelPath); int initialNgl = ResolveDynamicNglForModel(modelSizeBytes, slot); var runtimeNgl = new RuntimeNglState { @@ -2081,7 +2221,7 @@ private async Task RunLlamaBenchAsync( string logFile = Path.Combine(benchDir, "llamabench.md"); string cmd = slot.UsesGpu - ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl}{BuildTensorSplitArgs(slot)} -o md" + ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.LlamaBench)} -o md" : $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; await RunFixedCommandWithRetryAsync( @@ -2134,7 +2274,7 @@ private async Task RunPplBenchmarkAsync( } string cmd = slot.UsesGpu - ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot)} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}" + ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli)} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}" : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}"; await RunFixedCommandWithRetryAsync( @@ -2630,10 +2770,28 @@ private sealed record BenchmarkExecutionPlan( ulong NativeModelSizeBytes = 0, int NativeStableNgl = 0, string NativeQuantizationKey = "", - int MaxCandidateNgl = 35, + int MaxCandidateNgl = 0, string GpuMemoryLimitsJson = "{}", - string TensorSplitJson = "{}") - { + string TensorSplitJson = "{}", + IReadOnlyList? IndependentSlots = null, + ulong IndependentMaxModelSizeBytes = 0, + double SharedMeasuredJobsPerSecond = 0, + double SharedMeasuredSecondsPerPass = 0, + double IndependentMeasuredJobsPerSecond = 0, + double IndependentMeasuredSecondsPerPass = 0) + { + public BenchmarkTopologyProfile SharedProfile => new( + "shared", + Slots, + SharedMeasuredJobsPerSecond, + SharedMeasuredSecondsPerPass); + + public BenchmarkTopologyProfile IndependentProfile => new( + "independent", + IndependentSlots ?? [], + IndependentMeasuredJobsPerSecond, + IndependentMeasuredSecondsPerPass); + public static BenchmarkExecutionPlan CreateCpuPlan(string q8ModelPath) => new( PlanModelPath: q8ModelPath, @@ -2647,7 +2805,7 @@ public static BenchmarkExecutionPlan CreateCpuPlan(string q8ModelPath) NativeModelSizeBytes: 0, NativeStableNgl: 0, NativeQuantizationKey: string.Empty, - MaxCandidateNgl: NglCandidates.Max(), + MaxCandidateNgl: 0, GpuMemoryLimitsJson: SerializeGpuMemoryLimits(), TensorSplitJson: "{}"); } @@ -2658,56 +2816,6 @@ public sealed class RuntimeNglState public int LastSuccessfulNgl { get; set; } } - private sealed class BenchmarkSlot - { - public int SlotId { get; } - public int[] DeviceIndices { get; } - public bool UsesGpu => DeviceIndices.Length > 0; - public int DeviceCount => DeviceIndices.Length; - - public BenchmarkSlot(int slotId, int[] deviceIndices) - { - SlotId = slotId; - DeviceIndices = deviceIndices; - } - - public string DisplayName => - UsesGpu - ? $"GPU[{string.Join(",", DeviceIndices)}]" - : "CPU"; - - public IReadOnlyDictionary? BuildProcessEnv() - { - if (!UsesGpu) - return null; - - string visible = string.Join(",", DeviceIndices); - - return new Dictionary(StringComparer.OrdinalIgnoreCase) - { - ["CUDA_VISIBLE_DEVICES"] = visible, - ["HIP_VISIBLE_DEVICES"] = visible, - ["ROCR_VISIBLE_DEVICES"] = visible - }; - } - } - - private sealed class BenchmarkSlotLease : IAsyncDisposable - { - public BenchmarkSlot Slot { get; } - - public BenchmarkSlotLease(BenchmarkSlot slot) - { - Slot = slot; - } - - public ValueTask DisposeAsync() - { - ReturnBenchmarkSlot(Slot); - return ValueTask.CompletedTask; - } - } - private sealed record ExecutionPlanCacheKey( string HardwareFingerprint, string QuantizedModelFingerprint, diff --git a/MagicQuant/Services/GgufMetadataReader.cs b/MagicQuant/Services/GgufMetadataReader.cs new file mode 100644 index 0000000..ee061a3 --- /dev/null +++ b/MagicQuant/Services/GgufMetadataReader.cs @@ -0,0 +1,121 @@ +using System.Text.Json; +using MagicQuant.Helpers; + +namespace MagicQuant.Services; + +internal sealed class GgufMetadataReader +{ + private readonly PythonManager _python; + + public GgufMetadataReader(PythonManager python) + { + _python = python ?? throw new ArgumentNullException(nameof(python)); + } + + public async Task ReadAsync( + string ggufPath, + string workingDirectory, + CancellationToken ct = default) + { + if (string.IsNullOrWhiteSpace(ggufPath) || !File.Exists(ggufPath)) + throw new FileNotFoundException("GGUF metadata source was not found.", ggufPath); + + Directory.CreateDirectory(workingDirectory); + + string unique = Guid.NewGuid().ToString("N"); + string payloadPath = Path.Combine(workingDirectory, $"read_gguf_tensors_{unique}.json"); + string resultPath = Path.Combine(workingDirectory, $"read_gguf_tensors_result_{unique}.json"); + string scriptPath = Path.Combine(workingDirectory, $"read_gguf_tensors_{unique}.py"); + + try + { + await File.WriteAllTextAsync( + payloadPath, + JsonSerializer.Serialize(new { gguf_path = ggufPath, output_path = resultPath }), + ct); + + const string py = """ + import json + import sys + + payload_path = sys.argv[1] + with open(payload_path, "r", encoding="utf-8") as f: + payload = json.load(f) + + output_path = payload["output_path"] + + def resolve_type_name(t): + for attr in ["type_name", "tensor_type", "type"]: + v = getattr(t, attr, None) + if v is None: + continue + if hasattr(v, "name"): + return str(v.name) + return str(v) + return "UNKNOWN" + + try: + import gguf + reader = gguf.GGUFReader(payload["gguf_path"]) + tensor_names = [t.name for t in reader.tensors] + tensor_types = {t.name: resolve_type_name(t) for t in reader.tensors} + + def read_scalar(key): + field = reader.fields.get(key) + if field is None: + return None + value = field.contents() + return value.item() if hasattr(value, "item") else value + + architecture = read_scalar("general.architecture") + architecture_key = str(architecture) if architecture is not None else None + block_count = read_scalar(f"{architecture_key}.block_count") if architecture_key else None + nextn_layers = read_scalar(f"{architecture_key}.nextn_predict_layers") if architecture_key else None + result = { + "Architecture": architecture_key, + "BlockCount": int(block_count) if block_count is not None else None, + "NextnPredictLayers": int(nextn_layers) if nextn_layers is not None else None, + "TensorNames": tensor_names, + "TensorTypes": tensor_types + } + except Exception as e: + result = {"Error": str(e), "TensorNames": [], "TensorTypes": {}} + + with open(output_path, "w", encoding="utf-8") as f: + json.dump(result, f, indent=2) + """; + + await File.WriteAllTextAsync(scriptPath, py, ct); + await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); + + var result = JsonSerializer.Deserialize( + await File.ReadAllTextAsync(resultPath, ct)); + + if (result == null) + throw new InvalidOperationException("Failed to parse GGUF metadata result."); + if (!string.IsNullOrWhiteSpace(result.Error)) + throw new InvalidOperationException($"Failed to read GGUF metadata: {result.Error}"); + + return result; + } + finally + { + TryDelete(payloadPath); + TryDelete(resultPath); + TryDelete(scriptPath); + } + } + + private static void TryDelete(string path) + { + try + { + if (File.Exists(path)) + File.Delete(path); + } + catch + { + // Best-effort cleanup; metadata success/failure is the authoritative result. + } + } +} diff --git a/MagicQuant/Services/LlamaGpuArgumentBuilder.cs b/MagicQuant/Services/LlamaGpuArgumentBuilder.cs new file mode 100644 index 0000000..893e79b --- /dev/null +++ b/MagicQuant/Services/LlamaGpuArgumentBuilder.cs @@ -0,0 +1,43 @@ +using System.Globalization; + +namespace MagicQuant.Services; + +internal enum LlamaGpuTool +{ + CommonCli = 1, + LlamaBench = 2 +} + +internal static class LlamaGpuArgumentBuilder +{ + public static string BuildTensorSplitArgs( + IReadOnlyList deviceIndices, + IReadOnlyDictionary gpuMemoryLimitsGb, + LlamaGpuTool tool) + { + ArgumentNullException.ThrowIfNull(deviceIndices); + ArgumentNullException.ThrowIfNull(gpuMemoryLimitsGb); + + if (deviceIndices.Count <= 1 || gpuMemoryLimitsGb.Count == 0) + return string.Empty; + + var missing = deviceIndices + .Where(i => !gpuMemoryLimitsGb.ContainsKey(i)) + .ToArray(); + + if (missing.Length > 0) + { + throw new InvalidOperationException( + $"GPU memory limits were configured, but limits are missing for GPU(s): {string.Join(", ", missing)}."); + } + + // llama-bench uses commas to generate a Cartesian product of benchmark cases; + // members of one multi-GPU split are slash-separated. The common llama.cpp + // argument parser used by llama-perplexity expects comma-separated members. + string separator = tool == LlamaGpuTool.LlamaBench ? "/" : ","; + string split = string.Join(separator, deviceIndices.Select(i => + gpuMemoryLimitsGb[i].ToString("0.###", CultureInfo.InvariantCulture))); + + return $" --tensor-split {split}"; + } +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 06ce316..05b2be7 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -49,6 +49,7 @@ public class QuantizationService private readonly ModelArtifactPathService _paths; private readonly ScratchStorageService _scratchStorage; private readonly PythonManager _python; + private readonly GgufMetadataReader _ggufMetadataReader; private readonly SemaphoreSlim _cpuQuantLock; private readonly int _quantThreadsPerProcess; private readonly int _maxConcurrentQuantizations; @@ -67,6 +68,7 @@ public QuantizationService(BenchmarkService benchmarker) { _benchmarker = benchmarker ?? throw new ArgumentNullException(nameof(benchmarker)); _python = _benchmarker._pyManager; + _ggufMetadataReader = new GgufMetadataReader(_python); if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) throw new Exception( @@ -2812,85 +2814,7 @@ private List ResolveConcreteTensorOverrides( private async Task ReadTensorMetadataFromGgufAsync(string ggufPath, string workingDirectory) { - string workingDir = workingDirectory; - string unique = Guid.NewGuid().ToString("N"); - string payloadPath = Path.Combine(workingDir, $"read_gguf_tensors_{unique}.json"); - string resultPath = Path.Combine(workingDir, $"read_gguf_tensors_result_{unique}.json"); - string scriptPath = Path.Combine(workingDir, $"read_gguf_tensors_{unique}.py"); - - try - { - await File.WriteAllTextAsync(payloadPath, - JsonSerializer.Serialize(new { gguf_path = ggufPath, output_path = resultPath })); - - const string py = """ - import json - import sys - - payload_path = sys.argv[1] - with open(payload_path, "r", encoding="utf-8") as f: - payload = json.load(f) - - output_path = payload["output_path"] - - def resolve_type_name(t): - for attr in ["type_name", "tensor_type", "type"]: - v = getattr(t, attr, None) - if v is None: - continue - if hasattr(v, "name"): - return str(v.name) - return str(v) - return "UNKNOWN" - - try: - import gguf - reader = gguf.GGUFReader(payload["gguf_path"]) - tensor_names = [t.name for t in reader.tensors] - tensor_types = {t.name: resolve_type_name(t) for t in reader.tensors} - - def read_scalar(key): - field = reader.fields.get(key) - if field is None: - return None - value = field.contents() - return value.item() if hasattr(value, "item") else value - - architecture = read_scalar("general.architecture") - architecture_key = str(architecture) if architecture is not None else None - block_count = read_scalar(f"{architecture_key}.block_count") if architecture_key else None - nextn_layers = read_scalar(f"{architecture_key}.nextn_predict_layers") if architecture_key else None - result = { - "Architecture": architecture_key, - "BlockCount": int(block_count) if block_count is not None else None, - "NextnPredictLayers": int(nextn_layers) if nextn_layers is not None else None, - "TensorNames": tensor_names, - "TensorTypes": tensor_types - } - except Exception as e: - result = {"Error": str(e), "TensorNames": [], "TensorTypes": {}} - - with open(output_path, "w", encoding="utf-8") as f: - json.dump(result, f, indent=2) - """; - - await File.WriteAllTextAsync(scriptPath, py); - await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); - - var result = JsonSerializer.Deserialize(await File.ReadAllTextAsync(resultPath)); - if (result == null) - throw new InvalidOperationException("Failed to parse GGUF tensor list result."); - if (!string.IsNullOrWhiteSpace(result.Error)) - throw new InvalidOperationException($"Failed to read GGUF tensor names: {result.Error}"); - - return result; - } - finally - { - if (File.Exists(payloadPath)) File.Delete(payloadPath); - if (File.Exists(resultPath)) File.Delete(resultPath); - if (File.Exists(scriptPath)) File.Delete(scriptPath); - } + return await _ggufMetadataReader.ReadAsync(ggufPath, workingDirectory); } From 59e79b5b0cec6d9c18b0f4880bb98004f29b0a10 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 05:56:57 -0400 Subject: [PATCH 229/258] Support pinned historical baseline revisions --- .../HuggingFaceRevisionConfigTests.cs | 49 ++++++++ MagicQuant/Commands/Evolution.cs | 3 +- .../Configuration/MagicQuantYamlConfig.cs | 2 + .../Configuration/MagicQuantYamlLoader.cs | 8 ++ .../Services/HuggingFaceBaselineService.cs | 31 ++++- .../Services/HybridBenchmarkRepository.cs | 9 +- MagicQuant/config.default.yaml | 5 +- MagicQuant/config.dev.yaml | 108 ++++++++++++++++++ 8 files changed, 205 insertions(+), 10 deletions(-) create mode 100644 MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs diff --git a/MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs b/MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs new file mode 100644 index 0000000..842f44a --- /dev/null +++ b/MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs @@ -0,0 +1,49 @@ +using MagicQuant.Configuration; +using Xunit; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace MagicQuant.Tests; + +public sealed class HuggingFaceRevisionConfigTests +{ + [Fact] + public void CustomRepositoryRevision_DeserializesPinnedCommit() + { + const string yaml = """ + baselines: + custom_repositories: + - repo_id: owner/model-GGUF + revision: 313447f257f7ebde0b968e4778feef774546ed81 + """; + + var config = Deserialize(yaml); + var repository = Assert.Single(config.Baselines.CustomRepositories); + + Assert.Equal("owner/model-GGUF", repository.RepoId); + Assert.Equal("313447f257f7ebde0b968e4778feef774546ed81", repository.Revision); + } + + [Fact] + public void CustomRepositoryRevision_RemainsOptional() + { + const string yaml = """ + baselines: + custom_repositories: + - repo_id: owner/model-GGUF + """; + + var repository = Assert.Single(Deserialize(yaml).Baselines.CustomRepositories); + + Assert.Null(repository.Revision); + } + + private static MagicQuantYamlConfig Deserialize(string yaml) + { + return new DeserializerBuilder() + .IgnoreUnmatchedProperties() + .WithNamingConvention(UnderscoredNamingConvention.Instance) + .Build() + .Deserialize(yaml); + } +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 25599c8..8adb587 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -728,8 +728,9 @@ private static void PrintCustomBaselineRuntimeSummary( bool inCarriers = carriers.Any(x => x.UniqueId == custom.DynamicBaselineId); bool inExplicit = explicitCandidates.Any(x => x.UniqueId == custom.DynamicBaselineId); + string revision = string.IsNullOrWhiteSpace(custom.Revision) ? "main" : custom.Revision; AnsiConsole.MarkupLine( - $" [cyan]{custom.DynamicBaselineId}[/] [yellow]{Markup.Escape(custom.DisplayName)}[/] family={Markup.Escape(custom.BaselineFamily)} file={Markup.Escape(custom.SourceFileName)} learning={inLearning} carrier={inCarriers} explicit={inExplicit}"); + $" [cyan]{custom.DynamicBaselineId}[/] [yellow]{Markup.Escape(custom.DisplayName)}[/] family={Markup.Escape(custom.BaselineFamily)} file={Markup.Escape(custom.SourceFileName)} revision={Markup.Escape(revision)} learning={inLearning} carrier={inCarriers} explicit={inExplicit}"); } } diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index f60eb60..f8d5bc7 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -421,6 +421,7 @@ public sealed class RuntimeBaselineConfig public sealed class CustomBaselineRepositoryConfig { public string RepoId { get; set; } = string.Empty; + public string? Revision { get; set; } public string? ShortSourceName { get; set; } public string SourceKind { get; set; } = "huggingface_gguf_repository"; public bool Enabled { get; set; } = true; @@ -454,6 +455,7 @@ public sealed class ResolvedCustomBaselineSpec public string CanonicalKey { get; set; } = string.Empty; public string DisplayName { get; set; } = string.Empty; public string RepoId { get; set; } = string.Empty; + public string? Revision { get; set; } public string SourceOwner { get; set; } = string.Empty; public string SourceFileName { get; set; } = string.Empty; public string ShortSourceName { get; set; } = string.Empty; diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 7faabe2..6fe510f 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -168,6 +168,14 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) config.AnomalyDetection.MaxConfirmedPairwiseOrderingAdjustmentKld = Math.Max(0d, config.AnomalyDetection.MaxConfirmedPairwiseOrderingAdjustmentKld); config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone = Math.Max(1, config.AnomalyDetection.MaxSmokeCandidatesPerReferenceZone); + foreach (var repository in config.Baselines.CustomRepositories) + { + repository.RepoId = repository.RepoId.Trim(); + repository.Revision = string.IsNullOrWhiteSpace(repository.Revision) + ? null + : repository.Revision.Trim(); + } + ApplyStandardBaselineFilters(config.Baselines); BaselineQuants.ResetDynamicCustomBaselines(); } diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index deb38f2..eb8100e 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -67,9 +67,13 @@ public async Task> PrecheckAndRegister if (repo.Includes.Count == 0) throw new InvalidOperationException($"Custom baseline repository '{repo.RepoId}' is enabled but has zero include entries."); - AnsiConsole.MarkupLine($"[cyan]Repo:[/] {Markup.Escape(repo.RepoId)} [grey](includes={repo.Includes.Count})[/]"); + string revisionLabel = string.IsNullOrWhiteSpace(repo.Revision) + ? "main" + : repo.Revision!; + AnsiConsole.MarkupLine( + $"[cyan]Repo:[/] {Markup.Escape(repo.RepoId)} [grey](revision={Markup.Escape(revisionLabel)}, includes={repo.Includes.Count})[/]"); - var repoFiles = await ListRepoFilesAsync(repo.RepoId, ct); + var repoFiles = await ListRepoFilesAsync(repo.RepoId, repo.Revision, ct); if (repoFiles.Count == 0) throw new InvalidOperationException($"No files were returned from Hugging Face repo '{repo.RepoId}'."); @@ -201,6 +205,7 @@ public async Task> PrecheckAndRegister CanonicalKey = dynamicBaseline.CanonicalKey, DisplayName = dynamicBaseline.Names[0], RepoId = repo.RepoId, + Revision = repo.Revision, SourceOwner = dynamicBaseline.SourceOwner ?? string.Empty, SourceFileName = dynamicBaseline.SourceFileName ?? string.Empty, ShortSourceName = dynamicBaseline.ShortSourceName ?? shortSourceName, @@ -309,6 +314,7 @@ public async Task DownloadBaselineAsync(BaselineQuants baseline, string await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new { repo_id = spec.RepoId, + revision = spec.Revision, file_name = spec.SourceFileName, target_path = destinationPath, force_redownload = forceRedownload, @@ -332,6 +338,7 @@ with open(payload_path, 'r', encoding='utf-8') as f: try: downloaded = hf_hub_download( repo_id=payload['repo_id'], + revision=payload.get('revision') or None, filename=payload['file_name'], local_dir=os.path.dirname(target_path), force_download=payload.get('force_redownload', False), @@ -582,7 +589,10 @@ private async Task EnsureHubSupportAsync() await _python.RunPipInstallAsync("--upgrade huggingface_hub"); } - private async Task> ListRepoFilesAsync(string repoId, CancellationToken ct) + private async Task> ListRepoFilesAsync( + string repoId, + string? revision, + CancellationToken ct) { string tempDir = Cache.ExternalBaselineCacheDirectory ?? Cache.MagicQuantDirectory ?? AppContext.BaseDirectory; Directory.CreateDirectory(tempDir); @@ -593,7 +603,12 @@ private async Task> ListRepoFilesAsync(string repoId, CancellationT try { - await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new { repo_id = repoId, result_path = resultPath }), ct); + await File.WriteAllTextAsync(payloadPath, JsonSerializer.Serialize(new + { + repo_id = repoId, + revision, + result_path = resultPath + }), ct); const string py = """ import json @@ -606,7 +621,10 @@ with open(payload_path, 'r', encoding='utf-8') as f: result_path = payload['result_path'] try: - files = HfApi().list_repo_files(repo_id=payload['repo_id']) + files = HfApi().list_repo_files( + repo_id=payload['repo_id'], + revision=payload.get('revision') or None, + ) result = {'success': True, 'files': files} except Exception as ex: result = {'success': False, 'error': str(ex), 'files': []} @@ -622,7 +640,8 @@ with open(result_path, 'w', encoding='utf-8') as f: if (!doc.RootElement.TryGetProperty("success", out var successProp) || !successProp.GetBoolean()) { string error = doc.RootElement.TryGetProperty("error", out var errProp) ? errProp.GetString() ?? "unknown error" : "unknown error"; - throw new InvalidOperationException($"Failed listing files for Hugging Face repo '{repoId}': {error}"); + string revisionSuffix = string.IsNullOrWhiteSpace(revision) ? string.Empty : $" at revision '{revision}'"; + throw new InvalidOperationException($"Failed listing files for Hugging Face repo '{repoId}'{revisionSuffix}: {error}"); } return doc.RootElement.GetProperty("files") diff --git a/MagicQuant/Services/HybridBenchmarkRepository.cs b/MagicQuant/Services/HybridBenchmarkRepository.cs index 5f84ba2..2e56076 100644 --- a/MagicQuant/Services/HybridBenchmarkRepository.cs +++ b/MagicQuant/Services/HybridBenchmarkRepository.cs @@ -481,7 +481,12 @@ public static string BuildDisplayName(HybridQuant quant) var resolved = Config.GetResolvedCustomBaseline(baseline.CanonicalKey); if (resolved != null && !string.IsNullOrWhiteSpace(resolved.RepoId)) - return $"https://huggingface.co/{resolved.RepoId}"; + { + string repositoryUrl = $"https://huggingface.co/{resolved.RepoId}"; + return string.IsNullOrWhiteSpace(resolved.Revision) + ? repositoryUrl + : $"{repositoryUrl}/tree/{Uri.EscapeDataString(resolved.Revision)}"; + } if (!string.IsNullOrWhiteSpace(baseline.SourceRepository)) return $"https://huggingface.co/{baseline.SourceRepository}"; @@ -501,4 +506,4 @@ public static string BuildDisplayName(HybridQuant quant) .Select(x => (int?)x.Id) .FirstOrDefaultAsync(ct); } -} \ No newline at end of file +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 0474ce6..977d1ec 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -414,6 +414,9 @@ baselines: # - "quantize_base_name" is the quant/base family name used in rebuild logic # # - repo_id: unsloth/Qwen3-4B-Instruct-2507-GGUF + # # Optional branch, tag, or commit. Pin this when a provider rotates files + # # so tensor digestion and later downloads remain reproducible. + # revision: # enabled: true # short_source_name: UD # source_kind: huggingface_gguf_repository @@ -509,4 +512,4 @@ synergy_detection: contaminating_passenger_detection_enabled: true min_failure_margin_for_contamination_kld: 0.00050 contamination_penalty_confidence_multiplier: 0.45 - suppress_repeated_contaminated_attempts: true \ No newline at end of file + suppress_repeated_contaminated_attempts: true diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 9b67c75..972d5db 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -470,6 +470,114 @@ baselines: allow_as_combination_carrier: false allow_as_explicit_group_candidate: true + # Unsloth replaced its dynamic-v2 files when dynamic-v3 was published. Keep + # the source generation explicit and reproducible instead of importing old + # MagicQuant winners or silently resolving these names against a moving main. + - repo_id: unsloth/Qwen3.8-27B-GGUF + revision: 313447f257f7ebde0b968e4778feef774546ed81 + enabled: true + short_source_name: UnslothV2 + source_kind: huggingface_gguf_repository + require_all_includes_to_resolve: true + validate_tensor_names_against_source_model: true + delete_partial_or_dirty_downloads: true + resume_or_retry_downloads: true + + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: false + + includes: + - file_name: Qwen3.8-27B-UD-IQ2_M.gguf + baseline_family: IQ2_M + quantize_base_name: IQ2_M + display_name: Unsloth-UD-IQ2_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q3_K_S.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_S + display_name: Unsloth-Q3_K_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q3_K_M.gguf + baseline_family: IQ3_M + quantize_base_name: IQ3_M + display_name: Unsloth-Q3_K_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-IQ4_XS.gguf + baseline_family: IQ4_XS + quantize_base_name: IQ4_XS + display_name: Unsloth-IQ4_XS + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-IQ4_NL.gguf + baseline_family: IQ4_NL + quantize_base_name: IQ4_NL + display_name: Unsloth-IQ4_NL + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q4_K_S.gguf + baseline_family: Q4_K_S + quantize_base_name: Q4_K_S + display_name: Unsloth-Q4_K_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q4_K_M.gguf + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M + display_name: Unsloth-Q4_K_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q5_K_S.gguf + baseline_family: Q5_K_S + quantize_base_name: Q5_K_S + display_name: Unsloth-Q5_K_S + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q5_K_M.gguf + baseline_family: Q5_K + quantize_base_name: Q5_K + display_name: Unsloth-Q5_K_M + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + + - file_name: Qwen3.8-27B-Q6_K.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: Unsloth-Q6_K + force_relearn: false + allow_as_learning_baseline: true + allow_as_combination_carrier: false + allow_as_explicit_group_candidate: true + # Counterfactual synergy templates generalize confirmed contextual anomaly evidence. # anomaly_detection remains the low-level compatibility section; synergy_detection controls # template transfer, composition probes, contamination suppression, and wing diagnostics. From 1a87e22964e7c420e3130f05c1809aa45f902f80 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 06:19:26 -0400 Subject: [PATCH 230/258] Handle symlinked external baseline caches --- .../HuggingFaceBaselineCacheTests.cs | 29 +++++++++++ .../Services/HuggingFaceBaselineService.cs | 49 +++++++++++++++---- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs b/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs index 9c76cd2..942ab5e 100644 --- a/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs +++ b/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs @@ -61,6 +61,35 @@ public void StagingCleanupPath_MustRemainInsideDestinationDirectory() Path.Combine(root, "outside.gguf"), cache)); } + [Fact] + public void StagingCleanupPath_ResolvesSymlinkedParentDirectory() + { + string root = Path.Combine(Path.GetTempPath(), "mq-hf-symlink-test-" + Guid.NewGuid().ToString("N")); + string physical = Path.Combine(root, "physical-model"); + string alias = Path.Combine(root, "model-alias"); + string cache = Path.Combine(physical, "MagicQuant", "ExternalBaselines"); + + try + { + Directory.CreateDirectory(cache); + Directory.CreateSymbolicLink(alias, physical); + + string downloadedPath = Path.Combine(cache, "source.gguf"); + File.WriteAllBytes(downloadedPath, "GGUF-source-payload"u8.ToArray()); + + string aliasedCache = Path.Combine(alias, "MagicQuant", "ExternalBaselines"); + Assert.True(HuggingFaceBaselineService.IsPathInsideDirectory(downloadedPath, aliasedCache)); + Assert.True(HuggingFaceBaselineService.PathsReferToSameLocation( + downloadedPath, + Path.Combine(aliasedCache, "source.gguf"))); + } + finally + { + if (Directory.Exists(root)) + Directory.Delete(root, recursive: true); + } + } + private sealed class TemporaryFiles : IDisposable { private readonly string _directory = Path.Combine( diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index eb8100e..58b1c6d 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -374,10 +374,7 @@ with open(result_path, 'w', encoding='utf-8') as f: throw new InvalidOperationException($"Downloaded external baseline is not a GGUF file: {downloadedPath}"); bool reused = CanReuseDownloadedFile(downloadedPath, destinationPath); - if (!reused && !string.Equals( - Path.GetFullPath(downloadedPath), - Path.GetFullPath(destinationPath), - StringComparison.OrdinalIgnoreCase)) + if (!reused && !PathsReferToSameLocation(downloadedPath, destinationPath)) { await CopyDownloadedFileAtomicallyAsync(downloadedPath, atomicStagingPath, destinationPath, ct); } @@ -385,10 +382,7 @@ with open(result_path, 'w', encoding='utf-8') as f: if (!File.Exists(destinationPath) || new FileInfo(destinationPath).Length == 0 || !HasGgufMagic(destinationPath)) throw new InvalidOperationException($"External baseline staging produced no valid GGUF file: {destinationPath}"); - if (!string.Equals( - Path.GetFullPath(downloadedPath), - Path.GetFullPath(destinationPath), - StringComparison.OrdinalIgnoreCase)) + if (!PathsReferToSameLocation(downloadedPath, destinationPath)) { string destinationDirectory = Path.GetDirectoryName(Path.GetFullPath(destinationPath))!; if (!IsPathInsideDirectory(downloadedPath, destinationDirectory)) @@ -431,14 +425,49 @@ internal static bool CanReuseDownloadedFile(string downloadedPath, string destin internal static bool IsPathInsideDirectory(string childPath, string parentDirectory) { - string child = Path.GetFullPath(childPath) + string child = ResolvePhysicalPath(childPath) .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - string parent = Path.GetFullPath(parentDirectory) + string parent = ResolvePhysicalPath(parentDirectory) .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); return child.StartsWith(parent + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase); } + internal static bool PathsReferToSameLocation(string firstPath, string secondPath) + { + return string.Equals( + ResolvePhysicalPath(firstPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), + ResolvePhysicalPath(secondPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), + StringComparison.OrdinalIgnoreCase); + } + + private static string ResolvePhysicalPath(string path) + { + string fullPath = Path.GetFullPath(path); + string root = Path.GetPathRoot(fullPath) + ?? throw new InvalidOperationException($"Path '{path}' has no filesystem root."); + string current = root; + string relative = Path.GetRelativePath(root, fullPath); + + foreach (string component in relative.Split( + [Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar], + StringSplitOptions.RemoveEmptyEntries)) + { + current = Path.Combine(current, component); + + FileSystemInfo info = Directory.Exists(current) + ? new DirectoryInfo(current) + : new FileInfo(current); + + if (!info.Exists || string.IsNullOrWhiteSpace(info.LinkTarget)) + continue; + + current = info.ResolveLinkTarget(returnFinalTarget: true)?.FullName ?? current; + } + + return Path.GetFullPath(current); + } + private static bool HasGgufMagic(string path) { try From a1b756da5bfb913af6a5011aa1e38ba48a31bb8e Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 06:21:18 -0400 Subject: [PATCH 231/258] Preserve resumable external baseline downloads --- ...xternalBaselineCacheCleanupServiceTests.cs | 55 +++++++++++++++++++ .../ExternalBaselineCacheCleanupService.cs | 31 +++++++++++ 2 files changed, 86 insertions(+) diff --git a/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs b/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs index 67598a0..13fbd27 100644 --- a/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs +++ b/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs @@ -1,4 +1,5 @@ using MagicQuant.Services; +using MagicQuant.Configuration; using MQ.DB; using Xunit; @@ -19,9 +20,11 @@ public async Task CleanupStaleArtifactsAsync_HardDeletesCacheTree() string? priorModelMagicQuantDirectory = Cache.ModelMagicQuantDirectory; string? priorExternalBaselineCacheDirectory = Cache.ExternalBaselineCacheDirectory; + var priorConfig = Config.Current; try { + Config.Load(MagicQuantYamlConfig.CreateDefault()); Cache.ModelMagicQuantDirectory = modelRoot; Cache.ExternalBaselineCacheDirectory = cacheRoot; @@ -34,6 +37,58 @@ public async Task CleanupStaleArtifactsAsync_HardDeletesCacheTree() { Cache.ModelMagicQuantDirectory = priorModelMagicQuantDirectory; Cache.ExternalBaselineCacheDirectory = priorExternalBaselineCacheDirectory; + Config.Load(priorConfig); + + if (Directory.Exists(temp)) + Directory.Delete(temp, recursive: true); + } + } + + [Fact] + public async Task CleanupStaleArtifactsAsync_PreservesCompletedAndInProgressResumableDownloads() + { + string temp = Path.Combine(Path.GetTempPath(), "mq-external-cache-resume-test-" + Guid.NewGuid().ToString("N")); + string modelRoot = Path.Combine(temp, "MagicQuant"); + string cacheRoot = Path.Combine(modelRoot, "ExternalBaselines"); + string hubCache = Path.Combine(cacheRoot, ".cache", "huggingface"); + string completed = Path.Combine(cacheRoot, "baseline.gguf"); + string incomplete = Path.Combine(hubCache, "baseline.gguf.incomplete"); + string transient = Path.Combine(cacheRoot, "baseline.gguf.partial.interrupted"); + + Directory.CreateDirectory(hubCache); + await File.WriteAllTextAsync(completed, "GGUF-complete"); + await File.WriteAllTextAsync(incomplete, "partial-download"); + await File.WriteAllTextAsync(transient, "partial-copy"); + + string? priorModelMagicQuantDirectory = Cache.ModelMagicQuantDirectory; + string? priorExternalBaselineCacheDirectory = Cache.ExternalBaselineCacheDirectory; + var priorConfig = Config.Current; + + try + { + var config = MagicQuantYamlConfig.CreateDefault(); + config.Baselines.CustomRepositories.Add(new CustomBaselineRepositoryConfig + { + RepoId = "owner/model", + Enabled = true, + ResumeOrRetryDownloads = true + }); + Config.Load(config); + Cache.ModelMagicQuantDirectory = modelRoot; + Cache.ExternalBaselineCacheDirectory = cacheRoot; + + bool cleaned = await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); + + Assert.True(cleaned); + Assert.True(File.Exists(completed)); + Assert.True(File.Exists(incomplete)); + Assert.False(File.Exists(transient)); + } + finally + { + Cache.ModelMagicQuantDirectory = priorModelMagicQuantDirectory; + Cache.ExternalBaselineCacheDirectory = priorExternalBaselineCacheDirectory; + Config.Load(priorConfig); if (Directory.Exists(temp)) Directory.Delete(temp, recursive: true); diff --git a/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs b/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs index 9f340b8..b735a94 100644 --- a/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs +++ b/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs @@ -22,12 +22,43 @@ public async Task CleanupStaleArtifactsAsync(CancellationToken ct = defaul if (!Directory.Exists(cacheRoot)) return false; + bool preserveResumableDownloads = Config.Current.Baselines.CustomRepositories + .Any(x => x.Enabled && x.ResumeOrRetryDownloads); + + if (preserveResumableDownloads) + { + int removedTransientFiles = await CleanupTransientTopLevelFilesAsync(cacheRoot, ct); + AnsiConsole.MarkupLine( + $"[green]Preserved resumable external-baseline downloads:[/] {Markup.Escape(cacheRoot)} " + + $"[grey](removed transient files={removedTransientFiles:N0})[/]"); + return removedTransientFiles > 0; + } + await HardDeleteHelper.DeleteDirectoryIfExistsAsync(cacheRoot, ct); AnsiConsole.MarkupLine( $"[green]Cleaned abandoned external-baseline artifacts:[/] {Markup.Escape(cacheRoot)}"); return true; } + private static async Task CleanupTransientTopLevelFilesAsync(string cacheRoot, CancellationToken ct) + { + int removed = 0; + foreach (string file in Directory.EnumerateFiles(cacheRoot, "*", SearchOption.TopDirectoryOnly)) + { + ct.ThrowIfCancellationRequested(); + + string fileName = Path.GetFileName(file); + bool isCompletedGguf = fileName.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase); + if (isCompletedGguf) + continue; + + await HardDeleteHelper.DeleteFileIfExistsAsync(file); + removed++; + } + + return removed; + } + internal static void ValidateCleanupRoot(string cacheRoot, string modelMagicQuantRoot) { string fullCacheRoot = Path.GetFullPath(cacheRoot) From 1d0f5c86b4c83469d79a4ee4263db29b71ef38c6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 06:40:43 -0400 Subject: [PATCH 232/258] Scope anomaly rules to measured fidelity contexts --- MagicQuant.Tests/AnomalyContextScopeTests.cs | 94 +++++++++++++++++++ .../Configuration/MagicQuantYamlConfig.cs | 2 + .../Configuration/MagicQuantYamlLoader.cs | 1 + .../AnomalyAdjustedPredictionService.cs | 46 +++++++++ MagicQuant/config.default.yaml | 4 + MagicQuant/config.dev.yaml | 2 + 6 files changed, 149 insertions(+) create mode 100644 MagicQuant.Tests/AnomalyContextScopeTests.cs diff --git a/MagicQuant.Tests/AnomalyContextScopeTests.cs b/MagicQuant.Tests/AnomalyContextScopeTests.cs new file mode 100644 index 0000000..771d512 --- /dev/null +++ b/MagicQuant.Tests/AnomalyContextScopeTests.cs @@ -0,0 +1,94 @@ +using MagicQuant.Configuration; +using MagicQuant.Services; +using MQ.DB; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Xunit; + +namespace MagicQuant.Tests; + +[CollectionDefinition(Name, DisableParallelization = true)] +public sealed class AnomalyContextScopeCollection +{ + public const string Name = "Anomaly context scope"; +} + +[Collection(AnomalyContextScopeCollection.Name)] +public sealed class AnomalyContextScopeTests +{ + [Fact] + public void BuildContextFidelityPredicate_BoundsOnlyNonRuleGroups() + { + var priorConfig = Config.Current; + var priorUnusedGroups = Cache.UnusedTensorGroups.ToList(); + + try + { + var config = MagicQuantYamlConfig.CreateDefault(); + config.SynergyDetection.ContextScopedRuleApplicationEnabled = true; + config.SynergyDetection.MaxNonRuleGroupsBelowReferenceTier = 1; + Config.Load(config); + Cache.UnusedTensorGroups.Clear(); + + var rule = new AnomalyInteractionRule + { + ReferenceQuantId = BaselineQuants.Q8_0.UniqueId, + GroupStates = + [ + new AnomalyInteractionRuleGroupState + { + TensorGroupId = TReg.Embeddings.UniqueId, + ReferenceQuantId = BaselineQuants.Q8_0.UniqueId, + CandidateQuantId = BaselineQuants.Q6_K.UniqueId + } + ] + }; + + string predicate = AnomalyAdjustedPredictionService.BuildContextFidelityPredicate(rule, "c"); + + Assert.Contains("c.LmHead", predicate, StringComparison.Ordinal); + Assert.Contains("c.AttnQ", predicate, StringComparison.Ordinal); + Assert.DoesNotContain("c.Embeddings", predicate, StringComparison.Ordinal); + Assert.EndsWith("<= 1)", predicate, StringComparison.Ordinal); + } + finally + { + Config.Load(priorConfig); + Cache.UnusedTensorGroups.Clear(); + Cache.UnusedTensorGroups.AddRange(priorUnusedGroups); + } + } + + [Fact] + public void BuildContextFidelityPredicate_ReturnsEmptyWhenDisabled() + { + var priorConfig = Config.Current; + + try + { + var config = MagicQuantYamlConfig.CreateDefault(); + config.SynergyDetection.ContextScopedRuleApplicationEnabled = false; + Config.Load(config); + + var rule = new AnomalyInteractionRule + { + ReferenceQuantId = BaselineQuants.Q8_0.UniqueId, + GroupStates = + [ + new AnomalyInteractionRuleGroupState + { + TensorGroupId = TReg.Embeddings.UniqueId, + ReferenceQuantId = BaselineQuants.Q8_0.UniqueId, + CandidateQuantId = BaselineQuants.Q6_K.UniqueId + } + ] + }; + + Assert.Empty(AnomalyAdjustedPredictionService.BuildContextFidelityPredicate(rule, "c")); + } + finally + { + Config.Load(priorConfig); + } + } +} diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index f8d5bc7..782bbfb 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -364,6 +364,8 @@ public sealed class RuntimeSynergyDetectionConfig public int MaxTransferProbesPerTemplate { get; set; } = 6; public int MaxTotalTransferProbesPerRun { get; set; } = 24; public RuntimeSynergyTransferProbeContextStrataConfig TransferProbeContextStrata { get; set; } = new(); + public bool ContextScopedRuleApplicationEnabled { get; set; } = true; + public int MaxNonRuleGroupsBelowReferenceTier { get; set; } = 1; public bool VerboseSynergyLogging { get; set; } = true; public double MinSmokeScore { get; set; } = 0.55d; public double MaxSmokeGapKld { get; set; } = 0.004d; diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 6fe510f..46b18cb 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -198,6 +198,7 @@ private static void NormalizeSynergyDetection(MagicQuantYamlConfig config) s.TransferProbeContextStrata ??= new RuntimeSynergyTransferProbeContextStrataConfig(); s.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, s.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6); s.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, s.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6); + s.MaxNonRuleGroupsBelowReferenceTier = Math.Clamp(s.MaxNonRuleGroupsBelowReferenceTier, 0, 9); s.MinSmokeScore = Math.Clamp(s.MinSmokeScore, 0d, 1d); s.MaxSmokeGapKld = Math.Max(0d, s.MaxSmokeGapKld); s.TopRejectedSmokePreview = Math.Max(1, s.TopRejectedSmokePreview); diff --git a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs index 45de43d..8c74f9f 100644 --- a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs +++ b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs @@ -302,9 +302,55 @@ private static string BuildRuleCandidateWhere(AnomalyInteractionRule rule, strin predicates.Add($"{EffectiveQuantSql(alias, column)} = {state.CandidateQuantId}"); } + string contextFidelityPredicate = BuildContextFidelityPredicate(rule, alias); + if (!string.IsNullOrWhiteSpace(contextFidelityPredicate)) + predicates.Add(contextFidelityPredicate); + return string.Join(" AND ", predicates); } + internal static string BuildContextFidelityPredicate(AnomalyInteractionRule rule, string? alias) + { + if (!Config.SynergyDetection.ContextScopedRuleApplicationEnabled) + return string.Empty; + + var fidelity = new QuantFidelityComparerService(); + int referenceTier = fidelity.EffectiveTier(rule.ReferenceQuantId); + if (referenceTier < 0) + return string.Empty; + + byte[] lowerFidelityQuantIds = BaselineQuants.All + .Select(x => (Quant: x, Tier: fidelity.EffectiveTier(x.UniqueId))) + .Where(x => !x.Quant.IsHighPrecisionExactAlias && x.Tier >= 0 && x.Tier < referenceTier) + .Select(x => x.Quant.UniqueId) + .Distinct() + .OrderBy(x => x) + .ToArray(); + + if (lowerFidelityQuantIds.Length == 0) + return string.Empty; + + var ruleGroupIds = rule.GroupStates + .Select(x => x.TensorGroupId) + .ToHashSet(); + string lowerIds = string.Join(", ", lowerFidelityQuantIds); + string[] terms = ActiveGroups() + .Where(group => !ruleGroupIds.Contains(group.UniqueId)) + .Select(group => ColumnNameForGroupId(group.UniqueId)) + .Where(column => column != null) + .Select(column => $"CASE WHEN {EffectiveQuantSql(alias, column!)} IN ({lowerIds}) THEN 1 ELSE 0 END") + .ToArray(); + + if (terms.Length == 0) + return string.Empty; + + int maximumBelowReferenceTier = Math.Clamp( + Config.SynergyDetection.MaxNonRuleGroupsBelowReferenceTier, + 0, + terms.Length); + return $"(({string.Join(" + ", terms)}) <= {maximumBelowReferenceTier})"; + } + private static string BuildPairwiseTwinJoinPredicate(AnomalyInteractionRule rule, string candidateAlias, string twinAlias) { if (rule.GroupStates.Count == 0) diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 977d1ec..e2ce355 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -499,6 +499,10 @@ synergy_detection: high_fidelity_max_non_reference_groups_below_q6: 1 mid_fidelity_max_non_reference_groups_below_q6: 3 low_fidelity_enabled: false + # Prevent a rule measured in a high-fidelity blanket from silently affecting + # a much lower-fidelity surrounding mix. Rule-selected groups are excluded. + context_scoped_rule_application_enabled: true + max_non_rule_groups_below_reference_tier: 1 verbose_synergy_logging: true min_smoke_score: 0.55 max_smoke_gap_kld: 0.004 diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 972d5db..f2d2b5a 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -599,6 +599,8 @@ synergy_detection: high_fidelity_max_non_reference_groups_below_q6: 1 mid_fidelity_max_non_reference_groups_below_q6: 3 low_fidelity_enabled: false + context_scoped_rule_application_enabled: true + max_non_rule_groups_below_reference_tier: 1 verbose_synergy_logging: true min_smoke_score: 0.55 max_smoke_gap_kld: 0.004 From 4c6032178d993caeaa4306e15570d08cdf95cc8f Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 06:45:11 -0400 Subject: [PATCH 233/258] Match anomaly rules to effective contexts --- MagicQuant.Tests/AnomalyContextScopeTests.cs | 48 ++++++++++++++- .../Configuration/MagicQuantYamlConfig.cs | 2 +- .../Configuration/MagicQuantYamlLoader.cs | 2 +- .../AnomalyAdjustedPredictionService.cs | 58 ++++++++++--------- MagicQuant/config.default.yaml | 6 +- MagicQuant/config.dev.yaml | 2 +- 6 files changed, 85 insertions(+), 33 deletions(-) diff --git a/MagicQuant.Tests/AnomalyContextScopeTests.cs b/MagicQuant.Tests/AnomalyContextScopeTests.cs index 771d512..ae9f2e2 100644 --- a/MagicQuant.Tests/AnomalyContextScopeTests.cs +++ b/MagicQuant.Tests/AnomalyContextScopeTests.cs @@ -26,13 +26,14 @@ public void BuildContextFidelityPredicate_BoundsOnlyNonRuleGroups() { var config = MagicQuantYamlConfig.CreateDefault(); config.SynergyDetection.ContextScopedRuleApplicationEnabled = true; - config.SynergyDetection.MaxNonRuleGroupsBelowReferenceTier = 1; + config.SynergyDetection.MaxNonRuleGroupContextMismatches = 1; Config.Load(config); Cache.UnusedTensorGroups.Clear(); var rule = new AnomalyInteractionRule { ReferenceQuantId = BaselineQuants.Q8_0.UniqueId, + ReferenceContextKey = $"{TReg.LmHead.UniqueId}:{BaselineQuants.Q4_K_M.UniqueId}", GroupStates = [ new AnomalyInteractionRuleGroupState @@ -47,6 +48,7 @@ public void BuildContextFidelityPredicate_BoundsOnlyNonRuleGroups() string predicate = AnomalyAdjustedPredictionService.BuildContextFidelityPredicate(rule, "c"); Assert.Contains("c.LmHead", predicate, StringComparison.Ordinal); + Assert.Contains($"= {BaselineQuants.Q4_K_M.UniqueId} THEN 0", predicate, StringComparison.Ordinal); Assert.Contains("c.AttnQ", predicate, StringComparison.Ordinal); Assert.DoesNotContain("c.Embeddings", predicate, StringComparison.Ordinal); Assert.EndsWith("<= 1)", predicate, StringComparison.Ordinal); @@ -59,6 +61,50 @@ public void BuildContextFidelityPredicate_BoundsOnlyNonRuleGroups() } } + [Fact] + public void BuildRuleCandidateWhere_UsesEffectiveContextInsteadOfCarrierIdentity() + { + var priorConfig = Config.Current; + var priorUnusedGroups = Cache.UnusedTensorGroups.ToList(); + + try + { + var config = MagicQuantYamlConfig.CreateDefault(); + config.SynergyDetection.ContextScopedRuleApplicationEnabled = true; + config.SynergyDetection.MaxNonRuleGroupContextMismatches = 0; + Config.Load(config); + Cache.UnusedTensorGroups.Clear(); + + var rule = new AnomalyInteractionRule + { + ReferenceQuantId = BaselineQuants.Q4_K_M.UniqueId, + ReferenceContextKey = string.Join("|", TReg.All.Select(x => $"{x.UniqueId}:{BaselineQuants.Q4_K_M.UniqueId}")), + GroupStates = + [ + new AnomalyInteractionRuleGroupState + { + TensorGroupId = TReg.Embeddings.UniqueId, + ReferenceQuantId = BaselineQuants.Q4_K_M.UniqueId, + CandidateQuantId = BaselineQuants.IQ4_NL.UniqueId + } + ] + }; + + string predicate = AnomalyAdjustedPredictionService.BuildRuleCandidateWhere(rule, "c"); + + Assert.DoesNotContain($"c.BaseQuant = {BaselineQuants.Q4_K_M.UniqueId}", predicate, StringComparison.Ordinal); + Assert.Contains("c.Embeddings", predicate, StringComparison.Ordinal); + Assert.Contains("c.LmHead", predicate, StringComparison.Ordinal); + Assert.Contains($"= {BaselineQuants.Q4_K_M.UniqueId} THEN 0", predicate, StringComparison.Ordinal); + } + finally + { + Config.Load(priorConfig); + Cache.UnusedTensorGroups.Clear(); + Cache.UnusedTensorGroups.AddRange(priorUnusedGroups); + } + } + [Fact] public void BuildContextFidelityPredicate_ReturnsEmptyWhenDisabled() { diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 782bbfb..db07af2 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -365,7 +365,7 @@ public sealed class RuntimeSynergyDetectionConfig public int MaxTotalTransferProbesPerRun { get; set; } = 24; public RuntimeSynergyTransferProbeContextStrataConfig TransferProbeContextStrata { get; set; } = new(); public bool ContextScopedRuleApplicationEnabled { get; set; } = true; - public int MaxNonRuleGroupsBelowReferenceTier { get; set; } = 1; + public int MaxNonRuleGroupContextMismatches { get; set; } = 1; public bool VerboseSynergyLogging { get; set; } = true; public double MinSmokeScore { get; set; } = 0.55d; public double MaxSmokeGapKld { get; set; } = 0.004d; diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 46b18cb..695cccc 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -198,7 +198,7 @@ private static void NormalizeSynergyDetection(MagicQuantYamlConfig config) s.TransferProbeContextStrata ??= new RuntimeSynergyTransferProbeContextStrataConfig(); s.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, s.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6); s.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, s.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6); - s.MaxNonRuleGroupsBelowReferenceTier = Math.Clamp(s.MaxNonRuleGroupsBelowReferenceTier, 0, 9); + s.MaxNonRuleGroupContextMismatches = Math.Clamp(s.MaxNonRuleGroupContextMismatches, 0, 9); s.MinSmokeScore = Math.Clamp(s.MinSmokeScore, 0d, 1d); s.MaxSmokeGapKld = Math.Max(0d, s.MaxSmokeGapKld); s.TopRejectedSmokePreview = Math.Max(1, s.TopRejectedSmokePreview); diff --git a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs index 8c74f9f..3dedd8e 100644 --- a/MagicQuant/Services/AnomalyAdjustedPredictionService.cs +++ b/MagicQuant/Services/AnomalyAdjustedPredictionService.cs @@ -273,7 +273,7 @@ await ExecuteAsync(c, $@" return new BroadRuleResult(before, log); } - private static string BuildRuleCandidateWhere(AnomalyInteractionRule rule, string? alias) + internal static string BuildRuleCandidateWhere(AnomalyInteractionRule rule, string? alias) { if (rule.GroupStates.Count == 0) return string.Empty; @@ -289,10 +289,12 @@ private static string BuildRuleCandidateWhere(AnomalyInteractionRule rule, strin var predicates = new List { $"COALESCE({q("IsProtectedAnchor")}, FALSE) = FALSE", - $"{q("BaseRankSafeKld")} IS NOT NULL", - $"{q("BaseQuant")} = {rule.ReferenceQuantId}" + $"{q("BaseRankSafeKld")} IS NOT NULL" }; + if (!Config.SynergyDetection.ContextScopedRuleApplicationEnabled) + predicates.Add($"{q("BaseQuant")} = {rule.ReferenceQuantId}"); + foreach (var state in rule.GroupStates.OrderBy(x => x.SortOrder)) { string? column = ColumnNameForGroupId(state.TensorGroupId); @@ -314,41 +316,45 @@ internal static string BuildContextFidelityPredicate(AnomalyInteractionRule rule if (!Config.SynergyDetection.ContextScopedRuleApplicationEnabled) return string.Empty; - var fidelity = new QuantFidelityComparerService(); - int referenceTier = fidelity.EffectiveTier(rule.ReferenceQuantId); - if (referenceTier < 0) - return string.Empty; - - byte[] lowerFidelityQuantIds = BaselineQuants.All - .Select(x => (Quant: x, Tier: fidelity.EffectiveTier(x.UniqueId))) - .Where(x => !x.Quant.IsHighPrecisionExactAlias && x.Tier >= 0 && x.Tier < referenceTier) - .Select(x => x.Quant.UniqueId) - .Distinct() - .OrderBy(x => x) - .ToArray(); - - if (lowerFidelityQuantIds.Length == 0) - return string.Empty; - var ruleGroupIds = rule.GroupStates .Select(x => x.TensorGroupId) .ToHashSet(); - string lowerIds = string.Join(", ", lowerFidelityQuantIds); + var referenceContext = ParseReferenceContextKey(rule.ReferenceContextKey); string[] terms = ActiveGroups() .Where(group => !ruleGroupIds.Contains(group.UniqueId)) - .Select(group => ColumnNameForGroupId(group.UniqueId)) - .Where(column => column != null) - .Select(column => $"CASE WHEN {EffectiveQuantSql(alias, column!)} IN ({lowerIds}) THEN 1 ELSE 0 END") + .Select(group => new + { + Column = ColumnNameForGroupId(group.UniqueId), + ExpectedQuantId = referenceContext.GetValueOrDefault(group.UniqueId, rule.ReferenceQuantId) + }) + .Where(x => x.Column != null) + .Select(x => $"CASE WHEN {EffectiveQuantSql(alias, x.Column!)} = {x.ExpectedQuantId} THEN 0 ELSE 1 END") .ToArray(); if (terms.Length == 0) return string.Empty; - int maximumBelowReferenceTier = Math.Clamp( - Config.SynergyDetection.MaxNonRuleGroupsBelowReferenceTier, + int maximumContextMismatches = Math.Clamp( + Config.SynergyDetection.MaxNonRuleGroupContextMismatches, 0, terms.Length); - return $"(({string.Join(" + ", terms)}) <= {maximumBelowReferenceTier})"; + return $"(({string.Join(" + ", terms)}) <= {maximumContextMismatches})"; + } + + private static IReadOnlyDictionary ParseReferenceContextKey(string? contextKey) + { + var result = new Dictionary(); + if (string.IsNullOrWhiteSpace(contextKey)) + return result; + + foreach (string item in contextKey.Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + string[] parts = item.Split(':', 2, StringSplitOptions.TrimEntries); + if (parts.Length == 2 && byte.TryParse(parts[0], out byte groupId) && byte.TryParse(parts[1], out byte quantId)) + result[groupId] = quantId; + } + + return result; } private static string BuildPairwiseTwinJoinPredicate(AnomalyInteractionRule rule, string candidateAlias, string twinAlias) diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index e2ce355..3110b78 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -499,10 +499,10 @@ synergy_detection: high_fidelity_max_non_reference_groups_below_q6: 1 mid_fidelity_max_non_reference_groups_below_q6: 3 low_fidelity_enabled: false - # Prevent a rule measured in a high-fidelity blanket from silently affecting - # a much lower-fidelity surrounding mix. Rule-selected groups are excluded. + # Match rules against their measured effective surrounding-group context, + # independent of the search row's carrier quant. Rule-selected groups are excluded. context_scoped_rule_application_enabled: true - max_non_rule_groups_below_reference_tier: 1 + max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true min_smoke_score: 0.55 max_smoke_gap_kld: 0.004 diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index f2d2b5a..23e45d9 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -600,7 +600,7 @@ synergy_detection: mid_fidelity_max_non_reference_groups_below_q6: 3 low_fidelity_enabled: false context_scoped_rule_application_enabled: true - max_non_rule_groups_below_reference_tier: 1 + max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true min_smoke_score: 0.55 max_smoke_gap_kld: 0.004 From 6b897acc9ca30fa44413982ea556af67668ed2a3 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 06:51:49 -0400 Subject: [PATCH 234/258] Probe templates across controlled fidelity contexts --- .../SynergyTransferConfigTests.cs | 37 ++ .../SynergyTransferPlanningTests.cs | 60 +++ MagicQuant/Configs/config.dev.yaml | 7 +- .../config.qwen3-4B-2507-Instruct.dev.yaml | 7 +- .../Configs/config.qwen3.6-27b.dev.yaml | 7 +- .../Configuration/MagicQuantYamlConfig.cs | 5 +- .../Configuration/MagicQuantYamlLoader.cs | 5 +- MagicQuant/Services/AnomalyRuleRepository.cs | 3 +- MagicQuant/Services/AnomalyWorkflowService.cs | 341 +++++++++++++++++- MagicQuant/config.default.yaml | 7 +- MagicQuant/config.dev.yaml | 7 +- 11 files changed, 469 insertions(+), 17 deletions(-) create mode 100644 MagicQuant.Tests/SynergyTransferConfigTests.cs create mode 100644 MagicQuant.Tests/SynergyTransferPlanningTests.cs diff --git a/MagicQuant.Tests/SynergyTransferConfigTests.cs b/MagicQuant.Tests/SynergyTransferConfigTests.cs new file mode 100644 index 0000000..7106332 --- /dev/null +++ b/MagicQuant.Tests/SynergyTransferConfigTests.cs @@ -0,0 +1,37 @@ +using MagicQuant.Configuration; +using Xunit; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace MagicQuant.Tests; + +public sealed class SynergyTransferConfigTests +{ + [Fact] + public void ContextStrata_DeserializeControlledBlanketQuantLists() + { + const string yaml = """ + synergy_detection: + transfer_probe_context_strata: + high_fidelity_reference_quants: [Q6_K, Q5_K] + mid_fidelity_reference_quants: [Q4_K_M] + low_fidelity_reference_quants: [IQ3_S] + low_fidelity_enabled: true + context_scoped_rule_application_enabled: true + max_non_rule_group_context_mismatches: 2 + """; + + var config = new DeserializerBuilder() + .IgnoreUnmatchedProperties() + .WithNamingConvention(UnderscoredNamingConvention.Instance) + .Build() + .Deserialize(yaml); + + Assert.Equal(["Q6_K", "Q5_K"], config.SynergyDetection.TransferProbeContextStrata.HighFidelityReferenceQuants); + Assert.Equal(["Q4_K_M"], config.SynergyDetection.TransferProbeContextStrata.MidFidelityReferenceQuants); + Assert.Equal(["IQ3_S"], config.SynergyDetection.TransferProbeContextStrata.LowFidelityReferenceQuants); + Assert.True(config.SynergyDetection.TransferProbeContextStrata.LowFidelityEnabled); + Assert.True(config.SynergyDetection.ContextScopedRuleApplicationEnabled); + Assert.Equal(2, config.SynergyDetection.MaxNonRuleGroupContextMismatches); + } +} diff --git a/MagicQuant.Tests/SynergyTransferPlanningTests.cs b/MagicQuant.Tests/SynergyTransferPlanningTests.cs new file mode 100644 index 0000000..061c93f --- /dev/null +++ b/MagicQuant.Tests/SynergyTransferPlanningTests.cs @@ -0,0 +1,60 @@ +using MagicQuant.Models; +using MagicQuant.Services; +using MQ.DB; +using MQ.DB.Models; +using Xunit; + +namespace MagicQuant.Tests; + +[Collection(AnomalyContextScopeCollection.Name)] +public sealed class SynergyTransferPlanningTests +{ + [Fact] + public void ControlledTransfer_ChangesOneGroupInsideExplicitLowFidelityBlanket() + { + var priorUnusedGroups = Cache.UnusedTensorGroups.ToList(); + + try + { + Cache.UnusedTensorGroups.Clear(); + + bool built = AnomalyWorkflowService.TryBuildControlledTransferConfig( + BaselineQuants.IQ3_S.UniqueId, + [(TReg.Embeddings.UniqueId, BaselineQuants.IQ4_NL.UniqueId)], + out var reference, + out var probe, + out var changed); + + Assert.True(built); + var movement = new QuantFidelityComparerService(); + Assert.All(movement.ActiveGroups, group => + Assert.Equal(BaselineQuants.IQ3_S.UniqueId, movement.EffectiveQuantId(reference, group))); + Assert.Equal(BaselineQuants.IQ4_NL.UniqueId, movement.EffectiveQuantId(probe, TReg.Embeddings)); + Assert.All(movement.ActiveGroups.Where(x => x.UniqueId != TReg.Embeddings.UniqueId), group => + Assert.Equal(BaselineQuants.IQ3_S.UniqueId, movement.EffectiveQuantId(probe, group))); + + var groupChange = Assert.Single(changed); + Assert.Equal(TReg.Embeddings.UniqueId, groupChange.Group.UniqueId); + Assert.Equal(QuantMovementKind.Upgrade, groupChange.Movement); + } + finally + { + Cache.UnusedTensorGroups.Clear(); + Cache.UnusedTensorGroups.AddRange(priorUnusedGroups); + } + } + + [Fact] + public void ControlledTransfer_SkipsTemplateStateEqualToBlanket() + { + bool built = AnomalyWorkflowService.TryBuildControlledTransferConfig( + BaselineQuants.Q4_K_M.UniqueId, + [(TReg.Embeddings.UniqueId, BaselineQuants.Q4_K_M.UniqueId)], + out _, + out _, + out var changed); + + Assert.False(built); + Assert.Empty(changed); + } +} diff --git a/MagicQuant/Configs/config.dev.yaml b/MagicQuant/Configs/config.dev.yaml index dc43967..3201789 100644 --- a/MagicQuant/Configs/config.dev.yaml +++ b/MagicQuant/Configs/config.dev.yaml @@ -387,9 +387,12 @@ synergy_detection: max_transfer_probes_per_template: 6 max_total_transfer_probes_per_run: 24 transfer_probe_context_strata: - high_fidelity_max_non_reference_groups_below_q6: 1 - mid_fidelity_max_non_reference_groups_below_q6: 3 + high_fidelity_reference_quants: [Q6_K, Q5_K] + mid_fidelity_reference_quants: [Q4_K_M] + low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false + context_scoped_rule_application_enabled: true + max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true min_smoke_score: 0.55 max_smoke_gap_kld: 0.004 diff --git a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml index b1064e9..7f7d673 100644 --- a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml +++ b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml @@ -375,9 +375,12 @@ synergy_detection: max_transfer_probes_per_template: 6 max_total_transfer_probes_per_run: 24 transfer_probe_context_strata: - high_fidelity_max_non_reference_groups_below_q6: 1 - mid_fidelity_max_non_reference_groups_below_q6: 3 + high_fidelity_reference_quants: [Q6_K, Q5_K] + mid_fidelity_reference_quants: [Q4_K_M] + low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false + context_scoped_rule_application_enabled: true + max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true min_smoke_score: 0.55 max_smoke_gap_kld: 0.004 diff --git a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml index a239024..c5a7e21 100644 --- a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml +++ b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml @@ -375,9 +375,12 @@ synergy_detection: max_transfer_probes_per_template: 6 max_total_transfer_probes_per_run: 24 transfer_probe_context_strata: - high_fidelity_max_non_reference_groups_below_q6: 1 - mid_fidelity_max_non_reference_groups_below_q6: 3 + high_fidelity_reference_quants: [Q6_K, Q5_K] + mid_fidelity_reference_quants: [Q4_K_M] + low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false + context_scoped_rule_application_enabled: true + max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true min_smoke_score: 0.55 max_smoke_gap_kld: 0.004 diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index db07af2..6f190b0 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -384,8 +384,9 @@ public sealed class RuntimeSynergyDetectionConfig public sealed class RuntimeSynergyTransferProbeContextStrataConfig { - public int HighFidelityMaxNonReferenceGroupsBelowQ6 { get; set; } = 1; - public int MidFidelityMaxNonReferenceGroupsBelowQ6 { get; set; } = 3; + public List HighFidelityReferenceQuants { get; set; } = ["Q6_K", "Q5_K"]; + public List MidFidelityReferenceQuants { get; set; } = ["Q4_K_M"]; + public List LowFidelityReferenceQuants { get; set; } = ["IQ3_S"]; public bool LowFidelityEnabled { get; set; } = false; } diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 695cccc..cee9efd 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -196,8 +196,9 @@ private static void NormalizeSynergyDetection(MagicQuantYamlConfig config) s.MaxTransferProbesPerTemplate = Math.Max(0, s.MaxTransferProbesPerTemplate); s.MaxTotalTransferProbesPerRun = Math.Max(0, s.MaxTotalTransferProbesPerRun); s.TransferProbeContextStrata ??= new RuntimeSynergyTransferProbeContextStrataConfig(); - s.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, s.TransferProbeContextStrata.HighFidelityMaxNonReferenceGroupsBelowQ6); - s.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6 = Math.Max(0, s.TransferProbeContextStrata.MidFidelityMaxNonReferenceGroupsBelowQ6); + s.TransferProbeContextStrata.HighFidelityReferenceQuants ??= new List(); + s.TransferProbeContextStrata.MidFidelityReferenceQuants ??= new List(); + s.TransferProbeContextStrata.LowFidelityReferenceQuants ??= new List(); s.MaxNonRuleGroupContextMismatches = Math.Clamp(s.MaxNonRuleGroupContextMismatches, 0, 9); s.MinSmokeScore = Math.Clamp(s.MinSmokeScore, 0d, 1d); s.MaxSmokeGapKld = Math.Max(0d, s.MaxSmokeGapKld); diff --git a/MagicQuant/Services/AnomalyRuleRepository.cs b/MagicQuant/Services/AnomalyRuleRepository.cs index f42bbc8..bb681ed 100644 --- a/MagicQuant/Services/AnomalyRuleRepository.cs +++ b/MagicQuant/Services/AnomalyRuleRepository.cs @@ -400,6 +400,7 @@ private static string ResolveRuleType(IReadOnlyList rows) "single" => "SingleGroupInversion", "pair" => "PairSynergy", "composition" => rows.Any(x => x.RuleDirection == AnomalyRuleDirection.Harmful) ? "HarmfulInterferenceComposition" : "CounterfactualSynergyComposition", + "context-transfer" => rows.Any(x => x.RuleDirection == AnomalyRuleDirection.Harmful) ? "HarmfulContextTransfer" : "ContextTransfer", "confirmed-neighborhood" => rows.Any(x => x.Classification == AnomalyProbeClassification.ContaminatingPassenger) ? "ContaminatingPassenger" : "ConfirmedAnomalyNeighborhood", "full" => rows.Any(x => x.Plan.ProbeGroups.Count >= 3) ? "HigherOrderSynergy" : "PairSynergy", "leave-one-out" => "HigherOrderSynergy", @@ -453,4 +454,4 @@ private static double ComputePredictionAdjustment(AnomalyProbeResult result, dou } private readonly record struct AnomalyScope(int ArchitectureFamilyId, int TensorGroupProfileId, uint AiModelHashId, int? ImatrixDefinitionId); -} \ No newline at end of file +} diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs index b828b37..39c7d22 100644 --- a/MagicQuant/Services/AnomalyWorkflowService.cs +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -3,6 +3,7 @@ using System.Numerics; using System.Text.Json; using DuckDB.NET.Data; +using MagicQuant.Configuration; using MagicQuant.Helpers; using MagicQuant.Models; using Microsoft.EntityFrameworkCore; @@ -98,6 +99,14 @@ public async Task RunAsync( var results = await ValidateProbesAsync(probes, ct); + var transferProbes = await PlanSynergyTransferProbesAsync(results, planningDiagnostics, ct); + if (transferProbes.Count > 0) + { + probes = probes.Concat(transferProbes).ToList(); + var transferResults = await ValidateProbesAsync(transferProbes, ct); + results = results.Concat(transferResults).ToList(); + } + var expansionProbes = await PlanConfirmedAnomalyExpansionProbesAsync(results, planningDiagnostics, ct); if (expansionProbes.Count > 0) { @@ -716,6 +725,314 @@ private async Task> PlanProbesAsync( } + private async Task> PlanSynergyTransferProbesAsync( + IReadOnlyList currentResults, + ProbePlanningDiagnostics diagnostics, + CancellationToken ct) + { + var cfg = Config.SynergyDetection; + if (!cfg.Enabled || !cfg.TransferProbeEnabled || cfg.MaxTotalTransferProbesPerRun <= 0) + return new List(); + + var contexts = ResolveTransferTargetContexts(cfg.TransferProbeContextStrata); + if (contexts.Count == 0) + { + AnsiConsole.MarkupLine("[yellow]Synergy transfer probes skipped:[/] no configured context-stratum quant names resolved to active baselines."); + return new List(); + } + + var historicalRules = await _rules.LoadApplicableRulesAsync(ct); + var templates = BuildTransferTemplates(historicalRules, currentResults) + .Where(x => x.Confidence >= cfg.MinConfidenceToScheduleTransferProbe) + .GroupBy(x => x.Key, StringComparer.Ordinal) + .Select(g => g + .OrderByDescending(x => x.Confidence) + .ThenByDescending(x => x.ActualEffectMagnitude) + .First()) + .ToList(); + + if (templates.Count == 0) + { + AnsiConsole.MarkupLine("[grey]Synergy transfer probes:[/] no confirmed templates met the transfer confidence threshold."); + return new List(); + } + + var existingRuleKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); + var candidatesByContext = contexts.ToDictionary( + x => x, + x => BuildTransferCandidatesForContext(x, templates, existingRuleKeys, diagnostics)); + + int globalLimit = Math.Max(0, cfg.MaxTotalTransferProbesPerRun); + int perTemplateLimit = Math.Max(1, cfg.MaxTransferProbesPerTemplate); + var cursors = contexts.ToDictionary(x => x, _ => 0); + var perTemplateCounts = new Dictionary(StringComparer.Ordinal); + var selectedKeys = new HashSet(StringComparer.Ordinal); + var plans = new List(); + + while (plans.Count < globalLimit) + { + bool addedInRound = false; + foreach (var context in contexts) + { + var candidates = candidatesByContext[context]; + while (cursors[context] < candidates.Count) + { + var candidate = candidates[cursors[context]++]; + int used = perTemplateCounts.GetValueOrDefault(candidate.TemplateKey); + if (used >= perTemplateLimit || !selectedKeys.Add(candidate.IdentityKey)) + continue; + + plans.Add(candidate.Plan); + perTemplateCounts[candidate.TemplateKey] = used + 1; + diagnostics.ProbesQueued++; + diagnostics.TransferProbesQueued++; + addedInRound = true; + break; + } + + if (plans.Count >= globalLimit) + break; + } + + if (!addedInRound) + break; + } + + int candidateCount = candidatesByContext.Values.Sum(x => x.Count); + diagnostics.SkippedBudget += Math.Max(0, candidateCount - plans.Count); + AnsiConsole.MarkupLine( + $"[yellow]Synergy context-transfer probes:[/] templates=[cyan]{templates.Count:N0}[/] " + + $"strata=[cyan]{contexts.Count:N0}[/] candidates=[cyan]{candidateCount:N0}[/] " + + $"queued=[cyan]{plans.Count:N0}[/] globalLimit=[cyan]{globalLimit:N0}[/] perTemplateLimit=[cyan]{perTemplateLimit:N0}[/]"); + + foreach (var context in contexts) + { + int queued = plans.Count(x => x.ReferenceConfig.BaseQuant == context.QuantId); + AnsiConsole.MarkupLine( + $"[grey] transfer stratum={Markup.Escape(context.Stratum)} reference={Markup.Escape(SafeName(context.QuantId))} " + + $"candidates={candidatesByContext[context].Count:N0} queued={queued:N0}[/]"); + } + + return plans; + } + + private List BuildTransferCandidatesForContext( + SynergyTransferContext context, + IReadOnlyList templates, + IReadOnlySet existingRuleKeys, + ProbePlanningDiagnostics diagnostics) + { + var candidates = new List(); + int targetTier = _movement.EffectiveTier(context.QuantId); + + foreach (var template in templates) + { + if (template.SourceReferenceQuantId == context.QuantId) + continue; + + if (!TryBuildControlledTransferConfig( + context.QuantId, + template.Groups.Select(x => (x.Group.UniqueId, x.CandidateQuantId)).ToList(), + out var reference, + out var probe, + out var changed) || + changed.Count > Config.AnomalyDetection.MaxProbeGroupCount) + { + continue; + } + + if (ShouldSkipInvalidContextualAnomalyConfig(probe, "synergy-context-transfer", out _)) + { + diagnostics.SkippedInvalidMovement++; + continue; + } + + if (existingRuleKeys.Contains(_rules.BuildRuleSuppressionKey(reference, changed))) + { + diagnostics.SkippedExistingRuleOrSuppression++; + continue; + } + + string identityKey = TensorConfigIdentity.ToKey(reference) + "=>" + TensorConfigIdentity.ToKey(probe); + var analyzedMovement = _movement.Analyze(reference, probe); + var seed = new AnomalySmokeCandidate + { + Source = $"synergy-template-transfer:{template.Source}", + CandidateConfig = probe, + TwinConfig = reference, + Movement = analyzedMovement, + SmokeScore = 1_000_000d + template.Confidence + template.ActualEffectMagnitude, + SmokeStrength = $"ControlledContextTransfer:{context.Stratum}", + SeedClass = AnomalySeedClass.SynergyTransferProbe, + MatchedConfirmedAnomalyPattern = true, + PlannedProbeWillMeasureSize = true, + Message = "Controlled blanket transfer probe: remeasures a confirmed tensor template under a different surrounding-fidelity context." + }; + + var plan = new AnomalyProbePlan + { + Seed = seed, + ReferenceConfig = reference, + ProbeConfig = probe, + ProbeGroups = changed, + ProbeType = "context-transfer", + HypothesisLabel = $"{_movement.DescribeGroups(changed)} in {context.Stratum} {SafeName(context.QuantId)} blanket", + SeedClass = AnomalySeedClass.SynergyTransferProbe, + ProbePriorityClass = AnomalySeedClass.SynergyTransferProbe + }; + + double candidateTierDistance = changed + .Select(x => Math.Abs(_movement.EffectiveTier(x.CandidateQuantId) - targetTier)) + .DefaultIfEmpty(int.MaxValue) + .Average(); + candidates.Add(new SynergyTransferCandidate( + template.Key, + identityKey, + plan, + changed.Count, + candidateTierDistance, + template.Confidence, + template.ActualEffectMagnitude)); + } + + return candidates + .OrderBy(x => x.GroupCount) + .ThenBy(x => x.CandidateTierDistance) + .ThenByDescending(x => x.Confidence) + .ThenByDescending(x => x.ActualEffectMagnitude) + .ThenBy(x => x.IdentityKey, StringComparer.Ordinal) + .ToList(); + } + + internal static bool TryBuildControlledTransferConfig( + byte targetContextQuantId, + IReadOnlyList<(byte TensorGroupId, byte CandidateQuantId)> templateGroups, + out TensorConfig reference, + out TensorConfig probe, + out IReadOnlyList changedGroups) + { + var movementService = new QuantFidelityComparerService(); + reference = movementService.CreateActivatedContextBlanket(targetContextQuantId); + probe = reference; + var changed = new List(); + var activeGroupsById = movementService.ActiveGroups.ToDictionary(x => x.UniqueId); + + foreach (var state in templateGroups.OrderBy(x => x.TensorGroupId)) + { + if (state.CandidateQuantId == targetContextQuantId || + !activeGroupsById.TryGetValue(state.TensorGroupId, out var group)) + { + continue; + } + + var movement = movementService.Compare(targetContextQuantId, state.CandidateQuantId); + if (movement == QuantMovementKind.Unknown || BaselineQuants.IsNativeExactAlias(state.CandidateQuantId)) + continue; + + byte candidateStored = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(state.CandidateQuantId); + changed.Add(new AnomalyChangedGroup + { + Group = group, + ReferenceQuantId = targetContextQuantId, + CandidateQuantId = state.CandidateQuantId, + ReferenceStoredSlot = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(targetContextQuantId), + CandidateStoredSlot = candidateStored, + Movement = movement + }); + probe = movementService.WithStoredSlot(probe, group, candidateStored); + } + + changedGroups = changed; + return changed.Count > 0; + } + + private List BuildTransferTemplates( + IReadOnlyList historicalRules, + IReadOnlyList currentResults) + { + var activeGroupsById = _movement.ActiveGroups.ToDictionary(x => x.UniqueId); + var knownQuantIds = BaselineQuants.All.Select(x => x.UniqueId).ToHashSet(); + var templates = new List(); + + foreach (var rule in historicalRules) + { + var groups = rule.GroupStates + .Where(x => activeGroupsById.ContainsKey(x.TensorGroupId)) + .Where(x => knownQuantIds.Contains(x.CandidateQuantId) && !BaselineQuants.IsNativeExactAlias(x.CandidateQuantId)) + .OrderBy(x => x.TensorGroupId) + .Select(x => new SynergyTransferTemplateGroup(activeGroupsById[x.TensorGroupId], x.CandidateQuantId)) + .ToList(); + if (groups.Count == 0) + continue; + + templates.Add(new SynergyTransferTemplate( + BuildTransferTemplateKey(groups), + $"historical-rule:{rule.Id:N}:{rule.RuleDirection}", + rule.ReferenceQuantId, + rule.Confidence, + Math.Abs(rule.MeanActualGainVsTwin), + groups)); + } + + double effectScale = Math.Max(Config.AnomalyDetection.MinActualGainVsTwinKld, 1e-9d); + foreach (var result in currentResults + .Where(x => x.Accepted && x.RuleDirection != AnomalyRuleDirection.SuppressionOnly) + .Where(x => x.ReferenceSnapshot != null && x.ProbeSnapshot != null)) + { + var groups = result.Plan.ProbeGroups + .Where(x => activeGroupsById.ContainsKey(x.Group.UniqueId)) + .Where(x => knownQuantIds.Contains(x.CandidateQuantId) && !BaselineQuants.IsNativeExactAlias(x.CandidateQuantId)) + .OrderBy(x => x.Group.UniqueId) + .Select(x => new SynergyTransferTemplateGroup(activeGroupsById[x.Group.UniqueId], x.CandidateQuantId)) + .ToList(); + if (groups.Count == 0) + continue; + + double confidence = Math.Clamp(0.70d + (0.15d * Math.Clamp(Math.Abs(result.ActualGainVsTwin) / effectScale, 0d, 2d)), 0d, 1d); + templates.Add(new SynergyTransferTemplate( + BuildTransferTemplateKey(groups), + $"current-probe:{result.Plan.ProbeType}:{result.RuleDirection}", + result.Plan.ReferenceConfig.BaseQuant, + confidence, + Math.Abs(result.ActualGainVsTwin), + groups)); + } + + return templates; + } + + private static string BuildTransferTemplateKey(IReadOnlyList groups) + => string.Join("|", groups.OrderBy(x => x.Group.UniqueId).Select(x => $"{x.Group.UniqueId}:{x.CandidateQuantId}")); + + private static List ResolveTransferTargetContexts(RuntimeSynergyTransferProbeContextStrataConfig strata) + { + var byName = BaselineQuants.All + .SelectMany(x => x.Names.Select(name => (Name: name, Quant: x))) + .GroupBy(x => x.Name, StringComparer.OrdinalIgnoreCase) + .ToDictionary(x => x.Key, x => x.First().Quant, StringComparer.OrdinalIgnoreCase); + var contexts = new List(); + + void add(IEnumerable names, string stratum) + { + foreach (string name in names.Where(x => !string.IsNullOrWhiteSpace(x))) + { + if (!byName.TryGetValue(name.Trim(), out var quant) || BaselineQuants.IsNativeExactAlias(quant.UniqueId)) + continue; + + if (contexts.All(x => x.QuantId != quant.UniqueId)) + contexts.Add(new SynergyTransferContext(quant.UniqueId, stratum)); + } + } + + add(strata.HighFidelityReferenceQuants, "high-fidelity"); + add(strata.MidFidelityReferenceQuants, "mid-fidelity"); + if (strata.LowFidelityEnabled) + add(strata.LowFidelityReferenceQuants, "low-fidelity"); + + return contexts; + } + + private async Task> PlanConfirmedAnomalyExpansionProbesAsync( IReadOnlyList initialResults, @@ -2137,6 +2454,28 @@ private static string SafeName(byte quantId) } + private sealed record SynergyTransferContext(byte QuantId, string Stratum); + + private sealed record SynergyTransferTemplateGroup(TensorGroup Group, byte CandidateQuantId); + + private sealed record SynergyTransferTemplate( + string Key, + string Source, + byte SourceReferenceQuantId, + double Confidence, + double ActualEffectMagnitude, + IReadOnlyList Groups); + + private sealed record SynergyTransferCandidate( + string TemplateKey, + string IdentityKey, + AnomalyProbePlan Plan, + int GroupCount, + double CandidateTierDistance, + double Confidence, + double ActualEffectMagnitude); + + private sealed class RejectedSmokePreview { public RejectedSmokePreview( @@ -2209,4 +2548,4 @@ private sealed record PredictionDuckRow( ulong PredictedSizeBytes, double PredictionConfidence, ulong PredictionRank); -} \ No newline at end of file +} diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 3110b78..bf27377 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -495,9 +495,12 @@ synergy_detection: transfer_probe_enabled: true max_transfer_probes_per_template: 6 max_total_transfer_probes_per_run: 24 + # Controlled all-surrounding-groups blankets. This measures whether a template's + # marginal behavior transfers—or flips—without importing an old winning mixture. transfer_probe_context_strata: - high_fidelity_max_non_reference_groups_below_q6: 1 - mid_fidelity_max_non_reference_groups_below_q6: 3 + high_fidelity_reference_quants: [Q6_K, Q5_K] + mid_fidelity_reference_quants: [Q4_K_M] + low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false # Match rules against their measured effective surrounding-group context, # independent of the search row's carrier quant. Rule-selected groups are excluded. diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 23e45d9..2e28d13 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -596,9 +596,10 @@ synergy_detection: max_transfer_probes_per_template: 6 max_total_transfer_probes_per_run: 24 transfer_probe_context_strata: - high_fidelity_max_non_reference_groups_below_q6: 1 - mid_fidelity_max_non_reference_groups_below_q6: 3 - low_fidelity_enabled: false + high_fidelity_reference_quants: [Q6_K, Q5_K] + mid_fidelity_reference_quants: [Q4_K_M] + low_fidelity_reference_quants: [IQ3_S] + low_fidelity_enabled: true context_scoped_rule_application_enabled: true max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true From 5f6f9445d77996d0eeabec55ac42a981857796e6 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 06:58:55 -0400 Subject: [PATCH 235/258] Restore deterministic full-suite validation --- MQ.DB/Data/MagicQuantContext.cs | 18 ++- MagicQuant.Tests/AssemblyInfo.cs | 3 + .../AuthorityUsageRegressionTests.cs | 7 +- .../BaselineCandidatePolicyTests.cs | 91 ++++++++------ ...zationRunAndBuildHybridsRegressionTests.cs | 119 ++++++++++++------ 5 files changed, 150 insertions(+), 88 deletions(-) create mode 100644 MagicQuant.Tests/AssemblyInfo.cs diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index 5f05430..22145bc 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -10,7 +10,7 @@ public class MagicQuantContext : DbContext // -------------------------------------------------------- // Self-Initialization Logic // -------------------------------------------------------- - private static bool _isInitialized = false; + private static readonly HashSet InitializedDatabaseDirectories = new(StringComparer.Ordinal); private static readonly object _initLock = new(); public MagicQuantContext() @@ -30,19 +30,25 @@ private void EnsureInitialized() if (IsDesignTime()) return; - if (_isInitialized) - return; - + string initializationKey = ResolveDatabaseDirectory(); lock (_initLock) { - if (_isInitialized) + if (InitializedDatabaseDirectories.Contains(initializationKey)) return; InitializeDatabase(); - _isInitialized = true; + InitializedDatabaseDirectories.Add(initializationKey); } } + private static string ResolveDatabaseDirectory() + { + string directory = string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory) + ? Directory.GetCurrentDirectory() + : Cache.MagicQuantDirectory; + return Path.GetFullPath(directory); + } + private void InitializeDatabase() { var directory = Cache.MagicQuantDirectory; diff --git a/MagicQuant.Tests/AssemblyInfo.cs b/MagicQuant.Tests/AssemblyInfo.cs new file mode 100644 index 0000000..2171200 --- /dev/null +++ b/MagicQuant.Tests/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using Xunit; + +[assembly: CollectionBehavior(DisableTestParallelization = true)] diff --git a/MagicQuant.Tests/AuthorityUsageRegressionTests.cs b/MagicQuant.Tests/AuthorityUsageRegressionTests.cs index e3a03ae..fdda4c7 100644 --- a/MagicQuant.Tests/AuthorityUsageRegressionTests.cs +++ b/MagicQuant.Tests/AuthorityUsageRegressionTests.cs @@ -7,11 +7,12 @@ public class AuthorityUsageRegressionTests [Fact] public void ComboGenerationPaths_DoNotUseLegacyAllAllowedHybridQuantsAuthority() { + string repositoryRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../..")); var files = new[] { - Path.Combine("..", "MagicQuant", "Helpers", "ComboLogic.cs"), - Path.Combine("..", "MagicQuant", "Helpers", "TensorConfigGenerator.cs"), - Path.Combine("..", "MagicQuant", "Services", "IsolationOptimizationService.cs") + Path.Combine(repositoryRoot, "MagicQuant", "Helpers", "ComboLogic.cs"), + Path.Combine(repositoryRoot, "MagicQuant", "Helpers", "TensorConfigGenerator.cs"), + Path.Combine(repositoryRoot, "MagicQuant", "Services", "IsolationOptimizationService.cs") }; foreach (var file in files) diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs index 44ba74e..0df97b0 100644 --- a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -26,22 +26,12 @@ public void Iq1Families_AreRegisteredAsImatrixLearningAndExplicitCandidates() } [Fact] - public void GetPureBaselineCandidates_NoImatrix_ReturnsExactlyIq4Xs() + public void GetPureBaselineCandidates_NoImatrix_ReturnsAllNonImatrixLearningBaselines() { var ids = BaselineQuants.GetPureBaselineCandidates(hasUsableImatrix: false) .Select(x => x.UniqueId) .ToArray(); - Assert.Equal([BaselineQuants.IQ4_XS.UniqueId], ids); - } - - [Fact] - public void GetCombinationCarrierBaselines_NoImatrix_ReturnsExactlySixExpectedBaselines() - { - var ids = BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix: false) - .Select(x => x.UniqueId) - .ToArray(); - Assert.Equal( [ BaselineQuants.Q8_0.UniqueId, @@ -49,12 +39,24 @@ public void GetCombinationCarrierBaselines_NoImatrix_ReturnsExactlySixExpectedBa BaselineQuants.Q5_K.UniqueId, BaselineQuants.Q4_K_M.UniqueId, BaselineQuants.IQ4_NL.UniqueId, - BaselineQuants.IQ4_XS.UniqueId + BaselineQuants.IQ4_XS.UniqueId, + BaselineQuants.Q5_K_S.UniqueId, + BaselineQuants.Q4_K_S.UniqueId ], ids); } [Fact] - public void GetGroupCombinationCandidates_NoImatrixNoHighPrecision_ReturnsExactlySixExpectedBaselines() + public void GetCombinationCarrierBaselines_UsesCanonicalQ8Carrier() + { + var ids = BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix: false) + .Select(x => x.UniqueId) + .ToArray(); + + Assert.Equal([BaselineQuants.Q8_0.UniqueId], ids); + } + + [Fact] + public void GetGroupCombinationCandidates_NoImatrix_ReturnsAllEligibleFourBitAndHigherBaselines() { var ids = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix: false, allowHighPrecisionHybrids: false) .Select(x => x.UniqueId) @@ -62,12 +64,14 @@ public void GetGroupCombinationCandidates_NoImatrixNoHighPrecision_ReturnsExactl Assert.Equal( [ - BaselineQuants.Q8_0.UniqueId, - BaselineQuants.Q6_K.UniqueId, - BaselineQuants.Q5_K.UniqueId, - BaselineQuants.Q4_K_M.UniqueId, + BaselineQuants.IQ4_XS.UniqueId, BaselineQuants.IQ4_NL.UniqueId, - BaselineQuants.IQ4_XS.UniqueId + BaselineQuants.Q4_K_S.UniqueId, + BaselineQuants.Q4_K_M.UniqueId, + BaselineQuants.Q5_K_S.UniqueId, + BaselineQuants.Q5_K.UniqueId, + BaselineQuants.Q6_K.UniqueId, + BaselineQuants.Q8_0.UniqueId ], ids); Assert.DoesNotContain(BaselineQuants.IQ3_S.UniqueId, ids); @@ -81,7 +85,7 @@ public void GetGroupCombinationCandidates_NoImatrixNoHighPrecision_ReturnsExactl } [Fact] - public void RuntimeSearchSpace_GetActiveCombinationBaselines_ReturnsExactlySixExpectedBaselines() + public void RuntimeSearchSpace_GetActiveCombinationBaselines_ReturnsCanonicalQ8Carrier() { RuntimeSearchSpace.ResetForNewModel(); RuntimeSearchSpace.SetImatrixAvailability(false); @@ -90,15 +94,7 @@ public void RuntimeSearchSpace_GetActiveCombinationBaselines_ReturnsExactlySixEx .Select(x => x.UniqueId) .ToArray(); - Assert.Equal( - [ - BaselineQuants.Q8_0.UniqueId, - BaselineQuants.Q6_K.UniqueId, - BaselineQuants.Q5_K.UniqueId, - BaselineQuants.Q4_K_M.UniqueId, - BaselineQuants.IQ4_NL.UniqueId, - BaselineQuants.IQ4_XS.UniqueId - ], ids); + Assert.Equal([BaselineQuants.Q8_0.UniqueId], ids); } [Fact] @@ -123,7 +119,7 @@ public void CandidateBanAuthority_DrivesAllowedCandidateSet() var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); var attnQIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.AttnQ.UniqueId); - Assert.DoesNotContain(BaselineQuants.Q6_K.UniqueId, allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.Q6_K), allowed[attnQIndex]); } [Fact] @@ -136,8 +132,8 @@ public void ComboLogic_WhenHighPrecisionDisabled_DoesNotInjectBf16OrF16() var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); var attnQIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.AttnQ.UniqueId); - Assert.DoesNotContain(BaselineQuants.BF16_Hybrid.UniqueId, allowed[attnQIndex]); - Assert.DoesNotContain(BaselineQuants.F16_Hybrid.UniqueId, allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.BF16_Hybrid), allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.F16_Hybrid), allowed[attnQIndex]); } [Fact] @@ -145,12 +141,31 @@ public void ComboLogic_UsesCandidateLevelBannedGroups() { RuntimeSearchSpace.ResetForNewModel(); RuntimeSearchSpace.SetImatrixAvailability(false); - - var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); - var moeRouterIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.MoeRouter.UniqueId); - - Assert.DoesNotContain(BaselineQuants.Q5_K.UniqueId, allowed[moeRouterIndex]); - Assert.DoesNotContain(BaselineQuants.IQ4_NL.UniqueId, allowed[moeRouterIndex]); - Assert.DoesNotContain(BaselineQuants.IQ4_XS.UniqueId, allowed[moeRouterIndex]); + var candidate = BaselineQuants.RegisterCustomExternalBaseline(new BaselineQuants.ExternalBaselineRegistration + { + CanonicalKey = "test:moe-router-banned", + DisplayName = "TEST-Q5-BANNED", + QuantizeBaseArgumentName = "Q5_K", + Repository = "test/repository", + RepositoryFileName = "test-q5.gguf", + OwnerShortName = "test", + BaselineFamilyName = "Q5_K", + TensorScheme = TensorWeightScheme.Q5_K, + AddAsGroupCandidate = true, + BitRange = 5, + BannedGroupIds = [TReg.MoeRouter.UniqueId] + }); + + try + { + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); + var moeRouterIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.MoeRouter.UniqueId); + + Assert.DoesNotContain(BaselineQuants.EncodeTensorConfigGroupSlot(candidate), allowed[moeRouterIndex]); + } + finally + { + BaselineQuants.ResetDynamicCustomBaselines(); + } } } diff --git a/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs b/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs index a1de027..8ab7d25 100644 --- a/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs +++ b/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs @@ -1,4 +1,5 @@ using MagicQuant.Commands; +using MagicQuant.Configuration; using MagicQuant.Models; using Microsoft.EntityFrameworkCore; using MQ.DB; @@ -15,58 +16,94 @@ public async Task QuantizationRun_PersistsAndLoads_ImatrixDefinitionForeignKey() { string tempRoot = Path.Combine(Path.GetTempPath(), "mq-quant-run-fk-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(tempRoot); + string? priorMagicQuantDirectory = Cache.MagicQuantDirectory; - Cache.MagicQuantDirectory = tempRoot; + try + { + Cache.MagicQuantDirectory = tempRoot; - await using var db = new MagicQuantContext(); + await using var db = new MagicQuantContext(); - var model = new AiModelHash - { - UniqueHash = "model-" + Guid.NewGuid().ToString("N") - }; + var model = new AiModelHash + { + UniqueHash = "model-" + Guid.NewGuid().ToString("N") + }; - var combo = new TensorCombo(); - db.AiModelHashes.Add(model); - db.TensorCombos.Add(combo); - await db.SaveChangesAsync(); + var combo = new TensorCombo(); + var architecture = new ArchitectureFamily + { + NormalizedName = "test-architecture-" + Guid.NewGuid().ToString("N"), + DisplayName = "Test architecture", + TensorSignatureHash = "signature-" + Guid.NewGuid().ToString("N"), + TensorCount = 1 + }; + var profile = new TensorGroupProfile + { + ArchitectureFamily = architecture, + FingerprintHash = "profile-" + Guid.NewGuid().ToString("N"), + SnapshotJson = "{}" + }; + db.AiModelHashes.Add(model); + db.TensorCombos.Add(combo); + db.ArchitectureFamilies.Add(architecture); + db.TensorGroupProfiles.Add(profile); + await db.SaveChangesAsync(); - var imatrix = new ImatrixDefinition - { - AiModelHashId = model.Id, - IdentityHash = "imatrix-" + Guid.NewGuid().ToString("N"), - SourceKind = "test" - }; - db.ImatrixDefinitions.Add(imatrix); - await db.SaveChangesAsync(); + var imatrix = new ImatrixDefinition + { + AiModelHashId = model.Id, + IdentityHash = "imatrix-" + Guid.NewGuid().ToString("N"), + SourceKind = "test" + }; + db.ImatrixDefinitions.Add(imatrix); + await db.SaveChangesAsync(); - var run = new QuantizationRun - { - AiModelHashId = model.Id, - ImatrixDefinitionId = imatrix.Id, - TensorComboId = combo.Id, - StartedUtc = DateTime.UtcNow.AddSeconds(-1), - CompletedUtc = DateTime.UtcNow, - DurationMs = 1000, - Succeeded = true, - OutputModelPath = Path.Combine(tempRoot, "output.gguf") - }; - db.QuantizationRuns.Add(run); - await db.SaveChangesAsync(); + var run = new QuantizationRun + { + ArchitectureFamilyId = architecture.Id, + TensorGroupProfileId = profile.Id, + AiModelHashId = model.Id, + ImatrixDefinitionId = imatrix.Id, + TensorComboId = combo.Id, + StartedUtc = DateTime.UtcNow.AddSeconds(-1), + CompletedUtc = DateTime.UtcNow, + DurationMs = 1000, + Succeeded = true, + OutputModelPath = Path.Combine(tempRoot, "output.gguf") + }; + db.QuantizationRuns.Add(run); + await db.SaveChangesAsync(); - var loaded = await db.QuantizationRuns - .Include(x => x.ImatrixDefinition) - .SingleAsync(x => x.Id == run.Id); + var loaded = await db.QuantizationRuns + .Include(x => x.ImatrixDefinition) + .SingleAsync(x => x.Id == run.Id); - Assert.Equal(imatrix.Id, loaded.ImatrixDefinitionId); - Assert.NotNull(loaded.ImatrixDefinition); - Assert.Equal(imatrix.IdentityHash, loaded.ImatrixDefinition!.IdentityHash); + Assert.Equal(imatrix.Id, loaded.ImatrixDefinitionId); + Assert.NotNull(loaded.ImatrixDefinition); + Assert.Equal(imatrix.IdentityHash, loaded.ImatrixDefinition!.IdentityHash); + } + finally + { + Cache.MagicQuantDirectory = priorMagicQuantDirectory; + if (Directory.Exists(tempRoot)) + Directory.Delete(tempRoot, recursive: true); + } } [Fact] - public async Task BuildHybrids_RunWithoutHelp_ThrowsNotImplementedException() + public async Task BuildHybrids_RunWithoutModel_RoutesThroughEvolutionValidation() { - var command = new BuildHybrids(); - var ex = await Assert.ThrowsAsync(() => command.Run(new List())); - Assert.Contains("disabled", ex.Message, StringComparison.OrdinalIgnoreCase); + var priorConfig = Config.Current; + try + { + Config.Load(MagicQuantYamlConfig.CreateDefault()); + var command = new BuildHybrids(); + var ex = await Assert.ThrowsAsync(() => command.Run(new List())); + Assert.Contains("model directory", ex.Message, StringComparison.OrdinalIgnoreCase); + } + finally + { + Config.Load(priorConfig); + } } } From 0c73ac4c70d3379a0d99465e05ce151293ae9ac7 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 07:18:30 -0400 Subject: [PATCH 236/258] Probe context-dependent quant rank reversals --- MagicQuant.Tests/AnomalyContextScopeTests.cs | 42 +++ .../SynergyTransferConfigTests.cs | 8 + .../SynergyTransferPlanningTests.cs | 40 ++ MagicQuant/Configs/config.dev.yaml | 4 + .../config.qwen3-4B-2507-Instruct.dev.yaml | 4 + .../Configs/config.qwen3.6-27b.dev.yaml | 4 + .../Configuration/MagicQuantYamlConfig.cs | 4 + .../Configuration/MagicQuantYamlLoader.cs | 9 + MagicQuant/Models/AnomalyDetectionModels.cs | 3 +- MagicQuant/Services/AnomalyRuleRepository.cs | 17 +- MagicQuant/Services/AnomalyWorkflowService.cs | 344 +++++++++++++++++- MagicQuant/config.default.yaml | 7 + MagicQuant/config.dev.yaml | 4 + 13 files changed, 479 insertions(+), 11 deletions(-) diff --git a/MagicQuant.Tests/AnomalyContextScopeTests.cs b/MagicQuant.Tests/AnomalyContextScopeTests.cs index ae9f2e2..d4b4a59 100644 --- a/MagicQuant.Tests/AnomalyContextScopeTests.cs +++ b/MagicQuant.Tests/AnomalyContextScopeTests.cs @@ -1,4 +1,5 @@ using MagicQuant.Configuration; +using MagicQuant.Models; using MagicQuant.Services; using MQ.DB; using MQ.DB.Models; @@ -137,4 +138,45 @@ public void BuildContextFidelityPredicate_ReturnsEmptyWhenDisabled() Config.Load(priorConfig); } } + + [Fact] + public void RuleSuppressionKey_DistinguishesEffectiveSurroundingContext() + { + var priorUnusedGroups = Cache.UnusedTensorGroups.ToList(); + + try + { + Cache.UnusedTensorGroups.Clear(); + var movement = new QuantFidelityComparerService(); + var repository = new AnomalyRuleRepository(movement); + var q8Context = movement.CreateActivatedContextBlanket(BaselineQuants.Q8_0.UniqueId); + var q4PassengerContext = movement.WithStoredSlot( + q8Context, + TReg.LmHead, + BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.Q4_K_M)); + var changed = new List + { + new() + { + Group = TReg.Embeddings, + ReferenceQuantId = BaselineQuants.Q8_0.UniqueId, + CandidateQuantId = BaselineQuants.Q6_K.UniqueId, + ReferenceStoredSlot = BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.Q8_0), + CandidateStoredSlot = BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.Q6_K), + Movement = QuantMovementKind.Downgrade + } + }; + + string q8Key = repository.BuildRuleSuppressionKey(q8Context, changed); + string q4PassengerKey = repository.BuildRuleSuppressionKey(q4PassengerContext, changed); + + Assert.NotEqual(q8Key, q4PassengerKey); + Assert.Contains($"{TReg.LmHead.UniqueId}:{BaselineQuants.Q4_K_M.UniqueId}", q4PassengerKey, StringComparison.Ordinal); + } + finally + { + Cache.UnusedTensorGroups.Clear(); + Cache.UnusedTensorGroups.AddRange(priorUnusedGroups); + } + } } diff --git a/MagicQuant.Tests/SynergyTransferConfigTests.cs b/MagicQuant.Tests/SynergyTransferConfigTests.cs index 7106332..e7c67ec 100644 --- a/MagicQuant.Tests/SynergyTransferConfigTests.cs +++ b/MagicQuant.Tests/SynergyTransferConfigTests.cs @@ -17,6 +17,10 @@ public void ContextStrata_DeserializeControlledBlanketQuantLists() mid_fidelity_reference_quants: [Q4_K_M] low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: true + exploratory_context_pair_enabled: true + max_exploratory_context_pairs_per_run: 11 + exploratory_pair_bit_ranges: [3, 4] + exploratory_pair_context_strata: [mid-fidelity, low-fidelity] context_scoped_rule_application_enabled: true max_non_rule_group_context_mismatches: 2 """; @@ -31,6 +35,10 @@ public void ContextStrata_DeserializeControlledBlanketQuantLists() Assert.Equal(["Q4_K_M"], config.SynergyDetection.TransferProbeContextStrata.MidFidelityReferenceQuants); Assert.Equal(["IQ3_S"], config.SynergyDetection.TransferProbeContextStrata.LowFidelityReferenceQuants); Assert.True(config.SynergyDetection.TransferProbeContextStrata.LowFidelityEnabled); + Assert.True(config.SynergyDetection.ExploratoryContextPairEnabled); + Assert.Equal(11, config.SynergyDetection.MaxExploratoryContextPairsPerRun); + Assert.Equal([3, 4], config.SynergyDetection.ExploratoryPairBitRanges); + Assert.Equal(["mid-fidelity", "low-fidelity"], config.SynergyDetection.ExploratoryPairContextStrata); Assert.True(config.SynergyDetection.ContextScopedRuleApplicationEnabled); Assert.Equal(2, config.SynergyDetection.MaxNonRuleGroupContextMismatches); } diff --git a/MagicQuant.Tests/SynergyTransferPlanningTests.cs b/MagicQuant.Tests/SynergyTransferPlanningTests.cs index 061c93f..83dddab 100644 --- a/MagicQuant.Tests/SynergyTransferPlanningTests.cs +++ b/MagicQuant.Tests/SynergyTransferPlanningTests.cs @@ -57,4 +57,44 @@ public void ControlledTransfer_SkipsTemplateStateEqualToBlanket() Assert.False(built); Assert.Empty(changed); } + + [Fact] + public void ControlledRankPair_ComparesTwoRecipesInsideSameLowFidelityBlanket() + { + var priorUnusedGroups = Cache.UnusedTensorGroups.ToList(); + + try + { + Cache.UnusedTensorGroups.Clear(); + + bool built = AnomalyWorkflowService.TryBuildControlledRankPairConfig( + BaselineQuants.IQ3_S.UniqueId, + TReg.Embeddings, + BaselineQuants.Q4_K_M.UniqueId, + BaselineQuants.IQ4_NL.UniqueId, + out var reference, + out var probe, + out var changed); + + Assert.True(built); + var movement = new QuantFidelityComparerService(); + Assert.Equal(BaselineQuants.Q4_K_M.UniqueId, movement.EffectiveQuantId(reference, TReg.Embeddings)); + Assert.Equal(BaselineQuants.IQ4_NL.UniqueId, movement.EffectiveQuantId(probe, TReg.Embeddings)); + Assert.All(movement.ActiveGroups.Where(x => x.UniqueId != TReg.Embeddings.UniqueId), group => + { + Assert.Equal(BaselineQuants.IQ3_S.UniqueId, movement.EffectiveQuantId(reference, group)); + Assert.Equal(BaselineQuants.IQ3_S.UniqueId, movement.EffectiveQuantId(probe, group)); + }); + + var groupChange = Assert.Single(changed); + Assert.Equal(BaselineQuants.Q4_K_M.UniqueId, groupChange.ReferenceQuantId); + Assert.Equal(BaselineQuants.IQ4_NL.UniqueId, groupChange.CandidateQuantId); + Assert.Equal(QuantMovementKind.LateralOrEquivalent, groupChange.Movement); + } + finally + { + Cache.UnusedTensorGroups.Clear(); + Cache.UnusedTensorGroups.AddRange(priorUnusedGroups); + } + } } diff --git a/MagicQuant/Configs/config.dev.yaml b/MagicQuant/Configs/config.dev.yaml index 3201789..e4086c0 100644 --- a/MagicQuant/Configs/config.dev.yaml +++ b/MagicQuant/Configs/config.dev.yaml @@ -391,6 +391,10 @@ synergy_detection: mid_fidelity_reference_quants: [Q4_K_M] low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false + exploratory_context_pair_enabled: true + max_exploratory_context_pairs_per_run: 14 + exploratory_pair_bit_ranges: [4] + exploratory_pair_context_strata: [mid-fidelity, low-fidelity] context_scoped_rule_application_enabled: true max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true diff --git a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml index 7f7d673..e2f409b 100644 --- a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml +++ b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml @@ -379,6 +379,10 @@ synergy_detection: mid_fidelity_reference_quants: [Q4_K_M] low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false + exploratory_context_pair_enabled: true + max_exploratory_context_pairs_per_run: 14 + exploratory_pair_bit_ranges: [4] + exploratory_pair_context_strata: [mid-fidelity, low-fidelity] context_scoped_rule_application_enabled: true max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true diff --git a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml index c5a7e21..57a136e 100644 --- a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml +++ b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml @@ -379,6 +379,10 @@ synergy_detection: mid_fidelity_reference_quants: [Q4_K_M] low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false + exploratory_context_pair_enabled: true + max_exploratory_context_pairs_per_run: 14 + exploratory_pair_bit_ranges: [4] + exploratory_pair_context_strata: [mid-fidelity, low-fidelity] context_scoped_rule_application_enabled: true max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 6f190b0..422c290 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -364,6 +364,10 @@ public sealed class RuntimeSynergyDetectionConfig public int MaxTransferProbesPerTemplate { get; set; } = 6; public int MaxTotalTransferProbesPerRun { get; set; } = 24; public RuntimeSynergyTransferProbeContextStrataConfig TransferProbeContextStrata { get; set; } = new(); + public bool ExploratoryContextPairEnabled { get; set; } = true; + public int MaxExploratoryContextPairsPerRun { get; set; } = 14; + public List ExploratoryPairBitRanges { get; set; } = [4]; + public List ExploratoryPairContextStrata { get; set; } = ["mid-fidelity", "low-fidelity"]; public bool ContextScopedRuleApplicationEnabled { get; set; } = true; public int MaxNonRuleGroupContextMismatches { get; set; } = 1; public bool VerboseSynergyLogging { get; set; } = true; diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index cee9efd..22e6e94 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -199,6 +199,15 @@ private static void NormalizeSynergyDetection(MagicQuantYamlConfig config) s.TransferProbeContextStrata.HighFidelityReferenceQuants ??= new List(); s.TransferProbeContextStrata.MidFidelityReferenceQuants ??= new List(); s.TransferProbeContextStrata.LowFidelityReferenceQuants ??= new List(); + s.MaxExploratoryContextPairsPerRun = Math.Max(0, s.MaxExploratoryContextPairsPerRun); + s.ExploratoryPairBitRanges ??= new List(); + s.ExploratoryPairBitRanges = s.ExploratoryPairBitRanges.Where(x => x is >= 1 and <= 16).Distinct().OrderBy(x => x).ToList(); + s.ExploratoryPairContextStrata ??= new List(); + s.ExploratoryPairContextStrata = s.ExploratoryPairContextStrata + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Select(x => x.Trim().ToLowerInvariant()) + .Distinct(StringComparer.Ordinal) + .ToList(); s.MaxNonRuleGroupContextMismatches = Math.Clamp(s.MaxNonRuleGroupContextMismatches, 0, 9); s.MinSmokeScore = Math.Clamp(s.MinSmokeScore, 0d, 1d); s.MaxSmokeGapKld = Math.Max(0d, s.MaxSmokeGapKld); diff --git a/MagicQuant/Models/AnomalyDetectionModels.cs b/MagicQuant/Models/AnomalyDetectionModels.cs index c2fe62a..06a4dac 100644 --- a/MagicQuant/Models/AnomalyDetectionModels.cs +++ b/MagicQuant/Models/AnomalyDetectionModels.cs @@ -203,6 +203,7 @@ public sealed class ProbePlanningDiagnostics public int ExpansionProbesQueued { get; set; } public int CompositionProbesQueued { get; set; } public int TransferProbesQueued { get; set; } + public int ExploratoryPairProbesQueued { get; set; } public int SkippedContaminationSuppression { get; set; } } @@ -237,4 +238,4 @@ public sealed class SynergyWingSummary public int ValidationFailureCount { get; set; } public int FinalSurvivorsFromZone { get; set; } public string Explanation { get; set; } = string.Empty; -} \ No newline at end of file +} diff --git a/MagicQuant/Services/AnomalyRuleRepository.cs b/MagicQuant/Services/AnomalyRuleRepository.cs index bb681ed..2f7d266 100644 --- a/MagicQuant/Services/AnomalyRuleRepository.cs +++ b/MagicQuant/Services/AnomalyRuleRepository.cs @@ -163,6 +163,7 @@ public async Task> UpsertRulesFromResultsA string groupSetHash = _movement.BuildChangedGroupHash(probeGroups); string direction = first.RuleDirection.ToString(); byte referenceQuantId = first.Plan.ReferenceConfig.BaseQuant; + string referenceContextKey = _movement.ReferenceContextKey(first.Plan.ReferenceConfig); var rule = await db.AnomalyInteractionRules .Include(x => x.GroupStates) @@ -173,6 +174,7 @@ public async Task> UpsertRulesFromResultsA x.ImatrixDefinitionId == scope.ImatrixDefinitionId && x.BenchmarkCategory == (byte)BenchmarkCategory.General && x.ReferenceQuantId == referenceQuantId && + x.ReferenceContextKey == referenceContextKey && x.GroupSetHash == groupSetHash && x.RuleDirection == direction, ct); @@ -188,7 +190,7 @@ public async Task> UpsertRulesFromResultsA ImatrixDefinitionId = scope.ImatrixDefinitionId, BenchmarkCategory = (byte)BenchmarkCategory.General, ReferenceQuantId = referenceQuantId, - ReferenceContextKey = _movement.ReferenceContextKey(first.Plan.ReferenceConfig), + ReferenceContextKey = referenceContextKey, ReferenceEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), JsonOptions), CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions), InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions), @@ -216,7 +218,7 @@ public async Task> UpsertRulesFromResultsA rule.ShrinkFactor = Config.AnomalyDetection.AnomalyAdjustmentShrinkFactor; rule.Confidence = ComputeConfidence(rows); rule.AppliedPredictionSpaceAdjustmentKld = ComputePredictionAdjustment(first, rule.Confidence); - rule.ReferenceContextKey = _movement.ReferenceContextKey(first.Plan.ReferenceConfig); + rule.ReferenceContextKey = referenceContextKey; rule.ReferenceEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ReferenceConfig), JsonOptions); rule.CandidateEffectiveGroupsJson = JsonSerializer.Serialize(_movement.BuildEffectiveGroupVector(first.Plan.ProbeConfig), JsonOptions); rule.InactiveGroupsJson = JsonSerializer.Serialize(_movement.BuildInactiveGroupList(), JsonOptions); @@ -299,16 +301,16 @@ public async Task> LoadExistingRuleSuppressionKeysAsync(Cancella .Where(x => x.ImatrixDefinitionId == scope.ImatrixDefinitionId) .Where(x => x.BenchmarkCategory == (byte)BenchmarkCategory.General) .Where(x => x.RuleStatus != AnomalyRuleStatus.Retired.ToString()) - .Select(x => new { x.ReferenceQuantId, x.GroupSetHash }) + .Select(x => new { x.ReferenceContextKey, x.GroupSetHash }) .ToListAsync(ct); return rows - .Select(x => BuildRuleSuppressionKey(x.ReferenceQuantId, x.GroupSetHash)) + .Select(x => BuildRuleSuppressionKey(x.ReferenceContextKey, x.GroupSetHash)) .ToHashSet(StringComparer.Ordinal); } public string BuildRuleSuppressionKey(TensorConfig reference, IReadOnlyList groups) - => BuildRuleSuppressionKey(reference.BaseQuant, _movement.BuildChangedGroupHash(groups)); + => BuildRuleSuppressionKey(_movement.ReferenceContextKey(reference), _movement.BuildChangedGroupHash(groups)); public async Task HasSuppressionOrRuleAsync( TensorConfig reference, @@ -319,8 +321,8 @@ public async Task HasSuppressionOrRuleAsync( return keys.Contains(BuildRuleSuppressionKey(reference, groups)); } - private static string BuildRuleSuppressionKey(byte referenceQuantId, string groupSetHash) - => $"ref={referenceQuantId}|groups={groupSetHash}"; + private static string BuildRuleSuppressionKey(string referenceContextKey, string groupSetHash) + => $"context={referenceContextKey}|groups={groupSetHash}"; private async Task ResolveScopeAsync(MagicQuantContext db, CancellationToken ct) { @@ -401,6 +403,7 @@ private static string ResolveRuleType(IReadOnlyList rows) "pair" => "PairSynergy", "composition" => rows.Any(x => x.RuleDirection == AnomalyRuleDirection.Harmful) ? "HarmfulInterferenceComposition" : "CounterfactualSynergyComposition", "context-transfer" => rows.Any(x => x.RuleDirection == AnomalyRuleDirection.Harmful) ? "HarmfulContextTransfer" : "ContextTransfer", + "context-rank-pair" => rows.Any(x => x.RuleDirection == AnomalyRuleDirection.Harmful) ? "HarmfulContextRankReversal" : "ContextRankPair", "confirmed-neighborhood" => rows.Any(x => x.Classification == AnomalyProbeClassification.ContaminatingPassenger) ? "ContaminatingPassenger" : "ConfirmedAnomalyNeighborhood", "full" => rows.Any(x => x.Plan.ProbeGroups.Count >= 3) ? "HigherOrderSynergy" : "PairSynergy", "leave-one-out" => "HigherOrderSynergy", diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs index 39c7d22..38989b5 100644 --- a/MagicQuant/Services/AnomalyWorkflowService.cs +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -99,7 +99,18 @@ public async Task RunAsync( var results = await ValidateProbesAsync(probes, ct); - var transferProbes = await PlanSynergyTransferProbesAsync(results, planningDiagnostics, ct); + var exploratoryPairProbes = await PlanExploratoryContextPairProbesAsync(planningDiagnostics, ct); + if (exploratoryPairProbes.Count > 0) + { + probes = probes.Concat(exploratoryPairProbes).ToList(); + var exploratoryPairResults = await ValidateProbesAsync(exploratoryPairProbes, ct); + results = results.Concat(exploratoryPairResults).ToList(); + } + + int remainingTransferBudget = Math.Max( + 0, + Config.SynergyDetection.MaxTotalTransferProbesPerRun - exploratoryPairProbes.Count); + var transferProbes = await PlanSynergyTransferProbesAsync(results, planningDiagnostics, remainingTransferBudget, ct); if (transferProbes.Count > 0) { probes = probes.Concat(transferProbes).ToList(); @@ -725,13 +736,270 @@ private async Task> PlanProbesAsync( } + private async Task> PlanExploratoryContextPairProbesAsync( + ProbePlanningDiagnostics diagnostics, + CancellationToken ct) + { + var cfg = Config.SynergyDetection; + int limit = Math.Min( + Math.Max(0, cfg.MaxExploratoryContextPairsPerRun), + Math.Max(0, cfg.MaxTotalTransferProbesPerRun)); + if (!cfg.Enabled || !cfg.TransferProbeEnabled || !cfg.ExploratoryContextPairEnabled || limit <= 0) + return new List(); + + var requestedStrata = cfg.ExploratoryPairContextStrata.ToHashSet(StringComparer.OrdinalIgnoreCase); + var contexts = ResolveTransferTargetContexts(cfg.TransferProbeContextStrata) + .Where(x => requestedStrata.Contains(x.Stratum)) + .ToList(); + if (contexts.Count == 0 || cfg.ExploratoryPairBitRanges.Count == 0) + return new List(); + + var isolationPairs = await LoadExploratoryIsolationPairsAsync( + cfg.ExploratoryPairBitRanges.ToHashSet(), + ct); + if (isolationPairs.Count == 0) + { + AnsiConsole.MarkupLine("[grey]Exploratory context-rank pairs:[/] no non-equivalent same-bit isolation winner/runner-up pairs were available."); + return new List(); + } + + var existingRuleKeys = await _rules.LoadExistingRuleSuppressionKeysAsync(ct); + var candidatesByContext = contexts.ToDictionary( + x => x, + x => isolationPairs + .Select(pair => TryBuildExploratoryContextPairPlan(x, pair, existingRuleKeys, diagnostics)) + .Where(x => x != null) + .Select(x => x!) + .OrderBy(x => x.Pair.Group.UniqueId) + .ThenByDescending(x => x.Pair.IsolationGap) + .ThenBy(x => x.IdentityKey, StringComparer.Ordinal) + .ToList()); + + var cursors = contexts.ToDictionary(x => x, _ => 0); + var plans = new List(); + var identities = new HashSet(StringComparer.Ordinal); + while (plans.Count < limit) + { + bool added = false; + foreach (var context in contexts) + { + var candidates = candidatesByContext[context]; + while (cursors[context] < candidates.Count) + { + var candidate = candidates[cursors[context]++]; + if (!identities.Add(candidate.IdentityKey)) + continue; + + plans.Add(candidate.Plan); + diagnostics.ProbesQueued++; + diagnostics.TransferProbesQueued++; + diagnostics.ExploratoryPairProbesQueued++; + added = true; + break; + } + + if (plans.Count >= limit) + break; + } + + if (!added) + break; + } + + int candidateCount = candidatesByContext.Values.Sum(x => x.Count); + diagnostics.SkippedBudget += Math.Max(0, candidateCount - plans.Count); + AnsiConsole.MarkupLine( + $"[yellow]Exploratory context-rank pairs:[/] isolationPairs=[cyan]{isolationPairs.Count:N0}[/] " + + $"strata=[cyan]{contexts.Count:N0}[/] candidates=[cyan]{candidateCount:N0}[/] " + + $"queued=[cyan]{plans.Count:N0}[/] budget=[cyan]{limit:N0}[/]"); + + foreach (var context in contexts) + { + int queued = plans.Count(x => x.ReferenceConfig.BaseQuant == context.QuantId); + AnsiConsole.MarkupLine( + $"[grey] rank-pair stratum={Markup.Escape(context.Stratum)} reference={Markup.Escape(SafeName(context.QuantId))} " + + $"candidates={candidatesByContext[context].Count:N0} queued={queued:N0}[/]"); + } + + return plans; + } + + private async Task> LoadExploratoryIsolationPairsAsync( + IReadOnlySet bitRanges, + CancellationToken ct) + { + var pairs = new List(); + var nativeExactScheme = TensorWeightScheme.GetCurrentNativePrecisionScheme(); + + foreach (var group in _movement.ActiveGroups.OrderBy(x => x.UniqueId)) + { + var observations = new List(); + // Deliberately include isolation-pruned candidates here. A candidate that loses in + // native/F16 surroundings is exactly the candidate that may reverse rank in a Q3 + // context; requiring it to survive that earlier pruning would make this probe blind. + // Invalid/unlearnable candidates still fall out because they have no isolation snapshot. + foreach (var baseline in RuntimeSearchSpace.GetRealExplicitCombinationCandidatesForGroup(group) + .Where(x => bitRanges.Contains(x.BitRange)) + .OrderBy(x => x.UniqueId)) + { + var isolation = HybridQuant.CreateExactBlanket( + BaselineQuants.Q8_0, + _movement.ActiveGroups, + nativeExactScheme); + isolation.SetLearnedCandidateOverride(group, baseline); + var snapshot = await _repository.LoadBenchmarkSnapshotAsync((TensorConfig)isolation, ct); + if (snapshot != null) + observations.Add(new ExploratoryIsolationObservation(group, baseline, snapshot)); + } + + var ordered = observations + .OrderBy(x => x.Snapshot.Kld) + .ThenBy(x => x.Snapshot.SizeBytes) + .ThenBy(x => x.Baseline.UniqueId) + .ToList(); + if (ordered.Count < 2) + continue; + + var winner = ordered[0]; + var runnerUp = ordered.Skip(1).FirstOrDefault(x => !IsolationOutcomesEquivalent(winner, x)); + if (runnerUp == null) + continue; + + pairs.Add(new ExploratoryIsolationPair( + group, + winner, + runnerUp, + Math.Max(0d, runnerUp.Snapshot.Kld - winner.Snapshot.Kld))); + } + + return pairs; + } + + private static bool IsolationOutcomesEquivalent( + ExploratoryIsolationObservation left, + ExploratoryIsolationObservation right) + { + return left.Snapshot.SizeBytes == right.Snapshot.SizeBytes && + Math.Abs(left.Snapshot.Kld - right.Snapshot.Kld) <= 1e-12d; + } + + private ExploratoryContextPairCandidate? TryBuildExploratoryContextPairPlan( + SynergyTransferContext context, + ExploratoryIsolationPair pair, + IReadOnlySet existingRuleKeys, + ProbePlanningDiagnostics diagnostics) + { + if (!TryBuildControlledRankPairConfig( + context.QuantId, + pair.Group, + pair.RunnerUp.Baseline.UniqueId, + pair.Winner.Baseline.UniqueId, + out var reference, + out var probe, + out var changed)) + { + diagnostics.SkippedInvalidMovement++; + return null; + } + + if (ShouldSkipInvalidContextualAnomalyConfig(reference, "exploratory-context-rank-reference", out _) || + ShouldSkipInvalidContextualAnomalyConfig(probe, "exploratory-context-rank-probe", out _)) + { + diagnostics.SkippedInvalidMovement++; + return null; + } + + if (existingRuleKeys.Contains(_rules.BuildRuleSuppressionKey(reference, changed))) + { + diagnostics.SkippedExistingRuleOrSuppression++; + return null; + } + + string identityKey = TensorConfigIdentity.ToKey(reference) + "=>" + TensorConfigIdentity.ToKey(probe); + var movement = _movement.Analyze(reference, probe); + var seed = new AnomalySmokeCandidate + { + Source = "exploratory-isolation-rank-transfer", + CandidateConfig = probe, + TwinConfig = reference, + Movement = movement, + CandidatePredictedKld = pair.Winner.Snapshot.Kld, + TwinPredictedKld = pair.RunnerUp.Snapshot.Kld, + CandidatePredictedSizeBytes = pair.Winner.Snapshot.SizeBytes, + TwinPredictedSizeBytes = pair.RunnerUp.Snapshot.SizeBytes, + PredictionSpaceGapVsTwin = pair.Winner.Snapshot.Kld - pair.RunnerUp.Snapshot.Kld, + SmokeScore = 2_000_000d + pair.IsolationGap, + SmokeStrength = $"IsolationRankPair:{context.Stratum}", + SeedClass = AnomalySeedClass.SynergyTransferProbe, + MatchedConfirmedAnomalyPattern = false, + PlannedProbeWillMeasureSize = true, + Message = "Remeasures a same-bit native-isolation winner and runner-up head-to-head inside a controlled surrounding-fidelity context." + }; + var plan = new AnomalyProbePlan + { + Seed = seed, + ReferenceConfig = reference, + ProbeConfig = probe, + ProbeGroups = changed, + ProbeType = "context-rank-pair", + HypothesisLabel = $"{pair.Group.Name}: isolation winner {pair.Winner.Baseline.Names[0]} vs runner-up {pair.RunnerUp.Baseline.Names[0]} in {context.Stratum} {SafeName(context.QuantId)} blanket", + SeedClass = AnomalySeedClass.SynergyTransferProbe, + ProbePriorityClass = AnomalySeedClass.SynergyTransferProbe + }; + + return new ExploratoryContextPairCandidate(identityKey, pair, plan); + } + + internal static bool TryBuildControlledRankPairConfig( + byte targetContextQuantId, + TensorGroup group, + byte referenceCandidateQuantId, + byte probeCandidateQuantId, + out TensorConfig reference, + out TensorConfig probe, + out IReadOnlyList changedGroups) + { + var movementService = new QuantFidelityComparerService(); + reference = movementService.CreateActivatedContextBlanket(targetContextQuantId); + probe = reference; + changedGroups = Array.Empty(); + + if (!movementService.ActiveGroups.Any(x => x.UniqueId == group.UniqueId) || + referenceCandidateQuantId == probeCandidateQuantId || + BaselineQuants.IsNativeExactAlias(referenceCandidateQuantId) || + BaselineQuants.IsNativeExactAlias(probeCandidateQuantId)) + { + return false; + } + + byte referenceStored = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(referenceCandidateQuantId); + byte probeStored = BaselineQuants.EncodeTensorConfigGroupSlotBaselineId(probeCandidateQuantId); + reference = movementService.WithStoredSlot(reference, group, referenceStored); + probe = movementService.WithStoredSlot(probe, group, probeStored); + changedGroups = + [ + new AnomalyChangedGroup + { + Group = group, + ReferenceQuantId = referenceCandidateQuantId, + CandidateQuantId = probeCandidateQuantId, + ReferenceStoredSlot = referenceStored, + CandidateStoredSlot = probeStored, + Movement = movementService.Compare(referenceCandidateQuantId, probeCandidateQuantId) + } + ]; + return changedGroups[0].Movement != QuantMovementKind.Unknown; + } + + private async Task> PlanSynergyTransferProbesAsync( IReadOnlyList currentResults, ProbePlanningDiagnostics diagnostics, + int availableBudget, CancellationToken ct) { var cfg = Config.SynergyDetection; - if (!cfg.Enabled || !cfg.TransferProbeEnabled || cfg.MaxTotalTransferProbesPerRun <= 0) + if (!cfg.Enabled || !cfg.TransferProbeEnabled || cfg.MaxTotalTransferProbesPerRun <= 0 || availableBudget <= 0) return new List(); var contexts = ResolveTransferTargetContexts(cfg.TransferProbeContextStrata); @@ -762,7 +1030,7 @@ private async Task> PlanSynergyTransferProbesAsync( x => x, x => BuildTransferCandidatesForContext(x, templates, existingRuleKeys, diagnostics)); - int globalLimit = Math.Max(0, cfg.MaxTotalTransferProbesPerRun); + int globalLimit = Math.Min(Math.Max(0, cfg.MaxTotalTransferProbesPerRun), Math.Max(0, availableBudget)); int perTemplateLimit = Math.Max(1, cfg.MaxTransferProbesPerTemplate); var cursors = contexts.ToDictionary(x => x, _ => 0); var perTemplateCounts = new Dictionary(StringComparer.Ordinal); @@ -1462,6 +1730,9 @@ private AnomalyProbeResult ClassifyProbe( }; } + if (plan.ProbeType == "context-rank-pair") + return ClassifyContextRankPair(plan, reference, probe); + double gain = reference.Kld - probe.Kld; bool sameOrSmaller = probe.SizeBytes <= reference.SizeBytes; if (sameOrSmaller && gain >= Config.AnomalyDetection.MinActualGainVsTwinKld) @@ -1531,6 +1802,57 @@ private AnomalyProbeResult ClassifyProbe( }; } + private static AnomalyProbeResult ClassifyContextRankPair( + AnomalyProbePlan plan, + BenchmarkSnapshotRecord reference, + BenchmarkSnapshotRecord probe) + { + double gain = reference.Kld - probe.Kld; + double threshold = Math.Max(Config.AnomalyDetection.MinActualGainVsTwinKld, 1e-12d); + if (gain >= threshold) + { + return new AnomalyProbeResult + { + Plan = plan, + ReferenceSnapshot = reference, + ProbeSnapshot = probe, + Classification = AnomalyProbeClassification.ContextOnly, + RuleDirection = AnomalyRuleDirection.Beneficial, + Accepted = true, + ActualGainVsTwin = gain, + Message = "The native-isolation winner retained a material KLD advantage over its same-bit runner-up in this measured context." + }; + } + + if (-gain >= threshold) + { + return new AnomalyProbeResult + { + Plan = plan, + ReferenceSnapshot = reference, + ProbeSnapshot = probe, + Classification = AnomalyProbeClassification.HarmfulInteraction, + RuleDirection = AnomalyRuleDirection.Harmful, + Accepted = true, + ActualGainVsTwin = gain, + Message = "Context rank reversal: the native-isolation winner became materially worse than its same-bit runner-up in this measured context." + }; + } + + return new AnomalyProbeResult + { + Plan = plan, + ReferenceSnapshot = reference, + ProbeSnapshot = probe, + Classification = AnomalyProbeClassification.NormalGravity, + RuleDirection = AnomalyRuleDirection.SuppressionOnly, + Accepted = false, + ActualGainVsTwin = gain, + FailureCode = "CONTEXT_RANK_PAIR_INCONCLUSIVE", + Message = "The same-bit context pair did not separate beyond the configured KLD evidence threshold." + }; + } + private async Task EmitQ8ContextReferenceDriftDiagnosticsAsync( IReadOnlyDictionary byKey, @@ -2475,6 +2797,22 @@ private sealed record SynergyTransferCandidate( double Confidence, double ActualEffectMagnitude); + private sealed record ExploratoryIsolationObservation( + TensorGroup Group, + BaselineQuants Baseline, + BenchmarkSnapshotRecord Snapshot); + + private sealed record ExploratoryIsolationPair( + TensorGroup Group, + ExploratoryIsolationObservation Winner, + ExploratoryIsolationObservation RunnerUp, + double IsolationGap); + + private sealed record ExploratoryContextPairCandidate( + string IdentityKey, + ExploratoryIsolationPair Pair, + AnomalyProbePlan Plan); + private sealed class RejectedSmokePreview { diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index bf27377..3f28c0d 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -502,6 +502,13 @@ synergy_detection: mid_fidelity_reference_quants: [Q4_K_M] low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false + # Remeasure the best and runner-up same-bit isolation recipes head-to-head inside + # controlled contexts. This detects context-dependent rank flips without replaying + # an old winning mixture. Low-fidelity contexts remain governed by the opt-in above. + exploratory_context_pair_enabled: true + max_exploratory_context_pairs_per_run: 14 + exploratory_pair_bit_ranges: [4] + exploratory_pair_context_strata: [mid-fidelity, low-fidelity] # Match rules against their measured effective surrounding-group context, # independent of the search row's carrier quant. Rule-selected groups are excluded. context_scoped_rule_application_enabled: true diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 2e28d13..6130723 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -600,6 +600,10 @@ synergy_detection: mid_fidelity_reference_quants: [Q4_K_M] low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: true + exploratory_context_pair_enabled: true + max_exploratory_context_pairs_per_run: 14 + exploratory_pair_bit_ranges: [4] + exploratory_pair_context_strata: [mid-fidelity, low-fidelity] context_scoped_rule_application_enabled: true max_non_rule_group_context_mismatches: 1 verbose_synergy_logging: true From 13d9bd066d28e20628cf08217039d8ab98106875 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 08:05:02 -0400 Subject: [PATCH 237/258] Prefer size-matched context probe contrasts --- MagicQuant/Services/AnomalyWorkflowService.cs | 34 +++++++++++-------- MagicQuant/config.default.yaml | 2 +- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs index 38989b5..8aec6be 100644 --- a/MagicQuant/Services/AnomalyWorkflowService.cs +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -759,7 +759,7 @@ private async Task> PlanExploratoryContextPairProbesAsync ct); if (isolationPairs.Count == 0) { - AnsiConsole.MarkupLine("[grey]Exploratory context-rank pairs:[/] no non-equivalent same-bit isolation winner/runner-up pairs were available."); + AnsiConsole.MarkupLine("[grey]Exploratory context-rank pairs:[/] no non-equivalent same-bit isolation winner/size-matched-contender pairs were available."); return new List(); } @@ -861,15 +861,21 @@ private async Task> LoadExploratoryIsolationPairs continue; var winner = ordered[0]; - var runnerUp = ordered.Skip(1).FirstOrDefault(x => !IsolationOutcomesEquivalent(winner, x)); - if (runnerUp == null) + var contender = ordered + .Skip(1) + .Where(x => !IsolationOutcomesEquivalent(winner, x)) + .OrderBy(x => Math.Abs((double)x.Snapshot.SizeBytes - winner.Snapshot.SizeBytes)) + .ThenBy(x => x.Snapshot.Kld) + .ThenBy(x => x.Baseline.UniqueId) + .FirstOrDefault(); + if (contender == null) continue; pairs.Add(new ExploratoryIsolationPair( group, winner, - runnerUp, - Math.Max(0d, runnerUp.Snapshot.Kld - winner.Snapshot.Kld))); + contender, + Math.Max(0d, contender.Snapshot.Kld - winner.Snapshot.Kld))); } return pairs; @@ -892,7 +898,7 @@ private static bool IsolationOutcomesEquivalent( if (!TryBuildControlledRankPairConfig( context.QuantId, pair.Group, - pair.RunnerUp.Baseline.UniqueId, + pair.Contender.Baseline.UniqueId, pair.Winner.Baseline.UniqueId, out var reference, out var probe, @@ -924,16 +930,16 @@ private static bool IsolationOutcomesEquivalent( TwinConfig = reference, Movement = movement, CandidatePredictedKld = pair.Winner.Snapshot.Kld, - TwinPredictedKld = pair.RunnerUp.Snapshot.Kld, + TwinPredictedKld = pair.Contender.Snapshot.Kld, CandidatePredictedSizeBytes = pair.Winner.Snapshot.SizeBytes, - TwinPredictedSizeBytes = pair.RunnerUp.Snapshot.SizeBytes, - PredictionSpaceGapVsTwin = pair.Winner.Snapshot.Kld - pair.RunnerUp.Snapshot.Kld, + TwinPredictedSizeBytes = pair.Contender.Snapshot.SizeBytes, + PredictionSpaceGapVsTwin = pair.Winner.Snapshot.Kld - pair.Contender.Snapshot.Kld, SmokeScore = 2_000_000d + pair.IsolationGap, SmokeStrength = $"IsolationRankPair:{context.Stratum}", SeedClass = AnomalySeedClass.SynergyTransferProbe, MatchedConfirmedAnomalyPattern = false, PlannedProbeWillMeasureSize = true, - Message = "Remeasures a same-bit native-isolation winner and runner-up head-to-head inside a controlled surrounding-fidelity context." + Message = "Remeasures a same-bit native-isolation winner and its closest-size non-equivalent contender head-to-head inside a controlled surrounding-fidelity context." }; var plan = new AnomalyProbePlan { @@ -942,7 +948,7 @@ private static bool IsolationOutcomesEquivalent( ProbeConfig = probe, ProbeGroups = changed, ProbeType = "context-rank-pair", - HypothesisLabel = $"{pair.Group.Name}: isolation winner {pair.Winner.Baseline.Names[0]} vs runner-up {pair.RunnerUp.Baseline.Names[0]} in {context.Stratum} {SafeName(context.QuantId)} blanket", + HypothesisLabel = $"{pair.Group.Name}: isolation winner {pair.Winner.Baseline.Names[0]} vs closest-size contender {pair.Contender.Baseline.Names[0]} in {context.Stratum} {SafeName(context.QuantId)} blanket", SeedClass = AnomalySeedClass.SynergyTransferProbe, ProbePriorityClass = AnomalySeedClass.SynergyTransferProbe }; @@ -1820,7 +1826,7 @@ private static AnomalyProbeResult ClassifyContextRankPair( RuleDirection = AnomalyRuleDirection.Beneficial, Accepted = true, ActualGainVsTwin = gain, - Message = "The native-isolation winner retained a material KLD advantage over its same-bit runner-up in this measured context." + Message = "The native-isolation winner retained a material KLD advantage over its closest-size same-bit contender in this measured context." }; } @@ -1835,7 +1841,7 @@ private static AnomalyProbeResult ClassifyContextRankPair( RuleDirection = AnomalyRuleDirection.Harmful, Accepted = true, ActualGainVsTwin = gain, - Message = "Context rank reversal: the native-isolation winner became materially worse than its same-bit runner-up in this measured context." + Message = "Context rank reversal: the native-isolation winner became materially worse than its closest-size same-bit contender in this measured context." }; } @@ -2805,7 +2811,7 @@ private sealed record ExploratoryIsolationObservation( private sealed record ExploratoryIsolationPair( TensorGroup Group, ExploratoryIsolationObservation Winner, - ExploratoryIsolationObservation RunnerUp, + ExploratoryIsolationObservation Contender, double IsolationGap); private sealed record ExploratoryContextPairCandidate( diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 3f28c0d..0d8cb2a 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -502,7 +502,7 @@ synergy_detection: mid_fidelity_reference_quants: [Q4_K_M] low_fidelity_reference_quants: [IQ3_S] low_fidelity_enabled: false - # Remeasure the best and runner-up same-bit isolation recipes head-to-head inside + # Remeasure the best and closest-size non-equivalent same-bit isolation recipes head-to-head inside # controlled contexts. This detects context-dependent rank flips without replaying # an old winning mixture. Low-fidelity contexts remain governed by the opt-in above. exploratory_context_pair_enabled: true From 91d35ae4dfac2863fff41316dc80573e448fd175 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 10:23:04 -0400 Subject: [PATCH 238/258] Prefer shared GPUs for singleton benchmarks --- MagicQuant.Tests/BenchmarkGpuPlanningTests.cs | 18 ++++++++++ MagicQuant/Services/BenchmarkGpuPlanning.cs | 11 ++++++ MagicQuant/Services/BenchmarkService.cs | 28 +++++++++------ MagicQuant/Services/QuantizationService.cs | 36 +++++++++++++++---- 4 files changed, 77 insertions(+), 16 deletions(-) diff --git a/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs b/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs index 02ba076..baedf3f 100644 --- a/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs +++ b/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs @@ -36,6 +36,24 @@ public void EstimateIndependentCrossover_UsesMeasuredPerSlotScaling() Assert.InRange(gib, 22d, 27d); } + [Theory] + [InlineData(true, 20_000_000_000UL, true)] + [InlineData(false, 20_000_000_000UL, false)] + [InlineData(true, 26_000_000_000UL, false)] + public void ShouldUseIndependentTopology_RequiresConcurrentBatchIntent( + bool allowIndependentTopology, + ulong modelSizeBytes, + bool expected) + { + bool result = BenchmarkGpuPlanner.ShouldUseIndependentTopology( + modelSizeBytes, + independentMaxModelSizeBytes: 25_000_000_000UL, + independentSlotCount: 2, + allowIndependentTopology); + + Assert.Equal(expected, result); + } + [Fact] public async Task ResourceScheduler_ReservesDisjointSingleGpuSlotsConcurrently() { diff --git a/MagicQuant/Services/BenchmarkGpuPlanning.cs b/MagicQuant/Services/BenchmarkGpuPlanning.cs index 661365f..9d378d2 100644 --- a/MagicQuant/Services/BenchmarkGpuPlanning.cs +++ b/MagicQuant/Services/BenchmarkGpuPlanning.cs @@ -160,6 +160,17 @@ internal static class BenchmarkGpuPlanner { internal const double DefaultIndependentSpeedupMargin = 1.10d; + public static bool ShouldUseIndependentTopology( + ulong modelSizeBytes, + ulong independentMaxModelSizeBytes, + int independentSlotCount, + bool allowIndependentTopology) + => allowIndependentTopology && + independentMaxModelSizeBytes > 0 && + modelSizeBytes > 0 && + modelSizeBytes <= independentMaxModelSizeBytes && + independentSlotCount > 0; + public static int ResolveNglForModel( ulong q8ModelSizeBytes, int q8StableNgl, diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 7c4bd55..cd39f5c 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -1120,17 +1120,18 @@ private async Task ProbePerplexityAtFixedNglAsync( private static async ValueTask AcquireBenchmarkSlotAsync( ulong modelSizeBytes, + bool allowIndependentTopology, CancellationToken ct = default) { if (_currentPlan == null) throw new InvalidOperationException( "Benchmark execution plan has not been initialized. Call EnsureExecutionPlanAsync() first."); - BenchmarkTopologyProfile profile = - _currentPlan.IndependentMaxModelSizeBytes > 0 && - modelSizeBytes > 0 && - modelSizeBytes <= _currentPlan.IndependentMaxModelSizeBytes && - _currentPlan.IndependentProfile.Slots.Count > 0 + BenchmarkTopologyProfile profile = BenchmarkGpuPlanner.ShouldUseIndependentTopology( + modelSizeBytes, + _currentPlan.IndependentMaxModelSizeBytes, + _currentPlan.IndependentProfile.Slots.Count, + allowIndependentTopology) ? _currentPlan.IndependentProfile : _currentPlan.SharedProfile; @@ -1297,7 +1298,8 @@ public async Task RunAllBenchmarksAsync( int? startNgl = null, string? klLogitsDir = null, bool saveLogits = false, - IReadOnlyCollection? domainsOverride = null) + IReadOnlyCollection? domainsOverride = null, + bool allowIndependentGpuTopology = true) { Directory.CreateDirectory(benchDir); @@ -1314,7 +1316,8 @@ public async Task RunAllBenchmarksAsync( klLogitsDir: klLogitsDir, saveLogits: saveLogits, requestedDomains: requestedDomains, - requireKld: requireKld); + requireKld: requireKld, + allowIndependentGpuTopology: allowIndependentGpuTopology); } using var db = new MagicQuantContext(); @@ -1409,7 +1412,9 @@ await SaveBenchmarkToDbAsync( } ulong modelSizeBytes = TryGetModelSize(modelPath); - await using var slotLease = await AcquireBenchmarkSlotAsync(modelSizeBytes); + await using var slotLease = await AcquireBenchmarkSlotAsync( + modelSizeBytes, + allowIndependentGpuTopology); var slot = slotLease.Slot; int initialNgl = ResolveDynamicNglForModel(modelSizeBytes, slot); @@ -1545,7 +1550,8 @@ private async Task RunAllBenchmarksTransientAsync( string? klLogitsDir, bool saveLogits, IReadOnlyCollection requestedDomains, - bool requireKld) + bool requireKld, + bool allowIndependentGpuTopology) { if (TryReadExistingBenchmarkArtifacts(benchDir, requestedDomains, requireKld, out var reused)) { @@ -1562,7 +1568,9 @@ private async Task RunAllBenchmarksTransientAsync( } ulong modelSizeBytes = TryGetModelSize(modelPath); - await using var slotLease = await AcquireBenchmarkSlotAsync(modelSizeBytes); + await using var slotLease = await AcquireBenchmarkSlotAsync( + modelSizeBytes, + allowIndependentGpuTopology); var slot = slotLease.Slot; int initialNgl = ResolveDynamicNglForModel(modelSizeBytes, slot); diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 05b2be7..7599194 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -254,7 +254,11 @@ await Parallel.ForEachAsync( }, async (baselinePlan, token) => { - records.Add(await ExecutePlanAsync(baselinePlan, stageProgress, token)); + records.Add(await ExecutePlanAsync( + baselinePlan, + stageProgress, + allowIndependentGpuTopology: learnableBaselinePlans.Count > 1, + ct: token)); }); var remainingPlans = plans.Except(learnableBaselinePlans).ToList(); @@ -302,12 +306,21 @@ await Parallel.ForEachAsync( new ParallelOptions { MaxDegreeOfParallelism = workerCount, CancellationToken = ct }, async (group, token) => { - records.Add(await ExecutePlanAsync(group.Source, stageProgress, token)); + records.Add(await ExecutePlanAsync( + group.Source, + stageProgress, + allowIndependentGpuTopology: primaryGroups.Count > 1, + ct: token)); foreach (var duplicatePlan in group.Duplicates) { token.ThrowIfCancellationRequested(); - records.Add(await ExecuteDuplicatePlanAsync(group.Source, duplicatePlan, stageProgress, token)); + records.Add(await ExecuteDuplicatePlanAsync( + group.Source, + duplicatePlan, + stageProgress, + allowIndependentGpuTopology: primaryGroups.Count > 1, + ct: token)); } }); @@ -338,6 +351,7 @@ private int CalculateBatchWorkerCount(int itemCount) private async Task ExecutePlanAsync( RequiredSamplePlan plan, StageProgressTracker? progress, + bool allowIndependentGpuTopology, CancellationToken ct) { var record = new SampleProcessingRecord @@ -349,7 +363,10 @@ private async Task ExecutePlanAsync( try { - var state = await ProcessHybridQuantAsync(plan.Quant, ct); + var state = await ProcessHybridQuantAsync( + plan.Quant, + allowIndependentGpuTopology, + ct); sw.Stop(); record.State = state; @@ -378,6 +395,7 @@ private async Task ExecuteDuplicatePlanAsync( RequiredSamplePlan sourcePlan, RequiredSamplePlan duplicatePlan, StageProgressTracker? progress, + bool allowIndependentGpuTopology, CancellationToken ct) { var record = new SampleProcessingRecord @@ -407,7 +425,11 @@ private async Task ExecuteDuplicatePlanAsync( } sw.Stop(); - return await ExecutePlanAsync(duplicatePlan, progress, ct); + return await ExecutePlanAsync( + duplicatePlan, + progress, + allowIndependentGpuTopology, + ct); } catch (Exception ex) { @@ -480,6 +502,7 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, scopedAi public async Task ProcessHybridQuantAsync( HybridQuant quant, + bool allowIndependentGpuTopology = true, CancellationToken ct = default) { string modelName = GenerateHybridName(quant); @@ -601,7 +624,8 @@ await _benchmarker.RunAllBenchmarksAsync( benchDir: modelBenchDir, klLogitsDir: baseLogitsDir, saveLogits: false, - domainsOverride: new[] { "general" }); + domainsOverride: new[] { "general" }, + allowIndependentGpuTopology: allowIndependentGpuTopology); if (IsLearnableBaselineRun(quant)) { From 8d3fed0b326d239f60c256fa6358e602a80449ae Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 13:57:49 -0400 Subject: [PATCH 239/258] Require learned carriers for controlled contexts --- MagicQuant/Services/AnomalyWorkflowService.cs | 23 +++++++++++++++++++ MagicQuant/config.dev.yaml | 5 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/MagicQuant/Services/AnomalyWorkflowService.cs b/MagicQuant/Services/AnomalyWorkflowService.cs index 8aec6be..1470a3f 100644 --- a/MagicQuant/Services/AnomalyWorkflowService.cs +++ b/MagicQuant/Services/AnomalyWorkflowService.cs @@ -1280,11 +1280,16 @@ private static string BuildTransferTemplateKey(IReadOnlyList ResolveTransferTargetContexts(RuntimeSynergyTransferProbeContextStrataConfig strata) { + var learnedContextIds = BaselineQuants + .GetLearningBaselines(RuntimeSearchSpace.HasUsableImatrix()) + .Select(x => x.UniqueId) + .ToHashSet(); var byName = BaselineQuants.All .SelectMany(x => x.Names.Select(name => (Name: name, Quant: x))) .GroupBy(x => x.Name, StringComparer.OrdinalIgnoreCase) .ToDictionary(x => x.Key, x => x.First().Quant, StringComparer.OrdinalIgnoreCase); var contexts = new List(); + var skippedWithoutLearnedCarrier = new List(); void add(IEnumerable names, string stratum) { @@ -1293,6 +1298,16 @@ void add(IEnumerable names, string stratum) if (!byName.TryGetValue(name.Trim(), out var quant) || BaselineQuants.IsNativeExactAlias(quant.UniqueId)) continue; + // Context blankets need a complete learned tensor map for their base so + // base-quant exception tensors can be reconstructed alongside explicit + // group overrides. In selected-baseline mode, a recognized built-in name + // is not necessarily enabled as a learning baseline. + if (!learnedContextIds.Contains(quant.UniqueId)) + { + skippedWithoutLearnedCarrier.Add($"{name.Trim()} ({stratum})"); + continue; + } + if (contexts.All(x => x.QuantId != quant.UniqueId)) contexts.Add(new SynergyTransferContext(quant.UniqueId, stratum)); } @@ -1303,6 +1318,14 @@ void add(IEnumerable names, string stratum) if (strata.LowFidelityEnabled) add(strata.LowFidelityReferenceQuants, "low-fidelity"); + if (skippedWithoutLearnedCarrier.Count > 0) + { + AnsiConsole.MarkupLine( + $"[yellow]Controlled context skipped:[/] no learned base-carrier mapping is configured for " + + $"{Markup.Escape(string.Join(", ", skippedWithoutLearnedCarrier.Distinct(StringComparer.OrdinalIgnoreCase)))}. " + + "Enable these names in baselines.enabled_standard_learning_baselines or configure learned external baseline names."); + } + return contexts; } diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 6130723..afd518f 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -267,7 +267,10 @@ baselines: # Use Unsloth GGUFs for dynamic learning/search while retaining the built-in # Q8 anchor required by the execution-plan and baseline pipeline. standard_baselines_mode: selected - enabled_standard_learning_baselines: [Q8_0] + # Controlled fidelity probes use these provider-neutral blankets. They must be + # learned so base-quant exception tensors have complete carrier mappings, but + # only Q8 remains a search carrier to avoid multiplying the global combinatorics. + enabled_standard_learning_baselines: [Q8_0, Q6_K, Q5_K, Q4_K_M, IQ3_S] enabled_standard_combination_carriers: [Q8_0] enabled_standard_explicit_group_candidates: [Q8_0] From 6c835a3893d32ba49e26bb26c643eb24951882e8 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 14:16:34 -0400 Subject: [PATCH 240/258] Prefer canonical baseline names over scheme aliases --- MQ.DB/Models/BaselineQuants.cs | 9 ++++++++- MagicQuant.Tests/BaselineCandidatePolicyTests.cs | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index 77d58a8..c955387 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -452,8 +452,15 @@ public static IReadOnlyList GetBuiltInStandardBaselines() => if (string.IsNullOrWhiteSpace(name)) return null; + // A canonical baseline name must win over a tensor-scheme alias. For example, + // IQ3_M's primary tensor scheme is IQ3_S, but a user who explicitly configures + // IQ3_S means the IQ3_S baseline, not the earlier IQ3_M registry entry. + var exactName = StandardBaselines.FirstOrDefault(x => + x.Names.Any(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase))); + if (exactName != null) + return exactName; + return StandardBaselines.FirstOrDefault(x => - x.Names.Any(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase)) || string.Equals(x.PrimaryTensorWeightScheme.Names[0], name, StringComparison.OrdinalIgnoreCase)); } diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs index 0df97b0..7ceb2ab 100644 --- a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -25,6 +25,13 @@ public void Iq1Families_AreRegisteredAsImatrixLearningAndExplicitCandidates() Assert.Equal("IQ1_M", TensorWeightScheme.FromId(TensorWeightScheme.IQ1_M.UniqueId).Names[0]); } + [Fact] + public void ResolveBuiltInStandardBaseline_PrefersCanonicalNameOverSharedTensorSchemeAlias() + { + Assert.Same(BaselineQuants.IQ3_S, BaselineQuants.ResolveBuiltInStandardBaseline("IQ3_S")); + Assert.Same(BaselineQuants.IQ3_M, BaselineQuants.ResolveBuiltInStandardBaseline("IQ3_M")); + } + [Fact] public void GetPureBaselineCandidates_NoImatrix_ReturnsAllNonImatrixLearningBaselines() { From 6546ccb728e81a99e494480536aeccd9a7663f06 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 14:19:22 -0400 Subject: [PATCH 241/258] Scope canonical resolution to standard roles --- MQ.DB/Models/BaselineQuants.cs | 24 +++++++++++++------ .../BaselineCandidatePolicyTests.cs | 6 ++--- .../Configuration/MagicQuantYamlLoader.cs | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/MQ.DB/Models/BaselineQuants.cs b/MQ.DB/Models/BaselineQuants.cs index c955387..3b26606 100644 --- a/MQ.DB/Models/BaselineQuants.cs +++ b/MQ.DB/Models/BaselineQuants.cs @@ -452,16 +452,26 @@ public static IReadOnlyList GetBuiltInStandardBaselines() => if (string.IsNullOrWhiteSpace(name)) return null; - // A canonical baseline name must win over a tensor-scheme alias. For example, - // IQ3_M's primary tensor scheme is IQ3_S, but a user who explicitly configures - // IQ3_S means the IQ3_S baseline, not the earlier IQ3_M registry entry. + return StandardBaselines.FirstOrDefault(x => + x.Names.Any(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase)) || + string.Equals(x.PrimaryTensorWeightScheme.Names[0], name, StringComparison.OrdinalIgnoreCase)); + } + + /// + /// Resolves user-facing standard-role configuration with canonical baseline names + /// taking precedence over shared tensor-scheme aliases. Keep the legacy resolver + /// unchanged because external baseline family normalization relies on its historical + /// scheme-first registry ordering. + /// + public static BaselineQuants? ResolveBuiltInStandardRoleBaseline(string name) + { + if (string.IsNullOrWhiteSpace(name)) + return null; + var exactName = StandardBaselines.FirstOrDefault(x => x.Names.Any(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase))); - if (exactName != null) - return exactName; - return StandardBaselines.FirstOrDefault(x => - string.Equals(x.PrimaryTensorWeightScheme.Names[0], name, StringComparison.OrdinalIgnoreCase)); + return exactName ?? ResolveBuiltInStandardBaseline(name); } public static IReadOnlyList GetAllRecognizedBaselines() => diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs index 7ceb2ab..6a77672 100644 --- a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -26,10 +26,10 @@ public void Iq1Families_AreRegisteredAsImatrixLearningAndExplicitCandidates() } [Fact] - public void ResolveBuiltInStandardBaseline_PrefersCanonicalNameOverSharedTensorSchemeAlias() + public void ResolveBuiltInStandardRoleBaseline_PrefersCanonicalNameOverSharedTensorSchemeAlias() { - Assert.Same(BaselineQuants.IQ3_S, BaselineQuants.ResolveBuiltInStandardBaseline("IQ3_S")); - Assert.Same(BaselineQuants.IQ3_M, BaselineQuants.ResolveBuiltInStandardBaseline("IQ3_M")); + Assert.Same(BaselineQuants.IQ3_S, BaselineQuants.ResolveBuiltInStandardRoleBaseline("IQ3_S")); + Assert.Same(BaselineQuants.IQ3_M, BaselineQuants.ResolveBuiltInStandardRoleBaseline("IQ3_M")); } [Fact] diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 22e6e94..63c1158 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -254,7 +254,7 @@ private static HashSet ResolveStandardBaselineIds(IEnumerable name if (string.IsNullOrWhiteSpace(raw)) continue; - var baseline = BaselineQuants.ResolveBuiltInStandardBaseline(raw.Trim()); + var baseline = BaselineQuants.ResolveBuiltInStandardRoleBaseline(raw.Trim()); if (baseline == null) { throw new InvalidOperationException( From f85401614063f354070839d65deccf90fcb71aaa Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 17:28:04 -0400 Subject: [PATCH 242/258] Skip smart tuning for context-only baselines --- .../SmartBaselineTuningFallbackTests.cs | 41 ++++++++++++++++++ .../SmartBaselineTuningFallbackService.cs | 42 +++++++++++++++++-- 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs diff --git a/MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs b/MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs new file mode 100644 index 0000000..3072606 --- /dev/null +++ b/MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs @@ -0,0 +1,41 @@ +using MagicQuant.Services; +using MQ.DB.Models; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class SmartBaselineTuningFallbackTests +{ + [Fact] + public void LearningOnlyBaselineWithMissingIsolationCoverageIsNotTunable() + { + var eligible = SmartBaselineTuningFallbackService.IsEligibleForSmartFallbackTuning( + BaselineQuants.Q6_K, + [BaselineQuants.Q8_0], + hasCompleteIsolationCoverage: false); + + Assert.False(eligible); + } + + [Fact] + public void ExplicitCandidateWithMissingIsolationCoverageRemainsAHardFailurePath() + { + var eligible = SmartBaselineTuningFallbackService.IsEligibleForSmartFallbackTuning( + BaselineQuants.Q6_K, + [BaselineQuants.Q8_0, BaselineQuants.Q6_K], + hasCompleteIsolationCoverage: false); + + Assert.True(eligible); + } + + [Fact] + public void HistoricalCompleteCoverageAllowsTuningWithoutCurrentExplicitRole() + { + var eligible = SmartBaselineTuningFallbackService.IsEligibleForSmartFallbackTuning( + BaselineQuants.Q6_K, + [BaselineQuants.Q8_0], + hasCompleteIsolationCoverage: true); + + Assert.True(eligible); + } +} diff --git a/MagicQuant/Services/SmartBaselineTuningFallbackService.cs b/MagicQuant/Services/SmartBaselineTuningFallbackService.cs index bae22d4..3056ef7 100644 --- a/MagicQuant/Services/SmartBaselineTuningFallbackService.cs +++ b/MagicQuant/Services/SmartBaselineTuningFallbackService.cs @@ -123,7 +123,23 @@ private async Task> BuildCandidatesAsync var context = await GetContextAsync(ct); var blanketAnchor = ResolveSmartBlanketAnchor(request.BaselineAnchor, blanketBaseline, context); - ValidateBlanketIsolationCoverage(blanketBaseline, context); + var missingIsolationGroups = GetMissingIsolationGroups(blanketBaseline, context); + var activeExplicitCandidates = BaselineQuants.GetGroupCombinationCandidates( + RuntimeSearchSpace.HasUsableImatrix(), + Config.Current.Flags.AllowHighPrecisionHybrids); + + if (!IsEligibleForSmartFallbackTuning( + blanketBaseline, + activeExplicitCandidates, + hasCompleteIsolationCoverage: missingIsolationGroups.Count == 0)) + { + AnsiConsole.MarkupLine( + $"[grey]Smart fallback skipped:[/] baseline [cyan]{Markup.Escape(blanketBaseline.Names[0])}[/] is learning/context-only in the active search space and intentionally lacks isolated group truth for " + + $"{missingIsolationGroups.Count:N0}/{context.ActiveGroups.Count:N0} active group(s). Enable it as an explicit group candidate before smart blanket tuning."); + return Array.Empty(); + } + + ValidateBlanketIsolationCoverage(blanketBaseline, context, missingIsolationGroups); var baseSize = blanketAnchor.SizeBytes; var baseKld = Math.Max(0d, blanketAnchor.Kld); @@ -263,14 +279,32 @@ private static bool IsUniformLearnedBlanket(BenchmarkSnapshotRecord anchor, Base return true; } - private static void ValidateBlanketIsolationCoverage( + internal static bool IsEligibleForSmartFallbackTuning( + BaselineQuants blanketBaseline, + IReadOnlyCollection activeExplicitCandidates, + bool hasCompleteIsolationCoverage) + { + if (hasCompleteIsolationCoverage) + return true; + + return activeExplicitCandidates.Any(x => x.UniqueId == blanketBaseline.UniqueId); + } + + private static IReadOnlyList GetMissingIsolationGroups( BaselineQuants blanketBaseline, RankSafeKldPredictionService.RankSafePredictionModel context) { - var missingGroups = context.ActiveGroups + return context.ActiveGroups .Where(group => !context.IsolationByGroupAndBaseline.ContainsKey((group.UniqueId, blanketBaseline.UniqueId))) .Select(group => group.Name) .ToList(); + } + + private static void ValidateBlanketIsolationCoverage( + BaselineQuants blanketBaseline, + RankSafeKldPredictionService.RankSafePredictionModel context, + IReadOnlyList missingGroups) + { if (missingGroups.Count == 0) return; @@ -839,4 +873,4 @@ private sealed class SmartCandidatePlan public long TotalSizeDeltaBytes { get; init; } public double Score { get; init; } } -} \ No newline at end of file +} From 4c4318aeac4d2963f29648d2f43957bb81b05ced Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 17:59:24 -0400 Subject: [PATCH 243/258] Assign benchmark models to best-fit GPUs --- MagicQuant.Tests/BenchmarkGpuPlanningTests.cs | 61 +++++++++++++++++++ MagicQuant/Services/BenchmarkGpuPlanning.cs | 48 +++++++++++++++ MagicQuant/Services/BenchmarkService.cs | 20 ++++-- 3 files changed, 124 insertions(+), 5 deletions(-) diff --git a/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs b/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs index baedf3f..65077d6 100644 --- a/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs +++ b/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs @@ -54,6 +54,67 @@ public void ShouldUseIndependentTopology_RequiresConcurrentBatchIntent( Assert.Equal(expected, result); } + [Fact] + public void RankIndependentSlots_NearFullCandidateUsesWeakerDevice() + { + var slots = new[] + { + Slot(0, 44, (35, 6.5), (44, 5.4)), + Slot(1, 57, (48, 4.3), (57, 3.1)) + }; + + var ranked = BenchmarkGpuPlanner.RankIndependentSlotsForModel( + slots, + Q8Size, + maxOffloadNgl: 66, + modelSizeBytes: 19_500_000_000UL); + + Assert.Equal(0, ranked[0].DeviceIndices[0]); + Assert.Equal(65, BenchmarkGpuPlanner.ResolveNglForModel(Q8Size, 44, 66, 19_500_000_000UL)); + } + + [Fact] + public void RankIndependentSlots_LargeCandidateUsesStrongerDevice() + { + var slots = new[] + { + Slot(0, 44, (35, 6.5), (44, 5.4)), + Slot(1, 57, (48, 4.3), (57, 3.1)) + }; + + var ranked = BenchmarkGpuPlanner.RankIndependentSlotsForModel( + slots, + Q8Size, + maxOffloadNgl: 66, + modelSizeBytes: 22_900_000_000UL); + + Assert.Equal(1, ranked[0].DeviceIndices[0]); + Assert.True( + BenchmarkGpuPlanner.ResolveNglForModel(Q8Size, 44, 66, 22_900_000_000UL) < + BenchmarkGpuPlanner.ResolveNglForModel(Q8Size, 57, 66, 22_900_000_000UL)); + } + + [Fact] + public async Task ResourceScheduler_UsesCandidateSpecificSlotRanking() + { + var scheduler = new GpuResourceScheduler(); + var slots = new[] + { + Slot(0, 44, (35, 6.5), (44, 5.4)), + Slot(1, 57, (48, 4.3), (57, 3.1)) + }; + var smaller = BenchmarkGpuPlanner.RankIndependentSlotsForModel( + slots, Q8Size, 66, 19_500_000_000UL); + var larger = BenchmarkGpuPlanner.RankIndependentSlotsForModel( + slots, Q8Size, 66, 22_900_000_000UL); + + await using var first = await scheduler.AcquireAsync(smaller); + await using var second = await scheduler.AcquireAsync(larger); + + Assert.Equal(0, first.Slot.DeviceIndices[0]); + Assert.Equal(1, second.Slot.DeviceIndices[0]); + } + [Fact] public async Task ResourceScheduler_ReservesDisjointSingleGpuSlotsConcurrently() { diff --git a/MagicQuant/Services/BenchmarkGpuPlanning.cs b/MagicQuant/Services/BenchmarkGpuPlanning.cs index 9d378d2..12bb1b6 100644 --- a/MagicQuant/Services/BenchmarkGpuPlanning.cs +++ b/MagicQuant/Services/BenchmarkGpuPlanning.cs @@ -159,6 +159,7 @@ private static BenchmarkTopologyProfile FromCacheProfile(BenchmarkTopologyCacheP internal static class BenchmarkGpuPlanner { internal const double DefaultIndependentSpeedupMargin = 1.10d; + internal const int NearFullOffloadToleranceLayers = 1; public static bool ShouldUseIndependentTopology( ulong modelSizeBytes, @@ -186,6 +187,53 @@ public static int ResolveNglForModel( return (int)Math.Clamp(scaled, 0d, maxOffloadNgl); } + public static IReadOnlyList RankIndependentSlotsForModel( + IReadOnlyList slots, + ulong q8ModelSizeBytes, + int maxOffloadNgl, + ulong modelSizeBytes, + int nearFullOffloadToleranceLayers = NearFullOffloadToleranceLayers) + { + ArgumentNullException.ThrowIfNull(slots); + if (slots.Count <= 1 || q8ModelSizeBytes == 0 || modelSizeBytes == 0 || maxOffloadNgl <= 0) + return slots; + + int nearFullThreshold = Math.Max(0, maxOffloadNgl - Math.Max(0, nearFullOffloadToleranceLayers)); + var ranked = slots + .Select(slot => new + { + Slot = slot, + Ngl = ResolveNglForModel( + q8ModelSizeBytes, + slot.Q8StableNgl, + maxOffloadNgl, + modelSizeBytes) + }) + .ToArray(); + + // A weaker device that is at most one layer shy of full offload is the best fit: + // using it preserves the stronger device for a larger concurrent model. When only + // one device is close to full offload, prefer that device. Otherwise use the device + // that can offload the most layers and accept the unavoidable partial-offload tail. + if (ranked.Any(x => x.Ngl >= nearFullThreshold)) + { + return ranked + .OrderByDescending(x => x.Ngl >= nearFullThreshold) + .ThenBy(x => x.Ngl >= nearFullThreshold ? x.Slot.Q8StableNgl : int.MaxValue) + .ThenByDescending(x => x.Ngl) + .ThenBy(x => x.Slot.SlotId) + .Select(x => x.Slot) + .ToArray(); + } + + return ranked + .OrderByDescending(x => x.Ngl) + .ThenByDescending(x => x.Slot.Q8StableNgl) + .ThenBy(x => x.Slot.SlotId) + .Select(x => x.Slot) + .ToArray(); + } + public static ulong EstimateIndependentCrossoverBytes( ulong q8ModelSizeBytes, int maxOffloadNgl, diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index cd39f5c..1f2041b 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -1127,15 +1127,25 @@ private async Task ProbePerplexityAtFixedNglAsync( throw new InvalidOperationException( "Benchmark execution plan has not been initialized. Call EnsureExecutionPlanAsync() first."); - BenchmarkTopologyProfile profile = BenchmarkGpuPlanner.ShouldUseIndependentTopology( + bool useIndependentTopology = BenchmarkGpuPlanner.ShouldUseIndependentTopology( modelSizeBytes, _currentPlan.IndependentMaxModelSizeBytes, _currentPlan.IndependentProfile.Slots.Count, - allowIndependentTopology) - ? _currentPlan.IndependentProfile - : _currentPlan.SharedProfile; + allowIndependentTopology); - return await _resourceScheduler.AcquireAsync(profile.Slots, ct); + BenchmarkTopologyProfile profile = useIndependentTopology + ? _currentPlan.IndependentProfile + : _currentPlan.SharedProfile; + + IReadOnlyList candidates = useIndependentTopology + ? BenchmarkGpuPlanner.RankIndependentSlotsForModel( + profile.Slots, + _currentPlan.Q8ModelSizeBytes, + _currentPlan.MaxCandidateNgl, + modelSizeBytes) + : profile.Slots; + + return await _resourceScheduler.AcquireAsync(candidates, ct); } // ---------------------------------------------------------------- From d837088a746178cc8a5f77d4d9ac688878379adb Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 18:05:54 -0400 Subject: [PATCH 244/258] Clarify isolation continuation configuration --- MagicQuant/Configs/config.dev.yaml | 2 +- MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml | 2 +- MagicQuant/Configs/config.qwen3.6-27b.dev.yaml | 2 +- MagicQuant/config.default.yaml | 8 ++++---- MagicQuant/config.dev.yaml | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/MagicQuant/Configs/config.dev.yaml b/MagicQuant/Configs/config.dev.yaml index e4086c0..b2147b4 100644 --- a/MagicQuant/Configs/config.dev.yaml +++ b/MagicQuant/Configs/config.dev.yaml @@ -104,7 +104,7 @@ imatrix: # Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. isolation_pruning: - # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 + # Preserve complete isolation truth for prediction and contextual probing. minimum_isolation_reduction_to_continue_ratio: 0.00 minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 maximum_isolation_ppl_delta_percent: 5.0 diff --git a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml index e2f409b..59bafa4 100644 --- a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml +++ b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml @@ -104,7 +104,7 @@ imatrix: # Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. isolation_pruning: - # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 + # Preserve complete isolation truth for prediction and contextual probing. minimum_isolation_reduction_to_continue_ratio: 0.00 minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 maximum_isolation_ppl_delta_percent: 5.0 diff --git a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml index 57a136e..d516fbe 100644 --- a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml +++ b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml @@ -104,7 +104,7 @@ imatrix: # Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. isolation_pruning: - # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 + # Preserve complete isolation truth for prediction and contextual probing. minimum_isolation_reduction_to_continue_ratio: 0.00 minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 maximum_isolation_ppl_delta_percent: 5.0 diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index 0d8cb2a..ec03982 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -165,10 +165,10 @@ isolation_pruning: # These values still matter for later isolation / bad-trade reasoning, # not for the old "skip candidate because learned tensor usage looked redundant" path. - # Minimum isolation reduction ratio required to continue considering the result meaningful. - # Currently broke, should be 0.04 but leave at 0 until fixed. Causes isolated samples not to be made - # which was once a requirement but need to go back and remove as all samples are needed for - # accurate predictions. DO not remove this comment till this is resolved. + # Minimum carrier-relative size reduction required before generating the remaining + # candidate-isolation samples for a tensor group. Keep this at 0 when prediction, + # contextual probing, or anomaly analysis needs complete isolation truth; later + # quality and bad-trade filters still remove unhelpful candidates. minimum_isolation_reduction_to_continue_ratio: 0.00 # Minimum reduction ratio before BF16 suppression logic is allowed to kick in. diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index afd518f..d976c16 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -104,7 +104,7 @@ imatrix: # Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. isolation_pruning: - # 0.04 is the goal, but this is currently causing prediction issues, leave at 0 + # Preserve complete isolation truth for prediction and contextual probing. minimum_isolation_reduction_to_continue_ratio: 0.00 minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 maximum_isolation_ppl_delta_percent: 5.0 From d049b756c2996ce88b67ce62321df5ac86b03cd0 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 18:16:04 -0400 Subject: [PATCH 245/258] Preserve the full Qwen3.8 Pareto frontier --- MagicQuant/config.dev.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index d976c16..698ebf5 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -163,9 +163,9 @@ candidate_selection: # Strict epsilon for lower-KLD comparisons after real benchmark validation. minimum_kld_improvement_epsilon: 1.0e-9 - # Final spacing pass: candidates closer than this fraction of the global survivor - # size span are collapsed unless one genuinely earns the slot. - minimum_neighbor_gap_fraction_of_global_span: 0.03 + # This campaign is the publishable union frontier: retain every nondominated + # size/quality tradeoff instead of collapsing nearby points for a shorter list. + minimum_neighbor_gap_fraction_of_global_span: 0.00 # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller # anchor must provide a stronger KLD gain to justify its existence. From b8c30cc5d2aa1bba727f46d45e23e8a99ec64c9a Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 19:08:03 -0400 Subject: [PATCH 246/258] Preserve configured runtime root during validation --- MagicQuant/Commands/InitializeLlamaCpp.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 3e092b9..1a7c488 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -121,7 +121,11 @@ public async Task Run(List args) // --------------------------------------------------------- // 6. Build Llama.cpp (Runs as Normal User) // --------------------------------------------------------- - Cache.MagicQuantDirectory = magicQuantPath; + // The installer always lives in the user's shared MagicQuant directory, but + // dependency validation is also invoked inside commands that may use an + // isolated --magic-quant-root. Do not overwrite that configured runtime root: + // doing so silently redirects SQLite and other campaign state back to the + // user's shared installation directory. var builder = new LlamaBuilder(magicQuantPath, sysInfo); await builder.PrepareAndBuildAsync(update); From 4eb639546280c0fbab09de79c54580a5657216b5 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 06:58:55 -0400 Subject: [PATCH 247/258] Restore deterministic full-suite validation --- MQ.DB/Data/MagicQuantContext.cs | 18 ++- MagicQuant.Tests/AssemblyInfo.cs | 3 + .../AuthorityUsageRegressionTests.cs | 7 +- .../BaselineCandidatePolicyTests.cs | 91 ++++++++------ ...zationRunAndBuildHybridsRegressionTests.cs | 119 ++++++++++++------ 5 files changed, 150 insertions(+), 88 deletions(-) create mode 100644 MagicQuant.Tests/AssemblyInfo.cs diff --git a/MQ.DB/Data/MagicQuantContext.cs b/MQ.DB/Data/MagicQuantContext.cs index 5f05430..22145bc 100644 --- a/MQ.DB/Data/MagicQuantContext.cs +++ b/MQ.DB/Data/MagicQuantContext.cs @@ -10,7 +10,7 @@ public class MagicQuantContext : DbContext // -------------------------------------------------------- // Self-Initialization Logic // -------------------------------------------------------- - private static bool _isInitialized = false; + private static readonly HashSet InitializedDatabaseDirectories = new(StringComparer.Ordinal); private static readonly object _initLock = new(); public MagicQuantContext() @@ -30,19 +30,25 @@ private void EnsureInitialized() if (IsDesignTime()) return; - if (_isInitialized) - return; - + string initializationKey = ResolveDatabaseDirectory(); lock (_initLock) { - if (_isInitialized) + if (InitializedDatabaseDirectories.Contains(initializationKey)) return; InitializeDatabase(); - _isInitialized = true; + InitializedDatabaseDirectories.Add(initializationKey); } } + private static string ResolveDatabaseDirectory() + { + string directory = string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory) + ? Directory.GetCurrentDirectory() + : Cache.MagicQuantDirectory; + return Path.GetFullPath(directory); + } + private void InitializeDatabase() { var directory = Cache.MagicQuantDirectory; diff --git a/MagicQuant.Tests/AssemblyInfo.cs b/MagicQuant.Tests/AssemblyInfo.cs new file mode 100644 index 0000000..2171200 --- /dev/null +++ b/MagicQuant.Tests/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using Xunit; + +[assembly: CollectionBehavior(DisableTestParallelization = true)] diff --git a/MagicQuant.Tests/AuthorityUsageRegressionTests.cs b/MagicQuant.Tests/AuthorityUsageRegressionTests.cs index e3a03ae..fdda4c7 100644 --- a/MagicQuant.Tests/AuthorityUsageRegressionTests.cs +++ b/MagicQuant.Tests/AuthorityUsageRegressionTests.cs @@ -7,11 +7,12 @@ public class AuthorityUsageRegressionTests [Fact] public void ComboGenerationPaths_DoNotUseLegacyAllAllowedHybridQuantsAuthority() { + string repositoryRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../..")); var files = new[] { - Path.Combine("..", "MagicQuant", "Helpers", "ComboLogic.cs"), - Path.Combine("..", "MagicQuant", "Helpers", "TensorConfigGenerator.cs"), - Path.Combine("..", "MagicQuant", "Services", "IsolationOptimizationService.cs") + Path.Combine(repositoryRoot, "MagicQuant", "Helpers", "ComboLogic.cs"), + Path.Combine(repositoryRoot, "MagicQuant", "Helpers", "TensorConfigGenerator.cs"), + Path.Combine(repositoryRoot, "MagicQuant", "Services", "IsolationOptimizationService.cs") }; foreach (var file in files) diff --git a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs index 44ba74e..0df97b0 100644 --- a/MagicQuant.Tests/BaselineCandidatePolicyTests.cs +++ b/MagicQuant.Tests/BaselineCandidatePolicyTests.cs @@ -26,22 +26,12 @@ public void Iq1Families_AreRegisteredAsImatrixLearningAndExplicitCandidates() } [Fact] - public void GetPureBaselineCandidates_NoImatrix_ReturnsExactlyIq4Xs() + public void GetPureBaselineCandidates_NoImatrix_ReturnsAllNonImatrixLearningBaselines() { var ids = BaselineQuants.GetPureBaselineCandidates(hasUsableImatrix: false) .Select(x => x.UniqueId) .ToArray(); - Assert.Equal([BaselineQuants.IQ4_XS.UniqueId], ids); - } - - [Fact] - public void GetCombinationCarrierBaselines_NoImatrix_ReturnsExactlySixExpectedBaselines() - { - var ids = BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix: false) - .Select(x => x.UniqueId) - .ToArray(); - Assert.Equal( [ BaselineQuants.Q8_0.UniqueId, @@ -49,12 +39,24 @@ public void GetCombinationCarrierBaselines_NoImatrix_ReturnsExactlySixExpectedBa BaselineQuants.Q5_K.UniqueId, BaselineQuants.Q4_K_M.UniqueId, BaselineQuants.IQ4_NL.UniqueId, - BaselineQuants.IQ4_XS.UniqueId + BaselineQuants.IQ4_XS.UniqueId, + BaselineQuants.Q5_K_S.UniqueId, + BaselineQuants.Q4_K_S.UniqueId ], ids); } [Fact] - public void GetGroupCombinationCandidates_NoImatrixNoHighPrecision_ReturnsExactlySixExpectedBaselines() + public void GetCombinationCarrierBaselines_UsesCanonicalQ8Carrier() + { + var ids = BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix: false) + .Select(x => x.UniqueId) + .ToArray(); + + Assert.Equal([BaselineQuants.Q8_0.UniqueId], ids); + } + + [Fact] + public void GetGroupCombinationCandidates_NoImatrix_ReturnsAllEligibleFourBitAndHigherBaselines() { var ids = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix: false, allowHighPrecisionHybrids: false) .Select(x => x.UniqueId) @@ -62,12 +64,14 @@ public void GetGroupCombinationCandidates_NoImatrixNoHighPrecision_ReturnsExactl Assert.Equal( [ - BaselineQuants.Q8_0.UniqueId, - BaselineQuants.Q6_K.UniqueId, - BaselineQuants.Q5_K.UniqueId, - BaselineQuants.Q4_K_M.UniqueId, + BaselineQuants.IQ4_XS.UniqueId, BaselineQuants.IQ4_NL.UniqueId, - BaselineQuants.IQ4_XS.UniqueId + BaselineQuants.Q4_K_S.UniqueId, + BaselineQuants.Q4_K_M.UniqueId, + BaselineQuants.Q5_K_S.UniqueId, + BaselineQuants.Q5_K.UniqueId, + BaselineQuants.Q6_K.UniqueId, + BaselineQuants.Q8_0.UniqueId ], ids); Assert.DoesNotContain(BaselineQuants.IQ3_S.UniqueId, ids); @@ -81,7 +85,7 @@ public void GetGroupCombinationCandidates_NoImatrixNoHighPrecision_ReturnsExactl } [Fact] - public void RuntimeSearchSpace_GetActiveCombinationBaselines_ReturnsExactlySixExpectedBaselines() + public void RuntimeSearchSpace_GetActiveCombinationBaselines_ReturnsCanonicalQ8Carrier() { RuntimeSearchSpace.ResetForNewModel(); RuntimeSearchSpace.SetImatrixAvailability(false); @@ -90,15 +94,7 @@ public void RuntimeSearchSpace_GetActiveCombinationBaselines_ReturnsExactlySixEx .Select(x => x.UniqueId) .ToArray(); - Assert.Equal( - [ - BaselineQuants.Q8_0.UniqueId, - BaselineQuants.Q6_K.UniqueId, - BaselineQuants.Q5_K.UniqueId, - BaselineQuants.Q4_K_M.UniqueId, - BaselineQuants.IQ4_NL.UniqueId, - BaselineQuants.IQ4_XS.UniqueId - ], ids); + Assert.Equal([BaselineQuants.Q8_0.UniqueId], ids); } [Fact] @@ -123,7 +119,7 @@ public void CandidateBanAuthority_DrivesAllowedCandidateSet() var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); var attnQIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.AttnQ.UniqueId); - Assert.DoesNotContain(BaselineQuants.Q6_K.UniqueId, allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.Q6_K), allowed[attnQIndex]); } [Fact] @@ -136,8 +132,8 @@ public void ComboLogic_WhenHighPrecisionDisabled_DoesNotInjectBf16OrF16() var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); var attnQIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.AttnQ.UniqueId); - Assert.DoesNotContain(BaselineQuants.BF16_Hybrid.UniqueId, allowed[attnQIndex]); - Assert.DoesNotContain(BaselineQuants.F16_Hybrid.UniqueId, allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.BF16_Hybrid), allowed[attnQIndex]); + Assert.DoesNotContain(BaselineQuants.EncodeTensorConfigGroupSlot(BaselineQuants.F16_Hybrid), allowed[attnQIndex]); } [Fact] @@ -145,12 +141,31 @@ public void ComboLogic_UsesCandidateLevelBannedGroups() { RuntimeSearchSpace.ResetForNewModel(); RuntimeSearchSpace.SetImatrixAvailability(false); - - var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); - var moeRouterIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.MoeRouter.UniqueId); - - Assert.DoesNotContain(BaselineQuants.Q5_K.UniqueId, allowed[moeRouterIndex]); - Assert.DoesNotContain(BaselineQuants.IQ4_NL.UniqueId, allowed[moeRouterIndex]); - Assert.DoesNotContain(BaselineQuants.IQ4_XS.UniqueId, allowed[moeRouterIndex]); + var candidate = BaselineQuants.RegisterCustomExternalBaseline(new BaselineQuants.ExternalBaselineRegistration + { + CanonicalKey = "test:moe-router-banned", + DisplayName = "TEST-Q5-BANNED", + QuantizeBaseArgumentName = "Q5_K", + Repository = "test/repository", + RepositoryFileName = "test-q5.gguf", + OwnerShortName = "test", + BaselineFamilyName = "Q5_K", + TensorScheme = TensorWeightScheme.Q5_K, + AddAsGroupCandidate = true, + BitRange = 5, + BannedGroupIds = [TReg.MoeRouter.UniqueId] + }); + + try + { + var allowed = ComboLogic.GetAllowedCandidateIdsPerGroup(BaselineQuants.Q8_0); + var moeRouterIndex = TReg.All.OrderBy(x => x.UniqueId).ToList().FindIndex(x => x.UniqueId == TReg.MoeRouter.UniqueId); + + Assert.DoesNotContain(BaselineQuants.EncodeTensorConfigGroupSlot(candidate), allowed[moeRouterIndex]); + } + finally + { + BaselineQuants.ResetDynamicCustomBaselines(); + } } } diff --git a/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs b/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs index a1de027..8ab7d25 100644 --- a/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs +++ b/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs @@ -1,4 +1,5 @@ using MagicQuant.Commands; +using MagicQuant.Configuration; using MagicQuant.Models; using Microsoft.EntityFrameworkCore; using MQ.DB; @@ -15,58 +16,94 @@ public async Task QuantizationRun_PersistsAndLoads_ImatrixDefinitionForeignKey() { string tempRoot = Path.Combine(Path.GetTempPath(), "mq-quant-run-fk-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(tempRoot); + string? priorMagicQuantDirectory = Cache.MagicQuantDirectory; - Cache.MagicQuantDirectory = tempRoot; + try + { + Cache.MagicQuantDirectory = tempRoot; - await using var db = new MagicQuantContext(); + await using var db = new MagicQuantContext(); - var model = new AiModelHash - { - UniqueHash = "model-" + Guid.NewGuid().ToString("N") - }; + var model = new AiModelHash + { + UniqueHash = "model-" + Guid.NewGuid().ToString("N") + }; - var combo = new TensorCombo(); - db.AiModelHashes.Add(model); - db.TensorCombos.Add(combo); - await db.SaveChangesAsync(); + var combo = new TensorCombo(); + var architecture = new ArchitectureFamily + { + NormalizedName = "test-architecture-" + Guid.NewGuid().ToString("N"), + DisplayName = "Test architecture", + TensorSignatureHash = "signature-" + Guid.NewGuid().ToString("N"), + TensorCount = 1 + }; + var profile = new TensorGroupProfile + { + ArchitectureFamily = architecture, + FingerprintHash = "profile-" + Guid.NewGuid().ToString("N"), + SnapshotJson = "{}" + }; + db.AiModelHashes.Add(model); + db.TensorCombos.Add(combo); + db.ArchitectureFamilies.Add(architecture); + db.TensorGroupProfiles.Add(profile); + await db.SaveChangesAsync(); - var imatrix = new ImatrixDefinition - { - AiModelHashId = model.Id, - IdentityHash = "imatrix-" + Guid.NewGuid().ToString("N"), - SourceKind = "test" - }; - db.ImatrixDefinitions.Add(imatrix); - await db.SaveChangesAsync(); + var imatrix = new ImatrixDefinition + { + AiModelHashId = model.Id, + IdentityHash = "imatrix-" + Guid.NewGuid().ToString("N"), + SourceKind = "test" + }; + db.ImatrixDefinitions.Add(imatrix); + await db.SaveChangesAsync(); - var run = new QuantizationRun - { - AiModelHashId = model.Id, - ImatrixDefinitionId = imatrix.Id, - TensorComboId = combo.Id, - StartedUtc = DateTime.UtcNow.AddSeconds(-1), - CompletedUtc = DateTime.UtcNow, - DurationMs = 1000, - Succeeded = true, - OutputModelPath = Path.Combine(tempRoot, "output.gguf") - }; - db.QuantizationRuns.Add(run); - await db.SaveChangesAsync(); + var run = new QuantizationRun + { + ArchitectureFamilyId = architecture.Id, + TensorGroupProfileId = profile.Id, + AiModelHashId = model.Id, + ImatrixDefinitionId = imatrix.Id, + TensorComboId = combo.Id, + StartedUtc = DateTime.UtcNow.AddSeconds(-1), + CompletedUtc = DateTime.UtcNow, + DurationMs = 1000, + Succeeded = true, + OutputModelPath = Path.Combine(tempRoot, "output.gguf") + }; + db.QuantizationRuns.Add(run); + await db.SaveChangesAsync(); - var loaded = await db.QuantizationRuns - .Include(x => x.ImatrixDefinition) - .SingleAsync(x => x.Id == run.Id); + var loaded = await db.QuantizationRuns + .Include(x => x.ImatrixDefinition) + .SingleAsync(x => x.Id == run.Id); - Assert.Equal(imatrix.Id, loaded.ImatrixDefinitionId); - Assert.NotNull(loaded.ImatrixDefinition); - Assert.Equal(imatrix.IdentityHash, loaded.ImatrixDefinition!.IdentityHash); + Assert.Equal(imatrix.Id, loaded.ImatrixDefinitionId); + Assert.NotNull(loaded.ImatrixDefinition); + Assert.Equal(imatrix.IdentityHash, loaded.ImatrixDefinition!.IdentityHash); + } + finally + { + Cache.MagicQuantDirectory = priorMagicQuantDirectory; + if (Directory.Exists(tempRoot)) + Directory.Delete(tempRoot, recursive: true); + } } [Fact] - public async Task BuildHybrids_RunWithoutHelp_ThrowsNotImplementedException() + public async Task BuildHybrids_RunWithoutModel_RoutesThroughEvolutionValidation() { - var command = new BuildHybrids(); - var ex = await Assert.ThrowsAsync(() => command.Run(new List())); - Assert.Contains("disabled", ex.Message, StringComparison.OrdinalIgnoreCase); + var priorConfig = Config.Current; + try + { + Config.Load(MagicQuantYamlConfig.CreateDefault()); + var command = new BuildHybrids(); + var ex = await Assert.ThrowsAsync(() => command.Run(new List())); + Assert.Contains("model directory", ex.Message, StringComparison.OrdinalIgnoreCase); + } + finally + { + Config.Load(priorConfig); + } } } From a06dd21d77a14eb3d3928c070d6ed4e4f1398c27 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Fri, 21 Aug 2026 20:12:32 -0400 Subject: [PATCH 248/258] Pin Qwen3.8 dynamic-v3 campaign source --- MagicQuant/config.dev.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml index 698ebf5..09e8ddc 100644 --- a/MagicQuant/config.dev.yaml +++ b/MagicQuant/config.dev.yaml @@ -276,6 +276,8 @@ baselines: custom_repositories: - repo_id: unsloth/Qwen3.8-27B-GGUF + # Immutable dynamic-v3 revision used by the completed Qwen3.8 campaign. + revision: 4ca720788d1e01f1bff70c033e0d0028fd02e502 enabled: true short_source_name: Unsloth source_kind: huggingface_gguf_repository From 8d1415a189a2cd917a005969507755398299034d Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 18:54:43 -0400 Subject: [PATCH 249/258] Prepare MagicQuant for contributors with documented workflows and CLI cleanup --- .editorconfig | 16 + .github/pull_request_template.md | 7 + .github/workflows/dotnet.yml | 26 + .gitignore | 13 + .../.idea/.gitignore | 38 - CONTRIBUTING.md | 40 + MQ.DB/Cache.cs | 2 +- MQ.DB/MQ.DB.csproj | 1 - MagicQuant-Pipeline.sln | 6 + MagicQuant.Tests/CliStartupTests.cs | 100 +++ .../CombinationDatabasePathTests.cs | 40 + .../ConfigurationContractTests.cs | 58 ++ .../HardwareInitializationTests.cs | 58 +- MagicQuant.Tests/OutputPathTests.cs | 65 ++ MagicQuant/Commands/BuildHybrids.cs | 10 +- MagicQuant/Commands/CloneRepositoryQuants.cs | 15 +- MagicQuant/Commands/CommandCatalog.cs | 19 + MagicQuant/Commands/Evolution.cs | 817 +----------------- MagicQuant/Commands/InitializeLlamaCpp.cs | 45 +- MagicQuant/Commands/QuantizationPipeline.cs | 805 +++++++++++++++++ MagicQuant/Commands/ValidatePredictions.cs | 9 +- MagicQuant/Config.cs | 25 +- MagicQuant/Configs/config.dev.yaml | 413 --------- .../config.qwen3-4B-2507-Instruct.dev.yaml | 401 --------- .../Configs/config.qwen3.6-27b.dev.yaml | 401 --------- .../Configuration/MagicQuantYamlConfig.cs | 84 +- .../Configuration/MagicQuantYamlLoader.cs | 38 +- MagicQuant/Helpers/CliHelpers.cs | 10 +- MagicQuant/MagicQuant.csproj | 13 - MagicQuant/Program.cs | 67 +- .../CloneConfigManifestGenerationService.cs | 2 +- .../CombinationDatabasePathService.cs | 37 + .../Services/ModelArtifactPathService.cs | 4 + .../Services/ModelRuntimePathService.cs | 3 + MagicQuant/Services/OutputPathService.cs | 27 + MagicQuant/Services/QuantDatabaseService.cs | 25 +- MagicQuant/Services/QuantizationService.cs | 4 +- .../Services/ReadmeGenerationService.cs | 2 +- .../Services/RemainingCombinationStore.cs | 36 +- .../Services/TensorGroupReviewService.cs | 2 +- MagicQuant/config.clone-unsloth.dev.yaml | 35 - MagicQuant/config.default.yaml | 25 +- MagicQuant/config.dev.yaml | 627 -------------- README.md | 78 ++ docs/architecture.md | 54 ++ docs/commands.md | 62 ++ docs/configuration.md | 55 ++ docs/migration.md | 35 + docs/setup.md | 55 ++ docs/storage.md | 47 + examples/clone.yaml | 9 + examples/pipeline.yaml | 12 + 52 files changed, 1818 insertions(+), 3060 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/dotnet.yml delete mode 100644 .idea/.idea.MagicQuant-Pipeline/.idea/.gitignore create mode 100644 CONTRIBUTING.md create mode 100644 MagicQuant.Tests/CliStartupTests.cs create mode 100644 MagicQuant.Tests/CombinationDatabasePathTests.cs create mode 100644 MagicQuant.Tests/ConfigurationContractTests.cs create mode 100644 MagicQuant.Tests/OutputPathTests.cs create mode 100644 MagicQuant/Commands/CommandCatalog.cs create mode 100644 MagicQuant/Commands/QuantizationPipeline.cs delete mode 100644 MagicQuant/Configs/config.dev.yaml delete mode 100644 MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml delete mode 100644 MagicQuant/Configs/config.qwen3.6-27b.dev.yaml create mode 100644 MagicQuant/Services/CombinationDatabasePathService.cs create mode 100644 MagicQuant/Services/OutputPathService.cs delete mode 100644 MagicQuant/config.clone-unsloth.dev.yaml delete mode 100644 MagicQuant/config.dev.yaml create mode 100644 README.md create mode 100644 docs/architecture.md create mode 100644 docs/commands.md create mode 100644 docs/configuration.md create mode 100644 docs/migration.md create mode 100644 docs/setup.md create mode 100644 docs/storage.md create mode 100644 examples/clone.yaml create mode 100644 examples/pipeline.yaml diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..468dfaa --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +charset = utf-8 +insert_final_newline = true +indent_style = space +indent_size = 4 + +[*.{json,yaml,yml}] +indent_size = 2 + +[*.cs] +csharp_style_namespace_declarations = file_scoped:suggestion + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..0b69677 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,7 @@ +## Change + +Describe the problem and resulting behavior. Note changes to numerical policy, configuration, paths, or persisted formats. + +## Validation + +List relevant tests and any model/hardware checks. State material limitations. diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 0000000..e1d6b8c --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -0,0 +1,26 @@ +name: .NET checks +on: + push: + branches: [main] + pull_request: +permissions: + contents: read +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + configuration: [Debug, Release] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: '10.0.x' + - run: dotnet restore MagicQuant-Pipeline.sln + - run: dotnet build MagicQuant-Pipeline.sln --configuration ${{ matrix.configuration }} --no-restore + - run: dotnet test MagicQuant-Pipeline.sln --configuration ${{ matrix.configuration }} --no-build --logger trx --results-directory TestResults + - uses: actions/upload-artifact@v4 + if: always() + with: + name: test-results-${{ matrix.configuration }} + path: TestResults/*.trx diff --git a/.gitignore b/.gitignore index 7e060eb..f4708ae 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,16 @@ obj/ # Other common C# stuff *.log *.vs/ + +# Local campaigns and generated model/runtime artifacts +config.local.yaml +*.local.yaml +*.dev.yaml +**/MagicQuant_SQLite.db* +*.duckdb +*.duckdb.wal +*.gguf +*.safetensors +.MagicQuant_tmp/ +TestResults/ +artifacts/ diff --git a/.idea/.idea.MagicQuant-Pipeline/.idea/.gitignore b/.idea/.idea.MagicQuant-Pipeline/.idea/.gitignore deleted file mode 100644 index b428136..0000000 --- a/.idea/.idea.MagicQuant-Pipeline/.idea/.gitignore +++ /dev/null @@ -1,38 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Rider ignored files -/modules.xml -/.idea.MagicQuant-Pipeline.iml -/contentModel.xml -/projectSettingsUpdater.xml -# Ignored default folder with query files -/queries/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ - -# Build results -bin/ -obj/ - -# Rider / JetBrains -.idea/ -*.sln.iml - -# Visual Studio user settings -*.user -*.userosscache -*.suo -*.cache -*.dbmdl -*.bak -*.ncb -*.opendb -*.VC.db - -# Other common C# stuff -*.log -*.vs/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..819609c --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,40 @@ +# Contributing + +Start with the [architecture map](docs/architecture.md), [configuration rules](docs/configuration.md), and the [research wiki](https://github.com/magiccodingman/MagicQuant-Wiki). This repository implements benchmark-driven discovery; the `evolution` name survives as a compatibility alias. + +## Local workflow + +```sh +dotnet restore MagicQuant-Pipeline.sln +dotnet build MagicQuant-Pipeline.sln -c Debug --no-restore +dotnet test MagicQuant-Pipeline.sln -c Debug --no-build +dotnet build MagicQuant-Pipeline.sln -c Release --no-restore +dotnet test MagicQuant-Pipeline.sln -c Release --no-build +``` + +CI runs both configurations on Linux. Use `--filter FullyQualifiedName~YourTestClass` to focus a test run during development. Tests run serially because configuration and runtime registries are global. Source-contract regression tests assume the normal repository/build layout; run the suite from the checkout rather than copying the test DLL elsewhere. + +Keep personal settings in an ignored `config.local.yaml` and pass `--config` explicitly. Do not add machine paths or automatic DEBUG campaigns to `Program.cs`. Use IDE run arguments for your campaign. Never commit weights, runtime databases, exported GGUFs, credentials, or local logs. + +## Making a change + +- Keep commands focused on orchestration; extract cohesive policy or path logic into services when it can be tested independently. +- Use existing baseline identity, effective-state, and path helpers. Do not duplicate database filenames, tensor-slot ordering, or custom-baseline normalization. +- Explain why a non-obvious constraint exists in a comment. Avoid comments that merely restate a method call or retain obsolete blocks of disabled implementation. +- When adding a config option, update the typed model, loader override if needed, commented default YAML, documentation, and a behavior test. Distinguish C# defaults from the distributed YAML profile. +- Test observable behavior: boundary cases, context scoping, cache reuse/invalidation, ranking/tie rules, path resolution, or failure propagation. Avoid tests that only repeat the implementation's constants without exercising a contract. +- Treat persisted IDs, database schemas, manifests, and artifact names as compatibility contracts. Provide an explicit migration plan for changes to them. + +For a bug fix, add a regression that fails without the fix. Tests that mutate `Config`, `Cache`, or registries must restore prior state in `finally`; use unique temporary roots. Keep unit tests free of network downloads, sudo, model quantization, and persistent changes to a developer's runtime. + +## Hardware integration changes + +Changes to conversion, quantization arguments, benchmark scheduling, or numerical selection need a small-model integration check in addition to unit tests. Record model identity, hardware, imatrix, dependency revisions, command/config, observed outputs, and before/after metrics. Use an isolated output directory. Do not claim full quantization parity from a passing unit suite. + +For documentation or path refactoring, verify examples against actual help and protect historical path rules. Avoid re-running expensive full campaigns when the changed behavior can be checked directly. + +## Pull request expectations + +Describe the concrete problem and resulting behavior, relevant compatibility effects, and validation performed. Separate numerical policy changes from mechanical cleanup when possible. Mention untested hardware/platform paths and any remaining compiler warnings. Prefer focused commits that can be reviewed without reconstructing the conversation that led to them. + +The maintainer still needs to choose a software license before an open-source release; do not infer one from generated model metadata or dependency licenses. diff --git a/MQ.DB/Cache.cs b/MQ.DB/Cache.cs index c8b21f4..a1d403a 100644 --- a/MQ.DB/Cache.cs +++ b/MQ.DB/Cache.cs @@ -130,7 +130,7 @@ public enum MainTorchType /// /// Clone/export-only flows may benchmark for release metadata without polluting the - /// learning/evolution SQLite truth tables. + /// learning/discovery SQLite truth tables. /// public static bool SuppressBenchmarkPersistence { get; set; } diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj index 6e40d18..a1e614b 100644 --- a/MQ.DB/MQ.DB.csproj +++ b/MQ.DB/MQ.DB.csproj @@ -19,7 +19,6 @@ - diff --git a/MagicQuant-Pipeline.sln b/MagicQuant-Pipeline.sln index a933660..b574b7d 100644 --- a/MagicQuant-Pipeline.sln +++ b/MagicQuant-Pipeline.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant", "MagicQuant\Ma EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQ.DB", "MQ.DB\MQ.DB.csproj", "{A97D6992-2659-47F9-9AC9-99425D2677A4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.Tests", "MagicQuant.Tests\MagicQuant.Tests.csproj", "{D106FC82-5FD7-4C95-BF20-0940C64A234C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,5 +20,9 @@ Global {A97D6992-2659-47F9-9AC9-99425D2677A4}.Debug|Any CPU.Build.0 = Debug|Any CPU {A97D6992-2659-47F9-9AC9-99425D2677A4}.Release|Any CPU.ActiveCfg = Release|Any CPU {A97D6992-2659-47F9-9AC9-99425D2677A4}.Release|Any CPU.Build.0 = Release|Any CPU + {D106FC82-5FD7-4C95-BF20-0940C64A234C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D106FC82-5FD7-4C95-BF20-0940C64A234C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D106FC82-5FD7-4C95-BF20-0940C64A234C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D106FC82-5FD7-4C95-BF20-0940C64A234C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/MagicQuant.Tests/CliStartupTests.cs b/MagicQuant.Tests/CliStartupTests.cs new file mode 100644 index 0000000..3501b14 --- /dev/null +++ b/MagicQuant.Tests/CliStartupTests.cs @@ -0,0 +1,100 @@ +using System.Diagnostics; +using MagicQuant.Commands; +using Xunit; + +namespace MagicQuant.Tests; + +/// Exercise the real executable so startup cannot hide side effects behind command help. +public sealed class CliStartupTests +{ + [Theory] + [InlineData("")] + [InlineData("help")] + [InlineData("--help")] + [InlineData("-h")] + [InlineData("pipeline")] + [InlineData("evolution")] + [InlineData("build-hybrids")] + [InlineData("clone-repository-quants")] + [InlineData("validate-predictions")] + [InlineData("initialize-llama-cpp")] + public async Task Help_succeeds_without_config_or_runtime_artifacts(string command) + { + string[] arguments = command switch + { + "" => [], + "help" or "--help" or "-h" => [command], + _ => [command, "--help", "--config", "does-not-exist.yaml"] + }; + var result = await RunAsync(arguments); + Assert.True(result.ExitCode == 0, result.Output); + Assert.DoesNotContain("Using config:", result.Output); + Assert.DoesNotContain("Checking environment", result.Output); + Assert.Empty(result.CreatedFiles); + } + + [Fact] + public async Task Unknown_command_is_escaped_and_returns_usage_error() + { + var result = await RunAsync(["[invalid]"]); + Assert.Equal(2, result.ExitCode); + Assert.Contains("does not exist", result.Output); + Assert.Empty(result.CreatedFiles); + } + + [Fact] + public async Task Missing_config_returns_failure_before_setup() + { + var result = await RunAsync(["pipeline", "--config", "missing.yaml"]); + Assert.Equal(1, result.ExitCode); + Assert.Contains("config file was not found", result.Output); + Assert.Empty(result.CreatedFiles); + } + + [Fact] + public void Historical_alias_uses_the_same_pipeline_implementation() + { + var commands = CommandCatalog.Create(); + Assert.IsType(commands["pipeline"].Factory()); + Assert.IsType(commands["EVOLUTION"].Factory()); + Assert.IsAssignableFrom(new Evolution()); + } + + private static async Task<(int ExitCode, string Output, string[] CreatedFiles)> RunAsync(string[] args) + { + string directory = Path.Combine(Path.GetTempPath(), $"mq-cli-{Guid.NewGuid():N}"); + Directory.CreateDirectory(directory); + try + { + var start = new ProcessStartInfo("dotnet") + { + WorkingDirectory = directory, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false + }; + start.ArgumentList.Add(typeof(QuantizationPipeline).Assembly.Location); + foreach (string arg in args) + start.ArgumentList.Add(arg); + using var process = Process.Start(start)!; + Task stdout = process.StandardOutput.ReadToEndAsync(); + Task stderr = process.StandardError.ReadToEndAsync(); + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(20)); + try + { + await process.WaitForExitAsync(timeout.Token); + } + catch (OperationCanceledException) + { + process.Kill(entireProcessTree: true); + await process.WaitForExitAsync(); + throw; + } + return (process.ExitCode, await stdout + await stderr, Directory.GetFileSystemEntries(directory)); + } + finally + { + Directory.Delete(directory, recursive: true); + } + } +} diff --git a/MagicQuant.Tests/CombinationDatabasePathTests.cs b/MagicQuant.Tests/CombinationDatabasePathTests.cs new file mode 100644 index 0000000..186c984 --- /dev/null +++ b/MagicQuant.Tests/CombinationDatabasePathTests.cs @@ -0,0 +1,40 @@ +using MagicQuant.Helpers; +using MagicQuant.Services; +using MQ.DB; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class CombinationDatabasePathTests +{ + [Theory] + [InlineData(false, null, false, "no-imatrix_hp-off")] + [InlineData(true, "abc123", false, "abc123_hp-off")] + [InlineData(true, null, true, "imatrix-unknown_hp-on")] + public void Reader_path_preserves_context_filename(bool imatrix, string? hash, bool highPrecision, string suffix) + { + var previous = (Cache.ModelMagicQuantDirectory, Cache.MagicQuantDirectory, Cache.CurrentModelId, + Cache.IsImatrixAvailable, Cache.ActiveImatrixIdentityHash, RuntimeSearchSpace.AllowHighPrecisionHybrids); + try + { + Cache.ModelMagicQuantDirectory = Path.Combine(Path.GetTempPath(), "model", "MagicQuant"); + Cache.MagicQuantDirectory = Path.Combine(Path.GetTempPath(), "shared"); + Cache.CurrentModelId = "model123"; + Cache.IsImatrixAvailable = imatrix; + Cache.ActiveImatrixIdentityHash = hash; + RuntimeSearchSpace.AllowHighPrecisionHybrids = highPrecision; + string expected = Path.Combine(Cache.ModelMagicQuantDirectory, $"MagicQuant_Combinations_model123_{suffix}.duckdb"); + Assert.Equal(expected, CombinationDatabasePathService.GetPath()); + Assert.Equal(expected, new RemainingCombinationStore().GetDatabaseFilePath()); + Cache.ModelMagicQuantDirectory = null; + Assert.Equal(Cache.MagicQuantDirectory, CombinationDatabasePathService.GetDirectory()); + Cache.MagicQuantDirectory = null; + Assert.Throws(() => CombinationDatabasePathService.GetPath()); + } + finally + { + (Cache.ModelMagicQuantDirectory, Cache.MagicQuantDirectory, Cache.CurrentModelId, + Cache.IsImatrixAvailable, Cache.ActiveImatrixIdentityHash, RuntimeSearchSpace.AllowHighPrecisionHybrids) = previous; + } + } +} diff --git a/MagicQuant.Tests/ConfigurationContractTests.cs b/MagicQuant.Tests/ConfigurationContractTests.cs new file mode 100644 index 0000000..eb12785 --- /dev/null +++ b/MagicQuant.Tests/ConfigurationContractTests.cs @@ -0,0 +1,58 @@ +using MagicQuant.Configuration; +using MagicQuant.Models; +using Xunit; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace MagicQuant.Tests; + +public sealed class ConfigurationContractTests +{ + [Fact] + public void Default_config_selection_does_not_depend_on_build_configuration() + { + Assert.Equal(Path.Combine(AppContext.BaseDirectory, "config.default.yaml"), MagicQuantYamlLoader.ResolveConfigPath([])); + } + + [Fact] + public void Explicit_config_path_is_relative_to_working_directory() + { + Assert.Equal(Path.GetFullPath("configs/my campaign.yaml"), MagicQuantYamlLoader.ResolveConfigPath( + [new CliArg { Name = "CONFIG", Value = "configs/my campaign.yaml" }])); + } + + [Theory] + [InlineData("MagicQuant/config.default.yaml")] + [InlineData("examples/pipeline.yaml")] + [InlineData("examples/clone.yaml")] + public void Distributed_configs_have_no_unknown_keys(string relativePath) + { + string root = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../..")); + var config = new DeserializerBuilder() + .WithNamingConvention(UnderscoredNamingConvention.Instance) + .Build() + .Deserialize(File.ReadAllText(Path.Combine(root, relativePath))); + Assert.NotNull(config.Paths); + Assert.NotNull(config.CandidateSelection); + } + [Fact] + public void Legacy_inactive_yaml_remains_compatible_with_current_selection_settings() + { + const string yaml = """ + evolution: + max_survival_rounds: 100 + survival: + max_selected_choices_per_bucket: 50 + brain_layers: [embeddings] + candidate_selection: + max_fallback_attempts_per_anchor: 7 + """; + var config = new DeserializerBuilder() + .IgnoreUnmatchedProperties() + .WithNamingConvention(UnderscoredNamingConvention.Instance) + .Build() + .Deserialize(yaml); + Assert.Equal(7, config.CandidateSelection.MaxFallbackAttemptsPerAnchor); + } + +} diff --git a/MagicQuant.Tests/HardwareInitializationTests.cs b/MagicQuant.Tests/HardwareInitializationTests.cs index 7982953..c1037b0 100644 --- a/MagicQuant.Tests/HardwareInitializationTests.cs +++ b/MagicQuant.Tests/HardwareInitializationTests.cs @@ -1,4 +1,5 @@ using MagicQuant.Commands; +using MagicQuant.Configuration; using MagicQuant.Models; using MQ.DB; using Xunit; @@ -14,8 +15,10 @@ public sealed class HardwareInitializationCollection [Collection(HardwareInitializationCollection.Name)] public sealed class HardwareInitializationTests { - [Fact] - public async Task Custom_environment_validation_populates_system_info() + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Custom_environment_validation_populates_system_info(bool useYamlPaths) { string testRoot = Path.Combine( Path.GetTempPath(), @@ -24,6 +27,8 @@ public async Task Custom_environment_validation_populates_system_info() string llamaBin = Path.Combine(llamaRoot, "build", "bin"); string convertScript = Path.Combine(llamaRoot, "convert_hf_to_gguf.py"); var previous = Cache.SysInfo; + var previousPaths = (Cache.LlamaRoot, Cache.LlamaBin, Cache.ConvertScript); + var previousConfig = Config.Current; try { @@ -31,13 +36,28 @@ public async Task Custom_environment_validation_populates_system_info() await File.WriteAllTextAsync(convertScript, "# test"); Cache.SysInfo = null; - await new InitializeLlamaCpp().Run( - [ - new CliArg { Name = "validate", Value = string.Empty }, - new CliArg { Name = "llama-root", Value = llamaRoot }, - new CliArg { Name = "llama-bin", Value = llamaBin }, - new CliArg { Name = "convert-script", Value = convertScript } - ]); + var config = MagicQuantYamlConfig.CreateDefault(); + Config.Load(config); + List args = [new() { Name = "validate", Value = string.Empty }]; + if (useYamlPaths) + { + config.Paths.LlamaRoot = llamaRoot; + config.Paths.LlamaBin = llamaBin; + config.Paths.ConvertScript = convertScript; + } + else + { + args.AddRange([ + new CliArg { Name = "llama-root", Value = llamaRoot }, + new CliArg { Name = "llama-bin", Value = llamaBin }, + new CliArg { Name = "convert-script", Value = convertScript } + ]); + } + await new InitializeLlamaCpp().Run(args); + + Assert.Equal(llamaRoot, Cache.LlamaRoot); + Assert.Equal(llamaBin, Cache.LlamaBin); + Assert.Equal(convertScript, Cache.ConvertScript); Assert.NotNull(Cache.SysInfo); Assert.True(Cache.SysInfo.ThreadCount > 0); @@ -46,8 +66,28 @@ public async Task Custom_environment_validation_populates_system_info() finally { Cache.SysInfo = previous; + (Cache.LlamaRoot, Cache.LlamaBin, Cache.ConvertScript) = previousPaths; + Config.Load(previousConfig); if (Directory.Exists(testRoot)) Directory.Delete(testRoot, recursive: true); } } + [Fact] + public async Task Partial_custom_paths_fail_before_setup() + { + var previous = Config.Current; + try + { + Config.Load(MagicQuantYamlConfig.CreateDefault()); + await Assert.ThrowsAsync(() => new InitializeLlamaCpp().Run( + [new CliArg { Name = "llama-root", Value = "/missing/llama.cpp" }])); + await Assert.ThrowsAsync(() => new InitializeLlamaCpp().Run( + [new CliArg { Name = "llama-bin", Value = "/missing/bin" }])); + } + finally + { + Config.Load(previous); + } + } + } diff --git a/MagicQuant.Tests/OutputPathTests.cs b/MagicQuant.Tests/OutputPathTests.cs new file mode 100644 index 0000000..4852817 --- /dev/null +++ b/MagicQuant.Tests/OutputPathTests.cs @@ -0,0 +1,65 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class OutputPathTests +{ + private static readonly string ModelWork = Path.Combine(Path.GetTempPath(), "model with spaces", "MagicQuant"); + + [Fact] + public void Default_destinations_preserve_existing_command_layouts() + { + Assert.Equal(Path.Combine(ModelWork, "Final_Outputs"), OutputPathService.Pipeline(ModelWork, null)); + Assert.Equal(Path.Combine(ModelWork, "FinalOutput"), OutputPathService.Clone(ModelWork, null, null)); + Assert.Equal(Path.Combine(ModelWork, "PredictionValidation"), OutputPathService.PredictionValidation(ModelWork, null, null)); + } + + [Fact] + public void Relative_pipeline_output_is_model_local_but_clone_output_is_cwd_relative() + { + Assert.Equal(Path.Combine(ModelWork, "exports"), OutputPathService.Pipeline(ModelWork, "exports")); + Assert.Equal(Path.GetFullPath("exports"), OutputPathService.Clone(ModelWork, null, "exports")); + } + + [Fact] + public void Absolute_pipeline_output_overrides_model_directory() + { + string output = Path.Combine(Path.GetTempPath(), "other exports"); + Assert.Equal(output, OutputPathService.Pipeline(ModelWork, output)); + } + + [Fact] + public void Explicit_validation_destination_does_not_add_a_subdirectory() + { + Assert.Equal(Path.GetFullPath("reports"), OutputPathService.PredictionValidation(ModelWork, "reports", "ignored")); + Assert.Equal(Path.Combine(Path.GetFullPath("exports"), "PredictionValidation"), + OutputPathService.PredictionValidation(ModelWork, null, "exports")); + Assert.Equal(Path.GetFullPath("exports"), OutputPathService.Clone(ModelWork, "exports", "ignored")); + } + + [Theory] + [InlineData("magicquant.final-survivors.json")] + [InlineData("magicquant-manifest/magicquant.final-survivors.json")] + [InlineData("magicquant-manifest\\magicquant.final-survivors.json")] + public void Manifest_links_have_one_directory_prefix_and_forward_slashes(string file) + { + Assert.Equal("magicquant-manifest/magicquant.final-survivors.json", MagicQuantManifestPathService.RelativeManifestPath(file)); + } + + [Fact] + public void Manifest_directory_creation_is_idempotent_with_trailing_separator() + { + string root = Path.Combine(Path.GetTempPath(), $"mq-manifest-{Guid.NewGuid():N}"); + try + { + string manifest = MagicQuantManifestPathService.EnsureManifestDirectory(root); + Assert.Equal(manifest, MagicQuantManifestPathService.EnsureManifestDirectory(manifest + Path.DirectorySeparatorChar)); + Assert.Empty(Directory.GetDirectories(manifest)); + } + finally + { + Directory.Delete(root, recursive: true); + } + } +} diff --git a/MagicQuant/Commands/BuildHybrids.cs b/MagicQuant/Commands/BuildHybrids.cs index 5ebff27..ef6fd9d 100644 --- a/MagicQuant/Commands/BuildHybrids.cs +++ b/MagicQuant/Commands/BuildHybrids.cs @@ -13,14 +13,14 @@ public async Task Run(List args) return; } - AnsiConsole.MarkupLine("[grey]build-hybrids now routes through the centralized evolution/survival/export pipeline.[/]"); - await new Evolution().Run(args); + AnsiConsole.MarkupLine("[grey]build-hybrids now routes through the centralized learning/selection/export pipeline.[/]"); + await new QuantizationPipeline().Run(args); } private static void ShowHelp() { AnsiConsole.MarkupLine("[bold yellow]Command: build-hybrids[/]"); - AnsiConsole.MarkupLine("Runs the centralized survival/export flow over the active MagicQuant evolution pipeline."); - AnsiConsole.MarkupLine("Usage: mq build-hybrids --model-dir \"\" [--config \"./config.default.yaml\"] [--output-dir \"\"] [--output-name-prefix \"Model\"] [--reuse-existing-final-artifacts] [--export-external-learned-baselines]"); + AnsiConsole.MarkupLine("Runs the centralized survival/export flow over the active MagicQuant pipeline."); + AnsiConsole.WriteLine("Usage: mq build-hybrids --model-dir \"\" [--config \"./config.default.yaml\"] [--output-dir \"\"] [--output-name-prefix \"Model\"] [--reuse-existing-final-artifacts] [--export-external-learned-baselines]"); } -} \ No newline at end of file +} diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 2c72b26..2da8823 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -1029,13 +1029,8 @@ private static async Task EnsureSqliteReadyAsync() private static string ResolveAndValidateOutputDirectory(IReadOnlyCollection args) { string? explicitOutput = Get(args, "output-dir"); - string outputDir = !string.IsNullOrWhiteSpace(explicitOutput) - ? explicitOutput! - : !string.IsNullOrWhiteSpace(Config.OutputDirectory) - ? Config.OutputDirectory! - : Path.Combine(Cache.ModelMagicQuantDirectory!, "FinalOutput"); - - outputDir = Path.GetFullPath(outputDir); + string outputDir = OutputPathService.Clone( + Cache.ModelMagicQuantDirectory!, explicitOutput, Config.OutputDirectory); Directory.CreateDirectory(outputDir); return outputDir; } @@ -1046,10 +1041,10 @@ private static string ResolveAndValidateOutputDirectory(IReadOnlyCollection\" --architecture-family \"\" --source-repo \"owner/repo\" [--output-dir \"\"] [--reuse-existing-final-artifacts]"); - AnsiConsole.MarkupLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-json \"\" [--output-dir \"\"] [--reuse-existing-final-artifacts]"); + AnsiConsole.WriteLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-repo \"owner/repo\" [--output-dir \"\"] [--reuse-existing-final-artifacts]"); + AnsiConsole.WriteLine(" mq clone-repository-quants --model-dir \"\" --architecture-family \"\" --source-json \"\" [--output-dir \"\"] [--reuse-existing-final-artifacts]"); AnsiConsole.MarkupLine("Options:"); AnsiConsole.MarkupLine($" --source-repo Hugging Face repo containing {MagicQuantManifestPathService.RelativeManifestPath(MagicQuantManifestPathService.CloneConfigsFileName)} or legacy root {MagicQuantManifestPathService.CloneConfigsFileName}"); AnsiConsole.MarkupLine(" --source-json Local or http(s) path to magicquant.clone-configs.json"); diff --git a/MagicQuant/Commands/CommandCatalog.cs b/MagicQuant/Commands/CommandCatalog.cs new file mode 100644 index 0000000..d4f3e81 --- /dev/null +++ b/MagicQuant/Commands/CommandCatalog.cs @@ -0,0 +1,19 @@ +namespace MagicQuant.Commands; + +/// One registry for dispatch and top-level help, including script compatibility aliases. +public static class CommandCatalog +{ + public static bool IsHelp(string argument) => + argument.Equals("help", StringComparison.OrdinalIgnoreCase) || argument is "--help" or "-h"; + + public static Dictionary Factory)> Create() => + new(StringComparer.OrdinalIgnoreCase) + { + ["pipeline"] = ("Learn baselines, discover hybrids, validate and export survivors", () => new QuantizationPipeline()), + ["evolution"] = ("Compatibility alias for pipeline", () => new QuantizationPipeline()), + ["validate-predictions"] = ("Compare KLD predictions with existing SQLite benchmarks", () => new ValidatePredictions()), + ["build-hybrids"] = ("Compatibility entry point for the full pipeline and export", () => new BuildHybrids()), + ["clone-repository-quants"] = ("Rebuild final tensor configurations from a compatible repository/manifest", () => new CloneRepositoryQuants()), + ["initialize-llama-cpp"] = ("Initialize or update llama.cpp and Python dependencies", () => new InitializeLlamaCpp()) + }; +} diff --git a/MagicQuant/Commands/Evolution.cs b/MagicQuant/Commands/Evolution.cs index 8adb587..d1a224b 100644 --- a/MagicQuant/Commands/Evolution.cs +++ b/MagicQuant/Commands/Evolution.cs @@ -1,814 +1,7 @@ -using MagicQuant.Configuration; -using MagicQuant.Helpers; -using MagicQuant.Models; -using MagicQuant.Services; -using MagicQuant.Services.Progress; -using MQ.DB; -using MQ.DB.Data; -using MQ.DB.Models; -using MQ.DB.Models.DbModels; -using Microsoft.EntityFrameworkCore; -using Spectre.Console; - namespace MagicQuant.Commands; -public class Evolution : ICommand -{ - private static readonly string[] RequiredNativeKldDomains = ["general", "code", "math"]; - - public async Task Run(List args) - { - if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) - { - ShowEvolutionHelp(); - return; - } - - string? modelDirRaw = args.FirstOrDefault(a => - string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; - - if (string.IsNullOrWhiteSpace(modelDirRaw)) - modelDirRaw = Config.Current.Paths.ModelDir; - - if (string.IsNullOrWhiteSpace(modelDirRaw)) - { - const string msg = "[red]Error:[/] Missing required model directory. Provide [yellow]--model-dir[/] or set [yellow]paths.model_dir[/] in YAML."; - AnsiConsole.MarkupLine(msg); - ShowEvolutionHelp(); - throw new InvalidOperationException("Missing required model directory."); - } - - string fullModelPath = Path.GetFullPath(modelDirRaw); - - if (!Directory.Exists(fullModelPath)) - { - string msg = - $"[red]Error:[/] The directory [yellow]{Markup.Escape(fullModelPath)}[/] does not exist."; - AnsiConsole.MarkupLine(msg); - ShowEvolutionHelp(); - throw new DirectoryNotFoundException($"The directory '{fullModelPath}' does not exist."); - } - - var safeTensorFiles = Directory.GetFiles(fullModelPath, "*.safetensors", SearchOption.TopDirectoryOnly); - - if (safeTensorFiles.Length == 0) - { - AnsiConsole.MarkupLine( - $"[red]Error:[/] No [yellow].safetensors[/] files found in [blue]{Markup.Escape(fullModelPath)}[/]."); - AnsiConsole.MarkupLine("[grey]Please ensure this is a valid HuggingFace model directory.[/]"); - throw new InvalidOperationException("No .safetensors files were found in the provided model directory."); - } - - Cache.ModelDirectory = fullModelPath; - Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); - ModelRuntimePathService.InitializeForCurrentModel(); - await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); - await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); - Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; - Cache.UseImatrix = Config.Current.Flags.UseImatrix; - Cache.ForceImatrixRebuild = Config.Current.Flags.ForceImatrixRebuild; - - RuntimeSearchSpace.ResetForNewModel(); - RuntimeSearchSpace.SetImatrixAvailability(false); - RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; - - JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); - - if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) - Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); - - Cache.OutputDirectory = ResolveAndValidateOutputDirectory(); - - AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); - AnsiConsole.Write(new Rule("[yellow]Evolution Configuration[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"Model Path: [blue]{Markup.Escape(Cache.ModelDirectory)}[/]"); - AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); - AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); - AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length:N0}[/] safe tensors"); - AnsiConsole.MarkupLine($"Tensor Review: [cyan]{(Cache.ConfirmTensorGroupProfile ? "prompt" : "skip prompt")}[/]"); - AnsiConsole.MarkupLine($"Regex Rebucket: [cyan]{(Cache.RebucketLearnedTensorGroupsFromExistingTruth ? "enabled" : "disabled")}[/]"); - - if (string.IsNullOrEmpty(Cache.LlamaBin)) - AnsiConsole.MarkupLine("[yellow]Warning:[/] Llama binaries path not set in Cache. (Did Initialization run?)"); - - AnsiConsole.MarkupLine("[grey]Acquiring unique model ID...[/]"); - Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); - AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); - - var pyManager = new PythonManager(Cache.MagicQuantDirectory!); - - await EnsureSqliteReadyAsync(); - - var benchmarkService = new BenchmarkService(pyManager); - var quantizationService = new QuantizationService(benchmarkService); - var imatrixService = new ImatrixService(); - - string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; - var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); - - var sidecarService = new ModelSidecarArtifactService(pyManager); - await sidecarService.EnsureMmprojArtifactAvailableAsync(); - - // Review the active regex profile against the native/BF16 tensor list before - // architecture/profile-scoped learning truth is persisted or reused. This is - // the early "do these groups look sane?" gate for catching YAML regex mistakes. - await new TensorGroupReviewService().ReviewNativeTensorGroupingAsync( - quantizationService: quantizationService, - nativeGgufPath: bf16ModelGgufPath, - requireConfirmation: Cache.ConfirmTensorGroupProfile); - - var architectureFamilyService = new ArchitectureFamilyService(pyManager); - await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(bf16ModelGgufPath); - - var tensorGroupProfileService = new TensorGroupProfileService(); - await tensorGroupProfileService.EnsureCurrentProfileAsync(); - - var customBaselineService = new HuggingFaceBaselineService(pyManager); - var resolvedCustomBaselines = await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); - - if (Config.Current.Baselines.CustomRepositories.Any(x => x.Enabled) && resolvedCustomBaselines.Count == 0) - { - throw new InvalidOperationException( - "Custom baseline repositories were enabled, but no custom baselines resolved into the runtime registry."); - } - - await new TargetedRelearnService().PlanConfirmAndExecuteAsync(resolvedCustomBaselines); - - var imatrixRequest = new ImatrixRequest - { - UseImatrix = Cache.UseImatrix, - ForceRebuild = Cache.ForceImatrixRebuild, - ImatrixUrl = Config.Current.Imatrix.ImatrixUrl, - DatasetRepo = Config.Current.Imatrix.DatasetRepo, - DatasetSplit = Config.Current.Imatrix.DatasetSplit, - DatasetConfig = Config.Current.Imatrix.DatasetConfig, - LocalDatasetFile = Config.Current.Imatrix.DatasetLocalFile, - ModelDirectory = Cache.ModelDirectory!, - MagicQuantDirectory = Cache.ModelMagicQuantDirectory! - }; - - var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest, ct: default); - - if (imatrixEnsureResult.Enabled) - { - string canonicalPath = imatrixEnsureResult.CanonicalImatrixPath ?? "n/a"; - string rebuiltText = imatrixEnsureResult.Rebuilt ? "yes" : "no"; - AnsiConsole.MarkupLine( - $"[green]Imatrix active:[/] {Markup.Escape(canonicalPath)} (rebuilt={rebuiltText})"); - } - else - { - AnsiConsole.MarkupLine("[grey]Imatrix disabled for this run.[/]"); - } - - // Re-assert the live runtime flag from the imatrix resolution result so later phases - // cannot accidentally inherit a stale default. - RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); - - if (Cache.RebucketLearnedTensorGroupsFromExistingTruth) - { - await new TensorGroupRebucketService().RebucketFromExistingProfileTruthAsync(); - } - - string baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); - bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && - await benchmarkService.TryInitializeDynamicExecutionPlanFromCacheAsync( - q8QuantizationKey: q8QuantizationKey, - nativeModelPath: bf16ModelGgufPath, - nativeQuantizationKey: baseTypeName); - - if (!loadedPlanFromCache) - { - AnsiConsole.MarkupLine("[grey]Dynamic execution-plan cache not usable; probing Q8 + native anchors...[/]"); - await using var q8Lease = await quantizationService.BuildPureQ8ProbeLeaseAsync(); - await benchmarkService.EnsureDynamicExecutionPlanAsync( - q8ModelPath: q8Lease.GgufPath, - nativeModelPath: bf16ModelGgufPath, - q8QuantizationKey: q8QuantizationKey, - nativeQuantizationKey: baseTypeName, - forceRediscovery: Cache.ForceRefreshHardwareProbe); - } - - bool nativeTruthAlreadyLearned = - await quantizationService.HasNativeSourceLearnedTruthAsync(); - if (nativeTruthAlreadyLearned && loadedPlanFromCache) - { - AnsiConsole.MarkupLine( - "[grey]Native-source truth already exists and dynamic plan loaded from cache.[/]"); - } - var benchmarkRootDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks"); - var baseBenchDir = Path.Combine(benchmarkRootDir, baseTypeName); - var baseLogitsDir = Path.Combine(baseBenchDir, "logits"); - var pplCorporaDir = Path.Combine(benchmarkRootDir, "_ppl_corpora"); - - var baseModelQuant = HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()); - - await EnsureNativeBenchmarkEnvironmentReadyAsync( - benchmarkService: benchmarkService, - quantizationService: quantizationService, - baseModelQuant: baseModelQuant, - bf16ModelGgufPath: bf16ModelGgufPath, - baseBenchDir: baseBenchDir, - baseLogitsDir: baseLogitsDir, - pplCorporaDir: pplCorporaDir, - nativeTruthAlreadyLearned: nativeTruthAlreadyLearned); - - var compatibilityService = new ModelCompatibilityService(pyManager); - await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); - - // Compatibility must not be allowed to silently downgrade the live policy flags for the - // remainder of the evolution run. Re-assert them here as a final safeguard. - RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); - RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; - - PrintCustomBaselineRuntimeSummary(resolvedCustomBaselines, imatrixEnsureResult.Enabled); - - // No longer needed - //CliHelpers.ValidateCombinationLogicWorks(true); - - var comboCountBefore = ComboCounter.CountAll(); - var totalLearnedPruningResult = new LearnedBaselinePruningResult(); - - AnsiConsole.MarkupLine("[grey]Learned-baseline early pruning is disabled for this build. Startup sampling will proceed without learned-scheme candidate elimination.[/]"); - - AnsiConsole.Write(new Rule("[yellow]Initial Isolation Startup Samples[/]") { Justification = Justify.Left }); - - var isolationPlanner = new IsolationPlanningService(); - var initialPlan = isolationPlanner.BuildInitialPlan(Cache.UnusedTensorGroups); - - AnsiConsole.MarkupLine($"[grey]Queued initial startup samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); - - var initialSummary = await quantizationService.ProcessHybridBatchAsync( - initialPlan.Plans, - new StageProgressOptions - { - StageName = "Initial isolation startup samples", - Total = initialPlan.TotalCount, - MinimumNonSkippedSamplesBeforeEta = 2, - ShowEta = true, - CountSkippedForEta = false - }, - default); - - AnsiConsole.MarkupLine("[bold green]Initial startup sampling complete.[/]"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {initialSummary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {initialSummary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {initialSummary.Failed:N0}"); - - AnsiConsole.MarkupLine("[bold magenta]Evolution flow marker:[/] startup sampling finished. Learned-baseline early pruning remains disabled for subsequent phases."); - - var isolationOptimizer = new IsolationOptimizationService(); - - AnsiConsole.Write(new Rule("[yellow]Initial Probe Analysis[/]") { Justification = Justify.Left }); - var initialAnalysis = await isolationOptimizer.AnalyzeInitialIsolationProbesAsync(initialPlan); - - AnsiConsole.Write(new Rule("[yellow]Initial Probe Group Decisions[/]") { Justification = Justify.Left }); - PrintIsolationGroupDecisions(initialAnalysis.GroupDetails); - - foreach (var note in initialAnalysis.Notes) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Initial Probe Analysis"); - - AnsiConsole.Write(new Rule("[yellow]Continuation Isolation Samples[/]") { Justification = Justify.Left }); - - AnsiConsole.MarkupLine($"[grey]Groups continuing after early probe:[/] [cyan]{initialAnalysis.GroupsToContinue.Count:N0}[/]"); - - var continuationPlan = isolationPlanner.BuildContinuationPlan( - initialAnalysis.GroupsToContinue, - Cache.UnusedTensorGroups); - - if (continuationPlan.TotalCount > 0) - { - AnsiConsole.MarkupLine($"[grey]Queued continuation samples:[/] [cyan]{continuationPlan.TotalCount:N0}[/]"); - - var continuationSummary = await quantizationService.ProcessHybridBatchAsync( - continuationPlan.Plans, - new StageProgressOptions - { - StageName = "Continuation isolation samples", - Total = continuationPlan.TotalCount, - MinimumNonSkippedSamplesBeforeEta = 2, - ShowEta = true, - CountSkippedForEta = false - }, - default); - - AnsiConsole.MarkupLine("[bold green]Continuation sampling complete.[/]"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {continuationSummary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {continuationSummary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {continuationSummary.Failed:N0}"); - } - else - { - AnsiConsole.MarkupLine("[grey]No continuation samples were required after smallest-first gating.[/]"); - } - - var mergedPlan = initialPlan.MergeWith(continuationPlan); - - var archivalGroupIds = TReg.All - .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) - .Select(x => x.UniqueId) - .Except(initialAnalysis.GroupsToContinue) - .OrderBy(x => x) - .ToList(); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Final Isolation Optimization"); - - AnsiConsole.Write(new Rule("[yellow]Final Isolation Optimization[/]") { Justification = Justify.Left }); - var isolationResult = await isolationOptimizer.AnalyzeAndApplyFinalAsync(mergedPlan); - - SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Final Isolation Optimization"); - - foreach (var gd in isolationResult.GroupDetails.OrderBy(x => x.GroupName)) - { - AnsiConsole.Write( - new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") - { - Justification = Justify.Left - }); - - AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); - AnsiConsole.MarkupLine($"[green]Winning candidate:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); - AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); - AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); - - foreach (var line in gd.Candidates) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); - } - - var comboCountAfterRulePruning = ComboCounter.CountAll(); - - var dbService = new QuantDatabaseService(); - await dbService.InitializeAsync(forceRebuild: true); - - // The old MDA/predicted-size ceiling pass is intentionally removed. - // DuckDB now stays as the allowed candidate universe, and the rank-safe - // isolation predictor chooses which candidates deserve real validation. - long predictedSizePruned = 0; - long highPrecisionPruned = await dbService.PruneHighPrecisionHybridCandidatesAsync(); - - AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {totalLearnedPruningResult.GroupCandidateEliminations:N0} [grey](early pruning disabled)[/]"); - AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {totalLearnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0} [grey](early pruning disabled)[/]"); - AnsiConsole.MarkupLine($"[green]Groups reduced to explicit-banned->Q8-fallback:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); - AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); - AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); - AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupCandidatesBanned:N0}"); - AnsiConsole.MarkupLine($"[green]Bad trade eliminations:[/] {isolationResult.BadTradeEliminations:N0}"); - AnsiConsole.MarkupLine($"[green]Synergy second-chance reinstatements:[/] {isolationResult.SynergySecondChanceReinstatements:N0}"); - AnsiConsole.MarkupLine($"[green]Final KLD cleanup eliminations:[/] {isolationResult.FinalKldCleanupEliminations:N0}"); - AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); - AnsiConsole.MarkupLine($"[green]Combination count after rule pruning:[/] {comboCountAfterRulePruning:N0}"); - AnsiConsole.MarkupLine($"[green]Predicted-size combo removals:[/] {predictedSizePruned:N0} [grey](obsolete MDA ceiling pruning removed)[/]"); - AnsiConsole.MarkupLine($"[green]Late-stage high-precision combo removals:[/] {highPrecisionPruned:N0}"); - - foreach (var note in isolationResult.Notes) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); - - long finalRemainingCombinationCount = await dbService.GetRemainingCombinationCountAsync(); - - AnsiConsole.MarkupLine($"[green]Final surviving combinations after stage-1 pruning:[/] {finalRemainingCombinationCount:N0}"); - - AnsiConsole.Write(new Rule("[yellow]Archival Isolation Coverage[/]") { Justification = Justify.Left }); - - var archivalCoveragePlan = isolationPlanner.BuildArchivalCoveragePlan( - groupIdsToArchive: archivalGroupIds, - existingPlanKeys: mergedPlan.Plans.Select(x => x.Key), - missingTensorGroups: Cache.UnusedTensorGroups); - - var archivalCoverageGroups = archivalCoveragePlan.Plans - .Where(x => x.TargetGroupId.HasValue) - .Select(x => x.TargetGroupId!.Value) - .Distinct() - .Count(); - - AnsiConsole.MarkupLine($"[grey]Groups queued for archival coverage:[/] [cyan]{archivalCoverageGroups:N0}[/]"); - AnsiConsole.MarkupLine($"[grey]Non-continuing groups targeted for archival fill:[/] [cyan]{archivalGroupIds.Count:N0}[/]"); - AnsiConsole.MarkupLine("[grey]This pass does not feed current-run pruning; it only fills missing isolated-sample coverage in the database for groups that were fixed/collapsed out of combo exploration.[/]"); - - if (archivalCoveragePlan.TotalCount > 0) - { - AnsiConsole.MarkupLine($"[grey]Queued archival isolation samples:[/] [cyan]{archivalCoveragePlan.TotalCount:N0}[/]"); - - var archivalCoverageSummary = await quantizationService.ProcessHybridBatchAsync( - archivalCoveragePlan.Plans, - new StageProgressOptions - { - StageName = "Archival isolation coverage samples", - Total = archivalCoveragePlan.TotalCount, - MinimumNonSkippedSamplesBeforeEta = 2, - ShowEta = true, - CountSkippedForEta = false - }, - default); - - AnsiConsole.MarkupLine("[bold green]Archival isolation coverage complete.[/]"); - AnsiConsole.MarkupLine($" [green]Completed:[/] {archivalCoverageSummary.Completed:N0}"); - AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {archivalCoverageSummary.Skipped:N0}"); - AnsiConsole.MarkupLine($" [red]Failed:[/] {archivalCoverageSummary.Failed:N0}"); - } - else - { - AnsiConsole.MarkupLine("[grey]No archival isolation coverage samples were required.[/]"); - } - - var finalIsolationManifestPlan = mergedPlan.MergeWith(archivalCoveragePlan); - - var survivalPipeline = new CombinationSurvivalPipelineService(quantizationService); - var finalizationResult = await survivalPipeline.RunAsync( - isolationSamplePlan: finalIsolationManifestPlan, - isolationOptimizationResult: isolationResult, - ct: default); - - AnsiConsole.Write(new Rule("[yellow]Export Summary[/]") { Justification = Justify.Left }); - AnsiConsole.MarkupLine($"[green]Export directory:[/] [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); - AnsiConsole.MarkupLine($"[green]Final brutal survivors:[/] [cyan]{finalizationResult.BrutalSurvivors.Count:N0}[/]"); - AnsiConsole.MarkupLine($"[green]Selected survivors:[/] [cyan]{finalizationResult.SelectedRows.Count(x => x.Enabled):N0}[/]"); - AnsiConsole.MarkupLine($"[green]Exported/linkable artifacts:[/] [cyan]{finalizationResult.ExportedArtifacts.Count:N0}[/]"); - } - - private static async Task EnsureNativeBenchmarkEnvironmentReadyAsync( - BenchmarkService benchmarkService, - QuantizationService quantizationService, - HybridQuant baseModelQuant, - string bf16ModelGgufPath, - string baseBenchDir, - string baseLogitsDir, - string pplCorporaDir, - bool nativeTruthAlreadyLearned) - { - var status = ValidateNativeBenchmarkEnvironment( - baseBenchDir: baseBenchDir, - baseLogitsDir: baseLogitsDir, - pplCorporaDir: pplCorporaDir, - requiredDomains: RequiredNativeKldDomains); - - bool mustRegenerateNativeBenchmarkArtifacts = - !status.IsValid; - - if (mustRegenerateNativeBenchmarkArtifacts) - { - AnsiConsole.Write(new Rule("[yellow]Native BF16 Benchmark/KLD Artifact Validation[/]") { Justification = Justify.Left }); - - AnsiConsole.MarkupLine("[yellow]Native BF16 benchmark/KLD artifacts are missing or incomplete.[/] Regenerating required artifacts."); - - PrintNativeBenchmarkEnvironmentIssues(status); - - await ForceRegenerateNativeBenchmarkArtifactsAsync( - benchmarkService: benchmarkService, - baseModelQuant: baseModelQuant, - bf16ModelGgufPath: bf16ModelGgufPath, - baseBenchDir: baseBenchDir, - baseLogitsDir: baseLogitsDir); - - status = ValidateNativeBenchmarkEnvironment( - baseBenchDir: baseBenchDir, - baseLogitsDir: baseLogitsDir, - pplCorporaDir: pplCorporaDir, - requiredDomains: RequiredNativeKldDomains); - - if (!status.IsValid) - { - var details = string.Join( - Environment.NewLine, - status.MissingOrInvalidArtifacts.Select(x => $"- {x}")); - - throw new InvalidOperationException( - "Native BF16 benchmark/logit generation completed, but required native benchmark artifacts are still missing or invalid. " + - "This is fatal because every non-base benchmark requires complete native KLD logits." + - Environment.NewLine + - details); - } - - AnsiConsole.MarkupLine("[green]Native BF16 benchmark/KLD artifacts validated.[/]"); - } - else - { - AnsiConsole.MarkupLine("[grey]Native BF16 benchmark/KLD artifacts already exist and passed validation.[/]"); - } - - // Disk artifact validation is not enough. Native tensor learning is tied to the - // persisted TensorCombo/AiBenchmark identity. The repair path above may run in - // transient mode so it can regenerate logits even when stale DB truth exists; after - // the artifacts are valid, explicitly hydrate/validate the SQLite benchmark row - // from those artifacts before native-source learning tries to attach to it. - await EnsureNativeBenchmarkDbTruthAsync( - benchmarkService: benchmarkService, - baseModelQuant: baseModelQuant, - bf16ModelGgufPath: bf16ModelGgufPath, - baseBenchDir: baseBenchDir, - baseLogitsDir: baseLogitsDir); - - if (!nativeTruthAlreadyLearned) - { - await quantizationService.LearnNativeSourceTruthAsync(bf16ModelGgufPath); - } - else - { - AnsiConsole.MarkupLine( - "[grey]Skipping native-source tensor relearn because learned native-source truth already exists.[/]"); - } - } - - private static async Task EnsureNativeBenchmarkDbTruthAsync( - BenchmarkService benchmarkService, - HybridQuant baseModelQuant, - string bf16ModelGgufPath, - string baseBenchDir, - string baseLogitsDir) - { - bool previousSuppressBenchmarkPersistence = Cache.SuppressBenchmarkPersistence; - - try - { - Cache.SuppressBenchmarkPersistence = false; - - await benchmarkService.RunAllBenchmarksAsync( - quantConfig: baseModelQuant, - modelPath: bf16ModelGgufPath, - benchDir: baseBenchDir, - klLogitsDir: baseLogitsDir, - saveLogits: true, - domainsOverride: RequiredNativeKldDomains); - - AnsiConsole.MarkupLine("[grey]Native BF16 benchmark DB truth hydrated/validated.[/]"); - } - finally - { - Cache.SuppressBenchmarkPersistence = previousSuppressBenchmarkPersistence; - } - } - - private static async Task ForceRegenerateNativeBenchmarkArtifactsAsync( - BenchmarkService benchmarkService, - HybridQuant baseModelQuant, - string bf16ModelGgufPath, - string baseBenchDir, - string baseLogitsDir) - { - if (Directory.Exists(baseBenchDir)) - { - AnsiConsole.MarkupLine( - $"[grey]Clearing incomplete/stale native benchmark directory:[/] {Markup.Escape(baseBenchDir)}"); - - Directory.Delete(baseBenchDir, recursive: true); - } - - Directory.CreateDirectory(baseBenchDir); - Directory.CreateDirectory(baseLogitsDir); - - bool previousSuppressBenchmarkPersistence = Cache.SuppressBenchmarkPersistence; - - try - { - // This is intentional. - // - // If persisted native BF16 benchmark rows already exist in SQLite, the normal - // BenchmarkService path may return DB truth without actually running llama-perplexity, - // which means missing KLD logits would stay missing forever. - // - // Transient mode forces this artifact-repair pass to rely on disk execution instead - // of DB benchmark truth. The native tensor truth is learned separately below. - Cache.SuppressBenchmarkPersistence = true; - - await benchmarkService.RunAllBenchmarksAsync( - quantConfig: baseModelQuant, - modelPath: bf16ModelGgufPath, - benchDir: baseBenchDir, - klLogitsDir: baseLogitsDir, - saveLogits: true, - domainsOverride: RequiredNativeKldDomains); - } - finally - { - Cache.SuppressBenchmarkPersistence = previousSuppressBenchmarkPersistence; - } - } - - private static NativeBenchmarkEnvironmentStatus ValidateNativeBenchmarkEnvironment( - string baseBenchDir, - string baseLogitsDir, - string pplCorporaDir, - IReadOnlyCollection requiredDomains) - { - var issues = new List(); - - if (string.IsNullOrWhiteSpace(baseBenchDir)) - { - issues.Add("Native benchmark directory path is null/empty."); - } - else if (!Directory.Exists(baseBenchDir)) - { - issues.Add($"Native benchmark directory does not exist: {baseBenchDir}"); - } - - if (string.IsNullOrWhiteSpace(baseLogitsDir)) - { - issues.Add("Native KLD logits directory path is null/empty."); - } - else if (!Directory.Exists(baseLogitsDir)) - { - issues.Add($"Native KLD logits directory does not exist: {baseLogitsDir}"); - } - - if (string.IsNullOrWhiteSpace(pplCorporaDir)) - { - issues.Add("_ppl_corpora directory path is null/empty."); - } - else if (!Directory.Exists(pplCorporaDir)) - { - issues.Add($"_ppl_corpora directory does not exist: {pplCorporaDir}"); - } - else if (!Directory.EnumerateFiles(pplCorporaDir, "*", SearchOption.AllDirectories).Any()) - { - issues.Add($"_ppl_corpora directory exists but contains no files: {pplCorporaDir}"); - } - - foreach (var domain in requiredDomains.OrderBy(x => x, StringComparer.Ordinal)) - { - if (!string.IsNullOrWhiteSpace(baseBenchDir) && Directory.Exists(baseBenchDir)) - { - var pplLog = Path.Combine(baseBenchDir, $"perplexity_{domain}.log"); - - if (!File.Exists(pplLog)) - { - issues.Add($"Missing native BF16 perplexity log for domain '{domain}': {pplLog}"); - } - else if (new FileInfo(pplLog).Length <= 0) - { - issues.Add($"Native BF16 perplexity log is empty for domain '{domain}': {pplLog}"); - } - } - - if (!string.IsNullOrWhiteSpace(baseLogitsDir) && Directory.Exists(baseLogitsDir)) - { - var logitsFile = Path.Combine(baseLogitsDir, $"kld_logits_{domain}.bin"); - - if (!File.Exists(logitsFile)) - { - issues.Add($"Missing native KLD logits for domain '{domain}': {logitsFile}"); - } - else if (new FileInfo(logitsFile).Length <= 0) - { - issues.Add($"Native KLD logits file is empty for domain '{domain}': {logitsFile}"); - } - } - } - - return new NativeBenchmarkEnvironmentStatus( - IsValid: issues.Count == 0, - MissingOrInvalidArtifacts: issues); - } - - private static void PrintNativeBenchmarkEnvironmentIssues(NativeBenchmarkEnvironmentStatus status) - { - if (status.IsValid) - return; - - foreach (var issue in status.MissingOrInvalidArtifacts.Take(20)) - AnsiConsole.MarkupLine($"[grey]- {Markup.Escape(issue)}[/]"); - - if (status.MissingOrInvalidArtifacts.Count > 20) - { - AnsiConsole.MarkupLine( - $"[grey]- ...and {status.MissingOrInvalidArtifacts.Count - 20:N0} more issue(s).[/]"); - } - } - - private sealed record NativeBenchmarkEnvironmentStatus( - bool IsValid, - IReadOnlyList MissingOrInvalidArtifacts); - - private static void PrintIsolationGroupDecisions(IEnumerable decisions) - { - foreach (var gd in decisions.OrderBy(x => x.GroupName)) - { - AnsiConsole.Write( - new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") - { - Justification = Justify.Left - }); - - AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); - AnsiConsole.MarkupLine($"[green]Winning candidate:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); - AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); - AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); - - foreach (var line in gd.Candidates) - AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); - } - } - - private static void PrintCustomBaselineRuntimeSummary( - IReadOnlyCollection resolvedCustomBaselines, - bool hasUsableImatrix) - { - AnsiConsole.Write(new Rule("[yellow]Custom Baseline Runtime Summary[/]") { Justification = Justify.Left }); - - var learning = BaselineQuants.GetLearningBaselines(hasUsableImatrix); - var carriers = BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix); - var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix, Config.Current.Flags.AllowHighPrecisionHybrids); - - AnsiConsole.MarkupLine($"[grey]Learning baselines in runtime registry:[/] [cyan]{learning.Count:N0}[/]"); - AnsiConsole.MarkupLine($"[grey]Combination carriers in runtime registry:[/] [cyan]{carriers.Count:N0}[/]"); - AnsiConsole.MarkupLine($"[grey]Explicit group candidates in runtime registry:[/] [cyan]{explicitCandidates.Count:N0}[/]"); - - if (resolvedCustomBaselines.Count == 0) - { - AnsiConsole.MarkupLine("[grey]No custom baselines were resolved for this run.[/]"); - return; - } - - AnsiConsole.MarkupLine($"[green]Custom baselines registered:[/] [cyan]{resolvedCustomBaselines.Count:N0}[/]"); - - foreach (var custom in resolvedCustomBaselines.OrderBy(x => x.DynamicBaselineId)) - { - bool inLearning = learning.Any(x => x.UniqueId == custom.DynamicBaselineId); - bool inCarriers = carriers.Any(x => x.UniqueId == custom.DynamicBaselineId); - bool inExplicit = explicitCandidates.Any(x => x.UniqueId == custom.DynamicBaselineId); - - string revision = string.IsNullOrWhiteSpace(custom.Revision) ? "main" : custom.Revision; - AnsiConsole.MarkupLine( - $" [cyan]{custom.DynamicBaselineId}[/] [yellow]{Markup.Escape(custom.DisplayName)}[/] family={Markup.Escape(custom.BaselineFamily)} file={Markup.Escape(custom.SourceFileName)} revision={Markup.Escape(revision)} learning={inLearning} carrier={inCarriers} explicit={inExplicit}"); - } - } - - private void ShowEvolutionHelp() - { - AnsiConsole.MarkupLine("[bold yellow]Command: evolution[/]"); - AnsiConsole.WriteLine("Runs the full quantization search on a target model, then uses rank-safe isolation prediction to choose validated final hybrids."); - AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Usage:[/]"); - AnsiConsole.WriteLine(" mq evolution --model-dir \"\" [options]"); - AnsiConsole.WriteLine(" mq evolution --config \"./config.default.yaml\""); - AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Arguments:[/]"); - AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Optional if set in YAML)"); - AnsiConsole.MarkupLine(" [green]--magic-quant-root[/] Isolated runtime root containing MagicQuant_SQLite.db and shared runtime assets (Optional)"); - AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); - AnsiConsole.MarkupLine(" [green]--use-imatrix[/] Enable imatrix acquisition/build and allow imatrix-required search candidates (Optional)"); - AnsiConsole.MarkupLine(" [green]--allow-high-precision-hybrids[/] Keep BF16/F16 explicit group candidates in final surviving combos (Optional, default false)"); - AnsiConsole.MarkupLine(" [green]--imatrix-force-rebuild[/] Delete/rebuild canonical imatrix artifacts before run (Optional)"); - AnsiConsole.MarkupLine(" [green]--imatrix-url[/] HTTPS URL for direct imatrix artifact download (Optional)"); - AnsiConsole.MarkupLine(" [green]--imatrix-dataset-repo[/] Hugging Face dataset repo ID for imatrix generation (Optional)"); - AnsiConsole.MarkupLine(" [green]--imatrix-dataset-split[/] Dataset split for HF/local dataset source metadata/build (Optional)"); - AnsiConsole.MarkupLine(" [green]--imatrix-dataset-config[/] Optional dataset config name for HF datasets (Optional)"); - AnsiConsole.MarkupLine(" [green]--imatrix-dataset-local-file[/] Full path to local .json/.jsonl dataset source (Optional)"); - AnsiConsole.MarkupLine(" [green]--selection-near-baseline-max-size-growth-percent[/] Phase-2 size premium for replacing a smaller/higher-damage anchor (Optional; default = 1.0)"); - AnsiConsole.MarkupLine(" [green]--selection-interior-window-fractions[/] Comma-separated phase-3 interior windows, e.g. 0.35,0.35 (Optional)"); - AnsiConsole.MarkupLine(" [green]--prediction-bit-stress-threshold-candidates[/] Comma-separated interaction-fit thresholds, e.g. 4,5,6,7,8,9,10,11,12 (Optional)"); - AnsiConsole.MarkupLine(" [green]--output-dir[/] Final export/output directory for selected survivor artifacts (Optional; default = /MagicQuant/Final_Outputs)"); - AnsiConsole.MarkupLine(" [green]--output-name-prefix[/] Output filename prefix for exported GGUF files (Optional; default = Model)"); - AnsiConsole.MarkupLine(" [green]--reuse-existing-final-artifacts[/] Reuse valid final GGUFs only when exact file name + benchmark byte size match (Optional; default false)"); - AnsiConsole.MarkupLine(" [green]--allow-eight-bit-anchor-replacements[/] Permit final prediction to try replacing 8-bit anchors like Q8_0 (Optional; default false)"); - AnsiConsole.MarkupLine(" [green]--export-external-learned-baselines[/] Also locally rebuild/export pure learned external baselines such as Unsloth (Optional; default false)"); - AnsiConsole.MarkupLine(" [green]--rebucket-learned-tensor-groups[/] Compatibility alias; regex rebucketing from DB is enabled by default"); - AnsiConsole.MarkupLine(" [green]--no-rebucket-learned-tensor-groups[/] Disable safe DB rebucketing and force the slower/full learned-group path instead"); - AnsiConsole.MarkupLine(" [green]--skip-tensor-group-confirm[/] Skip the native BF16 tensor-group review confirmation prompt for unattended runs (Optional; YAML default true asks)"); - AnsiConsole.MarkupLine(" [green]--selection-max-candidates-per-interior-window[/] Candidate count retained per interior window (Optional; default = 1)"); - AnsiConsole.MarkupLine(" [green]--config[/] Path to YAML runtime config. CLI flags override YAML values."); - AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold]Example:[/]"); - AnsiConsole.WriteLine(" mq evolution --model-dir \"C:\\Models\\Mistral-7B\""); - } - - private static string ResolveAndValidateOutputDirectory() - { - string resolved; - - if (!string.IsNullOrWhiteSpace(Config.Current.Output.OutputDir)) - { - resolved = Path.IsPathRooted(Config.Current.Output.OutputDir) - ? Path.GetFullPath(Config.Current.Output.OutputDir) - : Path.GetFullPath(Path.Combine(Cache.ModelMagicQuantDirectory!, Config.Current.Output.OutputDir)); - } - else - { - resolved = Path.Combine(Cache.ModelMagicQuantDirectory!, "Final_Outputs"); - } - - Directory.CreateDirectory(resolved); - - string probe = Path.Combine(resolved, $".write_test_{Guid.NewGuid():N}.tmp"); - File.WriteAllText(probe, "ok"); - File.Delete(probe); - - return resolved; - } - - private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) - { - await using var db = new MagicQuantContext(); - await db.Database.MigrateAsync(ct); - - var model = await db.AiModelHashes - .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); - - if (model != null) - return; - - db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); - await db.SaveChangesAsync(ct); - } -} +/// +/// Compatibility entry point for callers using the historical command class. +/// MagicQuant now performs benchmark-driven discovery rather than evolutionary search. +/// +public class Evolution : QuantizationPipeline; diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index 1a7c488..c12f726 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -12,23 +12,38 @@ public class InitializeLlamaCpp : ICommand { public async Task Run(List args) { + if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) + { + AnsiConsole.MarkupLine("[bold yellow]Command: initialize-llama-cpp[/]"); + AnsiConsole.WriteLine("Initialize llama.cpp and Python dependencies in the shared user MagicQuant directory."); + AnsiConsole.WriteLine(" --update Update dependencies and rebuild llama.cpp"); + AnsiConsole.WriteLine(" --llama-root Existing llama.cpp checkout (requires both paths below)"); + AnsiConsole.WriteLine(" --llama-bin Existing compiled binaries directory"); + AnsiConsole.WriteLine(" --convert-script Existing convert_hf_to_gguf.py file"); + AnsiConsole.WriteLine("Without custom paths, setup can download dependencies and request sudo on Linux."); + AnsiConsole.WriteLine("--validate / --verify retain setup behavior; they are not a read-only check."); + return; + } + // --------------------------------------------------------- // 1. Argument Parsing & Path Validation // --------------------------------------------------------- - bool validate = args.Any(a => a.Name?.ToLower() == "validate" || a.Name?.ToLower() == "verify"); bool update = args.Any(a => a.Name?.ToLower() == "update"); string? convertScript = args.FirstOrDefault(a => a.Name?.ToLower() == "convert-script")?.Value; string? llamaBin = args.FirstOrDefault(a => a.Name?.ToLower() == "llama-bin")?.Value; string? llamaRoot = args.FirstOrDefault(a => a.Name?.ToLower() == "llama-root")?.Value; + convertScript ??= Config.Current.Paths.ConvertScript; + llamaBin ??= Config.Current.Paths.LlamaBin; + llamaRoot ??= Config.Current.Paths.LlamaRoot; + // Custom Path Validation if (!string.IsNullOrEmpty(llamaRoot)) { if (string.IsNullOrEmpty(convertScript) || string.IsNullOrEmpty(llamaBin)) { - AnsiConsole.MarkupLine("[red]Error: If you provide custom paths, you must provide --llama-root, --llama-bin, AND --convert-script[/]"); - return; + throw new ArgumentException("Custom paths require --llama-root, --llama-bin, AND --convert-script (or their YAML equivalents)."); } // Normalize and Check @@ -38,18 +53,19 @@ public async Task Run(List args) if (!Directory.Exists(llamaRoot) || !Directory.Exists(llamaBin) || !File.Exists(convertScript)) { - AnsiConsole.MarkupLine("[red]Error: One or more provided custom paths do not exist.[/]"); - return; + throw new DirectoryNotFoundException("One or more custom llama.cpp paths do not exist."); } + Cache.LlamaRoot = llamaRoot; + Cache.LlamaBin = llamaBin; + Cache.ConvertScript = convertScript; AnsiConsole.MarkupLine("[green]✔ Custom Environment Validated.[/]"); _ = DetectAndCacheSystemInfo(); return; } else if (!string.IsNullOrEmpty(convertScript) || !string.IsNullOrEmpty(llamaBin)) { - AnsiConsole.MarkupLine("[red]Error: Partial paths provided. Provide ALL custom paths or NONE to use defaults.[/]"); - return; + throw new ArgumentException("Partial llama.cpp paths provided. Provide all three custom paths or none."); } // --------------------------------------------------------- @@ -88,10 +104,9 @@ public async Task Run(List args) { await RefreshSudoCredentialsAsync(); } - catch + catch (Exception ex) { - AnsiConsole.MarkupLine("[red]Error: Sudo access denied or cancelled. Cannot install system dependencies.[/]"); - return; + throw new InvalidOperationException("Sudo access denied or cancelled. Cannot install system dependencies.", ex); } // B. Run Install WITH sudo @@ -108,8 +123,7 @@ public async Task Run(List args) } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - AnsiConsole.MarkupLine("[red]Error: MacOS support coming soon.[/]"); - return; + throw new PlatformNotSupportedException("Automatic macOS setup is not implemented. Provide an existing llama.cpp environment."); } // --------------------------------------------------------- @@ -242,8 +256,11 @@ private static SystemInfo DetectAndCacheSystemInfo() private static async Task RunSimpleProcess(string exe, string args) { var startInfo = new ProcessStartInfo(exe, args) { UseShellExecute = false, CreateNoWindow = true }; - var p = Process.Start(startInfo); - await p!.WaitForExitAsync(); + using var p = Process.Start(startInfo) + ?? throw new InvalidOperationException($"Could not start {exe}."); + await p.WaitForExitAsync(); + if (p.ExitCode != 0) + throw new InvalidOperationException($"{exe} failed with exit code {p.ExitCode}."); } private async Task RefreshSudoCredentialsAsync() diff --git a/MagicQuant/Commands/QuantizationPipeline.cs b/MagicQuant/Commands/QuantizationPipeline.cs new file mode 100644 index 0000000..69f1e63 --- /dev/null +++ b/MagicQuant/Commands/QuantizationPipeline.cs @@ -0,0 +1,805 @@ +using MagicQuant.Configuration; +using MagicQuant.Helpers; +using MagicQuant.Models; +using MagicQuant.Services; +using MagicQuant.Services.Progress; +using MQ.DB; +using MQ.DB.Data; +using MQ.DB.Models; +using MQ.DB.Models.DbModels; +using Microsoft.EntityFrameworkCore; +using Spectre.Console; + +namespace MagicQuant.Commands; + +/// +/// Coordinates baseline learning, isolation measurements, prediction-guided selection, +/// real benchmark validation, and final export. Numerical policy lives in services. +/// +public class QuantizationPipeline : ICommand +{ + private static readonly string[] RequiredNativeKldDomains = ["general", "code", "math"]; + + public async Task Run(List args) + { + if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) + { + ShowPipelineHelp(); + return; + } + + string? modelDirRaw = args.FirstOrDefault(a => + string.Equals(a.Name, "model-dir", StringComparison.OrdinalIgnoreCase))?.Value; + + if (string.IsNullOrWhiteSpace(modelDirRaw)) + modelDirRaw = Config.Current.Paths.ModelDir; + + if (string.IsNullOrWhiteSpace(modelDirRaw)) + { + const string msg = "[red]Error:[/] Missing required model directory. Provide [yellow]--model-dir[/] or set [yellow]paths.model_dir[/] in YAML."; + AnsiConsole.MarkupLine(msg); + ShowPipelineHelp(); + throw new InvalidOperationException("Missing required model directory."); + } + + string fullModelPath = Path.GetFullPath(modelDirRaw); + + if (!Directory.Exists(fullModelPath)) + { + string msg = + $"[red]Error:[/] The directory [yellow]{Markup.Escape(fullModelPath)}[/] does not exist."; + AnsiConsole.MarkupLine(msg); + ShowPipelineHelp(); + throw new DirectoryNotFoundException($"The directory '{fullModelPath}' does not exist."); + } + + var safeTensorFiles = Directory.GetFiles(fullModelPath, "*.safetensors", SearchOption.TopDirectoryOnly); + + if (safeTensorFiles.Length == 0) + { + AnsiConsole.MarkupLine( + $"[red]Error:[/] No [yellow].safetensors[/] files found in [blue]{Markup.Escape(fullModelPath)}[/]."); + AnsiConsole.MarkupLine("[grey]Please ensure this is a valid HuggingFace model directory.[/]"); + throw new InvalidOperationException("No .safetensors files were found in the provided model directory."); + } + + Cache.ModelDirectory = fullModelPath; + Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); + ModelRuntimePathService.InitializeForCurrentModel(); + await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); + await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); + Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; + Cache.UseImatrix = Config.Current.Flags.UseImatrix; + Cache.ForceImatrixRebuild = Config.Current.Flags.ForceImatrixRebuild; + + RuntimeSearchSpace.ResetForNewModel(); + RuntimeSearchSpace.SetImatrixAvailability(false); + RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; + + JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); + + if (!Directory.Exists(Cache.ModelMagicQuantDirectory)) + Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); + + Cache.OutputDirectory = ResolveAndValidateOutputDirectory(); + + AnsiConsole.MarkupLine("[green]✔ Model Directory Validated[/]"); + AnsiConsole.Write(new Rule("[yellow]Pipeline Configuration[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"Model Path: [blue]{Markup.Escape(Cache.ModelDirectory)}[/]"); + AnsiConsole.MarkupLine($"Work Path: [blue]{Markup.Escape(Cache.ModelMagicQuantDirectory)}[/]"); + AnsiConsole.MarkupLine($"Export Path: [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); + AnsiConsole.MarkupLine($"Files Found: [green]{safeTensorFiles.Length:N0}[/] safe tensors"); + AnsiConsole.MarkupLine($"Tensor Review: [cyan]{(Cache.ConfirmTensorGroupProfile ? "prompt" : "skip prompt")}[/]"); + AnsiConsole.MarkupLine($"Regex Rebucket: [cyan]{(Cache.RebucketLearnedTensorGroupsFromExistingTruth ? "enabled" : "disabled")}[/]"); + + if (string.IsNullOrEmpty(Cache.LlamaBin)) + AnsiConsole.MarkupLine("[yellow]Warning:[/] Llama binaries path not set in Cache. (Did Initialization run?)"); + + AnsiConsole.MarkupLine("[grey]Acquiring unique model ID...[/]"); + Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); + AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); + + var pyManager = new PythonManager(Cache.MagicQuantDirectory!); + + await EnsureSqliteReadyAsync(); + + var benchmarkService = new BenchmarkService(pyManager); + var quantizationService = new QuantizationService(benchmarkService); + var imatrixService = new ImatrixService(); + + string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; + var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); + + var sidecarService = new ModelSidecarArtifactService(pyManager); + await sidecarService.EnsureMmprojArtifactAvailableAsync(); + + // Review the active regex profile against the native/BF16 tensor list before + // architecture/profile-scoped learning truth is persisted or reused. This is + // the early "do these groups look sane?" gate for catching YAML regex mistakes. + await new TensorGroupReviewService().ReviewNativeTensorGroupingAsync( + quantizationService: quantizationService, + nativeGgufPath: bf16ModelGgufPath, + requireConfirmation: Cache.ConfirmTensorGroupProfile); + + var architectureFamilyService = new ArchitectureFamilyService(pyManager); + await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(bf16ModelGgufPath); + + var tensorGroupProfileService = new TensorGroupProfileService(); + await tensorGroupProfileService.EnsureCurrentProfileAsync(); + + var customBaselineService = new HuggingFaceBaselineService(pyManager); + var resolvedCustomBaselines = await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); + + if (Config.Current.Baselines.CustomRepositories.Any(x => x.Enabled) && resolvedCustomBaselines.Count == 0) + { + throw new InvalidOperationException( + "Custom baseline repositories were enabled, but no custom baselines resolved into the runtime registry."); + } + + await new TargetedRelearnService().PlanConfirmAndExecuteAsync(resolvedCustomBaselines); + + var imatrixRequest = new ImatrixRequest + { + UseImatrix = Cache.UseImatrix, + ForceRebuild = Cache.ForceImatrixRebuild, + ImatrixUrl = Config.Current.Imatrix.ImatrixUrl, + DatasetRepo = Config.Current.Imatrix.DatasetRepo, + DatasetSplit = Config.Current.Imatrix.DatasetSplit, + DatasetConfig = Config.Current.Imatrix.DatasetConfig, + LocalDatasetFile = Config.Current.Imatrix.DatasetLocalFile, + ModelDirectory = Cache.ModelDirectory!, + MagicQuantDirectory = Cache.ModelMagicQuantDirectory! + }; + + var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest, ct: default); + + if (imatrixEnsureResult.Enabled) + { + string canonicalPath = imatrixEnsureResult.CanonicalImatrixPath ?? "n/a"; + string rebuiltText = imatrixEnsureResult.Rebuilt ? "yes" : "no"; + AnsiConsole.MarkupLine( + $"[green]Imatrix active:[/] {Markup.Escape(canonicalPath)} (rebuilt={rebuiltText})"); + } + else + { + AnsiConsole.MarkupLine("[grey]Imatrix disabled for this run.[/]"); + } + + // Re-assert the live runtime flag from the imatrix resolution result so later phases + // cannot accidentally inherit a stale default. + RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); + + if (Cache.RebucketLearnedTensorGroupsFromExistingTruth) + { + await new TensorGroupRebucketService().RebucketFromExistingProfileTruthAsync(); + } + + string baseTypeName = (Cache.TorchType ?? Cache.MainTorchType.BF16).ToString(); + bool loadedPlanFromCache = !Cache.ForceRefreshHardwareProbe && + await benchmarkService.TryInitializeDynamicExecutionPlanFromCacheAsync( + q8QuantizationKey: q8QuantizationKey, + nativeModelPath: bf16ModelGgufPath, + nativeQuantizationKey: baseTypeName); + + if (!loadedPlanFromCache) + { + AnsiConsole.MarkupLine("[grey]Dynamic execution-plan cache not usable; probing Q8 + native anchors...[/]"); + await using var q8Lease = await quantizationService.BuildPureQ8ProbeLeaseAsync(); + await benchmarkService.EnsureDynamicExecutionPlanAsync( + q8ModelPath: q8Lease.GgufPath, + nativeModelPath: bf16ModelGgufPath, + q8QuantizationKey: q8QuantizationKey, + nativeQuantizationKey: baseTypeName, + forceRediscovery: Cache.ForceRefreshHardwareProbe); + } + + bool nativeTruthAlreadyLearned = + await quantizationService.HasNativeSourceLearnedTruthAsync(); + if (nativeTruthAlreadyLearned && loadedPlanFromCache) + { + AnsiConsole.MarkupLine( + "[grey]Native-source truth already exists and dynamic plan loaded from cache.[/]"); + } + var benchmarkRootDir = Path.Combine(Cache.ModelMagicQuantDirectory!, "Benchmarks"); + var baseBenchDir = Path.Combine(benchmarkRootDir, baseTypeName); + var baseLogitsDir = Path.Combine(baseBenchDir, "logits"); + var pplCorporaDir = Path.Combine(benchmarkRootDir, "_ppl_corpora"); + + var baseModelQuant = HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()); + + await EnsureNativeBenchmarkEnvironmentReadyAsync( + benchmarkService: benchmarkService, + quantizationService: quantizationService, + baseModelQuant: baseModelQuant, + bf16ModelGgufPath: bf16ModelGgufPath, + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir, + pplCorporaDir: pplCorporaDir, + nativeTruthAlreadyLearned: nativeTruthAlreadyLearned); + + var compatibilityService = new ModelCompatibilityService(pyManager); + await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); + + // Compatibility must not be allowed to silently downgrade the live policy flags for the + // remainder of the pipeline run. Re-assert them here as a final safeguard. + RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Enabled); + RuntimeSearchSpace.AllowHighPrecisionHybrids = Config.Current.Flags.AllowHighPrecisionHybrids; + + PrintCustomBaselineRuntimeSummary(resolvedCustomBaselines, imatrixEnsureResult.Enabled); + + var comboCountBefore = ComboCounter.CountAll(); + var totalLearnedPruningResult = new LearnedBaselinePruningResult(); + + AnsiConsole.MarkupLine("[grey]Learned-baseline early pruning is disabled for this build. Startup sampling will proceed without learned-scheme candidate elimination.[/]"); + + AnsiConsole.Write(new Rule("[yellow]Initial Isolation Startup Samples[/]") { Justification = Justify.Left }); + + var isolationPlanner = new IsolationPlanningService(); + var initialPlan = isolationPlanner.BuildInitialPlan(Cache.UnusedTensorGroups); + + AnsiConsole.MarkupLine($"[grey]Queued initial startup samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); + + var initialSummary = await quantizationService.ProcessHybridBatchAsync( + initialPlan.Plans, + new StageProgressOptions + { + StageName = "Initial isolation startup samples", + Total = initialPlan.TotalCount, + MinimumNonSkippedSamplesBeforeEta = 2, + ShowEta = true, + CountSkippedForEta = false + }, + default); + + AnsiConsole.MarkupLine("[bold green]Initial startup sampling complete.[/]"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {initialSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {initialSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {initialSummary.Failed:N0}"); + + AnsiConsole.MarkupLine("[bold magenta]Pipeline progress:[/] startup sampling finished. Learned-baseline early pruning remains disabled for subsequent phases."); + + var isolationOptimizer = new IsolationOptimizationService(); + + AnsiConsole.Write(new Rule("[yellow]Initial Probe Analysis[/]") { Justification = Justify.Left }); + var initialAnalysis = await isolationOptimizer.AnalyzeInitialIsolationProbesAsync(initialPlan); + + AnsiConsole.Write(new Rule("[yellow]Initial Probe Group Decisions[/]") { Justification = Justify.Left }); + PrintIsolationGroupDecisions(initialAnalysis.GroupDetails); + + foreach (var note in initialAnalysis.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Initial Probe Analysis"); + + AnsiConsole.Write(new Rule("[yellow]Continuation Isolation Samples[/]") { Justification = Justify.Left }); + + AnsiConsole.MarkupLine($"[grey]Groups continuing after early probe:[/] [cyan]{initialAnalysis.GroupsToContinue.Count:N0}[/]"); + + var continuationPlan = isolationPlanner.BuildContinuationPlan( + initialAnalysis.GroupsToContinue, + Cache.UnusedTensorGroups); + + if (continuationPlan.TotalCount > 0) + { + AnsiConsole.MarkupLine($"[grey]Queued continuation samples:[/] [cyan]{continuationPlan.TotalCount:N0}[/]"); + + var continuationSummary = await quantizationService.ProcessHybridBatchAsync( + continuationPlan.Plans, + new StageProgressOptions + { + StageName = "Continuation isolation samples", + Total = continuationPlan.TotalCount, + MinimumNonSkippedSamplesBeforeEta = 2, + ShowEta = true, + CountSkippedForEta = false + }, + default); + + AnsiConsole.MarkupLine("[bold green]Continuation sampling complete.[/]"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {continuationSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {continuationSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {continuationSummary.Failed:N0}"); + } + else + { + AnsiConsole.MarkupLine("[grey]No continuation samples were required after smallest-first gating.[/]"); + } + + var mergedPlan = initialPlan.MergeWith(continuationPlan); + + var archivalGroupIds = TReg.All + .Where(x => !Cache.UnusedTensorGroups.Any(u => u.UniqueId == x.UniqueId)) + .Select(x => x.UniqueId) + .Except(initialAnalysis.GroupsToContinue) + .OrderBy(x => x) + .ToList(); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Final Isolation Optimization"); + + AnsiConsole.Write(new Rule("[yellow]Final Isolation Optimization[/]") { Justification = Justify.Left }); + var isolationResult = await isolationOptimizer.AnalyzeAndApplyFinalAsync(mergedPlan); + + SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Final Isolation Optimization"); + + foreach (var gd in isolationResult.GroupDetails.OrderBy(x => x.GroupName)) + { + AnsiConsole.Write( + new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") + { + Justification = Justify.Left + }); + + AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); + AnsiConsole.MarkupLine($"[green]Winning candidate:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); + AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); + AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); + + foreach (var line in gd.Candidates) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); + } + + var comboCountAfterRulePruning = ComboCounter.CountAll(); + + var dbService = new QuantDatabaseService(); + await dbService.InitializeAsync(forceRebuild: true); + + // The old MDA/predicted-size ceiling pass is intentionally removed. + // DuckDB now stays as the allowed candidate universe, and the rank-safe + // isolation predictor chooses which candidates deserve real validation. + long predictedSizePruned = 0; + long highPrecisionPruned = await dbService.PruneHighPrecisionHybridCandidatesAsync(); + + AnsiConsole.MarkupLine($"[green]Learned-baseline eliminations:[/] {totalLearnedPruningResult.GroupCandidateEliminations:N0} [grey](early pruning disabled)[/]"); + AnsiConsole.MarkupLine($"[green]Baselines skipped without learned rows:[/] {totalLearnedPruningResult.BaselinesSkippedWithoutLearnedRows:N0} [grey](early pruning disabled)[/]"); + AnsiConsole.MarkupLine($"[green]Groups reduced to explicit-banned->Q8-fallback:[/] {isolationResult.ExplicitQuantBannedGroups:N0}"); + AnsiConsole.MarkupLine($"[green]BF16-suppressed groups:[/] {isolationResult.Bf16SuppressedGroups:N0}"); + AnsiConsole.MarkupLine($"[green]Hard damage eliminations:[/] {isolationResult.HardDamageEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Dominance eliminations:[/] {isolationResult.DominatedGroupCandidatesBanned:N0}"); + AnsiConsole.MarkupLine($"[green]Bad trade eliminations:[/] {isolationResult.BadTradeEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Synergy second-chance reinstatements:[/] {isolationResult.SynergySecondChanceReinstatements:N0}"); + AnsiConsole.MarkupLine($"[green]Final KLD cleanup eliminations:[/] {isolationResult.FinalKldCleanupEliminations:N0}"); + AnsiConsole.MarkupLine($"[green]Disabled combination baselines:[/] {isolationResult.DisabledBaselines:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count before pruning:[/] {comboCountBefore:N0}"); + AnsiConsole.MarkupLine($"[green]Combination count after rule pruning:[/] {comboCountAfterRulePruning:N0}"); + AnsiConsole.MarkupLine($"[green]Predicted-size combo removals:[/] {predictedSizePruned:N0} [grey](obsolete MDA ceiling pruning removed)[/]"); + AnsiConsole.MarkupLine($"[green]Late-stage high-precision combo removals:[/] {highPrecisionPruned:N0}"); + + foreach (var note in isolationResult.Notes) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(note)}[/]"); + + long finalRemainingCombinationCount = await dbService.GetRemainingCombinationCountAsync(); + + AnsiConsole.MarkupLine($"[green]Final surviving combinations after stage-1 pruning:[/] {finalRemainingCombinationCount:N0}"); + + AnsiConsole.Write(new Rule("[yellow]Archival Isolation Coverage[/]") { Justification = Justify.Left }); + + var archivalCoveragePlan = isolationPlanner.BuildArchivalCoveragePlan( + groupIdsToArchive: archivalGroupIds, + existingPlanKeys: mergedPlan.Plans.Select(x => x.Key), + missingTensorGroups: Cache.UnusedTensorGroups); + + var archivalCoverageGroups = archivalCoveragePlan.Plans + .Where(x => x.TargetGroupId.HasValue) + .Select(x => x.TargetGroupId!.Value) + .Distinct() + .Count(); + + AnsiConsole.MarkupLine($"[grey]Groups queued for archival coverage:[/] [cyan]{archivalCoverageGroups:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Non-continuing groups targeted for archival fill:[/] [cyan]{archivalGroupIds.Count:N0}[/]"); + AnsiConsole.MarkupLine("[grey]This pass does not feed current-run pruning; it only fills missing isolated-sample coverage in the database for groups that were fixed/collapsed out of combo exploration.[/]"); + + if (archivalCoveragePlan.TotalCount > 0) + { + AnsiConsole.MarkupLine($"[grey]Queued archival isolation samples:[/] [cyan]{archivalCoveragePlan.TotalCount:N0}[/]"); + + var archivalCoverageSummary = await quantizationService.ProcessHybridBatchAsync( + archivalCoveragePlan.Plans, + new StageProgressOptions + { + StageName = "Archival isolation coverage samples", + Total = archivalCoveragePlan.TotalCount, + MinimumNonSkippedSamplesBeforeEta = 2, + ShowEta = true, + CountSkippedForEta = false + }, + default); + + AnsiConsole.MarkupLine("[bold green]Archival isolation coverage complete.[/]"); + AnsiConsole.MarkupLine($" [green]Completed:[/] {archivalCoverageSummary.Completed:N0}"); + AnsiConsole.MarkupLine($" [yellow]Skipped existing:[/] {archivalCoverageSummary.Skipped:N0}"); + AnsiConsole.MarkupLine($" [red]Failed:[/] {archivalCoverageSummary.Failed:N0}"); + } + else + { + AnsiConsole.MarkupLine("[grey]No archival isolation coverage samples were required.[/]"); + } + + var finalIsolationManifestPlan = mergedPlan.MergeWith(archivalCoveragePlan); + + var survivalPipeline = new CombinationSurvivalPipelineService(quantizationService); + var finalizationResult = await survivalPipeline.RunAsync( + isolationSamplePlan: finalIsolationManifestPlan, + isolationOptimizationResult: isolationResult, + ct: default); + + AnsiConsole.Write(new Rule("[yellow]Export Summary[/]") { Justification = Justify.Left }); + AnsiConsole.MarkupLine($"[green]Export directory:[/] [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); + AnsiConsole.MarkupLine($"[green]Final brutal survivors:[/] [cyan]{finalizationResult.BrutalSurvivors.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[green]Selected survivors:[/] [cyan]{finalizationResult.SelectedRows.Count(x => x.Enabled):N0}[/]"); + AnsiConsole.MarkupLine($"[green]Exported/linkable artifacts:[/] [cyan]{finalizationResult.ExportedArtifacts.Count:N0}[/]"); + } + + private static async Task EnsureNativeBenchmarkEnvironmentReadyAsync( + BenchmarkService benchmarkService, + QuantizationService quantizationService, + HybridQuant baseModelQuant, + string bf16ModelGgufPath, + string baseBenchDir, + string baseLogitsDir, + string pplCorporaDir, + bool nativeTruthAlreadyLearned) + { + var status = ValidateNativeBenchmarkEnvironment( + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir, + pplCorporaDir: pplCorporaDir, + requiredDomains: RequiredNativeKldDomains); + + bool mustRegenerateNativeBenchmarkArtifacts = + !status.IsValid; + + if (mustRegenerateNativeBenchmarkArtifacts) + { + AnsiConsole.Write(new Rule("[yellow]Native BF16 Benchmark/KLD Artifact Validation[/]") { Justification = Justify.Left }); + + AnsiConsole.MarkupLine("[yellow]Native BF16 benchmark/KLD artifacts are missing or incomplete.[/] Regenerating required artifacts."); + + PrintNativeBenchmarkEnvironmentIssues(status); + + await ForceRegenerateNativeBenchmarkArtifactsAsync( + benchmarkService: benchmarkService, + baseModelQuant: baseModelQuant, + bf16ModelGgufPath: bf16ModelGgufPath, + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir); + + status = ValidateNativeBenchmarkEnvironment( + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir, + pplCorporaDir: pplCorporaDir, + requiredDomains: RequiredNativeKldDomains); + + if (!status.IsValid) + { + var details = string.Join( + Environment.NewLine, + status.MissingOrInvalidArtifacts.Select(x => $"- {x}")); + + throw new InvalidOperationException( + "Native BF16 benchmark/logit generation completed, but required native benchmark artifacts are still missing or invalid. " + + "This is fatal because every non-base benchmark requires complete native KLD logits." + + Environment.NewLine + + details); + } + + AnsiConsole.MarkupLine("[green]Native BF16 benchmark/KLD artifacts validated.[/]"); + } + else + { + AnsiConsole.MarkupLine("[grey]Native BF16 benchmark/KLD artifacts already exist and passed validation.[/]"); + } + + // Disk artifact validation is not enough. Native tensor learning is tied to the + // persisted TensorCombo/AiBenchmark identity. The repair path above may run in + // transient mode so it can regenerate logits even when stale DB truth exists; after + // the artifacts are valid, explicitly hydrate/validate the SQLite benchmark row + // from those artifacts before native-source learning tries to attach to it. + await EnsureNativeBenchmarkDbTruthAsync( + benchmarkService: benchmarkService, + baseModelQuant: baseModelQuant, + bf16ModelGgufPath: bf16ModelGgufPath, + baseBenchDir: baseBenchDir, + baseLogitsDir: baseLogitsDir); + + if (!nativeTruthAlreadyLearned) + { + await quantizationService.LearnNativeSourceTruthAsync(bf16ModelGgufPath); + } + else + { + AnsiConsole.MarkupLine( + "[grey]Skipping native-source tensor relearn because learned native-source truth already exists.[/]"); + } + } + + private static async Task EnsureNativeBenchmarkDbTruthAsync( + BenchmarkService benchmarkService, + HybridQuant baseModelQuant, + string bf16ModelGgufPath, + string baseBenchDir, + string baseLogitsDir) + { + bool previousSuppressBenchmarkPersistence = Cache.SuppressBenchmarkPersistence; + + try + { + Cache.SuppressBenchmarkPersistence = false; + + await benchmarkService.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, + modelPath: bf16ModelGgufPath, + benchDir: baseBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: true, + domainsOverride: RequiredNativeKldDomains); + + AnsiConsole.MarkupLine("[grey]Native BF16 benchmark DB truth hydrated/validated.[/]"); + } + finally + { + Cache.SuppressBenchmarkPersistence = previousSuppressBenchmarkPersistence; + } + } + + private static async Task ForceRegenerateNativeBenchmarkArtifactsAsync( + BenchmarkService benchmarkService, + HybridQuant baseModelQuant, + string bf16ModelGgufPath, + string baseBenchDir, + string baseLogitsDir) + { + if (Directory.Exists(baseBenchDir)) + { + AnsiConsole.MarkupLine( + $"[grey]Clearing incomplete/stale native benchmark directory:[/] {Markup.Escape(baseBenchDir)}"); + + Directory.Delete(baseBenchDir, recursive: true); + } + + Directory.CreateDirectory(baseBenchDir); + Directory.CreateDirectory(baseLogitsDir); + + bool previousSuppressBenchmarkPersistence = Cache.SuppressBenchmarkPersistence; + + try + { + // This is intentional. + // + // If persisted native BF16 benchmark rows already exist in SQLite, the normal + // BenchmarkService path may return DB truth without actually running llama-perplexity, + // which means missing KLD logits would stay missing forever. + // + // Transient mode forces this artifact-repair pass to rely on disk execution instead + // of DB benchmark truth. The native tensor truth is learned separately below. + Cache.SuppressBenchmarkPersistence = true; + + await benchmarkService.RunAllBenchmarksAsync( + quantConfig: baseModelQuant, + modelPath: bf16ModelGgufPath, + benchDir: baseBenchDir, + klLogitsDir: baseLogitsDir, + saveLogits: true, + domainsOverride: RequiredNativeKldDomains); + } + finally + { + Cache.SuppressBenchmarkPersistence = previousSuppressBenchmarkPersistence; + } + } + + private static NativeBenchmarkEnvironmentStatus ValidateNativeBenchmarkEnvironment( + string baseBenchDir, + string baseLogitsDir, + string pplCorporaDir, + IReadOnlyCollection requiredDomains) + { + var issues = new List(); + + if (string.IsNullOrWhiteSpace(baseBenchDir)) + { + issues.Add("Native benchmark directory path is null/empty."); + } + else if (!Directory.Exists(baseBenchDir)) + { + issues.Add($"Native benchmark directory does not exist: {baseBenchDir}"); + } + + if (string.IsNullOrWhiteSpace(baseLogitsDir)) + { + issues.Add("Native KLD logits directory path is null/empty."); + } + else if (!Directory.Exists(baseLogitsDir)) + { + issues.Add($"Native KLD logits directory does not exist: {baseLogitsDir}"); + } + + if (string.IsNullOrWhiteSpace(pplCorporaDir)) + { + issues.Add("_ppl_corpora directory path is null/empty."); + } + else if (!Directory.Exists(pplCorporaDir)) + { + issues.Add($"_ppl_corpora directory does not exist: {pplCorporaDir}"); + } + else if (!Directory.EnumerateFiles(pplCorporaDir, "*", SearchOption.AllDirectories).Any()) + { + issues.Add($"_ppl_corpora directory exists but contains no files: {pplCorporaDir}"); + } + + foreach (var domain in requiredDomains.OrderBy(x => x, StringComparer.Ordinal)) + { + if (!string.IsNullOrWhiteSpace(baseBenchDir) && Directory.Exists(baseBenchDir)) + { + var pplLog = Path.Combine(baseBenchDir, $"perplexity_{domain}.log"); + + if (!File.Exists(pplLog)) + { + issues.Add($"Missing native BF16 perplexity log for domain '{domain}': {pplLog}"); + } + else if (new FileInfo(pplLog).Length <= 0) + { + issues.Add($"Native BF16 perplexity log is empty for domain '{domain}': {pplLog}"); + } + } + + if (!string.IsNullOrWhiteSpace(baseLogitsDir) && Directory.Exists(baseLogitsDir)) + { + var logitsFile = Path.Combine(baseLogitsDir, $"kld_logits_{domain}.bin"); + + if (!File.Exists(logitsFile)) + { + issues.Add($"Missing native KLD logits for domain '{domain}': {logitsFile}"); + } + else if (new FileInfo(logitsFile).Length <= 0) + { + issues.Add($"Native KLD logits file is empty for domain '{domain}': {logitsFile}"); + } + } + } + + return new NativeBenchmarkEnvironmentStatus( + IsValid: issues.Count == 0, + MissingOrInvalidArtifacts: issues); + } + + private static void PrintNativeBenchmarkEnvironmentIssues(NativeBenchmarkEnvironmentStatus status) + { + if (status.IsValid) + return; + + foreach (var issue in status.MissingOrInvalidArtifacts.Take(20)) + AnsiConsole.MarkupLine($"[grey]- {Markup.Escape(issue)}[/]"); + + if (status.MissingOrInvalidArtifacts.Count > 20) + { + AnsiConsole.MarkupLine( + $"[grey]- ...and {status.MissingOrInvalidArtifacts.Count - 20:N0} more issue(s).[/]"); + } + } + + private sealed record NativeBenchmarkEnvironmentStatus( + bool IsValid, + IReadOnlyList MissingOrInvalidArtifacts); + + private static void PrintIsolationGroupDecisions(IEnumerable decisions) + { + foreach (var gd in decisions.OrderBy(x => x.GroupName)) + { + AnsiConsole.Write( + new Rule($"[yellow]Isolation Group: {Markup.Escape(gd.GroupName)}[/]") + { + Justification = Justify.Left + }); + + AnsiConsole.MarkupLine($"[green]Best savings:[/] {gd.BestReductionRatio:P2}"); + AnsiConsole.MarkupLine($"[green]Winning candidate:[/] {Markup.Escape(gd.WinningCandidate ?? "n/a")}"); + AnsiConsole.MarkupLine($"[green]Explicit quant banned:[/] {(gd.ExplicitQuantBanned ? "[red]yes[/]" : "[green]no[/]")}"); + AnsiConsole.MarkupLine($"[green]BF16 suppressed:[/] {(gd.Bf16Suppressed ? "[yellow]yes[/]" : "[green]no[/]")}"); + + foreach (var line in gd.Candidates) + AnsiConsole.MarkupLine($" [grey]- {Markup.Escape(line)}[/]"); + } + } + + private static void PrintCustomBaselineRuntimeSummary( + IReadOnlyCollection resolvedCustomBaselines, + bool hasUsableImatrix) + { + AnsiConsole.Write(new Rule("[yellow]Custom Baseline Runtime Summary[/]") { Justification = Justify.Left }); + + var learning = BaselineQuants.GetLearningBaselines(hasUsableImatrix); + var carriers = BaselineQuants.GetCombinationCarrierBaselines(hasUsableImatrix); + var explicitCandidates = BaselineQuants.GetGroupCombinationCandidates(hasUsableImatrix, Config.Current.Flags.AllowHighPrecisionHybrids); + + AnsiConsole.MarkupLine($"[grey]Learning baselines in runtime registry:[/] [cyan]{learning.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Combination carriers in runtime registry:[/] [cyan]{carriers.Count:N0}[/]"); + AnsiConsole.MarkupLine($"[grey]Explicit group candidates in runtime registry:[/] [cyan]{explicitCandidates.Count:N0}[/]"); + + if (resolvedCustomBaselines.Count == 0) + { + AnsiConsole.MarkupLine("[grey]No custom baselines were resolved for this run.[/]"); + return; + } + + AnsiConsole.MarkupLine($"[green]Custom baselines registered:[/] [cyan]{resolvedCustomBaselines.Count:N0}[/]"); + + foreach (var custom in resolvedCustomBaselines.OrderBy(x => x.DynamicBaselineId)) + { + bool inLearning = learning.Any(x => x.UniqueId == custom.DynamicBaselineId); + bool inCarriers = carriers.Any(x => x.UniqueId == custom.DynamicBaselineId); + bool inExplicit = explicitCandidates.Any(x => x.UniqueId == custom.DynamicBaselineId); + + string revision = string.IsNullOrWhiteSpace(custom.Revision) ? "main" : custom.Revision; + AnsiConsole.MarkupLine( + $" [cyan]{custom.DynamicBaselineId}[/] [yellow]{Markup.Escape(custom.DisplayName)}[/] family={Markup.Escape(custom.BaselineFamily)} file={Markup.Escape(custom.SourceFileName)} revision={Markup.Escape(revision)} learning={inLearning} carrier={inCarriers} explicit={inExplicit}"); + } + } + + private void ShowPipelineHelp() + { + AnsiConsole.MarkupLine("[bold yellow]Command: pipeline (legacy alias: evolution)[/]"); + AnsiConsole.WriteLine("Runs the full quantization search on a target model, then uses rank-safe isolation prediction to choose validated final hybrids."); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold]Usage:[/]"); + AnsiConsole.WriteLine(" mq pipeline --model-dir \"\" [options]"); + AnsiConsole.WriteLine(" mq pipeline --config \"./config.default.yaml\""); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold]Arguments:[/]"); + AnsiConsole.MarkupLine(" [green]--model-dir[/] Path to the model directory containing .safetensors files (Optional if set in YAML)"); + AnsiConsole.MarkupLine(" [green]--magic-quant-root[/] Isolated runtime root containing MagicQuant_SQLite.db and shared runtime assets (Optional)"); + AnsiConsole.MarkupLine(" [green]--recheck-hardware-probe[/] Force hardware/Q8 probe and update cached plan in SQLite (Optional)"); + AnsiConsole.MarkupLine(" [green]--use-imatrix[/] Enable imatrix acquisition/build and allow imatrix-required search candidates (Optional)"); + AnsiConsole.MarkupLine(" [green]--allow-high-precision-hybrids[/] Keep BF16/F16 explicit group candidates in final surviving combos (Optional, default false)"); + AnsiConsole.MarkupLine(" [green]--imatrix-force-rebuild[/] Delete/rebuild canonical imatrix artifacts before run (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-url[/] HTTPS URL for direct imatrix artifact download (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-dataset-repo[/] Hugging Face dataset repo ID for imatrix generation (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-dataset-split[/] Dataset split for HF/local dataset source metadata/build (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-dataset-config[/] Optional dataset config name for HF datasets (Optional)"); + AnsiConsole.MarkupLine(" [green]--imatrix-dataset-local-file[/] Full path to local .json/.jsonl dataset source (Optional)"); + AnsiConsole.MarkupLine(" [green]--selection-near-baseline-max-size-growth-percent[/] Phase-2 size premium for replacing a smaller/higher-damage anchor (Optional; default = 1.0)"); + AnsiConsole.MarkupLine(" [green]--selection-interior-window-fractions[/] Comma-separated phase-3 interior windows, e.g. 0.35,0.35 (Optional)"); + AnsiConsole.MarkupLine(" [green]--prediction-bit-stress-threshold-candidates[/] Comma-separated interaction-fit thresholds, e.g. 4,5,6,7,8,9,10,11,12 (Optional)"); + AnsiConsole.MarkupLine(" [green]--output-dir[/] Final export/output directory for selected survivor artifacts (Optional; default = /MagicQuant/Final_Outputs)"); + AnsiConsole.MarkupLine(" [green]--output-name-prefix[/] Output filename prefix for exported GGUF files (Optional; default = Model)"); + AnsiConsole.MarkupLine(" [green]--reuse-existing-final-artifacts[/] Reuse valid final GGUFs only when exact file name + benchmark byte size match (Optional; default false)"); + AnsiConsole.MarkupLine(" [green]--allow-eight-bit-anchor-replacements[/] Permit final prediction to try replacing 8-bit anchors like Q8_0 (see YAML policy)"); + AnsiConsole.MarkupLine(" [green]--export-external-learned-baselines[/] Also locally rebuild/export pure learned external baselines such as Unsloth (Optional; default false)"); + AnsiConsole.MarkupLine(" [green]--rebucket-learned-tensor-groups[/] Compatibility alias; regex rebucketing from DB is enabled by default"); + AnsiConsole.MarkupLine(" [green]--no-rebucket-learned-tensor-groups[/] Disable safe DB rebucketing and force the slower/full learned-group path instead"); + AnsiConsole.MarkupLine(" [green]--skip-tensor-group-confirm[/] Skip the native BF16 tensor-group review confirmation prompt for unattended runs (Optional; YAML default true asks)"); + AnsiConsole.MarkupLine(" [green]--selection-max-candidates-per-interior-window[/] Candidate count retained per interior window (Optional; default = 1)"); + AnsiConsole.MarkupLine(" [green]--config[/] Path to YAML runtime config. CLI flags override YAML values."); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold]Example:[/]"); + AnsiConsole.WriteLine(" mq pipeline --model-dir \"C:\\Models\\Mistral-7B\""); + } + + private static string ResolveAndValidateOutputDirectory() + { + string resolved = OutputPathService.Pipeline( + Cache.ModelMagicQuantDirectory!, Config.Current.Output.OutputDir); + + Directory.CreateDirectory(resolved); + + string probe = Path.Combine(resolved, $".write_test_{Guid.NewGuid():N}.tmp"); + File.WriteAllText(probe, "ok"); + File.Delete(probe); + + return resolved; + } + + private static async Task EnsureSqliteReadyAsync(CancellationToken ct = default) + { + await using var db = new MagicQuantContext(); + await db.Database.MigrateAsync(ct); + + var model = await db.AiModelHashes + .FirstOrDefaultAsync(x => x.UniqueHash == Cache.CurrentModelId, ct); + + if (model != null) + return; + + db.AiModelHashes.Add(new AiModelHash { UniqueHash = Cache.CurrentModelId }); + await db.SaveChangesAsync(ct); + } +} diff --git a/MagicQuant/Commands/ValidatePredictions.cs b/MagicQuant/Commands/ValidatePredictions.cs index b272bb2..500a332 100644 --- a/MagicQuant/Commands/ValidatePredictions.cs +++ b/MagicQuant/Commands/ValidatePredictions.cs @@ -122,13 +122,8 @@ private static void ApplyOptionalImatrixContext(IReadOnlyList args) private static string ResolveOutputDirectory(IReadOnlyList args) { string? explicitOutput = args.FirstOrDefault(a => string.Equals(a.Name, "output-dir", StringComparison.OrdinalIgnoreCase))?.Value; - if (!string.IsNullOrWhiteSpace(explicitOutput)) - return Path.GetFullPath(explicitOutput); - - if (!string.IsNullOrWhiteSpace(Config.OutputDirectory)) - return Path.Combine(Path.GetFullPath(Config.OutputDirectory!), "PredictionValidation"); - - return Path.Combine(Cache.ModelMagicQuantDirectory!, "PredictionValidation"); + return OutputPathService.PredictionValidation( + Cache.ModelMagicQuantDirectory!, explicitOutput, Config.OutputDirectory); } private static void ShowHelp() diff --git a/MagicQuant/Config.cs b/MagicQuant/Config.cs index 6f45806..b815286 100644 --- a/MagicQuant/Config.cs +++ b/MagicQuant/Config.cs @@ -2,6 +2,10 @@ namespace MagicQuant; +/// +/// Process-wide normalized settings for one CLI run. Load through MagicQuantYamlLoader; +/// tests changing this state must restore the previous configuration. +/// public static class Config { public static MagicQuantYamlConfig Current { get; private set; } = MagicQuantYamlConfig.CreateDefault(); @@ -22,12 +26,6 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.Evolution.MaxDataCollectedPerCategory; - public static int MaxSurvivalRounds => Current.Evolution.MaxSurvivalRounds; - public static double CollapseMultiplier => Current.Evolution.CollapseMultiplier; - public static int BruteForceFinalCombinationThreshold => Current.Evolution.BruteForceFinalCombinationThreshold; public static ulong ManualMaxPredictedSizeBytes => Current.Prediction.ManualMaxPredictedSizeBytes; public static IReadOnlyList PredictionBitStressThresholdCandidates => @@ -110,17 +108,4 @@ public static void SetResolvedCustomBaselines(IEnumerable Current.Output.RequireMmprojForVisionModels; public static bool ReuseExistingFinalArtifacts => Current.Output.ReuseExistingFinalArtifacts; - public static int MaxSelectedChoicesPerBucket => Math.Max(1, Current.Survival.MaxSelectedChoicesPerBucket); - public static double SurvivalMeaningfulSizeBiasPercent => Current.Survival.MeaningfulSizeBiasPercent; - public static double SurvivalKldCloseCallAbsoluteEpsilon => Current.Survival.KldCloseCallAbsoluteEpsilon; - public static double SurvivalKldCloseCallRelativeFraction => Current.Survival.KldCloseCallRelativeFraction; - public static double SurvivalPplLargeDifferencePercent => Current.Survival.PplLargeDifferencePercent; - public static double SurvivalTradeScoreSizeBiasWeight => Current.Survival.TradeScoreSizeBiasWeight; - public static double SurvivalTradeScorePplWeight => Current.Survival.TradeScorePplWeight; - - public static List SensitivityProbeGroups => Current.SensitivityProbeGroups; - public static List SensitivityProbeGroupsMoe => Current.SensitivityProbeGroupsMoe; - public static List BrainLayers => Current.BrainLayers; - public static List CollapsePenaltySchemes => Current.CollapsePenaltySchemes; - public static List MoeIndicatorTensors => Current.MoeIndicatorTensors; -} \ No newline at end of file +} diff --git a/MagicQuant/Configs/config.dev.yaml b/MagicQuant/Configs/config.dev.yaml deleted file mode 100644 index b2147b4..0000000 --- a/MagicQuant/Configs/config.dev.yaml +++ /dev/null @@ -1,413 +0,0 @@ -paths: - magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ - llama_root: - llama_bin: - convert_script: - scratch_roots: - - /mnt/world8/ - - /home/slurp/ - - /mnt/world7/ - external_baseline_cache_dir_name: ExternalBaselines - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -learning: - # Destructive relearn options are intentionally targeted. - # These are transient runtime commands and are not persisted as DB state. - # When any option below is enabled, MagicQuant prints a count summary and asks - # for confirmation before deleting/relearning anything. - # - # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, - # and execution probe cache rows scoped to the active architecture family. - # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, - # TensorCombo, or BaselineQuantDefinition rows. - force_relearn_architecture_family: false - - # Relearn built-in/standard baselines by display/canonical name for the current - # architecture family and active tensor group profile. - # Example: - # force_relearn_standard_baselines: - # - Q6_K - # - IQ4_XS - force_relearn_standard_baselines: [] - - # Safety gate for tensor group regex/profile changes. After MagicQuant reads the - # native BF16 GGUF tensor list, it prints group counts, example tensors, - # ambiguous matches, unresolved tensors, and base-quant exception counts, then - # asks before continuing. Keep this true unless running fully unattended. - confirm_tensor_group_profile: true - - # Safe/idempotent repair mode for accidental regex mistakes. - # - # Default true: on every run MagicQuant checks whether older DB learned tensor - # truth can be copied into the active TensorGroupProfile by reapplying the - # current regex/base_quant_exceptions rules. If nothing changed or current rows - # already exist, it skips cleanly and does not create duplicates. - # - # This avoids needless re-download/re-quantization of pure learning baselines - # after regex-only regrouping. Old benchmarks/learned rows remain attached to - # their original TensorGroupProfile and are ignored unless that profile becomes - # active again. - # - # Disable only when you intentionally want the slower/full path to regenerate - # learned grouping truth instead of rebucketing from DB snapshots. - # CLI disable aliases: - # --no-rebucket-learned-tensor-groups - # --disable-tensor-group-rebucket - # --full-relearn-tensor-groups - rebucket_learned_tensor_groups_from_existing_truth: true - - -readme: - # Optional title model name override used in: - # # MagicQuant Hybrids (v2.0) - - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.6-27B - - # Hugging Face README frontmatter. - # Scalars render as: - # license: apache-2.0 - # Arrays render as: - # tags: - # - gguf - # - text-generation - # - # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. - frontmatter: - license: apache-2.0 - tags: - - gguf - - text-generation - - magicquant - - conversational - base_model: - - Qwen/Qwen3.6-27B - -hardware: - gpu_memory_limits_gb: - 0: 19 - 1: 23 - -imatrix: - imatrix_url: - dataset_repo: - dataset_split: text - dataset_config: - dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1_5m.jsonl - -# Legacy evolution survivor knobs were removed from YAML. -# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. - -isolation_pruning: - # Preserve complete isolation truth for prediction and contextual probing. - minimum_isolation_reduction_to_continue_ratio: 0.00 - minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 - maximum_isolation_ppl_delta_percent: 5.0 - maximum_isolation_kld: 0.1 - bad_trade_max_size_delta_percent: 4.0 - bad_trade_kld_multiplier: 2.5 - bad_trade_ppl_multiplier: 3.5 - floating_point_epsilon: 1.0e-8 - minimum_meaningful_base_only_reduction_ratio: 0.01 - - -prediction: - # Rank-safe isolation KLD predictor. - # - # manual_max_predicted_size_bytes is retained only as an emergency compatibility - # field for older helper code. Leave it at 0 for the new chooser. - manual_max_predicted_size_bytes: 0 - - # Candidate bit-stress thresholds for the low-bit interaction correction. - # The predictor fits each candidate threshold against existing category=General - # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. - bit_stress_threshold_candidates: - - 4.0 - - 5.0 - - 6.0 - - 7.0 - - 8.0 - - 9.0 - - 10.0 - - 11.0 - - 12.0 - - # Fallback threshold when too few benchmark rows exist to fit the interaction model. - default_bit_stress_threshold: 8.0 - - # Minimum benchmark rows required before fitting the interaction correction. - minimum_fit_rows: 12 - -candidate_selection: - - validate_all_anomaly_strict_candidates_after_success: false - - # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits - # inside this size premium and beats the real linear KLD improvement line. - near_baseline_max_size_growth_percent: 1.0 - - # Phase 3: interior windows between adjacent final anchors. - # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. - interior_window_fractions: - - 0.35 - - 0.35 - - # Number of predicted winners to keep per interior window. - max_candidates_per_interior_window: 1 - - # If the first predicted candidate fails real validation, try this many fallbacks. - max_fallback_attempts_per_anchor: 5 - - # Conservative SQLite/isolation-truth fallback. This runs only after the - # normal DuckDB prediction-guided attempts fail for a strict/premium/interior - # phase window. It starts from the anchor baseline blanket and only swaps - # tensor groups using surviving isolated group candidates, plus the baseline - # itself as the blanket state. - smart_fallback_enabled: true - smart_fallback_attempts_per_failure: 3 - smart_fallback_max_higher_fidelity_steps: 2 - - # Strict epsilon for lower-KLD comparisons after real benchmark validation. - minimum_kld_improvement_epsilon: 1.0e-9 - - # Final spacing pass: candidates closer than this fraction of the global survivor - # size span are collapsed unless one genuinely earns the slot. - minimum_neighbor_gap_fraction_of_global_span: 0.03 - - # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller - # anchor must provide a stronger KLD gain to justify its existence. - near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 - near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 - - # Default false: do not spend final prediction/build attempts trying to replace - # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. - # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. - allow_eight_bit_anchor_replacements: true - -anomaly_detection: - enabled: true - - # One anomaly refinement pass after smoke/probe/rule generation. - max_anomaly_refinement_rounds: 1 - - # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. - min_actual_gain_vs_twin_kld: 0.00025 - - # Minimum predicted size savings versus higher-bit twin/reference to probe. - min_predicted_size_savings_vs_twin_percent: 1.0 - - # Max changed groups in a candidate that can seed contextual probes. - max_probe_group_count: 4 - - # Max probes generated per anomaly seed. - max_probes_per_seed: 16 - - # Max anomaly probes in one run. - max_total_probes_per_run: 32 - - # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. - max_prediction_space_gap_vs_twin_kld: 0.00050 - - # Optional relative cap for prediction-space gap normalized by local anchor gap. - max_relative_prediction_penalty_vs_twin: 0.35 - - # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. - prediction_space_violation_margin: 0.00005 - - # Shrink applied to prediction-space adjustment after a rule is confirmed. - anomaly_adjustment_shrink_factor: 1.00 - - # Minimum confidence required before applying a confirmed anomaly rule. - min_rule_confidence_to_apply: 0.50 - - # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. - max_negative_adjustment_kld: 0.00400 - - # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. - max_positive_adjustment_kld: 0.00075 - - # Fractional cap relative to BaseRankSafeKld. - max_adjustment_fraction_of_base_kld: 0.75 - - # Number of top smoke candidates to consider per reference quant zone. - max_smoke_candidates_per_reference_zone: 12 - - # Store suppression-only results so false smoke is not repeatedly probed. - persist_suppression_results: true - - # Emit detailed anomaly logs. - verbose_anomaly_logging: true - - # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. - confirmed_anomaly_expansion: - enabled: true - max_neighbors_per_confirmed_rule: 6 - max_total_expansion_probes: 12 - allowed_reference_quants: - - Q8_0 - allowed_candidate_quants: - - Q6_K - - UD-Q6_K_XL - - Q5_K - - UD-Q5_K_XL - -output: - # Leave blank to default to /MagicQuant/Final_Outputs - output_dir: - output_name_prefix: Qwen3.6-27B - export_external_learned_baselines: true - - # false = normal behavior; delete/rebuild final outputs from scratch. - # true = preserve valid existing GGUFs and skip rebuilding them only when - # exact file name + byte size match benchmark truth. - # CLI --reuse-existing-final-artifacts overrides YAML. - reuse_existing_final_artifacts: false - -# Legacy bit-range bucket survival settings were removed. -# See candidate_selection above for the active final chooser settings. - -identity: - architecture_family_name: Qwen3.6-27B - allow_architecture_family_alias_override: false - -baselines: - standard_baselines_mode: all - enabled_standard_learning_baselines: [] - enabled_standard_combination_carriers: [] - enabled_standard_explicit_group_candidates: [] - - custom_repositories: - - repo_id: unsloth/Qwen3.6-27B-GGUF - enabled: true - short_source_name: Unsloth - source_kind: huggingface_gguf_repository - require_all_includes_to_resolve: true - validate_tensor_names_against_source_model: true - delete_partial_or_dirty_downloads: true - resume_or_retry_downloads: true - - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: false - - includes: - - - file_name: Qwen3.6-27B-UD-IQ2_M.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: UD-IQ2_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf - baseline_family: IQ2_XXS - quantize_base_name: IQ2_XXS - display_name: UD-IQ2_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf - baseline_family: IQ3_XXS - quantize_base_name: IQ3_XXS - display_name: UD-IQ3_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: UD-Q2_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_M - display_name: UD-Q3_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf - baseline_family: Q4_K_M - quantize_base_name: Q4_K_M - display_name: UD-Q4_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf - baseline_family: Q5_K - quantize_base_name: Q5_K - display_name: UD-Q5_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: UD-Q6_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - -# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. -# anomaly_detection remains the low-level compatibility section; synergy_detection controls -# template transfer, composition probes, contamination suppression, and wing diagnostics. -synergy_detection: - enabled: true - max_refinement_rounds: 1 - exact_context_confidence_multiplier: 1.00 - same_selected_groups_confidence_multiplier: 0.55 - equivalent_quant_family_confidence_multiplier: 0.30 - group_family_suspicion_confidence_multiplier: 0.15 - min_confidence_to_apply_adjustment: 0.35 - min_confidence_to_schedule_transfer_probe: 0.25 - max_negative_adjustment_kld: 0.002 - max_negative_adjustment_fraction_of_base_kld: 0.75 - transfer_probe_enabled: true - max_transfer_probes_per_template: 6 - max_total_transfer_probes_per_run: 24 - transfer_probe_context_strata: - high_fidelity_reference_quants: [Q6_K, Q5_K] - mid_fidelity_reference_quants: [Q4_K_M] - low_fidelity_reference_quants: [IQ3_S] - low_fidelity_enabled: false - exploratory_context_pair_enabled: true - max_exploratory_context_pairs_per_run: 14 - exploratory_pair_bit_ranges: [4] - exploratory_pair_context_strata: [mid-fidelity, low-fidelity] - context_scoped_rule_application_enabled: true - max_non_rule_group_context_mismatches: 1 - verbose_synergy_logging: true - min_smoke_score: 0.55 - max_smoke_gap_kld: 0.004 - top_rejected_smoke_preview: 25 - composition_probe_enabled: true - max_template_composition_group_count: 4 - max_composition_probes_per_run: 8 - max_templates_to_compose: 4 - min_template_confidence_for_composition: 0.50 - min_combined_expected_size_savings_percent: 1.0 - contaminating_passenger_detection_enabled: true - min_failure_margin_for_contamination_kld: 0.00050 - contamination_penalty_confidence_multiplier: 0.45 - suppress_repeated_contaminated_attempts: true diff --git a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml b/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml deleted file mode 100644 index 59bafa4..0000000 --- a/MagicQuant/Configs/config.qwen3-4B-2507-Instruct.dev.yaml +++ /dev/null @@ -1,401 +0,0 @@ -paths: - magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3-4B-Instruct-2507-unsloth/ - llama_root: - llama_bin: - convert_script: - scratch_roots: - - /mnt/world8/ - - /home/slurp/ - - /mnt/world7/ - external_baseline_cache_dir_name: ExternalBaselines - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -learning: - # Destructive relearn options are intentionally targeted. - # These are transient runtime commands and are not persisted as DB state. - # When any option below is enabled, MagicQuant prints a count summary and asks - # for confirmation before deleting/relearning anything. - # - # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, - # and execution probe cache rows scoped to the active architecture family. - # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, - # TensorCombo, or BaselineQuantDefinition rows. - force_relearn_architecture_family: false - - # Relearn built-in/standard baselines by display/canonical name for the current - # architecture family and active tensor group profile. - # Example: - # force_relearn_standard_baselines: - # - Q6_K - # - IQ4_XS - force_relearn_standard_baselines: [] - - # Safety gate for tensor group regex/profile changes. After MagicQuant reads the - # native BF16 GGUF tensor list, it prints group counts, example tensors, - # ambiguous matches, unresolved tensors, and base-quant exception counts, then - # asks before continuing. Keep this true unless running fully unattended. - confirm_tensor_group_profile: true - - # Safe/idempotent repair mode for accidental regex mistakes. - # - # Default true: on every run MagicQuant checks whether older DB learned tensor - # truth can be copied into the active TensorGroupProfile by reapplying the - # current regex/base_quant_exceptions rules. If nothing changed or current rows - # already exist, it skips cleanly and does not create duplicates. - # - # This avoids needless re-download/re-quantization of pure learning baselines - # after regex-only regrouping. Old benchmarks/learned rows remain attached to - # their original TensorGroupProfile and are ignored unless that profile becomes - # active again. - # - # Disable only when you intentionally want the slower/full path to regenerate - # learned grouping truth instead of rebucketing from DB snapshots. - # CLI disable aliases: - # --no-rebucket-learned-tensor-groups - # --disable-tensor-group-rebucket - # --full-relearn-tensor-groups - rebucket_learned_tensor_groups_from_existing_truth: true - - -readme: - # Optional title model name override used in: - # # MagicQuant Hybrids (v2.0) - - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.6-27B - - # Hugging Face README frontmatter. - # Scalars render as: - # license: apache-2.0 - # Arrays render as: - # tags: - # - gguf - # - text-generation - # - # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. - frontmatter: - license: apache-2.0 - tags: - - gguf - - text-generation - - magicquant - - conversational - base_model: - - Qwen/Qwen3.6-27B - -hardware: - gpu_memory_limits_gb: - 0: 19 - 1: 23 - -imatrix: - imatrix_url: - dataset_repo: - dataset_split: text - dataset_config: - dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1_5m.jsonl - -# Legacy evolution survivor knobs were removed from YAML. -# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. - -isolation_pruning: - # Preserve complete isolation truth for prediction and contextual probing. - minimum_isolation_reduction_to_continue_ratio: 0.00 - minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 - maximum_isolation_ppl_delta_percent: 5.0 - maximum_isolation_kld: 0.1 - bad_trade_max_size_delta_percent: 4.0 - bad_trade_kld_multiplier: 2.5 - bad_trade_ppl_multiplier: 3.5 - floating_point_epsilon: 1.0e-8 - minimum_meaningful_base_only_reduction_ratio: 0.01 - - -prediction: - # Rank-safe isolation KLD predictor. - # - # manual_max_predicted_size_bytes is retained only as an emergency compatibility - # field for older helper code. Leave it at 0 for the new chooser. - manual_max_predicted_size_bytes: 0 - - # Candidate bit-stress thresholds for the low-bit interaction correction. - # The predictor fits each candidate threshold against existing category=General - # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. - bit_stress_threshold_candidates: - - 4.0 - - 5.0 - - 6.0 - - 7.0 - - 8.0 - - 9.0 - - 10.0 - - 11.0 - - 12.0 - - # Fallback threshold when too few benchmark rows exist to fit the interaction model. - default_bit_stress_threshold: 8.0 - - # Minimum benchmark rows required before fitting the interaction correction. - minimum_fit_rows: 12 - -candidate_selection: - # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits - # inside this size premium and beats the real linear KLD improvement line. - near_baseline_max_size_growth_percent: 1.0 - - # Phase 3: interior windows between adjacent final anchors. - # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. - interior_window_fractions: - - 0.35 - - 0.35 - - # Number of predicted winners to keep per interior window. - max_candidates_per_interior_window: 1 - - # If the first predicted candidate fails real validation, try this many fallbacks. - max_fallback_attempts_per_anchor: 5 - - # Strict epsilon for lower-KLD comparisons after real benchmark validation. - minimum_kld_improvement_epsilon: 1.0e-9 - - # Final spacing pass: candidates closer than this fraction of the global survivor - # size span are collapsed unless one genuinely earns the slot. - minimum_neighbor_gap_fraction_of_global_span: 0.03 - - # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller - # anchor must provide a stronger KLD gain to justify its existence. - near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 - near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 - - # Default false: do not spend final prediction/build attempts trying to replace - # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. - # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. - allow_eight_bit_anchor_replacements: true - -anomaly_detection: - enabled: true - - # One anomaly refinement pass after smoke/probe/rule generation. - max_anomaly_refinement_rounds: 1 - - # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. - min_actual_gain_vs_twin_kld: 0.00025 - - # Minimum predicted size savings versus higher-bit twin/reference to probe. - min_predicted_size_savings_vs_twin_percent: 1.0 - - # Max changed groups in a candidate that can seed contextual probes. - max_probe_group_count: 4 - - # Max probes generated per anomaly seed. - max_probes_per_seed: 16 - - # Max anomaly probes in one run. - max_total_probes_per_run: 32 - - # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. - max_prediction_space_gap_vs_twin_kld: 0.00050 - - # Optional relative cap for prediction-space gap normalized by local anchor gap. - max_relative_prediction_penalty_vs_twin: 0.35 - - # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. - prediction_space_violation_margin: 0.00005 - - # Shrink applied to prediction-space adjustment after a rule is confirmed. - anomaly_adjustment_shrink_factor: 0.50 - - # Minimum confidence required before applying a confirmed anomaly rule. - min_rule_confidence_to_apply: 0.50 - - # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. - max_negative_adjustment_kld: 0.00075 - - # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. - max_positive_adjustment_kld: 0.00075 - - # Fractional cap relative to BaseRankSafeKld. - max_adjustment_fraction_of_base_kld: 0.75 - - # Number of top smoke candidates to consider per reference quant zone. - max_smoke_candidates_per_reference_zone: 12 - - # Store suppression-only results so false smoke is not repeatedly probed. - persist_suppression_results: true - - # Emit detailed anomaly logs. - verbose_anomaly_logging: true - - # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. - confirmed_anomaly_expansion: - enabled: true - max_neighbors_per_confirmed_rule: 6 - max_total_expansion_probes: 12 - allowed_reference_quants: - - Q8_0 - allowed_candidate_quants: - - Q6_K - - UD-Q6_K_XL - - Q5_K - - UD-Q5_K_XL - -output: - # Leave blank to default to /MagicQuant/Final_Outputs - output_dir: - output_name_prefix: Qwen3.6-27B - export_external_learned_baselines: true - - # false = normal behavior; delete/rebuild final outputs from scratch. - # true = preserve valid existing GGUFs and skip rebuilding them only when - # exact file name + byte size match benchmark truth. - # CLI --reuse-existing-final-artifacts overrides YAML. - reuse_existing_final_artifacts: false - -# Legacy bit-range bucket survival settings were removed. -# See candidate_selection above for the active final chooser settings. - -identity: - architecture_family_name: Qwen3-4B - allow_architecture_family_alias_override: false - -baselines: - standard_baselines_mode: all - enabled_standard_learning_baselines: [] - enabled_standard_combination_carriers: [] - enabled_standard_explicit_group_candidates: [] - - custom_repositories: - - repo_id: unsloth/Qwen3-4B-GGUF - enabled: true - short_source_name: Unsloth - source_kind: huggingface_gguf_repository - require_all_includes_to_resolve: true - validate_tensor_names_against_source_model: true - delete_partial_or_dirty_downloads: true - resume_or_retry_downloads: true - - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: false - - includes: - - - file_name: Qwen3-4B-UD-IQ2_M.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: UD-IQ2_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3-4B-UD-IQ2_XXS.gguf - baseline_family: IQ2_XXS - quantize_base_name: IQ2_XXS - display_name: UD-IQ2_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3-4B-UD-IQ3_XXS.gguf - baseline_family: IQ3_XXS - quantize_base_name: IQ3_XXS - display_name: UD-IQ3_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3-4B-UD-Q2_K_XL.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: UD-Q2_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3-4B-UD-Q3_K_XL.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_M - display_name: UD-Q3_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3-4B-UD-Q4_K_XL.gguf - baseline_family: Q4_K_M - quantize_base_name: Q4_K_M - display_name: UD-Q4_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3-4B-UD-Q5_K_XL.gguf - baseline_family: Q5_K - quantize_base_name: Q5_K - display_name: UD-Q5_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3-4B-UD-Q6_K_XL.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: UD-Q6_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - -# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. -# anomaly_detection remains the low-level compatibility section; synergy_detection controls -# template transfer, composition probes, contamination suppression, and wing diagnostics. -synergy_detection: - enabled: true - max_refinement_rounds: 1 - exact_context_confidence_multiplier: 1.00 - same_selected_groups_confidence_multiplier: 0.55 - equivalent_quant_family_confidence_multiplier: 0.30 - group_family_suspicion_confidence_multiplier: 0.15 - min_confidence_to_apply_adjustment: 0.35 - min_confidence_to_schedule_transfer_probe: 0.25 - max_negative_adjustment_kld: 0.002 - max_negative_adjustment_fraction_of_base_kld: 0.75 - transfer_probe_enabled: true - max_transfer_probes_per_template: 6 - max_total_transfer_probes_per_run: 24 - transfer_probe_context_strata: - high_fidelity_reference_quants: [Q6_K, Q5_K] - mid_fidelity_reference_quants: [Q4_K_M] - low_fidelity_reference_quants: [IQ3_S] - low_fidelity_enabled: false - exploratory_context_pair_enabled: true - max_exploratory_context_pairs_per_run: 14 - exploratory_pair_bit_ranges: [4] - exploratory_pair_context_strata: [mid-fidelity, low-fidelity] - context_scoped_rule_application_enabled: true - max_non_rule_group_context_mismatches: 1 - verbose_synergy_logging: true - min_smoke_score: 0.55 - max_smoke_gap_kld: 0.004 - top_rejected_smoke_preview: 25 - composition_probe_enabled: true - max_template_composition_group_count: 4 - max_composition_probes_per_run: 8 - max_templates_to_compose: 4 - min_template_confidence_for_composition: 0.50 - min_combined_expected_size_savings_percent: 1.0 - contaminating_passenger_detection_enabled: true - min_failure_margin_for_contamination_kld: 0.00050 - contamination_penalty_confidence_multiplier: 0.45 - suppress_repeated_contaminated_attempts: true diff --git a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml b/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml deleted file mode 100644 index d516fbe..0000000 --- a/MagicQuant/Configs/config.qwen3.6-27b.dev.yaml +++ /dev/null @@ -1,401 +0,0 @@ -paths: - magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.6-27B-Qwen/ - llama_root: - llama_bin: - convert_script: - scratch_roots: - - /mnt/world8/ - - /home/slurp/ - - /mnt/world7/ - external_baseline_cache_dir_name: ExternalBaselines - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -learning: - # Destructive relearn options are intentionally targeted. - # These are transient runtime commands and are not persisted as DB state. - # When any option below is enabled, MagicQuant prints a count summary and asks - # for confirmation before deleting/relearning anything. - # - # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, - # and execution probe cache rows scoped to the active architecture family. - # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, - # TensorCombo, or BaselineQuantDefinition rows. - force_relearn_architecture_family: false - - # Relearn built-in/standard baselines by display/canonical name for the current - # architecture family and active tensor group profile. - # Example: - # force_relearn_standard_baselines: - # - Q6_K - # - IQ4_XS - force_relearn_standard_baselines: [] - - # Safety gate for tensor group regex/profile changes. After MagicQuant reads the - # native BF16 GGUF tensor list, it prints group counts, example tensors, - # ambiguous matches, unresolved tensors, and base-quant exception counts, then - # asks before continuing. Keep this true unless running fully unattended. - confirm_tensor_group_profile: true - - # Safe/idempotent repair mode for accidental regex mistakes. - # - # Default true: on every run MagicQuant checks whether older DB learned tensor - # truth can be copied into the active TensorGroupProfile by reapplying the - # current regex/base_quant_exceptions rules. If nothing changed or current rows - # already exist, it skips cleanly and does not create duplicates. - # - # This avoids needless re-download/re-quantization of pure learning baselines - # after regex-only regrouping. Old benchmarks/learned rows remain attached to - # their original TensorGroupProfile and are ignored unless that profile becomes - # active again. - # - # Disable only when you intentionally want the slower/full path to regenerate - # learned grouping truth instead of rebucketing from DB snapshots. - # CLI disable aliases: - # --no-rebucket-learned-tensor-groups - # --disable-tensor-group-rebucket - # --full-relearn-tensor-groups - rebucket_learned_tensor_groups_from_existing_truth: true - - -readme: - # Optional title model name override used in: - # # MagicQuant Hybrids (v2.0) - - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.6-27B - - # Hugging Face README frontmatter. - # Scalars render as: - # license: apache-2.0 - # Arrays render as: - # tags: - # - gguf - # - text-generation - # - # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. - frontmatter: - license: apache-2.0 - tags: - - gguf - - text-generation - - magicquant - - conversational - base_model: - - Qwen/Qwen3.6-27B - -hardware: - gpu_memory_limits_gb: - 0: 19 - 1: 23 - -imatrix: - imatrix_url: - dataset_repo: - dataset_split: text - dataset_config: - dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1_5m.jsonl - -# Legacy evolution survivor knobs were removed from YAML. -# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. - -isolation_pruning: - # Preserve complete isolation truth for prediction and contextual probing. - minimum_isolation_reduction_to_continue_ratio: 0.00 - minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 - maximum_isolation_ppl_delta_percent: 5.0 - maximum_isolation_kld: 0.1 - bad_trade_max_size_delta_percent: 4.0 - bad_trade_kld_multiplier: 2.5 - bad_trade_ppl_multiplier: 3.5 - floating_point_epsilon: 1.0e-8 - minimum_meaningful_base_only_reduction_ratio: 0.01 - - -prediction: - # Rank-safe isolation KLD predictor. - # - # manual_max_predicted_size_bytes is retained only as an emergency compatibility - # field for older helper code. Leave it at 0 for the new chooser. - manual_max_predicted_size_bytes: 0 - - # Candidate bit-stress thresholds for the low-bit interaction correction. - # The predictor fits each candidate threshold against existing category=General - # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. - bit_stress_threshold_candidates: - - 4.0 - - 5.0 - - 6.0 - - 7.0 - - 8.0 - - 9.0 - - 10.0 - - 11.0 - - 12.0 - - # Fallback threshold when too few benchmark rows exist to fit the interaction model. - default_bit_stress_threshold: 8.0 - - # Minimum benchmark rows required before fitting the interaction correction. - minimum_fit_rows: 12 - -candidate_selection: - # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits - # inside this size premium and beats the real linear KLD improvement line. - near_baseline_max_size_growth_percent: 1.5 - - # Phase 3: interior windows between adjacent final anchors. - # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. - interior_window_fractions: - - 0.35 - - 0.35 - - # Number of predicted winners to keep per interior window. - max_candidates_per_interior_window: 1 - - # If the first predicted candidate fails real validation, try this many fallbacks. - max_fallback_attempts_per_anchor: 5 - - # Strict epsilon for lower-KLD comparisons after real benchmark validation. - minimum_kld_improvement_epsilon: 1.0e-9 - - # Final spacing pass: candidates closer than this fraction of the global survivor - # size span are collapsed unless one genuinely earns the slot. - minimum_neighbor_gap_fraction_of_global_span: 0.03 - - # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller - # anchor must provide a stronger KLD gain to justify its existence. - near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 - near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 - - # Default false: do not spend final prediction/build attempts trying to replace - # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. - # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. - allow_eight_bit_anchor_replacements: true - -anomaly_detection: - enabled: true - - # One anomaly refinement pass after smoke/probe/rule generation. - max_anomaly_refinement_rounds: 1 - - # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. - min_actual_gain_vs_twin_kld: 0.00025 - - # Minimum predicted size savings versus higher-bit twin/reference to probe. - min_predicted_size_savings_vs_twin_percent: 1.0 - - # Max changed groups in a candidate that can seed contextual probes. - max_probe_group_count: 4 - - # Max probes generated per anomaly seed. - max_probes_per_seed: 16 - - # Max anomaly probes in one run. - max_total_probes_per_run: 32 - - # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. - max_prediction_space_gap_vs_twin_kld: 0.00050 - - # Optional relative cap for prediction-space gap normalized by local anchor gap. - max_relative_prediction_penalty_vs_twin: 0.35 - - # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. - prediction_space_violation_margin: 0.00005 - - # Shrink applied to prediction-space adjustment after a rule is confirmed. - anomaly_adjustment_shrink_factor: 0.50 - - # Minimum confidence required before applying a confirmed anomaly rule. - min_rule_confidence_to_apply: 0.50 - - # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. - max_negative_adjustment_kld: 0.00075 - - # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. - max_positive_adjustment_kld: 0.00075 - - # Fractional cap relative to BaseRankSafeKld. - max_adjustment_fraction_of_base_kld: 0.75 - - # Number of top smoke candidates to consider per reference quant zone. - max_smoke_candidates_per_reference_zone: 12 - - # Store suppression-only results so false smoke is not repeatedly probed. - persist_suppression_results: true - - # Emit detailed anomaly logs. - verbose_anomaly_logging: true - - # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. - confirmed_anomaly_expansion: - enabled: true - max_neighbors_per_confirmed_rule: 6 - max_total_expansion_probes: 12 - allowed_reference_quants: - - Q8_0 - allowed_candidate_quants: - - Q6_K - - UD-Q6_K_XL - - Q5_K - - UD-Q5_K_XL - -output: - # Leave blank to default to /MagicQuant/Final_Outputs - output_dir: - output_name_prefix: Qwen3.6-27B - export_external_learned_baselines: true - - # false = normal behavior; delete/rebuild final outputs from scratch. - # true = preserve valid existing GGUFs and skip rebuilding them only when - # exact file name + byte size match benchmark truth. - # CLI --reuse-existing-final-artifacts overrides YAML. - reuse_existing_final_artifacts: false - -# Legacy bit-range bucket survival settings were removed. -# See candidate_selection above for the active final chooser settings. - -identity: - architecture_family_name: Qwen3.6-27B - allow_architecture_family_alias_override: false - -baselines: - standard_baselines_mode: all - enabled_standard_learning_baselines: [] - enabled_standard_combination_carriers: [] - enabled_standard_explicit_group_candidates: [] - - custom_repositories: - - repo_id: unsloth/Qwen3.6-27B-GGUF - enabled: true - short_source_name: Unsloth - source_kind: huggingface_gguf_repository - require_all_includes_to_resolve: true - validate_tensor_names_against_source_model: true - delete_partial_or_dirty_downloads: true - resume_or_retry_downloads: true - - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: false - - includes: - - - file_name: Qwen3.6-27B-UD-IQ2_M.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: UD-IQ2_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-IQ2_XXS.gguf - baseline_family: IQ2_XXS - quantize_base_name: IQ2_XXS - display_name: UD-IQ2_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-IQ3_XXS.gguf - baseline_family: IQ3_XXS - quantize_base_name: IQ3_XXS - display_name: UD-IQ3_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q2_K_XL.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: UD-Q2_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q3_K_XL.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_M - display_name: UD-Q3_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q4_K_XL.gguf - baseline_family: Q4_K_M - quantize_base_name: Q4_K_M - display_name: UD-Q4_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q5_K_XL.gguf - baseline_family: Q5_K - quantize_base_name: Q5_K - display_name: UD-Q5_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.6-27B-UD-Q6_K_XL.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: UD-Q6_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - -# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. -# anomaly_detection remains the low-level compatibility section; synergy_detection controls -# template transfer, composition probes, contamination suppression, and wing diagnostics. -synergy_detection: - enabled: true - max_refinement_rounds: 1 - exact_context_confidence_multiplier: 1.00 - same_selected_groups_confidence_multiplier: 0.55 - equivalent_quant_family_confidence_multiplier: 0.30 - group_family_suspicion_confidence_multiplier: 0.15 - min_confidence_to_apply_adjustment: 0.35 - min_confidence_to_schedule_transfer_probe: 0.25 - max_negative_adjustment_kld: 0.002 - max_negative_adjustment_fraction_of_base_kld: 0.75 - transfer_probe_enabled: true - max_transfer_probes_per_template: 6 - max_total_transfer_probes_per_run: 24 - transfer_probe_context_strata: - high_fidelity_reference_quants: [Q6_K, Q5_K] - mid_fidelity_reference_quants: [Q4_K_M] - low_fidelity_reference_quants: [IQ3_S] - low_fidelity_enabled: false - exploratory_context_pair_enabled: true - max_exploratory_context_pairs_per_run: 14 - exploratory_pair_bit_ranges: [4] - exploratory_pair_context_strata: [mid-fidelity, low-fidelity] - context_scoped_rule_application_enabled: true - max_non_rule_group_context_mismatches: 1 - verbose_synergy_logging: true - min_smoke_score: 0.55 - max_smoke_gap_kld: 0.004 - top_rejected_smoke_preview: 25 - composition_probe_enabled: true - max_template_composition_group_count: 4 - max_composition_probes_per_run: 8 - max_templates_to_compose: 4 - min_template_confidence_for_composition: 0.50 - min_combined_expected_size_savings_percent: 1.0 - contaminating_passenger_detection_enabled: true - min_failure_margin_for_contamination_kld: 0.00050 - contamination_penalty_confidence_multiplier: 0.45 - suppress_repeated_contaminated_attempts: true diff --git a/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 422c290..4457b5c 100644 --- a/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -8,80 +8,17 @@ public sealed class MagicQuantYamlConfig public RuntimeFlagConfig Flags { get; set; } = new(); public RuntimeReadmeConfig Readme { get; set; } = new(); public RuntimeImatrixConfig Imatrix { get; set; } = new(); - public RuntimeEvolutionConfig Evolution { get; set; } = new(); public RuntimeIsolationPruningConfig IsolationPruning { get; set; } = new(); public RuntimePredictionConfig Prediction { get; set; } = new(); public RuntimeIdentityConfig Identity { get; set; } = new(); public RuntimeBaselineConfig Baselines { get; set; } = new(); public RuntimeLearningConfig Learning { get; set; } = new(); public RuntimeOutputConfig Output { get; set; } = new(); - public RuntimeSurvivalConfig Survival { get; set; } = new(); public RuntimeCandidateSelectionConfig CandidateSelection { get; set; } = new(); public RuntimeAnomalyDetectionConfig AnomalyDetection { get; set; } = new(); public RuntimeSynergyDetectionConfig SynergyDetection { get; set; } = new(); public RuntimeHardwareConfig Hardware { get; set; } = new(); - public List SensitivityProbeGroups { get; set; } = - [ - "embeddings", - "lm_head", - "attn_q", - "attn_kv", - "attn_output", - "ffn_up_gate", - "ffn_down" - ]; - - public List SensitivityProbeGroupsMoe { get; set; } = - [ - "moe_router", - "moe_experts" - ]; - - public List BrainLayers { get; set; } = - [ - "embeddings", - "lm_head", - "attn_output" - ]; - - public List CollapsePenaltySchemes { get; set; } = - [ - "IQ1_S", - "IQ1_M", - "MXFP4", - "IQ2_XXS", - "IQ2_XS", - "IQ2_S" - ]; - - public List MoeIndicatorTensors { get; set; } = - [ - "blk.*.ffn_up_expert_0.weight", - "blk.*.ffn_gate_expert_0.weight", - "blk.*.ffn_down_expert_0.weight", - "blk.*.ffn_up_exps.weight", - "blk.*.ffn_gate_exps.weight", - "blk.*.ffn_down_exps.weight", - "blk.*.ffn_gate_inp.weight", - "router.weight", - "gate.weight", - "blk.*.router.*", - "blk.*.gate_proj.*", - "blk.*.gate_inp.*", - "model.language_model.layers.*.mlp.experts.gate_up_proj", - "model.language_model.layers.*.mlp.experts.down_proj", - "model.language_model.layers.*.mlp.gate.weight", - "model.language_model.layers.*.mlp.shared_expert.gate_proj.weight", - "model.language_model.layers.*.mlp.shared_expert.up_proj.weight", - "model.language_model.layers.*.mlp.shared_expert.down_proj.weight", - "model.language_model.layers.*.experts.gate_up_proj", - "model.language_model.layers.*.experts.down_proj", - "model.language_model.layers.*.router.proj.weight", - "model.language_model.layers.*.router.per_expert_scale", - "model.language_model.layers.*.router.scale" - ]; - public static MagicQuantYamlConfig CreateDefault() => new(); } @@ -122,14 +59,6 @@ public sealed class RuntimeImatrixConfig public string? DatasetLocalFile { get; set; } } -public sealed class RuntimeEvolutionConfig -{ - public int MaxDataCollectedPerCategory { get; set; } = 5; - public int MaxSurvivalRounds { get; set; } = 4; - public double CollapseMultiplier { get; set; } = 1.5d; - public int BruteForceFinalCombinationThreshold { get; set; } = 2_000; -} - public sealed class RuntimeIsolationPruningConfig { public double MinimumIsolationReductionToContinueRatio { get; set; } = 0.04d; @@ -188,17 +117,6 @@ public sealed class RuntimeOutputConfig public bool ReuseExistingFinalArtifacts { get; set; } = false; } -public sealed class RuntimeSurvivalConfig -{ - public int MaxSelectedChoicesPerBucket { get; set; } = 5; - public double MeaningfulSizeBiasPercent { get; set; } = 1.0d; - public double KldCloseCallAbsoluteEpsilon { get; set; } = 0.00075d; - public double KldCloseCallRelativeFraction { get; set; } = 0.02d; - public double PplLargeDifferencePercent { get; set; } = 0.75d; - public double TradeScoreSizeBiasWeight { get; set; } = 1.25d; - public double TradeScorePplWeight { get; set; } = 0.15d; -} - public sealed class RuntimeCandidateSelectionConfig { /// @@ -400,7 +318,7 @@ public sealed class RuntimeLearningConfig public List ForceRelearnStandardBaselines { get; set; } = new(); /// - /// Safety gate for regex/profile mistakes. When true, the evolution run prints + /// Safety gate for regex/profile mistakes. When true, the pipeline run prints /// native BF16 tensor-group counts and asks before continuing. /// public bool ConfirmTensorGroupProfile { get; set; } = true; diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index 63c1158..b23a0a9 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -10,6 +10,10 @@ namespace MagicQuant.Configuration; +/// +/// Loads one YAML document, applies supported CLI overrides, and initializes runtime state. +/// Custom files inherit typed defaults, not values from the distributed YAML profile. +/// public static class MagicQuantYamlLoader { public static MagicQuantYamlConfig LoadAndApply(string commandName, IReadOnlyList args) @@ -21,7 +25,7 @@ public static MagicQuantYamlConfig LoadAndApply(string commandName, IReadOnlyLis { throw new FileNotFoundException( $"MagicQuant config file was not found at '{configPath}'. " + - "Ensure config.default.yaml or config.dev.yaml is copied next to the build output, or pass --config."); + "Ensure config.default.yaml is copied next to the build output, or pass --config."); } var deserializer = new DeserializerBuilder() @@ -48,11 +52,6 @@ public static string ResolveConfigPath(IReadOnlyList args) if (!string.IsNullOrWhiteSpace(explicitPath)) return Path.GetFullPath(explicitPath); -#if DEBUG - string preferred = Path.Combine(AppContext.BaseDirectory, "config.dev.yaml"); - if (File.Exists(preferred)) - return preferred; -#endif return Path.Combine(AppContext.BaseDirectory, "config.default.yaml"); } @@ -114,9 +113,6 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) .Where(x => !string.IsNullOrWhiteSpace(x.Key) && !IsEmptyFrontmatterValue(x.Value)) .ToDictionary(x => x.Key.Trim(), x => x.Value, StringComparer.OrdinalIgnoreCase); - if (config.Survival.MaxSelectedChoicesPerBucket <= 0) - config.Survival.MaxSelectedChoicesPerBucket = 1; - if (config.Prediction.BitStressThresholdCandidates.Count == 0) config.Prediction.BitStressThresholdCandidates.Add(config.Prediction.DefaultBitStressThreshold); @@ -316,9 +312,6 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList config.Imatrix.DatasetConfig = Prefer(Get("imatrix-dataset-config"), config.Imatrix.DatasetConfig); config.Imatrix.DatasetLocalFile = Prefer(Get("imatrix-dataset-local-file"), config.Imatrix.DatasetLocalFile); - if (int.TryParse(Get("brute-force-final-combination-threshold"), out var bruteForceThreshold) && bruteForceThreshold > 0) - config.Evolution.BruteForceFinalCombinationThreshold = bruteForceThreshold; - if (ulong.TryParse(Get("manual-max-predicted-size-bytes"), out var manualBytes)) config.Prediction.ManualMaxPredictedSizeBytes = manualBytes; @@ -383,27 +376,6 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (Has("export-external-learned-baselines")) config.Output.ExportExternalLearnedBaselines = true; if (Has("reuse-existing-final-artifacts")) config.Output.ReuseExistingFinalArtifacts = true; - if (int.TryParse(Get("max-selected-choices-per-bucket"), out var maxSelectedChoicesPerBucket) && maxSelectedChoicesPerBucket > 0) - config.Survival.MaxSelectedChoicesPerBucket = maxSelectedChoicesPerBucket; - - if (double.TryParse(Get("survival-meaningful-size-bias-percent"), out var sizeBiasPercent) && sizeBiasPercent >= 0d) - config.Survival.MeaningfulSizeBiasPercent = sizeBiasPercent; - - if (double.TryParse(Get("survival-kld-close-call-absolute-epsilon"), out var kldCloseCallAbs) && kldCloseCallAbs >= 0d) - config.Survival.KldCloseCallAbsoluteEpsilon = kldCloseCallAbs; - - if (double.TryParse(Get("survival-kld-close-call-relative-fraction"), out var kldCloseCallRelative) && kldCloseCallRelative >= 0d) - config.Survival.KldCloseCallRelativeFraction = kldCloseCallRelative; - - if (double.TryParse(Get("survival-ppl-large-difference-percent"), out var pplLargeDiff) && pplLargeDiff >= 0d) - config.Survival.PplLargeDifferencePercent = pplLargeDiff; - - if (double.TryParse(Get("survival-trade-score-size-bias-weight"), out var sizeWeight) && sizeWeight >= 0d) - config.Survival.TradeScoreSizeBiasWeight = sizeWeight; - - if (double.TryParse(Get("survival-trade-score-ppl-weight"), out var pplWeight) && pplWeight >= 0d) - config.Survival.TradeScorePplWeight = pplWeight; - config.Identity.ArchitectureFamilyName = Prefer(Get("architecture-family"), config.Identity.ArchitectureFamilyName); if (Has("allow-architecture-family-alias-override")) config.Identity.AllowArchitectureFamilyAliasOverride = true; } diff --git a/MagicQuant/Helpers/CliHelpers.cs b/MagicQuant/Helpers/CliHelpers.cs index 9912289..d8756b3 100644 --- a/MagicQuant/Helpers/CliHelpers.cs +++ b/MagicQuant/Helpers/CliHelpers.cs @@ -73,14 +73,6 @@ public static void ValidateCombinationLogicWorks(bool realResults = false) public static void PrintTotalCombinationCount() { - /*const long MaxSupported = 4_000_000_000L; - - BigInteger total = ComboCounter.CountAll(); - - if (total > MaxSupported) - throw new InvalidOperationException( - $"Total combinations ({total:N0}) exceed database primary ID limit ({MaxSupported:N0}).");*/ - BigInteger total = ComboCounter.CountAll(); AnsiConsole.MarkupLine( @@ -164,7 +156,7 @@ public static void ShowHelp(Dictionary [blue][[--option value]][/]"); + AnsiConsole.MarkupLine("Usage: [bold]dotnet run --project MagicQuant --[/] [blue][[--option value]][/]"); AnsiConsole.MarkupLine("Config: [green]--config[/] [grey][/] (CLI flags override YAML)"); AnsiConsole.MarkupLine("Identity: [green]--architecture-family[/] [grey][/] | [green]--allow-architecture-family-alias-override[/]"); AnsiConsole.WriteLine(); diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 3138394..8fc994a 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -23,19 +23,6 @@ Always - - Always - - - Always - - - Always - - - - - diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 885c103..3a1ef6d 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -7,57 +7,9 @@ using MQ.DB.Models; using Spectre.Console; -#if DEBUG -if (args.Length == 0) -{ - // Use: "clone" or "evolution" - const string debugMode = "clone"; // switch to "evolution" to use the full learning/search pipeline again. +var commands = CommandCatalog.Create(); - if (string.Equals(debugMode, "clone", StringComparison.OrdinalIgnoreCase)) - { - args = - [ - "clone-repository-quants", - "--config", $"\"{Path.Combine(AppContext.BaseDirectory, "config.clone-unsloth.dev.yaml")}\"", - "--architecture-family", @"""Qwen3.8-27B""", - "--source-json", @"""/mnt/world8/AI/Models/Qwen3.8-27B-MagicQuant/magicquant-manifest/magicquant.clone-configs.json""", - "--model-dir", @"""/mnt/world8/AI/Models/Qwen3.8-27B-Qwen/""", - "--output-dir", @"""/mnt/world8/AI/Models/Qwen3.8-27B-MagicQuant-Unsloth/""" - ]; - } - else - { - // Previous DEBUG harness kept intact for quick full-pipeline testing. - // --reuse-existing-final-artifacts preserves/reuses valid existing final GGUFs by exact file name + byte size. - // Omit --reuse-existing-final-artifacts to force normal full rebuild behavior. - // "--config", @"/path/to/config.dev.yaml", - args = - [ - "evolution", - "--architecture-family", @"""Qwen3.8-27B""", - "--allow-architecture-family-alias-override" - ]; - } -} -else if (args.Length > 0 && - (string.Equals(args[0], "evolution", StringComparison.OrdinalIgnoreCase) || - string.Equals(args[0], "clone-repository-quants", StringComparison.OrdinalIgnoreCase)) && - !args.Any(x => string.Equals(x, "--architecture-family", StringComparison.OrdinalIgnoreCase))) -{ - args = args.Concat(["--architecture-family", @"""Qwen3-4B-Instruct-2507"""]).ToArray(); -} -#endif - -var commands = new Dictionary Factory)>(StringComparer.OrdinalIgnoreCase) -{ - { "evolution", ("Run the full evolutionary quantization search", () => new Evolution()) }, - { "validate-predictions", ("Validate rank-safe KLD predictions against existing SQLite benchmarks", () => new ValidatePredictions()) }, - { "build-hybrids", ("Export specific hybrid models with polished README", () => new BuildHybrids()) }, - { "clone-repository-quants", ("Clone final MagicQuant tensor configurations from a compatible repository/json", () => new CloneRepositoryQuants()) }, - { "initialize-llama-cpp", ("Initialize or update llama.cpp", () => new InitializeLlamaCpp()) } -}; - -if (args.Length == 0 || args[0].Equals("help", StringComparison.OrdinalIgnoreCase)) +if (args.Length == 0 || CommandCatalog.IsHelp(args[0])) { CliHelpers.ShowHelp(commands); return; @@ -67,8 +19,9 @@ if (!commands.TryGetValue(commandInput, out var commandInfo)) { - AnsiConsole.MarkupLine($"[red]Error:[/] The command [yellow]'{commandInput}'[/] does not exist."); + AnsiConsole.MarkupLine($"[red]Error:[/] The command [yellow]'{Markup.Escape(commandInput)}'[/] does not exist."); CliHelpers.ShowHelp(commands); + Environment.ExitCode = 2; return; } @@ -76,6 +29,15 @@ try { + // Help is a read-only operation: do not load config, clean caches, install + // dependencies, or open databases just to explain a command. + if (parsedArgs.Any(a => a.Name.Equals("help", StringComparison.OrdinalIgnoreCase)) || + args.Skip(1).Any(a => a == "-h")) + { + await commandInfo.Factory().Run([new CliArg { Name = "help", Value = string.Empty }]); + return; + } + var loadedConfig = MagicQuantYamlLoader.LoadAndApply(commandInput, parsedArgs); var startupScratch = new ScratchStorageService(); await startupScratch.CleanupStaleScratchArtifactsAsync(); @@ -109,12 +71,11 @@ await AnsiConsole.Status() AnsiConsole.WriteLine(); } - CliHelpers.ValidateCombinationLogicWorks(); - var commandInstance = commandInfo.Factory(); await commandInstance.Run(parsedArgs); } catch (Exception ex) { AnsiConsole.WriteException(ex); + Environment.ExitCode = 1; } diff --git a/MagicQuant/Services/CloneConfigManifestGenerationService.cs b/MagicQuant/Services/CloneConfigManifestGenerationService.cs index 9761fe4..e8331d5 100644 --- a/MagicQuant/Services/CloneConfigManifestGenerationService.cs +++ b/MagicQuant/Services/CloneConfigManifestGenerationService.cs @@ -44,7 +44,7 @@ public async Task GenerateAsync( SourceJson = sourceJson, SourceModelId = Cache.CurrentModelId, SourceArchitectureFamily = Cache.CurrentArchitectureFamilyName, - Notes = "Exact GGUF tensor quantization map for repository clone/reproducibility mode. This file is not a proof that another cloned model went through the full MagicQuant evolution pipeline. External reference finalists use persisted SQLite learned tensor truth when no local final GGUF was exported." + Notes = "Exact GGUF tensor quantization map for repository clone/reproducibility mode. This file is not a proof that another cloned model went through the full MagicQuant discovery pipeline. External reference finalists use persisted SQLite learned tensor truth when no local final GGUF was exported." }; double? referencePpl = ResolveReferencePpl(pplReference, exportedArtifacts.Select(x => x.Snapshot)); diff --git a/MagicQuant/Services/CombinationDatabasePathService.cs b/MagicQuant/Services/CombinationDatabasePathService.cs new file mode 100644 index 0000000..f1c5f47 --- /dev/null +++ b/MagicQuant/Services/CombinationDatabasePathService.cs @@ -0,0 +1,37 @@ +using MagicQuant.Helpers; +using MQ.DB; + +namespace MagicQuant.Services; + +/// +/// Shared path contract for the DuckDB writer and prediction reader. Changing this +/// filename opens a different candidate database; preserve it across refactors. +/// SQLite remains the authority for measured truth, while DuckDB is derived state. +/// +public static class CombinationDatabasePathService +{ + private const string DbFileNamePrefix = "MagicQuant_Combinations"; + + public static string GetPath() => Path.Combine(GetDirectory(), GetFileName()); + + public static string GetDirectory() + { + if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) + return Cache.ModelMagicQuantDirectory!; + + if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) + return Cache.MagicQuantDirectory!; + + throw new InvalidOperationException( + "Neither Cache.ModelMagicQuantDirectory nor Cache.MagicQuantDirectory is set."); + } + + public static string GetFileName() + { + string model = string.IsNullOrWhiteSpace(Cache.CurrentModelId) ? "unknown-model" : Cache.CurrentModelId; + string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixIdentityHash ?? "imatrix-unknown") : "no-imatrix"; + string hp = RuntimeSearchSpace.AllowHighPrecisionHybrids ? "hp-on" : "hp-off"; + return $"{DbFileNamePrefix}_{model}_{imatrix}_{hp}.duckdb"; + } + +} diff --git a/MagicQuant/Services/ModelArtifactPathService.cs b/MagicQuant/Services/ModelArtifactPathService.cs index 475386d..cf3535e 100644 --- a/MagicQuant/Services/ModelArtifactPathService.cs +++ b/MagicQuant/Services/ModelArtifactPathService.cs @@ -4,6 +4,10 @@ namespace MagicQuant.Services; +/// +/// Names durable model artifacts and quantization logs from the active model context. +/// Temporary heavy writes belong to ScratchStorageService leases instead. +/// public sealed class ModelArtifactPathService { public string ModelDirectory => Cache.ModelDirectory diff --git a/MagicQuant/Services/ModelRuntimePathService.cs b/MagicQuant/Services/ModelRuntimePathService.cs index aa84e70..239d359 100644 --- a/MagicQuant/Services/ModelRuntimePathService.cs +++ b/MagicQuant/Services/ModelRuntimePathService.cs @@ -2,6 +2,9 @@ namespace MagicQuant.Services; +/// +/// Initializes model-scoped cache locations after the command has selected its source model. +/// public static class ModelRuntimePathService { public static void InitializeForCurrentModel() diff --git a/MagicQuant/Services/OutputPathService.cs b/MagicQuant/Services/OutputPathService.cs new file mode 100644 index 0000000..5f503ef --- /dev/null +++ b/MagicQuant/Services/OutputPathService.cs @@ -0,0 +1,27 @@ +namespace MagicQuant.Services; + +/// +/// Resolves output locations without creating directories. Relative-path differences +/// are historical command contracts; keep them explicit to avoid relocating artifacts. +/// +public static class OutputPathService +{ + public static string Pipeline(string modelWorkDirectory, string? configuredOutput) => + string.IsNullOrWhiteSpace(configuredOutput) + ? Path.Combine(modelWorkDirectory, "Final_Outputs") + : Path.GetFullPath(Path.Combine(modelWorkDirectory, configuredOutput)); + + public static string Clone(string modelWorkDirectory, string? explicitOutput, string? configuredOutput) => + Path.GetFullPath(!string.IsNullOrWhiteSpace(explicitOutput) + ? explicitOutput + : !string.IsNullOrWhiteSpace(configuredOutput) + ? configuredOutput + : Path.Combine(modelWorkDirectory, "FinalOutput")); + + public static string PredictionValidation(string modelWorkDirectory, string? explicitOutput, string? configuredOutput) => + !string.IsNullOrWhiteSpace(explicitOutput) + ? Path.GetFullPath(explicitOutput) + : !string.IsNullOrWhiteSpace(configuredOutput) + ? Path.Combine(Path.GetFullPath(configuredOutput), "PredictionValidation") + : Path.Combine(modelWorkDirectory, "PredictionValidation"); +} diff --git a/MagicQuant/Services/QuantDatabaseService.cs b/MagicQuant/Services/QuantDatabaseService.cs index b7ccb71..daf6ad1 100644 --- a/MagicQuant/Services/QuantDatabaseService.cs +++ b/MagicQuant/Services/QuantDatabaseService.cs @@ -14,7 +14,6 @@ namespace MagicQuant.Services; public class QuantDatabaseService { - private const string DbFileNamePrefix = "MagicQuant_Combinations"; private const string TableName = CombinationDuckDbSchema.TableName; private static readonly string[] ExpectedColumnTypes = CombinationDuckDbSchema.ExpectedColumnTypes; @@ -116,31 +115,11 @@ ORDER BY return results; } - private static string GetDuckDbDirectory() - { - if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) - return Cache.ModelMagicQuantDirectory!; - - if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) - return Cache.MagicQuantDirectory!; - - throw new InvalidOperationException( - "Neither Cache.ModelMagicQuantDirectory nor Cache.MagicQuantDirectory is set."); - } - - private static string BuildContextAwareDuckDbFileName() - { - string model = string.IsNullOrWhiteSpace(Cache.CurrentModelId) ? "unknown-model" : Cache.CurrentModelId; - string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixIdentityHash ?? "imatrix-unknown") : "no-imatrix"; - string hp = RuntimeSearchSpace.AllowHighPrecisionHybrids ? "hp-on" : "hp-off"; - return $"{DbFileNamePrefix}_{model}_{imatrix}_{hp}.duckdb"; - } - - private string ConnectionString => $"Data Source={Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName())}"; + private string ConnectionString => $"Data Source={CombinationDatabasePathService.GetPath()}"; public async Task InitializeAsync(bool forceRebuild = false, CancellationToken ct = default) { - var duckDbDirectory = GetDuckDbDirectory(); + var duckDbDirectory = CombinationDatabasePathService.GetDirectory(); Directory.CreateDirectory(duckDbDirectory); using var connection = new DuckDBConnection(ConnectionString); diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 7599194..84fd4f8 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -72,10 +72,10 @@ public QuantizationService(BenchmarkService benchmarker) if (string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) throw new Exception( - "Cache.ModelMagicQuantDirectory not set. Evolution must set this before quantization starts."); + "Cache.ModelMagicQuantDirectory not set. The pipeline must set this before quantization starts."); if (string.IsNullOrWhiteSpace(Cache.ModelDirectory)) - throw new Exception("Cache.ModelDirectory not set. Evolution must set this before quantization starts."); + throw new Exception("Cache.ModelDirectory not set. The pipeline must set this before quantization starts."); if (string.IsNullOrWhiteSpace(Cache.LlamaBin)) throw new Exception("Cache.LlamaBin not set. Initialization must complete before quantization starts."); diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index aab3a1c..c28ec30 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -234,7 +234,7 @@ private static void AppendCloneNotice(StringBuilder sb, ReadmeCloneContext clone sb.AppendLine("Clone Notice"); sb.AppendLine(); sb.AppendLine( - $"This repository did not run through the full MagicQuant evolution/search pipeline. It is a clone of the final survivor tensor configurations from {source}, rebuilt and benchmarked locally for this model."); + $"This repository did not run through the full MagicQuant discovery pipeline. It is a clone of the final survivor tensor configurations from {source}, rebuilt and benchmarked locally for this model."); sb.AppendLine(); sb.AppendLine( "The archived MagicQuant JSON files in `magicquant-manifest/` are copied from the source release for durability. The clone benchmark JSON and the table below are from this clone run, so those metrics reflect the rebuilt outputs in this repository."); diff --git a/MagicQuant/Services/RemainingCombinationStore.cs b/MagicQuant/Services/RemainingCombinationStore.cs index 446eea4..6d753c9 100644 --- a/MagicQuant/Services/RemainingCombinationStore.cs +++ b/MagicQuant/Services/RemainingCombinationStore.cs @@ -10,12 +10,11 @@ namespace MagicQuant.Services; public sealed class RemainingCombinationStore { - private const string DbFileNamePrefix = "MagicQuant_Combinations"; private const string TableName = CombinationDuckDbSchema.TableName; - private static string ConnectionString => $"Data Source={GetDatabaseFilePathInternal()}"; + private static string ConnectionString => $"Data Source={CombinationDatabasePathService.GetPath()}"; - public string GetDatabaseFilePath() => GetDatabaseFilePathInternal(); + public string GetDatabaseFilePath() => CombinationDatabasePathService.GetPath(); public async Task CountAsync(CancellationToken ct = default) { @@ -801,35 +800,6 @@ private static async Task RecreateTableAsync(DuckDBConnection connection, Cancel await createCmd.ExecuteNonQueryAsync(ct); } - private static string GetDuckDbDirectory() - { - if (!string.IsNullOrWhiteSpace(Cache.ModelMagicQuantDirectory)) - return Cache.ModelMagicQuantDirectory!; - - if (!string.IsNullOrWhiteSpace(Cache.MagicQuantDirectory)) - return Cache.MagicQuantDirectory!; - - throw new InvalidOperationException( - "Neither Cache.ModelMagicQuantDirectory nor Cache.MagicQuantDirectory is set."); - } - - private static string GetDatabaseFilePathInternal() - { - return Path.Combine(GetDuckDbDirectory(), BuildContextAwareDuckDbFileName()); - } - - private static string BuildContextAwareDuckDbFileName() - { - // IMPORTANT: this must stay byte-for-byte compatible with QuantDatabaseService - // unless both services are changed together. The previous patch made only the - // prediction reader profile-aware, which opened a brand-new empty DuckDB file - // after stage-1 had populated the original file. - string model = string.IsNullOrWhiteSpace(Cache.CurrentModelId) ? "unknown-model" : Cache.CurrentModelId; - string imatrix = Cache.IsImatrixAvailable ? (Cache.ActiveImatrixIdentityHash ?? "imatrix-unknown") : "no-imatrix"; - string hp = RuntimeSearchSpace.AllowHighPrecisionHybrids ? "hp-on" : "hp-off"; - return $"{DbFileNamePrefix}_{model}_{imatrix}_{hp}.duckdb"; - } - private static async Task EnsureTensorConfigsTableExistsAsync(DuckDBConnection connection, CancellationToken ct) { using var cmd = connection.CreateCommand(); @@ -841,7 +811,7 @@ private static async Task EnsureTensorConfigsTableExistsAsync(DuckDBConnection c return; throw new InvalidOperationException( - $"DuckDB search-space table '{TableName}' does not exist in '{GetDatabaseFilePathInternal()}'. " + + $"DuckDB search-space table '{TableName}' does not exist in '{CombinationDatabasePathService.GetPath()}'. " + "This almost always means the generator and prediction reader are using different DuckDB filenames, " + "or prediction started before QuantDatabaseService initialized/rebuilt the search-space table."); } diff --git a/MagicQuant/Services/TensorGroupReviewService.cs b/MagicQuant/Services/TensorGroupReviewService.cs index b6651bb..298ada4 100644 --- a/MagicQuant/Services/TensorGroupReviewService.cs +++ b/MagicQuant/Services/TensorGroupReviewService.cs @@ -50,7 +50,7 @@ public async Task ReviewNativeTensorGroupingAsync( if (!confirmed) { throw new OperationCanceledException( - "Evolution run cancelled by user after tensor-group profile review. No tensor-group-scoped learning/search work was started."); + "Pipeline run cancelled by user after tensor-group profile review. No tensor-group-scoped learning/search work was started."); } } else diff --git a/MagicQuant/config.clone-unsloth.dev.yaml b/MagicQuant/config.clone-unsloth.dev.yaml deleted file mode 100644 index 29d4e8c..0000000 --- a/MagicQuant/config.clone-unsloth.dev.yaml +++ /dev/null @@ -1,35 +0,0 @@ -paths: - model_dir: /mnt/world8/AI/Models/Qwen3.8-27B-Qwen/ - scratch_roots: - - /mnt/world8/ - - /home/slurp/ - - /mnt/world7/ - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -hardware: - gpu_memory_limits_gb: - 0: 19 - 1: 23 - -# Prebuilt Unsloth imatrix. Keep the other source modes empty so ImatrixService -# sees exactly one active source. -imatrix: - imatrix_url: "https://huggingface.co/unsloth/Qwen3.8-27B-GGUF/resolve/main/imatrix_unsloth.gguf?download=true" - dataset_repo: - dataset_split: - dataset_config: - dataset_local_file: - -output: - output_dir: /mnt/world8/AI/Models/Qwen3.8-27B-MagicQuant-Unsloth/ - output_name_prefix: Qwen3.8-27B - reuse_existing_final_artifacts: false - -identity: - architecture_family_name: Qwen3.8-27B - allow_architecture_family_alias_override: false diff --git a/MagicQuant/config.default.yaml b/MagicQuant/config.default.yaml index ec03982..56acafd 100644 --- a/MagicQuant/config.default.yaml +++ b/MagicQuant/config.default.yaml @@ -6,7 +6,7 @@ # # General rules: # - CLI flags override values from this YAML. -# - In DEBUG, your dev config may be auto-selected instead. +# - Debug and Release use this file unless --config is supplied. # - Leave values blank when they must be provided per-machine or per-run. # # Notes: @@ -20,7 +20,7 @@ paths: # Root MagicQuant working directory. - # If blank, runtime may fall back to current working directory logic. + # If blank, defaults to /MagicQuant. magic_quant_root: # REQUIRED for real runs. @@ -117,14 +117,11 @@ readme: # # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. frontmatter: - license: apache-2.0 + # Set license and base_model to match the actual source model before publishing. tags: - gguf - text-generation - magicquant - - conversational - base_model: - - Username/Model_Name hardware: # Optional per-GPU usable VRAM limits in GB. @@ -262,9 +259,9 @@ candidate_selection: near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 - # Default false: do not spend final prediction/build attempts trying to replace + # This distributed config enables final prediction/build attempts to replace # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. - # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. + # Set false to keep Q8 as the highest-fidelity practical anchor. allow_eight_bit_anchor_replacements: true anomaly_detection: @@ -371,15 +368,15 @@ baselines: # all # Keep built-in standard baselines active AND allow custom repositories. # - # standard_only - # Use only built-in baselines. Ignore custom repositories. + # selected + # Use the explicit built-in role lists below; empty lists enable none for that role. # - # custom_only - # Use only custom repositories for learning/carrier/explicit-group roles, - # except for any internal anchors the runtime still requires. + # none + # Disable built-in learning/carrier/explicit-group roles. Internal native anchors + # may still be required. Custom repositories are configured independently. standard_baselines_mode: all - # If empty, runtime uses normal built-in defaults for that category. + # These lists apply only in selected mode. In all mode, built-in defaults apply. # # Example: # enabled_standard_learning_baselines: [Q8_0, Q6_K, Q5_K, Q4_K_M] diff --git a/MagicQuant/config.dev.yaml b/MagicQuant/config.dev.yaml deleted file mode 100644 index 09e8ddc..0000000 --- a/MagicQuant/config.dev.yaml +++ /dev/null @@ -1,627 +0,0 @@ -paths: - magic_quant_root: - model_dir: /mnt/world8/AI/Models/Qwen3.8-27B-Qwen/ - llama_root: - llama_bin: - convert_script: - scratch_roots: - - /mnt/world8/ - - /home/slurp/ - - /mnt/world7/ - external_baseline_cache_dir_name: ExternalBaselines - -flags: - use_imatrix: true - force_imatrix_rebuild: false - force_refresh_hardware_probe: false - allow_high_precision_hybrids: false - -learning: - # Destructive relearn options are intentionally targeted. - # These are transient runtime commands and are not persisted as DB state. - # When any option below is enabled, MagicQuant prints a count summary and asks - # for confirmation before deleting/relearning anything. - # - # Deletes learned mappings, benchmark truth, dependent benchmark/source rows, - # and execution probe cache rows scoped to the active architecture family. - # Does not delete AiModelHash, ArchitectureFamily, ImatrixDefinition, - # TensorCombo, or BaselineQuantDefinition rows. - force_relearn_architecture_family: false - - # Relearn built-in/standard baselines by display/canonical name for the current - # architecture family and active tensor group profile. - # Example: - # force_relearn_standard_baselines: - # - Q6_K - # - IQ4_XS - force_relearn_standard_baselines: [] - - # Safety gate for tensor group regex/profile changes. After MagicQuant reads the - # native BF16 GGUF tensor list, it prints group counts, example tensors, - # ambiguous matches, unresolved tensors, and base-quant exception counts, then - # asks before continuing. Keep this true unless running fully unattended. - confirm_tensor_group_profile: true - - # Safe/idempotent repair mode for accidental regex mistakes. - # - # Default true: on every run MagicQuant checks whether older DB learned tensor - # truth can be copied into the active TensorGroupProfile by reapplying the - # current regex/base_quant_exceptions rules. If nothing changed or current rows - # already exist, it skips cleanly and does not create duplicates. - # - # This avoids needless re-download/re-quantization of pure learning baselines - # after regex-only regrouping. Old benchmarks/learned rows remain attached to - # their original TensorGroupProfile and are ignored unless that profile becomes - # active again. - # - # Disable only when you intentionally want the slower/full path to regenerate - # learned grouping truth instead of rebucketing from DB snapshots. - # CLI disable aliases: - # --no-rebucket-learned-tensor-groups - # --disable-tensor-group-rebucket - # --full-relearn-tensor-groups - rebucket_learned_tensor_groups_from_existing_truth: true - - -readme: - # Optional title model name override used in: - # # MagicQuant Hybrids (v2.0) - - # If blank, MagicQuant uses identity.architecture_family_name. - title_model_name_override: Qwen3.8-27B - - # Hugging Face README frontmatter. - # Scalars render as: - # license: apache-2.0 - # Arrays render as: - # tags: - # - gguf - # - text-generation - # - # Add more keys freely, such as base_model, datasets, language, pipeline_tag, etc. - frontmatter: - license: apache-2.0 - tags: - - gguf - - text-generation - - magicquant - - conversational - base_model: - - Qwen/Qwen3.8-27B - -hardware: - gpu_memory_limits_gb: - 0: 19 - 1: 23 - -imatrix: - imatrix_url: - dataset_repo: - dataset_split: text - dataset_config: - dataset_local_file: /home/slurp/Documents/Output_Files/Dataset/artifacts/imatrix-general-v1-1_5m.jsonl - -# Legacy evolution survivor knobs were removed from YAML. -# Final hybrid selection is now driven by rank-safe isolation prediction plus candidate_selection. - -isolation_pruning: - # Preserve complete isolation truth for prediction and contextual probing. - minimum_isolation_reduction_to_continue_ratio: 0.00 - minimum_isolation_reduction_to_suppress_bf16_ratio: 0.10 - maximum_isolation_ppl_delta_percent: 5.0 - maximum_isolation_kld: 0.1 - bad_trade_max_size_delta_percent: 4.0 - bad_trade_kld_multiplier: 2.5 - bad_trade_ppl_multiplier: 3.5 - floating_point_epsilon: 1.0e-8 - minimum_meaningful_base_only_reduction_ratio: 0.01 - - -prediction: - # Rank-safe isolation KLD predictor. - # - # manual_max_predicted_size_bytes is retained only as an emergency compatibility - # field for older helper code. Leave it at 0 for the new chooser. - manual_max_predicted_size_bytes: 0 - - # Candidate bit-stress thresholds for the low-bit interaction correction. - # The predictor fits each candidate threshold against existing category=General - # benchmark truth and keeps the best MAE fit for the active model/imatrix bucket. - bit_stress_threshold_candidates: - - 4.0 - - 5.0 - - 6.0 - - 7.0 - - 8.0 - - 9.0 - - 10.0 - - 11.0 - - 12.0 - - # Fallback threshold when too few benchmark rows exist to fit the interaction model. - default_bit_stress_threshold: 8.0 - - # Minimum benchmark rows required before fitting the interaction correction. - minimum_fit_rows: 12 - -candidate_selection: - # Phase 2: a hybrid can replace the smaller/higher-damage anchor when it fits - # inside this size premium and beats the real linear KLD improvement line. - near_baseline_max_size_growth_percent: 1.5 - - # Phase 3: interior windows between adjacent final anchors. - # [0.35, 0.35] means test the first 35% of the size span, then the next 35%. - interior_window_fractions: - - 0.35 - - 0.35 - - # Number of predicted winners to keep per interior window. - max_candidates_per_interior_window: 1 - - # If the first predicted candidate fails real validation, try this many fallbacks. - max_fallback_attempts_per_anchor: 5 - - # Strict epsilon for lower-KLD comparisons after real benchmark validation. - minimum_kld_improvement_epsilon: 1.0e-9 - - # This campaign is the publishable union frontier: retain every nondominated - # size/quality tradeoff instead of collapsing nearby points for a shorter list. - minimum_neighbor_gap_fraction_of_global_span: 0.00 - - # Extra-brutal zone near the smaller anchor. A candidate this close to the smaller - # anchor must provide a stronger KLD gain to justify its existence. - near_lower_anchor_brutal_zone_fraction_of_pair_span: 0.02 - near_anchor_required_kld_gain_fraction_of_pair_gap: 0.05 - - # Default false: do not spend final prediction/build attempts trying to replace - # 8-bit anchors such as Q8_0 during strict dominance or near-anchor replacement. - # Q8 is treated as the highest-fidelity practical anchor unless this is enabled. - allow_eight_bit_anchor_replacements: true - -anomaly_detection: - enabled: true - - # One anomaly refinement pass after smoke/probe/rule generation. - max_anomaly_refinement_rounds: 1 - - # Minimum actual KLD gain versus higher-bit counterfactual twin to confirm anomaly. - min_actual_gain_vs_twin_kld: 0.00025 - - # Minimum predicted size savings versus higher-bit twin/reference to probe. - min_predicted_size_savings_vs_twin_percent: 1.0 - - # Max changed groups in a candidate that can seed contextual probes. - max_probe_group_count: 4 - - # Max probes generated per anomaly seed. - max_probes_per_seed: 16 - - # Max anomaly probes in one run. - max_total_probes_per_run: 32 - - # Strong smoke if a monotone downgrade candidate is this close to or better than its twin in prediction space. - max_prediction_space_gap_vs_twin_kld: 0.00050 - - # Optional relative cap for prediction-space gap normalized by local anchor gap. - max_relative_prediction_penalty_vs_twin: 0.35 - - # Minimum margin used when forcing confirmed anomalies below their higher-bit twin in prediction space. - prediction_space_violation_margin: 0.00005 - - # Shrink applied to prediction-space adjustment after a rule is confirmed. - anomaly_adjustment_shrink_factor: 0.50 - - # Minimum confidence required before applying a confirmed anomaly rule. - min_rule_confidence_to_apply: 0.50 - - # Absolute cap on total negative anomaly adjustment in prediction-space KLD units. - max_negative_adjustment_kld: 0.00075 - - # Absolute cap on positive harmful interaction adjustment in prediction-space KLD units. - max_positive_adjustment_kld: 0.00075 - - # Fractional cap relative to BaseRankSafeKld. - max_adjustment_fraction_of_base_kld: 0.75 - - # Number of top smoke candidates to consider per reference quant zone. - max_smoke_candidates_per_reference_zone: 12 - - # Store suppression-only results so false smoke is not repeatedly probed. - persist_suppression_results: true - - # Emit detailed anomaly logs. - verbose_anomaly_logging: true - - # Small bounded sniff pass around already-confirmed beneficial contextual anomalies. - confirmed_anomaly_expansion: - enabled: true - max_neighbors_per_confirmed_rule: 6 - max_total_expansion_probes: 12 - allowed_reference_quants: - - Q8_0 - allowed_candidate_quants: - - Q6_K - - UD-Q6_K_XL - - Q5_K - - UD-Q5_K_XL - -output: - # Leave blank to default to /MagicQuant/Final_Outputs - output_dir: - output_name_prefix: Qwen3.8-27B - export_external_learned_baselines: false - - # false = normal behavior; delete/rebuild final outputs from scratch. - # true = preserve valid existing GGUFs and skip rebuilding them only when - # exact file name + byte size match benchmark truth. - # CLI --reuse-existing-final-artifacts overrides YAML. - reuse_existing_final_artifacts: false - -# Legacy bit-range bucket survival settings were removed. -# See candidate_selection above for the active final chooser settings. - -identity: - architecture_family_name: Qwen3.8-27B - allow_architecture_family_alias_override: false - -baselines: - # Use Unsloth GGUFs for dynamic learning/search while retaining the built-in - # Q8 anchor required by the execution-plan and baseline pipeline. - standard_baselines_mode: selected - # Controlled fidelity probes use these provider-neutral blankets. They must be - # learned so base-quant exception tensors have complete carrier mappings, but - # only Q8 remains a search carrier to avoid multiplying the global combinatorics. - enabled_standard_learning_baselines: [Q8_0, Q6_K, Q5_K, Q4_K_M, IQ3_S] - enabled_standard_combination_carriers: [Q8_0] - enabled_standard_explicit_group_candidates: [Q8_0] - - custom_repositories: - - repo_id: unsloth/Qwen3.8-27B-GGUF - # Immutable dynamic-v3 revision used by the completed Qwen3.8 campaign. - revision: 4ca720788d1e01f1bff70c033e0d0028fd02e502 - enabled: true - short_source_name: Unsloth - source_kind: huggingface_gguf_repository - require_all_includes_to_resolve: true - validate_tensor_names_against_source_model: true - delete_partial_or_dirty_downloads: true - resume_or_retry_downloads: true - - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: false - - includes: - - # 1-bit - - - file_name: Qwen3.8-27B-UD-IQ1_S.gguf - baseline_family: IQ1_S - quantize_base_name: IQ1_S - display_name: Unsloth-UD-IQ1_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-IQ1_M.gguf - baseline_family: IQ1_M - quantize_base_name: IQ1_M - display_name: Unsloth-UD-IQ1_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - # 2-bit - - - file_name: Qwen3.8-27B-UD-IQ2_S.gguf - baseline_family: IQ2_S - quantize_base_name: IQ2_S - display_name: Unsloth-UD-IQ2_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-IQ2_XXS.gguf - baseline_family: IQ2_XXS - quantize_base_name: IQ2_XXS - display_name: Unsloth-UD-IQ2_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q2_K_XL.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: Unsloth-UD-Q2_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - # 3-bit - - - file_name: Qwen3.8-27B-UD-IQ3_S.gguf - baseline_family: IQ3_S - quantize_base_name: IQ3_S - display_name: Unsloth-UD-IQ3_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-IQ3_XXS.gguf - baseline_family: IQ3_XXS - quantize_base_name: IQ3_XXS - display_name: Unsloth-UD-IQ3_XXS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q3_K_XL.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_M - display_name: Unsloth-UD-Q3_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - # 4-bit - - - file_name: Qwen3.8-27B-UD-IQ4_XS.gguf - baseline_family: IQ4_XS - quantize_base_name: IQ4_XS - display_name: Unsloth-UD-IQ4_XS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q4_K_S.gguf - baseline_family: Q4_K_S - quantize_base_name: Q4_K_S - display_name: Unsloth-UD-Q4_K_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q4_K_M.gguf - baseline_family: Q4_K_M - quantize_base_name: Q4_K_M - display_name: Unsloth-UD-Q4_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q4_K_XL.gguf - baseline_family: Q4_K_M - quantize_base_name: Q4_K_M - display_name: Unsloth-UD-Q4_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - # 5-bit - - - file_name: Qwen3.8-27B-UD-Q5_K_S.gguf - baseline_family: Q5_K_S - quantize_base_name: Q5_K_S - display_name: Unsloth-UD-Q5_K_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q5_K_M.gguf - baseline_family: Q5_K - quantize_base_name: Q5_K - display_name: Unsloth-UD-Q5_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q5_K_XL.gguf - baseline_family: Q5_K - quantize_base_name: Q5_K - display_name: Unsloth-UD-Q5_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - # 6-bit - - - file_name: Qwen3.8-27B-UD-Q6_K.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: Unsloth-UD-Q6_K - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q6_K_M.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: Unsloth-UD-Q6_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q6_K_L.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: Unsloth-UD-Q6_K_L - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-UD-Q6_K_XL.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: Unsloth-UD-Q6_K_XL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - # Unsloth replaced its dynamic-v2 files when dynamic-v3 was published. Keep - # the source generation explicit and reproducible instead of importing old - # MagicQuant winners or silently resolving these names against a moving main. - - repo_id: unsloth/Qwen3.8-27B-GGUF - revision: 313447f257f7ebde0b968e4778feef774546ed81 - enabled: true - short_source_name: UnslothV2 - source_kind: huggingface_gguf_repository - require_all_includes_to_resolve: true - validate_tensor_names_against_source_model: true - delete_partial_or_dirty_downloads: true - resume_or_retry_downloads: true - - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: false - - includes: - - file_name: Qwen3.8-27B-UD-IQ2_M.gguf - baseline_family: IQ2_M - quantize_base_name: IQ2_M - display_name: Unsloth-UD-IQ2_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q3_K_S.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_S - display_name: Unsloth-Q3_K_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q3_K_M.gguf - baseline_family: IQ3_M - quantize_base_name: IQ3_M - display_name: Unsloth-Q3_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-IQ4_XS.gguf - baseline_family: IQ4_XS - quantize_base_name: IQ4_XS - display_name: Unsloth-IQ4_XS - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-IQ4_NL.gguf - baseline_family: IQ4_NL - quantize_base_name: IQ4_NL - display_name: Unsloth-IQ4_NL - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q4_K_S.gguf - baseline_family: Q4_K_S - quantize_base_name: Q4_K_S - display_name: Unsloth-Q4_K_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q4_K_M.gguf - baseline_family: Q4_K_M - quantize_base_name: Q4_K_M - display_name: Unsloth-Q4_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q5_K_S.gguf - baseline_family: Q5_K_S - quantize_base_name: Q5_K_S - display_name: Unsloth-Q5_K_S - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q5_K_M.gguf - baseline_family: Q5_K - quantize_base_name: Q5_K - display_name: Unsloth-Q5_K_M - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - - - file_name: Qwen3.8-27B-Q6_K.gguf - baseline_family: Q6_K - quantize_base_name: Q6_K - display_name: Unsloth-Q6_K - force_relearn: false - allow_as_learning_baseline: true - allow_as_combination_carrier: false - allow_as_explicit_group_candidate: true - -# Counterfactual synergy templates generalize confirmed contextual anomaly evidence. -# anomaly_detection remains the low-level compatibility section; synergy_detection controls -# template transfer, composition probes, contamination suppression, and wing diagnostics. -synergy_detection: - enabled: true - max_refinement_rounds: 1 - exact_context_confidence_multiplier: 1.00 - same_selected_groups_confidence_multiplier: 0.55 - equivalent_quant_family_confidence_multiplier: 0.30 - group_family_suspicion_confidence_multiplier: 0.15 - min_confidence_to_apply_adjustment: 0.35 - min_confidence_to_schedule_transfer_probe: 0.25 - max_negative_adjustment_kld: 0.002 - max_negative_adjustment_fraction_of_base_kld: 0.75 - transfer_probe_enabled: true - max_transfer_probes_per_template: 6 - max_total_transfer_probes_per_run: 24 - transfer_probe_context_strata: - high_fidelity_reference_quants: [Q6_K, Q5_K] - mid_fidelity_reference_quants: [Q4_K_M] - low_fidelity_reference_quants: [IQ3_S] - low_fidelity_enabled: true - exploratory_context_pair_enabled: true - max_exploratory_context_pairs_per_run: 14 - exploratory_pair_bit_ranges: [4] - exploratory_pair_context_strata: [mid-fidelity, low-fidelity] - context_scoped_rule_application_enabled: true - max_non_rule_group_context_mismatches: 1 - verbose_synergy_logging: true - min_smoke_score: 0.55 - max_smoke_gap_kld: 0.004 - top_rejected_smoke_preview: 25 - composition_probe_enabled: true - max_template_composition_group_count: 4 - max_composition_probes_per_run: 8 - max_templates_to_compose: 4 - min_template_confidence_for_composition: 0.50 - min_combined_expected_size_savings_percent: 1.0 - contaminating_passenger_detection_enabled: true - min_failure_margin_for_contamination_kld: 0.00050 - contamination_penalty_confidence_multiplier: 0.45 - suppress_repeated_contaminated_attempts: true diff --git a/README.md b/README.md new file mode 100644 index 0000000..09ae52d --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +# MagicQuant Pipeline + +MagicQuant is a benchmark-driven GGUF evaluation and hybrid-discovery system. It measures standard and external quantization baselines, probes tensor groups, predicts promising combinations, and validates final survivors with real benchmarks. Hybrids earn a place only when their size/fidelity tradeoff is worthwhile. + +This repository contains the .NET command-line application. The [MagicQuant research wiki](https://github.com/magiccodingman/MagicQuant-Wiki) explains the methodology and results. Despite the historical `evolution` command name, the current pipeline does **not** perform evolutionary search. + +## Build and inspect + +Install the .NET 10 SDK, then run from the repository root: + +```sh +dotnet restore MagicQuant-Pipeline.sln +dotnet build MagicQuant-Pipeline.sln -c Release +dotnet test MagicQuant-Pipeline.sln -c Release --no-build +dotnet run --project MagicQuant -c Release --no-build -- --help +dotnet run --project MagicQuant -c Release --no-build -- pipeline --help +``` + +Building, testing, and viewing help do not require model weights or llama.cpp. Running without arguments also shows help, in both Debug and Release. + +## Run a model + +Real quantization needs a complete local Hugging Face model directory (top-level `.safetensors`, model configuration, and tokenizer assets), llama.cpp, a Python environment, and enough RAM/VRAM and disk space for native GGUFs, baselines, logits, and exports. Hardware requirements depend on the model. Linux with an apt-based distribution is the primary automatic setup path; Windows has setup code but is not covered by the Linux CI job. Automatic macOS setup is not implemented. + +1. Copy the distributed tuning profile and edit the paths and model identity: + + ```sh + cp MagicQuant/config.default.yaml config.local.yaml + ``` + + Set `paths.model_dir` and `identity.architecture_family_name`. Choose a dedicated `output.output_dir` and set `output.output_name_prefix`. Before publishing generated model cards, set `readme.frontmatter` to the source model's actual license and metadata. Use absolute paths for a portable campaign invocation. + +2. Prepare dependencies: + + ```sh + dotnet run --project MagicQuant -c Release --no-build -- initialize-llama-cpp + ``` + + This can download/build llama.cpp, install Python packages, and request sudo for apt packages on Linux. It uses `/MagicQuant`. To use existing llama.cpp files, configure **all three** of `paths.llama_root`, `paths.llama_bin`, and `paths.convert_script`, and pass `--config config.local.yaml`. See [setup](docs/setup.md) for Python requirements and custom runtime roots. + +3. Start the campaign: + + ```sh + dotnet run --project MagicQuant -c Release --no-build -- pipeline --config config.local.yaml + ``` + + Review the tensor grouping prompt before allowing learning to continue. The run learns/reuses benchmark truth and exports its selected survivors. Runtime dependency validation may perform setup when using the default environment. + +**Use a dedicated export directory:** normal export cleans/rebuilds its contents. `--reuse-existing-final-artifacts` permits reuse only when artifacts match the command's validation rules. Do not point output at your source model directory or another directory containing files you need to keep. + +## Commands + +| Command | Purpose | +| --- | --- | +| `pipeline` | Full baseline learning, isolation probing, prediction, real validation, and export | +| `evolution` | Backward-compatible alias for `pipeline` | +| `build-hybrids` | Existing entry point for the full pipeline, including export; not an export-only shortcut | +| `clone-repository-quants` | Rebuild configurations from a compatible repository or clone manifest | +| `validate-predictions` | Compare predictions with existing SQLite benchmark truth and export reports | +| `initialize-llama-cpp` | Set up or update native/Python dependencies | + +Append `--help` to any command. Arguments after `--` belong to MagicQuant, not `dotnet run`. [Command examples](docs/commands.md) cover cloning and validation. + +## Documentation + +- [Setup and troubleshooting](docs/setup.md) +- [Configuration and path rules](docs/configuration.md) +- [Commands and workflows](docs/commands.md) +- [Architecture and code map](docs/architecture.md) +- [Storage, caching, and reruns](docs/storage.md) +- [Contributing and tests](CONTRIBUTING.md) +- [Compatibility notes for existing users](docs/migration.md) + +The small [example configurations](examples/) demonstrate the required fields. They use C# defaults for omitted settings; they are **not** merged with `config.default.yaml`. Copy the full default file when you want its distributed tuning values. + +## Project status + +The research pipeline is active software with model- and hardware-dependent integration requirements. Unit/regression tests run without quantizing a model; a passing test suite alone does not establish numerical parity for a full hardware campaign. The repository does not yet contain a software license; the maintainer must choose one before an open-source release. A generated model card's license field does not license this program. diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 0000000..664b064 --- /dev/null +++ b/docs/architecture.md @@ -0,0 +1,54 @@ +# Architecture and contributor code map + +## Execution flow + +`Program.cs` dispatches through `CommandCatalog`. Help returns before runtime initialization. A normal command loads YAML into `Config.Current`, applies run state to `MQ.DB.Cache`, cleans stale scratch artifacts, validates invariants/dependencies, and invokes an `ICommand`. + +`Commands/QuantizationPipeline.cs` coordinates full discovery. `Evolution.cs` preserves the historical C# entry point and the CLI registry keeps `evolution` as an alias. The orchestrator should describe stage order; reusable behavior belongs in services. + +1. Validate source model and initialize model-local paths. +2. Obtain model hash; prepare native GGUF and optional projector; review tensor grouping. +3. Resolve architecture family and tensor-group profile; register custom baselines; handle targeted relearn. +4. Acquire imatrix context, rebucket existing tensor truth when appropriate, and load/probe the hardware plan. +5. Establish native benchmark truth and compatibility, then run initial/continuation isolation samples. +6. Apply isolation policy and materialize the remaining candidate space in DuckDB. +7. Fit predictions, investigate contextual evidence, choose candidates, and validate them with real benchmarks. +8. Finalize survivors and write GGUFs, manifests, benchmark summaries, and model cards. + +The [research wiki](https://github.com/magiccodingman/MagicQuant-Wiki) is the source for the mathematical motivation. This guide maps the implementation, not a new algorithm specification. + +## Where to change things + +| Concern | Main files / services | +| --- | --- | +| CLI routing/help | `Program.cs`, `Commands/CommandCatalog.cs`, command `ShowHelp` methods | +| YAML shape and CLI overrides | `Configuration/MagicQuantYamlConfig.cs`, `MagicQuantYamlLoader.cs`, `config.default.yaml` | +| Baseline identity and roles | `MQ.DB/Models/BaselineQuants.cs`, `BaselineDefinitionResolver`, `HuggingFaceBaselineService` | +| Tensor grouping and profile review | `MQ.DB/tensor_groups.yaml`, `TensorGroupReviewService`, `TensorGroupProfileService`, `TensorGroupRebucketService` | +| Process/tool setup | `InitializeLlamaCpp`, `Helpers/LlamaBuilder`, `Helpers/PythonManager`, `HardwareHelper` | +| Native conversion and quantization | `QuantizationService`, `ExternalBaselineTensorParity`, `CloneManifestTensorMapBuildService` | +| Benchmark execution and GPU planning | `BenchmarkService`, `BenchmarkGpuPlanning`, `LlamaGpuArgumentBuilder` | +| Isolation sampling and policy | `IsolationPlanningService`, `IsolationOptimizationService`, `Helpers/RuntimeSearchSpace` | +| SQLite measured truth | `MQ.DB/Data/MagicQuantContext.cs`, `MQ.DB/Models/DbModels`, `HybridBenchmarkRepository` | +| DuckDB candidate data | `QuantDatabaseService`, `RemainingCombinationStore`, `CombinationDuckDbSchema` | +| KLD prediction and final selection | `RankSafeKldPredictionService`, `PredictionGuidedHybridSelectionService`, `SmartBaselineTuningFallbackService` | +| Contextual anomaly/synergy evidence | `AnomalyWorkflowService`, `AnomalyRuleRepository`, `AnomalyAdjustedPredictionService` | +| Release artifacts | `HybridArtifactExportService`, `FinalArtifactNamingService`, `FinalReleaseMetadataService`, `ReadmeGenerationService` | +| Paths and lifecycle | `ModelArtifactPathService`, `ModelRuntimePathService`, `OutputPathService`, `CombinationDatabasePathService`, `ScratchStorageService` | + +## Invariants worth protecting + +- **Measured truth and prediction are different.** SQLite stores observations and context. DuckDB is a derived candidate/prediction workspace. Do not turn predictions into benchmark truth or silently substitute a standard family for a missing exact custom baseline measurement. +- **Identity is scoped.** Model hash, architecture family, tensor-group profile, baseline identity, and imatrix context determine which evidence may be reused. Similar display names are not sufficient. +- **Effective tensor assignments matter.** A carrier quant and an explicit group quant can describe the same effective assignment. Use existing resolvers and canonical baseline identities instead of inventing equality rules. +- **Runtime search state is mutable.** `RuntimeSearchSpace` controls the current allowed universe. Do not revive legacy static candidate lists as an authority. +- **Paths are contracts.** Writer and reader use `CombinationDatabasePathService` for the same DuckDB file. `OutputPathService` preserves command-specific destinations. Keep existing filenames, schema IDs, and serialized manifests stable unless a migration is part of the change. +- **Scratch is leased; downloads are durable.** Use `ScratchStorageService` leases for heavy temporary GGUF work, and durable external-baseline paths for reusable downloads. Do not add ad hoc cleanup of model roots. + +## Global state and tests + +`Config.Current`, `Cache`, and several baseline/search registries are process-wide mutable state. The CLI runs one command per process. Do not run multiple campaigns concurrently inside one process without redesigning those boundaries. + +Tests currently disable parallel execution because these globals are shared. Tests that change them must save and restore the prior state in `finally`, use unique temporary directories, and clean up only those directories. Prefer testing a pure policy/path helper when possible. Executable-level CLI tests protect the entry point separately from command implementation tests. + +Large benchmark, quantization, and selection services remain candidates for incremental extraction. Extract a cohesive responsibility behind regression tests instead of splitting files by arbitrary line count or changing numerical policy during a readability patch. diff --git a/docs/commands.md b/docs/commands.md new file mode 100644 index 0000000..fbe6d50 --- /dev/null +++ b/docs/commands.md @@ -0,0 +1,62 @@ +# Commands and workflows + +Run these examples from the repository root after a Release build. Replace paths and identities with your own. `mq` in older command help is shorthand for invoking the MagicQuant executable; this repository does not install a global `mq` tool. + +## Full discovery pipeline + +```sh +dotnet run --project MagicQuant -c Release --no-build -- pipeline \ + --config config.local.yaml \ + --model-dir /data/models/my-model \ + --architecture-family my-model-family \ + --output-dir /data/exports/my-model \ + --output-name-prefix MyModel +``` + +The pipeline converts/loads the native source, reviews tensor groups, resolves identity and baselines, measures isolation samples, predicts useful hybrids, validates them, and exports survivors. The exact runtime choices depend on benchmark truth and YAML policy. `evolution` invokes the same implementation. `build-hybrids` also enters the full pipeline; it is not limited to exporting previously selected files. + +`--use-imatrix` enables the configured acquisition/build flow. Provide an appropriate `imatrix` source in YAML. `--recheck-hardware-probe` refreshes execution-plan probing after hardware changes. `--skip-tensor-group-confirm` suppresses the tensor-group prompt for an already reviewed unattended campaign; it does not bypass all other confirmations. + +## Clone known tensor configurations + +```sh +dotnet run --project MagicQuant -c Release --no-build -- clone-repository-quants \ + --config config.local.yaml \ + --model-dir /data/models/compatible-model \ + --architecture-family my-model-family \ + --source-json /data/releases/source/magicquant-manifest/magicquant.clone-configs.json \ + --output-dir /data/exports/cloned-model +``` + +Use `--source-repo owner/repo` instead to read a Hugging Face repository. `--source-json` also accepts an HTTP(S) URL. Clone mode rebuilds the tensor configurations and benchmarks them locally; it does not establish that the new model passed full discovery. + +By default the manifest must match the target tensor inventory. `--allow-missing-manifest-tensors` explicitly allows a strict subset; unmatched target tensors use base quantization. `--missing-manifest-base-quant Q8_0` additionally selects that base quant. Use these only when that compatibility tradeoff is intended. + +## Validate predictions against existing measurements + +```sh +dotnet run --project MagicQuant -c Release --no-build -- validate-predictions \ + --config config.local.yaml \ + --model-dir /data/models/my-model \ + --architecture-family my-model-family \ + --output-dir /data/reports/my-model +``` + +This writes `prediction_validation_general.csv` and `prediction_validation_general.md`. It uses existing general-category SQLite benchmark truth rather than launching a fresh full discovery campaign. Startup still runs the common dependency validation and stale-artifact cleanup. + +For imatrix measurements supply `--imatrix-path /data/imatrix.dat` or `--imatrix-identity-hash ` to select the exact bucket. Merely enabling `flags.use_imatrix` does not select a validation bucket. Without either option, validation uses no-imatrix truth. + +## Rerun and reuse + +```sh +dotnet run --project MagicQuant -c Release --no-build -- pipeline \ + --config config.local.yaml --reuse-existing-final-artifacts +``` + +The normal pipeline can reuse scoped measurements, but final export normally cleans/rebuilds output. Artifact reuse is an additional opt-in: pipeline exports require exact filename and benchmark byte-size matches; clone mode also validates its benchmark JSON rows. A file merely existing is not sufficient. + +## Help and exit status + +No arguments, `help`, `--help`, or `-h` display top-level help. ` --help` and ` -h` display command help without config loading, cleanup, database access, or dependency installation. + +The host returns `0` on normal completion/help, `2` for an unknown command, and `1` for an exception caught at the command boundary. Services may handle individual candidate failures and continue a campaign, so also inspect the reported sample failures and final artifacts. diff --git a/docs/configuration.md b/docs/configuration.md new file mode 100644 index 0000000..837f396 --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,55 @@ +# Configuration and paths + +## Loading and precedence + +`--config ` selects a YAML file; otherwise the executable loads its adjacent `config.default.yaml`. Debug and Release follow the same rule. `config.dev.yaml` is no longer selected automatically. + +The loader deserializes the selected file into `MagicQuantYamlConfig`, whose property initializers supply omitted fields, applies supported CLI overrides, normalizes values, and updates `Config.Current` and `MQ.DB.Cache`. It does **not** merge a custom file with `config.default.yaml`. The distributed YAML intentionally differs from C# defaults for some research tuning settings. Copy that whole file to reproduce its profile. + +CLI string options generally override nonblank YAML values. Many boolean switches only enable a feature; use YAML to disable it unless a specific negative CLI switch exists. Use `--name value` or `--name=value`; quote paths with spaces using normal shell quoting. + +Unknown YAML keys are currently ignored for compatibility. This means misspellings can be silently ignored. Compare with the commented default file and `MagicQuant/Configuration/MagicQuantYamlConfig.cs`. CI strictly parses the distributed examples so their keys cannot silently drift. + +## Main sections + +| Section | Responsibility | +| --- | --- | +| `paths` | Source model, runtime root, llama.cpp, scratch roots, external cache directory name | +| `identity` | Architecture-family name and explicit alias override | +| `flags` | Imatrix, hardware reprobe, high-precision hybrid policy | +| `learning` | Tensor review, safe profile rebucketing, targeted relearn requests | +| `baselines` | Built-in roles and external repository definitions, including optional revision pins | +| `hardware` | Per-GPU usable VRAM limits | +| `imatrix` | Local/remote matrix or dataset source | +| `isolation_pruning` | Isolation gating and damage/tradeoff thresholds | +| `prediction` | Rank-safe KLD fitting and combination memory limits | +| `candidate_selection` | Final candidate windows, fallback attempts, and spacing/tradeoff rules | +| `anomaly_detection`, `synergy_detection` | Bounded contextual probes and evidence adjustments | +| `output` | Export destination, naming, reuse, and external-baseline export policy | +| `readme` | Generated model card title and frontmatter | + +Built-in `standard_baselines_mode` is `all`, `selected`, or `none`. `selected` uses the explicit role lists; an empty list enables none for that role. In `all`, those lists do not restrict built-ins. Custom repositories are configured independently. Native anchors may still be required by the runtime. Historical `standard_only`/`custom_only` comments did not describe implemented filtering modes. + +## Path contracts + +Paths do not expand shell variables or `~` inside YAML. Prefer absolute paths. Relative `--config`, model, runtime, llama.cpp, dataset, and scratch paths resolve against the process working directory, **not** the YAML file's directory. + +| Setting / command | Blank default | Relative value | +| --- | --- | --- | +| `paths.magic_quant_root` | `/MagicQuant` | Process working directory | +| `paths.model_dir` | Required for model commands | Process working directory | +| Model work directory | `/MagicQuant` | Derived from model path | +| `pipeline` / `build-hybrids` output | `/MagicQuant/Final_Outputs` | Under `/MagicQuant` | +| Clone output | `/MagicQuant/FinalOutput` | Process working directory | +| Prediction validation output via CLI | `/MagicQuant/PredictionValidation` if no YAML output | Process working directory, exact destination | +| Prediction validation with YAML output only | `/PredictionValidation` | YAML output resolves against process working directory | + +These historical output differences are preserved for existing campaigns. `OutputPathService` owns the rules. An absolute `output_dir` avoids ambiguity. + +`external_baseline_cache_dir_name` is intended to be a folder name beneath the model work directory; use a simple name such as `ExternalBaselines`. Scratch roots are separate from durable downloads. They need not be physically distinct disks, but the scheduler's one-heavy-writer-per-root policy assumes you choose them thoughtfully. + +## Model metadata and compatibility + +`readme.frontmatter` accepts arbitrary scalar/list metadata. Set `license`, `base_model`, and other provenance fields for the actual exported model; no model license is inferred for you. + +The old `evolution`, `survival`, sensitivity-group, brain-layer, and collapse-penalty config surfaces had no active consumers and have been removed from the typed configuration. Old YAML containing them is still tolerated, but they do not tune the current algorithm. See `candidate_selection` and the research wiki for current selection policy. diff --git a/docs/migration.md b/docs/migration.md new file mode 100644 index 0000000..589461a --- /dev/null +++ b/docs/migration.md @@ -0,0 +1,35 @@ +# Compatibility notes for existing users + +This cleanup retains numerical selection policy, SQLite schemas/migrations, manifest names, and existing output destinations. There is no data migration. + +## Startup and names + +- Prefer `pipeline`; `evolution` remains a case-insensitive CLI alias. The historical `Evolution` C# class delegates through inheritance to `QuantizationPipeline`. +- Debug builds no longer inject a personal command, model path, or architecture family. With no arguments, both build configurations show help. +- Pass `--config /path/to/your-config.yaml` explicitly for personal campaigns. Debug no longer auto-selects `config.dev.yaml`. Previously tracked developer campaigns were replaced by portable examples; existing local copies still load when selected explicitly. +- Command help runs before initialization. Unknown commands return status 2; caught command/config failures return status 1. +- `initialize-llama-cpp` now honors custom paths from YAML directly, caches the validated paths, and reports invalid custom environments as failures instead of printing an error and returning successfully. + +## Removed inactive surfaces + +The old evolution/survival knobs and unused sensitivity, brain-layer, collapse-penalty, and MoE-indicator config lists had no active runtime consumers. Their typed properties and inactive CLI overrides were removed. Legacy YAML keys remain ignored by the permissive loader; they never tuned the current chooser. Use `prediction`, `candidate_selection`, `anomaly_detection`, and `synergy_detection` for current policy. + +Startup no longer enumerates the entire combination universe merely to compare it with a count. That diagnostic helper remains available for explicit development checks. Actual pipeline generation and policy checks remain in place. + +## Paths and metadata + +Relative-path behavior is preserved, including `Final_Outputs` for pipeline and `FinalOutput` for clone. The shared path services make those differences explicit. Use absolute output paths when sharing configs. + +The distributed model-card template no longer asserts `apache-2.0` or a placeholder `base_model`. Set real provenance in your campaign YAML before publishing. Existing explicit frontmatter is still honored. + +The documentation now uses the implemented built-in baseline modes (`all`, `selected`, `none`). Older comments referring to `standard_only` and `custom_only` did not match the loader's behavior. + +To recover a previously tracked campaign before adopting the new layout, save it as +an ignored local config (replace the revision placeholder): + +```sh +git show :MagicQuant/config.dev.yaml > config.local.yaml +``` + +Then continue with `pipeline --config config.local.yaml` and your explicit model/family +arguments. The change to DEBUG startup does not change values inside that saved YAML. diff --git a/docs/setup.md b/docs/setup.md new file mode 100644 index 0000000..0f0c2c8 --- /dev/null +++ b/docs/setup.md @@ -0,0 +1,55 @@ +# Setup and troubleshooting + +## Development requirements + +All three projects target `net10.0`. Use the .NET 10 SDK. NuGet restore downloads the managed packages and native SQLite/DuckDB assets. The solution includes `MagicQuant`, `MQ.DB`, and `MagicQuant.Tests`. + +```sh +dotnet restore MagicQuant-Pipeline.sln +dotnet build MagicQuant-Pipeline.sln -c Release +dotnet test MagicQuant-Pipeline.sln -c Release --no-build +``` + +These commands do not install llama.cpp or Python packages. Some regression tests create temporary SQLite databases and inspect local hardware. Tests do not require CUDA or model weights. + +## Runtime setup + +`initialize-llama-cpp` prepares the shared `/MagicQuant` installation. On apt-based Linux it checks build tools, CMake, Ninja, Git, Python/venv/pip, and libcurl development files; NVIDIA detection also adds a CUDA toolkit package. Missing packages may trigger sudo. The Python setup installs model conversion and dataset dependencies, PyTorch, and llama-cpp-python. `--update` requests dependency updates and a native rebuild, so record the resulting llama.cpp revision for reproducible research. + +The installer uses the configured hardware to choose native build options. macOS automatic setup is unsupported. Windows bootstrap code exists, but validate it on the target machine rather than assuming parity with Linux. + +You can bypass automatic native setup by providing an existing environment: + +```yaml +paths: + llama_root: /opt/llama.cpp + llama_bin: /opt/llama.cpp/build/bin + convert_script: /opt/llama.cpp/convert_hf_to_gguf.py +``` + +All three paths are required together. This branch validates their existence and detects hardware; it does not install the Python dependencies. The application expects its Python executable under `/MagicQuant-Env/bin/python` on Linux, or `MagicQuant-Env/python.exe` on Windows. Use the normal initializer first when using the default runtime root. `--validate` and `--verify` are historical setup flags, not read-only dependency checks. + +An explicit `paths.magic_quant_root` changes the SQLite/runtime root but **does not relocate the shared installer**. If you isolate that root, provision its expected Python environment as well. For example, on Linux an isolated root can link `MagicQuant-Env` to an already initialized shared environment. Make that choice explicitly; sharing Python still shares installed dependency versions. See [storage](storage.md) before moving existing campaign data. + +## Model input + +Use a complete source model directory supported by your llama.cpp conversion revision. `pipeline` requires at least one top-level `.safetensors` file. Model config/tokenizer files are also needed by conversion. A directory containing only a downloaded quantized GGUF is not a source model directory. + +Architecture-family identity scopes learned data. Choose the intended family deliberately; `--allow-architecture-family-alias-override` bypasses an identity guard and should not be a routine setup flag. + +## Troubleshooting + +| Symptom | Check | +| --- | --- | +| Missing SDK / unsupported target framework | `dotnet --info`; install a .NET 10 SDK | +| Missing config | Pass `--config` explicitly; the default is next to the executable | +| Missing model directory or safetensors | Set `paths.model_dir` to the complete local source model | +| Partial custom llama.cpp paths | Supply root, binary directory, and converter together | +| Python executable/package failure with isolated root | Check `/MagicQuant-Env` and its installed packages | +| Conversion or unknown tensor failure | Check model support in the actual llama.cpp checkout and review tensor grouping | +| Hardware plan no longer fits | Check GPU visibility and configured VRAM limits; use `--recheck-hardware-probe` after hardware changes | +| Unexpected export location | Check command-specific relative-path rules in [configuration](configuration.md) | +| Prediction reports use the wrong bucket | Use the same model/runtime database and exact imatrix identity as the measured run | +| No cached results reused | Check model hash, architecture family, tensor-group profile, and imatrix scope before considering relearn | + +For a useful bug report include the command, sanitized YAML, commit, .NET/OS/native dependency versions, GPU information, and relevant logs. Do not attach model weights or a whole runtime database by default. diff --git a/docs/storage.md b/docs/storage.md new file mode 100644 index 0000000..60fd09c --- /dev/null +++ b/docs/storage.md @@ -0,0 +1,47 @@ +# Storage, caching, and reruns + +The runtime root and model work directory are different things. With default settings: + +```text +/MagicQuant/ + MagicQuant_SQLite.db # measured truth, learned mappings, hardware probe state + llama.cpp/ # shared native checkout/build + MagicQuant-Env/ # Python environment + +/ + *.safetensors # input weights + config.json # source metadata and tokenizer assets alongside it + MagicQuant/ + GGUF/ # durable native/base artifacts + Benchmarks/ # measurements, corpora, reference logits + Logs/Quantization/ # quantization process logs + ExternalBaselines/ # durable downloaded external GGUFs + MagicQuant_Combinations___.duckdb + Final_Outputs/ # pipeline default export directory + FinalOutput/ # clone default export directory + PredictionValidation/ # prediction report default +``` + +Output is configurable and only directories needed by a run are created. Other service-specific files may also appear. Final exports put JSON evidence under `magicquant-manifest/`, including clone configurations, final survivors, replacements, hybrid maps, isolation samples, and bad-trade reports as appropriate to the workflow. `MagicQuantManifestPathService` owns those filenames and links. + +## Durable truth + +SQLite is initialized/migrated automatically when its context is opened. Existing migrations and stored IDs are compatibility boundaries. Back up the database and related model artifacts while the process is stopped before manual moves or experiments. Do not delete the database as routine troubleshooting: it contains measured evidence that can be expensive to reconstruct. + +Changing the runtime root selects another SQLite database. It does not move data or the shared native installer. Changing a tensor-group regex/profile changes the applicable evidence scope; normal rebucketing can copy existing learned mappings into the new profile without erasing the original observations. Targeted relearn settings explicitly delete scoped truth after the program's confirmation step. + +## Derived candidate space + +DuckDB stores the current allowed combinations and prediction materialization. Both writer and reader resolve the same model/imatrix/high-precision filename through `CombinationDatabasePathService`. The pipeline rebuilds candidate data; the database is not interchangeable with SQLite benchmark evidence. + +Do not rename its files or add a scope component on only one side of a writer/reader pair. That can make a populated candidate space appear empty. + +## Scratch and cleanup + +Configured `paths.scratch_roots` hold `.MagicQuant_tmp` directories for leased heavy writes; blank configuration uses the model-local fallback. The service enforces one heavy writer per root and uses artifact leases and stale-state checks. Separate directory names do not establish separate physical disks. + +External baseline downloads are durable and managed separately from transient quantization artifacts. Startup cleanup runs before commands and model-specific cleanup runs after model paths are initialized. Keep personal files outside both managed scratch and dedicated export directories. + +## Reproducibility + +Retain the exact command, selected YAML, program commit, llama.cpp revision, model source revision/hash, external repository revision pins, imatrix identity/source, hardware plan, and emitted manifests/benchmark reports for a release. Custom repository `revision` can pin a branch, tag, or commit; a commit avoids moving references. Reusing output does not replace recording these inputs. diff --git a/examples/clone.yaml b/examples/clone.yaml new file mode 100644 index 0000000..42d4c3e --- /dev/null +++ b/examples/clone.yaml @@ -0,0 +1,9 @@ +# Supply --source-json or --source-repo on the command line. +paths: + model_dir: /data/models/my-compatible-model +identity: + architecture_family_name: my-model-family +output: + output_dir: /data/exports/my-cloned-model + output_name_prefix: MyClonedModel + reuse_existing_final_artifacts: true diff --git a/examples/pipeline.yaml b/examples/pipeline.yaml new file mode 100644 index 0000000..65e0031 --- /dev/null +++ b/examples/pipeline.yaml @@ -0,0 +1,12 @@ +# Copy to config.local.yaml and edit. Pass explicitly with --config. +# Omitted settings use C# defaults, not a merge with config.default.yaml. +# For the full distributed tuning profile, copy MagicQuant/config.default.yaml instead. +paths: + model_dir: /data/models/my-model +identity: + architecture_family_name: my-model-family +output: + output_dir: /data/exports/my-model-MagicQuant + output_name_prefix: MyModel +learning: + confirm_tensor_group_profile: true From acd78914dc508d6a3145114dda8baed965ceca0c Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 18:57:06 -0400 Subject: [PATCH 250/258] Keep CLI help detection null-safe --- MagicQuant/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 3a1ef6d..1a6d7d9 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -31,7 +31,7 @@ { // Help is a read-only operation: do not load config, clean caches, install // dependencies, or open databases just to explain a command. - if (parsedArgs.Any(a => a.Name.Equals("help", StringComparison.OrdinalIgnoreCase)) || + if (parsedArgs.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase)) || args.Skip(1).Any(a => a == "-h")) { await commandInfo.Factory().Run([new CliArg { Name = "help", Value = string.Empty }]); From 5541d717c8524a12ed2b467861e2e68a2aca52f4 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 19:31:52 -0400 Subject: [PATCH 251/258] Harden campaign boundaries and add tested native execution and contributor workflows --- .github/workflows/dotnet.yml | 11 +- .github/workflows/model-smoke.yml | 41 ++ CONTRIBUTING.md | 8 +- Directory.Build.props | 5 + MQ.DB/MQ.DB.csproj | 8 +- MQ.DB/Models/DbModels/AiModelHash.cs | 6 +- MQ.DB/Models/LlamaBinaries.cs | 8 +- MQ.DB/Models/TensorGroup.cs | 4 +- MQ.DB/packages.lock.json | 365 ++++++++++++++++++ MagicQuant-Pipeline.sln | 9 + .../MagicQuant.ProcessFixture.csproj | 9 + MagicQuant.ProcessFixture/Program.cs | 40 ++ MagicQuant.ProcessFixture/packages.lock.json | 6 + MagicQuant.Tests/BenchmarkContractTests.cs | 61 +++ MagicQuant.Tests/CliOptionValidationTests.cs | 30 ++ MagicQuant.Tests/CliStartupTests.cs | 24 +- MagicQuant.Tests/ConfigurationReadTests.cs | 46 +++ MagicQuant.Tests/LlamaBinaryPathTests.cs | 25 ++ MagicQuant.Tests/MagicQuant.Tests.csproj | 1 + MagicQuant.Tests/ModelSmokeTests.cs | 131 +++++++ MagicQuant.Tests/NativeConversionTests.cs | 69 ++++ MagicQuant.Tests/PreflightTests.cs | 102 +++++ MagicQuant.Tests/ProcessRunnerTests.cs | 92 +++++ .../QuantizationConcurrencyTests.cs | 21 + MagicQuant.Tests/RunProvenanceTests.cs | 35 ++ MagicQuant.Tests/YamlDiagnosticsTests.cs | 52 +++ MagicQuant.Tests/packages.lock.json | 359 +++++++++++++++++ MagicQuant/Commands/CloneRepositoryQuants.cs | 20 + MagicQuant/Commands/InitializeLlamaCpp.cs | 69 ++-- MagicQuant/Commands/QuantizationPipeline.cs | 22 +- MagicQuant/Commands/ValidatePredictions.cs | 4 + .../Configuration/CliOptionValidator.cs | 125 ++++++ MagicQuant/Configuration/CommandPreflight.cs | 77 ++++ .../ConfigurationShapeValidator.cs | 47 +++ .../Configuration/MagicQuantYamlLoader.cs | 91 +++-- .../YamlConfigurationDiagnostics.cs | 51 +++ MagicQuant/Helpers/DependencyManager.cs | 32 +- MagicQuant/Helpers/LinuxHelper.cs | 19 +- MagicQuant/Helpers/LlamaBuilder.cs | 24 +- MagicQuant/Helpers/PythonManager.cs | 100 ++--- MagicQuant/MagicQuant.csproj | 2 +- MagicQuant/Program.cs | 49 ++- MagicQuant/Runtime/IProcessRunner.cs | 10 + MagicQuant/Runtime/NativeCommand.cs | 20 + MagicQuant/Runtime/ProcessRunner.cs | 84 ++++ MagicQuant/Runtime/RunCancellation.cs | 24 ++ MagicQuant/Services/BenchmarkCommands.cs | 34 ++ MagicQuant/Services/BenchmarkLogParser.cs | 112 ++++++ MagicQuant/Services/BenchmarkService.cs | 285 ++++---------- .../Services/FinalArtifactNamingService.cs | 4 +- MagicQuant/Services/GgufMetadataReader.cs | 2 +- .../Services/HuggingFaceBaselineService.cs | 9 + MagicQuant/Services/ImatrixService.cs | 16 +- .../Services/NativeModelConversionService.cs | 127 ++++++ MagicQuant/Services/PathSafety.cs | 49 +++ .../Services/QuantizationConcurrencyPlan.cs | 16 + MagicQuant/Services/QuantizationService.cs | 340 ++++------------ .../Services/ReadmeGenerationService.cs | 8 +- .../RepositoryCloneManifestService.cs | 5 +- MagicQuant/Services/RunProvenanceService.cs | 98 +++++ MagicQuant/packages.lock.json | 259 +++++++++++++ README.md | 18 +- docs/architecture.md | 10 +- docs/commands.md | 6 +- docs/configuration.md | 14 +- docs/extending.md | 27 ++ docs/migration.md | 10 +- docs/setup.md | 2 +- docs/storage.md | 7 + docs/testing.md | 53 +++ 70 files changed, 3218 insertions(+), 731 deletions(-) create mode 100644 .github/workflows/model-smoke.yml create mode 100644 Directory.Build.props create mode 100644 MQ.DB/packages.lock.json create mode 100644 MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj create mode 100644 MagicQuant.ProcessFixture/Program.cs create mode 100644 MagicQuant.ProcessFixture/packages.lock.json create mode 100644 MagicQuant.Tests/BenchmarkContractTests.cs create mode 100644 MagicQuant.Tests/CliOptionValidationTests.cs create mode 100644 MagicQuant.Tests/ConfigurationReadTests.cs create mode 100644 MagicQuant.Tests/LlamaBinaryPathTests.cs create mode 100644 MagicQuant.Tests/ModelSmokeTests.cs create mode 100644 MagicQuant.Tests/NativeConversionTests.cs create mode 100644 MagicQuant.Tests/PreflightTests.cs create mode 100644 MagicQuant.Tests/ProcessRunnerTests.cs create mode 100644 MagicQuant.Tests/QuantizationConcurrencyTests.cs create mode 100644 MagicQuant.Tests/RunProvenanceTests.cs create mode 100644 MagicQuant.Tests/YamlDiagnosticsTests.cs create mode 100644 MagicQuant.Tests/packages.lock.json create mode 100644 MagicQuant/Configuration/CliOptionValidator.cs create mode 100644 MagicQuant/Configuration/CommandPreflight.cs create mode 100644 MagicQuant/Configuration/ConfigurationShapeValidator.cs create mode 100644 MagicQuant/Configuration/YamlConfigurationDiagnostics.cs create mode 100644 MagicQuant/Runtime/IProcessRunner.cs create mode 100644 MagicQuant/Runtime/NativeCommand.cs create mode 100644 MagicQuant/Runtime/ProcessRunner.cs create mode 100644 MagicQuant/Runtime/RunCancellation.cs create mode 100644 MagicQuant/Services/BenchmarkCommands.cs create mode 100644 MagicQuant/Services/BenchmarkLogParser.cs create mode 100644 MagicQuant/Services/NativeModelConversionService.cs create mode 100644 MagicQuant/Services/PathSafety.cs create mode 100644 MagicQuant/Services/QuantizationConcurrencyPlan.cs create mode 100644 MagicQuant/Services/RunProvenanceService.cs create mode 100644 MagicQuant/packages.lock.json create mode 100644 docs/extending.md create mode 100644 docs/testing.md diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index e1d6b8c..0468c5b 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -7,20 +7,23 @@ permissions: contents: read jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + timeout-minutes: 15 strategy: + fail-fast: false matrix: + os: [ubuntu-latest, windows-latest] configuration: [Debug, Release] steps: - uses: actions/checkout@v4 - uses: actions/setup-dotnet@v4 with: dotnet-version: '10.0.x' - - run: dotnet restore MagicQuant-Pipeline.sln - - run: dotnet build MagicQuant-Pipeline.sln --configuration ${{ matrix.configuration }} --no-restore + - run: dotnet restore MagicQuant-Pipeline.sln --locked-mode -warnaserror + - run: dotnet build MagicQuant-Pipeline.sln --configuration ${{ matrix.configuration }} --no-restore -warnaserror - run: dotnet test MagicQuant-Pipeline.sln --configuration ${{ matrix.configuration }} --no-build --logger trx --results-directory TestResults - uses: actions/upload-artifact@v4 if: always() with: - name: test-results-${{ matrix.configuration }} + name: test-results-${{ matrix.os }}-${{ matrix.configuration }} path: TestResults/*.trx diff --git a/.github/workflows/model-smoke.yml b/.github/workflows/model-smoke.yml new file mode 100644 index 0000000..17833cc --- /dev/null +++ b/.github/workflows/model-smoke.yml @@ -0,0 +1,41 @@ +name: Model smoke (manual) +on: + workflow_dispatch: + inputs: + model_path: + description: Complete source model on the trusted runner + required: true + llama_root: + description: Existing llama.cpp checkout on the trusted runner + required: true + runtime_root: + description: Existing MagicQuant runtime with Python dependencies + required: true + output_root: + description: Dedicated smoke output directory on the trusted runner + required: true +permissions: + contents: read +jobs: + smoke: + # Never trigger self-hosted execution automatically from untrusted pull requests. + runs-on: [self-hosted, magicquant-smoke] + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: '10.0.x' + - run: dotnet restore MagicQuant-Pipeline.sln --locked-mode + - run: dotnet test MagicQuant.Tests -c Release --no-restore --filter Category=ModelSmoke --logger trx --results-directory TestResults + env: + MQ_RUN_MODEL_SMOKE: '1' + MQ_SMOKE_MODEL: ${{ inputs.model_path }} + MQ_SMOKE_LLAMA_ROOT: ${{ inputs.llama_root }} + MQ_SMOKE_RUNTIME_ROOT: ${{ inputs.runtime_root }} + MQ_SMOKE_OUTPUT: ${{ inputs.output_root }} + - uses: actions/upload-artifact@v4 + if: always() + with: + name: smoke-test-results + path: TestResults/*.trx diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 819609c..b80f416 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,14 +5,14 @@ Start with the [architecture map](docs/architecture.md), [configuration rules](d ## Local workflow ```sh -dotnet restore MagicQuant-Pipeline.sln +dotnet restore MagicQuant-Pipeline.sln --locked-mode dotnet build MagicQuant-Pipeline.sln -c Debug --no-restore dotnet test MagicQuant-Pipeline.sln -c Debug --no-build dotnet build MagicQuant-Pipeline.sln -c Release --no-restore dotnet test MagicQuant-Pipeline.sln -c Release --no-build ``` -CI runs both configurations on Linux. Use `--filter FullyQualifiedName~YourTestClass` to focus a test run during development. Tests run serially because configuration and runtime registries are global. Source-contract regression tests assume the normal repository/build layout; run the suite from the checkout rather than copying the test DLL elsewhere. +CI runs both configurations on Linux and Windows with warnings treated as errors and locked package restores. Use `--filter FullyQualifiedName~YourTestClass` to focus a test run during development. Tests run serially because configuration and runtime registries are global. Source-contract regression tests assume the normal repository/build layout; run the suite from the checkout rather than copying the test DLL elsewhere. Keep personal settings in an ignored `config.local.yaml` and pass `--config` explicitly. Do not add machine paths or automatic DEBUG campaigns to `Program.cs`. Use IDE run arguments for your campaign. Never commit weights, runtime databases, exported GGUFs, credentials, or local logs. @@ -38,3 +38,7 @@ For documentation or path refactoring, verify examples against actual help and p Describe the concrete problem and resulting behavior, relevant compatibility effects, and validation performed. Separate numerical policy changes from mechanical cleanup when possible. Mention untested hardware/platform paths and any remaining compiler warnings. Prefer focused commits that can be reviewed without reconstructing the conversation that led to them. The maintainer still needs to choose a software license before an open-source release; do not infer one from generated model metadata or dependency licenses. + +See [testing and merge checks](docs/testing.md) for the manual small-model workflow, +package lock updates, and required-check setup. [Worked examples](docs/extending.md) +show how to add configuration and test native/process/path changes. diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..f860221 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,5 @@ + + + true + + diff --git a/MQ.DB/MQ.DB.csproj b/MQ.DB/MQ.DB.csproj index a1e614b..dbbc1aa 100644 --- a/MQ.DB/MQ.DB.csproj +++ b/MQ.DB/MQ.DB.csproj @@ -7,13 +7,13 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/MQ.DB/Models/DbModels/AiModelHash.cs b/MQ.DB/Models/DbModels/AiModelHash.cs index 20a1f9c..2f6e1f4 100644 --- a/MQ.DB/Models/DbModels/AiModelHash.cs +++ b/MQ.DB/Models/DbModels/AiModelHash.cs @@ -13,11 +13,11 @@ namespace MQ.DB.Models.DbModels; public class AiModelHash : ISQLiteEntity { public uint Id { get; set; } - public string UniqueHash { get; set; } - + public string UniqueHash { get; set; } = null!; // Required; assigned by EF or model registration. + public void Configure(EntityTypeBuilder builder) { builder.HasKey(x => x.Id); builder.HasIndex(h => h.UniqueHash); } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/LlamaBinaries.cs b/MQ.DB/Models/LlamaBinaries.cs index f034d7b..e1a8a36 100644 --- a/MQ.DB/Models/LlamaBinaries.cs +++ b/MQ.DB/Models/LlamaBinaries.cs @@ -8,9 +8,11 @@ public class LlamaBinaries public string Ppl { get; } public string Cli { get; } - public LlamaBinaries(string root) + public LlamaBinaries(string? root) { - var binDir = Cache.LlamaBin; + var binDir = !string.IsNullOrWhiteSpace(Cache.LlamaBin) ? Cache.LlamaBin + : !string.IsNullOrWhiteSpace(root) ? Path.Combine(root, "build", "bin") + : throw new InvalidOperationException("Set the llama.cpp binary directory before constructing LlamaBinaries."); Bench = Path.Combine(binDir, "llama-bench"); Ppl = Path.Combine(binDir, "llama-perplexity"); Cli = Path.Combine(binDir, "llama-cli"); @@ -32,4 +34,4 @@ public void Validate() if (missing.Any()) throw new FileNotFoundException($"Missing llama.cpp binaries:\n{string.Join("\n", missing)}"); } -} \ No newline at end of file +} diff --git a/MQ.DB/Models/TensorGroup.cs b/MQ.DB/Models/TensorGroup.cs index 06f81dd..f5814a8 100644 --- a/MQ.DB/Models/TensorGroup.cs +++ b/MQ.DB/Models/TensorGroup.cs @@ -7,7 +7,7 @@ namespace MQ.DB.Models; public class TensorGroupInfo { - public TensorGroup Group { get; set; } + public TensorGroup Group { get; set; } = null!; } /// @@ -408,4 +408,4 @@ private sealed class BaseQuantExceptionYamlDefinition public List Patterns { get; set; } = []; } -} \ No newline at end of file +} diff --git a/MQ.DB/packages.lock.json b/MQ.DB/packages.lock.json new file mode 100644 index 0000000..dba06cc --- /dev/null +++ b/MQ.DB/packages.lock.json @@ -0,0 +1,365 @@ +{ + "version": 1, + "dependencies": { + "net10.0": { + "Microsoft.Data.Sqlite": { + "type": "Direct", + "requested": "[10.0.11, )", + "resolved": "10.0.11", + "contentHash": "7je7UELzm131GiLYc4PpZvfKXIgIyzPM+v+tjcd/nbnuWRfgcONYKzDTqJlURxwVCFsVnlpmq6y6yn4qvR8QXQ==", + "dependencies": { + "Microsoft.Data.Sqlite.Core": "10.0.11", + "SQLitePCLRaw.bundle_e_sqlite3": "2.1.12", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.EntityFrameworkCore": { + "type": "Direct", + "requested": "[10.0.11, )", + "resolved": "10.0.11", + "contentHash": "VOSGU8en6HZJs8t7UMFN+9vGcRgVOOn6fA44Ngcg2NyvJ3P1KE94iAb0XzaVaGhXGtt+qaM/VtEn0/hzluQJeg==", + "dependencies": { + "Microsoft.EntityFrameworkCore.Abstractions": "10.0.11", + "Microsoft.EntityFrameworkCore.Analyzers": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11" + } + }, + "Microsoft.EntityFrameworkCore.Design": { + "type": "Direct", + "requested": "[10.0.11, )", + "resolved": "10.0.11", + "contentHash": "0zlzPs/jtrp2jGNZSxHLd0bRgDB/TlCDT17pnt8hovTVgCmC6qbX1277/goAlnZbRip1mZp1TVjjG+tj9PQ/Uw==", + "dependencies": { + "Humanizer.Core": "2.14.1", + "Microsoft.Build.Framework": "18.0.2", + "Microsoft.CodeAnalysis.CSharp": "5.0.0", + "Microsoft.CodeAnalysis.CSharp.Workspaces": "5.0.0", + "Microsoft.CodeAnalysis.Workspaces.MSBuild": "5.0.0", + "Microsoft.EntityFrameworkCore.Relational": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyModel": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11", + "Mono.TextTemplating": "3.0.0", + "Newtonsoft.Json": "13.0.4" + } + }, + "Microsoft.EntityFrameworkCore.Sqlite": { + "type": "Direct", + "requested": "[10.0.11, )", + "resolved": "10.0.11", + "contentHash": "jc7iVrhQyInR3loraMESfEFaFOtQOB1mRKHjX6QYC9o7YDbfMNbAPnIwlpffnFwhXd6/27FKaaV+sWSoLd4F1g==", + "dependencies": { + "Microsoft.EntityFrameworkCore.Sqlite.Core": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyModel": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11", + "SQLitePCLRaw.bundle_e_sqlite3": "2.1.12", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "YamlDotNet": { + "type": "Direct", + "requested": "[17.0.1, )", + "resolved": "17.0.1", + "contentHash": "qVir5fehR/W5nTJyoJUibypETXaW4iRAF9cQa0FQIC9TJ3VC0qDOwm4o/RxANewj8KzPF8WMF2abBfUgi6LC4w==" + }, + "Humanizer.Core": { + "type": "Transitive", + "resolved": "2.14.1", + "contentHash": "lQKvtaTDOXnoVJ20ibTuSIOf2i0uO0MPbDhd1jm238I+U/2ZnRENj0cktKZhtchBMtCUSRQ5v4xBCUbKNmyVMw==" + }, + "Microsoft.Build.Framework": { + "type": "Transitive", + "resolved": "18.0.2", + "contentHash": "sOSb+0J4G/jCBW/YqmRuL0eOMXgfw1KQLdC9TkbvfA5xs7uNm+PBQXJCOzSJGXtZcZrtXozcwxPmUiRUbmd7FA==" + }, + "Microsoft.CodeAnalysis.Analyzers": { + "type": "Transitive", + "resolved": "3.11.0", + "contentHash": "v/EW3UE8/lbEYHoC2Qq7AR/DnmvpgdtAMndfQNmpuIMx/Mto8L5JnuCfdBYtgvalQOtfNCnxFejxuRrryvUTsg==" + }, + "Microsoft.CodeAnalysis.Common": { + "type": "Transitive", + "resolved": "5.0.0", + "contentHash": "ZXRAdvH6GiDeHRyd3q/km8Z44RoM6FBWHd+gen/la81mVnAdHTEsEkO5J0TCNXBymAcx5UYKt5TvgKBhaLJEow==", + "dependencies": { + "Microsoft.CodeAnalysis.Analyzers": "3.11.0" + } + }, + "Microsoft.CodeAnalysis.CSharp": { + "type": "Transitive", + "resolved": "5.0.0", + "contentHash": "5DSyJ9bk+ATuDy7fp2Zt0mJStDVKbBoiz1DyfAwSa+k4H4IwykAUcV3URelw5b8/iVbfSaOwkwmPUZH6opZKCw==", + "dependencies": { + "Microsoft.CodeAnalysis.Analyzers": "3.11.0", + "Microsoft.CodeAnalysis.Common": "[5.0.0]" + } + }, + "Microsoft.CodeAnalysis.CSharp.Workspaces": { + "type": "Transitive", + "resolved": "5.0.0", + "contentHash": "Al/Q8B+yO8odSqGVpSvrShMFDvlQdIBU//F3E6Rb0YdiLSALE9wh/pvozPNnfmh5HDnvU+mkmSjpz4hQO++jaA==", + "dependencies": { + "Humanizer.Core": "2.14.1", + "Microsoft.CodeAnalysis.Analyzers": "3.11.0", + "Microsoft.CodeAnalysis.CSharp": "[5.0.0]", + "Microsoft.CodeAnalysis.Common": "[5.0.0]", + "Microsoft.CodeAnalysis.Workspaces.Common": "[5.0.0]", + "System.Composition": "9.0.0" + } + }, + "Microsoft.CodeAnalysis.Workspaces.Common": { + "type": "Transitive", + "resolved": "5.0.0", + "contentHash": "ZbUmIvT6lqTNKiv06Jl5wf0MTMi1vQ1oH7ou4CLcs2C/no/L7EhP3T8y3XXvn9VbqMcJaJnEsNA1jwYUMgc5jg==", + "dependencies": { + "Humanizer.Core": "2.14.1", + "Microsoft.CodeAnalysis.Analyzers": "3.11.0", + "Microsoft.CodeAnalysis.Common": "[5.0.0]", + "System.Composition": "9.0.0" + } + }, + "Microsoft.CodeAnalysis.Workspaces.MSBuild": { + "type": "Transitive", + "resolved": "5.0.0", + "contentHash": "/G+LVoAGMz6Ae8nm+PGLxSw+F5RjYx/J7irbTO5uKAPw1bxHyQJLc/YOnpDxt+EpPtYxvC9wvBsg/kETZp1F9Q==", + "dependencies": { + "Humanizer.Core": "2.14.1", + "Microsoft.Build.Framework": "17.11.31", + "Microsoft.CodeAnalysis.Analyzers": "3.11.0", + "Microsoft.CodeAnalysis.Workspaces.Common": "[5.0.0]", + "Microsoft.Extensions.DependencyInjection": "9.0.0", + "Microsoft.Extensions.Logging": "9.0.0", + "Microsoft.Extensions.Logging.Abstractions": "9.0.0", + "Microsoft.Extensions.Options": "9.0.0", + "Microsoft.Extensions.Primitives": "9.0.0", + "Microsoft.VisualStudio.SolutionPersistence": "1.0.52", + "Newtonsoft.Json": "13.0.3", + "System.Composition": "9.0.0" + } + }, + "Microsoft.Data.Sqlite.Core": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "hubA20AGenQ4Sx0ElWaPpB8DISjXpdx463+1zOGRslsT0e/t/06ITv+pHsop8CcJ0d8PZLfgnT7juCDVD79Dkw==", + "dependencies": { + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.EntityFrameworkCore.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "6auJR+9+9VunznKfH7WGrHMrnrmA0F7JZ22EXzwXvVhjfnbu9Xq7NSIWaOf3KJsOanM2qf5ajJ2JR5TlcPZTLA==" + }, + "Microsoft.EntityFrameworkCore.Analyzers": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "Bv7X4wSSnzCQED9WYXKJ8fwgyvKwf0xZM1GO8xkf6CF9zl+UBnvjxmcPnokJRy0JKjc1SlHSzzhx1HcL4jitTQ==" + }, + "Microsoft.EntityFrameworkCore.Relational": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "grznnTJgEYxaWpdKAsTzg6j+89jHgCXWYp+QGtlX5O92+w/VuhWM6JLPYb+uw8M9VhGUvOTsO76dYOy9vNPd5Q==", + "dependencies": { + "Microsoft.EntityFrameworkCore": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11" + } + }, + "Microsoft.EntityFrameworkCore.Sqlite.Core": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "ywTQKt32xnVhCzjEQAqFufpEyXkOUfvW/EC/s4xnS8Xaor2xXE+TMUyzhgACqXtZEU5IR95y94RDzHto55Fx7w==", + "dependencies": { + "Microsoft.Data.Sqlite.Core": "10.0.11", + "Microsoft.EntityFrameworkCore.Relational": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyModel": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.Extensions.Caching.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "vUl798SmruTqqlt/xH2gDk3tJlhk6k3HdOXAHirlRfbNKDym4g/kRpUL9S4sl6F6FsOTOMW+ZsDapqlZMOOiEw==", + "dependencies": { + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Caching.Memory": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "el1g0mBEbDBGY2bT9mcSfrTWO8QlPdq2nOCnvQugioOFwHV+bVBMeiakoI0dNOdj8d6Hi9K6HY2xzRUWJiDR3w==", + "dependencies": { + "Microsoft.Extensions.Caching.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11", + "Microsoft.Extensions.Logging.Abstractions": "10.0.11", + "Microsoft.Extensions.Options": "10.0.11", + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Configuration.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "fVi053xdpda9Em7vSkmgVxO/PtgC2m78ekReKWsgcyskqY0U82Bz/MONwxpGzI0hElYKJfw+fupqMVeKW3fSaA==", + "dependencies": { + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.DependencyInjection": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "PSmotV19c7E3lKed++uYo1kSiXFI+uTl37CBSrhq+CfLC3FCHjG7R91+xPnNehQfHS1b0Tzo/CCLPWH3qaEheg==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11" + } + }, + "Microsoft.Extensions.DependencyInjection.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "/a1aJz4m7ylhEDf25ugQChLQoN5XwoGjWw/BoR/ZWWKsO1v4DdJElS1uyngahz4B/eOzjFk1KNTkarRLE5wsIg==" + }, + "Microsoft.Extensions.DependencyModel": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "PJPtFYsZ+r+uz9qqXWUTEyKeJ1EiBGIJtqavkg9ZXijjGSFAk4Fgi5sqIxj+uAyLZwEKgexDUQXhWhvU6l3+og==" + }, + "Microsoft.Extensions.Logging": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "nUOJwgFkSiLHiVGFpU22pIJtuWYewuSYQ3JVuP/gdK8ASMT807Px+TYQiRWs6uSsOmoyFTaVCwKXTasczV6BpA==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection": "10.0.11", + "Microsoft.Extensions.Logging.Abstractions": "10.0.11", + "Microsoft.Extensions.Options": "10.0.11" + } + }, + "Microsoft.Extensions.Logging.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "Ljd0Uxoq5XpScD2Bg0nM/r3mwx7Ao5Uq24eo2ARxbGvqJ7Zht6rt2cJtwVRH4Cv+1ZVMdXz6TB43KbpmsxRrvQ==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11" + } + }, + "Microsoft.Extensions.Options": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "eY1GAKcTfD2maP27J84X9IovT3yjHJ2dVDzPmDg6/XqYvt3jMzJhtfQCLjG9pVsZGAd+8DQ2QrjaDcs2+VQLGw==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11", + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Primitives": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "SXcz+kF+4Oo9b1+55zntpJFYfwb1jw66ioxptyNOOTDc8g2FHnBFWjZpsWfCvZIhzr0x+4e2trVTs4OKwQfBtw==" + }, + "Microsoft.VisualStudio.SolutionPersistence": { + "type": "Transitive", + "resolved": "1.0.52", + "contentHash": "oNv2JtYXhpdJrX63nibx1JT3uCESOBQ1LAk7Dtz/sr0+laW0KRM6eKp4CZ3MHDR2siIkKsY8MmUkeP5DKkQQ5w==" + }, + "Mono.TextTemplating": { + "type": "Transitive", + "resolved": "3.0.0", + "contentHash": "YqueG52R/Xej4VVbKuRIodjiAhV0HR/XVbLbNrJhCZnzjnSjgMJ/dCdV0akQQxavX6hp/LC6rqLGLcXeQYU7XA==", + "dependencies": { + "System.CodeDom": "6.0.0" + } + }, + "Newtonsoft.Json": { + "type": "Transitive", + "resolved": "13.0.4", + "contentHash": "pdgNNMai3zv51W5aq268sujXUyx7SNdE2bj1wZcWjAQrKMFZV260lbqYop1d2GM67JI1huLRwxo9ZqnfF/lC6A==" + }, + "SQLitePCLRaw.bundle_e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "mAgscpQMLw5/nfA1Q5oJVAT29yROUo1ifZGbbTpx/lwZpSxMUGoYbKfmvdm8oXER+RzxqBmmQzeBEVKfeHv2nw==", + "dependencies": { + "SQLitePCLRaw.lib.e_sqlite3": "2.1.12", + "SQLitePCLRaw.provider.e_sqlite3": "2.1.12" + } + }, + "SQLitePCLRaw.core": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "ETpNw9DY3ckWLgRRAeCHj+GKOuPi61aeczkXhgHexUvqoZBAYg8RYESE2J7O1M7+o6QbdSEZwrw9bfqztUVWXg==" + }, + "SQLitePCLRaw.lib.e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "fWi8Dbknuhgg72fWinIdjXVaqO1hHL4YBBwVLnr7e1c9TAZwJ0QE38j9syW1hwx6HaqEVTwI+O07WPdZn8Rp0w==" + }, + "SQLitePCLRaw.provider.e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "W3oH4XIfCzFrgUSDKHhN6N+dgzA5YHOR2VxX8GB6Qy7CyrJJgxPEG8NirgYWlPQC5P2jz2knSsexWu4tDUL33g==", + "dependencies": { + "SQLitePCLRaw.core": "2.1.12" + } + }, + "System.CodeDom": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "CPc6tWO1LAer3IzfZufDBRL+UZQcj5uS207NHALQzP84Vp/z6wF0Aa0YZImOQY8iStY0A2zI/e3ihKNPfUm8XA==" + }, + "System.Composition": { + "type": "Transitive", + "resolved": "9.0.0", + "contentHash": "3Djj70fFTraOarSKmRnmRy/zm4YurICm+kiCtI0dYRqGJnLX6nJ+G3WYuFJ173cAPax/gh96REcbNiVqcrypFQ==", + "dependencies": { + "System.Composition.AttributedModel": "9.0.0", + "System.Composition.Convention": "9.0.0", + "System.Composition.Hosting": "9.0.0", + "System.Composition.Runtime": "9.0.0", + "System.Composition.TypedParts": "9.0.0" + } + }, + "System.Composition.AttributedModel": { + "type": "Transitive", + "resolved": "9.0.0", + "contentHash": "iri00l/zIX9g4lHMY+Nz0qV1n40+jFYAmgsaiNn16xvt2RDwlqByNG4wgblagnDYxm3YSQQ0jLlC/7Xlk9CzyA==" + }, + "System.Composition.Convention": { + "type": "Transitive", + "resolved": "9.0.0", + "contentHash": "+vuqVP6xpi582XIjJi6OCsIxuoTZfR0M7WWufk3uGDeCl3wGW6KnpylUJ3iiXdPByPE0vR5TjJgR6hDLez4FQg==", + "dependencies": { + "System.Composition.AttributedModel": "9.0.0" + } + }, + "System.Composition.Hosting": { + "type": "Transitive", + "resolved": "9.0.0", + "contentHash": "OFqSeFeJYr7kHxDfaViGM1ymk7d4JxK//VSoNF9Ux0gpqkLsauDZpu89kTHHNdCWfSljbFcvAafGyBoY094btQ==", + "dependencies": { + "System.Composition.Runtime": "9.0.0" + } + }, + "System.Composition.Runtime": { + "type": "Transitive", + "resolved": "9.0.0", + "contentHash": "w1HOlQY1zsOWYussjFGZCEYF2UZXgvoYnS94NIu2CBnAGMbXFAX8PY8c92KwUItPmowal68jnVLBCzdrWLeEKA==" + }, + "System.Composition.TypedParts": { + "type": "Transitive", + "resolved": "9.0.0", + "contentHash": "aRZlojCCGEHDKqh43jaDgaVpYETsgd7Nx4g1zwLKMtv4iTo0627715ajEFNpEEBTgLmvZuv8K0EVxc3sM4NWJA==", + "dependencies": { + "System.Composition.AttributedModel": "9.0.0", + "System.Composition.Hosting": "9.0.0", + "System.Composition.Runtime": "9.0.0" + } + } + } + } +} \ No newline at end of file diff --git a/MagicQuant-Pipeline.sln b/MagicQuant-Pipeline.sln index b574b7d..a76ed7f 100644 --- a/MagicQuant-Pipeline.sln +++ b/MagicQuant-Pipeline.sln @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQ.DB", "MQ.DB\MQ.DB.csproj EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.Tests", "MagicQuant.Tests\MagicQuant.Tests.csproj", "{D106FC82-5FD7-4C95-BF20-0940C64A234C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.ProcessFixture", "MagicQuant.ProcessFixture\MagicQuant.ProcessFixture.csproj", "{6F77FCF7-A105-44A9-A708-2B9F9F2B3B6D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +26,12 @@ Global {D106FC82-5FD7-4C95-BF20-0940C64A234C}.Debug|Any CPU.Build.0 = Debug|Any CPU {D106FC82-5FD7-4C95-BF20-0940C64A234C}.Release|Any CPU.ActiveCfg = Release|Any CPU {D106FC82-5FD7-4C95-BF20-0940C64A234C}.Release|Any CPU.Build.0 = Release|Any CPU + {6F77FCF7-A105-44A9-A708-2B9F9F2B3B6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6F77FCF7-A105-44A9-A708-2B9F9F2B3B6D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6F77FCF7-A105-44A9-A708-2B9F9F2B3B6D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6F77FCF7-A105-44A9-A708-2B9F9F2B3B6D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE EndGlobalSection EndGlobal diff --git a/MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj b/MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj new file mode 100644 index 0000000..0fda630 --- /dev/null +++ b/MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj @@ -0,0 +1,9 @@ + + + Exe + net10.0 + enable + enable + false + + diff --git a/MagicQuant.ProcessFixture/Program.cs b/MagicQuant.ProcessFixture/Program.cs new file mode 100644 index 0000000..bd451b0 --- /dev/null +++ b/MagicQuant.ProcessFixture/Program.cs @@ -0,0 +1,40 @@ +using System.Diagnostics; + +namespace MagicQuant.ProcessFixture; + +/// Offline child-process fixture. Used only by process lifetime regression tests. +public static class Program +{ + public static async Task Main(string[] args) + { + switch (args[0]) + { + case "echo": + foreach (string arg in args.Skip(1)) Console.WriteLine(arg); + return 0; + case "flood": + for (int i = 0; i < 12000; i++) + { + Console.WriteLine($"stdout-{i:D5}"); + Console.Error.WriteLine($"stderr-{i:D5}"); + } + return 7; + case "tree": + var start = new ProcessStartInfo("dotnet"); + start.ArgumentList.Add(typeof(Program).Assembly.Location); + start.ArgumentList.Add("wait"); + using (var child = Process.Start(start)!) + { + Console.WriteLine($"child:{child.Id}"); + await Task.Delay(TimeSpan.FromMinutes(5)); + } + return 0; + case "wait": + Console.WriteLine($"ready:{Environment.ProcessId}"); + await Task.Delay(TimeSpan.FromMinutes(5)); + return 0; + default: + return 2; + } + } +} diff --git a/MagicQuant.ProcessFixture/packages.lock.json b/MagicQuant.ProcessFixture/packages.lock.json new file mode 100644 index 0000000..4a91a8c --- /dev/null +++ b/MagicQuant.ProcessFixture/packages.lock.json @@ -0,0 +1,6 @@ +{ + "version": 1, + "dependencies": { + "net10.0": {} + } +} \ No newline at end of file diff --git a/MagicQuant.Tests/BenchmarkContractTests.cs b/MagicQuant.Tests/BenchmarkContractTests.cs new file mode 100644 index 0000000..401eb0c --- /dev/null +++ b/MagicQuant.Tests/BenchmarkContractTests.cs @@ -0,0 +1,61 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class BenchmarkContractTests +{ + [Fact] + public void Cpu_and_gpu_benchmark_arguments_preserve_runtime_policy() + { + var cpu = BenchmarkCommands.Bench("bench", "/models/a b.gguf", false, 99, ""); + Assert.Equal(["-m", "/models/a b.gguf", "-p", "8", "-t", "16", "-ngl", "0", "-o", "md"], cpu.Arguments); + var gpu = BenchmarkCommands.Perplexity("ppl", "model", "corpus", true, 32, " --tensor-split 19,23", "logits", true); + Assert.Equal(["-m", "model", "-ngl", "32", "--tensor-split", "19,23", "-t", "4", "-c", "2048", "--file", "corpus", "--kl-divergence-base", "logits", "--kl-divergence"], gpu.Arguments); + Assert.DoesNotContain("--kl-divergence", BenchmarkCommands.Perplexity("ppl", "model", "corpus", false, 32, "", "logits", false).Arguments); + } + + [Theory] + [InlineData("PPL = 12.5 +/- 0.2\nMean KLD: 1.2e-3", 12.5, 0.0012)] + [InlineData("\u001b[32mMean PPL(Q) : 8.1 ± 0.1\u001b[0m\nKL-divergence = 0.02", 8.1, 0.02)] + public void Parses_plain_and_ansi_scientific_notation_logs(string content, double ppl, double kld) + { + WithLog(content, path => + { + var metrics = BenchmarkLogParser.ParsePerplexity(path, false); + Assert.Equal(ppl, metrics.Ppl); + Assert.Equal(kld, metrics.Kld); + }); + } + + [Fact] + public void Missing_kld_is_allowed_only_for_native_reference_logs() + { + WithLog("PPL = 12.5 +/- 0.2", path => + { + Assert.Null(BenchmarkLogParser.ParsePerplexity(path, true).Kld); + Assert.Throws(() => BenchmarkLogParser.ParsePerplexity(path, false)); + }); + WithLog("process failed before measurement", path => + Assert.Throws(() => BenchmarkLogParser.ParsePerplexity(path, true))); + } + + [Fact] + public void Parses_benchmark_table_by_column_name() + { + WithLog("| test | backend | t/s | ngl |\n|---|---|---|---|\n| pp8 | CPU | 123.45 ± 0.1 | 0 |", path => + { + var metrics = BenchmarkLogParser.ParseLlamaBench(path); + Assert.Equal(123.45, metrics.Tps); + Assert.Equal("CPU", metrics.Backend); + Assert.Equal("pp8", metrics.Test); + }); + } + + private static void WithLog(string text, Action test) + { + string path = Path.GetTempFileName(); + try { File.WriteAllText(path, text); test(path); } + finally { File.Delete(path); } + } +} diff --git a/MagicQuant.Tests/CliOptionValidationTests.cs b/MagicQuant.Tests/CliOptionValidationTests.cs new file mode 100644 index 0000000..0cb3236 --- /dev/null +++ b/MagicQuant.Tests/CliOptionValidationTests.cs @@ -0,0 +1,30 @@ +using MagicQuant.Configuration; +using MagicQuant.Models; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class CliOptionValidationTests +{ + [Theory] + [InlineData("modle-dir", "/model")] + [InlineData("model-dir", "")] + [InlineData("use-imatrix", "false")] + [InlineData("config", "")] + [InlineData("prediction-minimum-fit-rows", "one")] + [InlineData("prediction-minimum-fit-rows", "1")] + [InlineData("prediction-default-bit-stress-threshold", "NaN")] + [InlineData("selection-interior-window-fractions", "0.3,broken")] + [InlineData("selection-diversify-validation-candidates", "maybe")] + public void Rejects_typos_missing_values_and_ambiguous_flags(string name, string value) + { + Assert.Throws(() => CliOptionValidator.Validate([new CliArg { Name = name, Value = value }])); + } + + [Fact] + public void Duplicate_options_are_not_silently_resolved_by_order() + { + Assert.Throws(() => CliOptionValidator.Validate( + [new CliArg { Name = "model-dir", Value = "one" }, new CliArg { Name = "MODEL-DIR", Value = "two" }])); + } +} diff --git a/MagicQuant.Tests/CliStartupTests.cs b/MagicQuant.Tests/CliStartupTests.cs index 3501b14..fab8788 100644 --- a/MagicQuant.Tests/CliStartupTests.cs +++ b/MagicQuant.Tests/CliStartupTests.cs @@ -60,10 +60,32 @@ public void Historical_alias_uses_the_same_pipeline_implementation() Assert.IsAssignableFrom(new Evolution()); } - private static async Task<(int ExitCode, string Output, string[] CreatedFiles)> RunAsync(string[] args) + [Fact] + public async Task Invalid_model_fails_before_config_application_or_dependency_setup() + { + var result = await RunAsync(["pipeline", "--config", "bad.yaml"], directory => + File.WriteAllText(Path.Combine(directory, "bad.yaml"), "paths:\n magic_quant_root: runtime-must-not-exist\n model_dir: missing-model\n")); + Assert.Equal(1, result.ExitCode); + Assert.DoesNotContain("Using config:", result.Output); + Assert.DoesNotContain("Checking environment", result.Output); + Assert.Single(result.CreatedFiles); + } + + [Fact] + public async Task Check_config_does_not_initialize_or_clean_runtime_state() + { + var result = await RunAsync(["initialize-llama-cpp", "--config", "check.yaml", "--check-config", "--strict-config"], directory => + File.WriteAllText(Path.Combine(directory, "check.yaml"), "paths:\n magic_quant_root: runtime-must-not-exist\n")); + Assert.Equal(0, result.ExitCode); + Assert.Contains("No runtime setup was performed", result.Output); + Assert.Single(result.CreatedFiles); + } + + private static async Task<(int ExitCode, string Output, string[] CreatedFiles)> RunAsync(string[] args, Action? setup = null) { string directory = Path.Combine(Path.GetTempPath(), $"mq-cli-{Guid.NewGuid():N}"); Directory.CreateDirectory(directory); + setup?.Invoke(directory); try { var start = new ProcessStartInfo("dotnet") diff --git a/MagicQuant.Tests/ConfigurationReadTests.cs b/MagicQuant.Tests/ConfigurationReadTests.cs new file mode 100644 index 0000000..4023973 --- /dev/null +++ b/MagicQuant.Tests/ConfigurationReadTests.cs @@ -0,0 +1,46 @@ +using System.Globalization; +using MagicQuant.Configuration; +using MagicQuant.Models; +using MQ.DB; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class ConfigurationReadTests +{ + [Fact] + public void Read_does_not_mutate_runtime_state_and_cli_decimal_is_culture_independent() + { + string file = Path.GetTempFileName(); + var oldCulture = CultureInfo.CurrentCulture; + string? oldRoot = Cache.MagicQuantDirectory; + var oldConfig = Config.Current; + try + { + File.WriteAllText(file, "prediction:\n default_bit_stress_threshold: 5.0\n"); + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE"); + var loaded = MagicQuantYamlLoader.Read([ + new CliArg { Name = "config", Value = file }, + new CliArg { Name = "prediction-default-bit-stress-threshold", Value = "7.5" } + ]); + Assert.Equal(7.5, loaded.Settings.Prediction.DefaultBitStressThreshold); + Assert.Same(oldConfig, Config.Current); + Assert.Equal(oldRoot, Cache.MagicQuantDirectory); + } + finally { CultureInfo.CurrentCulture = oldCulture; File.Delete(file); } + } + + [Fact] + public void Strict_config_rejects_typos_that_normal_mode_reports() + { + string file = Path.GetTempFileName(); + try + { + File.WriteAllText(file, "paths:\n modle_dir: /model\n"); + var arg = new CliArg { Name = "config", Value = file }; + Assert.Single(MagicQuantYamlLoader.Read([arg]).Warnings); + Assert.Throws(() => MagicQuantYamlLoader.Read([arg, new CliArg { Name = "strict-config", Value = "" }])); + } + finally { File.Delete(file); } + } +} diff --git a/MagicQuant.Tests/LlamaBinaryPathTests.cs b/MagicQuant.Tests/LlamaBinaryPathTests.cs new file mode 100644 index 0000000..8b1e9e0 --- /dev/null +++ b/MagicQuant.Tests/LlamaBinaryPathTests.cs @@ -0,0 +1,25 @@ +using MQ.DB; +using MQ.DB.Models; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class LlamaBinaryPathTests +{ + [Fact] + public void Explicit_binary_directory_wins_and_checkout_root_is_a_real_fallback() + { + string? old = Cache.LlamaBin; + try + { + string suffix = OperatingSystem.IsWindows() ? ".exe" : ""; + Cache.LlamaBin = Path.Combine(Path.GetTempPath(), "custom bin"); + Assert.Equal(Path.Combine(Cache.LlamaBin, "llama-bench" + suffix), new LlamaBinaries("ignored").Bench); + Cache.LlamaBin = null; + string root = Path.Combine(Path.GetTempPath(), "llama"); + Assert.Equal(Path.Combine(root, "build", "bin", "llama-cli" + suffix), new LlamaBinaries(root).Cli); + Assert.Throws(() => new LlamaBinaries(null)); + } + finally { Cache.LlamaBin = old; } + } +} diff --git a/MagicQuant.Tests/MagicQuant.Tests.csproj b/MagicQuant.Tests/MagicQuant.Tests.csproj index 6677456..5e66388 100644 --- a/MagicQuant.Tests/MagicQuant.Tests.csproj +++ b/MagicQuant.Tests/MagicQuant.Tests.csproj @@ -14,5 +14,6 @@ + diff --git a/MagicQuant.Tests/ModelSmokeTests.cs b/MagicQuant.Tests/ModelSmokeTests.cs new file mode 100644 index 0000000..3f9b0a6 --- /dev/null +++ b/MagicQuant.Tests/ModelSmokeTests.cs @@ -0,0 +1,131 @@ +using System.Diagnostics; +using System.Text.Json; +using MagicQuant.Configuration; +using MagicQuant.Helpers; +using MagicQuant.Runtime; +using MagicQuant.Services; +using MQ.DB; +using MQ.DB.Models; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class ModelSmokeFactAttribute : FactAttribute +{ + public ModelSmokeFactAttribute() + { + if (Environment.GetEnvironmentVariable("MQ_RUN_MODEL_SMOKE") != "1") + Skip = "Opt in with MQ_RUN_MODEL_SMOKE=1 and the documented model/toolchain paths."; + } +} + +/// Small-model integration, isolated from ordinary PR checks and existing campaign state. +public sealed class ModelSmokeTests +{ + [ModelSmokeFact] + [Trait("Category", "ModelSmoke")] + public async Task Convert_quantize_read_metadata_benchmark_and_reuse_native_artifact() + { + string Required(string key) => Environment.GetEnvironmentVariable(key) + ?? throw new InvalidOperationException($"Set {key}; see docs/testing.md."); + string source = Path.GetFullPath(Required("MQ_SMOKE_MODEL")); + string llama = Path.GetFullPath(Required("MQ_SMOKE_LLAMA_ROOT")); + string runtime = Path.GetFullPath(Required("MQ_SMOKE_RUNTIME_ROOT")); + string output = Path.GetFullPath(Required("MQ_SMOKE_OUTPUT")); + string root = Path.Combine(output, $"smoke-{Guid.NewGuid():N}"); + string model = Path.Combine(root, "model with spaces"); + Assert.True(Directory.Exists(source)); + Directory.CreateDirectory(model); + // Inputs are copied/linked into a new directory; no source-model files are modified. + foreach (string file in Directory.EnumerateFiles(source)) + { + string destination = Path.Combine(model, Path.GetFileName(file)); + if (file.EndsWith(".safetensors", StringComparison.Ordinal)) File.CreateSymbolicLink(destination, file); + else File.Copy(file, destination); + } + + var oldConfig = Config.Current; + var oldPaths = (Cache.ModelDirectory, Cache.ModelMagicQuantDirectory, Cache.MagicQuantDirectory, + Cache.LlamaRoot, Cache.LlamaBin, Cache.ConvertScript, Cache.CurrentModelId); + var oldPrecision = Cache.TorchType; + var oldScratch = Cache.ScratchRoots; + var oldImatrix = (Cache.UseImatrix, Cache.IsImatrixAvailable); + try + { + using var timeout = new CancellationTokenSource(TimeSpan.FromMinutes(20)); + using var scope = RunCancellation.Use(timeout.Token); + Config.Load(MagicQuantYamlConfig.CreateDefault()); + Cache.ModelDirectory = model; + Cache.ModelMagicQuantDirectory = Path.Combine(model, "MagicQuant"); + Cache.MagicQuantDirectory = runtime; + Cache.LlamaRoot = llama; + Cache.LlamaBin = Path.Combine(llama, "build", "bin"); + Cache.ConvertScript = Path.Combine(llama, "convert_hf_to_gguf.py"); + Cache.CurrentModelId = "isolated-smoke"; + Cache.ScratchRoots = [root]; + Cache.UseImatrix = false; + Cache.IsImatrixAvailable = false; + JsonHelper.DetectAndSetTorchType(model); + var python = new PythonManager(runtime); + var quantizer = new QuantizationService(new BenchmarkService(python)); + string native = await quantizer.EnsureBaseModelFileAsync(); + DateTime nativeTimestamp = File.GetLastWriteTimeUtc(native); + Assert.Equal(native, await quantizer.EnsureBaseModelFileAsync()); + Assert.Equal(nativeTimestamp, File.GetLastWriteTimeUtc(native)); + + string export = Path.Combine(root, "export"); + Directory.CreateDirectory(export); + string q8 = Path.Combine(export, "smoke Q8_0.gguf"); + string scratch; + await using (var lease = await quantizer.BuildPureQ8ProbeLeaseAsync(timeout.Token)) + { + scratch = lease.GgufPath; + Assert.True(new FileInfo(scratch).Length > 0); + + } + Assert.False(File.Exists(scratch)); + await quantizer.BuildExportArtifactAsync(HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), q8, forceRebuild: true, ct: timeout.Token); + DateTime exportTimestamp = File.GetLastWriteTimeUtc(q8); + await quantizer.BuildExportArtifactAsync(HybridQuant.CreatePureBaseline(BaselineQuants.Q8_0), q8, ct: timeout.Token); + Assert.Equal(exportTimestamp, File.GetLastWriteTimeUtc(q8)); + var reader = new GgufMetadataReader(python); + var nativeMetadata = await reader.ReadAsync(native, root, timeout.Token); + var q8Metadata = await reader.ReadAsync(q8, root, timeout.Token); + Assert.NotEmpty(nativeMetadata.TensorNames); + Assert.Equal(nativeMetadata.TensorNames.OrderBy(x => x), q8Metadata.TensorNames.OrderBy(x => x)); + string benchLog = Path.Combine(export, "llamabench.md"); + var command = BenchmarkCommands.Bench(Path.Combine(Cache.LlamaBin, OperatingSystem.IsWindows() ? "llama-bench.exe" : "llama-bench"), q8, false, 0, ""); + var result = await new ProcessRunner().RunAsync(command.CreateStartInfo(), benchLog, ct: timeout.Token); + Assert.True(result.Success, result.CombinedOutput); + var metrics = BenchmarkLogParser.ParseLlamaBench(benchLog); + Assert.True(metrics.Tps > 0); + string manifest = MagicQuantManifestPathService.GetManifestFilePath(export, "smoke.tensor-map.json"); + await File.WriteAllTextAsync(manifest, JsonSerializer.Serialize(q8Metadata.TensorTypes)); + await File.WriteAllTextAsync(Path.Combine(root, "smoke-result.json"), JsonSerializer.Serialize(new + { + SourceModel = source, + LlamaRoot = llama, + RuntimeRoot = runtime, + NativeBytes = new FileInfo(native).Length, + Q8Bytes = new FileInfo(q8).Length, + TensorCount = q8Metadata.TensorNames.Count, + metrics.Tps, + NativeReuseVerified = true, + ScratchCleanupVerified = true, + CompletedUtc = DateTimeOffset.UtcNow + }, new JsonSerializerOptions { WriteIndented = true })); + } + finally + { + Config.Load(oldConfig); + (Cache.ModelDirectory, Cache.ModelMagicQuantDirectory, Cache.MagicQuantDirectory, + Cache.LlamaRoot, Cache.LlamaBin, Cache.ConvertScript, Cache.CurrentModelId) = oldPaths; + Cache.TorchType = oldPrecision; + Cache.ScratchRoots = oldScratch; + (Cache.UseImatrix, Cache.IsImatrixAvailable) = oldImatrix; + // Keep only logs, metadata and the result report. Always remove heavy test weights. + foreach (string file in Directory.EnumerateFiles(root, "*.gguf", SearchOption.AllDirectories)) File.Delete(file); + foreach (string file in Directory.EnumerateFiles(model, "*.safetensors")) File.Delete(file); + } + } +} diff --git a/MagicQuant.Tests/NativeConversionTests.cs b/MagicQuant.Tests/NativeConversionTests.cs new file mode 100644 index 0000000..115b5b9 --- /dev/null +++ b/MagicQuant.Tests/NativeConversionTests.cs @@ -0,0 +1,69 @@ +using System.Diagnostics; +using MagicQuant.Helpers; +using MagicQuant.Runtime; +using MagicQuant.Services; +using MQ.DB; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class NativeConversionTests +{ + [Theory] + [InlineData(0)] + [InlineData(7)] + [InlineData(-1)] + public async Task Only_completed_conversion_is_reusable(int exitCode) + { + string root = Path.Combine(Path.GetTempPath(), $"mq-conversion-{Guid.NewGuid():N}"); + var old = (Cache.ModelDirectory, Cache.ModelMagicQuantDirectory, Cache.ConvertScript, Cache.TorchType); + try + { + Cache.ModelDirectory = Path.Combine(root, "source with spaces"); + Cache.ModelMagicQuantDirectory = Path.Combine(Cache.ModelDirectory, "MagicQuant"); + Cache.ConvertScript = Path.Combine(root, "converter with spaces.py"); + Cache.TorchType = Cache.MainTorchType.BF16; + var paths = new ModelArtifactPathService(); + Directory.CreateDirectory(paths.GgufDir); + var runner = new ConverterStub(exitCode); + var converter = new NativeModelConversionService(paths, new PythonManager(root), runner); + string native = paths.GetNativeBaseGgufPath(); + if (exitCode == 0) + { + Assert.Equal(native, await converter.EnsureAsync()); + Assert.Equal(native, await converter.EnsureAsync()); + Assert.Equal(1, runner.Calls); + // A stale marker cannot make a truncated artifact look complete. + File.WriteAllText(native, ""); + await converter.EnsureAsync(); + Assert.Equal(2, runner.Calls); + } + else + { + await Assert.ThrowsAnyAsync(() => converter.EnsureAsync()); + Assert.False(File.Exists(native)); + Assert.False(File.Exists(native + ".success.json")); + } + } + finally + { + (Cache.ModelDirectory, Cache.ModelMagicQuantDirectory, Cache.ConvertScript, Cache.TorchType) = old; + if (Directory.Exists(root)) Directory.Delete(root, true); + } + } + + private sealed class ConverterStub(int exitCode) : IProcessRunner + { + public int Calls { get; private set; } + public Task RunAsync(ProcessStartInfo start, string? logPath = null, Action? onLine = null, CancellationToken ct = default) + { + Calls++; + Assert.Equal(Cache.ConvertScript, start.ArgumentList[0]); + Assert.Equal(Cache.ModelDirectory, start.ArgumentList[1]); + int outputIndex = start.ArgumentList.IndexOf("--outfile") + 1; + File.WriteAllText(start.ArgumentList[outputIndex], "GGUF fixture"); + if (exitCode == -1) throw new OperationCanceledException(); + return Task.FromResult(new ProcessResult(exitCode, "", "")); + } + } +} diff --git a/MagicQuant.Tests/PreflightTests.cs b/MagicQuant.Tests/PreflightTests.cs new file mode 100644 index 0000000..88fd772 --- /dev/null +++ b/MagicQuant.Tests/PreflightTests.cs @@ -0,0 +1,102 @@ +using MagicQuant.Configuration; +using MagicQuant.Models; +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class PreflightTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), $"mq-preflight-{Guid.NewGuid():N}"); + private readonly MagicQuantYamlConfig _config = MagicQuantYamlConfig.CreateDefault(); + + public PreflightTests() + { + Directory.CreateDirectory(_root); + _config.Paths.ModelDir = Path.Combine(_root, "model"); + _config.Paths.MagicQuantRoot = Path.Combine(_root, "runtime"); + _config.Identity.ArchitectureFamilyName = "test-family"; + Directory.CreateDirectory(_config.Paths.ModelDir); + File.WriteAllText(Path.Combine(_config.Paths.ModelDir, "test.safetensors"), "fixture"); + File.WriteAllText(Path.Combine(_config.Paths.ModelDir, "config.json"), "{}"); + } + + [Fact] + public void Valid_preflight_does_not_create_runtime_or_model_work_directories() + { + CommandPreflight.Validate("pipeline", _config, []); + Assert.False(Directory.Exists(_config.Paths.MagicQuantRoot)); + Assert.False(Directory.Exists(Path.Combine(_config.Paths.ModelDir!, "MagicQuant"))); + } + + [Theory] + [InlineData(".")] + [InlineData("..")] + [InlineData("../..")] + [InlineData("GGUF")] + [InlineData("GGUF/nested")] + [InlineData("Benchmarks")] + public void Output_cannot_destroy_source_or_managed_artifacts(string output) + { + _config.Output.OutputDir = output; + Assert.Throws(() => CommandPreflight.Validate("pipeline", _config, [])); + } + + [Fact] + public void Symlinked_output_is_checked_against_the_physical_target() + { + if (OperatingSystem.IsWindows()) return; // Windows CI does not grant symlink privilege. + string link = Path.Combine(_root, "export-link"); + Directory.CreateSymbolicLink(link, _config.Paths.ModelDir!); + _config.Output.OutputDir = link; + Assert.Throws(() => CommandPreflight.Validate("pipeline", _config, [])); + } + + [Theory] + [InlineData("config.json")] + [InlineData("test.safetensors")] + public void Incomplete_models_fail_before_work_starts(string missing) + { + File.Delete(Path.Combine(_config.Paths.ModelDir!, missing)); + Assert.Throws(() => CommandPreflight.Validate("pipeline", _config, [])); + Assert.False(Directory.Exists(_config.Paths.MagicQuantRoot)); + } + + [Fact] + public void Clone_source_is_required_and_legacy_alias_is_accepted() + { + Assert.Throws(() => CommandPreflight.Validate("clone-repository-quants", _config, [])); + CommandPreflight.Validate("clone-repository-quants", _config, + [new CliArg { Name = "clone-json", Value = Path.Combine(_config.Paths.ModelDir!, "config.json") }]); + } + + [Fact] + public void Misspelled_baseline_mode_does_not_silently_enable_all_baselines() + { + _config.Baselines.StandardBaselinesMode = "selcted"; + Assert.Throws(() => CommandPreflight.Validate("pipeline", _config, [])); + } + + [Theory] + [InlineData("../downloads")] + [InlineData("/tmp/downloads")] + [InlineData("a\\b")] + public void External_cache_name_cannot_escape_model_storage(string name) + { + _config.Paths.ExternalBaselineCacheDirName = name; + Assert.Throws(() => CommandPreflight.Validate("pipeline", _config, [])); + } + + [Fact] + public void Export_cannot_clean_the_runtime_toolchain_or_a_scratch_parent() + { + _config.Output.OutputDir = Path.Combine(_config.Paths.MagicQuantRoot!, "llama.cpp"); + Assert.Throws(() => CommandPreflight.Validate("pipeline", _config, [])); + string scratchRoot = Path.Combine(_root, "scratch"); + _config.Paths.ScratchRoots = [scratchRoot]; + _config.Output.OutputDir = scratchRoot; + Assert.Throws(() => CommandPreflight.Validate("pipeline", _config, [])); + } + + public void Dispose() => Directory.Delete(_root, recursive: true); +} diff --git a/MagicQuant.Tests/ProcessRunnerTests.cs b/MagicQuant.Tests/ProcessRunnerTests.cs new file mode 100644 index 0000000..6a1d6f4 --- /dev/null +++ b/MagicQuant.Tests/ProcessRunnerTests.cs @@ -0,0 +1,92 @@ +using System.Diagnostics; +using MagicQuant.Runtime; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class ProcessRunnerTests +{ + private static ProcessStartInfo Start(params string[] args) + { + var start = new ProcessStartInfo("dotnet"); + start.ArgumentList.Add(typeof(ProcessFixture.Program).Assembly.Location); + foreach (string arg in args) start.ArgumentList.Add(arg); + return start; + } + + [Fact] + public async Task Arguments_preserve_spaces_quotes_and_shell_metacharacters() + { + string[] values = ["folder with spaces", "file'with\"quotes", "$(touch not-a-command)", "C:\\models\\my model", "a;b&c"]; + var result = await new ProcessRunner().RunAsync(Start(["echo", .. values])); + Assert.True(result.Success); + Assert.Equal(string.Join(Environment.NewLine, values) + Environment.NewLine, result.StdOut); + } + + [Fact] + public async Task Drains_both_full_pipes_and_preserves_nonzero_exit_and_log() + { + string log = Path.GetTempFileName(); + try + { + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30)); + var result = await new ProcessRunner().RunAsync(Start("flood"), log, ct: timeout.Token); + Assert.Equal(7, result.ExitCode); + Assert.Contains("stdout-11999", result.StdOut); + Assert.Contains("stderr-11999", result.StdErr); + Assert.Contains("stderr-11999", File.ReadAllText(log)); + using var exclusive = new FileStream(log, FileMode.Open, FileAccess.ReadWrite, FileShare.None); + } + finally { File.Delete(log); } + } + + [Fact] + public async Task Cancellation_reaps_native_work_and_releases_log() + { + string log = Path.GetTempFileName(); + using var cancel = new CancellationTokenSource(TimeSpan.FromSeconds(20)); + int pid = 0; + try + { + var task = new ProcessRunner().RunAsync(Start("wait"), log, (line, _) => + { + if (line.StartsWith("ready:")) { pid = int.Parse(line[6..]); cancel.Cancel(); } + }, cancel.Token); + await Assert.ThrowsAnyAsync(() => task); + Assert.True(pid > 0); + Assert.False(IsRunning(pid)); + using var exclusive = new FileStream(log, FileMode.Open, FileAccess.ReadWrite, FileShare.None); + } + finally { File.Delete(log); } + } + + [Fact] + public async Task Command_scope_cancellation_stops_legacy_callers_without_an_explicit_token() + { + using var cancel = new CancellationTokenSource(); + using (RunCancellation.Use(cancel.Token)) + { + cancel.Cancel(); + await Assert.ThrowsAnyAsync(() => new ProcessRunner().RunAsync(Start("wait"))); + } + Assert.False(RunCancellation.Token.IsCancellationRequested); + } + + [Fact] + public async Task Callback_failure_does_not_leave_the_child_running() + { + int pid = 0; + await Assert.ThrowsAsync(() => new ProcessRunner().RunAsync(Start("wait"), onLine: (line, _) => + { + pid = int.Parse(line[6..]); + throw new IOException("Simulated log failure"); + })); + Assert.False(IsRunning(pid)); + } + + private static bool IsRunning(int pid) + { + try { using var process = Process.GetProcessById(pid); return !process.HasExited; } + catch (ArgumentException) { return false; } + } +} diff --git a/MagicQuant.Tests/QuantizationConcurrencyTests.cs b/MagicQuant.Tests/QuantizationConcurrencyTests.cs new file mode 100644 index 0000000..61f7f85 --- /dev/null +++ b/MagicQuant.Tests/QuantizationConcurrencyTests.cs @@ -0,0 +1,21 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class QuantizationConcurrencyTests +{ + [Theory] + [InlineData(1, 4, 1, 1)] + [InlineData(4, 4, 1, 3)] + [InlineData(8, 4, 1, 6)] + [InlineData(32, 2, 2, 15)] + [InlineData(32, 10, 7, 4)] + public void Writer_capacity_and_cpu_budget_bound_concurrency(int threads, int writers, int concurrent, int perProcess) + { + var plan = QuantizationConcurrencyPlan.Create(threads, writers); + Assert.Equal(concurrent, plan.Concurrency); + Assert.Equal(perProcess, plan.ThreadsPerProcess); + Assert.True(plan.Concurrency * plan.ThreadsPerProcess <= Math.Max(1, threads)); + } +} diff --git a/MagicQuant.Tests/RunProvenanceTests.cs b/MagicQuant.Tests/RunProvenanceTests.cs new file mode 100644 index 0000000..a110a4b --- /dev/null +++ b/MagicQuant.Tests/RunProvenanceTests.cs @@ -0,0 +1,35 @@ +using System.Text.Json; +using MagicQuant.Configuration; +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class RunProvenanceTests +{ + [Theory] + [InlineData("completed")] + [InlineData("failed")] + [InlineData("canceled")] + public void Records_immutable_inputs_and_terminal_status_atomically(string status) + { + string root = Path.Combine(Path.GetTempPath(), $"mq-provenance-{Guid.NewGuid():N}"); + Directory.CreateDirectory(root); + try + { + string configPath = Path.Combine(root, "config.yaml"); + File.WriteAllText(configPath, "paths: {}"); + var config = MagicQuantYamlConfig.CreateDefault(); + config.Paths.MagicQuantRoot = root; + var provenance = new RunProvenanceService("initialize-llama-cpp", ["initialize-llama-cpp"], new(configPath, config, [])); + config.Output.OutputNamePrefix = "changed-after-start"; + provenance.Complete(status); + using var json = JsonDocument.Parse(File.ReadAllText(provenance.ManifestPath)); + Assert.Equal(status, json.RootElement.GetProperty("status").GetString()); + Assert.Equal("Model", json.RootElement.GetProperty("configuration").GetProperty("Output").GetProperty("OutputNamePrefix").GetString()); + Assert.Equal(64, json.RootElement.GetProperty("configSha256").GetString()!.Length); + Assert.False(File.Exists(provenance.ManifestPath + ".tmp")); + } + finally { Directory.Delete(root, true); } + } +} diff --git a/MagicQuant.Tests/YamlDiagnosticsTests.cs b/MagicQuant.Tests/YamlDiagnosticsTests.cs new file mode 100644 index 0000000..28ab89e --- /dev/null +++ b/MagicQuant.Tests/YamlDiagnosticsTests.cs @@ -0,0 +1,52 @@ +using MagicQuant.Configuration; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class YamlDiagnosticsTests +{ + [Fact] + public void Unknown_nested_and_sequence_keys_report_the_full_setting_path() + { + var warnings = YamlConfigurationDiagnostics.Inspect(""" + paths: + modle_dir: /models/test + baselines: + custom_repositories: + - repo_id: owner/model + revison: main + """); + Assert.Contains(warnings, x => x.Contains("paths.modle_dir")); + Assert.Contains(warnings, x => x.Contains("baselines.custom_repositories[0].revison")); + } + + [Fact] + public void Frontmatter_and_gpu_dictionary_keys_are_not_schema_properties() + { + Assert.Empty(YamlConfigurationDiagnostics.Inspect(""" + readme: + frontmatter: + arbitrary_metadata: [hello, world] + hardware: + gpu_memory_limits_gb: + 0: 12 + """)); + } + + [Fact] + public void Removed_destructive_key_is_rejected_but_comments_are_not_options() + { + Assert.Throws(() => YamlConfigurationDiagnostics.Inspect("flags:\n force_relearn_baseline_tensor_mappings: true")); + Assert.Empty(YamlConfigurationDiagnostics.Inspect("# force_relearn_baseline_tensor_mappings was removed\npaths: {}")); + } + + [Fact] + public void Invalid_shape_and_nonfinite_numbers_fail_with_setting_names() + { + var config = MagicQuantYamlConfig.CreateDefault(); + config.Prediction = null!; + Assert.Contains("prediction", Assert.Throws(() => ConfigurationShapeValidator.Validate(config)).Message); + config.Prediction = new RuntimePredictionConfig { DefaultBitStressThreshold = double.NaN }; + Assert.Contains("default_bit_stress_threshold", Assert.Throws(() => ConfigurationShapeValidator.Validate(config)).Message); + } +} diff --git a/MagicQuant.Tests/packages.lock.json b/MagicQuant.Tests/packages.lock.json new file mode 100644 index 0000000..6439e93 --- /dev/null +++ b/MagicQuant.Tests/packages.lock.json @@ -0,0 +1,359 @@ +{ + "version": 1, + "dependencies": { + "net10.0": { + "Microsoft.NET.Test.Sdk": { + "type": "Direct", + "requested": "[17.14.1, )", + "resolved": "17.14.1", + "contentHash": "HJKqKOE+vshXra2aEHpi2TlxYX7Z9VFYkr+E5rwEvHC8eIXiyO+K9kNm8vmNom3e2rA56WqxU+/N9NJlLGXsJQ==", + "dependencies": { + "Microsoft.CodeCoverage": "17.14.1", + "Microsoft.TestPlatform.TestHost": "17.14.1" + } + }, + "xunit": { + "type": "Direct", + "requested": "[2.9.2, )", + "resolved": "2.9.2", + "contentHash": "7LhFS2N9Z6Xgg8aE5lY95cneYivRMfRI8v+4PATa4S64D5Z/Plkg0qa8dTRHSiGRgVZ/CL2gEfJDE5AUhOX+2Q==", + "dependencies": { + "xunit.analyzers": "1.16.0", + "xunit.assert": "2.9.2", + "xunit.core": "[2.9.2]" + } + }, + "xunit.runner.visualstudio": { + "type": "Direct", + "requested": "[2.8.2, )", + "resolved": "2.8.2", + "contentHash": "vm1tbfXhFmjFMUmS4M0J0ASXz3/U5XvXBa6DOQUL3fEz4Vt6YPhv+ESCarx6M6D+9kJkJYZKCNvJMas1+nVfmQ==" + }, + "Blake3": { + "type": "Transitive", + "resolved": "2.2.0", + "contentHash": "RM6sZLZDx2wGi00aTj9s2jUcrI4s9dS2ibcT7lSujpUpBGp+TLf71F3XdBKJyYSxHnZ+FL7Dm36Pl0Y+cfcXvw==" + }, + "DuckDB.NET.Bindings.Full": { + "type": "Transitive", + "resolved": "1.4.3", + "contentHash": "hZwm0zTKJ5HdUGKcase2JX52Lquyh7dCUFweECvR877QEA2gF8gSl3qrtb71BvRlgZ7pfjh0bRBCiAKOJMLE+A==" + }, + "DuckDB.NET.Data.Full": { + "type": "Transitive", + "resolved": "1.4.3", + "contentHash": "tg1FWmePN+k536O1cx2VhKWa3xT7DXrcGg4kGgiSWQyur9UWwZ2i2YMpD+XVYv1ARozyy1Tt7OW2cMrNPoPj9g==", + "dependencies": { + "DuckDB.NET.Bindings.Full": "1.4.3" + } + }, + "LibGit2Sharp": { + "type": "Transitive", + "resolved": "0.31.0", + "contentHash": "b3+UfV7LjKMjAHWwl7VawejiOv2gJIC6dTCA/S0puLTHACAA/Oeb5JJmWUQMeyH/T/WR/LaIK8bk2RbdFnrZvg==", + "dependencies": { + "LibGit2Sharp.NativeBinaries": "[2.0.323]" + } + }, + "LibGit2Sharp.NativeBinaries": { + "type": "Transitive", + "resolved": "2.0.323", + "contentHash": "Kg+fJGWhGj5qRXG0Ilj4ddhuodGXZg57yhfX6OVUDR0M2DKg/UR42/d74+qv5l1qotc1qJilo/ho7xQnULP6yA==" + }, + "Microsoft.CodeCoverage": { + "type": "Transitive", + "resolved": "17.14.1", + "contentHash": "pmTrhfFIoplzFVbhVwUquT+77CbGH+h4/3mBpdmIlYtBi9nAB+kKI6dN3A/nV4DFi3wLLx/BlHIPK+MkbQ6Tpg==" + }, + "Microsoft.Data.Sqlite": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "7je7UELzm131GiLYc4PpZvfKXIgIyzPM+v+tjcd/nbnuWRfgcONYKzDTqJlURxwVCFsVnlpmq6y6yn4qvR8QXQ==", + "dependencies": { + "Microsoft.Data.Sqlite.Core": "10.0.11", + "SQLitePCLRaw.bundle_e_sqlite3": "2.1.12", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.Data.Sqlite.Core": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "hubA20AGenQ4Sx0ElWaPpB8DISjXpdx463+1zOGRslsT0e/t/06ITv+pHsop8CcJ0d8PZLfgnT7juCDVD79Dkw==", + "dependencies": { + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.EntityFrameworkCore": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "VOSGU8en6HZJs8t7UMFN+9vGcRgVOOn6fA44Ngcg2NyvJ3P1KE94iAb0XzaVaGhXGtt+qaM/VtEn0/hzluQJeg==", + "dependencies": { + "Microsoft.EntityFrameworkCore.Abstractions": "10.0.11", + "Microsoft.EntityFrameworkCore.Analyzers": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11" + } + }, + "Microsoft.EntityFrameworkCore.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "6auJR+9+9VunznKfH7WGrHMrnrmA0F7JZ22EXzwXvVhjfnbu9Xq7NSIWaOf3KJsOanM2qf5ajJ2JR5TlcPZTLA==" + }, + "Microsoft.EntityFrameworkCore.Analyzers": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "Bv7X4wSSnzCQED9WYXKJ8fwgyvKwf0xZM1GO8xkf6CF9zl+UBnvjxmcPnokJRy0JKjc1SlHSzzhx1HcL4jitTQ==" + }, + "Microsoft.EntityFrameworkCore.Relational": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "grznnTJgEYxaWpdKAsTzg6j+89jHgCXWYp+QGtlX5O92+w/VuhWM6JLPYb+uw8M9VhGUvOTsO76dYOy9vNPd5Q==", + "dependencies": { + "Microsoft.EntityFrameworkCore": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11" + } + }, + "Microsoft.EntityFrameworkCore.Sqlite": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "jc7iVrhQyInR3loraMESfEFaFOtQOB1mRKHjX6QYC9o7YDbfMNbAPnIwlpffnFwhXd6/27FKaaV+sWSoLd4F1g==", + "dependencies": { + "Microsoft.EntityFrameworkCore.Sqlite.Core": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyModel": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11", + "SQLitePCLRaw.bundle_e_sqlite3": "2.1.12", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.EntityFrameworkCore.Sqlite.Core": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "ywTQKt32xnVhCzjEQAqFufpEyXkOUfvW/EC/s4xnS8Xaor2xXE+TMUyzhgACqXtZEU5IR95y94RDzHto55Fx7w==", + "dependencies": { + "Microsoft.Data.Sqlite.Core": "10.0.11", + "Microsoft.EntityFrameworkCore.Relational": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyModel": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.Extensions.Caching.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "vUl798SmruTqqlt/xH2gDk3tJlhk6k3HdOXAHirlRfbNKDym4g/kRpUL9S4sl6F6FsOTOMW+ZsDapqlZMOOiEw==", + "dependencies": { + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Caching.Memory": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "el1g0mBEbDBGY2bT9mcSfrTWO8QlPdq2nOCnvQugioOFwHV+bVBMeiakoI0dNOdj8d6Hi9K6HY2xzRUWJiDR3w==", + "dependencies": { + "Microsoft.Extensions.Caching.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11", + "Microsoft.Extensions.Logging.Abstractions": "10.0.11", + "Microsoft.Extensions.Options": "10.0.11", + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Configuration.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "fVi053xdpda9Em7vSkmgVxO/PtgC2m78ekReKWsgcyskqY0U82Bz/MONwxpGzI0hElYKJfw+fupqMVeKW3fSaA==", + "dependencies": { + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.DependencyInjection": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "PSmotV19c7E3lKed++uYo1kSiXFI+uTl37CBSrhq+CfLC3FCHjG7R91+xPnNehQfHS1b0Tzo/CCLPWH3qaEheg==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11" + } + }, + "Microsoft.Extensions.DependencyInjection.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "/a1aJz4m7ylhEDf25ugQChLQoN5XwoGjWw/BoR/ZWWKsO1v4DdJElS1uyngahz4B/eOzjFk1KNTkarRLE5wsIg==" + }, + "Microsoft.Extensions.DependencyModel": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "PJPtFYsZ+r+uz9qqXWUTEyKeJ1EiBGIJtqavkg9ZXijjGSFAk4Fgi5sqIxj+uAyLZwEKgexDUQXhWhvU6l3+og==" + }, + "Microsoft.Extensions.Logging": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "nUOJwgFkSiLHiVGFpU22pIJtuWYewuSYQ3JVuP/gdK8ASMT807Px+TYQiRWs6uSsOmoyFTaVCwKXTasczV6BpA==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection": "10.0.11", + "Microsoft.Extensions.Logging.Abstractions": "10.0.11", + "Microsoft.Extensions.Options": "10.0.11" + } + }, + "Microsoft.Extensions.Logging.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "Ljd0Uxoq5XpScD2Bg0nM/r3mwx7Ao5Uq24eo2ARxbGvqJ7Zht6rt2cJtwVRH4Cv+1ZVMdXz6TB43KbpmsxRrvQ==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11" + } + }, + "Microsoft.Extensions.Options": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "eY1GAKcTfD2maP27J84X9IovT3yjHJ2dVDzPmDg6/XqYvt3jMzJhtfQCLjG9pVsZGAd+8DQ2QrjaDcs2+VQLGw==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11", + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Primitives": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "SXcz+kF+4Oo9b1+55zntpJFYfwb1jw66ioxptyNOOTDc8g2FHnBFWjZpsWfCvZIhzr0x+4e2trVTs4OKwQfBtw==" + }, + "Microsoft.TestPlatform.ObjectModel": { + "type": "Transitive", + "resolved": "17.14.1", + "contentHash": "xTP1W6Mi6SWmuxd3a+jj9G9UoC850WGwZUps1Wah9r1ZxgXhdJfj1QqDLJkFjHDCvN42qDL2Ps5KjQYWUU0zcQ==" + }, + "Microsoft.TestPlatform.TestHost": { + "type": "Transitive", + "resolved": "17.14.1", + "contentHash": "d78LPzGKkJwsJXAQwsbJJ7LE7D1wB+rAyhHHAaODF+RDSQ0NgMjDFkSA1Djw18VrxO76GlKAjRUhl+H8NL8Z+Q==", + "dependencies": { + "Microsoft.TestPlatform.ObjectModel": "17.14.1", + "Newtonsoft.Json": "13.0.3" + } + }, + "Newtonsoft.Json": { + "type": "Transitive", + "resolved": "13.0.3", + "contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ==" + }, + "Spectre.Console": { + "type": "Transitive", + "resolved": "0.54.0", + "contentHash": "StDXCFayfy0yB1xzUHT2tgEpV1/HFTiS4JgsAQS49EYTfMixSwwucaQs/bIOCwXjWwIQTMuxjUIxcB5XsJkFJA==" + }, + "SQLitePCLRaw.bundle_e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "mAgscpQMLw5/nfA1Q5oJVAT29yROUo1ifZGbbTpx/lwZpSxMUGoYbKfmvdm8oXER+RzxqBmmQzeBEVKfeHv2nw==", + "dependencies": { + "SQLitePCLRaw.lib.e_sqlite3": "2.1.12", + "SQLitePCLRaw.provider.e_sqlite3": "2.1.12" + } + }, + "SQLitePCLRaw.core": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "ETpNw9DY3ckWLgRRAeCHj+GKOuPi61aeczkXhgHexUvqoZBAYg8RYESE2J7O1M7+o6QbdSEZwrw9bfqztUVWXg==" + }, + "SQLitePCLRaw.lib.e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "fWi8Dbknuhgg72fWinIdjXVaqO1hHL4YBBwVLnr7e1c9TAZwJ0QE38j9syW1hwx6HaqEVTwI+O07WPdZn8Rp0w==" + }, + "SQLitePCLRaw.provider.e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "W3oH4XIfCzFrgUSDKHhN6N+dgzA5YHOR2VxX8GB6Qy7CyrJJgxPEG8NirgYWlPQC5P2jz2knSsexWu4tDUL33g==", + "dependencies": { + "SQLitePCLRaw.core": "2.1.12" + } + }, + "System.CodeDom": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "v40pNeBoZTYsiVxz+PzyZmmIr2JIhpK4VsFpQqZSZCXa51PDlNXIN2ESm8kDU0voZYVfLhxF9HvmBsxCJmkiRg==" + }, + "System.Management": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "xyNn8KGbWI88LoUwg3rB8qcpFFST6dr8Ro/qS8GBu2GOwR0v7J82kVFHTiiPtvEKS79VbMTxs/sIKQ+Cq1Zs1g==", + "dependencies": { + "System.CodeDom": "10.0.11" + } + }, + "xunit.abstractions": { + "type": "Transitive", + "resolved": "2.0.3", + "contentHash": "pot1I4YOxlWjIb5jmwvvQNbTrZ3lJQ+jUGkGjWE3hEFM0l5gOnBWS+H3qsex68s5cO52g+44vpGzhAt+42vwKg==" + }, + "xunit.analyzers": { + "type": "Transitive", + "resolved": "1.16.0", + "contentHash": "hptYM7vGr46GUIgZt21YHO4rfuBAQS2eINbFo16CV/Dqq+24Tp+P5gDCACu1AbFfW4Sp/WRfDPSK8fmUUb8s0Q==" + }, + "xunit.assert": { + "type": "Transitive", + "resolved": "2.9.2", + "contentHash": "QkNBAQG4pa66cholm28AxijBjrmki98/vsEh4Sx5iplzotvPgpiotcxqJQMRC8d7RV7nIT8ozh97957hDnZwsQ==" + }, + "xunit.core": { + "type": "Transitive", + "resolved": "2.9.2", + "contentHash": "O6RrNSdmZ0xgEn5kT927PNwog5vxTtKrWMihhhrT0Sg9jQ7iBDciYOwzBgP2krBEk5/GBXI18R1lKvmnxGcb4w==", + "dependencies": { + "xunit.extensibility.core": "[2.9.2]", + "xunit.extensibility.execution": "[2.9.2]" + } + }, + "xunit.extensibility.core": { + "type": "Transitive", + "resolved": "2.9.2", + "contentHash": "Ol+KlBJz1x8BrdnhN2DeOuLrr1I/cTwtHCggL9BvYqFuVd/TUSzxNT5O0NxCIXth30bsKxgMfdqLTcORtM52yQ==", + "dependencies": { + "xunit.abstractions": "2.0.3" + } + }, + "xunit.extensibility.execution": { + "type": "Transitive", + "resolved": "2.9.2", + "contentHash": "rKMpq4GsIUIJibXuZoZ8lYp5EpROlnYaRpwu9Zr0sRZXE7JqJfEEbCsUriZqB+ByXCLFBJyjkTRULMdC+U566g==", + "dependencies": { + "xunit.extensibility.core": "[2.9.2]" + } + }, + "YamlDotNet": { + "type": "Transitive", + "resolved": "17.0.1", + "contentHash": "qVir5fehR/W5nTJyoJUibypETXaW4iRAF9cQa0FQIC9TJ3VC0qDOwm4o/RxANewj8KzPF8WMF2abBfUgi6LC4w==" + }, + "magicquant": { + "type": "Project", + "dependencies": { + "Blake3": "[2.2.0, )", + "DuckDB.NET.Data.Full": "[1.4.3, )", + "LibGit2Sharp": "[0.31.0, )", + "MQ.DB": "[1.0.0, )", + "Spectre.Console": "[0.54.0, )", + "System.Management": "[10.0.11, )", + "YamlDotNet": "[17.0.1, )" + } + }, + "magicquant.processfixture": { + "type": "Project" + }, + "mq.db": { + "type": "Project", + "dependencies": { + "Microsoft.Data.Sqlite": "[10.0.11, )", + "Microsoft.EntityFrameworkCore": "[10.0.11, )", + "Microsoft.EntityFrameworkCore.Sqlite": "[10.0.11, )", + "YamlDotNet": "[17.0.1, )" + } + } + } + } +} \ No newline at end of file diff --git a/MagicQuant/Commands/CloneRepositoryQuants.cs b/MagicQuant/Commands/CloneRepositoryQuants.cs index 2da8823..18fdc18 100644 --- a/MagicQuant/Commands/CloneRepositoryQuants.cs +++ b/MagicQuant/Commands/CloneRepositoryQuants.cs @@ -69,7 +69,9 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; @@ -99,6 +101,7 @@ public async Task Run(List args) Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(Cache.ModelDirectory); AnsiConsole.MarkupLine($"[green]Model ID Created/Found:[/] [cyan]{Markup.Escape(Cache.CurrentModelId)}[/]"); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await EnsureSqliteReadyAsync(); var pyManager = new PythonManager(Cache.MagicQuantDirectory!); @@ -108,6 +111,7 @@ public async Task Run(List args) string? sourceRepo = Get(args, "source-repo") ?? Get(args, "clone-repo"); string? sourceJson = Get(args, "source-json") ?? Get(args, "clone-json"); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var (manifest, manifestLocalPath, sourceDescription) = await manifestService.ResolveAsync( sourceRepo, sourceJson, @@ -124,15 +128,20 @@ public async Task Run(List args) string baseModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); var sidecarService = new ModelSidecarArtifactService(pyManager); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await sidecarService.EnsureMmprojArtifactAvailableAsync(); var architectureFamilyService = new ArchitectureFamilyService(pyManager); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(baseModelGgufPath); var tensorGroupProfileService = new TensorGroupProfileService(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await tensorGroupProfileService.EnsureCurrentProfileAsync(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var resolvedCustomBaselines = await hf.PrecheckAndRegisterConfiguredBaselinesAsync(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new TargetedRelearnService().PlanConfirmAndExecuteAsync(resolvedCustomBaselines); var imatrixRequest = new ImatrixRequest @@ -148,6 +157,7 @@ public async Task Run(List args) MagicQuantDirectory = Cache.ModelMagicQuantDirectory! }; + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest); RuntimeSearchSpace.SetImatrixAvailability(imatrixEnsureResult.Available); @@ -161,8 +171,10 @@ public async Task Run(List args) benchmarkCache: preCleanBenchmarkCache, records: out var reusableRecords); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await CleanOutputDirectoryAsync(Cache.OutputDirectory!, Config.ReuseExistingFinalArtifacts); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var archivedManifestFiles = await CopySourceManifestFilesAsync( outputDirectory: Cache.OutputDirectory!, sourceManifestLocalPath: manifestLocalPath, @@ -175,6 +187,7 @@ public async Task Run(List args) manifest.SourceRepository = sourceRepo; manifest.SourceJson = string.IsNullOrWhiteSpace(sourceRepo) ? sourceDescription : manifest.SourceJson; string outputCloneManifestPath = MagicQuantManifestPathService.GetManifestFilePath(Cache.OutputDirectory!, MagicQuantManifestPathService.CloneConfigsFileName); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await File.WriteAllTextAsync(outputCloneManifestPath, JsonSerializer.Serialize(manifest, JsonOptions)); archivedManifestFiles.Add(MagicQuantManifestPathService.CloneConfigsFileName); @@ -304,11 +317,16 @@ await EnsureCloneNativeBenchmarkArtifactsReadyAsync( ApplyCloneReferencePplDeltas(records); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await CopyModelAdjacentFilesAsync(Cache.OutputDirectory!); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await CopyImatrixArtifactsAsync(Cache.OutputDirectory!); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await sidecarService.CopyMmprojArtifactsAsync(Cache.OutputDirectory!); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await WriteCloneBenchmarkSummaryAsync(Cache.OutputDirectory!, records); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await WriteResolvedCloneConfigManifestAsync( outputCloneManifestPath, records, @@ -318,6 +336,7 @@ await WriteResolvedCloneConfigManifestAsync( hasMissingManifestBaseQuantOverride); archivedManifestFiles.Add(MagicQuantManifestPathService.CloneBenchmarksFileName); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new CloneReadmeGenerationService().GenerateAsync( Cache.OutputDirectory!, new DirectoryInfo(Cache.ModelDirectory!).Name, @@ -326,6 +345,7 @@ await WriteResolvedCloneConfigManifestAsync( records, archivedManifestFiles); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await CleanCloneExportSidecarsAsync(Cache.OutputDirectory!); AnsiConsole.MarkupLine("[bold green]Repository quant clone complete.[/]"); diff --git a/MagicQuant/Commands/InitializeLlamaCpp.cs b/MagicQuant/Commands/InitializeLlamaCpp.cs index c12f726..d8602d2 100644 --- a/MagicQuant/Commands/InitializeLlamaCpp.cs +++ b/MagicQuant/Commands/InitializeLlamaCpp.cs @@ -29,7 +29,7 @@ public async Task Run(List args) // 1. Argument Parsing & Path Validation // --------------------------------------------------------- bool update = args.Any(a => a.Name?.ToLower() == "update"); - + string? convertScript = args.FirstOrDefault(a => a.Name?.ToLower() == "convert-script")?.Value; string? llamaBin = args.FirstOrDefault(a => a.Name?.ToLower() == "llama-bin")?.Value; string? llamaRoot = args.FirstOrDefault(a => a.Name?.ToLower() == "llama-root")?.Value; @@ -65,7 +65,7 @@ public async Task Run(List args) } else if (!string.IsNullOrEmpty(convertScript) || !string.IsNullOrEmpty(llamaBin)) { - throw new ArgumentException("Partial llama.cpp paths provided. Provide all three custom paths or none."); + throw new ArgumentException("Partial llama.cpp paths provided. Provide all three custom paths or none."); } // --------------------------------------------------------- @@ -85,12 +85,12 @@ public async Task Run(List args) // --------------------------------------------------------- if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - var requiredPackages = new List - { - "build-essential", "cmake", "ninja-build", "git", - "python3", "python3-venv", "python3-pip", "libcurl4-openssl-dev" + var requiredPackages = new List + { + "build-essential", "cmake", "ninja-build", "git", + "python3", "python3-venv", "python3-pip", "libcurl4-openssl-dev" }; - + if (sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor == GpuVendor.Nvidia) requiredPackages.Add("nvidia-cuda-toolkit"); // Check if updates are needed @@ -98,13 +98,13 @@ public async Task Run(List args) { AnsiConsole.MarkupLine("[yellow]System dependencies are missing or update requested.[/]"); AnsiConsole.MarkupLine("[grey]Sudo permissions are required to install system packages via apt.[/]"); - + // A. Ask for Sudo permission upfront - try + try { await RefreshSudoCredentialsAsync(); } - catch (Exception ex) + catch (Exception ex) when (ex is not OperationCanceledException) { throw new InvalidOperationException("Sudo access denied or cancelled. Cannot install system dependencies.", ex); } @@ -112,7 +112,7 @@ public async Task Run(List args) // B. Run Install WITH sudo AnsiConsole.MarkupLine("[cyan]Installing/Updating System Dependencies (sudo apt)...[/]"); string aptArgs = "install -y " + string.Join(" ", requiredPackages); - + // We run 'sudo' directly here await RunSimpleProcess("sudo", "apt " + aptArgs); } @@ -149,7 +149,7 @@ public async Task Run(List args) AnsiConsole.Write(new Rule("[yellow]Installing Python Libraries[/]") { Justification = Justify.Left }); // Helper to decide if we need to install - async Task EnsurePackage(string name, string installCmd, Dictionary? env = null) + async Task EnsurePackage(string name, string installCmd, Dictionary? env = null) { if (!update) { @@ -160,7 +160,7 @@ async Task EnsurePackage(string name, string installCmd, Dictionary AnsiConsole.WriteLine(line)); + if (!result.Success) throw new InvalidOperationException($"{exe} failed with exit code {result.ExitCode}."); } - private async Task RefreshSudoCredentialsAsync() - { - // "sudo -v" updates the user's cached credentials. - // It will prompt for a password if necessary in the standard input. - var psi = new ProcessStartInfo - { - FileName = "sudo", - Arguments = "-v", - UseShellExecute = false // Required to handle password prompt - }; - - var p = Process.Start(psi); - await p!.WaitForExitAsync(); - - if (p.ExitCode != 0) - { - throw new Exception("Sudo access denied."); - } - } + private static Task RefreshSudoCredentialsAsync() => LinuxHelper.RefreshSudoCredentialsAsync(); private bool AreLinuxPackagesInstalled(List packages) { diff --git a/MagicQuant/Commands/QuantizationPipeline.cs b/MagicQuant/Commands/QuantizationPipeline.cs index 69f1e63..8f97f6e 100644 --- a/MagicQuant/Commands/QuantizationPipeline.cs +++ b/MagicQuant/Commands/QuantizationPipeline.cs @@ -66,7 +66,9 @@ public async Task Run(List args) Cache.ModelDirectory = fullModelPath; Cache.ModelMagicQuantDirectory = Path.Combine(fullModelPath, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Cache.ForceRefreshHardwareProbe = Config.Current.Flags.ForceRefreshHardwareProbe; Cache.UseImatrix = Config.Current.Flags.UseImatrix; @@ -101,6 +103,7 @@ public async Task Run(List args) var pyManager = new PythonManager(Cache.MagicQuantDirectory!); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await EnsureSqliteReadyAsync(); var benchmarkService = new BenchmarkService(pyManager); @@ -108,26 +111,32 @@ public async Task Run(List args) var imatrixService = new ImatrixService(); string q8QuantizationKey = BaselineQuants.Q8_0.Names[0]; + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var bf16ModelGgufPath = await quantizationService.EnsureBaseModelFileAsync(true); var sidecarService = new ModelSidecarArtifactService(pyManager); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await sidecarService.EnsureMmprojArtifactAvailableAsync(); // Review the active regex profile against the native/BF16 tensor list before // architecture/profile-scoped learning truth is persisted or reused. This is // the early "do these groups look sane?" gate for catching YAML regex mistakes. + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new TensorGroupReviewService().ReviewNativeTensorGroupingAsync( quantizationService: quantizationService, nativeGgufPath: bf16ModelGgufPath, requireConfirmation: Cache.ConfirmTensorGroupProfile); var architectureFamilyService = new ArchitectureFamilyService(pyManager); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await architectureFamilyService.EnsureCurrentArchitectureFamilyAsync(bf16ModelGgufPath); var tensorGroupProfileService = new TensorGroupProfileService(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await tensorGroupProfileService.EnsureCurrentProfileAsync(); var customBaselineService = new HuggingFaceBaselineService(pyManager); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var resolvedCustomBaselines = await customBaselineService.PrecheckAndRegisterConfiguredBaselinesAsync(); if (Config.Current.Baselines.CustomRepositories.Any(x => x.Enabled) && resolvedCustomBaselines.Count == 0) @@ -136,6 +145,7 @@ public async Task Run(List args) "Custom baseline repositories were enabled, but no custom baselines resolved into the runtime registry."); } + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new TargetedRelearnService().PlanConfirmAndExecuteAsync(resolvedCustomBaselines); var imatrixRequest = new ImatrixRequest @@ -151,7 +161,8 @@ public async Task Run(List args) MagicQuantDirectory = Cache.ModelMagicQuantDirectory! }; - var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest, ct: default); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); + var imatrixEnsureResult = await imatrixService.EnsureImatrixAsync(imatrixRequest, ct: MagicQuant.Runtime.RunCancellation.Token); if (imatrixEnsureResult.Enabled) { @@ -207,6 +218,7 @@ await benchmarkService.EnsureDynamicExecutionPlanAsync( var baseModelQuant = HybridQuant.CreatePureBaseline(BaselineQuants.GetBF16Quant()); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await EnsureNativeBenchmarkEnvironmentReadyAsync( benchmarkService: benchmarkService, quantizationService: quantizationService, @@ -218,6 +230,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( nativeTruthAlreadyLearned: nativeTruthAlreadyLearned); var compatibilityService = new ModelCompatibilityService(pyManager); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await compatibilityService.RunCompatibilityCheckAsync(bf16ModelGgufPath); // Compatibility must not be allowed to silently downgrade the live policy flags for the @@ -239,6 +252,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( AnsiConsole.MarkupLine($"[grey]Queued initial startup samples:[/] [cyan]{initialPlan.TotalCount:N0}[/]"); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var initialSummary = await quantizationService.ProcessHybridBatchAsync( initialPlan.Plans, new StageProgressOptions @@ -261,6 +275,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( var isolationOptimizer = new IsolationOptimizationService(); AnsiConsole.Write(new Rule("[yellow]Initial Probe Analysis[/]") { Justification = Justify.Left }); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var initialAnalysis = await isolationOptimizer.AnalyzeInitialIsolationProbesAsync(initialPlan); AnsiConsole.Write(new Rule("[yellow]Initial Probe Group Decisions[/]") { Justification = Justify.Left }); @@ -317,6 +332,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space Before Final Isolation Optimization"); AnsiConsole.Write(new Rule("[yellow]Final Isolation Optimization[/]") { Justification = Justify.Left }); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var isolationResult = await isolationOptimizer.AnalyzeAndApplyFinalAsync(mergedPlan); SearchSpaceDebugPrinter.PrintCurrentSearchSpace("Search Space After Final Isolation Optimization"); @@ -341,6 +357,7 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( var comboCountAfterRulePruning = ComboCounter.CountAll(); var dbService = new QuantDatabaseService(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await dbService.InitializeAsync(forceRebuild: true); // The old MDA/predicted-size ceiling pass is intentionally removed. @@ -417,10 +434,11 @@ await EnsureNativeBenchmarkEnvironmentReadyAsync( var finalIsolationManifestPlan = mergedPlan.MergeWith(archivalCoveragePlan); var survivalPipeline = new CombinationSurvivalPipelineService(quantizationService); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); var finalizationResult = await survivalPipeline.RunAsync( isolationSamplePlan: finalIsolationManifestPlan, isolationOptimizationResult: isolationResult, - ct: default); + ct: MagicQuant.Runtime.RunCancellation.Token); AnsiConsole.Write(new Rule("[yellow]Export Summary[/]") { Justification = Justify.Left }); AnsiConsole.MarkupLine($"[green]Export directory:[/] [blue]{Markup.Escape(Cache.OutputDirectory ?? "n/a")}[/]"); diff --git a/MagicQuant/Commands/ValidatePredictions.cs b/MagicQuant/Commands/ValidatePredictions.cs index 500a332..eead297 100644 --- a/MagicQuant/Commands/ValidatePredictions.cs +++ b/MagicQuant/Commands/ValidatePredictions.cs @@ -35,13 +35,16 @@ public async Task Run(List args) Cache.ModelDirectory = modelDir; Cache.ModelMagicQuantDirectory = Path.Combine(modelDir, "MagicQuant"); ModelRuntimePathService.InitializeForCurrentModel(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new ExternalBaselineCacheCleanupService().CleanupStaleArtifactsAsync(); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await new ScratchStorageService(new ModelArtifactPathService()).CleanupStaleScratchArtifactsAsync(); Directory.CreateDirectory(Cache.ModelMagicQuantDirectory); Cache.CurrentModelId = MagicQuantModelId.GetOrCreateModelId(modelDir); JsonHelper.DetectAndSetTorchType(Cache.ModelDirectory); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await ResolveArchitectureFamilyFromConfigAsync(); ApplyOptionalImatrixContext(args); @@ -54,6 +57,7 @@ public async Task Run(List args) var prediction = new RankSafeKldPredictionService(repository, effectiveResolver); var validator = new PredictionValidationService(repository, prediction); + MagicQuant.Runtime.RunCancellation.Token.ThrowIfCancellationRequested(); await validator.ExportAsync(outputDir); } diff --git a/MagicQuant/Configuration/CliOptionValidator.cs b/MagicQuant/Configuration/CliOptionValidator.cs new file mode 100644 index 0000000..9a3fbd3 --- /dev/null +++ b/MagicQuant/Configuration/CliOptionValidator.cs @@ -0,0 +1,125 @@ +using System.Globalization; +using MagicQuant.Models; + +namespace MagicQuant.Configuration; + +/// Rejects typos and ambiguous CLI values before any configuration or runtime mutation. +public static class CliOptionValidator +{ + private static readonly HashSet Flags = new(StringComparer.OrdinalIgnoreCase) + { + "allow-architecture-family-alias-override", + "allow-eight-bit-anchor-replacements", + "allow-high-precision-hybrids", + "allow-missing-manifest-tensors", + "check-config", + "disable-tensor-group-rebucket", + "export-external-learned-baselines", + "force-refresh-hardware-probe", + "force-relearn-baseline-tensor-mappings", + "force_refresh_hardware_probe", + "full-relearn-tensor-groups", + "help", + "imatrix-force-rebuild", + "no-rebucket-learned-tensor-groups", + "rebucket-learned-tensor-groups", + "rebucket-tensor-groups-from-db", + "recheck-hardware-probe", + "relearn-baseline-mappings", + "relearn-tensor-groups-from-db", + "reuse-existing-final-artifacts", + "skip-tensor-group-confirm", + "strict-config", + "update", + "use-imatrix", + "validate", + "validate-all-anomaly-strict-candidates-after-success", + "verify", + "yes-tensor-groups", + }; + private static readonly HashSet Values = new(StringComparer.OrdinalIgnoreCase) + { + "architecture-family", + "clone-json", + "clone-repo", + "config", + "convert-script", + "imatrix-dataset-config", + "imatrix-dataset-local-file", + "imatrix-dataset-repo", + "imatrix-dataset-split", + "imatrix-identity-hash", + "imatrix-path", + "imatrix-url", + "llama-bin", + "llama-root", + "magic-quant-root", + "manual-max-predicted-size-bytes", + "missing-manifest-base-quant", + "model-dir", + "output-dir", + "output-name-prefix", + "prediction-bit-stress-threshold-candidates", + "prediction-default-bit-stress-threshold", + "prediction-minimum-fit-rows", + "selection-diversify-validation-candidates", + "selection-diversity-low-bit-only", + "selection-diversity-scan-max-candidates", + "selection-diversity-scan-min-candidates", + "selection-diversity-scan-multiplier", + "selection-interior-window-fractions", + "selection-max-candidates-per-interior-window", + "selection-max-fallback-attempts-per-anchor", + "selection-minimum-kld-improvement-epsilon", + "selection-minimum-neighbor-gap-fraction", + "selection-near-anchor-required-kld-gain-fraction", + "selection-near-baseline-max-size-growth-percent", + "selection-near-lower-anchor-brutal-zone-fraction", + "source-json", + "source-repo", + }; + + public static void Validate(IReadOnlyList args) + { + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (var arg in args) + { + string name = arg.Name ?? ""; + if (!seen.Add(name)) throw new ArgumentException($"Duplicate option --{name}. Supply it once."); + if (Flags.Contains(name)) + { + if (!string.IsNullOrEmpty(arg.Value)) throw new ArgumentException($"--{name} is a flag and takes no value. Set boolean policy in YAML when disabling it."); + } + else if (Values.Contains(name)) + { + if (string.IsNullOrWhiteSpace(arg.Value)) throw new ArgumentException($"--{name} requires a value."); + ValidateTypedValue(name.ToLowerInvariant(), arg.Value); + } + else throw new ArgumentException($"Unknown or removed option --{name}. See command --help and docs/configuration.md."); + } + } + private static void ValidateTypedValue(string name, string value) + { + string[] integers = ["prediction-minimum-fit-rows", "selection-max-candidates-per-interior-window", + "selection-max-fallback-attempts-per-anchor", "selection-diversity-scan-multiplier", + "selection-diversity-scan-min-candidates", "selection-diversity-scan-max-candidates"]; + string[] numbers = ["prediction-default-bit-stress-threshold", "selection-near-baseline-max-size-growth-percent", + "selection-minimum-kld-improvement-epsilon", "selection-minimum-neighbor-gap-fraction", + "selection-near-lower-anchor-brutal-zone-fraction", "selection-near-anchor-required-kld-gain-fraction"]; + bool valid = true; + if (integers.Contains(name)) + valid = int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int n) && n >= (name == "prediction-minimum-fit-rows" ? 2 : 1); + else if (numbers.Contains(name)) + valid = double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out double n) && double.IsFinite(n) && + (name == "prediction-default-bit-stress-threshold" ? n > 0 : n >= 0); + else if (name == "manual-max-predicted-size-bytes") + valid = ulong.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out _); + else if (name is "prediction-bit-stress-threshold-candidates" or "selection-interior-window-fractions") + valid = value.Split(',').All(v => double.TryParse(v, NumberStyles.Float, CultureInfo.InvariantCulture, out double n) && + double.IsFinite(n) && n > 0 && (name != "selection-interior-window-fractions" || n <= 1)); + else if (name is "selection-diversify-validation-candidates" or "selection-diversity-low-bit-only") + valid = new[] { "true", "false", "1", "0", "yes", "no", "y", "n", "on", "off" }.Contains(value, StringComparer.OrdinalIgnoreCase); + if (!valid) throw new ArgumentException($"Invalid value '{value}' for --{name}. Check the option's type/range; decimals use a dot."); + } + +} diff --git a/MagicQuant/Configuration/CommandPreflight.cs b/MagicQuant/Configuration/CommandPreflight.cs new file mode 100644 index 0000000..9dace48 --- /dev/null +++ b/MagicQuant/Configuration/CommandPreflight.cs @@ -0,0 +1,77 @@ +using System.Text.Json; +using MagicQuant.Models; +using MagicQuant.Services; +using MQ.DB.Models; + +namespace MagicQuant.Configuration; + +/// Read-only checks before cleanup, dependency setup, model hashing, or database initialization. +public static class CommandPreflight +{ + public static void Validate(string command, MagicQuantYamlConfig config, IReadOnlyList args) + { + string? Get(string name) => args.FirstOrDefault(a => string.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase))?.Value; + if (!new[] { "all", "none", "selected" }.Contains(config.Baselines.StandardBaselinesMode.Trim(), StringComparer.OrdinalIgnoreCase)) + throw new InvalidOperationException("baselines.standard_baselines_mode must be all, selected, or none."); + PathSafety.ValidateFolderName(config.Paths.ExternalBaselineCacheDirName, "paths.external_baseline_cache_dir_name"); + PathSafety.ValidateFolderName(config.Output.OutputNamePrefix, "output.output_name_prefix"); + var custom = new[] { config.Paths.LlamaRoot, config.Paths.LlamaBin, config.Paths.ConvertScript }; + if (custom.Any(p => !string.IsNullOrWhiteSpace(p))) + { + if (custom.Any(string.IsNullOrWhiteSpace)) + throw new InvalidOperationException("Provide all three custom llama.cpp paths: llama_root, llama_bin, convert_script."); + if (!Directory.Exists(custom[0]) || !Directory.Exists(custom[1]) || !File.Exists(custom[2])) + throw new InvalidOperationException("One or more custom llama.cpp paths do not exist."); + } + if (command.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) return; + string model = config.Paths.ModelDir ?? ""; + if (string.IsNullOrWhiteSpace(model) || !Directory.Exists(model)) + throw new InvalidOperationException("A valid model directory is required. Set paths.model_dir or --model-dir."); + model = Path.GetFullPath(model); + string work = Path.Combine(model, "MagicQuant"); + string runtime = string.IsNullOrWhiteSpace(config.Paths.MagicQuantRoot) + ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), MagicConstants.MagicQuantFolder) + : Path.GetFullPath(config.Paths.MagicQuantRoot); + bool validation = command.Equals("validate-predictions", StringComparison.OrdinalIgnoreCase); + bool clone = command.Equals("clone-repository-quants", StringComparison.OrdinalIgnoreCase); + if (!validation) + { + if (!Directory.EnumerateFiles(model, "*.safetensors").Any()) + throw new InvalidOperationException("The model directory has no top-level .safetensors files."); + string modelConfig = Path.Combine(model, "config.json"); + if (!File.Exists(modelConfig)) throw new InvalidOperationException("The source model is missing config.json."); + using var parsed = JsonDocument.Parse(File.ReadAllText(modelConfig)); + if (parsed.RootElement.ValueKind != JsonValueKind.Object) throw new InvalidOperationException("Model config.json must contain a JSON object."); + if (string.IsNullOrWhiteSpace(config.Identity.ArchitectureFamilyName)) + throw new InvalidOperationException("Set identity.architecture_family_name or --architecture-family explicitly."); + } + string output = validation ? OutputPathService.PredictionValidation(work, Get("output-dir"), config.Output.OutputDir) + : clone ? OutputPathService.Clone(work, Get("output-dir"), config.Output.OutputDir) + : OutputPathService.Pipeline(work, config.Output.OutputDir); + var managed = new List + { + Path.Combine(work, "GGUF"), Path.Combine(work, "Benchmarks"), Path.Combine(work, "Logs"), Path.Combine(work, "Runs"), + Path.Combine(work, config.Paths.ExternalBaselineCacheDirName), Path.Combine(work, ".MagicQuant_tmp"), + Path.Combine(runtime, MagicConstants.LlamaRepoName), Path.Combine(runtime, MagicConstants.EnvName), Path.Combine(runtime, "Runs") + }; + if (!string.IsNullOrWhiteSpace(config.Paths.LlamaRoot)) managed.Add(config.Paths.LlamaRoot); + if (!string.IsNullOrWhiteSpace(config.Paths.LlamaBin)) managed.Add(config.Paths.LlamaBin); + managed.AddRange(config.Paths.ScratchRoots.Select(s => Path.Combine(s, ".MagicQuant_tmp"))); + PathSafety.ValidateExportDirectory(output, model, runtime, managed.ToArray()); + if (clone) + { + string? repo = Get("source-repo") ?? Get("clone-repo"); + string? source = Get("source-json") ?? Get("clone-json"); + if (string.IsNullOrWhiteSpace(repo) == string.IsNullOrWhiteSpace(source)) + throw new InvalidOperationException("Clone requires exactly one of --source-repo or --source-json."); + if (source != null && !IsHttpUrl(source) && !File.Exists(source)) + throw new InvalidOperationException("The local --source-json manifest does not exist."); + } + if (validation && Get("imatrix-path") is { } matrix && !File.Exists(matrix)) + throw new InvalidOperationException("The validation --imatrix-path does not exist."); + if (config.Flags.UseImatrix && config.Imatrix.DatasetLocalFile is { Length: > 0 } dataset && !File.Exists(dataset)) + throw new InvalidOperationException("imatrix.dataset_local_file does not exist."); + } + + private static bool IsHttpUrl(string source) => Uri.TryCreate(source, UriKind.Absolute, out var uri) && uri.Scheme is "http" or "https"; +} diff --git a/MagicQuant/Configuration/ConfigurationShapeValidator.cs b/MagicQuant/Configuration/ConfigurationShapeValidator.cs new file mode 100644 index 0000000..5bb607f --- /dev/null +++ b/MagicQuant/Configuration/ConfigurationShapeValidator.cs @@ -0,0 +1,47 @@ +using System.Collections; +using System.Reflection; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace MagicQuant.Configuration; + +/// Rejects malformed state before normalization or global caches are changed. +public static class ConfigurationShapeValidator +{ + public static void Validate(MagicQuantYamlConfig config) => ValidateObject(config, ""); + + private static void ValidateObject(object value, string path) + { + if (value is double number && !double.IsFinite(number)) + throw new InvalidOperationException($"Configuration '{path}' must be finite."); + if (value is string || value.GetType().IsValueType) + return; + if (value is IDictionary dictionary) + { + foreach (DictionaryEntry entry in dictionary) + if (entry.Value != null) ValidateObject(entry.Value, $"{path}[{entry.Key}]"); + return; + } + if (value is IEnumerable sequence) + { + foreach (var item in sequence) + { + if (item == null) throw new InvalidOperationException($"Configuration '{path}' cannot contain null items."); + ValidateObject(item, path); + } + return; + } + var nullability = new NullabilityInfoContext(); + foreach (var property in value.GetType().GetProperties().Where(p => p.GetCustomAttribute() == null)) + { + string key = UnderscoredNamingConvention.Instance.Apply(property.Name); + string fullKey = path.Length == 0 ? key : $"{path}.{key}"; + // Model-card metadata is intentionally free-form and supports null values. + if (fullKey == "readme.frontmatter") continue; + var member = property.GetValue(value); + if (member == null && nullability.Create(property).ReadState == NullabilityState.NotNull) + throw new InvalidOperationException($"Configuration '{fullKey}' cannot be null. Omit it to use its default."); + if (member != null) ValidateObject(member, fullKey); + } + } +} diff --git a/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/MagicQuant/Configuration/MagicQuantYamlLoader.cs index b23a0a9..83e96c9 100644 --- a/MagicQuant/Configuration/MagicQuantYamlLoader.cs +++ b/MagicQuant/Configuration/MagicQuantYamlLoader.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using System.Text.Json; +using System.Globalization; using MagicQuant.Helpers; using MagicQuant.Models; using MQ.DB; @@ -16,34 +15,44 @@ namespace MagicQuant.Configuration; /// public static class MagicQuantYamlLoader { + public sealed record LoadedConfiguration(string Path, MagicQuantYamlConfig Settings, IReadOnlyList Warnings); + public static MagicQuantYamlConfig LoadAndApply(string commandName, IReadOnlyList args) { - string configPath = ResolveConfigPath(args); - Cache.ActiveConfigPath = configPath; + var loaded = Read(args); + Apply(loaded); + return loaded.Settings; + } + /// Reads and validates configuration without changing globals or creating directories. + public static LoadedConfiguration Read(IReadOnlyList args) + { + string configPath = ResolveConfigPath(args); if (!File.Exists(configPath)) - { - throw new FileNotFoundException( - $"MagicQuant config file was not found at '{configPath}'. " + - "Ensure config.default.yaml is copied next to the build output, or pass --config."); - } - + throw new FileNotFoundException($"MagicQuant config file was not found at '{configPath}'. Pass --config or copy config.default.yaml next to the executable."); + string yaml = File.ReadAllText(configPath); + var warnings = YamlConfigurationDiagnostics.Inspect(yaml); + if (warnings.Count > 0 && args.Any(a => string.Equals(a.Name, "strict-config", StringComparison.OrdinalIgnoreCase))) + throw new InvalidOperationException(string.Join(Environment.NewLine, warnings)); var deserializer = new DeserializerBuilder() .IgnoreUnmatchedProperties() .WithNamingConvention(UnderscoredNamingConvention.Instance) .Build(); + var config = deserializer.Deserialize(yaml) ?? MagicQuantYamlConfig.CreateDefault(); + ConfigurationShapeValidator.Validate(config); + ApplyCliOverrides(config, args); + ConfigurationShapeValidator.Validate(config); + return new LoadedConfiguration(configPath, config, warnings); + } - var yaml = File.ReadAllText(configPath); - RejectLegacyGlobalRelearnYaml(yaml, configPath); - var loaded = deserializer.Deserialize(yaml) ?? MagicQuantYamlConfig.CreateDefault(); - - ApplyCliOverrides(loaded, args); - NormalizeAndApply(loaded); - - Config.Load(loaded); - - AnsiConsole.MarkupLine($"[grey]Using config:[/] {Markup.Escape(configPath)}"); - return loaded; + public static void Apply(LoadedConfiguration loaded) + { + Cache.ActiveConfigPath = loaded.Path; + NormalizeAndApply(loaded.Settings); + Config.Load(loaded.Settings); + AnsiConsole.MarkupLine($"[grey]Using config:[/] {Markup.Escape(loaded.Path)}"); + foreach (string warning in loaded.Warnings) + AnsiConsole.MarkupLine($"[yellow]{Markup.Escape(warning)}[/]"); } public static string ResolveConfigPath(IReadOnlyList args) @@ -89,7 +98,7 @@ private static void NormalizeAndApply(MagicQuantYamlConfig config) Cache.AllowArchitectureFamilyAliasOverride = config.Identity.AllowArchitectureFamilyAliasOverride; - + Cache.CurrentArchitectureFamilyId = null; Cache.CurrentTensorGroupProfileId = null; Cache.CurrentTensorGroupProfileFingerprintHash = null; @@ -265,17 +274,6 @@ private static HashSet ResolveStandardBaselineIds(IEnumerable name } - private static void RejectLegacyGlobalRelearnYaml(string yaml, string configPath) - { - if (yaml.IndexOf("force_relearn_baseline_tensor_mappings", StringComparison.OrdinalIgnoreCase) < 0) - return; - - throw new InvalidOperationException( - $"Config '{configPath}' contains removed option 'flags.force_relearn_baseline_tensor_mappings'. " + - "This global destructive relearn mode has been removed. Use targeted relearn commands under 'learning:' " + - "or per custom include 'force_relearn: true'."); - } - private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList args) { config.Learning ??= new RuntimeLearningConfig(); @@ -315,7 +313,7 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (ulong.TryParse(Get("manual-max-predicted-size-bytes"), out var manualBytes)) config.Prediction.ManualMaxPredictedSizeBytes = manualBytes; - if (double.TryParse(Get("prediction-default-bit-stress-threshold"), out var defaultBitStress) && defaultBitStress > 0d) + if (TryParseFiniteDouble(Get("prediction-default-bit-stress-threshold"), out var defaultBitStress) && defaultBitStress > 0d) config.Prediction.DefaultBitStressThreshold = defaultBitStress; if (int.TryParse(Get("prediction-minimum-fit-rows"), out var minFitRows) && minFitRows >= 2) @@ -325,7 +323,7 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (bitStressCandidates.Count > 0) config.Prediction.BitStressThresholdCandidates = bitStressCandidates; - if (double.TryParse(Get("selection-near-baseline-max-size-growth-percent"), out var nearPct) && nearPct >= 0d) + if (TryParseFiniteDouble(Get("selection-near-baseline-max-size-growth-percent"), out var nearPct) && nearPct >= 0d) config.CandidateSelection.NearBaselineMaxSizeGrowthPercent = nearPct; var windows = ParseDoubleList(Get("selection-interior-window-fractions")); @@ -338,16 +336,16 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (int.TryParse(Get("selection-max-fallback-attempts-per-anchor"), out var maxFallbacks) && maxFallbacks > 0) config.CandidateSelection.MaxFallbackAttemptsPerAnchor = maxFallbacks; - if (double.TryParse(Get("selection-minimum-kld-improvement-epsilon"), out var minKldEpsilon) && minKldEpsilon >= 0d) + if (TryParseFiniteDouble(Get("selection-minimum-kld-improvement-epsilon"), out var minKldEpsilon) && minKldEpsilon >= 0d) config.CandidateSelection.MinimumKldImprovementEpsilon = minKldEpsilon; - if (double.TryParse(Get("selection-minimum-neighbor-gap-fraction"), out var neighborGap) && neighborGap >= 0d) + if (TryParseFiniteDouble(Get("selection-minimum-neighbor-gap-fraction"), out var neighborGap) && neighborGap >= 0d) config.CandidateSelection.MinimumNeighborGapFractionOfGlobalSpan = neighborGap; - if (double.TryParse(Get("selection-near-lower-anchor-brutal-zone-fraction"), out var brutalZone) && brutalZone >= 0d) + if (TryParseFiniteDouble(Get("selection-near-lower-anchor-brutal-zone-fraction"), out var brutalZone) && brutalZone >= 0d) config.CandidateSelection.NearLowerAnchorBrutalZoneFractionOfPairSpan = brutalZone; - if (double.TryParse(Get("selection-near-anchor-required-kld-gain-fraction"), out var brutalGain) && brutalGain >= 0d) + if (TryParseFiniteDouble(Get("selection-near-anchor-required-kld-gain-fraction"), out var brutalGain) && brutalGain >= 0d) config.CandidateSelection.NearAnchorRequiredKldGainFractionOfPairGap = brutalGain; if (Has("allow-eight-bit-anchor-replacements")) @@ -372,7 +370,7 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList config.CandidateSelection.DiversityLowBitOnly = diversityLowBitOnly; config.Output.OutputDir = Prefer(Get("output-dir"), config.Output.OutputDir); - config.Output.OutputNamePrefix = Prefer(Get("output-name-prefix"), config.Output.OutputNamePrefix); + config.Output.OutputNamePrefix = Prefer(Get("output-name-prefix"), config.Output.OutputNamePrefix) ?? "Model"; if (Has("export-external-learned-baselines")) config.Output.ExportExternalLearnedBaselines = true; if (Has("reuse-existing-final-artifacts")) config.Output.ReuseExistingFinalArtifacts = true; @@ -380,6 +378,15 @@ private static void ApplyCliOverrides(MagicQuantYamlConfig config, IReadOnlyList if (Has("allow-architecture-family-alias-override")) config.Identity.AllowArchitectureFamilyAliasOverride = true; } + private static bool TryParseFiniteDouble(string? value, out double result) + { + result = 0; + if (value == null) return false; + if (!double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result) || !double.IsFinite(result)) + throw new ArgumentException($"Invalid numeric option value '{value}'. Use a finite number with a decimal point."); + return true; + } + private static bool TryParseBool(string? value, out bool result) { result = false; @@ -418,7 +425,7 @@ private static List ParseDoubleList(string? value) return value .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) - .Select(x => double.TryParse(x, out var parsed) ? (double?)parsed : null) + .Select(x => TryParseFiniteDouble(x, out var parsed) ? (double?)parsed : null) .Where(x => x.HasValue) .Select(x => x!.Value) .ToList(); @@ -466,7 +473,7 @@ private static List NormalizeScratchRoots(IEnumerable? roots) return roots .Where(x => !string.IsNullOrWhiteSpace(x)) .Select(x => Path.GetFullPath(x.Trim())) - .Distinct(StringComparer.OrdinalIgnoreCase) + .Distinct(OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal) .ToList(); } private static string? NormalizeNullOrFullPath(string? value) diff --git a/MagicQuant/Configuration/YamlConfigurationDiagnostics.cs b/MagicQuant/Configuration/YamlConfigurationDiagnostics.cs new file mode 100644 index 0000000..edd7111 --- /dev/null +++ b/MagicQuant/Configuration/YamlConfigurationDiagnostics.cs @@ -0,0 +1,51 @@ +using System.Reflection; +using YamlDotNet.RepresentationModel; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace MagicQuant.Configuration; + +/// Checks document keys against the typed schema while leaving free-form metadata alone. +public static class YamlConfigurationDiagnostics +{ + public static IReadOnlyList Inspect(string yaml) + { + var stream = new YamlStream(); + stream.Load(new StringReader(yaml)); + if (stream.Documents.Count > 1) + throw new InvalidOperationException("Expected one YAML configuration document."); + var warnings = new List(); + if (stream.Documents.Count == 1) + InspectNode(stream.Documents[0].RootNode, typeof(MagicQuantYamlConfig), "", warnings); + return warnings; + } + + private static void InspectNode(YamlNode node, Type type, string path, List warnings) + { + // Dictionary keys (frontmatter, GPU indices) belong to the user, not the C# schema. + if (type == typeof(object) || type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))) + return; + if (node is YamlSequenceNode sequence && type.IsGenericType) + { + for (int i = 0; i < sequence.Children.Count; i++) + InspectNode(sequence.Children[i], type.GetGenericArguments()[0], $"{path}[{i}]", warnings); + return; + } + if (node is not YamlMappingNode mapping) + return; + var properties = type.GetProperties() + .Where(p => p.GetCustomAttribute() == null) + .ToDictionary(p => UnderscoredNamingConvention.Instance.Apply(p.Name), StringComparer.Ordinal); + foreach (var entry in mapping.Children) + { + string key = ((YamlScalarNode)entry.Key).Value ?? ""; + string fullKey = path.Length == 0 ? key : $"{path}.{key}"; + if (fullKey == "flags.force_relearn_baseline_tensor_mappings") + throw new InvalidOperationException("Removed destructive option flags.force_relearn_baseline_tensor_mappings. Use targeted learning options instead."); + if (!properties.TryGetValue(key, out var property)) + warnings.Add($"Unknown or inactive YAML setting '{fullKey}' (line {entry.Key.Start.Line}). It will be ignored."); + else + InspectNode(entry.Value, property.PropertyType, fullKey, warnings); + } + } +} diff --git a/MagicQuant/Helpers/DependencyManager.cs b/MagicQuant/Helpers/DependencyManager.cs index 172df4e..8f1caa1 100644 --- a/MagicQuant/Helpers/DependencyManager.cs +++ b/MagicQuant/Helpers/DependencyManager.cs @@ -16,7 +16,7 @@ public static class DependencyManager public static async Task EnsureDependenciesAsync(SystemInfo sysInfo) { // 1. Check CMake (Download if missing on Windows) - string cmakePath = GetCmakePath(); + string? cmakePath = GetCmakePath(); if (cmakePath == null) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) @@ -55,7 +55,7 @@ private static async Task ValidateGpuToolkitAsync(SystemInfo sysInfo) AnsiConsole.MarkupLine("[red]CUDA Toolkit not found![/]"); AnsiConsole.MarkupLine("To use your NVIDIA GPU, you must install the CUDA Toolkit."); AnsiConsole.MarkupLine("[link]https://developer.nvidia.com/cuda-downloads[/]"); - + if (!AnsiConsole.Confirm("Have you installed the CUDA Toolkit and are ready to retry?")) { throw new Exception("CUDA Toolkit required for Nvidia build."); @@ -70,12 +70,12 @@ private static async Task ValidateGpuToolkitAsync(SystemInfo sysInfo) } else if (sysInfo.GpuInfo.FirstOrDefault()?.GpuVendor == GpuVendor.Intel) { - if (!CheckCommandExists("icx")) // Intel OneAPI Compiler - { - AnsiConsole.MarkupLine("[yellow]Warning: Intel OneAPI Base Toolkit not found.[/]"); - AnsiConsole.MarkupLine("For optimal Intel performance (SYCL), install OneAPI: [blue]https://www.intel.com/content/www/us/en/developer/tools/oneapi/base-toolkit.html[/]"); - AnsiConsole.MarkupLine("Proceeding with CPU/Vulkan fallback if build fails."); - } + if (!CheckCommandExists("icx")) // Intel OneAPI Compiler + { + AnsiConsole.MarkupLine("[yellow]Warning: Intel OneAPI Base Toolkit not found.[/]"); + AnsiConsole.MarkupLine("For optimal Intel performance (SYCL), install OneAPI: [blue]https://www.intel.com/content/www/us/en/developer/tools/oneapi/base-toolkit.html[/]"); + AnsiConsole.MarkupLine("Proceeding with CPU/Vulkan fallback if build fails."); + } } // AMD on Linux usually handled by "sudo apt install hipcc" or rocm libs } @@ -88,7 +88,7 @@ private static async Task ValidateGpuToolkitAsync(SystemInfo sysInfo) if (CheckCommandExists("cmake")) return "cmake"; // 2. Check Local 'MagicQuant/cmake/bin' - string localPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), + string localPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), MagicConstants.MagicQuantFolder, "cmake", "bin", "cmake.exe"); return File.Exists(localPath) ? localPath : null; } @@ -99,7 +99,7 @@ private static async Task DownloadAndInstallCmakeAsync() string zipPath = Path.Combine(magicPath, "cmake.zip"); string extractPath = Path.Combine(magicPath, "cmake"); - AnsiConsole.Status().Start("Downloading CMake...", ctx => + AnsiConsole.Status().Start("Downloading CMake...", ctx => { using var client = new HttpClient(); var bytes = client.GetByteArrayAsync(CmakeWinUrl).Result; @@ -108,13 +108,13 @@ private static async Task DownloadAndInstallCmakeAsync() AnsiConsole.MarkupLine("Extracting CMake..."); if (Directory.Exists(extractPath)) Directory.Delete(extractPath, true); - + ZipFile.ExtractToDirectory(zipPath, magicPath); - + // Rename the extracted folder (e.g., cmake-3.29-windows...) to just "cmake" var extractedDir = Directory.GetDirectories(magicPath, "cmake-*").First(); Directory.Move(extractedDir, extractPath); - + File.Delete(zipPath); AnsiConsole.MarkupLine("[green]CMake installed successfully.[/]"); } @@ -132,7 +132,7 @@ private static void PromptForVisualStudio() AnsiConsole.Write(new Rule("[red]Missing Visual Studio[/]")); AnsiConsole.MarkupLine("MagicQuant requires [bold]Visual Studio Build Tools 2022[/] with C++ Desktop Development."); AnsiConsole.MarkupLine("[blue]https://visualstudio.microsoft.com/downloads/#build-tools[/]"); - + if (!AnsiConsole.Confirm("Have you installed Visual Studio Build Tools?")) { throw new Exception("Visual Studio is required to compile on Windows."); @@ -141,7 +141,7 @@ private static void PromptForVisualStudio() private static bool CheckCommandExists(string cmd) { - try + try { var psi = new ProcessStartInfo { @@ -157,4 +157,4 @@ private static bool CheckCommandExists(string cmd) } catch { return false; } } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/LinuxHelper.cs b/MagicQuant/Helpers/LinuxHelper.cs index 567ded6..9910aeb 100644 --- a/MagicQuant/Helpers/LinuxHelper.cs +++ b/MagicQuant/Helpers/LinuxHelper.cs @@ -8,7 +8,7 @@ public class LinuxHelper public static async Task RefreshSudoCredentialsAsync() { AnsiConsole.MarkupLine("[grey]Verifying sudo access for system installs...[/]"); - + // "sudo -v" updates the user's cached credentials. // It will prompt for a password if necessary. var psi = new ProcessStartInfo @@ -17,13 +17,20 @@ public static async Task RefreshSudoCredentialsAsync() Arguments = "-v", UseShellExecute = false // Let standard input handle the password prompt }; - - var p = Process.Start(psi); - await p!.WaitForExitAsync(); - + + using var p = Process.Start(psi) ?? throw new InvalidOperationException("Could not start sudo."); + try { await p.WaitForExitAsync(MagicQuant.Runtime.RunCancellation.Token); } + catch (OperationCanceledException) + { + try { if (!p.HasExited) p.Kill(entireProcessTree: true); } + catch (InvalidOperationException) { } + await p.WaitForExitAsync(CancellationToken.None); + throw; + } + if (p.ExitCode != 0) { throw new Exception("Sudo access denied or cancelled."); } } -} \ No newline at end of file +} diff --git a/MagicQuant/Helpers/LlamaBuilder.cs b/MagicQuant/Helpers/LlamaBuilder.cs index ece373d..c0a8885 100644 --- a/MagicQuant/Helpers/LlamaBuilder.cs +++ b/MagicQuant/Helpers/LlamaBuilder.cs @@ -259,27 +259,9 @@ private async Task RunProcessAsync(string exe, IReadOnlyList args, foreach (string arg in args) psi.ArgumentList.Add(arg); - using var p = Process.Start(psi); - if (p == null) - return false; - - p.OutputDataReceived += (_, e) => - { - if (e.Data != null) - AnsiConsole.WriteLine(e.Data); - }; - - p.ErrorDataReceived += (_, e) => - { - if (e.Data != null) - AnsiConsole.WriteLine(e.Data); - }; - - p.BeginOutputReadLine(); - p.BeginErrorReadLine(); - await p.WaitForExitAsync(); - - return p.ExitCode == 0; + var result = await new MagicQuant.Runtime.ProcessRunner().RunAsync(psi, + onLine: (line, _) => AnsiConsole.WriteLine(line)); + return result.Success; } private static string ResolveConvertScriptPath(string llamaRoot) diff --git a/MagicQuant/Helpers/PythonManager.cs b/MagicQuant/Helpers/PythonManager.cs index 75301d3..c381b1a 100644 --- a/MagicQuant/Helpers/PythonManager.cs +++ b/MagicQuant/Helpers/PythonManager.cs @@ -32,44 +32,13 @@ public PythonManager(string basePath) $"except Exception:\n" + $" print('NONE')\n"; - string python = GetPythonExecutable(); - string exe, args; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - exe = "cmd.exe"; - args = $"/c \"{python}\" -c \"{script}\""; - } - else - { - exe = python; - args = $"-c \"{script}\""; - } - - var psi = new ProcessStartInfo - { - FileName = exe, - Arguments = args, - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true - }; - - using var proc = Process.Start(psi) - ?? throw new InvalidOperationException("Failed to start Python process"); - - string stdout = await proc.StandardOutput.ReadToEndAsync(); - string stderr = await proc.StandardError.ReadToEndAsync(); - - await proc.WaitForExitAsync(); - - if (proc.ExitCode != 0) - { - throw new Exception( - $"Python package check failed for '{packageName}'.\n{stderr}" - ); - } + var psi = new ProcessStartInfo(GetPythonExecutable()); + psi.ArgumentList.Add("-c"); + psi.ArgumentList.Add(script); + var result = await new MagicQuant.Runtime.ProcessRunner().RunAsync(psi); + if (!result.Success) + throw new InvalidOperationException($"Python package check failed for '{packageName}'.\n{result.StdErr}"); + string stdout = result.StdOut; string version = stdout.Trim(); @@ -140,7 +109,7 @@ private async Task SetupWindowsEmbedAsync() File.Delete(zipPath); // Modify .pth file to allow importing site-packages (Crucial for pip) - string pthFile = Directory.GetFiles(_envPath, "*._pth").FirstOrDefault(); + string? pthFile = Directory.GetFiles(_envPath, "*._pth").FirstOrDefault(); if (pthFile != null) { var lines = await File.ReadAllLinesAsync(pthFile); @@ -170,7 +139,7 @@ private async Task SetupPipRunnerAsync() { AnsiConsole.MarkupLine("[yellow]Warning: pip_runner.py not found in Helpers.[/]"); } - + string python = GetPythonExecutable(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { @@ -213,24 +182,20 @@ private void WriteSuccessMarker() => public Task RunPythonScriptAsync(string scriptPath, string args = "", Dictionary? envVars = null) { - string python = GetPythonExecutable(); - string exe, finalArgs; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - exe = "cmd.exe"; - finalArgs = $"/c \"\"{python}\" \"{scriptPath}\" {args}\""; - } - else - { - exe = python; - finalArgs = $"\"{scriptPath}\" {args}"; - } + return RunShellCommand(GetPythonExecutable(), $"\"{scriptPath}\" {args}", _envPath, envVars); + } - return RunShellCommand(exe, finalArgs, _envPath, envVars); + public async Task RunPythonScriptAsync(string scriptPath, IReadOnlyList args, + Dictionary? envVars = null, CancellationToken ct = default) + { + var start = new MagicQuant.Runtime.NativeCommand(GetPythonExecutable(), [scriptPath, .. args]).CreateStartInfo(envVars); + start.WorkingDirectory = _envPath; + var result = await new MagicQuant.Runtime.ProcessRunner().RunAsync(start, + onLine: (line, _) => AnsiConsole.WriteLine(line), ct: ct); + if (!result.Success) throw new InvalidOperationException($"Python script '{scriptPath}' failed (exit {result.ExitCode}). {result.StdErr}"); } - // The Method Signature causing the issue + // Legacy string arguments are retained here; ProcessRunner owns native lifetime. private async Task RunShellCommand(string exe, string args, string workingDir, Dictionary? envVars = null) { @@ -251,24 +216,9 @@ private async Task RunShellCommand(string exe, string args, string workingDir, foreach (var kvp in envVars) psi.Environment[kvp.Key] = kvp.Value; - using var proc = Process.Start(psi); - if (proc == null) throw new InvalidOperationException($"Failed to start: {exe}"); - - proc.OutputDataReceived += (s, e) => - { - if (e.Data != null) AnsiConsole.MarkupLine($"[grey]{Markup.Escape(e.Data)}[/]"); - }; - proc.ErrorDataReceived += (s, e) => - { - if (e.Data != null) AnsiConsole.MarkupLine($"[red]{Markup.Escape(e.Data)}[/]"); - }; - - proc.BeginOutputReadLine(); - proc.BeginErrorReadLine(); - - await proc.WaitForExitAsync(); - - if (proc.ExitCode != 0) - throw new Exception($"Command failed (exit {proc.ExitCode}): {exe} {args}"); + var result = await new MagicQuant.Runtime.ProcessRunner().RunAsync(psi, + onLine: (line, error) => AnsiConsole.MarkupLine($"[{(error ? "red" : "grey")}]{Markup.Escape(line)}[/]")); + if (!result.Success) + throw new InvalidOperationException($"Command failed (exit {result.ExitCode}): {exe} {args}"); } -} \ No newline at end of file +} diff --git a/MagicQuant/MagicQuant.csproj b/MagicQuant/MagicQuant.csproj index 8fc994a..a109893 100644 --- a/MagicQuant/MagicQuant.csproj +++ b/MagicQuant/MagicQuant.csproj @@ -12,7 +12,7 @@ - + diff --git a/MagicQuant/Program.cs b/MagicQuant/Program.cs index 1a6d7d9..e99e1fa 100644 --- a/MagicQuant/Program.cs +++ b/MagicQuant/Program.cs @@ -27,6 +27,19 @@ List parsedArgs = CliHelpers.ParseArguments(args.Skip(1)); +using var cancellation = new CancellationTokenSource(); +using var cancellationScope = MagicQuant.Runtime.RunCancellation.Use(cancellation.Token); +ConsoleCancelEventHandler onCancel = (_, e) => +{ + // First Ctrl+C cooperatively unwinds leases/processes; a second uses OS termination. + e.Cancel = !cancellation.IsCancellationRequested; + cancellation.Cancel(); +}; +Console.CancelKeyPress += onCancel; +RunProvenanceService? provenance = null; +string completionStatus = "failed"; +string? completionError = null; + try { // Help is a read-only operation: do not load config, clean caches, install @@ -38,7 +51,18 @@ return; } - var loadedConfig = MagicQuantYamlLoader.LoadAndApply(commandInput, parsedArgs); + CliOptionValidator.Validate(parsedArgs); + var loaded = MagicQuantYamlLoader.Read(parsedArgs); + CommandPreflight.Validate(commandInput, loaded.Settings, parsedArgs); + if (parsedArgs.Any(a => string.Equals(a.Name, "check-config", StringComparison.OrdinalIgnoreCase))) + { + foreach (string warning in loaded.Warnings) AnsiConsole.WriteLine(warning); + AnsiConsole.WriteLine("Configuration and input paths are valid. No runtime setup was performed."); + return; + } + MagicQuantYamlLoader.Apply(loaded); + var loadedConfig = loaded.Settings; + provenance = new RunProvenanceService(commandInput, args, loaded); var startupScratch = new ScratchStorageService(); await startupScratch.CleanupStaleScratchArtifactsAsync(); @@ -71,11 +95,34 @@ await AnsiConsole.Status() AnsiConsole.WriteLine(); } + if (!commandInput.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) + await provenance.CaptureToolchainAsync(); + cancellation.Token.ThrowIfCancellationRequested(); var commandInstance = commandInfo.Factory(); await commandInstance.Run(parsedArgs); + if (commandInput.Equals("initialize-llama-cpp", StringComparison.OrdinalIgnoreCase)) + await provenance.CaptureToolchainAsync(); + cancellation.Token.ThrowIfCancellationRequested(); + completionStatus = "completed"; +} +catch (OperationCanceledException) +{ + AnsiConsole.WriteLine("Run canceled. Active native work has been stopped."); + Environment.ExitCode = 130; + completionStatus = "canceled"; } catch (Exception ex) { + completionError = ex.Message; AnsiConsole.WriteException(ex); Environment.ExitCode = 1; } +finally +{ + Console.CancelKeyPress -= onCancel; + try { provenance?.Complete(completionStatus, completionError); } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + AnsiConsole.WriteLine($"Could not finalize local run provenance: {ex.Message}"); + } +} diff --git a/MagicQuant/Runtime/IProcessRunner.cs b/MagicQuant/Runtime/IProcessRunner.cs new file mode 100644 index 0000000..cabb6f9 --- /dev/null +++ b/MagicQuant/Runtime/IProcessRunner.cs @@ -0,0 +1,10 @@ +using System.Diagnostics; + +namespace MagicQuant.Runtime; + +/// Inject native execution at IO boundaries without mocking numerical policy. +public interface IProcessRunner +{ + Task RunAsync(ProcessStartInfo start, string? logPath = null, + Action? onLine = null, CancellationToken ct = default); +} diff --git a/MagicQuant/Runtime/NativeCommand.cs b/MagicQuant/Runtime/NativeCommand.cs new file mode 100644 index 0000000..66159a3 --- /dev/null +++ b/MagicQuant/Runtime/NativeCommand.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using System.Text.Json; + +namespace MagicQuant.Runtime; + +/// Executable and literal argv; never interpreted by a shell. +public sealed record NativeCommand(string Executable, IReadOnlyList Arguments) +{ + public ProcessStartInfo CreateStartInfo(IReadOnlyDictionary? environment = null) + { + var start = new ProcessStartInfo(Executable); + foreach (string arg in Arguments) start.ArgumentList.Add(arg); + if (environment != null) + foreach (var (key, value) in environment) start.Environment[key] = value; + return start; + } + + // Diagnostic representation only, not a command to execute or shell-escape. + public override string ToString() => JsonSerializer.Serialize(new { Executable, Arguments }); +} diff --git a/MagicQuant/Runtime/ProcessRunner.cs b/MagicQuant/Runtime/ProcessRunner.cs new file mode 100644 index 0000000..02f7307 --- /dev/null +++ b/MagicQuant/Runtime/ProcessRunner.cs @@ -0,0 +1,84 @@ +using System.Diagnostics; +using System.Text; + +namespace MagicQuant.Runtime; + +public sealed record ProcessResult(int ExitCode, string StdOut, string StdErr) +{ + public bool Success => ExitCode == 0; + public string CombinedOutput => StdOut + StdErr; +} + +/// +/// Owns native process lifetime, drains both pipes concurrently, and closes logs on +/// every exit path. Cancellation kills and reaps the child tree before callers may +/// dispose scratch leases. Exit codes remain explicit so each caller owns retry policy. +/// +public sealed class ProcessRunner : IProcessRunner +{ + public async Task RunAsync(ProcessStartInfo start, string? logPath = null, + Action? onLine = null, CancellationToken ct = default) + { + using var cancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, RunCancellation.Token); + ct = cancellation.Token; + ct.ThrowIfCancellationRequested(); + start.RedirectStandardOutput = true; + start.RedirectStandardError = true; + start.UseShellExecute = false; + start.CreateNoWindow = true; + using var log = logPath == null ? null : new StreamWriter(new FileStream(logPath, FileMode.Create, FileAccess.Write, FileShare.Read)) { AutoFlush = true }; + using var process = new Process { StartInfo = start }; + if (!process.Start()) throw new InvalidOperationException($"Failed to start '{start.FileName}'."); + var stdout = new StringBuilder(); + var stderr = new StringBuilder(); + object sync = new(); + + async Task DrainAsync(StreamReader reader, StringBuilder buffer, bool error) + { + try + { + while (await reader.ReadLineAsync() is { } line) + { + lock (sync) + { + buffer.AppendLine(line); + log?.WriteLine(line); + onLine?.Invoke(line, error); + } + } + } + catch + { + // Wake the other drain when this pipe/callback fails. + try { if (!process.HasExited) process.Kill(entireProcessTree: true); } + catch (InvalidOperationException) { } + throw; + } + } + + Task drains = Task.WhenAll(DrainAsync(process.StandardOutput, stdout, false), DrainAsync(process.StandardError, stderr, true)); + try + { + // A logging callback or pipe failure must terminate the child as well, + // rather than letting it hang forever with an undrained output pipe. + Task exited = process.WaitForExitAsync(ct); + Task completed = await Task.WhenAny(exited, drains); + if (completed == drains) await drains; + await exited; + await drains.WaitAsync(ct); + ct.ThrowIfCancellationRequested(); + return new ProcessResult(process.ExitCode, stdout.ToString(), stderr.ToString()); + } + catch + { + if (!process.HasExited) + { + try { process.Kill(entireProcessTree: true); } + catch (InvalidOperationException) { /* The child exited between the check and kill. */ } + } + await process.WaitForExitAsync(CancellationToken.None); + try { await drains; } catch { /* Preserve the original cancellation/pipe failure. */ } + throw; + } + } +} diff --git a/MagicQuant/Runtime/RunCancellation.cs b/MagicQuant/Runtime/RunCancellation.cs new file mode 100644 index 0000000..143fb57 --- /dev/null +++ b/MagicQuant/Runtime/RunCancellation.cs @@ -0,0 +1,24 @@ +namespace MagicQuant.Runtime; + +/// +/// Cancellation for the current async command scope. Legacy service APIs without a +/// token still stop native work; new APIs should also accept explicit caller tokens. +/// This value flows into tasks and is restored when the command finishes. +/// +public static class RunCancellation +{ + private static readonly AsyncLocal Ambient = new(); + public static CancellationToken Token => Ambient.Value; + + public static IDisposable Use(CancellationToken token) + { + var previous = Ambient.Value; + Ambient.Value = token; + return new Scope(previous); + } + + private sealed class Scope(CancellationToken previous) : IDisposable + { + public void Dispose() => Ambient.Value = previous; + } +} diff --git a/MagicQuant/Services/BenchmarkCommands.cs b/MagicQuant/Services/BenchmarkCommands.cs new file mode 100644 index 0000000..4905548 --- /dev/null +++ b/MagicQuant/Services/BenchmarkCommands.cs @@ -0,0 +1,34 @@ +using System.Globalization; +using MagicQuant.Runtime; + +namespace MagicQuant.Services; + +/// Native benchmark arguments, separated from scheduling and measured-truth persistence. +internal static class BenchmarkCommands +{ + public static NativeCommand Bench(string executable, string model, bool gpu, int ngl, string tensorSplit) + { + List args = ["-m", model, "-p", "8", "-t", "16"]; + if (gpu) args.AddRange(["-ngl", ngl.ToString(CultureInfo.InvariantCulture), .. SplitArgs(tensorSplit)]); + else args.AddRange(["-ngl", "0"]); + args.AddRange(["-o", "md"]); + return new NativeCommand(executable, args); + } + + public static NativeCommand Perplexity(string executable, string model, string corpus, bool gpu, + int ngl, string tensorSplit, string? logitsFile = null, bool compareLogits = false) + { + List args = ["-m", model, "-ngl", (gpu ? ngl : 0).ToString(CultureInfo.InvariantCulture)]; + if (gpu) args.AddRange(SplitArgs(tensorSplit)); + args.AddRange(["-t", "4", "-c", "2048", "--file", corpus]); + if (logitsFile != null) + { + args.AddRange(["--kl-divergence-base", logitsFile]); + if (compareLogits) args.Add("--kl-divergence"); + } + return new NativeCommand(executable, args); + } + + // This fragment is emitted only by LlamaGpuArgumentBuilder (flag + numeric vector). + private static string[] SplitArgs(string tensorSplit) => tensorSplit.Split(' ', StringSplitOptions.RemoveEmptyEntries); +} diff --git a/MagicQuant/Services/BenchmarkLogParser.cs b/MagicQuant/Services/BenchmarkLogParser.cs new file mode 100644 index 0000000..acd425a --- /dev/null +++ b/MagicQuant/Services/BenchmarkLogParser.cs @@ -0,0 +1,112 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using MQ.DB.Models; + +namespace MagicQuant.Services; + +/// Parses llama.cpp logs without scheduling work or opening the benchmark database. +public static class BenchmarkLogParser +{ + public static LlamaBenchMetrics ParseLlamaBench(string logPath) + { + var metrics = new LlamaBenchMetrics { LogPath = Path.GetFileName(logPath) }; + if (!File.Exists(logPath)) + return metrics; + + var lines = File.ReadAllLines(logPath); + int headerIdx = -1; + for (int i = 0; i < lines.Length; i++) + { + if (lines[i].Contains("|") && lines[i].Contains("backend")) + { + headerIdx = i; + break; + } + } + + if (headerIdx == -1 || lines.Length <= headerIdx + 2) + return metrics; + + var headers = lines[headerIdx] + .Split('|', StringSplitOptions.RemoveEmptyEntries) + .Select(h => h.Trim()) + .ToList(); + + var dataRow = lines[headerIdx + 2] + .Split('|', StringSplitOptions.RemoveEmptyEntries) + .Select(d => d.Trim()) + .ToList(); + + if (headers.Count != dataRow.Count) + return metrics; + + var row = headers + .Zip(dataRow, (h, d) => new { Header = h, Data = d }) + .ToDictionary(x => x.Header, x => x.Data, StringComparer.OrdinalIgnoreCase); + + string tpsStr = row.ContainsKey("t/s") + ? row["t/s"] + : (row.ContainsKey("tps") ? row["tps"] : "0"); + + var match = Regex.Match(tpsStr, @"([0-9.]+)"); + if (match.Success && + double.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out double tps)) + { + metrics.Tps = tps; + metrics.Backend = row.ContainsKey("backend") ? row["backend"] : "unknown"; + metrics.Test = row.ContainsKey("test") ? row["test"] : "unknown"; + + if (row.ContainsKey("ngl") && + int.TryParse(row["ngl"], NumberStyles.Any, CultureInfo.InvariantCulture, out int ngl)) + { + metrics.Ngl = ngl; + } + } + + return metrics; + } + + public static PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) + { + var metrics = new PplMetrics { LogPath = Path.GetFileName(logPath) }; + + if (!File.Exists(logPath)) + throw new FileNotFoundException($"Perplexity log file was not created: {logPath}"); + + string text = File.ReadAllText(logPath); + string cleanText = StripAnsi(text); + + var pplMatch = Regex.Match( + cleanText, + @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*(?:±|\+/-)\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)", + RegexOptions.IgnoreCase); + + if (!pplMatch.Success) + { + throw new InvalidOperationException( + $"Failed to parse PPL from log: {logPath}\n\nLast log content:\n{cleanText}"); + } + + metrics.Ppl = double.Parse(pplMatch.Groups[1].Value, CultureInfo.InvariantCulture); + metrics.PplError = double.Parse(pplMatch.Groups[2].Value, CultureInfo.InvariantCulture); + + var kldMatch = Regex.Match( + cleanText, + @"(?:Mean\s+KLD|Mean\s+KL|KL[-_\s]*divergence|KLD|kl[-_\s]*div)\s*[:=]\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)", + RegexOptions.IgnoreCase); + + if (kldMatch.Success) + { + metrics.Kld = double.Parse(kldMatch.Groups[1].Value, CultureInfo.InvariantCulture); + } + else if (!allowMissingKld) + { + throw new InvalidOperationException( + $"KLD was expected but could not be parsed from log: {logPath}\n\nLast log content:\n{cleanText}"); + } + + return metrics; + } + + public static string StripAnsi(string text) => Regex.Replace(text, @"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])", ""); +} diff --git a/MagicQuant/Services/BenchmarkService.cs b/MagicQuant/Services/BenchmarkService.cs index 1f2041b..29a1ad9 100644 --- a/MagicQuant/Services/BenchmarkService.cs +++ b/MagicQuant/Services/BenchmarkService.cs @@ -1,3 +1,4 @@ +using MagicQuant.Runtime; using System.Diagnostics; using System.Globalization; using System.Runtime.InteropServices; @@ -92,6 +93,9 @@ public async Task EnsureExecutionPlanAsync( bool forceRediscovery = false, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); await EnsureDynamicExecutionPlanAsync( q8ModelPath: q8ModelPath, nativeModelPath: q8ModelPath, @@ -111,6 +115,9 @@ public async Task EnsureDynamicExecutionPlanAsync( bool forceRediscovery = false, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(q8ModelPath)) throw new ArgumentException("Q8 model path was null or empty.", nameof(q8ModelPath)); if (string.IsNullOrWhiteSpace(nativeModelPath)) @@ -238,6 +245,9 @@ public async Task TryInitializeDynamicExecutionPlanFromCacheAsync( string? preferredPlanModelPath = null, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(q8QuantizationKey)) throw new ArgumentException("Quantization key was null or empty.", nameof(q8QuantizationKey)); @@ -277,6 +287,9 @@ public async Task ClampStaticNglWithBaseModelAsync( int discoveryTokenTarget = 8192, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(baseModelPath)) throw new ArgumentException("Base model path was null or empty.", nameof(baseModelPath)); @@ -308,7 +321,7 @@ public async Task ClampStaticNglWithBaseModelAsync( int? chosen = null; AnsiConsole.Write(new Rule("[yellow]Clamping Static ngl With Base Model[/]") - { Justification = Justify.Left }); + { Justification = Justify.Left }); AnsiConsole.MarkupLine($"[grey]Base model:[/] {Markup.Escape(baseModelPath)}"); AnsiConsole.MarkupLine($"[grey]Starting from Q8-discovered ngl:[/] [cyan]{startingNgl}[/]"); @@ -716,12 +729,11 @@ private async Task ProbePerplexitySampleAsync( probeRoot, $"probe_ppl_{phase}_{slot.ProfileName}_gpu{devices}_ngl{fixedNgl}.log"); - string cmd = slot.UsesGpu - ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli)} -t 4 -c 2048 --file \"{corpusPath}\"" - : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\""; + var cmd = BenchmarkCommands.Perplexity(_bins.Ppl, modelPath, corpusPath, slot.UsesGpu, + fixedNgl, BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli)); var stopwatch = Stopwatch.StartNew(); - var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); + var result = await RunNativeCommandAsync(cmd, logFile, slot.BuildProcessEnv()); stopwatch.Stop(); if (!result.Success) @@ -729,8 +741,8 @@ private async Task ProbePerplexitySampleAsync( try { - var parsed = ParsePerplexity(logFile, allowMissingKld: true); - string clean = StripAnsi(result.LogOutput); + var parsed = BenchmarkLogParser.ParsePerplexity(logFile, allowMissingKld: true); + string clean = BenchmarkLogParser.StripAnsi(result.LogOutput); var passMatch = Regex.Match( clean, @"([0-9]+(?:\.[0-9]+)?)\s+seconds per pass", @@ -1067,18 +1079,17 @@ private async Task ProbeLlamaBenchAtFixedNglAsync( probeRoot, $"probe_llamabench_slot{slot.SlotId}_g{slot.DeviceCount}_ngl{fixedNgl}.md"); - string cmd = slot.UsesGpu - ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.LlamaBench)} -o md" - : $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; + var cmd = BenchmarkCommands.Bench(_bins.Bench, modelPath, slot.UsesGpu, + fixedNgl, BuildTensorSplitArgs(slot, LlamaGpuTool.LlamaBench)); - var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); + var result = await RunNativeCommandAsync(cmd, logFile, slot.BuildProcessEnv()); if (!result.Success) return false; try { - var parsed = ParseLlamaBench(logFile); + var parsed = BenchmarkLogParser.ParseLlamaBench(logFile); return parsed.Tps.HasValue && parsed.Tps.Value > 0; } catch @@ -1098,18 +1109,17 @@ private async Task ProbePerplexityAtFixedNglAsync( probeRoot, $"probe_ppl_general_slot{slot.SlotId}_g{slot.DeviceCount}_ngl{fixedNgl}.log"); - string cmd = slot.UsesGpu - ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli)} -t 4 -c 2048 --file \"{corpusPath}\"" - : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\""; + var cmd = BenchmarkCommands.Perplexity(_bins.Ppl, modelPath, corpusPath, slot.UsesGpu, + fixedNgl, BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli)); - var result = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); + var result = await RunNativeCommandAsync(cmd, logFile, slot.BuildProcessEnv()); if (!result.Success) return false; try { - var parsed = ParsePerplexity(logFile, allowMissingKld: true); + var parsed = BenchmarkLogParser.ParsePerplexity(logFile, allowMissingKld: true); return parsed.Ppl > 0; } catch @@ -1123,6 +1133,9 @@ private async Task ProbePerplexityAtFixedNglAsync( bool allowIndependentTopology, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (_currentPlan == null) throw new InvalidOperationException( "Benchmark execution plan has not been initialized. Call EnsureExecutionPlanAsync() first."); @@ -1434,7 +1447,7 @@ await SaveBenchmarkToDbAsync( LastSuccessfulNgl = initialNgl }; AnsiConsole.MarkupLine( - $"[grey]Dynamic NGL:[/] model={Markup.Escape(Path.GetFileName(modelPath))}, size={(modelSizeBytes / 1024d / 1024d / 1024d):F2} GB, q8={( _currentPlan.Q8ModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.Q8StableNgl}, native={(_currentPlan.NativeModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.NativeStableNgl}, slot={Markup.Escape(slot.DisplayName)}, chosen={initialNgl}"); + $"[grey]Dynamic NGL:[/] model={Markup.Escape(Path.GetFileName(modelPath))}, size={(modelSizeBytes / 1024d / 1024d / 1024d):F2} GB, q8={(_currentPlan.Q8ModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.Q8StableNgl}, native={(_currentPlan.NativeModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.NativeStableNgl}, slot={Markup.Escape(slot.DisplayName)}, chosen={initialNgl}"); var result = new BenchmarkResult { @@ -1515,7 +1528,7 @@ await SaveBenchmarkToDbAsync( Error = null }); } - catch (Exception ex) + catch (Exception ex) when (ex is not OperationCanceledException) { sw.Stop(); @@ -1590,7 +1603,7 @@ private async Task RunAllBenchmarksTransientAsync( LastSuccessfulNgl = initialNgl }; AnsiConsole.MarkupLine( - $"[grey]Dynamic NGL:[/] model={Markup.Escape(Path.GetFileName(modelPath))}, size={(modelSizeBytes / 1024d / 1024d / 1024d):F2} GB, q8={( _currentPlan.Q8ModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.Q8StableNgl}, native={(_currentPlan.NativeModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.NativeStableNgl}, slot={Markup.Escape(slot.DisplayName)}, chosen={initialNgl}"); + $"[grey]Dynamic NGL:[/] model={Markup.Escape(Path.GetFileName(modelPath))}, size={(modelSizeBytes / 1024d / 1024d / 1024d):F2} GB, q8={(_currentPlan.Q8ModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.Q8StableNgl}, native={(_currentPlan.NativeModelSizeBytes / 1024d / 1024d / 1024d):F2} GB/{_currentPlan.NativeStableNgl}, slot={Markup.Escape(slot.DisplayName)}, chosen={initialNgl}"); var result = new BenchmarkResult { @@ -1664,6 +1677,9 @@ private async Task RunAllBenchmarksTransientAsync( HybridQuant quantConfig, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); var currentHashStr = Cache.CurrentModelId; if (string.IsNullOrWhiteSpace(currentHashStr)) throw new InvalidOperationException("Cache.CurrentModelId is not set."); @@ -1695,6 +1711,9 @@ private async Task GetOrCreateTensorComboAsync( HybridQuant quant, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); var c = (TensorConfig)quant; var existing = await db.TensorCombos.FirstOrDefaultAsync(x => @@ -1874,7 +1893,7 @@ private async Task SaveBenchmarkToDbAsync( await transaction.CommitAsync(); } - catch (Exception ex) + catch (Exception ex) when (ex is not OperationCanceledException) { await transaction.RollbackAsync(); @@ -2110,7 +2129,7 @@ private bool TryReadExistingLlamaBenchLog(string logPath, out LlamaBenchMetrics try { - var parsed = ParseLlamaBench(logPath); + var parsed = BenchmarkLogParser.ParseLlamaBench(logPath); if (parsed.Tps.HasValue && parsed.Tps.Value > 0) { metrics = parsed; @@ -2140,7 +2159,7 @@ private bool TryReadExistingPplLog( try { - var parsed = ParsePerplexity(logPath, allowMissingKld); + var parsed = BenchmarkLogParser.ParsePerplexity(logPath, allowMissingKld); if (parsed.Ppl <= 0) return false; @@ -2238,9 +2257,8 @@ private async Task RunLlamaBenchAsync( { string logFile = Path.Combine(benchDir, "llamabench.md"); - string cmd = slot.UsesGpu - ? $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.LlamaBench)} -o md" - : $"\"{_bins.Bench}\" -m \"{modelPath}\" -p 8 -t 16 -backend cpu -o md"; + var cmd = BenchmarkCommands.Bench(_bins.Bench, modelPath, slot.UsesGpu, + fixedNgl, BuildTensorSplitArgs(slot, LlamaGpuTool.LlamaBench)); await RunFixedCommandWithRetryAsync( label: "llama-bench", @@ -2250,7 +2268,7 @@ await RunFixedCommandWithRetryAsync( attempts: 2, requirePplMarker: false); - var parsed = ParseLlamaBench(logFile); + var parsed = BenchmarkLogParser.ParseLlamaBench(logFile); if (!parsed.Tps.HasValue || parsed.Tps.Value <= 0) { throw new InvalidOperationException( @@ -2272,7 +2290,7 @@ private async Task RunPplBenchmarkAsync( { string logFile = Path.Combine(benchDir, $"perplexity_{domain}.log"); - string kldArgs = ""; + string? logitsPath = null; bool expectKld = false; if (!string.IsNullOrEmpty(klLogitsDir)) @@ -2281,19 +2299,18 @@ private async Task RunPplBenchmarkAsync( if (saveLogits) { - kldArgs = $"--kl-divergence-base \"{logitsFile}\""; + logitsPath = logitsFile; expectKld = false; } else if (File.Exists(logitsFile)) { - kldArgs = $"--kl-divergence-base \"{logitsFile}\" --kl-divergence"; + logitsPath = logitsFile; expectKld = true; } } - string cmd = slot.UsesGpu - ? $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl {fixedNgl}{BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli)} -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}" - : $"\"{_bins.Ppl}\" -m \"{modelPath}\" -ngl 0 -t 4 -c 2048 --file \"{corpusPath}\" {kldArgs}"; + var cmd = BenchmarkCommands.Perplexity(_bins.Ppl, modelPath, corpusPath, slot.UsesGpu, + fixedNgl, BuildTensorSplitArgs(slot, LlamaGpuTool.CommonCli), logitsPath, expectKld); await RunFixedCommandWithRetryAsync( label: $"perplexity-{domain}", @@ -2304,7 +2321,7 @@ await RunFixedCommandWithRetryAsync( requirePplMarker: true); bool allowMissingKld = !expectKld; - var parsed = ParsePerplexity(logFile, allowMissingKld); + var parsed = BenchmarkLogParser.ParsePerplexity(logFile, allowMissingKld); if (expectKld && !HasMeaningfulKld(parsed.Kld)) { @@ -2347,7 +2364,7 @@ private async Task RunPplBenchmarkWithNglFallbackAsync( runtimeNgl.LastSuccessfulNgl = ngl; return metrics; } - catch (Exception ex) + catch (Exception ex) when (ex is not OperationCanceledException) { lastException = ex; string content = ex.ToString(); @@ -2366,7 +2383,7 @@ private async Task RunPplBenchmarkWithNglFallbackAsync( private async Task RunFixedCommandWithRetryAsync( string label, - string cmd, + NativeCommand cmd, string logFile, BenchmarkSlot slot, int attempts, @@ -2376,7 +2393,7 @@ private async Task RunFixedCommandWithRetryAsync( for (int attempt = 1; attempt <= attempts; attempt++) { - last = await RunShellCommandAsync(cmd, logFile, slot.BuildProcessEnv()); + last = await RunNativeCommandAsync(cmd, logFile, slot.BuildProcessEnv()); string logContent = !string.IsNullOrWhiteSpace(last.LogOutput) ? last.LogOutput @@ -2420,7 +2437,7 @@ private bool LooksLikeSuccessfulPerplexityRun(string logFile, string logContent) try { - var parsed = ParsePerplexity(logFile, allowMissingKld: true); + var parsed = BenchmarkLogParser.ParsePerplexity(logFile, allowMissingKld: true); return parsed.Ppl > 0; } catch @@ -2446,12 +2463,10 @@ private static bool LooksLikeRetryableGpuFailure(string logContent) return false; } - private static int ExtractNglFromCommand(string cmd) + private static int ExtractNglFromCommand(NativeCommand cmd) { - var match = Regex.Match(cmd, @"(?:\s-ngl\s+)(\d+)", RegexOptions.IgnoreCase); - if (match.Success && int.TryParse(match.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int ngl)) - return ngl; - + for (int i = 0; i < cmd.Arguments.Count - 1; i++) + if (cmd.Arguments[i] == "-ngl" && int.TryParse(cmd.Arguments[i + 1], out int ngl)) return ngl; return 0; } @@ -2459,107 +2474,6 @@ private static int ExtractNglFromCommand(string cmd) // Parsers // ---------------------------------------------------------------- - private LlamaBenchMetrics ParseLlamaBench(string logPath) - { - var metrics = new LlamaBenchMetrics { LogPath = GetRelativePath(logPath) }; - if (!File.Exists(logPath)) - return metrics; - - var lines = File.ReadAllLines(logPath); - int headerIdx = -1; - for (int i = 0; i < lines.Length; i++) - { - if (lines[i].Contains("|") && lines[i].Contains("backend")) - { - headerIdx = i; - break; - } - } - - if (headerIdx == -1 || lines.Length <= headerIdx + 2) - return metrics; - - var headers = lines[headerIdx] - .Split('|', StringSplitOptions.RemoveEmptyEntries) - .Select(h => h.Trim()) - .ToList(); - - var dataRow = lines[headerIdx + 2] - .Split('|', StringSplitOptions.RemoveEmptyEntries) - .Select(d => d.Trim()) - .ToList(); - - if (headers.Count != dataRow.Count) - return metrics; - - var row = headers - .Zip(dataRow, (h, d) => new { Header = h, Data = d }) - .ToDictionary(x => x.Header, x => x.Data, StringComparer.OrdinalIgnoreCase); - - string tpsStr = row.ContainsKey("t/s") - ? row["t/s"] - : (row.ContainsKey("tps") ? row["tps"] : "0"); - - var match = Regex.Match(tpsStr, @"([0-9.]+)"); - if (match.Success && - double.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out double tps)) - { - metrics.Tps = tps; - metrics.Backend = row.ContainsKey("backend") ? row["backend"] : "unknown"; - metrics.Test = row.ContainsKey("test") ? row["test"] : "unknown"; - - if (row.ContainsKey("ngl") && - int.TryParse(row["ngl"], NumberStyles.Any, CultureInfo.InvariantCulture, out int ngl)) - { - metrics.Ngl = ngl; - } - } - - return metrics; - } - - private PplMetrics ParsePerplexity(string logPath, bool allowMissingKld) - { - var metrics = new PplMetrics { LogPath = GetRelativePath(logPath) }; - - if (!File.Exists(logPath)) - throw new FileNotFoundException($"Perplexity log file was not created: {logPath}"); - - string text = File.ReadAllText(logPath); - string cleanText = StripAnsi(text); - - var pplMatch = Regex.Match( - cleanText, - @"(?:Mean PPL\(Q\)|PPL)\s*[:=]\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*(?:±|\+/-)\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)", - RegexOptions.IgnoreCase); - - if (!pplMatch.Success) - { - throw new InvalidOperationException( - $"Failed to parse PPL from log: {logPath}\n\nLast log content:\n{cleanText}"); - } - - metrics.Ppl = double.Parse(pplMatch.Groups[1].Value, CultureInfo.InvariantCulture); - metrics.PplError = double.Parse(pplMatch.Groups[2].Value, CultureInfo.InvariantCulture); - - var kldMatch = Regex.Match( - cleanText, - @"(?:Mean\s+KLD|Mean\s+KL|KL[-_\s]*divergence|KLD|kl[-_\s]*div)\s*[:=]\s*([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)", - RegexOptions.IgnoreCase); - - if (kldMatch.Success) - { - metrics.Kld = double.Parse(kldMatch.Groups[1].Value, CultureInfo.InvariantCulture); - } - else if (!allowMissingKld) - { - throw new InvalidOperationException( - $"KLD was expected but could not be parsed from log: {logPath}\n\nLast log content:\n{cleanText}"); - } - - return metrics; - } - // ---------------------------------------------------------------- // Corpus preparation // ---------------------------------------------------------------- @@ -2625,18 +2539,9 @@ with open(out_path, 'w', encoding='utf-8') as f: string scriptPath = Path.Combine(Path.GetDirectoryName(outPath)!, $"gen_{domain}.py"); await File.WriteAllTextAsync(scriptPath, pyScript); - string pythonExe = _pyManager.GetPythonExecutable(); - string args = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? $"/c \"{pythonExe}\" \"{scriptPath}\"" - : $"\"{scriptPath}\""; - - string runner = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : pythonExe; - await _pyManager.RunPipInstallAsync("datasets"); - var generationResult = await RunShellCommandAsync(runner + " " + args, null); - - if (File.Exists(scriptPath)) - File.Delete(scriptPath); + var generationResult = await RunNativeCommandAsync( + new NativeCommand(_pyManager.GetPythonExecutable(), [scriptPath]), null); if (!generationResult.Success) { @@ -2696,77 +2601,21 @@ private sealed class CommandRunResult public string LogOutput { get; init; } = string.Empty; } - private async Task RunShellCommandAsync( - string cmd, + private async Task RunNativeCommandAsync( + NativeCommand cmd, string? logPath, IReadOnlyDictionary? extraEnv = null) { - var startInfo = new ProcessStartInfo - { - FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/bash", - Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"/c {cmd}" : $"-c \"{cmd}\"", - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true - }; - - if (extraEnv != null) - { - foreach (var kvp in extraEnv) - { - startInfo.Environment[kvp.Key] = kvp.Value; - } - } - - using var process = new Process { StartInfo = startInfo }; - FileStream? fs = null; - StreamWriter? sw = null; - - if (logPath != null) - { - fs = new FileStream(logPath, FileMode.Create, FileAccess.Write, FileShare.Read); - sw = new StreamWriter(fs) { AutoFlush = true }; - } - - process.Start(); - - var stdoutTask = process.StandardOutput.ReadToEndAsync(); - var stderrTask = process.StandardError.ReadToEndAsync(); - - await process.WaitForExitAsync(); - - string stdout = await stdoutTask; - string stderr = await stderrTask; - - if (!string.IsNullOrWhiteSpace(stdout)) - sw?.WriteLine(stdout); - - if (!string.IsNullOrWhiteSpace(stderr)) - sw?.WriteLine(stderr); - - sw?.Dispose(); - fs?.Dispose(); - - string combinedLog; - if (logPath != null && File.Exists(logPath)) - combinedLog = File.ReadAllText(logPath); - else - combinedLog = $"{stdout}\n{stderr}"; - + var startInfo = cmd.CreateStartInfo(extraEnv); + var result = await new MagicQuant.Runtime.ProcessRunner().RunAsync(startInfo, logPath); return new CommandRunResult { - Success = process.ExitCode == 0, - ExitCode = process.ExitCode, - LogOutput = combinedLog + Success = result.Success, + ExitCode = result.ExitCode, + LogOutput = result.CombinedOutput }; } - private string StripAnsi(string text) - { - return Regex.Replace(text, @"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])", ""); - } - private string GetRelativePath(string fullPath) { return Path.GetFileName(fullPath); diff --git a/MagicQuant/Services/FinalArtifactNamingService.cs b/MagicQuant/Services/FinalArtifactNamingService.cs index 43080cc..255ccde 100644 --- a/MagicQuant/Services/FinalArtifactNamingService.cs +++ b/MagicQuant/Services/FinalArtifactNamingService.cs @@ -396,7 +396,7 @@ private string BuildProviderQuantFallback( ? (snapshot.IsHybrid ? "MagicQuant" : HybridBenchmarkRepository.ResolveProviderName(snapshot.Quant, exportNaming: false)) : string.Empty); - string resolvedFamily = quantFamily; + string? resolvedFamily = quantFamily; if (string.IsNullOrWhiteSpace(resolvedFamily) && snapshot != null) resolvedFamily = snapshot.BaselineFamily; if (string.IsNullOrWhiteSpace(resolvedFamily) && snapshot != null && context != null) @@ -514,4 +514,4 @@ public sealed class ProviderCredit public string Name { get; init; } = string.Empty; public string Url { get; init; } = string.Empty; public string Note { get; init; } = string.Empty; -} \ No newline at end of file +} diff --git a/MagicQuant/Services/GgufMetadataReader.cs b/MagicQuant/Services/GgufMetadataReader.cs index ee061a3..2ecd3c7 100644 --- a/MagicQuant/Services/GgufMetadataReader.cs +++ b/MagicQuant/Services/GgufMetadataReader.cs @@ -86,7 +86,7 @@ with open(output_path, "w", encoding="utf-8") as f: """; await File.WriteAllTextAsync(scriptPath, py, ct); - await _python.RunPythonScriptAsync(scriptPath, $"\"{payloadPath}\""); + await _python.RunPythonScriptAsync(scriptPath, [payloadPath], ct: ct); var result = JsonSerializer.Deserialize( await File.ReadAllTextAsync(resultPath, ct)); diff --git a/MagicQuant/Services/HuggingFaceBaselineService.cs b/MagicQuant/Services/HuggingFaceBaselineService.cs index 58b1c6d..0d8e901 100644 --- a/MagicQuant/Services/HuggingFaceBaselineService.cs +++ b/MagicQuant/Services/HuggingFaceBaselineService.cs @@ -22,6 +22,9 @@ public HuggingFaceBaselineService(PythonManager python) public async Task> PrecheckAndRegisterConfiguredBaselinesAsync(CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); await EnsureHubSupportAsync(); int architectureFamilyId = Cache.CurrentArchitectureFamilyId @@ -294,6 +297,9 @@ private static byte ResolveDynamicBaselineId( public async Task DownloadBaselineAsync(BaselineQuants baseline, string destinationPath, bool forceRedownload = false, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (!baseline.IsExternalRepositoryBaseline) throw new InvalidOperationException($"Baseline '{baseline.Names[0]}' is not an external repository baseline."); @@ -519,6 +525,9 @@ public async Task DownloadRepositoryFileAsync( bool forceRedownload = true, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); await EnsureHubSupportAsync(); if (string.IsNullOrWhiteSpace(repoId)) diff --git a/MagicQuant/Services/ImatrixService.cs b/MagicQuant/Services/ImatrixService.cs index c5bf8c4..a88cf69 100644 --- a/MagicQuant/Services/ImatrixService.cs +++ b/MagicQuant/Services/ImatrixService.cs @@ -19,6 +19,9 @@ public sealed class ImatrixService public async Task EnsureImatrixAsync(ImatrixRequest request, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); AnsiConsole.MarkupLine("[grey]Imatrix: starting ensure flow...[/]"); if (!request.UseImatrix) @@ -489,8 +492,8 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s $"-m \"{baseModelPath}\" " + $"-f \"{datasetPath}\" " + $"-o \"{datPath}\" " + - // $"-b 128 " + - // $"-ub 64 " + + // $"-b 128 " + + // $"-ub 64 " + $"-fa off", RedirectStandardOutput = true, RedirectStandardError = true, @@ -498,9 +501,9 @@ private static async Task BuildImatrixFromDatasetTextAsync(string datasetPath, s }; psi.Environment["GGML_CUDA_DISABLE_GRAPHS"] = "1"; - + string launchedCommand = $"\"{imatrixBin}\" {psi.Arguments}"; - + AnsiConsole.MarkupLine($"[grey]Imatrix: launching command:[/] [cyan]{Markup.Escape(launchedCommand)}[/]"); using var p = System.Diagnostics.Process.Start(psi) @@ -608,10 +611,9 @@ private static async Task ExportLocalDatasetToCorpusA if (ext == ".jsonl") { using var reader = new StreamReader(datasetPath, Encoding.UTF8); - while (!reader.EndOfStream) + while (await reader.ReadLineAsync(ct) is { } line) { ct.ThrowIfCancellationRequested(); - string? line = await reader.ReadLineAsync(); if (string.IsNullOrWhiteSpace(line)) continue; @@ -931,4 +933,4 @@ private static string ResolvePythonExecutableOrThrow() return pythonExe; } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/NativeModelConversionService.cs b/MagicQuant/Services/NativeModelConversionService.cs new file mode 100644 index 0000000..a064c34 --- /dev/null +++ b/MagicQuant/Services/NativeModelConversionService.cs @@ -0,0 +1,127 @@ +using MagicQuant.Helpers; +using MagicQuant.Runtime; +using MQ.DB; +using Spectre.Console; + +namespace MagicQuant.Services; + +/// +/// Owns native GGUF conversion and its success-marker lifecycle. It has no benchmark +/// or learned-truth dependency, so conversion can be exercised independently. +/// +public sealed class NativeModelConversionService(ModelArtifactPathService paths, PythonManager python, IProcessRunner? runner = null) +{ + private readonly ModelArtifactPathService _paths = paths; + private readonly PythonManager _python = python; + private readonly IProcessRunner _runner = runner ?? new ProcessRunner(); + private static readonly SemaphoreSlim BaseModelLock = new(1, 1); + + public async Task EnsureAsync(bool deleteProcess = false) + { + await BaseModelLock.WaitAsync(MagicQuant.Runtime.RunCancellation.Token); + try + { + string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; + var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; + string typeStr = torchType.ToString(); + + string fileName = $"{modelName}-{typeStr}.gguf"; + string outputPath = Path.Combine(_paths.GgufDir, fileName); + string successFile = Path.Combine(_paths.GgufDir, $"{fileName}.success.json"); + string convertLogPath = outputPath + ".convert.log"; + + if (deleteProcess) + { + if (!Directory.Exists(_paths.GgufDir)) + Directory.CreateDirectory(_paths.GgufDir); + + var normalizedFileName = Path.GetFileName(fileName); + var successFileName = normalizedFileName + ".success.json"; + var successFilePath = Path.Combine(_paths.GgufDir, successFileName); + bool isImmune = File.Exists(successFilePath); + + foreach (var filePath in Directory.EnumerateFiles(_paths.GgufDir, "*.gguf", SearchOption.TopDirectoryOnly)) + { + var currentFileName = Path.GetFileName(filePath); + var currentModelName = Path.GetFileNameWithoutExtension(currentFileName); + + if (isImmune && + string.Equals(currentFileName, normalizedFileName, StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + if (!string.IsNullOrWhiteSpace(currentModelName) && IsProtectedModel(currentModelName)) + { + continue; + } + + await HardDeleteHelper.DeleteFileIfExistsAsync(filePath); + } + } + + if (!File.Exists(outputPath) || new FileInfo(outputPath).Length == 0 || !File.Exists(successFile)) + { + AnsiConsole.MarkupLine($"[bold cyan]Converting to {Markup.Escape(typeStr)}...[/]"); + + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + + string convertScript = Cache.ConvertScript + ?? throw new Exception("ConvertScript path missing in Cache"); + + string outTypeArg = typeStr.ToLowerInvariant(); + + var psi = new MagicQuant.Runtime.NativeCommand(_python.GetPythonExecutable(), + [convertScript, Cache.ModelDirectory!, "--outtype", outTypeArg, "--outfile", outputPath]).CreateStartInfo(); + psi.WorkingDirectory = Cache.LlamaRoot; + + MagicQuant.Runtime.ProcessResult result; + try + { + result = await _runner.RunAsync(psi, convertLogPath, (line, _) => { if (Cache.VerboseProcessOutput) AnsiConsole.WriteLine(line); }, RunCancellation.Token); + } + catch + { + // No success marker may survive an interrupted/failed conversion. + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + await HardDeleteHelper.DeleteFileIfExistsAsync(successFile); + throw; + } + + if (result.ExitCode != 0) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + + throw new Exception( + $"{typeStr} conversion failed. ExitCode={result.ExitCode}. See '{convertLogPath}'."); + } + + if (!File.Exists(outputPath) || new FileInfo(outputPath).Length == 0) + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + + throw new InvalidOperationException( + $"Conversion exited successfully but produced no valid GGUF output: {outputPath}"); + } + + await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); + } + + return outputPath; + } + finally + { + BaseModelLock.Release(); + } + } + + + private static bool IsProtectedModel(string name) + { + return name.EndsWith("BF16", StringComparison.OrdinalIgnoreCase) || + name.EndsWith("F16", StringComparison.OrdinalIgnoreCase) || + name.EndsWith("F32", StringComparison.OrdinalIgnoreCase) || + name.EndsWith("Q8_0", StringComparison.OrdinalIgnoreCase); + } + +} diff --git a/MagicQuant/Services/PathSafety.cs b/MagicQuant/Services/PathSafety.cs new file mode 100644 index 0000000..5bbafa3 --- /dev/null +++ b/MagicQuant/Services/PathSafety.cs @@ -0,0 +1,49 @@ +namespace MagicQuant.Services; + +/// Filesystem-aware containment checks for directories that the program may clean. +public static class PathSafety +{ + private static StringComparison Comparison => OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + public static string ResolvePhysicalPath(string path) + { + string full = Path.GetFullPath(path); + string current = Path.GetPathRoot(full)!; + foreach (string part in full[current.Length..].Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)) + { + current = Path.Combine(current, part); + var info = new DirectoryInfo(current); + if (info.LinkTarget != null) + current = info.ResolveLinkTarget(returnFinalTarget: true)?.FullName + ?? throw new IOException($"Cannot resolve directory link '{current}'."); + } + return Path.TrimEndingDirectorySeparator(current); + } + + public static bool Contains(string parent, string child) + { + string root = ResolvePhysicalPath(parent); + string candidate = ResolvePhysicalPath(child); + return string.Equals(root, candidate, Comparison) || + candidate.StartsWith(Path.EndsInDirectorySeparator(root) ? root : root + Path.DirectorySeparatorChar, Comparison); + } + + public static void ValidateExportDirectory(string output, string model, string runtimeRoot, params string[] managedDirectories) + { + // An export cannot contain source/runtime data, nor live inside managed working data. + foreach (string protectedPath in new[] { model, runtimeRoot, Path.Combine(model, "MagicQuant") }.Concat(managedDirectories)) + if (Contains(output, protectedPath)) + throw new InvalidOperationException($"Output '{output}' contains protected data '{protectedPath}'. Choose a dedicated export directory."); + foreach (string managed in managedDirectories) + if (Contains(managed, output)) + throw new InvalidOperationException($"Output '{output}' overlaps managed artifacts '{managed}'. Choose a dedicated export directory."); + if (File.Exists(output)) + throw new InvalidOperationException($"Output '{output}' is a file, not a directory."); + } + + public static void ValidateFolderName(string name, string setting) + { + if (string.IsNullOrWhiteSpace(name) || name is "." or ".." || name.IndexOfAny(['/', '\\', ':']) >= 0 || name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) + throw new InvalidOperationException($"{setting} must be one folder/file name, not a path."); + } +} diff --git a/MagicQuant/Services/QuantizationConcurrencyPlan.cs b/MagicQuant/Services/QuantizationConcurrencyPlan.cs new file mode 100644 index 0000000..4ddcd5e --- /dev/null +++ b/MagicQuant/Services/QuantizationConcurrencyPlan.cs @@ -0,0 +1,16 @@ +namespace MagicQuant.Services; + +/// CPU and storage writer limits, independent of model IO and benchmark execution. +public sealed record QuantizationConcurrencyPlan(int ReservedThreads, int UsableThreads, + int NaturalConcurrency, int Concurrency, int ThreadsPerProcess) +{ + public static QuantizationConcurrencyPlan Create(int threadCount, int scratchWriterCapacity) + { + const int minimumThreadsPerProcess = 4; + int reserved = threadCount switch { >= 8 => 2, >= 4 => 1, _ => 0 }; + int usable = Math.Max(1, threadCount - reserved); + int natural = Math.Max(1, usable / minimumThreadsPerProcess); + int concurrent = Math.Max(1, Math.Min(natural, scratchWriterCapacity)); + return new(reserved, usable, natural, concurrent, Math.Max(1, usable / concurrent)); + } +} diff --git a/MagicQuant/Services/QuantizationService.cs b/MagicQuant/Services/QuantizationService.cs index 84fd4f8..df4cccd 100644 --- a/MagicQuant/Services/QuantizationService.cs +++ b/MagicQuant/Services/QuantizationService.cs @@ -58,7 +58,6 @@ public class QuantizationService private readonly TensorGroupingAuditService _tensorGroupingAuditService; private readonly TensorLearningDiagnosticWriter _tensorLearningDiagnosticWriter; - private static readonly SemaphoreSlim BaseModelLock = new(1, 1); private const byte UnknownTensorGroupId = 255; private static readonly Lazy> QuantAliasLookup = @@ -93,41 +92,13 @@ public QuantizationService(BenchmarkService benchmarker) int threadCount = Cache.SysInfo?.ThreadCount ?? Environment.ProcessorCount; - // Minimum desired threads per llama-quantize process. - // This is used to decide the natural concurrency first. - const int minimumQuantThreadsPerProcess = 4; - - // Keep a little workstation breathing room. - int reservedThreads = threadCount switch - { - >= 16 => 2, - >= 8 => 2, - >= 4 => 1, - _ => 0 - }; - - int usableThreads = Math.Max(1, threadCount - reservedThreads); - - // First decide how many quantization processes the CPU budget would naturally allow. - int naturalConcurrentQuantizations = Math.Max( - 1, - usableThreads / minimumQuantThreadsPerProcess); - - // Then cap it to avoid hammering the output drive with too many giant writers. int scratchWriterCapacity = _scratchStorage.WriterCapacity; - _maxConcurrentQuantizations = Math.Max( - 1, - Math.Min(naturalConcurrentQuantizations, scratchWriterCapacity)); - - // Divide the usable thread budget evenly across the allowed quantization processes. - // Example on 7950X3D: - // 32 total - 2 reserved = 30 usable - // natural = 30 / 8 = 3 - // capped = min(2, 3) = 2 - // threads/process = 30 / 2 = 15 - _quantThreadsPerProcess = Math.Max( - 1, - usableThreads / _maxConcurrentQuantizations); + var cpuPlan = QuantizationConcurrencyPlan.Create(threadCount, scratchWriterCapacity); + int reservedThreads = cpuPlan.ReservedThreads; + int usableThreads = cpuPlan.UsableThreads; + int naturalConcurrentQuantizations = cpuPlan.NaturalConcurrency; + _maxConcurrentQuantizations = cpuPlan.Concurrency; + _quantThreadsPerProcess = cpuPlan.ThreadsPerProcess; _cpuQuantLock = new SemaphoreSlim( _maxConcurrentQuantizations, @@ -179,6 +150,9 @@ public async Task ProcessHybridBatchAsync( StageProgressOptions? progressOptions, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (quants == null) throw new ArgumentNullException(nameof(quants)); @@ -205,6 +179,9 @@ public async Task ProcessHybridBatchAsync( StageProgressOptions? progressOptions, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (plans == null) throw new ArgumentNullException(nameof(plans)); @@ -377,7 +354,7 @@ private async Task ExecutePlanAsync( progress?.ReportFinished(state, record.ModelName, sw.Elapsed); return record; } - catch (Exception ex) + catch (Exception ex) when (ex is not OperationCanceledException) { sw.Stop(); record.State = SampleProcessState.Failed; @@ -431,7 +408,7 @@ private async Task ExecuteDuplicatePlanAsync( allowIndependentGpuTopology, ct); } - catch (Exception ex) + catch (Exception ex) when (ex is not OperationCanceledException) { sw.Stop(); record.State = SampleProcessState.Failed; @@ -505,6 +482,9 @@ public async Task ProcessHybridQuantAsync( bool allowIndependentGpuTopology = true, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); string modelName = GenerateHybridName(quant); string modelBenchDir = _paths.GetBenchmarkDir(modelName); string baseLogitsDir = GetBaseLogitsDirectory(); @@ -647,7 +627,7 @@ await PersistQuantizationRunAsync( return SampleProcessState.Completed; } - catch (Exception ex) + catch (Exception ex) when (ex is not OperationCanceledException) { try { @@ -749,6 +729,9 @@ private async Task ValidateExternalBaselineT private async Task HasLearnedTruthForBaselineAsync(BaselineQuants baseline, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (baseline.UniqueId == BaselineQuants.NativeSourceUniqueId) return await HasNativeSourceLearnedTruthAsync(ct); @@ -1237,6 +1220,9 @@ private async Task PersistQuantizationRunAsync( string? error, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) throw new InvalidOperationException("Cache.CurrentModelId is not set."); @@ -1316,14 +1302,6 @@ await ImatrixIdentityService.ResolveCurrentImatrixDefinitionIdAsync(db, persiste await db.SaveChangesAsync(ct); } - private bool IsProtectedModel(string name) - { - return name.EndsWith("BF16", StringComparison.OrdinalIgnoreCase) || - name.EndsWith("F16", StringComparison.OrdinalIgnoreCase) || - name.EndsWith("F32", StringComparison.OrdinalIgnoreCase) || - name.EndsWith("Q8_0", StringComparison.OrdinalIgnoreCase); - } - // ---------------------------------------------------------------- // Base/native model helpers // ---------------------------------------------------------------- @@ -1356,104 +1334,8 @@ await _benchmarker.RunAllBenchmarksAsync( return outputPath; } - public async Task EnsureBaseModelFileAsync(bool deleteProcess = false) - { - await BaseModelLock.WaitAsync(); - try - { - string modelName = new DirectoryInfo(Cache.ModelDirectory!).Name; - var torchType = Cache.TorchType ?? Cache.MainTorchType.BF16; - string typeStr = torchType.ToString(); - - string fileName = $"{modelName}-{typeStr}.gguf"; - string outputPath = Path.Combine(_paths.GgufDir, fileName); - string successFile = Path.Combine(_paths.GgufDir, $"{fileName}.success.json"); - string convertLogPath = outputPath + ".convert.log"; - - if (deleteProcess) - { - if (!Directory.Exists(_paths.GgufDir)) - Directory.CreateDirectory(_paths.GgufDir); - - var normalizedFileName = Path.GetFileName(fileName); - var successFileName = normalizedFileName + ".success.json"; - var successFilePath = Path.Combine(_paths.GgufDir, successFileName); - bool isImmune = File.Exists(successFilePath); - - foreach (var filePath in Directory.EnumerateFiles(_paths.GgufDir, "*.gguf", SearchOption.TopDirectoryOnly)) - { - var currentFileName = Path.GetFileName(filePath); - var currentModelName = Path.GetFileNameWithoutExtension(currentFileName); - - if (isImmune && - string.Equals(currentFileName, normalizedFileName, StringComparison.OrdinalIgnoreCase)) - { - continue; - } - - if (!string.IsNullOrWhiteSpace(currentModelName) && IsProtectedModel(currentModelName)) - { - continue; - } - - await HardDeleteHelper.DeleteFileIfExistsAsync(filePath); - } - } - - if (!File.Exists(outputPath) || !File.Exists(successFile)) - { - AnsiConsole.MarkupLine($"[bold cyan]Converting to {Markup.Escape(typeStr)}...[/]"); - - await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - - string convertScript = Cache.ConvertScript - ?? throw new Exception("ConvertScript path missing in Cache"); - - string outTypeArg = typeStr.ToLowerInvariant(); - - string arguments = - $"\"{convertScript}\" \"{Cache.ModelDirectory}\" " + - $"--outtype {outTypeArg} " + - $"--outfile \"{outputPath}\""; - - string python = _python.GetPythonExecutable(); - - var psi = new ProcessStartInfo - { - FileName = python, - Arguments = arguments, - WorkingDirectory = Cache.LlamaRoot - }; - - var result = await RunLoggedProcessAsync(psi, convertLogPath); - - if (result.ExitCode != 0) - { - await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - - throw new Exception( - $"{typeStr} conversion failed. ExitCode={result.ExitCode}. See '{convertLogPath}'."); - } - - if (!File.Exists(outputPath) || new FileInfo(outputPath).Length == 0) - { - await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); - - throw new InvalidOperationException( - $"Conversion exited successfully but produced no valid GGUF output: {outputPath}"); - } - - await File.WriteAllTextAsync(successFile, "{\"status\":\"success\"}"); - } - - return outputPath; - } - finally - { - BaseModelLock.Release(); - } - } - + public Task EnsureBaseModelFileAsync(bool deleteProcess = false) + => new NativeModelConversionService(_paths, _python).EnsureAsync(deleteProcess); public async Task BuildExportArtifactFromExactTensorMapAsync( IReadOnlyDictionary tensorTypes, @@ -1462,6 +1344,9 @@ public async Task BuildExportArtifactFromExactTensorMapAsync( bool forceRebuild = false, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (tensorTypes == null || tensorTypes.Count == 0) throw new ArgumentException("A clone tensor map must contain at least one tensor entry.", nameof(tensorTypes)); @@ -1474,7 +1359,7 @@ public async Task BuildExportArtifactFromExactTensorMapAsync( Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); - if (!forceRebuild && File.Exists(outputPath) && new FileInfo(outputPath).Length > 0) + if (!forceRebuild && File.Exists(outputPath) && new FileInfo(outputPath).Length > 0 && File.Exists(outputPath + ".success.json")) return outputPath; if (forceRebuild && File.Exists(outputPath)) @@ -1494,6 +1379,12 @@ await RunLlamaQuantizeWithExactTensorMapAsync( await File.WriteAllTextAsync(outputPath + ".success.json", "{\"status\":\"success\"}", ct); return outputPath; } + catch + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath + ".success.json"); + throw; + } finally { _cpuQuantLock.Release(); @@ -1506,6 +1397,9 @@ public async Task BuildExportArtifactAsync( bool forceRebuild = false, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (quant == null) throw new ArgumentNullException(nameof(quant)); @@ -1514,7 +1408,7 @@ public async Task BuildExportArtifactAsync( Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); - if (!forceRebuild && File.Exists(outputPath) && new FileInfo(outputPath).Length > 0) + if (!forceRebuild && File.Exists(outputPath) && new FileInfo(outputPath).Length > 0 && File.Exists(outputPath + ".success.json")) return outputPath; if (forceRebuild && File.Exists(outputPath)) @@ -1556,6 +1450,12 @@ await RunLlamaQuantizeAsync( await File.WriteAllTextAsync(outputPath + ".success.json", "{\"status\":\"success\"}", ct); return outputPath; } + catch + { + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath); + await HardDeleteHelper.DeleteFileIfExistsAsync(outputPath + ".success.json"); + throw; + } finally { _cpuQuantLock.Release(); @@ -1565,6 +1465,9 @@ await RunLlamaQuantizeAsync( public async Task BuildPureQ8ProbeLeaseAsync(CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); string basePath = await EnsureBaseModelFileAsync(); var pureQ8 = new HybridQuant @@ -1620,6 +1523,9 @@ private async Task RunLlamaQuantizeWithExactTensorM string? metadataWorkingDirectory = null, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); @@ -1662,7 +1568,7 @@ private async Task RunLlamaQuantizeWithExactTensorM var args = new List(capacity: concreteOverrides.Count + 8); foreach (var overrideItem in concreteOverrides) - args.Add($"--tensor-type \"{overrideItem.TensorName}={overrideItem.SchemeName}\""); + args.AddRange(["--tensor-type", $"{overrideItem.TensorName}={overrideItem.SchemeName}"]); if (_imatrixService.ShouldUseImatrixForQuant(HybridQuant.CreatePureBaseline(baseQuant))) { @@ -1671,11 +1577,11 @@ private async Task RunLlamaQuantizeWithExactTensorM throw new InvalidOperationException( $"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); - args.Add($"--imatrix \"{imatrixPath}\""); + args.AddRange(["--imatrix", imatrixPath]); } - args.Add($"\"{inputFile}\""); - args.Add($"\"{outputFile}\""); + args.Add(inputFile); + args.Add(outputFile); args.Add(baseQuant.QuantizeBaseArgumentName); args.Add(_quantThreadsPerProcess.ToString()); @@ -1688,11 +1594,7 @@ private async Task RunLlamaQuantizeWithExactTensorM AnsiConsole.MarkupLine( $"[cyan]Quantizing clone artifact:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); - var result = await RunLoggedProcessAsync(new ProcessStartInfo - { - FileName = bin, - Arguments = string.Join(" ", args) - }, quantizeLogPath, ct); + var result = await RunLoggedProcessAsync(new MagicQuant.Runtime.NativeCommand(bin, args).CreateStartInfo(), quantizeLogPath, ct); if (result.ExitCode != 0) { @@ -1726,6 +1628,9 @@ private async Task RunLlamaQuantizeAsync( string? metadataWorkingDirectory = null, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile)) throw new FileNotFoundException($"Input GGUF not found: {inputFile}"); @@ -1780,7 +1685,7 @@ private async Task RunLlamaQuantizeAsync( foreach (var overrideItem in concreteOverrides) { - args.Add($"--tensor-type \"{overrideItem.TensorName}={overrideItem.SchemeName}\""); + args.AddRange(["--tensor-type", $"{overrideItem.TensorName}={overrideItem.SchemeName}"]); } if (ShouldApplyImatrix(quant)) @@ -1790,16 +1695,14 @@ private async Task RunLlamaQuantizeAsync( throw new InvalidOperationException( $"Imatrix was marked active but canonical artifact is missing: {imatrixPath}"); - args.Add($"--imatrix \"{imatrixPath}\""); + args.AddRange(["--imatrix", imatrixPath]); } - args.Add($"\"{inputFile}\""); - args.Add($"\"{outputFile}\""); + args.Add(inputFile); + args.Add(outputFile); args.Add(ResolveQuantizeBaseArgument(quant, concreteOverrides)); args.Add(_quantThreadsPerProcess.ToString()); - string arguments = string.Join(" ", args); - string bin = Path.Combine( Cache.LlamaBin!, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "llama-quantize.exe" : "llama-quantize"); @@ -1807,11 +1710,7 @@ private async Task RunLlamaQuantizeAsync( string quantizeLogPath = string.IsNullOrWhiteSpace(logPath) ? outputFile + ".quantize.log" : logPath; Directory.CreateDirectory(Path.GetDirectoryName(quantizeLogPath)!); - var psi = new ProcessStartInfo - { - FileName = bin, - Arguments = arguments - }; + var psi = new MagicQuant.Runtime.NativeCommand(bin, args).CreateStartInfo(); AnsiConsole.MarkupLine( $"[cyan]Quantizing:[/] {Markup.Escape(Path.GetFileName(outputFile))} [grey](log: {Markup.Escape(quantizeLogPath)})[/]"); @@ -1882,6 +1781,9 @@ public async Task> ReadExactTensorTypesAsync string ggufPath, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); var meta = await ReadTensorMetadataFromGgufAsync(ggufPath, Path.GetDirectoryName(ggufPath)!); return meta.TensorTypes .OrderBy(x => x.Key, StringComparer.Ordinal) @@ -1902,6 +1804,9 @@ public Task InvalidateBaselineArtifactsAsync(CancellationToken ct = default) public async Task HasNativeSourceLearnedTruthAsync(CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(Cache.CurrentModelId)) return false; @@ -1925,6 +1830,9 @@ public async Task LearnNativeSourceTruthAsync( string nativeGgufPath, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(nativeGgufPath) || !File.Exists(nativeGgufPath)) throw new FileNotFoundException($"Native GGUF path not found for learning: {nativeGgufPath}"); @@ -2521,7 +2429,7 @@ private List BuildRequestedTensorOverrides( }); } - if (!hasExplicitGroupOverrides) + if (quant.Tensors == null || !hasExplicitGroupOverrides) return result; foreach (var hybrid in quant.Tensors) @@ -3258,95 +3166,13 @@ private sealed class LoggedProcessResult } private async Task RunLoggedProcessAsync( - ProcessStartInfo psi, - string? logPath, - CancellationToken ct = default) - { - psi.RedirectStandardOutput = true; - psi.RedirectStandardError = true; - psi.UseShellExecute = false; - psi.CreateNoWindow = true; - - using var process = new Process - { - StartInfo = psi, - EnableRaisingEvents = true - }; - - var stdoutBuilder = new StringBuilder(); - var stderrBuilder = new StringBuilder(); - object sync = new(); - - var stdoutClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var stderrClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - - StreamWriter? logWriter = null; - FileStream? logStream = null; - - if (!string.IsNullOrWhiteSpace(logPath)) - { - logStream = new FileStream(logPath, FileMode.Create, FileAccess.Write, FileShare.Read); - logWriter = new StreamWriter(logStream) { AutoFlush = true }; - } - - void HandleLine(string? line, bool isError) - { - if (line == null) - { - if (isError) - stderrClosed.TrySetResult(true); - else - stdoutClosed.TrySetResult(true); - - return; - } - - lock (sync) - { - if (isError) - stderrBuilder.AppendLine(line); - else - stdoutBuilder.AppendLine(line); - - logWriter?.WriteLine(line); - } - - if (Cache.VerboseProcessOutput) - AnsiConsole.WriteLine(line); - } - - process.OutputDataReceived += (_, e) => HandleLine(e.Data, isError: false); - process.ErrorDataReceived += (_, e) => HandleLine(e.Data, isError: true); - - if (!process.Start()) - throw new InvalidOperationException($"Failed to start process: {psi.FileName}"); - - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); - - using var ctr = ct.Register(() => - { - try - { - if (!process.HasExited) - process.Kill(entireProcessTree: true); - } - catch - { - } - }); - - await process.WaitForExitAsync(ct); - await Task.WhenAll(stdoutClosed.Task, stderrClosed.Task); - - logWriter?.Dispose(); - logStream?.Dispose(); - - return new LoggedProcessResult - { - ExitCode = process.ExitCode, - StdOut = stdoutBuilder.ToString(), - StdErr = stderrBuilder.ToString() - }; + ProcessStartInfo psi, string? logPath, CancellationToken ct = default) + { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); + var result = await new MagicQuant.Runtime.ProcessRunner().RunAsync(psi, logPath, + (line, _) => { if (Cache.VerboseProcessOutput) AnsiConsole.WriteLine(line); }, ct); + return new LoggedProcessResult { ExitCode = result.ExitCode, StdOut = result.StdOut, StdErr = result.StdErr }; } } diff --git a/MagicQuant/Services/ReadmeGenerationService.cs b/MagicQuant/Services/ReadmeGenerationService.cs index c28ec30..19fe600 100644 --- a/MagicQuant/Services/ReadmeGenerationService.cs +++ b/MagicQuant/Services/ReadmeGenerationService.cs @@ -201,7 +201,7 @@ private async Task GenerateCoreAsync( sb.AppendLine(""); sb.AppendLine(); //} - + sb.AppendLine("
"); sb.AppendLine("Re-Uploading External Provider Baselines"); sb.AppendLine(); @@ -272,7 +272,9 @@ private static bool TryGetReplacementHint(IReadOnlyDictionary 0; + if (!replacementHints.TryGetValue(key.Trim(), out var found) || found.Count == 0) return false; + replaced = found; + return true; } private static Dictionary> LoadCloneReplacementHints(string outputDirectory) @@ -721,4 +723,4 @@ private sealed class ReadmeCloneContext public bool SourceWasHuggingFaceRepo { get; init; } public IReadOnlyList ArchivedManifestFileNames { get; init; } = Array.Empty(); } -} \ No newline at end of file +} diff --git a/MagicQuant/Services/RepositoryCloneManifestService.cs b/MagicQuant/Services/RepositoryCloneManifestService.cs index 15092ec..7153bd8 100644 --- a/MagicQuant/Services/RepositoryCloneManifestService.cs +++ b/MagicQuant/Services/RepositoryCloneManifestService.cs @@ -26,6 +26,9 @@ public RepositoryCloneManifestService(HuggingFaceBaselineService huggingFace) string modelMagicQuantDirectory, CancellationToken ct = default) { + using var runCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct, MagicQuant.Runtime.RunCancellation.Token); + ct = runCancellation.Token; + ct.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(sourceRepo) && string.IsNullOrWhiteSpace(sourceJson)) throw new InvalidOperationException("Clone mode requires --source-repo or --source-json ."); @@ -108,7 +111,7 @@ await _huggingFace.DownloadRepositoryFileAsync( AnsiConsole.MarkupLine($"[green]Downloaded clone manifest:[/] {Markup.Escape(repoId)}/{Markup.Escape(candidate)}"); return localPath; } - catch (Exception ex) + catch (Exception ex) when (ex is not OperationCanceledException) { errors.Add($"{candidate}: {ex.Message}"); } diff --git a/MagicQuant/Services/RunProvenanceService.cs b/MagicQuant/Services/RunProvenanceService.cs new file mode 100644 index 0000000..7f7ba85 --- /dev/null +++ b/MagicQuant/Services/RunProvenanceService.cs @@ -0,0 +1,98 @@ +using System.Reflection; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Text.Json; +using MagicQuant.Configuration; +using MagicQuant.Helpers; +using MagicQuant.Runtime; +using MQ.DB; + +namespace MagicQuant.Services; + +/// +/// Records local campaign inputs and completion independently of export cleanup. +/// It is not published into model cards: config/argv can contain private paths or URLs. +/// +public sealed class RunProvenanceService +{ + private readonly string _path; + private readonly Dictionary _record; + private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true }; + + public RunProvenanceService(string command, string[] args, MagicQuantYamlLoader.LoadedConfiguration loaded) + { + string root = string.IsNullOrWhiteSpace(loaded.Settings.Paths.ModelDir) + ? loaded.Settings.Paths.MagicQuantRoot! + : Path.Combine(Path.GetFullPath(loaded.Settings.Paths.ModelDir), "MagicQuant"); + string runId = $"{DateTime.UtcNow:yyyyMMddTHHmmssfffZ}-{Guid.NewGuid():N}"; + _path = Path.Combine(root, "Runs", runId, "run.json"); + _record = new() + { + ["schemaVersion"] = 1, + ["runId"] = runId, + ["command"] = command, + ["arguments"] = args, + ["startedUtc"] = DateTimeOffset.UtcNow, + ["status"] = "running", + ["programVersion"] = typeof(RunProvenanceService).Assembly.GetCustomAttribute()?.InformationalVersion, + ["dotnetVersion"] = Environment.Version.ToString(), + ["operatingSystem"] = RuntimeInformation.OSDescription, + ["configPath"] = loaded.Path, + ["configSha256"] = Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(loaded.Path))).ToLowerInvariant(), + // Serialize now: dynamic custom-baseline registration must not rewrite the input snapshot. + ["configuration"] = JsonSerializer.SerializeToElement(loaded.Settings, JsonOptions) + }; + Write(); + } + + public string ManifestPath => _path; + + public async Task CaptureToolchainAsync() + { + _record["llamaRoot"] = Cache.LlamaRoot; + _record["llamaBin"] = Cache.LlamaBin; + _record["llamaRevision"] = await TryReadToolAsync("git", ["-C", Cache.LlamaRoot ?? "", "rev-parse", "HEAD"]); + string python = new PythonManager(Cache.MagicQuantDirectory!).GetPythonExecutable(); + _record["pythonExecutable"] = python; + _record["pythonVersion"] = await TryReadToolAsync(python, ["--version"]); + _record["pythonPackages"] = await TryReadToolAsync(python, ["-m", "pip", "freeze"]); + Write(); + } + + public void Complete(string status, string? error = null) + { + _record["status"] = status; + _record["completedUtc"] = DateTimeOffset.UtcNow; + _record["error"] = error; + _record["modelId"] = Cache.CurrentModelId; + _record["architectureFamily"] = Cache.CurrentArchitectureFamilyName; + _record["tensorGroupProfile"] = Cache.CurrentTensorGroupProfileFingerprintHash; + _record["imatrixIdentity"] = Cache.ActiveImatrixIdentityHash; + _record["outputDirectory"] = Cache.OutputDirectory; + Write(); + } + + private void Write() + { + Directory.CreateDirectory(Path.GetDirectoryName(_path)!); + string temporary = _path + ".tmp"; + try + { + File.WriteAllText(temporary, JsonSerializer.Serialize(_record, JsonOptions)); + File.Move(temporary, _path, overwrite: true); + } + finally { if (File.Exists(temporary)) File.Delete(temporary); } + } + + private static async Task TryReadToolAsync(string executable, string[] args) + { + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + try + { + var result = await new ProcessRunner().RunAsync(new NativeCommand(executable, args).CreateStartInfo(), ct: timeout.Token); + return result.Success ? result.CombinedOutput.Trim() : null; + } + catch (OperationCanceledException) when (!RunCancellation.Token.IsCancellationRequested) { return null; } + catch (System.ComponentModel.Win32Exception) { return null; } + } +} diff --git a/MagicQuant/packages.lock.json b/MagicQuant/packages.lock.json new file mode 100644 index 0000000..1784fe7 --- /dev/null +++ b/MagicQuant/packages.lock.json @@ -0,0 +1,259 @@ +{ + "version": 1, + "dependencies": { + "net10.0": { + "Blake3": { + "type": "Direct", + "requested": "[2.2.0, )", + "resolved": "2.2.0", + "contentHash": "RM6sZLZDx2wGi00aTj9s2jUcrI4s9dS2ibcT7lSujpUpBGp+TLf71F3XdBKJyYSxHnZ+FL7Dm36Pl0Y+cfcXvw==" + }, + "DuckDB.NET.Data.Full": { + "type": "Direct", + "requested": "[1.4.3, )", + "resolved": "1.4.3", + "contentHash": "tg1FWmePN+k536O1cx2VhKWa3xT7DXrcGg4kGgiSWQyur9UWwZ2i2YMpD+XVYv1ARozyy1Tt7OW2cMrNPoPj9g==", + "dependencies": { + "DuckDB.NET.Bindings.Full": "1.4.3" + } + }, + "LibGit2Sharp": { + "type": "Direct", + "requested": "[0.31.0, )", + "resolved": "0.31.0", + "contentHash": "b3+UfV7LjKMjAHWwl7VawejiOv2gJIC6dTCA/S0puLTHACAA/Oeb5JJmWUQMeyH/T/WR/LaIK8bk2RbdFnrZvg==", + "dependencies": { + "LibGit2Sharp.NativeBinaries": "[2.0.323]" + } + }, + "Spectre.Console": { + "type": "Direct", + "requested": "[0.54.0, )", + "resolved": "0.54.0", + "contentHash": "StDXCFayfy0yB1xzUHT2tgEpV1/HFTiS4JgsAQS49EYTfMixSwwucaQs/bIOCwXjWwIQTMuxjUIxcB5XsJkFJA==" + }, + "System.Management": { + "type": "Direct", + "requested": "[10.0.11, )", + "resolved": "10.0.11", + "contentHash": "xyNn8KGbWI88LoUwg3rB8qcpFFST6dr8Ro/qS8GBu2GOwR0v7J82kVFHTiiPtvEKS79VbMTxs/sIKQ+Cq1Zs1g==", + "dependencies": { + "System.CodeDom": "10.0.11" + } + }, + "YamlDotNet": { + "type": "Direct", + "requested": "[17.0.1, )", + "resolved": "17.0.1", + "contentHash": "qVir5fehR/W5nTJyoJUibypETXaW4iRAF9cQa0FQIC9TJ3VC0qDOwm4o/RxANewj8KzPF8WMF2abBfUgi6LC4w==" + }, + "DuckDB.NET.Bindings.Full": { + "type": "Transitive", + "resolved": "1.4.3", + "contentHash": "hZwm0zTKJ5HdUGKcase2JX52Lquyh7dCUFweECvR877QEA2gF8gSl3qrtb71BvRlgZ7pfjh0bRBCiAKOJMLE+A==" + }, + "LibGit2Sharp.NativeBinaries": { + "type": "Transitive", + "resolved": "2.0.323", + "contentHash": "Kg+fJGWhGj5qRXG0Ilj4ddhuodGXZg57yhfX6OVUDR0M2DKg/UR42/d74+qv5l1qotc1qJilo/ho7xQnULP6yA==" + }, + "Microsoft.Data.Sqlite": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "7je7UELzm131GiLYc4PpZvfKXIgIyzPM+v+tjcd/nbnuWRfgcONYKzDTqJlURxwVCFsVnlpmq6y6yn4qvR8QXQ==", + "dependencies": { + "Microsoft.Data.Sqlite.Core": "10.0.11", + "SQLitePCLRaw.bundle_e_sqlite3": "2.1.12", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.Data.Sqlite.Core": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "hubA20AGenQ4Sx0ElWaPpB8DISjXpdx463+1zOGRslsT0e/t/06ITv+pHsop8CcJ0d8PZLfgnT7juCDVD79Dkw==", + "dependencies": { + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.EntityFrameworkCore": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "VOSGU8en6HZJs8t7UMFN+9vGcRgVOOn6fA44Ngcg2NyvJ3P1KE94iAb0XzaVaGhXGtt+qaM/VtEn0/hzluQJeg==", + "dependencies": { + "Microsoft.EntityFrameworkCore.Abstractions": "10.0.11", + "Microsoft.EntityFrameworkCore.Analyzers": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11" + } + }, + "Microsoft.EntityFrameworkCore.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "6auJR+9+9VunznKfH7WGrHMrnrmA0F7JZ22EXzwXvVhjfnbu9Xq7NSIWaOf3KJsOanM2qf5ajJ2JR5TlcPZTLA==" + }, + "Microsoft.EntityFrameworkCore.Analyzers": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "Bv7X4wSSnzCQED9WYXKJ8fwgyvKwf0xZM1GO8xkf6CF9zl+UBnvjxmcPnokJRy0JKjc1SlHSzzhx1HcL4jitTQ==" + }, + "Microsoft.EntityFrameworkCore.Relational": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "grznnTJgEYxaWpdKAsTzg6j+89jHgCXWYp+QGtlX5O92+w/VuhWM6JLPYb+uw8M9VhGUvOTsO76dYOy9vNPd5Q==", + "dependencies": { + "Microsoft.EntityFrameworkCore": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11" + } + }, + "Microsoft.EntityFrameworkCore.Sqlite": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "jc7iVrhQyInR3loraMESfEFaFOtQOB1mRKHjX6QYC9o7YDbfMNbAPnIwlpffnFwhXd6/27FKaaV+sWSoLd4F1g==", + "dependencies": { + "Microsoft.EntityFrameworkCore.Sqlite.Core": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyModel": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11", + "SQLitePCLRaw.bundle_e_sqlite3": "2.1.12", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.EntityFrameworkCore.Sqlite.Core": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "ywTQKt32xnVhCzjEQAqFufpEyXkOUfvW/EC/s4xnS8Xaor2xXE+TMUyzhgACqXtZEU5IR95y94RDzHto55Fx7w==", + "dependencies": { + "Microsoft.Data.Sqlite.Core": "10.0.11", + "Microsoft.EntityFrameworkCore.Relational": "10.0.11", + "Microsoft.Extensions.Caching.Memory": "10.0.11", + "Microsoft.Extensions.Configuration.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyModel": "10.0.11", + "Microsoft.Extensions.Logging": "10.0.11", + "SQLitePCLRaw.core": "2.1.12" + } + }, + "Microsoft.Extensions.Caching.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "vUl798SmruTqqlt/xH2gDk3tJlhk6k3HdOXAHirlRfbNKDym4g/kRpUL9S4sl6F6FsOTOMW+ZsDapqlZMOOiEw==", + "dependencies": { + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Caching.Memory": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "el1g0mBEbDBGY2bT9mcSfrTWO8QlPdq2nOCnvQugioOFwHV+bVBMeiakoI0dNOdj8d6Hi9K6HY2xzRUWJiDR3w==", + "dependencies": { + "Microsoft.Extensions.Caching.Abstractions": "10.0.11", + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11", + "Microsoft.Extensions.Logging.Abstractions": "10.0.11", + "Microsoft.Extensions.Options": "10.0.11", + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Configuration.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "fVi053xdpda9Em7vSkmgVxO/PtgC2m78ekReKWsgcyskqY0U82Bz/MONwxpGzI0hElYKJfw+fupqMVeKW3fSaA==", + "dependencies": { + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.DependencyInjection": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "PSmotV19c7E3lKed++uYo1kSiXFI+uTl37CBSrhq+CfLC3FCHjG7R91+xPnNehQfHS1b0Tzo/CCLPWH3qaEheg==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11" + } + }, + "Microsoft.Extensions.DependencyInjection.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "/a1aJz4m7ylhEDf25ugQChLQoN5XwoGjWw/BoR/ZWWKsO1v4DdJElS1uyngahz4B/eOzjFk1KNTkarRLE5wsIg==" + }, + "Microsoft.Extensions.DependencyModel": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "PJPtFYsZ+r+uz9qqXWUTEyKeJ1EiBGIJtqavkg9ZXijjGSFAk4Fgi5sqIxj+uAyLZwEKgexDUQXhWhvU6l3+og==" + }, + "Microsoft.Extensions.Logging": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "nUOJwgFkSiLHiVGFpU22pIJtuWYewuSYQ3JVuP/gdK8ASMT807Px+TYQiRWs6uSsOmoyFTaVCwKXTasczV6BpA==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection": "10.0.11", + "Microsoft.Extensions.Logging.Abstractions": "10.0.11", + "Microsoft.Extensions.Options": "10.0.11" + } + }, + "Microsoft.Extensions.Logging.Abstractions": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "Ljd0Uxoq5XpScD2Bg0nM/r3mwx7Ao5Uq24eo2ARxbGvqJ7Zht6rt2cJtwVRH4Cv+1ZVMdXz6TB43KbpmsxRrvQ==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11" + } + }, + "Microsoft.Extensions.Options": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "eY1GAKcTfD2maP27J84X9IovT3yjHJ2dVDzPmDg6/XqYvt3jMzJhtfQCLjG9pVsZGAd+8DQ2QrjaDcs2+VQLGw==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.11", + "Microsoft.Extensions.Primitives": "10.0.11" + } + }, + "Microsoft.Extensions.Primitives": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "SXcz+kF+4Oo9b1+55zntpJFYfwb1jw66ioxptyNOOTDc8g2FHnBFWjZpsWfCvZIhzr0x+4e2trVTs4OKwQfBtw==" + }, + "SQLitePCLRaw.bundle_e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "mAgscpQMLw5/nfA1Q5oJVAT29yROUo1ifZGbbTpx/lwZpSxMUGoYbKfmvdm8oXER+RzxqBmmQzeBEVKfeHv2nw==", + "dependencies": { + "SQLitePCLRaw.lib.e_sqlite3": "2.1.12", + "SQLitePCLRaw.provider.e_sqlite3": "2.1.12" + } + }, + "SQLitePCLRaw.core": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "ETpNw9DY3ckWLgRRAeCHj+GKOuPi61aeczkXhgHexUvqoZBAYg8RYESE2J7O1M7+o6QbdSEZwrw9bfqztUVWXg==" + }, + "SQLitePCLRaw.lib.e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "fWi8Dbknuhgg72fWinIdjXVaqO1hHL4YBBwVLnr7e1c9TAZwJ0QE38j9syW1hwx6HaqEVTwI+O07WPdZn8Rp0w==" + }, + "SQLitePCLRaw.provider.e_sqlite3": { + "type": "Transitive", + "resolved": "2.1.12", + "contentHash": "W3oH4XIfCzFrgUSDKHhN6N+dgzA5YHOR2VxX8GB6Qy7CyrJJgxPEG8NirgYWlPQC5P2jz2knSsexWu4tDUL33g==", + "dependencies": { + "SQLitePCLRaw.core": "2.1.12" + } + }, + "System.CodeDom": { + "type": "Transitive", + "resolved": "10.0.11", + "contentHash": "v40pNeBoZTYsiVxz+PzyZmmIr2JIhpK4VsFpQqZSZCXa51PDlNXIN2ESm8kDU0voZYVfLhxF9HvmBsxCJmkiRg==" + }, + "mq.db": { + "type": "Project", + "dependencies": { + "Microsoft.Data.Sqlite": "[10.0.11, )", + "Microsoft.EntityFrameworkCore": "[10.0.11, )", + "Microsoft.EntityFrameworkCore.Sqlite": "[10.0.11, )", + "YamlDotNet": "[17.0.1, )" + } + } + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 09ae52d..48779f8 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,11 @@ dotnet run --project MagicQuant -c Release --no-build -- --help dotnet run --project MagicQuant -c Release --no-build -- pipeline --help ``` -Building, testing, and viewing help do not require model weights or llama.cpp. Running without arguments also shows help, in both Debug and Release. +Ordinary tests skip the explicitly opt-in model smoke test. Building, ordinary testing, and viewing help do not require model weights or llama.cpp. Running without arguments also shows help, in both Debug and Release. ## Run a model -Real quantization needs a complete local Hugging Face model directory (top-level `.safetensors`, model configuration, and tokenizer assets), llama.cpp, a Python environment, and enough RAM/VRAM and disk space for native GGUFs, baselines, logits, and exports. Hardware requirements depend on the model. Linux with an apt-based distribution is the primary automatic setup path; Windows has setup code but is not covered by the Linux CI job. Automatic macOS setup is not implemented. +Real quantization needs a complete local Hugging Face model directory (top-level `.safetensors`, model configuration, and tokenizer assets), llama.cpp, a Python environment, and enough RAM/VRAM and disk space for native GGUFs, baselines, logits, and exports. Hardware requirements depend on the model. Linux with an apt-based distribution is the primary automatic setup path; Windows has setup code but is not exercised by the model smoke test. Automatic macOS setup is not implemented. 1. Copy the distributed tuning profile and edit the paths and model identity: @@ -38,7 +38,13 @@ Real quantization needs a complete local Hugging Face model directory (top-level This can download/build llama.cpp, install Python packages, and request sudo for apt packages on Linux. It uses `/MagicQuant`. To use existing llama.cpp files, configure **all three** of `paths.llama_root`, `paths.llama_bin`, and `paths.convert_script`, and pass `--config config.local.yaml`. See [setup](docs/setup.md) for Python requirements and custom runtime roots. -3. Start the campaign: +3. Validate before starting the campaign: + + ```sh + dotnet run --project MagicQuant -c Release --no-build -- pipeline --config config.local.yaml --check-config --strict-config + ``` + + Then start it: ```sh dotnet run --project MagicQuant -c Release --no-build -- pipeline --config config.local.yaml @@ -68,11 +74,13 @@ Append `--help` to any command. Arguments after `--` belong to MagicQuant, not ` - [Commands and workflows](docs/commands.md) - [Architecture and code map](docs/architecture.md) - [Storage, caching, and reruns](docs/storage.md) -- [Contributing and tests](CONTRIBUTING.md) +- [Contributing](CONTRIBUTING.md) +- [Tests, model smoke workflow, and merge checks](docs/testing.md) +- [Worked contributor examples](docs/extending.md) - [Compatibility notes for existing users](docs/migration.md) The small [example configurations](examples/) demonstrate the required fields. They use C# defaults for omitted settings; they are **not** merged with `config.default.yaml`. Copy the full default file when you want its distributed tuning values. ## Project status -The research pipeline is active software with model- and hardware-dependent integration requirements. Unit/regression tests run without quantizing a model; a passing test suite alone does not establish numerical parity for a full hardware campaign. The repository does not yet contain a software license; the maintainer must choose one before an open-source release. A generated model card's license field does not license this program. +The research pipeline is active software with model- and hardware-dependent integration requirements. Ordinary unit/regression tests run without quantizing a model; a passing test suite alone does not establish numerical parity for a full hardware campaign. The repository does not yet contain a software license; the maintainer must choose one before an open-source release. A generated model card's license field does not license this program. diff --git a/docs/architecture.md b/docs/architecture.md index 664b064..a3f0090 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -2,7 +2,7 @@ ## Execution flow -`Program.cs` dispatches through `CommandCatalog`. Help returns before runtime initialization. A normal command loads YAML into `Config.Current`, applies run state to `MQ.DB.Cache`, cleans stale scratch artifacts, validates invariants/dependencies, and invokes an `ICommand`. +`Program.cs` dispatches through `CommandCatalog`. Help returns before runtime initialization. A normal command reads and validates YAML/CLI/input paths before loading `Config.Current` and run state into `MQ.DB.Cache`. It records provenance, cleans stale scratch, checks dependencies, and invokes an `ICommand`. `--check-config` exits before those runtime changes. `Commands/QuantizationPipeline.cs` coordinates full discovery. `Evolution.cs` preserves the historical C# entry point and the CLI registry keeps `evolution` as an alias. The orchestrator should describe stage order; reusable behavior belongs in services. @@ -26,14 +26,16 @@ The [research wiki](https://github.com/magiccodingman/MagicQuant-Wiki) is the so | Baseline identity and roles | `MQ.DB/Models/BaselineQuants.cs`, `BaselineDefinitionResolver`, `HuggingFaceBaselineService` | | Tensor grouping and profile review | `MQ.DB/tensor_groups.yaml`, `TensorGroupReviewService`, `TensorGroupProfileService`, `TensorGroupRebucketService` | | Process/tool setup | `InitializeLlamaCpp`, `Helpers/LlamaBuilder`, `Helpers/PythonManager`, `HardwareHelper` | -| Native conversion and quantization | `QuantizationService`, `ExternalBaselineTensorParity`, `CloneManifestTensorMapBuildService` | -| Benchmark execution and GPU planning | `BenchmarkService`, `BenchmarkGpuPlanning`, `LlamaGpuArgumentBuilder` | +| Native conversion and quantization | `NativeModelConversionService`, `QuantizationService`, `ExternalBaselineTensorParity`, `CloneManifestTensorMapBuildService` | +| Benchmark execution and GPU planning | `BenchmarkCommands`, `BenchmarkLogParser`, `BenchmarkService`, `BenchmarkGpuPlanning`, `LlamaGpuArgumentBuilder` | | Isolation sampling and policy | `IsolationPlanningService`, `IsolationOptimizationService`, `Helpers/RuntimeSearchSpace` | | SQLite measured truth | `MQ.DB/Data/MagicQuantContext.cs`, `MQ.DB/Models/DbModels`, `HybridBenchmarkRepository` | | DuckDB candidate data | `QuantDatabaseService`, `RemainingCombinationStore`, `CombinationDuckDbSchema` | | KLD prediction and final selection | `RankSafeKldPredictionService`, `PredictionGuidedHybridSelectionService`, `SmartBaselineTuningFallbackService` | | Contextual anomaly/synergy evidence | `AnomalyWorkflowService`, `AnomalyRuleRepository`, `AnomalyAdjustedPredictionService` | | Release artifacts | `HybridArtifactExportService`, `FinalArtifactNamingService`, `FinalReleaseMetadataService`, `ReadmeGenerationService` | +| Native process lifetime | `Runtime/ProcessRunner`, `NativeCommand`, `RunCancellation` | +| Run provenance | `RunProvenanceService` | | Paths and lifecycle | `ModelArtifactPathService`, `ModelRuntimePathService`, `OutputPathService`, `CombinationDatabasePathService`, `ScratchStorageService` | ## Invariants worth protecting @@ -52,3 +54,5 @@ The [research wiki](https://github.com/magiccodingman/MagicQuant-Wiki) is the so Tests currently disable parallel execution because these globals are shared. Tests that change them must save and restore the prior state in `finally`, use unique temporary directories, and clean up only those directories. Prefer testing a pure policy/path helper when possible. Executable-level CLI tests protect the entry point separately from command implementation tests. Large benchmark, quantization, and selection services remain candidates for incremental extraction. Extract a cohesive responsibility behind regression tests instead of splitting files by arbitrary line count or changing numerical policy during a readability patch. + +`NativeModelConversionService` owns native artifact completion, while `QuantizationConcurrencyPlan` computes CPU/storage limits without IO. `IProcessRunner` permits failure/cancellation tests at that boundary. `RunCancellation` is an async-scoped bridge for legacy service APIs; new APIs should accept explicit cancellation tokens as well. Numerical policy and persisted evidence remain in their existing services. diff --git a/docs/commands.md b/docs/commands.md index fbe6d50..4e37d13 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -59,4 +59,8 @@ The normal pipeline can reuse scoped measurements, but final export normally cle No arguments, `help`, `--help`, or `-h` display top-level help. ` --help` and ` -h` display command help without config loading, cleanup, database access, or dependency installation. -The host returns `0` on normal completion/help, `2` for an unknown command, and `1` for an exception caught at the command boundary. Services may handle individual candidate failures and continue a campaign, so also inspect the reported sample failures and final artifacts. +The host returns `0` on normal completion/help, `2` for an unknown command, `130` for cooperative cancellation, and `1` for an exception caught at the command boundary. Services may handle individual candidate failures and continue a campaign, so also inspect the reported sample failures and final artifacts. + +Use `--check-config` on a normal command to validate local inputs without running it. +Use `--strict-config` to reject unknown/inactive YAML settings rather than warning. +See [testing](testing.md) for the opt-in real-model workflow. diff --git a/docs/configuration.md b/docs/configuration.md index 837f396..4e261c2 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -6,9 +6,9 @@ The loader deserializes the selected file into `MagicQuantYamlConfig`, whose property initializers supply omitted fields, applies supported CLI overrides, normalizes values, and updates `Config.Current` and `MQ.DB.Cache`. It does **not** merge a custom file with `config.default.yaml`. The distributed YAML intentionally differs from C# defaults for some research tuning settings. Copy that whole file to reproduce its profile. -CLI string options generally override nonblank YAML values. Many boolean switches only enable a feature; use YAML to disable it unless a specific negative CLI switch exists. Use `--name value` or `--name=value`; quote paths with spaces using normal shell quoting. +Unknown CLI options, duplicate options, missing values, and values supplied to presence-only flags are rejected. CLI string options generally override nonblank YAML values. Many boolean switches only enable a feature; use YAML to disable it unless a specific negative CLI switch exists. Use `--name value` or `--name=value`; quote paths with spaces using normal shell quoting. -Unknown YAML keys are currently ignored for compatibility. This means misspellings can be silently ignored. Compare with the commented default file and `MagicQuant/Configuration/MagicQuantYamlConfig.cs`. CI strictly parses the distributed examples so their keys cannot silently drift. +Unknown or inactive YAML keys produce a warning with their setting path and line number; `--strict-config` rejects them. Compare with the commented default file and `MagicQuant/Configuration/MagicQuantYamlConfig.cs`. CI strictly parses the distributed examples so their keys cannot silently drift. ## Main sections @@ -52,4 +52,12 @@ These historical output differences are preserved for existing campaigns. `Outpu `readme.frontmatter` accepts arbitrary scalar/list metadata. Set `license`, `base_model`, and other provenance fields for the actual exported model; no model license is inferred for you. -The old `evolution`, `survival`, sensitivity-group, brain-layer, and collapse-penalty config surfaces had no active consumers and have been removed from the typed configuration. Old YAML containing them is still tolerated, but they do not tune the current algorithm. See `candidate_selection` and the research wiki for current selection policy. +The old `evolution`, `survival`, sensitivity-group, brain-layer, and collapse-penalty config surfaces had no active consumers and have been removed from the typed configuration. Old YAML containing them is tolerated with warnings unless `--strict-config` is selected; they do not tune the current algorithm. See `candidate_selection` and the research wiki for current selection policy. + +## Preflight and cancellation + +Normal runs validate local inputs before applying global state, creating runtime directories, installing dependencies, or cleaning artifacts. `--check-config` performs only this check. Non-finite numeric values and null required sections are rejected. Clone preflight requires one source manifest/repository, and model discovery requires explicit architecture-family identity. + +Exports cannot contain the source model/runtime root or overlap protected model work directories such as GGUF, Benchmarks, Logs, Runs, and ExternalBaselines. Physical symlink targets are considered. These guards do not make arbitrary existing export contents safe: still choose a dedicated directory. + +Ctrl+C requests cooperative cancellation, stops active native work, and returns status 130. A second Ctrl+C requests immediate OS termination. Process/lease cleanup is cooperative; forced termination or power loss can still require stale-artifact cleanup on the next run. diff --git a/docs/extending.md b/docs/extending.md new file mode 100644 index 0000000..c4edf4d --- /dev/null +++ b/docs/extending.md @@ -0,0 +1,27 @@ +# Worked contributor examples + +## Add a configuration option + +Suppose a future change adds a limit to a selection stage. First find the owning policy (`RuntimeCandidateSelectionConfig` and its service), and decide whether the option actually affects behavior. Do not add another dormant knob. + +1. Add a clearly named typed property with its default and a comment describing the decision it controls. +2. Add the underscored YAML key to `config.default.yaml`; decide explicitly whether the distributed tuning profile uses the same default. +3. If a CLI override is useful, add it to `MagicQuantYamlLoader.ApplyCliOverrides` and the value/flag contract in `CliOptionValidator`. Validate numeric input with invariant culture. Add preflight validation for constraints that should fail before work begins. +4. Add tests for omitted/default values, CLI precedence, and the observable stage behavior. Keep any changed `Config.Current`/`Cache` state scoped and restored. +5. Update command help and configuration documentation. Run the strict distributed-config tests and the complete suite. + +`YamlConfigurationDiagnostics` derives known keys from the typed schema, so it does not need a duplicate property-name list. Free-form `readme.frontmatter` and dictionary keys remain user-defined. + +## Change a path or process invocation + +For a new benchmark option, update `BenchmarkCommands`, then assert the literal argv sequence in `BenchmarkContractTests`. Use `NativeCommand.CreateStartInfo`, not interpolated shell strings. Include a path with spaces and metacharacters in the test. Keep retries in the calling service; `ProcessRunner` returns a nonzero exit code and only throws for launch/IO/cancellation failures. + +For conversion behavior, `NativeModelConversionService` accepts `IProcessRunner`. `NativeConversionTests` injects a small fake that writes a partial output and returns failure or cancellation. That verifies incomplete artifacts never acquire a reusable success marker without invoking Python or a model. Keep production code using the real runner by default. + +For output destinations, update `OutputPathService` and `PathSafety`, preserving existing command semantics or documenting a deliberate migration. Test ordinary paths, parent/child collisions, similarly prefixed sibling directories, and linked directories. Run the optional smoke workflow if native argument or artifact lifecycle behavior changed. + +## Work on numerical policy + +Read the research wiki and the owning service before editing. Add a regression around the actual measured/predicted tradeoff and its context identity. Do not replace exact custom tensor assignments with a built-in family surrogate merely to make a test pass. Model hash, architecture/profile identity, and imatrix scope are part of the input. + +Global configuration and registries still exist; this cleanup does not support multiple concurrent campaigns in one process. New helpers should accept explicit inputs, return results, and be testable without mutating those registries. Extract a responsibility with behavior tests rather than mechanically splitting a large class into partial files. diff --git a/docs/migration.md b/docs/migration.md index 589461a..23d9713 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -12,7 +12,7 @@ This cleanup retains numerical selection policy, SQLite schemas/migrations, mani ## Removed inactive surfaces -The old evolution/survival knobs and unused sensitivity, brain-layer, collapse-penalty, and MoE-indicator config lists had no active runtime consumers. Their typed properties and inactive CLI overrides were removed. Legacy YAML keys remain ignored by the permissive loader; they never tuned the current chooser. Use `prediction`, `candidate_selection`, `anomaly_detection`, and `synergy_detection` for current policy. +The old evolution/survival knobs and unused sensitivity, brain-layer, collapse-penalty, and MoE-indicator config lists had no active runtime consumers. Their typed properties and inactive CLI overrides were removed. Legacy YAML keys warn (or fail with `--strict-config`); they never tuned the current chooser. Use `prediction`, `candidate_selection`, `anomaly_detection`, and `synergy_detection` for current policy. Startup no longer enumerates the entire combination universe merely to compare it with a count. That diagnostic helper remains available for explicit development checks. Actual pipeline generation and policy checks remain in place. @@ -33,3 +33,11 @@ git show :MagicQuant/config.dev.yaml > config.local.yaml Then continue with `pipeline --config config.local.yaml` and your explicit model/family arguments. The change to DEBUG startup does not change values inside that saved YAML. + +## Deeper readiness changes + +CLI typos/duplicate options and missing values now fail before work; unknown YAML keys warn, and `--strict-config` makes them errors. `--check-config` performs read-only preflight. Unsafe managed/output overlaps and missing model inputs fail before dependency setup or cleanup. Numeric CLI parsing uses an invariant decimal point. + +Native processes now share cancellation/log cleanup and use literal argv for benchmark and quantization launches. CPU llama-bench uses `-ngl 0` because the tested native version rejects the historical `-backend cpu` argument. Native conversion and low-level export require completion markers for reuse; partial/canceled builds are removed. Existing higher-level benchmark-size reuse checks remain in place. + +.NET package references were updated within the 10.0 patch line to remove the previously reported transitive vulnerabilities. Package lock files are committed. Database schemas and research selection formulas were not changed. diff --git a/docs/setup.md b/docs/setup.md index 0f0c2c8..4f376d2 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -2,7 +2,7 @@ ## Development requirements -All three projects target `net10.0`. Use the .NET 10 SDK. NuGet restore downloads the managed packages and native SQLite/DuckDB assets. The solution includes `MagicQuant`, `MQ.DB`, and `MagicQuant.Tests`. +All solution projects target `net10.0`. Use the .NET 10 SDK. NuGet restore downloads the managed packages and native SQLite/DuckDB assets. The solution includes `MagicQuant`, `MQ.DB`, `MagicQuant.Tests`, and the offline `MagicQuant.ProcessFixture` test helper. ```sh dotnet restore MagicQuant-Pipeline.sln diff --git a/docs/storage.md b/docs/storage.md index 60fd09c..bfe81f1 100644 --- a/docs/storage.md +++ b/docs/storage.md @@ -15,6 +15,7 @@ The runtime root and model work directory are different things. With default set GGUF/ # durable native/base artifacts Benchmarks/ # measurements, corpora, reference logits Logs/Quantization/ # quantization process logs + Runs//run.json # local campaign provenance and terminal status ExternalBaselines/ # durable downloaded external GGUFs MagicQuant_Combinations___.duckdb Final_Outputs/ # pipeline default export directory @@ -45,3 +46,9 @@ External baseline downloads are durable and managed separately from transient qu ## Reproducibility Retain the exact command, selected YAML, program commit, llama.cpp revision, model source revision/hash, external repository revision pins, imatrix identity/source, hardware plan, and emitted manifests/benchmark reports for a release. Custom repository `revision` can pin a branch, tag, or commit; a commit avoids moving references. Reusing output does not replace recording these inputs. + +## Local run provenance + +After preflight, each real command creates a unique `Runs//run.json` under the model work directory (or runtime root for setup). It snapshots argv, normalized input configuration, config SHA-256, program/.NET versions, and timestamps. Available llama.cpp revision, Python version/package inventory, final model/profile/imatrix identities, output path, and completion/failure/cancellation status are recorded as execution progresses. An unfinalized `running` record may indicate abrupt termination. + +Writes replace the manifest atomically. Records are separate from export cleanup and are **not published automatically**: argv/config may include private paths or URLs. Review before sharing. A missing tool-version field means it could not be obtained, not that the tool had a known default version. diff --git a/docs/testing.md b/docs/testing.md new file mode 100644 index 0000000..15366f2 --- /dev/null +++ b/docs/testing.md @@ -0,0 +1,53 @@ +# Testing and PR checks + +## Ordinary checks + +```sh +dotnet restore MagicQuant-Pipeline.sln --locked-mode -warnaserror +dotnet build MagicQuant-Pipeline.sln -c Release --no-restore -warnaserror +dotnet test MagicQuant-Pipeline.sln -c Release --no-build +``` + +Repeat with `-c Debug` when changing startup or compilation-dependent behavior. CI runs both configurations on Linux and Windows. It restores the committed NuGet lock files, treats warnings as errors, runs all ordinary tests, and uploads TRX reports. `MagicQuant.ProcessFixture` is a small offline executable used to test native process exit, full stdout/stderr pipes, literal arguments, and cancellation; it is not a user command. + +The suite covers CLI startup/preflight, YAML contracts, managed/output path containment, symlinks on Linux, SQLite/DuckDB identity, native argument construction, log parsing, conversion success markers, concurrency policy, and existing research-policy regressions. It does not establish numerical equivalence for every model or hardware topology. Tests remain serial because the legacy runtime registries are shared. + +To update dependencies intentionally, edit package versions, run an unlocked `dotnet restore`, review `packages.lock.json` changes, and rerun the suite. Audit with: + +```sh +dotnet list MagicQuant-Pipeline.sln package --vulnerable --include-transitive +``` + +## Read-only campaign validation + +```sh +dotnet run --project MagicQuant -c Release -- pipeline \ + --config config.local.yaml --check-config --strict-config +``` + +This reads YAML and local input/path metadata but does not create a database, initialize tools, clean artifacts, or quantize. `--strict-config` turns unknown/inactive YAML keys into errors; without it, those keys produce warnings. Normal runs perform the same preflight before runtime mutation. This check verifies local paths and model input structure, not available RAM, remote repository existence, tokenizer compatibility with every converter, or numerical quality. + +## Opt-in small-model smoke test + +The test is explicitly skipped in ordinary CI. On a prepared Linux machine, set these variables to existing resources and a dedicated writable output parent: + +```sh +MQ_RUN_MODEL_SMOKE=1 \ +MQ_SMOKE_MODEL=/data/models/small-model \ +MQ_SMOKE_LLAMA_ROOT=/opt/llama.cpp \ +MQ_SMOKE_RUNTIME_ROOT=/data/MagicQuant \ +MQ_SMOKE_OUTPUT=/data/test-results/magicquant \ +dotnet test MagicQuant.Tests -c Release --filter Category=ModelSmoke +``` + +The runtime root must contain `MagicQuant-Env` with the converter/gguf dependencies already installed. The test does not install dependencies or download a model. It copies source metadata and links weights into a unique test model directory containing spaces, then exercises native conversion/reuse, Q8 scratch leases, export/reuse, GGUF metadata parity, the native CPU benchmark, and manifest-path writing. It has a 20-minute cancellation deadline. It removes generated GGUFs and input weight links and retains logs plus `smoke-result.json` beneath the output parent. The sample tensor-map JSON is a smoke artifact, not a full clone/release manifest. + +This is an IO/toolchain smoke check, not a full discovery campaign or a PPL/KLD parity study. Quantization/selection policy changes still need before/after measurements on representative models. + +A manual GitHub Actions workflow is provided for a trusted self-hosted runner labeled `magicquant-smoke`. Review the selected ref before dispatching it. It never runs automatically for an incoming PR, and no runner has been provisioned by this change. Do not route untrusted PR code to a machine containing private model or campaign data. + +## Requiring checks before merge + +The workflow reports failures; branch protection or a ruleset must require its checks to block merges. Configure `main` to require all four Linux/Windows Debug/Release test jobs after the workflow has run. If merge queues are enabled later, add a `merge_group` workflow trigger as well. + +The repository's current private-repository plan returned HTTP 403 when branch protection was queried, explaining that an eligible plan or public visibility is required. The code change cannot override that GitHub restriction. Once supported, enable required checks and verify that a deliberately failing test PR cannot merge. Choosing visibility, billing, and the project software license remains a maintainer decision. From b017367e5e0e7c194c97972ebac85957b33ef8ab Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 19:34:53 -0400 Subject: [PATCH 252/258] Release pooled SQLite test handles before Windows cleanup --- .../QuantizationRunAndBuildHybridsRegressionTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs b/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs index 8ab7d25..7055a96 100644 --- a/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs +++ b/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs @@ -2,6 +2,7 @@ using MagicQuant.Configuration; using MagicQuant.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.Data.Sqlite; using MQ.DB; using MQ.DB.Data; using MQ.DB.Models.DbModels; @@ -85,6 +86,9 @@ public async Task QuantizationRun_PersistsAndLoads_ImatrixDefinitionForeignKey() finally { Cache.MagicQuantDirectory = priorMagicQuantDirectory; + // Disposing the context returns connections to SQLite's pool. Release + // those handles before deleting this test's database on Windows. + SqliteConnection.ClearAllPools(); if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, recursive: true); } From c31ea26e44537e961cc329b65be958bf6ca782a9 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 19:56:08 -0400 Subject: [PATCH 253/258] Organize application, tests and operational documentation for unified repository --- .../.editorconfig => .editorconfig | 0 .../pull_request_template.md | 0 .../.github => .github}/workflows/dotnet.yml | 6 ++-- .../workflows/model-smoke.yml | 4 +-- .gitignore | 36 +++++++++++++++++++ .../CONTRIBUTING.md => CONTRIBUTING.md | 10 +++--- ...ctory.Build.props => Directory.Build.props | 0 .../MagicQuant-Pipeline.sln => MagicQuant.sln | 10 +++--- .../docs => docs}/architecture.md | 0 {pipeline-import/docs => docs}/commands.md | 8 ++--- .../docs => docs}/configuration.md | 0 {pipeline-import/docs => docs}/extending.md | 0 {pipeline-import/docs => docs}/migration.md | 0 .../pipeline-migration-readme.md | 18 +++++----- {pipeline-import/docs => docs}/setup.md | 6 ++-- {pipeline-import/docs => docs}/storage.md | 0 {pipeline-import/docs => docs}/testing.md | 12 +++---- .../examples => examples}/clone.yaml | 0 .../examples => examples}/pipeline.yaml | 0 pipeline-import/.gitignore | 35 ------------------ {pipeline-import => src}/MQ.DB/Cache.cs | 0 .../MQ.DB/Data/MagicQuantContext.cs | 0 .../MQ.DB/Interfaces/ISQLiteEntity.cs | 0 {pipeline-import => src}/MQ.DB/MQ.DB.csproj | 0 .../20260501195554_InitialCreate.Designer.cs | 0 .../20260501195554_InitialCreate.cs | 0 ..._PredictionEngineAnomalyDetect.Designer.cs | 0 ...503222121_PredictionEngineAnomalyDetect.cs | 0 ...PredictionEngineAnomalyDetect2.Designer.cs | 0 ...03224957_PredictionEngineAnomalyDetect2.cs | 0 .../MagicQuantContextModelSnapshot.cs | 0 .../MQ.DB/Models/BaselineQuants.cs | 0 .../MQ.DB/Models/BenchmarkResult.cs | 0 .../MQ.DB/Models/DbModels/AiBenchmark.cs | 0 .../DbModels/AiBenchmarkLearnedSource.cs | 0 .../MQ.DB/Models/DbModels/AiModelHash.cs | 0 .../Models/DbModels/AnomalyProbeSession.cs | 0 .../Models/DbModels/ArchitectureFamily.cs | 0 .../DbModels/ArchitectureFamilyModelHash.cs | 0 .../DbModels/BaselineQuantDefinition.cs | 0 .../MQ.DB/Models/DbModels/BenchmarkRun.cs | 0 .../DbModels/ExecutionPlanProbeCache.cs | 0 .../Models/DbModels/ImatrixDefinition.cs | 0 .../DbModels/LearnedBaselineTensorQuant.cs | 0 .../MQ.DB/Models/DbModels/QuantizationRun.cs | 0 .../MQ.DB/Models/DbModels/TensorCombo.cs | 0 .../Models/DbModels/TensorGroupProfile.cs | 0 .../MQ.DB/Models/HybridQuant.cs | 0 .../MQ.DB/Models/IsolationRules.cs | 0 .../MQ.DB/Models/LlamaBenchMetrics.cs | 0 .../MQ.DB/Models/LlamaBinaries.cs | 0 .../MQ.DB/Models/PplMetrics.cs | 0 .../MQ.DB/Models/RequiredSamplePlan.cs | 0 .../MQ.DB/Models/SystemInfo.cs | 0 .../MQ.DB/Models/TensorConfigs.cs | 0 .../MQ.DB/Models/TensorGroup.cs | 0 .../MQ.DB/Models/TensorGroupSynergy.cs | 0 .../MQ.DB/Models/TensorWeight.cs | 0 .../MQ.DB/Models/TensorWeightScheme.cs | 0 .../MQ.DB/packages.lock.json | 0 .../MQ.DB/tensor_groups.yaml | 0 .../MagicQuant/Commands/BuildHybrids.cs | 0 .../Commands/CloneRepositoryQuants.cs | 0 .../MagicQuant/Commands/CommandCatalog.cs | 0 .../MagicQuant/Commands/Evolution.cs | 0 .../MagicQuant/Commands/InitializeLlamaCpp.cs | 0 .../Commands/QuantizationPipeline.cs | 0 .../Commands/ValidatePredictions.cs | 0 {pipeline-import => src}/MagicQuant/Config.cs | 0 .../Configuration/CliOptionValidator.cs | 0 .../Configuration/CommandPreflight.cs | 0 .../ConfigurationShapeValidator.cs | 0 .../Configuration/MagicQuantYamlConfig.cs | 0 .../Configuration/MagicQuantYamlLoader.cs | 0 .../YamlConfigurationDiagnostics.cs | 0 .../MagicQuant/Helpers/CliHelpers.cs | 0 .../MagicQuant/Helpers/ComboLogic.cs | 0 .../MagicQuant/Helpers/DependencyManager.cs | 0 .../Helpers/EquivalentTruthSelectionHelper.cs | 0 .../MagicQuant/Helpers/HardDeleteHelper.cs | 0 .../MagicQuant/Helpers/HardwareHelper.cs | 0 .../Helpers/IsolationPruningConfig.cs | 0 .../MagicQuant/Helpers/JsonHelper.cs | 0 .../MagicQuant/Helpers/LinuxHelper.cs | 0 .../MagicQuant/Helpers/LlamaBuilder.cs | 0 .../Helpers/MagicQuantDiagnostics.cs | 0 .../MagicQuant/Helpers/MagicQuantModelId.cs | 0 .../Helpers/NativePrecisionNormalization.cs | 0 .../MagicQuant/Helpers/PythonManager.cs | 0 .../MagicQuant/Helpers/RuntimeSearchSpace.cs | 0 .../Helpers/SearchSpaceDebugPrinter.cs | 0 .../Helpers/TensorConfigGenerator.cs | 0 .../MagicQuant/Helpers/pip_runner.py | 0 .../MagicQuant/Interfaces/ICommand.cs | 0 .../MagicQuant/MagicQuant.csproj | 0 .../Models/AnomalyDetectionModels.cs | 0 .../MagicQuant/Models/CliArg.cs | 0 .../Models/HybridFinalizationModels.cs | 0 .../MagicQuant/Models/ImatrixModels.cs | 0 .../Models/Learning/TensorLearningModels.cs | 0 .../Models/PredictionSelectionModels.cs | 0 .../Models/RepositoryCloneModels.cs | 0 .../MagicQuant/Program.cs | 0 .../MagicQuant/Properties/AssemblyInfo.cs | 0 .../MagicQuant/Runtime/IProcessRunner.cs | 0 .../MagicQuant/Runtime/NativeCommand.cs | 0 .../MagicQuant/Runtime/ProcessRunner.cs | 0 .../MagicQuant/Runtime/RunCancellation.cs | 0 .../AnomalyAdjustedPredictionService.cs | 0 .../Services/AnomalyRuleRepository.cs | 0 .../Services/AnomalyWorkflowService.cs | 0 .../Services/ArchitectureFamilyService.cs | 0 .../Services/BaselineDefinitionResolver.cs | 0 .../MagicQuant/Services/BenchmarkCommands.cs | 0 .../Services/BenchmarkGpuPlanning.cs | 0 .../MagicQuant/Services/BenchmarkLogParser.cs | 0 .../MagicQuant/Services/BenchmarkService.cs | 0 .../CloneConfigManifestGenerationService.cs | 0 .../CloneManifestTensorMapBuildService.cs | 0 .../Services/CloneReadmeGenerationService.cs | 0 .../CombinationDatabasePathService.cs | 0 .../Services/CombinationDuckDbSchema.cs | 0 .../CombinationSurvivalPipelineService.cs | 0 .../DuckDbPredictionMaterializationService.cs | 0 .../EffectiveCandidateStateResolverService.cs | 0 .../ExternalBaselineCacheCleanupService.cs | 0 .../Services/ExternalBaselineTensorParity.cs | 0 .../Services/FinalArtifactNamingService.cs | 0 .../FinalRealBenchmarkEliminationService.cs | 0 .../Services/FinalReleaseMetadataService.cs | 0 .../FinalSurvivorSelectionCliService.cs | 0 .../MagicQuant/Services/GgufMetadataReader.cs | 0 .../Services/HuggingFaceBaselineService.cs | 0 .../Services/HybridArtifactExportService.cs | 0 .../Services/HybridBenchmarkRepository.cs | 0 .../Services/HybridMapGenerationService.cs | 0 .../Services/ImatrixIdentityService.cs | 0 .../MagicQuant/Services/ImatrixService.cs | 0 .../IsolationDiagnosticsManifestService.cs | 0 .../Services/IsolationOptimizationService.cs | 0 .../Services/IsolationPlanningService.cs | 0 .../Services/LearnedBaselinePruningService.cs | 0 .../Learning/TensorGroupingAuditService.cs | 0 .../TensorLearningDiagnosticWriter.cs | 0 .../Services/LlamaGpuArgumentBuilder.cs | 0 .../Services/MagicQuantManifestPathService.cs | 0 .../Services/ModelArtifactPathService.cs | 0 .../Services/ModelCompatibilityService.cs | 0 .../Services/ModelRuntimePathService.cs | 0 .../Services/ModelSidecarArtifactService.cs | 0 .../Services/NativeModelConversionService.cs | 0 .../MagicQuant/Services/OutputPathService.cs | 0 .../MagicQuant/Services/PathSafety.cs | 0 .../PredictionGuidedHybridSelectionService.cs | 0 .../Services/PredictionValidationService.cs | 0 .../Services/Progress/StageProgressOptions.cs | 0 .../Progress/StageProgressSnapshot.cs | 0 .../Services/Progress/StageProgressTracker.cs | 0 .../Services/QuantDatabaseService.cs | 0 .../Services/QuantFidelityComparerService.cs | 0 .../Services/QuantizationConcurrencyPlan.cs | 0 .../Services/QuantizationService.cs | 0 .../Services/RankSafeKldPredictionService.cs | 0 .../Services/ReadmeGenerationService.cs | 0 .../Services/RemainingCombinationStore.cs | 0 .../RepositoryCloneManifestService.cs | 0 .../Services/RunProvenanceService.cs | 0 .../Services/ScratchStorageService.cs | 0 .../SelectionDiagnosticsLogService.cs | 0 .../SmartBaselineTuningFallbackService.cs | 0 .../Services/TargetedRelearnService.cs | 0 .../Services/TensorGroupProfileService.cs | 0 .../Services/TensorGroupRebucketService.cs | 0 .../Services/TensorGroupReviewService.cs | 0 .../MagicQuant/config.default.yaml | 0 .../MagicQuant/packages.lock.json | 0 .../MagicQuant.ProcessFixture.csproj | 0 .../MagicQuant.ProcessFixture/Program.cs | 0 .../packages.lock.json | 0 .../AnomalyContextScopeTests.cs | 0 .../MagicQuant.Tests/AssemblyInfo.cs | 0 .../AuthorityUsageRegressionTests.cs | 8 ++--- .../BaselineCandidatePolicyTests.cs | 0 .../BenchmarkContractTests.cs | 0 .../MagicQuant.Tests/BenchmarkCorpusTests.cs | 0 .../BenchmarkGpuPlanningTests.cs | 0 .../CliArgumentParsingTests.cs | 0 .../CliOptionValidationTests.cs | 0 .../MagicQuant.Tests/CliStartupTests.cs | 0 .../CombinationDatabasePathTests.cs | 0 .../ConfigurationContractTests.cs | 4 +-- .../ConfigurationReadTests.cs | 0 ...xternalBaselineCacheCleanupServiceTests.cs | 0 .../ExternalBaselineTensorParityTests.cs | 0 .../HardwareInitializationTests.cs | 0 .../HuggingFaceBaselineCacheTests.cs | 0 .../HuggingFaceRevisionConfigTests.cs | 0 .../ImatrixIdentityServiceTests.cs | 0 .../LearnedBaselinePruningServiceTests.cs | 0 .../MagicQuant.Tests/LlamaBinaryPathTests.cs | 0 .../LlamaGpuArgumentBuilderTests.cs | 0 .../MagicQuant.Tests/MagicQuant.Tests.csproj | 2 +- .../MagicQuant.Tests/ModelSmokeTests.cs | 0 .../MagicQuant.Tests/NativeConversionTests.cs | 0 .../MagicQuant.Tests/OutputPathTests.cs | 0 .../MagicQuant.Tests/PreflightTests.cs | 0 .../MagicQuant.Tests/ProcessRunnerTests.cs | 0 .../QuantizationConcurrencyTests.cs | 0 ...zationRunAndBuildHybridsRegressionTests.cs | 0 .../MagicQuant.Tests/RunProvenanceTests.cs | 0 ...RuntimeSearchSpaceObsoleteContractTests.cs | 0 .../ScratchStorageServiceTests.cs | 0 .../SmartBaselineTuningFallbackTests.cs | 0 .../SynergyTransferConfigTests.cs | 0 .../SynergyTransferPlanningTests.cs | 0 .../MagicQuant.Tests/YamlDiagnosticsTests.cs | 0 .../MagicQuant.Tests/packages.lock.json | 0 217 files changed, 80 insertions(+), 79 deletions(-) rename pipeline-import/.editorconfig => .editorconfig (100%) rename {pipeline-import/.github => .github}/pull_request_template.md (100%) rename {pipeline-import/.github => .github}/workflows/dotnet.yml (63%) rename {pipeline-import/.github => .github}/workflows/model-smoke.yml (85%) rename pipeline-import/CONTRIBUTING.md => CONTRIBUTING.md (93%) rename pipeline-import/Directory.Build.props => Directory.Build.props (100%) rename pipeline-import/MagicQuant-Pipeline.sln => MagicQuant.sln (81%) rename {pipeline-import/docs => docs}/architecture.md (100%) rename {pipeline-import/docs => docs}/commands.md (92%) rename {pipeline-import/docs => docs}/configuration.md (100%) rename {pipeline-import/docs => docs}/extending.md (100%) rename {pipeline-import/docs => docs}/migration.md (100%) rename pipeline-import/README.md => docs/pipeline-migration-readme.md (88%) rename {pipeline-import/docs => docs}/setup.md (96%) rename {pipeline-import/docs => docs}/storage.md (100%) rename {pipeline-import/docs => docs}/testing.md (90%) rename {pipeline-import/examples => examples}/clone.yaml (100%) rename {pipeline-import/examples => examples}/pipeline.yaml (100%) delete mode 100644 pipeline-import/.gitignore rename {pipeline-import => src}/MQ.DB/Cache.cs (100%) rename {pipeline-import => src}/MQ.DB/Data/MagicQuantContext.cs (100%) rename {pipeline-import => src}/MQ.DB/Interfaces/ISQLiteEntity.cs (100%) rename {pipeline-import => src}/MQ.DB/MQ.DB.csproj (100%) rename {pipeline-import => src}/MQ.DB/Migrations/20260501195554_InitialCreate.Designer.cs (100%) rename {pipeline-import => src}/MQ.DB/Migrations/20260501195554_InitialCreate.cs (100%) rename {pipeline-import => src}/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs (100%) rename {pipeline-import => src}/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs (100%) rename {pipeline-import => src}/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs (100%) rename {pipeline-import => src}/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs (100%) rename {pipeline-import => src}/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/BaselineQuants.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/BenchmarkResult.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/AiBenchmark.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/AiModelHash.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/AnomalyProbeSession.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/ArchitectureFamily.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/BenchmarkRun.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/ImatrixDefinition.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/QuantizationRun.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/TensorCombo.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/DbModels/TensorGroupProfile.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/HybridQuant.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/IsolationRules.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/LlamaBenchMetrics.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/LlamaBinaries.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/PplMetrics.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/RequiredSamplePlan.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/SystemInfo.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/TensorConfigs.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/TensorGroup.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/TensorGroupSynergy.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/TensorWeight.cs (100%) rename {pipeline-import => src}/MQ.DB/Models/TensorWeightScheme.cs (100%) rename {pipeline-import => src}/MQ.DB/packages.lock.json (100%) rename {pipeline-import => src}/MQ.DB/tensor_groups.yaml (100%) rename {pipeline-import => src}/MagicQuant/Commands/BuildHybrids.cs (100%) rename {pipeline-import => src}/MagicQuant/Commands/CloneRepositoryQuants.cs (100%) rename {pipeline-import => src}/MagicQuant/Commands/CommandCatalog.cs (100%) rename {pipeline-import => src}/MagicQuant/Commands/Evolution.cs (100%) rename {pipeline-import => src}/MagicQuant/Commands/InitializeLlamaCpp.cs (100%) rename {pipeline-import => src}/MagicQuant/Commands/QuantizationPipeline.cs (100%) rename {pipeline-import => src}/MagicQuant/Commands/ValidatePredictions.cs (100%) rename {pipeline-import => src}/MagicQuant/Config.cs (100%) rename {pipeline-import => src}/MagicQuant/Configuration/CliOptionValidator.cs (100%) rename {pipeline-import => src}/MagicQuant/Configuration/CommandPreflight.cs (100%) rename {pipeline-import => src}/MagicQuant/Configuration/ConfigurationShapeValidator.cs (100%) rename {pipeline-import => src}/MagicQuant/Configuration/MagicQuantYamlConfig.cs (100%) rename {pipeline-import => src}/MagicQuant/Configuration/MagicQuantYamlLoader.cs (100%) rename {pipeline-import => src}/MagicQuant/Configuration/YamlConfigurationDiagnostics.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/CliHelpers.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/ComboLogic.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/DependencyManager.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/HardDeleteHelper.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/HardwareHelper.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/IsolationPruningConfig.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/JsonHelper.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/LinuxHelper.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/LlamaBuilder.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/MagicQuantDiagnostics.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/MagicQuantModelId.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/NativePrecisionNormalization.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/PythonManager.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/RuntimeSearchSpace.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/TensorConfigGenerator.cs (100%) rename {pipeline-import => src}/MagicQuant/Helpers/pip_runner.py (100%) rename {pipeline-import => src}/MagicQuant/Interfaces/ICommand.cs (100%) rename {pipeline-import => src}/MagicQuant/MagicQuant.csproj (100%) rename {pipeline-import => src}/MagicQuant/Models/AnomalyDetectionModels.cs (100%) rename {pipeline-import => src}/MagicQuant/Models/CliArg.cs (100%) rename {pipeline-import => src}/MagicQuant/Models/HybridFinalizationModels.cs (100%) rename {pipeline-import => src}/MagicQuant/Models/ImatrixModels.cs (100%) rename {pipeline-import => src}/MagicQuant/Models/Learning/TensorLearningModels.cs (100%) rename {pipeline-import => src}/MagicQuant/Models/PredictionSelectionModels.cs (100%) rename {pipeline-import => src}/MagicQuant/Models/RepositoryCloneModels.cs (100%) rename {pipeline-import => src}/MagicQuant/Program.cs (100%) rename {pipeline-import => src}/MagicQuant/Properties/AssemblyInfo.cs (100%) rename {pipeline-import => src}/MagicQuant/Runtime/IProcessRunner.cs (100%) rename {pipeline-import => src}/MagicQuant/Runtime/NativeCommand.cs (100%) rename {pipeline-import => src}/MagicQuant/Runtime/ProcessRunner.cs (100%) rename {pipeline-import => src}/MagicQuant/Runtime/RunCancellation.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/AnomalyAdjustedPredictionService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/AnomalyRuleRepository.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/AnomalyWorkflowService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ArchitectureFamilyService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/BaselineDefinitionResolver.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/BenchmarkCommands.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/BenchmarkGpuPlanning.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/BenchmarkLogParser.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/BenchmarkService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/CloneConfigManifestGenerationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/CloneManifestTensorMapBuildService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/CloneReadmeGenerationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/CombinationDatabasePathService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/CombinationDuckDbSchema.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/CombinationSurvivalPipelineService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/DuckDbPredictionMaterializationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/EffectiveCandidateStateResolverService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ExternalBaselineTensorParity.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/FinalArtifactNamingService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/FinalReleaseMetadataService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/FinalSurvivorSelectionCliService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/GgufMetadataReader.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/HuggingFaceBaselineService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/HybridArtifactExportService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/HybridBenchmarkRepository.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/HybridMapGenerationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ImatrixIdentityService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ImatrixService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/IsolationDiagnosticsManifestService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/IsolationOptimizationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/IsolationPlanningService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/LearnedBaselinePruningService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/Learning/TensorGroupingAuditService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/LlamaGpuArgumentBuilder.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/MagicQuantManifestPathService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ModelArtifactPathService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ModelCompatibilityService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ModelRuntimePathService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ModelSidecarArtifactService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/NativeModelConversionService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/OutputPathService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/PathSafety.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/PredictionValidationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/Progress/StageProgressOptions.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/Progress/StageProgressSnapshot.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/Progress/StageProgressTracker.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/QuantDatabaseService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/QuantFidelityComparerService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/QuantizationConcurrencyPlan.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/QuantizationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/RankSafeKldPredictionService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ReadmeGenerationService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/RemainingCombinationStore.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/RepositoryCloneManifestService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/RunProvenanceService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/ScratchStorageService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/SelectionDiagnosticsLogService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/SmartBaselineTuningFallbackService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/TargetedRelearnService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/TensorGroupProfileService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/TensorGroupRebucketService.cs (100%) rename {pipeline-import => src}/MagicQuant/Services/TensorGroupReviewService.cs (100%) rename {pipeline-import => src}/MagicQuant/config.default.yaml (100%) rename {pipeline-import => src}/MagicQuant/packages.lock.json (100%) rename {pipeline-import => tests}/MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj (100%) rename {pipeline-import => tests}/MagicQuant.ProcessFixture/Program.cs (100%) rename {pipeline-import => tests}/MagicQuant.ProcessFixture/packages.lock.json (100%) rename {pipeline-import => tests}/MagicQuant.Tests/AnomalyContextScopeTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/AssemblyInfo.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/AuthorityUsageRegressionTests.cs (58%) rename {pipeline-import => tests}/MagicQuant.Tests/BaselineCandidatePolicyTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/BenchmarkContractTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/BenchmarkCorpusTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/CliArgumentParsingTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/CliOptionValidationTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/CliStartupTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/CombinationDatabasePathTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/ConfigurationContractTests.cs (95%) rename {pipeline-import => tests}/MagicQuant.Tests/ConfigurationReadTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/ExternalBaselineTensorParityTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/HardwareInitializationTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/ImatrixIdentityServiceTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/LlamaBinaryPathTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/MagicQuant.Tests.csproj (89%) rename {pipeline-import => tests}/MagicQuant.Tests/ModelSmokeTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/NativeConversionTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/OutputPathTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/PreflightTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/ProcessRunnerTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/QuantizationConcurrencyTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/RunProvenanceTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/ScratchStorageServiceTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/SynergyTransferConfigTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/SynergyTransferPlanningTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/YamlDiagnosticsTests.cs (100%) rename {pipeline-import => tests}/MagicQuant.Tests/packages.lock.json (100%) diff --git a/pipeline-import/.editorconfig b/.editorconfig similarity index 100% rename from pipeline-import/.editorconfig rename to .editorconfig diff --git a/pipeline-import/.github/pull_request_template.md b/.github/pull_request_template.md similarity index 100% rename from pipeline-import/.github/pull_request_template.md rename to .github/pull_request_template.md diff --git a/pipeline-import/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml similarity index 63% rename from pipeline-import/.github/workflows/dotnet.yml rename to .github/workflows/dotnet.yml index 0468c5b..4f5af7a 100644 --- a/pipeline-import/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -19,9 +19,9 @@ jobs: - uses: actions/setup-dotnet@v4 with: dotnet-version: '10.0.x' - - run: dotnet restore MagicQuant-Pipeline.sln --locked-mode -warnaserror - - run: dotnet build MagicQuant-Pipeline.sln --configuration ${{ matrix.configuration }} --no-restore -warnaserror - - run: dotnet test MagicQuant-Pipeline.sln --configuration ${{ matrix.configuration }} --no-build --logger trx --results-directory TestResults + - run: dotnet restore MagicQuant.sln --locked-mode -warnaserror + - run: dotnet build MagicQuant.sln --configuration ${{ matrix.configuration }} --no-restore -warnaserror + - run: dotnet test MagicQuant.sln --configuration ${{ matrix.configuration }} --no-build --logger trx --results-directory TestResults - uses: actions/upload-artifact@v4 if: always() with: diff --git a/pipeline-import/.github/workflows/model-smoke.yml b/.github/workflows/model-smoke.yml similarity index 85% rename from pipeline-import/.github/workflows/model-smoke.yml rename to .github/workflows/model-smoke.yml index 17833cc..ae587fc 100644 --- a/pipeline-import/.github/workflows/model-smoke.yml +++ b/.github/workflows/model-smoke.yml @@ -26,8 +26,8 @@ jobs: - uses: actions/setup-dotnet@v4 with: dotnet-version: '10.0.x' - - run: dotnet restore MagicQuant-Pipeline.sln --locked-mode - - run: dotnet test MagicQuant.Tests -c Release --no-restore --filter Category=ModelSmoke --logger trx --results-directory TestResults + - run: dotnet restore MagicQuant.sln --locked-mode + - run: dotnet test tests/MagicQuant.Tests -c Release --no-restore --filter Category=ModelSmoke --logger trx --results-directory TestResults env: MQ_RUN_MODEL_SMOKE: '1' MQ_SMOKE_MODEL: ${{ inputs.model_path }} diff --git a/.gitignore b/.gitignore index f6b6248..8d53c5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,37 @@ .obsidian/ + +# Build results +bin/ +obj/ + +# Rider / JetBrains +.idea/ +*.sln.iml + +# Visual Studio user settings +*.user +*.userosscache +*.suo +*.cache +*.dbmdl +*.bak +*.ncb +*.opendb +*.VC.db + +# Other common C# stuff +*.log +*.vs/ + +# Local campaigns and generated model/runtime artifacts +config.local.yaml +*.local.yaml +*.dev.yaml +**/MagicQuant_SQLite.db* +*.duckdb +*.duckdb.wal +*.gguf +*.safetensors +.MagicQuant_tmp/ +TestResults/ +artifacts/ diff --git a/pipeline-import/CONTRIBUTING.md b/CONTRIBUTING.md similarity index 93% rename from pipeline-import/CONTRIBUTING.md rename to CONTRIBUTING.md index b80f416..1ef0ae5 100644 --- a/pipeline-import/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,11 +5,11 @@ Start with the [architecture map](docs/architecture.md), [configuration rules](d ## Local workflow ```sh -dotnet restore MagicQuant-Pipeline.sln --locked-mode -dotnet build MagicQuant-Pipeline.sln -c Debug --no-restore -dotnet test MagicQuant-Pipeline.sln -c Debug --no-build -dotnet build MagicQuant-Pipeline.sln -c Release --no-restore -dotnet test MagicQuant-Pipeline.sln -c Release --no-build +dotnet restore MagicQuant.sln --locked-mode +dotnet build MagicQuant.sln -c Debug --no-restore +dotnet test MagicQuant.sln -c Debug --no-build +dotnet build MagicQuant.sln -c Release --no-restore +dotnet test MagicQuant.sln -c Release --no-build ``` CI runs both configurations on Linux and Windows with warnings treated as errors and locked package restores. Use `--filter FullyQualifiedName~YourTestClass` to focus a test run during development. Tests run serially because configuration and runtime registries are global. Source-contract regression tests assume the normal repository/build layout; run the suite from the checkout rather than copying the test DLL elsewhere. diff --git a/pipeline-import/Directory.Build.props b/Directory.Build.props similarity index 100% rename from pipeline-import/Directory.Build.props rename to Directory.Build.props diff --git a/pipeline-import/MagicQuant-Pipeline.sln b/MagicQuant.sln similarity index 81% rename from pipeline-import/MagicQuant-Pipeline.sln rename to MagicQuant.sln index a76ed7f..05df0cb 100644 --- a/pipeline-import/MagicQuant-Pipeline.sln +++ b/MagicQuant.sln @@ -1,12 +1,12 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant", "MagicQuant\MagicQuant.csproj", "{9259012B-0EB2-4AD8-81E5-807FD4465AA3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant", "src\MagicQuant\MagicQuant.csproj", "{9259012B-0EB2-4AD8-81E5-807FD4465AA3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQ.DB", "MQ.DB\MQ.DB.csproj", "{A97D6992-2659-47F9-9AC9-99425D2677A4}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQ.DB", "src\MQ.DB\MQ.DB.csproj", "{A97D6992-2659-47F9-9AC9-99425D2677A4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.Tests", "MagicQuant.Tests\MagicQuant.Tests.csproj", "{D106FC82-5FD7-4C95-BF20-0940C64A234C}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.Tests", "tests\MagicQuant.Tests\MagicQuant.Tests.csproj", "{D106FC82-5FD7-4C95-BF20-0940C64A234C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.ProcessFixture", "MagicQuant.ProcessFixture\MagicQuant.ProcessFixture.csproj", "{6F77FCF7-A105-44A9-A708-2B9F9F2B3B6D}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicQuant.ProcessFixture", "tests\MagicQuant.ProcessFixture\MagicQuant.ProcessFixture.csproj", "{6F77FCF7-A105-44A9-A708-2B9F9F2B3B6D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/pipeline-import/docs/architecture.md b/docs/architecture.md similarity index 100% rename from pipeline-import/docs/architecture.md rename to docs/architecture.md diff --git a/pipeline-import/docs/commands.md b/docs/commands.md similarity index 92% rename from pipeline-import/docs/commands.md rename to docs/commands.md index 4e37d13..ed63850 100644 --- a/pipeline-import/docs/commands.md +++ b/docs/commands.md @@ -5,7 +5,7 @@ Run these examples from the repository root after a Release build. Replace paths ## Full discovery pipeline ```sh -dotnet run --project MagicQuant -c Release --no-build -- pipeline \ +dotnet run --project src/MagicQuant -c Release --no-build -- pipeline \ --config config.local.yaml \ --model-dir /data/models/my-model \ --architecture-family my-model-family \ @@ -20,7 +20,7 @@ The pipeline converts/loads the native source, reviews tensor groups, resolves i ## Clone known tensor configurations ```sh -dotnet run --project MagicQuant -c Release --no-build -- clone-repository-quants \ +dotnet run --project src/MagicQuant -c Release --no-build -- clone-repository-quants \ --config config.local.yaml \ --model-dir /data/models/compatible-model \ --architecture-family my-model-family \ @@ -35,7 +35,7 @@ By default the manifest must match the target tensor inventory. `--allow-missing ## Validate predictions against existing measurements ```sh -dotnet run --project MagicQuant -c Release --no-build -- validate-predictions \ +dotnet run --project src/MagicQuant -c Release --no-build -- validate-predictions \ --config config.local.yaml \ --model-dir /data/models/my-model \ --architecture-family my-model-family \ @@ -49,7 +49,7 @@ For imatrix measurements supply `--imatrix-path /data/imatrix.dat` or `--imatrix ## Rerun and reuse ```sh -dotnet run --project MagicQuant -c Release --no-build -- pipeline \ +dotnet run --project src/MagicQuant -c Release --no-build -- pipeline \ --config config.local.yaml --reuse-existing-final-artifacts ``` diff --git a/pipeline-import/docs/configuration.md b/docs/configuration.md similarity index 100% rename from pipeline-import/docs/configuration.md rename to docs/configuration.md diff --git a/pipeline-import/docs/extending.md b/docs/extending.md similarity index 100% rename from pipeline-import/docs/extending.md rename to docs/extending.md diff --git a/pipeline-import/docs/migration.md b/docs/migration.md similarity index 100% rename from pipeline-import/docs/migration.md rename to docs/migration.md diff --git a/pipeline-import/README.md b/docs/pipeline-migration-readme.md similarity index 88% rename from pipeline-import/README.md rename to docs/pipeline-migration-readme.md index 48779f8..fb01630 100644 --- a/pipeline-import/README.md +++ b/docs/pipeline-migration-readme.md @@ -9,11 +9,11 @@ This repository contains the .NET command-line application. The [MagicQuant rese Install the .NET 10 SDK, then run from the repository root: ```sh -dotnet restore MagicQuant-Pipeline.sln -dotnet build MagicQuant-Pipeline.sln -c Release -dotnet test MagicQuant-Pipeline.sln -c Release --no-build -dotnet run --project MagicQuant -c Release --no-build -- --help -dotnet run --project MagicQuant -c Release --no-build -- pipeline --help +dotnet restore MagicQuant.sln +dotnet build MagicQuant.sln -c Release +dotnet test MagicQuant.sln -c Release --no-build +dotnet run --project src/MagicQuant -c Release --no-build -- --help +dotnet run --project src/MagicQuant -c Release --no-build -- pipeline --help ``` Ordinary tests skip the explicitly opt-in model smoke test. Building, ordinary testing, and viewing help do not require model weights or llama.cpp. Running without arguments also shows help, in both Debug and Release. @@ -25,7 +25,7 @@ Real quantization needs a complete local Hugging Face model directory (top-level 1. Copy the distributed tuning profile and edit the paths and model identity: ```sh - cp MagicQuant/config.default.yaml config.local.yaml + cp src/MagicQuant/config.default.yaml config.local.yaml ``` Set `paths.model_dir` and `identity.architecture_family_name`. Choose a dedicated `output.output_dir` and set `output.output_name_prefix`. Before publishing generated model cards, set `readme.frontmatter` to the source model's actual license and metadata. Use absolute paths for a portable campaign invocation. @@ -33,7 +33,7 @@ Real quantization needs a complete local Hugging Face model directory (top-level 2. Prepare dependencies: ```sh - dotnet run --project MagicQuant -c Release --no-build -- initialize-llama-cpp + dotnet run --project src/MagicQuant -c Release --no-build -- initialize-llama-cpp ``` This can download/build llama.cpp, install Python packages, and request sudo for apt packages on Linux. It uses `/MagicQuant`. To use existing llama.cpp files, configure **all three** of `paths.llama_root`, `paths.llama_bin`, and `paths.convert_script`, and pass `--config config.local.yaml`. See [setup](docs/setup.md) for Python requirements and custom runtime roots. @@ -41,13 +41,13 @@ Real quantization needs a complete local Hugging Face model directory (top-level 3. Validate before starting the campaign: ```sh - dotnet run --project MagicQuant -c Release --no-build -- pipeline --config config.local.yaml --check-config --strict-config + dotnet run --project src/MagicQuant -c Release --no-build -- pipeline --config config.local.yaml --check-config --strict-config ``` Then start it: ```sh - dotnet run --project MagicQuant -c Release --no-build -- pipeline --config config.local.yaml + dotnet run --project src/MagicQuant -c Release --no-build -- pipeline --config config.local.yaml ``` Review the tensor grouping prompt before allowing learning to continue. The run learns/reuses benchmark truth and exports its selected survivors. Runtime dependency validation may perform setup when using the default environment. diff --git a/pipeline-import/docs/setup.md b/docs/setup.md similarity index 96% rename from pipeline-import/docs/setup.md rename to docs/setup.md index 4f376d2..614cc54 100644 --- a/pipeline-import/docs/setup.md +++ b/docs/setup.md @@ -5,9 +5,9 @@ All solution projects target `net10.0`. Use the .NET 10 SDK. NuGet restore downloads the managed packages and native SQLite/DuckDB assets. The solution includes `MagicQuant`, `MQ.DB`, `MagicQuant.Tests`, and the offline `MagicQuant.ProcessFixture` test helper. ```sh -dotnet restore MagicQuant-Pipeline.sln -dotnet build MagicQuant-Pipeline.sln -c Release -dotnet test MagicQuant-Pipeline.sln -c Release --no-build +dotnet restore MagicQuant.sln +dotnet build MagicQuant.sln -c Release +dotnet test MagicQuant.sln -c Release --no-build ``` These commands do not install llama.cpp or Python packages. Some regression tests create temporary SQLite databases and inspect local hardware. Tests do not require CUDA or model weights. diff --git a/pipeline-import/docs/storage.md b/docs/storage.md similarity index 100% rename from pipeline-import/docs/storage.md rename to docs/storage.md diff --git a/pipeline-import/docs/testing.md b/docs/testing.md similarity index 90% rename from pipeline-import/docs/testing.md rename to docs/testing.md index 15366f2..26f172d 100644 --- a/pipeline-import/docs/testing.md +++ b/docs/testing.md @@ -3,9 +3,9 @@ ## Ordinary checks ```sh -dotnet restore MagicQuant-Pipeline.sln --locked-mode -warnaserror -dotnet build MagicQuant-Pipeline.sln -c Release --no-restore -warnaserror -dotnet test MagicQuant-Pipeline.sln -c Release --no-build +dotnet restore MagicQuant.sln --locked-mode -warnaserror +dotnet build MagicQuant.sln -c Release --no-restore -warnaserror +dotnet test MagicQuant.sln -c Release --no-build ``` Repeat with `-c Debug` when changing startup or compilation-dependent behavior. CI runs both configurations on Linux and Windows. It restores the committed NuGet lock files, treats warnings as errors, runs all ordinary tests, and uploads TRX reports. `MagicQuant.ProcessFixture` is a small offline executable used to test native process exit, full stdout/stderr pipes, literal arguments, and cancellation; it is not a user command. @@ -15,13 +15,13 @@ The suite covers CLI startup/preflight, YAML contracts, managed/output path cont To update dependencies intentionally, edit package versions, run an unlocked `dotnet restore`, review `packages.lock.json` changes, and rerun the suite. Audit with: ```sh -dotnet list MagicQuant-Pipeline.sln package --vulnerable --include-transitive +dotnet list MagicQuant.sln package --vulnerable --include-transitive ``` ## Read-only campaign validation ```sh -dotnet run --project MagicQuant -c Release -- pipeline \ +dotnet run --project src/MagicQuant -c Release -- pipeline \ --config config.local.yaml --check-config --strict-config ``` @@ -37,7 +37,7 @@ MQ_SMOKE_MODEL=/data/models/small-model \ MQ_SMOKE_LLAMA_ROOT=/opt/llama.cpp \ MQ_SMOKE_RUNTIME_ROOT=/data/MagicQuant \ MQ_SMOKE_OUTPUT=/data/test-results/magicquant \ -dotnet test MagicQuant.Tests -c Release --filter Category=ModelSmoke +dotnet test tests/MagicQuant.Tests -c Release --filter Category=ModelSmoke ``` The runtime root must contain `MagicQuant-Env` with the converter/gguf dependencies already installed. The test does not install dependencies or download a model. It copies source metadata and links weights into a unique test model directory containing spaces, then exercises native conversion/reuse, Q8 scratch leases, export/reuse, GGUF metadata parity, the native CPU benchmark, and manifest-path writing. It has a 20-minute cancellation deadline. It removes generated GGUFs and input weight links and retains logs plus `smoke-result.json` beneath the output parent. The sample tensor-map JSON is a smoke artifact, not a full clone/release manifest. diff --git a/pipeline-import/examples/clone.yaml b/examples/clone.yaml similarity index 100% rename from pipeline-import/examples/clone.yaml rename to examples/clone.yaml diff --git a/pipeline-import/examples/pipeline.yaml b/examples/pipeline.yaml similarity index 100% rename from pipeline-import/examples/pipeline.yaml rename to examples/pipeline.yaml diff --git a/pipeline-import/.gitignore b/pipeline-import/.gitignore deleted file mode 100644 index f4708ae..0000000 --- a/pipeline-import/.gitignore +++ /dev/null @@ -1,35 +0,0 @@ -# Build results -bin/ -obj/ - -# Rider / JetBrains -.idea/ -*.sln.iml - -# Visual Studio user settings -*.user -*.userosscache -*.suo -*.cache -*.dbmdl -*.bak -*.ncb -*.opendb -*.VC.db - -# Other common C# stuff -*.log -*.vs/ - -# Local campaigns and generated model/runtime artifacts -config.local.yaml -*.local.yaml -*.dev.yaml -**/MagicQuant_SQLite.db* -*.duckdb -*.duckdb.wal -*.gguf -*.safetensors -.MagicQuant_tmp/ -TestResults/ -artifacts/ diff --git a/pipeline-import/MQ.DB/Cache.cs b/src/MQ.DB/Cache.cs similarity index 100% rename from pipeline-import/MQ.DB/Cache.cs rename to src/MQ.DB/Cache.cs diff --git a/pipeline-import/MQ.DB/Data/MagicQuantContext.cs b/src/MQ.DB/Data/MagicQuantContext.cs similarity index 100% rename from pipeline-import/MQ.DB/Data/MagicQuantContext.cs rename to src/MQ.DB/Data/MagicQuantContext.cs diff --git a/pipeline-import/MQ.DB/Interfaces/ISQLiteEntity.cs b/src/MQ.DB/Interfaces/ISQLiteEntity.cs similarity index 100% rename from pipeline-import/MQ.DB/Interfaces/ISQLiteEntity.cs rename to src/MQ.DB/Interfaces/ISQLiteEntity.cs diff --git a/pipeline-import/MQ.DB/MQ.DB.csproj b/src/MQ.DB/MQ.DB.csproj similarity index 100% rename from pipeline-import/MQ.DB/MQ.DB.csproj rename to src/MQ.DB/MQ.DB.csproj diff --git a/pipeline-import/MQ.DB/Migrations/20260501195554_InitialCreate.Designer.cs b/src/MQ.DB/Migrations/20260501195554_InitialCreate.Designer.cs similarity index 100% rename from pipeline-import/MQ.DB/Migrations/20260501195554_InitialCreate.Designer.cs rename to src/MQ.DB/Migrations/20260501195554_InitialCreate.Designer.cs diff --git a/pipeline-import/MQ.DB/Migrations/20260501195554_InitialCreate.cs b/src/MQ.DB/Migrations/20260501195554_InitialCreate.cs similarity index 100% rename from pipeline-import/MQ.DB/Migrations/20260501195554_InitialCreate.cs rename to src/MQ.DB/Migrations/20260501195554_InitialCreate.cs diff --git a/pipeline-import/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs b/src/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs similarity index 100% rename from pipeline-import/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs rename to src/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.Designer.cs diff --git a/pipeline-import/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs b/src/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs similarity index 100% rename from pipeline-import/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs rename to src/MQ.DB/Migrations/20260503222121_PredictionEngineAnomalyDetect.cs diff --git a/pipeline-import/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs b/src/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs similarity index 100% rename from pipeline-import/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs rename to src/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.Designer.cs diff --git a/pipeline-import/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs b/src/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs similarity index 100% rename from pipeline-import/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs rename to src/MQ.DB/Migrations/20260503224957_PredictionEngineAnomalyDetect2.cs diff --git a/pipeline-import/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs b/src/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs similarity index 100% rename from pipeline-import/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs rename to src/MQ.DB/Migrations/MagicQuantContextModelSnapshot.cs diff --git a/pipeline-import/MQ.DB/Models/BaselineQuants.cs b/src/MQ.DB/Models/BaselineQuants.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/BaselineQuants.cs rename to src/MQ.DB/Models/BaselineQuants.cs diff --git a/pipeline-import/MQ.DB/Models/BenchmarkResult.cs b/src/MQ.DB/Models/BenchmarkResult.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/BenchmarkResult.cs rename to src/MQ.DB/Models/BenchmarkResult.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/AiBenchmark.cs b/src/MQ.DB/Models/DbModels/AiBenchmark.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/AiBenchmark.cs rename to src/MQ.DB/Models/DbModels/AiBenchmark.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs b/src/MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs rename to src/MQ.DB/Models/DbModels/AiBenchmarkLearnedSource.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/AiModelHash.cs b/src/MQ.DB/Models/DbModels/AiModelHash.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/AiModelHash.cs rename to src/MQ.DB/Models/DbModels/AiModelHash.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/AnomalyProbeSession.cs b/src/MQ.DB/Models/DbModels/AnomalyProbeSession.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/AnomalyProbeSession.cs rename to src/MQ.DB/Models/DbModels/AnomalyProbeSession.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/ArchitectureFamily.cs b/src/MQ.DB/Models/DbModels/ArchitectureFamily.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/ArchitectureFamily.cs rename to src/MQ.DB/Models/DbModels/ArchitectureFamily.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs b/src/MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs rename to src/MQ.DB/Models/DbModels/ArchitectureFamilyModelHash.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs b/src/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs rename to src/MQ.DB/Models/DbModels/BaselineQuantDefinition.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/BenchmarkRun.cs b/src/MQ.DB/Models/DbModels/BenchmarkRun.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/BenchmarkRun.cs rename to src/MQ.DB/Models/DbModels/BenchmarkRun.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs b/src/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs rename to src/MQ.DB/Models/DbModels/ExecutionPlanProbeCache.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/ImatrixDefinition.cs b/src/MQ.DB/Models/DbModels/ImatrixDefinition.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/ImatrixDefinition.cs rename to src/MQ.DB/Models/DbModels/ImatrixDefinition.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs b/src/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs rename to src/MQ.DB/Models/DbModels/LearnedBaselineTensorQuant.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/QuantizationRun.cs b/src/MQ.DB/Models/DbModels/QuantizationRun.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/QuantizationRun.cs rename to src/MQ.DB/Models/DbModels/QuantizationRun.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/TensorCombo.cs b/src/MQ.DB/Models/DbModels/TensorCombo.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/TensorCombo.cs rename to src/MQ.DB/Models/DbModels/TensorCombo.cs diff --git a/pipeline-import/MQ.DB/Models/DbModels/TensorGroupProfile.cs b/src/MQ.DB/Models/DbModels/TensorGroupProfile.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/DbModels/TensorGroupProfile.cs rename to src/MQ.DB/Models/DbModels/TensorGroupProfile.cs diff --git a/pipeline-import/MQ.DB/Models/HybridQuant.cs b/src/MQ.DB/Models/HybridQuant.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/HybridQuant.cs rename to src/MQ.DB/Models/HybridQuant.cs diff --git a/pipeline-import/MQ.DB/Models/IsolationRules.cs b/src/MQ.DB/Models/IsolationRules.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/IsolationRules.cs rename to src/MQ.DB/Models/IsolationRules.cs diff --git a/pipeline-import/MQ.DB/Models/LlamaBenchMetrics.cs b/src/MQ.DB/Models/LlamaBenchMetrics.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/LlamaBenchMetrics.cs rename to src/MQ.DB/Models/LlamaBenchMetrics.cs diff --git a/pipeline-import/MQ.DB/Models/LlamaBinaries.cs b/src/MQ.DB/Models/LlamaBinaries.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/LlamaBinaries.cs rename to src/MQ.DB/Models/LlamaBinaries.cs diff --git a/pipeline-import/MQ.DB/Models/PplMetrics.cs b/src/MQ.DB/Models/PplMetrics.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/PplMetrics.cs rename to src/MQ.DB/Models/PplMetrics.cs diff --git a/pipeline-import/MQ.DB/Models/RequiredSamplePlan.cs b/src/MQ.DB/Models/RequiredSamplePlan.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/RequiredSamplePlan.cs rename to src/MQ.DB/Models/RequiredSamplePlan.cs diff --git a/pipeline-import/MQ.DB/Models/SystemInfo.cs b/src/MQ.DB/Models/SystemInfo.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/SystemInfo.cs rename to src/MQ.DB/Models/SystemInfo.cs diff --git a/pipeline-import/MQ.DB/Models/TensorConfigs.cs b/src/MQ.DB/Models/TensorConfigs.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/TensorConfigs.cs rename to src/MQ.DB/Models/TensorConfigs.cs diff --git a/pipeline-import/MQ.DB/Models/TensorGroup.cs b/src/MQ.DB/Models/TensorGroup.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/TensorGroup.cs rename to src/MQ.DB/Models/TensorGroup.cs diff --git a/pipeline-import/MQ.DB/Models/TensorGroupSynergy.cs b/src/MQ.DB/Models/TensorGroupSynergy.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/TensorGroupSynergy.cs rename to src/MQ.DB/Models/TensorGroupSynergy.cs diff --git a/pipeline-import/MQ.DB/Models/TensorWeight.cs b/src/MQ.DB/Models/TensorWeight.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/TensorWeight.cs rename to src/MQ.DB/Models/TensorWeight.cs diff --git a/pipeline-import/MQ.DB/Models/TensorWeightScheme.cs b/src/MQ.DB/Models/TensorWeightScheme.cs similarity index 100% rename from pipeline-import/MQ.DB/Models/TensorWeightScheme.cs rename to src/MQ.DB/Models/TensorWeightScheme.cs diff --git a/pipeline-import/MQ.DB/packages.lock.json b/src/MQ.DB/packages.lock.json similarity index 100% rename from pipeline-import/MQ.DB/packages.lock.json rename to src/MQ.DB/packages.lock.json diff --git a/pipeline-import/MQ.DB/tensor_groups.yaml b/src/MQ.DB/tensor_groups.yaml similarity index 100% rename from pipeline-import/MQ.DB/tensor_groups.yaml rename to src/MQ.DB/tensor_groups.yaml diff --git a/pipeline-import/MagicQuant/Commands/BuildHybrids.cs b/src/MagicQuant/Commands/BuildHybrids.cs similarity index 100% rename from pipeline-import/MagicQuant/Commands/BuildHybrids.cs rename to src/MagicQuant/Commands/BuildHybrids.cs diff --git a/pipeline-import/MagicQuant/Commands/CloneRepositoryQuants.cs b/src/MagicQuant/Commands/CloneRepositoryQuants.cs similarity index 100% rename from pipeline-import/MagicQuant/Commands/CloneRepositoryQuants.cs rename to src/MagicQuant/Commands/CloneRepositoryQuants.cs diff --git a/pipeline-import/MagicQuant/Commands/CommandCatalog.cs b/src/MagicQuant/Commands/CommandCatalog.cs similarity index 100% rename from pipeline-import/MagicQuant/Commands/CommandCatalog.cs rename to src/MagicQuant/Commands/CommandCatalog.cs diff --git a/pipeline-import/MagicQuant/Commands/Evolution.cs b/src/MagicQuant/Commands/Evolution.cs similarity index 100% rename from pipeline-import/MagicQuant/Commands/Evolution.cs rename to src/MagicQuant/Commands/Evolution.cs diff --git a/pipeline-import/MagicQuant/Commands/InitializeLlamaCpp.cs b/src/MagicQuant/Commands/InitializeLlamaCpp.cs similarity index 100% rename from pipeline-import/MagicQuant/Commands/InitializeLlamaCpp.cs rename to src/MagicQuant/Commands/InitializeLlamaCpp.cs diff --git a/pipeline-import/MagicQuant/Commands/QuantizationPipeline.cs b/src/MagicQuant/Commands/QuantizationPipeline.cs similarity index 100% rename from pipeline-import/MagicQuant/Commands/QuantizationPipeline.cs rename to src/MagicQuant/Commands/QuantizationPipeline.cs diff --git a/pipeline-import/MagicQuant/Commands/ValidatePredictions.cs b/src/MagicQuant/Commands/ValidatePredictions.cs similarity index 100% rename from pipeline-import/MagicQuant/Commands/ValidatePredictions.cs rename to src/MagicQuant/Commands/ValidatePredictions.cs diff --git a/pipeline-import/MagicQuant/Config.cs b/src/MagicQuant/Config.cs similarity index 100% rename from pipeline-import/MagicQuant/Config.cs rename to src/MagicQuant/Config.cs diff --git a/pipeline-import/MagicQuant/Configuration/CliOptionValidator.cs b/src/MagicQuant/Configuration/CliOptionValidator.cs similarity index 100% rename from pipeline-import/MagicQuant/Configuration/CliOptionValidator.cs rename to src/MagicQuant/Configuration/CliOptionValidator.cs diff --git a/pipeline-import/MagicQuant/Configuration/CommandPreflight.cs b/src/MagicQuant/Configuration/CommandPreflight.cs similarity index 100% rename from pipeline-import/MagicQuant/Configuration/CommandPreflight.cs rename to src/MagicQuant/Configuration/CommandPreflight.cs diff --git a/pipeline-import/MagicQuant/Configuration/ConfigurationShapeValidator.cs b/src/MagicQuant/Configuration/ConfigurationShapeValidator.cs similarity index 100% rename from pipeline-import/MagicQuant/Configuration/ConfigurationShapeValidator.cs rename to src/MagicQuant/Configuration/ConfigurationShapeValidator.cs diff --git a/pipeline-import/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/src/MagicQuant/Configuration/MagicQuantYamlConfig.cs similarity index 100% rename from pipeline-import/MagicQuant/Configuration/MagicQuantYamlConfig.cs rename to src/MagicQuant/Configuration/MagicQuantYamlConfig.cs diff --git a/pipeline-import/MagicQuant/Configuration/MagicQuantYamlLoader.cs b/src/MagicQuant/Configuration/MagicQuantYamlLoader.cs similarity index 100% rename from pipeline-import/MagicQuant/Configuration/MagicQuantYamlLoader.cs rename to src/MagicQuant/Configuration/MagicQuantYamlLoader.cs diff --git a/pipeline-import/MagicQuant/Configuration/YamlConfigurationDiagnostics.cs b/src/MagicQuant/Configuration/YamlConfigurationDiagnostics.cs similarity index 100% rename from pipeline-import/MagicQuant/Configuration/YamlConfigurationDiagnostics.cs rename to src/MagicQuant/Configuration/YamlConfigurationDiagnostics.cs diff --git a/pipeline-import/MagicQuant/Helpers/CliHelpers.cs b/src/MagicQuant/Helpers/CliHelpers.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/CliHelpers.cs rename to src/MagicQuant/Helpers/CliHelpers.cs diff --git a/pipeline-import/MagicQuant/Helpers/ComboLogic.cs b/src/MagicQuant/Helpers/ComboLogic.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/ComboLogic.cs rename to src/MagicQuant/Helpers/ComboLogic.cs diff --git a/pipeline-import/MagicQuant/Helpers/DependencyManager.cs b/src/MagicQuant/Helpers/DependencyManager.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/DependencyManager.cs rename to src/MagicQuant/Helpers/DependencyManager.cs diff --git a/pipeline-import/MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs b/src/MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs rename to src/MagicQuant/Helpers/EquivalentTruthSelectionHelper.cs diff --git a/pipeline-import/MagicQuant/Helpers/HardDeleteHelper.cs b/src/MagicQuant/Helpers/HardDeleteHelper.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/HardDeleteHelper.cs rename to src/MagicQuant/Helpers/HardDeleteHelper.cs diff --git a/pipeline-import/MagicQuant/Helpers/HardwareHelper.cs b/src/MagicQuant/Helpers/HardwareHelper.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/HardwareHelper.cs rename to src/MagicQuant/Helpers/HardwareHelper.cs diff --git a/pipeline-import/MagicQuant/Helpers/IsolationPruningConfig.cs b/src/MagicQuant/Helpers/IsolationPruningConfig.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/IsolationPruningConfig.cs rename to src/MagicQuant/Helpers/IsolationPruningConfig.cs diff --git a/pipeline-import/MagicQuant/Helpers/JsonHelper.cs b/src/MagicQuant/Helpers/JsonHelper.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/JsonHelper.cs rename to src/MagicQuant/Helpers/JsonHelper.cs diff --git a/pipeline-import/MagicQuant/Helpers/LinuxHelper.cs b/src/MagicQuant/Helpers/LinuxHelper.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/LinuxHelper.cs rename to src/MagicQuant/Helpers/LinuxHelper.cs diff --git a/pipeline-import/MagicQuant/Helpers/LlamaBuilder.cs b/src/MagicQuant/Helpers/LlamaBuilder.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/LlamaBuilder.cs rename to src/MagicQuant/Helpers/LlamaBuilder.cs diff --git a/pipeline-import/MagicQuant/Helpers/MagicQuantDiagnostics.cs b/src/MagicQuant/Helpers/MagicQuantDiagnostics.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/MagicQuantDiagnostics.cs rename to src/MagicQuant/Helpers/MagicQuantDiagnostics.cs diff --git a/pipeline-import/MagicQuant/Helpers/MagicQuantModelId.cs b/src/MagicQuant/Helpers/MagicQuantModelId.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/MagicQuantModelId.cs rename to src/MagicQuant/Helpers/MagicQuantModelId.cs diff --git a/pipeline-import/MagicQuant/Helpers/NativePrecisionNormalization.cs b/src/MagicQuant/Helpers/NativePrecisionNormalization.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/NativePrecisionNormalization.cs rename to src/MagicQuant/Helpers/NativePrecisionNormalization.cs diff --git a/pipeline-import/MagicQuant/Helpers/PythonManager.cs b/src/MagicQuant/Helpers/PythonManager.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/PythonManager.cs rename to src/MagicQuant/Helpers/PythonManager.cs diff --git a/pipeline-import/MagicQuant/Helpers/RuntimeSearchSpace.cs b/src/MagicQuant/Helpers/RuntimeSearchSpace.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/RuntimeSearchSpace.cs rename to src/MagicQuant/Helpers/RuntimeSearchSpace.cs diff --git a/pipeline-import/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs b/src/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs rename to src/MagicQuant/Helpers/SearchSpaceDebugPrinter.cs diff --git a/pipeline-import/MagicQuant/Helpers/TensorConfigGenerator.cs b/src/MagicQuant/Helpers/TensorConfigGenerator.cs similarity index 100% rename from pipeline-import/MagicQuant/Helpers/TensorConfigGenerator.cs rename to src/MagicQuant/Helpers/TensorConfigGenerator.cs diff --git a/pipeline-import/MagicQuant/Helpers/pip_runner.py b/src/MagicQuant/Helpers/pip_runner.py similarity index 100% rename from pipeline-import/MagicQuant/Helpers/pip_runner.py rename to src/MagicQuant/Helpers/pip_runner.py diff --git a/pipeline-import/MagicQuant/Interfaces/ICommand.cs b/src/MagicQuant/Interfaces/ICommand.cs similarity index 100% rename from pipeline-import/MagicQuant/Interfaces/ICommand.cs rename to src/MagicQuant/Interfaces/ICommand.cs diff --git a/pipeline-import/MagicQuant/MagicQuant.csproj b/src/MagicQuant/MagicQuant.csproj similarity index 100% rename from pipeline-import/MagicQuant/MagicQuant.csproj rename to src/MagicQuant/MagicQuant.csproj diff --git a/pipeline-import/MagicQuant/Models/AnomalyDetectionModels.cs b/src/MagicQuant/Models/AnomalyDetectionModels.cs similarity index 100% rename from pipeline-import/MagicQuant/Models/AnomalyDetectionModels.cs rename to src/MagicQuant/Models/AnomalyDetectionModels.cs diff --git a/pipeline-import/MagicQuant/Models/CliArg.cs b/src/MagicQuant/Models/CliArg.cs similarity index 100% rename from pipeline-import/MagicQuant/Models/CliArg.cs rename to src/MagicQuant/Models/CliArg.cs diff --git a/pipeline-import/MagicQuant/Models/HybridFinalizationModels.cs b/src/MagicQuant/Models/HybridFinalizationModels.cs similarity index 100% rename from pipeline-import/MagicQuant/Models/HybridFinalizationModels.cs rename to src/MagicQuant/Models/HybridFinalizationModels.cs diff --git a/pipeline-import/MagicQuant/Models/ImatrixModels.cs b/src/MagicQuant/Models/ImatrixModels.cs similarity index 100% rename from pipeline-import/MagicQuant/Models/ImatrixModels.cs rename to src/MagicQuant/Models/ImatrixModels.cs diff --git a/pipeline-import/MagicQuant/Models/Learning/TensorLearningModels.cs b/src/MagicQuant/Models/Learning/TensorLearningModels.cs similarity index 100% rename from pipeline-import/MagicQuant/Models/Learning/TensorLearningModels.cs rename to src/MagicQuant/Models/Learning/TensorLearningModels.cs diff --git a/pipeline-import/MagicQuant/Models/PredictionSelectionModels.cs b/src/MagicQuant/Models/PredictionSelectionModels.cs similarity index 100% rename from pipeline-import/MagicQuant/Models/PredictionSelectionModels.cs rename to src/MagicQuant/Models/PredictionSelectionModels.cs diff --git a/pipeline-import/MagicQuant/Models/RepositoryCloneModels.cs b/src/MagicQuant/Models/RepositoryCloneModels.cs similarity index 100% rename from pipeline-import/MagicQuant/Models/RepositoryCloneModels.cs rename to src/MagicQuant/Models/RepositoryCloneModels.cs diff --git a/pipeline-import/MagicQuant/Program.cs b/src/MagicQuant/Program.cs similarity index 100% rename from pipeline-import/MagicQuant/Program.cs rename to src/MagicQuant/Program.cs diff --git a/pipeline-import/MagicQuant/Properties/AssemblyInfo.cs b/src/MagicQuant/Properties/AssemblyInfo.cs similarity index 100% rename from pipeline-import/MagicQuant/Properties/AssemblyInfo.cs rename to src/MagicQuant/Properties/AssemblyInfo.cs diff --git a/pipeline-import/MagicQuant/Runtime/IProcessRunner.cs b/src/MagicQuant/Runtime/IProcessRunner.cs similarity index 100% rename from pipeline-import/MagicQuant/Runtime/IProcessRunner.cs rename to src/MagicQuant/Runtime/IProcessRunner.cs diff --git a/pipeline-import/MagicQuant/Runtime/NativeCommand.cs b/src/MagicQuant/Runtime/NativeCommand.cs similarity index 100% rename from pipeline-import/MagicQuant/Runtime/NativeCommand.cs rename to src/MagicQuant/Runtime/NativeCommand.cs diff --git a/pipeline-import/MagicQuant/Runtime/ProcessRunner.cs b/src/MagicQuant/Runtime/ProcessRunner.cs similarity index 100% rename from pipeline-import/MagicQuant/Runtime/ProcessRunner.cs rename to src/MagicQuant/Runtime/ProcessRunner.cs diff --git a/pipeline-import/MagicQuant/Runtime/RunCancellation.cs b/src/MagicQuant/Runtime/RunCancellation.cs similarity index 100% rename from pipeline-import/MagicQuant/Runtime/RunCancellation.cs rename to src/MagicQuant/Runtime/RunCancellation.cs diff --git a/pipeline-import/MagicQuant/Services/AnomalyAdjustedPredictionService.cs b/src/MagicQuant/Services/AnomalyAdjustedPredictionService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/AnomalyAdjustedPredictionService.cs rename to src/MagicQuant/Services/AnomalyAdjustedPredictionService.cs diff --git a/pipeline-import/MagicQuant/Services/AnomalyRuleRepository.cs b/src/MagicQuant/Services/AnomalyRuleRepository.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/AnomalyRuleRepository.cs rename to src/MagicQuant/Services/AnomalyRuleRepository.cs diff --git a/pipeline-import/MagicQuant/Services/AnomalyWorkflowService.cs b/src/MagicQuant/Services/AnomalyWorkflowService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/AnomalyWorkflowService.cs rename to src/MagicQuant/Services/AnomalyWorkflowService.cs diff --git a/pipeline-import/MagicQuant/Services/ArchitectureFamilyService.cs b/src/MagicQuant/Services/ArchitectureFamilyService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ArchitectureFamilyService.cs rename to src/MagicQuant/Services/ArchitectureFamilyService.cs diff --git a/pipeline-import/MagicQuant/Services/BaselineDefinitionResolver.cs b/src/MagicQuant/Services/BaselineDefinitionResolver.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/BaselineDefinitionResolver.cs rename to src/MagicQuant/Services/BaselineDefinitionResolver.cs diff --git a/pipeline-import/MagicQuant/Services/BenchmarkCommands.cs b/src/MagicQuant/Services/BenchmarkCommands.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/BenchmarkCommands.cs rename to src/MagicQuant/Services/BenchmarkCommands.cs diff --git a/pipeline-import/MagicQuant/Services/BenchmarkGpuPlanning.cs b/src/MagicQuant/Services/BenchmarkGpuPlanning.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/BenchmarkGpuPlanning.cs rename to src/MagicQuant/Services/BenchmarkGpuPlanning.cs diff --git a/pipeline-import/MagicQuant/Services/BenchmarkLogParser.cs b/src/MagicQuant/Services/BenchmarkLogParser.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/BenchmarkLogParser.cs rename to src/MagicQuant/Services/BenchmarkLogParser.cs diff --git a/pipeline-import/MagicQuant/Services/BenchmarkService.cs b/src/MagicQuant/Services/BenchmarkService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/BenchmarkService.cs rename to src/MagicQuant/Services/BenchmarkService.cs diff --git a/pipeline-import/MagicQuant/Services/CloneConfigManifestGenerationService.cs b/src/MagicQuant/Services/CloneConfigManifestGenerationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/CloneConfigManifestGenerationService.cs rename to src/MagicQuant/Services/CloneConfigManifestGenerationService.cs diff --git a/pipeline-import/MagicQuant/Services/CloneManifestTensorMapBuildService.cs b/src/MagicQuant/Services/CloneManifestTensorMapBuildService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/CloneManifestTensorMapBuildService.cs rename to src/MagicQuant/Services/CloneManifestTensorMapBuildService.cs diff --git a/pipeline-import/MagicQuant/Services/CloneReadmeGenerationService.cs b/src/MagicQuant/Services/CloneReadmeGenerationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/CloneReadmeGenerationService.cs rename to src/MagicQuant/Services/CloneReadmeGenerationService.cs diff --git a/pipeline-import/MagicQuant/Services/CombinationDatabasePathService.cs b/src/MagicQuant/Services/CombinationDatabasePathService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/CombinationDatabasePathService.cs rename to src/MagicQuant/Services/CombinationDatabasePathService.cs diff --git a/pipeline-import/MagicQuant/Services/CombinationDuckDbSchema.cs b/src/MagicQuant/Services/CombinationDuckDbSchema.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/CombinationDuckDbSchema.cs rename to src/MagicQuant/Services/CombinationDuckDbSchema.cs diff --git a/pipeline-import/MagicQuant/Services/CombinationSurvivalPipelineService.cs b/src/MagicQuant/Services/CombinationSurvivalPipelineService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/CombinationSurvivalPipelineService.cs rename to src/MagicQuant/Services/CombinationSurvivalPipelineService.cs diff --git a/pipeline-import/MagicQuant/Services/DuckDbPredictionMaterializationService.cs b/src/MagicQuant/Services/DuckDbPredictionMaterializationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/DuckDbPredictionMaterializationService.cs rename to src/MagicQuant/Services/DuckDbPredictionMaterializationService.cs diff --git a/pipeline-import/MagicQuant/Services/EffectiveCandidateStateResolverService.cs b/src/MagicQuant/Services/EffectiveCandidateStateResolverService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/EffectiveCandidateStateResolverService.cs rename to src/MagicQuant/Services/EffectiveCandidateStateResolverService.cs diff --git a/pipeline-import/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs b/src/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs rename to src/MagicQuant/Services/ExternalBaselineCacheCleanupService.cs diff --git a/pipeline-import/MagicQuant/Services/ExternalBaselineTensorParity.cs b/src/MagicQuant/Services/ExternalBaselineTensorParity.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ExternalBaselineTensorParity.cs rename to src/MagicQuant/Services/ExternalBaselineTensorParity.cs diff --git a/pipeline-import/MagicQuant/Services/FinalArtifactNamingService.cs b/src/MagicQuant/Services/FinalArtifactNamingService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/FinalArtifactNamingService.cs rename to src/MagicQuant/Services/FinalArtifactNamingService.cs diff --git a/pipeline-import/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs b/src/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs rename to src/MagicQuant/Services/FinalRealBenchmarkEliminationService.cs diff --git a/pipeline-import/MagicQuant/Services/FinalReleaseMetadataService.cs b/src/MagicQuant/Services/FinalReleaseMetadataService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/FinalReleaseMetadataService.cs rename to src/MagicQuant/Services/FinalReleaseMetadataService.cs diff --git a/pipeline-import/MagicQuant/Services/FinalSurvivorSelectionCliService.cs b/src/MagicQuant/Services/FinalSurvivorSelectionCliService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/FinalSurvivorSelectionCliService.cs rename to src/MagicQuant/Services/FinalSurvivorSelectionCliService.cs diff --git a/pipeline-import/MagicQuant/Services/GgufMetadataReader.cs b/src/MagicQuant/Services/GgufMetadataReader.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/GgufMetadataReader.cs rename to src/MagicQuant/Services/GgufMetadataReader.cs diff --git a/pipeline-import/MagicQuant/Services/HuggingFaceBaselineService.cs b/src/MagicQuant/Services/HuggingFaceBaselineService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/HuggingFaceBaselineService.cs rename to src/MagicQuant/Services/HuggingFaceBaselineService.cs diff --git a/pipeline-import/MagicQuant/Services/HybridArtifactExportService.cs b/src/MagicQuant/Services/HybridArtifactExportService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/HybridArtifactExportService.cs rename to src/MagicQuant/Services/HybridArtifactExportService.cs diff --git a/pipeline-import/MagicQuant/Services/HybridBenchmarkRepository.cs b/src/MagicQuant/Services/HybridBenchmarkRepository.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/HybridBenchmarkRepository.cs rename to src/MagicQuant/Services/HybridBenchmarkRepository.cs diff --git a/pipeline-import/MagicQuant/Services/HybridMapGenerationService.cs b/src/MagicQuant/Services/HybridMapGenerationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/HybridMapGenerationService.cs rename to src/MagicQuant/Services/HybridMapGenerationService.cs diff --git a/pipeline-import/MagicQuant/Services/ImatrixIdentityService.cs b/src/MagicQuant/Services/ImatrixIdentityService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ImatrixIdentityService.cs rename to src/MagicQuant/Services/ImatrixIdentityService.cs diff --git a/pipeline-import/MagicQuant/Services/ImatrixService.cs b/src/MagicQuant/Services/ImatrixService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ImatrixService.cs rename to src/MagicQuant/Services/ImatrixService.cs diff --git a/pipeline-import/MagicQuant/Services/IsolationDiagnosticsManifestService.cs b/src/MagicQuant/Services/IsolationDiagnosticsManifestService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/IsolationDiagnosticsManifestService.cs rename to src/MagicQuant/Services/IsolationDiagnosticsManifestService.cs diff --git a/pipeline-import/MagicQuant/Services/IsolationOptimizationService.cs b/src/MagicQuant/Services/IsolationOptimizationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/IsolationOptimizationService.cs rename to src/MagicQuant/Services/IsolationOptimizationService.cs diff --git a/pipeline-import/MagicQuant/Services/IsolationPlanningService.cs b/src/MagicQuant/Services/IsolationPlanningService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/IsolationPlanningService.cs rename to src/MagicQuant/Services/IsolationPlanningService.cs diff --git a/pipeline-import/MagicQuant/Services/LearnedBaselinePruningService.cs b/src/MagicQuant/Services/LearnedBaselinePruningService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/LearnedBaselinePruningService.cs rename to src/MagicQuant/Services/LearnedBaselinePruningService.cs diff --git a/pipeline-import/MagicQuant/Services/Learning/TensorGroupingAuditService.cs b/src/MagicQuant/Services/Learning/TensorGroupingAuditService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/Learning/TensorGroupingAuditService.cs rename to src/MagicQuant/Services/Learning/TensorGroupingAuditService.cs diff --git a/pipeline-import/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs b/src/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs rename to src/MagicQuant/Services/Learning/TensorLearningDiagnosticWriter.cs diff --git a/pipeline-import/MagicQuant/Services/LlamaGpuArgumentBuilder.cs b/src/MagicQuant/Services/LlamaGpuArgumentBuilder.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/LlamaGpuArgumentBuilder.cs rename to src/MagicQuant/Services/LlamaGpuArgumentBuilder.cs diff --git a/pipeline-import/MagicQuant/Services/MagicQuantManifestPathService.cs b/src/MagicQuant/Services/MagicQuantManifestPathService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/MagicQuantManifestPathService.cs rename to src/MagicQuant/Services/MagicQuantManifestPathService.cs diff --git a/pipeline-import/MagicQuant/Services/ModelArtifactPathService.cs b/src/MagicQuant/Services/ModelArtifactPathService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ModelArtifactPathService.cs rename to src/MagicQuant/Services/ModelArtifactPathService.cs diff --git a/pipeline-import/MagicQuant/Services/ModelCompatibilityService.cs b/src/MagicQuant/Services/ModelCompatibilityService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ModelCompatibilityService.cs rename to src/MagicQuant/Services/ModelCompatibilityService.cs diff --git a/pipeline-import/MagicQuant/Services/ModelRuntimePathService.cs b/src/MagicQuant/Services/ModelRuntimePathService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ModelRuntimePathService.cs rename to src/MagicQuant/Services/ModelRuntimePathService.cs diff --git a/pipeline-import/MagicQuant/Services/ModelSidecarArtifactService.cs b/src/MagicQuant/Services/ModelSidecarArtifactService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ModelSidecarArtifactService.cs rename to src/MagicQuant/Services/ModelSidecarArtifactService.cs diff --git a/pipeline-import/MagicQuant/Services/NativeModelConversionService.cs b/src/MagicQuant/Services/NativeModelConversionService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/NativeModelConversionService.cs rename to src/MagicQuant/Services/NativeModelConversionService.cs diff --git a/pipeline-import/MagicQuant/Services/OutputPathService.cs b/src/MagicQuant/Services/OutputPathService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/OutputPathService.cs rename to src/MagicQuant/Services/OutputPathService.cs diff --git a/pipeline-import/MagicQuant/Services/PathSafety.cs b/src/MagicQuant/Services/PathSafety.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/PathSafety.cs rename to src/MagicQuant/Services/PathSafety.cs diff --git a/pipeline-import/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs b/src/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs rename to src/MagicQuant/Services/PredictionGuidedHybridSelectionService.cs diff --git a/pipeline-import/MagicQuant/Services/PredictionValidationService.cs b/src/MagicQuant/Services/PredictionValidationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/PredictionValidationService.cs rename to src/MagicQuant/Services/PredictionValidationService.cs diff --git a/pipeline-import/MagicQuant/Services/Progress/StageProgressOptions.cs b/src/MagicQuant/Services/Progress/StageProgressOptions.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/Progress/StageProgressOptions.cs rename to src/MagicQuant/Services/Progress/StageProgressOptions.cs diff --git a/pipeline-import/MagicQuant/Services/Progress/StageProgressSnapshot.cs b/src/MagicQuant/Services/Progress/StageProgressSnapshot.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/Progress/StageProgressSnapshot.cs rename to src/MagicQuant/Services/Progress/StageProgressSnapshot.cs diff --git a/pipeline-import/MagicQuant/Services/Progress/StageProgressTracker.cs b/src/MagicQuant/Services/Progress/StageProgressTracker.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/Progress/StageProgressTracker.cs rename to src/MagicQuant/Services/Progress/StageProgressTracker.cs diff --git a/pipeline-import/MagicQuant/Services/QuantDatabaseService.cs b/src/MagicQuant/Services/QuantDatabaseService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/QuantDatabaseService.cs rename to src/MagicQuant/Services/QuantDatabaseService.cs diff --git a/pipeline-import/MagicQuant/Services/QuantFidelityComparerService.cs b/src/MagicQuant/Services/QuantFidelityComparerService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/QuantFidelityComparerService.cs rename to src/MagicQuant/Services/QuantFidelityComparerService.cs diff --git a/pipeline-import/MagicQuant/Services/QuantizationConcurrencyPlan.cs b/src/MagicQuant/Services/QuantizationConcurrencyPlan.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/QuantizationConcurrencyPlan.cs rename to src/MagicQuant/Services/QuantizationConcurrencyPlan.cs diff --git a/pipeline-import/MagicQuant/Services/QuantizationService.cs b/src/MagicQuant/Services/QuantizationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/QuantizationService.cs rename to src/MagicQuant/Services/QuantizationService.cs diff --git a/pipeline-import/MagicQuant/Services/RankSafeKldPredictionService.cs b/src/MagicQuant/Services/RankSafeKldPredictionService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/RankSafeKldPredictionService.cs rename to src/MagicQuant/Services/RankSafeKldPredictionService.cs diff --git a/pipeline-import/MagicQuant/Services/ReadmeGenerationService.cs b/src/MagicQuant/Services/ReadmeGenerationService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ReadmeGenerationService.cs rename to src/MagicQuant/Services/ReadmeGenerationService.cs diff --git a/pipeline-import/MagicQuant/Services/RemainingCombinationStore.cs b/src/MagicQuant/Services/RemainingCombinationStore.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/RemainingCombinationStore.cs rename to src/MagicQuant/Services/RemainingCombinationStore.cs diff --git a/pipeline-import/MagicQuant/Services/RepositoryCloneManifestService.cs b/src/MagicQuant/Services/RepositoryCloneManifestService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/RepositoryCloneManifestService.cs rename to src/MagicQuant/Services/RepositoryCloneManifestService.cs diff --git a/pipeline-import/MagicQuant/Services/RunProvenanceService.cs b/src/MagicQuant/Services/RunProvenanceService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/RunProvenanceService.cs rename to src/MagicQuant/Services/RunProvenanceService.cs diff --git a/pipeline-import/MagicQuant/Services/ScratchStorageService.cs b/src/MagicQuant/Services/ScratchStorageService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/ScratchStorageService.cs rename to src/MagicQuant/Services/ScratchStorageService.cs diff --git a/pipeline-import/MagicQuant/Services/SelectionDiagnosticsLogService.cs b/src/MagicQuant/Services/SelectionDiagnosticsLogService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/SelectionDiagnosticsLogService.cs rename to src/MagicQuant/Services/SelectionDiagnosticsLogService.cs diff --git a/pipeline-import/MagicQuant/Services/SmartBaselineTuningFallbackService.cs b/src/MagicQuant/Services/SmartBaselineTuningFallbackService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/SmartBaselineTuningFallbackService.cs rename to src/MagicQuant/Services/SmartBaselineTuningFallbackService.cs diff --git a/pipeline-import/MagicQuant/Services/TargetedRelearnService.cs b/src/MagicQuant/Services/TargetedRelearnService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/TargetedRelearnService.cs rename to src/MagicQuant/Services/TargetedRelearnService.cs diff --git a/pipeline-import/MagicQuant/Services/TensorGroupProfileService.cs b/src/MagicQuant/Services/TensorGroupProfileService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/TensorGroupProfileService.cs rename to src/MagicQuant/Services/TensorGroupProfileService.cs diff --git a/pipeline-import/MagicQuant/Services/TensorGroupRebucketService.cs b/src/MagicQuant/Services/TensorGroupRebucketService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/TensorGroupRebucketService.cs rename to src/MagicQuant/Services/TensorGroupRebucketService.cs diff --git a/pipeline-import/MagicQuant/Services/TensorGroupReviewService.cs b/src/MagicQuant/Services/TensorGroupReviewService.cs similarity index 100% rename from pipeline-import/MagicQuant/Services/TensorGroupReviewService.cs rename to src/MagicQuant/Services/TensorGroupReviewService.cs diff --git a/pipeline-import/MagicQuant/config.default.yaml b/src/MagicQuant/config.default.yaml similarity index 100% rename from pipeline-import/MagicQuant/config.default.yaml rename to src/MagicQuant/config.default.yaml diff --git a/pipeline-import/MagicQuant/packages.lock.json b/src/MagicQuant/packages.lock.json similarity index 100% rename from pipeline-import/MagicQuant/packages.lock.json rename to src/MagicQuant/packages.lock.json diff --git a/pipeline-import/MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj b/tests/MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj similarity index 100% rename from pipeline-import/MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj rename to tests/MagicQuant.ProcessFixture/MagicQuant.ProcessFixture.csproj diff --git a/pipeline-import/MagicQuant.ProcessFixture/Program.cs b/tests/MagicQuant.ProcessFixture/Program.cs similarity index 100% rename from pipeline-import/MagicQuant.ProcessFixture/Program.cs rename to tests/MagicQuant.ProcessFixture/Program.cs diff --git a/pipeline-import/MagicQuant.ProcessFixture/packages.lock.json b/tests/MagicQuant.ProcessFixture/packages.lock.json similarity index 100% rename from pipeline-import/MagicQuant.ProcessFixture/packages.lock.json rename to tests/MagicQuant.ProcessFixture/packages.lock.json diff --git a/pipeline-import/MagicQuant.Tests/AnomalyContextScopeTests.cs b/tests/MagicQuant.Tests/AnomalyContextScopeTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/AnomalyContextScopeTests.cs rename to tests/MagicQuant.Tests/AnomalyContextScopeTests.cs diff --git a/pipeline-import/MagicQuant.Tests/AssemblyInfo.cs b/tests/MagicQuant.Tests/AssemblyInfo.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/AssemblyInfo.cs rename to tests/MagicQuant.Tests/AssemblyInfo.cs diff --git a/pipeline-import/MagicQuant.Tests/AuthorityUsageRegressionTests.cs b/tests/MagicQuant.Tests/AuthorityUsageRegressionTests.cs similarity index 58% rename from pipeline-import/MagicQuant.Tests/AuthorityUsageRegressionTests.cs rename to tests/MagicQuant.Tests/AuthorityUsageRegressionTests.cs index fdda4c7..56d4360 100644 --- a/pipeline-import/MagicQuant.Tests/AuthorityUsageRegressionTests.cs +++ b/tests/MagicQuant.Tests/AuthorityUsageRegressionTests.cs @@ -7,12 +7,12 @@ public class AuthorityUsageRegressionTests [Fact] public void ComboGenerationPaths_DoNotUseLegacyAllAllowedHybridQuantsAuthority() { - string repositoryRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../..")); + string repositoryRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../..")); var files = new[] { - Path.Combine(repositoryRoot, "MagicQuant", "Helpers", "ComboLogic.cs"), - Path.Combine(repositoryRoot, "MagicQuant", "Helpers", "TensorConfigGenerator.cs"), - Path.Combine(repositoryRoot, "MagicQuant", "Services", "IsolationOptimizationService.cs") + Path.Combine(repositoryRoot, "src", "MagicQuant", "Helpers", "ComboLogic.cs"), + Path.Combine(repositoryRoot, "src", "MagicQuant", "Helpers", "TensorConfigGenerator.cs"), + Path.Combine(repositoryRoot, "src", "MagicQuant", "Services", "IsolationOptimizationService.cs") }; foreach (var file in files) diff --git a/pipeline-import/MagicQuant.Tests/BaselineCandidatePolicyTests.cs b/tests/MagicQuant.Tests/BaselineCandidatePolicyTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/BaselineCandidatePolicyTests.cs rename to tests/MagicQuant.Tests/BaselineCandidatePolicyTests.cs diff --git a/pipeline-import/MagicQuant.Tests/BenchmarkContractTests.cs b/tests/MagicQuant.Tests/BenchmarkContractTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/BenchmarkContractTests.cs rename to tests/MagicQuant.Tests/BenchmarkContractTests.cs diff --git a/pipeline-import/MagicQuant.Tests/BenchmarkCorpusTests.cs b/tests/MagicQuant.Tests/BenchmarkCorpusTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/BenchmarkCorpusTests.cs rename to tests/MagicQuant.Tests/BenchmarkCorpusTests.cs diff --git a/pipeline-import/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs b/tests/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs rename to tests/MagicQuant.Tests/BenchmarkGpuPlanningTests.cs diff --git a/pipeline-import/MagicQuant.Tests/CliArgumentParsingTests.cs b/tests/MagicQuant.Tests/CliArgumentParsingTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/CliArgumentParsingTests.cs rename to tests/MagicQuant.Tests/CliArgumentParsingTests.cs diff --git a/pipeline-import/MagicQuant.Tests/CliOptionValidationTests.cs b/tests/MagicQuant.Tests/CliOptionValidationTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/CliOptionValidationTests.cs rename to tests/MagicQuant.Tests/CliOptionValidationTests.cs diff --git a/pipeline-import/MagicQuant.Tests/CliStartupTests.cs b/tests/MagicQuant.Tests/CliStartupTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/CliStartupTests.cs rename to tests/MagicQuant.Tests/CliStartupTests.cs diff --git a/pipeline-import/MagicQuant.Tests/CombinationDatabasePathTests.cs b/tests/MagicQuant.Tests/CombinationDatabasePathTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/CombinationDatabasePathTests.cs rename to tests/MagicQuant.Tests/CombinationDatabasePathTests.cs diff --git a/pipeline-import/MagicQuant.Tests/ConfigurationContractTests.cs b/tests/MagicQuant.Tests/ConfigurationContractTests.cs similarity index 95% rename from pipeline-import/MagicQuant.Tests/ConfigurationContractTests.cs rename to tests/MagicQuant.Tests/ConfigurationContractTests.cs index eb12785..88fa5cc 100644 --- a/pipeline-import/MagicQuant.Tests/ConfigurationContractTests.cs +++ b/tests/MagicQuant.Tests/ConfigurationContractTests.cs @@ -22,12 +22,12 @@ public void Explicit_config_path_is_relative_to_working_directory() } [Theory] - [InlineData("MagicQuant/config.default.yaml")] + [InlineData("src/MagicQuant/config.default.yaml")] [InlineData("examples/pipeline.yaml")] [InlineData("examples/clone.yaml")] public void Distributed_configs_have_no_unknown_keys(string relativePath) { - string root = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../..")); + string root = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../..")); var config = new DeserializerBuilder() .WithNamingConvention(UnderscoredNamingConvention.Instance) .Build() diff --git a/pipeline-import/MagicQuant.Tests/ConfigurationReadTests.cs b/tests/MagicQuant.Tests/ConfigurationReadTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/ConfigurationReadTests.cs rename to tests/MagicQuant.Tests/ConfigurationReadTests.cs diff --git a/pipeline-import/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs b/tests/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs rename to tests/MagicQuant.Tests/ExternalBaselineCacheCleanupServiceTests.cs diff --git a/pipeline-import/MagicQuant.Tests/ExternalBaselineTensorParityTests.cs b/tests/MagicQuant.Tests/ExternalBaselineTensorParityTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/ExternalBaselineTensorParityTests.cs rename to tests/MagicQuant.Tests/ExternalBaselineTensorParityTests.cs diff --git a/pipeline-import/MagicQuant.Tests/HardwareInitializationTests.cs b/tests/MagicQuant.Tests/HardwareInitializationTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/HardwareInitializationTests.cs rename to tests/MagicQuant.Tests/HardwareInitializationTests.cs diff --git a/pipeline-import/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs b/tests/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs rename to tests/MagicQuant.Tests/HuggingFaceBaselineCacheTests.cs diff --git a/pipeline-import/MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs b/tests/MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs rename to tests/MagicQuant.Tests/HuggingFaceRevisionConfigTests.cs diff --git a/pipeline-import/MagicQuant.Tests/ImatrixIdentityServiceTests.cs b/tests/MagicQuant.Tests/ImatrixIdentityServiceTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/ImatrixIdentityServiceTests.cs rename to tests/MagicQuant.Tests/ImatrixIdentityServiceTests.cs diff --git a/pipeline-import/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs b/tests/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs rename to tests/MagicQuant.Tests/LearnedBaselinePruningServiceTests.cs diff --git a/pipeline-import/MagicQuant.Tests/LlamaBinaryPathTests.cs b/tests/MagicQuant.Tests/LlamaBinaryPathTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/LlamaBinaryPathTests.cs rename to tests/MagicQuant.Tests/LlamaBinaryPathTests.cs diff --git a/pipeline-import/MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs b/tests/MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs rename to tests/MagicQuant.Tests/LlamaGpuArgumentBuilderTests.cs diff --git a/pipeline-import/MagicQuant.Tests/MagicQuant.Tests.csproj b/tests/MagicQuant.Tests/MagicQuant.Tests.csproj similarity index 89% rename from pipeline-import/MagicQuant.Tests/MagicQuant.Tests.csproj rename to tests/MagicQuant.Tests/MagicQuant.Tests.csproj index 5e66388..3ae77ff 100644 --- a/pipeline-import/MagicQuant.Tests/MagicQuant.Tests.csproj +++ b/tests/MagicQuant.Tests/MagicQuant.Tests.csproj @@ -13,7 +13,7 @@ - + diff --git a/pipeline-import/MagicQuant.Tests/ModelSmokeTests.cs b/tests/MagicQuant.Tests/ModelSmokeTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/ModelSmokeTests.cs rename to tests/MagicQuant.Tests/ModelSmokeTests.cs diff --git a/pipeline-import/MagicQuant.Tests/NativeConversionTests.cs b/tests/MagicQuant.Tests/NativeConversionTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/NativeConversionTests.cs rename to tests/MagicQuant.Tests/NativeConversionTests.cs diff --git a/pipeline-import/MagicQuant.Tests/OutputPathTests.cs b/tests/MagicQuant.Tests/OutputPathTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/OutputPathTests.cs rename to tests/MagicQuant.Tests/OutputPathTests.cs diff --git a/pipeline-import/MagicQuant.Tests/PreflightTests.cs b/tests/MagicQuant.Tests/PreflightTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/PreflightTests.cs rename to tests/MagicQuant.Tests/PreflightTests.cs diff --git a/pipeline-import/MagicQuant.Tests/ProcessRunnerTests.cs b/tests/MagicQuant.Tests/ProcessRunnerTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/ProcessRunnerTests.cs rename to tests/MagicQuant.Tests/ProcessRunnerTests.cs diff --git a/pipeline-import/MagicQuant.Tests/QuantizationConcurrencyTests.cs b/tests/MagicQuant.Tests/QuantizationConcurrencyTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/QuantizationConcurrencyTests.cs rename to tests/MagicQuant.Tests/QuantizationConcurrencyTests.cs diff --git a/pipeline-import/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs b/tests/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs rename to tests/MagicQuant.Tests/QuantizationRunAndBuildHybridsRegressionTests.cs diff --git a/pipeline-import/MagicQuant.Tests/RunProvenanceTests.cs b/tests/MagicQuant.Tests/RunProvenanceTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/RunProvenanceTests.cs rename to tests/MagicQuant.Tests/RunProvenanceTests.cs diff --git a/pipeline-import/MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs b/tests/MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs rename to tests/MagicQuant.Tests/RuntimeSearchSpaceObsoleteContractTests.cs diff --git a/pipeline-import/MagicQuant.Tests/ScratchStorageServiceTests.cs b/tests/MagicQuant.Tests/ScratchStorageServiceTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/ScratchStorageServiceTests.cs rename to tests/MagicQuant.Tests/ScratchStorageServiceTests.cs diff --git a/pipeline-import/MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs b/tests/MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs rename to tests/MagicQuant.Tests/SmartBaselineTuningFallbackTests.cs diff --git a/pipeline-import/MagicQuant.Tests/SynergyTransferConfigTests.cs b/tests/MagicQuant.Tests/SynergyTransferConfigTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/SynergyTransferConfigTests.cs rename to tests/MagicQuant.Tests/SynergyTransferConfigTests.cs diff --git a/pipeline-import/MagicQuant.Tests/SynergyTransferPlanningTests.cs b/tests/MagicQuant.Tests/SynergyTransferPlanningTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/SynergyTransferPlanningTests.cs rename to tests/MagicQuant.Tests/SynergyTransferPlanningTests.cs diff --git a/pipeline-import/MagicQuant.Tests/YamlDiagnosticsTests.cs b/tests/MagicQuant.Tests/YamlDiagnosticsTests.cs similarity index 100% rename from pipeline-import/MagicQuant.Tests/YamlDiagnosticsTests.cs rename to tests/MagicQuant.Tests/YamlDiagnosticsTests.cs diff --git a/pipeline-import/MagicQuant.Tests/packages.lock.json b/tests/MagicQuant.Tests/packages.lock.json similarity index 100% rename from pipeline-import/MagicQuant.Tests/packages.lock.json rename to tests/MagicQuant.Tests/packages.lock.json From 3e328e3c082491a8ee573bb1308332de20861486 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 20:12:13 -0400 Subject: [PATCH 254/258] Package the AGPL CLI with launch documentation, funding and trusted NuGet releases --- .github/FUNDING.yml | 2 - .github/workflows/dotnet.yml | 18 + .github/workflows/publish-nuget.yml | 84 + .gitignore | 44 + .gitleaks.toml | 10 + CONTRIBUTING.md | 8 +- LICENSE | 661 ++++++++ README.md | 257 +-- THIRD-PARTY-NOTICES.md | 43 + assets/icon.png | Bin 0 -> 1475 bytes docs/architecture.md | 6 +- docs/best-practices.md | 58 + docs/commands.md | 14 +- docs/configuration.md | 4 +- docs/index.md | 14 + docs/nuget-readme.md | 34 + docs/pipeline-migration-readme.md | 86 - docs/releases.md | 43 + docs/setup.md | 37 +- docs/testing.md | 20 +- examples/pipeline.yaml | 2 +- licenses/Blake3-license.txt | 29 + ...uckDB.NET.Bindings.Full-LICENSE-DuckDB.txt | 7 + licenses/DuckDB.NET.Bindings.Full-LICENSE.md | 21 + licenses/LibGit2Sharp-LICENSE.md | 21 + ...t2Sharp.NativeBinaries-libgit2.license.txt | 1410 ++++++++++++++++ ...ching.Abstractions-THIRD-PARTY-NOTICES.TXT | 1418 +++++++++++++++++ licenses/SQLitePCLRaw-LICENSE.TXT | 202 +++ licenses/Spectre.Console-LICENSE.md | 21 + licenses/YamlDotNet-LICENSE.txt | 19 + licenses/dotnet-LICENSE.TXT | 23 + release-version.txt | 1 + scripts/package_smoke.py | 80 + scripts/release_version.py | 84 + scripts/scan_secrets.py | 34 + scripts/test_doc_links.py | 32 + scripts/test_release_version.py | 60 + src/MagicQuant/Commands/CommandCatalog.cs | 1 + src/MagicQuant/Commands/InitConfig.cs | 26 + src/MagicQuant/Helpers/CliHelpers.cs | 2 +- src/MagicQuant/MagicQuant.csproj | 25 +- src/MagicQuant/Program.cs | 13 + .../Services/ReadmeGenerationService.cs | 2 +- tests/MagicQuant.Tests/CliStartupTests.cs | 19 + .../ConfigurationContractTests.cs | 20 + .../MagicQuant.Tests/ReadmeGenerationTests.cs | 29 + wiki/index.md | 2 + wiki/overview.md | 222 +++ 48 files changed, 4972 insertions(+), 296 deletions(-) create mode 100644 .github/workflows/publish-nuget.yml create mode 100644 .gitleaks.toml create mode 100644 LICENSE create mode 100644 THIRD-PARTY-NOTICES.md create mode 100644 assets/icon.png create mode 100644 docs/best-practices.md create mode 100644 docs/index.md create mode 100644 docs/nuget-readme.md delete mode 100644 docs/pipeline-migration-readme.md create mode 100644 docs/releases.md create mode 100644 licenses/Blake3-license.txt create mode 100644 licenses/DuckDB.NET.Bindings.Full-LICENSE-DuckDB.txt create mode 100644 licenses/DuckDB.NET.Bindings.Full-LICENSE.md create mode 100644 licenses/LibGit2Sharp-LICENSE.md create mode 100644 licenses/LibGit2Sharp.NativeBinaries-libgit2.license.txt create mode 100644 licenses/Microsoft.Extensions.Caching.Abstractions-THIRD-PARTY-NOTICES.TXT create mode 100644 licenses/SQLitePCLRaw-LICENSE.TXT create mode 100644 licenses/Spectre.Console-LICENSE.md create mode 100644 licenses/YamlDotNet-LICENSE.txt create mode 100644 licenses/dotnet-LICENSE.TXT create mode 100644 release-version.txt create mode 100644 scripts/package_smoke.py create mode 100644 scripts/release_version.py create mode 100644 scripts/scan_secrets.py create mode 100644 scripts/test_doc_links.py create mode 100644 scripts/test_release_version.py create mode 100644 src/MagicQuant/Commands/InitConfig.cs create mode 100644 tests/MagicQuant.Tests/ReadmeGenerationTests.cs create mode 100644 wiki/overview.md diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index ef0b61a..13152d5 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,4 +1,2 @@ -# These are supported funding model platforms - github: [magiccodingman] custom: ['https://sayou.biz/support', 'https://paypal.me/lancewr'] diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 4f5af7a..065449b 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -19,11 +19,29 @@ jobs: - uses: actions/setup-dotnet@v4 with: dotnet-version: '10.0.x' + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: python -m unittest discover -s scripts -p "test_*.py" - run: dotnet restore MagicQuant.sln --locked-mode -warnaserror - run: dotnet build MagicQuant.sln --configuration ${{ matrix.configuration }} --no-restore -warnaserror - run: dotnet test MagicQuant.sln --configuration ${{ matrix.configuration }} --no-build --logger trx --results-directory TestResults + - run: dotnet pack src/MagicQuant --configuration ${{ matrix.configuration }} --no-restore -p:Version=0.0.0-ci -o artifacts -warnaserror + - run: python scripts/package_smoke.py artifacts/MagicQuant.0.0.0-ci.nupkg - uses: actions/upload-artifact@v4 if: always() with: name: test-results-${{ matrix.os }}-${{ matrix.configuration }} path: TestResults/*.trx + secrets: + name: Secret scan + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: python scripts/scan_secrets.py diff --git a/.github/workflows/publish-nuget.yml b/.github/workflows/publish-nuget.yml new file mode 100644 index 0000000..8317b0c --- /dev/null +++ b/.github/workflows/publish-nuget.yml @@ -0,0 +1,84 @@ +name: Publish NuGet +on: + push: + branches: [release] + workflow_dispatch: +permissions: + contents: read +jobs: + validate: + if: github.ref == 'refs/heads/release' + runs-on: ${{ matrix.os }} + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: '10.0.x' + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: dotnet restore MagicQuant.sln --locked-mode -warnaserror + - run: dotnet build MagicQuant.sln -c Release --no-restore -warnaserror + - run: dotnet test MagicQuant.sln -c Release --no-build + - run: python -m unittest discover -s scripts -p "test_*.py" + - run: dotnet pack src/MagicQuant -c Release --no-restore -p:Version=0.0.0-ci -o artifacts -warnaserror + - run: python scripts/package_smoke.py artifacts/MagicQuant.0.0.0-ci.nupkg + publish: + needs: validate + runs-on: ubuntu-latest + timeout-minutes: 20 + environment: release + permissions: + contents: write + id-token: write + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: '10.0.x' + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Reserve or reuse the version for this commit + id: version + run: | + git config user.name 'github-actions[bot]' + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' + python scripts/release_version.py --reserve + - run: dotnet restore MagicQuant.sln --locked-mode -warnaserror + - name: Pack exact release version + env: + RELEASE_VERSION: ${{ steps.version.outputs.version }} + run: dotnet pack src/MagicQuant -c Release --no-restore -p:Version="$RELEASE_VERSION" -p:ContinuousIntegrationBuild=true -o artifacts -warnaserror + - name: Validate the release package + env: + RELEASE_VERSION: ${{ steps.version.outputs.version }} + run: python scripts/package_smoke.py "artifacts/MagicQuant.$RELEASE_VERSION.nupkg" + - uses: actions/upload-artifact@v4 + with: + name: nuget-${{ steps.version.outputs.version }} + path: artifacts/*.nupkg + - name: NuGet login through trusted publishing + uses: NuGet/login@v1 + id: login + with: + user: ${{ secrets.NUGET_USER }} + - name: Publish tested package + env: + NUGET_API_KEY: ${{ steps.login.outputs.NUGET_API_KEY }} + run: dotnet nuget push artifacts/*.nupkg --source https://api.nuget.org/v3/index.json --api-key "$NUGET_API_KEY" --skip-duplicate + - name: Create release record + env: + GH_TOKEN: ${{ github.token }} + RELEASE_VERSION: ${{ steps.version.outputs.version }} + run: | + if ! gh release view "v$RELEASE_VERSION" >/dev/null 2>&1; then + gh release create "v$RELEASE_VERSION" --verify-tag --title "MagicQuant $RELEASE_VERSION" --generate-notes + fi diff --git a/.gitignore b/.gitignore index 8d53c5f..df057d6 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,47 @@ config.local.yaml .MagicQuant_tmp/ TestResults/ artifacts/ + +__pycache__/ + +# Visual Studio / Rider / VS Code per-user and machine state +.vs/ +.idea/ +.vscode/* +!.vscode/extensions.json +!.vscode/settings.example.json +*.DotSettings.user +*.sln.DotSettings.user +*.slnx.user +*.rsuser +*.sln.docstates +_ReSharper*/ +*.ncrunch* +_NCrunch*/ +[Bb]enchmark[Dd]ot[Nn]et.[Aa]rtifacts/ +[Tt]est[Rr]esults/ +coverage/ +*.coverage +*.coveragexml +*.testlog + +# Local secrets, package output and OS/editor temporary files +.env +.env.* +!.env.example +*.pfx +*.p12 +*.key +*.pem +*.nupkg +*.snupkg +.DS_Store +Thumbs.db +Desktop.ini +*~ +*.swp +*.swo + +# Local campaign configuration and tool installs +config.yaml +.tool-install/ diff --git a/.gitleaks.toml b/.gitleaks.toml new file mode 100644 index 0000000..dcbd30a --- /dev/null +++ b/.gitleaks.toml @@ -0,0 +1,10 @@ +# Retain every default detector. This exact tensor fixture is not an API key. +[extend] +useDefault = true + +[[allowlists]] +description = "Exact native/external tensor-name fixture, including its historical path" +condition = "AND" +paths = ['''(^|/)MagicQuant.Tests/ExternalBaselineTensorParityTests\.cs$'''] +regexTarget = "line" +regexes = ['''^\s*var (native|external) = Metadata\(nextnLayers: 1, "token_embd\.weight", "blk\.0\.attn_q\.weight", "blk\.64\.nextn\.eh_proj\.weight"\);$'''] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1ef0ae5..08172ab 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ # Contributing -Start with the [architecture map](docs/architecture.md), [configuration rules](docs/configuration.md), and the [research wiki](https://github.com/magiccodingman/MagicQuant-Wiki). This repository implements benchmark-driven discovery; the `evolution` name survives as a compatibility alias. +Start with the [architecture map](docs/architecture.md), [configuration rules](docs/configuration.md), and the [research wiki](https://github.com/magiccodingman/MagicQuant). This repository implements benchmark-driven discovery; the `evolution` name survives as a compatibility alias. ## Local workflow @@ -42,3 +42,9 @@ The maintainer still needs to choose a software license before an open-source re See [testing and merge checks](docs/testing.md) for the manual small-model workflow, package lock updates, and required-check setup. [Worked examples](docs/extending.md) show how to add configuration and test native/process/path changes. + +## Repository layout and release safety + +Application projects are under `src/`; test projects are under `tests/`. Program guides belong in `docs/`; research explanations belong in `wiki/`. Use relative links so documentation remains useful in checkouts and forks. Source paths are separate from model/runtime data paths. + +Run the [installed-package checks](docs/testing.md#installed-package-and-release-checks) when changing paths, packaging, startup, or bundled files. Publication is controlled by [release branch automation](docs/releases.md). Never put NuGet keys or personal configuration into a workflow. Original contributions are accepted under the repository's AGPL-3.0-only license; retain third-party attribution. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..be3f7b2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/README.md b/README.md index 0954c99..214e216 100644 --- a/README.md +++ b/README.md @@ -1,222 +1,103 @@ -# MagicQuant (v2.0) +# MagicQuant -**MagicQuant is a benchmark-driven GGUF evaluation and hybrid-discovery system.** - -> **Which quantized models are actually worth using at each size?** - -Most quant releases give you a pile of files, AKA: Q8, Q6, Q5, Q4, and leave you to guess. MagicQuant replaces that guesswork with benchmarks, tensor-group probing, mixed hybrid GGUF builds when they are worth it, and a final survivor list built around meaningful size/fidelity tradeoffs. +[![NuGet version](https://img.shields.io/nuget/v/MagicQuant.svg)](https://www.nuget.org/packages/MagicQuant/) +[![NuGet downloads](https://img.shields.io/nuget/dt/MagicQuant.svg)](https://www.nuget.org/packages/MagicQuant/) +[![Build and tests](https://github.com/magiccodingman/MagicQuant/actions/workflows/dotnet.yml/badge.svg)](https://github.com/magiccodingman/MagicQuant/actions/workflows/dotnet.yml) +[![License: AGPL v3](https://img.shields.io/badge/License-AGPL_v3-blue.svg)](LICENSE) ---- +**Benchmark-driven GGUF quantization and mixed-precision hybrid discovery for llama.cpp.** -## What MagicQuant Does +MagicQuant helps answer: **which quantized versions of a model are worth keeping at each size?** It measures baseline quantizations, learns tensor-group assignments, explores hybrid combinations, and validates candidates against size and fidelity criteria. The result is a selected set of GGUF artifacts with supporting measurements, rather than an unranked collection of quantization levels. -MagicQuant takes the messy quantization space and turns it into a judged survivor list. - -It tests standard baselines, learns from external quant strategies, and builds mixed tensor-group hybrids when there may be a better size/fidelity trade hiding between normal quant levels. - -Then it validates the results. - -MagicQuant does not assume hybrids are better. It does not assume baselines are safe. Every option has to earn its slot. - -A final MagicQuant release is meant to show: - -* what is smallest -* what is safest -* what is meaningfully in-between -* what was removed as redundant or not worth the damage -* and what the real benchmark numbers say - -If a model survives MagicQuant, it survived because the trade was worth showing. +It is a .NET command-line application that orchestrates llama.cpp and Python tooling. It does not invent a new quantization format or use evolutionary search. Hybrids must earn their place: a standard baseline can be the better result. ---- +## How it works -## Example +1. **Establish baselines.** Read a local source model and measure standard quantization choices. Optionally learn tensor assignments from compatible external GGUFs. +2. **Probe tensor groups.** Measure how changes to groups such as attention, embeddings, and feed-forward tensors affect the model. +3. **Discover hybrids.** Use measured evidence and predictions to explore mixed-precision combinations with promising size/fidelity tradeoffs. +4. **Validate and select.** Measure candidates, reject poor or redundant trades, and export survivors with metadata and local provenance. -The following example is Qwen3-4B-2507-Instruct going through MagicQuants pipeline and the final results: +KLD and perplexity help evaluate fidelity; throughput and file size provide additional context. The results depend on the model, calibration/evaluation data, configuration, and hardware. A smaller KLD in one campaign is not a universal claim about downstream task quality. Read the [research overview](wiki/index.md) for the selection policy and its assumptions. -| Name | Provider | Quant Family | KLD | Size (GB) | -| ----------------------------------------------------------------------------------------- | ---------- | ------------ | -------: | --------: | -| LM-Q8_0 | llama.cpp | Q8_0 | 0.001339 | 3.99 | -| MQ-Q6_K_1 | MagicQuant | Q6_K | 0.001817 | 3.58 | -| UD-Q6_K_XL | Unsloth | UD-Q6_K_XL | 0.002111 | 3.41 | -| LM-Q6_K | llama.cpp | Q6_K | 0.004640 | 3.08 | -| [MQ-Q5_K_1](#winner-notes "Replaced: MQ-Q5_K") | MagicQuant | Q5_K | 0.006632 | 2.88 | -| [UD-Q5_K_XL](#winner-notes "Replaced: LM-Q5_K, LM-Q5_K_S") | Unsloth | UD-Q5_K_XL | 0.009839 | 2.73 | -| [MQ-Q4_K_M_1](#winner-notes "Replaced: MQ-Q4_K_M, UD-Q4_K_XL, LM-Q4_K_M + 1 more") | MagicQuant | Q4_K_M | 0.020346 | 2.44 | -| [LM-Q4_K_S](#winner-notes "Replaced: LM-IQ4_NL") | llama.cpp | Q4_K_S | 0.029803 | 2.22 | -| LM-IQ4_XS | llama.cpp | IQ4_XS | 0.031300 | 2.11 | -| UD-Q3_K_XL | Unsloth | UD-Q3_K_XL | 0.072278 | 1.98 | +## Support the project -The table above includes a mix of standard llama.cpp quantizations, Unsloth Dynamic GGUF models, and MagicQuant hybrids. +I build and maintain MagicQuant on the side, for free. Developing it and experimenting with quantizations has put a frankly ridiculous amount of terabytes written (TBW) on my drives! My Hugging Face storage is also creeping toward its cap, so there will eventually be more storage to fund. If this project helps you, [supporting the work](https://sayou.biz/support) helps with those costs. Anything helps and is always appreciated. ❤️ -In some cases, dominance is absolute. For example, Unsloth’s **Q5_K_XL** fully replaces the standard llama.cpp **Q5_K**, as MagicQuant determined the baseline offered no meaningful tradeoff in comparison. +## Install and run -More interesting are the hybrid outcomes. **MQ-Q4_K_M_1** emerged as a clear dominant variant, replacing multiple candidates simultaneously (_UD-Q4_K_XL, MQ-Q4_K_M, LM-Q4_K_M_). While baseline quants can sometimes achieve similar dominance, this case highlights a hybrid configuration that decisively outperformed across the board. +**Linux is the tested campaign platform.** Windows has automated build, unit-test, and packaged CLI checks; full Windows quantization campaigns have not been validated. No macOS campaign support is claimed. -**MQ-Q5_K_1** is another notable result. It leverages Unsloth’s learned tensor behavior (_Q5_K_XL_) within the `ffn_up_gate`, discovering a middle ground between **UD-Q5_K_XL** and **LM-Q6_K**. The result is a hybrid that achieves a disproportionately large KLD improvement relative to the additional size cost, exceeding a simple linear tradeoff. +Install the [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.0), then install the CLI from [NuGet](https://www.nuget.org/packages/MagicQuant/): -The table below breaks down these MagicQuant hybrids by tensor group, showing the assigned quantization for each, whether derived from llama.cpp baselines or Unsloth’s learned tensor mappings. - -| Name | embeddings | attn_q | attn_kv | attn_output | ffn_up_gate | ffn_down | -| ----------- | ---------- | ------ | ------- | ----------- | ----------- | -------- | -| MQ-Q6_K_1 | Q8_0 | Q8_0 | Q8_0 | Q8_0 | Q6_K | Q8_0 | -| MQ-Q5_K_1 | Q8_0 | Q5_K | Q8_0 | Q6_K | UD-Q5_K_XL | Q5_K_S | -| MQ-Q4_K_M_1 | Q8_0 | Q5_K | Q8_0 | Q6_K | IQ4_XS | IQ4_XS | - ---- - -## Nonlinear Wins - -MagicQuant does not look for simple "winners" in sub space between baselines. Instead it only allows nonlinear trade wins. Documentation presented later goes further into detail on this subject, but here's the TLDR: - -Imagine a graph like this: -``` -Size → -| -| Q6 -| / -| / -| Q5 -| / -|Q4 -+---------------- +```bash +dotnet tool install --global MagicQuant +magicquant --version +magicquant init-config --output config.yaml ``` -A nonlinear win looks like: -``` - Q6 - / - / ← MQ-Q5_K_1 (above the line) - Q5 - / -Q4 -``` +The package becomes available after the first successful release publication; until then use the [source installation instructions](docs/setup.md#build-from-source). -That hybrid sits above the straight line between Q4 and Q5. +Edit the generated config for your source model, architecture identity, export destination, and storage. Initialize the external toolchain, validate the config, then start the campaign: -Meaning: -👉 It’s a **more efficient trade** than the normal step-up +```bash +magicquant initialize-llama-cpp +magicquant pipeline --config ./config.yaml --check-config --strict-config +magicquant pipeline --config ./config.yaml +``` -This is what MagicQuant calls a "nonlinear trade/win" when such wordage is used. +Initialization can download/build llama.cpp and install Python dependencies. NuGet installs MagicQuant, not model weights or a complete GPU toolchain. Follow the [installation guide](docs/setup.md) for native prerequisites, GPU setup, custom toolchains, and environment paths. ---- +For updates: `dotnet tool update --global MagicQuant`. For reproducible runs, install a particular release with `--version X.Y.Z` and retain your config, model revision, and run provenance. -## Deeper Understanding +## Configure a campaign -For a deeper dive into MagicQuant and how it works, the [wiki index](https://github.com/magiccodingman/MagicQuant-Wiki/blob/main/wiki/index.md) is a good place to start. +A minimal example (replace these paths and the architecture identity): -When you see a MagicQuant hybrid, it’s not just a “Q4.5” sitting somewhere between Q4 and Q5. It represents a discovered configuration where the **KLD reduction is non-linear relative to the size increase**, a genuinely better trade space. Not universally “better” than everything else, but a variant that earned its place through measurable advantage. +```yaml +paths: + model_dir: /data/models/my-model + scratch_roots: + - /mnt/nvme-a/magicquant-scratch + - /mnt/nvme-b/magicquant-scratch +identity: + architecture_family_name: my-model-family +output: + output_dir: /data/exports/my-model-MagicQuant + output_name_prefix: MyModel +learning: + confirm_tensor_group_profile: true +``` -Whether the winner is a hybrid or a pure baseline from llama.cpp or Unsloth, any quant that removes another from the final selection does so because its dominance made the alternative no longer worth considering. +Custom YAML uses typed defaults for omitted values; it does not merge with the bundled tuning profile. Start with `init-config` when you want that complete profile. See [configuration](docs/configuration.md), [examples](examples), and the [command reference](docs/commands.md). -The goal is not to flood the space with near-duplicates offering negligible KLD gains for minimal size differences, nor to claim superiority for the sake of it. In fact, that’s explicitly what MagicQuant avoids. +**Plan scratch storage early.** Quantization writes and rereads large intermediate models, and storage can be a major throughput limitation. Fast SSD/NVMe scratch disks, especially separate physical devices, can materially improve throughput when IO is the bottleneck. Multiple folders on the same device still share its bandwidth. Allow space for concurrent intermediate artifacts and keep unrelated data out of managed scratch/export directories. See [storage](docs/storage.md) and [best practices](docs/best-practices.md). -MagicQuant is built around transparency, honesty, maintainability, and most importantly trust. As it evaluates new architectures and quant families, it doesn’t invent quantization schemes in isolation. Instead, it learns from proven tensor assignments provided by trusted sources like llama.cpp and Unsloth. If those baselines are stable, MagicQuant operates within that same safe space, extending rather than reinventing. +## Learning from external quantizations -Historical sources expand that tensor vocabulary; they do not vote on the current winner. MagicQuant pins the source revision, rebuilds the available recipes under current controlled conditions, and relearns their effects rather than replaying an old final mixture. +External providers are optional. MagicQuant can run using its local baseline choices alone, but compatible external tensor assignments can provide valuable additional evidence. -That said, the system is designed to adapt. Edge cases can exist, but the architecture is intentionally flexible to handle them. +**Unsloth is the maintainer's recommended starting point** for external GGUF baselines. MagicQuant can learn their tensor-group patterns, rebuild a controlled equivalent from your local source model, and benchmark it in your campaign. It does not simply trust an external file's label or score. Choose the exact matching model and revision, and review its license. See the [Unsloth configuration walkthrough](docs/best-practices.md#optional-unsloth-baselines) and [research explanation](wiki/docs/Learning-From-Existing-Quantizations.md). -### How MagicQuant Works +## Documentation -``` - ┌────────────────────────────┐ - │ Input Quantized Models │ - │ ───────────────────────── │ - │ llama.cpp / Unsloth / etc │ - └────────────┬──────────────┘ - │ - │ Inspect tensors - ▼ - ┌────────────────────────────┐ - │ Tensor Extraction Layer │ - │ ───────────────────────── │ - │ - Read all tensors │ - │ - Detect quant types │ - │ - Capture F32 / BF16 │ - └────────────┬──────────────┘ - │ - │ Group by role - ▼ - ┌────────────────────────────┐ - │ Tensor Group Mapping │ - │ ───────────────────────── │ - │ embeddings │ - │ attn_q / attn_kv / output │ - │ ffn_up_gate / ffn_down │ - │ lm_head / moe_* │ - └────────────┬──────────────┘ - │ - │ Learn configs - ▼ - ┌────────────────────────────┐ - │ Learned Config Library │ - │ ───────────────────────── │ - │ "Q5_K attn_q pattern" │ - │ "UD-Q5_K_XL ffn pattern" │ - │ etc │ - └────────────┬──────────────┘ - │ - │ Normalize external configs - ▼ - ┌────────────────────────────┐ - │ Controlled Rebuild Layer │ - │ ───────────────────────── │ - │ - Apply configs to BF16 │ - │ - Use MagicQuant imatrix │ - │ - Equal comparison ground │ - └────────────┬──────────────┘ - │ - │ Feed into - ▼ - ┌────────────────────────────┐ - │ Hybrid Construction Engine │ - │ ───────────────────────── │ - │ Mix tensor groups across │ - │ learned configurations │ - └────────────┬──────────────┘ - │ - │ Evaluate candidates - ▼ - ┌────────────────────────────┐ - │ Prediction + Isolation │ - │ ───────────────────────── │ - │ - Group-level testing │ - │ - Rank-safe prediction │ - │ - Controlled context tests │ - └────────────┬──────────────┘ - │ - │ Build real GGUF - ▼ - ┌────────────────────────────┐ - │ Benchmark Layer │ - │ ───────────────────────── │ - │ - KLD (primary) │ - │ - PPL (secondary) │ - │ - Measured GPU scheduling │ - └────────────┬──────────────┘ - │ - │ Final decision - ▼ - ┌────────────────────────────┐ - │ Survivor Selection │ - │ ───────────────────────── │ - │ - Dominance pruning │ - │ - Nonlinear winners │ - │ - Spacing collapse │ - └────────────────────────────┘ -``` +| Start here | What you will find | +| --- | --- | +| [Installation](docs/setup.md) | NuGet, native prerequisites, custom environments, source builds | +| [Configuration](docs/configuration.md) | YAML, overrides, read-only validation, profiles | +| [Commands](docs/commands.md) | Pipeline, setup, cloning, prediction validation | +| [Best practices](docs/best-practices.md) | Scratch disks, Unsloth, reproducibility, first campaigns | +| [Storage](docs/storage.md) | Persistent data, scratch leases, cache and output ownership | +| [Research](wiki/index.md) | Measurements, prediction, pruning, hybrid selection | +| [Contributing](CONTRIBUTING.md) | Development workflow, tests, code boundaries | +| [Releases](docs/releases.md) | Automatic versions and NuGet trusted publishing | -The controlled context tests check whether a promising group choice still behaves the same way when the surrounding model moves from a Q4-or-better regime into more aggressive compression. They are bounded and evidence-driven because exhaustive context testing would recreate the full combinatorial problem. +## Development and history -GPU scheduling is also measured rather than assumed. A large benchmark can use multiple GPUs in one shared process, while batches of smaller candidates can run concurrently on independent GPUs when that produces higher aggregate throughput. +Application code lives in `src/`, tests in `tests/`, operational guides in `docs/`, and research documentation in `wiki/`. Both the former MagicQuant-Wiki and MagicQuant-Pipeline histories are retained. The `evolution` command remains a compatibility alias for `pipeline`; existing database and artifact contracts are preserved. Historical research remains under `archival/` and is not current setup guidance. -The final release is a curated survivor menu. Research campaigns and cross-run audits should preserve the full nondominated evidence frontier before applying spacing, so that a presentation decision does not erase valid results. +## License -## Deep Dive Documentation +MagicQuant's original code and documentation are licensed under **GNU AGPL version 3 only** (`AGPL-3.0-only`). Commercial use is permitted subject to its terms. Distribution and remote interaction with modified versions carry source-availability obligations; the [license text](LICENSE) controls the details. -- [Wiki index](./wiki/index.md) -- [Prediction Engine](./wiki/docs/Prediction-Engine.md) -- [Regime-Aware Tensor Search](./wiki/docs/Regime-Aware-Search.md) -- [GPU Benchmark Scheduling](./wiki/docs/GPU-Benchmark-Scheduling.md) -- [Pareto Archives and Reproducibility](./wiki/docs/Pareto-Archives-And-Reproducibility.md) +This does not automatically relicense model weights or generated GGUFs. Model, dataset, external-provider, and third-party dependency licenses still apply. See [third-party notices](THIRD-PARTY-NOTICES.md). diff --git a/THIRD-PARTY-NOTICES.md b/THIRD-PARTY-NOTICES.md new file mode 100644 index 0000000..e7e5f5b --- /dev/null +++ b/THIRD-PARTY-NOTICES.md @@ -0,0 +1,43 @@ +# Third-party notices + +MagicQuant's original code is AGPL-3.0-only. Dependencies retain their own licenses; they are not relicensed by this repository. The runtime dependency inventory below is taken from the committed application lock file. License texts and bundled notices are retained in [licenses/](licenses/), also included in the tool package. + +LibGit2Sharp is MIT; its native libgit2 component is GPL version 2 with an explicit linking exception permitting combinations with other programs. Keep that exception with its license. SQLitePCLRaw is Apache-2.0; SQLite itself is public domain. Other listed managed components use MIT or BSD-2-Clause. Build/test-only dependencies remain governed by their package notices. + +| Package | Version | License | +| --- | --- | --- | +| Blake3 | 2.2.0 | BSD-2-Clause | +| DuckDB.NET.Data.Full | 1.4.3 | MIT | +| LibGit2Sharp | 0.31.0 | MIT | +| Spectre.Console | 0.54.0 | MIT | +| System.Management | 10.0.11 | MIT | +| YamlDotNet | 17.0.1 | MIT | +| DuckDB.NET.Bindings.Full | 1.4.3 | MIT | +| LibGit2Sharp.NativeBinaries | 2.0.323 | GPL-2.0 with linking exception | +| Microsoft.Data.Sqlite | 10.0.11 | MIT | +| Microsoft.Data.Sqlite.Core | 10.0.11 | MIT | +| Microsoft.EntityFrameworkCore | 10.0.11 | MIT | +| Microsoft.EntityFrameworkCore.Abstractions | 10.0.11 | MIT | +| Microsoft.EntityFrameworkCore.Analyzers | 10.0.11 | MIT | +| Microsoft.EntityFrameworkCore.Relational | 10.0.11 | MIT | +| Microsoft.EntityFrameworkCore.Sqlite | 10.0.11 | MIT | +| Microsoft.EntityFrameworkCore.Sqlite.Core | 10.0.11 | MIT | +| Microsoft.Extensions.Caching.Abstractions | 10.0.11 | MIT | +| Microsoft.Extensions.Caching.Memory | 10.0.11 | MIT | +| Microsoft.Extensions.Configuration.Abstractions | 10.0.11 | MIT | +| Microsoft.Extensions.DependencyInjection | 10.0.11 | MIT | +| Microsoft.Extensions.DependencyInjection.Abstractions | 10.0.11 | MIT | +| Microsoft.Extensions.DependencyModel | 10.0.11 | MIT | +| Microsoft.Extensions.Logging | 10.0.11 | MIT | +| Microsoft.Extensions.Logging.Abstractions | 10.0.11 | MIT | +| Microsoft.Extensions.Options | 10.0.11 | MIT | +| Microsoft.Extensions.Primitives | 10.0.11 | MIT | +| SQLitePCLRaw.bundle_e_sqlite3 | 2.1.12 | Apache-2.0 | +| SQLitePCLRaw.core | 2.1.12 | Apache-2.0 | +| SQLitePCLRaw.lib.e_sqlite3 | 2.1.12 | Apache-2.0 | +| SQLitePCLRaw.provider.e_sqlite3 | 2.1.12 | Apache-2.0 | +| System.CodeDom | 10.0.11 | MIT | + +The native/Python toolchain (including llama.cpp, Python, PyTorch, llama-cpp-python and downloaded packages) is installed separately and retains its own licenses and notices. Model weights, datasets and external GGUF baselines are also separate works; check their specific terms before downloading or redistributing them. MagicQuant does not grant rights to third-party models or training data. + +When updating dependencies, refresh the lock files, review their license metadata and native component notices, and update this inventory. Package source repositories and exact source commits are recorded in their NuGet metadata; retain upstream notices when redistributing binaries. diff --git a/assets/icon.png b/assets/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a2ca809a9618abcbfcfae4569dbb0a5547bf341c GIT binary patch literal 1475 zcmb_c{WIHl9RGZicnDI|=INr;(9LFS!iRx{`SwuFfB55Zfq!kZAgvio=U_ZI{5AWB#?{%+xKbM?~!eNYTjQ{|c z@UKJ80|4nD1Pl>6Col{60sy_z@Q^d#=G4BMKb`r(MQa*=JJpi9qqAVAloS_7+-Ji_ zgHq7NxsAtN!W@;3@u}Q;ZO~VT@DGteM6v4y{@5XD`vb|9Z#+%jkNb~AioU4n#>N-Y&HoGj-Rks!5Hxk$s^aQsLWwWVoH;O2X{rQ#0(5S3$(2KG2tSVP4P zhJ>SGupMNQwVGHrWnY9Q5J>kab_i>H30u3QXofc@iY6XP%C!%SvVwE0Qmv)ads2ax z5rw=d(tCP*|ug z5x<+Fnqbluwd2A#2kOtI6daP7bQes2tW3Da?!<$4ReLnl^*&4m%oNuGTaO)$S>S~* zjyK}*u#g-9mV9;~u8jN_nRcpRnSS4nU@nG}V{cIf(~%r0Y=}5HtmI9&O=h{-Xyi3> z{_u*WG5YnhLILNi^#TiSgOtfb`62h8D<7G2wP#@mR^UJ#-}E~J4-LPO44C-UBLwyA zwN+poAoS9p|3!4I^|;F||5TD3t{Hf!u^)CLD=8G>h8{5UYqf?k^srI<1RDm5xg)>3+{G44a2?8_FY1Y^a6wjyqO?bIb77dSb~4 zg|heCi)5d)CeVV_XbQ#|L&?YTPt+fb5tGFL+E-EFY;|W=CXRlfUP_sPK|T4CHaYmA z@<1K&G?LM>Z90tO4Ca_J@%axSY+W|}dUQ+=h}Hh>#^ljn`RP*j ze{k2o(4O8u_G4JO@MBjYk(BgUjm zVSh;jgiZa)M`>Iw0+YY^iKIx-#3Cve`NqLhkdk%`T_n&Y}%jN=`P;N z{hgOZuNWvB=cyJ)GT5IkOYVgJY6@&uboJJ7OG*C-QL(k{i)DVZ8yvY4^>T-PEWJ4j z+0Hvp{CuhAU_O()C0Xv7o>{wXl_aV-`8GChP_4=+U%7FwP4~+_W5=6-V56qQzQ~&?~ literal 0 HcmV?d00001 diff --git a/docs/architecture.md b/docs/architecture.md index a3f0090..896250d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -2,9 +2,9 @@ ## Execution flow -`Program.cs` dispatches through `CommandCatalog`. Help returns before runtime initialization. A normal command reads and validates YAML/CLI/input paths before loading `Config.Current` and run state into `MQ.DB.Cache`. It records provenance, cleans stale scratch, checks dependencies, and invokes an `ICommand`. `--check-config` exits before those runtime changes. +`src/MagicQuant/Program.cs` dispatches through `CommandCatalog`. Help returns before runtime initialization. A normal command reads and validates YAML/CLI/input paths before loading `Config.Current` and run state into `MQ.DB.Cache`. It records provenance, cleans stale scratch, checks dependencies, and invokes an `ICommand`. `--check-config` exits before those runtime changes. -`Commands/QuantizationPipeline.cs` coordinates full discovery. `Evolution.cs` preserves the historical C# entry point and the CLI registry keeps `evolution` as an alias. The orchestrator should describe stage order; reusable behavior belongs in services. +`src/MagicQuant/Commands/QuantizationPipeline.cs` coordinates full discovery. `Evolution.cs` preserves the historical C# entry point and the CLI registry keeps `evolution` as an alias. The orchestrator should describe stage order; reusable behavior belongs in services. 1. Validate source model and initialize model-local paths. 2. Obtain model hash; prepare native GGUF and optional projector; review tensor grouping. @@ -15,7 +15,7 @@ 7. Fit predictions, investigate contextual evidence, choose candidates, and validate them with real benchmarks. 8. Finalize survivors and write GGUFs, manifests, benchmark summaries, and model cards. -The [research wiki](https://github.com/magiccodingman/MagicQuant-Wiki) is the source for the mathematical motivation. This guide maps the implementation, not a new algorithm specification. +The [research wiki](https://github.com/magiccodingman/MagicQuant) is the source for the mathematical motivation. This guide maps the implementation, not a new algorithm specification. ## Where to change things diff --git a/docs/best-practices.md b/docs/best-practices.md new file mode 100644 index 0000000..1d6f534 --- /dev/null +++ b/docs/best-practices.md @@ -0,0 +1,58 @@ +# Campaign best practices + +## Start with a small, identifiable campaign + +Use a complete local source model that the selected llama.cpp converter supports. Keep the exact model revision, architecture/profile identity, imatrix settings, and evaluation data consistent when comparing runs. Begin with a small model and the generated profile before scaling up. Run `magicquant pipeline --config config.yaml --check-config --strict-config` first; this validates input structure and paths, not memory capacity or numerical quality. + +## Give scratch IO its own resources + +Large intermediate GGUFs make storage throughput a potential bottleneck. Prefer fast local SSD/NVMe scratch storage; separate physical disks can allow independent heavy writers. MagicQuant permits one heavy writer per configured scratch root, so two directories on the same disk do not create independent bandwidth and can increase contention. + +```yaml +paths: + scratch_roots: + - /mnt/nvme-a/magicquant-scratch + - /mnt/nvme-b/magicquant-scratch +``` + +Use existing writable parent locations dedicated to this work. Allow space for several large model artifacts, monitor free space and device throughput, and leave room for durable downloads and exports too. A faster disk helps when IO is limiting; GPU/CPU compute, RAM, and evaluation workload can instead dominate. Avoid fixed speedup expectations. See [storage ownership and cleanup](storage.md). + +## Optional Unsloth baselines + +The maintainer recommends Unsloth as a primary place to look for external GGUF tensor assignments. These sources are optional, and their value depends on model compatibility and measured results. Start with a repository for the exact source model; a similar name or matching architecture alone is insufficient. + +In the generated configuration, edit `baselines.custom_repositories`. The following is a structural example, not a promise that a particular upstream file exists. Replace the model/file placeholders, pin `revision` to the provider commit you inspected, and retain the rest of your campaign configuration: + +```yaml +baselines: + custom_repositories: + - repo_id: unsloth/YOUR-EXACT-MODEL-GGUF + revision: PROVIDER_COMMIT_SHA + enabled: true + short_source_name: UD + source_kind: huggingface_gguf_repository + require_all_includes_to_resolve: true + validate_tensor_names_against_source_model: true + includes: + - file_name: YOUR-EXACT-MODEL-UD-Q4_K_XL.gguf + baseline_family: Q4_K_M + quantize_base_name: Q4_K_M + display_name: UD_Q4_K_XL + allow_as_learning_baseline: true + allow_as_combination_carrier: true + allow_as_explicit_group_candidate: true +``` + +MagicQuant resolves the specified files, validates tensor-name parity, learns assignments, rebuilds using the local source model, and benchmarks the reconstruction. External downloads are durable cache data, distinct from temporary scratch artifacts. Review provider/model licensing and available disk space before enabling sources. Pure external learned baselines are not exported by default; inspect `output.export_external_learned_baselines` if you need them. + +For a provider-free campaign leave `baselines.custom_repositories` empty. See [learning from existing quantizations](../wiki/docs/Learning-From-Existing-Quantizations.md) for the research rationale. + +## Retain enough evidence to reproduce a result + +Pin the MagicQuant package version and provider/model revisions. Keep the YAML, imatrix/evaluation data identity, llama.cpp revision, hardware context, and local `Runs/*/run.json` records. Provenance captures available versions and settings; it is not a complete frozen environment or numerical reproducibility guarantee. Remove private paths or credentials before sharing logs. + +Use the same measurement conditions for comparisons. Reuse validated caches deliberately; changing runtime roots or tensor profiles can change which evidence is selected. Avoid multiple campaigns in the same process and competing runs in the same model/runtime workspace. + +## Platform expectations + +Linux campaigns have been exercised, including a small-model conversion/quantization smoke test. Windows CI validates builds, ordinary tests, and tool packaging; end-to-end Windows campaigns remain unvalidated. Report failures with package/toolchain versions, sanitized configuration, and relevant logs. diff --git a/docs/commands.md b/docs/commands.md index ed63850..ba5e337 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -1,11 +1,15 @@ # Commands and workflows -Run these examples from the repository root after a Release build. Replace paths and identities with your own. `mq` in older command help is shorthand for invoking the MagicQuant executable; this repository does not install a global `mq` tool. +Run these examples with the NuGet-installed `magicquant` CLI. Replace paths and identities with your own. For source builds, substitute `dotnet run --project src/MagicQuant -c Release --no-build --` for `magicquant`. `mq` in older help is shorthand, not an installed command. + +## Config creation and version + +`magicquant init-config --output config.yaml` copies the bundled tuning profile without runtime setup and refuses to overwrite a file. `magicquant --version` prints the application version and available source revision. ## Full discovery pipeline ```sh -dotnet run --project src/MagicQuant -c Release --no-build -- pipeline \ +magicquant pipeline \ --config config.local.yaml \ --model-dir /data/models/my-model \ --architecture-family my-model-family \ @@ -20,7 +24,7 @@ The pipeline converts/loads the native source, reviews tensor groups, resolves i ## Clone known tensor configurations ```sh -dotnet run --project src/MagicQuant -c Release --no-build -- clone-repository-quants \ +magicquant clone-repository-quants \ --config config.local.yaml \ --model-dir /data/models/compatible-model \ --architecture-family my-model-family \ @@ -35,7 +39,7 @@ By default the manifest must match the target tensor inventory. `--allow-missing ## Validate predictions against existing measurements ```sh -dotnet run --project src/MagicQuant -c Release --no-build -- validate-predictions \ +magicquant validate-predictions \ --config config.local.yaml \ --model-dir /data/models/my-model \ --architecture-family my-model-family \ @@ -49,7 +53,7 @@ For imatrix measurements supply `--imatrix-path /data/imatrix.dat` or `--imatrix ## Rerun and reuse ```sh -dotnet run --project src/MagicQuant -c Release --no-build -- pipeline \ +magicquant pipeline \ --config config.local.yaml --reuse-existing-final-artifacts ``` diff --git a/docs/configuration.md b/docs/configuration.md index 4e261c2..c7e8e3d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1,5 +1,7 @@ # Configuration and paths +Create the complete editable tuning profile with `magicquant init-config --output config.yaml`, then pass it explicitly with `--config config.yaml`. See [best practices](best-practices.md) for scratch storage and optional Unsloth baselines. + ## Loading and precedence `--config ` selects a YAML file; otherwise the executable loads its adjacent `config.default.yaml`. Debug and Release follow the same rule. `config.dev.yaml` is no longer selected automatically. @@ -8,7 +10,7 @@ The loader deserializes the selected file into `MagicQuantYamlConfig`, whose pro Unknown CLI options, duplicate options, missing values, and values supplied to presence-only flags are rejected. CLI string options generally override nonblank YAML values. Many boolean switches only enable a feature; use YAML to disable it unless a specific negative CLI switch exists. Use `--name value` or `--name=value`; quote paths with spaces using normal shell quoting. -Unknown or inactive YAML keys produce a warning with their setting path and line number; `--strict-config` rejects them. Compare with the commented default file and `MagicQuant/Configuration/MagicQuantYamlConfig.cs`. CI strictly parses the distributed examples so their keys cannot silently drift. +Unknown or inactive YAML keys produce a warning with their setting path and line number; `--strict-config` rejects them. Compare with the commented default file and `src/MagicQuant/Configuration/MagicQuantYamlConfig.cs`. CI strictly parses the distributed examples so their keys cannot silently drift. ## Main sections diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..63f04c9 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,14 @@ +# Using and developing MagicQuant + +Start with the [project briefing and quick start](../README.md). These guides describe the application; the [research wiki](../wiki/index.md) explains its measurement and selection methodology. + +- [Setup](setup.md): NuGet installation, toolchain preparation, source builds, troubleshooting. +- [Configuration](configuration.md): YAML profile, CLI overrides and path rules. +- [Commands](commands.md): discovery, clone/export and prediction validation. +- [Best practices](best-practices.md): scratch disks, optional Unsloth baselines and reproducibility. +- [Storage](storage.md): runtime/model directories, caches, scratch and provenance. +- [Architecture](architecture.md): code map, service responsibilities and numerical invariants. +- [Extending the code](extending.md): worked examples for contributors. +- [Testing](testing.md): ordinary PR checks, package tests and opt-in model smoke. +- [Releases](releases.md): automatic versioning and trusted publishing setup. +- [Migration](migration.md): legacy command/config compatibility and repository history. diff --git a/docs/nuget-readme.md b/docs/nuget-readme.md new file mode 100644 index 0000000..efdf4a3 --- /dev/null +++ b/docs/nuget-readme.md @@ -0,0 +1,34 @@ +# MagicQuant + +**Benchmark-driven GGUF quantization and mixed-precision hybrid discovery for llama.cpp.** + +MagicQuant measures baselines, learns tensor-group assignments, discovers promising hybrid quantizations, and validates size/fidelity tradeoffs before exporting selected GGUF artifacts. It is a command-line tool, not an evolutionary search algorithm or a new quantization format. + +## Install + +Install the .NET 10 SDK, then: + +```sh +dotnet tool install --global MagicQuant +magicquant init-config --output config.yaml +``` + +Edit model, architecture, output, and scratch paths, then prepare the native toolchain and run: + +```sh +magicquant initialize-llama-cpp +magicquant pipeline --config config.yaml --check-config --strict-config +magicquant pipeline --config config.yaml +``` + +NuGet does not bundle model weights or a ready-to-use GPU toolchain. Linux is the tested campaign platform; Windows has automated build/unit/package checks but full campaigns remain unvalidated. + +Fast, separate physical scratch disks can help substantially when repeated large GGUF writes are the bottleneck. External quantization providers are optional; Unsloth is the maintainer's recommended starting point for compatible tensor-assignment evidence. + +- [Project briefing and research](https://github.com/magiccodingman/MagicQuant) +- [Installation and prerequisites](https://github.com/magiccodingman/MagicQuant/blob/main/docs/setup.md) +- [Configuration](https://github.com/magiccodingman/MagicQuant/blob/main/docs/configuration.md) +- [Scratch disks and optional Unsloth learning](https://github.com/magiccodingman/MagicQuant/blob/main/docs/best-practices.md) +- [Support spare-time development and storage costs](https://sayou.biz/support) + +MagicQuant is licensed under **AGPL-3.0-only**. Model weights and generated GGUFs retain their applicable licenses. See [license and third-party notices](https://github.com/magiccodingman/MagicQuant/blob/main/THIRD-PARTY-NOTICES.md). diff --git a/docs/pipeline-migration-readme.md b/docs/pipeline-migration-readme.md deleted file mode 100644 index fb01630..0000000 --- a/docs/pipeline-migration-readme.md +++ /dev/null @@ -1,86 +0,0 @@ -# MagicQuant Pipeline - -MagicQuant is a benchmark-driven GGUF evaluation and hybrid-discovery system. It measures standard and external quantization baselines, probes tensor groups, predicts promising combinations, and validates final survivors with real benchmarks. Hybrids earn a place only when their size/fidelity tradeoff is worthwhile. - -This repository contains the .NET command-line application. The [MagicQuant research wiki](https://github.com/magiccodingman/MagicQuant-Wiki) explains the methodology and results. Despite the historical `evolution` command name, the current pipeline does **not** perform evolutionary search. - -## Build and inspect - -Install the .NET 10 SDK, then run from the repository root: - -```sh -dotnet restore MagicQuant.sln -dotnet build MagicQuant.sln -c Release -dotnet test MagicQuant.sln -c Release --no-build -dotnet run --project src/MagicQuant -c Release --no-build -- --help -dotnet run --project src/MagicQuant -c Release --no-build -- pipeline --help -``` - -Ordinary tests skip the explicitly opt-in model smoke test. Building, ordinary testing, and viewing help do not require model weights or llama.cpp. Running without arguments also shows help, in both Debug and Release. - -## Run a model - -Real quantization needs a complete local Hugging Face model directory (top-level `.safetensors`, model configuration, and tokenizer assets), llama.cpp, a Python environment, and enough RAM/VRAM and disk space for native GGUFs, baselines, logits, and exports. Hardware requirements depend on the model. Linux with an apt-based distribution is the primary automatic setup path; Windows has setup code but is not exercised by the model smoke test. Automatic macOS setup is not implemented. - -1. Copy the distributed tuning profile and edit the paths and model identity: - - ```sh - cp src/MagicQuant/config.default.yaml config.local.yaml - ``` - - Set `paths.model_dir` and `identity.architecture_family_name`. Choose a dedicated `output.output_dir` and set `output.output_name_prefix`. Before publishing generated model cards, set `readme.frontmatter` to the source model's actual license and metadata. Use absolute paths for a portable campaign invocation. - -2. Prepare dependencies: - - ```sh - dotnet run --project src/MagicQuant -c Release --no-build -- initialize-llama-cpp - ``` - - This can download/build llama.cpp, install Python packages, and request sudo for apt packages on Linux. It uses `/MagicQuant`. To use existing llama.cpp files, configure **all three** of `paths.llama_root`, `paths.llama_bin`, and `paths.convert_script`, and pass `--config config.local.yaml`. See [setup](docs/setup.md) for Python requirements and custom runtime roots. - -3. Validate before starting the campaign: - - ```sh - dotnet run --project src/MagicQuant -c Release --no-build -- pipeline --config config.local.yaml --check-config --strict-config - ``` - - Then start it: - - ```sh - dotnet run --project src/MagicQuant -c Release --no-build -- pipeline --config config.local.yaml - ``` - - Review the tensor grouping prompt before allowing learning to continue. The run learns/reuses benchmark truth and exports its selected survivors. Runtime dependency validation may perform setup when using the default environment. - -**Use a dedicated export directory:** normal export cleans/rebuilds its contents. `--reuse-existing-final-artifacts` permits reuse only when artifacts match the command's validation rules. Do not point output at your source model directory or another directory containing files you need to keep. - -## Commands - -| Command | Purpose | -| --- | --- | -| `pipeline` | Full baseline learning, isolation probing, prediction, real validation, and export | -| `evolution` | Backward-compatible alias for `pipeline` | -| `build-hybrids` | Existing entry point for the full pipeline, including export; not an export-only shortcut | -| `clone-repository-quants` | Rebuild configurations from a compatible repository or clone manifest | -| `validate-predictions` | Compare predictions with existing SQLite benchmark truth and export reports | -| `initialize-llama-cpp` | Set up or update native/Python dependencies | - -Append `--help` to any command. Arguments after `--` belong to MagicQuant, not `dotnet run`. [Command examples](docs/commands.md) cover cloning and validation. - -## Documentation - -- [Setup and troubleshooting](docs/setup.md) -- [Configuration and path rules](docs/configuration.md) -- [Commands and workflows](docs/commands.md) -- [Architecture and code map](docs/architecture.md) -- [Storage, caching, and reruns](docs/storage.md) -- [Contributing](CONTRIBUTING.md) -- [Tests, model smoke workflow, and merge checks](docs/testing.md) -- [Worked contributor examples](docs/extending.md) -- [Compatibility notes for existing users](docs/migration.md) - -The small [example configurations](examples/) demonstrate the required fields. They use C# defaults for omitted settings; they are **not** merged with `config.default.yaml`. Copy the full default file when you want its distributed tuning values. - -## Project status - -The research pipeline is active software with model- and hardware-dependent integration requirements. Ordinary unit/regression tests run without quantizing a model; a passing test suite alone does not establish numerical parity for a full hardware campaign. The repository does not yet contain a software license; the maintainer must choose one before an open-source release. A generated model card's license field does not license this program. diff --git a/docs/releases.md b/docs/releases.md new file mode 100644 index 0000000..b94b364 --- /dev/null +++ b/docs/releases.md @@ -0,0 +1,43 @@ +# NuGet releases + +MagicQuant is packaged as a .NET tool, package ID `MagicQuant`, executable `magicquant`. `.github/workflows/publish-nuget.yml` publishes when a commit reaches `release`, normally through a merged PR. Pushes directly to `release` also trigger it; use branch protection to require PRs if desired. Manual dispatch is available to retry publication and only runs on `release`. + +## Trusted publishing setup + +Configure the following on NuGet.org under your account's **Trusted Publishing** settings: + +| Field | Value | +| --- | --- | +| Repository owner | `magiccodingman` | +| Repository | `MagicQuant` | +| Workflow filename | `publish-nuget.yml` | +| Environment | `release` | +| Package scope | `MagicQuant` (allow creation for the first release) | + +The workflow filename has no `.github/workflows/` prefix in NuGet's policy. The publishing job uses **`environment: release`** and requests `id-token: write`. Create that GitHub environment and restrict its deployment branch to `release`. Add the repository secret **`NUGET_USER`** containing your NuGet profile username, not an email address or API key. Choose the intended package owner when creating the policy. + +The workflow uses `NuGet/login@v1` to exchange GitHub OIDC identity for a temporary NuGet key immediately before pushing. No permanent NuGet API key is needed. See [NuGet's official guide](https://learn.microsoft.com/en-us/nuget/nuget-org/trusted-publishing). Private-repository policies may need activation within NuGet's stated time window; make sure the source for a public package is accessible to its recipients when launching. + +## Automatic versions + +The first release is **0.1.0**. Each new release commit increments the greatest reserved stable version's patch number: `0.1.0`, `0.1.1`, `0.1.2`, and so on. You do not edit a version for ordinary patch releases. + +`release-version.txt` is a **minimum next version**, not a counter. To release a new minor or major version, raise it in the release PR (for example, `0.2.0` or `1.0.0`). Leaving it unchanged continues patch increments. Only stable `major.minor.patch` versions are supported by the release workflow. + +After Linux/Windows validation passes, the workflow reserves the chosen version with an annotated `vX.Y.Z` tag pointing at the exact commit. Tag creation on the remote is the atomic claim; concurrent releases retry a version collision without sharing the same version. Jobs are not canceled merely because another release arrives. Publication order may differ if runs finish at different times. + +A retry of the **same commit** reuses its tag/version, even if later releases exist. A failed pack or push can leave a reserved tag; rerun that workflow to finish it. Do not delete or move release tags. A new commit gets a new version. Gaps are acceptable if a failed release is intentionally abandoned. + +The release package is built with that version and source revision, installed into a clean tool directory, and tested before publication. `--skip-duplicate` makes retrying an already published version harmless; the first successfully published package remains authoritative. NuGet versions are immutable. A GitHub release record is created after publication. + +## Validation and maintenance + +PR CI validates Linux and Windows, Debug and Release, with locked restores and warnings as errors. Each job tests a locally packed `0.0.0-ci` package without publishing it. Release validation repeats ordinary and package checks on Linux and Windows before reserving a version. The exact versioned release package is then verified again on Linux before upload. + +Package checks inspect bundled config, Python helper, native libraries, license/readme/icon metadata, and tool startup from a directory outside the source tree. They exercise config creation, refusal to overwrite, and read-only path validation. These are not full Windows campaigns or numerical parity tests. + +The package uses committed dependencies; the installed llama.cpp/Python runtime is separately managed. Record both for research reproducibility. Required merge checks and environment policies are repository settings, not guaranteed merely by committing a workflow. + +## Migration PR merge method + +The launch PR connects the original Wiki and Pipeline histories through an unsquashed subtree import, then reorganizes the tree in later commits. **Merge that PR with a merge commit, not squash or rebase**, to retain both histories in `main`. Do not force-push the existing repository history. Future ordinary PRs can use the project's preferred merge policy. diff --git a/docs/setup.md b/docs/setup.md index 614cc54..04224ae 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -1,16 +1,43 @@ # Setup and troubleshooting -## Development requirements +## Install from NuGet -All solution projects target `net10.0`. Use the .NET 10 SDK. NuGet restore downloads the managed packages and native SQLite/DuckDB assets. The solution includes `MagicQuant`, `MQ.DB`, `MagicQuant.Tests`, and the offline `MagicQuant.ProcessFixture` test helper. +Linux is the tested campaign platform. Windows CI covers builds, ordinary tests and installed-package startup; full Windows campaigns remain unvalidated. Install the [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.0) first. ```sh -dotnet restore MagicQuant.sln -dotnet build MagicQuant.sln -c Release +dotnet tool install --global MagicQuant +magicquant --version +magicquant init-config --output config.yaml +``` + +NuGet publication begins with the first successful release. If the package is not yet listed, use the source-build route below. The CLI command is `magicquant`; `dotnet add package` is not the installation command for this application. + +If your shell cannot find `magicquant`, ensure the .NET tools directory is on `PATH`: `$HOME/.dotnet/tools` on Linux or `%USERPROFILE%\.dotnet\tools` on Windows, then reopen the shell. Use `dotnet tool update --global MagicQuant` to update, `dotnet tool uninstall --global MagicQuant` to remove the tool, or add `--version X.Y.Z` to install an exact version. Removing/updating the tool does not remove model/runtime data. + +Edit the generated YAML; set `paths.model_dir`, `identity.architecture_family_name`, `output.output_dir`, and dedicated `paths.scratch_roots`. For a first run: + +```sh +magicquant initialize-llama-cpp +magicquant pipeline --config ./config.yaml --check-config --strict-config +magicquant pipeline --config ./config.yaml +``` + +`init-config` copies the full bundled tuning profile and refuses to overwrite existing files. `--check-config` is read-only. The actual pipeline can perform dependency setup and write model/runtime artifacts. Fast scratch disks are especially valuable for the repeated large GGUF writes; read [best practices](best-practices.md) before a large campaign. + +## Build from source + +All solution projects target `net10.0`. The solution includes `src/MagicQuant`, `src/MQ.DB`, `tests/MagicQuant.Tests`, and the offline `tests/MagicQuant.ProcessFixture` helper. + +```sh +git clone https://github.com/magiccodingman/MagicQuant.git +cd MagicQuant +dotnet restore MagicQuant.sln --locked-mode -warnaserror +dotnet build MagicQuant.sln -c Release --no-restore -warnaserror dotnet test MagicQuant.sln -c Release --no-build +dotnet run --project src/MagicQuant -c Release --no-build -- init-config --output config.yaml ``` -These commands do not install llama.cpp or Python packages. Some regression tests create temporary SQLite databases and inspect local hardware. Tests do not require CUDA or model weights. +For source execution, replace `magicquant` in the other examples with `dotnet run --project src/MagicQuant -c Release --no-build --`. Build/test commands do not install llama.cpp or Python packages; ordinary tests do not require CUDA or weights. To exercise the actual package locally, see [testing](testing.md). ## Runtime setup diff --git a/docs/testing.md b/docs/testing.md index 26f172d..a062963 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -50,4 +50,22 @@ A manual GitHub Actions workflow is provided for a trusted self-hosted runner la The workflow reports failures; branch protection or a ruleset must require its checks to block merges. Configure `main` to require all four Linux/Windows Debug/Release test jobs after the workflow has run. If merge queues are enabled later, add a `merge_group` workflow trigger as well. -The repository's current private-repository plan returned HTTP 403 when branch protection was queried, explaining that an eligible plan or public visibility is required. The code change cannot override that GitHub restriction. Once supported, enable required checks and verify that a deliberately failing test PR cannot merge. Choosing visibility, billing, and the project software license remains a maintainer decision. +The unified MagicQuant repository is public. At launch preparation, `main` had no branch protection configured. Require the four `test (OS, Configuration)` checks plus `Secret scan` in repository settings if you want failures to block merging. Configure the `release` branch and deployment environment deliberately before publishing; a workflow alone does not prevent bypassing checks. + +## Installed-package and release checks + +```sh +python3 -m unittest discover -s scripts -p 'test_*.py' +dotnet pack src/MagicQuant -c Release --no-restore -p:Version=0.0.0-ci -o artifacts -warnaserror +python3 scripts/package_smoke.py artifacts/MagicQuant.0.0.0-ci.nupkg +``` + +Use `python` instead of `python3` where appropriate. The package smoke installs only from a temporary local feed, verifies shipped assets and native library presence, exercises CLI help/version and config creation outside the checkout, checks paths containing spaces, and confirms preflight is read-only. It never installs a model or native toolchain. Release-version tests use an isolated local bare Git remote; they never push to GitHub. + +PR CI runs these checks on both operating systems. [Release documentation](releases.md) explains the separately gated trusted-publishing workflow. + +## Secret checks + +The `Secret scan` CI job runs a checksum-pinned Gitleaks binary on full fetched history and the current tree. On Linux x64 run `python3 scripts/scan_secrets.py`. Reports redact candidate credentials. `.gitleaks.toml` retains default detectors and narrowly allows only the exact known tensor-name test fixture; do not suppress whole directories to silence new findings. + +A scanner is one check, not proof that every kind of sensitive information is absent. Review changes for private model names, personal paths, datasets, and credentials too. Git history preserves deleted files and author metadata. If a real credential is found, rotate it before planning any history rewrite. diff --git a/examples/pipeline.yaml b/examples/pipeline.yaml index 65e0031..6385850 100644 --- a/examples/pipeline.yaml +++ b/examples/pipeline.yaml @@ -1,6 +1,6 @@ # Copy to config.local.yaml and edit. Pass explicitly with --config. # Omitted settings use C# defaults, not a merge with config.default.yaml. -# For the full distributed tuning profile, copy MagicQuant/config.default.yaml instead. +# For the full distributed tuning profile, run magicquant init-config instead. paths: model_dir: /data/models/my-model identity: diff --git a/licenses/Blake3-license.txt b/licenses/Blake3-license.txt new file mode 100644 index 0000000..e091cb9 --- /dev/null +++ b/licenses/Blake3-license.txt @@ -0,0 +1,29 @@ +Copyright (c) 2020, Alexandre Mutel +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification +, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +> The underlying blake3_dotnet native library is using the code from https://github.com/BLAKE3-team/BLAKE3 +> with the following license https://github.com/BLAKE3-team/BLAKE3/blob/master/LICENSE + +This work is released into the public domain with CC0 1.0. Alternatively, it is +licensed under the Apache License 2.0. \ No newline at end of file diff --git a/licenses/DuckDB.NET.Bindings.Full-LICENSE-DuckDB.txt b/licenses/DuckDB.NET.Bindings.Full-LICENSE-DuckDB.txt new file mode 100644 index 0000000..a79d955 --- /dev/null +++ b/licenses/DuckDB.NET.Bindings.Full-LICENSE-DuckDB.txt @@ -0,0 +1,7 @@ +Copyright 2018-2022 Stichting DuckDB Foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/licenses/DuckDB.NET.Bindings.Full-LICENSE.md b/licenses/DuckDB.NET.Bindings.Full-LICENSE.md new file mode 100644 index 0000000..7a5aace --- /dev/null +++ b/licenses/DuckDB.NET.Bindings.Full-LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Giorgi Dalakishvili + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/licenses/LibGit2Sharp-LICENSE.md b/licenses/LibGit2Sharp-LICENSE.md new file mode 100644 index 0000000..c705543 --- /dev/null +++ b/licenses/LibGit2Sharp-LICENSE.md @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) LibGit2Sharp contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/licenses/LibGit2Sharp.NativeBinaries-libgit2.license.txt b/licenses/LibGit2Sharp.NativeBinaries-libgit2.license.txt new file mode 100644 index 0000000..701792e --- /dev/null +++ b/licenses/LibGit2Sharp.NativeBinaries-libgit2.license.txt @@ -0,0 +1,1410 @@ + libgit2 is Copyright (C) the libgit2 contributors, + unless otherwise stated. See the AUTHORS file for details. + + Note that the only valid version of the GPL as far as this project + is concerned is _this_ particular version of the license (ie v2, not + v2.2 or v3.x or whatever), unless explicitly otherwise stated. + +---------------------------------------------------------------------- + + LINKING EXCEPTION + + In addition to the permissions in the GNU General Public License, + the authors give you unlimited permission to link the compiled + version of this library into combinations with other programs, + and to distribute those combinations without any restriction + coming from the use of this file. (The General Public License + restrictions do apply in other respects; for example, they cover + modification of the file, and distribution when not linked into + a combined executable.) + +---------------------------------------------------------------------- + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. + +---------------------------------------------------------------------- + +The bundled ZLib code is licensed under the ZLib license: + + (C) 1995-2022 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + +---------------------------------------------------------------------- + +The Clar framework is licensed under the ISC license: + +Copyright (c) 2011-2015 Vicent Marti + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +---------------------------------------------------------------------- + +The bundled PCRE implementation (deps/pcre/) is licensed under the BSD +license. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the University of Cambridge nor the name of Google + Inc. nor the names of their contributors may be used to endorse or + promote products derived from this software without specific prior + written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +---------------------------------------------------------------------- + +The bundled winhttp definition files (deps/winhttp/) are licensed under +the GNU LGPL (available at the end of this file). + +Copyright (C) 2007 Francois Gouget + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + +---------------------------------------------------------------------- + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + +---------------------------------------------------------------------- + +The bundled SHA1 collision detection code is licensed under the MIT license: + +MIT License + +Copyright (c) 2017: + Marc Stevens + Cryptology Group + Centrum Wiskunde & Informatica + P.O. Box 94079, 1090 GB Amsterdam, Netherlands + marc@marc-stevens.nl + + Dan Shumow + Microsoft Research + danshu@microsoft.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +---------------------------------------------------------------------- + +The bundled wildmatch code is licensed under the BSD license: + +Copyright Rich Salz. +All rights reserved. + +Redistribution and use in any form are permitted provided that the +following restrictions are are met: + +1. Source distributions must retain this entire copyright notice + and comment. +2. Binary distributions must include the acknowledgement ``This + product includes software developed by Rich Salz'' in the + documentation or other materials provided with the + distribution. This must not be represented as an endorsement + or promotion without specific prior written permission. +3. The origin of this software must not be misrepresented, either + by explicit claim or by omission. Credits must appear in the + source and documentation. +4. Altered versions must be plainly marked as such in the source + and documentation and must not be misrepresented as being the + original software. + +THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + +---------------------------------------------------------------------- + +Portions of the OpenSSL headers are included under the OpenSSL license: + +Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) +All rights reserved. + +This package is an SSL implementation written +by Eric Young (eay@cryptsoft.com). +The implementation was written so as to conform with Netscapes SSL. + +This library is free for commercial and non-commercial use as long as +the following conditions are aheared to. The following conditions +apply to all code found in this distribution, be it the RC4, RSA, +lhash, DES, etc., code; not just the SSL code. The SSL documentation +included with this distribution is covered by the same copyright terms +except that the holder is Tim Hudson (tjh@cryptsoft.com). + +Copyright remains Eric Young's, and as such any Copyright notices in +the code are not to be removed. +If this package is used in a product, Eric Young should be given attribution +as the author of the parts of the library used. +This can be in the form of a textual message at program startup or +in documentation (online or textual) provided with the package. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + "This product includes cryptographic software written by + Eric Young (eay@cryptsoft.com)" + The word 'cryptographic' can be left out if the rouines from the library + being used are not cryptographic related :-). +4. If you include any Windows specific code (or a derivative thereof) from + the apps directory (application code) you must include an acknowledgement: + "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + +THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +The licence and distribution terms for any publically available version or +derivative of this code cannot be changed. i.e. this code cannot simply be +copied and put under another distribution licence +[including the GNU Public Licence.] + +==================================================================== +Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + +3. All advertising materials mentioning features or use of this + software must display the following acknowledgment: + "This product includes software developed by the OpenSSL Project + for use in the OpenSSL Toolkit. (http://www.openssl.org/)" + +4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + endorse or promote products derived from this software without + prior written permission. For written permission, please contact + openssl-core@openssl.org. + +5. Products derived from this software may not be called "OpenSSL" + nor may "OpenSSL" appear in their names without prior written + permission of the OpenSSL Project. + +6. Redistributions of any form whatsoever must retain the following + acknowledgment: + "This product includes software developed by the OpenSSL Project + for use in the OpenSSL Toolkit (http://www.openssl.org/)" + +THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY +EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR +ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED +OF THE POSSIBILITY OF SUCH DAMAGE. + +---------------------------------------------------------------------- + +The xoroshiro256** implementation is licensed in the public domain: + +Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . + +---------------------------------------------------------------------- + +The built-in SHA256 support (src/hash/rfc6234) is taken from RFC 6234 +under the following license: + +Copyright (c) 2011 IETF Trust and the persons identified as +authors of the code. All rights reserved. + +Redistribution and use in source and binary forms, with or +without modification, are permitted provided that the following +conditions are met: + +- Redistributions of source code must retain the above + copyright notice, this list of conditions and + the following disclaimer. + +- Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + +- Neither the name of Internet Society, IETF or IETF Trust, nor + the names of specific contributors, may be used to endorse or + promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +---------------------------------------------------------------------- + +The built-in git_fs_path_basename_r() function is based on the +Android implementation, BSD licensed: + +Copyright (C) 2008 The Android Open Source Project +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +---------------------------------------------------------------------- + +The bundled ntlmclient code is licensed under the MIT license: + +Copyright (c) Edward Thomson. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +---------------------------------------------------------------------- + +Portions of this software derived from Team Explorer Everywhere: + +Copyright (c) Microsoft Corporation + +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +--------------------------------------------------------------------------- + +Portions of this software derived from the LLVM Compiler Infrastructure: + +Copyright (c) 2003-2016 University of Illinois at Urbana-Champaign. +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +--------------------------------------------------------------------------- + +Portions of this software derived from Unicode, Inc: + +Copyright 2001-2004 Unicode, Inc. + +Disclaimer + +This source code is provided as is by Unicode, Inc. No claims are +made as to fitness for any particular purpose. No warranties of any +kind are expressed or implied. The recipient agrees to determine +applicability of information provided. If this file has been +purchased on magnetic or optical media from Unicode, Inc., the +sole remedy for any claim will be exchange of defective media +within 90 days of receipt. + +Limitations on Rights to Redistribute This Code + +Unicode, Inc. hereby grants the right to freely use the information +supplied in this file in the creation of products supporting the +Unicode Standard, and to make copies of this file in any form +for internal or external distribution as long as this notice +remains attached. + +--------------------------------------------------------------------------- + +Portions of this software derived from sheredom/utf8.h: + +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to + +--------------------------------------------------------------------------- + +Portions of this software derived from RFC 1320: + +Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. + +License to copy and use this software is granted provided that it +is identified as the "RSA Data Security, Inc. MD4 Message-Digest +Algorithm" in all material mentioning or referencing this software +or this function. + +License is also granted to make and use derivative works provided +that such works are identified as "derived from the RSA Data +Security, Inc. MD4 Message-Digest Algorithm" in all material +mentioning or referencing the derived work. + +RSA Data Security, Inc. makes no representations concerning either +the merchantability of this software or the suitability of this +software for any particular purpose. It is provided "as is" +without express or implied warranty of any kind. + +These notices must be retained in any copies of any part of this +documentation and/or software. + +---------------------------------------------------------------------- + +The bundled llhttp dependency is licensed under the MIT license: + +Copyright Fedor Indutny, 2018. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/licenses/Microsoft.Extensions.Caching.Abstractions-THIRD-PARTY-NOTICES.TXT b/licenses/Microsoft.Extensions.Caching.Abstractions-THIRD-PARTY-NOTICES.TXT new file mode 100644 index 0000000..1e194f3 --- /dev/null +++ b/licenses/Microsoft.Extensions.Caching.Abstractions-THIRD-PARTY-NOTICES.TXT @@ -0,0 +1,1418 @@ +.NET Runtime uses third-party libraries or other resources that may be +distributed under licenses different than the .NET Runtime software. + +In the event that we accidentally failed to list a required notice, please +bring it to our attention. Post an issue or email us: + + dotnet@microsoft.com + +The attached notices are provided for information only. + +License notice for ASP.NET +------------------------------- + +Copyright (c) .NET Foundation. All rights reserved. +Licensed under the Apache License, Version 2.0. + +Available at +https://github.com/dotnet/aspnetcore/blob/main/LICENSE.txt + +License notice for Slicing-by-8 +------------------------------- + +http://sourceforge.net/projects/slicing-by-8/ + +Copyright (c) 2004-2006 Intel Corporation - All Rights Reserved + + +This software program is licensed subject to the BSD License, available at +http://www.opensource.org/licenses/bsd-license.html. + + +License notice for Unicode data +------------------------------- + +https://www.unicode.org/license.html + +Copyright © 1991-2024 Unicode, Inc. All rights reserved. +Distributed under the Terms of Use in https://www.unicode.org/copyright.html. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of data files and any associated documentation (the "Data Files") or +software and any associated documentation (the "Software") to deal in the +Data Files or Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, and/or sell +copies of the Data Files or Software, and to permit persons to whom the +Data Files or Software are furnished to do so, provided that either (a) +this copyright and permission notice appear with all copies of the Data +Files or Software, or (b) this copyright and permission notice appear in +associated Documentation. + +THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF +THIRD PARTY RIGHTS. + +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE +BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA +FILES OR SOFTWARE. + +Except as contained in this notice, the name of a copyright holder shall +not be used in advertising or otherwise to promote the sale, use or other +dealings in these Data Files or Software without prior written +authorization of the copyright holder. + +License notice for zlib-ng +----------------------- + +https://github.com/zlib-ng/zlib-ng/blob/d54e3769be0c522015b784eca2af258b1c026107/LICENSE.md + +(C) 1995-2024 Jean-loup Gailly and Mark Adler + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + +3. This notice may not be removed or altered from any source distribution. + +License notice for opentelemetry-dotnet +--------------------------------------- + +https://github.com/open-telemetry/opentelemetry-dotnet/blob/805dd6b4abfa18ef2706d04c30d0ed28dbc2955e/LICENSE.TXT#L1 + +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +Copyright The OpenTelemetry Authors + + +License notice for LinuxTracepoints +----------------------------------- + +https://github.com/microsoft/LinuxTracepoints/blob/main/LICENSE + +Copyright (c) Microsoft Corporation. + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE + +License notice for Mono +------------------------------- + +http://www.mono-project.com/docs/about-mono/ + +Copyright (c) .NET Foundation Contributors + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the Software), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +License notice for International Organization for Standardization +----------------------------------------------------------------- + +Portions (C) International Organization for Standardization 1986: + Permission to copy in any form is granted for use with + conforming SGML systems and applications as defined in + ISO 8879, provided this notice is included in all copies. + +License notice for Intel +------------------------ + +"Copyright (c) 2004-2006 Intel Corporation - All Rights Reserved + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for Xamarin and Novell +------------------------------------- + +Copyright (c) 2015 Xamarin, Inc (http://www.xamarin.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +Copyright (c) 2011 Novell, Inc (http://www.novell.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +Third party notice for W3C +-------------------------- + +"W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE +Status: This license takes effect 13 May, 2015. +This work is being provided by the copyright holders under the following license. +License +By obtaining and/or copying this work, you (the licensee) agree that you have read, understood, and will comply with the following terms and conditions. +Permission to copy, modify, and distribute this work, with or without modification, for any purpose and without fee or royalty is hereby granted, provided that you include the following on ALL copies of the work or portions thereof, including modifications: +The full text of this NOTICE in a location viewable to users of the redistributed or derivative work. +Any pre-existing intellectual property disclaimers, notices, or terms and conditions. If none exist, the W3C Software and Document Short Notice should be included. +Notice of any changes or modifications, through a copyright statement on the new code or document such as "This software or document includes material copied from or derived from [title and URI of the W3C document]. Copyright © [YEAR] W3C® (MIT, ERCIM, Keio, Beihang)." +Disclaimers +THIS WORK IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENT WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. +COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENT. +The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the work without specific, written prior permission. Title to copyright in this work will at all times remain with copyright holders." + +License notice for Bit Twiddling Hacks +-------------------------------------- + +Bit Twiddling Hacks + +By Sean Eron Anderson +seander@cs.stanford.edu + +Individually, the code snippets here are in the public domain (unless otherwise +noted) — feel free to use them however you please. The aggregate collection and +descriptions are © 1997-2005 Sean Eron Anderson. The code and descriptions are +distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY and +without even the implied warranty of merchantability or fitness for a particular +purpose. + +License notice for Brotli +-------------------------------------- + +Copyright (c) 2009, 2010, 2013-2016 by the Brotli Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +compress_fragment.c: +Copyright (c) 2011, Google Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +""AS IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +decode_fuzzer.c: +Copyright (c) 2015 The Chromium Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +""AS IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." + +License notice for Json.NET +------------------------------- + +https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md + +The MIT License (MIT) + +Copyright (c) 2007 James Newton-King + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +License notice for vectorized base64 encoding / decoding +-------------------------------------------------------- + +Copyright (c) 2005-2007, Nick Galbreath +Copyright (c) 2013-2017, Alfred Klomp +Copyright (c) 2015-2017, Wojciech Mula +Copyright (c) 2016-2017, Matthieu Darbois +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +- Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for vectorized hex parsing +-------------------------------------------------------- + +Copyright (c) 2022, Geoff Langdale +Copyright (c) 2022, Wojciech Mula +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +- Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for RFC 3492 +--------------------------- + +The punycode implementation is based on the sample code in RFC 3492 + +Copyright (C) The Internet Society (2003). All Rights Reserved. + +This document and translations of it may be copied and furnished to +others, and derivative works that comment on or otherwise explain it +or assist in its implementation may be prepared, copied, published +and distributed, in whole or in part, without restriction of any +kind, provided that the above copyright notice and this paragraph are +included on all such copies and derivative works. However, this +document itself may not be modified in any way, such as by removing +the copyright notice or references to the Internet Society or other +Internet organizations, except as needed for the purpose of +developing Internet standards in which case the procedures for +copyrights defined in the Internet Standards process must be +followed, or as required to translate it into languages other than +English. + +The limited permissions granted above are perpetual and will not be +revoked by the Internet Society or its successors or assigns. + +This document and the information contained herein is provided on an +"AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING +TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING +BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION +HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF +MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + +Copyright(C) The Internet Society 1997. All Rights Reserved. + +This document and translations of it may be copied and furnished to others, +and derivative works that comment on or otherwise explain it or assist in +its implementation may be prepared, copied, published and distributed, in +whole or in part, without restriction of any kind, provided that the above +copyright notice and this paragraph are included on all such copies and +derivative works.However, this document itself may not be modified in any +way, such as by removing the copyright notice or references to the Internet +Society or other Internet organizations, except as needed for the purpose of +developing Internet standards in which case the procedures for copyrights +defined in the Internet Standards process must be followed, or as required +to translate it into languages other than English. + +The limited permissions granted above are perpetual and will not be revoked +by the Internet Society or its successors or assigns. + +This document and the information contained herein is provided on an "AS IS" +basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING TASK FORCE +DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY +RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A +PARTICULAR PURPOSE. + +License notice for Algorithm from RFC 4122 - +A Universally Unique IDentifier (UUID) URN Namespace +---------------------------------------------------- + +Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc. +Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. & +Digital Equipment Corporation, Maynard, Mass. +Copyright (c) 1998 Microsoft. +To anyone who acknowledges that this file is provided "AS IS" +without any express or implied warranty: permission to use, copy, +modify, and distribute this file for any purpose is hereby +granted without fee, provided that the above copyright notices and +this notice appears in all source code copies, and that none of +the names of Open Software Foundation, Inc., Hewlett-Packard +Company, Microsoft, or Digital Equipment Corporation be used in +advertising or publicity pertaining to distribution of the software +without specific, written prior permission. Neither Open Software +Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital +Equipment Corporation makes any representations about the +suitability of this software for any purpose." + +License notice for The LLVM Compiler Infrastructure +--------------------------------------------------- + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +License notice for Bob Jenkins +------------------------------ + +By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this +code any way you wish, private, educational, or commercial. It's free. + +License notice for Greg Parker +------------------------------ + +Greg Parker gparker@cs.stanford.edu December 2000 +This code is in the public domain and may be copied or modified without +permission. + +License notice for libunwind based code +---------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +License notice for Printing Floating-Point Numbers (Dragon4) +------------------------------------------------------------ + +/****************************************************************************** + Copyright (c) 2014 Ryan Juckett + http://www.ryanjuckett.com/ + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +******************************************************************************/ + +License notice for Printing Floating-point Numbers (Grisu3) +----------------------------------------------------------- + +Copyright 2012 the V8 project authors. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for xxHash +------------------------- + +xxHash - Extremely Fast Hash algorithm +Header File +Copyright (C) 2012-2021 Yann Collet + +BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php) + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +You can contact the author at: + - xxHash homepage: https://www.xxhash.com + - xxHash source repository: https://github.com/Cyan4973/xxHash + +License notice for Berkeley SoftFloat Release 3e +------------------------------------------------ + +https://github.com/ucb-bar/berkeley-softfloat-3 +https://github.com/ucb-bar/berkeley-softfloat-3/blob/master/COPYING.txt + +License for Berkeley SoftFloat Release 3e + +John R. Hauser +2018 January 20 + +The following applies to the whole of SoftFloat Release 3e as well as to +each source file individually. + +Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 The Regents of the +University of California. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions, and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions, and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE +DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for xoshiro RNGs +-------------------------------- + +Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . + +License for fastmod (https://github.com/lemire/fastmod), ibm-fpgen (https://github.com/nigeltao/parse-number-fxx-test-data) and fastrange (https://github.com/lemire/fastrange) +-------------------------------------- + + Copyright 2018 Daniel Lemire + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +License for sse4-strstr (https://github.com/WojciechMula/sse4-strstr) +-------------------------------------- + + Copyright (c) 2008-2016, Wojciech Mula + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for The C++ REST SDK +----------------------------------- + +C++ REST SDK + +The MIT License (MIT) + +Copyright (c) Microsoft Corporation + +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +License notice for MessagePack-CSharp +------------------------------------- + +MessagePack for C# + +MIT License + +Copyright (c) 2017 Yoshifumi Kawai + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +License notice for lz4net +------------------------------------- + +lz4net + +Copyright (c) 2013-2017, Milosz Krajewski + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for Nerdbank.Streams +----------------------------------- + +The MIT License (MIT) + +Copyright (c) Andrew Arnott + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +License notice for RapidJSON +---------------------------- + +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +Licensed under the MIT License (the "License"); you may not use this file except +in compliance with the License. You may obtain a copy of the License at + +http://opensource.org/licenses/MIT + +Unless required by applicable law or agreed to in writing, software distributed +under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. + +License notice for DirectX Math Library +--------------------------------------- + +https://github.com/microsoft/DirectXMath/blob/master/LICENSE + + The MIT License (MIT) + +Copyright (c) 2011-2020 Microsoft Corp + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be included in all copies +or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF +CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +License notice for ldap4net +--------------------------- + +The MIT License (MIT) + +Copyright (c) 2018 Alexander Chermyanin + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +License notice for vectorized sorting code +------------------------------------------ + +MIT License + +Copyright (c) 2020 Dan Shechter + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +License notice for musl +----------------------- + +musl as a whole is licensed under the following standard MIT license: + +Copyright © 2005-2020 Rich Felker, et al. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +License notice for "Faster Unsigned Division by Constants" +------------------------------ + +Reference implementations of computing and using the "magic number" approach to dividing +by constants, including codegen instructions. The unsigned division incorporates the +"round down" optimization per ridiculous_fish. + +This is free and unencumbered software. Any copyright is dedicated to the Public Domain. + + +License notice for mimalloc +----------------------------------- + +MIT License + +Copyright (c) 2019 Microsoft Corporation, Daan Leijen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +License for remote stack unwind (https://github.com/llvm/llvm-project/blob/main/lldb/source/Symbol/CompactUnwindInfo.cpp) +-------------------------------------- + +Copyright 2019 LLVM Project + +Licensed under the Apache License, Version 2.0 (the "License") with LLVM Exceptions; +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +https://llvm.org/LICENSE.txt + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +License notice for Apple header files +------------------------------------- + +Copyright (c) 1980, 1986, 1993 + The Regents of the University of California. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed by the University of + California, Berkeley and its contributors. +4. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +License notice for JavaScript queues +------------------------------------- + +CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. + +Statement of Purpose +The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). +Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. +For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: +the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; +moral rights retained by the original author(s) and/or performer(s); +publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; +rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; +rights protecting the extraction, dissemination, use and reuse of data in a Work; +database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and +other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. +2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. +3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. +4. Limitations and Disclaimers. +a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. +b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. +c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. +d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. + + +License notice for FastFloat algorithm +------------------------------------- +MIT License +Copyright (c) 2021 csFastFloat authors +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +License notice for MsQuic +-------------------------------------- + +Copyright (c) Microsoft Corporation. +Licensed under the MIT License. + +Available at +https://github.com/microsoft/msquic/blob/main/LICENSE + +License notice for m-ou-se/floatconv +------------------------------- + +Copyright (c) 2020 Mara Bos +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License notice for code from The Practice of Programming +------------------------------- + +Copyright (C) 1999 Lucent Technologies + +Excerpted from 'The Practice of Programming +by Brian W. Kernighan and Rob Pike + +You may use this code for any purpose, as long as you leave the copyright notice and book citation attached. + +Notice for Euclidean Affine Functions and Applications to Calendar +Algorithms +------------------------------- + +Aspects of Date/Time processing based on algorithm described in "Euclidean Affine Functions and Applications to Calendar +Algorithms", Cassio Neri and Lorenz Schneider. https://arxiv.org/pdf/2102.06959.pdf + +License notice for amd/aocl-libm-ose +------------------------------- + +Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +License notice for fmtlib/fmt +------------------------------- + +Formatting library for C++ + +Copyright (c) 2012 - present, Victor Zverovich + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +License for Jb Evain +--------------------- + +Copyright (c) 2006 Jb Evain (jbevain@gmail.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +--- Optional exception to the license --- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into a machine-executable object form of such +source code, you may redistribute such embedded portions in such object form +without including the above copyright and permission notices. + + +License for MurmurHash3 +-------------------------------------- + +https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp + +MurmurHash3 was written by Austin Appleby, and is placed in the public +domain. The author hereby disclaims copyright to this source + +License for Fast CRC Computation +-------------------------------------- + +https://github.com/intel/isa-l/blob/33a2d9484595c2d6516c920ce39a694c144ddf69/crc/crc32_ieee_by4.asm +https://github.com/intel/isa-l/blob/33a2d9484595c2d6516c920ce39a694c144ddf69/crc/crc64_ecma_norm_by8.asm + +Copyright(c) 2011-2015 Intel Corporation All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Intel Corporation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License for C# Implementation of Fast CRC Computation +----------------------------------------------------- + +https://github.com/SixLabors/ImageSharp/blob/f4f689ce67ecbcc35cebddba5aacb603e6d1068a/src/ImageSharp/Formats/Png/Zlib/Crc32.cs + +Copyright (c) Six Labors. +Licensed under the Apache License, Version 2.0. + +Available at +https://github.com/SixLabors/ImageSharp/blob/f4f689ce67ecbcc35cebddba5aacb603e6d1068a/LICENSE + +License for the Teddy multi-substring searching implementation +-------------------------------------- + +https://github.com/BurntSushi/aho-corasick + +The MIT License (MIT) + +Copyright (c) 2015 Andrew Gallant + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +License notice for Avx512Vbmi base64 encoding / decoding +-------------------------------------------------------- + +Copyright (c) 2015-2018, Wojciech Muła +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +-------------------------------------------------------- + +Aspects of base64 encoding / decoding are based on algorithm described in "Base64 encoding and decoding at almost the speed of a memory +copy", Wojciech Muła and Daniel Lemire. https://arxiv.org/pdf/1910.05109.pdf + +License for FormatJS Intl.Segmenter grapheme segmentation algorithm +-------------------------------------------------------------------------- +Available at https://github.com/formatjs/formatjs/blob/58d6a7b398d776ca3d2726d72ae1573b65cc3bef/packages/intl-segmenter/LICENSE.md + +MIT License + +Copyright (c) 2022 FormatJS + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +License for SharpFuzz and related samples +-------------------------------------- + +https://github.com/Metalnem/sharpfuzz +https://github.com/Metalnem/dotnet-fuzzers +https://github.com/Metalnem/libfuzzer-dotnet + +MIT License + +Copyright (c) 2018 Nemanja Mijailovic + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +License for National Institute of Standards and Technology ACVP Data +-------------------------------------------------------------------- +Available at https://github.com/usnistgov/ACVP-Server/blob/85f8742965b2691862079172982683757d8d91db/README.md#License + +NIST-developed software is provided by NIST as a public service. You may use, copy, and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify, and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software. + +NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. + +You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States. + diff --git a/licenses/SQLitePCLRaw-LICENSE.TXT b/licenses/SQLitePCLRaw-LICENSE.TXT new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/licenses/SQLitePCLRaw-LICENSE.TXT @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/licenses/Spectre.Console-LICENSE.md b/licenses/Spectre.Console-LICENSE.md new file mode 100644 index 0000000..a8373c4 --- /dev/null +++ b/licenses/Spectre.Console-LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Patrik Svensson, Phil Scott, Nils Andresen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/licenses/YamlDotNet-LICENSE.txt b/licenses/YamlDotNet-LICENSE.txt new file mode 100644 index 0000000..d4f2924 --- /dev/null +++ b/licenses/YamlDotNet-LICENSE.txt @@ -0,0 +1,19 @@ +Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Antoine Aubry and contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/licenses/dotnet-LICENSE.TXT b/licenses/dotnet-LICENSE.TXT new file mode 100644 index 0000000..a616ed1 --- /dev/null +++ b/licenses/dotnet-LICENSE.TXT @@ -0,0 +1,23 @@ +The MIT License (MIT) + +Copyright (c) .NET Foundation and Contributors + +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/release-version.txt b/release-version.txt new file mode 100644 index 0000000..6e8bf73 --- /dev/null +++ b/release-version.txt @@ -0,0 +1 @@ +0.1.0 diff --git a/scripts/package_smoke.py b/scripts/package_smoke.py new file mode 100644 index 0000000..9e36603 --- /dev/null +++ b/scripts/package_smoke.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +"""Install the actual nupkg into a clean local tool directory and test portable startup.""" +import argparse +import os +from pathlib import Path +import subprocess +import tempfile +import xml.etree.ElementTree as ET +import zipfile + + +def run(args, cwd, success=True): + result = subprocess.run(list(map(str, args)), cwd=cwd, capture_output=True, text=True, timeout=120) + if success and result.returncode != 0: + raise RuntimeError(result.stdout + result.stderr) + if not success and result.returncode == 0: + raise AssertionError("Command unexpectedly succeeded: " + str(args)) + return result.stdout + result.stderr + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("package", type=Path) + args = parser.parse_args() + package = args.package.resolve() + with zipfile.ZipFile(package) as archive: + names = archive.namelist() + for expected in ["README.md", "LICENSE", "icon.png", "THIRD-PARTY-NOTICES.md", + "tools/net10.0/any/config.default.yaml", "tools/net10.0/any/Helpers/pip_runner.py", + "tools/net10.0/any/MQ.DB.dll"]: + assert expected in names, f"Missing packaged asset: {expected}" + for native in ["libe_sqlite3.so", "e_sqlite3.dll", "libduckdb.so", "duckdb.dll"]: + assert any(n.endswith('/'+native) for n in names), f"Missing native asset: {native}" + assert any(n.startswith("licenses/") for n in names), "Missing third-party license texts" + root = ET.fromstring(archive.read("MagicQuant.nuspec")) + ns = {"n": root.tag.split("}")[0][1:]} + meta = root.find("n:metadata", ns) + version = meta.find("n:version", ns).text + assert meta.find("n:license", ns).text == "AGPL-3.0-only" + with tempfile.TemporaryDirectory(prefix="magicquant package smoke ") as temp: + root = Path(temp) + feed = root / "feed" + feed.mkdir() + import shutil + shutil.copy2(package, feed / package.name) + config = root / "nuget.config" + config.write_text('') + tool = root / "tool" + run(["dotnet", "tool", "install", "MagicQuant", "--tool-path", tool, "--version", version, + "--configfile", config], root) + command = tool / ("magicquant.exe" if os.name == "nt" else "magicquant") + work = root / "unrelated working directory" + work.mkdir() + assert run([command, "--version"], work).strip().split("+")[0] == version + for sub in [[], ["pipeline"], ["init-config"], ["initialize-llama-cpp"], ["clone-repository-quants"]]: + run([command, *sub, "--help"], work) + assert list(work.iterdir()) == [], "Help created runtime files" + run([command, "init-config", "--output", "campaign with spaces.yaml"], work) + campaign = work / "campaign with spaces.yaml" + original = campaign.read_bytes() + assert b"scratch_roots:" in original and b"custom_repositories:" in original + run([command, "init-config", "--output", campaign], work, success=False) + assert campaign.read_bytes() == original, "init-config overwrote user settings" + # Fake input metadata is enough for read-only path checks, not conversion. + model = work / "model with spaces" + model.mkdir() + (model / "config.json").write_text('{}') + (model / "model.safetensors").touch() + import json + preflight = work / "preflight.yaml" + preflight.write_text('paths:\n model_dir: ' + json.dumps(str(model)) + '\n magic_quant_root: ' + + json.dumps(str(work / 'runtime')) + '\nidentity:\n architecture_family_name: package-test\n') + run([command, "pipeline", "--config", preflight, "--check-config", "--strict-config"], work) + assert not (work / "runtime").exists() and not (model / "MagicQuant").exists() + assert "not found" in run([command, "pipeline", "--config", "missing.yaml"], work, success=False) + print(f"Installed package {version}: assets, native libraries, help, config generation, and path preflight passed.") + + +if __name__ == "__main__": + main() diff --git a/scripts/release_version.py b/scripts/release_version.py new file mode 100644 index 0000000..de11825 --- /dev/null +++ b/scripts/release_version.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +"""Reserve a release version with an immutable remote Git tag. + +Tag creation is the atomic claim: concurrent releases retry collisions, while +reruns of the same commit reuse its reservation even if publication failed. +""" +import argparse +import os +from pathlib import Path +import re +import subprocess + +PATTERN = re.compile(r"v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)\Z") + + +def parse_version(value): + match = PATTERN.fullmatch("v" + value) + if not match: + raise ValueError("Release version must be a stable major.minor.patch value.") + return tuple(map(int, match.groups())) + + +def select_version(tags, current_tags, minimum): + existing = [t for t in current_tags if PATTERN.fullmatch(t)] + if len(existing) > 1: + raise ValueError("Commit has multiple release tags; resolve the ambiguity manually.") + if existing: + return existing[0][1:] + versions = [parse_version(t[1:]) for t in tags if PATTERN.fullmatch(t)] + version = parse_version(minimum) + if versions: + major, minor, patch = max(versions) + version = max(version, (major, minor, patch + 1)) + return ".".join(map(str, version)) + + +def git(*args, check=True): + return subprocess.run(["git", *args], check=check, capture_output=True, text=True) + + +def reserve(minimum): + head = git("rev-parse", "HEAD").stdout.strip() + for _ in range(20): + git("fetch", "origin", "--tags") + tags = git("tag", "--list").stdout.splitlines() + current = git("tag", "--points-at", head).stdout.splitlines() + version = select_version(tags, current, minimum) + tag = "v" + version + if tag in current: + # A local tag alone is not a successful remote reservation. + remote = git("ls-remote", "origin", "refs/tags/" + tag).stdout.strip() + if remote: + return version + else: + git("tag", "-a", tag, "-m", f"MagicQuant {version}; reserved for {head}", head) + pushed = git("push", "origin", "refs/tags/" + tag, check=False) + if pushed.returncode == 0: + return version + git("tag", "-d", tag) + # Only a genuine tag race is retryable; auth/network failures must fail. + if not git("ls-remote", "origin", "refs/tags/" + tag).stdout.strip(): + raise RuntimeError(pushed.stderr) + raise RuntimeError("Could not reserve a release version after 20 concurrent tag collisions.") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--reserve", action="store_true", help="Create and push an immutable version tag") + args = parser.parse_args() + minimum = Path("release-version.txt").read_text().strip() + parse_version(minimum) + if args.reserve: + version = reserve(minimum) + else: + version = select_version(git("tag", "--list").stdout.splitlines(), + git("tag", "--points-at", "HEAD").stdout.splitlines(), minimum) + print(version) + if os.environ.get("GITHUB_OUTPUT"): + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: + output.write(f"version={version}\n") + + +if __name__ == "__main__": + main() diff --git a/scripts/scan_secrets.py b/scripts/scan_secrets.py new file mode 100644 index 0000000..cf12023 --- /dev/null +++ b/scripts/scan_secrets.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +"""Run a checksum-pinned Gitleaks build on Git history and the working tree (Linux x64).""" +import hashlib +from pathlib import Path +import subprocess +import tarfile +import tempfile +import urllib.request + +VERSION = '8.30.1' +SHA256 = '551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb' + + +def main(): + with tempfile.TemporaryDirectory(prefix='mq-secret-scan-') as temp: + root = Path(temp) + archive = root / 'gitleaks.tar.gz' + url = f'https://github.com/gitleaks/gitleaks/releases/download/v{VERSION}/gitleaks_{VERSION}_linux_x64.tar.gz' + urllib.request.urlretrieve(url, archive) + if hashlib.sha256(archive.read_bytes()).hexdigest() != SHA256: + raise RuntimeError('Gitleaks checksum mismatch') + binary = root / 'gitleaks' + with tarfile.open(archive) as tar: + binary.write_bytes(tar.extractfile('gitleaks').read()) + binary.chmod(0o700) + results = [] + for mode, extra in [('git', ['--log-opts=--all']), ('dir', [])]: + results.append(subprocess.run([str(binary), mode, '.', *extra, '--redact', '--config', '.gitleaks.toml']).returncode) + if any(results): + raise SystemExit(1) + + +if __name__ == '__main__': + main() diff --git a/scripts/test_doc_links.py b/scripts/test_doc_links.py new file mode 100644 index 0000000..be886b5 --- /dev/null +++ b/scripts/test_doc_links.py @@ -0,0 +1,32 @@ +"""Protect active documentation links; archival documents retain their historical context.""" +from pathlib import Path +import re +import unittest +from urllib.parse import unquote + +ROOT = Path(__file__).resolve().parent.parent + + +class DocumentationTests(unittest.TestCase): + def test_local_documentation_targets_exist(self): + files = [ROOT / name for name in ['README.md', 'CONTRIBUTING.md', 'THIRD-PARTY-NOTICES.md']] + files += list((ROOT / 'docs').rglob('*.md')) + list((ROOT / 'wiki').rglob('*.md')) + errors = [] + for path in files: + for match in re.finditer(r'\]\(([^)]+)\)', path.read_text(encoding='utf-8')): + url = match.group(1).split(' "')[0].strip('<>') + if url.startswith(('https:', 'http:', 'mailto:', '#')): + continue + target = unquote(url.split('#')[0]) + if target and not (path.parent / target).exists(): + errors.append(f'{path.relative_to(ROOT)}: {url}') + self.assertEqual([], errors) + + def test_current_docs_use_canonical_repository_url(self): + files = [ROOT / 'README.md', *list((ROOT / 'docs').rglob('*.md')), *list((ROOT / 'wiki').rglob('*.md'))] + for path in files: + self.assertNotIn('github.com/magiccodingman/magicquant-wiki', path.read_text(encoding='utf-8').lower(), str(path)) + + +if __name__ == '__main__': + unittest.main() diff --git a/scripts/test_release_version.py b/scripts/test_release_version.py new file mode 100644 index 0000000..2c122cc --- /dev/null +++ b/scripts/test_release_version.py @@ -0,0 +1,60 @@ +import importlib.util +from pathlib import Path +import subprocess +import tempfile +import unittest + +spec = importlib.util.spec_from_file_location("release_version", Path(__file__).with_name("release_version.py")) +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) + + +class ReleaseVersionTests(unittest.TestCase): + def test_first_release(self): + self.assertEqual("0.1.0", module.select_version([], [], "0.1.0")) + + def test_patch_uses_numeric_order(self): + self.assertEqual("0.1.11", module.select_version(["v0.1.9", "v0.1.10", "research-v2"], [], "0.1.0")) + + def test_retry_reuses_original_version(self): + self.assertEqual("0.1.2", module.select_version(["v0.1.2", "v0.1.3"], ["v0.1.2"], "1.0.0")) + + def test_minor_and_major_floor(self): + for floor in ["0.2.0", "1.0.0"]: + self.assertEqual(floor, module.select_version(["v0.1.9"], [], floor)) + + def test_invalid_versions_and_ambiguous_tags_fail(self): + for value in ["1.0", "1.0.0-rc.1", "01.2.3", "1.0.0\nmalicious"]: + with self.assertRaises(ValueError): + module.parse_version(value) + with self.assertRaises(ValueError): + module.select_version([], ["v0.1.0", "v0.1.1"], "0.1.0") + + def test_real_remote_reservation_and_retry(self): + with tempfile.TemporaryDirectory() as temp: + root = Path(temp) + def git(*args): + subprocess.run(["git", *args], cwd=root, check=True, capture_output=True) + git("init", "--bare", "remote.git") + git("clone", "remote.git", "checkout") + checkout = root / "checkout" + def local(*args): + subprocess.run(["git", *args], cwd=checkout, check=True, capture_output=True) + local("config", "user.email", "test@example.invalid") + local("config", "user.name", "Test") + (checkout / "release-version.txt").write_text("0.1.0\n") + local("add", ".") + local("commit", "-m", "first") + script = str(Path(__file__).with_name("release_version.py").resolve()) + def reserve(): + import sys + return subprocess.check_output([sys.executable, script, "--reserve"], cwd=checkout, text=True).strip() + self.assertEqual("0.1.0", reserve()) + self.assertEqual("0.1.0", reserve()) + local("commit", "--allow-empty", "-m", "second") + self.assertEqual("0.1.1", reserve()) + self.assertEqual("0.1.1", reserve()) + + +if __name__ == "__main__": + unittest.main() diff --git a/src/MagicQuant/Commands/CommandCatalog.cs b/src/MagicQuant/Commands/CommandCatalog.cs index d4f3e81..24abee9 100644 --- a/src/MagicQuant/Commands/CommandCatalog.cs +++ b/src/MagicQuant/Commands/CommandCatalog.cs @@ -9,6 +9,7 @@ public static bool IsHelp(string argument) => public static Dictionary Factory)> Create() => new(StringComparer.OrdinalIgnoreCase) { + ["init-config"] = ("Create an editable config from the packaged profile", () => new InitConfig()), ["pipeline"] = ("Learn baselines, discover hybrids, validate and export survivors", () => new QuantizationPipeline()), ["evolution"] = ("Compatibility alias for pipeline", () => new QuantizationPipeline()), ["validate-predictions"] = ("Compare KLD predictions with existing SQLite benchmarks", () => new ValidatePredictions()), diff --git a/src/MagicQuant/Commands/InitConfig.cs b/src/MagicQuant/Commands/InitConfig.cs new file mode 100644 index 0000000..d9ad85b --- /dev/null +++ b/src/MagicQuant/Commands/InitConfig.cs @@ -0,0 +1,26 @@ +using MagicQuant.Models; + +namespace MagicQuant.Commands; + +/// Copies the packaged profile to a user-owned file without initializing runtime state. +public sealed class InitConfig : ICommand +{ + public async Task Run(List args) + { + if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) + { + Console.WriteLine("Usage: magicquant init-config [--output config.yaml]"); + Console.WriteLine("Copy the complete bundled profile. Existing files are never overwritten."); + return; + } + if (args.Any(a => !string.Equals(a.Name, "output", StringComparison.OrdinalIgnoreCase)) || args.Count > 1) + throw new ArgumentException("init-config accepts only one --output path."); + if (args.Count == 1 && string.IsNullOrWhiteSpace(args[0].Value)) + throw new ArgumentException("--output requires a file path."); + string destination = Path.GetFullPath(args.Count == 0 ? "config.yaml" : args[0].Value!); + using var source = File.OpenRead(Path.Combine(AppContext.BaseDirectory, "config.default.yaml")); + using var target = new FileStream(destination, FileMode.CreateNew, FileAccess.Write); + await source.CopyToAsync(target); + Console.WriteLine($"Created {destination}. Edit model, architecture, output and scratch settings before running."); + } +} diff --git a/src/MagicQuant/Helpers/CliHelpers.cs b/src/MagicQuant/Helpers/CliHelpers.cs index d8756b3..68fa3d7 100644 --- a/src/MagicQuant/Helpers/CliHelpers.cs +++ b/src/MagicQuant/Helpers/CliHelpers.cs @@ -156,7 +156,7 @@ public static void ShowHelp(Dictionary [blue][[--option value]][/]"); + AnsiConsole.MarkupLine("Usage: [bold]magicquant[/] [blue][[--option value]][/]"); AnsiConsole.MarkupLine("Config: [green]--config[/] [grey][/] (CLI flags override YAML)"); AnsiConsole.MarkupLine("Identity: [green]--architecture-family[/] [grey][/] | [green]--allow-architecture-family-alias-override[/]"); AnsiConsole.WriteLine(); diff --git a/src/MagicQuant/MagicQuant.csproj b/src/MagicQuant/MagicQuant.csproj index a109893..3a5621d 100644 --- a/src/MagicQuant/MagicQuant.csproj +++ b/src/MagicQuant/MagicQuant.csproj @@ -1,7 +1,21 @@ - + Exe + true + magicquant + MagicQuant + 0.1.0 + MagicCodingMan + Benchmark-driven GGUF quantization and mixed-precision tensor-group hybrid discovery for llama.cpp. + GGUF;quantization;llama.cpp;LLM;mixed-precision;benchmark + https://github.com/magiccodingman/MagicQuant + https://github.com/magiccodingman/MagicQuant + git + true + AGPL-3.0-only + README.md + icon.png net10.0 enable enable @@ -19,9 +33,11 @@ PreserveNewest + PreserveNewest Always + Always @@ -29,4 +45,11 @@ + + + + + + + diff --git a/src/MagicQuant/Program.cs b/src/MagicQuant/Program.cs index e99e1fa..1c3480e 100644 --- a/src/MagicQuant/Program.cs +++ b/src/MagicQuant/Program.cs @@ -7,6 +7,13 @@ using MQ.DB.Models; using Spectre.Console; +if (args.Length == 1 && args[0] == "--version") +{ + Console.WriteLine(typeof(CommandCatalog).Assembly.GetCustomAttributes(typeof(System.Reflection.AssemblyInformationalVersionAttribute), false) + .Cast().Single().InformationalVersion); + return; +} + var commands = CommandCatalog.Create(); if (args.Length == 0 || CommandCatalog.IsHelp(args[0])) @@ -51,6 +58,12 @@ return; } + if (commandInput.Equals("init-config", StringComparison.OrdinalIgnoreCase)) + { + await commandInfo.Factory().Run(parsedArgs); + return; + } + CliOptionValidator.Validate(parsedArgs); var loaded = MagicQuantYamlLoader.Read(parsedArgs); CommandPreflight.Validate(commandInput, loaded.Settings, parsedArgs); diff --git a/src/MagicQuant/Services/ReadmeGenerationService.cs b/src/MagicQuant/Services/ReadmeGenerationService.cs index 19fe600..4bf11da 100644 --- a/src/MagicQuant/Services/ReadmeGenerationService.cs +++ b/src/MagicQuant/Services/ReadmeGenerationService.cs @@ -156,7 +156,7 @@ private async Task GenerateCoreAsync( sb.AppendLine($"# MagicQuant Hybrids (v2.0) - {resolvedModelName}"); sb.AppendLine(); sb.AppendLine( - "MagicQuant is a benchmark driven GGUF hybrid discovery and validation system focused on finding real, practical GGUF quants specific to each architecture."); + "[MagicQuant](https://github.com/magiccodingman/MagicQuant) is a benchmark driven GGUF hybrid discovery and validation system focused on finding real, practical GGUF quants specific to each architecture."); sb.AppendLine(); sb.AppendLine( "Whether it's a pure baseline model built by llama.cpp, learned tensor configurations from Unsloth, or a custom built MagicQuant hybrid, the model table below shows quants that have won dominance checks, survived collapse spaces, and/or were found to be nonlinearly better. Instead of dumping every quant type possible, MagicQuant tests, validates, and brutally murders anything deemed unworthy."); diff --git a/tests/MagicQuant.Tests/CliStartupTests.cs b/tests/MagicQuant.Tests/CliStartupTests.cs index fab8788..fccbb4a 100644 --- a/tests/MagicQuant.Tests/CliStartupTests.cs +++ b/tests/MagicQuant.Tests/CliStartupTests.cs @@ -12,6 +12,7 @@ public sealed class CliStartupTests [InlineData("help")] [InlineData("--help")] [InlineData("-h")] + [InlineData("init-config")] [InlineData("pipeline")] [InlineData("evolution")] [InlineData("build-hybrids")] @@ -81,6 +82,24 @@ public async Task Check_config_does_not_initialize_or_clean_runtime_state() Assert.Single(result.CreatedFiles); } + [Fact] + public async Task Init_config_copies_the_packaged_profile_without_runtime_setup() + { + var result = await RunAsync(["init-config", "--output", "my config.yaml"]); + Assert.Equal(0, result.ExitCode); + Assert.Single(result.CreatedFiles); + Assert.Contains("Created", result.Output); + } + + [Fact] + public async Task Init_config_refuses_to_overwrite_an_existing_file() + { + var result = await RunAsync(["init-config", "--output", "existing.yaml"], directory => + File.WriteAllText(Path.Combine(directory, "existing.yaml"), "user-owned")); + Assert.Equal(1, result.ExitCode); + Assert.Single(result.CreatedFiles); + } + private static async Task<(int ExitCode, string Output, string[] CreatedFiles)> RunAsync(string[] args, Action? setup = null) { string directory = Path.Combine(Path.GetTempPath(), $"mq-cli-{Guid.NewGuid():N}"); diff --git a/tests/MagicQuant.Tests/ConfigurationContractTests.cs b/tests/MagicQuant.Tests/ConfigurationContractTests.cs index 88fa5cc..7b84c2b 100644 --- a/tests/MagicQuant.Tests/ConfigurationContractTests.cs +++ b/tests/MagicQuant.Tests/ConfigurationContractTests.cs @@ -35,6 +35,26 @@ public void Distributed_configs_have_no_unknown_keys(string relativePath) Assert.NotNull(config.Paths); Assert.NotNull(config.CandidateSelection); } + [Theory] + [InlineData("README.md")] + [InlineData("docs/best-practices.md")] + public void Onboarding_yaml_examples_match_the_configuration_contract(string relativePath) + { + string root = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../..")); + string markdown = File.ReadAllText(Path.Combine(root, relativePath)); + var snippets = System.Text.RegularExpressions.Regex.Matches(markdown, @"```yaml\r?\n(.*?)```", + System.Text.RegularExpressions.RegexOptions.Singleline); + Assert.NotEmpty(snippets); + foreach (System.Text.RegularExpressions.Match snippet in snippets) + { + string yaml = snippet.Groups[1].Value; + Assert.Empty(YamlConfigurationDiagnostics.Inspect(yaml)); + var config = new DeserializerBuilder().WithNamingConvention(UnderscoredNamingConvention.Instance) + .Build().Deserialize(yaml); + ConfigurationShapeValidator.Validate(config); + } + } + [Fact] public void Legacy_inactive_yaml_remains_compatible_with_current_selection_settings() { diff --git a/tests/MagicQuant.Tests/ReadmeGenerationTests.cs b/tests/MagicQuant.Tests/ReadmeGenerationTests.cs new file mode 100644 index 0000000..8100c2f --- /dev/null +++ b/tests/MagicQuant.Tests/ReadmeGenerationTests.cs @@ -0,0 +1,29 @@ +using MagicQuant.Services; +using Xunit; + +namespace MagicQuant.Tests; + +public sealed class ReadmeGenerationTests +{ + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Exported_readmes_link_to_the_canonical_project(bool clone) + { + string root = Path.Combine(Path.GetTempPath(), "mq-readme-" + Guid.NewGuid().ToString("N")); + try + { + var service = new ReadmeGenerationService(); + string file = clone + ? await service.GenerateCloneAsync(root, "test", "owner/model", true, []) + : await service.GenerateAsync(root, "test", [], []); + string readme = await File.ReadAllTextAsync(file); + Assert.Contains("[MagicQuant](https://github.com/magiccodingman/MagicQuant)", readme); + Assert.DoesNotContain("magicquant-wiki", readme, StringComparison.OrdinalIgnoreCase); + } + finally + { + if (Directory.Exists(root)) Directory.Delete(root, recursive: true); + } + } +} diff --git a/wiki/index.md b/wiki/index.md index 04fb1a6..9da77c4 100644 --- a/wiki/index.md +++ b/wiki/index.md @@ -1,5 +1,7 @@ # MagicQuant v2 Documentation +For installation and CLI usage see the [project README](../README.md) and [program docs](../docs/index.md). The [research overview](overview.md) retains worked examples and motivation. + MagicQuant is a benchmark-driven GGUF evaluation and hybrid-discovery system. These pages explain not only what the pipeline does, but why its search, measurement, and survivor rules exist. ## Start Here diff --git a/wiki/overview.md b/wiki/overview.md new file mode 100644 index 0000000..ed99f61 --- /dev/null +++ b/wiki/overview.md @@ -0,0 +1,222 @@ +# MagicQuant (v2.0) + +**MagicQuant is a benchmark-driven GGUF evaluation and hybrid-discovery system.** + +> **Which quantized models are actually worth using at each size?** + +Most quant releases give you a pile of files, AKA: Q8, Q6, Q5, Q4, and leave you to guess. MagicQuant replaces that guesswork with benchmarks, tensor-group probing, mixed hybrid GGUF builds when they are worth it, and a final survivor list built around meaningful size/fidelity tradeoffs. + +--- + +## What MagicQuant Does + +MagicQuant takes the messy quantization space and turns it into a judged survivor list. + +It tests standard baselines, learns from external quant strategies, and builds mixed tensor-group hybrids when there may be a better size/fidelity trade hiding between normal quant levels. + +Then it validates the results. + +MagicQuant does not assume hybrids are better. It does not assume baselines are safe. Every option has to earn its slot. + +A final MagicQuant release is meant to show: + +* what is smallest +* what is safest +* what is meaningfully in-between +* what was removed as redundant or not worth the damage +* and what the real benchmark numbers say + +If a model survives MagicQuant, it survived because the trade was worth showing. + +--- + +## Example + +The following example is Qwen3-4B-2507-Instruct going through MagicQuants pipeline and the final results: + +| Name | Provider | Quant Family | KLD | Size (GB) | +| ----------------------------------------------------------------------------------------- | ---------- | ------------ | -------: | --------: | +| LM-Q8_0 | llama.cpp | Q8_0 | 0.001339 | 3.99 | +| MQ-Q6_K_1 | MagicQuant | Q6_K | 0.001817 | 3.58 | +| UD-Q6_K_XL | Unsloth | UD-Q6_K_XL | 0.002111 | 3.41 | +| LM-Q6_K | llama.cpp | Q6_K | 0.004640 | 3.08 | +| [MQ-Q5_K_1](#winner-notes "Replaced: MQ-Q5_K") | MagicQuant | Q5_K | 0.006632 | 2.88 | +| [UD-Q5_K_XL](#winner-notes "Replaced: LM-Q5_K, LM-Q5_K_S") | Unsloth | UD-Q5_K_XL | 0.009839 | 2.73 | +| [MQ-Q4_K_M_1](#winner-notes "Replaced: MQ-Q4_K_M, UD-Q4_K_XL, LM-Q4_K_M + 1 more") | MagicQuant | Q4_K_M | 0.020346 | 2.44 | +| [LM-Q4_K_S](#winner-notes "Replaced: LM-IQ4_NL") | llama.cpp | Q4_K_S | 0.029803 | 2.22 | +| LM-IQ4_XS | llama.cpp | IQ4_XS | 0.031300 | 2.11 | +| UD-Q3_K_XL | Unsloth | UD-Q3_K_XL | 0.072278 | 1.98 | + +The table above includes a mix of standard llama.cpp quantizations, Unsloth Dynamic GGUF models, and MagicQuant hybrids. + +In some cases, dominance is absolute. For example, Unsloth’s **Q5_K_XL** fully replaces the standard llama.cpp **Q5_K**, as MagicQuant determined the baseline offered no meaningful tradeoff in comparison. + +More interesting are the hybrid outcomes. **MQ-Q4_K_M_1** emerged as a clear dominant variant, replacing multiple candidates simultaneously (_UD-Q4_K_XL, MQ-Q4_K_M, LM-Q4_K_M_). While baseline quants can sometimes achieve similar dominance, this case highlights a hybrid configuration that decisively outperformed across the board. + +**MQ-Q5_K_1** is another notable result. It leverages Unsloth’s learned tensor behavior (_Q5_K_XL_) within the `ffn_up_gate`, discovering a middle ground between **UD-Q5_K_XL** and **LM-Q6_K**. The result is a hybrid that achieves a disproportionately large KLD improvement relative to the additional size cost, exceeding a simple linear tradeoff. + +The table below breaks down these MagicQuant hybrids by tensor group, showing the assigned quantization for each, whether derived from llama.cpp baselines or Unsloth’s learned tensor mappings. + +| Name | embeddings | attn_q | attn_kv | attn_output | ffn_up_gate | ffn_down | +| ----------- | ---------- | ------ | ------- | ----------- | ----------- | -------- | +| MQ-Q6_K_1 | Q8_0 | Q8_0 | Q8_0 | Q8_0 | Q6_K | Q8_0 | +| MQ-Q5_K_1 | Q8_0 | Q5_K | Q8_0 | Q6_K | UD-Q5_K_XL | Q5_K_S | +| MQ-Q4_K_M_1 | Q8_0 | Q5_K | Q8_0 | Q6_K | IQ4_XS | IQ4_XS | + +--- + +## Nonlinear Wins + +MagicQuant does not look for simple "winners" in sub space between baselines. Instead it only allows nonlinear trade wins. Documentation presented later goes further into detail on this subject, but here's the TLDR: + +Imagine a graph like this: +``` +Size → +| +| Q6 +| / +| / +| Q5 +| / +|Q4 ++---------------- +``` + +A nonlinear win looks like: +``` + Q6 + / + / ← MQ-Q5_K_1 (above the line) + Q5 + / +Q4 +``` + +That hybrid sits above the straight line between Q4 and Q5. + +Meaning: +👉 It’s a **more efficient trade** than the normal step-up + +This is what MagicQuant calls a "nonlinear trade/win" when such wordage is used. + +--- + +## Deeper Understanding + +For a deeper dive into MagicQuant and how it works, the [wiki index](https://github.com/magiccodingman/MagicQuant/blob/main/wiki/index.md) is a good place to start. + +When you see a MagicQuant hybrid, it’s not just a “Q4.5” sitting somewhere between Q4 and Q5. It represents a discovered configuration where the **KLD reduction is non-linear relative to the size increase**, a genuinely better trade space. Not universally “better” than everything else, but a variant that earned its place through measurable advantage. + +Whether the winner is a hybrid or a pure baseline from llama.cpp or Unsloth, any quant that removes another from the final selection does so because its dominance made the alternative no longer worth considering. + +The goal is not to flood the space with near-duplicates offering negligible KLD gains for minimal size differences, nor to claim superiority for the sake of it. In fact, that’s explicitly what MagicQuant avoids. + +MagicQuant is built around transparency, honesty, maintainability, and most importantly trust. As it evaluates new architectures and quant families, it doesn’t invent quantization schemes in isolation. Instead, it learns from proven tensor assignments provided by trusted sources like llama.cpp and Unsloth. If those baselines are stable, MagicQuant operates within that same safe space, extending rather than reinventing. + +Historical sources expand that tensor vocabulary; they do not vote on the current winner. MagicQuant pins the source revision, rebuilds the available recipes under current controlled conditions, and relearns their effects rather than replaying an old final mixture. + +That said, the system is designed to adapt. Edge cases can exist, but the architecture is intentionally flexible to handle them. + +### How MagicQuant Works + +``` + ┌────────────────────────────┐ + │ Input Quantized Models │ + │ ───────────────────────── │ + │ llama.cpp / Unsloth / etc │ + └────────────┬──────────────┘ + │ + │ Inspect tensors + ▼ + ┌────────────────────────────┐ + │ Tensor Extraction Layer │ + │ ───────────────────────── │ + │ - Read all tensors │ + │ - Detect quant types │ + │ - Capture F32 / BF16 │ + └────────────┬──────────────┘ + │ + │ Group by role + ▼ + ┌────────────────────────────┐ + │ Tensor Group Mapping │ + │ ───────────────────────── │ + │ embeddings │ + │ attn_q / attn_kv / output │ + │ ffn_up_gate / ffn_down │ + │ lm_head / moe_* │ + └────────────┬──────────────┘ + │ + │ Learn configs + ▼ + ┌────────────────────────────┐ + │ Learned Config Library │ + │ ───────────────────────── │ + │ "Q5_K attn_q pattern" │ + │ "UD-Q5_K_XL ffn pattern" │ + │ etc │ + └────────────┬──────────────┘ + │ + │ Normalize external configs + ▼ + ┌────────────────────────────┐ + │ Controlled Rebuild Layer │ + │ ───────────────────────── │ + │ - Apply configs to BF16 │ + │ - Use MagicQuant imatrix │ + │ - Equal comparison ground │ + └────────────┬──────────────┘ + │ + │ Feed into + ▼ + ┌────────────────────────────┐ + │ Hybrid Construction Engine │ + │ ───────────────────────── │ + │ Mix tensor groups across │ + │ learned configurations │ + └────────────┬──────────────┘ + │ + │ Evaluate candidates + ▼ + ┌────────────────────────────┐ + │ Prediction + Isolation │ + │ ───────────────────────── │ + │ - Group-level testing │ + │ - Rank-safe prediction │ + │ - Controlled context tests │ + └────────────┬──────────────┘ + │ + │ Build real GGUF + ▼ + ┌────────────────────────────┐ + │ Benchmark Layer │ + │ ───────────────────────── │ + │ - KLD (primary) │ + │ - PPL (secondary) │ + │ - Measured GPU scheduling │ + └────────────┬──────────────┘ + │ + │ Final decision + ▼ + ┌────────────────────────────┐ + │ Survivor Selection │ + │ ───────────────────────── │ + │ - Dominance pruning │ + │ - Nonlinear winners │ + │ - Spacing collapse │ + └────────────────────────────┘ +``` + +The controlled context tests check whether a promising group choice still behaves the same way when the surrounding model moves from a Q4-or-better regime into more aggressive compression. They are bounded and evidence-driven because exhaustive context testing would recreate the full combinatorial problem. + +GPU scheduling is also measured rather than assumed. A large benchmark can use multiple GPUs in one shared process, while batches of smaller candidates can run concurrently on independent GPUs when that produces higher aggregate throughput. + +The final release is a curated survivor menu. Research campaigns and cross-run audits should preserve the full nondominated evidence frontier before applying spacing, so that a presentation decision does not erase valid results. + +## Deep Dive Documentation + +- [Wiki index](./index.md) +- [Prediction Engine](./docs/Prediction-Engine.md) +- [Regime-Aware Tensor Search](./docs/Regime-Aware-Search.md) +- [GPU Benchmark Scheduling](./docs/GPU-Benchmark-Scheduling.md) +- [Pareto Archives and Reproducibility](./docs/Pareto-Archives-And-Reproducibility.md) From 3b068a35059114c0f6df900993dec92412d27c19 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 20:14:27 -0400 Subject: [PATCH 255/258] Validate config creation syntax and finalize workflow checks --- .github/actionlint.yaml | 3 +++ src/MagicQuant/Commands/InitConfig.cs | 10 ++++++++++ src/MagicQuant/Program.cs | 1 + src/MagicQuant/Services/ReadmeGenerationService.cs | 2 +- tests/MagicQuant.Tests/CliStartupTests.cs | 8 ++++++++ wiki/overview.md | 2 ++ 6 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 .github/actionlint.yaml diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml new file mode 100644 index 0000000..3006923 --- /dev/null +++ b/.github/actionlint.yaml @@ -0,0 +1,3 @@ +self-hosted-runner: + labels: + - magicquant-smoke diff --git a/src/MagicQuant/Commands/InitConfig.cs b/src/MagicQuant/Commands/InitConfig.cs index d9ad85b..a3e4e65 100644 --- a/src/MagicQuant/Commands/InitConfig.cs +++ b/src/MagicQuant/Commands/InitConfig.cs @@ -5,6 +5,16 @@ namespace MagicQuant.Commands; /// Copies the packaged profile to a user-owned file without initializing runtime state. public sealed class InitConfig : ICommand { + public static void ValidateTokens(string[] tokens) + { + bool valid = tokens.Length == 0 + || (tokens.Length == 2 && tokens[0].Equals("--output", StringComparison.OrdinalIgnoreCase) + && !string.IsNullOrWhiteSpace(tokens[1]) && !tokens[1].StartsWith("--", StringComparison.Ordinal)) + || (tokens.Length == 1 && tokens[0].StartsWith("--output=", StringComparison.OrdinalIgnoreCase) + && !string.IsNullOrWhiteSpace(tokens[0][9..])); + if (!valid) throw new ArgumentException("Usage: magicquant init-config [--output config.yaml]"); + } + public async Task Run(List args) { if (args.Any(a => string.Equals(a.Name, "help", StringComparison.OrdinalIgnoreCase))) diff --git a/src/MagicQuant/Program.cs b/src/MagicQuant/Program.cs index 1c3480e..9ca83b5 100644 --- a/src/MagicQuant/Program.cs +++ b/src/MagicQuant/Program.cs @@ -60,6 +60,7 @@ if (commandInput.Equals("init-config", StringComparison.OrdinalIgnoreCase)) { + InitConfig.ValidateTokens(args.Skip(1).ToArray()); await commandInfo.Factory().Run(parsedArgs); return; } diff --git a/src/MagicQuant/Services/ReadmeGenerationService.cs b/src/MagicQuant/Services/ReadmeGenerationService.cs index 4bf11da..733a902 100644 --- a/src/MagicQuant/Services/ReadmeGenerationService.cs +++ b/src/MagicQuant/Services/ReadmeGenerationService.cs @@ -153,7 +153,7 @@ private async Task GenerateCoreAsync( AppendHuggingFaceFrontmatter(sb); string resolvedModelName = ResolveReadmeTitleModelName(modelName); - sb.AppendLine($"# MagicQuant Hybrids (v2.0) - {resolvedModelName}"); + sb.AppendLine($"# MagicQuant Hybrids - {resolvedModelName}"); sb.AppendLine(); sb.AppendLine( "[MagicQuant](https://github.com/magiccodingman/MagicQuant) is a benchmark driven GGUF hybrid discovery and validation system focused on finding real, practical GGUF quants specific to each architecture."); diff --git a/tests/MagicQuant.Tests/CliStartupTests.cs b/tests/MagicQuant.Tests/CliStartupTests.cs index fccbb4a..28093e2 100644 --- a/tests/MagicQuant.Tests/CliStartupTests.cs +++ b/tests/MagicQuant.Tests/CliStartupTests.cs @@ -100,6 +100,14 @@ public async Task Init_config_refuses_to_overwrite_an_existing_file() Assert.Single(result.CreatedFiles); } + [Fact] + public async Task Init_config_rejects_a_positional_filename_without_writing_a_default() + { + var result = await RunAsync(["init-config", "unexpected.yaml"]); + Assert.Equal(1, result.ExitCode); + Assert.Empty(result.CreatedFiles); + } + private static async Task<(int ExitCode, string Output, string[] CreatedFiles)> RunAsync(string[] args, Action? setup = null) { string directory = Path.Combine(Path.GetTempPath(), $"mq-cli-{Guid.NewGuid():N}"); diff --git a/wiki/overview.md b/wiki/overview.md index ed99f61..ed33a4a 100644 --- a/wiki/overview.md +++ b/wiki/overview.md @@ -47,6 +47,8 @@ The following example is Qwen3-4B-2507-Instruct going through MagicQuants pipeli | LM-IQ4_XS | llama.cpp | IQ4_XS | 0.031300 | 2.11 | | UD-Q3_K_XL | Unsloth | UD-Q3_K_XL | 0.072278 | 1.98 | +## Winner notes + The table above includes a mix of standard llama.cpp quantizations, Unsloth Dynamic GGUF models, and MagicQuant hybrids. In some cases, dominance is absolute. For example, Unsloth’s **Q5_K_XL** fully replaces the standard llama.cpp **Q5_K**, as MagicQuant determined the baseline offered no meaningful tradeoff in comparison. From 2323abac25830078d60fbaf721b7409373bb0bd2 Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 20:15:42 -0400 Subject: [PATCH 256/258] Capture stalled-test diagnostics and bound CI test hangs --- .github/workflows/dotnet.yml | 4 ++-- .github/workflows/publish-nuget.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 065449b..9889be7 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -25,14 +25,14 @@ jobs: - run: python -m unittest discover -s scripts -p "test_*.py" - run: dotnet restore MagicQuant.sln --locked-mode -warnaserror - run: dotnet build MagicQuant.sln --configuration ${{ matrix.configuration }} --no-restore -warnaserror - - run: dotnet test MagicQuant.sln --configuration ${{ matrix.configuration }} --no-build --logger trx --results-directory TestResults + - run: dotnet test MagicQuant.sln --configuration ${{ matrix.configuration }} --no-build --blame-hang-timeout 2m --blame-hang-dump-type none --logger trx --logger "console;verbosity=normal" --results-directory TestResults - run: dotnet pack src/MagicQuant --configuration ${{ matrix.configuration }} --no-restore -p:Version=0.0.0-ci -o artifacts -warnaserror - run: python scripts/package_smoke.py artifacts/MagicQuant.0.0.0-ci.nupkg - uses: actions/upload-artifact@v4 if: always() with: name: test-results-${{ matrix.os }}-${{ matrix.configuration }} - path: TestResults/*.trx + path: TestResults/ secrets: name: Secret scan runs-on: ubuntu-latest diff --git a/.github/workflows/publish-nuget.yml b/.github/workflows/publish-nuget.yml index 8317b0c..74d242f 100644 --- a/.github/workflows/publish-nuget.yml +++ b/.github/workflows/publish-nuget.yml @@ -24,7 +24,7 @@ jobs: python-version: '3.12' - run: dotnet restore MagicQuant.sln --locked-mode -warnaserror - run: dotnet build MagicQuant.sln -c Release --no-restore -warnaserror - - run: dotnet test MagicQuant.sln -c Release --no-build + - run: dotnet test MagicQuant.sln -c Release --no-build --blame-hang-timeout 2m --blame-hang-dump-type none - run: python -m unittest discover -s scripts -p "test_*.py" - run: dotnet pack src/MagicQuant -c Release --no-restore -p:Version=0.0.0-ci -o artifacts -warnaserror - run: python scripts/package_smoke.py artifacts/MagicQuant.0.0.0-ci.nupkg From 9ab287fb5640b54ca987353a4feba0ed4537669c Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 20:25:35 -0400 Subject: [PATCH 257/258] Explain upstream attribution, variant cloning and tensor-copying limits --- README.md | 2 ++ docs/best-practices.md | 30 +++++++++++++++++++ docs/commands.md | 2 ++ examples/clone.yaml | 3 ++ .../Services/ReadmeGenerationService.cs | 2 ++ src/MagicQuant/config.default.yaml | 5 ++++ 6 files changed, 44 insertions(+) diff --git a/README.md b/README.md index 214e216..303aeea 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ External providers are optional. MagicQuant can run using its local baseline cho **Unsloth is the maintainer's recommended starting point** for external GGUF baselines. MagicQuant can learn their tensor-group patterns, rebuild a controlled equivalent from your local source model, and benchmark it in your campaign. It does not simply trust an external file's label or score. Choose the exact matching model and revision, and review its license. See the [Unsloth configuration walkthrough](docs/best-practices.md#optional-unsloth-baselines) and [research explanation](wiki/docs/Learning-From-Existing-Quantizations.md). +For the same model, prefer linking to the original provider's surviving baselines. For a compatible variant they do not host, cloning can rebuild the full selected set locally. Learning tensor assignments does not automatically reproduce a provider's other processing techniques. See [publishing and cloning guidance](docs/best-practices.md#link-upstream-for-the-same-model-build-locally-for-variants). + ## Documentation | Start here | What you will find | diff --git a/docs/best-practices.md b/docs/best-practices.md index 1d6f534..deb8858 100644 --- a/docs/best-practices.md +++ b/docs/best-practices.md @@ -47,6 +47,36 @@ MagicQuant resolves the specified files, validates tensor-name parity, learns as For a provider-free campaign leave `baselines.custom_repositories` empty. See [learning from existing quantizations](../wiki/docs/Learning-From-Existing-Quantizations.md) for the research rationale. +## Link upstream for the same model; build locally for variants + +For a release of the **same source model** that an external provider such as Unsloth already hosts, the maintainer recommends leaving this pipeline setting off: + +```yaml +output: + export_external_learned_baselines: false +``` + +MagicQuant will link external pure-baseline survivors to the provider instead of exporting local copies. This gives the original creator credit and downloads, avoids unnecessary duplicate hosting, and is the friendly default. MagicQuant's comparisons measure locally reconstructed tensor configurations under its own conditions. They do not, by themselves, establish whether the provider's original artifact is better or worse. Finding a useful hybrid or size/fidelity trade is not a reason to claim superiority over an untested upstream release. + +For a **different model variant**, such as an uncensored model or another fine-tune, the upstream repository may not host those weights. In that case, build the full selected set locally, including both MagicQuant hybrids and external-derived baseline configurations. If running the pipeline on that variant, enable local external-baseline exports: + +```yaml +output: + export_external_learned_baselines: true +``` + +The equivalent pipeline switch is `--export-external-learned-baselines`. Retain provider attribution and the applicable licenses even when rebuilding from different weights. + +**Clone command distinction:** `clone-repository-quants` already rebuilds every artifact entry in its input clone manifest, including external-derived entries; it does not consult this pipeline export flag. You do not need to enable the flag for that command. A source release can leave external export off and still include those configurations in its clone manifest. Clone mode rebuilds the entries present in that manifest, not every quantization ever offered by the provider. + +Cloning is a practical way to reuse a strong set of tensor configurations on a compatible variant without repeating full discovery. In the maintainer's experience, repeating discovery for modest fine-tunes can cost substantial time for little additional improvement. That is a starting assumption, not a guarantee: larger weight changes can shift the useful tradeoffs. Clone mode benchmarks the rebuilt artifacts locally, but does not repeat the full search or prove that inherited choices are optimal. Run discovery again when the model changes substantially, the measurements look poor, or you need stronger evidence for the target model. See the [clone command](commands.md#clone-known-tensor-configurations). + +## Limits of tensor-configuration copying + +MagicQuant learns quantization assignments for tensors and tensor groups, then rebuilds using the local source weights and its supported toolchain. **It does not automatically reproduce every technique used to create an external artifact.** A provider's extra weight transformations, custom quantization procedures, calibration recipes, or other processing are not reproduced merely because their tensor configuration was learned. Such behavior must be explicitly supported to be reproduced. + +Treat external configurations as evidence about useful assignments, not as a byte-for-byte clone of the provider's GGUF or a replication of its entire production process. Keep this distinction clear in release descriptions and benchmark claims. See the [research guide](../wiki/docs/Learning-From-Existing-Quantizations.md). + ## Retain enough evidence to reproduce a result Pin the MagicQuant package version and provider/model revisions. Keep the YAML, imatrix/evaluation data identity, llama.cpp revision, hardware context, and local `Runs/*/run.json` records. Provenance captures available versions and settings; it is not a complete frozen environment or numerical reproducibility guarantee. Remove private paths or credentials before sharing logs. diff --git a/docs/commands.md b/docs/commands.md index ba5e337..22dd852 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -34,6 +34,8 @@ magicquant clone-repository-quants \ Use `--source-repo owner/repo` instead to read a Hugging Face repository. `--source-json` also accepts an HTTP(S) URL. Clone mode rebuilds the tensor configurations and benchmarks them locally; it does not establish that the new model passed full discovery. +Clone mode rebuilds all entries present in the manifest, including configurations originally learned from external providers. It does not use `output.export_external_learned_baselines`, which controls the pipeline's choice between local exports and upstream links. This is useful when cloning to a fine-tuned or uncensored variant that the original provider does not host. Review the [upstream-link versus variant-export guidance](best-practices.md#link-upstream-for-the-same-model-build-locally-for-variants) and [limits of tensor-configuration copying](best-practices.md#limits-of-tensor-configuration-copying). + By default the manifest must match the target tensor inventory. `--allow-missing-manifest-tensors` explicitly allows a strict subset; unmatched target tensors use base quantization. `--missing-manifest-base-quant Q8_0` additionally selects that base quant. Use these only when that compatibility tradeoff is intended. ## Validate predictions against existing measurements diff --git a/examples/clone.yaml b/examples/clone.yaml index 42d4c3e..29482f7 100644 --- a/examples/clone.yaml +++ b/examples/clone.yaml @@ -1,4 +1,7 @@ # Supply --source-json or --source-repo on the command line. +# Clone rebuilds all manifest entries, including external-derived baselines. +# output.export_external_learned_baselines is a pipeline-only export choice; +# it is not needed to include those entries when cloning to a model variant. paths: model_dir: /data/models/my-compatible-model identity: diff --git a/src/MagicQuant/Services/ReadmeGenerationService.cs b/src/MagicQuant/Services/ReadmeGenerationService.cs index 733a902..5f2874b 100644 --- a/src/MagicQuant/Services/ReadmeGenerationService.cs +++ b/src/MagicQuant/Services/ReadmeGenerationService.cs @@ -192,6 +192,8 @@ private async Task GenerateCoreAsync( sb.AppendLine(); sb.AppendLine("External/custom baselines are normalized into MagicQuant's controlled comparison flow. MagicQuant rebuilds a learned baseline under native-source / MagicQuant-controlled conditions, including its own imatrix handling, so hybrids or external baselines (like Unsloth) can be judged on a more equal footing. That does **not** mean MagicQuant proved the original upstream artifact or upstream imatrix was worse. These comparisons exist for internal hybrid-search consistency and equal playing field comparisons, not as a universal judgment of the original creator's exact release artifact."); sb.AppendLine(); + sb.AppendLine("MagicQuant learns tensor quantization assignments and rebuilds from local source weights. It does not automatically reproduce a provider's additional weight transformations, calibration recipes, custom processing, or other techniques unless explicitly supported. These results are not a byte-for-byte reproduction or a test of the provider's original GGUF."); + sb.AppendLine(); sb.AppendLine("**Easier to digest explanation:**"); sb.AppendLine(); sb.AppendLine("MagicQuant compares and benchmarks the models quant to tensor configurations, but not the original artifact. And there's different reasons MagicQuant chooses to lift up a winning quant, not all winners are purely \"better\". It depends heavily on a variety of factors. Though choices are always documented in the repo under the manifest folder. You can always view what and why decisions were made by the automated system."); diff --git a/src/MagicQuant/config.default.yaml b/src/MagicQuant/config.default.yaml index 56acafd..edb42bc 100644 --- a/src/MagicQuant/config.default.yaml +++ b/src/MagicQuant/config.default.yaml @@ -346,6 +346,11 @@ output: # Set true if you explicitly want MagicQuant to rebuild/export those external learned # baselines locally under MagicQuant-controlled conditions (for example when testing a # modified model where the upstream artifact does not really exist for your case). + # Recommended: leave off when the original provider hosts the same model; + # link to their release and credit their work. Enable for pipeline runs on + # variants they do not host. clone-repository-quants already rebuilds every + # manifest entry locally and does not use this pipeline export setting. + # Learning tensor assignments does not reproduce other provider techniques. export_external_learned_baselines: false # false = normal behavior; delete/rebuild final outputs from scratch. From bf55142d8dd1c0a5b694648cfd2aae87adf1042c Mon Sep 17 00:00:00 2001 From: MagicCodingMan Date: Mon, 7 Sep 2026 20:28:45 -0400 Subject: [PATCH 258/258] Make starter settings model-neutral and external presets opt-in --- docs/best-practices.md | 2 +- docs/configuration.md | 8 ++ examples/pipeline-external.yaml | 40 +++++++ scripts/package_smoke.py | 6 +- .../Configuration/MagicQuantYamlConfig.cs | 3 +- src/MagicQuant/config.default.yaml | 100 ++---------------- .../ConfigurationContractTests.cs | 27 +++++ 7 files changed, 92 insertions(+), 94 deletions(-) create mode 100644 examples/pipeline-external.yaml diff --git a/docs/best-practices.md b/docs/best-practices.md index deb8858..a43d666 100644 --- a/docs/best-practices.md +++ b/docs/best-practices.md @@ -21,7 +21,7 @@ Use existing writable parent locations dedicated to this work. Allow space for s The maintainer recommends Unsloth as a primary place to look for external GGUF tensor assignments. These sources are optional, and their value depends on model compatibility and measured results. Start with a repository for the exact source model; a similar name or matching architecture alone is insufficient. -In the generated configuration, edit `baselines.custom_repositories`. The following is a structural example, not a promise that a particular upstream file exists. Replace the model/file placeholders, pin `revision` to the provider commit you inspected, and retain the rest of your campaign configuration: +In the generated configuration, edit `baselines.custom_repositories`. A fuller, explicitly opt-in template is available in [examples/pipeline-external.yaml](../examples/pipeline-external.yaml), including how to add an external baseline to confirmed-anomaly expansion. The following is a structural example, not a promise that a particular upstream file exists. Replace the model/file placeholders, pin `revision` to the provider commit you inspected, and retain the rest of your campaign configuration: ```yaml baselines: diff --git a/docs/configuration.md b/docs/configuration.md index c7e8e3d..d80611e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -12,6 +12,14 @@ Unknown CLI options, duplicate options, missing values, and values supplied to p Unknown or inactive YAML keys produce a warning with their setting path and line number; `--strict-config` rejects them. Compare with the commented default file and `src/MagicQuant/Configuration/MagicQuantYamlConfig.cs`. CI strictly parses the distributed examples so their keys cannot silently drift. +## Model-neutral startup + +The bundled profile selects no model, architecture, provider repository, imatrix source, GPU limit, or scratch disk. `init-config` produces that same profile. Set model/architecture paths and review output/storage before your first campaign; a blank model is intentionally rejected by preflight. + +Standard llama.cpp families and general research thresholds remain populated so the file describes a usable starting policy. These values are not a Qwen preset or a claim that every model shares an optimum. The Qwen/other-family patterns in `src/MQ.DB/tensor_groups.yaml` are model-compatibility rules, not a selected campaign; do not erase them when configuring a different model. + +Confirmed-anomaly expansion defaults to built-in Q6_K/Q5_K candidates. Add exact external baseline names only after configuring that source. The [external-provider template](../examples/pipeline-external.yaml) shows this opt-in; it contains placeholders and must be edited before use. Existing explicit local campaign configs are not rewritten. + ## Main sections | Section | Responsibility | diff --git a/examples/pipeline-external.yaml b/examples/pipeline-external.yaml new file mode 100644 index 0000000..81c3b16 --- /dev/null +++ b/examples/pipeline-external.yaml @@ -0,0 +1,40 @@ +# Optional provider template, NOT a ready-to-run model preset. +# Replace ALL placeholders and confirm the exact model/revision/file match. +# For the bundled research tuning profile, first run magicquant init-config, +# then copy the baselines/anomaly sections below into your generated config. +# Loading this file directly uses typed defaults for omitted sections. +paths: + model_dir: /data/models/YOUR-SOURCE-MODEL +identity: + architecture_family_name: YOUR-MODEL-FAMILY +output: + output_dir: /data/exports/YOUR-MODEL-MagicQuant + output_name_prefix: YOUR-MODEL + # Prefer upstream links when the provider hosts the same model. + export_external_learned_baselines: false +baselines: + standard_baselines_mode: all + custom_repositories: + - repo_id: unsloth/YOUR-EXACT-MODEL-GGUF + revision: PROVIDER_COMMIT_SHA + enabled: true + short_source_name: UD + source_kind: huggingface_gguf_repository + require_all_includes_to_resolve: true + validate_tensor_names_against_source_model: true + delete_partial_or_dirty_downloads: true + resume_or_retry_downloads: true + includes: + - file_name: YOUR-EXACT-MODEL-UD-Q6_K_XL.gguf + baseline_family: Q6_K + quantize_base_name: Q6_K + display_name: UD_Q6_K_XL + allow_as_learning_baseline: true + allow_as_combination_carrier: true + allow_as_explicit_group_candidate: true +# Optional: include the configured external baseline in the small expansion pass +# around confirmed anomalies. This is not required just to learn from a provider. +anomaly_detection: + confirmed_anomaly_expansion: + allowed_reference_quants: [Q8_0] + allowed_candidate_quants: [Q6_K, Q5_K, UD_Q6_K_XL] diff --git a/scripts/package_smoke.py b/scripts/package_smoke.py index 9e36603..fcca021 100644 --- a/scripts/package_smoke.py +++ b/scripts/package_smoke.py @@ -9,8 +9,8 @@ import zipfile -def run(args, cwd, success=True): - result = subprocess.run(list(map(str, args)), cwd=cwd, capture_output=True, text=True, timeout=120) +def run(args, cwd, success=True, env=None): + result = subprocess.run(list(map(str, args)), cwd=cwd, capture_output=True, text=True, timeout=120, env=env) if success and result.returncode != 0: raise RuntimeError(result.stdout + result.stderr) if not success and result.returncode == 0: @@ -47,7 +47,7 @@ def main(): config.write_text('') tool = root / "tool" run(["dotnet", "tool", "install", "MagicQuant", "--tool-path", tool, "--version", version, - "--configfile", config], root) + "--configfile", config], root, env={**os.environ, "NUGET_PACKAGES": str(root / "packages")}) command = tool / ("magicquant.exe" if os.name == "nt" else "magicquant") work = root / "unrelated working directory" work.mkdir() diff --git a/src/MagicQuant/Configuration/MagicQuantYamlConfig.cs b/src/MagicQuant/Configuration/MagicQuantYamlConfig.cs index 4457b5c..a8c93fd 100644 --- a/src/MagicQuant/Configuration/MagicQuantYamlConfig.cs +++ b/src/MagicQuant/Configuration/MagicQuantYamlConfig.cs @@ -262,7 +262,8 @@ public sealed class RuntimeConfirmedAnomalyExpansionConfig public int MaxNeighborsPerConfirmedRule { get; set; } = 6; public int MaxTotalExpansionProbes { get; set; } = 12; public List AllowedReferenceQuants { get; set; } = ["Q8_0"]; - public List AllowedCandidateQuants { get; set; } = ["Q6_K", "UD-Q6_K_XL", "Q5_K", "UD-Q5_K_XL"]; + // External display names are campaign-specific; opt in after configuring that source. + public List AllowedCandidateQuants { get; set; } = ["Q6_K", "Q5_K"]; } diff --git a/src/MagicQuant/config.default.yaml b/src/MagicQuant/config.default.yaml index edb42bc..b0809f5 100644 --- a/src/MagicQuant/config.default.yaml +++ b/src/MagicQuant/config.default.yaml @@ -1,8 +1,10 @@ # ============================================================ -# MagicQuant - Default Production Configuration +# MagicQuant - Model-neutral Starter Configuration # ============================================================ # -# This file is intended to be the safe baseline config for production use. +# This is a model-neutral starting profile, not a tuned preset for any model. +# Set the model path and architecture identity deliberately before running. +# Research thresholds are starting policies, not universal quality guarantees. # # General rules: # - CLI flags override values from this YAML. @@ -103,7 +105,7 @@ learning: readme: # Optional title model name override used in: - # # MagicQuant Hybrids (v2.0) - + # # MagicQuant Hybrids - # If blank, MagicQuant uses identity.architecture_family_name. title_model_name_override: @@ -325,11 +327,11 @@ anomaly_detection: max_total_expansion_probes: 12 allowed_reference_quants: - Q8_0 + # Built-in families only. Add exact custom baseline names explicitly after + # configuring a compatible provider; see examples/pipeline-external.yaml. allowed_candidate_quants: - Q6_K - - UD-Q6_K_XL - Q5_K - - UD-Q5_K_XL output: # Optional explicit output directory. @@ -395,90 +397,10 @@ baselines: # enabled_standard_explicit_group_candidates: [Q8_0, Q6_K, Q5_K, Q4_K_M, IQ4_NL, IQ4_XS] enabled_standard_explicit_group_candidates: [] - custom_repositories: - # ======================================================== - # Example custom repository entry - # ======================================================== - # - # This is for external/custom GGUF baselines such as Unsloth. - # - # Flow: - # 1. MagicQuant resolves included files from the repo - # 2. downloads the external GGUF - # 3. validates tensor-name parity against the local source model - # 4. learns tensor behavior from that external GGUF - # 5. rebuilds a MagicQuant-controlled equivalent from the local source model - # 6. benchmarks the rebuilt version instead of trusting the original external artifact - # - # Important: - # - "includes" must be plural - # - "baseline_family" is the internal MagicQuant family being attached to - # - "quantize_base_name" is the quant/base family name used in rebuild logic - # - # - repo_id: unsloth/Qwen3-4B-Instruct-2507-GGUF - # # Optional branch, tag, or commit. Pin this when a provider rotates files - # # so tensor digestion and later downloads remain reproducible. - # revision: - # enabled: true - # short_source_name: UD - # source_kind: huggingface_gguf_repository - # - # # Repository-level defaults: - # allow_as_learning_baseline: true - # allow_as_combination_carrier: false - # allow_as_explicit_group_candidate: false - # - # # Validation / download behavior: - # require_all_includes_to_resolve: true - # validate_tensor_names_against_source_model: true - # delete_partial_or_dirty_downloads: true - # resume_or_retry_downloads: true - # - # includes: - # - file_name: Qwen3-4B-Instruct-2507-UD-Q4_K_XL.gguf - # baseline_family: Q4_K_M - # quantize_base_name: Q4_K_M - # display_name: UD_Q4_K_XL - # # Transient command only. Do not store as DB truth. - # # Deletes/relearns only this custom baseline under the active architecture family + tensor group profile. - # force_relearn: false - # allow_as_learning_baseline: true - # allow_as_combination_carrier: true - # allow_as_explicit_group_candidate: true - # requires_imatrix: false - # banned_group_ids: [] - # - # - file_name: Qwen3-4B-Instruct-2507-UD-Q5_K_XL.gguf - # baseline_family: Q5_K - # quantize_base_name: Q5_K - # display_name: UD_Q5_K_XL - # force_relearn: false - # allow_as_learning_baseline: true - # allow_as_combination_carrier: true - # allow_as_explicit_group_candidate: true - # - # - file_name: Qwen3-4B-Instruct-2507-UD-Q6_K_XL.gguf - # baseline_family: Q6_K - # quantize_base_name: Q6_K - # display_name: UD_Q6_K_XL - # force_relearn: false - # allow_as_learning_baseline: true - # allow_as_combination_carrier: true - # allow_as_explicit_group_candidate: true - # - # - file_name: Qwen3-4B-Instruct-2507-UD-Q3_K_XL.gguf - # baseline_family: IQ3_S - # quantize_base_name: IQ3_S - # display_name: UD_Q3_K_XL - # force_relearn: false - # allow_as_learning_baseline: true - # allow_as_combination_carrier: false - # allow_as_explicit_group_candidate: true - # - # # Example note: - # # If the repo does not actually contain IQ3_XS, do not reference it. - # # Use only filenames that truly exist in the repository. - [] + # Optional external tensor-configuration sources. No provider/model is selected. + # See examples/pipeline-external.yaml and docs/best-practices.md for a template. + # Use the exact source model and provider filenames; pin the provider revision. + custom_repositories: [] # Counterfactual synergy templates generalize confirmed contextual anomaly evidence. # anomaly_detection remains the low-level compatibility section; synergy_detection controls diff --git a/tests/MagicQuant.Tests/ConfigurationContractTests.cs b/tests/MagicQuant.Tests/ConfigurationContractTests.cs index 7b84c2b..f76ce9d 100644 --- a/tests/MagicQuant.Tests/ConfigurationContractTests.cs +++ b/tests/MagicQuant.Tests/ConfigurationContractTests.cs @@ -25,6 +25,7 @@ public void Explicit_config_path_is_relative_to_working_directory() [InlineData("src/MagicQuant/config.default.yaml")] [InlineData("examples/pipeline.yaml")] [InlineData("examples/clone.yaml")] + [InlineData("examples/pipeline-external.yaml")] public void Distributed_configs_have_no_unknown_keys(string relativePath) { string root = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../..")); @@ -55,6 +56,32 @@ public void Onboarding_yaml_examples_match_the_configuration_contract(string rel } } + [Fact] + public void Starter_and_typed_defaults_do_not_select_a_model_or_external_provider() + { + var yaml = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "config.default.yaml")); + var starter = new DeserializerBuilder().WithNamingConvention(UnderscoredNamingConvention.Instance) + .Build().Deserialize(yaml); + Assert.DoesNotContain("Qwen", yaml, StringComparison.OrdinalIgnoreCase); + foreach (var config in new[] { starter, MagicQuantYamlConfig.CreateDefault() }) + { + Assert.True(string.IsNullOrWhiteSpace(config.Paths.ModelDir)); + Assert.True(string.IsNullOrWhiteSpace(config.Paths.MagicQuantRoot)); + Assert.True(string.IsNullOrWhiteSpace(config.Identity.ArchitectureFamilyName)); + Assert.True(string.IsNullOrWhiteSpace(config.Readme.TitleModelNameOverride)); + Assert.True(string.IsNullOrWhiteSpace(config.Imatrix.DatasetRepo)); + Assert.Empty(config.Paths.ScratchRoots); + Assert.Empty(config.Hardware.GpuMemoryLimitsGb); + Assert.Empty(config.Baselines.CustomRepositories); + Assert.False(config.Output.ExportExternalLearnedBaselines); + Assert.False(config.Learning.ForceRelearnArchitectureFamily); + Assert.True(config.Learning.ConfirmTensorGroupProfile); + Assert.Equal(new[] { "Q6_K", "Q5_K" }, config.AnomalyDetection.ConfirmedAnomalyExpansion.AllowedCandidateQuants); + Assert.False(config.Readme.Frontmatter.ContainsKey("license")); + Assert.False(config.Readme.Frontmatter.ContainsKey("base_model")); + } + } + [Fact] public void Legacy_inactive_yaml_remains_compatible_with_current_selection_settings() {